Skip to content
Browse files

add support for file write plugins

fix write_multi() writing to scalars
  https://rt.cpan.org/Ticket/Display.html?id=19982

add support for writing to ICO/CUR image files

added file limit check functions to the API
  • Loading branch information...
1 parent c64eee3 commit 2b405c9e0e655ef3c1a219f53195da7ddb6eadbc Tony Cook committed Jun 26, 2006
Showing with 2,783 additions and 197 deletions.
  1. +91 −1 ICO/ICO.pm
  2. +91 −0 ICO/ICO.xs
  3. +2 −0 ICO/Makefile.PL
  4. +436 −14 ICO/imicon.c
  5. +12 −0 ICO/imicon.h
  6. +7 −0 ICO/lib/Imager/File/CUR.pm
  7. +733 −25 ICO/msicon.c
  8. +10 −0 ICO/msicon.h
  9. +169 −7 ICO/t/t10icon.t
  10. +73 −0 ICO/t/t30cursor.t
  11. +10 −0 ICO/t/t40readcurone.t
  12. +10 −0 ICO/t/t41curmultread.t
  13. +337 −0 ICO/t/t50readfail.t
  14. +253 −0 ICO/t/t60writefail.t
  15. +11 −0 ICO/t/t70icosing.t
  16. +11 −0 ICO/t/t71icomult.t
  17. +11 −0 ICO/t/t72cursing.t
  18. +11 −0 ICO/t/t73curmult.t
  19. BIN ICO/testimg/pal43232.cur
  20. +230 −117 Imager.pm
  21. +15 −5 Imager.xs
  22. +11 −0 MANIFEST
  23. +4 −0 MANIFEST.SKIP
  24. +1 −1 Makefile.PL
  25. +0 −2 image.c
  26. +2 −0 imager.h
  27. +0 −3 imageri.h
  28. +5 −0 imext.c
  29. +7 −0 imext.h
  30. +7 −2 imexttypes.h
  31. +17 −13 iolayer.c
  32. +2 −2 iolayert.h
  33. +187 −3 lib/Imager/Files.pod
  34. +17 −2 t/t106tiff.t
