gr-sim: have the png2rle utils use common png load code

hopefully this doesn't break everything
This commit is contained in:
Vince Weaver 2019-09-04 15:49:53 -04:00
parent efb86b5cde
commit d857e16e7a
9 changed files with 102 additions and 561 deletions

View File

@ -10,11 +10,13 @@ all: png2gr png2gr_text png2rle png2lz4 png_to_40x48d png_to_40x96
loadpng.o: loadpng.c loadpng.h
$(CC) $(CFLAGS) -c loadpng.c
rle_common.o: rle_common.c rle_common.h
$(CC) $(CFLAGS) -c rle_common.c
###
png2gr: png2gr.o loadpng.o
$(CC) $(LFLAGS) -lpng -o png2gr png2gr.o loadpng.o
$(CC) $(LFLAGS) -o png2gr png2gr.o loadpng.o -lpng
png2gr.o: png2gr.c loadpng.h
$(CC) $(CFLAGS) -c png2gr.c
@ -22,24 +24,24 @@ png2gr.o: png2gr.c loadpng.h
###
png2gr_text: png2gr_text.o loadpng.o
$(CC) $(LFLAGS) -lpng -o png2gr_text png2gr_text.o loadpng.o
$(CC) $(LFLAGS) -o png2gr_text png2gr_text.o loadpng.o -lpng
png2gr_text.o: png2gr_text.c loadpng.h
$(CC) $(CFLAGS) -c png2gr_text.c
###
png2rle: png2rle.o loadpng.o
$(CC) $(LFLAGS) -lpng -o png2rle png2rle.o loadpng.o
png2rle: png2rle.o loadpng.o rle_common.o
$(CC) $(LFLAGS) -o png2rle png2rle.o loadpng.o rle_common.o -lpng
png2rle.o: png2rle.c loadpng.h
png2rle.o: png2rle.c loadpng.h rle_common.h
$(CC) $(CFLAGS) -c png2rle.c
###
png2lz4: png2lz4.o loadpng.o
$(CC) $(LFLAGS) -llz4 -lpng -o png2lz4 png2lz4.o loadpng.o
$(CC) $(LFLAGS) -o png2lz4 png2lz4.o loadpng.o -llz4 -lpng
png2lz4.o: png2lz4.c loadpng.h
$(CC) $(CFLAGS) -c png2lz4.c
@ -49,17 +51,17 @@ png2lz4.o: png2lz4.c loadpng.h
###
png_to_40x48d: png_to_40x48d.o
$(CC) $(LFLAGS) -lpng -o png_to_40x48d png_to_40x48d.o
$(CC) $(LFLAGS) -o png_to_40x48d png_to_40x48d.o -lpng
png_to_40x48d.o: png_to_40x48d.c
$(CC) $(CFLAGS) -c png_to_40x48d.c
###
png_to_40x96: png_to_40x96.o
$(CC) $(LFLAGS) -lpng -o png_to_40x96 png_to_40x96.o
png_to_40x96: png_to_40x96.o loadpng.o
$(CC) $(LFLAGS) -o png_to_40x96 png_to_40x96.o loadpng.o -lpng
png_to_40x96.o: png_to_40x96.c
png_to_40x96.o: png_to_40x96.c loadpng.h
$(CC) $(CFLAGS) -c png_to_40x96.c

View File

