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