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