]> granicus.if.org Git - imagemagick/blob - coders/dib.c
Horizon validity (anti-aliased) added to Plane2Cylinder
[imagemagick] / coders / dib.c
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 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
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 \f
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"
47 #include "magick/color-private.h"
48 #include "magick/colormap.h"
49 #include "magick/colormap-private.h"
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 \f
69 /*
70   Typedef declarations.
71 */
72 typedef struct _DIBInfo
73 {
74   size_t
75     size;
76
77   ssize_t
78     width,
79     height;
80
81   unsigned short
82     planes,
83     bits_per_pixel;
84
85   size_t
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
97   ssize_t
98     colorspace;
99
100   PointInfo
101     red_primary,
102     green_primary,
103     blue_primary,
104     gamma_scale;
105 } DIBInfo;
106 \f
107 /*
108   Forward declarations.
109 */
110 static MagickBooleanType
111   WriteDIBImage(const ImageInfo *,Image *);
112 \f
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 {
154 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
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
164   ssize_t
165     y;
166
167   register ssize_t
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;
189   for (y=0; y < (ssize_t) image->rows; )
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 \f
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,
303 %      const size_t bytes_per_line,const unsigned char *pixels,
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 */
319 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
320   const unsigned char *pixels,unsigned char *compressed_pixels)
321 {
322   ssize_t
323     y;
324
325   register const unsigned char
326     *p;
327
328   register ssize_t
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;
347   for (y=0; y < (ssize_t) image->rows; y++)
348   {
349     for (x=0; x < (ssize_t) bytes_per_line; x+=i)
350     {
351       /*
352         Determine runlength.
353       */
354       for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
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 \f
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 \f
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
438 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
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
463   ssize_t
464     bit,
465     y;
466
467   MagickBooleanType
468     status;
469
470   register IndexPacket
471     *indexes;
472
473   register ssize_t
474     x;
475
476   register PixelPacket
477     *q;
478
479   register ssize_t
480     i;
481
482   register unsigned char
483     *p;
484
485   size_t
486     bytes_per_line,
487     length;
488
489   ssize_t
490     count;
491
492   unsigned char
493     *pixels;
494
495   /*
496     Open image file.
497   */
498   assert(image_info != (const ImageInfo *) NULL);
499   assert(image_info->signature == MagickSignature);
500   if (image_info->debug != MagickFalse)
501     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
502       image_info->filename);
503   assert(exception != (ExceptionInfo *) NULL);
504   assert(exception->signature == MagickSignature);
505   image=AcquireImage(image_info);
506   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
507   if (status == MagickFalse)
508     {
509       image=DestroyImageList(image);
510       return((Image *) NULL);
511     }
512   /*
513     Determine if this a DIB file.
514   */
515   (void) ResetMagickMemory(&dib_info,0,sizeof(dib_info));
516   dib_info.size=ReadBlobLSBLong(image);
517   if (dib_info.size!=40)
518     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
519   /*
520     Microsoft Windows 3.X DIB image file.
521   */
522   dib_info.width=(short) ReadBlobLSBLong(image);
523   dib_info.height=(short) ReadBlobLSBLong(image);
524   dib_info.planes=ReadBlobLSBShort(image);
525   dib_info.bits_per_pixel=ReadBlobLSBShort(image);
526   dib_info.compression=ReadBlobLSBLong(image);
527   dib_info.image_size=ReadBlobLSBLong(image);
528   dib_info.x_pixels=ReadBlobLSBLong(image);
529   dib_info.y_pixels=ReadBlobLSBLong(image);
530   dib_info.number_colors=ReadBlobLSBLong(image);
531   dib_info.colors_important=ReadBlobLSBLong(image);
532   if ((dib_info.compression == BI_BITFIELDS) &&
533       ((dib_info.bits_per_pixel == 16) || (dib_info.bits_per_pixel == 32)))
534     {
535       dib_info.red_mask=ReadBlobLSBLong(image);
536       dib_info.green_mask=ReadBlobLSBLong(image);
537       dib_info.blue_mask=ReadBlobLSBLong(image);
538     }
539   image->matte=dib_info.bits_per_pixel == 32 ? MagickTrue : MagickFalse;
540   image->columns=(size_t) MagickAbsoluteValue(dib_info.width);
541   image->rows=(size_t) MagickAbsoluteValue(dib_info.height);
542   image->depth=8;
543   if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
544     {
545       size_t
546         one;
547
548       image->storage_class=PseudoClass;
549       image->colors=dib_info.number_colors;
550       one=1;
551       if (image->colors == 0)
552         image->colors=one << dib_info.bits_per_pixel;
553     }
554   if (image_info->size)
555     {
556       RectangleInfo
557         geometry;
558
559       MagickStatusType
560         flags;
561
562       flags=ParseAbsoluteGeometry(image_info->size,&geometry);
563       if (flags & WidthValue)
564         if ((geometry.width != 0) && (geometry.width < image->columns))
565           image->columns=geometry.width;
566       if (flags & HeightValue)
567         if ((geometry.height != 0) && (geometry.height < image->rows))
568           image->rows=geometry.height;
569     }
570   if (image->storage_class == PseudoClass)
571     {
572       size_t
573         length,
574         packet_size;
575
576       unsigned char
577         *dib_colormap;
578
579       /*
580         Read DIB raster colormap.
581       */
582       if (AcquireImageColormap(image,image->colors) == MagickFalse)
583         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
584       length=(size_t) image->colors;
585       dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
586         4*sizeof(*dib_colormap));
587       if (dib_colormap == (unsigned char *) NULL)
588         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
589       packet_size=4;
590       count=ReadBlob(image,packet_size*image->colors,dib_colormap);
591       if (count != (ssize_t) (packet_size*image->colors))
592         ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
593       p=dib_colormap;
594       for (i=0; i < (ssize_t) image->colors; i++)
595       {
596         image->colormap[i].blue=ScaleCharToQuantum(*p++);
597         image->colormap[i].green=ScaleCharToQuantum(*p++);
598         image->colormap[i].red=ScaleCharToQuantum(*p++);
599         if (packet_size == 4)
600           p++;
601       }
602       dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
603     }
604   /*
605     Read image data.
606   */
607   if (dib_info.compression == BI_RLE4)
608     dib_info.bits_per_pixel<<=1;
609   bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
610   length=bytes_per_line*image->rows;
611   pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
612     MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
613   if (pixels == (unsigned char *) NULL)
614     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
615   if ((dib_info.compression == BI_RGB) ||
616       (dib_info.compression == BI_BITFIELDS))
617     {
618       count=ReadBlob(image,length,pixels);
619       if (count != (ssize_t) (length))
620         ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
621     }
622   else
623     {
624       /*
625         Convert run-length encoded raster pixels.
626       */
627       status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
628         pixels);
629       if (status == MagickFalse)
630         ThrowReaderException(CorruptImageError,"UnableToRunlengthDecodeImage");
631     }
632   /*
633     Initialize image structure.
634   */
635   image->units=PixelsPerCentimeterResolution;
636   image->x_resolution=(double) dib_info.x_pixels/100.0;
637   image->y_resolution=(double) dib_info.y_pixels/100.0;
638   /*
639     Convert DIB raster image to pixel packets.
640   */
641   switch (dib_info.bits_per_pixel)
642   {
643     case 1:
644     {
645       /*
646         Convert bitmap scanline.
647       */
648       for (y=(ssize_t) image->rows-1; y >= 0; y--)
649       {
650         p=pixels+(image->rows-y-1)*bytes_per_line;
651         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
652         if (q == (PixelPacket *) NULL)
653           break;
654         indexes=GetAuthenticIndexQueue(image);
655         for (x=0; x < ((ssize_t) image->columns-7); x+=8)
656         {
657           for (bit=0; bit < 8; bit++)
658           {
659             index=(IndexPacket) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
660             SetIndexPixelComponent(indexes+x+bit,index);
661           }
662           p++;
663         }
664         if ((image->columns % 8) != 0)
665           {
666             for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
667             {
668               index=(IndexPacket) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
669               SetIndexPixelComponent(indexes+x+bit,index);
670             }
671             p++;
672           }
673         if (SyncAuthenticPixels(image,exception) == MagickFalse)
674           break;
675         if (image->previous == (Image *) NULL)
676           {
677             status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
678               image->rows);
679             if (status == MagickFalse)
680               break;
681           }
682       }
683       (void) SyncImage(image);
684       break;
685     }
686     case 4:
687     {
688       /*
689         Convert PseudoColor scanline.
690       */
691       for (y=(ssize_t) image->rows-1; y >= 0; y--)
692       {
693         p=pixels+(image->rows-y-1)*bytes_per_line;
694         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
695         if (q == (PixelPacket *) NULL)
696           break;
697         indexes=GetAuthenticIndexQueue(image);
698         for (x=0; x < ((ssize_t) image->columns-1); x+=2)
699         {
700           index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
701           SetIndexPixelComponent(indexes+x,index);
702           index=ConstrainColormapIndex(image,*p & 0xf);
703           SetIndexPixelComponent(indexes+x+1,index);
704           p++;
705         }
706         if ((image->columns % 2) != 0)
707           {
708             index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
709             SetIndexPixelComponent(indexes+x,index);
710             p++;
711           }
712         if (SyncAuthenticPixels(image,exception) == MagickFalse)
713           break;
714         if (image->previous == (Image *) NULL)
715           {
716             status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
717               image->rows);
718             if (status == MagickFalse)
719               break;
720           }
721       }
722       (void) SyncImage(image);
723       break;
724     }
725     case 8:
726     {
727       /*
728         Convert PseudoColor scanline.
729       */
730       if ((dib_info.compression == BI_RLE8) ||
731           (dib_info.compression == BI_RLE4))
732         bytes_per_line=image->columns;
733       for (y=(ssize_t) image->rows-1; y >= 0; y--)
734       {
735         p=pixels+(image->rows-y-1)*bytes_per_line;
736         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
737         if (q == (PixelPacket *) NULL)
738           break;
739         indexes=GetAuthenticIndexQueue(image);
740         for (x=0; x < (ssize_t) image->columns; x++)
741         {
742           index=ConstrainColormapIndex(image,*p);
743           SetIndexPixelComponent(indexes+x,index);
744           p++;
745           q++;
746         }
747         if (SyncAuthenticPixels(image,exception) == MagickFalse)
748           break;
749         if (image->previous == (Image *) NULL)
750           {
751             status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
752               image->rows);
753             if (status == MagickFalse)
754               break;
755           }
756       }
757       (void) SyncImage(image);
758       break;
759     }
760     case 16:
761     {
762       unsigned short
763         word;
764
765       /*
766         Convert PseudoColor scanline.
767       */
768       image->storage_class=DirectClass;
769       if (dib_info.compression == BI_RLE8)
770         bytes_per_line=2*image->columns;
771       for (y=(ssize_t) image->rows-1; y >= 0; y--)
772       {
773         p=pixels+(image->rows-y-1)*bytes_per_line;
774         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
775         if (q == (PixelPacket *) NULL)
776           break;
777         for (x=0; x < (ssize_t) image->columns; x++)
778         {
779           word=(*p++);
780           word|=(*p++ << 8);
781           if (dib_info.red_mask == 0)
782             {
783               SetRedPixelComponent(q,ScaleCharToQuantum(ScaleColor5to8(
784                 (unsigned char) ((word >> 10) & 0x1f))));
785               SetGreenPixelComponent(q,ScaleCharToQuantum(ScaleColor5to8(
786                 (unsigned char) ((word >> 5) & 0x1f))));
787               SetBluePixelComponent(q,ScaleCharToQuantum(ScaleColor5to8(
788                 (unsigned char) (word & 0x1f))));
789             }
790           else
791             {
792               SetRedPixelComponent(q,ScaleCharToQuantum(ScaleColor5to8(
793                 (unsigned char) ((word >> 11) & 0x1f))));
794               SetGreenPixelComponent(q,ScaleCharToQuantum(ScaleColor6to8(
795                 (unsigned char) ((word >> 5) & 0x3f))));
796               SetBluePixelComponent(q,ScaleCharToQuantum(ScaleColor5to8(
797                 (unsigned char) (word & 0x1f))));
798             }
799           q++;
800         }
801         if (SyncAuthenticPixels(image,exception) == MagickFalse)
802           break;
803         if (image->previous == (Image *) NULL)
804           {
805             status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
806               image->rows);
807             if (status == MagickFalse)
808               break;
809           }
810       }
811       break;
812     }
813     case 24:
814     case 32:
815     {
816       /*
817         Convert DirectColor scanline.
818       */
819       for (y=(ssize_t) image->rows-1; y >= 0; y--)
820       {
821         p=pixels+(image->rows-y-1)*bytes_per_line;
822         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
823         if (q == (PixelPacket *) NULL)
824           break;
825         for (x=0; x < (ssize_t) image->columns; x++)
826         {
827           SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
828           SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
829           SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
830           if (image->matte != MagickFalse)
831             SetOpacityPixelComponent(q,ScaleCharToQuantum(*p++));
832           q++;
833         }
834         if (SyncAuthenticPixels(image,exception) == MagickFalse)
835           break;
836         if (image->previous == (Image *) NULL)
837           {
838             status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
839               image->rows);
840             if (status == MagickFalse)
841               break;
842           }
843       }
844       break;
845     }
846     default:
847       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
848   }
849   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
850   if (EOFBlob(image) != MagickFalse)
851     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
852       image->filename);
853   if (dib_info.height < 0)
854     {
855       Image
856         *flipped_image;
857
858       /*
859         Correct image orientation.
860       */
861       flipped_image=FlipImage(image,exception);
862       if (flipped_image != (Image *) NULL)
863         {
864           DuplicateBlob(flipped_image,image);
865           image=DestroyImage(image);
866           image=flipped_image;
867         }
868     }
869   (void) CloseBlob(image);
870   return(GetFirstImageInList(image));
871 }
872 \f
873 /*
874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
875 %                                                                             %
876 %                                                                             %
877 %                                                                             %
878 %   R e g i s t e r D I B I m a g e                                           %
879 %                                                                             %
880 %                                                                             %
881 %                                                                             %
882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
883 %
884 %  RegisterDIBImage() adds attributes for the DIB image format to
885 %  the list of supported formats.  The attributes include the image format
886 %  tag, a method to read and/or write the format, whether the format
887 %  supports the saving of more than one frame to the same file or blob,
888 %  whether the format supports native in-memory I/O, and a brief
889 %  description of the format.
890 %
891 %  The format of the RegisterDIBImage method is:
892 %
893 %      size_t RegisterDIBImage(void)
894 %
895 */
896 ModuleExport size_t RegisterDIBImage(void)
897 {
898   MagickInfo
899     *entry;
900
901   entry=SetMagickInfo("DIB");
902   entry->decoder=(DecodeImageHandler *) ReadDIBImage;
903   entry->encoder=(EncodeImageHandler *) WriteDIBImage;
904   entry->magick=(IsImageFormatHandler *) IsDIB;
905   entry->adjoin=MagickFalse;
906   entry->stealth=MagickTrue;
907   entry->description=ConstantString(
908     "Microsoft Windows 3.X Packed Device-Independent Bitmap");
909   entry->module=ConstantString("DIB");
910   (void) RegisterMagickInfo(entry);
911   return(MagickImageCoderSignature);
912 }
913 \f
914 /*
915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
916 %                                                                             %
917 %                                                                             %
918 %                                                                             %
919 %   U n r e g i s t e r D I B I m a g e                                       %
920 %                                                                             %
921 %                                                                             %
922 %                                                                             %
923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924 %
925 %  UnregisterDIBImage() removes format registrations made by the
926 %  DIB module from the list of supported formats.
927 %
928 %  The format of the UnregisterDIBImage method is:
929 %
930 %      UnregisterDIBImage(void)
931 %
932 */
933 ModuleExport void UnregisterDIBImage(void)
934 {
935   (void) UnregisterMagickInfo("DIB");
936 }
937 \f
938 /*
939 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940 %                                                                             %
941 %                                                                             %
942 %                                                                             %
943 %   W r i t e D I B I m a g e                                                 %
944 %                                                                             %
945 %                                                                             %
946 %                                                                             %
947 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
948 %
949 %  WriteDIBImage() writes an image in Microsoft Windows bitmap encoded
950 %  image format.
951 %
952 %  The format of the WriteDIBImage method is:
953 %
954 %      MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
955 %
956 %  A description of each parameter follows.
957 %
958 %    o image_info: the image info.
959 %
960 %    o image:  The image.
961 %
962 */
963 static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
964 {
965   DIBInfo
966     dib_info;
967
968   MagickBooleanType
969     status;
970
971   register const IndexPacket
972     *indexes;
973
974   register const PixelPacket
975     *p;
976
977   register ssize_t
978     i,
979     x;
980
981   register unsigned char
982     *q;
983
984   size_t
985     bytes_per_line;
986
987   ssize_t
988     y;
989
990   unsigned char
991     *dib_data,
992     *pixels;
993
994   /*
995     Open output image file.
996   */
997   assert(image_info != (const ImageInfo *) NULL);
998   assert(image_info->signature == MagickSignature);
999   assert(image != (Image *) NULL);
1000   assert(image->signature == MagickSignature);
1001   if (image->debug != MagickFalse)
1002     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1003   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1004   if (status == MagickFalse)
1005     return(status);
1006   /*
1007     Initialize DIB raster file header.
1008   */
1009   if (image->colorspace != RGBColorspace)
1010     (void) TransformImageColorspace(image,RGBColorspace);
1011   if (image->storage_class == DirectClass)
1012     {
1013       /*
1014         Full color DIB raster.
1015       */
1016       dib_info.number_colors=0;
1017       dib_info.bits_per_pixel=(unsigned short) (image->matte ? 32 : 24);
1018     }
1019   else
1020     {
1021       /*
1022         Colormapped DIB raster.
1023       */
1024       dib_info.bits_per_pixel=8;
1025       if (image_info->depth > 8)
1026         dib_info.bits_per_pixel=16;
1027       if (IsMonochromeImage(image,&image->exception) != MagickFalse)
1028         dib_info.bits_per_pixel=1;
1029       dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
1030         (1UL << dib_info.bits_per_pixel);
1031     }
1032   bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1033   dib_info.size=40;
1034   dib_info.width=(ssize_t) image->columns;
1035   dib_info.height=(ssize_t) image->rows;
1036   dib_info.planes=1;
1037   dib_info.compression=(size_t) (dib_info.bits_per_pixel == 16 ?
1038     BI_BITFIELDS : BI_RGB);
1039   dib_info.image_size=bytes_per_line*image->rows;
1040   dib_info.x_pixels=75*39;
1041   dib_info.y_pixels=75*39;
1042   switch (image->units)
1043   {
1044     case UndefinedResolution:
1045     case PixelsPerInchResolution:
1046     {
1047       dib_info.x_pixels=(size_t) (100.0*image->x_resolution/2.54);
1048       dib_info.y_pixels=(size_t) (100.0*image->y_resolution/2.54);
1049       break;
1050     }
1051     case PixelsPerCentimeterResolution:
1052     {
1053       dib_info.x_pixels=(size_t) (100.0*image->x_resolution);
1054       dib_info.y_pixels=(size_t) (100.0*image->y_resolution);
1055       break;
1056     }
1057   }
1058   dib_info.colors_important=dib_info.number_colors;
1059   /*
1060     Convert MIFF to DIB raster pixels.
1061   */
1062   pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size,
1063     sizeof(*pixels));
1064   if (pixels == (unsigned char *) NULL)
1065     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1066   (void) ResetMagickMemory(pixels,0,dib_info.image_size);
1067   switch (dib_info.bits_per_pixel)
1068   {
1069     case 1:
1070     {
1071       register unsigned char
1072         bit,
1073         byte;
1074
1075       /*
1076         Convert PseudoClass image to a DIB monochrome image.
1077       */
1078       for (y=0; y < (ssize_t) image->rows; y++)
1079       {
1080         p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1081         if (p == (const PixelPacket *) NULL)
1082           break;
1083         indexes=GetVirtualIndexQueue(image);
1084         q=pixels+(image->rows-y-1)*bytes_per_line;
1085         bit=0;
1086         byte=0;
1087         for (x=0; x < (ssize_t) image->columns; x++)
1088         {
1089           byte<<=1;
1090           byte|=GetIndexPixelComponent(indexes+x) != 0 ? 0x01 : 0x00;
1091           bit++;
1092           if (bit == 8)
1093             {
1094               *q++=byte;
1095               bit=0;
1096               byte=0;
1097             }
1098            p++;
1099          }
1100          if (bit != 0)
1101            {
1102              *q++=(unsigned char) (byte << (8-bit));
1103              x++;
1104            }
1105         for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1106           *q++=0x00;
1107         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1108           image->rows);
1109         if (status == MagickFalse)
1110           break;
1111       }
1112       break;
1113     }
1114     case 8:
1115     {
1116       /*
1117         Convert PseudoClass packet to DIB pixel.
1118       */
1119       for (y=0; y < (ssize_t) image->rows; y++)
1120       {
1121         p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1122         if (p == (const PixelPacket *) NULL)
1123           break;
1124         indexes=GetVirtualIndexQueue(image);
1125         q=pixels+(image->rows-y-1)*bytes_per_line;
1126         for (x=0; x < (ssize_t) image->columns; x++)
1127           *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
1128         for ( ; x < (ssize_t) bytes_per_line; x++)
1129           *q++=0x00;
1130         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1131           image->rows);
1132         if (status == MagickFalse)
1133           break;
1134       }
1135       break;
1136     }
1137     case 16:
1138     {
1139       unsigned short
1140         word;
1141       /*
1142         Convert PseudoClass packet to DIB pixel. 
1143       */
1144       for (y=0; y < (ssize_t) image->rows; y++)
1145       {
1146         p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1147         if (p == (const PixelPacket *) NULL)
1148           break;
1149         q=pixels+(image->rows-y-1)*bytes_per_line;
1150         for (x=0; x < (ssize_t) image->columns; x++)
1151         {
1152           word=(unsigned short) ((ScaleColor8to5((unsigned char)
1153             ScaleQuantumToChar(GetRedPixelComponent(p))) << 11) |
1154             (ScaleColor8to6((unsigned char) ScaleQuantumToChar(
1155             GetGreenPixelComponent(p))) << 5) | (ScaleColor8to5((unsigned char)
1156             ScaleQuantumToChar((unsigned char) GetBluePixelComponent(p)) <<
1157             0)));
1158           *q++=(unsigned char)(word & 0xff);
1159           *q++=(unsigned char)(word >> 8);
1160           p++;
1161         }
1162         for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++)
1163           *q++=0x00;
1164         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1165           image->rows);
1166         if (status == MagickFalse)
1167           break;
1168       }
1169       break;
1170     }
1171     case 24:
1172     case 32:
1173     {
1174       /*
1175         Convert DirectClass packet to DIB RGB pixel.
1176       */
1177       for (y=0; y < (ssize_t) image->rows; y++)
1178       {
1179         p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1180         if (p == (const PixelPacket *) NULL)
1181           break;
1182         q=pixels+(image->rows-y-1)*bytes_per_line;
1183         for (x=0; x < (ssize_t) image->columns; x++)
1184         {
1185           *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1186           *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1187           *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1188           if (image->matte != MagickFalse)
1189             *q++=ScaleQuantumToChar(GetOpacityPixelComponent(p));
1190           p++;
1191         }
1192         if (dib_info.bits_per_pixel == 24)
1193           for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++)
1194             *q++=0x00;
1195         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1196           image->rows);
1197         if (status == MagickFalse)
1198           break;
1199       }
1200       break;
1201     }
1202   }
1203   if (dib_info.bits_per_pixel == 8)
1204     if (image_info->compression != NoCompression)
1205       {
1206         size_t
1207           length;
1208
1209         /*
1210           Convert run-length encoded raster pixels.
1211         */
1212         length=2UL*(bytes_per_line+2UL)+2UL;
1213         dib_data=(unsigned char *) AcquireQuantumMemory(length,
1214           (image->rows+2UL)*sizeof(*dib_data));
1215         if (pixels == (unsigned char *) NULL)
1216           {
1217             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1218             ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1219           }
1220         dib_info.image_size=(size_t) EncodeImage(image,bytes_per_line,
1221           pixels,dib_data);
1222         pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1223         pixels=dib_data;
1224         dib_info.compression = BI_RLE8;
1225       }
1226   /*
1227     Write DIB header.
1228   */
1229   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.size);
1230   (void) WriteBlobLSBLong(image,dib_info.width);
1231   (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1232   (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1233   (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1234   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.compression);
1235   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.image_size);
1236   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.x_pixels);
1237   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.y_pixels);
1238   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.number_colors);
1239   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.colors_important);
1240   if (image->storage_class == PseudoClass)
1241     {
1242       if (dib_info.bits_per_pixel <= 8)
1243         {
1244           unsigned char
1245             *dib_colormap;
1246
1247           /*
1248             Dump colormap to file.
1249           */
1250           dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1251             (1UL << dib_info.bits_per_pixel),4*sizeof(dib_colormap));
1252           if (dib_colormap == (unsigned char *) NULL)
1253             ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1254           q=dib_colormap;
1255           for (i=0; i < (ssize_t) MagickMin(image->colors,dib_info.number_colors); i++)
1256           {
1257             *q++=ScaleQuantumToChar(image->colormap[i].blue);
1258             *q++=ScaleQuantumToChar(image->colormap[i].green);
1259             *q++=ScaleQuantumToChar(image->colormap[i].red);
1260             *q++=(Quantum) 0x0;
1261           }
1262           for ( ; i < (ssize_t) (1L << dib_info.bits_per_pixel); i++)
1263           {
1264             *q++=(Quantum) 0x0;
1265             *q++=(Quantum) 0x0;
1266             *q++=(Quantum) 0x0;
1267             *q++=(Quantum) 0x0;
1268           }
1269           (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1270             dib_colormap);
1271           dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1272         }
1273       else
1274         if ((dib_info.bits_per_pixel == 16) &&
1275             (dib_info.compression == BI_BITFIELDS))
1276           {
1277             (void) WriteBlobLSBLong(image,0xf800);
1278             (void) WriteBlobLSBLong(image,0x07e0);
1279             (void) WriteBlobLSBLong(image,0x001f);
1280           }
1281     }
1282   (void) WriteBlob(image,dib_info.image_size,pixels);
1283   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1284   (void) CloseBlob(image);
1285   return(MagickTrue);
1286 }