summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCedric BAIL <cedric.bail@free.fr>2013-01-03 07:57:18 +0000
committerCedric BAIL <cedric.bail@free.fr>2013-01-03 07:57:18 +0000
commit1e8b9a90bf0acbdcef9c83b1ea7a3ad3fb153f8f (patch)
treed6f4cffbea104e2d4fb9a2db1a7743baa3dff448 /src
parentf4d463032885dea28fa91a80ba3e73c05386320a (diff)
efl: use Eina_File for PNG to.
SVN revision: 82047
Diffstat (limited to 'src')
-rw-r--r--src/modules/evas/loaders/png/evas_image_load_png.c136
1 files changed, 84 insertions, 52 deletions
diff --git a/src/modules/evas/loaders/png/evas_image_load_png.c b/src/modules/evas/loaders/png/evas_image_load_png.c
index e2623019f0..8704b0f544 100644
--- a/src/modules/evas/loaders/png/evas_image_load_png.c
+++ b/src/modules/evas/loaders/png/evas_image_load_png.c
@@ -10,23 +10,11 @@
10# include <Evil.h> 10# include <Evil.h>
11#endif 11#endif
12 12
13#ifdef _WIN32_WCE
14# define E_FOPEN(file, mode) evil_fopen_native((file), (mode))
15# define E_FREAD(buffer, size, count, stream) evil_fread_native(buffer, size, count, stream)
16# define E_FCLOSE(stream) evil_fclose_native(stream)
17#else
18# define E_FOPEN(file, mode) fopen((file), (mode))
19# define E_FREAD(buffer, size, count, stream) fread(buffer, size, count, stream)
20# define E_FCLOSE(stream) fclose(stream)
21#endif
22
23#include "evas_common.h" 13#include "evas_common.h"
24#include "evas_private.h" 14#include "evas_private.h"
25 15
26
27#define PNG_BYTES_TO_CHECK 4 16#define PNG_BYTES_TO_CHECK 4
28 17
29
30static Eina_Bool evas_image_load_file_head_png(Image_Entry *ie, const char *file, const char *key, int *error) EINA_ARG_NONNULL(1, 2, 4); 18static Eina_Bool evas_image_load_file_head_png(Image_Entry *ie, const char *file, const char *key, int *error) EINA_ARG_NONNULL(1, 2, 4);
31static Eina_Bool evas_image_load_file_data_png(Image_Entry *ie, const char *file, const char *key, int *error) EINA_ARG_NONNULL(1, 2, 4); 19static Eina_Bool evas_image_load_file_data_png(Image_Entry *ie, const char *file, const char *key, int *error) EINA_ARG_NONNULL(1, 2, 4);
32 20
@@ -39,34 +27,63 @@ static Evas_Image_Load_Func evas_image_load_png_func =
39 EINA_FALSE 27 EINA_FALSE
40}; 28};
41 29
30typedef struct _Evas_PNG_Info Evas_PNG_Info;
31struct _Evas_PNG_Info
32{
33 unsigned char *map;
34 size_t length;
35 size_t position;
36};
37
38static void
39_evas_image_png_read(png_structp png_ptr, png_bytep out, png_size_t count)
40{
41 Evas_PNG_Info *epi = png_ptr->io_ptr;
42
43 if (!epi) return ;
44 if (epi->position == epi->length) return ;
45
46 if (epi->position + count > epi->length) count = epi->length - epi->position;
47 memcpy(out, epi->map + epi->position, count);
48 epi->position += count;
49}
50
42static Eina_Bool 51static Eina_Bool
43evas_image_load_file_head_png(Image_Entry *ie, const char *file, const char *key EINA_UNUSED, int *error) 52evas_image_load_file_head_png(Image_Entry *ie, const char *file, const char *key EINA_UNUSED, int *error)
44{ 53{
45 png_uint_32 w32, h32; 54 Eina_File *f;
46 FILE *f; 55 Evas_PNG_Info epi;
47 png_structp png_ptr = NULL; 56 png_structp png_ptr = NULL;
48 png_infop info_ptr = NULL; 57 png_infop info_ptr = NULL;
58 png_uint_32 w32, h32;
49 int bit_depth, color_type, interlace_type; 59 int bit_depth, color_type, interlace_type;
50 unsigned char buf[PNG_BYTES_TO_CHECK];
51 char hasa; 60 char hasa;
61 Eina_Bool r = EINA_FALSE;
52 62
53 hasa = 0; 63 hasa = 0;
54 f = E_FOPEN(file, "rb"); 64 f = eina_file_open(file, EINA_FALSE);
55 if (!f) 65 if (!f)
56 { 66 {
57 ERR("File: '%s' does not exist\n", file);
58 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST; 67 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
59 return EINA_FALSE; 68 return EINA_FALSE;
60 } 69 }
61 70
62 /* if we havent read the header before, set the header data */ 71 epi.map = eina_file_map_all(f, EINA_FILE_SEQUENTIAL);
63 if (E_FREAD(buf, PNG_BYTES_TO_CHECK, 1, f) != 1) 72 if (!epi.map)
73 {
74 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
75 goto close_file;
76 }
77 epi.length = eina_file_size_get(f);
78 epi.position = 0;
79
80 if (epi.length < PNG_BYTES_TO_CHECK)
64 { 81 {
65 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT; 82 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
66 goto close_file; 83 goto close_file;
67 } 84 }
68 85
69 if (png_sig_cmp(buf, 0, PNG_BYTES_TO_CHECK)) 86 if (png_sig_cmp(epi.map, 0, PNG_BYTES_TO_CHECK))
70 { 87 {
71 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT; 88 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
72 goto close_file; 89 goto close_file;
@@ -82,18 +99,18 @@ evas_image_load_file_head_png(Image_Entry *ie, const char *file, const char *key
82 info_ptr = png_create_info_struct(png_ptr); 99 info_ptr = png_create_info_struct(png_ptr);
83 if (!info_ptr) 100 if (!info_ptr)
84 { 101 {
85 png_destroy_read_struct(&png_ptr, NULL, NULL);
86 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; 102 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
87 goto close_file; 103 goto close_file;
88 } 104 }
105
106 png_set_read_fn(png_ptr, &epi, _evas_image_png_read);
107
89 if (setjmp(png_jmpbuf(png_ptr))) 108 if (setjmp(png_jmpbuf(png_ptr)))
90 { 109 {
91 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
92 *error = EVAS_LOAD_ERROR_CORRUPT_FILE; 110 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
93 goto close_file; 111 goto close_file;
94 } 112 }
95 png_init_io(png_ptr, f); 113
96 png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK);
97 png_read_info(png_ptr, info_ptr); 114 png_read_info(png_ptr, info_ptr);
98 png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *) (&w32), 115 png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *) (&w32),
99 (png_uint_32 *) (&h32), &bit_depth, &color_type, 116 (png_uint_32 *) (&h32), &bit_depth, &color_type,
@@ -101,7 +118,6 @@ evas_image_load_file_head_png(Image_Entry *ie, const char *file, const char *key
101 if ((w32 < 1) || (h32 < 1) || (w32 > IMG_MAX_SIZE) || (h32 > IMG_MAX_SIZE) || 118 if ((w32 < 1) || (h32 < 1) || (w32 > IMG_MAX_SIZE) || (h32 > IMG_MAX_SIZE) ||
102 IMG_TOO_BIG(w32, h32)) 119 IMG_TOO_BIG(w32, h32))
103 { 120 {
104 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
105 if (IMG_TOO_BIG(w32, h32)) 121 if (IMG_TOO_BIG(w32, h32))
106 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; 122 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
107 else 123 else
@@ -127,52 +143,66 @@ evas_image_load_file_head_png(Image_Entry *ie, const char *file, const char *key
127 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) hasa = 1; 143 if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) hasa = 1;
128 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) hasa = 1; 144 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) hasa = 1;
129 if (hasa) ie->flags.alpha = 1; 145 if (hasa) ie->flags.alpha = 1;
130 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
131 E_FCLOSE(f);
132 146
133 *error = EVAS_LOAD_ERROR_NONE; 147 *error = EVAS_LOAD_ERROR_NONE;
134 return EINA_TRUE; 148 r = EINA_TRUE;
135 149
136 close_file: 150 close_file:
137 E_FCLOSE(f); 151 if (png_ptr) png_destroy_read_struct(&png_ptr,
138 return EINA_FALSE; 152 info_ptr ? &info_ptr : NULL,
153 NULL);
154 if (epi.map) eina_file_map_free(f, epi. map);
155 eina_file_close(f);
156
157 return r;
139} 158}
140 159
141static Eina_Bool 160static Eina_Bool
142evas_image_load_file_data_png(Image_Entry *ie, const char *file, const char *key EINA_UNUSED, int *error) 161evas_image_load_file_data_png(Image_Entry *ie, const char *file, const char *key EINA_UNUSED, int *error)
143{ 162{
163 Eina_File *f;
144 unsigned char *surface; 164 unsigned char *surface;
145 png_uint_32 w32, h32; 165 unsigned char **lines;
146 int w, h; 166 unsigned char *tmp_line;
147 FILE *f; 167 DATA32 *src_ptr, *dst_ptr;
148 png_structp png_ptr = NULL; 168 png_structp png_ptr = NULL;
149 png_infop info_ptr = NULL; 169 png_infop info_ptr = NULL;
170 Evas_PNG_Info epi;
171 png_uint_32 w32, h32;
172 int w, h;
150 int bit_depth, color_type, interlace_type; 173 int bit_depth, color_type, interlace_type;
151 unsigned char buf[PNG_BYTES_TO_CHECK];
152 unsigned char **lines;
153 char hasa; 174 char hasa;
154 int i, j; 175 int i, j;
155 int scale_ratio = 1, image_w = 0; 176 int scale_ratio = 1, image_w = 0;
156 unsigned char *tmp_line; 177 Eina_Bool r = EINA_FALSE;
157 DATA32 *src_ptr, *dst_ptr;
158 178
159 hasa = 0; 179 hasa = 0;
160 f = E_FOPEN(file, "rb"); 180 f = eina_file_open(file, EINA_FALSE);
161 if (!f) 181 if (!f)
162 { 182 {
163 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST; 183 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
164 return EINA_FALSE; 184 return EINA_FALSE;
165 } 185 }
166 186
167 /* if we havent read the header before, set the header data */ 187 epi.map = eina_file_map_all(f, EINA_FILE_SEQUENTIAL);
168 if (E_FREAD(buf, PNG_BYTES_TO_CHECK, 1, f) != 1) 188 if (!epi.map)
169 { 189 {
170 *error = EVAS_LOAD_ERROR_CORRUPT_FILE; 190 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
171 goto close_file; 191 goto close_file;
172 } 192 }
173 if (png_sig_cmp(buf, 0, PNG_BYTES_TO_CHECK)) 193 epi.length = eina_file_size_get(f);
194 epi.position = 0;
195
196 if (epi.length < PNG_BYTES_TO_CHECK)
174 { 197 {
175 *error = EVAS_LOAD_ERROR_CORRUPT_FILE; 198 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
199 goto close_file;
200 }
201
202 /* if we havent read the header before, set the header data */
203 if (png_sig_cmp(epi.map, 0, PNG_BYTES_TO_CHECK))
204 {
205 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
176 goto close_file; 206 goto close_file;
177 } 207 }
178 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 208 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
@@ -189,14 +219,16 @@ evas_image_load_file_data_png(Image_Entry *ie, const char *file, const char *key
189 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; 219 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
190 goto close_file; 220 goto close_file;
191 } 221 }
222
223 png_set_read_fn(png_ptr, &epi, _evas_image_png_read);
224
192 if (setjmp(png_jmpbuf(png_ptr))) 225 if (setjmp(png_jmpbuf(png_ptr)))
193 { 226 {
194 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); 227 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
195 *error = EVAS_LOAD_ERROR_CORRUPT_FILE; 228 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
196 goto close_file; 229 goto close_file;
197 } 230 }
198 png_init_io(png_ptr, f); 231
199 png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK);
200 png_read_info(png_ptr, info_ptr); 232 png_read_info(png_ptr, info_ptr);
201 png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *) (&w32), 233 png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *) (&w32),
202 (png_uint_32 *) (&h32), &bit_depth, &color_type, 234 (png_uint_32 *) (&h32), &bit_depth, &color_type,
@@ -212,13 +244,11 @@ evas_image_load_file_data_png(Image_Entry *ie, const char *file, const char *key
212 surface = (unsigned char *) evas_cache_image_pixels(ie); 244 surface = (unsigned char *) evas_cache_image_pixels(ie);
213 if (!surface) 245 if (!surface)
214 { 246 {
215 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
216 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; 247 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
217 goto close_file; 248 goto close_file;
218 } 249 }
219 if ((w32 != ie->w) || (h32 != ie->h)) 250 if ((w32 != ie->w) || (h32 != ie->h))
220 { 251 {
221 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
222 *error = EVAS_LOAD_ERROR_GENERIC; 252 *error = EVAS_LOAD_ERROR_GENERIC;
223 goto close_file; 253 goto close_file;
224 } 254 }
@@ -286,16 +316,18 @@ evas_image_load_file_data_png(Image_Entry *ie, const char *file, const char *key
286 } 316 }
287 } 317 }
288 318
289 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
290 E_FCLOSE(f);
291 evas_common_image_premul(ie); 319 evas_common_image_premul(ie);
292 320
293 *error = EVAS_LOAD_ERROR_NONE; 321 *error = EVAS_LOAD_ERROR_NONE;
294 return EINA_TRUE; 322 r = EINA_TRUE;
295 323
296 close_file: 324 close_file:
297 E_FCLOSE(f); 325 if (png_ptr) png_destroy_read_struct(&png_ptr,
298 return EINA_FALSE; 326 info_ptr ? &info_ptr : NULL,
327 NULL);
328 if (epi.map) eina_file_map_free(f, epi.map);
329 eina_file_close(f);
330 return r;
299} 331}
300 332
301static int 333static int