/* Free all memory used by the read */ void PNGAPI png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr) { png_structp png_ptr = NULL; png_infop info_ptr = NULL, end_info_ptr = NULL; #ifdef PNG_USER_MEM_SUPPORTED png_free_ptr free_fn = NULL; png_voidp mem_ptr = NULL; #endif png_debug(1, "in png_destroy_read_struct"); if (png_ptr_ptr != NULL) png_ptr = *png_ptr_ptr; if (png_ptr == NULL) return; #ifdef PNG_USER_MEM_SUPPORTED free_fn = png_ptr->free_fn; mem_ptr = png_ptr->mem_ptr; #endif if (info_ptr_ptr != NULL) info_ptr = *info_ptr_ptr; if (end_info_ptr_ptr != NULL) end_info_ptr = *end_info_ptr_ptr; png_read_destroy(png_ptr, info_ptr, end_info_ptr); if (info_ptr != NULL) { #ifdef PNG_TEXT_SUPPORTED png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1); #endif #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn, (png_voidp)mem_ptr); #else png_destroy_struct((png_voidp)info_ptr); #endif *info_ptr_ptr = NULL; } if (end_info_ptr != NULL) { #ifdef PNG_READ_TEXT_SUPPORTED png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1); #endif #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn, (png_voidp)mem_ptr); #else png_destroy_struct((png_voidp)end_info_ptr); #endif *end_info_ptr_ptr = NULL; } if (png_ptr != NULL) { #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn, (png_voidp)mem_ptr); #else png_destroy_struct((png_voidp)png_ptr); #endif *png_ptr_ptr = NULL; } }
/* free all memory used by the read */ void png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr) { png_structp png_ptr = NULL; png_infop info_ptr = NULL, end_info_ptr = NULL; #ifdef PNG_USER_MEM_SUPPORTED png_free_ptr free_fn = NULL; #endif /* PNG_USER_MEM_SUPPORTED */ png_debug(1, "in png_destroy_read_struct\n"); /* save jump buffer and error functions */ if (png_ptr_ptr != NULL) png_ptr = *png_ptr_ptr; if (info_ptr_ptr != NULL) info_ptr = *info_ptr_ptr; if (end_info_ptr_ptr != NULL) end_info_ptr = *end_info_ptr_ptr; #ifdef PNG_USER_MEM_SUPPORTED free_fn = png_ptr->free_fn; #endif png_read_destroy(png_ptr, info_ptr, end_info_ptr); if (info_ptr != NULL) { #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED) png_free(png_ptr, info_ptr->text); #endif #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)info_ptr, free_fn); #else png_destroy_struct((png_voidp)info_ptr); #endif *info_ptr_ptr = (png_infop)NULL; } if (end_info_ptr != NULL) { #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED) png_free(png_ptr, end_info_ptr->text); #endif #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)end_info_ptr, free_fn); #else png_destroy_struct((png_voidp)end_info_ptr); #endif *end_info_ptr_ptr = (png_infop)NULL; } if (png_ptr != NULL) { #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)png_ptr, free_fn); #else png_destroy_struct((png_voidp)png_ptr); #endif *png_ptr_ptr = (png_structp)NULL; } }
int main(int argc, char **argv) { FILE *fin = NULL; FILE *fout = NULL; char header[8]; uint8_t *image_data = NULL; png_bytep *row_pointers = NULL; int i; if (argc < 3) ERR("too few arguments") if ((fin = fopen(argv[1], "rb")) == NULL) ERR("cannot open man, lame!"); printf("%s opened\n", argv[1]); fread(header, 1, 8, fin); if (png_check_sig(header, 8) == 0) ERR("not a valid .png image") png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) ERR("sdf"); png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); ERR2("fssdfs"); } if (setjmp(png_ptr->jmpbuf)) { png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return 0; } png_init_io(png_ptr, fin); png_set_sig_bytes(png_ptr, 8); png_read_info(png_ptr, info_ptr); printf("width : %d\n", info_ptr->width); printf("height : %d\n", info_ptr->height); printf("bpp : %d\n", info_ptr->bit_depth); printf("palette : %d entries\n", info_ptr->num_palette); printf("rowbytes : %d\n", info_ptr->rowbytes); /* fclose(fin); return 0; png_infop end_info = png_create_info_struct(png_ptr); if (!end_info) { png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); ERR2("fjsh"); } */ // optional call to update palette with transformations // png_start_read_image(png_ptr); // optional call to update the info structure // png_read_update_info(png_ptr, info_ptr); if ((image_data = (uint8_t*)malloc(info_ptr->rowbytes*info_ptr->height)) == NULL) { png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return 0; } row_pointers = (png_bytep*)malloc(sizeof(png_bytep)*info_ptr->height); for (i=0; i<info_ptr->height; ++i) row_pointers[i] = image_data + i*info_ptr->rowbytes; // the easiest way to read the image png_read_image(png_ptr, row_pointers); // void write_binimg(char *fname, uint8_t *data, png_color *pal_data, int pal_size, int width, int height, int bpp) char filename[256]; strcpy(filename, argv[2]); /* write_binimg(strcat(filename, ".raw"), image_data, info_ptr->palette, info_ptr->num_palette, info_ptr->width, info_ptr->height, info_ptr->bit_depth); */ write_2c(argv[2], image_data, info_ptr->palette, info_ptr->num_palette, info_ptr->width, info_ptr->height, info_ptr->bit_depth); // clean up after the read, and free any memory allocated png_read_destroy(png_ptr, info_ptr, (png_infop)0); // png_read_end(png_ptr, NULL); i_am_done: if (image_data != NULL) free(image_data); if (row_pointers != NULL) free(row_pointers); if (fin != NULL) fclose(fin); if (fout != NULL) fclose(fout); return 0; }
/* free all memory used by the read */ void PNGAPI png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr) { png_structp png_ptr = NULL; png_infop info_ptr = NULL, end_info_ptr = NULL; #ifdef PNG_USER_MEM_SUPPORTED png_free_ptr free_fn = NULL; png_voidp mem_ptr = NULL; #endif png_debug(1, "in png_destroy_read_struct\n"); /* save jump buffer and error functions */ if (png_ptr_ptr != NULL) png_ptr = *png_ptr_ptr; if (info_ptr_ptr != NULL) info_ptr = *info_ptr_ptr; if (end_info_ptr_ptr != NULL) end_info_ptr = *end_info_ptr_ptr; #ifdef PNG_USER_MEM_SUPPORTED free_fn = png_ptr->free_fn; mem_ptr = png_ptr->mem_ptr; #endif png_read_destroy(png_ptr, info_ptr, end_info_ptr); if (info_ptr != NULL) { #if defined(PNG_TEXT_SUPPORTED) png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1); #endif #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn, (png_voidp)mem_ptr); #else png_destroy_struct((png_voidp)info_ptr); #endif *info_ptr_ptr = (png_infop)NULL; } if (end_info_ptr != NULL) { #if defined(PNG_READ_TEXT_SUPPORTED) png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1); #endif #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn, (png_voidp)mem_ptr); #else png_destroy_struct((png_voidp)end_info_ptr); #endif *end_info_ptr_ptr = (png_infop)NULL; } if (png_ptr != NULL) { #ifdef PNG_USER_MEM_SUPPORTED png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn, (png_voidp)mem_ptr); #else png_destroy_struct((png_voidp)png_ptr); #endif *png_ptr_ptr = (png_structp)NULL; } }