Jump to content
  • Advertisement
Sign in to follow this  
Asheh

TGA File Writing

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi peeps, having some trouble with my targa file writer, the image it creates seems to have problems with offsets im not sure, but the code is here:

	static bool WriteBufferToFile(std::string fn, CBuffer<CColour> &buffer, short w, short h) 
	{
		FILE * file;

		//open up file
		if((file = fopen(fn.c_str(),"w+"))==NULL)
		{
			return false;
		}

		//write out identification number
		char idSize = (char)0;
		fwrite(&idSize, sizeof(unsigned char), 1, file);

		char colorMap = (char)0;
		fwrite(&colorMap, sizeof(unsigned char), 1, file);

		char colorType = (char)2;
		fwrite(&colorType, sizeof(char), 1, file);

		//colour map specification
		short int colourMapEntryOffset = 0;
		short int colourMapLength = 0;
		unsigned char colourMapEntrySize = 0;
		fwrite(&colourMapEntryOffset, sizeof(short int), 1, file);
		fwrite(&colourMapLength, sizeof(short int), 1, file);
		fwrite(&colourMapEntrySize, sizeof(unsigned char), 1, file);

		short int xOrig = 0;
		short int yOrig = 0;
		short int width = w;
		short int height = h;
		unsigned char bitdepth = (char)24;
		unsigned char imageDesc = (char)0;
		
		fwrite(&xOrig,sizeof(short int),1,file);
		fwrite(&yOrig,sizeof(short int),1,file);
		fwrite(&width,sizeof(short int),1,file);
		fwrite(&height,sizeof(short int),1,file);
		fwrite(&bitdepth,sizeof(unsigned char),1,file);

		imageDesc = 0;
		imageDesc = 0x00000000;
		fwrite(&imageDesc,sizeof(char),1,file);
	
		//write data to file
		for(int i = 0; i < h; i++)
		{
			for(int j = 0; j < w; j++)
			{
				CColour pixel = buffer.Get(j,i);
				pixel.Cap();
				char r, g, b;
				r = (char)(pixel.mR * 255);
				g = (char)(pixel.mG * 255);
				b = (char)(pixel.mB * 255);
				fwrite(&b,sizeof(char),1,file);
				fwrite(&g,sizeof(char),1,file);
				fwrite(&r,sizeof(char),1,file);
				
			}
		}
		fclose(file);

		return true;
	}

Clicky for image

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!