pcx2hgr now works

it generates color images that can be loaded with
  BLOAD IMAGE,A$2000
This commit is contained in:
Vince Weaver 2012-05-04 15:33:00 -04:00
parent b10bc5746b
commit 77db999e7d
3 changed files with 189 additions and 89 deletions

View File

@ -5,10 +5,14 @@ LFLAGS =
all: test.dsk
test.dsk: TEST
test.dsk: TEST VINCE.IMG
../dos33 test.dsk SAVE A TEST
../dos33 test.dsk SAVE B VINCE.IMG
VINCE.IMG: vince.pcx ../pcx2hgr
../pcx2hgr vince.pcx > VINCE.IMG
TEST: test.bas
../tokenize_asoft < test.bas > TEST

10
asoft_presenter/hgr.pal Normal file
View File

@ -0,0 +1,10 @@
GIMP Palette
Name: Apple II HGR
Columns: 0
#
0 0 0 Black
27 203 1 Green
228 52 254 Purple
255 255 255 White
205 91 1 Orange
27 154 254 Blue

262
pcx2hgr.c
View File

@ -1,22 +1,22 @@
/* Converts 280x160 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 <string.h> /* For strncmp */
#include <fcntl.h> /* for open() */
#include <unistd.h> /* for lseek() */
#include <sys/stat.h> /* for file modes */
#include <stdlib.h> /* free() */
#define PCX_UNKNOWN 0
#define PCX_8BIT 1
#define PCX_24BIT 2
int debug=1;
static int debug=0;
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,debug=1,pcx_fd;
int xmin,ymin,xmax,ymax,version=PCX_UNKNOWN,bpp,pcx_fd;
int result;
/* Open the file */
@ -90,16 +90,18 @@ static int vmwGetPCXInfo(char *filename, int *xsize, int *ysize, int *type) {
return 0;
}
unsigned char colors[256];
static int vmwLoadPCX(char *filename, unsigned char *framebuffer) {
int pcx_fd;
/*
int x,y,i,numacross,xsize,ysize,xmin,ymin;
unsigned int r,g,b;
int bpp,planes,bpl,xmax,ymax,version;
int xmax,ymax;
unsigned char pcx_header[128];
unsigned char temp_byte;
*/
int result;
unsigned char *pointer=framebuffer;
/* Open the file */
pcx_fd=open(filename,O_RDONLY);
@ -109,102 +111,93 @@ static int vmwLoadPCX(char *filename, unsigned char *framebuffer) {
return -1;
}
#if 0
/*************** DECODE THE HEADER *************************/
read(pcx_fd,&pcx_header,128);
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];
ymin=(pcx_header[7]<<8)+pcx_header[6];
xmax=(pcx_header[9]<<8)+pcx_header[8];
ymax=(pcx_header[11]<<8)+pcx_header[10];
version=pcx_header[1];
bpp=pcx_header[3];
planes=pcx_header[65];
bpl=(pcx_header[67]<<8)+pcx_header[66];
xsize=((xmax-xmin)+1);
ysize=((ymax-ymin)+1);
/* Possibly add some sanity checking in the header at some point... */
/* Or actually even get the proper VALUES from the header. Some day... */
if (LoadPic) {
unsigned char *pointer=target->memory;
x=0; y=0;
x=0; y=0;
while (x<xsize*ysize) {
read(pcx_fd,&temp_byte,1);
if (0xc0 == (temp_byte&0xc0)) {
numacross=temp_byte&0x3f;
read(pcx_fd,&temp_byte,1);
// y++; if (y%2) temp_byte=0xff;
// temp_byte=0xff;
// printf("%i pixels of %i\n",numacross,temp_byte);
for(i=0;i<numacross;i++) {
*pointer=temp_byte;
pointer++;
x++;
}
while (x<xsize*ysize) {
//printf("Color=%i Across=%i\n",temp_byte,numacross);
//vmwDrawHLine(x,y,numacross,temp_byte,target);
//x+=numacross;
}
else {
//vmwPutPixel(x,y,temp_byte,target);
//printf("%i, %i Color=%i\n",x,y,temp_byte);
/* read a byte */
result=read(pcx_fd,&temp_byte,1);
/* if > 0xc0, then it's a RLE byte */
if (0xc0 == (temp_byte&0xc0)) {
numacross=temp_byte&0x3f;
result=read(pcx_fd,&temp_byte,1);
for(i=0;i<numacross;i++) {
*pointer=temp_byte;
// if (temp_byte!=0) printf("COLOR=%i\n",temp_byte);
pointer++;
x++;
}
/* why is this needed? */
// if (x%xsize==0) {
// pointer++;
// }
//printf("WARNING! X=%i\n",x);
// x=0;
// y++;
// }
}
}
else {
*pointer=temp_byte;
pointer++;
x++;
}
}
/*Load Palette*/
if (LoadPal) {
lseek(pcx_fd,-769,SEEK_END);
/*Load Palette*/
result=lseek(pcx_fd,-769,SEEK_END);
read(pcx_fd,&temp_byte,1);
if (temp_byte!=12) {
printf("Error! No palette found!\n");
}
else
for(i=0;i<255;i++) {
read(pcx_fd,&temp_byte,1);
r=temp_byte;
read(pcx_fd,&temp_byte,1);
g=temp_byte;
read(pcx_fd,&temp_byte,1);
b=temp_byte;
vmwLoadPalette(graph_state,
r,
g,
b,i);
// printf("%i: 0x%x %x %x\n",i,r,g,b);
}
vmwFlushPalette(graph_state);
result=read(pcx_fd,&temp_byte,1);
if (temp_byte!=12) {
fprintf(stderr,"Error! No palette found!\n");
return -1;
}
#endif
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
int colors[8]={0, /* black 0 */
1, /* purple */
2, /* green */
3, /* white 0 */
0, /* black 1 */
1, /* blue */
2, /* orange */
3, /* white */
};
#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);
}
close(pcx_fd);
return 0;
@ -213,18 +206,111 @@ static int vmwLoadPCX(char *filename, unsigned char *framebuffer) {
int main(int argc, char **argv) {
int xsize=0,ysize=0,type;
unsigned char *framebuffer;
int xsize=0,ysize=0,type,x,y;
unsigned char *in_framebuffer,*pointer;
unsigned char *out_framebuffer,*pcx,*hgr;
vmwGetPCXInfo("vince.pcx",&xsize,&ysize,&type);
char *filename;
framebuffer=calloc(xsize*ysize,sizeof(unsigned char));
if (framebuffer==NULL) {
if (argc<2) {
fprintf(stderr,"\nUsage: %s PCXFILE\n\n",argv[0]);
exit(1);
}
filename=strdup(argv[1]);
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;
}
vmwLoadPCX("vince.pcx",framebuffer);
vmwLoadPCX(filename,in_framebuffer);
pointer=in_framebuffer;
#if 0
for(y=0;y<ysize;y++) {
for(x=0;x<xsize;x++) {
printf("%1x",*pointer);
pointer++;
}
printf("\n");
}
#endif
out_framebuffer=calloc(8192,sizeof(unsigned char));
if (out_framebuffer==NULL) {
fprintf(stderr,"Error allocating memory!\n");
return -1;
}
short fourteen_bits;
int i,pal[2],yoffset=0;
unsigned char byte1,byte2;
int page,block,leaf;
pcx=in_framebuffer;
hgr=out_framebuffer;
for(y=0;y<ysize;y++) {
for(x=0;x<20;x++) {
fourteen_bits=0;
pal[0]=0; pal[1]=0;
for(i=0;i<7;i++) {
fourteen_bits|= ((colors[(*pcx)&0x7]&0x3)<<(i*2));
/* choose which palette */
pal[i/4]+= (colors[*pcx]&0x80) - (colors[*pcx]&0x40);
pcx++;
}
byte1=(fourteen_bits&0x7f)|((pal[0]>0)<<7);
byte2=((fourteen_bits>>7)&0x7f)|((pal[1]>0)<<7);
page=(y%8);
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));
*/
hgr=out_framebuffer+yoffset+(x*2);
*hgr=byte1;
*(hgr+1)=byte2;
}
}
unsigned char header[4];
/* assume HGR page 1 */
int offset=8192;
int file_size=8184;
header[0]=offset&0xff;
header[1]=(offset>>8)&0xff;
header[2]=file_size&0xff;
header[3]=(file_size>>8)&0xff;
fwrite(header,sizeof(unsigned char),4,stdout);
/* Don't need the last 8 bytes; makes it fit in one fewer disk sectors */
fwrite(out_framebuffer,sizeof(unsigned char),8184,stdout);
free(out_framebuffer);
free(in_framebuffer);
return 0;