From 554913e92736d9b764b3cdf9410e8f98d8d7ff91 Mon Sep 17 00:00:00 2001 From: Vince Weaver Date: Wed, 3 May 2017 22:21:19 -0400 Subject: [PATCH] png2gr: split up file in prep of code reuse --- gr-utils/Makefile | 14 ++-- gr-utils/loadpng.c | 168 +++++++++++++++++++++++++++++++++++++++++++++ gr-utils/loadpng.h | 2 + gr-utils/png2gr.c | 162 +++++-------------------------------------- 4 files changed, 195 insertions(+), 151 deletions(-) create mode 100644 gr-utils/loadpng.c create mode 100644 gr-utils/loadpng.h diff --git a/gr-utils/Makefile b/gr-utils/Makefile index d3637d59..dda823ef 100644 --- a/gr-utils/Makefile +++ b/gr-utils/Makefile @@ -1,12 +1,17 @@ include ../Makefile.inc +CFLAGS = -g -Wall -O2 + all: png2gr -png2gr: png2gr.o - $(CC) $(LFLAGS) -lpng -o png2gr png2gr.o +loadpng.o: loadpng.c loadpng.h + $(CC) $(CFLAGS) -c loadpng.c -png2gr.o: png2gr.c - $(CC) $(CFLAGS) -c png2gr.c +png2gr: png2gr.o loadpng.o + $(CC) $(LFLAGS) -lpng -o png2gr png2gr.o loadpng.o + +png2gr.o: png2gr.c loadpng.h + $(CC) $(CFLAGS) -c png2gr.c install: @@ -15,4 +20,3 @@ install: clean: rm -f *~ *.o png2gr - diff --git a/gr-utils/loadpng.c b/gr-utils/loadpng.c new file mode 100644 index 00000000..c139bd81 --- /dev/null +++ b/gr-utils/loadpng.c @@ -0,0 +1,168 @@ +#include +#include + +#include +#include +#include +#include +#include + +#include + +/* 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; + int debug=0; + unsigned char *image; + int width, height; + + 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/2; + *ysize=height; + +// 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); + } + +// 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 #include -#include +#include "loadpng.h" -/* TODO: */ -/* 40x48 images */ -/* 80x40, 80x48 double-highres images */ +/* Converts a PNG to a GR file you can BLOAD to 0x400 */ +/* HOWEVER you *never* want to do this in real life */ +/* as it will clobber important values in the memory holes */ int main(int argc, char **argv) { - int image[40][48]; - int x,y; - FILE *infile,*outfile; - int debug=0; + int row=0; + int col=0; + int enough=0; + int x; - memset(image,0,sizeof(int)*40*48); + unsigned char *image; + int xsize,ysize; + FILE *outfile; if (argc<3) { fprintf(stderr,"Usage:\t%s INFILE OUTFILE\n\n",argv[0]); @@ -33,149 +35,17 @@ int main(int argc, char **argv) { exit(-1); } - int width, height; -// png_byte color_type; - png_byte bit_depth; - - png_structp png_ptr; - png_infop info_ptr; -// int number_of_passes; - png_bytep * row_pointers; - - unsigned char header[8]; - - /* open file and test for it being a png */ - infile = fopen(argv[1], "rb"); - if (infile==NULL) { - fprintf(stderr,"Error! Could not open %s\n",argv[1]); + if (loadpng(argv[1],&image,&xsize,&ysize)<0) { + fprintf(stderr,"Error loading png!\n"); exit(-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",argv[1]); - exit(-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); -// 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); - } - -// 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; y0x3f8) break; enough++;