diff options
author | John Hawthorn <jhawthor@uvic.ca> | 2008-05-22 11:50:44 -0700 |
---|---|---|
committer | John Hawthorn <jhawthor@uvic.ca> | 2008-05-22 11:50:44 -0700 |
commit | 72573e45e04541413fa50336a56ae8a88f8f0cb8 (patch) | |
tree | 698ec95ceb0357d33b0638edd6ab814a613d86bb | |
parent | f6fd9b38f5e1a1088646c86ac6abde0af3475e9c (diff) | |
download | mirror-meh-72573e45e04541413fa50336a56ae8a88f8f0cb8.tar.gz mirror-meh-72573e45e04541413fa50336a56ae8a88f8f0cb8.tar.bz2 mirror-meh-72573e45e04541413fa50336a56ae8a88f8f0cb8.zip |
basic png support
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/png.c | 61 |
2 files changed, 61 insertions, 2 deletions
@@ -7,7 +7,7 @@ OBJFILES := $(SRCFILES:%.c=%.o) DEPFILES := $(OBJFILES:%.o=%.d) CLEANFILES := $(CLEANFILES) $(DEPFILES) $(OBJFILES) meh CFLAGS := -O3 -Wall -g -ggdb -LIBS := -lX11 -ljpeg +LIBS := -lX11 -ljpeg -lpng meh: $(OBJFILES) $(CC) -o $@ $(OBJFILES) $(LIBS) @@ -1,6 +1,65 @@ +#include <png.h> + #include "meh.h" unsigned char *loadpng(FILE *infile, int *bufwidth, int *bufheight){ - return NULL; + png_structp png_ptr; + png_infop info_ptr; + png_infop end_info; + int numpasses = 1; + unsigned char *buf; + + if((png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL) + return NULL; + if((info_ptr = png_create_info_struct(png_ptr)) == NULL){ + png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); + return NULL; + } + if((end_info = png_create_info_struct(png_ptr)) == NULL){ + png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); + return NULL; + } + if(setjmp(png_jmpbuf(png_ptr))){ + png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); + return NULL; + } + + png_init_io(png_ptr, infile); + + /* Start reading */ + png_read_info(png_ptr, info_ptr); + buf = malloc(100); + { + png_byte color_type = png_get_color_type(png_ptr, info_ptr); + if(color_type == PNG_COLOR_TYPE_PALETTE) + png_set_palette_to_rgb(png_ptr); + if(color_type == PNG_COLOR_TYPE_GRAY && png_get_bit_depth(png_ptr, info_ptr) < 8) + png_set_gray_1_2_4_to_8(png_ptr); + if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) + png_set_tRNS_to_alpha(png_ptr); + if (png_get_bit_depth(png_ptr, info_ptr) == 16) + png_set_strip_16(png_ptr); + if(color_type & PNG_COLOR_MASK_ALPHA) + png_set_strip_alpha(png_ptr); + if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) + png_set_gray_to_rgb(png_ptr); + if(png_get_interlace_type == PNG_INTERLACE_ADAM7) + numpasses = png_set_interlace_handling(png_ptr); + png_read_update_info(png_ptr, info_ptr); + } + { + int y; + *bufwidth = png_get_image_width(png_ptr, info_ptr); + *bufheight = png_get_image_height(png_ptr, info_ptr); + buf = malloc((*bufwidth) * (*bufheight) * 3); + printf("%ix%i\n", *bufwidth, *bufheight); + while(numpasses--){ + for(y = 0; y < *bufheight; y++) + png_read_row(png_ptr, &buf[y * (*bufwidth) * 3], NULL); + } + } + + printf("reached end\n"); + return buf; } |