Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Getting a .pcx file from a strange format in a BITMAP *

This thread is locked; no one can reply to it. rss feed Print
Getting a .pcx file from a strange format in a BITMAP *
Laurent Dufour
Member #2,666
August 2002

I'm really unfamiliar with the grabber and reading bytes from file and I need to get a .pcx pictur from a compacted file with this definition:
from byte x to y file offset where PCX data is located
from byte x' to y' length of pcx data in byte

For those who know I want to extract BITMAP *'s from .sff and .fnt file format form Elecbyte's Mugen (http://www.elecbyte.com) to make an utility.

Thank's a lot for any help.

HoopsMan
Member #1,943
February 2002

Are you trying to read a PCX file...coz support is already there in Allegro. Check This.
Mebbe you can be more specific about the file format?

spellcaster
Member #1,493
September 2001
avatar

In the allegro/src directory is a file called "pcx.c". It contains the pcx reading / loading code. What you need to do is to copy that file into your project and then change it slightly.
(Like skipping to the position of the pcx in the file)

--
There are no stupid questions, but there are a lot of inquisitive idiots.

gillius
Member #119
April 2000

One possibility is to read in the part of the file that is the PCX data into a memory buffer. Then you save that buffer to a file.

So essentially what you do is make a PCX extractor that extracts PCX data from the files and writes out PCX files which you can load.

Now if the PCX data doesn't have the complete header set or other misc stuff that makes it a full self-standing PCX file, then using the code right from Allegro might be a good choice.

Gillius
Gillius's Programming -- http://gillius.org/

A NMC
Member #2,192
April 2002
avatar

I'm working on it.
This is my class:

class spriteObj{
   public:
      spriteObj();
      ~spriteObj();
      spriteObj* searchSprite(int,int);

      BITMAP* img;
      int groupNo, imgNo;
      int x, y;
      PALETTE ownPal;
      bool samePal;
      bool hasPreviousCopy;
      spriteObj* next;
};

And the code is there:
<code>
spriteObj::spriteObj(){
x=y=0;
groupNo=imgNo=0;
img=NULL;
next=NULL;
hasPreviousCopy=false;
}

spriteObj::~spriteObj(){
if(img!=NULL&&!hasPreviousCopy)destroy_bitmap(img);
if(next!=NULL)delete next;
}

spriteObj* spriteObj::searchSprite(int inGroupNo, int inImgNo){
if(groupNo==inGroupNo&&imgNo==inImgNo) return this;
if(next!=NULL)return next->searchSprite(inGroupNo,inImgNo);
return NULL;
}

spriteObj* load_sff(char* directory, const char* filename){
FILE* infile;
char newFilename[256];
strcpy(newFilename,directory);
strcpy(newFilename+strlen(directory),filename);
spriteObj* Head=NULL;

char signature[12];
char ver[4];
int noOfGroup;
int noOfImg;
int offsetOfFirstFile;
int sizeOfSubheader;
char palType;
char blank[3];
char comment[476];

int offsetOfNextSubfile;
int subfileLength;
int xCoord,yCoord;
int unsigned groupNo;
int unsigned imgNo;
int unsigned indexOfPreviousCopy;
bool samePal;
char imgBlank[14];
PALETTE pal;

int xmin,ymin,width,height,bpp,bytes_per_line;
BITMAP *tmpBitMap;
int sizeCount;
bool gotPal=false;
PALETTE previousPal;

_fmode=O_BINARY;
if((infile=fopen(newFilename,"r"))!=NULL){
printf("loadinf file %s...\n",newFilename);
fread(signature,12,1,infile);
fread(ver,4,1,infile);
fread(&noOfGroup,4,1,infile);
fread(&noOfImg,4,1,infile);
fread(&offsetOfFirstFile,4,1,infile);
fread(&sizeOfSubheader,4,1,infile);
fread(&palType,1,1,infile);
fread(blank,3,1,infile);
for(int i=0;i<476;i++)comment<i>=getc(infile);

/*printf("\nsignture: %s\n", signature);
printf("version: %d%d%d%d\n", ver[0], ver[1], ver[2], ver[3]);
printf("no of group: %d\n", noOfGroup);
printf("no of img: %d\n", noOfImg);
printf("offset of first subheader: %d\n", offsetOfFirstFile);
printf("size of subheader: %d\n", sizeOfSubheader);
printf("palette type: %d\n", palType);
printf("blank: %s\n", blank);
printf("comment: %s\n", comment);*/

Head = new spriteObj;
spriteObj* current = Head;
for(int i=0;i<noOfImg;i++){
tmpBitMap=NULL;
xmin=ymin=width=height=bytes_per_line=0;
xCoord=yCoord=groupNo=imgNo=indexOfPreviousCopy=0;

fread(&offsetOfNextSubfile,4,1,infile);
fread(&subfileLength,4,1,infile);
fread(&xCoord,2,1,infile);
if(xCoord&0x00008000)xCoord-=65536;
fread(&yCoord,2,1,infile);
if(yCoord&0x00008000)yCoord-=65536;;
fread(&groupNo,2,1,infile);
fread(&imgNo,2,1,infile);
fread(&indexOfPreviousCopy,2,1,infile);
fread(&samePal,1,1,infile);
fread(blank,13,1,infile);
sizeCount=subfileLength;
if(/*indexOfPreviousCopy==0*/subfileLength!=0){
for(int i=0;i<4;i++)fgetc(infile);
fread(&xmin,2,1,infile);
fread(&ymin,2,1,infile);
fread(&width,2,1,infile);
fread(&height,2,1,infile);
width+=1-xmin;
height+=1-ymin;
for(int i=0;i<53;i++)fgetc(infile);
bpp=fgetc(infile)*8;
fread(&bytes_per_line,2,1,infile);
for(int i=0;i<60;i++)fgetc(infile);

tmpBitMap = create_bitmap_ex(bpp,width-xmin+1,height-ymin+1);

sizeCount-=128;
if(bpp == 8){
for(int liney=0;liney<height;liney++){
int linex=0, linexx=0;
while(linex<bytes_per_line){
char ch=fgetc(infile);
sizeCount--;
int c=0;
if(0xc0 == (0xc0&ch)){
c=(ch&0x3f);
ch=fgetc(infile);
sizeCount--;
}else c=1;
while (c--) {
if (linex < tmpBitMap->w)
tmpBitMap->line[liney][linex] = ch;
linex++;
}
}
}

if(!samePal){
while(sizeCount>0){
fgetc(infile);
sizeCount--;
if(sizeCount==768){
for(int i=0;i<256;i++){
current->ownPal<i>.r=(int)fgetc(infile)/4;
current->ownPal<i>.g=(int)fgetc(infile)/4;
current->ownPal<i>.b=(int)fgetc(infile)/4;
sizeCount-=3;
}
current->ownPal[0].r=63;
current->ownPal[0].g=0;
current->ownPal[0].b=63;

if(!gotPal||palType==0){
previousPal=current->ownPal;
gotPal=true;
}
break;
}
}
}else{
current->ownPal = previousPal;
}
}
}else{
spriteObj* tmpPointer=Head;
for(int i=0;i<indexOfPreviousCopy&&tmpPointer->next!=NULL;i++){
tmpPointer=tmpPointer->next;
}
tmpBitMap = tmpPointer->img;
current->ownPal = tmpPointer->ownPal;
current->hasPreviousCopy = true;
}
if(palType==1)for(;sizeCount>0;sizeCount--)fgetc(infile);
else if(samePal){fgetc(infile);sizeCount--;}
/* printf("\noffset of next subfile: %d\n", offsetOfNextSubfile);
printf("subfile length: %d\n", subfileLength);
printf("axis x coord: %d\n", xCoord);
printf("axis y coord: %d\n", yCoord);
printf("group no.: %d\n", groupNo);
printf("image no.: %d\n", imgNo);
printf("index of previous copy: %d\n", indexOfPreviousCopy);
printf("same palette: %s\n", (samePal)?"true":"false");
printf("xmin: %d\n",xmin);
printf("ymin: %d\n",xmin);
printf("width: %d\n",width);
printf("height: %d\n",height);
printf("bpp: %d\n",bpp);
printf("bytes_per_line: %d\n",bytes_per_line);
printf("byte leave: %d\n", sizeCount);
system("PAUSE"); */
current->x = xCoord;
current->y = yCoord;
current->img = tmpBitMap;
current->groupNo = groupNo;
current->imgNo = imgNo;
current->next = new spriteObj;
current = current->next;
}

fclose(infile);
current = Head;
if(current->next != NULL){
while(current->next->next != NULL)current=current->next;
delete current->next;
current->next = NULL;
}
}else printf("\ncan't open file %s\n",newFilename);
_fmode=O_TEXT;
printf("load finish.\n");
return Head;
}
<code>

This work fine for 8bits pcx since the newest version doesn't support 16bits sprites.

Go to: