diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/evas/src/modules/loaders/ico/evas_image_load_ico.c | 770 |
1 files changed, 770 insertions, 0 deletions
diff --git a/libraries/evas/src/modules/loaders/ico/evas_image_load_ico.c b/libraries/evas/src/modules/loaders/ico/evas_image_load_ico.c new file mode 100644 index 0000000..17a7f7e --- /dev/null +++ b/libraries/evas/src/modules/loaders/ico/evas_image_load_ico.c | |||
@@ -0,0 +1,770 @@ | |||
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 "evas_common.h" | ||
12 | #include "evas_private.h" | ||
13 | |||
14 | static Eina_Bool evas_image_load_file_head_ico(Image_Entry *ie, const char *file, const char *key, int *error) EINA_ARG_NONNULL(1, 2, 4); | ||
15 | static Eina_Bool evas_image_load_file_data_ico(Image_Entry *ie, const char *file, const char *key, int *error) EINA_ARG_NONNULL(1, 2, 4); | ||
16 | |||
17 | static Evas_Image_Load_Func evas_image_load_ico_func = | ||
18 | { | ||
19 | EINA_TRUE, | ||
20 | evas_image_load_file_head_ico, | ||
21 | evas_image_load_file_data_ico, | ||
22 | NULL | ||
23 | }; | ||
24 | |||
25 | static int | ||
26 | read_ushort(FILE *file, unsigned short *ret) | ||
27 | { | ||
28 | unsigned char b[2]; | ||
29 | if (fread(b, sizeof(unsigned char), 2, file) != 2) return 0; | ||
30 | *ret = (b[1] << 8) | b[0]; | ||
31 | return 1; | ||
32 | } | ||
33 | |||
34 | static int | ||
35 | read_uint(FILE *file, unsigned int *ret) | ||
36 | { | ||
37 | unsigned char b[4]; | ||
38 | if (fread(b, sizeof(unsigned char), 4, file) != 4) return 0; | ||
39 | *ret = ARGB_JOIN(b[3], b[2], b[1], b[0]); | ||
40 | return 1; | ||
41 | } | ||
42 | |||
43 | enum | ||
44 | { | ||
45 | SMALLEST, | ||
46 | BIGGEST, | ||
47 | SMALLER, | ||
48 | BIGGER | ||
49 | }; | ||
50 | |||
51 | enum | ||
52 | { | ||
53 | ICON = 1, | ||
54 | CURSOR = 2 | ||
55 | }; | ||
56 | |||
57 | static Eina_Bool | ||
58 | evas_image_load_file_head_ico(Image_Entry *ie, const char *file, const char *key, int *error) | ||
59 | { | ||
60 | unsigned short word; | ||
61 | unsigned char byte; | ||
62 | FILE *f; | ||
63 | int wanted_w = 0, wanted_h = 0, w, h, cols, i, planes = 0, | ||
64 | hot_x = 0, hot_y = 0, bpp = 0, pdelta, search = -1, have_choice = 0, | ||
65 | hasa = 1; | ||
66 | unsigned int bmoffset, bmsize, fsize; | ||
67 | unsigned short reserved, type, count; | ||
68 | struct { | ||
69 | int pdelta; | ||
70 | int w, h; | ||
71 | int cols; | ||
72 | int bpp, planes; | ||
73 | int hot_x, hot_y; | ||
74 | unsigned int bmoffset, bmsize; | ||
75 | } chosen = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||
76 | |||
77 | f = fopen(file, "rb"); | ||
78 | if (!f) | ||
79 | { | ||
80 | *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST; | ||
81 | return EINA_FALSE; | ||
82 | } | ||
83 | |||
84 | *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT; | ||
85 | fseek(f, 0, SEEK_END); | ||
86 | fsize = ftell(f); | ||
87 | fseek(f, 0, SEEK_SET); | ||
88 | if (fsize < (6 + 16 + 40)) goto close_file; | ||
89 | |||
90 | // key: | ||
91 | // NULL == highest res | ||
92 | // biggest == highest res | ||
93 | // smallest == lowest res | ||
94 | // | ||
95 | // smaller == next size SMALLER than load opts WxH (if possible) | ||
96 | // bigger == next size BIGGER than load opts WxH (if possible) | ||
97 | // more ? | ||
98 | |||
99 | search = BIGGEST; | ||
100 | if ((ie->load_opts.w > 0) && (ie->load_opts.h > 0)) | ||
101 | { | ||
102 | wanted_w = ie->load_opts.w; | ||
103 | wanted_h = ie->load_opts.h; | ||
104 | search = SMALLER; | ||
105 | } | ||
106 | |||
107 | if (!read_ushort(f, &reserved)) goto close_file; | ||
108 | if (!read_ushort(f, &type)) goto close_file; | ||
109 | if (!read_ushort(f, &count)) goto close_file; | ||
110 | if (!((reserved == 0) && | ||
111 | ((type == ICON) || (type == CURSOR)) && (count > 0))) | ||
112 | goto close_file; | ||
113 | *error = EVAS_LOAD_ERROR_CORRUPT_FILE; | ||
114 | |||
115 | if (key) | ||
116 | { | ||
117 | if (!strcmp(key, "biggest")) | ||
118 | { | ||
119 | wanted_w = 0; | ||
120 | wanted_h = 0; | ||
121 | search = BIGGEST; | ||
122 | chosen.pdelta = 0; | ||
123 | } | ||
124 | else if (!strcmp(key, "smallest")) | ||
125 | { | ||
126 | wanted_w = 1; | ||
127 | wanted_h = 1; | ||
128 | search = SMALLEST; | ||
129 | chosen.pdelta = 0x7fffffff; | ||
130 | } | ||
131 | else if (!strcmp(key, "smaller")) | ||
132 | { | ||
133 | chosen.pdelta = 0x7fffffff; | ||
134 | search = SMALLER; | ||
135 | } | ||
136 | else if (!strcmp(key, "bigger")) | ||
137 | { | ||
138 | chosen.pdelta = 0x7fffffff; | ||
139 | search = BIGGER; | ||
140 | } | ||
141 | } | ||
142 | for (i = 0; i < count; i++) | ||
143 | { | ||
144 | if (fread(&byte, 1, 1, f) != 1) goto close_file; | ||
145 | w = byte; | ||
146 | if (w <= 0) w = 256; | ||
147 | if (fread(&byte, 1, 1, f) != 1) goto close_file; | ||
148 | h = byte; | ||
149 | if (h <= 0) h = 256; | ||
150 | if (fread(&byte, 1, 1, f) != 1) goto close_file; | ||
151 | cols = byte; | ||
152 | if (cols <= 0) cols = 256; | ||
153 | if (fread(&byte, 1, 1, f) != 1) goto close_file; | ||
154 | if (!read_ushort(f, &word)) goto close_file; | ||
155 | if (type == CURSOR) planes = word; | ||
156 | else hot_x = word; | ||
157 | if (!read_ushort(f, &word)) goto close_file; | ||
158 | if (type == CURSOR) bpp = word; | ||
159 | else hot_y = word; | ||
160 | if (!read_uint(f, &bmsize)) goto close_file; | ||
161 | if (!read_uint(f, &bmoffset)) goto close_file; | ||
162 | if ((bmsize <= 0) || (bmoffset <= 0) || (bmoffset >= fsize)) goto close_file; | ||
163 | if (search == BIGGEST) | ||
164 | { | ||
165 | pdelta = w * h; | ||
166 | if ((!have_choice) || | ||
167 | ((pdelta >= chosen.pdelta) && | ||
168 | (((bpp >= 3) && (bpp >= chosen.bpp)) || | ||
169 | ((bpp < 3) && (cols >= chosen.cols))))) | ||
170 | { | ||
171 | have_choice = 1; | ||
172 | chosen.pdelta = pdelta; | ||
173 | chosen.w = w; | ||
174 | chosen.h = h; | ||
175 | chosen.cols = cols; | ||
176 | chosen.bpp = bpp; | ||
177 | chosen.planes = planes; | ||
178 | chosen.bmsize = bmsize; | ||
179 | chosen.bmoffset = bmoffset; | ||
180 | } | ||
181 | } | ||
182 | else | ||
183 | { | ||
184 | if (search == SMALLEST) | ||
185 | { | ||
186 | pdelta = w * h; | ||
187 | if ((!have_choice) || | ||
188 | ((pdelta <= chosen.pdelta) && | ||
189 | (((bpp >= 3) && (bpp >= chosen.bpp)) || | ||
190 | ((bpp < 3) && (cols >= chosen.cols))))) | ||
191 | { | ||
192 | have_choice = 1; | ||
193 | chosen.pdelta = pdelta; | ||
194 | chosen.w = w; | ||
195 | chosen.h = h; | ||
196 | chosen.cols = cols; | ||
197 | chosen.bpp = bpp; | ||
198 | chosen.planes = planes; | ||
199 | chosen.bmsize = bmsize; | ||
200 | chosen.bmoffset = bmoffset; | ||
201 | } | ||
202 | } | ||
203 | else if (search == SMALLER) | ||
204 | { | ||
205 | pdelta = (wanted_w * wanted_h) - (w * h); | ||
206 | if ((!have_choice) || | ||
207 | ((w <= wanted_w) && (h <= wanted_h) && | ||
208 | (pdelta <= chosen.pdelta) && | ||
209 | (((bpp >= 3) && (bpp >= chosen.bpp)) || | ||
210 | ((bpp < 3) && (cols >= chosen.cols))))) | ||
211 | { | ||
212 | have_choice = 1; | ||
213 | if (pdelta < 0) pdelta = 0x7fffffff; | ||
214 | chosen.pdelta = pdelta; | ||
215 | chosen.w = w; | ||
216 | chosen.h = h; | ||
217 | chosen.cols = cols; | ||
218 | chosen.bpp = bpp; | ||
219 | chosen.planes = planes; | ||
220 | chosen.bmsize = bmsize; | ||
221 | chosen.bmoffset = bmoffset; | ||
222 | } | ||
223 | } | ||
224 | else if (search == BIGGER) | ||
225 | { | ||
226 | pdelta = (w * h) - (wanted_w * wanted_h); | ||
227 | if ((!have_choice) || | ||
228 | ((w >= wanted_w) && (h >= wanted_h) && | ||
229 | (pdelta <= chosen.pdelta) && | ||
230 | (((bpp >= 3) && (bpp >= chosen.bpp)) || | ||
231 | ((bpp < 3) && (cols >= chosen.cols))))) | ||
232 | { | ||
233 | have_choice = 1; | ||
234 | if (pdelta < 0) pdelta = 0x7fffffff; | ||
235 | chosen.pdelta = pdelta; | ||
236 | chosen.w = w; | ||
237 | chosen.h = h; | ||
238 | chosen.cols = cols; | ||
239 | chosen.bpp = bpp; | ||
240 | chosen.planes = planes; | ||
241 | chosen.bmsize = bmsize; | ||
242 | chosen.bmoffset = bmoffset; | ||
243 | } | ||
244 | } | ||
245 | } | ||
246 | } | ||
247 | if (chosen.bmoffset == 0) goto close_file; | ||
248 | if (fseek(f, chosen.bmoffset, SEEK_SET) != 0) goto close_file; | ||
249 | |||
250 | w = chosen.w; | ||
251 | h = chosen.h; | ||
252 | if ((w > 256) || (h > 256)) goto close_file; | ||
253 | if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE) || | ||
254 | IMG_TOO_BIG(w, h)) | ||
255 | { | ||
256 | if (IMG_TOO_BIG(w, h)) | ||
257 | *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; | ||
258 | else | ||
259 | *error = EVAS_LOAD_ERROR_GENERIC; | ||
260 | goto close_file; | ||
261 | } | ||
262 | |||
263 | ie->w = w; | ||
264 | ie->h = h; | ||
265 | if (hasa) ie->flags.alpha = 1; | ||
266 | |||
267 | fclose(f); | ||
268 | *error = EVAS_LOAD_ERROR_NONE; | ||
269 | return EINA_TRUE; | ||
270 | |||
271 | close_file: | ||
272 | fclose(f); | ||
273 | return EINA_FALSE; | ||
274 | } | ||
275 | |||
276 | static Eina_Bool | ||
277 | evas_image_load_file_data_ico(Image_Entry *ie, const char *file, const char *key, int *error) | ||
278 | { | ||
279 | unsigned short word; | ||
280 | unsigned char byte; | ||
281 | unsigned int dword; | ||
282 | FILE *f; | ||
283 | int wanted_w = 0, wanted_h = 0, w, h, cols, i, planes = 0, | ||
284 | hot_x = 0, hot_y = 0, bpp = 0, pdelta, search = -1, have_choice = 0, | ||
285 | stride, pstride, j, right_way_up = 0, diff_size = 0, cols2; | ||
286 | unsigned int bmoffset, bmsize, bitcount, compression, imagesize, fsize, | ||
287 | colorsused, colorsimportant, *pal, *surface, *pix, none_zero_alpha = 0; | ||
288 | unsigned short reserved, type, count, planes2; | ||
289 | unsigned char *maskbuf, *pixbuf, *p; | ||
290 | struct { | ||
291 | int pdelta; | ||
292 | int w, h; | ||
293 | int cols; | ||
294 | int bpp, planes; | ||
295 | int hot_x, hot_y; | ||
296 | unsigned int bmoffset, bmsize; | ||
297 | } chosen = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||
298 | |||
299 | f = fopen(file, "rb"); | ||
300 | if (!f) | ||
301 | { | ||
302 | *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST; | ||
303 | return EINA_FALSE; | ||
304 | } | ||
305 | |||
306 | *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT; | ||
307 | fseek(f, 0, SEEK_END); | ||
308 | fsize = ftell(f); | ||
309 | fseek(f, 0, SEEK_SET); | ||
310 | if (fsize < (6 + 16 + 40)) goto close_file; | ||
311 | |||
312 | // key: | ||
313 | // NULL == highest res | ||
314 | // biggest == highest res | ||
315 | // smallest == lowest res | ||
316 | // | ||
317 | // smaller == next size SMALLER than load opts WxH (if possible) | ||
318 | // bigger == next size BIGGER than load opts WxH (if possible) | ||
319 | // more ? | ||
320 | |||
321 | search = BIGGEST; | ||
322 | if ((ie->load_opts.w > 0) && (ie->load_opts.h > 0)) | ||
323 | { | ||
324 | wanted_w = ie->load_opts.w; | ||
325 | wanted_h = ie->load_opts.h; | ||
326 | search = SMALLER; | ||
327 | } | ||
328 | |||
329 | if (!read_ushort(f, &reserved)) goto close_file; | ||
330 | if (!read_ushort(f, &type)) goto close_file; | ||
331 | if (!read_ushort(f, &count)) goto close_file; | ||
332 | if (!((reserved == 0) && | ||
333 | ((type == ICON) || (type == CURSOR)) && (count > 0))) | ||
334 | goto close_file; | ||
335 | *error = EVAS_LOAD_ERROR_CORRUPT_FILE; | ||
336 | |||
337 | if (key) | ||
338 | { | ||
339 | if (!strcmp(key, "biggest")) | ||
340 | { | ||
341 | wanted_w = 0; | ||
342 | wanted_h = 0; | ||
343 | search = BIGGEST; | ||
344 | chosen.pdelta = 0; | ||
345 | } | ||
346 | else if (!strcmp(key, "smallest")) | ||
347 | { | ||
348 | wanted_w = 1; | ||
349 | wanted_h = 1; | ||
350 | search = SMALLEST; | ||
351 | chosen.pdelta = 0x7fffffff; | ||
352 | } | ||
353 | else if (!strcmp(key, "smaller")) | ||
354 | { | ||
355 | chosen.pdelta = 0x7fffffff; | ||
356 | search = SMALLER; | ||
357 | } | ||
358 | else if (!strcmp(key, "bigger")) | ||
359 | { | ||
360 | chosen.pdelta = 0x7fffffff; | ||
361 | search = BIGGER; | ||
362 | } | ||
363 | } | ||
364 | for (i = 0; i < count; i++) | ||
365 | { | ||
366 | if (fread(&byte, 1, 1, f) != 1) goto close_file; | ||
367 | w = byte; | ||
368 | if (w <= 0) w = 256; | ||
369 | if (fread(&byte, 1, 1, f) != 1) goto close_file; | ||
370 | h = byte; | ||
371 | if (h <= 0) h = 256; | ||
372 | if (fread(&byte, 1, 1, f) != 1) goto close_file; | ||
373 | cols = byte; | ||
374 | if (cols <= 0) cols = 256; | ||
375 | if (fread(&byte, 1, 1, f) != 1) goto close_file; | ||
376 | if (!read_ushort(f, &word)) goto close_file; | ||
377 | if (type == 1) planes = word; | ||
378 | else hot_x = word; | ||
379 | if (!read_ushort(f, &word)) goto close_file; | ||
380 | if (type == 1) bpp = word; | ||
381 | else hot_y = word; | ||
382 | if (!read_uint(f, &bmsize)) goto close_file; | ||
383 | if (!read_uint(f, &bmoffset)) goto close_file; | ||
384 | if ((bmsize <= 0) || (bmoffset <= 0) || (bmoffset >= fsize)) goto close_file; | ||
385 | if (search == BIGGEST) | ||
386 | { | ||
387 | pdelta = w * h; | ||
388 | if ((!have_choice) || | ||
389 | ((pdelta >= chosen.pdelta) && | ||
390 | (((bpp >= 3) && (bpp >= chosen.bpp)) || | ||
391 | ((bpp < 3) && (cols >= chosen.cols))))) | ||
392 | { | ||
393 | have_choice = 1; | ||
394 | chosen.pdelta = pdelta; | ||
395 | chosen.w = w; | ||
396 | chosen.h = h; | ||
397 | chosen.cols = cols; | ||
398 | chosen.bpp = bpp; | ||
399 | chosen.planes = planes; | ||
400 | chosen.bmsize = bmsize; | ||
401 | chosen.bmoffset = bmoffset; | ||
402 | } | ||
403 | } | ||
404 | else | ||
405 | { | ||
406 | if (search == SMALLEST) | ||
407 | { | ||
408 | pdelta = w * h; | ||
409 | if ((!have_choice) || | ||
410 | ((pdelta <= chosen.pdelta) && | ||
411 | (((bpp >= 3) && (bpp >= chosen.bpp)) || | ||
412 | ((bpp < 3) && (cols >= chosen.cols))))) | ||
413 | { | ||
414 | have_choice = 1; | ||
415 | chosen.pdelta = pdelta; | ||
416 | chosen.w = w; | ||
417 | chosen.h = h; | ||
418 | chosen.cols = cols; | ||
419 | chosen.bpp = bpp; | ||
420 | chosen.planes = planes; | ||
421 | chosen.bmsize = bmsize; | ||
422 | chosen.bmoffset = bmoffset; | ||
423 | } | ||
424 | } | ||
425 | else if (search == SMALLER) | ||
426 | { | ||
427 | pdelta = (wanted_w * wanted_h) - (w * h); | ||
428 | if ((!have_choice) || | ||
429 | ((w <= wanted_w) && (h <= wanted_h) && | ||
430 | (pdelta <= chosen.pdelta) && | ||
431 | (((bpp >= 3) && (bpp >= chosen.bpp)) || | ||
432 | ((bpp < 3) && (cols >= chosen.cols))))) | ||
433 | { | ||
434 | have_choice = 1; | ||
435 | if (pdelta < 0) pdelta = 0x7fffffff; | ||
436 | chosen.pdelta = pdelta; | ||
437 | chosen.w = w; | ||
438 | chosen.h = h; | ||
439 | chosen.cols = cols; | ||
440 | chosen.bpp = bpp; | ||
441 | chosen.planes = planes; | ||
442 | chosen.bmsize = bmsize; | ||
443 | chosen.bmoffset = bmoffset; | ||
444 | } | ||
445 | } | ||
446 | else if (search == BIGGER) | ||
447 | { | ||
448 | pdelta = (w * h) - (wanted_w * wanted_h); | ||
449 | if ((!have_choice) || | ||
450 | ((w >= wanted_w) && (h >= wanted_h) && | ||
451 | (pdelta <= chosen.pdelta) && | ||
452 | (((bpp >= 3) && (bpp >= chosen.bpp)) || | ||
453 | ((bpp < 3) && (cols >= chosen.cols))))) | ||
454 | { | ||
455 | have_choice = 1; | ||
456 | if (pdelta < 0) pdelta = 0x7fffffff; | ||
457 | chosen.pdelta = pdelta; | ||
458 | chosen.w = w; | ||
459 | chosen.h = h; | ||
460 | chosen.cols = cols; | ||
461 | chosen.bpp = bpp; | ||
462 | chosen.planes = planes; | ||
463 | chosen.bmsize = bmsize; | ||
464 | chosen.bmoffset = bmoffset; | ||
465 | } | ||
466 | } | ||
467 | } | ||
468 | } | ||
469 | if (chosen.bmoffset == 0) goto close_file; | ||
470 | if (fseek(f, chosen.bmoffset, SEEK_SET) != 0) goto close_file; | ||
471 | |||
472 | w = chosen.w; | ||
473 | h = chosen.h; | ||
474 | cols = chosen.cols; | ||
475 | bpp = chosen.bpp; | ||
476 | // changed since we loaded header? | ||
477 | if (((int)ie->w != w) || ((int)ie->h != h)) goto close_file; | ||
478 | |||
479 | // read bmp header time... let's do some checking | ||
480 | if (!read_uint(f, &dword)) goto close_file; // headersize - dont care | ||
481 | if (!read_uint(f, &dword)) goto close_file; // width | ||
482 | if (dword > 0) | ||
483 | { | ||
484 | if ((int)dword != w) | ||
485 | { | ||
486 | w = dword; | ||
487 | diff_size = 1; | ||
488 | } | ||
489 | } | ||
490 | if (!read_uint(f, &dword)) goto close_file; // height | ||
491 | if (dword > 0) | ||
492 | { | ||
493 | if ((int)dword != (h * 2)) | ||
494 | { | ||
495 | h = dword / 2; | ||
496 | diff_size = 1; | ||
497 | } | ||
498 | } | ||
499 | if (diff_size) | ||
500 | { | ||
501 | ERR("Broken ICO file: %s - " | ||
502 | " Reporting size of %ix%i in index, but bitmap is %ix%i. " | ||
503 | " May be expanded or cropped.", | ||
504 | file, ie->w, ie->h, w, h); | ||
505 | } | ||
506 | if (!read_ushort(f, &word)) goto close_file; // planes | ||
507 | planes2 = word; | ||
508 | if (!read_ushort(f, &word)) goto close_file; // bitcount | ||
509 | bitcount = word; | ||
510 | if (!read_uint(f, &dword)) goto close_file; // compression | ||
511 | compression = dword; | ||
512 | if (!read_uint(f, &dword)) goto close_file; // imagesize | ||
513 | imagesize = dword; | ||
514 | if (!read_uint(f, &dword)) goto close_file; // z pixels per m | ||
515 | if (!read_uint(f, &dword)) goto close_file; // y pizels per m | ||
516 | if (!read_uint(f, &dword)) goto close_file; // colors used | ||
517 | colorsused = dword; | ||
518 | if (!read_uint(f, &dword)) goto close_file; // colors important | ||
519 | colorsimportant = dword; | ||
520 | |||
521 | evas_cache_image_surface_alloc(ie, ie->w, ie->h); | ||
522 | surface = evas_cache_image_pixels(ie); | ||
523 | if (!surface) | ||
524 | { | ||
525 | *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; | ||
526 | goto close_file; | ||
527 | } | ||
528 | memset(surface, 0, ie->w * ie->h * 4); | ||
529 | |||
530 | if (!((bitcount == 1) || (bitcount == 4) || (bitcount == 8) || | ||
531 | (bitcount == 24) || (bitcount == 32))) | ||
532 | goto close_file; | ||
533 | if (bitcount <= 8) | ||
534 | { | ||
535 | cols2 = 1 << bitcount; | ||
536 | if (cols == 0) cols = cols2; | ||
537 | if (cols > cols2) cols = cols2; | ||
538 | if (cols > 256) cols = 256; | ||
539 | } | ||
540 | else | ||
541 | cols = 0; | ||
542 | if (bitcount > 8) cols = 0; | ||
543 | |||
544 | pal = alloca(256 * 4); | ||
545 | for (i = 0; i < cols; i++) | ||
546 | { | ||
547 | unsigned char a, r, g, b; | ||
548 | |||
549 | if (fread(&b, 1, 1, f) != 1) goto close_file; | ||
550 | if (fread(&g, 1, 1, f) != 1) goto close_file; | ||
551 | if (fread(&r, 1, 1, f) != 1) goto close_file; | ||
552 | if (fread(&a, 1, 1, f) != 1) goto close_file; | ||
553 | a = 0xff; | ||
554 | pal[i] = ARGB_JOIN(a, r, g, b); | ||
555 | } | ||
556 | stride = ((w + 31) / 32); | ||
557 | maskbuf = alloca(stride * h); | ||
558 | pixbuf = alloca(stride * 32 * 4); // more than enough | ||
559 | if (bitcount == 1) | ||
560 | { | ||
561 | pstride = stride * 4; | ||
562 | for (i = 0; i < h; i++) | ||
563 | { | ||
564 | pix = surface + (i * ie->w); | ||
565 | if (!right_way_up) pix = surface + ((ie->h - 1 - i) * ie->w); | ||
566 | if (fread(pixbuf, pstride, 1, f) != 1) goto close_file; | ||
567 | p = pixbuf; | ||
568 | if (i >= (int)ie->h) continue; | ||
569 | for (j = 0; j < w; j++) | ||
570 | { | ||
571 | if (j >= (int)ie->w) break; | ||
572 | if ((j & 0x7) == 0x0) | ||
573 | { | ||
574 | *pix = pal[*p >> 7]; | ||
575 | } | ||
576 | else if ((j & 0x7) == 0x1) | ||
577 | { | ||
578 | *pix = pal[(*p >> 6) & 0x1]; | ||
579 | } | ||
580 | else if ((j & 0x7) == 0x2) | ||
581 | { | ||
582 | *pix = pal[(*p >> 5) & 0x1]; | ||
583 | } | ||
584 | else if ((j & 0x7) == 0x3) | ||
585 | { | ||
586 | *pix = pal[(*p >> 4) & 0x1]; | ||
587 | } | ||
588 | else if ((j & 0x7) == 0x4) | ||
589 | { | ||
590 | *pix = pal[(*p >> 3) & 0x1]; | ||
591 | } | ||
592 | else if ((j & 0x7) == 0x5) | ||
593 | { | ||
594 | *pix = pal[(*p >> 2) & 0x1]; | ||
595 | } | ||
596 | else if ((j & 0x7) == 0x6) | ||
597 | { | ||
598 | *pix = pal[(*p >> 1) & 0x1]; | ||
599 | } | ||
600 | else | ||
601 | { | ||
602 | *pix = pal[*p & 0x1]; | ||
603 | p++; | ||
604 | } | ||
605 | pix++; | ||
606 | } | ||
607 | } | ||
608 | } | ||
609 | else if (bitcount == 4) | ||
610 | { | ||
611 | pstride = ((w + 7) / 8) * 4; | ||
612 | for (i = 0; i < h; i++) | ||
613 | { | ||
614 | pix = surface + (i * ie->w); | ||
615 | if (!right_way_up) pix = surface + ((ie->h - 1 - i) * ie->w); | ||
616 | if (fread(pixbuf, pstride, 1, f) != 1) goto close_file; | ||
617 | p = pixbuf; | ||
618 | if (i >= (int)ie->h) continue; | ||
619 | for (j = 0; j < w; j++) | ||
620 | { | ||
621 | if (j >= (int)ie->w) break; | ||
622 | if ((j & 0x1) == 0x1) | ||
623 | { | ||
624 | *pix = pal[*p & 0x0f]; | ||
625 | p++; | ||
626 | } | ||
627 | else | ||
628 | { | ||
629 | *pix = pal[*p >> 4]; | ||
630 | } | ||
631 | pix++; | ||
632 | } | ||
633 | } | ||
634 | } | ||
635 | else if (bitcount == 8) | ||
636 | { | ||
637 | pstride = ((w + 3) / 4) * 4; | ||
638 | for (i = 0; i < h; i++) | ||
639 | { | ||
640 | pix = surface + (i * ie->w); | ||
641 | if (!right_way_up) pix = surface + ((ie->h - 1 - i) * ie->w); | ||
642 | if (fread(pixbuf, pstride, 1, f) != 1) goto close_file; | ||
643 | p = pixbuf; | ||
644 | if (i >= (int)ie->h) continue; | ||
645 | for (j = 0; j < w; j++) | ||
646 | { | ||
647 | if (j >= (int)ie->w) break; | ||
648 | *pix = pal[*p]; | ||
649 | p++; | ||
650 | pix++; | ||
651 | } | ||
652 | } | ||
653 | } | ||
654 | else if (bitcount == 24) | ||
655 | { | ||
656 | pstride = w * 3; | ||
657 | for (i = 0; i < h; i++) | ||
658 | { | ||
659 | pix = surface + (i * ie->w); | ||
660 | if (!right_way_up) pix = surface + ((ie->h - 1 - i) * ie->w); | ||
661 | if (fread(pixbuf, pstride, 1, f) != 1) goto close_file; | ||
662 | p = pixbuf; | ||
663 | if (i >= (int)ie->h) continue; | ||
664 | for (j = 0; j < w; j++) | ||
665 | { | ||
666 | unsigned char a, r, g, b; | ||
667 | |||
668 | if (j >= (int)ie->w) break; | ||
669 | b = p[0]; | ||
670 | g = p[1]; | ||
671 | r = p[2]; | ||
672 | p += 3; | ||
673 | a = 0xff; | ||
674 | *pix = ARGB_JOIN(a, r, g, b); | ||
675 | pix++; | ||
676 | } | ||
677 | } | ||
678 | } | ||
679 | else if (bitcount == 32) | ||
680 | { | ||
681 | pstride = w * 4; | ||
682 | for (i = 0; i < h; i++) | ||
683 | { | ||
684 | pix = surface + (i * ie->w); | ||
685 | if (!right_way_up) pix = surface + ((ie->h - 1 - i) * ie->w); | ||
686 | if (fread(pixbuf, pstride, 1, f) != 1) goto close_file; | ||
687 | p = pixbuf; | ||
688 | if (i >= (int)ie->h) continue; | ||
689 | for (j = 0; j < w; j++) | ||
690 | { | ||
691 | unsigned char a, r, g, b; | ||
692 | |||
693 | if (j >= (int)ie->w) break; | ||
694 | b = p[0]; | ||
695 | g = p[1]; | ||
696 | r = p[2]; | ||
697 | a = p[3]; | ||
698 | p += 4; | ||
699 | if (a) none_zero_alpha = 1; | ||
700 | *pix = ARGB_JOIN(a, r, g, b); | ||
701 | pix++; | ||
702 | } | ||
703 | } | ||
704 | } | ||
705 | if (!none_zero_alpha) | ||
706 | { | ||
707 | if (fread(maskbuf, stride * 4 * h, 1, f) != 1) goto close_file; | ||
708 | // apply mask | ||
709 | pix = surface; | ||
710 | for (i = 0; i < h; i++) | ||
711 | { | ||
712 | unsigned char *m; | ||
713 | |||
714 | pix = surface + (i * ie->w); | ||
715 | if (!right_way_up) pix = surface + ((ie->h - 1 - i) * ie->w); | ||
716 | m = maskbuf + (stride * i * 4); | ||
717 | if (i >= (int)ie->h) continue; | ||
718 | for (j = 0; j < w; j++) | ||
719 | { | ||
720 | if (j >= (int)ie->w) break; | ||
721 | if (*m & (1 << (7 - (j & 0x7)))) | ||
722 | A_VAL(pix) = 0x00; | ||
723 | else | ||
724 | A_VAL(pix) = 0xff; | ||
725 | if ((j & 0x7) == 0x7) m++; | ||
726 | pix++; | ||
727 | } | ||
728 | } | ||
729 | } | ||
730 | |||
731 | fclose(f); | ||
732 | |||
733 | evas_common_image_premul(ie); | ||
734 | *error = EVAS_LOAD_ERROR_NONE; | ||
735 | return EINA_TRUE; | ||
736 | |||
737 | close_file: | ||
738 | fclose(f); | ||
739 | return EINA_FALSE; | ||
740 | } | ||
741 | |||
742 | static int | ||
743 | module_open(Evas_Module *em) | ||
744 | { | ||
745 | if (!em) return 0; | ||
746 | em->functions = (void *)(&evas_image_load_ico_func); | ||
747 | return 1; | ||
748 | } | ||
749 | |||
750 | static void | ||
751 | module_close(Evas_Module *em __UNUSED__) | ||
752 | { | ||
753 | } | ||
754 | |||
755 | static Evas_Module_Api evas_modapi = | ||
756 | { | ||
757 | EVAS_MODULE_API_VERSION, | ||
758 | "ico", | ||
759 | "none", | ||
760 | { | ||
761 | module_open, | ||
762 | module_close | ||
763 | } | ||
764 | }; | ||
765 | |||
766 | EVAS_MODULE_DEFINE(EVAS_MODULE_TYPE_IMAGE_LOADER, image_loader, ico); | ||
767 | |||
768 | #ifndef EVAS_STATIC_BUILD_ICO | ||
769 | EVAS_EINA_MODULE_DEFINE(image_loader, ico); | ||
770 | #endif | ||