hgr-utils: pcx2hr -- clean up whitespace, remove compiler warnings

This commit is contained in:
Vince Weaver 2016-03-11 16:14:59 -05:00
parent 9efa352160
commit f39f91863b

View File

@ -1,401 +1,389 @@
/* Converts 140x192 8-bit PCX file with correct palette to Apple II HGR */ /* Converts 140x192 8-bit PCX file with correct palette to Apple II HGR */
#include <stdio.h> /* For FILE I/O */ #include <stdio.h> /* For FILE I/O */
#include <string.h> /* For strncmp */ #include <string.h> /* For strncmp */
#include <fcntl.h> /* for open() */ #include <fcntl.h> /* for open() */
#include <unistd.h> /* for lseek() */ #include <unistd.h> /* for lseek() */
#include <sys/stat.h> /* for file modes */ #include <sys/stat.h> /* for file modes */
#include <stdlib.h> /* free() */ #include <stdlib.h> /* free() */
#define PCX_UNKNOWN 0 #define PCX_UNKNOWN 0
#define PCX_8BIT 1 #define PCX_8BIT 1
#define PCX_24BIT 2 #define PCX_24BIT 2
static int debug=0; static int debug=0;
static int vmwGetPCXInfo(char *filename, int *xsize, int *ysize, int *type) { static int vmwGetPCXInfo(char *filename, int *xsize, int *ysize, int *type) {
unsigned char pcx_header[128];
int xmin,ymin,xmax,ymax,version=PCX_UNKNOWN,bpp,pcx_fd;
int result;
/* Open the file */ unsigned char pcx_header[128];
pcx_fd=open(filename,O_RDONLY); int xmin,ymin,xmax,ymax,version=PCX_UNKNOWN,bpp,pcx_fd;
int result;
if (pcx_fd<0) {
fprintf(stderr,"ERROR! File \"%s\" not found!\n",filename);
return -1;
}
lseek(pcx_fd,0,SEEK_SET);
result=read(pcx_fd,&pcx_header,128);
if (result<0) {
fprintf(stderr,"Error! Could not read header from file %s\n",filename);
return -1;
}
xmin=(pcx_header[5]<<8)+pcx_header[4]; /* Open the file */
ymin=(pcx_header[7]<<8)+pcx_header[6]; pcx_fd=open(filename,O_RDONLY);
xmax=(pcx_header[9]<<8)+pcx_header[8];
ymax=(pcx_header[11]<<8)+pcx_header[10];
version=pcx_header[1]; if (pcx_fd<0) {
bpp=pcx_header[3]; fprintf(stderr,"ERROR! File \"%s\" not found!\n",filename);
return -1;
if (debug) { }
printf("Manufacturer: ");
if (pcx_header[0]==10) printf("Zsoft\n");
else printf("Unknown %i\n",pcx_header[0]);
printf("Version: ");
switch(version) { lseek(pcx_fd,0,SEEK_SET);
case 0: printf("2.5\n"); break;
case 2: printf("2.8 w palette\n"); break;
case 3: printf("2.8 w/o palette\n"); break;
case 4: printf("Paintbrush for Windows\n"); break;
case 5: printf("3.0+\n"); break;
default: printf("Unknown %i\n",version);
}
printf("Encoding: ");
if (pcx_header[2]==1) printf("RLE\n");
else printf("Unknown %i\n",pcx_header[2]);
printf("BitsPerPixelPerPlane: %i\n",bpp);
printf("File goes from %i,%i to %i,%i\n",xmin,ymin,xmax,ymax);
printf("Horizontal DPI: %i\n",(pcx_header[13]<<8)+pcx_header[12]);
printf("Vertical DPI: %i\n",(pcx_header[15]<<8)+pcx_header[14]);
printf("Number of colored planes: %i\n",pcx_header[65]);
printf("Bytes per line: %i\n",(pcx_header[67]<<8)+pcx_header[66]);
printf("Palette Type: %i\n",(pcx_header[69]<<8)+pcx_header[68]);
printf("Hscreen Size: %i\n",(pcx_header[71]<<8)+pcx_header[70]);
printf("Vscreen Size: %i\n",(pcx_header[73]<<8)+pcx_header[72]);
}
*xsize=(xmax-xmin+1);
*ysize=(ymax-ymin+1);
if ((version==5) && (bpp==8) && (pcx_header[65]==3)) *type=PCX_24BIT; result=read(pcx_fd,&pcx_header,128);
else if (version==5) *type=PCX_8BIT; if (result<0) {
else *type=PCX_UNKNOWN; fprintf(stderr,"Error! Could not read header from file %s\n",filename);
return -1;
}
close(pcx_fd); xmin=(pcx_header[5]<<8)+pcx_header[4];
ymin=(pcx_header[7]<<8)+pcx_header[6];
return 0;
xmax=(pcx_header[9]<<8)+pcx_header[8];
ymax=(pcx_header[11]<<8)+pcx_header[10];
version=pcx_header[1];
bpp=pcx_header[3];
if (debug) {
printf("Manufacturer: ");
if (pcx_header[0]==10) printf("Zsoft\n");
else printf("Unknown %i\n",pcx_header[0]);
printf("Version: ");
switch(version) {
case 0: printf("2.5\n"); break;
case 2: printf("2.8 w palette\n"); break;
case 3: printf("2.8 w/o palette\n"); break;
case 4: printf("Paintbrush for Windows\n"); break;
case 5: printf("3.0+\n"); break;
default: printf("Unknown %i\n",version);
}
printf("Encoding: ");
if (pcx_header[2]==1) printf("RLE\n");
else printf("Unknown %i\n",pcx_header[2]);
printf("BitsPerPixelPerPlane: %i\n",bpp);
printf("File goes from %i,%i to %i,%i\n",xmin,ymin,xmax,ymax);
printf("Horizontal DPI: %i\n",(pcx_header[13]<<8)+pcx_header[12]);
printf("Vertical DPI: %i\n",(pcx_header[15]<<8)+pcx_header[14]);
printf("Number of colored planes: %i\n",pcx_header[65]);
printf("Bytes per line: %i\n",(pcx_header[67]<<8)+pcx_header[66]);
printf("Palette Type: %i\n",(pcx_header[69]<<8)+pcx_header[68]);
printf("Hscreen Size: %i\n",(pcx_header[71]<<8)+pcx_header[70]);
printf("Vscreen Size: %i\n",(pcx_header[73]<<8)+pcx_header[72]);
}
*xsize=(xmax-xmin+1);
*ysize=(ymax-ymin+1);
if ((version==5) && (bpp==8) && (pcx_header[65]==3)) *type=PCX_24BIT;
else if (version==5) *type=PCX_8BIT;
else *type=PCX_UNKNOWN;
close(pcx_fd);
return 0;
} }
unsigned char colors[256]; static unsigned char colors[256];
static int vmwLoadPCX(char *filename, unsigned char *framebuffer) { static int vmwLoadPCX(char *filename, unsigned char *framebuffer) {
int pcx_fd; int pcx_fd;
int x,y,i,numacross,xsize,ysize,xmin,ymin; int x,i,numacross,xsize,ysize,xmin,ymin;
unsigned int r,g,b; unsigned int r,g,b;
int xmax,ymax; int xmax,ymax;
unsigned char pcx_header[128]; unsigned char pcx_header[128];
unsigned char temp_byte; unsigned char temp_byte;
int result; int result;
unsigned char *pointer=framebuffer; unsigned char *pointer=framebuffer;
/* Open the file */ /* Open the file */
pcx_fd=open(filename,O_RDONLY); pcx_fd=open(filename,O_RDONLY);
if (pcx_fd<0) {
fprintf(stderr,"ERROR! File \"%s\" not found!\n",filename);
return -1;
}
/*************** DECODE THE HEADER *************************/
result=read(pcx_fd,&pcx_header,128);
if (result<0) {
fprintf(stderr,"ERROR opening header of file %s\n",filename);
}
xmin=(pcx_header[5]<<8)+pcx_header[4]; if (pcx_fd<0) {
ymin=(pcx_header[7]<<8)+pcx_header[6]; fprintf(stderr,"ERROR! File \"%s\" not found!\n",filename);
return -1;
xmax=(pcx_header[9]<<8)+pcx_header[8]; }
ymax=(pcx_header[11]<<8)+pcx_header[10];
xsize=((xmax-xmin)+1); /*************** DECODE THE HEADER *************************/
ysize=((ymax-ymin)+1); result=read(pcx_fd,&pcx_header,128);
if (result<0) {
x=0; y=0; fprintf(stderr,"ERROR opening header of file %s\n",filename);
}
while (x<xsize*ysize) {
/* read a byte */ xmin=(pcx_header[5]<<8)+pcx_header[4];
result=read(pcx_fd,&temp_byte,1); ymin=(pcx_header[7]<<8)+pcx_header[6];
/* if > 0xc0, then it's a RLE byte */ xmax=(pcx_header[9]<<8)+pcx_header[8];
if (0xc0 == (temp_byte&0xc0)) { ymax=(pcx_header[11]<<8)+pcx_header[10];
numacross=temp_byte&0x3f;
result=read(pcx_fd,&temp_byte,1); xsize=((xmax-xmin)+1);
for(i=0;i<numacross;i++) { ysize=((ymax-ymin)+1);
*pointer=temp_byte;
pointer++; x=0;
x++;
} while (x<xsize*ysize) {
}
else { /* read a byte */
*pointer=temp_byte; result=read(pcx_fd,&temp_byte,1);
pointer++;
x++; /* if > 0xc0, then it's a RLE byte */
} if (0xc0 == (temp_byte&0xc0)) {
} numacross=temp_byte&0x3f;
result=read(pcx_fd,&temp_byte,1);
/*Load Palette*/ for(i=0;i<numacross;i++) {
*pointer=temp_byte;
result=lseek(pcx_fd,-769,SEEK_END); pointer++;
x++;
result=read(pcx_fd,&temp_byte,1); }
if (temp_byte!=12) { }
fprintf(stderr,"Error! No palette found!\n"); else {
return -1; *pointer=temp_byte;
} pointer++;
x++;
for(i=0;i<255;i++) { }
result=read(pcx_fd,&temp_byte,1); }
r=temp_byte;
result=read(pcx_fd,&temp_byte,1); /*Load Palette*/
g=temp_byte;
result=read(pcx_fd,&temp_byte,1); result=lseek(pcx_fd,-769,SEEK_END);
b=temp_byte;
result=read(pcx_fd,&temp_byte,1);
if (temp_byte!=12) {
fprintf(stderr,"Error! No palette found!\n");
return -1;
}
for(i=0;i<255;i++) {
result=read(pcx_fd,&temp_byte,1);
r=temp_byte;
result=read(pcx_fd,&temp_byte,1);
g=temp_byte;
result=read(pcx_fd,&temp_byte,1);
b=temp_byte;
#if 0 #if 0
int colors[8]={0, /* black 0 */ int colors[8]={ 0, /* black 0 */
1, /* purple */ 1, /* purple */
2, /* green */ 2, /* green */
3, /* white 0 */ 3, /* white 0 */
0, /* black 1 */ 0, /* black 1 */
1, /* blue */ 1, /* blue */
2, /* orange */ 2, /* orange */
3, /* white */ 3, /* white */
}; };
#endif #endif
if ((r==0x00) && (g==0x00) && (b==0x00)) colors[i]=0; /* black */
else
if ((r==0xff) && (g==0xff) && (b==0xff)) colors[i]=3; /* white */
else
if ((r==0x1b) && (g==0x9a) && (b==0xfe)) colors[i]=0x81; /* blue */
else
if ((r==0xe4) && (g==0x34) && (b==0xfe)) colors[i]=0x41; /* purple */
else
if ((r==0xcd) && (g==0x5b) && (b==0x1)) colors[i]=0x82; /* orange */
else
if ((r==0x1b) && (g==0xcb) && (b==0x1)) colors[i]=0x42; /* green */
else fprintf(stderr,"Unknown color %i %x %x %x\n",i,r,g,b);
// printf("%i: 0x%x %x %x\n",i,r,g,b); if ((r==0x00) && (g==0x00) && (b==0x00)) colors[i]=0; /* black */
else
} if ((r==0xff) && (g==0xff) && (b==0xff)) colors[i]=3; /* white */
else
if ((r==0x1b) && (g==0x9a) && (b==0xfe)) colors[i]=0x81; /* blue */
else
if ((r==0xe4) && (g==0x34) && (b==0xfe)) colors[i]=0x41; /* purple */
else
if ((r==0xcd) && (g==0x5b) && (b==0x1)) colors[i]=0x82; /* orange */
else
if ((r==0x1b) && (g==0xcb) && (b==0x1)) colors[i]=0x42; /* green */
else fprintf(stderr,"Unknown color %i %x %x %x\n",i,r,g,b);
close(pcx_fd); // printf("%i: 0x%x %x %x\n",i,r,g,b);
return 0; }
close(pcx_fd);
return 0;
} }
static int make_bw_image(unsigned char *in_framebuffer, static int make_bw_image(unsigned char *in_framebuffer,
unsigned char *out_framebuffer, unsigned char *out_framebuffer,
int xsize, int ysize) { int xsize, int ysize) {
short fourteen_bits; short fourteen_bits;
int i,yoffset=0; int i,yoffset=0;
int y,x; int y,x;
unsigned char byte1,byte2; unsigned char byte1,byte2;
int page,block,leaf; int page,block,leaf;
unsigned char *pointer; unsigned char *pcx,*hgr;
unsigned char *pcx,*hgr;
pointer=in_framebuffer; pcx=in_framebuffer;
hgr=out_framebuffer;
pcx=in_framebuffer; for(y=0;y<ysize;y++) {
hgr=out_framebuffer; for(x=0;x<20;x++) {
for(y=0;y<ysize;y++) { fourteen_bits=0;
for(x=0;x<20;x++) {
fourteen_bits=0; for(i=0;i<14;i++) {
fourteen_bits|= ((colors[(*pcx)&0x7]&0x1)<<i);
pcx++;
}
byte1=(fourteen_bits&0x7f)|(0<<7);
byte2=((fourteen_bits>>7)&0x7f)|(0<<7);
for(i=0;i<14;i++) { page=(y%8);
fourteen_bits|= ((colors[(*pcx)&0x7]&0x1)<<i); block=((y/8)%8);
pcx++; leaf=(y/64);
}
byte1=(fourteen_bits&0x7f)|(0<<7);
byte2=((fourteen_bits>>7)&0x7f)|(0<<7);
page=(y%8); yoffset=(page*1024) + (block*128) + (leaf*40);
block=((y/8)%8);
leaf=(y/64);
yoffset=(page*1024) + (block*128) + (leaf*40); // printf("%d %d = %x %x %x\n",x,y,fourteen_bits,yoffset,
/* // yoffset+(x*2));
printf("%d %d = %x %x %x\n",x,y,fourteen_bits,yoffset,
yoffset+(x*2));
*/
hgr=out_framebuffer+yoffset+(x*2);
*hgr=byte1; hgr=out_framebuffer+yoffset+(x*2);
*(hgr+1)=byte2;
} *hgr=byte1;
} *(hgr+1)=byte2;
return 0; }
}
return 0;
} }
static int make_color_image(unsigned char *in_framebuffer, static int make_color_image(unsigned char *in_framebuffer,
unsigned char *out_framebuffer, unsigned char *out_framebuffer,
int xsize, int ysize) { int xsize, int ysize) {
short fourteen_bits; short fourteen_bits;
int i,pal[2],yoffset=0; int i,pal[2],yoffset=0;
int y,x; int y,x;
unsigned char byte1,byte2; unsigned char byte1,byte2;
int page,block,leaf; int page,block,leaf;
unsigned char *pcx,*hgr;
unsigned char *pointer;
unsigned char *pcx,*hgr;
pointer=in_framebuffer;
#if 0 #if 0
for(y=0;y<ysize;y++) { unsigned char *pointer=in_framebuffer;
for(x=0;x<xsize;x++) {
printf("%1x",*pointer); for(y=0;y<ysize;y++) {
pointer++; for(x=0;x<xsize;x++) {
} printf("%1x",*pointer);
printf("\n"); pointer++;
} }
printf("\n");
}
#endif #endif
pcx=in_framebuffer; pcx=in_framebuffer;
hgr=out_framebuffer; hgr=out_framebuffer;
for(y=0;y<ysize;y++) { for(y=0;y<ysize;y++) {
for(x=0;x<20;x++) { for(x=0;x<20;x++) {
fourteen_bits=0; fourteen_bits=0;
pal[0]=0; pal[1]=0; pal[0]=0; pal[1]=0;
for(i=0;i<7;i++) { for(i=0;i<7;i++) {
fourteen_bits|= ((colors[(*pcx)&0x7]&0x3)<<(i*2)); fourteen_bits|= ((colors[(*pcx)&0x7]&0x3)<<(i*2));
/* choose which palette */
pal[i/4]+= (colors[*pcx]&0x80) - (colors[*pcx]&0x40);
pcx++; /* choose which palette */
} pal[i/4]+= (colors[*pcx]&0x80) - (colors[*pcx]&0x40);
byte1=(fourteen_bits&0x7f)|((pal[0]>0)<<7); pcx++;
byte2=((fourteen_bits>>7)&0x7f)|((pal[1]>0)<<7); }
byte1=(fourteen_bits&0x7f)|((pal[0]>0)<<7);
byte2=((fourteen_bits>>7)&0x7f)|((pal[1]>0)<<7);
page=(y%8); page=(y%8);
block=((y/8)%8); block=((y/8)%8);
leaf=(y/64); leaf=(y/64);
yoffset=(page*1024) + (block*128) + (leaf*40); yoffset=(page*1024) + (block*128) + (leaf*40);
/*
printf("%d %d = %x %x %x\n",x,y,fourteen_bits,yoffset,
yoffset+(x*2));
*/
hgr=out_framebuffer+yoffset+(x*2);
*hgr=byte1; // printf("%d %d = %x %x %x\n",x,y,fourteen_bits,yoffset,
*(hgr+1)=byte2; // yoffset+(x*2));
}
} hgr=out_framebuffer+yoffset+(x*2);
return 0;
*hgr=byte1;
*(hgr+1)=byte2;
}
}
return 0;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
int xsize=0,ysize=0,type; int xsize=0,ysize=0,type;
unsigned char *in_framebuffer; unsigned char *in_framebuffer;
unsigned char *out_framebuffer; unsigned char *out_framebuffer;
char *filename; char *filename;
if (argc<2) { if (argc<2) {
fprintf(stderr,"\nUsage: %s PCXFILE\n\n",argv[0]); fprintf(stderr,"\nUsage: %s PCXFILE\n\n",argv[0]);
exit(1); exit(1);
} }
filename=strdup(argv[1]); filename=strdup(argv[1]);
vmwGetPCXInfo(filename,&xsize,&ysize,&type);
vmwGetPCXInfo(filename,&xsize,&ysize,&type); in_framebuffer=calloc(xsize*ysize,sizeof(unsigned char));
if (in_framebuffer==NULL) {
fprintf(stderr,"Error allocating memory!\n");
return -1;
}
in_framebuffer=calloc(xsize*ysize,sizeof(unsigned char)); vmwLoadPCX(filename,in_framebuffer);
if (in_framebuffer==NULL) {
fprintf(stderr,"Error allocating memory!\n");
return -1;
}
vmwLoadPCX(filename,in_framebuffer); out_framebuffer=calloc(8192,sizeof(unsigned char));
if (out_framebuffer==NULL) {
fprintf(stderr,"Error allocating memory!\n");
return -1;
}
out_framebuffer=calloc(8192,sizeof(unsigned char)); if ((ysize==160) || (ysize=192)) {
if (out_framebuffer==NULL) { /* HGR or HGR2 */
fprintf(stderr,"Error allocating memory!\n"); }
return -1; else {
} fprintf(stderr,"Warning, possibly truncating due to ysize: %d\n",ysize);
}
if ((ysize==160) || (ysize=192)) { if (xsize==140) {
/* HGR or HGR2 */ make_color_image(in_framebuffer,out_framebuffer,xsize,ysize);
} }
else { else if (xsize==280) {
fprintf(stderr,"Warning, possibly truncating due to ysize: %d\n",ysize); make_bw_image(in_framebuffer,out_framebuffer,xsize,ysize);
} }
else {
fprintf(stderr,"Error! PCX file wrong xsize %d\n",xsize);
}
unsigned char header[4];
if (xsize==140) { /* assume HGR page 1 */
make_color_image(in_framebuffer,out_framebuffer,xsize,ysize); int offset=8192;
}
else if (xsize==280) {
make_bw_image(in_framebuffer,out_framebuffer,xsize,ysize);
}
else {
fprintf(stderr,"Error! PCX file wrong xsize %d\n",xsize);
}
unsigned char header[4]; /* Last 8 bytes are ignored anyway; by not saving them we can fit */
/* in 33 disk sectors rather than 34 */
int file_size=8184;
header[0]=offset&0xff;
/* assume HGR page 1 */ header[1]=(offset>>8)&0xff;
int offset=8192; header[2]=file_size&0xff;
header[3]=(file_size>>8)&0xff;
/* Last 8 bytes are ignored anyway; by not saving them we can fit */ fwrite(header,sizeof(unsigned char),4,stdout);
/* in 33 disk sectors rather than 34 */
int file_size=8184;
header[0]=offset&0xff; /* Don't need the last 8 bytes; makes it fit in one fewer disk sectors */
header[1]=(offset>>8)&0xff; fwrite(out_framebuffer,sizeof(unsigned char),8184,stdout);
header[2]=file_size&0xff;
header[3]=(file_size>>8)&0xff;
fwrite(header,sizeof(unsigned char),4,stdout); free(out_framebuffer);
free(in_framebuffer);
/* Don't need the last 8 bytes; makes it fit in one fewer disk sectors */ return 0;
fwrite(out_framebuffer,sizeof(unsigned char),8184,stdout);
free(out_framebuffer);
free(in_framebuffer);
return 0;
} }