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