Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 13 Mar 2007
Offline Last Active Jun 29 2014 08:36 PM

Topics I've Started

Libtiff Reading BandW image into 2D array

26 June 2014 - 07:33 PM

ultimately i would like to have a 2D array loaded from a black and white tiff file such as

bool image[width][length];


where true is pixel present and false is blank, or vice versa i don't care.

Modify some bits and write it back out to a tiff




I'm using libtiff and C++, i attached a sample tiff


Basically, i followed one of the examples. Just modified it to print the buffer to a output file and it the result is bizzare

This is what the output looks like


   €   €T €   €T €   €   €   €   €   €   €   €   €   €   €   €   €   €   €   €   €   €   €   €   €   €


Looks like its still in some kind of compressed format.


I tried other examples on libtiff site, and some from stackoverflow and haven't got anything really working.


Any help would be very MUCH appreciated!!




#include <stdio.h> 
#include <tiffio.h>
#include <memory>
#include <fstream>
#include <iostream>
#pragma comment(lib,"libtiff.lib")
int main(int argc, char *argv[])
TIFF *image; 
uint16 photo, bps, spp, fillorder;   
uint32 width;   
tsize_t stripSize; 
unsigned long imageOffset, result;   
int stripMax, stripCount;   
char *buffer, tempbyte;  
unsigned long bufferSize, count;  
// Open the TIFF image   
if((image = TIFFOpen("test.tiff", "r")) == NULL)
std::cout << "Could not open incoming image\n" << std::endl;     
return (42);   
int imageWidth, imageLength, tileWidth, tileLength;
TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &imageWidth);
TIFFGetField(image, TIFFTAG_IMAGELENGTH, &imageLength);
TIFFGetField(image, TIFFTAG_TILEWIDTH, &tileWidth);
TIFFGetField(image, TIFFTAG_TILELENGTH, &tileLength);
std::cout << "Image Width: " << imageWidth << std::endl;
std::cout << "Image Height: " << imageLength << std::endl;
std::cout << "Tile Width : " << tileWidth << std::endl;
std::cout << "Tile Height : " << tileLength << std::endl;
// Check that it is of a type that we support   
if((TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bps) == 0) || (bps != 1))
fprintf(stderr, "Either undefined or unsupported number of bits per sample\n");     
if((TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp) == 0) || (spp != 1))
fprintf(stderr, "Either undefined or unsupported number of samples per pixel\n");   
// Read in the possibly multiple strips   
stripSize = TIFFStripSize (image);   
stripMax = TIFFNumberOfStrips (image);   
imageOffset = 0;      
bufferSize = TIFFNumberOfStrips (image) * stripSize;   
if((buffer = (char *) malloc(bufferSize)) == NULL)
fprintf(stderr, "Could not allocate enough memory for the uncompressed image\n");     return(42);  
for (stripCount = 0; stripCount < stripMax; stripCount++)
if((result = TIFFReadEncodedStrip (image, stripCount,           buffer + imageOffset,           stripSize)) == -1)
fprintf(stderr, "Read error on input strip number %d\n", stripCount);       
imageOffset += result;   
std::cout << "strip count: " << stripMax << std::endl;
// Deal with photometric interpretations   
if(TIFFGetField(image, TIFFTAG_PHOTOMETRIC, &photo) == 0)
fprintf(stderr, "Image has an undefined photometric interpretation\n");    
// Flip bits     
printf("Fixing the photometric interpretation\n");  
for(count = 0; count < bufferSize; count++)       
buffer[count] = ~buffer[count];  
// Deal with fillorder   
if(TIFFGetField(image, TIFFTAG_FILLORDER, &fillorder) == 0)
fprintf(stderr, "Image has an undefined fillorder\n");    
// return(47);  
if(fillorder != FILLORDER_MSB2LSB)
{     // We need to swap bits -- ABCDEFGH becomes HGFEDCBA     
printf("Fixing the fillorder\n");  
for(count = 0; count < bufferSize; count++)
tempbyte = 0;       
if(buffer[count] & 128) tempbyte += 1;     
if(buffer[count] & 64) tempbyte += 2;       
if(buffer[count] & 32) tempbyte += 4;   
if(buffer[count] & 16) tempbyte += 8;      
if(buffer[count] & 8) tempbyte += 16;      
if(buffer[count] & 4) tempbyte += 32;       
if(buffer[count] & 2) tempbyte += 64;     
if(buffer[count] & 1) tempbyte += 128;    
buffer[count] = tempbyte;     
// Do whatever it is we do with the buffer -- we dump it in hex  
if(TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width) == 0)
fprintf(stderr, "Image does not define its width\n");     
std::fstream file("output.txt",std::ios_base::out);
for(int i = 0; i < bufferSize; ++ i)
file << buffer[i];
std::cout << bufferSize << std::endl;
return 0;

