summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Drake <tlsa@netsurf-browser.org>2014-06-09 09:42:19 (GMT)
committer Michael Drake <tlsa@netsurf-browser.org>2014-06-09 09:42:19 (GMT)
commit630dfaac914df94311641e5994a1798eaa0be031 (patch)
tree7f2c744494cea82f94dd81139fbf17de8819272e
parent8798e2cb4467ac762c302775e52610d1f37d38f0 (diff)
downloadalphagen-630dfaac914df94311641e5994a1798eaa0be031.tar.gz
alphagen-630dfaac914df94311641e5994a1798eaa0be031.tar.bz2
Add support for input images with redundant alpha channels.HEADmaster
Some PNG exporters are daft.
-rw-r--r--src/alphagen.c30
1 files 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 */