png2hgr: sort of handle half-pixel colors better

This commit is contained in:
Vince Weaver 2018-08-25 01:20:52 -04:00
parent ec27c1d645
commit eefdf16b8d

View File

@ -1,5 +1,12 @@
/* Converts 280x192 8-bit PNG file with correct palette to Apple II HGR */ /* Converts 280x192 8-bit PNG file with correct palette to Apple II HGR */
#define VERSION "0.0.1" #define VERSION "0.0.1"
#include <stdio.h> /* For FILE I/O */ #include <stdio.h> /* For FILE I/O */
@ -91,7 +98,7 @@ int loadpng(char *filename,
png_infop info_ptr; png_infop info_ptr;
png_bytep *row_pointers; png_bytep *row_pointers;
png_byte color_type; png_byte color_type;
int row_bytes; int row_bytes,bytes_per_pixel;
unsigned char header[8]; unsigned char header[8];
@ -175,6 +182,12 @@ int loadpng(char *filename,
row_bytes = png_get_rowbytes(png_ptr, info_ptr); row_bytes = png_get_rowbytes(png_ptr, info_ptr);
// *pChannels = (int)png_get_channels(png_ptr, info_ptr); // *pChannels = (int)png_get_channels(png_ptr, info_ptr);
bytes_per_pixel=row_bytes/width;
if (debug) {
fprintf(stderr,"Rowbytes=%d bytes per pixel=%d\n",
row_bytes,row_bytes/width);
}
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height); row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
for (y=0; y<height; y++) { for (y=0; y<height; y++) {
@ -193,9 +206,9 @@ int loadpng(char *filename,
for(y=0;y<height;y++) { for(y=0;y<height;y++) {
for(x=0;x<width;x++) { for(x=0;x<width;x++) {
color= (row_pointers[y][x*3]<<16)+ color= (row_pointers[y][x*bytes_per_pixel]<<16)+
(row_pointers[y][x*3+1]<<8)+ (row_pointers[y][x*bytes_per_pixel+1]<<8)+
(row_pointers[y][x*3+2]); (row_pointers[y][x*bytes_per_pixel+2]);
// if (debug) { // if (debug) {
// fprintf(stderr,"%x ",color); // fprintf(stderr,"%x ",color);
// } // }
@ -270,7 +283,8 @@ static int color_low(int color) {
return 0; return 0;
} }
static int colors_to_bytes(unsigned char colors[7],
static int colors_to_bytes(unsigned char colors[14],
unsigned char *byte1, unsigned char *byte1,
unsigned char *byte2) { unsigned char *byte2) {
@ -285,49 +299,81 @@ static int colors_to_bytes(unsigned char colors[7],
highbit1+=color_high(colors[1]); highbit1+=color_high(colors[1]);
highbit1+=color_high(colors[2]); highbit1+=color_high(colors[2]);
highbit1+=color_high(colors[3]); highbit1+=color_high(colors[3]);
highbit1+=color_high(colors[4]);
highbit1+=color_high(colors[5]);
highbit1+=color_high(colors[6]);
highbit2+=color_high(colors[3]); highbit2+=color_high(colors[7]);
highbit2+=color_high(colors[4]); highbit2+=color_high(colors[8]);
highbit2+=color_high(colors[5]); highbit2+=color_high(colors[9]);
highbit2+=color_high(colors[6]); highbit2+=color_high(colors[10]);
highbit2+=color_high(colors[11]);
highbit2+=color_high(colors[12]);
highbit2+=color_high(colors[13]);
lowbit1+=color_low(colors[0]); lowbit1+=color_low(colors[0]);
lowbit1+=color_low(colors[1]); lowbit1+=color_low(colors[1]);
lowbit1+=color_low(colors[2]); lowbit1+=color_low(colors[2]);
lowbit1+=color_low(colors[3]); lowbit1+=color_low(colors[3]);
lowbit1+=color_low(colors[4]);
lowbit1+=color_low(colors[5]);
lowbit1+=color_low(colors[6]);
lowbit2+=color_low(colors[3]); lowbit2+=color_low(colors[7]);
lowbit2+=color_low(colors[4]); lowbit2+=color_low(colors[8]);
lowbit2+=color_low(colors[5]); lowbit2+=color_low(colors[9]);
lowbit2+=color_low(colors[6]); lowbit2+=color_low(colors[10]);
lowbit2+=color_low(colors[11]);
lowbit2+=color_low(colors[12]);
lowbit2+=color_low(colors[13]);
if (highbit1==4) hb1=1;
else if (lowbit1==4) hb1=0;
if (highbit1==7) hb1=1;
else if (lowbit1==7) hb1=0;
else { else {
error=1; error=1;
if (highbit1>lowbit1) hb1=1; if (highbit1>lowbit1) hb1=1;
else hb1=0; else hb1=0;
} }
if (highbit2==4) hb2=1; if (highbit2==7) hb2=1;
else if (lowbit2==4) hb2=0; else if (lowbit2==7) hb2=0;
else { else {
error=2; error=2;
if (highbit2>lowbit2) hb2=1; if (highbit2>lowbit2) hb2=1;
else hb2=0; else hb2=0;
} }
/*
0 0 0 0 -> 00 00
1 1 1 1 -> 01 01
2 2 2 2 -> 10 10
3 3 3 3 -> 11 11
1 3 3 1 -> 01 11
*byte1|=(colors[0]&3); 1 1 2 2 3 3 0 -> 0 11 10 01
*byte1|=(colors[1]&3)<<2;
*byte1|=(colors[2]&3)<<4; */
*byte1|=(colors[3]&1)<<6;
*byte1|=(colors[0]&1)<<0;
*byte1|=(colors[1]&2)<<0;
*byte1|=(colors[2]&1)<<2;
*byte1|=(colors[3]&2)<<2;
*byte1|=(colors[4]&1)<<4;
*byte1|=(colors[5]&2)<<4;
*byte1|=(colors[6]&1)<<6;
*byte1|=hb1<<7; *byte1|=hb1<<7;
*byte2|=(colors[6]&3)<<5; *byte2|=(colors[7]&2)>>1;
*byte2|=(colors[5]&3)<<3; *byte2|=(colors[8]&1)<<1;
*byte2|=(colors[4]&3)<<1; *byte2|=(colors[9]&2)<<1;
*byte2|=(colors[3]&2)>>1; *byte2|=(colors[10]&1)<<3;
*byte2|=(colors[11]&2)<<3;
*byte2|=(colors[12]&1)<<5;
*byte2|=(colors[13]&2)<<5;
*byte2|=hb2<<7; *byte2|=hb2<<7;
return error; return error;
@ -339,15 +385,15 @@ static unsigned char apple2_image[8192];
int main(int argc, char **argv) { int main(int argc, char **argv) {
int xsize=0,ysize=0,error; int xsize=0,ysize=0,error;
int c,x,y,z,color1,color2; int c,x,y,z,color1;
unsigned char *image; unsigned char *image;
unsigned char byte1,byte2,colors[7]; unsigned char byte1,byte2,colors[14];
char *filename; char *filename;
/* Parse command line arguments */ /* Parse command line arguments */
while ( (c=getopt(argc, argv, "hv") ) != -1) { while ( (c=getopt(argc, argv, "hvd") ) != -1) {
switch(c) { switch(c) {
@ -357,6 +403,9 @@ int main(int argc, char **argv) {
case 'v': case 'v':
print_help(argv[0],1); print_help(argv[0],1);
break; break;
case 'd':
debug=1;
break;
default: default:
print_help(argv[0],0); print_help(argv[0],0);
break; break;
@ -381,14 +430,13 @@ int main(int argc, char **argv) {
for(y=0;y<192;y++) { for(y=0;y<192;y++) {
for(x=0;x<20;x++) { for(x=0;x<20;x++) {
for(z=0;z<7;z++) { for(z=0;z<14;z++) {
color1=image[y*280+x*14+z*2]; color1=image[y*280+x*14+z];
color2=image[y*280+x*14+z*2+1]; // if (color1!=color2) {
if (color1!=color2) { // fprintf(stderr,"Warning: color at %d x %d doesn't match\n",
fprintf(stderr,"Error, color at %d x %d doesn't match\n", // x*14+z*2,y);
x*14+z*2,y); //
// }
}
colors[z]=color1; colors[z]=color1;
} }
error=colors_to_bytes(colors,&byte1,&byte2); error=colors_to_bytes(colors,&byte1,&byte2);