std::cin question

18 September 2013 - 10:52 PM

I'll give an example because i don't really know how to word it.


cout << "Input a filename :";

cin >> file;


I would like to print

Input a filename :autosave.txt_<cursor here>
autosave.txt can be backspaced on and edited.


I'd like to put some stuff in cins buffer before i ask and have it displayed and be editable. 


i can only think of platform specific ways of achieving something similar.

Simulating key presses or rolling your input function that pops data off on backspace, pushes chars in, and sets the cursor position accordingly.


I know, iostreams let you hack around at a pretty low level with them so i assume this possible some how. Maybe?




Zooming about to a point (2D surface)

24 February 2013 - 04:20 AM




Hello, I need some help with this math. I've been at this for hours now.


I have a large surface (the whole tiger) and a view in blue that is 700,400. Right now, i have it zooming and panning with bounds checking fine, but can't figure out how to zoom to the mouse cursor (red dots).


Say, im at 660, 300 local coordinates of view A and zoom in, how do I get what appears in view B? Currently, it just zooms into 0,0 and would get a close up of the ear instead. Basically, I want the functionality of Paint, or Paint.NET or any image editor.

This is all 2D math btw


    // x, and y are local coordinates
    void Canvas::setScale(float s, int x, int y) 
        // m_x and m_y are are coordinates into the full canvas
        // the view treats them as 0,0
        // in view marked A, these would something like 200,100 and would be at the tip of the ear
        // after this function i want them to point right above the eye like in the view B example

        float oldscale = m_scale;
        m_scale = s;

        // TODO add the math 



Right now, i have some math filled in but it only kinda working.. Its more of a hack and is definately not fluid or percise.
I know there most be a standard way of doing this.

Scaling Sprites trouble

05 June 2012 - 07:20 PM

I'm creating a 2D game with DX9 using sprites and want to make a hit point bar.
I created a image that with no scale would fill the hud's life bar to 100% because the bar is exactly the right size when drawn.

It works fine when the scale is 1.0 but at any other value it is not right.
It scales fine the problem is it is drawn in the wrong position.

instead of the it being drawn at
pos1.x = 12;
pos1.y = 768 - 48 + 24;

it looks like it is being drawn at this position
pos1.x = 6;

And then it over laps part of the HUD graphic.

I don't understand why its moving the sprite to the left.. It gets worse the more scale there is.


pos1.x = 12;

pos1.y = 768 - 48 + 24;

pos1.z = 0;

D3DXMATRIX scale_matrix;

D3DXVECTOR2 scale(0.5f,1.0f); // 50% scale in then x-direciton only

D3DXMATRIX original;




window::sprite_obj->Draw( txt_manager.get_texture("hp"),NULL,NULL,&pos1, D3DCOLOR_XRGB(255,255,255));


do i need to use a translation vector? If so how would i make it right every scale value?
Why is this happening?

Also how can i set the transparency of a sprite at run time before it is drawn?

2D movement trouble

02 June 2011 - 05:49 PM

Hello, im working a game. I've been programming for a long time but this is the first time doing graphics programming.
Im having trouble making the movement of my space invaders look good.

The invaders move downward pretty nicely. But adding any movement along the x-axis is really choppy and poor. The invader will move straight down for 20 pixels smoothly and then just jump right or left; when it should be just a smooth slope.

this is the invaders update function.. It is also responsible for drawing the invader
void invader::update(Timer& time)



       if (_time == 0)

              _time = time.get_ticks();




              _y = _y + ((time.get_ticks() - _time) * .10);

              _x = _x + ((time.get_ticks() - _time) * .05);



              _time = time.get_ticks();

              // Apply texture



This is my game loop.. Its very small as i've dumbed it down .. Only one invader and nothing else.. It still has the same trouble.
[font="Calibri"][size="3"] [/size][/font]

//While the user hasn't quit

while( quit == false )


              //While there's an event to handle

              while( SDL_PollEvent( &event ) )


                     //If the user has Xed out the window

              if( event.type == SDL_QUIT )


                       //Quit the program

                       quit = true;





//Increment the frame counter



              //Apply the background to the screen

              apply_surface( 0, 0, background, screen );




              //Update the screen

              if( SDL_Flip( screen ) == -1 )


                     return 1;    



//If we want to cap the frame rate

if( ( cap == true ) && ( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) )


//Sleep the remaining frame time

SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );






The timer class is lazyfoos it just makes SDL_GetTicks more useful by giving the elapsed ticks not the total.
It uses

Any ideas, thoughts or demo on proper 2D movements with velocitys and timers would be much much appreciated!