View
92 ICO/ICO.pm
@@ -46,6 +46,88 @@ Imager->register_reader
},
);
+# the readers can read CUR files too
+Imager->register_reader
+ (
+ type=>'cur',
+ single =>
+ sub {
+ my ($im, $io, %hsh) = @_;
+ $im->{IMG} = i_readico_single($io, $hsh{page} || 0);
+
+ unless ($im->{IMG}) {
+ $im->_set_error(Imager->_error_as_msg);
+ return;
+ }
+ return $im;
+ },
+ multiple =>
+ sub {
+ my ($io, %hsh) = @_;
+
+ my @imgs = i_readico_multi($io);
+ unless (@imgs) {
+ Imager->_set_error(Imager->_error_as_msg);
+ return;
+ }
+ return map {
+ bless { IMG => $_, DEBUG => $Imager::DEBUG, ERRSTR => undef }, 'Imager'
+ } @imgs;
+ },
+ );
+
+Imager->register_writer
+ (
+ type=>'ico',
+ single =>
+ sub {
+ my ($im, $io, %hsh) = @_;
+
+ unless (i_writeico_wiol($io, $im->{IMG})) {
+ $im->_set_error(Imager->_error_as_msg);
+ return;
+ }
+ return $im;
+ },
+ multiple =>
+ sub {
+ my ($class, $io, $opts, @images) = @_;
+
+ if (!i_writeico_multi_wiol($io, map $_->{IMG}, @images)) {
+ Imager->_set_error(Imager->_error_as_msg);
+ return;
+ }
+
+ return 1;
+ },
+ );
+
+Imager->register_writer
+ (
+ type=>'cur',
+ single =>
+ sub {
+ my ($im, $io, %hsh) = @_;
+
+ unless (i_writecur_wiol($io, $im->{IMG})) {
+ $im->_set_error(Imager->_error_as_msg);
+ return;
+ }
+ return $im;
+ },
+ multiple =>
+ sub {
+ my ($class, $io, $opts, @images) = @_;
+
+ if (!i_writecur_multi_wiol($io, map $_->{IMG}, @images)) {
+ Imager->_set_error(Imager->_error_as_msg);
+ return;
+ }
+
+ return 1;
+ },
+ );
+
1;
__END__
@@ -65,12 +147,20 @@ Imager::File::ICO - read MS Icon files
my @imgs = Imager->read_multi(file => "foo.ico")
or die Imager->errstr;
-=head1 DESCRIPTION
+ $img->write(file => "foo.ico")
+ or die $img->errstr;
+ Imager->write_multi({ file => "foo.ico" }, @imgs)
+ or die Imager->errstr;
+
+=head1 DESCRIPTION
+Imager's MS Icon support is documented in L<Imager::Files>.
=head1 AUTHOR
+Tony Cook <tony@imager.perl.org>
+
=head1 SEE ALSO
Imager, Imager::Files.
View
91 ICO/ICO.xs
@@ -35,6 +35,97 @@ i_readico_multi(ig)
myfree(imgs);
}
+int
+i_writeico_wiol(ig, im)
+ Imager::IO ig
+ Imager::ImgRaw im
+
+undef_int
+i_writeico_multi_wiol(ig, ...)
+ Imager::IO ig
+ PREINIT:
+ int i;
+ int img_count;
+ i_img **imgs;
+ CODE:
+ if (items < 2)
+ croak("Usage: i_writeico_multi_wiol(ig, images...)");
+ img_count = items - 1;
+ RETVAL = 1;
+ if (img_count < 1) {
+ RETVAL = 0;
+ i_clear_error();
+ i_push_error(0, "You need to specify images to save");
+ }
+ else {
+ imgs = mymalloc(sizeof(i_img *) * img_count);
+ for (i = 0; i < img_count; ++i) {
+ SV *sv = ST(1+i);
+ imgs[i] = NULL;
+ if (SvROK(sv) && sv_derived_from(sv, "Imager::ImgRaw")) {
+ imgs[i] = INT2PTR(i_img *, SvIV((SV*)SvRV(sv)));
+ }
+ else {
+ i_clear_error();
+ i_push_error(0, "Only images can be saved");
+ myfree(imgs);
+ RETVAL = 0;
+ break;
+ }
+ }
+ if (RETVAL) {
+ RETVAL = i_writeico_multi_wiol(ig, imgs, img_count);
+ }
+ myfree(imgs);
+ }
+ OUTPUT:
+ RETVAL
+
+int
+i_writecur_wiol(ig, im)
+ Imager::IO ig
+ Imager::ImgRaw im
+
+undef_int
+i_writecur_multi_wiol(ig, ...)
+ Imager::IO ig
+ PREINIT:
+ int i;
+ int img_count;
+ i_img **imgs;
+ CODE:
+ if (items < 2)
+ croak("Usage: i_writecur_multi_wiol(ig, images...)");
+ img_count = items - 1;
+ RETVAL = 1;
+ if (img_count < 1) {
+ RETVAL = 0;
+ i_clear_error();
+ i_push_error(0, "You need to specify images to save");
+ }
+ else {
+ imgs = mymalloc(sizeof(i_img *) * img_count);
+ for (i = 0; i < img_count; ++i) {
+ SV *sv = ST(1+i);
+ imgs[i] = NULL;
+ if (SvROK(sv) && sv_derived_from(sv, "Imager::ImgRaw")) {
+ imgs[i] = INT2PTR(i_img *, SvIV((SV*)SvRV(sv)));
+ }
+ else {
+ i_clear_error();
+ i_push_error(0, "Only images can be saved");
+ myfree(imgs);
+ RETVAL = 0;
+ break;
+ }
+ }
+ if (RETVAL) {
+ RETVAL = i_writecur_multi_wiol(ig, imgs, img_count);
+ }
+ myfree(imgs);
+ }
+ OUTPUT:
+ RETVAL
BOOT:
PERL_INITIALIZE_IMAGER_CALLBACKS;
View
2 ICO/Makefile.PL
@@ -1,3 +1,5 @@
+#!perl -w
+use strict;
use ExtUtils::MakeMaker;
my %opts =
View
450 ICO/imicon.c
@@ -1,6 +1,7 @@
#include "imext.h"
#include "imicon.h"
#include "msicon.h"
+#include <string.h>
static void
ico_push_error(int error) {
@@ -19,24 +20,30 @@ read_one_icon(ico_reader_t *file, int index) {
image = ico_image_read(file, index, &error);
if (!image) {
- ico_reader_close(file);
ico_push_error(error);
+ i_push_error(0, "error reading ICO/CUR image");
return NULL;
}
if (image->direct) {
int x, y;
- i_color *line_buf = mymalloc(image->width * sizeof(i_color));
+ i_color *line_buf;
i_color *outp;
ico_color_t *inp = image->image_data;
+ if (!i_int_check_image_file_limits(image->width, image->height, 4, 1)) {
+ ico_image_release(image);
+ return NULL;
+ }
+
result = i_img_8_new(image->width, image->height, 4);
if (!result) {
ico_image_release(image);
- ico_reader_close(file);
return NULL;
}
+ line_buf = mymalloc(image->width * sizeof(i_color));
+
for (y = 0; y < image->height; ++y) {
outp = line_buf;
for (x = 0; x < image->width; ++x) {
@@ -57,10 +64,14 @@ read_one_icon(ico_reader_t *file, int index) {
int y;
unsigned char *image_data;
+ if (!i_int_check_image_file_limits(image->width, image->height, 3, 1)) {
+ ico_image_release(image);
+ return NULL;
+ }
+
result = i_img_pal_new(image->width, image->height, 3, 256);
if (!result) {
ico_image_release(image);
- ico_reader_close(file);
return NULL;
}
@@ -75,7 +86,6 @@ read_one_icon(ico_reader_t *file, int index) {
if (i_addcolors(result, &c, 1) < 0) {
i_push_error(0, "could not add color to palette");
ico_image_release(image);
- ico_reader_close(file);
i_img_destroy(result);
return NULL;
}
@@ -111,13 +121,23 @@ read_one_icon(ico_reader_t *file, int index) {
}
*outp++ = '\0';
- i_tags_set(&result->tags, "ico_mask", mask, (outp-mask)-1);
+ if (ico_type(file) == ICON_ICON)
+ i_tags_set(&result->tags, "ico_mask", mask, (outp-mask)-1);
+ else
+ i_tags_set(&result->tags, "cur_mask", mask, (outp-mask)-1);
myfree(mask);
}
- i_tags_setn(&result->tags, "ico_bits", image->bit_count);
- i_tags_set(&result->tags, "ico_type", ico_type(file) == ICON_ICON ? "icon" : "cursor", -1);
- i_tags_set(&result->tags, "i_format", "ico", 3);
+ if (ico_type(file) == ICON_ICON) {
+ i_tags_setn(&result->tags, "ico_bits", image->bit_count);
+ i_tags_set(&result->tags, "i_format", "ico", 3);
+ }
+ else {
+ i_tags_setn(&result->tags, "cur_bits", image->bit_count);
+ i_tags_set(&result->tags, "i_format", "cur", 3);
+ i_tags_setn(&result->tags, "cur_hotspotx", image->hotspot_x);
+ i_tags_setn(&result->tags, "cur_hotspoty", image->hotspot_y);
+ }
ico_image_release(image);
@@ -130,16 +150,16 @@ i_readico_single(io_glue *ig, int index) {
i_img *result;
int error;
+ i_clear_error();
+
file = ico_reader_open(ig, &error);
if (!file) {
ico_push_error(error);
+ i_push_error(0, "error opening ICO/CUR file");
return NULL;
}
- if (index < 0 && index >= ico_image_count(file)) {
- i_push_error(0, "page out of range");
- return NULL;
- }
+ /* the index is range checked by msicon.c - don't duplicate it here */
result = read_one_icon(file, index);
ico_reader_close(file);
@@ -154,9 +174,12 @@ i_readico_multi(io_glue *ig, int *count) {
int error;
i_img **imgs;
+ i_clear_error();
+
file = ico_reader_open(ig, &error);
if (!file) {
ico_push_error(error);
+ i_push_error(0, "error opening ICO/CUR file");
return NULL;
}
@@ -165,7 +188,7 @@ i_readico_multi(io_glue *ig, int *count) {
*count = 0;
for (index = 0; index < ico_image_count(file); ++index) {
i_img *im = read_one_icon(file, index);
- if (!im)
+ if (!im)
break;
imgs[(*count)++] = im;
@@ -180,3 +203,402 @@ i_readico_multi(io_glue *ig, int *count) {
return imgs;
}
+
+static int
+validate_image(i_img *im) {
+ if (im->xsize > 255 || im->ysize > 255) {
+ i_push_error(0, "image too large for ico file");
+ return 0;
+ }
+ if (im->channels < 1 || im->channels > 4) {
+ /* this shouldn't happen, but check anyway */
+ i_push_error(0, "invalid channels");
+ return 0;
+ }
+
+ return 1;
+}
+
+static int
+translate_mask(i_img *im, unsigned char *out, const char *in) {
+ int x, y;
+ int one, zero;
+ int len = strlen(in);
+ int pos;
+ int newline; /* set to the first newline type we see */
+ int notnewline; /* set to whatever in ( "\n\r" newline isn't ) */
+
+ if (len < 3)
+ return 0;
+
+ zero = in[0];
+ one = in[1];
+ if (in[2] == '\n' || in[2] == '\r') {
+ newline = in[2];
+ notnewline = '\n' + '\r' - newline;
+ }
+ else {
+ return 0;
+ }
+
+ pos = 3;
+ y = 0;
+ while (y < im->ysize && pos < len) {
+ x = 0;
+ while (x < im->xsize && pos < len) {
+ if (in[pos] == newline) {
+ /* don't process it, we look for it later */
+ break;
+ }
+ else if (in[pos] == notnewline) {
+ ++pos; /* just drop it */
+ }
+ else if (in[pos] == one) {
+ *out++ = 1;
+ ++x;
+ ++pos;
+ }
+ else if (in[pos] == zero) {
+ *out++ = 0;
+ ++x;
+ ++pos;
+ }
+ else if (in[pos] == ' ' || in[pos] == '\t') {
+ /* just ignore whitespace */
+ ++pos;
+ }
+ else {
+ return 0;
+ }
+ }
+ while (x++ < im->xsize) {
+ *out++ = 0;
+ }
+ while (pos < len && in[pos] != newline)
+ ++pos;
+ if (pos < len && in[pos] == newline)
+ ++pos; /* actually skip the newline */
+
+ ++y;
+ }
+ while (y++ < im->ysize) {
+ for (x = 0; x < im->xsize; ++x)
+ *out++ = 0;
+ }
+
+ return 1;
+}
+
+static void
+derive_mask(i_img *im, ico_image_t *ico) {
+
+ if (im->channels == 1 || im->channels == 3) {
+ /* msicon.c's default mask is what we want */
+ myfree(ico->mask_data);
+ ico->mask_data = NULL;
+ }
+ else {
+ int channel = im->channels - 1;
+ i_sample_t *linebuf = mymalloc(sizeof(i_sample_t) * im->xsize);
+ int x, y;
+ unsigned char *out = ico->mask_data;
+
+ for (y = 0; y < im->ysize; ++y) {
+ i_gsamp(im, 0, im->xsize, y, linebuf, &channel, 1);
+ for (x = 0; x < im->xsize; ++x) {
+ *out++ = linebuf[x] == 255 ? 0 : 1;
+ }
+ }
+ myfree(linebuf);
+ }
+}
+
+static void
+fill_image_base(i_img *im, ico_image_t *ico, const char *mask_name) {
+ int x, y;
+
+ ico->width = im->xsize;
+ ico->height = im->ysize;
+ ico->direct = im->type == i_direct_type;
+ if (ico->direct) {
+ int channels[4];
+ int set_alpha = 0;
+ ico_color_t *out;
+ i_sample_t *in;
+ unsigned char *linebuf = mymalloc(ico->width * 4);
+ ico->image_data = mymalloc(sizeof(ico_color_t) * ico->width * ico->height);
+
+ switch (im->channels) {
+ case 1:
+ channels[0] = channels[1] = channels[2] = channels[3] = 0;
+ ++set_alpha;
+ break;
+
+ case 2:
+ channels[0] = channels[1] = channels[2] = 0;
+ channels[3] = 1;
+ break;
+
+ case 3:
+ channels[0] = 0;
+ channels[1] = 1;
+ channels[2] = 2;
+ channels[3] = 2;
+ ++set_alpha;
+ break;
+
+ case 4:
+ channels[0] = 0;
+ channels[1] = 1;
+ channels[2] = 2;
+ channels[3] = 3;
+ break;
+ }
+
+ out = ico->image_data;
+ for (y = 0; y < im->ysize; ++y) {
+ i_gsamp(im, 0, im->xsize, y, linebuf, channels, 4);
+ in = linebuf;
+ for (x = 0; x < im->xsize; ++x) {
+ out->r = *in++;
+ out->g = *in++;
+ out->b = *in++;
+ out->a = set_alpha ? 255 : *in;
+ in++;
+ ++out;
+ }
+ }
+ myfree(linebuf);
+ ico->palette = NULL;
+ }
+ else {
+ unsigned char *out;
+ i_color *colors;
+ int i;
+ i_palidx *in;
+ i_palidx *linebuf = mymalloc(sizeof(i_palidx) * ico->width);
+
+ ico->image_data = mymalloc(sizeof(ico_color_t) * ico->width * ico->height);
+
+ out = ico->image_data;
+ for (y = 0; y < im->ysize; ++y) {
+ i_gpal(im, 0, im->xsize, y, linebuf);
+ in = linebuf;
+ for (x = 0; x < im->xsize; ++x) {
+ *out++ = *in++;
+ }
+ }
+ myfree(linebuf);
+
+ ico->palette_size = i_colorcount(im);
+ ico->palette = mymalloc(sizeof(ico_color_t) * ico->palette_size);
+ colors = mymalloc(sizeof(i_color) * ico->palette_size);
+ i_getcolors(im, 0, colors, ico->palette_size);
+ for (i = 0; i < ico->palette_size; ++i) {
+ if (im->channels == 1 || im->channels == 2) {
+ ico->palette[i].r = ico->palette[i].g =
+ ico->palette[i].b = colors[i].rgba.r;
+ }
+ else {
+ ico->palette[i].r = colors[i].rgba.r;
+ ico->palette[i].g = colors[i].rgba.g;
+ ico->palette[i].b = colors[i].rgba.b;
+ }
+ }
+ myfree(colors);
+ }
+
+ {
+ /* build the mask */
+ int mask_index;
+
+ ico->mask_data = mymalloc(im->xsize * im->ysize);
+
+ if (!i_tags_find(&im->tags, mask_name, 0, &mask_index)
+ || !im->tags.tags[mask_index].data
+ || !translate_mask(im, ico->mask_data,
+ im->tags.tags[mask_index].data)) {
+ derive_mask(im, ico);
+ }
+ }
+}
+
+static void
+unfill_image(ico_image_t *ico) {
+ myfree(ico->image_data);
+ if (ico->palette)
+ myfree(ico->palette);
+ if (ico->mask_data)
+ myfree(ico->mask_data);
+}
+
+static void
+fill_image_icon(i_img *im, ico_image_t *ico) {
+ fill_image_base(im, ico, "ico_mask");
+ ico->hotspot_x = ico->hotspot_y = 0;
+}
+
+int
+i_writeico_wiol(i_io_glue_t *ig, i_img *im) {
+ ico_image_t ico;
+ int error;
+
+ i_clear_error();
+
+ if (!validate_image(im))
+ return 0;
+
+ fill_image_icon(im, &ico);
+
+ if (!ico_write(ig, &ico, 1, ICON_ICON, &error)) {
+ ico_push_error(error);
+ unfill_image(&ico);
+ return 0;
+ }
+
+ unfill_image(&ico);
+
+ if (i_io_close(ig) < 0) {
+ i_push_error(0, "error closing output");
+ return 0;
+ }
+
+ return 1;
+}
+
+int
+i_writeico_multi_wiol(i_io_glue_t *ig, i_img **ims, int count) {
+ ico_image_t *icons;
+ int error;
+ int i;
+
+ i_clear_error();
+
+ if (count > 0xFFFF) {
+ i_push_error(0, "too many images for ico files");
+ return 0;
+ }
+
+ for (i = 0; i < count; ++i)
+ if (!validate_image(ims[i]))
+ return 0;
+
+ icons = mymalloc(sizeof(ico_image_t) * count);
+
+ for (i = 0; i < count; ++i)
+ fill_image_icon(ims[i], icons + i);
+
+ if (!ico_write(ig, icons, count, ICON_ICON, &error)) {
+ ico_push_error(error);
+ for (i = 0; i < count; ++i)
+ unfill_image(icons + i);
+ myfree(icons);
+ return 0;
+ }
+
+ for (i = 0; i < count; ++i)
+ unfill_image(icons + i);
+ myfree(icons);
+
+ if (i_io_close(ig) < 0) {
+ i_push_error(0, "error closing output");
+ return 0;
+ }
+
+ return 1;
+}
+
+void
+fill_image_cursor(i_img *im, ico_image_t *ico) {
+ int hotx, hoty;
+ fill_image_base(im, ico, "ico_mask");
+
+ if (!i_tags_get_int(&im->tags, "cur_hotspotx", 0, &hotx))
+ hotx = 0;
+ if (!i_tags_get_int(&im->tags, "cur_hotspoty", 0, &hoty))
+ hoty = 0;
+
+ if (hotx < 0)
+ hotx = 0;
+ else if (hotx >= im->xsize)
+ hotx = im->xsize - 1;
+
+ if (hoty < 0)
+ hoty = 0;
+ else if (hoty >= im->ysize)
+ hoty = im->ysize - 1;
+
+ ico->hotspot_x = hotx;
+ ico->hotspot_y = hoty;
+}
+
+int
+i_writecur_wiol(i_io_glue_t *ig, i_img *im) {
+ ico_image_t ico;
+ int error;
+
+ i_clear_error();
+
+ if (!validate_image(im))
+ return 0;
+
+ fill_image_cursor(im, &ico);
+
+ if (!ico_write(ig, &ico, 1, ICON_CURSOR, &error)) {
+ ico_push_error(error);
+ unfill_image(&ico);
+ return 0;
+ }
+
+ unfill_image(&ico);
+
+ if (i_io_close(ig) < 0) {
+ i_push_error(0, "error closing output");
+ return 0;
+ }
+
+ return 1;
+}
+
+int
+i_writecur_multi_wiol(i_io_glue_t *ig, i_img **ims, int count) {
+ ico_image_t *icons;
+ int error;
+ int i;
+
+ i_clear_error();
+
+ if (count > 0xFFFF) {
+ i_push_error(0, "too many images for ico files");
+ return 0;
+ }
+
+ for (i = 0; i < count; ++i)
+ if (!validate_image(ims[i]))
+ return 0;
+
+ icons = mymalloc(sizeof(ico_image_t) * count);
+
+ for (i = 0; i < count; ++i)
+ fill_image_cursor(ims[i], icons + i);
+
+ if (!ico_write(ig, icons, count, ICON_CURSOR, &error)) {
+ ico_push_error(error);
+ for (i = 0; i < count; ++i)
+ unfill_image(icons + i);
+ myfree(icons);
+ return 0;
+ }
+
+ for (i = 0; i < count; ++i)
+ unfill_image(icons + i);
+ myfree(icons);
+
+ if (i_io_close(ig) < 0) {
+ i_push_error(0, "error closing output");
+ return 0;
+ }
+
+ return 1;
+}
+
View
12 ICO/imicon.h
@@ -8,4 +8,16 @@ i_readico_single(io_glue *ig, int index);
extern i_img **
i_readico_multi(io_glue *ig, int *count);
+extern int
+i_writeico_wiol(i_io_glue_t *ig, i_img *im);
+
+extern int
+i_writeico_multi_wiol(i_io_glue_t *ig, i_img **im, int count);
+
+extern int
+i_writecur_wiol(i_io_glue_t *ig, i_img *im);
+
+extern int
+i_writecur_multi_wiol(i_io_glue_t *ig, i_img **im, int count);
+
#endif
View
7 ICO/lib/Imager/File/CUR.pm
@@ -0,0 +1,7 @@
+package Imager::File::CUR;
+use strict;
+
+# all the work is done by Imager::File::ICO
+use Imager::File::ICO;
+
+1;
View
758 ICO/msicon.c
@@ -3,7 +3,10 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
+#include <assert.h>
+static
+int read_packed(io_glue *ig, const char *format, ...);
static int
read_palette(ico_reader_t *file, ico_image_t *image, int *error);
static int
@@ -16,12 +19,31 @@ static int
read_1bit_data(ico_reader_t *file, ico_image_t *image, int *error);
static int
read_mask(ico_reader_t *file, ico_image_t *image, int *error);
+static int
+ico_write_validate(ico_image_t const *images, int image_count, int *error);
+static int
+ico_image_size(ico_image_t const *image, int *bits, int *colors);
+static int
+write_packed(i_io_glue_t *ig, char const *format, ...);
+static int
+write_palette(i_io_glue_t *ig, ico_image_t const *image, int *error);
+static int
+write_32_bit(i_io_glue_t *ig, ico_image_t const *image, int *error);
+static int
+write_8_bit(i_io_glue_t *ig, ico_image_t const *image, int *error);
+static int
+write_4_bit(i_io_glue_t *ig, ico_image_t const *image, int *error);
+static int
+write_1_bit(i_io_glue_t *ig, ico_image_t const *image, int *error);
+static int
+write_mask(i_io_glue_t *ig, ico_image_t const *image, int *error);
typedef struct {
int width;
int height;
long offset;
long size;
+ int hotspot_x, hotspot_y;
} ico_reader_image_entry;
/* this was previously declared, now define it */
@@ -39,9 +61,6 @@ struct ico_reader_tag {
ico_reader_image_entry *images;
};
-static
-int read_packed(io_glue *ig, const char *format, ...);
-
/*
=head1 NAME
@@ -133,13 +152,31 @@ ico_reader_open(i_io_glue_t *ig, int *error) {
for (i = 0; i < count; ++i) {
long width, height, bytes_in_res, image_offset;
+
ico_reader_image_entry *image = file->images + i;
- if (!read_packed(ig, "bbxxxxxxdd", &width, &height, &bytes_in_res,
- &image_offset)) {
- free(file->images);
- free(file);
- *error = ICOERR_Short_File;
- return NULL;
+ if (type == ICON_ICON) {
+ if (!read_packed(ig, "bb xxxxxx dd", &width, &height, &bytes_in_res,
+ &image_offset)) {
+ free(file->images);
+ free(file);
+ *error = ICOERR_Short_File;
+ return NULL;
+ }
+ image->hotspot_x = image->hotspot_y = 0;
+ }
+ else {
+ long hotspot_x, hotspot_y;
+
+ if (!read_packed(ig, "bb xx ww dd", &width, &height,
+ &hotspot_x, &hotspot_y, &bytes_in_res,
+ &image_offset)) {
+ free(file->images);
+ free(file);
+ *error = ICOERR_Short_File;
+ return NULL;
+ }
+ image->hotspot_x = hotspot_x;
+ image->hotspot_y = hotspot_y;
}
image->width = width;
@@ -168,7 +205,7 @@ ico_image_count(ico_reader_t *file) {
/*
=item ico_type
- // type of file - 1 for icon, 2 for cursor
+ // type of file - ICON_ICON for icon, ICON_CURSOR for cursor
type = ico_type(file);
=cut
@@ -219,6 +256,11 @@ ico_image_read(ico_reader_t *file, int index, int *error) {
return NULL;
}
+ if (bit_count != 1 && bit_count != 4 && bit_count != 8 && bit_count != 32) {
+ *error = ICOERR_Unknown_Bits;
+ return 0;
+ }
+
result = malloc(sizeof(ico_image_t));
if (!result) {
*error = ICOERR_Out_Of_Memory;
@@ -231,8 +273,10 @@ ico_image_read(ico_reader_t *file, int index, int *error) {
result->palette = NULL;
result->image_data = NULL;
result->mask_data = NULL;
+ result->hotspot_x = im->hotspot_x;
+ result->hotspot_y = im->hotspot_y;
- if (result->direct) {
+ if (bit_count == 32) {
result->palette_size = 0;
result->image_data = malloc(result->width * result->height * sizeof(ico_color_t));
@@ -241,31 +285,31 @@ ico_image_read(ico_reader_t *file, int index, int *error) {
*error = ICOERR_Out_Of_Memory;
return NULL;
}
- if (bit_count == 32) {
- if (!read_32bit_data(file, result, error)) {
- free(result->image_data);
- free(result);
- return NULL;
- }
- }
- else {
- *error = ICOERR_Unknown_Bits;
+ if (!read_32bit_data(file, result, error)) {
free(result->image_data);
free(result);
return NULL;
}
}
else {
int read_result;
+
result->palette_size = 1 << bit_count;
result->palette = malloc(sizeof(ico_color_t) * result->palette_size);
- result->image_data = malloc(result->width * result->height);
if (!result->palette) {
free(result);
*error = ICOERR_Out_Of_Memory;
return NULL;
}
+ result->image_data = malloc(result->width * result->height);
+ if (!result->image_data) {
+ *error = ICOERR_Out_Of_Memory;
+ free(result->palette);
+ free(result);
+ return 0;
+ }
+
if (!read_palette(file, result, error)) {
free(result->palette);
free(result->image_data);
@@ -287,8 +331,8 @@ ico_image_read(ico_reader_t *file, int index, int *error) {
break;
default:
+ assert(0); /* this can't happen in theory */
read_result = 0;
- *error = ICOERR_Unknown_Bits;
break;
}
@@ -339,7 +383,7 @@ ico_image_release(ico_image_t *image) {
/*
=item ico_reader_close
-Releases the file structure.
+Releases the read file structure.
=cut
*/
@@ -352,6 +396,141 @@ ico_reader_close(ico_reader_t *file) {
}
/*
+=back
+
+=head1 WRITING ICON FILES
+
+=over
+
+=item ico_write(ig, images, image_count, type, &error)
+
+Parameters:
+
+=over
+
+=item *
+
+io_glue *ig - an Imager IO object. This only needs to implement
+writing for ico_write()
+
+=item *
+
+ico_image_t *images - array of images to be written.
+
+=item *
+
+int image_count - number of images
+
+=item *
+
+int type - must be ICON_ICON or ICON_CURSOR
+
+=item *
+
+int *error - set to an error code on failure.
+
+=back
+
+Returns non-zero on success.
+
+=cut
+*/
+
+int
+ico_write(i_io_glue_t *ig, ico_image_t const *images, int image_count,
+ int type, int *error) {
+ int i;
+ int start_offset = 6 + 16 * image_count;
+ int current_offset = start_offset;
+
+ if (type != ICON_ICON && type != ICON_CURSOR) {
+ *error = ICOERR_Bad_File_Type;
+ return 0;
+ }
+
+ /* validate the images */
+ if (!ico_write_validate(images, image_count, error))
+ return 0;
+
+ /* write the header */
+ if (!write_packed(ig, "www", 0, type, image_count)) {
+ *error = ICOERR_Write_Failure;
+ return 0;
+ }
+
+ /* work out the offsets of each image */
+ for (i = 0; i < image_count; ++i) {
+ ico_image_t const *image = images + i;
+ int bits, colors;
+ int size = ico_image_size(image, &bits, &colors);
+
+ if (type == ICON_ICON) {
+ if (!write_packed(ig, "bbbbwwdd", image->width, image->height,
+ colors, 0, 1, bits, (unsigned long)size,
+ (unsigned long)current_offset)) {
+ *error = ICOERR_Write_Failure;
+ return 0;
+ }
+ }
+ else {
+ int hotspot_x = image->hotspot_x;
+ int hotspot_y = image->hotspot_y;
+
+ if (hotspot_x < 0)
+ hotspot_x = 0;
+ else if (hotspot_x >= image->width)
+ hotspot_x = image->width - 1;
+ if (hotspot_y < 0)
+ hotspot_y = 0;
+ else if (hotspot_y >= image->height)
+ hotspot_y = image->height - 1;
+
+ if (!write_packed(ig, "bbbbwwdd", image->width, image->height,
+ colors, 0, hotspot_x, hotspot_y, (unsigned long)size,
+ (unsigned long)current_offset)) {
+ *error = ICOERR_Write_Failure;
+ return 0;
+ }
+ }
+ current_offset += size;
+ }
+
+ /* write out each image */
+ for (i = 0; i < image_count; ++i) {
+ ico_image_t const *image = images + i;
+
+ if (image->direct) {
+ if (!write_32_bit(ig, image, error))
+ return 0;
+ }
+ else {
+ if (image->palette_size <= 2) {
+ if (!write_1_bit(ig, image, error))
+ return 0;
+ }
+ else if (image->palette_size <= 16) {
+ if (!write_4_bit(ig, image, error))
+ return 0;
+ }
+ else {
+ if (!write_8_bit(ig, image, error))
+ return 0;
+ }
+ }
+ if (!write_mask(ig, image, error))
+ return 0;
+ }
+
+ return 1;
+}
+
+/*
+=back
+
+=head1 ERROR MESSAGES
+
+=over
+
=item ico_error_message
Converts an error code into an error message.
@@ -373,6 +552,10 @@ ico_error_message(int error, char *buffer, size_t buffer_size) {
msg = "I/O error";
break;
+ case ICOERR_Write_Failure:
+ msg = "Write failure";
+ break;
+
case ICOERR_Invalid_File:
msg = "Not an icon file";
break;
@@ -385,6 +568,26 @@ ico_error_message(int error, char *buffer, size_t buffer_size) {
msg = "Image index out of range";
break;
+ case ICOERR_Bad_File_Type:
+ msg = "Bad file type parameter";
+ break;
+
+ case ICOERR_Invalid_Width:
+ msg = "Invalid image width";
+ break;
+
+ case ICOERR_Invalid_Height:
+ msg = "Invalid image height";
+ break;
+
+ case ICOERR_Invalid_Palette:
+ msg = "Invalid Palette";
+ break;
+
+ case ICOERR_No_Data:
+ msg = "No image data in image supplied to ico_write";
+ break;
+
case ICOERR_Out_Of_Memory:
msg = "Out of memory";
break;
@@ -725,6 +928,7 @@ read_mask(ico_reader_t *file, ico_image_t *image, int *error) {
unsigned char *read_buffer = malloc(line_bytes);
int y;
int x;
+ int mask;
unsigned char *inp, *outp;
if (!read_buffer) {
@@ -741,10 +945,14 @@ read_mask(ico_reader_t *file, ico_image_t *image, int *error) {
outp = image->mask_data + y * image->width;
inp = read_buffer;
+ mask = 0x80;
for (x = 0; x < image->width; ++x) {
- *outp++ = (*inp >> (7 - (x & 7))) & 1;
- if ((x & 7) == 7)
+ *outp++ = (*inp & mask) ? 1 : 0;
+ mask >>= 1;
+ if (!mask) {
+ mask = 0x80;
++inp;
+ }
}
}
free(read_buffer);
@@ -753,6 +961,506 @@ read_mask(ico_reader_t *file, ico_image_t *image, int *error) {
}
/*
+=item ico_write_validate
+
+Check each image to make sure it can go into an icon file.
+
+=cut
+*/
+
+static int
+ico_write_validate(ico_image_t const *images, int image_count, int *error) {
+ int i;
+
+ for (i = 0; i < image_count; ++i) {
+ ico_image_t const *image = images + i;
+
+ if (image->width < 1 || image->width > 255) {
+ *error = ICOERR_Invalid_Width;
+ return 0;
+ }
+ if (image->height < 1 || image->height > 255) {
+ *error = ICOERR_Invalid_Height;
+ return 0;
+ }
+ if (!image->image_data) {
+ *error = ICOERR_No_Data;
+ return 0;
+ }
+ if (!image->direct) {
+ if (image->palette_size < 0 || image->palette_size > 256
+ || !image->palette) {
+ *error = ICOERR_Invalid_Palette;
+ return 0;
+ }
+ }
+ }
+
+ return 1;
+}
+
+/*
+=item ico_image_size
+
+Calculate how much space the icon takes up in the file.
+
+=cut
+*/
+
+static int
+ico_image_size(ico_image_t const *image, int *bits, int *colors) {
+ int size = 40; /* start with the BITMAPINFOHEADER */
+
+ /* add in the image area */
+ if (image->direct) {
+ *bits = 32;
+ *colors = 0;
+ size += image->width * 4 * image->height;
+ }
+ else {
+ if (image->palette_size <= 2) {
+ *bits = 1;
+ *colors = 2;
+ }
+ else if (image->palette_size <= 16) {
+ *bits = 4;
+ *colors = 16;
+ }
+ else {
+ *bits = 8;
+ *colors = 0;
+ }
+
+ /* palette size */
+ size += *colors * 4;
+
+ /* image data size */
+ size += (image->width * *bits + 31) / 32 * 4 * image->height;
+ }
+
+ /* add in the mask */
+ size += (image->width + 31) / 32 * 4 * image->height;
+
+ return size;
+}
+
+/*
+=item write_packed
+
+Pack numbers given a format to a stream.
+
+=cut
+*/
+
+static int
+write_packed(i_io_glue_t *ig, char const *format, ...) {
+ unsigned char buffer[100];
+ va_list ap;
+ unsigned long p;
+ int size;
+ const char *formatp;
+ unsigned char *bufp;
+
+ /* write efficiently, work out the size of the buffer */
+ size = 0;
+ formatp = format;
+ while (*formatp) {
+ switch (*formatp++) {
+ case 'b': size++; break;
+ case 'w': size += 2; break;
+ case 'd': size += 4; break;
+ case ' ': break; /* space to separate components */
+ default:
+ fprintf(stderr, "invalid unpack char in %s\n", format);
+ exit(1);
+ }
+ }
+
+ if (size > sizeof(buffer)) {
+ /* catch if we need a bigger buffer, but 100 is plenty */
+ fprintf(stderr, "format %s too long for buffer\n", format);
+ exit(1);
+ }
+
+ va_start(ap, format);
+
+ bufp = buffer;
+ while (*format) {
+
+ switch (*format) {
+ case 'b':
+ p = va_arg(ap, int);
+ *bufp++ = p;
+ break;
+
+ case 'w':
+ p = va_arg(ap, int);
+ *bufp++ = p & 0xFF;
+ *bufp++ = (p >> 8) & 0xFF;
+ break;
+
+ case 'd':
+ p = va_arg(ap, unsigned long);
+ *bufp++ = p & 0xFF;
+ *bufp++ = (p >> 8) & 0xFF;
+ *bufp++ = (p >> 16) & 0xFF;
+ *bufp++ = (p >> 24) & 0xFF;
+ break;
+
+ case ' ':
+ /* nothing to do */
+ break;
+ }
+ ++format;
+ }
+
+ if (i_io_write(ig, buffer, size) != size)
+ return 0;
+
+ return 1;
+}
+
+/*
+=item write_palette
+
+Write the palette for an icon.
+
+=cut
+*/
+
+static int
+write_palette(i_io_glue_t *ig, ico_image_t const *image, int *error) {
+ int full_size = image->palette_size;
+ unsigned char *writebuf, *outp;
+ ico_color_t *colorp;
+ int i;
+
+ if (image->palette_size <= 2)
+ full_size = 2;
+ else if (image->palette_size <= 16)
+ full_size = 16;
+ else
+ full_size = 256;
+
+ writebuf = calloc(full_size, 4);
+ if (!writebuf) {
+ *error = ICOERR_Out_Of_Memory;
+ return 0;
+ }
+ outp = writebuf;
+ colorp = image->palette;
+ for (i = 0; i < image->palette_size; ++i) {
+ *outp++ = colorp->b;
+ *outp++ = colorp->g;
+ *outp++ = colorp->r;
+ *outp++ = 0xFF;
+ ++colorp;
+ }
+ for (; i < full_size; ++i) {
+ *outp++ = 0;
+ *outp++ = 0;
+ *outp++ = 0;
+ *outp++ = 0;
+ }
+
+ if (i_io_write(ig, writebuf, full_size * 4) != full_size * 4) {
+ *error = ICOERR_Write_Failure;
+ free(writebuf);
+ return 0;
+ }
+
+ free(writebuf);
+
+ return 1;
+}
+
+/*
+=item write_bitmapinfoheader
+
+Write the BITMAPINFOHEADER for an icon image.
+
+=cut
+*/
+
+static int
+write_bitmapinfoheader(i_io_glue_t *ig, ico_image_t const *image, int *error,
+ int bit_count, int clr_used) {
+ if (!write_packed(ig, "d dd w w d d dd dd",
+ 40UL, /* biSize */
+ (unsigned long)image->width,
+ (unsigned long)2 * image->height, /* biWidth/biHeight */
+ 1, bit_count, /* biPlanes, biBitCount */
+ 0UL, 0UL, /* biCompression, biSizeImage */
+ 0UL, 0UL, /* bi(X|Y)PetsPerMeter */
+ (unsigned long)clr_used, /* biClrUsed */
+ 0UL)) { /* biClrImportant */
+ *error = ICOERR_Write_Failure;
+ return 0;
+ }
+
+ return 1;
+}
+
+/*
+=item write_32_bit
+
+Write 32-bit image data to the icon.
+
+=cut
+*/
+
+static int
+write_32_bit(i_io_glue_t *ig, ico_image_t const *image, int *error) {
+ unsigned char *writebuf;
+ ico_color_t *data = image->image_data, *colorp;
+ unsigned char *writep;
+ int x, y;
+
+ if (!write_bitmapinfoheader(ig, image, error, 32, 0)) {
+ return 0;
+ }
+
+ writebuf = malloc(image->width * 4);
+ if (!writebuf) {
+ *error = ICOERR_Out_Of_Memory;
+ return 0;
+ }
+
+ for (y = image->height-1; y >= 0; --y) {
+ writep = writebuf;
+ colorp = data + y * image->width;
+ for (x = 0; x < image->width; ++x) {
+ *writep++ = colorp->b;
+ *writep++ = colorp->g;
+ *writep++ = colorp->r;
+ *writep++ = colorp->a;
+ ++colorp;
+ }
+ if (i_io_write(ig, writebuf, image->width * 4) != image->width * 4) {
+ *error = ICOERR_Write_Failure;
+ free(writebuf);
+ return 0;
+ }
+ }
+
+ free(writebuf);
+
+ return 1;
+}
+
+/*
+=item write_8_bit
+
+Write 8 bit image data.
+
+=cut
+*/
+
+static int
+write_8_bit(i_io_glue_t *ig, ico_image_t const *image, int *error) {
+ static const unsigned char zeros[3] = { '\0' };
+ int y;
+ const unsigned char *data = image->image_data;
+ int zero_count = (0U - (unsigned)image->width) & 3;
+
+ if (!write_bitmapinfoheader(ig, image, error, 8, 256)) {
+ return 0;
+ }
+
+ if (!write_palette(ig, image, error))
+ return 0;
+
+ for (y = image->height-1; y >= 0; --y) {
+ if (i_io_write(ig, data + y * image->width,
+ image->width) != image->width) {
+ *error = ICOERR_Write_Failure;
+ return 0;
+ }
+ if (zero_count) {
+ if (i_io_write(ig, zeros, zero_count) != zero_count) {
+ *error = ICOERR_Write_Failure;
+ return 0;
+ }
+ }
+ }
+
+ return 1;
+}
+
+/*
+=item write_4_bit
+
+Write 4 bit image data.
+
+=cut
+*/
+
+static int
+write_4_bit(i_io_glue_t *ig, ico_image_t const *image, int *error) {
+ int line_size = ((image->width + 1) / 2 + 3) / 4 * 4;
+ unsigned char *writebuf, *outp;
+ int x, y;
+ unsigned char const *data = image->image_data;
+ unsigned char const *pixelp;
+
+ if (!write_bitmapinfoheader(ig, image, error, 4, 16)) {
+ return 0;
+ }
+
+ if (!write_palette(ig, image, error))
+ return 0;
+
+ writebuf = malloc(line_size);
+ if (!writebuf) {
+ *error = ICOERR_Out_Of_Memory;
+ return 0;
+ }
+
+ for (y = image->height-1; y >= 0; --y) {
+ pixelp = data + y * image->width;
+ outp = writebuf;
+ memset(writebuf, 0, line_size);
+ for (x = 0; x < image->width; ++x) {
+ if (x & 1) {
+ *outp |= *pixelp++ & 0x0F;
+ ++outp;
+ }
+ else {
+ *outp |= *pixelp++ << 4;
+ }
+ }
+
+ if (i_io_write(ig, writebuf, line_size) != line_size) {
+ *error = ICOERR_Write_Failure;
+ free(writebuf);
+ return 0;
+ }
+ }
+
+ free(writebuf);
+
+ return 1;
+}
+
+/*
+=item write_1_bit
+
+Write 1 bit image data.
+
+=cut
+*/
+
+static int
+write_1_bit(i_io_glue_t *ig, ico_image_t const *image, int *error) {
+ int line_size = (image->width + 31) / 32 * 4;
+ unsigned char *writebuf = malloc(line_size);
+ unsigned char *outp;
+ unsigned char const *data, *pixelp;
+ int x,y;
+ unsigned mask;
+
+ if (!write_bitmapinfoheader(ig, image, error, 1, 2)) {
+ return 0;
+ }
+
+ if (!write_palette(ig, image, error))
+ return 0;
+
+ if (!writebuf) {
+ *error = ICOERR_Out_Of_Memory;
+ return 0;
+ }
+
+ data = image->image_data;
+ for (y = image->height-1; y >= 0; --y) {
+ memset(writebuf, 0, line_size);
+ pixelp = data + y * image->width;
+ outp = writebuf;
+ mask = 0x80;
+ for (x = 0; x < image->width; ++x) {
+ if (*pixelp)
+ *outp |= mask;
+ mask >>= 1;
+ if (!mask) {
+ mask = 0x80;
+ outp++;
+ }
+ }
+ if (i_io_write(ig, writebuf, line_size) != line_size) {
+ *error = ICOERR_Write_Failure;
+ free(writebuf);
+ return 0;
+ }
+ }
+
+ free(writebuf);
+
+ return 1;
+}
+
+/*
+=item write_mask
+
+Write the AND mask.
+
+=cut
+*/
+
+static int
+write_mask(i_io_glue_t *ig, ico_image_t const *image, int *error) {
+ int line_size = (image->width + 31) / 32 * 4;
+ unsigned char *writebuf = malloc(line_size);
+ unsigned char *outp;
+ unsigned char const *data, *pixelp;
+ int x,y;
+ unsigned mask;
+
+ if (!writebuf) {
+ *error = ICOERR_Out_Of_Memory;
+ return 0;
+ }
+
+ data = image->mask_data;
+ if (data) {
+ for (y = image->height-1; y >= 0; --y) {
+ memset(writebuf, 0, line_size);
+ pixelp = data + y * image->width;
+ outp = writebuf;
+ mask = 0x80;
+ for (x = 0; x < image->width; ++x) {
+ if (*pixelp)
+ *outp |= mask;
+ mask >>= 1;
+ if (!mask) {
+ mask = 0x80;
+ outp++;
+ }
+ ++pixelp;
+ }
+ if (i_io_write(ig, writebuf, line_size) != line_size) {
+ *error = ICOERR_Write_Failure;
+ free(writebuf);
+ return 0;
+ }
+ }
+ }
+ else {
+ memset(writebuf, 0, line_size);
+ for (y = image->height-1; y >= 0; --y) {
+ if (i_io_write(ig, writebuf, line_size) != line_size) {
+ *error = ICOERR_Write_Failure;
+ free(writebuf);
+ return 0;
+ }
+ }
+ }
+
+ free(writebuf);
+
+ return 1;
+}
+
+/*
=back
=head1 AUTHOR
View
10 ICO/msicon.h
@@ -21,6 +21,7 @@ typedef struct {
int palette_size;
ico_color_t *palette;
unsigned char *mask_data;
+ int hotspot_x, hotspot_y;
} ico_image_t;
extern ico_reader_t *ico_reader_open(i_io_glue_t *ig, int *error);
@@ -30,17 +31,26 @@ extern ico_image_t *ico_image_read(ico_reader_t *file, int index, int *error);
extern void ico_image_release(ico_image_t *image);
extern void ico_reader_close(ico_reader_t *file);
+extern int ico_write(i_io_glue_t *ig, ico_image_t const *images,
+ int image_count, int type, int *error);
+
extern size_t ico_error_message(int error, char *buffer, size_t buffer_size);
#define ICO_MAX_MESSAGE 80
#define ICOERR_Short_File 100
#define ICOERR_File_Error 101
+#define ICOERR_Write_Failure 102
#define ICOERR_Invalid_File 200
#define ICOERR_Unknown_Bits 201
#define ICOERR_Bad_Image_Index 300
+#define ICOERR_Bad_File_Type 301
+#define ICOERR_Invalid_Width 302
+#define ICOERR_Invalid_Height 303
+#define ICOERR_Invalid_Palette 304
+#define ICOERR_No_Data 305
#define ICOERR_Out_Of_Memory 400
View
176 ICO/t/t10icon.t
@@ -1,12 +1,14 @@
#!perl -w
use strict;
-use Test::More tests => 60;
+use Test::More tests => 94;
BEGIN { use_ok('Imager::File::ICO'); }
-d 'testout' or mkdir 'testout';
my $im = Imager->new;
+# type=>'ico' or 'cur' and read ico and cur since they're pretty much
+# the same
ok($im->read(file => "testimg/rgba3232.ico", type=>"ico"),
"read 32 bit")
or print "# ", $im->errstr, "\n";
@@ -15,7 +17,6 @@ is($im->getwidth, 32, "check height");
is($im->type, 'direct', "check type");
is($im->tags(name => 'ico_bits'), 32, "check ico_bits tag");
is($im->tags(name => 'i_format'), 'ico', "check i_format tag");
-is($im->tags(name => 'ico_type'), 'icon', "check ico_type tag");
my $mask = '.*
..........................******
..........................******
@@ -72,7 +73,6 @@ is($im->type, 'paletted', "check type");
is($im->colorcount, 256, "color count");
is($im->tags(name => 'ico_bits'), 8, "check ico_bits tag");
is($im->tags(name => 'i_format'), 'ico', "check i_format tag");
-is($im->tags(name => 'ico_type'), 'icon', "check ico_type tag");
SKIP:
{
my $comp = Imager->new;
@@ -92,7 +92,6 @@ is($im->type, 'paletted', "check type");
is($im->colorcount, 16, "color count");
is($im->tags(name => 'ico_bits'), 4, "check ico_bits tag");
is($im->tags(name => 'i_format'), 'ico', "check i_format tag");
-is($im->tags(name => 'ico_type'), 'icon', "check ico_type tag");
SKIP:
{
my $comp = Imager->new;
@@ -110,9 +109,8 @@ is($im->getwidth, 32, "check width");
is($im->getwidth, 32, "check height");
is($im->type, 'paletted', "check type");
is($im->colorcount, 2, "color count");
-is($im->tags(name => 'ico_bits'), 1, "check ico_bits tag");
-is($im->tags(name => 'i_format'), 'ico', "check i_format tag");
-is($im->tags(name => 'ico_type'), 'cursor', "check ico_type tag");
+is($im->tags(name => 'cur_bits'), 1, "check ico_bits tag");
+is($im->tags(name => 'i_format'), 'cur', "check i_format tag");
$im->write(file=>'testout/pal13232.ppm');
# combo was created with the GIMP, which has a decent mechanism for selecting
@@ -159,3 +157,167 @@ is_deeply([ $imgs[1]->getpixel(x=>31, 'y'=>31)->rgba ], [ 17, 231, 177, 255 ],
"check image data 1(31,31)");
is_deeply([ $imgs[2]->getpixel(x=>15, 'y'=>15)->rgba ], [ 17, 231, 177, 255 ],
"check image data 2(15,15)");
+
+$im = Imager->new(xsize=>32, ysize=>32);
+$im->box(filled=>1, color=>'FF0000');
+$im->box(filled=>1, color=>'0000FF', xmin => 6, ymin=>0, xmax => 21, ymax=>15);
+$im->box(filled=>1, color=>'00FF00', xmin => 10, ymin=>16, xmax => 25, ymax=>31);
+
+ok($im->write(file=>'testout/t10_32.ico', type=>'ico'),
+ "write 32-bit icon");
+
+my $im2 = Imager->new;
+ok($im2->read(file=>'testout/t10_32.ico', type=>'ico'),
+ "read it back in");
+
+is(Imager::i_img_diff($im->{IMG}, $im2->{IMG}), 0,
+ "check they're the same");
+is($im->bits, $im2->bits, "check same bits");
+
+{
+ my $im = Imager->new(xsize => 32, ysize => 32);
+ $im->box(filled=>1, color=>'#FF00FF');
+ my $data;
+ ok(Imager->write_multi({ data => \$data, type=>'ico' }, $im, $im),
+ "write multi icons");
+ ok(length $data, "and it wrote data");
+ my @im = Imager->read_multi(data => $data);
+ is(@im, 2, "got all the images back");
+ is(Imager::i_img_diff($im->{IMG}, $im[0]{IMG}), 0, "check first image");
+ is(Imager::i_img_diff($im->{IMG}, $im[1]{IMG}), 0, "check second image");
+}
+
+{ # 1 channel image
+ my $im = Imager->new(xsize => 32, ysize => 32, channels => 1);
+ $im->box(filled=>1, color => [ 128, 0, 0 ]);
+ my $data;
+ ok($im->write(data => \$data, type=>'ico'), "write 1 channel image");
+ my $im2 = Imager->new;
+ ok($im2->read(data => $data), "read it back");
+ is($im2->getchannels, 4, "check channels");
+ my $imrgb = $im->convert(preset => 'rgb')
+ ->convert(preset => 'addalpha');
+ is(Imager::i_img_diff($imrgb->{IMG}, $im2->{IMG}), 0,
+ "check image matches expected");
+}
+
+{ # 2 channel image
+ my $base = Imager->new(xsize => 32, ysize => 32, channels => 2);
+ $base->box(filled => 1, color => [ 64, 192, 0 ]);
+ my $data;
+ ok($base->write(data => \$data, type=>'ico'), "write 2 channel image");
+ my $read = Imager->new;
+ ok($read->read(data => $data), "read it back");
+ is($read->getchannels, 4, "check channels");
+ my $imrgb = $base->convert(preset => 'rgb');
+ is(Imager::i_img_diff($imrgb->{IMG}, $read->{IMG}), 0,
+ "check image matches expected");
+}
+
+{ # 4 channel image
+ my $base = Imager->new(xsize => 32, ysize => 32, channels => 4);
+ $base->box(filled=>1, ymax => 15, color => [ 255, 0, 255, 128 ]);
+ $base->box(filled=>1, ymin => 16, color => [ 0, 255, 255, 255 ]);
+ my $data;
+ ok($base->write(data => \$data, type=>'ico'), "write 4 channel image");
+ my $read = Imager->new;
+ ok($read->read(data => $data, type=>'ico'), "read it back")
+ or print "# ", $read->errstr, "\n";
+ is(Imager::i_img_diff($base->{IMG}, $read->{IMG}), 0,
+ "check image matches expected");
+}
+
+{ # mask handling
+ my $base = Imager->new(xsize => 16, ysize => 16, channels => 3);
+ $base->box(filled=>1, xmin => 5, xmax => 10, color => '#0000FF');
+ $base->box(filled=>1, ymin => 5, ymax => 10, color => '#0000FF');
+ my $mask = <<EOS; # CR in this to test it's skipped correctly
+01
+0000011111100000
+00000111111 00000xx
+00000111111000
+00000111111000
+0000011111100000
+1111111111111111
+1111111111111111
+1111111111111111
+1111111111111111
+1111111111111111
+1111111111111111
+1010101010101010
+1010101010101010
+1010101010101010
+1010101010101010
+1010101010101010
+EOS
+ $mask =~ s/\n/\r\n/g; # to test alternate newline handling is correct
+ $base->settag(name => 'ico_mask', value => $mask);
+ my $saved_mask = $base->tags(name => 'ico_mask');
+ my $data;
+ ok($base->write(data => \$data, type => 'ico'),
+ "write with mask tag set");
+ my $read = Imager->new;
+ ok($read->read(data => $data), "read it back");
+ my $mask2 = $mask;
+ $mask2 =~ tr/01/.*/;
+ $mask2 =~ s/\n$//;
+ $mask2 =~ tr/\r x//d;
+ $mask2 =~ s/^(.{3,19})$/$1 . "." x (16 - length $1)/gem;
+ my $read_mask = $read->tags(name => 'ico_mask');
+ is($read_mask, $mask2, "check mask is correct");
+}
+
+{ # mask too short to handle
+ my $mask = "xx";
+ my $base = Imager->new(xsize => 16, ysize => 16, channels => 3);
+ $base->box(filled=>1, xmin => 5, xmax => 10, color => '#0000FF');
+ $base->box(filled=>1, ymin => 5, ymax => 10, color => '#0000FF');
+ $base->settag(name => 'ico_mask', value => $mask);
+ my $data;
+ ok($base->write(data => \$data, type=>'ico'),
+ "save icon with short mask tag");
+ my $read = Imager->new;
+ ok($read->read(data => $data), "read it back");
+ my $read_mask = $read->tags(name => 'ico_mask');
+ my $expected_mask = ".*" . ( "\n" . "." x 16 ) x 16;
+ is($read_mask, $expected_mask, "check the mask");
+
+ # mask that doesn't match what we expect
+ $base->settag(name => 'ico_mask', value => 'abcd');
+ ok($base->write(data => \$data, type => 'ico'),
+ "write with bad format mask tag");
+ ok($read->read(data => $data), "read it back");
+ $read_mask = $read->tags(name => 'ico_mask');
+ is($read_mask, $expected_mask, "check the mask");
+
+ # mask with invalid char
+ $base->settag(name => 'ico_mask', value => ".*\n....xxx..");
+ ok($base->write(data => \$data, type => 'ico'),
+ "write with unexpected chars in mask");
+ ok($read->read(data => $data), "read it back");
+ $read_mask = $read->tags(name => 'ico_mask');
+ is($read_mask, $expected_mask, "check the mask");
+}
+
+{ # check handling of greyscale paletted
+ my $base = Imager->new(xsize => 16, ysize => 16, channels => 1,
+ type => 'paletted');
+ my @grays = map Imager::Color->new($_),
+ "000000", "666666", "CCCCCC", "FFFFFF";
+ ok($base->addcolors(colors => \@grays), "add some colors");
+ $base->box(filled => 1, color => $grays[1], xmax => 7, ymax => 7);
+ $base->box(filled => 1, color => $grays[1], xmax => 7, ymin => 8);
+ $base->box(filled => 1, color => $grays[1], xmin => 8, ymax => 7);
+ $base->box(filled => 1, color => $grays[1], xmin => 8, ymax => 8);
+ my $data;
+ ok($base->write(data => \$data, type => 'ico'),
+ "write grayscale paletted");
+ my $read = Imager->new;
+ ok($read->read(data => $data), "read it back")
+ or print "# ", $read->errstr, "\n";
+ is($read->type, 'paletted', "check type");
+ is($read->getchannels, 3, "check channels");
+ my $as_rgb = $base->convert(preset => 'rgb');
+ is(Imager::i_img_diff($base->{IMG}, $read->{IMG}), 0,
+ "check the image");
+}
View
73 ICO/t/t30cursor.t
@@ -0,0 +1,73 @@
+#!perl -w
+use strict;
+use Test::More tests => 25;
+
+BEGIN { use_ok('Imager::File::CUR'); }
+
+-d 'testout' or mkdir 'testout';
+
+my $im = Imager->new;
+
+ok($im->read(file => 'testimg/pal43232.cur', type=>'cur'),
+ "read 4 bit");
+is($im->getwidth, 32, "check width");
+is($im->getheight, 32, "check width");
+is($im->type, 'paletted', "check type");
+is($im->tags(name => 'cur_bits'), 4, "check cur_bits tag");
+is($im->tags(name => 'i_format'), 'cur', "check i_format tag");
+is($im->tags(name => 'cur_hotspotx'), 1, "check cur_hotspotx tag");
+is($im->tags(name => 'cur_hotspoty'), 18, "check cur_hotspoty tag");
+my $mask = ".*" . ("\n" . "." x 32) x 32;
+is($im->tags(name => 'cur_mask'), $mask, "check cur_mask tag");
+
+# these should get pushed back into range on saving
+$im->settag(name => 'cur_hotspotx', value => 32);
+$im->settag(name => 'cur_hotspoty', value => -1);
+ok($im->write(file=>'testout/hotspot.cur', type=>'cur'),
+ "save with oor hotspot")
+ or print "# ",$im->errstr, "\n";
+{
+ my $im2 = Imager->new;
+ ok($im2->read(file=>'testout/hotspot.cur', type=>'cur'),
+ "re-read the hotspot set cursor")
+ or print "# ", $im->errstr, "\n";
+ is($im2->tags(name => 'cur_hotspotx'), 31, "check cur_hotspotx tag");
+ is($im2->tags(name => 'cur_hotspoty'), 0, "check cur_hotspoty tag");
+}
+
+$im->settag(name => 'cur_hotspotx', value => -1);
+$im->settag(name => 'cur_hotspoty', value => 32);
+ok($im->write(file=>'testout/hotspot2.cur', type=>'cur'),
+ "save with oor hotspot")
+ or print "# ",$im->errstr, "\n";
+
+{
+ my $im2 = Imager->new;
+ ok($im2->read(file=>'testout/hotspot2.cur', type=>'cur'),
+ "re-read the hotspot set cursor")
+ or print "# ", $im->errstr, "\n";
+ is($im2->tags(name => 'cur_hotspotx'), 0, "check cur_hotspotx tag");
+ is($im2->tags(name => 'cur_hotspoty'), 31, "check cur_hotspoty tag");
+}
+
+{
+ my $data = '';
+ ok($im->write(data => \$data, type => 'cur'),
+ "write single to data");
+ print "# ", length $data, " bytes written\n";
+ my $im2 = Imager->new;
+ ok($im2->read(data => $data), "read back in");
+ is(Imager::i_img_diff($im->{IMG}, $im2->{IMG}), 0, "check image");
+}
+
+{
+ my $data = '';
+ ok(Imager->write_multi({ type => 'cur', data => \$data }, $im, $im),
+ "write multiple images");
+ print "# ", length $data, " bytes written\n";
+ my @im = Imager->read_multi(type => 'cur', data => $data)
+ or print "# ", Imager->errstr, "\n";
+ is(@im, 2, "read them back in");
+ is(Imager::i_img_diff($im->{IMG}, $im[0]{IMG}), 0, "check first image");
+ is(Imager::i_img_diff($im->{IMG}, $im[1]{IMG}), 0, "check second image");
+}
View
10 ICO/t/t40readcurone.t
@@ -0,0 +1,10 @@
+#!perl -w
+use strict;
+use Test::More tests => 1;
+use Imager;
+
+# checks that we load the CUR handler automatically
+my $im = Imager->new;
+ok($im->read(file => 'testimg/pal43232.cur'),
+ "check that cursor reader loaded correctly for singles")
+ or print "# ", $im->errstr, "\n";
View
10 ICO/t/t41curmultread.t
@@ -0,0 +1,10 @@
+#!perl -w
+use strict;
+use Test::More tests => 1;
+use Imager;
+
+# checks that we load the CUR handler automatically for multiple image reads
+my @im = Imager->read_multi(file=>'testimg/pal43232.cur');
+is(scalar(@im), 1,
+ "check that cursor reader loaded correctly for singles")
+ or print "# ", Imager->errstr, "\n";
View
337 ICO/t/t50readfail.t
@@ -0,0 +1,337 @@
+#!perl -w
+use strict;
+use Imager;
+use Test::More tests => 40;
+
+sub get_data;
+
+{ # test file limits are obeyed (paletted)
+ Imager->set_file_limits(reset => 1, width => 10);
+ my $im = Imager->new;
+ ok(!$im->read(file => 'testimg/pal13232.ico'), "can't read overwide image");
+ like($im->errstr, qr/image width/, "check message");
+}
+
+{ # test file limits are obeyed (direct)
+ Imager->set_file_limits(reset => 1, width => 10);
+ my $im = Imager->new;
+ ok(!$im->read(file => 'testimg/rgba3232.ico'), "can't read overwide image");
+ like($im->errstr, qr/image width/, "check message");
+}
+
+Imager->set_file_limits(reset => 1);
+
+{ # file too short for magic
+ my $im = Imager->new;
+ ok(!$im->read(data=>"XXXX", type=>'ico'), "Can't read short image file");
+ is($im->errstr, "error opening ICO/CUR file: Short read",
+ "check error message");
+}
+
+{ # read non-icon
+ my $im = Imager->new;
+ ok(!$im->read(file=>'t/t50readfail.t', type=>'ico'),
+ "script isn't an icon");
+ is($im->errstr, "error opening ICO/CUR file: Not an icon file",
+ "check message");
+}
+
+{ # file with not enough icon structures
+ my $im = Imager->new;
+ my $data = pack "H*", "00000100010000";
+ ok(!$im->read(data => $data, type=>'ico'),
+ "ico file broken at resource entries");
+ is($im->errstr, "error opening ICO/CUR file: Short read",
+ "check error message");
+}
+{
+ my $im = Imager->new;
+ my $data = pack "H*", "00000200010000";
+ ok(!$im->read(data => $data, type=>'cur'),
+ "cursor file broken at resource entries");
+ is($im->errstr, "error opening ICO/CUR file: Short read",
+ "check error message");
+}
+
+{ # read negative index image
+ my $im = Imager->new;
+ ok(!$im->read(file=>'testimg/pal13232.ico', type=>'ico', page=>-1),
+ "read page -1");
+ is($im->errstr, "error reading ICO/CUR image: Image index out of range",
+ "check error message");
+}
+
+{ # read too high image index
+ my $im = Imager->new;
+ ok(!$im->read(file=>'testimg/pal13232.ico', type=>'ico', page=>1),
+ "read page 1");
+ is($im->errstr, "error reading ICO/CUR image: Image index out of range",
+ "check error message");
+}
+
+{ # image offset beyond end of file
+ my $im = Imager->new;
+ my $data = get_data <<EOS;
+; header - icon with 1 image
+0000 0100 0100
+; image record 32 x 32, offset 0xFFFF
+20 20 00 00 0000 0000 00200000 FFFF0000
+EOS
+ ok(!$im->read(data => $data, type=>'ico'),
+ "read from icon with bad offset");
+ # bad offset causes the seek to fail on an in-memory "file"
+ # it may not fail this way on a real file.
+ is($im->errstr, "error reading ICO/CUR image: I/O error",
+ "check error message");
+}
+
+{ # short read on bmiheader
+ my $im = Imager->new;
+ my $data = get_data <<EOS;
+; header - icon with 1 image
+0000 0100 0100
+; image record 32 x 32, offset 0xFFFF
+20 20 00 00 0000 0000 00200000 16000000
+; bmiheader for the first image
+2800 0000 2000 0000 4000 0000 ; size, width, height
+; short here
+EOS
+ ok(!$im->read(data => $data, type=>'ico'),
+ "read from icon with a short bitmap header");
+ is($im->errstr, "error reading ICO/CUR image: Short read",
+ "check error message");
+}
+
+{ # invalid bmiheader
+ my $im = Imager->new;
+ my $data = get_data <<EOS;
+; header - icon with 1 image
+0000 0100 0100
+; image record 32 x 32, offset 0xFFFF
+20 20 00 00 0000 0000 00200000 16000000
+; bmiheader for the first image
+2000 0000 2000 0000 4000 0000 ; size should be 0x28, width, height
+0100 2000 ; planes, bit count
+; data we read but ignore
+0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
+EOS
+ ok(!$im->read(data => $data, type=>'ico'),
+ "read from icon with an invalid sub-image header");
+ is($im->errstr, "error reading ICO/CUR image: Not an icon file",
+ "check error message");
+}
+
+{ # invalid bit count for "direct" image
+ my $im = Imager->new;
+ my $data = get_data <<EOS;
+; header - icon with 1 image
+0000 0100 0100
+; image record 32 x 32, offset 0xFFFF
+20 20 00 00 0000 0000 00200000 16000000
+; bmiheader for the first image
+2800 0000 2000 0000 4000 0000 ; size, width, height
+0100 2100 ; planes, bit count
+; data we read but ignore
+0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
+EOS
+ ok(!$im->read(data => $data, type=>'ico'),
+ "read from icon with an invalid 'direct' bits per pixel");
+ is($im->errstr, "error reading ICO/CUR image: Unknown value for bits/pixel",
+ "check error message");
+}
+
+{ # short file reading palette
+ my $im = Imager->new;
+ my $data = get_data <<EOS;
+; header - icon with 1 image
+0000 0100 0100
+; image record 32 x 32, offset 0xFFFF
+20 20 00 00 0000 0000 00200000 16000000
+; bmiheader for the first image
+2800 0000 2000 0000 4000 0000 ; size, width, height
+0100 0100 ; planes, bit count == 1
+; data we read but ignore
+0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
+; dummy palette - one color but 2 needed
+FFFFFF00
+EOS
+ ok(!$im->read(data => $data, type=>'ico'),
+ "read from icon with short palette");
+ is($im->errstr, "error reading ICO/CUR image: Short read",
+ "check error message");
+}
+
+{ # short file reading 1 bit image data
+ my $im = Imager->new;
+ my $data = get_data <<EOS;
+; header - icon with 1 image
+0000 0100 0100
+; image record 32 x 32, offset 0x20
+20 20 00 00 0000 0000 00200000 16000000
+; bmiheader for the first image
+2800 0000 2000 0000 4000 0000 ; size, width, height
+0100 0100 ; planes, bit count == 1
+; data we read but ignore
+0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
+; palette
+00000000
+FFFFFF00
+; image data - short
+00 ff
+EOS
+ ok(!$im->read(data => $data, type=>'ico'),
+ "read from icon with short image data (1 bit)");
+ is($im->errstr, "error reading ICO/CUR image: Short read",
+ "check error message");
+}
+
+{ # short file reading 32 bit image data
+ my $im = Imager->new;
+ my $data = get_data <<EOS;
+; header - icon with 1 image
+0000 0100 0100
+; image record 32 x 32, offset 0x20
+20 20 00 00 0000 0000 00200000 16000000
+; bmiheader for the first image
+2800 0000 2000 0000 4000 0000 ; size, width, height
+0100 2000 ; planes, bit count == 32
+; data we read but ignore
+0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
+; nopalette
+; image data - short
+FFFFFFFF
+EOS
+ ok(!$im->read(data => $data, type=>'ico'),
+ "read from icon with short image data (32 bit)");
+ is($im->errstr, "error reading ICO/CUR image: Short read",
+ "check error message");
+}
+
+{ # short file reading 4 bit image data
+ my $im = Imager->new;
+ my $data = get_data <<EOS;
+; header - icon with 1 image
+0000 0100 0100
+; image record 32 x 32, offset 0x20
+20 20 00 00 0000 0000 00200000 16000000
+; bmiheader for the first image
+2800 0000 2000 0000 4000 0000 ; size, width, height
+0100 0400 ; planes, bit count == 4
+; data we read but ignore
+0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
+; 16-color palette
+00000000 FFFFFF00 00000000 FFFFFF00
+00000000 FFFFFF00 00000000 FFFFFF00
+00000000 FFFFFF00 00000000 FFFFFF00
+00000000 FFFFFF00 00000000 FFFFFF00
+; image data - short
+FFFFFFFF
+EOS
+ ok(!$im->read(data => $data, type=>'ico'),