Archived

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

DarkTerrax

Loading .pcx file into d3d8 texture.

Recommended Posts

I am looking to load a picture that''s in .pcx format into a usable texture. I looking in some of the d3dx routines, but none of them seemed to support .pcx files. Are there other usable libraries out there for image loading, or am I going to have to write my own class to parse the .pcx file. Also, if I do have to do this, how would I then put the data into a new texture. -DT

Share this post


Link to post
Share on other sites
Have you tried just using D3DXCreateTextureFromFile()? I haven't tried PCX, but I know that many different formats (BMP, TGA, JPG) are supported, so you might want to just try and see if PCX is too.

Edited by - jaxson on August 2, 2001 10:09:02 PM

Share this post


Link to post
Share on other sites
Is there another format that you can use? For example, GIF files work just as well or better with 256 color images and I''m pretty sure that they are supported. For more than 256 colors, I think you''d be better off with JPEG (assuming you can lose a little color info without wrecking yourself)

But if you have to use PCX for some reason, the format isn''t very complex and you could probably find some examples on how to load it into memory (if not a DirectX Surface). Or you could just get a reference on the format (from wotsit.org, maybe) and figure it out yourself. I remember doing that a few years back and it''s great when it actually starts working the first time

Share this post


Link to post
Share on other sites
Yah, i actually started to implement it myself, but then someone else came to my rescue and gave me some code for it. It probably would have been a better learning experience to complete the code myself, but i just wanted to get it done.

Anyway, thought i would share the code:

  
bool LoadTex_PCX(IDirect3DDevice8 *pDevice, char *fp_szFileName, IDirect3DTexture8 *pTexture)
{
// load the pcx bitmap

FILE *file;
unsigned char palette[769];
unsigned char *indexdata;
long filesize=0;
char chartemp=0;
unsigned char repeat=0, color=0;
int x=0,y=0;

int width;
int height;
unsigned char *data;

//

// Open File

//

if( (file=fopen(fp_szFileName,"rb") ) == NULL) {
return FALSE;
}

//

// Get file length and make sure file is large enough to be a PCX

//

fseek(file, 0, SEEK_END);
filesize = ftell(file);
if (filesize<128) {
fclose(file);
return FALSE;
}

//

// Check PCX manufacturer

//

fseek(file,0,SEEK_SET);
if (fgetc(file) != 0xA) {
fclose(file);
return FALSE;
}

//

// Get image dimensions

//

width = 0; height = 0;
fseek(file,8,0);
fread(&width, 2,1,file);
fread(&height, 2,1,file);

//

// Load 256 color palette. Quit if not 256 colors (didn''t bother adding support for 16 color pcx)

//

fseek(file,-769,SEEK_END);
if (fgetc(file) != 0xC) {
fclose(file);
return FALSE;
}
fread(&palette,768,1,file);

//

// Allocate mem for data

//

//indexdata = (unsigned char *) malloc( (image.width+1) * (image.height+1) );

indexdata = new unsigned char [(width+1) * (height+1)];
if (indexdata == NULL) {
fclose(file);
delete [] indexdata;
return FALSE;
}

//

// Decode the image

//

fseek(file,128,0);
for (y=0;y<=height;y++)
for (x=0;x<=width;x++) {

if (!repeat)
{
color = getc(file);
if ( (color & 0xC0)==0xC0)
{
repeat = color & 0x3F;
color = getc(file);
}
else
repeat=1;
}
repeat--;
indexdata[x+y*(width+1)] = color;
}

fclose(file);

//if ((image.data = (unsigned char *) malloc((image.width+1) * (image.height+1) * 3)) == NULL)

data = new unsigned char [(width+1) * (height+1) * 4];

//convert to rgb format

for (x=0;x<(width*height);x++)
{
data[(x)*4 + 0] = palette[ indexdata[x]*3 + 2];
data[(x)*4 + 1] = palette[ indexdata[x]*3 + 1];
data[(x)*4 + 2] = palette[ indexdata[x]*3 + 0] ;
data[(x)*4 + 3] = 255; // alpha

}

delete [] indexdata;

height++;
width++;


//IDirect3DSurface8* surface;

HRESULT hr;

// create the texture


//hr = Device->CreateImageSurface(width, height, D3DFMT_A8R8G8B8, &surface);

hr = D3DXCreateTexture(pDevice, width, height, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture);

D3DLOCKED_RECT bits;
//bits.pBits = new unsigned char[width * height * 4];

hr = pTexture->LockRect(0, &bits, NULL, 0);
memcpy(bits.pBits, data, width * height * 4);
hr = pTexture->UnlockRect(0);

delete [] data;

return true;

}//endfunction

Share this post


Link to post
Share on other sites
I think D3DXCreateTexture may change the format and size of texture.
So what you should more do is to get width,height and format of the surface from surface desc and then fill surface using right values.

Share this post


Link to post
Share on other sites