| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % DDDD IIIII BBBB % |
| 7 | % D D I B B % |
| 8 | % D D I BBBB % |
| 9 | % D D I B B % |
| 10 | % DDDD IIIII BBBB % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write Windows DIB Image 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 | b56bb24 | 2014-11-25 17:12:48 +0000 | [diff] [blame] | 20 | % Copyright 1999-2015 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 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 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" |
| 43 | #include "MagickCore/attribute.h" |
| 44 | #include "MagickCore/blob.h" |
| 45 | #include "MagickCore/blob-private.h" |
| 46 | #include "MagickCore/cache.h" |
| 47 | #include "MagickCore/color.h" |
| 48 | #include "MagickCore/color-private.h" |
| 49 | #include "MagickCore/colormap.h" |
| 50 | #include "MagickCore/colormap-private.h" |
| 51 | #include "MagickCore/colorspace.h" |
| cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 52 | #include "MagickCore/colorspace-private.h" |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 53 | #include "MagickCore/draw.h" |
| 54 | #include "MagickCore/exception.h" |
| 55 | #include "MagickCore/exception-private.h" |
| 56 | #include "MagickCore/geometry.h" |
| 57 | #include "MagickCore/image.h" |
| 58 | #include "MagickCore/image-private.h" |
| 59 | #include "MagickCore/list.h" |
| 60 | #include "MagickCore/log.h" |
| 61 | #include "MagickCore/magick.h" |
| 62 | #include "MagickCore/memory_.h" |
| 63 | #include "MagickCore/monitor.h" |
| 64 | #include "MagickCore/monitor-private.h" |
| 65 | #include "MagickCore/pixel-accessor.h" |
| 66 | #include "MagickCore/quantum-private.h" |
| 67 | #include "MagickCore/static.h" |
| 68 | #include "MagickCore/string_.h" |
| 69 | #include "MagickCore/module.h" |
| 70 | #include "MagickCore/transform.h" |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | Typedef declarations. |
| 74 | */ |
| 75 | typedef struct _DIBInfo |
| 76 | { |
| cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 77 | size_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 78 | size; |
| 79 | |
| cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 80 | ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 81 | width, |
| 82 | height; |
| 83 | |
| 84 | unsigned short |
| 85 | planes, |
| 86 | bits_per_pixel; |
| 87 | |
| cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 88 | size_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 89 | compression, |
| 90 | image_size, |
| 91 | x_pixels, |
| 92 | y_pixels, |
| 93 | number_colors, |
| 94 | red_mask, |
| 95 | green_mask, |
| 96 | blue_mask, |
| 97 | alpha_mask, |
| 98 | colors_important; |
| 99 | |
| cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 100 | ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 101 | colorspace; |
| 102 | |
| 103 | PointInfo |
| 104 | red_primary, |
| 105 | green_primary, |
| 106 | blue_primary, |
| 107 | gamma_scale; |
| 108 | } DIBInfo; |
| 109 | |
| 110 | /* |
| 111 | Forward declarations. |
| 112 | */ |
| 113 | static MagickBooleanType |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 114 | WriteDIBImage(const ImageInfo *,Image *,ExceptionInfo *); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 118 | % % |
| 119 | % % |
| 120 | % % |
| 121 | % D e c o d e I m a g e % |
| 122 | % % |
| 123 | % % |
| 124 | % % |
| 125 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 126 | % |
| 127 | % DecodeImage unpacks the packed image pixels into runlength-encoded |
| 128 | % pixel packets. |
| 129 | % |
| 130 | % The format of the DecodeImage method is: |
| 131 | % |
| 132 | % MagickBooleanType DecodeImage(Image *image, |
| 133 | % const MagickBooleanType compression,unsigned char *pixels) |
| 134 | % |
| 135 | % A description of each parameter follows: |
| 136 | % |
| 137 | % o image: the address of a structure of type Image. |
| 138 | % |
| 139 | % o compression: A value of 1 means the compressed pixels are runlength |
| 140 | % encoded for a 256-color bitmap. A value of 2 means a 16-color bitmap. |
| 141 | % |
| 142 | % o pixels: The address of a byte (8 bits) array of pixel data created by |
| 143 | % the decoding process. |
| 144 | % |
| 145 | */ |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 146 | static MagickBooleanType DecodeImage(Image *image, |
| 147 | const MagickBooleanType compression,unsigned char *pixels) |
| 148 | { |
| cristy | 07a3cca | 2012-12-10 13:09:10 +0000 | [diff] [blame] | 149 | #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__) || defined(__MINGW64__) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 150 | #define BI_RGB 0 |
| 151 | #define BI_RLE8 1 |
| 152 | #define BI_RLE4 2 |
| 153 | #define BI_BITFIELDS 3 |
| cristy | f8dcd77 | 2014-12-19 12:11:44 +0000 | [diff] [blame] | 154 | #undef BI_JPEG |
| 155 | #define BI_JPEG 4 |
| 156 | #undef BI_PNG |
| 157 | #define BI_PNG 5 |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 158 | #endif |
| 159 | |
| 160 | int |
| 161 | count; |
| 162 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 163 | ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 164 | y; |
| 165 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 166 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 167 | i, |
| 168 | x; |
| 169 | |
| 170 | register unsigned char |
| 171 | *p, |
| 172 | *q; |
| 173 | |
| 174 | unsigned char |
| 175 | byte; |
| 176 | |
| 177 | assert(image != (Image *) NULL); |
| 178 | assert(image->signature == MagickSignature); |
| 179 | if (image->debug != MagickFalse) |
| 180 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 181 | assert(pixels != (unsigned char *) NULL); |
| 182 | (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows* |
| 183 | sizeof(*pixels)); |
| 184 | byte=0; |
| 185 | x=0; |
| 186 | p=pixels; |
| 187 | q=pixels+(size_t) image->columns*image->rows; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 188 | for (y=0; y < (ssize_t) image->rows; ) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 189 | { |
| 190 | if ((p < pixels) || (p >= q)) |
| 191 | break; |
| 192 | count=ReadBlobByte(image); |
| 193 | if (count == EOF) |
| 194 | break; |
| 195 | if (count != 0) |
| 196 | { |
| 197 | count=(int) MagickMin((size_t) count,(size_t) (q-p)); |
| 198 | /* |
| 199 | Encoded mode. |
| 200 | */ |
| 201 | byte=(unsigned char) ReadBlobByte(image); |
| 202 | if (compression == BI_RLE8) |
| 203 | { |
| 204 | for (i=0; i < count; i++) |
| 205 | *p++=(unsigned char) byte; |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | for (i=0; i < count; i++) |
| 210 | *p++=(unsigned char) |
| 211 | ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f)); |
| 212 | } |
| 213 | x+=count; |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | /* |
| 218 | Escape mode. |
| 219 | */ |
| 220 | count=ReadBlobByte(image); |
| 221 | if (count == 0x01) |
| 222 | return(MagickTrue); |
| 223 | switch (count) |
| 224 | { |
| 225 | case 0x00: |
| 226 | { |
| 227 | /* |
| 228 | End of line. |
| 229 | */ |
| 230 | x=0; |
| 231 | y++; |
| 232 | p=pixels+y*image->columns; |
| 233 | break; |
| 234 | } |
| 235 | case 0x02: |
| 236 | { |
| 237 | /* |
| 238 | Delta mode. |
| 239 | */ |
| 240 | x+=ReadBlobByte(image); |
| 241 | y+=ReadBlobByte(image); |
| 242 | p=pixels+y*image->columns+x; |
| 243 | break; |
| 244 | } |
| 245 | default: |
| 246 | { |
| 247 | /* |
| 248 | Absolute mode. |
| 249 | */ |
| 250 | count=(int) MagickMin((size_t) count,(size_t) (q-p)); |
| 251 | if (compression == BI_RLE8) |
| 252 | for (i=0; i < count; i++) |
| 253 | *p++=(unsigned char) ReadBlobByte(image); |
| 254 | else |
| 255 | for (i=0; i < count; i++) |
| 256 | { |
| 257 | if ((i & 0x01) == 0) |
| 258 | byte=(unsigned char) ReadBlobByte(image); |
| 259 | *p++=(unsigned char) |
| 260 | ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f)); |
| 261 | } |
| 262 | x+=count; |
| 263 | /* |
| 264 | Read pad byte. |
| 265 | */ |
| 266 | if (compression == BI_RLE8) |
| 267 | { |
| 268 | if ((count & 0x01) != 0) |
| 269 | (void) ReadBlobByte(image); |
| 270 | } |
| 271 | else |
| 272 | if (((count & 0x03) == 1) || ((count & 0x03) == 2)) |
| 273 | (void) ReadBlobByte(image); |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse) |
| 279 | break; |
| 280 | } |
| 281 | (void) ReadBlobByte(image); /* end of line */ |
| 282 | (void) ReadBlobByte(image); |
| 283 | return(MagickTrue); |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 288 | % % |
| 289 | % % |
| 290 | % % |
| 291 | % E n c o d e I m a g e % |
| 292 | % % |
| 293 | % % |
| 294 | % % |
| 295 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 296 | % |
| 297 | % EncodeImage compresses pixels using a runlength encoded format. |
| 298 | % |
| 299 | % The format of the EncodeImage method is: |
| 300 | % |
| 301 | % static MagickBooleanType EncodeImage(Image *image, |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 302 | % const size_t bytes_per_line,const unsigned char *pixels, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 303 | % unsigned char *compressed_pixels) |
| 304 | % |
| 305 | % A description of each parameter follows: |
| 306 | % |
| 307 | % o image: The image. |
| 308 | % |
| 309 | % o bytes_per_line: the number of bytes in a scanline of compressed pixels |
| 310 | % |
| 311 | % o pixels: The address of a byte (8 bits) array of pixel data created by |
| 312 | % the compression process. |
| 313 | % |
| 314 | % o compressed_pixels: The address of a byte (8 bits) array of compressed |
| 315 | % pixel data. |
| 316 | % |
| 317 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 318 | static size_t EncodeImage(Image *image,const size_t bytes_per_line, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 319 | const unsigned char *pixels,unsigned char *compressed_pixels) |
| 320 | { |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 321 | ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 322 | y; |
| 323 | |
| 324 | register const unsigned char |
| 325 | *p; |
| 326 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 327 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 328 | i, |
| 329 | x; |
| 330 | |
| 331 | register unsigned char |
| 332 | *q; |
| 333 | |
| 334 | /* |
| 335 | Runlength encode pixels. |
| 336 | */ |
| 337 | assert(image != (Image *) NULL); |
| 338 | assert(image->signature == MagickSignature); |
| 339 | if (image->debug != MagickFalse) |
| 340 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 341 | assert(pixels != (const unsigned char *) NULL); |
| 342 | assert(compressed_pixels != (unsigned char *) NULL); |
| 343 | p=pixels; |
| 344 | q=compressed_pixels; |
| 345 | i=0; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 346 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 347 | { |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 348 | for (x=0; x < (ssize_t) bytes_per_line; x+=i) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 349 | { |
| 350 | /* |
| 351 | Determine runlength. |
| 352 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 353 | for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 354 | if ((*(p+i) != *p) || (i == 255)) |
| 355 | break; |
| 356 | *q++=(unsigned char) i; |
| 357 | *q++=(*p); |
| 358 | p+=i; |
| 359 | } |
| 360 | /* |
| 361 | End of line. |
| 362 | */ |
| 363 | *q++=0x00; |
| 364 | *q++=0x00; |
| 365 | if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse) |
| 366 | break; |
| 367 | } |
| 368 | /* |
| 369 | End of bitmap. |
| 370 | */ |
| 371 | *q++=0; |
| 372 | *q++=0x01; |
| 373 | return((size_t) (q-compressed_pixels)); |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 378 | % % |
| 379 | % % |
| 380 | % % |
| 381 | % I s D I B % |
| 382 | % % |
| 383 | % % |
| 384 | % % |
| 385 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 386 | % |
| 387 | % IsDIB() returns MagickTrue if the image format type, identified by the |
| 388 | % magick string, is DIB. |
| 389 | % |
| 390 | % The format of the IsDIB method is: |
| 391 | % |
| 392 | % MagickBooleanType IsDIB(const unsigned char *magick,const size_t length) |
| 393 | % |
| 394 | % A description of each parameter follows: |
| 395 | % |
| 396 | % o magick: compare image format pattern against these bytes. |
| 397 | % |
| 398 | % o length: Specifies the length of the magick string. |
| 399 | % |
| 400 | */ |
| 401 | static MagickBooleanType IsDIB(const unsigned char *magick,const size_t length) |
| 402 | { |
| 403 | if (length < 2) |
| 404 | return(MagickFalse); |
| 405 | if (memcmp(magick,"\050\000",2) == 0) |
| 406 | return(MagickTrue); |
| 407 | return(MagickFalse); |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 412 | % % |
| 413 | % % |
| 414 | % % |
| 415 | % R e a d D I B I m a g e % |
| 416 | % % |
| 417 | % % |
| 418 | % % |
| 419 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 420 | % |
| 421 | % ReadDIBImage() reads a Microsoft Windows bitmap image file and |
| 422 | % returns it. It allocates the memory necessary for the new Image structure |
| 423 | % and returns a pointer to the new image. |
| 424 | % |
| 425 | % The format of the ReadDIBImage method is: |
| 426 | % |
| 427 | % image=ReadDIBImage(image_info) |
| 428 | % |
| 429 | % A description of each parameter follows: |
| 430 | % |
| 431 | % o image_info: the image info. |
| 432 | % |
| 433 | % o exception: return any errors or warnings in this structure. |
| 434 | % |
| 435 | */ |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 436 | static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 437 | { |
| 438 | DIBInfo |
| 439 | dib_info; |
| 440 | |
| 441 | Image |
| 442 | *image; |
| 443 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 444 | MagickBooleanType |
| 445 | status; |
| 446 | |
| cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 447 | MemoryInfo |
| 448 | *pixel_info; |
| 449 | |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 450 | Quantum |
| 451 | index; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 452 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 453 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 454 | x; |
| 455 | |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 456 | register Quantum |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 457 | *q; |
| 458 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 459 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 460 | i; |
| 461 | |
| 462 | register unsigned char |
| 463 | *p; |
| 464 | |
| 465 | size_t |
| cristy | ddbc41b | 2011-04-24 14:27:48 +0000 | [diff] [blame] | 466 | bytes_per_line, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 467 | length; |
| 468 | |
| 469 | ssize_t |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 470 | bit, |
| 471 | count, |
| 472 | y; |
| 473 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 474 | |
| 475 | unsigned char |
| 476 | *pixels; |
| 477 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 478 | /* |
| 479 | Open image file. |
| 480 | */ |
| 481 | assert(image_info != (const ImageInfo *) NULL); |
| 482 | assert(image_info->signature == MagickSignature); |
| 483 | if (image_info->debug != MagickFalse) |
| 484 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 485 | image_info->filename); |
| 486 | assert(exception != (ExceptionInfo *) NULL); |
| 487 | assert(exception->signature == MagickSignature); |
| cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 488 | image=AcquireImage(image_info,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 489 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 490 | if (status == MagickFalse) |
| 491 | { |
| 492 | image=DestroyImageList(image); |
| 493 | return((Image *) NULL); |
| 494 | } |
| 495 | /* |
| 496 | Determine if this a DIB file. |
| 497 | */ |
| 498 | (void) ResetMagickMemory(&dib_info,0,sizeof(dib_info)); |
| 499 | dib_info.size=ReadBlobLSBLong(image); |
| cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 500 | if (dib_info.size != 40) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 501 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 502 | /* |
| 503 | Microsoft Windows 3.X DIB image file. |
| 504 | */ |
| 505 | dib_info.width=(short) ReadBlobLSBLong(image); |
| 506 | dib_info.height=(short) ReadBlobLSBLong(image); |
| 507 | dib_info.planes=ReadBlobLSBShort(image); |
| 508 | dib_info.bits_per_pixel=ReadBlobLSBShort(image); |
| cristy | f8dcd77 | 2014-12-19 12:11:44 +0000 | [diff] [blame] | 509 | if (dib_info.bits_per_pixel > 32) |
| 510 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 511 | dib_info.compression=ReadBlobLSBLong(image); |
| 512 | dib_info.image_size=ReadBlobLSBLong(image); |
| 513 | dib_info.x_pixels=ReadBlobLSBLong(image); |
| 514 | dib_info.y_pixels=ReadBlobLSBLong(image); |
| 515 | dib_info.number_colors=ReadBlobLSBLong(image); |
| 516 | dib_info.colors_important=ReadBlobLSBLong(image); |
| cristy | ca793d7 | 2015-01-23 13:03:55 +0000 | [diff] [blame] | 517 | if ((dib_info.bits_per_pixel != 1) && (dib_info.bits_per_pixel != 4) && |
| 518 | (dib_info.bits_per_pixel != 8) && (dib_info.bits_per_pixel != 16) && |
| 519 | (dib_info.bits_per_pixel != 24) && (dib_info.bits_per_pixel != 32)) |
| 520 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 521 | if ((dib_info.compression == BI_BITFIELDS) && |
| 522 | ((dib_info.bits_per_pixel == 16) || (dib_info.bits_per_pixel == 32))) |
| 523 | { |
| 524 | dib_info.red_mask=ReadBlobLSBLong(image); |
| 525 | dib_info.green_mask=ReadBlobLSBLong(image); |
| 526 | dib_info.blue_mask=ReadBlobLSBLong(image); |
| 527 | } |
| cristy | ca793d7 | 2015-01-23 13:03:55 +0000 | [diff] [blame] | 528 | if (EOFBlob(image) != MagickFalse) |
| 529 | ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile"); |
| cristy | f8dcd77 | 2014-12-19 12:11:44 +0000 | [diff] [blame] | 530 | if (dib_info.width <= 0) |
| 531 | ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize"); |
| 532 | if (dib_info.height == 0) |
| 533 | ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize"); |
| 534 | if (dib_info.planes != 1) |
| 535 | ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne"); |
| 536 | if ((dib_info.bits_per_pixel != 1) && (dib_info.bits_per_pixel != 4) && |
| 537 | (dib_info.bits_per_pixel != 8) && (dib_info.bits_per_pixel != 16) && |
| 538 | (dib_info.bits_per_pixel != 24) && (dib_info.bits_per_pixel != 32)) |
| 539 | ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel"); |
| 540 | if (dib_info.bits_per_pixel < 16 && |
| cristy | 58d4fe1 | 2015-02-07 11:53:05 +0000 | [diff] [blame] | 541 | dib_info.number_colors > (1UL << dib_info.bits_per_pixel)) |
| cristy | f8dcd77 | 2014-12-19 12:11:44 +0000 | [diff] [blame] | 542 | ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors"); |
| 543 | if ((dib_info.compression == 1) && (dib_info.bits_per_pixel != 8)) |
| 544 | ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel"); |
| 545 | if ((dib_info.compression == 2) && (dib_info.bits_per_pixel != 4)) |
| 546 | ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel"); |
| 547 | if ((dib_info.compression == 3) && (dib_info.bits_per_pixel < 16)) |
| 548 | ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel"); |
| 549 | switch (dib_info.compression) |
| 550 | { |
| 551 | case BI_RGB: |
| 552 | case BI_RLE8: |
| 553 | case BI_RLE4: |
| 554 | case BI_BITFIELDS: |
| 555 | break; |
| 556 | case BI_JPEG: |
| 557 | ThrowReaderException(CoderError,"JPEGCompressNotSupported"); |
| 558 | case BI_PNG: |
| 559 | ThrowReaderException(CoderError,"PNGCompressNotSupported"); |
| 560 | default: |
| 561 | ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression"); |
| 562 | } |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 563 | image->columns=(size_t) MagickAbsoluteValue(dib_info.width); |
| 564 | image->rows=(size_t) MagickAbsoluteValue(dib_info.height); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 565 | image->depth=8; |
| cristy | f8dcd77 | 2014-12-19 12:11:44 +0000 | [diff] [blame] | 566 | image->alpha_trait=dib_info.bits_per_pixel == 32 ? BlendPixelTrait : |
| 567 | UndefinedPixelTrait; |
| cristy | ca793d7 | 2015-01-23 13:03:55 +0000 | [diff] [blame] | 568 | if ((dib_info.number_colors > 256) || (dib_info.colors_important > 256)) |
| 569 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 570 | if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16)) |
| 571 | { |
| cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 572 | size_t |
| 573 | one; |
| 574 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 575 | image->storage_class=PseudoClass; |
| 576 | image->colors=dib_info.number_colors; |
| cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 577 | one=1; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 578 | if (image->colors == 0) |
| cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 579 | image->colors=one << dib_info.bits_per_pixel; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 580 | } |
| 581 | if (image_info->size) |
| 582 | { |
| 583 | RectangleInfo |
| 584 | geometry; |
| 585 | |
| 586 | MagickStatusType |
| 587 | flags; |
| 588 | |
| 589 | flags=ParseAbsoluteGeometry(image_info->size,&geometry); |
| 590 | if (flags & WidthValue) |
| 591 | if ((geometry.width != 0) && (geometry.width < image->columns)) |
| 592 | image->columns=geometry.width; |
| 593 | if (flags & HeightValue) |
| 594 | if ((geometry.height != 0) && (geometry.height < image->rows)) |
| 595 | image->rows=geometry.height; |
| 596 | } |
| cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 597 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 598 | if (status == MagickFalse) |
| 599 | return(DestroyImageList(image)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 600 | if (image->storage_class == PseudoClass) |
| 601 | { |
| 602 | size_t |
| 603 | length, |
| 604 | packet_size; |
| 605 | |
| 606 | unsigned char |
| 607 | *dib_colormap; |
| 608 | |
| 609 | /* |
| 610 | Read DIB raster colormap. |
| 611 | */ |
| cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 612 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 613 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 614 | length=(size_t) image->colors; |
| 615 | dib_colormap=(unsigned char *) AcquireQuantumMemory(length, |
| 616 | 4*sizeof(*dib_colormap)); |
| 617 | if (dib_colormap == (unsigned char *) NULL) |
| 618 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 619 | packet_size=4; |
| 620 | count=ReadBlob(image,packet_size*image->colors,dib_colormap); |
| 621 | if (count != (ssize_t) (packet_size*image->colors)) |
| 622 | ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile"); |
| 623 | p=dib_colormap; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 624 | for (i=0; i < (ssize_t) image->colors; i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 625 | { |
| 626 | image->colormap[i].blue=ScaleCharToQuantum(*p++); |
| 627 | image->colormap[i].green=ScaleCharToQuantum(*p++); |
| 628 | image->colormap[i].red=ScaleCharToQuantum(*p++); |
| 629 | if (packet_size == 4) |
| 630 | p++; |
| 631 | } |
| 632 | dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap); |
| 633 | } |
| 634 | /* |
| 635 | Read image data. |
| 636 | */ |
| 637 | if (dib_info.compression == BI_RLE4) |
| 638 | dib_info.bits_per_pixel<<=1; |
| 639 | bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32); |
| 640 | length=bytes_per_line*image->rows; |
| cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 641 | pixel_info=AcquireVirtualMemory((size_t) image->rows,MagickMax( |
| 642 | bytes_per_line,image->columns+256UL)*sizeof(*pixels)); |
| 643 | if (pixel_info == (MemoryInfo *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 644 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 645 | pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 646 | if ((dib_info.compression == BI_RGB) || |
| 647 | (dib_info.compression == BI_BITFIELDS)) |
| 648 | { |
| 649 | count=ReadBlob(image,length,pixels); |
| 650 | if (count != (ssize_t) (length)) |
| 651 | ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile"); |
| 652 | } |
| 653 | else |
| 654 | { |
| 655 | /* |
| 656 | Convert run-length encoded raster pixels. |
| 657 | */ |
| 658 | status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse, |
| 659 | pixels); |
| 660 | if (status == MagickFalse) |
| 661 | ThrowReaderException(CorruptImageError,"UnableToRunlengthDecodeImage"); |
| 662 | } |
| 663 | /* |
| 664 | Initialize image structure. |
| 665 | */ |
| 666 | image->units=PixelsPerCentimeterResolution; |
| cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 667 | image->resolution.x=(double) dib_info.x_pixels/100.0; |
| 668 | image->resolution.y=(double) dib_info.y_pixels/100.0; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 669 | /* |
| 670 | Convert DIB raster image to pixel packets. |
| 671 | */ |
| 672 | switch (dib_info.bits_per_pixel) |
| 673 | { |
| 674 | case 1: |
| 675 | { |
| 676 | /* |
| 677 | Convert bitmap scanline. |
| 678 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 679 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 680 | { |
| 681 | p=pixels+(image->rows-y-1)*bytes_per_line; |
| 682 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 683 | if (q == (Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 684 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 685 | for (x=0; x < ((ssize_t) image->columns-7); x+=8) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 686 | { |
| 687 | for (bit=0; bit < 8; bit++) |
| 688 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 689 | index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00); |
| 690 | SetPixelIndex(image,index,q); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 691 | q+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 692 | } |
| 693 | p++; |
| 694 | } |
| 695 | if ((image->columns % 8) != 0) |
| 696 | { |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 697 | for (bit=0; bit < (ssize_t) (image->columns % 8); bit++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 698 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 699 | index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00); |
| 700 | SetPixelIndex(image,index,q); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 701 | q+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 702 | } |
| 703 | p++; |
| 704 | } |
| 705 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 706 | break; |
| 707 | if (image->previous == (Image *) NULL) |
| 708 | { |
| 709 | status=SetImageProgress(image,LoadImageTag,image->rows-y-1, |
| 710 | image->rows); |
| 711 | if (status == MagickFalse) |
| 712 | break; |
| 713 | } |
| 714 | } |
| cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 715 | (void) SyncImage(image,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 716 | break; |
| 717 | } |
| 718 | case 4: |
| 719 | { |
| 720 | /* |
| 721 | Convert PseudoColor scanline. |
| 722 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 723 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 724 | { |
| 725 | p=pixels+(image->rows-y-1)*bytes_per_line; |
| 726 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 727 | if (q == (Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 728 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 729 | for (x=0; x < ((ssize_t) image->columns-1); x+=2) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 730 | { |
| cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 731 | index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 732 | SetPixelIndex(image,index,q); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 733 | q+=GetPixelChannels(image); |
| cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 734 | index=ConstrainColormapIndex(image,*p & 0xf,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 735 | SetPixelIndex(image,index,q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 736 | p++; |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 737 | q+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 738 | } |
| 739 | if ((image->columns % 2) != 0) |
| 740 | { |
| cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 741 | index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 742 | SetPixelIndex(image,index,q); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 743 | q+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 744 | p++; |
| 745 | } |
| 746 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 747 | break; |
| 748 | if (image->previous == (Image *) NULL) |
| 749 | { |
| 750 | status=SetImageProgress(image,LoadImageTag,image->rows-y-1, |
| 751 | image->rows); |
| 752 | if (status == MagickFalse) |
| 753 | break; |
| 754 | } |
| 755 | } |
| cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 756 | (void) SyncImage(image,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 757 | break; |
| 758 | } |
| 759 | case 8: |
| 760 | { |
| 761 | /* |
| 762 | Convert PseudoColor scanline. |
| 763 | */ |
| 764 | if ((dib_info.compression == BI_RLE8) || |
| 765 | (dib_info.compression == BI_RLE4)) |
| 766 | bytes_per_line=image->columns; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 767 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 768 | { |
| 769 | p=pixels+(image->rows-y-1)*bytes_per_line; |
| 770 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 771 | if (q == (Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 772 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 773 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 774 | { |
| cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 775 | index=ConstrainColormapIndex(image,*p,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 776 | SetPixelIndex(image,index,q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 777 | p++; |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 778 | q+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 779 | } |
| 780 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 781 | break; |
| 782 | if (image->previous == (Image *) NULL) |
| 783 | { |
| 784 | status=SetImageProgress(image,LoadImageTag,image->rows-y-1, |
| 785 | image->rows); |
| 786 | if (status == MagickFalse) |
| 787 | break; |
| 788 | } |
| 789 | } |
| cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 790 | (void) SyncImage(image,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 791 | break; |
| 792 | } |
| 793 | case 16: |
| 794 | { |
| 795 | unsigned short |
| 796 | word; |
| 797 | |
| 798 | /* |
| 799 | Convert PseudoColor scanline. |
| 800 | */ |
| 801 | image->storage_class=DirectClass; |
| 802 | if (dib_info.compression == BI_RLE8) |
| 803 | bytes_per_line=2*image->columns; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 804 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 805 | { |
| 806 | p=pixels+(image->rows-y-1)*bytes_per_line; |
| 807 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 808 | if (q == (Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 809 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 810 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 811 | { |
| 812 | word=(*p++); |
| 813 | word|=(*p++ << 8); |
| 814 | if (dib_info.red_mask == 0) |
| 815 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 816 | SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8( |
| 817 | (unsigned char) ((word >> 10) & 0x1f))),q); |
| 818 | SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8( |
| 819 | (unsigned char) ((word >> 5) & 0x1f))),q); |
| 820 | SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8( |
| 821 | (unsigned char) (word & 0x1f))),q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 822 | } |
| 823 | else |
| 824 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 825 | SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8( |
| 826 | (unsigned char) ((word >> 11) & 0x1f))),q); |
| 827 | SetPixelGreen(image,ScaleCharToQuantum(ScaleColor6to8( |
| 828 | (unsigned char) ((word >> 5) & 0x3f))),q); |
| 829 | SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8( |
| 830 | (unsigned char) (word & 0x1f))),q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 831 | } |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 832 | q+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 833 | } |
| 834 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 835 | break; |
| 836 | if (image->previous == (Image *) NULL) |
| 837 | { |
| 838 | status=SetImageProgress(image,LoadImageTag,image->rows-y-1, |
| 839 | image->rows); |
| 840 | if (status == MagickFalse) |
| 841 | break; |
| 842 | } |
| 843 | } |
| 844 | break; |
| 845 | } |
| 846 | case 24: |
| 847 | case 32: |
| 848 | { |
| 849 | /* |
| 850 | Convert DirectColor scanline. |
| 851 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 852 | for (y=(ssize_t) image->rows-1; y >= 0; y--) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 853 | { |
| 854 | p=pixels+(image->rows-y-1)*bytes_per_line; |
| 855 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 856 | if (q == (Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 857 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 858 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 859 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 860 | SetPixelBlue(image,ScaleCharToQuantum(*p++),q); |
| 861 | SetPixelGreen(image,ScaleCharToQuantum(*p++),q); |
| 862 | SetPixelRed(image,ScaleCharToQuantum(*p++),q); |
| cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 863 | if (image->alpha_trait != UndefinedPixelTrait) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 864 | SetPixelAlpha(image,ScaleCharToQuantum(*p++),q); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 865 | q+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 866 | } |
| 867 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 868 | break; |
| 869 | if (image->previous == (Image *) NULL) |
| 870 | { |
| 871 | status=SetImageProgress(image,LoadImageTag,image->rows-y-1, |
| 872 | image->rows); |
| 873 | if (status == MagickFalse) |
| 874 | break; |
| 875 | } |
| 876 | } |
| 877 | break; |
| 878 | } |
| 879 | default: |
| 880 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 881 | } |
| cristy | 0553bd5 | 2013-06-30 15:53:50 +0000 | [diff] [blame] | 882 | pixel_info=RelinquishVirtualMemory(pixel_info); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 883 | if (EOFBlob(image) != MagickFalse) |
| 884 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 885 | image->filename); |
| 886 | if (dib_info.height < 0) |
| 887 | { |
| 888 | Image |
| 889 | *flipped_image; |
| 890 | |
| 891 | /* |
| 892 | Correct image orientation. |
| 893 | */ |
| 894 | flipped_image=FlipImage(image,exception); |
| cristy | bbfd4cd | 2010-04-13 21:54:39 +0000 | [diff] [blame] | 895 | if (flipped_image != (Image *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 896 | { |
| cristy | bbfd4cd | 2010-04-13 21:54:39 +0000 | [diff] [blame] | 897 | DuplicateBlob(flipped_image,image); |
| 898 | image=DestroyImage(image); |
| 899 | image=flipped_image; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 900 | } |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 901 | } |
| 902 | (void) CloseBlob(image); |
| 903 | return(GetFirstImageInList(image)); |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 908 | % % |
| 909 | % % |
| 910 | % % |
| 911 | % R e g i s t e r D I B I m a g e % |
| 912 | % % |
| 913 | % % |
| 914 | % % |
| 915 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 916 | % |
| 917 | % RegisterDIBImage() adds attributes for the DIB image format to |
| 918 | % the list of supported formats. The attributes include the image format |
| 919 | % tag, a method to read and/or write the format, whether the format |
| 920 | % supports the saving of more than one frame to the same file or blob, |
| 921 | % whether the format supports native in-memory I/O, and a brief |
| 922 | % description of the format. |
| 923 | % |
| 924 | % The format of the RegisterDIBImage method is: |
| 925 | % |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 926 | % size_t RegisterDIBImage(void) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 927 | % |
| 928 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 929 | ModuleExport size_t RegisterDIBImage(void) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 930 | { |
| 931 | MagickInfo |
| 932 | *entry; |
| 933 | |
| 934 | entry=SetMagickInfo("DIB"); |
| 935 | entry->decoder=(DecodeImageHandler *) ReadDIBImage; |
| 936 | entry->encoder=(EncodeImageHandler *) WriteDIBImage; |
| 937 | entry->magick=(IsImageFormatHandler *) IsDIB; |
| dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame^] | 938 | entry->flags^=CoderAdjoinFlag; |
| 939 | entry->flags|=CoderStealthFlag; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 940 | entry->description=ConstantString( |
| 941 | "Microsoft Windows 3.X Packed Device-Independent Bitmap"); |
| 942 | entry->module=ConstantString("DIB"); |
| 943 | (void) RegisterMagickInfo(entry); |
| 944 | return(MagickImageCoderSignature); |
| 945 | } |
| 946 | |
| 947 | /* |
| 948 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 949 | % % |
| 950 | % % |
| 951 | % % |
| 952 | % U n r e g i s t e r D I B I m a g e % |
| 953 | % % |
| 954 | % % |
| 955 | % % |
| 956 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 957 | % |
| 958 | % UnregisterDIBImage() removes format registrations made by the |
| 959 | % DIB module from the list of supported formats. |
| 960 | % |
| 961 | % The format of the UnregisterDIBImage method is: |
| 962 | % |
| 963 | % UnregisterDIBImage(void) |
| 964 | % |
| 965 | */ |
| 966 | ModuleExport void UnregisterDIBImage(void) |
| 967 | { |
| 968 | (void) UnregisterMagickInfo("DIB"); |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 973 | % % |
| 974 | % % |
| 975 | % % |
| 976 | % W r i t e D I B I m a g e % |
| 977 | % % |
| 978 | % % |
| 979 | % % |
| 980 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 981 | % |
| 982 | % WriteDIBImage() writes an image in Microsoft Windows bitmap encoded |
| 983 | % image format. |
| 984 | % |
| 985 | % The format of the WriteDIBImage method is: |
| 986 | % |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 987 | % MagickBooleanType WriteDIBImage(const ImageInfo *image_info, |
| 988 | % Image *image,ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 989 | % |
| 990 | % A description of each parameter follows. |
| 991 | % |
| 992 | % o image_info: the image info. |
| 993 | % |
| 994 | % o image: The image. |
| 995 | % |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 996 | % o exception: return any errors or warnings in this structure. |
| 997 | % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 998 | */ |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 999 | static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image, |
| 1000 | ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1001 | { |
| 1002 | DIBInfo |
| 1003 | dib_info; |
| 1004 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1005 | MagickBooleanType |
| 1006 | status; |
| 1007 | |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1008 | register const Quantum |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1009 | *p; |
| 1010 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1011 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1012 | i, |
| 1013 | x; |
| 1014 | |
| 1015 | register unsigned char |
| 1016 | *q; |
| 1017 | |
| cristy | ddbc41b | 2011-04-24 14:27:48 +0000 | [diff] [blame] | 1018 | size_t |
| 1019 | bytes_per_line; |
| 1020 | |
| 1021 | ssize_t |
| 1022 | y; |
| 1023 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1024 | unsigned char |
| 1025 | *dib_data, |
| 1026 | *pixels; |
| 1027 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1028 | /* |
| 1029 | Open output image file. |
| 1030 | */ |
| 1031 | assert(image_info != (const ImageInfo *) NULL); |
| 1032 | assert(image_info->signature == MagickSignature); |
| 1033 | assert(image != (Image *) NULL); |
| 1034 | assert(image->signature == MagickSignature); |
| 1035 | if (image->debug != MagickFalse) |
| 1036 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 1037 | assert(exception != (ExceptionInfo *) NULL); |
| 1038 | assert(exception->signature == MagickSignature); |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1039 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1040 | if (status == MagickFalse) |
| 1041 | return(status); |
| 1042 | /* |
| 1043 | Initialize DIB raster file header. |
| 1044 | */ |
| cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 1045 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1046 | if (image->storage_class == DirectClass) |
| 1047 | { |
| 1048 | /* |
| 1049 | Full color DIB raster. |
| 1050 | */ |
| 1051 | dib_info.number_colors=0; |
| cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 1052 | dib_info.bits_per_pixel=(unsigned short) (image->alpha_trait ? 32 : 24); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1053 | } |
| 1054 | else |
| 1055 | { |
| 1056 | /* |
| 1057 | Colormapped DIB raster. |
| 1058 | */ |
| 1059 | dib_info.bits_per_pixel=8; |
| 1060 | if (image_info->depth > 8) |
| 1061 | dib_info.bits_per_pixel=16; |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1062 | if (IsImageMonochrome(image,exception) != MagickFalse) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1063 | dib_info.bits_per_pixel=1; |
| 1064 | dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 : |
| 1065 | (1UL << dib_info.bits_per_pixel); |
| 1066 | } |
| 1067 | bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32); |
| 1068 | dib_info.size=40; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1069 | dib_info.width=(ssize_t) image->columns; |
| 1070 | dib_info.height=(ssize_t) image->rows; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1071 | dib_info.planes=1; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1072 | dib_info.compression=(size_t) (dib_info.bits_per_pixel == 16 ? |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1073 | BI_BITFIELDS : BI_RGB); |
| 1074 | dib_info.image_size=bytes_per_line*image->rows; |
| 1075 | dib_info.x_pixels=75*39; |
| 1076 | dib_info.y_pixels=75*39; |
| 1077 | switch (image->units) |
| 1078 | { |
| 1079 | case UndefinedResolution: |
| 1080 | case PixelsPerInchResolution: |
| 1081 | { |
| cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1082 | dib_info.x_pixels=(size_t) (100.0*image->resolution.x/2.54); |
| 1083 | dib_info.y_pixels=(size_t) (100.0*image->resolution.y/2.54); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1084 | break; |
| 1085 | } |
| 1086 | case PixelsPerCentimeterResolution: |
| 1087 | { |
| cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1088 | dib_info.x_pixels=(size_t) (100.0*image->resolution.x); |
| 1089 | dib_info.y_pixels=(size_t) (100.0*image->resolution.y); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1090 | break; |
| 1091 | } |
| 1092 | } |
| 1093 | dib_info.colors_important=dib_info.number_colors; |
| 1094 | /* |
| 1095 | Convert MIFF to DIB raster pixels. |
| 1096 | */ |
| 1097 | pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size, |
| 1098 | sizeof(*pixels)); |
| 1099 | if (pixels == (unsigned char *) NULL) |
| 1100 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1101 | (void) ResetMagickMemory(pixels,0,dib_info.image_size); |
| 1102 | switch (dib_info.bits_per_pixel) |
| 1103 | { |
| 1104 | case 1: |
| 1105 | { |
| 1106 | register unsigned char |
| 1107 | bit, |
| 1108 | byte; |
| 1109 | |
| 1110 | /* |
| 1111 | Convert PseudoClass image to a DIB monochrome image. |
| 1112 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1113 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1114 | { |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1115 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1116 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1117 | break; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1118 | q=pixels+(image->rows-y-1)*bytes_per_line; |
| 1119 | bit=0; |
| 1120 | byte=0; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1121 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1122 | { |
| 1123 | byte<<=1; |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1124 | byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1125 | bit++; |
| 1126 | if (bit == 8) |
| 1127 | { |
| 1128 | *q++=byte; |
| 1129 | bit=0; |
| 1130 | byte=0; |
| 1131 | } |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1132 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1133 | } |
| 1134 | if (bit != 0) |
| 1135 | { |
| 1136 | *q++=(unsigned char) (byte << (8-bit)); |
| 1137 | x++; |
| 1138 | } |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1139 | for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1140 | *q++=0x00; |
| cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1141 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| cristy | ddbc41b | 2011-04-24 14:27:48 +0000 | [diff] [blame] | 1142 | image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1143 | if (status == MagickFalse) |
| 1144 | break; |
| 1145 | } |
| 1146 | break; |
| 1147 | } |
| 1148 | case 8: |
| 1149 | { |
| 1150 | /* |
| 1151 | Convert PseudoClass packet to DIB pixel. |
| 1152 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1153 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1154 | { |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1155 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1156 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1157 | break; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1158 | q=pixels+(image->rows-y-1)*bytes_per_line; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1159 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1160 | { |
| 1161 | *q++=(unsigned char) GetPixelIndex(image,p); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1162 | p+=GetPixelChannels(image); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1163 | } |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1164 | for ( ; x < (ssize_t) bytes_per_line; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1165 | *q++=0x00; |
| cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1166 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| cristy | ddbc41b | 2011-04-24 14:27:48 +0000 | [diff] [blame] | 1167 | image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1168 | if (status == MagickFalse) |
| 1169 | break; |
| 1170 | } |
| 1171 | break; |
| 1172 | } |
| 1173 | case 16: |
| 1174 | { |
| 1175 | unsigned short |
| 1176 | word; |
| 1177 | /* |
| cristy | ca793d7 | 2015-01-23 13:03:55 +0000 | [diff] [blame] | 1178 | Convert PseudoClass packet to DIB pixel. |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1179 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1180 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1181 | { |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1182 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1183 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1184 | break; |
| 1185 | q=pixels+(image->rows-y-1)*bytes_per_line; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1186 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1187 | { |
| 1188 | word=(unsigned short) ((ScaleColor8to5((unsigned char) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1189 | ScaleQuantumToChar(GetPixelRed(image,p))) << 11) | (ScaleColor8to6( |
| 1190 | (unsigned char) ScaleQuantumToChar(GetPixelGreen(image,p))) << 5) | |
| 1191 | (ScaleColor8to5((unsigned char) ScaleQuantumToChar((unsigned char) |
| 1192 | GetPixelBlue(image,p)) << 0))); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1193 | *q++=(unsigned char)(word & 0xff); |
| 1194 | *q++=(unsigned char)(word >> 8); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1195 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1196 | } |
| cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 1197 | for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1198 | *q++=0x00; |
| cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1199 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| cristy | ddbc41b | 2011-04-24 14:27:48 +0000 | [diff] [blame] | 1200 | image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1201 | if (status == MagickFalse) |
| 1202 | break; |
| 1203 | } |
| 1204 | break; |
| 1205 | } |
| 1206 | case 24: |
| 1207 | case 32: |
| 1208 | { |
| 1209 | /* |
| 1210 | Convert DirectClass packet to DIB RGB pixel. |
| 1211 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1212 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1213 | { |
| cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 1214 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1215 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1216 | break; |
| 1217 | q=pixels+(image->rows-y-1)*bytes_per_line; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1218 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1219 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1220 | *q++=ScaleQuantumToChar(GetPixelBlue(image,p)); |
| 1221 | *q++=ScaleQuantumToChar(GetPixelGreen(image,p)); |
| 1222 | *q++=ScaleQuantumToChar(GetPixelRed(image,p)); |
| cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 1223 | if (image->alpha_trait != UndefinedPixelTrait) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1224 | *q++=ScaleQuantumToChar(GetPixelAlpha(image,p)); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1225 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1226 | } |
| 1227 | if (dib_info.bits_per_pixel == 24) |
| cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 1228 | for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1229 | *q++=0x00; |
| cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1230 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| cristy | ddbc41b | 2011-04-24 14:27:48 +0000 | [diff] [blame] | 1231 | image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1232 | if (status == MagickFalse) |
| 1233 | break; |
| 1234 | } |
| 1235 | break; |
| 1236 | } |
| 1237 | } |
| 1238 | if (dib_info.bits_per_pixel == 8) |
| 1239 | if (image_info->compression != NoCompression) |
| 1240 | { |
| 1241 | size_t |
| 1242 | length; |
| 1243 | |
| 1244 | /* |
| 1245 | Convert run-length encoded raster pixels. |
| 1246 | */ |
| 1247 | length=2UL*(bytes_per_line+2UL)+2UL; |
| 1248 | dib_data=(unsigned char *) AcquireQuantumMemory(length, |
| 1249 | (image->rows+2UL)*sizeof(*dib_data)); |
| cristy | 4d0ca34 | 2014-05-01 00:42:09 +0000 | [diff] [blame] | 1250 | if (dib_data == (unsigned char *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1251 | { |
| 1252 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 1253 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1254 | } |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1255 | dib_info.image_size=(size_t) EncodeImage(image,bytes_per_line, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1256 | pixels,dib_data); |
| 1257 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 1258 | pixels=dib_data; |
| 1259 | dib_info.compression = BI_RLE8; |
| 1260 | } |
| 1261 | /* |
| 1262 | Write DIB header. |
| 1263 | */ |
| cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 1264 | (void) WriteBlobLSBLong(image,(unsigned int) dib_info.size); |
| cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 1265 | (void) WriteBlobLSBLong(image,dib_info.width); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1266 | (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height); |
| 1267 | (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes); |
| 1268 | (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel); |
| cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 1269 | (void) WriteBlobLSBLong(image,(unsigned int) dib_info.compression); |
| 1270 | (void) WriteBlobLSBLong(image,(unsigned int) dib_info.image_size); |
| 1271 | (void) WriteBlobLSBLong(image,(unsigned int) dib_info.x_pixels); |
| 1272 | (void) WriteBlobLSBLong(image,(unsigned int) dib_info.y_pixels); |
| 1273 | (void) WriteBlobLSBLong(image,(unsigned int) dib_info.number_colors); |
| 1274 | (void) WriteBlobLSBLong(image,(unsigned int) dib_info.colors_important); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1275 | if (image->storage_class == PseudoClass) |
| 1276 | { |
| 1277 | if (dib_info.bits_per_pixel <= 8) |
| 1278 | { |
| 1279 | unsigned char |
| 1280 | *dib_colormap; |
| 1281 | |
| 1282 | /* |
| 1283 | Dump colormap to file. |
| 1284 | */ |
| 1285 | dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t) |
| cristy | 4d0ca34 | 2014-05-01 00:42:09 +0000 | [diff] [blame] | 1286 | (1UL << dib_info.bits_per_pixel),4*sizeof(*dib_colormap)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1287 | if (dib_colormap == (unsigned char *) NULL) |
| 1288 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1289 | q=dib_colormap; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1290 | for (i=0; i < (ssize_t) MagickMin(image->colors,dib_info.number_colors); i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1291 | { |
| 1292 | *q++=ScaleQuantumToChar(image->colormap[i].blue); |
| 1293 | *q++=ScaleQuantumToChar(image->colormap[i].green); |
| 1294 | *q++=ScaleQuantumToChar(image->colormap[i].red); |
| 1295 | *q++=(Quantum) 0x0; |
| 1296 | } |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1297 | for ( ; i < (ssize_t) (1L << dib_info.bits_per_pixel); i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1298 | { |
| 1299 | *q++=(Quantum) 0x0; |
| 1300 | *q++=(Quantum) 0x0; |
| 1301 | *q++=(Quantum) 0x0; |
| 1302 | *q++=(Quantum) 0x0; |
| 1303 | } |
| 1304 | (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)), |
| 1305 | dib_colormap); |
| 1306 | dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap); |
| 1307 | } |
| 1308 | else |
| 1309 | if ((dib_info.bits_per_pixel == 16) && |
| 1310 | (dib_info.compression == BI_BITFIELDS)) |
| 1311 | { |
| 1312 | (void) WriteBlobLSBLong(image,0xf800); |
| 1313 | (void) WriteBlobLSBLong(image,0x07e0); |
| 1314 | (void) WriteBlobLSBLong(image,0x001f); |
| 1315 | } |
| 1316 | } |
| 1317 | (void) WriteBlob(image,dib_info.image_size,pixels); |
| 1318 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 1319 | (void) CloseBlob(image); |
| 1320 | return(MagickTrue); |
| 1321 | } |