diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/evas/src/modules/loaders/bmp/evas_image_load_bmp.c | 1451 |
1 files changed, 1451 insertions, 0 deletions
diff --git a/libraries/evas/src/modules/loaders/bmp/evas_image_load_bmp.c b/libraries/evas/src/modules/loaders/bmp/evas_image_load_bmp.c new file mode 100644 index 0000000..38e3680 --- /dev/null +++ b/libraries/evas/src/modules/loaders/bmp/evas_image_load_bmp.c | |||
@@ -0,0 +1,1451 @@ | |||
1 | #ifdef HAVE_CONFIG_H | ||
2 | # include <config.h> | ||
3 | #endif | ||
4 | |||
5 | #include <stdio.h> | ||
6 | |||
7 | #ifdef HAVE_EVIL | ||
8 | # include <Evil.h> | ||
9 | #endif | ||
10 | |||
11 | #include <math.h> | ||
12 | |||
13 | #include "evas_common.h" | ||
14 | #include "evas_private.h" | ||
15 | |||
16 | static Eina_Bool evas_image_load_file_head_bmp(Image_Entry *ie, const char *file, const char *key, int *error) EINA_ARG_NONNULL(1, 2, 4); | ||
17 | static Eina_Bool evas_image_load_file_data_bmp(Image_Entry *ie, const char *file, const char *key, int *error) EINA_ARG_NONNULL(1, 2, 4); | ||
18 | |||
19 | static Evas_Image_Load_Func evas_image_load_bmp_func = | ||
20 | { | ||
21 | EINA_TRUE, | ||
22 | evas_image_load_file_head_bmp, | ||
23 | evas_image_load_file_data_bmp, | ||
24 | NULL | ||
25 | }; | ||
26 | |||
27 | static int | ||
28 | read_short(FILE *file, short *ret) | ||
29 | { | ||
30 | unsigned char b[2]; | ||
31 | if (fread(b, sizeof(unsigned char), 2, file) != 2) return 0; | ||
32 | *ret = (b[1] << 8) | b[0]; | ||
33 | return 1; | ||
34 | } | ||
35 | |||
36 | static int | ||
37 | read_ushort(FILE *file, unsigned short *ret) | ||
38 | { | ||
39 | unsigned char b[2]; | ||
40 | if (fread(b, sizeof(unsigned char), 2, file) != 2) return 0; | ||
41 | *ret = (b[1] << 8) | b[0]; | ||
42 | return 1; | ||
43 | } | ||
44 | |||
45 | static int | ||
46 | read_int(FILE *file, int *ret) | ||
47 | { | ||
48 | unsigned char b[4]; | ||
49 | if (fread(b, sizeof(unsigned char), 4, file) != 4) return 0; | ||
50 | *ret = ARGB_JOIN(b[3], b[2], b[1], b[0]); | ||
51 | return 1; | ||
52 | } | ||
53 | |||
54 | static int | ||
55 | read_uint(FILE *file, unsigned int *ret) | ||
56 | { | ||
57 | unsigned char b[4]; | ||
58 | if (fread(b, sizeof(unsigned char), 4, file) != 4) return 0; | ||
59 | *ret = ARGB_JOIN(b[3], b[2], b[1], b[0]); | ||
60 | return 1; | ||
61 | } | ||
62 | |||
63 | static Eina_Bool | ||
64 | evas_image_load_file_head_bmp(Image_Entry *ie, const char *file, const char *key __UNUSED__, int *error) | ||
65 | { | ||
66 | FILE *f; | ||
67 | char buf[4096]; | ||
68 | char hasa = 0; | ||
69 | int w = 0, h = 0, planes = 0, bit_count = 0, | ||
70 | image_size = 0, comp = 0, hdpi = 0, vdpi = 0, | ||
71 | palette_size = -1, important_colors = 0; | ||
72 | unsigned int offset, head_size, rmask = 0, gmask = 0, bmask = 0, amask = 0; | ||
73 | unsigned int pal_num = 0; | ||
74 | int right_way_up = 0; | ||
75 | int fsize = 0; | ||
76 | unsigned int bmpsize; | ||
77 | unsigned short res1, res2; | ||
78 | |||
79 | f = fopen(file, "rb"); | ||
80 | if (!f) | ||
81 | { | ||
82 | *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST; | ||
83 | return EINA_FALSE; | ||
84 | } | ||
85 | |||
86 | *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT; | ||
87 | fseek(f, 0, SEEK_END); | ||
88 | fsize = ftell(f); | ||
89 | fseek(f, 0, SEEK_SET); | ||
90 | if (fsize < 2) goto close_file; | ||
91 | |||
92 | if (fread(buf, 2, 1, f) != 1) goto close_file; | ||
93 | if (strncmp(buf, "BM", 2)) goto close_file; // magic number | ||
94 | *error = EVAS_LOAD_ERROR_CORRUPT_FILE; | ||
95 | if (!read_uint(f, &bmpsize)) goto close_file; | ||
96 | if (!read_ushort(f, &res1)) goto close_file; | ||
97 | if (!read_ushort(f, &res2)) goto close_file; | ||
98 | if (!read_uint(f, &offset)) goto close_file; | ||
99 | if (!read_uint(f, &head_size)) goto close_file; | ||
100 | if (head_size == 12) // OS/2 V1 + Windows 3.0 | ||
101 | { | ||
102 | short tmp; | ||
103 | |||
104 | if (!read_short(f, &tmp)) goto close_file; | ||
105 | w = tmp; // width | ||
106 | if (!read_short(f, &tmp)) goto close_file; | ||
107 | h = tmp; // height | ||
108 | if (!read_short(f, &tmp)) goto close_file; | ||
109 | planes = tmp; // must be 1 | ||
110 | if (!read_short(f, &tmp)) goto close_file; | ||
111 | bit_count = tmp; // bits per pixel: 1, 4, 8 & 24 | ||
112 | } | ||
113 | else if (head_size == 64) // OS/2 V2 | ||
114 | { | ||
115 | short tmp; | ||
116 | int tmp2; | ||
117 | |||
118 | if (!read_int(f, &tmp2)) goto close_file; | ||
119 | w = tmp2; // width | ||
120 | if (!read_int(f, &tmp2)) goto close_file; | ||
121 | h = tmp2; // height | ||
122 | if (!read_short(f, &tmp)) goto close_file; | ||
123 | planes = tmp; // must be 1 | ||
124 | if (!read_short(f, &tmp)) goto close_file; | ||
125 | bit_count = tmp; // bits per pixel: 1, 4, 8, 16, 24 & 32 | ||
126 | if (!read_int(f, &tmp2)) goto close_file; | ||
127 | comp = tmp2; // compression method | ||
128 | if (!read_int(f, &tmp2)) goto close_file; | ||
129 | image_size = tmp2; // bitmap data size | ||
130 | if (!read_int(f, &tmp2)) goto close_file; | ||
131 | hdpi = (tmp2 * 254) / 10000; // horizontal pixels/meter | ||
132 | if (!read_int(f, &tmp2)) goto close_file; | ||
133 | vdpi = (tmp2 * 254) / 10000; // vertical pixles/meter | ||
134 | if (!read_int(f, &tmp2)) goto close_file; | ||
135 | palette_size = tmp2; // number of palette colors power (2^n - so 0 - 8) | ||
136 | if (!read_int(f, &tmp2)) goto close_file; | ||
137 | important_colors = tmp2; // number of important colors - 0 if all | ||
138 | if (fread(buf, 24, 1, f) != 1) goto close_file; // skip unused header | ||
139 | if (image_size == 0) image_size = fsize - offset; | ||
140 | } | ||
141 | else if (head_size == 40) // Windows 3.0 + (v3) | ||
142 | { | ||
143 | short tmp; | ||
144 | int tmp2; | ||
145 | |||
146 | if (!read_int(f, &tmp2)) goto close_file; | ||
147 | w = tmp2; // width | ||
148 | if (!read_int(f, &tmp2)) goto close_file; | ||
149 | h = tmp2; // height | ||
150 | if (!read_short(f, &tmp)) goto close_file; | ||
151 | planes = tmp; // must be 1 | ||
152 | if (!read_short(f, &tmp)) goto close_file; | ||
153 | bit_count = tmp; // bits per pixel: 1, 4, 8, 16, 24 & 32 | ||
154 | if (!read_int(f, &tmp2)) goto close_file; | ||
155 | comp = tmp2; // compression method | ||
156 | if (!read_int(f, &tmp2)) goto close_file; | ||
157 | image_size = tmp2; // bitmap data size | ||
158 | if (!read_int(f, &tmp2)) goto close_file; | ||
159 | hdpi = (tmp2 * 254) / 10000; // horizontal pixels/meter | ||
160 | if (!read_int(f, &tmp2)) goto close_file; | ||
161 | vdpi = (tmp2 * 254) / 10000; // vertical pixles/meter | ||
162 | if (!read_int(f, &tmp2)) goto close_file; | ||
163 | palette_size = tmp2; // number of palette colors power (2^n - so 0 - 8) | ||
164 | if (!read_int(f, &tmp2)) goto close_file; | ||
165 | important_colors = tmp2; // number of important colors - 0 if all | ||
166 | if (image_size == 0) image_size = fsize - offset; | ||
167 | if ((comp == 0) && (bit_count == 32)) hasa = 1; // GIMP seems to store it this way | ||
168 | } | ||
169 | else if (head_size == 108) // Windows 95/NT4 + (v4) | ||
170 | { | ||
171 | short tmp; | ||
172 | int tmp2; | ||
173 | |||
174 | if (!read_int(f, &tmp2)) goto close_file; | ||
175 | w = tmp2; // width | ||
176 | if (!read_int(f, &tmp2)) goto close_file; | ||
177 | h = tmp2; // height | ||
178 | if (!read_short(f, &tmp)) goto close_file; | ||
179 | planes = tmp; // must be 1 | ||
180 | if (!read_short(f, &tmp)) goto close_file; | ||
181 | bit_count = tmp; // bits per pixel: 1, 4, 8, 16, 24 & 32 | ||
182 | if (!read_int(f, &tmp2)) goto close_file; | ||
183 | comp = tmp2; // compression method | ||
184 | if (!read_int(f, &tmp2)) goto close_file; | ||
185 | image_size = tmp2; // bitmap data size | ||
186 | if (!read_int(f, &tmp2)) goto close_file; | ||
187 | hdpi = (tmp2 * 254) / 10000; // horizontal pixels/meter | ||
188 | if (!read_int(f, &tmp2)) goto close_file; | ||
189 | vdpi = (tmp2 * 254) / 10000; // vertical pixles/meter | ||
190 | if (!read_int(f, &tmp2)) goto close_file; | ||
191 | palette_size = tmp2; // number of palette colors power (2^n - so 0 - 8) | ||
192 | if (!read_int(f, &tmp2)) goto close_file; | ||
193 | important_colors = tmp2; // number of important colors - 0 if all | ||
194 | if (!read_int(f, &tmp2)) goto close_file; | ||
195 | rmask = tmp2; // red mask | ||
196 | if (!read_int(f, &tmp2)) goto close_file; | ||
197 | gmask = tmp2; // green mask | ||
198 | if (!read_int(f, &tmp2)) goto close_file; | ||
199 | bmask = tmp2; // blue mask | ||
200 | if (!read_int(f, &tmp2)) goto close_file; | ||
201 | amask = tmp2; // alpha mask | ||
202 | if (fread(buf, 36, 1, f) != 1) goto close_file; // skip unused cie | ||
203 | if (fread(buf, 12, 1, f) != 1) goto close_file; // skip unused gamma | ||
204 | if (image_size == 0) image_size = fsize - offset; | ||
205 | if ((amask) && (bit_count == 32)) hasa = 1; | ||
206 | } | ||
207 | else if (head_size == 124) // Windows 98/2000 + (v5) | ||
208 | { | ||
209 | short tmp; | ||
210 | int tmp2; | ||
211 | |||
212 | if (!read_int(f, &tmp2)) goto close_file; | ||
213 | w = tmp2; // width | ||
214 | if (!read_int(f, &tmp2)) goto close_file; | ||
215 | h = tmp2; // height | ||
216 | if (!read_short(f, &tmp)) goto close_file; | ||
217 | planes = tmp; // must be 1 | ||
218 | if (!read_short(f, &tmp)) goto close_file; | ||
219 | bit_count = tmp; // bits per pixel: 1, 4, 8, 16, 24 & 32 | ||
220 | if (!read_int(f, &tmp2)) goto close_file; | ||
221 | comp = tmp2; // compression method | ||
222 | if (!read_int(f, &tmp2)) goto close_file; | ||
223 | image_size = tmp2; // bitmap data size | ||
224 | if (!read_int(f, &tmp2)) goto close_file; | ||
225 | hdpi = (tmp2 * 254) / 10000; // horizontal pixels/meter | ||
226 | if (!read_int(f, &tmp2)) goto close_file; | ||
227 | vdpi = (tmp2 * 254) / 10000; // vertical pixles/meter | ||
228 | if (!read_int(f, &tmp2)) goto close_file; | ||
229 | palette_size = tmp2; // number of palette colors power (2^n - so 0 - 8) | ||
230 | if (!read_int(f, &tmp2)) goto close_file; | ||
231 | important_colors = tmp2; // number of important colors - 0 if all | ||
232 | if (!read_int(f, &tmp2)) goto close_file; | ||
233 | rmask = tmp2; // red mask | ||
234 | if (!read_int(f, &tmp2)) goto close_file; | ||
235 | gmask = tmp2; // green mask | ||
236 | if (!read_int(f, &tmp2)) goto close_file; | ||
237 | bmask = tmp2; // blue mask | ||
238 | if (!read_int(f, &tmp2)) goto close_file; | ||
239 | amask = tmp2; // alpha mask | ||
240 | if (fread(buf, 36, 1, f) != 1) goto close_file; // skip unused cie | ||
241 | if (fread(buf, 12, 1, f) != 1) goto close_file; // skip unused gamma | ||
242 | if (fread(buf, 16, 1, f) != 1) goto close_file; // skip others | ||
243 | if (image_size == 0) image_size = fsize - offset; | ||
244 | if ((amask) && (bit_count == 32)) hasa = 1; | ||
245 | } | ||
246 | else | ||
247 | goto close_file; | ||
248 | |||
249 | if (h < 0) | ||
250 | { | ||
251 | h = -h; | ||
252 | right_way_up = 1; | ||
253 | } | ||
254 | |||
255 | if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE) || | ||
256 | IMG_TOO_BIG(w, h)) | ||
257 | { | ||
258 | if (IMG_TOO_BIG(w, h)) | ||
259 | *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; | ||
260 | else | ||
261 | *error = EVAS_LOAD_ERROR_GENERIC; | ||
262 | goto close_file; | ||
263 | } | ||
264 | /* It is not bad idea that bmp loader support scale down decoding | ||
265 | * because of memory issue in mobile world.*/ | ||
266 | if (ie->load_opts.scale_down_by > 1) | ||
267 | { | ||
268 | w /= ie->load_opts.scale_down_by; | ||
269 | h /= ie->load_opts.scale_down_by; | ||
270 | } | ||
271 | |||
272 | if (bit_count < 16) | ||
273 | { | ||
274 | if ((palette_size < 0) || (palette_size > 256)) pal_num = 256; | ||
275 | else pal_num = palette_size; | ||
276 | if (bit_count == 1) | ||
277 | { | ||
278 | if (comp == 0) // no compression | ||
279 | { | ||
280 | } | ||
281 | else | ||
282 | goto close_file; | ||
283 | } | ||
284 | else if (bit_count == 4) | ||
285 | { | ||
286 | if (comp == 0) // no compression | ||
287 | { | ||
288 | } | ||
289 | else if (comp == 2) // rle 4bit/pixel | ||
290 | { | ||
291 | } | ||
292 | else | ||
293 | goto close_file; | ||
294 | } | ||
295 | else if (bit_count == 8) | ||
296 | { | ||
297 | if (comp == 0) // no compression | ||
298 | { | ||
299 | } | ||
300 | else if (comp == 1) // rle 8bit/pixel | ||
301 | { | ||
302 | } | ||
303 | else | ||
304 | goto close_file; | ||
305 | } | ||
306 | } | ||
307 | else if ((bit_count == 16) || (bit_count == 24) || (bit_count == 32)) | ||
308 | { | ||
309 | if (comp == 0) // no compression | ||
310 | { | ||
311 | // handled | ||
312 | } | ||
313 | else if (comp == 3) // bit field | ||
314 | { | ||
315 | // handled | ||
316 | } | ||
317 | else if (comp == 4) // jpeg - only printer drivers | ||
318 | goto close_file; | ||
319 | else if (comp == 3) // png - only printer drivers | ||
320 | goto close_file; | ||
321 | else | ||
322 | goto close_file; | ||
323 | } | ||
324 | else | ||
325 | goto close_file; | ||
326 | |||
327 | ie->w = w; | ||
328 | ie->h = h; | ||
329 | if (hasa) ie->flags.alpha = 1; | ||
330 | |||
331 | fclose(f); | ||
332 | *error = EVAS_LOAD_ERROR_NONE; | ||
333 | return EINA_TRUE; | ||
334 | |||
335 | close_file: | ||
336 | fclose(f); | ||
337 | return EINA_FALSE; | ||
338 | } | ||
339 | |||
340 | static Eina_Bool | ||
341 | evas_image_load_file_data_bmp(Image_Entry *ie, const char *file, const char *key __UNUSED__, int *error) | ||
342 | { | ||
343 | FILE *f; | ||
344 | char buf[4096]; | ||
345 | unsigned char *buffer = NULL, *buffer_end = NULL, *p; | ||
346 | char hasa = 0; | ||
347 | int x = 0, y = 0, w = 0, h = 0, planes = 0, bit_count = 0, image_size = 0, | ||
348 | comp = 0, hdpi = 0, vdpi = 0, palette_size = -1, important_colors = 0; | ||
349 | unsigned int offset = 0, head_size = 0; | ||
350 | unsigned int *pal = NULL, pal_num = 0, *pix = NULL, *surface = NULL, fix, | ||
351 | rmask = 0, gmask = 0, bmask = 0, amask = 0; | ||
352 | int right_way_up = 0; | ||
353 | unsigned char r, g, b, a; | ||
354 | int fsize = 0; | ||
355 | unsigned int bmpsize; | ||
356 | unsigned short res1, res2; | ||
357 | |||
358 | /* for scale decoding */ | ||
359 | unsigned int *scale_surface = NULL, *scale_pix = NULL; | ||
360 | int scale_ratio = 1, image_w = 0, image_h = 0; | ||
361 | int row_size = 0; /* Row size is rounded up to a multiple of 4bytes */ | ||
362 | int read_line = 0; /* total read line */ | ||
363 | |||
364 | |||
365 | f = fopen(file, "rb"); | ||
366 | if (!f) | ||
367 | { | ||
368 | *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST; | ||
369 | return EINA_FALSE; | ||
370 | } | ||
371 | |||
372 | *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT; | ||
373 | fseek(f, 0, SEEK_END); | ||
374 | fsize = ftell(f); | ||
375 | fseek(f, 0, SEEK_SET); | ||
376 | if (fsize < 2) goto close_file; | ||
377 | |||
378 | if (fread(buf, 2, 1, f) != 1) goto close_file; | ||
379 | if (strncmp(buf, "BM", 2)) goto close_file; // magic number | ||
380 | *error = EVAS_LOAD_ERROR_CORRUPT_FILE; | ||
381 | if (!read_uint(f, &bmpsize)) goto close_file; | ||
382 | if (!read_ushort(f, &res1)) goto close_file; | ||
383 | if (!read_ushort(f, &res2)) goto close_file; | ||
384 | if (!read_uint(f, &offset)) goto close_file; | ||
385 | if (!read_uint(f, &head_size)) goto close_file; | ||
386 | image_size = fsize - offset; | ||
387 | if (image_size < 1) goto close_file; | ||
388 | |||
389 | if (head_size == 12) // OS/2 V1 + Windows 3.0 | ||
390 | { | ||
391 | short tmp; | ||
392 | |||
393 | if (!read_short(f, &tmp)) goto close_file; | ||
394 | w = tmp; // width | ||
395 | if (!read_short(f, &tmp)) goto close_file; | ||
396 | h = tmp; // height | ||
397 | if (!read_short(f, &tmp)) goto close_file; | ||
398 | planes = tmp; // must be 1 | ||
399 | if (!read_short(f, &tmp)) goto close_file; | ||
400 | bit_count = tmp; // bits per pixel: 1, 4, 8 & 24 | ||
401 | } | ||
402 | else if (head_size == 64) // OS/2 V2 | ||
403 | { | ||
404 | short tmp; | ||
405 | int tmp2; | ||
406 | |||
407 | if (!read_int(f, &tmp2)) goto close_file; | ||
408 | w = tmp2; // width | ||
409 | if (!read_int(f, &tmp2)) goto close_file; | ||
410 | h = tmp2; // height | ||
411 | if (!read_short(f, &tmp)) goto close_file; | ||
412 | planes = tmp; // must be 1 | ||
413 | if (!read_short(f, &tmp)) goto close_file; | ||
414 | bit_count = tmp; // bits per pixel: 1, 4, 8, 16, 24 & 32 | ||
415 | if (!read_int(f, &tmp2)) goto close_file; | ||
416 | comp = tmp2; // compression method | ||
417 | if (!read_int(f, &tmp2)) goto close_file; | ||
418 | image_size = tmp2; // bitmap data size | ||
419 | if (!read_int(f, &tmp2)) goto close_file; | ||
420 | hdpi = (tmp2 * 254) / 10000; // horizontal pixels/meter | ||
421 | if (!read_int(f, &tmp2)) goto close_file; | ||
422 | vdpi = (tmp2 * 254) / 10000; // vertical pixles/meter | ||
423 | if (!read_int(f, &tmp2)) goto close_file; | ||
424 | palette_size = tmp2; // number of palette colors power (2^n - so 0 - 8) | ||
425 | if (!read_int(f, &tmp2)) goto close_file; | ||
426 | important_colors = tmp2; // number of important colors - 0 if all | ||
427 | if (fread(buf, 24, 1, f) != 1) goto close_file; // skip unused header | ||
428 | if (image_size == 0) image_size = fsize - offset; | ||
429 | } | ||
430 | else if (head_size == 40) // Windows 3.0 + (v3) | ||
431 | { | ||
432 | short tmp; | ||
433 | int tmp2; | ||
434 | |||
435 | if (!read_int(f, &tmp2)) goto close_file; | ||
436 | w = tmp2; // width | ||
437 | if (!read_int(f, &tmp2)) goto close_file; | ||
438 | h = tmp2; // height | ||
439 | if (!read_short(f, &tmp)) goto close_file; | ||
440 | planes = tmp; // must be 1 | ||
441 | if (!read_short(f, &tmp)) goto close_file; | ||
442 | bit_count = tmp; // bits per pixel: 1, 4, 8, 16, 24 & 32 | ||
443 | if (!read_int(f, &tmp2)) goto close_file; | ||
444 | comp = tmp2; // compression method | ||
445 | if (!read_int(f, &tmp2)) goto close_file; | ||
446 | image_size = tmp2; // bitmap data size | ||
447 | if (!read_int(f, &tmp2)) goto close_file; | ||
448 | hdpi = (tmp2 * 254) / 10000; // horizontal pixels/meter | ||
449 | if (!read_int(f, &tmp2)) goto close_file; | ||
450 | vdpi = (tmp2 * 254) / 10000; // vertical pixles/meter | ||
451 | if (!read_int(f, &tmp2)) goto close_file; | ||
452 | palette_size = tmp2; // number of palette colors power (2^n - so 0 - 8) | ||
453 | if (!read_int(f, &tmp2)) goto close_file; | ||
454 | important_colors = tmp2; // number of important colors - 0 if all | ||
455 | if (image_size == 0) image_size = fsize - offset; | ||
456 | if ((comp == 0) && (bit_count == 32)) hasa = 1; // GIMP seems to store it this way | ||
457 | } | ||
458 | else if (head_size == 108) // Windows 95/NT4 + (v4) | ||
459 | { | ||
460 | short tmp; | ||
461 | int tmp2; | ||
462 | |||
463 | if (!read_int(f, &tmp2)) goto close_file; | ||
464 | w = tmp2; // width | ||
465 | if (!read_int(f, &tmp2)) goto close_file; | ||
466 | h = tmp2; // height | ||
467 | if (!read_short(f, &tmp)) goto close_file; | ||
468 | planes = tmp; // must be 1 | ||
469 | if (!read_short(f, &tmp)) goto close_file; | ||
470 | bit_count = tmp; // bits per pixel: 1, 4, 8, 16, 24 & 32 | ||
471 | if (!read_int(f, &tmp2)) goto close_file; | ||
472 | comp = tmp2; // compression method | ||
473 | if (!read_int(f, &tmp2)) goto close_file; | ||
474 | image_size = tmp2; // bitmap data size | ||
475 | if (!read_int(f, &tmp2)) goto close_file; | ||
476 | hdpi = (tmp2 * 254) / 10000; // horizontal pixels/meter | ||
477 | if (!read_int(f, &tmp2)) goto close_file; | ||
478 | vdpi = (tmp2 * 254) / 10000; // vertical pixles/meter | ||
479 | if (!read_int(f, &tmp2)) goto close_file; | ||
480 | palette_size = tmp2; // number of palette colors power (2^n - so 0 - 8) | ||
481 | if (!read_int(f, &tmp2)) goto close_file; | ||
482 | important_colors = tmp2; // number of important colors - 0 if all | ||
483 | if (!read_int(f, &tmp2)) goto close_file; | ||
484 | rmask = tmp2; // red mask | ||
485 | if (!read_int(f, &tmp2)) goto close_file; | ||
486 | gmask = tmp2; // green mask | ||
487 | if (!read_int(f, &tmp2)) goto close_file; | ||
488 | bmask = tmp2; // blue mask | ||
489 | if (!read_int(f, &tmp2)) goto close_file; | ||
490 | amask = tmp2; // alpha mask | ||
491 | if (fread(buf, 36, 1, f) != 1) goto close_file; // skip unused cie | ||
492 | if (fread(buf, 12, 1, f) != 1) goto close_file; // skip unused gamma | ||
493 | if (image_size == 0) image_size = fsize - offset; | ||
494 | if ((amask) && (bit_count == 32)) hasa = 1; | ||
495 | } | ||
496 | else if (head_size == 124) // Windows 98/2000 + (v5) | ||
497 | { | ||
498 | short tmp; | ||
499 | int tmp2; | ||
500 | |||
501 | if (!read_int(f, &tmp2)) goto close_file; | ||
502 | w = tmp2; // width | ||
503 | if (!read_int(f, &tmp2)) goto close_file; | ||
504 | h = tmp2; // height | ||
505 | if (!read_short(f, &tmp)) goto close_file; | ||
506 | planes = tmp; // must be 1 | ||
507 | if (!read_short(f, &tmp)) goto close_file; | ||
508 | bit_count = tmp; // bits per pixel: 1, 4, 8, 16, 24 & 32 | ||
509 | if (!read_int(f, &tmp2)) goto close_file; | ||
510 | comp = tmp2; // compression method | ||
511 | if (!read_int(f, &tmp2)) goto close_file; | ||
512 | image_size = tmp2; // bitmap data size | ||
513 | if (!read_int(f, &tmp2)) goto close_file; | ||
514 | hdpi = (tmp2 * 254) / 10000; // horizontal pixels/meter | ||
515 | if (!read_int(f, &tmp2)) goto close_file; | ||
516 | vdpi = (tmp2 * 254) / 10000; // vertical pixles/meter | ||
517 | if (!read_int(f, &tmp2)) goto close_file; | ||
518 | palette_size = tmp2; // number of palette colors power (2^n - so 0 - 8) | ||
519 | if (!read_int(f, &tmp2)) goto close_file; | ||
520 | important_colors = tmp2; // number of important colors - 0 if all | ||
521 | if (!read_int(f, &tmp2)) goto close_file; | ||
522 | rmask = tmp2; // red mask | ||
523 | if (!read_int(f, &tmp2)) goto close_file; | ||
524 | gmask = tmp2; // green mask | ||
525 | if (!read_int(f, &tmp2)) goto close_file; | ||
526 | bmask = tmp2; // blue mask | ||
527 | if (!read_int(f, &tmp2)) goto close_file; | ||
528 | amask = tmp2; // alpha mask | ||
529 | if (fread(buf, 36, 1, f) != 1) goto close_file; // skip unused cie | ||
530 | if (fread(buf, 12, 1, f) != 1) goto close_file; // skip unused gamma | ||
531 | if (fread(buf, 16, 1, f) != 1) goto close_file; // skip others | ||
532 | if (image_size == 0) image_size = fsize - offset; | ||
533 | if ((amask) && (bit_count == 32)) hasa = 1; | ||
534 | } | ||
535 | else | ||
536 | goto close_file; | ||
537 | |||
538 | if (h < 0) | ||
539 | { | ||
540 | h = -h; | ||
541 | right_way_up = 1; | ||
542 | } | ||
543 | if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE) || | ||
544 | IMG_TOO_BIG(w, h)) | ||
545 | { | ||
546 | if (IMG_TOO_BIG(w, h)) | ||
547 | *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; | ||
548 | else | ||
549 | *error = EVAS_LOAD_ERROR_GENERIC; | ||
550 | goto close_file; | ||
551 | } | ||
552 | /* It is not bad idea that bmp loader support scale down decoding | ||
553 | * because of memory issue in mobile world. */ | ||
554 | if (ie->load_opts.scale_down_by > 1) | ||
555 | scale_ratio = ie->load_opts.scale_down_by; | ||
556 | image_w = w; | ||
557 | image_h = h; | ||
558 | |||
559 | if (scale_ratio > 1) | ||
560 | { | ||
561 | w /= scale_ratio; | ||
562 | h /= scale_ratio; | ||
563 | |||
564 | if ((w < 1) || (h < 1) ) | ||
565 | { | ||
566 | *error = EVAS_LOAD_ERROR_GENERIC; | ||
567 | goto close_file; | ||
568 | } | ||
569 | } | ||
570 | |||
571 | if ((w != (int)ie->w) || (h != (int)ie->h)) | ||
572 | { | ||
573 | *error = EVAS_LOAD_ERROR_GENERIC; | ||
574 | goto close_file; | ||
575 | } | ||
576 | evas_cache_image_surface_alloc(ie, ie->w, ie->h); | ||
577 | surface = evas_cache_image_pixels(ie); | ||
578 | if (!surface) | ||
579 | { | ||
580 | *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; | ||
581 | goto close_file; | ||
582 | } | ||
583 | |||
584 | row_size = ceil((double)(image_w * bit_count) / 32) * 4; | ||
585 | |||
586 | if (bit_count < 16) | ||
587 | { | ||
588 | unsigned int i; | ||
589 | |||
590 | if (bit_count == 1) | ||
591 | { | ||
592 | if ((palette_size <= 0) || (palette_size > 2)) pal_num = 2; | ||
593 | else pal_num = palette_size; | ||
594 | } | ||
595 | else if (bit_count == 4) | ||
596 | { | ||
597 | if ((palette_size <= 0) || (palette_size > 16)) pal_num = 16; | ||
598 | else pal_num = palette_size; | ||
599 | } | ||
600 | else if (bit_count == 8) | ||
601 | { | ||
602 | if ((palette_size <= 0) || (palette_size > 256)) pal_num = 256; | ||
603 | else pal_num = palette_size; | ||
604 | } | ||
605 | pal = alloca(256 * 4); | ||
606 | for (i = 0; i < pal_num; i++) | ||
607 | { | ||
608 | if (fread(&b, 1, 1, f) != 1) goto close_file; | ||
609 | if (fread(&g, 1, 1, f) != 1) goto close_file; | ||
610 | if (fread(&r, 1, 1, f) != 1) goto close_file; | ||
611 | if ((head_size != 12) /*&& (palette_size != 0)*/) | ||
612 | { // OS/2 V1 doesn't do the pad byte | ||
613 | if (fread(&a, 1, 1, f) != 1) goto close_file; | ||
614 | } | ||
615 | a = 0xff; // fillin a as solid for paletted images | ||
616 | pal[i] = ARGB_JOIN(a, r, g, b); | ||
617 | } | ||
618 | fseek(f, offset, SEEK_SET); | ||
619 | |||
620 | if ((scale_ratio == 1) || (comp !=0)) | ||
621 | buffer = malloc(image_size + 8); // add 8 for padding to avoid checks | ||
622 | else | ||
623 | { | ||
624 | scale_surface = malloc(image_w * sizeof(DATA32)); //for one line decoding | ||
625 | if (!scale_surface) | ||
626 | { | ||
627 | *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; | ||
628 | goto close_file; | ||
629 | } | ||
630 | buffer = malloc(row_size); // scale down is usually set because of memory issue, so read line by line | ||
631 | } | ||
632 | |||
633 | if (!buffer) | ||
634 | { | ||
635 | *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; | ||
636 | goto close_file; | ||
637 | } | ||
638 | if ((scale_ratio == 1) || (comp !=0)) | ||
639 | buffer_end = buffer + image_size; | ||
640 | else | ||
641 | buffer_end = buffer + row_size; | ||
642 | p = buffer; | ||
643 | |||
644 | if ((scale_ratio == 1) || (comp !=0)) | ||
645 | { | ||
646 | if (fread(buffer, image_size, 1, f) != 1) goto close_file; | ||
647 | } | ||
648 | else | ||
649 | { | ||
650 | if (fread(buffer, row_size, 1, f) != 1) goto close_file; | ||
651 | } | ||
652 | |||
653 | if (bit_count == 1) | ||
654 | { | ||
655 | if (comp == 0) // no compression | ||
656 | { | ||
657 | pix = surface; | ||
658 | |||
659 | for (y = 0; y < h; y++) | ||
660 | { | ||
661 | if (!right_way_up) pix = surface + ((h - 1 - y) * w); | ||
662 | if (scale_ratio > 1) pix = scale_surface; // one line decoding | ||
663 | |||
664 | for (x = 0; x < image_w; x++) | ||
665 | { | ||
666 | if ((x & 0x7) == 0x0) | ||
667 | { | ||
668 | *pix = pal[*p >> 7]; | ||
669 | } | ||
670 | else if ((x & 0x7) == 0x1) | ||
671 | { | ||
672 | *pix = pal[(*p >> 6) & 0x1]; | ||
673 | } | ||
674 | else if ((x & 0x7) == 0x2) | ||
675 | { | ||
676 | *pix = pal[(*p >> 5) & 0x1]; | ||
677 | } | ||
678 | else if ((x & 0x7) == 0x3) | ||
679 | { | ||
680 | *pix = pal[(*p >> 4) & 0x1]; | ||
681 | } | ||
682 | else if ((x & 0x7) == 0x4) | ||
683 | { | ||
684 | *pix = pal[(*p >> 3) & 0x1]; | ||
685 | } | ||
686 | else if ((x & 0x7) == 0x5) | ||
687 | { | ||
688 | *pix = pal[(*p >> 2) & 0x1]; | ||
689 | } | ||
690 | else if ((x & 0x7) == 0x6) | ||
691 | { | ||
692 | *pix = pal[(*p >> 1) & 0x1]; | ||
693 | } | ||
694 | else | ||
695 | { | ||
696 | *pix = pal[*p & 0x1]; | ||
697 | p++; | ||
698 | } | ||
699 | if (p >= buffer_end) break; | ||
700 | pix++; | ||
701 | } | ||
702 | |||
703 | if (scale_ratio > 1) | ||
704 | { | ||
705 | if (!right_way_up) scale_pix = surface + ((h - 1 - y) * w); | ||
706 | else scale_pix = surface + (y * w); | ||
707 | |||
708 | pix = scale_surface; | ||
709 | for (x = 0; x < w; x++) | ||
710 | { | ||
711 | *scale_pix = *pix; | ||
712 | scale_pix ++; | ||
713 | pix += scale_ratio; | ||
714 | } | ||
715 | read_line += scale_ratio; | ||
716 | if (read_line >= image_h) break; | ||
717 | |||
718 | fseek(f, row_size * (scale_ratio - 1), SEEK_CUR); | ||
719 | if (fread(buffer, row_size, 1, f) != 1) goto close_file; | ||
720 | p = buffer; | ||
721 | buffer_end = buffer + row_size; | ||
722 | } | ||
723 | else | ||
724 | { | ||
725 | if ((x & 0x7) != 0) p++; | ||
726 | fix = (int)(((unsigned long)p) & 0x3); | ||
727 | if (fix > 0) p += 4 - fix; // align row read | ||
728 | if (p >= buffer_end) break; | ||
729 | } | ||
730 | } | ||
731 | } | ||
732 | else | ||
733 | goto close_file; | ||
734 | } | ||
735 | else if (bit_count == 4) | ||
736 | { | ||
737 | if (comp == 0) // no compression | ||
738 | { | ||
739 | pix = surface; | ||
740 | for (y = 0; y < h; y++) | ||
741 | { | ||
742 | if (!right_way_up) pix = surface + ((h - 1 - y) * w); | ||
743 | if (scale_ratio > 1) pix = scale_surface; // one line decoding | ||
744 | for (x = 0; x < image_w; x++) | ||
745 | { | ||
746 | if ((x & 0x1) == 0x1) | ||
747 | { | ||
748 | *pix = pal[*p & 0x0f]; | ||
749 | p++; | ||
750 | } | ||
751 | else | ||
752 | { | ||
753 | *pix = pal[*p >> 4]; | ||
754 | } | ||
755 | if (p >= buffer_end) break; | ||
756 | pix++; | ||
757 | } | ||
758 | if (scale_ratio > 1) | ||
759 | { | ||
760 | if (!right_way_up) scale_pix = surface + ((h - 1 - y) * w); | ||
761 | else scale_pix = surface + (y * w); | ||
762 | |||
763 | pix = scale_surface; | ||
764 | for (x = 0; x < w; x++) | ||
765 | { | ||
766 | *scale_pix = *pix; | ||
767 | scale_pix ++; | ||
768 | pix += scale_ratio; | ||
769 | } | ||
770 | read_line += scale_ratio; | ||
771 | if (read_line >= image_h) break; | ||
772 | |||
773 | fseek(f, row_size * (scale_ratio - 1), SEEK_CUR); | ||
774 | if (fread(buffer, row_size, 1, f) != 1) goto close_file; | ||
775 | p = buffer; | ||
776 | buffer_end = buffer + row_size; | ||
777 | } | ||
778 | else | ||
779 | { | ||
780 | if ((x & 0x1) != 0) p++; | ||
781 | fix = (int)(((unsigned long)p) & 0x3); | ||
782 | if (fix > 0) p += 4 - fix; // align row read | ||
783 | if (p >= buffer_end) break; | ||
784 | } | ||
785 | } | ||
786 | } | ||
787 | else if (comp == 2) // rle 4bit/pixel | ||
788 | { | ||
789 | int count = 0, done = 0, wpad; | ||
790 | int scale_x = 0, scale_y = 0; | ||
791 | Eina_Bool scale_down_line = EINA_TRUE; | ||
792 | |||
793 | pix = surface; | ||
794 | if (!right_way_up) pix = surface + ((h - 1 - y) * w); | ||
795 | wpad = ((image_w + 1) / 2) * 2; | ||
796 | while (p < buffer_end) | ||
797 | { | ||
798 | if (p[0]) | ||
799 | { | ||
800 | if (scale_down_line) | ||
801 | { | ||
802 | if ((x + p[0]) <= wpad) | ||
803 | { | ||
804 | unsigned int col1 = pal[p[1] >> 4]; | ||
805 | unsigned int col2 = pal[p[1] & 0xf]; | ||
806 | |||
807 | count = p[0] / 2; | ||
808 | while (count > 0) | ||
809 | { | ||
810 | if (x < w) | ||
811 | { | ||
812 | if (((x % scale_ratio) == 0) && (scale_x < w)) | ||
813 | { | ||
814 | *pix = col1; | ||
815 | pix++; | ||
816 | scale_x++; | ||
817 | } | ||
818 | x++; | ||
819 | } | ||
820 | if (x < w) | ||
821 | { | ||
822 | if (((x % scale_ratio) == 0) && (scale_x < w)) | ||
823 | { | ||
824 | *pix = col2; | ||
825 | pix++; | ||
826 | scale_x++; | ||
827 | } | ||
828 | x++; | ||
829 | } | ||
830 | count--; | ||
831 | } | ||
832 | if (p[0] & 0x1) | ||
833 | { | ||
834 | if (((x % scale_ratio) == 0) && (scale_x < w)) | ||
835 | { | ||
836 | *pix = col1; | ||
837 | pix++; | ||
838 | scale_x++; | ||
839 | } | ||
840 | x++; | ||
841 | } | ||
842 | } | ||
843 | } | ||
844 | p += 2; | ||
845 | } | ||
846 | else | ||
847 | { | ||
848 | switch (p[1]) | ||
849 | { | ||
850 | case 0: // EOL | ||
851 | x = 0; | ||
852 | scale_x = 0; | ||
853 | y++; | ||
854 | if ((y % scale_ratio) == 0) | ||
855 | { | ||
856 | scale_y++; | ||
857 | scale_down_line = EINA_TRUE; | ||
858 | if (!right_way_up) | ||
859 | pix = surface + ((h - 1 - scale_y) * w); | ||
860 | else | ||
861 | pix = surface + (scale_y * w); | ||
862 | } | ||
863 | else | ||
864 | scale_down_line = EINA_FALSE; | ||
865 | if (scale_y >= h) | ||
866 | { | ||
867 | p = buffer_end; | ||
868 | } | ||
869 | p += 2; | ||
870 | break; | ||
871 | case 1: // EOB | ||
872 | p = buffer_end; | ||
873 | break; | ||
874 | case 2: // DELTA | ||
875 | x += p[2]; | ||
876 | y += p[3]; | ||
877 | scale_x = x / scale_ratio; | ||
878 | scale_y = y / scale_ratio; | ||
879 | if ((scale_x >= w) || (scale_y >= h)) | ||
880 | { | ||
881 | p = buffer_end; | ||
882 | } | ||
883 | if (!right_way_up) | ||
884 | pix = surface + scale_x + ((h - 1 - scale_y) * w); | ||
885 | else | ||
886 | pix = surface + scale_x + (scale_y * w); | ||
887 | p += 4; | ||
888 | break; | ||
889 | default: | ||
890 | count = p[1]; | ||
891 | if (((p + count) > buffer_end) || | ||
892 | ((x + count) > w)) | ||
893 | { | ||
894 | p = buffer_end; | ||
895 | break; | ||
896 | } | ||
897 | p += 2; | ||
898 | done = count; | ||
899 | count /= 2; | ||
900 | while (count > 0) | ||
901 | { | ||
902 | if (((x % scale_ratio) == 0) && (scale_x < w)) | ||
903 | { | ||
904 | *pix = pal[*p >> 4]; | ||
905 | pix++; | ||
906 | scale_x++; | ||
907 | } | ||
908 | x++; | ||
909 | if (((x % scale_ratio) == 0) && (scale_x < w)) | ||
910 | { | ||
911 | *pix = pal[*p & 0xf]; | ||
912 | pix++; | ||
913 | scale_x++; | ||
914 | } | ||
915 | x++; | ||
916 | |||
917 | p++; | ||
918 | count--; | ||
919 | } | ||
920 | |||
921 | if (done & 0x1) | ||
922 | { | ||
923 | if (((x % scale_ratio) == 0) && (scale_x < w)) | ||
924 | { | ||
925 | *pix = pal[*p >> 4]; | ||
926 | scale_x++; | ||
927 | } | ||
928 | x++; | ||
929 | p++; | ||
930 | } | ||
931 | if ((done & 0x3) == 0x1) | ||
932 | p += 2; | ||
933 | else if ((done & 0x3) == 0x2) | ||
934 | p += 1; | ||
935 | break; | ||
936 | } | ||
937 | } | ||
938 | } | ||
939 | } | ||
940 | else | ||
941 | goto close_file; | ||
942 | } | ||
943 | else if (bit_count == 8) | ||
944 | { | ||
945 | if (comp == 0) // no compression | ||
946 | { | ||
947 | pix = surface; | ||
948 | for (y = 0; y < h; y++) | ||
949 | { | ||
950 | if (!right_way_up) pix = surface + ((h - 1 - y) * w); | ||
951 | for (x = 0; x < w; x++) | ||
952 | { | ||
953 | *pix = pal[*p]; | ||
954 | p += scale_ratio; | ||
955 | if (p >= buffer_end) break; | ||
956 | pix++; | ||
957 | } | ||
958 | if (scale_ratio > 1) | ||
959 | { | ||
960 | read_line += scale_ratio; | ||
961 | if (read_line >= image_h) break; | ||
962 | |||
963 | fseek(f, row_size * (scale_ratio - 1), SEEK_CUR); | ||
964 | if (fread(buffer, row_size, 1, f) != 1) goto close_file; | ||
965 | p = buffer; | ||
966 | buffer_end = buffer + row_size; | ||
967 | } | ||
968 | else | ||
969 | { | ||
970 | fix = (int)(((unsigned long)p) & 0x3); | ||
971 | if (fix > 0) p += 4 - fix; // align row read | ||
972 | if (p >= buffer_end) break; | ||
973 | } | ||
974 | } | ||
975 | } | ||
976 | else if (comp == 1) // rle 8bit/pixel | ||
977 | { | ||
978 | int count = 0, done = 0; | ||
979 | int scale_x = 0, scale_y = 0; | ||
980 | Eina_Bool scale_down_line = EINA_TRUE; | ||
981 | |||
982 | pix = surface; | ||
983 | if (!right_way_up) pix = surface + ((h - 1 - y) * w); | ||
984 | |||
985 | while (p < buffer_end) | ||
986 | { | ||
987 | if (p[0]) | ||
988 | { | ||
989 | if (scale_down_line) | ||
990 | { | ||
991 | if ((x + p[0]) <= image_w) | ||
992 | { | ||
993 | unsigned int col = pal[p[1]]; | ||
994 | |||
995 | count = p[0]; | ||
996 | while (count > 0) | ||
997 | { | ||
998 | if (((x % scale_ratio) == 0) && (scale_x < w)) | ||
999 | { | ||
1000 | *pix = col; | ||
1001 | pix++; | ||
1002 | scale_x ++; | ||
1003 | } | ||
1004 | x++; | ||
1005 | count--; | ||
1006 | } | ||
1007 | } | ||
1008 | } | ||
1009 | p += 2; | ||
1010 | } | ||
1011 | else | ||
1012 | { | ||
1013 | switch (p[1]) | ||
1014 | { | ||
1015 | case 0: // EOL | ||
1016 | x = 0; | ||
1017 | scale_x = 0; | ||
1018 | y++; | ||
1019 | if ((y % scale_ratio) == 0) | ||
1020 | { | ||
1021 | scale_y++; | ||
1022 | scale_down_line = EINA_TRUE; | ||
1023 | if (!right_way_up) | ||
1024 | pix = surface + ((h - 1 - scale_y) * w); | ||
1025 | else | ||
1026 | pix = surface + (scale_y * w); | ||
1027 | } | ||
1028 | else | ||
1029 | scale_down_line = EINA_FALSE; | ||
1030 | |||
1031 | if (scale_y >= h) | ||
1032 | { | ||
1033 | p = buffer_end; | ||
1034 | } | ||
1035 | p += 2; | ||
1036 | break; | ||
1037 | case 1: // EOB | ||
1038 | p = buffer_end; | ||
1039 | break; | ||
1040 | case 2: // DELTA | ||
1041 | x += p[2]; | ||
1042 | y += p[3]; | ||
1043 | scale_x = x / scale_ratio; | ||
1044 | scale_y = y / scale_ratio; | ||
1045 | if ((scale_x >= w) || (scale_y >= h)) | ||
1046 | { | ||
1047 | p = buffer_end; | ||
1048 | } | ||
1049 | if (!right_way_up) | ||
1050 | pix = surface + scale_x + ((h - 1 - scale_y) * w); | ||
1051 | else | ||
1052 | pix = surface + scale_x + (scale_y * w); | ||
1053 | p += 4; | ||
1054 | break; | ||
1055 | default: | ||
1056 | count = p[1]; | ||
1057 | if (((p + count) > buffer_end) || | ||
1058 | ((x + count) > image_w)) | ||
1059 | { | ||
1060 | p = buffer_end; | ||
1061 | break; | ||
1062 | } | ||
1063 | p += 2; | ||
1064 | done = count; | ||
1065 | while (count > 0) | ||
1066 | { | ||
1067 | if (((x % scale_ratio) == 0) && (scale_x < w)) | ||
1068 | { | ||
1069 | *pix = pal[*p]; | ||
1070 | pix++; | ||
1071 | scale_x ++; | ||
1072 | } | ||
1073 | p++; | ||
1074 | x++; | ||
1075 | count--; | ||
1076 | } | ||
1077 | if (done & 0x1) p++; | ||
1078 | break; | ||
1079 | } | ||
1080 | } | ||
1081 | } | ||
1082 | } | ||
1083 | else | ||
1084 | goto close_file; | ||
1085 | } | ||
1086 | } | ||
1087 | else if ((bit_count == 16) || (bit_count == 24) || (bit_count == 32)) | ||
1088 | { | ||
1089 | if (comp == 0) // no compression | ||
1090 | { | ||
1091 | fseek(f, offset, SEEK_SET); | ||
1092 | if (scale_ratio == 1) | ||
1093 | buffer = malloc(image_size + 8); // add 8 for padding to avoid checks | ||
1094 | else | ||
1095 | buffer = malloc(row_size); // scale down is usually set because of memory issue, so read line by line | ||
1096 | if (!buffer) | ||
1097 | { | ||
1098 | *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; | ||
1099 | goto close_file; | ||
1100 | } | ||
1101 | if (scale_ratio == 1) | ||
1102 | buffer_end = buffer + image_size; | ||
1103 | else | ||
1104 | buffer_end = buffer + row_size; | ||
1105 | |||
1106 | p = buffer; | ||
1107 | if (scale_ratio == 1) | ||
1108 | { | ||
1109 | if (fread(buffer, image_size, 1, f) != 1) goto close_file; | ||
1110 | } | ||
1111 | else | ||
1112 | { | ||
1113 | if (fread(buffer, row_size, 1, f) != 1) goto close_file; | ||
1114 | } | ||
1115 | if (bit_count == 16) | ||
1116 | { | ||
1117 | unsigned short tmp; | ||
1118 | |||
1119 | pix = surface; | ||
1120 | for (y = 0; y < h; y++) | ||
1121 | { | ||
1122 | if (!right_way_up) pix = surface + ((h - 1 - y) * w); | ||
1123 | for (x = 0; x < w; x++) | ||
1124 | { | ||
1125 | tmp = *((unsigned short *)(p)); | ||
1126 | |||
1127 | r = (tmp >> 7) & 0xf8; r |= r >> 5; | ||
1128 | g = (tmp >> 2) & 0xf8; g |= g >> 5; | ||
1129 | b = (tmp << 3) & 0xf8; b |= b >> 5; | ||
1130 | *pix = ARGB_JOIN(0xff, r, g, b); | ||
1131 | |||
1132 | p += 2 * scale_ratio; | ||
1133 | |||
1134 | if (p >= buffer_end) break; | ||
1135 | pix++; | ||
1136 | } | ||
1137 | if (scale_ratio > 1) | ||
1138 | { | ||
1139 | read_line += scale_ratio; | ||
1140 | if (read_line >= image_h) break; | ||
1141 | |||
1142 | fseek(f, row_size * (scale_ratio - 1), SEEK_CUR); | ||
1143 | if (fread(buffer, row_size, 1, f) != 1) goto close_file; | ||
1144 | p = buffer; | ||
1145 | buffer_end = buffer + row_size; | ||
1146 | } | ||
1147 | else | ||
1148 | { | ||
1149 | fix = (int)(((unsigned long)p) & 0x3); | ||
1150 | if (fix > 0) p += 4 - fix; // align row read | ||
1151 | if (p >= buffer_end) break; | ||
1152 | } | ||
1153 | } | ||
1154 | } | ||
1155 | else if (bit_count == 24) | ||
1156 | { | ||
1157 | pix = surface; | ||
1158 | for (y = 0; y < h; y++) | ||
1159 | { | ||
1160 | if (!right_way_up) pix = surface + ((h - 1 - y) * w); | ||
1161 | for (x = 0; x < w; x++) | ||
1162 | { | ||
1163 | b = p[0]; | ||
1164 | g = p[1]; | ||
1165 | r = p[2]; | ||
1166 | *pix = ARGB_JOIN(0xff, r, g, b); | ||
1167 | p += 3 * scale_ratio; | ||
1168 | if (p >= buffer_end) break; | ||
1169 | pix++; | ||
1170 | } | ||
1171 | if (scale_ratio > 1) | ||
1172 | { | ||
1173 | read_line += scale_ratio; | ||
1174 | if (read_line >= image_h) break; | ||
1175 | |||
1176 | fseek(f, row_size * (scale_ratio - 1), SEEK_CUR); | ||
1177 | if (fread(buffer, row_size, 1, f) != 1) goto close_file; | ||
1178 | p = buffer; | ||
1179 | buffer_end = buffer + row_size; | ||
1180 | } | ||
1181 | else | ||
1182 | { | ||
1183 | fix = (int)(((unsigned long)p) & 0x3); | ||
1184 | if (fix > 0) p += 4 - fix; // align row read | ||
1185 | if (p >= buffer_end) break; | ||
1186 | } | ||
1187 | } | ||
1188 | } | ||
1189 | else if (bit_count == 32) | ||
1190 | { | ||
1191 | int none_zero_alpha = 0; | ||
1192 | pix = surface; | ||
1193 | for (y = 0; y < h; y++) | ||
1194 | { | ||
1195 | if (!right_way_up) pix = surface + ((h - 1 - y) * w); | ||
1196 | for (x = 0; x < w; x++) | ||
1197 | { | ||
1198 | b = p[0]; | ||
1199 | g = p[1]; | ||
1200 | r = p[2]; | ||
1201 | a = p[3]; | ||
1202 | if (a) none_zero_alpha = 1; | ||
1203 | if (!hasa) a = 0xff; | ||
1204 | *pix = ARGB_JOIN(a, r, g, b); | ||
1205 | p += 4 * scale_ratio; | ||
1206 | |||
1207 | if (p >= buffer_end) break; | ||
1208 | pix++; | ||
1209 | } | ||
1210 | if (scale_ratio > 1) | ||
1211 | { | ||
1212 | read_line += scale_ratio; | ||
1213 | if (read_line >= image_h) break; | ||
1214 | |||
1215 | fseek(f, row_size * (scale_ratio - 1), SEEK_CUR); | ||
1216 | if (fread(buffer, row_size, 1, f) != 1) goto close_file; | ||
1217 | p = buffer; | ||
1218 | buffer_end = buffer + row_size; | ||
1219 | } | ||
1220 | else | ||
1221 | { | ||
1222 | fix = (int)(((unsigned long)p) & 0x3); | ||
1223 | if (fix > 0) p += 4 - fix; // align row read | ||
1224 | if (p >= buffer_end) break; | ||
1225 | } | ||
1226 | } | ||
1227 | if (!none_zero_alpha) | ||
1228 | { | ||
1229 | ie->flags.alpha = 0; | ||
1230 | if (hasa) | ||
1231 | { | ||
1232 | unsigned int *pixend = surface + (w * h); | ||
1233 | |||
1234 | for (pix = surface; pix < pixend; pix++) | ||
1235 | A_VAL(pix) = 0xff; | ||
1236 | } | ||
1237 | } | ||
1238 | } | ||
1239 | else | ||
1240 | goto close_file; | ||
1241 | } | ||
1242 | else if (comp == 3) // bit field | ||
1243 | { | ||
1244 | if (!read_uint(f, &rmask)) goto close_file; | ||
1245 | if (!read_uint(f, &gmask)) goto close_file; | ||
1246 | if (!read_uint(f, &bmask)) goto close_file; | ||
1247 | |||
1248 | fseek(f, offset, SEEK_SET); | ||
1249 | if (scale_ratio == 1) | ||
1250 | buffer = malloc(image_size + 8); // add 8 for padding to avoid checks | ||
1251 | else | ||
1252 | buffer = malloc(row_size); // scale down is usually set because of memory issue, so read line by line | ||
1253 | |||
1254 | if (!buffer) | ||
1255 | { | ||
1256 | *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; | ||
1257 | goto close_file; | ||
1258 | } | ||
1259 | if (scale_ratio == 1) | ||
1260 | buffer_end = buffer + image_size; | ||
1261 | else | ||
1262 | buffer_end = buffer + row_size; | ||
1263 | |||
1264 | p = buffer; | ||
1265 | if (scale_ratio == 1) | ||
1266 | { | ||
1267 | if (fread(buffer, image_size, 1, f) != 1) goto close_file; | ||
1268 | } | ||
1269 | else | ||
1270 | { | ||
1271 | if (fread(buffer, row_size, 1, f) != 1) goto close_file; | ||
1272 | } | ||
1273 | |||
1274 | if ((bit_count == 16) && | ||
1275 | (rmask == 0xf800) && (gmask == 0x07e0) && (bmask == 0x001f) | ||
1276 | ) | ||
1277 | { | ||
1278 | unsigned short tmp; | ||
1279 | |||
1280 | pix = surface; | ||
1281 | for (y = 0; y < h; y++) | ||
1282 | { | ||
1283 | if (!right_way_up) pix = surface + ((h - 1 - y) * w); | ||
1284 | for (x = 0; x < w; x++) | ||
1285 | { | ||
1286 | tmp = *((unsigned short *)(p)); | ||
1287 | |||
1288 | r = (tmp >> 8) & 0xf8; r |= r >> 5; | ||
1289 | g = (tmp >> 3) & 0xfc; g |= g >> 6; | ||
1290 | b = (tmp << 3) & 0xf8; b |= b >> 5; | ||
1291 | *pix = ARGB_JOIN(0xff, r, g, b); | ||
1292 | |||
1293 | p += 2 * scale_ratio; | ||
1294 | |||
1295 | if (p >= buffer_end) break; | ||
1296 | pix++; | ||
1297 | } | ||
1298 | if (scale_ratio > 1) | ||
1299 | { | ||
1300 | read_line += scale_ratio; | ||
1301 | if (read_line >= image_h) break; | ||
1302 | fseek(f, row_size * (scale_ratio - 1), SEEK_CUR); | ||
1303 | if (fread(buffer, row_size, 1, f) != 1) goto close_file; | ||
1304 | p = buffer; | ||
1305 | buffer_end = buffer + row_size; | ||
1306 | } | ||
1307 | else | ||
1308 | { | ||
1309 | fix = (int)(((unsigned long)p) & 0x3); | ||
1310 | if (fix > 0) p += 4 - fix; // align row read | ||
1311 | if (p >= buffer_end) break; | ||
1312 | } | ||
1313 | } | ||
1314 | } | ||
1315 | else if ((bit_count == 16) && | ||
1316 | (rmask == 0x7c00) && (gmask == 0x03e0) && (bmask == 0x001f) | ||
1317 | ) | ||
1318 | { | ||
1319 | unsigned short tmp; | ||
1320 | pix = surface; | ||
1321 | for (y = 0; y < h; y++) | ||
1322 | { | ||
1323 | if (!right_way_up) pix = surface + ((h - 1 - y) * w); | ||
1324 | for (x = 0; x < w; x++) | ||
1325 | { | ||
1326 | tmp = *((unsigned short *)(p)); | ||
1327 | |||
1328 | r = (tmp >> 7) & 0xf8; r |= r >> 5; | ||
1329 | g = (tmp >> 2) & 0xf8; g |= g >> 5; | ||
1330 | b = (tmp << 3) & 0xf8; b |= b >> 5; | ||
1331 | *pix = ARGB_JOIN(0xff, r, g, b); | ||
1332 | p += 2 * scale_ratio; | ||
1333 | |||
1334 | if (p >= buffer_end) break; | ||
1335 | pix++; | ||
1336 | } | ||
1337 | if (scale_ratio > 1) | ||
1338 | { | ||
1339 | read_line += scale_ratio; | ||
1340 | if (read_line >= image_h) break; | ||
1341 | fseek(f, row_size * (scale_ratio - 1), SEEK_CUR); | ||
1342 | if (fread(buffer, row_size, 1, f) != 1) goto close_file; | ||
1343 | p = buffer; | ||
1344 | buffer_end = buffer + row_size; | ||
1345 | } | ||
1346 | else | ||
1347 | { | ||
1348 | fix = (int)(((unsigned long)p) & 0x3); | ||
1349 | if (fix > 0) p += 4 - fix; // align row read | ||
1350 | if (p >= buffer_end) break; | ||
1351 | } | ||
1352 | } | ||
1353 | } | ||
1354 | else if (bit_count == 32) | ||
1355 | { | ||
1356 | pix = surface; | ||
1357 | for (y = 0; y < h; y++) | ||
1358 | { | ||
1359 | if (!right_way_up) pix = surface + ((h - 1 - y) * w); | ||
1360 | for (x = 0; x < w; x++) | ||
1361 | { | ||
1362 | b = p[0]; | ||
1363 | g = p[1]; | ||
1364 | r = p[2]; | ||
1365 | a = p[3]; | ||
1366 | if (!hasa) a = 0xff; | ||
1367 | *pix = ARGB_JOIN(a, r, g, b); | ||
1368 | |||
1369 | p += 4 * scale_ratio; | ||
1370 | |||
1371 | if (p >= buffer_end) break; | ||
1372 | pix++; | ||
1373 | } | ||
1374 | if (scale_ratio > 1) | ||
1375 | { | ||
1376 | read_line += scale_ratio; | ||
1377 | if (read_line >= image_h) break; | ||
1378 | fseek(f, row_size * (scale_ratio - 1), SEEK_CUR); | ||
1379 | if (fread(buffer, row_size, 1, f) != 1) goto close_file; | ||
1380 | p = buffer; | ||
1381 | buffer_end = buffer + row_size; | ||
1382 | } | ||
1383 | else | ||
1384 | { | ||
1385 | fix = (int)(((unsigned long)p) & 0x3); | ||
1386 | if (fix > 0) p += 4 - fix; // align row read | ||
1387 | if (p >= buffer_end) break; | ||
1388 | } | ||
1389 | } | ||
1390 | } | ||
1391 | else | ||
1392 | goto close_file; | ||
1393 | } | ||
1394 | else if (comp == 4) // jpeg - only printer drivers | ||
1395 | { | ||
1396 | goto close_file; | ||
1397 | } | ||
1398 | else if (comp == 3) // png - only printer drivers | ||
1399 | { | ||
1400 | goto close_file; | ||
1401 | } | ||
1402 | else | ||
1403 | goto close_file; | ||
1404 | } | ||
1405 | else | ||
1406 | goto close_file; | ||
1407 | |||
1408 | if (buffer) free(buffer); | ||
1409 | if (scale_surface) free(scale_surface); | ||
1410 | fclose(f); | ||
1411 | |||
1412 | evas_common_image_premul(ie); | ||
1413 | *error = EVAS_LOAD_ERROR_NONE; | ||
1414 | return EINA_TRUE; | ||
1415 | |||
1416 | close_file: | ||
1417 | if (buffer) free(buffer); | ||
1418 | if (scale_surface) free(scale_surface); | ||
1419 | fclose(f); | ||
1420 | return EINA_FALSE; | ||
1421 | } | ||
1422 | |||
1423 | static int | ||
1424 | module_open(Evas_Module *em) | ||
1425 | { | ||
1426 | if (!em) return 0; | ||
1427 | em->functions = (void *)(&evas_image_load_bmp_func); | ||
1428 | return 1; | ||
1429 | } | ||
1430 | |||
1431 | static void | ||
1432 | module_close(Evas_Module *em __UNUSED__) | ||
1433 | { | ||
1434 | } | ||
1435 | |||
1436 | static Evas_Module_Api evas_modapi = | ||
1437 | { | ||
1438 | EVAS_MODULE_API_VERSION, | ||
1439 | "bmp", | ||
1440 | "none", | ||
1441 | { | ||
1442 | module_open, | ||
1443 | module_close | ||
1444 | } | ||
1445 | }; | ||
1446 | |||
1447 | EVAS_MODULE_DEFINE(EVAS_MODULE_TYPE_IMAGE_LOADER, image_loader, bmp); | ||
1448 | |||
1449 | #ifndef EVAS_STATIC_BUILD_BMP | ||
1450 | EVAS_EINA_MODULE_DEFINE(image_loader, bmp); | ||
1451 | #endif | ||