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