png loading changes to fix greyscale loads apparently on 1.2.19 libpng...

SVN revision: 31488
This commit is contained in:
Carsten Haitzler 2007-08-25 02:36:18 +00:00
parent 8ad14fe1ba
commit c1d4685780
1 changed files with 28 additions and 38 deletions

View File

@ -31,11 +31,10 @@ evas_image_load_file_head_png(RGBA_Image *im, const char *file, const char *key)
png_infop info_ptr = NULL; png_infop info_ptr = NULL;
int bit_depth, color_type, interlace_type; int bit_depth, color_type, interlace_type;
unsigned char buf[PNG_BYTES_TO_CHECK]; unsigned char buf[PNG_BYTES_TO_CHECK];
char hasa, hasg; char hasa;
if ((!file)) return 0; if ((!file)) return 0;
hasa = 0; hasa = 0;
hasg = 0;
f = fopen(file, "rb"); f = fopen(file, "rb");
if (!f) return 0; 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->w = (int) w32;
im->image->h = (int) h32; im->image->h = (int) h32;
if (color_type == PNG_COLOR_TYPE_PALETTE) if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) hasa = 1;
{ if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) hasa = 1;
png_set_expand(png_ptr); if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) hasa = 1;
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 (hasa) im->flags |= RGBA_IMAGE_HAS_ALPHA; if (hasa) im->flags |= RGBA_IMAGE_HAS_ALPHA;
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
fclose(f); 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; int bit_depth, color_type, interlace_type;
unsigned char buf[PNG_BYTES_TO_CHECK]; unsigned char buf[PNG_BYTES_TO_CHECK];
unsigned char **lines; unsigned char **lines;
char hasa, hasg; char hasa;
int i; int i;
if ((!file)) return 0; if ((!file)) return 0;
hasa = 0; hasa = 0;
hasg = 0;
f = fopen(file, "rb"); f = fopen(file, "rb");
if (!f) return 0; 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); fclose(f);
return 0; return 0;
} }
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 (color_type == PNG_COLOR_TYPE_RGB_ALPHA) hasa = 1;
if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) hasa = 1;
{
hasa = 1;
hasg = 1;
}
if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY) hasg = 1;
if (hasa) im->flags |= RGBA_IMAGE_HAS_ALPHA; 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; w = im->image->w;
h = im->image->h; h = im->image->h;
if (hasa) png_set_expand(png_ptr);
/* we want ARGB */ /* we want ARGB */
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
png_set_swap_alpha(png_ptr); 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 #else
png_set_bgr(png_ptr); 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 #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); evas_common_image_surface_alloc(im->image);
if (!im->image->data) 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 *)); 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++) for (i = 0; i < h; i++)
lines[i] = ((unsigned char *)(im->image->data)) + (i * w * sizeof(DATA32)); lines[i] = ((unsigned char *)(im->image->data)) + (i * w * sizeof(DATA32));
png_read_image(png_ptr, lines); png_read_image(png_ptr, lines);