8 #include "evas_common.h"
9 #include "evas_private.h"
12 #define PNG_BYTES_TO_CHECK 4
15 int evas_image_load_file_head_png(Image_Entry *ie, const char *file, const char *key);
16 int evas_image_load_file_data_png(Image_Entry *ie, const char *file, const char *key);
18 Evas_Image_Load_Func evas_image_load_png_func =
20 evas_image_load_file_head_png,
21 evas_image_load_file_data_png
26 evas_image_load_file_head_png(Image_Entry *ie, const char *file, const char *key)
30 png_structp png_ptr = NULL;
31 png_infop info_ptr = NULL;
32 int bit_depth, color_type, interlace_type;
33 unsigned char buf[PNG_BYTES_TO_CHECK];
36 if ((!file)) return 0;
38 f = fopen(file, "rb");
41 /* if we havent read the header before, set the header data */
42 if (fread(buf, PNG_BYTES_TO_CHECK, 1, f) != 1)
47 if (!png_check_sig(buf, PNG_BYTES_TO_CHECK))
53 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
59 info_ptr = png_create_info_struct(png_ptr);
62 png_destroy_read_struct(&png_ptr, NULL, NULL);
66 if (setjmp(png_ptr->jmpbuf))
68 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
72 png_init_io(png_ptr, f);
73 png_read_info(png_ptr, info_ptr);
74 png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *) (&w32),
75 (png_uint_32 *) (&h32), &bit_depth, &color_type,
76 &interlace_type, NULL, NULL);
77 if ((w32 < 1) || (h32 < 1) || (w32 > 8192) || (h32 > 8192))
79 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
85 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) hasa = 1;
86 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) hasa = 1;
87 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) hasa = 1;
88 if (hasa) ie->flags.alpha = 1;
89 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
96 evas_image_load_file_data_png(Image_Entry *ie, const char *file, const char *key)
98 unsigned char *surface;
102 png_structp png_ptr = NULL;
103 png_infop info_ptr = NULL;
104 int bit_depth, color_type, interlace_type;
105 unsigned char buf[PNG_BYTES_TO_CHECK];
106 unsigned char **lines;
110 if ((!file)) return 0;
112 f = fopen(file, "rb");
115 /* if we havent read the header before, set the header data */
116 fread(buf, 1, PNG_BYTES_TO_CHECK, f);
117 if (!png_check_sig(buf, PNG_BYTES_TO_CHECK))
123 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
129 info_ptr = png_create_info_struct(png_ptr);
132 png_destroy_read_struct(&png_ptr, NULL, NULL);
136 if (setjmp(png_ptr->jmpbuf))
138 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
142 png_init_io(png_ptr, f);
143 png_read_info(png_ptr, info_ptr);
144 png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *) (&w32),
145 (png_uint_32 *) (&h32), &bit_depth, &color_type,
146 &interlace_type, NULL, NULL);
147 evas_cache_image_surface_alloc(ie, w32, h32);
148 surface = (unsigned char *) evas_cache_image_pixels(ie);
151 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
155 if ((w32 != ie->w) || (h32 != ie->h))
157 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
161 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) hasa = 1;
162 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) hasa = 1;
163 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) hasa = 1;
164 if (hasa) ie->flags.alpha = 1;
166 /* Prep for transformations... ultimately we want ARGB */
167 /* expand palette -> RGB if necessary */
168 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png_ptr);
169 /* expand gray (w/reduced bits) -> 8-bit RGB if necessary */
170 if ((color_type == PNG_COLOR_TYPE_GRAY) ||
171 (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
173 png_set_gray_to_rgb(png_ptr);
174 if (bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr);
176 /* expand transparency entry -> alpha channel if present */
177 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
178 png_set_tRNS_to_alpha(png_ptr);
179 /* reduce 16bit color -> 8bit color if necessary */
180 if (bit_depth > 8) png_set_strip_16(png_ptr);
181 /* pack all pixels to byte boundaries */
182 png_set_packing(png_ptr);
187 #ifdef WORDS_BIGENDIAN
188 png_set_swap_alpha(png_ptr);
189 if (!hasa) png_set_filler(png_ptr, 0xff, PNG_FILLER_BEFORE);
191 png_set_bgr(png_ptr);
192 if (!hasa) png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
194 lines = (unsigned char **) alloca(h * sizeof(unsigned char *));
196 for (i = 0; i < h; i++)
197 lines[i] = surface + (i * w * sizeof(DATA32));
198 png_read_image(png_ptr, lines);
199 png_read_end(png_ptr, info_ptr);
200 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
202 evas_common_image_premul(ie);
209 module_open(Evas_Module *em)
212 em->functions = (void *)(&evas_image_load_png_func);
222 EAPI Evas_Module_Api evas_modapi =
224 EVAS_MODULE_API_VERSION,
225 EVAS_MODULE_TYPE_IMAGE_LOADER,