]> granicus.if.org Git - imagemagick/blob - coders/pict.c
(no commit message)
[imagemagick] / coders / pict.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                        PPPP   IIIII   CCCC  TTTTT                           %
7 %                        P   P    I    C        T                             %
8 %                        PPPP     I    C        T                             %
9 %                        P        I    C        T                             %
10 %                        P      IIIII   CCCC    T                             %
11 %                                                                             %
12 %                                                                             %
13 %               Read/Write Apple Macintosh QuickDraw/PICT Format              %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2010 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-private.h"
47 #include "magick/colormap.h"
48 #include "magick/colormap-private.h"
49 #include "magick/colorspace.h"
50 #include "magick/composite.h"
51 #include "magick/constitute.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/image.h"
55 #include "magick/image-private.h"
56 #include "magick/list.h"
57 #include "magick/log.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/monitor.h"
61 #include "magick/monitor-private.h"
62 #include "magick/profile.h"
63 #include "magick/resource_.h"
64 #include "magick/quantum-private.h"
65 #include "magick/static.h"
66 #include "magick/string_.h"
67 #include "magick/module.h"
68 #include "magick/transform.h"
69 #include "magick/utility.h"
70 \f
71 /*
72   ImageMagick Macintosh PICT Methods.
73 */
74 #define ReadPixmap(pixmap) \
75 { \
76   pixmap.version=(short) ReadBlobMSBShort(image); \
77   pixmap.pack_type=(short) ReadBlobMSBShort(image); \
78   pixmap.pack_size=ReadBlobMSBLong(image); \
79   pixmap.horizontal_resolution=1UL*ReadBlobMSBShort(image); \
80   (void) ReadBlobMSBShort(image); \
81   pixmap.vertical_resolution=1UL*ReadBlobMSBShort(image); \
82   (void) ReadBlobMSBShort(image); \
83   pixmap.pixel_type=(short) ReadBlobMSBShort(image); \
84   pixmap.bits_per_pixel=(short) ReadBlobMSBShort(image); \
85   pixmap.component_count=(short) ReadBlobMSBShort(image); \
86   pixmap.component_size=(short) ReadBlobMSBShort(image); \
87   pixmap.plane_bytes=ReadBlobMSBLong(image); \
88   pixmap.table=ReadBlobMSBLong(image); \
89   pixmap.reserved=ReadBlobMSBLong(image); \
90   if ((pixmap.bits_per_pixel <= 0) || (pixmap.bits_per_pixel > 32) || \
91       (pixmap.component_count <= 0) || (pixmap.component_count > 4) || \
92       (pixmap.component_size <= 0)) \
93     ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
94 }
95
96 #define ReadRectangle(image,rectangle) \
97 { \
98   rectangle.top=(short) ReadBlobMSBShort(image); \
99   rectangle.left=(short) ReadBlobMSBShort(image); \
100   rectangle.bottom=(short) ReadBlobMSBShort(image); \
101   rectangle.right=(short) ReadBlobMSBShort(image); \
102   if ((rectangle.left > rectangle.right) || \
103       (rectangle.top > rectangle.bottom)) \
104     ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
105 }
106
107 typedef struct _PICTCode
108 {
109   const char
110     *name;
111
112   ssize_t
113     length;
114
115   const char
116     *description;
117 } PICTCode;
118
119 typedef struct _PICTPixmap
120 {
121   short
122     version,
123     pack_type;
124
125   size_t
126     pack_size,
127     horizontal_resolution,
128     vertical_resolution;
129
130   short
131     pixel_type,
132     bits_per_pixel,
133     component_count,
134     component_size;
135
136   size_t
137     plane_bytes,
138     table,
139     reserved;
140 } PICTPixmap;
141
142 typedef struct _PICTRectangle
143 {
144   short
145     top,
146     left,
147     bottom,
148     right;
149 } PICTRectangle;
150
151 static const PICTCode
152   codes[] =
153   {
154     /* 0x00 */ { "NOP", 0, "nop" },
155     /* 0x01 */ { "Clip", 0, "clip" },
156     /* 0x02 */ { "BkPat", 8, "background pattern" },
157     /* 0x03 */ { "TxFont", 2, "text font (word)" },
158     /* 0x04 */ { "TxFace", 1, "text face (byte)" },
159     /* 0x05 */ { "TxMode", 2, "text mode (word)" },
160     /* 0x06 */ { "SpExtra", 4, "space extra (fixed point)" },
161     /* 0x07 */ { "PnSize", 4, "pen size (point)" },
162     /* 0x08 */ { "PnMode", 2, "pen mode (word)" },
163     /* 0x09 */ { "PnPat", 8, "pen pattern" },
164     /* 0x0a */ { "FillPat", 8, "fill pattern" },
165     /* 0x0b */ { "OvSize", 4, "oval size (point)" },
166     /* 0x0c */ { "Origin", 4, "dh, dv (word)" },
167     /* 0x0d */ { "TxSize", 2, "text size (word)" },
168     /* 0x0e */ { "FgColor", 4, "foreground color (ssize_tword)" },
169     /* 0x0f */ { "BkColor", 4, "background color (ssize_tword)" },
170     /* 0x10 */ { "TxRatio", 8, "numerator (point), denominator (point)" },
171     /* 0x11 */ { "Version", 1, "version (byte)" },
172     /* 0x12 */ { "BkPixPat", 0, "color background pattern" },
173     /* 0x13 */ { "PnPixPat", 0, "color pen pattern" },
174     /* 0x14 */ { "FillPixPat", 0, "color fill pattern" },
175     /* 0x15 */ { "PnLocHFrac", 2, "fractional pen position" },
176     /* 0x16 */ { "ChExtra", 2, "extra for each character" },
177     /* 0x17 */ { "reserved", 0, "reserved for Apple use" },
178     /* 0x18 */ { "reserved", 0, "reserved for Apple use" },
179     /* 0x19 */ { "reserved", 0, "reserved for Apple use" },
180     /* 0x1a */ { "RGBFgCol", 6, "RGB foreColor" },
181     /* 0x1b */ { "RGBBkCol", 6, "RGB backColor" },
182     /* 0x1c */ { "HiliteMode", 0, "hilite mode flag" },
183     /* 0x1d */ { "HiliteColor", 6, "RGB hilite color" },
184     /* 0x1e */ { "DefHilite", 0, "Use default hilite color" },
185     /* 0x1f */ { "OpColor", 6, "RGB OpColor for arithmetic modes" },
186     /* 0x20 */ { "Line", 8, "pnLoc (point), newPt (point)" },
187     /* 0x21 */ { "LineFrom", 4, "newPt (point)" },
188     /* 0x22 */ { "ShortLine", 6, "pnLoc (point, dh, dv (-128 .. 127))" },
189     /* 0x23 */ { "ShortLineFrom", 2, "dh, dv (-128 .. 127)" },
190     /* 0x24 */ { "reserved", -1, "reserved for Apple use" },
191     /* 0x25 */ { "reserved", -1, "reserved for Apple use" },
192     /* 0x26 */ { "reserved", -1, "reserved for Apple use" },
193     /* 0x27 */ { "reserved", -1, "reserved for Apple use" },
194     /* 0x28 */ { "LongText", 0, "txLoc (point), count (0..255), text" },
195     /* 0x29 */ { "DHText", 0, "dh (0..255), count (0..255), text" },
196     /* 0x2a */ { "DVText", 0, "dv (0..255), count (0..255), text" },
197     /* 0x2b */ { "DHDVText", 0, "dh, dv (0..255), count (0..255), text" },
198     /* 0x2c */ { "reserved", -1, "reserved for Apple use" },
199     /* 0x2d */ { "reserved", -1, "reserved for Apple use" },
200     /* 0x2e */ { "reserved", -1, "reserved for Apple use" },
201     /* 0x2f */ { "reserved", -1, "reserved for Apple use" },
202     /* 0x30 */ { "frameRect", 8, "rect" },
203     /* 0x31 */ { "paintRect", 8, "rect" },
204     /* 0x32 */ { "eraseRect", 8, "rect" },
205     /* 0x33 */ { "invertRect", 8, "rect" },
206     /* 0x34 */ { "fillRect", 8, "rect" },
207     /* 0x35 */ { "reserved", 8, "reserved for Apple use" },
208     /* 0x36 */ { "reserved", 8, "reserved for Apple use" },
209     /* 0x37 */ { "reserved", 8, "reserved for Apple use" },
210     /* 0x38 */ { "frameSameRect", 0, "rect" },
211     /* 0x39 */ { "paintSameRect", 0, "rect" },
212     /* 0x3a */ { "eraseSameRect", 0, "rect" },
213     /* 0x3b */ { "invertSameRect", 0, "rect" },
214     /* 0x3c */ { "fillSameRect", 0, "rect" },
215     /* 0x3d */ { "reserved", 0, "reserved for Apple use" },
216     /* 0x3e */ { "reserved", 0, "reserved for Apple use" },
217     /* 0x3f */ { "reserved", 0, "reserved for Apple use" },
218     /* 0x40 */ { "frameRRect", 8, "rect" },
219     /* 0x41 */ { "paintRRect", 8, "rect" },
220     /* 0x42 */ { "eraseRRect", 8, "rect" },
221     /* 0x43 */ { "invertRRect", 8, "rect" },
222     /* 0x44 */ { "fillRRrect", 8, "rect" },
223     /* 0x45 */ { "reserved", 8, "reserved for Apple use" },
224     /* 0x46 */ { "reserved", 8, "reserved for Apple use" },
225     /* 0x47 */ { "reserved", 8, "reserved for Apple use" },
226     /* 0x48 */ { "frameSameRRect", 0, "rect" },
227     /* 0x49 */ { "paintSameRRect", 0, "rect" },
228     /* 0x4a */ { "eraseSameRRect", 0, "rect" },
229     /* 0x4b */ { "invertSameRRect", 0, "rect" },
230     /* 0x4c */ { "fillSameRRect", 0, "rect" },
231     /* 0x4d */ { "reserved", 0, "reserved for Apple use" },
232     /* 0x4e */ { "reserved", 0, "reserved for Apple use" },
233     /* 0x4f */ { "reserved", 0, "reserved for Apple use" },
234     /* 0x50 */ { "frameOval", 8, "rect" },
235     /* 0x51 */ { "paintOval", 8, "rect" },
236     /* 0x52 */ { "eraseOval", 8, "rect" },
237     /* 0x53 */ { "invertOval", 8, "rect" },
238     /* 0x54 */ { "fillOval", 8, "rect" },
239     /* 0x55 */ { "reserved", 8, "reserved for Apple use" },
240     /* 0x56 */ { "reserved", 8, "reserved for Apple use" },
241     /* 0x57 */ { "reserved", 8, "reserved for Apple use" },
242     /* 0x58 */ { "frameSameOval", 0, "rect" },
243     /* 0x59 */ { "paintSameOval", 0, "rect" },
244     /* 0x5a */ { "eraseSameOval", 0, "rect" },
245     /* 0x5b */ { "invertSameOval", 0, "rect" },
246     /* 0x5c */ { "fillSameOval", 0, "rect" },
247     /* 0x5d */ { "reserved", 0, "reserved for Apple use" },
248     /* 0x5e */ { "reserved", 0, "reserved for Apple use" },
249     /* 0x5f */ { "reserved", 0, "reserved for Apple use" },
250     /* 0x60 */ { "frameArc", 12, "rect, startAngle, arcAngle" },
251     /* 0x61 */ { "paintArc", 12, "rect, startAngle, arcAngle" },
252     /* 0x62 */ { "eraseArc", 12, "rect, startAngle, arcAngle" },
253     /* 0x63 */ { "invertArc", 12, "rect, startAngle, arcAngle" },
254     /* 0x64 */ { "fillArc", 12, "rect, startAngle, arcAngle" },
255     /* 0x65 */ { "reserved", 12, "reserved for Apple use" },
256     /* 0x66 */ { "reserved", 12, "reserved for Apple use" },
257     /* 0x67 */ { "reserved", 12, "reserved for Apple use" },
258     /* 0x68 */ { "frameSameArc", 4, "rect, startAngle, arcAngle" },
259     /* 0x69 */ { "paintSameArc", 4, "rect, startAngle, arcAngle" },
260     /* 0x6a */ { "eraseSameArc", 4, "rect, startAngle, arcAngle" },
261     /* 0x6b */ { "invertSameArc", 4, "rect, startAngle, arcAngle" },
262     /* 0x6c */ { "fillSameArc", 4, "rect, startAngle, arcAngle" },
263     /* 0x6d */ { "reserved", 4, "reserved for Apple use" },
264     /* 0x6e */ { "reserved", 4, "reserved for Apple use" },
265     /* 0x6f */ { "reserved", 4, "reserved for Apple use" },
266     /* 0x70 */ { "framePoly", 0, "poly" },
267     /* 0x71 */ { "paintPoly", 0, "poly" },
268     /* 0x72 */ { "erasePoly", 0, "poly" },
269     /* 0x73 */ { "invertPoly", 0, "poly" },
270     /* 0x74 */ { "fillPoly", 0, "poly" },
271     /* 0x75 */ { "reserved", 0, "reserved for Apple use" },
272     /* 0x76 */ { "reserved", 0, "reserved for Apple use" },
273     /* 0x77 */ { "reserved", 0, "reserved for Apple use" },
274     /* 0x78 */ { "frameSamePoly", 0, "poly (NYI)" },
275     /* 0x79 */ { "paintSamePoly", 0, "poly (NYI)" },
276     /* 0x7a */ { "eraseSamePoly", 0, "poly (NYI)" },
277     /* 0x7b */ { "invertSamePoly", 0, "poly (NYI)" },
278     /* 0x7c */ { "fillSamePoly", 0, "poly (NYI)" },
279     /* 0x7d */ { "reserved", 0, "reserved for Apple use" },
280     /* 0x7e */ { "reserved", 0, "reserved for Apple use" },
281     /* 0x7f */ { "reserved", 0, "reserved for Apple use" },
282     /* 0x80 */ { "frameRgn", 0, "region" },
283     /* 0x81 */ { "paintRgn", 0, "region" },
284     /* 0x82 */ { "eraseRgn", 0, "region" },
285     /* 0x83 */ { "invertRgn", 0, "region" },
286     /* 0x84 */ { "fillRgn", 0, "region" },
287     /* 0x85 */ { "reserved", 0, "reserved for Apple use" },
288     /* 0x86 */ { "reserved", 0, "reserved for Apple use" },
289     /* 0x87 */ { "reserved", 0, "reserved for Apple use" },
290     /* 0x88 */ { "frameSameRgn", 0, "region (NYI)" },
291     /* 0x89 */ { "paintSameRgn", 0, "region (NYI)" },
292     /* 0x8a */ { "eraseSameRgn", 0, "region (NYI)" },
293     /* 0x8b */ { "invertSameRgn", 0, "region (NYI)" },
294     /* 0x8c */ { "fillSameRgn", 0, "region (NYI)" },
295     /* 0x8d */ { "reserved", 0, "reserved for Apple use" },
296     /* 0x8e */ { "reserved", 0, "reserved for Apple use" },
297     /* 0x8f */ { "reserved", 0, "reserved for Apple use" },
298     /* 0x90 */ { "BitsRect", 0, "copybits, rect clipped" },
299     /* 0x91 */ { "BitsRgn", 0, "copybits, rgn clipped" },
300     /* 0x92 */ { "reserved", -1, "reserved for Apple use" },
301     /* 0x93 */ { "reserved", -1, "reserved for Apple use" },
302     /* 0x94 */ { "reserved", -1, "reserved for Apple use" },
303     /* 0x95 */ { "reserved", -1, "reserved for Apple use" },
304     /* 0x96 */ { "reserved", -1, "reserved for Apple use" },
305     /* 0x97 */ { "reserved", -1, "reserved for Apple use" },
306     /* 0x98 */ { "PackBitsRect", 0, "packed copybits, rect clipped" },
307     /* 0x99 */ { "PackBitsRgn", 0, "packed copybits, rgn clipped" },
308     /* 0x9a */ { "DirectBitsRect", 0, "PixMap, srcRect, dstRect, mode, PixData" },
309     /* 0x9b */ { "DirectBitsRgn", 0, "PixMap, srcRect, dstRect, mode, maskRgn, PixData" },
310     /* 0x9c */ { "reserved", -1, "reserved for Apple use" },
311     /* 0x9d */ { "reserved", -1, "reserved for Apple use" },
312     /* 0x9e */ { "reserved", -1, "reserved for Apple use" },
313     /* 0x9f */ { "reserved", -1, "reserved for Apple use" },
314     /* 0xa0 */ { "ShortComment", 2, "kind (word)" },
315     /* 0xa1 */ { "LongComment", 0, "kind (word), size (word), data" }
316   };
317
318 /*
319   Forward declarations.
320 */
321 static MagickBooleanType
322   WritePICTImage(const ImageInfo *,Image *);
323 \f
324 /*
325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
326 %                                                                             %
327 %                                                                             %
328 %                                                                             %
329 %   D e c o d e I m a g e                                                     %
330 %                                                                             %
331 %                                                                             %
332 %                                                                             %
333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 %
335 %  DecodeImage decompresses an image via Macintosh pack bits decoding for
336 %  Macintosh PICT images.
337 %
338 %  The format of the DecodeImage method is:
339 %
340 %      unsigned char *DecodeImage(Image *blob,Image *image,
341 %        size_t bytes_per_line,const int bits_per_pixel,
342 %        unsigned size_t extent)
343 %
344 %  A description of each parameter follows:
345 %
346 %    o image_info: the image info.
347 %
348 %    o blob,image: the address of a structure of type Image.
349 %
350 %    o bytes_per_line: This integer identifies the number of bytes in a
351 %      scanline.
352 %
353 %    o bits_per_pixel: the number of bits in a pixel.
354 %
355 %    o extent: the number of pixels allocated.
356 %
357 */
358
359 static unsigned char *ExpandBuffer(unsigned char *pixels,
360   MagickSizeType *bytes_per_line,const unsigned int bits_per_pixel)
361 {
362   register ssize_t
363     i;
364
365   register unsigned char
366     *p,
367     *q;
368
369   static unsigned char
370     scanline[8*256];
371
372   p=pixels;
373   q=scanline;
374   switch (bits_per_pixel)
375   {
376     case 8:
377     case 16:
378     case 32:
379       return(pixels);
380     case 4:
381     {
382       for (i=0; i < (ssize_t) *bytes_per_line; i++)
383       {
384         *q++=(*p >> 4) & 0xff;
385         *q++=(*p & 15);
386         p++;
387       }
388       *bytes_per_line*=2;
389       break;
390     }
391     case 2:
392     {
393       for (i=0; i < (ssize_t) *bytes_per_line; i++)
394       {
395         *q++=(*p >> 6) & 0x03;
396         *q++=(*p >> 4) & 0x03;
397         *q++=(*p >> 2) & 0x03;
398         *q++=(*p & 3);
399         p++;
400       }
401       *bytes_per_line*=4;
402       break;
403     }
404     case 1:
405     {
406       for (i=0; i < (ssize_t) *bytes_per_line; i++)
407       {
408         *q++=(*p >> 7) & 0x01;
409         *q++=(*p >> 6) & 0x01;
410         *q++=(*p >> 5) & 0x01;
411         *q++=(*p >> 4) & 0x01;
412         *q++=(*p >> 3) & 0x01;
413         *q++=(*p >> 2) & 0x01;
414         *q++=(*p >> 1) & 0x01;
415         *q++=(*p & 0x01);
416         p++;
417       }
418       *bytes_per_line*=8;
419       break;
420     }
421     default:
422       break;
423   }
424   return(scanline);
425 }
426
427 static unsigned char *DecodeImage(Image *blob,Image *image,
428   size_t bytes_per_line,const unsigned int bits_per_pixel,size_t *extent)
429 {
430   ssize_t
431     j,
432     y;
433
434   MagickSizeType
435     number_pixels;
436
437   register ssize_t
438     i;
439
440   register unsigned char
441     *p,
442     *q;
443
444   size_t
445     length,
446     row_bytes;
447
448   ssize_t
449     count;
450
451   unsigned char
452     *pixels,
453     *scanline;
454
455   size_t
456     bytes_per_pixel,
457     scanline_length,
458     width;
459
460   /*
461     Determine pixel buffer size.
462   */
463   if (bits_per_pixel <= 8)
464     bytes_per_line&=0x7fff;
465   width=image->columns;
466   bytes_per_pixel=1;
467   if (bits_per_pixel == 16)
468     {
469       bytes_per_pixel=2;
470       width*=2;
471     }
472   else
473     if (bits_per_pixel == 32)
474       width*=image->matte ? 4 : 3;
475   if (bytes_per_line == 0)
476     bytes_per_line=width;
477   row_bytes=(size_t) (image->columns | 0x8000);
478   if (image->storage_class == DirectClass)
479     row_bytes=(size_t) ((4*image->columns) | 0x8000);
480   /*
481     Allocate pixel and scanline buffer.
482   */
483   pixels=(unsigned char *) AcquireQuantumMemory(image->rows,row_bytes*
484     sizeof(*pixels));
485   if (pixels == (unsigned char *) NULL)
486     return((unsigned char *) NULL);
487   *extent=row_bytes*image->rows*sizeof(*pixels);
488   (void) ResetMagickMemory(pixels,0,*extent);
489   scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
490   if (scanline == (unsigned char *) NULL)
491     return((unsigned char *) NULL);
492   if (bytes_per_line < 8)
493     {
494       /*
495         Pixels are already uncompressed.
496       */
497       for (y=0; y < (ssize_t) image->rows; y++)
498       {
499         q=pixels+y*width;
500         number_pixels=bytes_per_line;
501         count=ReadBlob(blob,(size_t) number_pixels,scanline);
502         p=ExpandBuffer(scanline,&number_pixels,bits_per_pixel);
503         if ((q+number_pixels) > (pixels+(*extent)))
504           {
505             (void) ThrowMagickException(&image->exception,GetMagickModule(),
506               CorruptImageError,"UnableToUncompressImage","`%s'",
507               image->filename);
508             break;
509           }
510         (void) CopyMagickMemory(q,p,(size_t) number_pixels);
511       }
512       scanline=(unsigned char *) RelinquishMagickMemory(scanline);
513       return(pixels);
514     }
515   /*
516     Uncompress RLE pixels into uncompressed pixel buffer.
517   */
518   for (y=0; y < (ssize_t) image->rows; y++)
519   {
520     q=pixels+y*width;
521     if (bytes_per_line > 200)
522       scanline_length=ReadBlobMSBShort(blob);
523     else
524       scanline_length=1UL*ReadBlobByte(blob);
525     if (scanline_length >= row_bytes)
526       {
527         (void) ThrowMagickException(&image->exception,GetMagickModule(),
528           CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
529         break;
530       }
531     count=ReadBlob(blob,scanline_length,scanline);
532     for (j=0; j < (ssize_t) scanline_length; )
533       if ((scanline[j] & 0x80) == 0)
534         {
535           length=(size_t) ((scanline[j] & 0xff)+1);
536           number_pixels=length*bytes_per_pixel;
537           p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
538           if ((q-pixels+number_pixels) <= *extent)
539             (void) CopyMagickMemory(q,p,(size_t) number_pixels);
540           q+=number_pixels;
541           j+=(ssize_t) (length*bytes_per_pixel+1);
542         }
543       else
544         {
545           length=(size_t) (((scanline[j] ^ 0xff) & 0xff)+2);
546           number_pixels=bytes_per_pixel;
547           p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
548           for (i=0; i < (ssize_t) length; i++)
549           {
550             if ((q-pixels+number_pixels) <= *extent)
551               (void) CopyMagickMemory(q,p,(size_t) number_pixels);
552             q+=number_pixels;
553           }
554           j+=(ssize_t) bytes_per_pixel+1;
555         }
556   }
557   scanline=(unsigned char *) RelinquishMagickMemory(scanline);
558   return(pixels);
559 }
560 \f
561 /*
562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563 %                                                                             %
564 %                                                                             %
565 %                                                                             %
566 %   E n c o d e I m a g e                                                     %
567 %                                                                             %
568 %                                                                             %
569 %                                                                             %
570 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571 %
572 %  EncodeImage compresses an image via Macintosh pack bits encoding
573 %  for Macintosh PICT images.
574 %
575 %  The format of the EncodeImage method is:
576 %
577 %      size_t EncodeImage(Image *image,const unsigned char *scanline,
578 %        const size_t bytes_per_line,unsigned char *pixels)
579 %
580 %  A description of each parameter follows:
581 %
582 %    o image: the address of a structure of type Image.
583 %
584 %    o scanline: A pointer to an array of characters to pack.
585 %
586 %    o bytes_per_line: the number of bytes in a scanline.
587 %
588 %    o pixels: A pointer to an array of characters where the packed
589 %      characters are stored.
590 %
591 */
592 static size_t EncodeImage(Image *image,const unsigned char *scanline,
593   const size_t bytes_per_line,unsigned char *pixels)
594 {
595 #define MaxCount  128
596 #define MaxPackbitsRunlength  128
597
598   ssize_t
599     count,
600     repeat_count,
601     runlength;
602
603   register const unsigned char
604     *p;
605
606   register ssize_t
607     i;
608
609   register unsigned char
610     *q;
611
612   size_t
613     length;
614
615   unsigned char
616     index;
617
618   /*
619     Pack scanline.
620   */
621   assert(image != (Image *) NULL);
622   assert(image->signature == MagickSignature);
623   if (image->debug != MagickFalse)
624     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
625   assert(scanline != (unsigned char *) NULL);
626   assert(pixels != (unsigned char *) NULL);
627   count=0;
628   runlength=0;
629   p=scanline+(bytes_per_line-1);
630   q=pixels;
631   index=(*p);
632   for (i=(ssize_t) bytes_per_line-1; i >= 0; i--)
633   {
634     if (index == *p)
635       runlength++;
636     else
637       {
638         if (runlength < 3)
639           while (runlength > 0)
640           {
641             *q++=(unsigned char) index;
642             runlength--;
643             count++;
644             if (count == MaxCount)
645               {
646                 *q++=(unsigned char) (MaxCount-1);
647                 count-=MaxCount;
648               }
649           }
650         else
651           {
652             if (count > 0)
653               *q++=(unsigned char) (count-1);
654             count=0;
655             while (runlength > 0)
656             {
657               repeat_count=runlength;
658               if (repeat_count > MaxPackbitsRunlength)
659                 repeat_count=MaxPackbitsRunlength;
660               *q++=(unsigned char) index;
661               *q++=(unsigned char) (257-repeat_count);
662               runlength-=repeat_count;
663             }
664           }
665         runlength=1;
666       }
667     index=(*p);
668     p--;
669   }
670   if (runlength < 3)
671     while (runlength > 0)
672     {
673       *q++=(unsigned char) index;
674       runlength--;
675       count++;
676       if (count == MaxCount)
677         {
678           *q++=(unsigned char) (MaxCount-1);
679           count-=MaxCount;
680         }
681     }
682   else
683     {
684       if (count > 0)
685         *q++=(unsigned char) (count-1);
686       count=0;
687       while (runlength > 0)
688       {
689         repeat_count=runlength;
690         if (repeat_count > MaxPackbitsRunlength)
691           repeat_count=MaxPackbitsRunlength;
692         *q++=(unsigned char) index;
693         *q++=(unsigned char) (257-repeat_count);
694         runlength-=repeat_count;
695       }
696     }
697   if (count > 0)
698     *q++=(unsigned char) (count-1);
699   /*
700     Write the number of and the packed length.
701   */
702   length=(size_t) (q-pixels);
703   if (bytes_per_line > 200)
704     {
705       (void) WriteBlobMSBShort(image,(unsigned short) length);
706       length+=2;
707     }
708   else
709     {
710       (void) WriteBlobByte(image,(unsigned char) length);
711       length++;
712     }
713   while (q != pixels)
714   {
715     q--;
716     (void) WriteBlobByte(image,*q);
717   }
718   return(length);
719 }
720 \f
721 /*
722 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723 %                                                                             %
724 %                                                                             %
725 %                                                                             %
726 %   I s P I C T                                                               %
727 %                                                                             %
728 %                                                                             %
729 %                                                                             %
730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 %
732 %  IsPICT()() returns MagickTrue if the image format type, identified by the
733 %  magick string, is PICT.
734 %
735 %  The format of the ReadPICTImage method is:
736 %
737 %      MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
738 %
739 %  A description of each parameter follows:
740 %
741 %    o magick: compare image format pattern against these bytes.
742 %
743 %    o length: Specifies the length of the magick string.
744 %
745 */
746 static MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
747 {
748   if (length < 528)
749     return(MagickFalse);
750   if (memcmp(magick+522,"\000\021\002\377\014\000",6) == 0)
751     return(MagickTrue);
752   return(MagickFalse);
753 }
754 \f
755 #if !defined(macintosh)
756 /*
757 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758 %                                                                             %
759 %                                                                             %
760 %                                                                             %
761 %   R e a d P I C T I m a g e                                                 %
762 %                                                                             %
763 %                                                                             %
764 %                                                                             %
765 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766 %
767 %  ReadPICTImage() reads an Apple Macintosh QuickDraw/PICT image file
768 %  and returns it.  It allocates the memory necessary for the new Image
769 %  structure and returns a pointer to the new image.
770 %
771 %  The format of the ReadPICTImage method is:
772 %
773 %      Image *ReadPICTImage(const ImageInfo *image_info,
774 %        ExceptionInfo *exception)
775 %
776 %  A description of each parameter follows:
777 %
778 %    o image_info: the image info.
779 %
780 %    o exception: return any errors or warnings in this structure.
781 %
782 */
783
784 static inline size_t MagickMax(const size_t x,
785   const size_t y)
786 {
787   if (x > y)
788     return(x);
789   return(y);
790 }
791
792 static Image *ReadPICTImage(const ImageInfo *image_info,
793   ExceptionInfo *exception)
794 {
795   char
796     geometry[MaxTextExtent];
797
798   Image
799     *image;
800
801   IndexPacket
802     index;
803
804   int
805     c,
806     code;
807
808   ssize_t
809     flags,
810     j,
811     version,
812     y;
813
814   MagickBooleanType
815     jpeg,
816     status;
817
818   PICTRectangle
819     frame;
820
821   PICTPixmap
822     pixmap;
823
824   register IndexPacket
825     *indexes;
826
827   register ssize_t
828     x;
829
830   register PixelPacket
831     *q;
832
833   register ssize_t
834     i;
835
836   size_t
837     extent,
838     length;
839
840   ssize_t
841     count;
842
843   StringInfo
844     *profile;
845
846   /*
847     Open image file.
848   */
849   assert(image_info != (const ImageInfo *) NULL);
850   assert(image_info->signature == MagickSignature);
851   if (image_info->debug != MagickFalse)
852     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
853       image_info->filename);
854   assert(exception != (ExceptionInfo *) NULL);
855   assert(exception->signature == MagickSignature);
856   image=AcquireImage(image_info);
857   image->depth=8;
858   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
859   if (status == MagickFalse)
860     {
861       image=DestroyImageList(image);
862       return((Image *) NULL);
863     }
864   /*
865     Read PICT header.
866   */
867   pixmap.bits_per_pixel=0;
868   pixmap.component_count=0;
869   for (i=0; i < 512; i++)
870     (void) ReadBlobByte(image);  /* skip header */
871   (void) ReadBlobMSBShort(image);  /* skip picture size */
872   ReadRectangle(image,frame);
873   while ((c=ReadBlobByte(image)) == 0) ;
874   if (c != 0x11)
875     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
876   version=ReadBlobByte(image);
877   if (version == 2)
878     {
879       c=ReadBlobByte(image);
880       if (c != 0xff)
881         ThrowReaderException(CorruptImageError,"ImproperImageHeader");
882     }
883   else
884     if (version != 1)
885       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
886   if ((frame.left < 0) || (frame.right < 0) || (frame.top < 0) ||
887       (frame.bottom < 0) || (frame.left >= frame.right) ||
888       (frame.top >= frame.bottom))
889     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
890   /*
891     Create black canvas.
892   */
893   flags=0;
894   image->columns=1UL*(frame.right-frame.left);
895   image->rows=1UL*(frame.bottom-frame.top);
896   image->x_resolution=DefaultResolution;
897   image->y_resolution=DefaultResolution;
898   image->units=UndefinedResolution;
899   /*
900     Interpret PICT opcodes.
901   */
902   jpeg=MagickFalse;
903   for (code=0; EOFBlob(image) == MagickFalse; )
904   {
905     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
906       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
907         break;
908     if ((version == 1) || ((TellBlob(image) % 2) != 0))
909       code=ReadBlobByte(image);
910     if (version == 2)
911       code=(int) ReadBlobMSBShort(image);
912     if (code > 0xa1)
913       {
914         if (image->debug != MagickFalse)
915           (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%04X:",code);
916       }
917     else
918       {
919         if (image->debug != MagickFalse)
920           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
921             "  %04X %s: %s",code,codes[code].name,codes[code].description);
922         switch (code)
923         {
924           case 0x01:
925           {
926             /*
927               Clipping rectangle.
928             */
929             length=ReadBlobMSBShort(image);
930             if (length != 0x000a)
931               {
932                 for (i=0; i < (ssize_t) (length-2); i++)
933                   (void) ReadBlobByte(image);
934                 break;
935               }
936             ReadRectangle(image,frame);
937             if (((frame.left & 0x8000) != 0) || ((frame.top & 0x8000) != 0))
938               break;
939             image->columns=1UL*(frame.right-frame.left);
940             image->rows=1UL*(frame.bottom-frame.top);
941             (void) SetImageBackgroundColor(image);
942             break;
943           }
944           case 0x12:
945           case 0x13:
946           case 0x14:
947           {
948             ssize_t
949               pattern;
950
951             size_t
952               height,
953               width;
954
955             /*
956               Skip pattern definition.
957             */
958             pattern=1L*ReadBlobMSBShort(image);
959             for (i=0; i < 8; i++)
960               (void) ReadBlobByte(image);
961             if (pattern == 2)
962               {
963                 for (i=0; i < 5; i++)
964                   (void) ReadBlobByte(image);
965                 break;
966               }
967             if (pattern != 1)
968               ThrowReaderException(CorruptImageError,"UnknownPatternType");
969             length=ReadBlobMSBShort(image);
970             ReadRectangle(image,frame);
971             ReadPixmap(pixmap);
972             image->depth=1UL*pixmap.component_size;
973             image->x_resolution=1.0*pixmap.horizontal_resolution;
974             image->y_resolution=1.0*pixmap.vertical_resolution;
975             image->units=PixelsPerInchResolution;
976             (void) ReadBlobMSBLong(image);
977             flags=1L*ReadBlobMSBShort(image);
978             length=ReadBlobMSBShort(image);
979             for (i=0; i <= (ssize_t) length; i++)
980               (void) ReadBlobMSBLong(image);
981             width=1UL*(frame.bottom-frame.top);
982             height=1UL*(frame.right-frame.left);
983             if (pixmap.bits_per_pixel <= 8)
984               length&=0x7fff;
985             if (pixmap.bits_per_pixel == 16)
986               width<<=1;
987             if (length == 0)
988               length=width;
989             if (length < 8)
990               {
991                 for (i=0; i < (ssize_t) (length*height); i++)
992                   (void) ReadBlobByte(image);
993               }
994             else
995               for (j=0; j < (int) height; j++)
996                 if (length > 200)
997                   for (j=0; j < (ssize_t) ReadBlobMSBShort(image); j++)
998                     (void) ReadBlobByte(image);
999                 else
1000                   for (j=0; j < (ssize_t) ReadBlobByte(image); j++)
1001                     (void) ReadBlobByte(image);
1002             break;
1003           }
1004           case 0x1b:
1005           {
1006             /*
1007               Initialize image background color.
1008             */
1009             image->background_color.red=(Quantum)
1010               ScaleShortToQuantum(ReadBlobMSBShort(image));
1011             image->background_color.green=(Quantum)
1012               ScaleShortToQuantum(ReadBlobMSBShort(image));
1013             image->background_color.blue=(Quantum)
1014               ScaleShortToQuantum(ReadBlobMSBShort(image));
1015             break;
1016           }
1017           case 0x70:
1018           case 0x71:
1019           case 0x72:
1020           case 0x73:
1021           case 0x74:
1022           case 0x75:
1023           case 0x76:
1024           case 0x77:
1025           {
1026             /*
1027               Skip polygon or region.
1028             */
1029             length=ReadBlobMSBShort(image);
1030             for (i=0; i < (ssize_t) (length-2); i++)
1031               (void) ReadBlobByte(image);
1032             break;
1033           }
1034           case 0x90:
1035           case 0x91:
1036           case 0x98:
1037           case 0x99:
1038           case 0x9a:
1039           case 0x9b:
1040           {
1041             ssize_t
1042               bytes_per_line;
1043
1044             PICTRectangle
1045               source,
1046               destination;
1047
1048             register unsigned char
1049               *p;
1050
1051             size_t
1052               j;
1053
1054             unsigned char
1055               *pixels;
1056
1057             Image
1058               *tile_image;
1059
1060             /*
1061               Pixmap clipped by a rectangle.
1062             */
1063             bytes_per_line=0;
1064             if ((code != 0x9a) && (code != 0x9b))
1065               bytes_per_line=1L*ReadBlobMSBShort(image);
1066             else
1067               {
1068                 (void) ReadBlobMSBShort(image);
1069                 (void) ReadBlobMSBShort(image);
1070                 (void) ReadBlobMSBShort(image);
1071               }
1072             ReadRectangle(image,frame);
1073             /*
1074               Initialize tile image.
1075             */
1076             tile_image=CloneImage(image,1UL*(frame.right-frame.left),
1077               1UL*(frame.bottom-frame.top),MagickTrue,exception);
1078             if (tile_image == (Image *) NULL)
1079               return((Image *) NULL);
1080             if ((code == 0x9a) || (code == 0x9b) ||
1081                 ((bytes_per_line & 0x8000) != 0))
1082               {
1083                 ReadPixmap(pixmap);
1084                 tile_image->depth=1UL*pixmap.component_size;
1085                 tile_image->matte=pixmap.component_count == 4 ?
1086                   MagickTrue : MagickFalse;
1087                 tile_image->x_resolution=(double) pixmap.horizontal_resolution;
1088                 tile_image->y_resolution=(double) pixmap.vertical_resolution;
1089                 tile_image->units=PixelsPerInchResolution;
1090                 if (tile_image->matte != MagickFalse)
1091                   image->matte=tile_image->matte;
1092               }
1093             if ((code != 0x9a) && (code != 0x9b))
1094               {
1095                 /*
1096                   Initialize colormap.
1097                 */
1098                 tile_image->colors=2;
1099                 if ((bytes_per_line & 0x8000) != 0)
1100                   {
1101                     (void) ReadBlobMSBLong(image);
1102                     flags=1L*ReadBlobMSBShort(image);
1103                     tile_image->colors=1UL*ReadBlobMSBShort(image)+1;
1104                   }
1105                 status=AcquireImageColormap(tile_image,tile_image->colors);
1106                 if (status == MagickFalse)
1107                   {
1108                     tile_image=DestroyImage(tile_image);
1109                     ThrowReaderException(ResourceLimitError,
1110                       "MemoryAllocationFailed");
1111                   }
1112                 if ((bytes_per_line & 0x8000) != 0)
1113                   {
1114                     for (i=0; i < (ssize_t) tile_image->colors; i++)
1115                     {
1116                       j=ReadBlobMSBShort(image) % tile_image->colors;
1117                       if ((flags & 0x8000) != 0)
1118                         j=(size_t) i;
1119                       tile_image->colormap[j].red=(Quantum)
1120                         ScaleShortToQuantum(ReadBlobMSBShort(image));
1121                       tile_image->colormap[j].green=(Quantum)
1122                         ScaleShortToQuantum(ReadBlobMSBShort(image));
1123                       tile_image->colormap[j].blue=(Quantum)
1124                         ScaleShortToQuantum(ReadBlobMSBShort(image));
1125                     }
1126                   }
1127                 else
1128                   {
1129                     for (i=0; i < (ssize_t) tile_image->colors; i++)
1130                     {
1131                       tile_image->colormap[i].red=(Quantum) (QuantumRange-
1132                         tile_image->colormap[i].red);
1133                       tile_image->colormap[i].green=(Quantum) (QuantumRange-
1134                         tile_image->colormap[i].green);
1135                       tile_image->colormap[i].blue=(Quantum) (QuantumRange-
1136                         tile_image->colormap[i].blue);
1137                     }
1138                   }
1139               }
1140             ReadRectangle(image,source);
1141             ReadRectangle(image,destination);
1142             (void) ReadBlobMSBShort(image);
1143             if ((code == 0x91) || (code == 0x99) || (code == 0x9b))
1144               {
1145                 /*
1146                   Skip region.
1147                 */
1148                 length=ReadBlobMSBShort(image);
1149                 for (i=0; i < (ssize_t) (length-2); i++)
1150                   (void) ReadBlobByte(image);
1151               }
1152             if ((code != 0x9a) && (code != 0x9b) &&
1153                 (bytes_per_line & 0x8000) == 0)
1154               pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1,&extent);
1155             else
1156               pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1U*
1157                 pixmap.bits_per_pixel,&extent);
1158             if (pixels == (unsigned char *) NULL)
1159               {
1160                 tile_image=DestroyImage(tile_image);
1161                 ThrowReaderException(ResourceLimitError,
1162                   "MemoryAllocationFailed");
1163               }
1164             /*
1165               Convert PICT tile image to pixel packets.
1166             */
1167             p=pixels;
1168             for (y=0; y < (ssize_t) tile_image->rows; y++)
1169             {
1170               if (p > (pixels+extent+image->columns))
1171                 ThrowReaderException(CorruptImageError,"NotEnoughPixelData");
1172               q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,
1173                 exception);
1174               if (q == (PixelPacket *) NULL)
1175                 break;
1176               indexes=GetAuthenticIndexQueue(tile_image);
1177               for (x=0; x < (ssize_t) tile_image->columns; x++)
1178               {
1179                 if (tile_image->storage_class == PseudoClass)
1180                   {
1181                     index=ConstrainColormapIndex(tile_image,*p);
1182                     indexes[x]=index;
1183                     q->red=tile_image->colormap[(ssize_t) index].red;
1184                     q->green=tile_image->colormap[(ssize_t) index].green;
1185                     q->blue=tile_image->colormap[(ssize_t) index].blue;
1186                   }
1187                 else
1188                   {
1189                     if (pixmap.bits_per_pixel == 16)
1190                       {
1191                         i=(*p++);
1192                         j=(*p);
1193                         q->red=ScaleCharToQuantum((unsigned char)
1194                           ((i & 0x7c) << 1));
1195                         q->green=ScaleCharToQuantum((unsigned char)
1196                           (((i & 0x03) << 6) | ((j & 0xe0) >> 2)));
1197                         q->blue=ScaleCharToQuantum((unsigned char)
1198                           ((j & 0x1f) << 3));
1199                       }
1200                     else
1201                       if (tile_image->matte == MagickFalse)
1202                         {
1203                           if (p > (pixels+extent+2*image->columns))
1204                             ThrowReaderException(CorruptImageError,
1205                               "NotEnoughPixelData");
1206                           q->red=ScaleCharToQuantum(*p);
1207                           q->green=ScaleCharToQuantum(
1208                             *(p+tile_image->columns));
1209                           q->blue=ScaleCharToQuantum(
1210                             *(p+2*tile_image->columns));
1211                         }
1212                       else
1213                         {
1214                           if (p > (pixels+extent+3*image->columns))
1215                             ThrowReaderException(CorruptImageError,
1216                               "NotEnoughPixelData");
1217                           q->opacity=(Quantum) (QuantumRange-
1218                             ScaleCharToQuantum(*p));
1219                           q->red=ScaleCharToQuantum(*(p+tile_image->columns));
1220                           q->green=(Quantum) ScaleCharToQuantum(
1221                             *(p+2*tile_image->columns));
1222                           q->blue=ScaleCharToQuantum(
1223                             *(p+3*tile_image->columns));
1224                         }
1225                   }
1226                 p++;
1227                 q++;
1228               }
1229               if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
1230                 break;
1231               if ((tile_image->storage_class == DirectClass) &&
1232                   (pixmap.bits_per_pixel != 16))
1233                 {
1234                   p+=(pixmap.component_count-1)*tile_image->columns;
1235                   if (p < pixels)
1236                     break;
1237                 }
1238               status=SetImageProgress(image,LoadImageTag,y,tile_image->rows);
1239               if (status == MagickFalse)
1240                 break;
1241             }
1242             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1243             if (jpeg == MagickFalse)
1244               if ((code == 0x9a) || (code == 0x9b) ||
1245                   ((bytes_per_line & 0x8000) != 0))
1246                 (void) CompositeImage(image,CopyCompositeOp,tile_image,
1247                   destination.left,destination.top);
1248             tile_image=DestroyImage(tile_image);
1249             break;
1250           }
1251           case 0xa1:
1252           {
1253             unsigned char
1254               *info;
1255
1256             size_t
1257               type;
1258
1259             /*
1260               Comment.
1261             */
1262             type=ReadBlobMSBShort(image);
1263             length=ReadBlobMSBShort(image);
1264             if (length == 0)
1265               break;
1266             (void) ReadBlobMSBLong(image);
1267             length-=4;
1268             if (length == 0)
1269               break;
1270             info=(unsigned char *) AcquireQuantumMemory(length,sizeof(*info));
1271             if (info == (unsigned char *) NULL)
1272               break;
1273             count=ReadBlob(image,length,info);
1274             switch (type)
1275             {
1276               case 0xe0:
1277               {
1278                 if (length == 0)
1279                   break;
1280                 profile=AcquireStringInfo(length);
1281                 SetStringInfoDatum(profile,info);
1282                 status=SetImageProfile(image,"icc",profile);
1283                 profile=DestroyStringInfo(profile);
1284                 if (status == MagickFalse)
1285                   ThrowReaderException(ResourceLimitError,
1286                     "MemoryAllocationFailed");
1287                 break;
1288               }
1289               case 0x1f2:
1290               {
1291                 if (length == 0)
1292                   break;
1293                 profile=AcquireStringInfo(length);
1294                 SetStringInfoDatum(profile,info);
1295                 status=SetImageProfile(image,"iptc",profile);
1296                 if (status == MagickFalse)
1297                   ThrowReaderException(ResourceLimitError,
1298                     "MemoryAllocationFailed");
1299                 profile=DestroyStringInfo(profile);
1300                 break;
1301               }
1302               default:
1303                 break;
1304             }
1305             info=(unsigned char *) RelinquishMagickMemory(info);
1306             break;
1307           }
1308           default:
1309           {
1310             /*
1311               Skip to next op code.
1312             */
1313             if (codes[code].length == -1)
1314               (void) ReadBlobMSBShort(image);
1315             else
1316               for (i=0; i < (ssize_t) codes[code].length; i++)
1317                 (void) ReadBlobByte(image);
1318           }
1319         }
1320       }
1321     if (code == 0xc00)
1322       {
1323         /*
1324           Skip header.
1325         */
1326         for (i=0; i < 24; i++)
1327           (void) ReadBlobByte(image);
1328         continue;
1329       }
1330     if (((code >= 0xb0) && (code <= 0xcf)) ||
1331         ((code >= 0x8000) && (code <= 0x80ff)))
1332       continue;
1333     if (code == 0x8200)
1334       {
1335         FILE
1336           *file;
1337
1338         Image
1339           *tile_image;
1340
1341         ImageInfo
1342           *read_info;
1343
1344         int
1345           unique_file;
1346
1347         /*
1348           Embedded JPEG.
1349         */
1350         jpeg=MagickTrue;
1351         read_info=CloneImageInfo(image_info);
1352         SetImageInfoBlob(read_info,(void *) NULL,0);
1353         file=(FILE *) NULL;
1354         unique_file=AcquireUniqueFileResource(read_info->filename);
1355         if (unique_file != -1)
1356           file=fdopen(unique_file,"wb");
1357         if ((unique_file == -1) || (file == (FILE *) NULL))
1358           {
1359             (void) CopyMagickString(image->filename,read_info->filename,
1360               MaxTextExtent);
1361             ThrowFileException(exception,FileOpenError,
1362               "UnableToCreateTemporaryFile",image->filename);
1363             image=DestroyImageList(image);
1364             return((Image *) NULL);
1365           }
1366         length=ReadBlobMSBLong(image);
1367         for (i=0; i < 6; i++)
1368           (void) ReadBlobMSBLong(image);
1369         ReadRectangle(image,frame);
1370         for (i=0; i < 122; i++)
1371           (void) ReadBlobByte(image);
1372         for (i=0; i < (ssize_t) (length-154); i++)
1373         {
1374           c=ReadBlobByte(image);
1375           (void) fputc(c,file);
1376         }
1377         (void) fclose(file);
1378         tile_image=ReadImage(read_info,exception);
1379         (void) RelinquishUniqueFileResource(read_info->filename);
1380         read_info=DestroyImageInfo(read_info);
1381         if (tile_image == (Image *) NULL)
1382           continue;
1383         (void) FormatMagickString(geometry,MaxTextExtent,"%.20gx%.20g",
1384           (double) MagickMax(image->columns,tile_image->columns),
1385           (double) MagickMax(image->rows,tile_image->rows));
1386         (void) SetImageExtent(image,
1387           MagickMax(image->columns,tile_image->columns),
1388           MagickMax(image->rows,tile_image->rows));
1389         if (image->colorspace != RGBColorspace)
1390           (void) TransformImageColorspace(image,tile_image->colorspace);
1391         (void) CompositeImage(image,CopyCompositeOp,tile_image,frame.left,
1392           frame.right);
1393         image->compression=tile_image->compression;
1394         tile_image=DestroyImage(tile_image);
1395         continue;
1396       }
1397     if ((code == 0xff) || (code == 0xffff))
1398       break;
1399     if (((code >= 0xd0) && (code <= 0xfe)) ||
1400         ((code >= 0x8100) && (code <= 0xffff)))
1401       {
1402         /*
1403           Skip reserved.
1404         */
1405         length=ReadBlobMSBShort(image);
1406         for (i=0; i < (ssize_t) length; i++)
1407           (void) ReadBlobByte(image);
1408         continue;
1409       }
1410     if ((code >= 0x100) && (code <= 0x7fff))
1411       {
1412         /*
1413           Skip reserved.
1414         */
1415         length=(size_t) ((code >> 7) & 0xff);
1416         for (i=0; i < (ssize_t) length; i++)
1417           (void) ReadBlobByte(image);
1418         continue;
1419       }
1420   }
1421   (void) CloseBlob(image);
1422   return(GetFirstImageInList(image));
1423 }
1424 #endif
1425 \f
1426 /*
1427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1428 %                                                                             %
1429 %                                                                             %
1430 %                                                                             %
1431 %   R e g i s t e r P I C T I m a g e                                         %
1432 %                                                                             %
1433 %                                                                             %
1434 %                                                                             %
1435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1436 %
1437 %  RegisterPICTImage() adds attributes for the PICT image format to
1438 %  the list of supported formats.  The attributes include the image format
1439 %  tag, a method to read and/or write the format, whether the format
1440 %  supports the saving of more than one frame to the same file or blob,
1441 %  whether the format supports native in-memory I/O, and a brief
1442 %  description of the format.
1443 %
1444 %  The format of the RegisterPICTImage method is:
1445 %
1446 %      size_t RegisterPICTImage(void)
1447 %
1448 */
1449 ModuleExport size_t RegisterPICTImage(void)
1450 {
1451   MagickInfo
1452     *entry;
1453
1454   entry=SetMagickInfo("PCT");
1455   entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1456   entry->encoder=(EncodeImageHandler *) WritePICTImage;
1457   entry->adjoin=MagickFalse;
1458   entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1459   entry->magick=(IsImageFormatHandler *) IsPICT;
1460   entry->module=ConstantString("PICT");
1461   (void) RegisterMagickInfo(entry);
1462   entry=SetMagickInfo("PICT");
1463   entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1464   entry->encoder=(EncodeImageHandler *) WritePICTImage;
1465   entry->adjoin=MagickFalse;
1466   entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1467   entry->magick=(IsImageFormatHandler *) IsPICT;
1468   entry->module=ConstantString("PICT");
1469   (void) RegisterMagickInfo(entry);
1470   return(MagickImageCoderSignature);
1471 }
1472 \f
1473 /*
1474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1475 %                                                                             %
1476 %                                                                             %
1477 %                                                                             %
1478 %   U n r e g i s t e r P I C T I m a g e                                     %
1479 %                                                                             %
1480 %                                                                             %
1481 %                                                                             %
1482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1483 %
1484 %  UnregisterPICTImage() removes format registrations made by the
1485 %  PICT module from the list of supported formats.
1486 %
1487 %  The format of the UnregisterPICTImage method is:
1488 %
1489 %      UnregisterPICTImage(void)
1490 %
1491 */
1492 ModuleExport void UnregisterPICTImage(void)
1493 {
1494   (void) UnregisterMagickInfo("PCT");
1495   (void) UnregisterMagickInfo("PICT");
1496 }
1497 \f
1498 /*
1499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1500 %                                                                             %
1501 %                                                                             %
1502 %                                                                             %
1503 %   W r i t e P I C T I m a g e                                               %
1504 %                                                                             %
1505 %                                                                             %
1506 %                                                                             %
1507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1508 %
1509 %  WritePICTImage() writes an image to a file in the Apple Macintosh
1510 %  QuickDraw/PICT image format.
1511 %
1512 %  The format of the WritePICTImage method is:
1513 %
1514 %      MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1515 %        Image *image)
1516 %
1517 %  A description of each parameter follows.
1518 %
1519 %    o image_info: the image info.
1520 %
1521 %    o image:  The image.
1522 %
1523 */
1524 static MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1525   Image *image)
1526 {
1527 #define MaxCount  128
1528 #define PictCropRegionOp  0x01
1529 #define PictEndOfPictureOp  0xff
1530 #define PictJPEGOp  0x8200
1531 #define PictInfoOp  0x0C00
1532 #define PictInfoSize  512
1533 #define PictPixmapOp  0x9A
1534 #define PictPICTOp  0x98
1535 #define PictVersion  0x11
1536
1537   const StringInfo
1538     *profile;
1539
1540   double
1541     x_resolution,
1542     y_resolution;
1543
1544   ssize_t
1545     y;
1546
1547   MagickBooleanType
1548     status;
1549
1550   MagickOffsetType
1551     offset;
1552
1553   PICTPixmap
1554     pixmap;
1555
1556   PICTRectangle
1557     bounds,
1558     crop_rectangle,
1559     destination_rectangle,
1560     frame_rectangle,
1561     size_rectangle,
1562     source_rectangle;
1563
1564   register const IndexPacket
1565     *indexes;
1566
1567   register const PixelPacket
1568     *p;
1569
1570   register ssize_t
1571     i,
1572     x;
1573
1574   size_t
1575     count;
1576
1577   unsigned char
1578     *buffer,
1579     *packed_scanline,
1580     *scanline;
1581
1582   size_t
1583     bytes_per_line,
1584     storage_class;
1585
1586   unsigned short
1587     base_address,
1588     row_bytes,
1589     transfer_mode;
1590
1591   /*
1592     Open output image file.
1593   */
1594   assert(image_info != (const ImageInfo *) NULL);
1595   assert(image_info->signature == MagickSignature);
1596   assert(image != (Image *) NULL);
1597   assert(image->signature == MagickSignature);
1598   if (image->debug != MagickFalse)
1599     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1600   if ((image->columns > 65535L) || (image->rows > 65535L))
1601     ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1602   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1603   if (status == MagickFalse)
1604     return(status);
1605   if (image->colorspace != RGBColorspace)
1606     (void) TransformImageColorspace(image,RGBColorspace);
1607   /*
1608     Initialize image info.
1609   */
1610   size_rectangle.top=0;
1611   size_rectangle.left=0;
1612   size_rectangle.bottom=(short) image->rows;
1613   size_rectangle.right=(short) image->columns;
1614   frame_rectangle=size_rectangle;
1615   crop_rectangle=size_rectangle;
1616   source_rectangle=size_rectangle;
1617   destination_rectangle=size_rectangle;
1618   base_address=0xff;
1619   row_bytes=(unsigned short) (image->columns | 0x8000);
1620   bounds.top=0;
1621   bounds.left=0;
1622   bounds.bottom=(short) image->rows;
1623   bounds.right=(short) image->columns;
1624   pixmap.version=0;
1625   pixmap.pack_type=0;
1626   pixmap.pack_size=0;
1627   pixmap.pixel_type=0;
1628   pixmap.bits_per_pixel=8;
1629   pixmap.component_count=1;
1630   pixmap.component_size=8;
1631   pixmap.plane_bytes=0;
1632   pixmap.table=0;
1633   pixmap.reserved=0;
1634   transfer_mode=0;
1635   x_resolution=image->x_resolution != 0.0 ? image->x_resolution :
1636     DefaultResolution;
1637   y_resolution=image->y_resolution != 0.0 ? image->y_resolution :
1638     DefaultResolution;
1639   storage_class=image->storage_class;
1640   if (image_info->compression == JPEGCompression)
1641     storage_class=DirectClass;
1642   if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1643     {
1644       pixmap.component_count=image->matte ? 4 : 3;
1645       pixmap.pixel_type=16;
1646       pixmap.bits_per_pixel=32;
1647       pixmap.pack_type=0x04;
1648       transfer_mode=0x40;
1649       row_bytes=(unsigned short) ((4*image->columns) | 0x8000);
1650     }
1651   /*
1652     Allocate memory.
1653   */
1654   bytes_per_line=image->columns;
1655   if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1656     bytes_per_line*=image->matte ? 4 : 3;
1657   buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer));
1658   packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t)
1659    (row_bytes+MaxCount),sizeof(*packed_scanline));
1660   scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
1661   if ((buffer == (unsigned char *) NULL) ||
1662       (packed_scanline == (unsigned char *) NULL) ||
1663       (scanline == (unsigned char *) NULL))
1664     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1665   (void) ResetMagickMemory(scanline,0,row_bytes);
1666   (void) ResetMagickMemory(packed_scanline,0,(size_t) (row_bytes+MaxCount));
1667   /*
1668     Write header, header size, size bounding box, version, and reserved.
1669   */
1670   (void) ResetMagickMemory(buffer,0,PictInfoSize);
1671   (void) WriteBlob(image,PictInfoSize,buffer);
1672   (void) WriteBlobMSBShort(image,0);
1673   (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top);
1674   (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left);
1675   (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom);
1676   (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right);
1677   (void) WriteBlobMSBShort(image,PictVersion);
1678   (void) WriteBlobMSBShort(image,0x02ff);  /* version #2 */
1679   (void) WriteBlobMSBShort(image,PictInfoOp);
1680   (void) WriteBlobMSBLong(image,0xFFFE0000UL);
1681   /*
1682     Write full size of the file, resolution, frame bounding box, and reserved.
1683   */
1684   (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1685   (void) WriteBlobMSBShort(image,0x0000);
1686   (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1687   (void) WriteBlobMSBShort(image,0x0000);
1688   (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top);
1689   (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left);
1690   (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom);
1691   (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right);
1692   (void) WriteBlobMSBLong(image,0x00000000L);
1693   profile=GetImageProfile(image,"iptc");
1694   if (profile != (StringInfo *) NULL)
1695     {
1696       (void) WriteBlobMSBShort(image,0xa1);
1697       (void) WriteBlobMSBShort(image,0x1f2);
1698       (void) WriteBlobMSBShort(image,(unsigned short)
1699         (GetStringInfoLength(profile)+4));
1700       (void) WriteBlobString(image,"8BIM");
1701       (void) WriteBlob(image,GetStringInfoLength(profile),
1702         GetStringInfoDatum(profile));
1703     }
1704   profile=GetImageProfile(image,"icc");
1705   if (profile != (StringInfo *) NULL)
1706     {
1707       (void) WriteBlobMSBShort(image,0xa1);
1708       (void) WriteBlobMSBShort(image,0xe0);
1709       (void) WriteBlobMSBShort(image,(unsigned short)
1710         (GetStringInfoLength(profile)+4));
1711       (void) WriteBlobMSBLong(image,0x00000000UL);
1712       (void) WriteBlob(image,GetStringInfoLength(profile),
1713         GetStringInfoDatum(profile));
1714       (void) WriteBlobMSBShort(image,0xa1);
1715       (void) WriteBlobMSBShort(image,0xe0);
1716       (void) WriteBlobMSBShort(image,4);
1717       (void) WriteBlobMSBLong(image,0x00000002UL);
1718     }
1719   /*
1720     Write crop region opcode and crop bounding box.
1721   */
1722   (void) WriteBlobMSBShort(image,PictCropRegionOp);
1723   (void) WriteBlobMSBShort(image,0xa);
1724   (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top);
1725   (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left);
1726   (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom);
1727   (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right);
1728   if (image_info->compression == JPEGCompression)
1729     {
1730       Image
1731         *jpeg_image;
1732
1733       ImageInfo
1734         *jpeg_info;
1735
1736       size_t
1737         length;
1738
1739       unsigned char
1740         *blob;
1741
1742       jpeg_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1743       if (jpeg_image == (Image *) NULL)
1744         {
1745           (void) CloseBlob(image);
1746           return(MagickFalse);
1747         }
1748       jpeg_info=CloneImageInfo(image_info);
1749       (void) CopyMagickString(jpeg_info->magick,"JPEG",MaxTextExtent);
1750       length=0;
1751       blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length,
1752         &image->exception);
1753       jpeg_info=DestroyImageInfo(jpeg_info);
1754       if (blob == (unsigned char *) NULL)
1755         return(MagickFalse);
1756       jpeg_image=DestroyImage(jpeg_image);
1757       (void) WriteBlobMSBShort(image,PictJPEGOp);
1758       (void) WriteBlobMSBLong(image,(unsigned int) length+154);
1759       (void) WriteBlobMSBShort(image,0x0000);
1760       (void) WriteBlobMSBLong(image,0x00010000UL);
1761       (void) WriteBlobMSBLong(image,0x00000000UL);
1762       (void) WriteBlobMSBLong(image,0x00000000UL);
1763       (void) WriteBlobMSBLong(image,0x00000000UL);
1764       (void) WriteBlobMSBLong(image,0x00010000UL);
1765       (void) WriteBlobMSBLong(image,0x00000000UL);
1766       (void) WriteBlobMSBLong(image,0x00000000UL);
1767       (void) WriteBlobMSBLong(image,0x00000000UL);
1768       (void) WriteBlobMSBLong(image,0x40000000UL);
1769       (void) WriteBlobMSBLong(image,0x00000000UL);
1770       (void) WriteBlobMSBLong(image,0x00000000UL);
1771       (void) WriteBlobMSBLong(image,0x00000000UL);
1772       (void) WriteBlobMSBLong(image,0x00400000UL);
1773       (void) WriteBlobMSBShort(image,0x0000);
1774       (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1775       (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1776       (void) WriteBlobMSBShort(image,0x0000);
1777       (void) WriteBlobMSBShort(image,768);
1778       (void) WriteBlobMSBShort(image,0x0000);
1779       (void) WriteBlobMSBLong(image,0x00000000UL);
1780       (void) WriteBlobMSBLong(image,0x00566A70UL);
1781       (void) WriteBlobMSBLong(image,0x65670000UL);
1782       (void) WriteBlobMSBLong(image,0x00000000UL);
1783       (void) WriteBlobMSBLong(image,0x00000001UL);
1784       (void) WriteBlobMSBLong(image,0x00016170UL);
1785       (void) WriteBlobMSBLong(image,0x706C0000UL);
1786       (void) WriteBlobMSBLong(image,0x00000000UL);
1787       (void) WriteBlobMSBShort(image,768);
1788       (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1789       (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1790       (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1791       (void) WriteBlobMSBShort(image,0x0000);
1792       (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1793       (void) WriteBlobMSBLong(image,0x00000000UL);
1794       (void) WriteBlobMSBLong(image,0x87AC0001UL);
1795       (void) WriteBlobMSBLong(image,0x0B466F74UL);
1796       (void) WriteBlobMSBLong(image,0x6F202D20UL);
1797       (void) WriteBlobMSBLong(image,0x4A504547UL);
1798       (void) WriteBlobMSBLong(image,0x00000000UL);
1799       (void) WriteBlobMSBLong(image,0x00000000UL);
1800       (void) WriteBlobMSBLong(image,0x00000000UL);
1801       (void) WriteBlobMSBLong(image,0x00000000UL);
1802       (void) WriteBlobMSBLong(image,0x00000000UL);
1803       (void) WriteBlobMSBLong(image,0x0018FFFFUL);
1804       (void) WriteBlob(image,length,blob);
1805       if ((length & 0x01) != 0)
1806         (void) WriteBlobByte(image,'\0');
1807       blob=(unsigned char *) RelinquishMagickMemory(blob);
1808     }
1809   /*
1810     Write picture opcode, row bytes, and picture bounding box, and version.
1811   */
1812   if (storage_class == PseudoClass)
1813     (void) WriteBlobMSBShort(image,PictPICTOp);
1814   else
1815     {
1816       (void) WriteBlobMSBShort(image,PictPixmapOp);
1817       (void) WriteBlobMSBLong(image,(size_t) base_address);
1818     }
1819   (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000));
1820   (void) WriteBlobMSBShort(image,(unsigned short) bounds.top);
1821   (void) WriteBlobMSBShort(image,(unsigned short) bounds.left);
1822   (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom);
1823   (void) WriteBlobMSBShort(image,(unsigned short) bounds.right);
1824   /*
1825     Write pack type, pack size, resolution, pixel type, and pixel size.
1826   */
1827   (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version);
1828   (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type);
1829   (void) WriteBlobMSBLong(image,(unsigned int) pixmap.pack_size);
1830   (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5));
1831   (void) WriteBlobMSBShort(image,0x0000);
1832   (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5));
1833   (void) WriteBlobMSBShort(image,0x0000);
1834   (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type);
1835   (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel);
1836   /*
1837     Write component count, size, plane bytes, table size, and reserved.
1838   */
1839   (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count);
1840   (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size);
1841   (void) WriteBlobMSBLong(image,(unsigned int) pixmap.plane_bytes);
1842   (void) WriteBlobMSBLong(image,(unsigned int) pixmap.table);
1843   (void) WriteBlobMSBLong(image,(unsigned int) pixmap.reserved);
1844   if (storage_class == PseudoClass)
1845     {
1846       /*
1847         Write image colormap.
1848       */
1849       (void) WriteBlobMSBLong(image,0x00000000L);  /* color seed */
1850       (void) WriteBlobMSBShort(image,0L);  /* color flags */
1851       (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1));
1852       for (i=0; i < (ssize_t) image->colors; i++)
1853       {
1854         (void) WriteBlobMSBShort(image,(unsigned short) i);
1855         (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1856           image->colormap[i].red));
1857         (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1858           image->colormap[i].green));
1859         (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1860           image->colormap[i].blue));
1861       }
1862     }
1863   /*
1864     Write source and destination rectangle.
1865   */
1866   (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top);
1867   (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left);
1868   (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom);
1869   (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right);
1870   (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top);
1871   (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left);
1872   (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom);
1873   (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right);
1874   (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode);
1875   /*
1876     Write picture data.
1877   */
1878   count=0;
1879   if ((storage_class == PseudoClass) && (image->matte == MagickFalse))
1880     for (y=0; y < (ssize_t) image->rows; y++)
1881     {
1882       p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1883       if (p == (const PixelPacket *) NULL)
1884         break;
1885       indexes=GetVirtualIndexQueue(image);
1886       for (x=0; x < (ssize_t) image->columns; x++)
1887         scanline[x]=(unsigned char) indexes[x];
1888       count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
1889         packed_scanline);
1890       if (image->previous == (Image *) NULL)
1891         {
1892           status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1893                 image->rows);
1894           if (status == MagickFalse)
1895             break;
1896         }
1897     }
1898   else
1899     if (image_info->compression == JPEGCompression)
1900       {
1901         (void) ResetMagickMemory(scanline,0,row_bytes);
1902         for (y=0; y < (ssize_t) image->rows; y++)
1903           count+=EncodeImage(image,scanline,(size_t)
1904             (row_bytes & 0x7FFF),packed_scanline);
1905       }
1906     else
1907       {
1908         register unsigned char
1909           *blue,
1910           *green,
1911           *opacity,
1912           *red;
1913
1914         red=scanline;
1915         green=scanline+image->columns;
1916         blue=scanline+2*image->columns;
1917         opacity=scanline+3*image->columns;
1918         for (y=0; y < (ssize_t) image->rows; y++)
1919         {
1920           p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1921           if (p == (const PixelPacket *) NULL)
1922             break;
1923           red=scanline;
1924           green=scanline+image->columns;
1925           blue=scanline+2*image->columns;
1926           if (image->matte != MagickFalse)
1927             {
1928               opacity=scanline;
1929               red=scanline+image->columns;
1930               green=scanline+2*image->columns;
1931               blue=scanline+3*image->columns;
1932             }
1933           for (x=0; x < (ssize_t) image->columns; x++)
1934           {
1935             *red++=ScaleQuantumToChar(GetRedPixelComponent(p));
1936             *green++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1937             *blue++=ScaleQuantumToChar(GetBluePixelComponent(p));
1938             if (image->matte != MagickFalse)
1939               *opacity++=ScaleQuantumToChar((Quantum)
1940                 (GetAlphaPixelComponent(p)));
1941             p++;
1942           }
1943           count+=EncodeImage(image,scanline,bytes_per_line & 0x7FFF,
1944             packed_scanline);
1945           if (image->previous == (Image *) NULL)
1946             {
1947               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1948                 image->rows);
1949               if (status == MagickFalse)
1950                 break;
1951             }
1952         }
1953       }
1954   if ((count & 0x01) != 0)
1955     (void) WriteBlobByte(image,'\0');
1956   (void) WriteBlobMSBShort(image,PictEndOfPictureOp);
1957   offset=TellBlob(image);
1958   offset=SeekBlob(image,512,SEEK_SET);
1959   (void) WriteBlobMSBShort(image,(unsigned short) offset);
1960   scanline=(unsigned char *) RelinquishMagickMemory(scanline);
1961   packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline);
1962   buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1963   (void) CloseBlob(image);
1964   return(MagickTrue);
1965 }