@ -1,4 +1,4 @@
/* Loads a 80x48 PNG image into a 40x48 Apple II layout */
/* Loads a 80x48 (or 40x48) PNG image into a 40x48 Apple II layout */
/* It's not interleaved like an actual Apple II */
/* But the top/bottom are pre-packed into a naive 40x24 array */
@ -13,7 +13,7 @@
#include <stdarg.h>
#include <png.h>
#include "loadpng.h"
static int convert_color(int color) {
@ -44,10 +44,16 @@ static int convert_color(int color) {
return c;
}
/* expects a PNG where the xsize is *2 */
int loadpng(char *filename, unsigned char **image_ptr, int *xsize, int *ysize) {
/* expects a PNG where the xsize is either 40 or 80 */
/* if it is 80, it skips every other */
int x,y;
/* why do that? when editing an image the aspect ratio looks better if */
/* it is an 80 wide picture */
int loadpng(char *filename, unsigned char **image_ptr, int *xsize, int *ysize,
int png_type) {
int x,y,ystart,yadd,xadd;
int color;
FILE *infile;
int debug=0;
@ -60,7 +66,6 @@ int loadpng(char *filename, unsigned char **image_ptr, int *xsize, int *ysize) {
png_infop info_ptr;
png_bytep *row_pointers;
png_byte color_type;
// int number_of_passes;
unsigned char header[8];
@ -98,19 +103,49 @@ int loadpng(char *filename, unsigned char **image_ptr, int *xsize, int *ysize) {
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
*xsize=width/2;
*ysize=height;
if (width==40) {
*xsize=40;
xadd=1;
}
else if (width==80) {
*xsize=40;
xadd=2;
}
else {
fprintf(stderr,"Unsupported width %d\n",width);
return -1;
}
if (png_type==PNG_WHOLETHING) {
*ysize=height;
ystart=0;
yadd=2;
}
else if (png_type==PNG_ODDLINES) {
*ysize=height/2;
ystart=1;
yadd=4;
}
else if (png_type==PNG_EVENLINES) {
*ysize=height/2;
ystart=0;
yadd=4;
}
else {
fprintf(stderr,"Unknown PNG type\n");
return -1;
}
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
if (debug) {
printf("PNG: width=%d height=%d depth=%d\n",width,height,bit_depth);
if (color_type==PNG_COLOR_TYPE_RGB) printf("Type RGB\n");
else if (color_type==PNG_COLOR_TYPE_RGB_ALPHA) printf("Type RGBA\n");
else if (color_type==PNG_COLOR_TYPE_PALETTE) printf("Type palette\n");
printf("Generating output size %d x %d\n",*xsize,*ysize);
}
// number_of_passes = png_set_interlace_handling(png_ptr);
@ -118,6 +153,7 @@ int loadpng(char *filename, unsigned char **image_ptr, int *xsize, int *ysize) {
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
for (y=0; y<height; y++) {
/* FIXME: do we ever free these? */
row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));
}
@ -125,7 +161,8 @@ int loadpng(char *filename, unsigned char **image_ptr, int *xsize, int *ysize) {
fclose(infile);
image=calloc(width*height/2,sizeof(unsigned char));
/* FIXME: this should be 40x24 max??? */
image=calloc(width*height,sizeof(unsigned char));
if (image==NULL) {
fprintf(stderr,"Memory error!\n");
return -1;
@ -133,13 +170,13 @@ int loadpng(char *filename, unsigned char **image_ptr, int *xsize, int *ysize) {
out_ptr=image;
if (color_type==PNG_COLOR_TYPE_RGB_ALPHA) {
for(y=0;y<height;y+=2) {
for(x=0;x<width/2;x++) {
for(y=ystart;y<height;y+=yadd) {
for(x=0;x<width;x+=xadd) {
/* top color */
color= (row_pointers[y][x*2*4]<<16)+
(row_pointers[y][x*2*4+1]<<8)+
(row_pointers[y][x*2*4+2]);
color= (row_pointers[y][x*xadd*4]<<16)+
(row_pointers[y][x*xadd*4+1]<<8)+
(row_pointers[y][x*xadd*4+2]);
if (debug) {
printf("%x ",color);
}
@ -147,9 +184,9 @@ int loadpng(char *filename, unsigned char **image_ptr, int *xsize, int *ysize) {
a2_color=convert_color(color);
/* bottom color */
color= (row_pointers[y+1][x*2*4]<<16)+
(row_pointers[y+1][x*2*4+1]<<8)+
(row_pointers[y+1][x*2*4+2]);
color= (row_pointers[y+1][x*xadd*4]<<16)+
(row_pointers[y+1][x*xadd*4+1]<<8)+
(row_pointers[y+1][x*xadd*4+2]);
if (debug) {
printf("%x ",color);
}
@ -163,15 +200,15 @@ int loadpng(char *filename, unsigned char **image_ptr, int *xsize, int *ysize) {
}
}
else if (color_type==PNG_COLOR_TYPE_PALETTE) {
for(y=0;y<height;y+=2) {
for(x=0;x<width/2;x++) {
for(y=ystart;y<height;y+=yadd) {
for(x=0;x<width;x+=xadd) {
if (bit_depth==8) {
/* top color */
a2_color=row_pointers[y][x*2];
a2_color=row_pointers[y][x];
/* bottom color */
color=row_pointers[y+1][x*2];
color=row_pointers[y+(yadd/2)][x];
a2_color|=(color<<4);
@ -184,10 +221,18 @@ int loadpng(char *filename, unsigned char **image_ptr, int *xsize, int *ysize) {
}
else if (bit_depth==4) {
/* top color */
a2_color=(row_pointers[y][x])&0xf;
a2_color=row_pointers[y][x/2];
if (x%2) {
a2_color=(a2_color>>4);
}
a2_color&=0xf;
/* bottom color */
color=(row_pointers[y+1][x])&0xf;
color=row_pointers[y+(yadd/2)][x/2];
if (x%2) {
color=(color>>4);
}
color&=0xf;
a2_color|=(color<<4);

View File

@ -1,2 +1,7 @@
int loadpng(char *filename, unsigned char **image_ptr, int *xsize, int *ysize);
#define PNG_WHOLETHING 0
#define PNG_ODDLINES 1
#define PNG_EVENLINES 2
int loadpng(char *filename, unsigned char **image_ptr, int *xsize, int *ysize,
int png_type);

View File

@ -35,7 +35,7 @@ int main(int argc, char **argv) {
exit(-1);
}
if (loadpng(argv[1],&image,&xsize,&ysize)<0) {
if (loadpng(argv[1],&image,&xsize,&ysize,PNG_WHOLETHING)<0) {
fprintf(stderr,"Error loading png!\n");
exit(-1);
}

View File

@ -29,7 +29,7 @@ int main(int argc, char **argv) {
outfile=stdout;
if (loadpng(argv[1],&image,&xsize,&ysize)<0) {
if (loadpng(argv[1],&image,&xsize,&ysize,PNG_WHOLETHING)<0) {
fprintf(stderr,"Error loading png!\n");
exit(-1);
}

View File

@ -152,7 +152,7 @@ int main(int argc, char **argv) {
out_type=OUTPUT_RAW;
}
if (loadpng(argv[2],&image,&xsize,&ysize)<0) {
if (loadpng(argv[2],&image,&xsize,&ysize,PNG_WHOLETHING)<0) {
fprintf(stderr,"Error loading png!\n");
exit(-1);
}

View File

@ -8,10 +8,7 @@
#include <stdarg.h>
#include "loadpng.h"
#define OUTPUT_C 0
#define OUTPUT_ASM 1
#define OUTPUT_RAW 2
#include "rle_common.h"
@ -21,201 +18,6 @@
/*****************************************/
static int print_run(int count, int out_type, int run, int last) {
int size=0;
if (count==0) {
if (out_type==OUTPUT_C) {
printf("\n\t");
}
else if (out_type==OUTPUT_ASM) {
printf("\n\t.byte ");
}
}
else {
if (out_type==OUTPUT_C) {
}
else if (out_type==OUTPUT_ASM) {
printf(", ");
}
}
if (run==1) {
if ((last&0xf0)==0xA0) {
if (out_type==OUTPUT_C) {
printf("0x%02X,0x%02X,0x%02X,",0xa0,1,last);
}
else if (out_type==OUTPUT_ASM) {
printf("$%02X,$%02X,$%02X",0xa0,1,last);
}
else {
printf("%c%c%c",0xa0,1,last);
}
size+=3;
}
else {
if (out_type==OUTPUT_C) {
printf("0x%02X,",last);
}
else if (out_type==OUTPUT_ASM) {
printf("$%02X",last);
}
else {
printf("%c",last);
}
size++;
}
}
if (run==2) {
if ((last&0xf0)==0xA0) {
if (out_type==OUTPUT_C) {
printf("0x%02X,0x%02X,0x%02X,",0xa0,2,last);
}
else if (out_type==OUTPUT_ASM) {
printf("$%02X,$%02X,$%02X",0xa0,2,last);
}
else {
printf("%c%c%c",0xa0,2,last);
}
size+=3;
}
else {
if (out_type==OUTPUT_C) {
printf("0x%02X,0x%02X,",last,last);
}
else if (out_type==OUTPUT_ASM) {
printf("$%02X,$%02X",last,last);
}
else {
printf("%c",last);
printf("%c",last);
}
size+=2;
}
}
if ((run>2) && (run<16)) {
if (out_type==OUTPUT_C) {
printf("0x%02X,0x%02X,",0xA0|run,last);
}
else if (out_type==OUTPUT_ASM) {
printf("$%02X,$%02X",0xA0|run,last);
}
else {
printf("%c",0xA0|run);
printf("%c",last);
}
size+=2;
}
if (run>=16) {
if (out_type==OUTPUT_C) {
printf("0x%02X,0x%02X,0x%02X,",0xA0,run,last);
}
else if (out_type==OUTPUT_ASM) {
printf("$%02X,$%02X,$%02X",0xA0,run,last);
}
else {
printf("%c",0xA0);
printf("%c",run);
printf("%c",last);
}
size+=3;
}
return size;
}
int rle_smaller(int out_type, char *varname,
int xsize,int ysize, unsigned char *image) {
int run=0;
int x;
int last=-1,next;
int size=0;
int count=0;
x=0;
/* Write out xsize and ysize */
if (out_type==OUTPUT_C) {
fprintf(stdout,"unsigned char %s[]={\n",varname);
fprintf(stdout,"\t0x%X, /* ysize=%d */",xsize,ysize);
}
else if (out_type==OUTPUT_ASM) {
fprintf(stdout,"%s:",varname);
fprintf(stdout,"\t.byte $%X ; ysize=%d",xsize,ysize);
} else {
fprintf(stdout,"%c",xsize);
}
size+=1;
/* Get first top/bottom color pair */
last=image[x];
run++;
x++;
while(1) {
/* get next top/bottom color pair */
next=image[x];
// if ((next&0xf0)==0xA0) {
// fprintf(stderr,"Warning! Using color A (grey2)!\n");
// next&=~0xf0;
// next|=0x50; // substitute grey1
// }
/* If color change (or too big) then output our run */
if ((next!=last) || (run>254)) {
size+=print_run(count,out_type,run,last);
count++;
run=0;
last=next;
}
x++;
/* If we reach the end */
if (x>=xsize*(ysize/2)) {
run++;
size+=print_run(count,out_type,run,last);
break;
}
run++;
if (count>6) count=0;
}
/* Print closing marker */
if (out_type==OUTPUT_C) {
fprintf(stdout,"0xA1,");
fprintf(stdout,"\t};\n");
} else if (out_type==OUTPUT_ASM) {
fprintf(stdout,"\n\t.byte $A1\n");
} else {
fprintf(stdout,"%c",0xA1);
}
size+=1;
return size;
}
/* Converts a PNG to RLE compressed data */
@ -245,7 +47,7 @@ int main(int argc, char **argv) {
out_type=OUTPUT_RAW;
}
if (loadpng(argv[2],&image,&xsize,&ysize)<0) {
if (loadpng(argv[2],&image,&xsize,&ysize,PNG_WHOLETHING)<0) {
fprintf(stderr,"Error loading png!\n");
exit(-1);
}
@ -266,122 +68,3 @@ int main(int argc, char **argv) {
/* Converts a PNG to RLE compressed data */
int rle_original(int out_type, char *varname,
int xsize,int ysize, unsigned char *image) {
int run=0;
int x;
int last=-1,next;
int size=0;
int count=0;
x=0;
/* Write out xsize and ysize */
if (out_type==OUTPUT_C) {
fprintf(stdout,"unsigned char %s[]={\n",varname);
fprintf(stdout,"\t0x%X,0x%X,",xsize,ysize);
}
else if (out_type==OUTPUT_ASM) {
fprintf(stdout,"%s:",varname);
fprintf(stdout,"\t.byte $%X,$%X",xsize,ysize);
}
size+=2;
/* Get first top/bottom color pair */
last=image[x];
run++;
x++;
while(1) {
/* get next top/bottom color pair */
next=image[x];
/* If color change (or too big) then output our run */
/* Note 0xff for run length is special case meaning "finished" */
if ((next!=last) || (run>253)) {
/* handle new line */
if (out_type==OUTPUT_C) {
if (count==0) {
printf("\n\t");
}
}
else if (out_type==OUTPUT_ASM) {
if (count==0) {
printf("\n\t.byte ");
}
else {
printf(", ");
}
}
if (out_type==OUTPUT_C) {
printf("0x%02X,0x%02X, ",run,last);
}
else if (out_type==OUTPUT_ASM) {
printf("$%02X,$%02X",run,last);
}
else {
printf("%c",run);
printf("%c",last);
}
size+=2;
count++;
run=0;
last=next;
}
x++;
/* If we reach the end */
if (x>=xsize*(ysize/2)) {
run++;
/* print tailing value */
if (run!=0) {
if (out_type==OUTPUT_C) {
printf("0x%02X,0x%02X, ",run,last);
}
else if (out_type==OUTPUT_ASM) {
if (count==0) {
printf("\n\t.byte ");
}
else {
printf(", ");
}
printf("$%02X,$%02X\n",run,last);
} else {
printf("%c",run);
printf("%c",last);
}
size+=2;
}
break;
}
run++;
if (count>6) count=0;
}
/* Print closing marker */
if (out_type==OUTPUT_C) {
fprintf(stdout,"0xFF,0xFF,");
fprintf(stdout,"\t};\n");
} else if (out_type==OUTPUT_ASM) {
fprintf(stdout,"\t.byte $FF,$FF\n");
}
size+=2;
return size;
}

View File

@ -12,208 +12,11 @@
#include <png.h>
#define OUTPUT_C 0
#define OUTPUT_ASM 1
static int convert_color(int color) {
int c=0;
switch(color) {
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 */
default:
printf("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 high) {
int x,y;
int color;
FILE *infile;
int debug=0;
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 number_of_passes;
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/2;
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
if (debug) {
printf("PNG: width=%d height=%d depth=%d\n",width,height,bit_depth);
if (color_type==PNG_COLOR_TYPE_RGB) printf("Type RGB\n");
else if (color_type==PNG_COLOR_TYPE_RGB_ALPHA) printf("Type RGBA\n");
else if (color_type==PNG_COLOR_TYPE_PALETTE) printf("Type palette\n");
}
// number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
for (y=0; y<height; y++) {
row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));
}
png_read_image(png_ptr, row_pointers);
fclose(infile);
image=calloc(width*height,sizeof(unsigned char));
if (image==NULL) {
fprintf(stderr,"Memory error!\n");
return -1;
}
out_ptr=image;
if (color_type==PNG_COLOR_TYPE_RGB_ALPHA) {
for(y=high;y<height;y+=4) {
for(x=0;x<width;x++) {
/* top color */
color= (row_pointers[y][x*4]<<16)+
(row_pointers[y][x*4+1]<<8)+
(row_pointers[y][x*4+2]);
if (debug) {
printf("%x ",color);
}
a2_color=convert_color(color);
/* bottom color */
color= (row_pointers[y+2][x*4]<<16)+
(row_pointers[y+2][x*4+1]<<8)+
(row_pointers[y+2][x*4+2]);
if (debug) {
printf("%x ",color);
}
a2_color|=(convert_color(color)<<4);
*out_ptr=a2_color;
out_ptr++;
}
if (debug) printf("\n");
}
}
else if (color_type==PNG_COLOR_TYPE_PALETTE) {
for(y=high;y<height;y+=4) {
for(x=0;x<width;x++) {
/* top color */
a2_color=row_pointers[y][x];
/* bottom color */
color=row_pointers[y+2][x];
a2_color|=(color<<4);
if (debug) {
printf("%x ",a2_color);
}
*out_ptr=a2_color;
out_ptr++;
}
if (debug) printf("\n");
}
}
else {
printf("Unknown color type\n");
}
/* Stripe test image */
// for(x=0;x<40;x++) for(y=0;y<40;y++) image[(y*width)+x]=y%16;
/*
Addr Row /80 %40
$400 0 0 0 0
$428 28 16 0
$450 50 32 0
$480 80 2 1
$4A8 a8 18 1
$4D0 d0 34 1
$500 100 3 2
0,0 0,1 0,2....0,39 16,0 16,1 ....16,39 32,0..32,39, X X X X X X X X
*/
*image_ptr=image;
return 0;
}
#include "loadpng.h"
#define OUTPUT_C 0
#define OUTPUT_ASM 1
#define OUTPUT_RAW 2
/*****************************************/
/* \/ \/ */
@ -394,7 +197,7 @@ int main(int argc, char **argv) {
out_type=OUTPUT_ASM;
}
if (loadpng(argv[2],&image,&xsize,&ysize,1)<0) {
if (loadpng(argv[2],&image,&xsize,&ysize,PNG_ODDLINES)<0) {
fprintf(stderr,"Error loading png!\n");
exit(-1);
}
@ -409,7 +212,7 @@ int main(int argc, char **argv) {
fprintf(stderr,"Size %d bytes\n",size);
if (loadpng(argv[2],&image,&xsize,&ysize,0)<0) {
if (loadpng(argv[2],&image,&xsize,&ysize,PNG_EVENLINES)<0) {
fprintf(stderr,"Error loading png!\n");
exit(-1);
}

View File

@ -215,6 +215,7 @@ int rle_smaller(int out_type, char *varname,
return size;
}
#if 0
/* Converts a PNG to RLE compressed data */
@ -335,3 +336,5 @@ int rle_original(int out_type, char *varname,
return size;
}
#endif