Sign in to follow this  
Endar

Load PCX

Recommended Posts

I'm attempting to write a loader for pcx image files for use in openGL. I'm using this code as the main reference, but one thing I'm not sure about, what is the palette used for? Is it present in 8-bit files? Because, as far as I can see in that code, he loads it, but never uses it. This is what I've got so far, but I'm always getting an access violation when the data is passed to open gl via 'glTexImage2D'. It isn't completed yet.
unsigned char* OGLLoadPCXFile(io::CReadFile& f, video::dimension2d& dim, unsigned int* size)
{
	PCXHEADER pcxheader;		// pcx file header
	int line_size;	// the number of bytes required to hold a single uncompressed scan line
	unsigned char* scanline = NULL;	// temp storage for a scan line
	unsigned char* palette = NULL;	// the color palette (768 bytes)
	unsigned char* imagedata = NULL;// the image data

	// if the file is open
	if( !f.isOpen() ){
		util::Message::print("OGLLoadPCXFile: \"%s\" is not a valid filename.", f.getFilename() );
		f.closeFile();	// close the file
		return NULL;
	}


	f.seek(0, SEEK_SET);	// seek to the start of the file

	f.read(&pcxheader, sizeof(PCXHEADER) );		// read the header from the file

	// Find the image dimensions
	// width = xmax - xmin + 1
	dim.width = pcxheader.window[2] - pcxheader.window[0] + 1;
	// height = ymax - ymin + 1
	dim.height = pcxheader.window[3] - pcxheader.window[1] + 1;

	// calc the number of bytes required for each uncompressed scan line
	line_size = pcxheader.nplanes * pcxheader.bytespline;


	scanline = new unsigned char[line_size];
	// check if memory alloc is valid
	if( !scanline ){
		util::Message::print("OGLLoadPCXFile: memory for scanline temp storage could not be allocated.");
		f.closeFile();
		return NULL;
	}

	imagedata = new unsigned char[(line_size * (int)dim.height) + 1];
	*size = (line_size * (int)dim.height) + 1;
	// check if memory alloc is valid
	if( !imagedata ){
		util::Message::print("OGLLoadPCXFile: memory for image data could not be allocated.");
		f.closeFile();
		return NULL;
	}

	char temp;					// temp to read the first of each byte sequence
	for(int linecount=0; linecount < (int)dim.height; linecount++){
		// read in bytes from file for scanline
		int line_pos;
		for(line_pos=0; line_pos < line_size; /*nothing for line_pos*/ ){
			// read in a byte
			f.read(&temp, 1);

			if( temp & 0xc0 ){	// if the first two bits are set
				char loop_temp;	// hold the value to repeat insert (temp&0x3f) times
				f.read(&loop_temp, 1 );		// read the repeat value
				for(int i=0; i < (temp & 0x3f); i++)
					scanline[line_pos+i] = loop_temp;	// insert the value

				line_pos += (temp & 0x3f);	// advance line_pos by the amount of values inserted
			}
			else{				// else this is the byte to be added to the line
				scanline[line_pos] = temp;
				line_pos++;
			}
		}

		// once the scanline has been read in, copy it to the image data
		if( pcxheader.nplanes == 1 ){	// we have an 8-bit image
			// because it is 8-bit, we can just copy it straight across
			memcpy( &imagedata[linecount*line_size], scanline, line_size);
		}
		else if( pcxheader.nplanes == 3 ){	// we have a 24-bit image, we have to "interleave" RGB values
			unsigned char* interLine = &imagedata[linecount * line_size];
			for(line_pos=0; line_pos != (int)dim.width; line_pos++, interLine+=3 ){
				interLine[0] = scanline[line_pos];
				interLine[1] = scanline[line_pos + (int)dim.width];
				interLine[2] = scanline[line_pos + ((int)dim.width*2)];


			}// for
		}// if

	}// for


	palette = new unsigned char[line_size];
	// check if memory alloc is valid
	if( !palette ){
		util::Message::print("OGLLoadPCXFile: memory for palette temp storage could not be allocated.");
		f.closeFile();
		return NULL;
	}

	// load the palette
	// Do I even need this?




	delete[] palette;
	//delete[] scanline;

	return imagedata;
}



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