Sign in to follow this  
Asheh

TGA File Writing

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this