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