Simple question about TGA's

Started by
1 comment, last by Mastaba 17 years, 4 months ago
Hi, hello again! I have a little problem: I'm trying to save an image in TGA format, here are the functions I'm using:

bool CImage::WriteTGA(char *fileName, int offset)
{	
	CLogger logger("log.txt");	

	FILE* Output = fopen(fileName, "wb");	
		
	if ( !Output )
	{	
		logger.Message("Error - <WriteTGA>: Cannot write to file %s", fileName);
	}

	fseek(Output, offset, SEEK_SET);
			
	unsigned char header[18] = {0};
	header[2] = NO_RLE;	
	header[12] = width;	
	header[13] = width >> 8;
	header[14] = height;
	header[15] = height >> 8;			
	header[16] = components << 3;
														 		
	fwrite(header, 18, 1, Output);

	unsigned char *newData = new unsigned char[width * height * components];
	memcpy(newData, pixelData, width * height * components);
	
	// RGB -> BGR
	int i;
	for ( i = 0; i < width * height; i++)
	{												 
		unsigned char temp = newData;
		newData = pixelData;
		newData = temp;
	}

	fwrite(newData, width * height * components, <span class="cpp-number">1</span>, Output);
		
	fclose(Output);
		
	<span class="cpp-keyword">delete</span>[] newData;
	newData = NULL;

	<span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>;
}

<span class="cpp-keyword">void</span> CImage::SetPixel(<span class="cpp-keyword">int</span> i, <span class="cpp-keyword">int</span> j, <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> r, <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> g, <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> b, <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> a)
{	
	pixelData[i*width*components + j*components + <span class="cpp-number">0</span>] = r;	
	pixelData[i*width*components + j*components + <span class="cpp-number">1</span>] = g;
	pixelData[i*width*components + j*components + <span class="cpp-number">2</span>] = b;
	<span class="cpp-keyword">if</span> ( components == <span class="cpp-number">4</span> )
		pixelData[i*width*components + j*components + <span class="cpp-number">3</span>] = a;
}

tColor4ub CImage::GetPixel(<span class="cpp-keyword">int</span> i, <span class="cpp-keyword">int</span> j)
{	
	tColor4ub color;
	color.r = pixelData[i*width*components + j*components + <span class="cpp-number">0</span>];
	color.g = pixelData[i*width*components + j*components + <span class="cpp-number">1</span>];
	color.b = pixelData[i*width*components + j*components + <span class="cpp-number">2</span>];
	<span class="cpp-keyword">if</span> ( components == <span class="cpp-number">4</span> )
		color.a = pixelData[i*width*components + j*components + <span class="cpp-number">3</span>];
	
	<span class="cpp-keyword">return</span> color;
}

<span class="cpp-keyword">void</span> CImage::CreateEmpty(<span class="cpp-keyword">int</span> width, <span class="cpp-keyword">int</span> height, <span class="cpp-keyword">int</span> components, <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> *pixelData)
{
	<span class="cpp-keyword">this</span>-&gt;width = width;
	<span class="cpp-keyword">this</span>-&gt;height = height;
	<span class="cpp-keyword">this</span>-&gt;components = components;
		
	<span class="cpp-keyword">this</span>-&gt;pixelData = <span class="cpp-keyword">new</span> <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span>[width * height * components];
	memset(<span class="cpp-keyword">this</span>-&gt;pixelData, <span class="cpp-number">0</span>, width * height * components);
	<span class="cpp-keyword">if</span> (pixelData)
		memcpy(<span class="cpp-keyword">this</span>-&gt;pixelData, pixelData, width * height * components);
}

</pre></div><!–ENDSCRIPT–>

Okay I'm trying to create now, using the functions above an image that will have the top row all white. Instead of that I get an image that has the bottom wor all whitr, why? here is the code that implements this
<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
CImage img;
img.CreateEmpty(<span class="cpp-number">64</span>, <span class="cpp-number">64</span>, <span class="cpp-number">3</span>, NULL);
<span class="cpp-keyword">int</span> collon;
<span class="cpp-keyword">for</span> ( collon = <span class="cpp-number">0</span>; collon &lt; <span class="cpp-number">64</span>; ++collon )
	img.SetPixel( <span class="cpp-number">0</span>, collon, <span class="cpp-number">255</span>, <span class="cpp-number">255</span>, <span class="cpp-number">255</span>, <span class="cpp-number">255</span>);
img.WriteTGA(<span class="cpp-literal">"test.tga"</span>, <span class="cpp-number">0</span>);
img.Free();

</pre></div><!–ENDSCRIPT–>

and here is the output:
<a href="http://imageshack.us"><img src="http://img254.imageshack.us/img254/6845/testzg5.png" border="0" alt="Image Hosted by ImageShack.us" /></a>

(The last line is white :) )
Advertisement
because the coord's (x,0) are at the bottom of the image. try filling coords (x,63).....

If the height value is negative, then the image would go the other way (and your code would do as you expect). The only problem with that, is that most people don't support negative widths & heights. So basically, just remember that 0,0 is the bottom left....
The last byte of the header normally sets the origin of the image, be it top left, top right, bottom left or bottom right. See this.
.

This topic is closed to new replies.

Advertisement