cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % IIIII CCCC OOO N N % |
| 7 | % I C O O NN N % |
| 8 | % I C O O N N N % |
| 9 | % I C O O N NN % |
| 10 | % IIIII CCCC OOO N N % |
| 11 | % % |
| 12 | % % |
| 13 | % Read Microsoft Windows Icon Format % |
| 14 | % % |
| 15 | % Software Design % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
Cristy | f6ff9ea | 2016-12-05 09:53:35 -0500 | [diff] [blame] | 20 | % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
Cristy | f19d414 | 2017-04-24 11:34:30 -0400 | [diff] [blame] | 26 | % https://www.imagemagick.org/script/license.php % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
glennrp | aa192b1 | 2012-01-17 21:35:21 +0000 | [diff] [blame] | 43 | #include "MagickCore/artifact.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 44 | #include "MagickCore/blob.h" |
| 45 | #include "MagickCore/blob-private.h" |
| 46 | #include "MagickCore/cache.h" |
| 47 | #include "MagickCore/colormap.h" |
| 48 | #include "MagickCore/colorspace.h" |
cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 49 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 50 | #include "MagickCore/exception.h" |
| 51 | #include "MagickCore/exception-private.h" |
| 52 | #include "MagickCore/image.h" |
| 53 | #include "MagickCore/image-private.h" |
| 54 | #include "MagickCore/list.h" |
| 55 | #include "MagickCore/log.h" |
| 56 | #include "MagickCore/magick.h" |
| 57 | #include "MagickCore/memory_.h" |
| 58 | #include "MagickCore/monitor.h" |
| 59 | #include "MagickCore/monitor-private.h" |
cristy | 2c5fc27 | 2012-02-22 01:27:46 +0000 | [diff] [blame] | 60 | #include "MagickCore/nt-base-private.h" |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 61 | #include "MagickCore/option.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 62 | #include "MagickCore/pixel-accessor.h" |
| 63 | #include "MagickCore/quantize.h" |
| 64 | #include "MagickCore/quantum-private.h" |
| 65 | #include "MagickCore/static.h" |
| 66 | #include "MagickCore/string_.h" |
| 67 | #include "MagickCore/module.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | Define declarations. |
| 71 | */ |
cristy | 07a3cca | 2012-12-10 13:09:10 +0000 | [diff] [blame] | 72 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__) || defined(__MINGW64__) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 73 | #define BI_RGB 0 |
| 74 | #define BI_RLE8 1 |
| 75 | #define BI_BITFIELDS 3 |
| 76 | #endif |
| 77 | #define MaxIcons 1024 |
| 78 | |
| 79 | /* |
| 80 | Typedef declarations. |
| 81 | */ |
| 82 | typedef struct _IconEntry |
| 83 | { |
| 84 | unsigned char |
| 85 | width, |
| 86 | height, |
| 87 | colors, |
| 88 | reserved; |
| 89 | |
| 90 | unsigned short int |
| 91 | planes, |
| 92 | bits_per_pixel; |
| 93 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 94 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 95 | size, |
| 96 | offset; |
| 97 | } IconEntry; |
| 98 | |
| 99 | typedef struct _IconFile |
| 100 | { |
| 101 | short |
| 102 | reserved, |
| 103 | resource_type, |
| 104 | count; |
| 105 | |
| 106 | IconEntry |
| 107 | directory[MaxIcons]; |
| 108 | } IconFile; |
| 109 | |
| 110 | typedef struct _IconInfo |
| 111 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 112 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 113 | file_size, |
| 114 | ba_offset, |
| 115 | offset_bits, |
| 116 | size; |
| 117 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 118 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 119 | width, |
| 120 | height; |
| 121 | |
| 122 | unsigned short |
| 123 | planes, |
| 124 | bits_per_pixel; |
| 125 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 126 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 127 | compression, |
| 128 | image_size, |
| 129 | x_pixels, |
| 130 | y_pixels, |
| 131 | number_colors, |
| 132 | red_mask, |
| 133 | green_mask, |
| 134 | blue_mask, |
| 135 | alpha_mask, |
| 136 | colors_important; |
| 137 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 138 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 139 | colorspace; |
| 140 | } IconInfo; |
| 141 | |
| 142 | /* |
| 143 | Forward declaractions. |
| 144 | */ |
dirk | 501a11e | 2014-01-19 16:22:18 +0000 | [diff] [blame] | 145 | static Image |
| 146 | *AutoResizeImage(const Image *,const char *,MagickOffsetType *, |
| 147 | ExceptionInfo *); |
| 148 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 149 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 150 | WriteICONImage(const ImageInfo *,Image *,ExceptionInfo *); |
dirk | 501a11e | 2014-01-19 16:22:18 +0000 | [diff] [blame] | 151 | |
| 152 | Image *AutoResizeImage(const Image *image,const char *option, |
| 153 | MagickOffsetType *count,ExceptionInfo *exception) |
| 154 | { |
| 155 | #define MAX_SIZES 16 |
| 156 | |
| 157 | char |
| 158 | *q; |
| 159 | |
| 160 | const char |
| 161 | *p; |
| 162 | |
| 163 | Image |
| 164 | *resized, |
| 165 | *images; |
| 166 | |
| 167 | register ssize_t |
| 168 | i; |
| 169 | |
| 170 | size_t |
| 171 | sizes[MAX_SIZES]={256,192,128,96,64,48,40,32,24,16}; |
| 172 | |
| 173 | images=NULL; |
| 174 | *count=0; |
| 175 | i=0; |
| 176 | p=option; |
| 177 | while (*p != '\0' && i < MAX_SIZES) |
| 178 | { |
| 179 | size_t |
| 180 | size; |
| 181 | |
| 182 | while ((isspace((int) ((unsigned char) *p)) != 0)) |
| 183 | p++; |
| 184 | |
| 185 | size=(size_t)strtol(p,&q,10); |
cristy | 0f6fc2d | 2015-05-30 00:49:11 +0000 | [diff] [blame] | 186 | if ((p == q) || (size < 16) || (size > 256)) |
| 187 | return((Image *) NULL); |
dirk | 501a11e | 2014-01-19 16:22:18 +0000 | [diff] [blame] | 188 | |
| 189 | p=q; |
| 190 | sizes[i++]=size; |
| 191 | |
| 192 | while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ',')) |
| 193 | p++; |
| 194 | } |
| 195 | |
| 196 | if (i==0) |
| 197 | i=10; |
| 198 | *count=i; |
| 199 | for (i=0; i < *count; i++) |
| 200 | { |
| 201 | resized=ResizeImage(image,sizes[i],sizes[i],image->filter,exception); |
| 202 | if (resized == (Image *) NULL) |
| 203 | return(DestroyImageList(images)); |
| 204 | |
| 205 | if (images == (Image *) NULL) |
| 206 | images=resized; |
| 207 | else |
| 208 | AppendImageToList(&images,resized); |
| 209 | } |
| 210 | return(images); |
| 211 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 215 | % % |
| 216 | % % |
| 217 | % % |
| 218 | % R e a d I C O N I m a g e % |
| 219 | % % |
| 220 | % % |
| 221 | % % |
| 222 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 223 | % |
| 224 | % ReadICONImage() reads a Microsoft icon image file and returns it. It |
| 225 | % allocates the memory necessary for the new Image structure and returns a |
| 226 | % pointer to the new image. |
| 227 | % |
| 228 | % The format of the ReadICONImage method is: |
| 229 | % |
| 230 | % Image *ReadICONImage(const ImageInfo *image_info, |
| 231 | % ExceptionInfo *exception) |
| 232 | % |
| 233 | % A description of each parameter follows: |
| 234 | % |
| 235 | % o image_info: the image info. |
| 236 | % |
| 237 | % o exception: return any errors or warnings in this structure. |
| 238 | % |
| 239 | */ |
| 240 | static Image *ReadICONImage(const ImageInfo *image_info, |
| 241 | ExceptionInfo *exception) |
| 242 | { |
| 243 | IconFile |
| 244 | icon_file; |
| 245 | |
| 246 | IconInfo |
| 247 | icon_info; |
| 248 | |
| 249 | Image |
| 250 | *image; |
| 251 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 252 | MagickBooleanType |
| 253 | status; |
| 254 | |
Cristy | c2c1ece | 2017-04-23 10:39:00 -0400 | [diff] [blame] | 255 | MagickSizeType |
| 256 | extent; |
| 257 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 258 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 259 | i, |
| 260 | x; |
| 261 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 262 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 263 | *q; |
| 264 | |
| 265 | register unsigned char |
| 266 | *p; |
| 267 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 268 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 269 | bit, |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 270 | byte, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 271 | bytes_per_line, |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 272 | one, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 273 | scanline_pad; |
| 274 | |
cristy | ebc891a | 2011-04-24 23:04:16 +0000 | [diff] [blame] | 275 | ssize_t |
| 276 | count, |
| 277 | offset, |
| 278 | y; |
| 279 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 280 | /* |
| 281 | Open image file. |
| 282 | */ |
| 283 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 284 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 285 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image_info->filename); |
| 286 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 287 | assert(exception->signature == MagickCoreSignature); |
cristy | 1f1de18 | 2011-10-05 18:41:34 +0000 | [diff] [blame] | 288 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 289 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 290 | if (status == MagickFalse) |
| 291 | { |
| 292 | image=DestroyImageList(image); |
| 293 | return((Image *) NULL); |
| 294 | } |
| 295 | icon_file.reserved=(short) ReadBlobLSBShort(image); |
| 296 | icon_file.resource_type=(short) ReadBlobLSBShort(image); |
| 297 | icon_file.count=(short) ReadBlobLSBShort(image); |
| 298 | if ((icon_file.reserved != 0) || |
| 299 | ((icon_file.resource_type != 1) && (icon_file.resource_type != 2)) || |
| 300 | (icon_file.count > MaxIcons)) |
| 301 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
Cristy | c2c1ece | 2017-04-23 10:39:00 -0400 | [diff] [blame] | 302 | extent=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 303 | for (i=0; i < icon_file.count; i++) |
| 304 | { |
| 305 | icon_file.directory[i].width=(unsigned char) ReadBlobByte(image); |
| 306 | icon_file.directory[i].height=(unsigned char) ReadBlobByte(image); |
| 307 | icon_file.directory[i].colors=(unsigned char) ReadBlobByte(image); |
| 308 | icon_file.directory[i].reserved=(unsigned char) ReadBlobByte(image); |
| 309 | icon_file.directory[i].planes=(unsigned short) ReadBlobLSBShort(image); |
| 310 | icon_file.directory[i].bits_per_pixel=(unsigned short) |
| 311 | ReadBlobLSBShort(image); |
| 312 | icon_file.directory[i].size=ReadBlobLSBLong(image); |
| 313 | icon_file.directory[i].offset=ReadBlobLSBLong(image); |
cristy | 9c4177b | 2015-01-06 12:53:14 +0000 | [diff] [blame] | 314 | if (EOFBlob(image) != MagickFalse) |
Cristy | c6c70bb | 2016-04-04 16:22:30 -0400 | [diff] [blame] | 315 | break; |
Cristy | c2c1ece | 2017-04-23 10:39:00 -0400 | [diff] [blame] | 316 | extent=MagickMax(extent,icon_file.directory[i].size); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 317 | } |
Cristy | c2c1ece | 2017-04-23 10:39:00 -0400 | [diff] [blame] | 318 | if ((EOFBlob(image) != MagickFalse) || (extent > GetBlobSize(image))) |
Cristy | c6c70bb | 2016-04-04 16:22:30 -0400 | [diff] [blame] | 319 | ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile"); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 320 | one=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 321 | for (i=0; i < icon_file.count; i++) |
| 322 | { |
| 323 | /* |
| 324 | Verify Icon identifier. |
| 325 | */ |
| 326 | offset=(ssize_t) SeekBlob(image,(MagickOffsetType) |
| 327 | icon_file.directory[i].offset,SEEK_SET); |
| 328 | if (offset < 0) |
| 329 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 330 | icon_info.size=ReadBlobLSBLong(image); |
Cristy | 1fe0b87 | 2016-04-23 10:48:31 -0400 | [diff] [blame] | 331 | icon_info.width=(unsigned char) ReadBlobLSBSignedLong(image); |
| 332 | icon_info.height=(unsigned char) (ReadBlobLSBSignedLong(image)/2); |
cristy | 4e09e03 | 2011-10-05 19:42:10 +0000 | [diff] [blame] | 333 | icon_info.planes=ReadBlobLSBShort(image); |
| 334 | icon_info.bits_per_pixel=ReadBlobLSBShort(image); |
cristy | 995156f | 2015-01-05 22:42:52 +0000 | [diff] [blame] | 335 | if (EOFBlob(image) != MagickFalse) |
| 336 | { |
| 337 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 338 | image->filename); |
| 339 | break; |
| 340 | } |
cristy | 4360277 | 2014-01-29 14:07:29 +0000 | [diff] [blame] | 341 | if (((icon_info.planes == 18505) && (icon_info.bits_per_pixel == 21060)) || |
| 342 | (icon_info.size == 0x474e5089)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 343 | { |
| 344 | Image |
| 345 | *icon_image; |
| 346 | |
| 347 | ImageInfo |
| 348 | *read_info; |
| 349 | |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 350 | size_t |
| 351 | length; |
| 352 | |
| 353 | unsigned char |
| 354 | *png; |
| 355 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 356 | /* |
| 357 | Icon image encoded as a compressed PNG image. |
| 358 | */ |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 359 | length=icon_file.directory[i].size; |
dirk | 0ee195d | 2016-04-07 23:27:27 +0200 | [diff] [blame] | 360 | if ((length < 16) || (~length < 16)) |
cristy | 0f6fc2d | 2015-05-30 00:49:11 +0000 | [diff] [blame] | 361 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 4e09e03 | 2011-10-05 19:42:10 +0000 | [diff] [blame] | 362 | png=(unsigned char *) AcquireQuantumMemory(length+16,sizeof(*png)); |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 363 | if (png == (unsigned char *) NULL) |
| 364 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 365 | (void) CopyMagickMemory(png,"\211PNG\r\n\032\n\000\000\000\015",12); |
cristy | 4e09e03 | 2011-10-05 19:42:10 +0000 | [diff] [blame] | 366 | png[12]=(unsigned char) icon_info.planes; |
| 367 | png[13]=(unsigned char) (icon_info.planes >> 8); |
| 368 | png[14]=(unsigned char) icon_info.bits_per_pixel; |
| 369 | png[15]=(unsigned char) (icon_info.bits_per_pixel >> 8); |
| 370 | count=ReadBlob(image,length-16,png+16); |
dirk | 833908d | 2014-01-28 19:26:32 +0000 | [diff] [blame] | 371 | icon_image=(Image *) NULL; |
| 372 | if (count > 0) |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 373 | { |
dirk | 833908d | 2014-01-28 19:26:32 +0000 | [diff] [blame] | 374 | read_info=CloneImageInfo(image_info); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 375 | (void) CopyMagickString(read_info->magick,"PNG",MagickPathExtent); |
dirk | 833908d | 2014-01-28 19:26:32 +0000 | [diff] [blame] | 376 | icon_image=BlobToImage(read_info,png,length+16,exception); |
| 377 | read_info=DestroyImageInfo(read_info); |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 378 | } |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 379 | png=(unsigned char *) RelinquishMagickMemory(png); |
cristy | 47de5a9 | 2011-10-05 01:47:42 +0000 | [diff] [blame] | 380 | if (icon_image == (Image *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 381 | { |
dirk | 833908d | 2014-01-28 19:26:32 +0000 | [diff] [blame] | 382 | if (count != (ssize_t) (length-16)) |
| 383 | ThrowReaderException(CorruptImageError, |
| 384 | "InsufficientImageDataInFile"); |
cristy | 47de5a9 | 2011-10-05 01:47:42 +0000 | [diff] [blame] | 385 | image=DestroyImageList(image); |
| 386 | return((Image *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 387 | } |
cristy | 47de5a9 | 2011-10-05 01:47:42 +0000 | [diff] [blame] | 388 | DestroyBlob(icon_image); |
| 389 | icon_image->blob=ReferenceBlob(image->blob); |
| 390 | ReplaceImageInList(&image,icon_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 391 | } |
| 392 | else |
| 393 | { |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 394 | if (icon_info.bits_per_pixel > 32) |
| 395 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 396 | icon_info.compression=ReadBlobLSBLong(image); |
| 397 | icon_info.image_size=ReadBlobLSBLong(image); |
| 398 | icon_info.x_pixels=ReadBlobLSBLong(image); |
| 399 | icon_info.y_pixels=ReadBlobLSBLong(image); |
| 400 | icon_info.number_colors=ReadBlobLSBLong(image); |
| 401 | icon_info.colors_important=ReadBlobLSBLong(image); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 402 | image->alpha_trait=BlendPixelTrait; |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 403 | image->columns=(size_t) icon_file.directory[i].width; |
| 404 | if ((ssize_t) image->columns > icon_info.width) |
| 405 | image->columns=(size_t) icon_info.width; |
cristy | 4e09e03 | 2011-10-05 19:42:10 +0000 | [diff] [blame] | 406 | if (image->columns == 0) |
| 407 | image->columns=256; |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 408 | image->rows=(size_t) icon_file.directory[i].height; |
| 409 | if ((ssize_t) image->rows > icon_info.height) |
| 410 | image->rows=(size_t) icon_info.height; |
cristy | 4e09e03 | 2011-10-05 19:42:10 +0000 | [diff] [blame] | 411 | if (image->rows == 0) |
| 412 | image->rows=256; |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 413 | image->depth=icon_info.bits_per_pixel; |
| 414 | if (image->debug != MagickFalse) |
| 415 | { |
| 416 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 417 | " scene = %.20g",(double) i); |
| 418 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 419 | " size = %.20g",(double) icon_info.size); |
| 420 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 421 | " width = %.20g",(double) icon_file.directory[i].width); |
| 422 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 423 | " height = %.20g",(double) icon_file.directory[i].height); |
| 424 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 425 | " colors = %.20g",(double ) icon_info.number_colors); |
| 426 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 427 | " planes = %.20g",(double) icon_info.planes); |
| 428 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 429 | " bpp = %.20g",(double) icon_info.bits_per_pixel); |
| 430 | } |
cristy | a2fb29a | 2014-09-21 22:04:52 +0000 | [diff] [blame] | 431 | if ((icon_info.number_colors != 0) || (icon_info.bits_per_pixel <= 16U)) |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 432 | { |
| 433 | image->storage_class=PseudoClass; |
| 434 | image->colors=icon_info.number_colors; |
Dirk Lemstra | b3299a3 | 2017-05-03 23:58:25 +0200 | [diff] [blame] | 435 | if ((image->colors == 0) || (image->colors > 256)) |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 436 | image->colors=one << icon_info.bits_per_pixel; |
| 437 | } |
| 438 | if (image->storage_class == PseudoClass) |
| 439 | { |
| 440 | register ssize_t |
| 441 | i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 442 | |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 443 | unsigned char |
| 444 | *icon_colormap; |
| 445 | |
| 446 | /* |
| 447 | Read Icon raster colormap. |
| 448 | */ |
dirk | 833908d | 2014-01-28 19:26:32 +0000 | [diff] [blame] | 449 | if (AcquireImageColormap(image,image->colors,exception) == |
| 450 | MagickFalse) |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 451 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 452 | icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t) |
| 453 | image->colors,4UL*sizeof(*icon_colormap)); |
| 454 | if (icon_colormap == (unsigned char *) NULL) |
| 455 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 456 | count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap); |
| 457 | if (count != (ssize_t) (4*image->colors)) |
Dirk Lemstra | fbb1428 | 2017-05-02 08:34:04 +0200 | [diff] [blame] | 458 | { |
| 459 | icon_colormap=(unsigned char *) RelinquishMagickMemory( |
| 460 | icon_colormap); |
| 461 | ThrowReaderException(CorruptImageError, |
| 462 | "InsufficientImageDataInFile"); |
| 463 | } |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 464 | p=icon_colormap; |
| 465 | for (i=0; i < (ssize_t) image->colors; i++) |
| 466 | { |
| 467 | image->colormap[i].blue=(Quantum) ScaleCharToQuantum(*p++); |
| 468 | image->colormap[i].green=(Quantum) ScaleCharToQuantum(*p++); |
| 469 | image->colormap[i].red=(Quantum) ScaleCharToQuantum(*p++); |
| 470 | p++; |
| 471 | } |
| 472 | icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap); |
| 473 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 474 | /* |
| 475 | Convert Icon raster image to pixel packets. |
| 476 | */ |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 477 | if ((image_info->ping != MagickFalse) && |
| 478 | (image_info->number_scenes != 0)) |
| 479 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 480 | break; |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 481 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 482 | if (status == MagickFalse) |
| 483 | return(DestroyImageList(image)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 484 | bytes_per_line=(((image->columns*icon_info.bits_per_pixel)+31) & |
| 485 | ~31) >> 3; |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 486 | (void) bytes_per_line; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 487 | scanline_pad=((((image->columns*icon_info.bits_per_pixel)+31) & ~31)- |
| 488 | (image->columns*icon_info.bits_per_pixel)) >> 3; |
| 489 | switch (icon_info.bits_per_pixel) |
| 490 | { |
| 491 | case 1: |
| 492 | { |
| 493 | /* |
| 494 | Convert bitmap scanline. |
| 495 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 496 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 497 | { |
| 498 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 499 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 500 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 501 | for (x=0; x < (ssize_t) (image->columns-7); x+=8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 502 | { |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 503 | byte=(size_t) ReadBlobByte(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 504 | for (bit=0; bit < 8; bit++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 505 | { |
| 506 | SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 : |
| 507 | 0x00),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 508 | q+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 509 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 510 | } |
| 511 | if ((image->columns % 8) != 0) |
| 512 | { |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 513 | byte=(size_t) ReadBlobByte(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 514 | for (bit=0; bit < (image->columns % 8); bit++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 515 | { |
| 516 | SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 : |
| 517 | 0x00),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 518 | q+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 519 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 520 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 521 | for (x=0; x < (ssize_t) scanline_pad; x++) |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 522 | (void) ReadBlobByte(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 523 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 524 | break; |
| 525 | if (image->previous == (Image *) NULL) |
| 526 | { |
| 527 | status=SetImageProgress(image,LoadImageTag,image->rows-y-1, |
| 528 | image->rows); |
| 529 | if (status == MagickFalse) |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 | break; |
| 534 | } |
| 535 | case 4: |
| 536 | { |
| 537 | /* |
| 538 | Read 4-bit Icon scanline. |
| 539 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 540 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 541 | { |
| 542 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 543 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 544 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 545 | for (x=0; x < ((ssize_t) image->columns-1); x+=2) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 546 | { |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 547 | byte=(size_t) ReadBlobByte(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 548 | SetPixelIndex(image,((byte >> 4) & 0xf),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 549 | q+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 550 | SetPixelIndex(image,((byte) & 0xf),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 551 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 552 | } |
| 553 | if ((image->columns % 2) != 0) |
| 554 | { |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 555 | byte=(size_t) ReadBlobByte(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 556 | SetPixelIndex(image,((byte >> 4) & 0xf),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 557 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 558 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 559 | for (x=0; x < (ssize_t) scanline_pad; x++) |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 560 | (void) ReadBlobByte(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 561 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 562 | break; |
| 563 | if (image->previous == (Image *) NULL) |
| 564 | { |
| 565 | status=SetImageProgress(image,LoadImageTag,image->rows-y-1, |
| 566 | image->rows); |
| 567 | if (status == MagickFalse) |
| 568 | break; |
| 569 | } |
| 570 | } |
| 571 | break; |
| 572 | } |
| 573 | case 8: |
| 574 | { |
| 575 | /* |
| 576 | Convert PseudoColor scanline. |
| 577 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 578 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 579 | { |
| 580 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 581 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 582 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 583 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 584 | { |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 585 | byte=(size_t) ReadBlobByte(image); |
dirk | 1678aec | 2015-08-30 12:07:29 +0200 | [diff] [blame] | 586 | SetPixelIndex(image,(Quantum) byte,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 587 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 588 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 589 | for (x=0; x < (ssize_t) scanline_pad; x++) |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 590 | (void) ReadBlobByte(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 591 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 592 | break; |
| 593 | if (image->previous == (Image *) NULL) |
| 594 | { |
| 595 | status=SetImageProgress(image,LoadImageTag,image->rows-y-1, |
| 596 | image->rows); |
| 597 | if (status == MagickFalse) |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | break; |
| 602 | } |
| 603 | case 16: |
| 604 | { |
| 605 | /* |
| 606 | Convert PseudoColor scanline. |
| 607 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 608 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 609 | { |
| 610 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 611 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 612 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 613 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 614 | { |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 615 | byte=(size_t) ReadBlobByte(image); |
| 616 | byte|=(size_t) (ReadBlobByte(image) << 8); |
dirk | 1678aec | 2015-08-30 12:07:29 +0200 | [diff] [blame] | 617 | SetPixelIndex(image,(Quantum) byte,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 618 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 619 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 620 | for (x=0; x < (ssize_t) scanline_pad; x++) |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 621 | (void) ReadBlobByte(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 622 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 623 | break; |
| 624 | if (image->previous == (Image *) NULL) |
| 625 | { |
| 626 | status=SetImageProgress(image,LoadImageTag,image->rows-y-1, |
| 627 | image->rows); |
| 628 | if (status == MagickFalse) |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | break; |
| 633 | } |
| 634 | case 24: |
| 635 | case 32: |
| 636 | { |
| 637 | /* |
| 638 | Convert DirectColor scanline. |
| 639 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 640 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 641 | { |
| 642 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 643 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 644 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 645 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 646 | { |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 647 | SetPixelBlue(image,ScaleCharToQuantum((unsigned char) |
| 648 | ReadBlobByte(image)),q); |
| 649 | SetPixelGreen(image,ScaleCharToQuantum((unsigned char) |
| 650 | ReadBlobByte(image)),q); |
| 651 | SetPixelRed(image,ScaleCharToQuantum((unsigned char) |
| 652 | ReadBlobByte(image)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 653 | if (icon_info.bits_per_pixel == 32) |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 654 | SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) |
| 655 | ReadBlobByte(image)),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 656 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 657 | } |
| 658 | if (icon_info.bits_per_pixel == 24) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 659 | for (x=0; x < (ssize_t) scanline_pad; x++) |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 660 | (void) ReadBlobByte(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 661 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 662 | break; |
| 663 | if (image->previous == (Image *) NULL) |
| 664 | { |
| 665 | status=SetImageProgress(image,LoadImageTag,image->rows-y-1, |
| 666 | image->rows); |
| 667 | if (status == MagickFalse) |
| 668 | break; |
| 669 | } |
| 670 | } |
| 671 | break; |
| 672 | } |
| 673 | default: |
| 674 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 675 | } |
Cristy | e0fe989 | 2016-04-05 09:55:07 -0400 | [diff] [blame] | 676 | if ((image_info->ping == MagickFalse) && |
| 677 | (icon_info.bits_per_pixel <= 16)) |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 678 | (void) SyncImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 679 | if (icon_info.bits_per_pixel != 32) |
| 680 | { |
| 681 | /* |
| 682 | Read the ICON alpha mask. |
| 683 | */ |
| 684 | image->storage_class=DirectClass; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 685 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 686 | { |
| 687 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 688 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 689 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 690 | for (x=0; x < ((ssize_t) image->columns-7); x+=8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 691 | { |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 692 | byte=(size_t) ReadBlobByte(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 693 | for (bit=0; bit < 8; bit++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 694 | { |
| 695 | SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ? |
| 696 | TransparentAlpha : OpaqueAlpha),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 697 | q+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 698 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 699 | } |
| 700 | if ((image->columns % 8) != 0) |
| 701 | { |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 702 | byte=(size_t) ReadBlobByte(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 703 | for (bit=0; bit < (image->columns % 8); bit++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 704 | { |
| 705 | SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ? |
| 706 | TransparentAlpha : OpaqueAlpha),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 707 | q+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 708 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 709 | } |
cristy | 0e8200f | 2009-09-13 21:10:06 +0000 | [diff] [blame] | 710 | if ((image->columns % 32) != 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 711 | for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++) |
cristy | f054ee7 | 2011-10-05 17:04:08 +0000 | [diff] [blame] | 712 | (void) ReadBlobByte(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 713 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | if (EOFBlob(image) != MagickFalse) |
| 718 | { |
| 719 | ThrowFileException(exception,CorruptImageError, |
| 720 | "UnexpectedEndOfFile",image->filename); |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | /* |
| 725 | Proceed to next image. |
| 726 | */ |
| 727 | if (image_info->number_scenes != 0) |
| 728 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 729 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 730 | if (i < (ssize_t) (icon_file.count-1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 731 | { |
| 732 | /* |
| 733 | Allocate next image structure. |
| 734 | */ |
cristy | 1f1de18 | 2011-10-05 18:41:34 +0000 | [diff] [blame] | 735 | AcquireNextImage(image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 736 | if (GetNextImageInList(image) == (Image *) NULL) |
| 737 | { |
| 738 | image=DestroyImageList(image); |
| 739 | return((Image *) NULL); |
| 740 | } |
| 741 | image=SyncNextImageInList(image); |
| 742 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 743 | GetBlobSize(image)); |
| 744 | if (status == MagickFalse) |
| 745 | break; |
| 746 | } |
| 747 | } |
| 748 | (void) CloseBlob(image); |
| 749 | return(GetFirstImageInList(image)); |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 754 | % % |
| 755 | % % |
| 756 | % % |
| 757 | % R e g i s t e r I C O N I m a g e % |
| 758 | % % |
| 759 | % % |
| 760 | % % |
| 761 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 762 | % |
| 763 | % RegisterICONImage() adds attributes for the Icon image format to |
| 764 | % the list of supported formats. The attributes include the image format |
| 765 | % tag, a method to read and/or write the format, whether the format |
| 766 | % supports the saving of more than one frame to the same file or blob, |
| 767 | % whether the format supports native in-memory I/O, and a brief |
| 768 | % description of the format. |
| 769 | % |
| 770 | % The format of the RegisterICONImage method is: |
| 771 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 772 | % size_t RegisterICONImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 773 | % |
| 774 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 775 | ModuleExport size_t RegisterICONImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 776 | { |
| 777 | MagickInfo |
| 778 | *entry; |
| 779 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 780 | entry=AcquireMagickInfo("ICON","CUR","Microsoft icon"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 781 | entry->decoder=(DecodeImageHandler *) ReadICONImage; |
| 782 | entry->encoder=(EncodeImageHandler *) WriteICONImage; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 783 | entry->flags^=CoderAdjoinFlag; |
Dirk Lemstra | 8974d77 | 2017-01-22 03:19:27 +0100 | [diff] [blame] | 784 | entry->flags|=CoderDecoderSeekableStreamFlag; |
| 785 | entry->flags|=CoderEncoderSeekableStreamFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 786 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 787 | entry=AcquireMagickInfo("ICON","ICO","Microsoft icon"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 788 | entry->decoder=(DecodeImageHandler *) ReadICONImage; |
| 789 | entry->encoder=(EncodeImageHandler *) WriteICONImage; |
Dirk Lemstra | 8974d77 | 2017-01-22 03:19:27 +0100 | [diff] [blame] | 790 | entry->flags|=CoderDecoderSeekableStreamFlag; |
| 791 | entry->flags|=CoderEncoderSeekableStreamFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 792 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 793 | entry=AcquireMagickInfo("ICON","ICON","Microsoft icon"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 794 | entry->decoder=(DecodeImageHandler *) ReadICONImage; |
| 795 | entry->encoder=(EncodeImageHandler *) WriteICONImage; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 796 | entry->flags^=CoderAdjoinFlag; |
Dirk Lemstra | 8974d77 | 2017-01-22 03:19:27 +0100 | [diff] [blame] | 797 | entry->flags|=CoderDecoderSeekableStreamFlag; |
| 798 | entry->flags|=CoderEncoderSeekableStreamFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 799 | (void) RegisterMagickInfo(entry); |
| 800 | return(MagickImageCoderSignature); |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 805 | % % |
| 806 | % % |
| 807 | % % |
| 808 | % U n r e g i s t e r I C O N I m a g e % |
| 809 | % % |
| 810 | % % |
| 811 | % % |
| 812 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 813 | % |
| 814 | % UnregisterICONImage() removes format registrations made by the |
| 815 | % ICON module from the list of supported formats. |
| 816 | % |
| 817 | % The format of the UnregisterICONImage method is: |
| 818 | % |
| 819 | % UnregisterICONImage(void) |
| 820 | % |
| 821 | */ |
| 822 | ModuleExport void UnregisterICONImage(void) |
| 823 | { |
| 824 | (void) UnregisterMagickInfo("CUR"); |
| 825 | (void) UnregisterMagickInfo("ICO"); |
| 826 | (void) UnregisterMagickInfo("ICON"); |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 831 | % % |
| 832 | % % |
| 833 | % % |
| 834 | % W r i t e I C O N I m a g e % |
| 835 | % % |
| 836 | % % |
| 837 | % % |
| 838 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 839 | % |
| 840 | % WriteICONImage() writes an image in Microsoft Windows bitmap encoded |
| 841 | % image format, version 3 for Windows or (if the image has a matte channel) |
| 842 | % version 4. |
| 843 | % |
cristy | 83cb91e | 2012-10-31 00:58:22 +0000 | [diff] [blame] | 844 | % It encodes any subimage as a compressed PNG image ("BI_PNG)", only when its |
| 845 | % dimensions are 256x256 and image->compression is undefined or is defined as |
| 846 | % ZipCompression. |
glennrp | 4b708b8 | 2012-07-17 19:15:58 +0000 | [diff] [blame] | 847 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 848 | % The format of the WriteICONImage method is: |
| 849 | % |
| 850 | % MagickBooleanType WriteICONImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 851 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 852 | % |
| 853 | % A description of each parameter follows. |
| 854 | % |
| 855 | % o image_info: the image info. |
| 856 | % |
| 857 | % o image: The image. |
| 858 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 859 | % o exception: return any errors or warnings in this structure. |
| 860 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 861 | */ |
| 862 | static MagickBooleanType WriteICONImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 863 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 864 | { |
dirk | 501a11e | 2014-01-19 16:22:18 +0000 | [diff] [blame] | 865 | const char |
| 866 | *option; |
| 867 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 868 | IconFile |
| 869 | icon_file; |
| 870 | |
| 871 | IconInfo |
| 872 | icon_info; |
| 873 | |
| 874 | Image |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 875 | *images, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 876 | *next; |
| 877 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 878 | MagickBooleanType |
| 879 | status; |
| 880 | |
| 881 | MagickOffsetType |
| 882 | offset, |
| 883 | scene; |
| 884 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 885 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 886 | *p; |
| 887 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 888 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 889 | i, |
| 890 | x; |
| 891 | |
| 892 | register unsigned char |
| 893 | *q; |
| 894 | |
cristy | ebc891a | 2011-04-24 23:04:16 +0000 | [diff] [blame] | 895 | size_t |
| 896 | bytes_per_line, |
| 897 | scanline_pad; |
| 898 | |
| 899 | ssize_t |
| 900 | y; |
| 901 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 902 | unsigned char |
| 903 | bit, |
| 904 | byte, |
| 905 | *pixels; |
| 906 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 907 | /* |
| 908 | Open output image file. |
| 909 | */ |
| 910 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 911 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 912 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 913 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 914 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 915 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 916 | assert(exception->signature == MagickCoreSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 917 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 918 | if (status == MagickFalse) |
| 919 | return(status); |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 920 | images=(Image *) NULL; |
dirk | 501a11e | 2014-01-19 16:22:18 +0000 | [diff] [blame] | 921 | option=GetImageOption(image_info,"icon:auto-resize"); |
| 922 | if (option != (const char *) NULL) |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 923 | { |
dirk | 501a11e | 2014-01-19 16:22:18 +0000 | [diff] [blame] | 924 | images=AutoResizeImage(image,option,&scene,exception); |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 925 | if (images == (Image *) NULL) |
dirk | 501a11e | 2014-01-19 16:22:18 +0000 | [diff] [blame] | 926 | ThrowWriterException(ImageError,"InvalidDimensions"); |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 927 | } |
| 928 | else |
| 929 | { |
| 930 | scene=0; |
| 931 | next=image; |
| 932 | do |
| 933 | { |
| 934 | if ((image->columns > 256L) || (image->rows > 256L)) |
| 935 | ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit"); |
| 936 | scene++; |
| 937 | next=SyncNextImageInList(next); |
| 938 | } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse)); |
| 939 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 940 | /* |
| 941 | Dump out a ICON header template to be properly initialized later. |
| 942 | */ |
| 943 | (void) WriteBlobLSBShort(image,0); |
| 944 | (void) WriteBlobLSBShort(image,1); |
| 945 | (void) WriteBlobLSBShort(image,(unsigned char) scene); |
| 946 | (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file)); |
| 947 | (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info)); |
| 948 | scene=0; |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 949 | next=(images != (Image *) NULL) ? images : image; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 950 | do |
| 951 | { |
| 952 | (void) WriteBlobByte(image,icon_file.directory[scene].width); |
| 953 | (void) WriteBlobByte(image,icon_file.directory[scene].height); |
| 954 | (void) WriteBlobByte(image,icon_file.directory[scene].colors); |
| 955 | (void) WriteBlobByte(image,icon_file.directory[scene].reserved); |
| 956 | (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes); |
| 957 | (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel); |
cristy | 0b29b25 | 2010-05-30 01:59:46 +0000 | [diff] [blame] | 958 | (void) WriteBlobLSBLong(image,(unsigned int) |
| 959 | icon_file.directory[scene].size); |
| 960 | (void) WriteBlobLSBLong(image,(unsigned int) |
| 961 | icon_file.directory[scene].offset); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 962 | scene++; |
| 963 | next=SyncNextImageInList(next); |
| 964 | } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse)); |
| 965 | scene=0; |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 966 | next=(images != (Image *) NULL) ? images : image; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 967 | do |
| 968 | { |
glennrp | 4b708b8 | 2012-07-17 19:15:58 +0000 | [diff] [blame] | 969 | if ((next->columns > 255L) && (next->rows > 255L) && |
| 970 | ((next->compression == UndefinedCompression) || |
| 971 | (next->compression == ZipCompression))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 972 | { |
| 973 | Image |
| 974 | *write_image; |
| 975 | |
| 976 | ImageInfo |
| 977 | *write_info; |
| 978 | |
| 979 | size_t |
| 980 | length; |
| 981 | |
| 982 | unsigned char |
| 983 | *png; |
| 984 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 985 | write_image=CloneImage(next,0,0,MagickTrue,exception); |
dirk | c1e5200 | 2014-01-09 20:22:42 +0000 | [diff] [blame] | 986 | if (write_image == (Image *) NULL) |
| 987 | { |
| 988 | images=DestroyImageList(images); |
| 989 | return(MagickFalse); |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 990 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 991 | write_info=CloneImageInfo(image_info); |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 992 | (void) CopyMagickString(write_info->filename,"PNG:",MagickPathExtent); |
glennrp | 4b708b8 | 2012-07-17 19:15:58 +0000 | [diff] [blame] | 993 | |
| 994 | /* Don't write any ancillary chunks except for gAMA */ |
| 995 | (void) SetImageArtifact(write_image,"png:include-chunk","none,gama"); |
| 996 | |
| 997 | /* Only write PNG32 formatted PNG (32-bit RGBA), 8 bits per channel */ |
| 998 | (void) SetImageArtifact(write_image,"png:format","png32"); |
| 999 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1000 | png=(unsigned char *) ImageToBlob(write_info,write_image,&length, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1001 | exception); |
Cristy | 15bc0d6 | 2015-12-17 12:36:54 -0500 | [diff] [blame] | 1002 | write_image=DestroyImageList(write_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1003 | write_info=DestroyImageInfo(write_info); |
dirk | c1e5200 | 2014-01-09 20:22:42 +0000 | [diff] [blame] | 1004 | if (png == (unsigned char *) NULL) |
| 1005 | { |
| 1006 | images=DestroyImageList(images); |
| 1007 | return(MagickFalse); |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 1008 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1009 | icon_file.directory[scene].width=0; |
| 1010 | icon_file.directory[scene].height=0; |
| 1011 | icon_file.directory[scene].colors=0; |
| 1012 | icon_file.directory[scene].reserved=0; |
| 1013 | icon_file.directory[scene].planes=1; |
| 1014 | icon_file.directory[scene].bits_per_pixel=32; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1015 | icon_file.directory[scene].size=(size_t) length; |
| 1016 | icon_file.directory[scene].offset=(size_t) TellBlob(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1017 | (void) WriteBlob(image,(size_t) length,png); |
| 1018 | png=(unsigned char *) RelinquishMagickMemory(png); |
| 1019 | } |
| 1020 | else |
| 1021 | { |
| 1022 | /* |
| 1023 | Initialize ICON raster file header. |
| 1024 | */ |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 1025 | (void) TransformImageColorspace(next,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1026 | icon_info.file_size=14+12+28; |
| 1027 | icon_info.offset_bits=icon_info.file_size; |
| 1028 | icon_info.compression=BI_RGB; |
| 1029 | if ((next->storage_class != DirectClass) && (next->colors > 256)) |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1030 | (void) SetImageStorageClass(next,DirectClass,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1031 | if (next->storage_class == DirectClass) |
| 1032 | { |
| 1033 | /* |
| 1034 | Full color ICON raster. |
| 1035 | */ |
| 1036 | icon_info.number_colors=0; |
| 1037 | icon_info.bits_per_pixel=32; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1038 | icon_info.compression=(size_t) BI_RGB; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1039 | } |
| 1040 | else |
| 1041 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 1042 | size_t |
| 1043 | one; |
| 1044 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1045 | /* |
| 1046 | Colormapped ICON raster. |
| 1047 | */ |
| 1048 | icon_info.bits_per_pixel=8; |
| 1049 | if (next->colors <= 256) |
| 1050 | icon_info.bits_per_pixel=8; |
| 1051 | if (next->colors <= 16) |
| 1052 | icon_info.bits_per_pixel=4; |
| 1053 | if (next->colors <= 2) |
| 1054 | icon_info.bits_per_pixel=1; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 1055 | one=1; |
| 1056 | icon_info.number_colors=one << icon_info.bits_per_pixel; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1057 | if (icon_info.number_colors < next->colors) |
| 1058 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1059 | (void) SetImageStorageClass(next,DirectClass,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1060 | icon_info.number_colors=0; |
| 1061 | icon_info.bits_per_pixel=(unsigned short) 24; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1062 | icon_info.compression=(size_t) BI_RGB; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1063 | } |
| 1064 | else |
| 1065 | { |
cristy | 0b29b25 | 2010-05-30 01:59:46 +0000 | [diff] [blame] | 1066 | size_t |
| 1067 | one; |
| 1068 | |
| 1069 | one=1; |
| 1070 | icon_info.file_size+=3*(one << icon_info.bits_per_pixel); |
| 1071 | icon_info.offset_bits+=3*(one << icon_info.bits_per_pixel); |
| 1072 | icon_info.file_size+=(one << icon_info.bits_per_pixel); |
| 1073 | icon_info.offset_bits+=(one << icon_info.bits_per_pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) & |
| 1077 | ~31) >> 3; |
| 1078 | icon_info.ba_offset=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1079 | icon_info.width=(ssize_t) next->columns; |
| 1080 | icon_info.height=(ssize_t) next->rows; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1081 | icon_info.planes=1; |
| 1082 | icon_info.image_size=bytes_per_line*next->rows; |
| 1083 | icon_info.size=40; |
| 1084 | icon_info.size+=(4*icon_info.number_colors); |
| 1085 | icon_info.size+=icon_info.image_size; |
| 1086 | icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height; |
| 1087 | icon_info.file_size+=icon_info.image_size; |
| 1088 | icon_info.x_pixels=0; |
| 1089 | icon_info.y_pixels=0; |
| 1090 | switch (next->units) |
| 1091 | { |
| 1092 | case UndefinedResolution: |
| 1093 | case PixelsPerInchResolution: |
| 1094 | { |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1095 | icon_info.x_pixels=(size_t) (100.0*next->resolution.x/2.54); |
| 1096 | icon_info.y_pixels=(size_t) (100.0*next->resolution.y/2.54); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1097 | break; |
| 1098 | } |
| 1099 | case PixelsPerCentimeterResolution: |
| 1100 | { |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1101 | icon_info.x_pixels=(size_t) (100.0*next->resolution.x); |
| 1102 | icon_info.y_pixels=(size_t) (100.0*next->resolution.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1103 | break; |
| 1104 | } |
| 1105 | } |
| 1106 | icon_info.colors_important=icon_info.number_colors; |
| 1107 | /* |
| 1108 | Convert MIFF to ICON raster pixels. |
| 1109 | */ |
| 1110 | pixels=(unsigned char *) AcquireQuantumMemory((size_t) |
| 1111 | icon_info.image_size,sizeof(*pixels)); |
dirk | c1e5200 | 2014-01-09 20:22:42 +0000 | [diff] [blame] | 1112 | if (pixels == (unsigned char *) NULL) |
| 1113 | { |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 1114 | images=DestroyImageList(images); |
| 1115 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
dirk | c1e5200 | 2014-01-09 20:22:42 +0000 | [diff] [blame] | 1116 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1117 | (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size); |
| 1118 | switch (icon_info.bits_per_pixel) |
| 1119 | { |
| 1120 | case 1: |
| 1121 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1122 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1123 | bit, |
| 1124 | byte; |
| 1125 | |
| 1126 | /* |
| 1127 | Convert PseudoClass image to a ICON monochrome image. |
| 1128 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1129 | for (y=0; y < (ssize_t) next->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1130 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1131 | p=GetVirtualPixels(next,0,y,next->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1132 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1133 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1134 | q=pixels+(next->rows-y-1)*bytes_per_line; |
| 1135 | bit=0; |
| 1136 | byte=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1137 | for (x=0; x < (ssize_t) next->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1138 | { |
| 1139 | byte<<=1; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1140 | byte|=GetPixelIndex(next,p) != 0 ? 0x01 : 0x00; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1141 | bit++; |
| 1142 | if (bit == 8) |
| 1143 | { |
| 1144 | *q++=(unsigned char) byte; |
| 1145 | bit=0; |
| 1146 | byte=0; |
| 1147 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1148 | p+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1149 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1150 | if (bit != 0) |
| 1151 | *q++=(unsigned char) (byte << (8-bit)); |
| 1152 | if (next->previous == (Image *) NULL) |
| 1153 | { |
| 1154 | status=SetImageProgress(next,SaveImageTag,y,next->rows); |
| 1155 | if (status == MagickFalse) |
| 1156 | break; |
| 1157 | } |
| 1158 | } |
| 1159 | break; |
| 1160 | } |
| 1161 | case 4: |
| 1162 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1163 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1164 | nibble, |
| 1165 | byte; |
| 1166 | |
| 1167 | /* |
| 1168 | Convert PseudoClass image to a ICON monochrome image. |
| 1169 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1170 | for (y=0; y < (ssize_t) next->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1171 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1172 | p=GetVirtualPixels(next,0,y,next->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1173 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1174 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1175 | q=pixels+(next->rows-y-1)*bytes_per_line; |
| 1176 | nibble=0; |
| 1177 | byte=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1178 | for (x=0; x < (ssize_t) next->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1179 | { |
| 1180 | byte<<=4; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1181 | byte|=((size_t) GetPixelIndex(next,p) & 0x0f); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1182 | nibble++; |
| 1183 | if (nibble == 2) |
| 1184 | { |
| 1185 | *q++=(unsigned char) byte; |
| 1186 | nibble=0; |
| 1187 | byte=0; |
| 1188 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1189 | p+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1190 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1191 | if (nibble != 0) |
| 1192 | *q++=(unsigned char) (byte << 4); |
| 1193 | if (next->previous == (Image *) NULL) |
| 1194 | { |
| 1195 | status=SetImageProgress(next,SaveImageTag,y,next->rows); |
| 1196 | if (status == MagickFalse) |
| 1197 | break; |
| 1198 | } |
| 1199 | } |
| 1200 | break; |
| 1201 | } |
| 1202 | case 8: |
| 1203 | { |
| 1204 | /* |
| 1205 | Convert PseudoClass packet to ICON pixel. |
| 1206 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1207 | for (y=0; y < (ssize_t) next->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1208 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1209 | p=GetVirtualPixels(next,0,y,next->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1210 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1211 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1212 | q=pixels+(next->rows-y-1)*bytes_per_line; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1213 | for (x=0; x < (ssize_t) next->columns; x++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1214 | { |
| 1215 | *q++=(unsigned char) GetPixelIndex(next,p); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1216 | p+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1217 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1218 | if (next->previous == (Image *) NULL) |
| 1219 | { |
| 1220 | status=SetImageProgress(next,SaveImageTag,y,next->rows); |
| 1221 | if (status == MagickFalse) |
| 1222 | break; |
| 1223 | } |
| 1224 | } |
| 1225 | break; |
| 1226 | } |
| 1227 | case 24: |
| 1228 | case 32: |
| 1229 | { |
| 1230 | /* |
| 1231 | Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel. |
| 1232 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1233 | for (y=0; y < (ssize_t) next->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1234 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1235 | p=GetVirtualPixels(next,0,y,next->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1236 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1237 | break; |
| 1238 | q=pixels+(next->rows-y-1)*bytes_per_line; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1239 | for (x=0; x < (ssize_t) next->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1240 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1241 | *q++=ScaleQuantumToChar(GetPixelBlue(next,p)); |
| 1242 | *q++=ScaleQuantumToChar(GetPixelGreen(next,p)); |
| 1243 | *q++=ScaleQuantumToChar(GetPixelRed(next,p)); |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 1244 | if (next->alpha_trait == UndefinedPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1245 | *q++=ScaleQuantumToChar(QuantumRange); |
| 1246 | else |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1247 | *q++=ScaleQuantumToChar(GetPixelAlpha(next,p)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1248 | p+=GetPixelChannels(next); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1249 | } |
| 1250 | if (icon_info.bits_per_pixel == 24) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1251 | for (x=3L*(ssize_t) next->columns; x < (ssize_t) bytes_per_line; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1252 | *q++=0x00; |
| 1253 | if (next->previous == (Image *) NULL) |
| 1254 | { |
| 1255 | status=SetImageProgress(next,SaveImageTag,y,next->rows); |
| 1256 | if (status == MagickFalse) |
| 1257 | break; |
| 1258 | } |
| 1259 | } |
| 1260 | break; |
| 1261 | } |
| 1262 | } |
| 1263 | /* |
| 1264 | Write 40-byte version 3+ bitmap header. |
| 1265 | */ |
| 1266 | icon_file.directory[scene].width=(unsigned char) icon_info.width; |
| 1267 | icon_file.directory[scene].height=(unsigned char) icon_info.height; |
| 1268 | icon_file.directory[scene].colors=(unsigned char) |
| 1269 | icon_info.number_colors; |
| 1270 | icon_file.directory[scene].reserved=0; |
| 1271 | icon_file.directory[scene].planes=icon_info.planes; |
| 1272 | icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel; |
| 1273 | icon_file.directory[scene].size=icon_info.size; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1274 | icon_file.directory[scene].offset=(size_t) TellBlob(image); |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 1275 | (void) WriteBlobLSBLong(image,(unsigned int) 40); |
| 1276 | (void) WriteBlobLSBLong(image,(unsigned int) icon_info.width); |
| 1277 | (void) WriteBlobLSBLong(image,(unsigned int) icon_info.height*2); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1278 | (void) WriteBlobLSBShort(image,icon_info.planes); |
| 1279 | (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel); |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 1280 | (void) WriteBlobLSBLong(image,(unsigned int) icon_info.compression); |
| 1281 | (void) WriteBlobLSBLong(image,(unsigned int) icon_info.image_size); |
| 1282 | (void) WriteBlobLSBLong(image,(unsigned int) icon_info.x_pixels); |
| 1283 | (void) WriteBlobLSBLong(image,(unsigned int) icon_info.y_pixels); |
| 1284 | (void) WriteBlobLSBLong(image,(unsigned int) icon_info.number_colors); |
| 1285 | (void) WriteBlobLSBLong(image,(unsigned int) icon_info.colors_important); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1286 | if (next->storage_class == PseudoClass) |
| 1287 | { |
| 1288 | unsigned char |
| 1289 | *icon_colormap; |
| 1290 | |
| 1291 | /* |
| 1292 | Dump colormap to file. |
| 1293 | */ |
| 1294 | icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t) |
| 1295 | (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap)); |
dirk | c1e5200 | 2014-01-09 20:22:42 +0000 | [diff] [blame] | 1296 | if (icon_colormap == (unsigned char *) NULL) |
| 1297 | { |
| 1298 | images=DestroyImageList(images); |
| 1299 | ThrowWriterException(ResourceLimitError, |
| 1300 | "MemoryAllocationFailed"); |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 1301 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1302 | q=icon_colormap; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1303 | for (i=0; i < (ssize_t) next->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1304 | { |
| 1305 | *q++=ScaleQuantumToChar(next->colormap[i].blue); |
| 1306 | *q++=ScaleQuantumToChar(next->colormap[i].green); |
| 1307 | *q++=ScaleQuantumToChar(next->colormap[i].red); |
| 1308 | *q++=(unsigned char) 0x0; |
| 1309 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1310 | for ( ; i < (ssize_t) (1UL << icon_info.bits_per_pixel); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1311 | { |
| 1312 | *q++=(unsigned char) 0x00; |
| 1313 | *q++=(unsigned char) 0x00; |
| 1314 | *q++=(unsigned char) 0x00; |
| 1315 | *q++=(unsigned char) 0x00; |
| 1316 | } |
| 1317 | (void) WriteBlob(image,(size_t) (4UL*(1UL << |
| 1318 | icon_info.bits_per_pixel)),icon_colormap); |
| 1319 | icon_colormap=(unsigned char *) RelinquishMagickMemory( |
| 1320 | icon_colormap); |
| 1321 | } |
| 1322 | (void) WriteBlob(image,(size_t) icon_info.image_size,pixels); |
| 1323 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 1324 | /* |
| 1325 | Write matte mask. |
| 1326 | */ |
| 1327 | scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1328 | for (y=((ssize_t) next->rows - 1); y >= 0; y--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1329 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1330 | p=GetVirtualPixels(next,0,y,next->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1331 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1332 | break; |
| 1333 | bit=0; |
| 1334 | byte=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1335 | for (x=0; x < (ssize_t) next->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1336 | { |
| 1337 | byte<<=1; |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 1338 | if ((next->alpha_trait != UndefinedPixelTrait) && |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1339 | (GetPixelAlpha(next,p) == (Quantum) TransparentAlpha)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1340 | byte|=0x01; |
| 1341 | bit++; |
| 1342 | if (bit == 8) |
| 1343 | { |
| 1344 | (void) WriteBlobByte(image,(unsigned char) byte); |
| 1345 | bit=0; |
| 1346 | byte=0; |
| 1347 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1348 | p+=GetPixelChannels(next); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1349 | } |
| 1350 | if (bit != 0) |
| 1351 | (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit))); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1352 | for (i=0; i < (ssize_t) scanline_pad; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1353 | (void) WriteBlobByte(image,(unsigned char) 0); |
| 1354 | } |
| 1355 | } |
| 1356 | if (GetNextImageInList(next) == (Image *) NULL) |
| 1357 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1358 | status=SetImageProgress(next,SaveImagesTag,scene++, |
| 1359 | GetImageListLength(next)); |
| 1360 | if (status == MagickFalse) |
| 1361 | break; |
cristy | 5467fcf | 2014-05-18 17:20:55 +0000 | [diff] [blame] | 1362 | next=SyncNextImageInList(next); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1363 | } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse)); |
| 1364 | offset=SeekBlob(image,0,SEEK_SET); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 1365 | (void) offset; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1366 | (void) WriteBlobLSBShort(image,0); |
| 1367 | (void) WriteBlobLSBShort(image,1); |
| 1368 | (void) WriteBlobLSBShort(image,(unsigned short) (scene+1)); |
| 1369 | scene=0; |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 1370 | next=(images != (Image *) NULL) ? images : image; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1371 | do |
| 1372 | { |
| 1373 | (void) WriteBlobByte(image,icon_file.directory[scene].width); |
| 1374 | (void) WriteBlobByte(image,icon_file.directory[scene].height); |
| 1375 | (void) WriteBlobByte(image,icon_file.directory[scene].colors); |
| 1376 | (void) WriteBlobByte(image,icon_file.directory[scene].reserved); |
| 1377 | (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes); |
| 1378 | (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel); |
cristy | 0b29b25 | 2010-05-30 01:59:46 +0000 | [diff] [blame] | 1379 | (void) WriteBlobLSBLong(image,(unsigned int) |
| 1380 | icon_file.directory[scene].size); |
| 1381 | (void) WriteBlobLSBLong(image,(unsigned int) |
| 1382 | icon_file.directory[scene].offset); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1383 | scene++; |
| 1384 | next=SyncNextImageInList(next); |
| 1385 | } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse)); |
| 1386 | (void) CloseBlob(image); |
dirk | 9e13770 | 2014-01-09 19:29:59 +0000 | [diff] [blame] | 1387 | images=DestroyImageList(images); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1388 | return(MagickTrue); |
| 1389 | } |