]> granicus.if.org Git - imagemagick/blob - coders/pict.c
(no commit message)
[imagemagick] / coders / pict.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                        PPPP   IIIII   CCCC  TTTTT                           %
7 %                        P   P    I    C        T                             %
8 %                        PPPP     I    C        T                             %
9 %                        P        I    C        T                             %
10 %                        P      IIIII   CCCC    T                             %
11 %                                                                             %
12 %                                                                             %
13 %               Read/Write Apple Macintosh QuickDraw/PICT Format              %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
28 %  Unless required by applicable law or agreed to in writing, software        %
29 %  distributed under the License is distributed on an "AS IS" BASIS,          %
30 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31 %  See the License for the specific language governing permissions and        %
32 %  limitations under the License.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
42 #include "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                           SetOpacityPixelComponent(q,(Quantum) (QuantumRange-
1217                             ScaleCharToQuantum(*p)));
1218                           SetRedPixelComponent(q,ScaleCharToQuantum(
1219                             *(p+tile_image->columns)));
1220                           SetGreenPixelComponent(q,ScaleCharToQuantum(
1221                             *(p+2*tile_image->columns)));
1222                           SetBluePixelComponent(q,ScaleCharToQuantum(
1223                             *(p+3*tile_image->columns)));
1224                         }
1225                   }
1226                 p++;
1227                 q++;
1228               }
1229               if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
1230                 break;
1231               if ((tile_image->storage_class == DirectClass) &&
1232                   (pixmap.bits_per_pixel != 16))
1233                 {
1234                   p+=(pixmap.component_count-1)*tile_image->columns;
1235                   if (p < pixels)
1236                     break;
1237                 }
1238               status=SetImageProgress(image,LoadImageTag,y,tile_image->rows);
1239               if (status == MagickFalse)
1240                 break;
1241             }
1242             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1243             if (jpeg == MagickFalse)
1244               if ((code == 0x9a) || (code == 0x9b) ||
1245                   ((bytes_per_line & 0x8000) != 0))
1246                 (void) CompositeImage(image,CopyCompositeOp,tile_image,
1247                   destination.left,destination.top);
1248             tile_image=DestroyImage(tile_image);
1249             break;
1250           }
1251           case 0xa1:
1252           {
1253             unsigned char
1254               *info;
1255
1256             size_t
1257               type;
1258
1259             /*
1260               Comment.
1261             */
1262             type=ReadBlobMSBShort(image);
1263             length=ReadBlobMSBShort(image);
1264             if (length == 0)
1265               break;
1266             (void) ReadBlobMSBLong(image);
1267             length-=4;
1268             if (length == 0)
1269               break;
1270             info=(unsigned char *) AcquireQuantumMemory(length,sizeof(*info));
1271             if (info == (unsigned char *) NULL)
1272               break;
1273             count=ReadBlob(image,length,info);
1274             (void) count;
1275             switch (type)
1276             {
1277               case 0xe0:
1278               {
1279                 if (length == 0)
1280                   break;
1281                 profile=AcquireStringInfo(length);
1282                 SetStringInfoDatum(profile,info);
1283                 status=SetImageProfile(image,"icc",profile);
1284                 profile=DestroyStringInfo(profile);
1285                 if (status == MagickFalse)
1286                   ThrowReaderException(ResourceLimitError,
1287                     "MemoryAllocationFailed");
1288                 break;
1289               }
1290               case 0x1f2:
1291               {
1292                 if (length == 0)
1293                   break;
1294                 profile=AcquireStringInfo(length);
1295                 SetStringInfoDatum(profile,info);
1296                 status=SetImageProfile(image,"iptc",profile);
1297                 if (status == MagickFalse)
1298                   ThrowReaderException(ResourceLimitError,
1299                     "MemoryAllocationFailed");
1300                 profile=DestroyStringInfo(profile);
1301                 break;
1302               }
1303               default:
1304                 break;
1305             }
1306             info=(unsigned char *) RelinquishMagickMemory(info);
1307             break;
1308           }
1309           default:
1310           {
1311             /*
1312               Skip to next op code.
1313             */
1314             if (codes[code].length == -1)
1315               (void) ReadBlobMSBShort(image);
1316             else
1317               for (i=0; i < (ssize_t) codes[code].length; i++)
1318                 (void) ReadBlobByte(image);
1319           }
1320         }
1321       }
1322     if (code == 0xc00)
1323       {
1324         /*
1325           Skip header.
1326         */
1327         for (i=0; i < 24; i++)
1328           (void) ReadBlobByte(image);
1329         continue;
1330       }
1331     if (((code >= 0xb0) && (code <= 0xcf)) ||
1332         ((code >= 0x8000) && (code <= 0x80ff)))
1333       continue;
1334     if (code == 0x8200)
1335       {
1336         FILE
1337           *file;
1338
1339         Image
1340           *tile_image;
1341
1342         ImageInfo
1343           *read_info;
1344
1345         int
1346           unique_file;
1347
1348         /*
1349           Embedded JPEG.
1350         */
1351         jpeg=MagickTrue;
1352         read_info=CloneImageInfo(image_info);
1353         SetImageInfoBlob(read_info,(void *) NULL,0);
1354         file=(FILE *) NULL;
1355         unique_file=AcquireUniqueFileResource(read_info->filename);
1356         if (unique_file != -1)
1357           file=fdopen(unique_file,"wb");
1358         if ((unique_file == -1) || (file == (FILE *) NULL))
1359           {
1360             (void) CopyMagickString(image->filename,read_info->filename,
1361               MaxTextExtent);
1362             ThrowFileException(exception,FileOpenError,
1363               "UnableToCreateTemporaryFile",image->filename);
1364             image=DestroyImageList(image);
1365             return((Image *) NULL);
1366           }
1367         length=ReadBlobMSBLong(image);
1368         for (i=0; i < 6; i++)
1369           (void) ReadBlobMSBLong(image);
1370         ReadRectangle(image,frame);
1371         for (i=0; i < 122; i++)
1372           (void) ReadBlobByte(image);
1373         for (i=0; i < (ssize_t) (length-154); i++)
1374         {
1375           c=ReadBlobByte(image);
1376           (void) fputc(c,file);
1377         }
1378         (void) fclose(file);
1379         tile_image=ReadImage(read_info,exception);
1380         (void) RelinquishUniqueFileResource(read_info->filename);
1381         read_info=DestroyImageInfo(read_info);
1382         if (tile_image == (Image *) NULL)
1383           continue;
1384         (void) FormatMagickString(geometry,MaxTextExtent,"%.20gx%.20g",
1385           (double) MagickMax(image->columns,tile_image->columns),
1386           (double) MagickMax(image->rows,tile_image->rows));
1387         (void) SetImageExtent(image,
1388           MagickMax(image->columns,tile_image->columns),
1389           MagickMax(image->rows,tile_image->rows));
1390         if (image->colorspace != RGBColorspace)
1391           (void) TransformImageColorspace(image,tile_image->colorspace);
1392         (void) CompositeImage(image,CopyCompositeOp,tile_image,frame.left,
1393           frame.right);
1394         image->compression=tile_image->compression;
1395         tile_image=DestroyImage(tile_image);
1396         continue;
1397       }
1398     if ((code == 0xff) || (code == 0xffff))
1399       break;
1400     if (((code >= 0xd0) && (code <= 0xfe)) ||
1401         ((code >= 0x8100) && (code <= 0xffff)))
1402       {
1403         /*
1404           Skip reserved.
1405         */
1406         length=ReadBlobMSBShort(image);
1407         for (i=0; i < (ssize_t) length; i++)
1408           (void) ReadBlobByte(image);
1409         continue;
1410       }
1411     if ((code >= 0x100) && (code <= 0x7fff))
1412       {
1413         /*
1414           Skip reserved.
1415         */
1416         length=(size_t) ((code >> 7) & 0xff);
1417         for (i=0; i < (ssize_t) length; i++)
1418           (void) ReadBlobByte(image);
1419         continue;
1420       }
1421   }
1422   (void) CloseBlob(image);
1423   return(GetFirstImageInList(image));
1424 }
1425 #endif
1426 \f
1427 /*
1428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1429 %                                                                             %
1430 %                                                                             %
1431 %                                                                             %
1432 %   R e g i s t e r P I C T I m a g e                                         %
1433 %                                                                             %
1434 %                                                                             %
1435 %                                                                             %
1436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1437 %
1438 %  RegisterPICTImage() adds attributes for the PICT image format to
1439 %  the list of supported formats.  The attributes include the image format
1440 %  tag, a method to read and/or write the format, whether the format
1441 %  supports the saving of more than one frame to the same file or blob,
1442 %  whether the format supports native in-memory I/O, and a brief
1443 %  description of the format.
1444 %
1445 %  The format of the RegisterPICTImage method is:
1446 %
1447 %      size_t RegisterPICTImage(void)
1448 %
1449 */
1450 ModuleExport size_t RegisterPICTImage(void)
1451 {
1452   MagickInfo
1453     *entry;
1454
1455   entry=SetMagickInfo("PCT");
1456   entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1457   entry->encoder=(EncodeImageHandler *) WritePICTImage;
1458   entry->adjoin=MagickFalse;
1459   entry->seekable_stream=MagickTrue;
1460   entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1461   entry->magick=(IsImageFormatHandler *) IsPICT;
1462   entry->module=ConstantString("PICT");
1463   (void) RegisterMagickInfo(entry);
1464   entry=SetMagickInfo("PICT");
1465   entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1466   entry->encoder=(EncodeImageHandler *) WritePICTImage;
1467   entry->adjoin=MagickFalse;
1468   entry->seekable_stream=MagickTrue;
1469   entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1470   entry->magick=(IsImageFormatHandler *) IsPICT;
1471   entry->module=ConstantString("PICT");
1472   (void) RegisterMagickInfo(entry);
1473   return(MagickImageCoderSignature);
1474 }
1475 \f
1476 /*
1477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1478 %                                                                             %
1479 %                                                                             %
1480 %                                                                             %
1481 %   U n r e g i s t e r P I C T I m a g e                                     %
1482 %                                                                             %
1483 %                                                                             %
1484 %                                                                             %
1485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1486 %
1487 %  UnregisterPICTImage() removes format registrations made by the
1488 %  PICT module from the list of supported formats.
1489 %
1490 %  The format of the UnregisterPICTImage method is:
1491 %
1492 %      UnregisterPICTImage(void)
1493 %
1494 */
1495 ModuleExport void UnregisterPICTImage(void)
1496 {
1497   (void) UnregisterMagickInfo("PCT");
1498   (void) UnregisterMagickInfo("PICT");
1499 }
1500 \f
1501 /*
1502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1503 %                                                                             %
1504 %                                                                             %
1505 %                                                                             %
1506 %   W r i t e P I C T I m a g e                                               %
1507 %                                                                             %
1508 %                                                                             %
1509 %                                                                             %
1510 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1511 %
1512 %  WritePICTImage() writes an image to a file in the Apple Macintosh
1513 %  QuickDraw/PICT image format.
1514 %
1515 %  The format of the WritePICTImage method is:
1516 %
1517 %      MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1518 %        Image *image)
1519 %
1520 %  A description of each parameter follows.
1521 %
1522 %    o image_info: the image info.
1523 %
1524 %    o image:  The image.
1525 %
1526 */
1527 static MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1528   Image *image)
1529 {
1530 #define MaxCount  128
1531 #define PictCropRegionOp  0x01
1532 #define PictEndOfPictureOp  0xff
1533 #define PictJPEGOp  0x8200
1534 #define PictInfoOp  0x0C00
1535 #define PictInfoSize  512
1536 #define PictPixmapOp  0x9A
1537 #define PictPICTOp  0x98
1538 #define PictVersion  0x11
1539
1540   const StringInfo
1541     *profile;
1542
1543   double
1544     x_resolution,
1545     y_resolution;
1546
1547   MagickBooleanType
1548     status;
1549
1550   MagickOffsetType
1551     offset;
1552
1553   PICTPixmap
1554     pixmap;
1555
1556   PICTRectangle
1557     bounds,
1558     crop_rectangle,
1559     destination_rectangle,
1560     frame_rectangle,
1561     size_rectangle,
1562     source_rectangle;
1563
1564   register const IndexPacket
1565     *indexes;
1566
1567   register const PixelPacket
1568     *p;
1569
1570   register ssize_t
1571     i,
1572     x;
1573
1574   size_t
1575     bytes_per_line,
1576     count,
1577     storage_class;
1578
1579   ssize_t
1580     y;
1581
1582   unsigned char
1583     *buffer,
1584     *packed_scanline,
1585     *scanline;
1586
1587
1588   unsigned short
1589     base_address,
1590     row_bytes,
1591     transfer_mode;
1592
1593   /*
1594     Open output image file.
1595   */
1596   assert(image_info != (const ImageInfo *) NULL);
1597   assert(image_info->signature == MagickSignature);
1598   assert(image != (Image *) NULL);
1599   assert(image->signature == MagickSignature);
1600   if (image->debug != MagickFalse)
1601     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1602   if ((image->columns > 65535L) || (image->rows > 65535L))
1603     ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1604   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1605   if (status == MagickFalse)
1606     return(status);
1607   if (image->colorspace != RGBColorspace)
1608     (void) TransformImageColorspace(image,RGBColorspace);
1609   /*
1610     Initialize image info.
1611   */
1612   size_rectangle.top=0;
1613   size_rectangle.left=0;
1614   size_rectangle.bottom=(short) image->rows;
1615   size_rectangle.right=(short) image->columns;
1616   frame_rectangle=size_rectangle;
1617   crop_rectangle=size_rectangle;
1618   source_rectangle=size_rectangle;
1619   destination_rectangle=size_rectangle;
1620   base_address=0xff;
1621   row_bytes=(unsigned short) (image->columns | 0x8000);
1622   bounds.top=0;
1623   bounds.left=0;
1624   bounds.bottom=(short) image->rows;
1625   bounds.right=(short) image->columns;
1626   pixmap.version=0;
1627   pixmap.pack_type=0;
1628   pixmap.pack_size=0;
1629   pixmap.pixel_type=0;
1630   pixmap.bits_per_pixel=8;
1631   pixmap.component_count=1;
1632   pixmap.component_size=8;
1633   pixmap.plane_bytes=0;
1634   pixmap.table=0;
1635   pixmap.reserved=0;
1636   transfer_mode=0;
1637   x_resolution=image->x_resolution != 0.0 ? image->x_resolution :
1638     DefaultResolution;
1639   y_resolution=image->y_resolution != 0.0 ? image->y_resolution :
1640     DefaultResolution;
1641   storage_class=image->storage_class;
1642   if (image_info->compression == JPEGCompression)
1643     storage_class=DirectClass;
1644   if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1645     {
1646       pixmap.component_count=image->matte ? 4 : 3;
1647       pixmap.pixel_type=16;
1648       pixmap.bits_per_pixel=32;
1649       pixmap.pack_type=0x04;
1650       transfer_mode=0x40;
1651       row_bytes=(unsigned short) ((4*image->columns) | 0x8000);
1652     }
1653   /*
1654     Allocate memory.
1655   */
1656   bytes_per_line=image->columns;
1657   if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1658     bytes_per_line*=image->matte ? 4 : 3;
1659   buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer));
1660   packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t)
1661    (row_bytes+MaxCount),sizeof(*packed_scanline));
1662   scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
1663   if ((buffer == (unsigned char *) NULL) ||
1664       (packed_scanline == (unsigned char *) NULL) ||
1665       (scanline == (unsigned char *) NULL))
1666     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1667   (void) ResetMagickMemory(scanline,0,row_bytes);
1668   (void) ResetMagickMemory(packed_scanline,0,(size_t) (row_bytes+MaxCount));
1669   /*
1670     Write header, header size, size bounding box, version, and reserved.
1671   */
1672   (void) ResetMagickMemory(buffer,0,PictInfoSize);
1673   (void) WriteBlob(image,PictInfoSize,buffer);
1674   (void) WriteBlobMSBShort(image,0);
1675   (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top);
1676   (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left);
1677   (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom);
1678   (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right);
1679   (void) WriteBlobMSBShort(image,PictVersion);
1680   (void) WriteBlobMSBShort(image,0x02ff);  /* version #2 */
1681   (void) WriteBlobMSBShort(image,PictInfoOp);
1682   (void) WriteBlobMSBLong(image,0xFFFE0000UL);
1683   /*
1684     Write full size of the file, resolution, frame bounding box, and reserved.
1685   */
1686   (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1687   (void) WriteBlobMSBShort(image,0x0000);
1688   (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1689   (void) WriteBlobMSBShort(image,0x0000);
1690   (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top);
1691   (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left);
1692   (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom);
1693   (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right);
1694   (void) WriteBlobMSBLong(image,0x00000000L);
1695   profile=GetImageProfile(image,"iptc");
1696   if (profile != (StringInfo *) NULL)
1697     {
1698       (void) WriteBlobMSBShort(image,0xa1);
1699       (void) WriteBlobMSBShort(image,0x1f2);
1700       (void) WriteBlobMSBShort(image,(unsigned short)
1701         (GetStringInfoLength(profile)+4));
1702       (void) WriteBlobString(image,"8BIM");
1703       (void) WriteBlob(image,GetStringInfoLength(profile),
1704         GetStringInfoDatum(profile));
1705     }
1706   profile=GetImageProfile(image,"icc");
1707   if (profile != (StringInfo *) NULL)
1708     {
1709       (void) WriteBlobMSBShort(image,0xa1);
1710       (void) WriteBlobMSBShort(image,0xe0);
1711       (void) WriteBlobMSBShort(image,(unsigned short)
1712         (GetStringInfoLength(profile)+4));
1713       (void) WriteBlobMSBLong(image,0x00000000UL);
1714       (void) WriteBlob(image,GetStringInfoLength(profile),
1715         GetStringInfoDatum(profile));
1716       (void) WriteBlobMSBShort(image,0xa1);
1717       (void) WriteBlobMSBShort(image,0xe0);
1718       (void) WriteBlobMSBShort(image,4);
1719       (void) WriteBlobMSBLong(image,0x00000002UL);
1720     }
1721   /*
1722     Write crop region opcode and crop bounding box.
1723   */
1724   (void) WriteBlobMSBShort(image,PictCropRegionOp);
1725   (void) WriteBlobMSBShort(image,0xa);
1726   (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top);
1727   (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left);
1728   (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom);
1729   (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right);
1730   if (image_info->compression == JPEGCompression)
1731     {
1732       Image
1733         *jpeg_image;
1734
1735       ImageInfo
1736         *jpeg_info;
1737
1738       size_t
1739         length;
1740
1741       unsigned char
1742         *blob;
1743
1744       jpeg_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1745       if (jpeg_image == (Image *) NULL)
1746         {
1747           (void) CloseBlob(image);
1748           return(MagickFalse);
1749         }
1750       jpeg_info=CloneImageInfo(image_info);
1751       (void) CopyMagickString(jpeg_info->magick,"JPEG",MaxTextExtent);
1752       length=0;
1753       blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length,
1754         &image->exception);
1755       jpeg_info=DestroyImageInfo(jpeg_info);
1756       if (blob == (unsigned char *) NULL)
1757         return(MagickFalse);
1758       jpeg_image=DestroyImage(jpeg_image);
1759       (void) WriteBlobMSBShort(image,PictJPEGOp);
1760       (void) WriteBlobMSBLong(image,(unsigned int) length+154);
1761       (void) WriteBlobMSBShort(image,0x0000);
1762       (void) WriteBlobMSBLong(image,0x00010000UL);
1763       (void) WriteBlobMSBLong(image,0x00000000UL);
1764       (void) WriteBlobMSBLong(image,0x00000000UL);
1765       (void) WriteBlobMSBLong(image,0x00000000UL);
1766       (void) WriteBlobMSBLong(image,0x00010000UL);
1767       (void) WriteBlobMSBLong(image,0x00000000UL);
1768       (void) WriteBlobMSBLong(image,0x00000000UL);
1769       (void) WriteBlobMSBLong(image,0x00000000UL);
1770       (void) WriteBlobMSBLong(image,0x40000000UL);
1771       (void) WriteBlobMSBLong(image,0x00000000UL);
1772       (void) WriteBlobMSBLong(image,0x00000000UL);
1773       (void) WriteBlobMSBLong(image,0x00000000UL);
1774       (void) WriteBlobMSBLong(image,0x00400000UL);
1775       (void) WriteBlobMSBShort(image,0x0000);
1776       (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1777       (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1778       (void) WriteBlobMSBShort(image,0x0000);
1779       (void) WriteBlobMSBShort(image,768);
1780       (void) WriteBlobMSBShort(image,0x0000);
1781       (void) WriteBlobMSBLong(image,0x00000000UL);
1782       (void) WriteBlobMSBLong(image,0x00566A70UL);
1783       (void) WriteBlobMSBLong(image,0x65670000UL);
1784       (void) WriteBlobMSBLong(image,0x00000000UL);
1785       (void) WriteBlobMSBLong(image,0x00000001UL);
1786       (void) WriteBlobMSBLong(image,0x00016170UL);
1787       (void) WriteBlobMSBLong(image,0x706C0000UL);
1788       (void) WriteBlobMSBLong(image,0x00000000UL);
1789       (void) WriteBlobMSBShort(image,768);
1790       (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1791       (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1792       (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1793       (void) WriteBlobMSBShort(image,0x0000);
1794       (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1795       (void) WriteBlobMSBLong(image,0x00000000UL);
1796       (void) WriteBlobMSBLong(image,0x87AC0001UL);
1797       (void) WriteBlobMSBLong(image,0x0B466F74UL);
1798       (void) WriteBlobMSBLong(image,0x6F202D20UL);
1799       (void) WriteBlobMSBLong(image,0x4A504547UL);
1800       (void) WriteBlobMSBLong(image,0x00000000UL);
1801       (void) WriteBlobMSBLong(image,0x00000000UL);
1802       (void) WriteBlobMSBLong(image,0x00000000UL);
1803       (void) WriteBlobMSBLong(image,0x00000000UL);
1804       (void) WriteBlobMSBLong(image,0x00000000UL);
1805       (void) WriteBlobMSBLong(image,0x0018FFFFUL);
1806       (void) WriteBlob(image,length,blob);
1807       if ((length & 0x01) != 0)
1808         (void) WriteBlobByte(image,'\0');
1809       blob=(unsigned char *) RelinquishMagickMemory(blob);
1810     }
1811   /*
1812     Write picture opcode, row bytes, and picture bounding box, and version.
1813   */
1814   if (storage_class == PseudoClass)
1815     (void) WriteBlobMSBShort(image,PictPICTOp);
1816   else
1817     {
1818       (void) WriteBlobMSBShort(image,PictPixmapOp);
1819       (void) WriteBlobMSBLong(image,(size_t) base_address);
1820     }
1821   (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000));
1822   (void) WriteBlobMSBShort(image,(unsigned short) bounds.top);
1823   (void) WriteBlobMSBShort(image,(unsigned short) bounds.left);
1824   (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom);
1825   (void) WriteBlobMSBShort(image,(unsigned short) bounds.right);
1826   /*
1827     Write pack type, pack size, resolution, pixel type, and pixel size.
1828   */
1829   (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version);
1830   (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type);
1831   (void) WriteBlobMSBLong(image,(unsigned int) pixmap.pack_size);
1832   (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5));
1833   (void) WriteBlobMSBShort(image,0x0000);
1834   (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5));
1835   (void) WriteBlobMSBShort(image,0x0000);
1836   (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type);
1837   (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel);
1838   /*
1839     Write component count, size, plane bytes, table size, and reserved.
1840   */
1841   (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count);
1842   (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size);
1843   (void) WriteBlobMSBLong(image,(unsigned int) pixmap.plane_bytes);
1844   (void) WriteBlobMSBLong(image,(unsigned int) pixmap.table);
1845   (void) WriteBlobMSBLong(image,(unsigned int) pixmap.reserved);
1846   if (storage_class == PseudoClass)
1847     {
1848       /*
1849         Write image colormap.
1850       */
1851       (void) WriteBlobMSBLong(image,0x00000000L);  /* color seed */
1852       (void) WriteBlobMSBShort(image,0L);  /* color flags */
1853       (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1));
1854       for (i=0; i < (ssize_t) image->colors; i++)
1855       {
1856         (void) WriteBlobMSBShort(image,(unsigned short) i);
1857         (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1858           image->colormap[i].red));
1859         (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1860           image->colormap[i].green));
1861         (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1862           image->colormap[i].blue));
1863       }
1864     }
1865   /*
1866     Write source and destination rectangle.
1867   */
1868   (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top);
1869   (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left);
1870   (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom);
1871   (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right);
1872   (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top);
1873   (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left);
1874   (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom);
1875   (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right);
1876   (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode);
1877   /*
1878     Write picture data.
1879   */
1880   count=0;
1881   if ((storage_class == PseudoClass) && (image->matte == MagickFalse))
1882     for (y=0; y < (ssize_t) image->rows; y++)
1883     {
1884       p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1885       if (p == (const PixelPacket *) NULL)
1886         break;
1887       indexes=GetVirtualIndexQueue(image);
1888       for (x=0; x < (ssize_t) image->columns; x++)
1889         scanline[x]=(unsigned char) GetIndexPixelComponent(indexes+x);
1890       count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
1891         packed_scanline);
1892       if (image->previous == (Image *) NULL)
1893         {
1894           status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1895             image->rows);
1896           if (status == MagickFalse)
1897             break;
1898         }
1899     }
1900   else
1901     if (image_info->compression == JPEGCompression)
1902       {
1903         (void) ResetMagickMemory(scanline,0,row_bytes);
1904         for (y=0; y < (ssize_t) image->rows; y++)
1905           count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
1906             packed_scanline);
1907       }
1908     else
1909       {
1910         register unsigned char
1911           *blue,
1912           *green,
1913           *opacity,
1914           *red;
1915
1916         red=scanline;
1917         green=scanline+image->columns;
1918         blue=scanline+2*image->columns;
1919         opacity=scanline+3*image->columns;
1920         for (y=0; y < (ssize_t) image->rows; y++)
1921         {
1922           p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1923           if (p == (const PixelPacket *) NULL)
1924             break;
1925           red=scanline;
1926           green=scanline+image->columns;
1927           blue=scanline+2*image->columns;
1928           if (image->matte != MagickFalse)
1929             {
1930               opacity=scanline;
1931               red=scanline+image->columns;
1932               green=scanline+2*image->columns;
1933               blue=scanline+3*image->columns;
1934             }
1935           for (x=0; x < (ssize_t) image->columns; x++)
1936           {
1937             *red++=ScaleQuantumToChar(GetRedPixelComponent(p));
1938             *green++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1939             *blue++=ScaleQuantumToChar(GetBluePixelComponent(p));
1940             if (image->matte != MagickFalse)
1941               *opacity++=ScaleQuantumToChar((Quantum)
1942                 (GetAlphaPixelComponent(p)));
1943             p++;
1944           }
1945           count+=EncodeImage(image,scanline,bytes_per_line & 0x7FFF,
1946             packed_scanline);
1947           if (image->previous == (Image *) NULL)
1948             {
1949               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1950                 image->rows);
1951               if (status == MagickFalse)
1952                 break;
1953             }
1954         }
1955       }
1956   if ((count & 0x01) != 0)
1957     (void) WriteBlobByte(image,'\0');
1958   (void) WriteBlobMSBShort(image,PictEndOfPictureOp);
1959   offset=TellBlob(image);
1960   offset=SeekBlob(image,512,SEEK_SET);
1961   (void) WriteBlobMSBShort(image,(unsigned short) offset);
1962   scanline=(unsigned char *) RelinquishMagickMemory(scanline);
1963   packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline);
1964   buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1965   (void) CloseBlob(image);
1966   return(MagickTrue);
1967 }