/* Converts 280x192 8-bit PNG file with correct palette to Apple II HGR */ #define VERSION "0.0.1" #include /* For FILE I/O */ #include /* For strncmp */ #include /* for open() */ #include /* for lseek() */ #include /* for file modes */ #include /* free() */ #include #define OUTPUT_C 0 #define OUTPUT_ASM 1 #define OUTPUT_RAW 2 /* If you want default black a different color try the below */ /* Useful if you are trying to xdraw to get red/blue */ #if 0 /* Default is BLACK0 */ #define DEFAULT_BLACK -1 #else /* DEFAULT is BLACK1 */ #define DEFAULT_BLACK 1 #endif static int debug=0; static int convert_color(int color) { int c=0; switch(color) { #if 0 case 0x000000: c=0; break; /* black */ case 0xe31e60: c=1; break; /* magenta */ case 0x604ebd: c=2; break; /* dark blue */ case 0xff44fd: c=3; break; /* purple */ case 0x00a360: c=4; break; /* dark green */ case 0x9c9c9c: c=5; break; /* grey 1 */ case 0x14cffd: c=6; break; /* medium blue */ case 0xd0c3ff: c=7; break; /* light blue */ case 0x607203: c=8; break; /* brown */ case 0xff6a3c: c=9; break; /* orange */ case 0x9d9d9d: c=10; break; /* grey 2 */ case 0xffa0d0: c=11; break; /* pink */ case 0x14f53c: c=12; break; /* bright green */ case 0xd0dd8d: c=13; break; /* yellow */ case 0x72ffd0: c=14; break; /* aqua */ case 0xffffff: c=15; break; /* white */ #endif /* These use the questionable palette my older code used */ /* Also handle the newer one */ /* Bitflipped because HGR is backwards, woz is crazy */ case 0x000000: c=0; break; /* black */ case 0x1bcb01: c=2; break; /* bright green */ case 0x14f53c: c=2; break; /* bright green */ case 0xe434fe: c=1; break; /* magenta */ case 0xe31e60: c=1; break; /* magenta */ case 0xffffff: c=3; break; /* white */ case 0xcd5b01: c=6; break; /* orange */ case 0xff6a3c: c=6; break; /* orange */ case 0x1b9afe: c=5; break; /* medium blue */ case 0x14cffd: c=5; break; /* medium blue */ default: fprintf(stderr,"Unknown color %x\n",color); break; } return c; } /* expects a PNG where the xsize is *2 */ int loadpng(char *filename, unsigned char **image_ptr, int *xsize, int *ysize) { int x,y; int color; FILE *infile; unsigned char *image,*out_ptr; int width, height; int a2_color; png_byte bit_depth; png_structp png_ptr; png_infop info_ptr; png_bytep *row_pointers; png_byte color_type; int row_bytes; unsigned char header[8]; /* open file and test for it being a png */ infile = fopen(filename, "rb"); if (infile==NULL) { fprintf(stderr,"Error! Could not open %s\n",filename); return -1; } /* Check the header */ fread(header, 1, 8, infile); if (png_sig_cmp(header, 0, 8)) { fprintf(stderr,"Error! %s is not a PNG file\n",filename); return -1; } /* initialize stuff */ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { fprintf(stderr,"Error create_read_struct\n"); exit(-1); } info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { fprintf(stderr,"Error png_create_info_struct\n"); exit(-1); } png_init_io(png_ptr, infile); png_set_sig_bytes(png_ptr, 8); png_read_info(png_ptr, info_ptr); width = png_get_image_width(png_ptr, info_ptr); height = png_get_image_height(png_ptr, info_ptr); *xsize=width; *ysize=height; color_type = png_get_color_type(png_ptr, info_ptr); bit_depth = png_get_bit_depth(png_ptr, info_ptr); if (width!=280) { fprintf(stderr,"Unknown width %d\n",width); return -1; } if (height!=192) { fprintf(stderr,"Unknown height %d\n",height); return -1; } image=calloc(width*height,sizeof(unsigned char)); if (image==NULL) { fprintf(stderr,"Error allocating image\n"); return -1; } if (debug) { fprintf(stderr,"PNG: width=%d height=%d depth=%d\n", width,height,bit_depth); if (color_type==PNG_COLOR_TYPE_RGB) { fprintf(stderr,"Type RGB\n"); } else if (color_type==PNG_COLOR_TYPE_RGB_ALPHA) { fprintf(stderr,"Type RGBA\n"); } else if (color_type==PNG_COLOR_TYPE_PALETTE) { fprintf(stderr,"Type palette\n"); } } /* If palette, expand to RGB automatically */ if (color_type == PNG_COLOR_TYPE_PALETTE) { png_set_expand(png_ptr); } png_read_update_info(png_ptr, info_ptr); row_bytes = png_get_rowbytes(png_ptr, info_ptr); // *pChannels = (int)png_get_channels(png_ptr, info_ptr); row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height); for (y=0; y2) || (color==0)) return 1; return 0; } static int color_low(int color) { if ((color<4) || (color==7)) return 1; return 0; } static int colors_to_bytes(unsigned char colors[7], unsigned char *byte1, unsigned char *byte2) { int highbit1=0,highbit2=0,lowbit1=0,lowbit2=0; int hb1,hb2; int error=0; *byte1=0; *byte2=0; highbit1+=color_high(colors[0]); highbit1+=color_high(colors[1]); highbit1+=color_high(colors[2]); highbit1+=color_high(colors[3]); highbit2+=color_high(colors[3]); highbit2+=color_high(colors[4]); highbit2+=color_high(colors[5]); highbit2+=color_high(colors[6]); lowbit1+=color_low(colors[0]); lowbit1+=color_low(colors[1]); lowbit1+=color_low(colors[2]); lowbit1+=color_low(colors[3]); lowbit2+=color_low(colors[3]); lowbit2+=color_low(colors[4]); lowbit2+=color_low(colors[5]); lowbit2+=color_low(colors[6]); if (highbit1==4) hb1=1; else if (lowbit1==4) hb1=0; else { error=1; if (highbit1>lowbit1) hb1=1; else hb1=0; } if (highbit2==4) hb2=1; else if (lowbit2==4) hb2=0; else { error=2; if (highbit2>lowbit2) hb2=1; else hb2=0; } *byte1|=(colors[0]&3); *byte1|=(colors[1]&3)<<2; *byte1|=(colors[2]&3)<<4; *byte1|=(colors[3]&1)<<6; *byte1|=hb1<<7; *byte2|=(colors[6]&3)<<5; *byte2|=(colors[5]&3)<<3; *byte2|=(colors[4]&3)<<1; *byte2|=(colors[3]&2)>>1; *byte2|=hb2<<7; return error; } static unsigned char apple2_image[8192]; int main(int argc, char **argv) { int xsize=0,ysize=0,error; int c,x,y,z,color1,color2; unsigned char *image; unsigned char byte1,byte2,colors[7]; char *filename; /* Parse command line arguments */ while ( (c=getopt(argc, argv, "hv") ) != -1) { switch(c) { case 'h': print_help(argv[0],0); break; case 'v': print_help(argv[0],1); break; default: print_help(argv[0],0); break; } } if (optind>=argc) { printf("ERROR: Was expecting filename!\n"); exit(1); } filename=strdup(argv[optind]); memset(apple2_image,0,8192); if (loadpng(filename,&image,&xsize,&ysize)<0) { fprintf(stderr,"Error loading png!\n"); exit(-1); } fprintf(stderr,"Loaded image %d by %d\n",xsize,ysize); for(y=0;y<192;y++) { for(x=0;x<20;x++) { for(z=0;z<7;z++) { color1=image[y*280+x*14+z*2]; color2=image[y*280+x*14+z*2+1]; if (color1!=color2) { fprintf(stderr,"Error, color at %d x %d doesn't match\n", x*14+z*2,y); } colors[z]=color1; } error=colors_to_bytes(colors,&byte1,&byte2); if (error!=0) { fprintf(stderr,"Warning: mixing colors at %d x %d\n", x*14+error*7,y); } apple2_image[hgr_offset(y)+(x*2)+0]=byte1; apple2_image[hgr_offset(y)+(x*2)+1]=byte2; } } fwrite(apple2_image,8192,sizeof(unsigned char),stdout); return 0; }