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