Archived

This topic is now archived and is closed to further replies.

ChaosWars

WTF - my program is reading TGA files in octal

Recommended Posts

Erm... After struggling for a day to get my program to read filestreams using c++ fstream it now works. Well, sort of - like the title of the thread says, it insists in reading the file in sodding octal. How in the name of all that's holy did that happen? I thought it defaulted to binary? here's the code and a link to the program - note that you'll need Qt installed to run it. It was written in Linux if that makes a difference... Header file
#ifndef TARGA_H
#define TARGA_H

#include <GL/gl.h>
#include <fstream>
#include <iostream>

class LoadTexture
{
	public:
	LoadTexture(){}
	~LoadTexture(){ /*delete[] textureArray;*/ }
	
	/*typedef struct
	{
		GLubyte idLength;
		GLubyte colourMapType;
		GLubyte imageType;
		//Color Map Specification:
		//GLubyte firstEntryIndex1;
		//GLubyte firstEntryIndex2;
		GLubyte firstEntryIndex[2];
		//GLubyte colorMapLength1;
		//GLubyte colorMapLength2;
		GLubyte colorMapLength[2];
		GLubyte colorMapEntrySize;
		//image specification
		//GLubyte xOrigin1;
		//GLubyte xOrigin2;
		GLubyte xOrigin[2];
		//GLubyte yOrigin1;
		//GLubyte yOrigin2;
		GLubyte yOrigin[2];
		//GLubyte imageWidth1;
		//GLubyte imageWidth2;
		GLubyte imageWidth[2];
		//GLubyte imageHeight1;
		//GLubyte imageHeight2;
		GLubyte imageHeight[2];
		GLubyte pixelDepth;
		GLubyte imageDescriptor;
	} TGAHeader;*/
	
	typedef struct
	{
		GLbyte idLength;
		GLbyte colourMapType;
		GLbyte imageType;
		//Color Map Specification:

		GLbyte firstEntryIndex[2];		//2

		GLbyte colorMapLength[2];	//2

		GLbyte colorMapEntrySize;
		//image specification

		GLbyte xOrigin[2];	//2

		GLbyte yOrigin[2];	//2

		GLbyte imageWidth[2];	//2

		GLbyte imageHeight[2];	//2

		GLbyte pixelDepth;
		GLbyte imageDescriptor;
	} TGAHeader;
	
	typedef struct
	{
		GLubyte *imageData;
		GLint bitDepth;
		GLint width;
		GLint height;
		GLint texID;
		GLuint type;
	} Texture;
	
	TGAHeader tgaHeader;
	Texture texture;
	bool LoadTGATexture( char * );
	void LoadUncompressedTexture( TGAHeader *, std::fstream * );
	void LoadCompressedTexture( TGAHeader *, std::fstream * );
};

#endif	//TARGA_H
Source file
    #include "targa.h"

bool LoadTexture::LoadTGATexture( char *fileName )
{
	std::fstream file;
	file.open( fileName, std::ios::in | std::ios::binary );
	if ( !file ){
		printf( "Error : Could not open file %s\n", fileName );
		return false;
	}
	else{
		printf( "Opened file %s succesfully\n", fileName );
	}
	
	//file.seekg( 0, std::ios::beg );

	file.read( ( char * )&tgaHeader, sizeof( tgaHeader ) );
	if( ( tgaHeader.imageType = 2) ){
		LoadUncompressedTexture( &tgaHeader, &file );
	}
	else if( ( tgaHeader.imageType = 10 ) ){
		LoadCompressedTexture( &tgaHeader, &file );
	}
	
	file.close();
	return true;
}

void LoadTexture::LoadUncompressedTexture( TGAHeader *tgaHeader, std::fstream *file )
{
	printf( "Loading uncompressed tga image\n" );
}

void LoadTexture::LoadCompressedTexture( TGAHeader *tgaHeader, std::fstream *file )
{
	printf( "Loading compressed tga image\n" );
}
http://www.warmachine.demon.nl/seraphim.tar.gz Help? [edited by - ChaosWars on April 3, 2004 3:24:41 PM]

Share this post


Link to post
Share on other sites
I''m sure you dont mean this..

if( ( tgaHeader.imageType = 2) ){

I think you mean..

if( ( tgaHeader.imageType == 2) ){

Share this post


Link to post
Share on other sites
The only difference between reading in text and reading in binary is how newlines are handled. There''s no such thing as "reading in octal".

What, exactly, is going wrong?


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
rofl, what a mistake

Thanks for pointing that out, I probably would never have found it. It looked as though the program was reading the file and outputting the file stream in octal - I got this idea by opening up the TGA file I was using in a hex editor and if I viewed the file in octal mode then it looked the the output that gdb was giving me when I was watching the function When in reality I was setting the 3rd byte to be equal to 2 all the time

Once again, thanks for the prompt response.

Share this post


Link to post
Share on other sites