diff --git a/legacy/evas/src/modules/loaders/png/evas_image_load_png.c b/legacy/evas/src/modules/loaders/png/evas_image_load_png.c index 41f7f36fd7..8ef827eeef 100644 --- a/legacy/evas/src/modules/loaders/png/evas_image_load_png.c +++ b/legacy/evas/src/modules/loaders/png/evas_image_load_png.c @@ -31,11 +31,10 @@ evas_image_load_file_head_png(RGBA_Image *im, const char *file, const char *key) png_infop info_ptr = NULL; int bit_depth, color_type, interlace_type; unsigned char buf[PNG_BYTES_TO_CHECK]; - char hasa, hasg; + char hasa; if ((!file)) return 0; hasa = 0; - hasg = 0; f = fopen(file, "rb"); if (!f) return 0; @@ -87,18 +86,9 @@ evas_image_load_file_head_png(RGBA_Image *im, const char *file, const char *key) } im->image->w = (int) w32; im->image->h = (int) h32; - if (color_type == PNG_COLOR_TYPE_PALETTE) - { - png_set_expand(png_ptr); - if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) hasa = 1; - } - if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) hasa = 1; - if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) - { - hasa = 1; - hasg = 1; - } - if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY) hasg = 1; + if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) hasa = 1; + if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) hasa = 1; + if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) hasa = 1; if (hasa) im->flags |= RGBA_IMAGE_HAS_ALPHA; png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); fclose(f); @@ -117,12 +107,11 @@ evas_image_load_file_data_png(RGBA_Image *im, const char *file, const char *key) int bit_depth, color_type, interlace_type; unsigned char buf[PNG_BYTES_TO_CHECK]; unsigned char **lines; - char hasa, hasg; + char hasa; int i; if ((!file)) return 0; hasa = 0; - hasg = 0; f = fopen(file, "rb"); if (!f) return 0; @@ -164,32 +153,39 @@ evas_image_load_file_data_png(RGBA_Image *im, const char *file, const char *key) fclose(f); return 0; } - if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr); - if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) hasa = 1; - if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) - { - hasa = 1; - hasg = 1; - } - if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY) hasg = 1; + if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) hasa = 1; + if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) hasa = 1; + if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) hasa = 1; if (hasa) im->flags |= RGBA_IMAGE_HAS_ALPHA; + /* Prep for transformations... ultimately we want ARGB */ + /* expand palette -> RGB if necessary */ + if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png_ptr); + /* expand gray (w/reduced bits) -> 8-bit RGB if necessary */ + if ((color_type == PNG_COLOR_TYPE_GRAY) || + (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) + { + png_set_gray_to_rgb(png_ptr); + if (bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr); + } + /* expand transparency entry -> alpha channel if present */ + if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) + png_set_tRNS_to_alpha(png_ptr); + /* reduce 16bit color -> 8bit color if necessary */ + if (bit_depth > 8) png_set_strip_16(png_ptr); + /* pack all pixels to byte boundaries */ + png_set_packing(png_ptr); + w = im->image->w; h = im->image->h; - if (hasa) png_set_expand(png_ptr); /* we want ARGB */ #ifdef WORDS_BIGENDIAN png_set_swap_alpha(png_ptr); - png_set_filler(png_ptr, 0xff, PNG_FILLER_BEFORE); + if (!hasa) png_set_filler(png_ptr, 0xff, PNG_FILLER_BEFORE); #else png_set_bgr(png_ptr); - png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); + if (!hasa) png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); #endif - /* 16bit color -> 8bit color */ - png_set_strip_16(png_ptr); - /* pack all pixels to byte boundaires */ - png_set_packing(png_ptr); - if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr); evas_common_image_surface_alloc(im->image); if (!im->image->data) { @@ -200,12 +196,6 @@ evas_image_load_file_data_png(RGBA_Image *im, const char *file, const char *key) } lines = (unsigned char **) alloca(h * sizeof(unsigned char *)); - if (hasg) - { - png_set_gray_to_rgb(png_ptr); - if (png_get_bit_depth(png_ptr, info_ptr) < 8) - png_set_gray_1_2_4_to_8(png_ptr); - } for (i = 0; i < h; i++) lines[i] = ((unsigned char *)(im->image->data)) + (i * w * sizeof(DATA32)); png_read_image(png_ptr, lines);