]> git.imager.perl.org - imager.git/blobdiff - ICO/msicon.c
remove a pointless NULL check from i_img_destroy()
[imager.git] / ICO / msicon.c
index b59b5b3d4ad62824798e3b9838fb336520a44ecd..327f306bb391e934a39f3eae3914ff6a1c52b239 100644 (file)
@@ -1,3 +1,4 @@
+#include "imext.h"
 #include "msicon.h"
 #include <string.h>
 #include <stdlib.h>
@@ -181,8 +182,9 @@ ico_reader_open(i_io_glue_t *ig, int *error) {
       image->hotspot_y = hotspot_y;
     }
 
-    image->width = width;
-    image->height = height;
+    /* a width or height of zero here indicates a width/height of 256 */
+    image->width = width ? width : 256;
+    image->height = height ? height : 256;
     image->offset = image_offset;
     image->size = bytes_in_res;
   }
@@ -481,9 +483,11 @@ ico_write(i_io_glue_t *ig, ico_image_t const *images, int image_count,
     ico_image_t const *image = images + i;
     int bits, colors;
     int size = ico_image_size(image, &bits, &colors);
+    int width_byte = image->width == 256 ? 0 : image->width;
+    int height_byte = image->height == 256 ? 0 : image->height;
 
     if (type == ICON_ICON) {
-      if (!write_packed(ig, "bbbbwwdd", image->width, image->height,
+      if (!write_packed(ig, "bbbbwwdd", width_byte, height_byte,
                        colors, 0, 1, bits, (unsigned long)size, 
                        (unsigned long)current_offset)) {
        *error = ICOERR_Write_Failure;
@@ -503,7 +507,7 @@ ico_write(i_io_glue_t *ig, ico_image_t const *images, int image_count,
       else if (hotspot_y >= image->height)
        hotspot_y = image->height - 1;
 
-      if (!write_packed(ig, "bbbbwwdd", image->width, image->height,
+      if (!write_packed(ig, "bbbbwwdd", width_byte, height_byte,
                        colors, 0, hotspot_x, hotspot_y, (unsigned long)size, 
                        (unsigned long)current_offset)) {
        *error = ICOERR_Write_Failure;
@@ -692,7 +696,7 @@ int read_packed(io_glue *ig, const char *format, ...) {
 
     case 'd':
       p = va_arg(ap, long *);
-      *p = bufp[0] + (bufp[1] << 8) + (bufp[2] << 16) + (bufp[3] << 24);
+      *p = bufp[0] + (bufp[1] << 8) + (bufp[2] << 16) + ((unsigned long)bufp[3] << 24);
       bufp += 4;
       break;
 
@@ -1043,11 +1047,11 @@ ico_write_validate(ico_image_t const *images, int image_count, int *error) {
   for (i = 0; i < image_count; ++i) {
     ico_image_t const *image = images + i;
 
-    if (image->width < 1 || image->width > 255) {
+    if (image->width < 1 || image->width > 256) {
       *error = ICOERR_Invalid_Width;
       return 0;
     }
-    if (image->height < 1 || image->height > 255) {
+    if (image->height < 1 || image->height > 256) {
       *error = ICOERR_Invalid_Height;
       return 0;
     }
@@ -1421,7 +1425,7 @@ Write 1 bit image data.
 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 *writebuf;
   unsigned char *outp;
   unsigned char const *data, *pixelp;
   int x,y;
@@ -1434,6 +1438,7 @@ write_1_bit(i_io_glue_t *ig, ico_image_t const *image, int *error) {
   if (!write_palette(ig, image, error))
     return 0;
 
+  writebuf = malloc(line_size);
   if (!writebuf) {
     *error = ICOERR_Out_Of_Memory;
     return 0;