Skip to main content
GameDev.net gamedev.net
🔒 Locked

Targa writer problem

Started by Paul7 Apr 28, 2004 at 6:08 AM 4 replies 500+ views
Original Post
Paul7
Paul7
Have got my targa writer working but get strange artefacts in the image now and again. I pass my targa writer function my frame buffer, an array of floats. These are then converted in chars. I feel the problem lies here somewhere. my targa header structure is as follows
struct TGA {
	unsigned char idLength;
	unsigned char colourMapType;
	unsigned char imageType;
	unsigned short colourMapFirstEntryIndex;
	unsigned short colourMapLength;
	unsigned char colourMapEntrySize;
	unsigned short xOrigin;
	unsigned short yOrigin;
	unsigned short width;
	unsigned short height;
	unsigned char pixelDepth;
	unsigned char imageDescriptor;
};
Anyone have any ideas how to resolve this problem? Thanks
superpig
superpig
What''s the image you posted meant to look like?
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
_DarkWIng_
_DarkWIng_
Could be problem with memory aligment. Set aligmet/pack size to 1. If you use MSVC try something like this:

#pragma pack(push)
#pragma pack(1)
struct TGA {
...all header fields...
}
#pragma pack(pop)
You should never let your fears become the boundaries of your dreams.
Tachikoma
Tachikoma
[img]http://www.3dreal.co.uk/project/error.jpg[/img]

That's a screen capture of a wood textured surface, right? Are those green specs the strange artefacts you're talking about? If so, it looks like an unsigned overflow, or singned float conversion problem.

Let's assume you want to convert an array of floats float_buffer[] into and array of bytes byte_buffer[] :


unsigned char byte_buffer[ .... ]; //Make same lenght as float_buffer[]

int Temp = (int)float_buffer[j];
byte_buffer[j] = (unsigned char)((Temp < 0) ? 0 : ((Temp > 255) ? 255 : Temp));


Basically you're converting each float to a signed integer. Then you clamp the colour value between 0 and 255 and type cast the result into an unsigned char. Note, I'm assuming you're dealing with float values 'roughly' in the range of [0.0f, 255.0f].

That said, it's better if you pass an array of integers instead of floats. There are a several reason for this. One, float-to-int conversions are slow, especially on Visual C++, because it uses an internal _ftol() routine for converion. Second, it's best if you do image manipulation using integer instructions (if possible), as they're better suited for such tasks, and again they're faster.




[edited by - Tachikoma on April 28, 2004 11:28:24 AM]
Paul7
Paul7
Thanks for all the replys

Tachikoma your suggestion was what was needed, it works fine now thanks all the green specs have gone!

quote:
by the way, how do crate these nifty code 'boxes' in these forums?



In your message type the following without the ' :

'['source']'
your code
'['/source']'

tells you about it here: http://www.gamedev.net/community/forums/faq.asp near the bottom of the page

[edited by - Paul7 on April 29, 2004 7:40:27 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.