# reading from tga file type 3

This topic is 4597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello, i read data from a type 3 tga file (greyscaled) to get height information for a heightfield. this is the code i use:
unsigned char * data = new unsigned char [x_size*y_size];
if(fread(data, sizeof(unsigned char)*x_size*y_size, 1, fp) == 0) {
//file error handling
}

//fill my heightmap array
for (int x=0;x<MAXSIZE;x+=hf_stepsize) {
for (int y=0;y<MAXSIZE;y+=hf_stepsize) {
P[x][y] = (float) data[i] / (float)255.0f;
i++;
}
}


but somehow i dont get the x and y positions right. here is an example of the original tga file: and after i read it in: whats my error? thanks!

##### Share on other sites
It looks like the image data in the TGA file is 1 pixel wider than the surface you're reading it into (e.g. the width of the surface in memory is 128 and the width of the TGA file is 129).

Check that the image width in the TGA header (field 5.2) is what you expect it to be (could be an error with the program which generated the TGA or even simple user error).

A different possibility could be a problem with the creation of the surface e.g. in DirectX, some graphics cards require the width to be a power of 2, in cases where the surface isn't a power of 2, undefined things can happen.

Finally, if the destination you're copying the image data into is a locked DirectDraw/Direct3D surface, you must copy the texture line by line and take the surface pitch into account or you'll see very similar problems.

##### Share on other sites
Additionally, I also think you might be reading from the wrong offset in the file (remember it can change in TGA files).

The pixel in the top left hand corner seems to indicate that.

Another thing that could be your problem: what is the value of MAXSIZE, it should be the same as the width of your TGA file.

##### Share on other sites
yepp, you were right about the one pixel offset.
i changed the reading code to:

	for (int x=0;x<MAXSIZE-1;x+=hf_stepsize) {		for (int y=0;y<MAXSIZE-1;y+=hf_stepsize) {			P[x][y] = (float) data[i] / (float)255.0f;			i++;		}	}

notice the -1.

now the heightmap shows up correctly, except for the very left and the very bottom row which is zero, since the loop doesnt run to them anymore.

"Additionally, I also think you might be reading from the wrong offset in the file (remember it can change in TGA files). "

it's 18 bytes, afaik. right? what you mean by it can change?

"Another thing that could be your problem: what is the value of MAXSIZE"

(MAXSIZE-1) / hf_size is the same as the tga's width+height. i only use power-of-two and quadratic tgas.

##### Share on other sites

This topic is 4597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628706
• Total Posts
2984309

• 23
• 10
• 9
• 13
• 13