7 ico_push_error(int error) {
8 char error_buf[ICO_MAX_MESSAGE];
10 ico_error_message(error, error_buf, sizeof(error_buf));
11 i_push_error(error, error_buf);
16 read_one_icon(ico_reader_t *file, int index, int masked) {
21 image = ico_image_read(file, index, &error);
23 ico_push_error(error);
24 i_push_error(0, "error reading ICO/CUR image");
29 /* check to make sure we should do the masking, if the mask has
30 nothing set we don't mask */
32 int total = image->width * image->height;
33 unsigned char *inp = image->mask_data;
36 for (pos = 0; pos < total; ++pos) {
48 ico_color_t *inp = image->image_data;
50 if (!i_int_check_image_file_limits(image->width, image->height, 4, 1)) {
51 ico_image_release(image);
55 result = i_img_8_new(image->width, image->height, 4);
57 ico_image_release(image);
61 line_buf = mymalloc(image->width * sizeof(i_color));
63 for (y = 0; y < image->height; ++y) {
65 for (x = 0; x < image->width; ++x) {
66 outp->rgba.r = inp->r;
67 outp->rgba.g = inp->g;
68 outp->rgba.b = inp->b;
69 outp->rgba.a = inp->a;
73 i_plin(result, 0, image->width, y, line_buf);
81 unsigned char *image_data;
82 int channels = masked ? 4 : 3;
84 if (!i_int_check_image_file_limits(image->width, image->height, channels, 1)) {
85 ico_image_release(image);
89 result = i_img_pal_new(image->width, image->height, channels, 256);
91 ico_image_release(image);
95 /* fill in the palette */
96 for (pal_index = 0; pal_index < image->palette_size; ++pal_index) {
98 c.rgba.r = image->palette[pal_index].r;
99 c.rgba.g = image->palette[pal_index].g;
100 c.rgba.b = image->palette[pal_index].b;
103 if (i_addcolors(result, &c, 1) < 0) {
104 i_push_error(0, "could not add color to palette");
105 ico_image_release(image);
106 i_img_destroy(result);
111 /* fill in the image data */
112 image_data = image->image_data;
113 for (y = 0; y < image->height; ++y) {
114 i_ppal(result, 0, image->width, y, image_data);
115 image_data += image->width;
120 unsigned char *inp = image->mask_data;
124 /* fill in the mask tag */
125 /* space for " .\n", width + 1 chars per line and NUL */
126 mask = mymalloc(3 + (image->width + 1) * image->height + 1);
132 for (y = 0; y < image->height; ++y) {
133 for (x = 0; x < image->width; ++x) {
134 *outp++ = *inp++ ? '*' : '.';
136 if (y != image->height - 1) /* not on the last line */
141 if (ico_type(file) == ICON_ICON)
142 i_tags_set(&result->tags, "ico_mask", mask, (outp-mask)-1);
144 i_tags_set(&result->tags, "cur_mask", mask, (outp-mask)-1);
149 /* if the user requests, treat the mask as an alpha channel.
150 Note: this converts the image into a direct image if it was paletted
153 unsigned char *inp = image->mask_data;
155 i_color *line_buf = mymalloc(sizeof(i_color) * image->width);
157 for (y = 0; y < image->height; ++y) {
162 for (x = 0; x < image->width; ++x) {
166 i_glin(result, first, image->width, y, line_buf);
170 line_buf[x-first].rgba.a = 0;
174 i_plin(result, first, last + 1, y, line_buf);
179 if (ico_type(file) == ICON_ICON) {
180 i_tags_setn(&result->tags, "ico_bits", image->bit_count);
181 i_tags_set(&result->tags, "i_format", "ico", 3);
184 i_tags_setn(&result->tags, "cur_bits", image->bit_count);
185 i_tags_set(&result->tags, "i_format", "cur", 3);
186 i_tags_setn(&result->tags, "cur_hotspotx", image->hotspot_x);
187 i_tags_setn(&result->tags, "cur_hotspoty", image->hotspot_y);
190 ico_image_release(image);
196 i_readico_single(io_glue *ig, int index, int masked) {
203 file = ico_reader_open(ig, &error);
205 ico_push_error(error);
206 i_push_error(0, "error opening ICO/CUR file");
210 /* the index is range checked by msicon.c - don't duplicate it here */
212 result = read_one_icon(file, index, masked);
213 ico_reader_close(file);
219 i_readico_multi(io_glue *ig, int *count, int masked) {
227 file = ico_reader_open(ig, &error);
229 ico_push_error(error);
230 i_push_error(0, "error opening ICO/CUR file");
234 imgs = mymalloc(sizeof(i_img *) * ico_image_count(file));
237 for (index = 0; index < ico_image_count(file); ++index) {
238 i_img *im = read_one_icon(file, index, masked);
242 imgs[(*count)++] = im;
245 ico_reader_close(file);
256 validate_image(i_img *im) {
257 if (im->xsize > 255 || im->ysize > 255) {
258 i_push_error(0, "image too large for ico file");
261 if (im->channels < 1 || im->channels > 4) {
262 /* this shouldn't happen, but check anyway */
263 i_push_error(0, "invalid channels");
271 translate_mask(i_img *im, unsigned char *out, const char *in) {
274 int len = strlen(in);
276 int newline; /* set to the first newline type we see */
277 int notnewline; /* set to whatever in ( "\n\r" newline isn't ) */
284 if (in[2] == '\n' || in[2] == '\r') {
286 notnewline = '\n' + '\r' - newline;
294 while (y < im->ysize && pos < len) {
296 while (x < im->xsize && pos < len) {
297 if (in[pos] == newline) {
298 /* don't process it, we look for it later */
301 else if (in[pos] == notnewline) {
302 ++pos; /* just drop it */
304 else if (in[pos] == one) {
309 else if (in[pos] == zero) {
314 else if (in[pos] == ' ' || in[pos] == '\t') {
315 /* just ignore whitespace */
322 while (x++ < im->xsize) {
325 while (pos < len && in[pos] != newline)
327 if (pos < len && in[pos] == newline)
328 ++pos; /* actually skip the newline */
332 while (y++ < im->ysize) {
333 for (x = 0; x < im->xsize; ++x)
341 derive_mask(i_img *im, ico_image_t *ico) {
343 if (im->channels == 1 || im->channels == 3) {
344 /* msicon.c's default mask is what we want */
345 myfree(ico->mask_data);
346 ico->mask_data = NULL;
349 int channel = im->channels - 1;
350 i_sample_t *linebuf = mymalloc(sizeof(i_sample_t) * im->xsize);
352 unsigned char *out = ico->mask_data;
354 for (y = 0; y < im->ysize; ++y) {
355 i_gsamp(im, 0, im->xsize, y, linebuf, &channel, 1);
356 for (x = 0; x < im->xsize; ++x) {
357 *out++ = linebuf[x] == 255 ? 0 : 1;
365 fill_image_base(i_img *im, ico_image_t *ico, const char *mask_name) {
368 ico->width = im->xsize;
369 ico->height = im->ysize;
370 ico->direct = im->type == i_direct_type;
376 unsigned char *linebuf = mymalloc(ico->width * 4);
377 ico->image_data = mymalloc(sizeof(ico_color_t) * ico->width * ico->height);
379 switch (im->channels) {
381 channels[0] = channels[1] = channels[2] = channels[3] = 0;
386 channels[0] = channels[1] = channels[2] = 0;
406 out = ico->image_data;
407 for (y = 0; y < im->ysize; ++y) {
408 i_gsamp(im, 0, im->xsize, y, linebuf, channels, 4);
410 for (x = 0; x < im->xsize; ++x) {
414 out->a = set_alpha ? 255 : *in;
427 i_palidx *linebuf = mymalloc(sizeof(i_palidx) * ico->width);
429 ico->image_data = mymalloc(sizeof(ico_color_t) * ico->width * ico->height);
431 out = ico->image_data;
432 for (y = 0; y < im->ysize; ++y) {
433 i_gpal(im, 0, im->xsize, y, linebuf);
435 for (x = 0; x < im->xsize; ++x) {
441 ico->palette_size = i_colorcount(im);
442 ico->palette = mymalloc(sizeof(ico_color_t) * ico->palette_size);
443 colors = mymalloc(sizeof(i_color) * ico->palette_size);
444 i_getcolors(im, 0, colors, ico->palette_size);
445 for (i = 0; i < ico->palette_size; ++i) {
446 if (im->channels == 1 || im->channels == 2) {
447 ico->palette[i].r = ico->palette[i].g =
448 ico->palette[i].b = colors[i].rgba.r;
451 ico->palette[i].r = colors[i].rgba.r;
452 ico->palette[i].g = colors[i].rgba.g;
453 ico->palette[i].b = colors[i].rgba.b;
463 ico->mask_data = mymalloc(im->xsize * im->ysize);
465 if (!i_tags_find(&im->tags, mask_name, 0, &mask_index)
466 || !im->tags.tags[mask_index].data
467 || !translate_mask(im, ico->mask_data,
468 im->tags.tags[mask_index].data)) {
469 derive_mask(im, ico);
475 unfill_image(ico_image_t *ico) {
476 myfree(ico->image_data);
478 myfree(ico->palette);
480 myfree(ico->mask_data);
484 fill_image_icon(i_img *im, ico_image_t *ico) {
485 fill_image_base(im, ico, "ico_mask");
486 ico->hotspot_x = ico->hotspot_y = 0;
490 i_writeico_wiol(i_io_glue_t *ig, i_img *im) {
496 if (!validate_image(im))
499 fill_image_icon(im, &ico);
501 if (!ico_write(ig, &ico, 1, ICON_ICON, &error)) {
502 ico_push_error(error);
509 if (i_io_close(ig) < 0) {
510 i_push_error(0, "error closing output");
518 i_writeico_multi_wiol(i_io_glue_t *ig, i_img **ims, int count) {
525 if (count > 0xFFFF) {
526 i_push_error(0, "too many images for ico files");
530 for (i = 0; i < count; ++i)
531 if (!validate_image(ims[i]))
534 icons = mymalloc(sizeof(ico_image_t) * count);
536 for (i = 0; i < count; ++i)
537 fill_image_icon(ims[i], icons + i);
539 if (!ico_write(ig, icons, count, ICON_ICON, &error)) {
540 ico_push_error(error);
541 for (i = 0; i < count; ++i)
542 unfill_image(icons + i);
547 for (i = 0; i < count; ++i)
548 unfill_image(icons + i);
551 if (i_io_close(ig) < 0) {
552 i_push_error(0, "error closing output");
560 fill_image_cursor(i_img *im, ico_image_t *ico) {
562 fill_image_base(im, ico, "ico_mask");
564 if (!i_tags_get_int(&im->tags, "cur_hotspotx", 0, &hotx))
566 if (!i_tags_get_int(&im->tags, "cur_hotspoty", 0, &hoty))
571 else if (hotx >= im->xsize)
572 hotx = im->xsize - 1;
576 else if (hoty >= im->ysize)
577 hoty = im->ysize - 1;
579 ico->hotspot_x = hotx;
580 ico->hotspot_y = hoty;
584 i_writecur_wiol(i_io_glue_t *ig, i_img *im) {
590 if (!validate_image(im))
593 fill_image_cursor(im, &ico);
595 if (!ico_write(ig, &ico, 1, ICON_CURSOR, &error)) {
596 ico_push_error(error);
603 if (i_io_close(ig) < 0) {
604 i_push_error(0, "error closing output");
612 i_writecur_multi_wiol(i_io_glue_t *ig, i_img **ims, int count) {
619 if (count > 0xFFFF) {
620 i_push_error(0, "too many images for ico files");
624 for (i = 0; i < count; ++i)
625 if (!validate_image(ims[i]))
628 icons = mymalloc(sizeof(ico_image_t) * count);
630 for (i = 0; i < count; ++i)
631 fill_image_cursor(ims[i], icons + i);
633 if (!ico_write(ig, icons, count, ICON_CURSOR, &error)) {
634 ico_push_error(error);
635 for (i = 0; i < count; ++i)
636 unfill_image(icons + i);
641 for (i = 0; i < count; ++i)
642 unfill_image(icons + i);
645 if (i_io_close(ig) < 0) {
646 i_push_error(0, "error closing output");