#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