From 630dfaac914df94311641e5994a1798eaa0be031 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Mon, 9 Jun 2014 10:42:19 +0100 Subject: Add support for input images with redundant alpha channels. Some PNG exporters are daft. --- src/alphagen.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/alphagen.c b/src/alphagen.c index 6ced487..a8461c7 100644 --- a/src/alphagen.c +++ b/src/alphagen.c @@ -266,10 +266,9 @@ bool image_read_png(char *file_name, struct image *img) png_structp png_ptr; png_infop info_ptr; unsigned int sig_read = 0; - unsigned int row_data_width; png_uint_32 width, height; int bit_depth, color_type; - int i; + int y; FILE *fp; if ((fp = fopen(file_name, "rb")) == NULL) @@ -317,17 +316,36 @@ bool image_read_png(char *file_name, struct image *img) bit_depth = png_get_bit_depth(png_ptr, info_ptr); color_type = png_get_color_type(png_ptr, info_ptr); - if (color_type != PNG_COLOR_TYPE_RGB || bit_depth != 8) + if (color_type != PNG_COLOR_TYPE_RGB && + color_type != PNG_COLOR_TYPE_RGB_ALPHA) { + printf("Unsupported colour format: %d\n", color_type); + return false; + } + + if (bit_depth != 8) { + printf("Unsupported bit depth: %d\n", bit_depth); return false; + } if (!image_init(img, width, height, 3)) return false; png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr); - row_data_width = img->width * img->channels; - for (i = 0; i < img->height; i++) { - memcpy(img->d[i], row_pointers[i], row_data_width); + if (color_type == PNG_COLOR_TYPE_RGB) { + unsigned int row_data_width = img->width * img->channels; + for (y = 0; y < img->height; y++) { + memcpy(img->d[y], row_pointers[y], row_data_width); + } + } else { + int x; + for (y = 0; y < img->height; y++) { + for (x = 0; x < img->width; x++) { + img->d[y][3*x + 0] = row_pointers[y][4*x + 0]; + img->d[y][3*x + 1] = row_pointers[y][4*x + 1]; + img->d[y][3*x + 2] = row_pointers[y][4*x + 2]; + } + } } /* clean up after the read, and free any memory allocated */ -- cgit v1.2.3