Thread: need help with TGA writer.

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    need help with TGA writer.

    I'm working on a TGA writer for a ray-tracer, but I can't get it to work. Can anyone see what is wrong with it?

    Code:
    #include <fstream.h>
    #include <iostream.h>
    
    typedef unsigned char UCHAR;
    typedef signed char SCHAR;
    typedef unsigned short int USINT;
    
    class TARGA
    {
    public:
    	TARGA();
    	UCHAR idfield_no;		// no. of chars in id field.
    	UCHAR colormap_type;	// 1 = map 0 = no map
    	UCHAR image_type;		// 1 = color mapped, 2 = rgb, 3 = b&w. All uncompressed
    	USINT colormap_origin;	// index of first entry (lo-hi)
    	USINT colormap_length;	// no. of color map entries  (lo-hi)
    	UCHAR colormap_bit;		// 16 for 16bit, 24 for 24bit...
    	USINT x_origin;			// lower left corner (lo-hi)
    	USINT y_origin;			// lower left corner (lo-hi)
    	USINT width;			// width of image (lo-hi)
    	USINT height;			// height of image (lo-hi)
    	UCHAR pixel_bit;		// 16 for 16bit, 24 for 24bit...
    	UCHAR img_descriptor;	// Wierd bit-thingy (ignore?)
    	//UCHAR* idfield;		// Usually omitted (idfield_no=0)
    	//UCHAR* colormap;		// Omitted if truecolor
    	UCHAR* image_data;		// data stored differently depending on bitrate and colormap etc.
    							// The footer is ignored
    };
    TARGA::TARGA()
    {
    	idfield_no	= 0;	
    	colormap_type	= 0;	
    	image_type 	= 2;		// truecolor
    	colormap_origin = 0;
    	colormap_length = 0;
    	colormap_bit 	= 24;	// 24 bit
    	x_origin	= 0;	
    	y_origin	= 0;	
    	width		= 2;		// width
    	height		= 2;		// height
    	pixel_bit	= 24;		// 24 bit
    	img_descriptor	= 0;
    	//idfield; omitted!	
    	//colormap; omitted!	
    	image_data = new UCHAR[width*height*3];
    
    	for (int n=0; n<width*height*3; n++)
    	{
    		image_data[n]= 128; // should make all pixels gray
    	}
    }	
    
    
    main(int argc, char* argv)
    {
    	fstream file;
    	TARGA targa;
    	
    	char outfile[] = "outfile.tga";
    
    	file.open(outfile, ios::out);	// should it be "ios::binary"?
    	if (!file)
    	{
    		cout << "Error in opening " << outfile << " for writing" << endl;
    		return 1;
    	}
    
    	file.write((UCHAR *)&targa,sizeof(targa));
    	file.close();
    
    	file.open(outfile, ios::in);
    	if (!file)
    	{
    		cout << "Error in opening " << outfile << " for reading" << endl;
    		return 1;
    	}
    
    	char c;
    	int n=0;
    	while (true)
    	{
    		file.get(c);
    		cout << n << ": " << int(c) << endl;
    
    		if (file.eof()) { break; }
    		
    		n++;
    	}
    	//file.read((UCHAR *)&targa,sizeof(targa));
    	file.close();
    
    
    	return 0;
    }
    The written file is wierd, as one can see from the output. Why, for example, are there values in the written file that were not in the object targa?

    Some bytes of the file seems to be correct while others are not. For example the forth byte, is "-52" (or something) when it should be "0' (colormap origin). Or is that because it is a USINT?

    Any suggestions?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >should it be "ios::binary"?

    Yep.

    You should create a method in your class to write each field to a file individually (or overload the << operator).

    When classes/structs are written to a file directly any alignment padding will also get written (your first three fields (each one byte long) will probably get padded to four bytes so that the following integer (colormap_origin) falls on a double word boundary which is why your values are out), and any pointers that get written will just be the pointers and not the values that they point to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Type Writer Effect help please.
    By Fujitaka in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 10:11 AM
  2. Replies: 5
    Last Post: 01-10-2009, 02:36 PM
  3. Can't Load TGA?
    By arew264 in forum Game Programming
    Replies: 9
    Last Post: 05-02-2007, 11:37 AM
  4. TGA to HBITMAP
    By Mastadex in forum Windows Programming
    Replies: 2
    Last Post: 07-18-2006, 11:48 AM
  5. OpenGL; Using TGA Textures
    By cboard_member in forum Game Programming
    Replies: 6
    Last Post: 12-19-2005, 02:01 PM