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;
49 int channels = masked || image->bit_count == 32 ? 4 : 3;
51 if (!i_int_check_image_file_limits(image->width, image->height, channels, 1)) {
52 ico_image_release(image);
57 result = i_img_8_new(image->width, image->height, channels);
59 ico_image_release(image);
63 line_buf = mymalloc(image->width * sizeof(i_color));
65 for (y = 0; y < image->height; ++y) {
67 for (x = 0; x < image->width; ++x) {
68 outp->rgba.r = inp->r;
69 outp->rgba.g = inp->g;
70 outp->rgba.b = inp->b;
71 outp->rgba.a = inp->a;
75 i_plin(result, 0, image->width, y, line_buf);
83 unsigned char *image_data;
84 int channels = masked ? 4 : 3;
86 if (!i_int_check_image_file_limits(image->width, image->height, channels, 1)) {
87 ico_image_release(image);
91 result = i_img_pal_new(image->width, image->height, channels, 256);
93 ico_image_release(image);
97 /* fill in the palette */
98 for (pal_index = 0; pal_index < image->palette_size; ++pal_index) {
100 c.rgba.r = image->palette[pal_index].r;
101 c.rgba.g = image->palette[pal_index].g;
102 c.rgba.b = image->palette[pal_index].b;
105 if (i_addcolors(result, &c, 1) < 0) {
106 i_push_error(0, "could not add color to palette");
107 ico_image_release(image);
108 i_img_destroy(result);
113 /* fill in the image data */
114 image_data = image->image_data;
115 for (y = 0; y < image->height; ++y) {
116 i_ppal(result, 0, image->width, y, image_data);
117 image_data += image->width;
122 unsigned char *inp = image->mask_data;
126 /* fill in the mask tag */
127 /* space for " .\n", width + 1 chars per line and NUL */
128 mask = mymalloc(3 + (image->width + 1) * image->height + 1);
134 for (y = 0; y < image->height; ++y) {
135 for (x = 0; x < image->width; ++x) {
136 *outp++ = *inp++ ? '*' : '.';
138 if (y != image->height - 1) /* not on the last line */
143 if (ico_type(file) == ICON_ICON)
144 i_tags_set(&result->tags, "ico_mask", mask, (outp-mask)-1);
146 i_tags_set(&result->tags, "cur_mask", mask, (outp-mask)-1);
151 /* if the user requests, treat the mask as an alpha channel.
152 Note: this converts the image into a direct image if it was paletted
155 unsigned char *inp = image->mask_data;
157 i_color *line_buf = mymalloc(sizeof(i_color) * image->width);
159 for (y = 0; y < image->height; ++y) {
164 for (x = 0; x < image->width; ++x) {
168 i_glin(result, first, image->width, y, line_buf);
172 line_buf[x-first].rgba.a = 0;
176 i_plin(result, first, last + 1, y, line_buf);
181 if (ico_type(file) == ICON_ICON) {
182 i_tags_setn(&result->tags, "ico_bits", image->bit_count);
183 i_tags_set(&result->tags, "i_format", "ico", 3);
186 i_tags_setn(&result->tags, "cur_bits", image->bit_count);
187 i_tags_set(&result->tags, "i_format", "cur", 3);
188 i_tags_setn(&result->tags, "cur_hotspotx", image->hotspot_x);
189 i_tags_setn(&result->tags, "cur_hotspoty", image->hotspot_y);
192 ico_image_release(image);
198 i_readico_single(io_glue *ig, int index, int masked) {
205 file = ico_reader_open(ig, &error);
207 ico_push_error(error);
208 i_push_error(0, "error opening ICO/CUR file");
212 /* the index is range checked by msicon.c - don't duplicate it here */
214 result = read_one_icon(file, index, masked);
215 ico_reader_close(file);
221 i_readico_multi(io_glue *ig, int *count, int masked) {
229 file = ico_reader_open(ig, &error);
231 ico_push_error(error);
232 i_push_error(0, "error opening ICO/CUR file");
236 imgs = mymalloc(sizeof(i_img *) * ico_image_count(file));
239 for (index = 0; index < ico_image_count(file); ++index) {
240 i_img *im = read_one_icon(file, index, masked);
244 imgs[(*count)++] = im;
247 ico_reader_close(file);
258 validate_image(i_img *im) {
259 if (im->xsize > 256 || im->ysize > 256) {
260 i_push_error(0, "image too large for ico file");
263 if (im->channels < 1 || im->channels > 4) {
264 /* this shouldn't happen, but check anyway */
265 i_push_error(0, "invalid channels");
273 translate_mask(i_img *im, unsigned char *out, const char *in) {
276 int len = strlen(in);
278 int newline; /* set to the first newline type we see */
279 int notnewline; /* set to whatever in ( "\n\r" newline isn't ) */
286 if (in[2] == '\n' || in[2] == '\r') {
288 notnewline = '\n' + '\r' - newline;
296 while (y < im->ysize && pos < len) {
298 while (x < im->xsize && pos < len) {
299 if (in[pos] == newline) {
300 /* don't process it, we look for it later */
303 else if (in[pos] == notnewline) {
304 ++pos; /* just drop it */
306 else if (in[pos] == one) {
311 else if (in[pos] == zero) {
316 else if (in[pos] == ' ' || in[pos] == '\t') {
317 /* just ignore whitespace */
324 while (x++ < im->xsize) {
327 while (pos < len && in[pos] != newline)
329 if (pos < len && in[pos] == newline)
330 ++pos; /* actually skip the newline */
334 while (y++ < im->ysize) {
335 for (x = 0; x < im->xsize; ++x)
343 derive_mask(i_img *im, ico_image_t *ico) {
345 if (im->channels == 1 || im->channels == 3) {
346 /* msicon.c's default mask is what we want */
347 myfree(ico->mask_data);
348 ico->mask_data = NULL;
351 int channel = im->channels - 1;
352 i_sample_t *linebuf = mymalloc(sizeof(i_sample_t) * im->xsize);
354 unsigned char *out = ico->mask_data;
356 for (y = 0; y < im->ysize; ++y) {
357 i_gsamp(im, 0, im->xsize, y, linebuf, &channel, 1);
358 for (x = 0; x < im->xsize; ++x) {
359 *out++ = linebuf[x] == 255 ? 0 : 1;
367 fill_image_base(i_img *im, ico_image_t *ico, const char *mask_name) {
370 ico->width = im->xsize;
371 ico->height = im->ysize;
372 ico->direct = im->type == i_direct_type;
378 unsigned char *linebuf = mymalloc(ico->width * 4);
379 ico->image_data = mymalloc(sizeof(ico_color_t) * ico->width * ico->height);
381 switch (im->channels) {
383 channels[0] = channels[1] = channels[2] = channels[3] = 0;
388 channels[0] = channels[1] = channels[2] = 0;
408 out = ico->image_data;
409 for (y = 0; y < im->ysize; ++y) {
410 i_gsamp(im, 0, im->xsize, y, linebuf, channels, 4);
412 for (x = 0; x < im->xsize; ++x) {
416 out->a = set_alpha ? 255 : *in;
429 i_palidx *linebuf = mymalloc(sizeof(i_palidx) * ico->width);
431 ico->image_data = mymalloc(sizeof(ico_color_t) * ico->width * ico->height);
433 out = ico->image_data;
434 for (y = 0; y < im->ysize; ++y) {
435 i_gpal(im, 0, im->xsize, y, linebuf);
437 for (x = 0; x < im->xsize; ++x) {
443 ico->palette_size = i_colorcount(im);
444 ico->palette = mymalloc(sizeof(ico_color_t) * ico->palette_size);
445 colors = mymalloc(sizeof(i_color) * ico->palette_size);
446 i_getcolors(im, 0, colors, ico->palette_size);
447 for (i = 0; i < ico->palette_size; ++i) {
448 if (im->channels == 1 || im->channels == 2) {
449 ico->palette[i].r = ico->palette[i].g =
450 ico->palette[i].b = colors[i].rgba.r;
453 ico->palette[i].r = colors[i].rgba.r;
454 ico->palette[i].g = colors[i].rgba.g;
455 ico->palette[i].b = colors[i].rgba.b;
465 ico->mask_data = mymalloc(im->xsize * im->ysize);
467 if (!i_tags_find(&im->tags, mask_name, 0, &mask_index)
468 || !im->tags.tags[mask_index].data
469 || !translate_mask(im, ico->mask_data,
470 im->tags.tags[mask_index].data)) {
471 derive_mask(im, ico);
477 unfill_image(ico_image_t *ico) {
478 myfree(ico->image_data);
480 myfree(ico->palette);
482 myfree(ico->mask_data);
486 fill_image_icon(i_img *im, ico_image_t *ico) {
487 fill_image_base(im, ico, "ico_mask");
488 ico->hotspot_x = ico->hotspot_y = 0;
492 i_writeico_wiol(i_io_glue_t *ig, i_img *im) {
498 if (!validate_image(im))
501 fill_image_icon(im, &ico);
503 if (!ico_write(ig, &ico, 1, ICON_ICON, &error)) {
504 ico_push_error(error);
511 if (i_io_close(ig) < 0) {
512 i_push_error(0, "error closing output");
520 i_writeico_multi_wiol(i_io_glue_t *ig, i_img **ims, int count) {
527 if (count > 0xFFFF) {
528 i_push_error(0, "too many images for ico files");
532 for (i = 0; i < count; ++i)
533 if (!validate_image(ims[i]))
536 icons = mymalloc(sizeof(ico_image_t) * count);
538 for (i = 0; i < count; ++i)
539 fill_image_icon(ims[i], icons + i);
541 if (!ico_write(ig, icons, count, ICON_ICON, &error)) {
542 ico_push_error(error);
543 for (i = 0; i < count; ++i)
544 unfill_image(icons + i);
549 for (i = 0; i < count; ++i)
550 unfill_image(icons + i);
553 if (i_io_close(ig) < 0) {
554 i_push_error(0, "error closing output");
562 fill_image_cursor(i_img *im, ico_image_t *ico) {
564 fill_image_base(im, ico, "ico_mask");
566 if (!i_tags_get_int(&im->tags, "cur_hotspotx", 0, &hotx))
568 if (!i_tags_get_int(&im->tags, "cur_hotspoty", 0, &hoty))
573 else if (hotx >= im->xsize)
574 hotx = im->xsize - 1;
578 else if (hoty >= im->ysize)
579 hoty = im->ysize - 1;
581 ico->hotspot_x = hotx;
582 ico->hotspot_y = hoty;
586 i_writecur_wiol(i_io_glue_t *ig, i_img *im) {
592 if (!validate_image(im))
595 fill_image_cursor(im, &ico);
597 if (!ico_write(ig, &ico, 1, ICON_CURSOR, &error)) {
598 ico_push_error(error);
605 if (i_io_close(ig) < 0) {
606 i_push_error(0, "error closing output");
614 i_writecur_multi_wiol(i_io_glue_t *ig, i_img **ims, int count) {
621 if (count > 0xFFFF) {
622 i_push_error(0, "too many images for ico files");
626 for (i = 0; i < count; ++i)
627 if (!validate_image(ims[i]))
630 icons = mymalloc(sizeof(ico_image_t) * count);
632 for (i = 0; i < count; ++i)
633 fill_image_cursor(ims[i], icons + i);
635 if (!ico_write(ig, icons, count, ICON_CURSOR, &error)) {
636 ico_push_error(error);
637 for (i = 0; i < count; ++i)
638 unfill_image(icons + i);
643 for (i = 0; i < count; ++i)
644 unfill_image(icons + i);
647 if (i_io_close(ig) < 0) {
648 i_push_error(0, "error closing output");