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