diff options
-rw-r--r-- | png.c | 108 |
1 files changed, 0 insertions, 108 deletions
@@ -114,111 +114,3 @@ void write_png(FILE *file, void *image, int width, int height, flam3_img_comment } -#define SIG_CHECK_SIZE 8 - -unsigned char *read_png(FILE *ifp, int *width, int *height) { - unsigned char sig_buf [SIG_CHECK_SIZE]; - png_struct *png_ptr; - png_info *info_ptr; - png_byte **png_image = NULL; - unsigned int linesize, x, y; - unsigned char *p, *q; - - if (fread (sig_buf, 1, SIG_CHECK_SIZE, ifp) != SIG_CHECK_SIZE) { - fprintf (stderr, "input file empty or too short\n"); - return 0; - } - if (png_sig_cmp (sig_buf, (png_size_t) 0, (png_size_t) SIG_CHECK_SIZE) != 0) { - fprintf (stderr, "input file not a PNG file\n"); - return 0; - } - - png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (png_ptr == NULL) { - fprintf (stderr, "cannot allocate LIBPNG structure\n"); - return 0; - } - if (setjmp(png_jmpbuf(png_ptr))) { - if (png_image) { - for (y = 0 ; y < png_get_image_height(png_ptr, info_ptr) ; y++) - free (png_image[y]); - free (png_image); - } - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); - perror("reading file"); - return 0; - } - info_ptr = png_create_info_struct (png_ptr); - if (info_ptr == NULL) { - png_destroy_read_struct (&png_ptr, (png_infopp)NULL, (png_infopp)NULL); - fprintf (stderr, "cannot allocate LIBPNG structures\n"); - return 0; - } - - png_init_io (png_ptr, ifp); - png_set_sig_bytes (png_ptr, SIG_CHECK_SIZE); - png_read_info (png_ptr, info_ptr); - - if (8 != png_get_bit_depth(png_ptr, info_ptr)) { - fprintf(stderr, "bit depth type must be 8, not %d.\n", - png_get_bit_depth(png_ptr, info_ptr)); - return 0; - } - - *width = png_get_image_width(png_ptr, info_ptr); - *height = png_get_image_height(png_ptr, info_ptr); - p = q = malloc(4 * *width * *height); - png_image = (png_byte **)malloc (*height * sizeof (png_byte*)); - - linesize = *width; - switch (png_get_color_type(png_ptr, info_ptr)) { - case PNG_COLOR_TYPE_RGB: - linesize *= 3; - break; - case PNG_COLOR_TYPE_RGBA: - linesize *= 4; - break; - default: - fprintf(stderr, "color type must be RGB or RGBA not %d.\n", - png_get_color_type(png_ptr, info_ptr)); - return 0; - } - - for (y = 0 ; y < *height ; y++) { - png_image[y] = malloc (linesize); - } - png_read_image (png_ptr, png_image); - png_read_end (png_ptr, info_ptr); - - for (y = 0 ; y < *height ; y++) { - unsigned char *s = png_image[y]; - for (x = 0 ; x < *width ; x++) { - - switch (png_get_color_type(png_ptr, info_ptr)) { - case PNG_COLOR_TYPE_RGB: - p[0] = s[0]; - p[1] = s[1]; - p[2] = s[2]; - p[3] = 255; - s += 3; - p += 4; - break; - case PNG_COLOR_TYPE_RGBA: - p[0] = s[0]; - p[1] = s[1]; - p[2] = s[2]; - p[3] = s[3]; - s += 4; - p += 4; - break; - } - } - } - - for (y = 0 ; y < *height ; y++) - free (png_image[y]); - free (png_image); - png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp)NULL); - - return q; -} |