void SDGF_Image::load_pcx(const char *name,SDGF_Canvas &Canvas)
{
FILE *target;
unsigned long int width,heigth,index,position,length,uncompressed_length;
unsigned char repeat;
unsigned char *original;
unsigned char *uncompressed;
PCX_head head;
target=fopen(name,"rb");
if(target==NULL)
{
puts("Can't open a image file");
exit(EXIT_FAILURE);
}
fseek(target,0,SEEK_END);
length=ftell(target)-128;
rewind(target);
fread(&head,128,1,target);
if(head.color*head.planes!=24)
{
puts("Incorrect image format");
exit(EXIT_FAILURE);
}
if(head.compress!=1)
{
puts("Incorrect image format");
exit(EXIT_FAILURE);
}
width=head.max_x-head.min_x+1;
heigth=head.max_y-head.min_y+1;
uncompressed_length=3*width*heigth;
index=0;
position=0;
original=(unsigned char*)calloc(length,1);
if(original==NULL)
{
puts("Can't allocate memory for image buffer");
exit(EXIT_FAILURE);
}
uncompressed=(unsigned char*)calloc(uncompressed_length,1);
if(uncompressed==NULL)
{
puts("Can't allocate memory for image buffer");
exit(EXIT_FAILURE);
}
fread(original,length,1,target);
fclose(target);
while (index<length)
{
if (original[index]<192)
{
uncompressed[position]=original[index];
position++;
index++;
}
else
{
for (repeat=original[index]-192;repeat>0;repeat--)
{
uncompressed[position]=original[index+1];
position++;
}
index+=2;
}
}
free(original);
Canvas.load_image(uncompressed,width,heigth);
free(uncompressed);
}
void SDGF_Canvas::load_image(const unsigned char *buffer,const unsigned long int image_width,const unsigned long int image_height)
{
unsigned long int length;
length=image_width*image_height*3;
width=image_width;
height=image_height;
if(image!=NULL) free(image);
image=(SDGF_Color*)calloc(length,1);
if (image==NULL)
{
puts("Can't allocate memory for image buffer");
exit(EXIT_FAILURE);
}
memmove(image,buffer,length);
}
struct SDGF_Color
{
unsigned char blue:8;
unsigned char green:8;
unsigned char red:8;
};