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