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