2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % PPPP IIIII CCCC TTTTT %
13 % Read/Write Apple Macintosh QuickDraw/PICT Format %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/color-private.h"
47 #include "magick/colormap.h"
48 #include "magick/colormap-private.h"
49 #include "magick/colorspace.h"
50 #include "magick/composite.h"
51 #include "magick/constitute.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/image.h"
55 #include "magick/image-private.h"
56 #include "magick/list.h"
57 #include "magick/log.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/monitor.h"
61 #include "magick/monitor-private.h"
62 #include "magick/profile.h"
63 #include "magick/resource_.h"
64 #include "magick/quantum-private.h"
65 #include "magick/static.h"
66 #include "magick/string_.h"
67 #include "magick/module.h"
68 #include "magick/transform.h"
69 #include "magick/utility.h"
72 ImageMagick Macintosh PICT Methods.
74 #define ReadPixmap(pixmap) \
76 pixmap.version=(short) ReadBlobMSBShort(image); \
77 pixmap.pack_type=(short) ReadBlobMSBShort(image); \
78 pixmap.pack_size=ReadBlobMSBLong(image); \
79 pixmap.horizontal_resolution=1UL*ReadBlobMSBShort(image); \
80 (void) ReadBlobMSBShort(image); \
81 pixmap.vertical_resolution=1UL*ReadBlobMSBShort(image); \
82 (void) ReadBlobMSBShort(image); \
83 pixmap.pixel_type=(short) ReadBlobMSBShort(image); \
84 pixmap.bits_per_pixel=(short) ReadBlobMSBShort(image); \
85 pixmap.component_count=(short) ReadBlobMSBShort(image); \
86 pixmap.component_size=(short) ReadBlobMSBShort(image); \
87 pixmap.plane_bytes=ReadBlobMSBLong(image); \
88 pixmap.table=ReadBlobMSBLong(image); \
89 pixmap.reserved=ReadBlobMSBLong(image); \
90 if ((pixmap.bits_per_pixel <= 0) || (pixmap.bits_per_pixel > 32) || \
91 (pixmap.component_count <= 0) || (pixmap.component_count > 4) || \
92 (pixmap.component_size <= 0)) \
93 ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
96 #define ReadRectangle(image,rectangle) \
98 rectangle.top=(short) ReadBlobMSBShort(image); \
99 rectangle.left=(short) ReadBlobMSBShort(image); \
100 rectangle.bottom=(short) ReadBlobMSBShort(image); \
101 rectangle.right=(short) ReadBlobMSBShort(image); \
102 if ((rectangle.left > rectangle.right) || \
103 (rectangle.top > rectangle.bottom)) \
104 ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
107 typedef struct _PICTCode
119 typedef struct _PICTPixmap
127 horizontal_resolution,
142 typedef struct _PICTRectangle
151 static const PICTCode
154 /* 0x00 */ { "NOP", 0, "nop" },
155 /* 0x01 */ { "Clip", 0, "clip" },
156 /* 0x02 */ { "BkPat", 8, "background pattern" },
157 /* 0x03 */ { "TxFont", 2, "text font (word)" },
158 /* 0x04 */ { "TxFace", 1, "text face (byte)" },
159 /* 0x05 */ { "TxMode", 2, "text mode (word)" },
160 /* 0x06 */ { "SpExtra", 4, "space extra (fixed point)" },
161 /* 0x07 */ { "PnSize", 4, "pen size (point)" },
162 /* 0x08 */ { "PnMode", 2, "pen mode (word)" },
163 /* 0x09 */ { "PnPat", 8, "pen pattern" },
164 /* 0x0a */ { "FillPat", 8, "fill pattern" },
165 /* 0x0b */ { "OvSize", 4, "oval size (point)" },
166 /* 0x0c */ { "Origin", 4, "dh, dv (word)" },
167 /* 0x0d */ { "TxSize", 2, "text size (word)" },
168 /* 0x0e */ { "FgColor", 4, "foreground color (ssize_tword)" },
169 /* 0x0f */ { "BkColor", 4, "background color (ssize_tword)" },
170 /* 0x10 */ { "TxRatio", 8, "numerator (point), denominator (point)" },
171 /* 0x11 */ { "Version", 1, "version (byte)" },
172 /* 0x12 */ { "BkPixPat", 0, "color background pattern" },
173 /* 0x13 */ { "PnPixPat", 0, "color pen pattern" },
174 /* 0x14 */ { "FillPixPat", 0, "color fill pattern" },
175 /* 0x15 */ { "PnLocHFrac", 2, "fractional pen position" },
176 /* 0x16 */ { "ChExtra", 2, "extra for each character" },
177 /* 0x17 */ { "reserved", 0, "reserved for Apple use" },
178 /* 0x18 */ { "reserved", 0, "reserved for Apple use" },
179 /* 0x19 */ { "reserved", 0, "reserved for Apple use" },
180 /* 0x1a */ { "RGBFgCol", 6, "RGB foreColor" },
181 /* 0x1b */ { "RGBBkCol", 6, "RGB backColor" },
182 /* 0x1c */ { "HiliteMode", 0, "hilite mode flag" },
183 /* 0x1d */ { "HiliteColor", 6, "RGB hilite color" },
184 /* 0x1e */ { "DefHilite", 0, "Use default hilite color" },
185 /* 0x1f */ { "OpColor", 6, "RGB OpColor for arithmetic modes" },
186 /* 0x20 */ { "Line", 8, "pnLoc (point), newPt (point)" },
187 /* 0x21 */ { "LineFrom", 4, "newPt (point)" },
188 /* 0x22 */ { "ShortLine", 6, "pnLoc (point, dh, dv (-128 .. 127))" },
189 /* 0x23 */ { "ShortLineFrom", 2, "dh, dv (-128 .. 127)" },
190 /* 0x24 */ { "reserved", -1, "reserved for Apple use" },
191 /* 0x25 */ { "reserved", -1, "reserved for Apple use" },
192 /* 0x26 */ { "reserved", -1, "reserved for Apple use" },
193 /* 0x27 */ { "reserved", -1, "reserved for Apple use" },
194 /* 0x28 */ { "LongText", 0, "txLoc (point), count (0..255), text" },
195 /* 0x29 */ { "DHText", 0, "dh (0..255), count (0..255), text" },
196 /* 0x2a */ { "DVText", 0, "dv (0..255), count (0..255), text" },
197 /* 0x2b */ { "DHDVText", 0, "dh, dv (0..255), count (0..255), text" },
198 /* 0x2c */ { "reserved", -1, "reserved for Apple use" },
199 /* 0x2d */ { "reserved", -1, "reserved for Apple use" },
200 /* 0x2e */ { "reserved", -1, "reserved for Apple use" },
201 /* 0x2f */ { "reserved", -1, "reserved for Apple use" },
202 /* 0x30 */ { "frameRect", 8, "rect" },
203 /* 0x31 */ { "paintRect", 8, "rect" },
204 /* 0x32 */ { "eraseRect", 8, "rect" },
205 /* 0x33 */ { "invertRect", 8, "rect" },
206 /* 0x34 */ { "fillRect", 8, "rect" },
207 /* 0x35 */ { "reserved", 8, "reserved for Apple use" },
208 /* 0x36 */ { "reserved", 8, "reserved for Apple use" },
209 /* 0x37 */ { "reserved", 8, "reserved for Apple use" },
210 /* 0x38 */ { "frameSameRect", 0, "rect" },
211 /* 0x39 */ { "paintSameRect", 0, "rect" },
212 /* 0x3a */ { "eraseSameRect", 0, "rect" },
213 /* 0x3b */ { "invertSameRect", 0, "rect" },
214 /* 0x3c */ { "fillSameRect", 0, "rect" },
215 /* 0x3d */ { "reserved", 0, "reserved for Apple use" },
216 /* 0x3e */ { "reserved", 0, "reserved for Apple use" },
217 /* 0x3f */ { "reserved", 0, "reserved for Apple use" },
218 /* 0x40 */ { "frameRRect", 8, "rect" },
219 /* 0x41 */ { "paintRRect", 8, "rect" },
220 /* 0x42 */ { "eraseRRect", 8, "rect" },
221 /* 0x43 */ { "invertRRect", 8, "rect" },
222 /* 0x44 */ { "fillRRrect", 8, "rect" },
223 /* 0x45 */ { "reserved", 8, "reserved for Apple use" },
224 /* 0x46 */ { "reserved", 8, "reserved for Apple use" },
225 /* 0x47 */ { "reserved", 8, "reserved for Apple use" },
226 /* 0x48 */ { "frameSameRRect", 0, "rect" },
227 /* 0x49 */ { "paintSameRRect", 0, "rect" },
228 /* 0x4a */ { "eraseSameRRect", 0, "rect" },
229 /* 0x4b */ { "invertSameRRect", 0, "rect" },
230 /* 0x4c */ { "fillSameRRect", 0, "rect" },
231 /* 0x4d */ { "reserved", 0, "reserved for Apple use" },
232 /* 0x4e */ { "reserved", 0, "reserved for Apple use" },
233 /* 0x4f */ { "reserved", 0, "reserved for Apple use" },
234 /* 0x50 */ { "frameOval", 8, "rect" },
235 /* 0x51 */ { "paintOval", 8, "rect" },
236 /* 0x52 */ { "eraseOval", 8, "rect" },
237 /* 0x53 */ { "invertOval", 8, "rect" },
238 /* 0x54 */ { "fillOval", 8, "rect" },
239 /* 0x55 */ { "reserved", 8, "reserved for Apple use" },
240 /* 0x56 */ { "reserved", 8, "reserved for Apple use" },
241 /* 0x57 */ { "reserved", 8, "reserved for Apple use" },
242 /* 0x58 */ { "frameSameOval", 0, "rect" },
243 /* 0x59 */ { "paintSameOval", 0, "rect" },
244 /* 0x5a */ { "eraseSameOval", 0, "rect" },
245 /* 0x5b */ { "invertSameOval", 0, "rect" },
246 /* 0x5c */ { "fillSameOval", 0, "rect" },
247 /* 0x5d */ { "reserved", 0, "reserved for Apple use" },
248 /* 0x5e */ { "reserved", 0, "reserved for Apple use" },
249 /* 0x5f */ { "reserved", 0, "reserved for Apple use" },
250 /* 0x60 */ { "frameArc", 12, "rect, startAngle, arcAngle" },
251 /* 0x61 */ { "paintArc", 12, "rect, startAngle, arcAngle" },
252 /* 0x62 */ { "eraseArc", 12, "rect, startAngle, arcAngle" },
253 /* 0x63 */ { "invertArc", 12, "rect, startAngle, arcAngle" },
254 /* 0x64 */ { "fillArc", 12, "rect, startAngle, arcAngle" },
255 /* 0x65 */ { "reserved", 12, "reserved for Apple use" },
256 /* 0x66 */ { "reserved", 12, "reserved for Apple use" },
257 /* 0x67 */ { "reserved", 12, "reserved for Apple use" },
258 /* 0x68 */ { "frameSameArc", 4, "rect, startAngle, arcAngle" },
259 /* 0x69 */ { "paintSameArc", 4, "rect, startAngle, arcAngle" },
260 /* 0x6a */ { "eraseSameArc", 4, "rect, startAngle, arcAngle" },
261 /* 0x6b */ { "invertSameArc", 4, "rect, startAngle, arcAngle" },
262 /* 0x6c */ { "fillSameArc", 4, "rect, startAngle, arcAngle" },
263 /* 0x6d */ { "reserved", 4, "reserved for Apple use" },
264 /* 0x6e */ { "reserved", 4, "reserved for Apple use" },
265 /* 0x6f */ { "reserved", 4, "reserved for Apple use" },
266 /* 0x70 */ { "framePoly", 0, "poly" },
267 /* 0x71 */ { "paintPoly", 0, "poly" },
268 /* 0x72 */ { "erasePoly", 0, "poly" },
269 /* 0x73 */ { "invertPoly", 0, "poly" },
270 /* 0x74 */ { "fillPoly", 0, "poly" },
271 /* 0x75 */ { "reserved", 0, "reserved for Apple use" },
272 /* 0x76 */ { "reserved", 0, "reserved for Apple use" },
273 /* 0x77 */ { "reserved", 0, "reserved for Apple use" },
274 /* 0x78 */ { "frameSamePoly", 0, "poly (NYI)" },
275 /* 0x79 */ { "paintSamePoly", 0, "poly (NYI)" },
276 /* 0x7a */ { "eraseSamePoly", 0, "poly (NYI)" },
277 /* 0x7b */ { "invertSamePoly", 0, "poly (NYI)" },
278 /* 0x7c */ { "fillSamePoly", 0, "poly (NYI)" },
279 /* 0x7d */ { "reserved", 0, "reserved for Apple use" },
280 /* 0x7e */ { "reserved", 0, "reserved for Apple use" },
281 /* 0x7f */ { "reserved", 0, "reserved for Apple use" },
282 /* 0x80 */ { "frameRgn", 0, "region" },
283 /* 0x81 */ { "paintRgn", 0, "region" },
284 /* 0x82 */ { "eraseRgn", 0, "region" },
285 /* 0x83 */ { "invertRgn", 0, "region" },
286 /* 0x84 */ { "fillRgn", 0, "region" },
287 /* 0x85 */ { "reserved", 0, "reserved for Apple use" },
288 /* 0x86 */ { "reserved", 0, "reserved for Apple use" },
289 /* 0x87 */ { "reserved", 0, "reserved for Apple use" },
290 /* 0x88 */ { "frameSameRgn", 0, "region (NYI)" },
291 /* 0x89 */ { "paintSameRgn", 0, "region (NYI)" },
292 /* 0x8a */ { "eraseSameRgn", 0, "region (NYI)" },
293 /* 0x8b */ { "invertSameRgn", 0, "region (NYI)" },
294 /* 0x8c */ { "fillSameRgn", 0, "region (NYI)" },
295 /* 0x8d */ { "reserved", 0, "reserved for Apple use" },
296 /* 0x8e */ { "reserved", 0, "reserved for Apple use" },
297 /* 0x8f */ { "reserved", 0, "reserved for Apple use" },
298 /* 0x90 */ { "BitsRect", 0, "copybits, rect clipped" },
299 /* 0x91 */ { "BitsRgn", 0, "copybits, rgn clipped" },
300 /* 0x92 */ { "reserved", -1, "reserved for Apple use" },
301 /* 0x93 */ { "reserved", -1, "reserved for Apple use" },
302 /* 0x94 */ { "reserved", -1, "reserved for Apple use" },
303 /* 0x95 */ { "reserved", -1, "reserved for Apple use" },
304 /* 0x96 */ { "reserved", -1, "reserved for Apple use" },
305 /* 0x97 */ { "reserved", -1, "reserved for Apple use" },
306 /* 0x98 */ { "PackBitsRect", 0, "packed copybits, rect clipped" },
307 /* 0x99 */ { "PackBitsRgn", 0, "packed copybits, rgn clipped" },
308 /* 0x9a */ { "DirectBitsRect", 0, "PixMap, srcRect, dstRect, mode, PixData" },
309 /* 0x9b */ { "DirectBitsRgn", 0, "PixMap, srcRect, dstRect, mode, maskRgn, PixData" },
310 /* 0x9c */ { "reserved", -1, "reserved for Apple use" },
311 /* 0x9d */ { "reserved", -1, "reserved for Apple use" },
312 /* 0x9e */ { "reserved", -1, "reserved for Apple use" },
313 /* 0x9f */ { "reserved", -1, "reserved for Apple use" },
314 /* 0xa0 */ { "ShortComment", 2, "kind (word)" },
315 /* 0xa1 */ { "LongComment", 0, "kind (word), size (word), data" }
319 Forward declarations.
321 static MagickBooleanType
322 WritePICTImage(const ImageInfo *,Image *);
325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 % D e c o d e I m a g e %
333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 % DecodeImage decompresses an image via Macintosh pack bits decoding for
336 % Macintosh PICT images.
338 % The format of the DecodeImage method is:
340 % unsigned char *DecodeImage(Image *blob,Image *image,
341 % size_t bytes_per_line,const int bits_per_pixel,
342 % unsigned size_t extent)
344 % A description of each parameter follows:
346 % o image_info: the image info.
348 % o blob,image: the address of a structure of type Image.
350 % o bytes_per_line: This integer identifies the number of bytes in a
353 % o bits_per_pixel: the number of bits in a pixel.
355 % o extent: the number of pixels allocated.
359 static unsigned char *ExpandBuffer(unsigned char *pixels,
360 MagickSizeType *bytes_per_line,const unsigned int bits_per_pixel)
365 register unsigned char
374 switch (bits_per_pixel)
382 for (i=0; i < (ssize_t) *bytes_per_line; i++)
384 *q++=(*p >> 4) & 0xff;
393 for (i=0; i < (ssize_t) *bytes_per_line; i++)
395 *q++=(*p >> 6) & 0x03;
396 *q++=(*p >> 4) & 0x03;
397 *q++=(*p >> 2) & 0x03;
406 for (i=0; i < (ssize_t) *bytes_per_line; i++)
408 *q++=(*p >> 7) & 0x01;
409 *q++=(*p >> 6) & 0x01;
410 *q++=(*p >> 5) & 0x01;
411 *q++=(*p >> 4) & 0x01;
412 *q++=(*p >> 3) & 0x01;
413 *q++=(*p >> 2) & 0x01;
414 *q++=(*p >> 1) & 0x01;
427 static unsigned char *DecodeImage(Image *blob,Image *image,
428 size_t bytes_per_line,const unsigned int bits_per_pixel,size_t *extent)
436 register unsigned char
457 Determine pixel buffer size.
459 if (bits_per_pixel <= 8)
460 bytes_per_line&=0x7fff;
461 width=image->columns;
463 if (bits_per_pixel == 16)
469 if (bits_per_pixel == 32)
470 width*=image->matte ? 4 : 3;
471 if (bytes_per_line == 0)
472 bytes_per_line=width;
473 row_bytes=(size_t) (image->columns | 0x8000);
474 if (image->storage_class == DirectClass)
475 row_bytes=(size_t) ((4*image->columns) | 0x8000);
477 Allocate pixel and scanline buffer.
479 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,row_bytes*
481 if (pixels == (unsigned char *) NULL)
482 return((unsigned char *) NULL);
483 *extent=row_bytes*image->rows*sizeof(*pixels);
484 (void) ResetMagickMemory(pixels,0,*extent);
485 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
486 if (scanline == (unsigned char *) NULL)
487 return((unsigned char *) NULL);
488 if (bytes_per_line < 8)
491 Pixels are already uncompressed.
493 for (y=0; y < (ssize_t) image->rows; y++)
496 number_pixels=bytes_per_line;
497 count=ReadBlob(blob,(size_t) number_pixels,scanline);
499 p=ExpandBuffer(scanline,&number_pixels,bits_per_pixel);
500 if ((q+number_pixels) > (pixels+(*extent)))
502 (void) ThrowMagickException(&image->exception,GetMagickModule(),
503 CorruptImageError,"UnableToUncompressImage","`%s'",
507 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
509 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
513 Uncompress RLE pixels into uncompressed pixel buffer.
515 for (y=0; y < (ssize_t) image->rows; y++)
518 if (bytes_per_line > 200)
519 scanline_length=ReadBlobMSBShort(blob);
521 scanline_length=1UL*ReadBlobByte(blob);
522 if (scanline_length >= row_bytes)
524 (void) ThrowMagickException(&image->exception,GetMagickModule(),
525 CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
528 count=ReadBlob(blob,scanline_length,scanline);
529 for (j=0; j < (ssize_t) scanline_length; )
530 if ((scanline[j] & 0x80) == 0)
532 length=(size_t) ((scanline[j] & 0xff)+1);
533 number_pixels=length*bytes_per_pixel;
534 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
535 if ((q-pixels+number_pixels) <= *extent)
536 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
538 j+=(ssize_t) (length*bytes_per_pixel+1);
542 length=(size_t) (((scanline[j] ^ 0xff) & 0xff)+2);
543 number_pixels=bytes_per_pixel;
544 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
545 for (i=0; i < (ssize_t) length; i++)
547 if ((q-pixels+number_pixels) <= *extent)
548 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
551 j+=(ssize_t) bytes_per_pixel+1;
554 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563 % E n c o d e I m a g e %
567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
569 % EncodeImage compresses an image via Macintosh pack bits encoding
570 % for Macintosh PICT images.
572 % The format of the EncodeImage method is:
574 % size_t EncodeImage(Image *image,const unsigned char *scanline,
575 % const size_t bytes_per_line,unsigned char *pixels)
577 % A description of each parameter follows:
579 % o image: the address of a structure of type Image.
581 % o scanline: A pointer to an array of characters to pack.
583 % o bytes_per_line: the number of bytes in a scanline.
585 % o pixels: A pointer to an array of characters where the packed
586 % characters are stored.
589 static size_t EncodeImage(Image *image,const unsigned char *scanline,
590 const size_t bytes_per_line,unsigned char *pixels)
593 #define MaxPackbitsRunlength 128
595 register const unsigned char
601 register unsigned char
618 assert(image != (Image *) NULL);
619 assert(image->signature == MagickSignature);
620 if (image->debug != MagickFalse)
621 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
622 assert(scanline != (unsigned char *) NULL);
623 assert(pixels != (unsigned char *) NULL);
626 p=scanline+(bytes_per_line-1);
629 for (i=(ssize_t) bytes_per_line-1; i >= 0; i--)
636 while (runlength > 0)
638 *q++=(unsigned char) index;
641 if (count == MaxCount)
643 *q++=(unsigned char) (MaxCount-1);
650 *q++=(unsigned char) (count-1);
652 while (runlength > 0)
654 repeat_count=runlength;
655 if (repeat_count > MaxPackbitsRunlength)
656 repeat_count=MaxPackbitsRunlength;
657 *q++=(unsigned char) index;
658 *q++=(unsigned char) (257-repeat_count);
659 runlength-=repeat_count;
668 while (runlength > 0)
670 *q++=(unsigned char) index;
673 if (count == MaxCount)
675 *q++=(unsigned char) (MaxCount-1);
682 *q++=(unsigned char) (count-1);
684 while (runlength > 0)
686 repeat_count=runlength;
687 if (repeat_count > MaxPackbitsRunlength)
688 repeat_count=MaxPackbitsRunlength;
689 *q++=(unsigned char) index;
690 *q++=(unsigned char) (257-repeat_count);
691 runlength-=repeat_count;
695 *q++=(unsigned char) (count-1);
697 Write the number of and the packed length.
699 length=(size_t) (q-pixels);
700 if (bytes_per_line > 200)
702 (void) WriteBlobMSBShort(image,(unsigned short) length);
707 (void) WriteBlobByte(image,(unsigned char) length);
713 (void) WriteBlobByte(image,*q);
719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
729 % IsPICT()() returns MagickTrue if the image format type, identified by the
730 % magick string, is PICT.
732 % The format of the ReadPICTImage method is:
734 % MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
736 % A description of each parameter follows:
738 % o magick: compare image format pattern against these bytes.
740 % o length: Specifies the length of the magick string.
743 static MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
747 if (memcmp(magick+522,"\000\021\002\377\014\000",6) == 0)
752 #if !defined(macintosh)
754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758 % R e a d P I C T I m a g e %
762 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
764 % ReadPICTImage() reads an Apple Macintosh QuickDraw/PICT image file
765 % and returns it. It allocates the memory necessary for the new Image
766 % structure and returns a pointer to the new image.
768 % The format of the ReadPICTImage method is:
770 % Image *ReadPICTImage(const ImageInfo *image_info,
771 % ExceptionInfo *exception)
773 % A description of each parameter follows:
775 % o image_info: the image info.
777 % o exception: return any errors or warnings in this structure.
781 static inline size_t MagickMax(const size_t x,
789 static Image *ReadPICTImage(const ImageInfo *image_info,
790 ExceptionInfo *exception)
793 geometry[MaxTextExtent];
844 assert(image_info != (const ImageInfo *) NULL);
845 assert(image_info->signature == MagickSignature);
846 if (image_info->debug != MagickFalse)
847 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
848 image_info->filename);
849 assert(exception != (ExceptionInfo *) NULL);
850 assert(exception->signature == MagickSignature);
851 image=AcquireImage(image_info);
853 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
854 if (status == MagickFalse)
856 image=DestroyImageList(image);
857 return((Image *) NULL);
862 pixmap.bits_per_pixel=0;
863 pixmap.component_count=0;
864 for (i=0; i < 512; i++)
865 (void) ReadBlobByte(image); /* skip header */
866 (void) ReadBlobMSBShort(image); /* skip picture size */
867 ReadRectangle(image,frame);
868 while ((c=ReadBlobByte(image)) == 0) ;
870 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
871 version=ReadBlobByte(image);
874 c=ReadBlobByte(image);
876 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
880 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
881 if ((frame.left < 0) || (frame.right < 0) || (frame.top < 0) ||
882 (frame.bottom < 0) || (frame.left >= frame.right) ||
883 (frame.top >= frame.bottom))
884 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
889 image->columns=1UL*(frame.right-frame.left);
890 image->rows=1UL*(frame.bottom-frame.top);
891 image->x_resolution=DefaultResolution;
892 image->y_resolution=DefaultResolution;
893 image->units=UndefinedResolution;
895 Interpret PICT opcodes.
898 for (code=0; EOFBlob(image) == MagickFalse; )
900 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
901 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
903 if ((version == 1) || ((TellBlob(image) % 2) != 0))
904 code=ReadBlobByte(image);
906 code=(int) ReadBlobMSBShort(image);
909 if (image->debug != MagickFalse)
910 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%04X:",code);
914 if (image->debug != MagickFalse)
915 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
916 " %04X %s: %s",code,codes[code].name,codes[code].description);
924 length=ReadBlobMSBShort(image);
925 if (length != 0x000a)
927 for (i=0; i < (ssize_t) (length-2); i++)
928 (void) ReadBlobByte(image);
931 ReadRectangle(image,frame);
932 if (((frame.left & 0x8000) != 0) || ((frame.top & 0x8000) != 0))
934 image->columns=1UL*(frame.right-frame.left);
935 image->rows=1UL*(frame.bottom-frame.top);
936 (void) SetImageBackgroundColor(image);
951 Skip pattern definition.
953 pattern=1L*ReadBlobMSBShort(image);
954 for (i=0; i < 8; i++)
955 (void) ReadBlobByte(image);
958 for (i=0; i < 5; i++)
959 (void) ReadBlobByte(image);
963 ThrowReaderException(CorruptImageError,"UnknownPatternType");
964 length=ReadBlobMSBShort(image);
965 ReadRectangle(image,frame);
967 image->depth=1UL*pixmap.component_size;
968 image->x_resolution=1.0*pixmap.horizontal_resolution;
969 image->y_resolution=1.0*pixmap.vertical_resolution;
970 image->units=PixelsPerInchResolution;
971 (void) ReadBlobMSBLong(image);
972 flags=1L*ReadBlobMSBShort(image);
973 length=ReadBlobMSBShort(image);
974 for (i=0; i <= (ssize_t) length; i++)
975 (void) ReadBlobMSBLong(image);
976 width=1UL*(frame.bottom-frame.top);
977 height=1UL*(frame.right-frame.left);
978 if (pixmap.bits_per_pixel <= 8)
980 if (pixmap.bits_per_pixel == 16)
986 for (i=0; i < (ssize_t) (length*height); i++)
987 (void) ReadBlobByte(image);
990 for (j=0; j < (int) height; j++)
992 for (j=0; j < (ssize_t) ReadBlobMSBShort(image); j++)
993 (void) ReadBlobByte(image);
995 for (j=0; j < (ssize_t) ReadBlobByte(image); j++)
996 (void) ReadBlobByte(image);
1002 Initialize image background color.
1004 image->background_color.red=(Quantum)
1005 ScaleShortToQuantum(ReadBlobMSBShort(image));
1006 image->background_color.green=(Quantum)
1007 ScaleShortToQuantum(ReadBlobMSBShort(image));
1008 image->background_color.blue=(Quantum)
1009 ScaleShortToQuantum(ReadBlobMSBShort(image));
1022 Skip polygon or region.
1024 length=ReadBlobMSBShort(image);
1025 for (i=0; i < (ssize_t) (length-2); i++)
1026 (void) ReadBlobByte(image);
1043 register unsigned char
1056 Pixmap clipped by a rectangle.
1059 if ((code != 0x9a) && (code != 0x9b))
1060 bytes_per_line=1L*ReadBlobMSBShort(image);
1063 (void) ReadBlobMSBShort(image);
1064 (void) ReadBlobMSBShort(image);
1065 (void) ReadBlobMSBShort(image);
1067 ReadRectangle(image,frame);
1069 Initialize tile image.
1071 tile_image=CloneImage(image,1UL*(frame.right-frame.left),
1072 1UL*(frame.bottom-frame.top),MagickTrue,exception);
1073 if (tile_image == (Image *) NULL)
1074 return((Image *) NULL);
1075 if ((code == 0x9a) || (code == 0x9b) ||
1076 ((bytes_per_line & 0x8000) != 0))
1079 tile_image->depth=1UL*pixmap.component_size;
1080 tile_image->matte=pixmap.component_count == 4 ?
1081 MagickTrue : MagickFalse;
1082 tile_image->x_resolution=(double) pixmap.horizontal_resolution;
1083 tile_image->y_resolution=(double) pixmap.vertical_resolution;
1084 tile_image->units=PixelsPerInchResolution;
1085 if (tile_image->matte != MagickFalse)
1086 image->matte=tile_image->matte;
1088 if ((code != 0x9a) && (code != 0x9b))
1091 Initialize colormap.
1093 tile_image->colors=2;
1094 if ((bytes_per_line & 0x8000) != 0)
1096 (void) ReadBlobMSBLong(image);
1097 flags=1L*ReadBlobMSBShort(image);
1098 tile_image->colors=1UL*ReadBlobMSBShort(image)+1;
1100 status=AcquireImageColormap(tile_image,tile_image->colors);
1101 if (status == MagickFalse)
1103 tile_image=DestroyImage(tile_image);
1104 ThrowReaderException(ResourceLimitError,
1105 "MemoryAllocationFailed");
1107 if ((bytes_per_line & 0x8000) != 0)
1109 for (i=0; i < (ssize_t) tile_image->colors; i++)
1111 j=ReadBlobMSBShort(image) % tile_image->colors;
1112 if ((flags & 0x8000) != 0)
1114 tile_image->colormap[j].red=(Quantum)
1115 ScaleShortToQuantum(ReadBlobMSBShort(image));
1116 tile_image->colormap[j].green=(Quantum)
1117 ScaleShortToQuantum(ReadBlobMSBShort(image));
1118 tile_image->colormap[j].blue=(Quantum)
1119 ScaleShortToQuantum(ReadBlobMSBShort(image));
1124 for (i=0; i < (ssize_t) tile_image->colors; i++)
1126 tile_image->colormap[i].red=(Quantum) (QuantumRange-
1127 tile_image->colormap[i].red);
1128 tile_image->colormap[i].green=(Quantum) (QuantumRange-
1129 tile_image->colormap[i].green);
1130 tile_image->colormap[i].blue=(Quantum) (QuantumRange-
1131 tile_image->colormap[i].blue);
1135 ReadRectangle(image,source);
1136 ReadRectangle(image,destination);
1137 (void) ReadBlobMSBShort(image);
1138 if ((code == 0x91) || (code == 0x99) || (code == 0x9b))
1143 length=ReadBlobMSBShort(image);
1144 for (i=0; i < (ssize_t) (length-2); i++)
1145 (void) ReadBlobByte(image);
1147 if ((code != 0x9a) && (code != 0x9b) &&
1148 (bytes_per_line & 0x8000) == 0)
1149 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1,&extent);
1151 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1U*
1152 pixmap.bits_per_pixel,&extent);
1153 if (pixels == (unsigned char *) NULL)
1155 tile_image=DestroyImage(tile_image);
1156 ThrowReaderException(ResourceLimitError,
1157 "MemoryAllocationFailed");
1160 Convert PICT tile image to pixel packets.
1163 for (y=0; y < (ssize_t) tile_image->rows; y++)
1165 if (p > (pixels+extent+image->columns))
1166 ThrowReaderException(CorruptImageError,"NotEnoughPixelData");
1167 q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,
1169 if (q == (PixelPacket *) NULL)
1171 indexes=GetAuthenticIndexQueue(tile_image);
1172 for (x=0; x < (ssize_t) tile_image->columns; x++)
1174 if (tile_image->storage_class == PseudoClass)
1176 index=ConstrainColormapIndex(tile_image,*p);
1177 SetIndexPixelComponent(indexes+x,index);
1178 SetRedPixelComponent(q,
1179 tile_image->colormap[(ssize_t) index].red);
1180 SetGreenPixelComponent(q,
1181 tile_image->colormap[(ssize_t) index].green);
1182 SetBluePixelComponent(q,
1183 tile_image->colormap[(ssize_t) index].blue);
1187 if (pixmap.bits_per_pixel == 16)
1191 SetRedPixelComponent(q,ScaleCharToQuantum(
1192 (unsigned char) ((i & 0x7c) << 1)));
1193 SetGreenPixelComponent(q,ScaleCharToQuantum(
1194 (unsigned char) (((i & 0x03) << 6) |
1195 ((j & 0xe0) >> 2))));
1196 SetBluePixelComponent(q,ScaleCharToQuantum(
1197 (unsigned char) ((j & 0x1f) << 3)));
1200 if (tile_image->matte == MagickFalse)
1202 if (p > (pixels+extent+2*image->columns))
1203 ThrowReaderException(CorruptImageError,
1204 "NotEnoughPixelData");
1205 SetRedPixelComponent(q,ScaleCharToQuantum(*p));
1206 SetGreenPixelComponent(q,ScaleCharToQuantum(
1207 *(p+tile_image->columns)));
1208 SetBluePixelComponent(q,ScaleCharToQuantum(
1209 *(p+2*tile_image->columns)));
1213 if (p > (pixels+extent+3*image->columns))
1214 ThrowReaderException(CorruptImageError,
1215 "NotEnoughPixelData");
1216 SetOpacityPixelComponent(q,(Quantum) (QuantumRange-
1217 ScaleCharToQuantum(*p)));
1218 SetRedPixelComponent(q,ScaleCharToQuantum(
1219 *(p+tile_image->columns)));
1220 SetGreenPixelComponent(q,ScaleCharToQuantum(
1221 *(p+2*tile_image->columns)));
1222 SetBluePixelComponent(q,ScaleCharToQuantum(
1223 *(p+3*tile_image->columns)));
1229 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
1231 if ((tile_image->storage_class == DirectClass) &&
1232 (pixmap.bits_per_pixel != 16))
1234 p+=(pixmap.component_count-1)*tile_image->columns;
1238 status=SetImageProgress(image,LoadImageTag,y,tile_image->rows);
1239 if (status == MagickFalse)
1242 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1243 if (jpeg == MagickFalse)
1244 if ((code == 0x9a) || (code == 0x9b) ||
1245 ((bytes_per_line & 0x8000) != 0))
1246 (void) CompositeImage(image,CopyCompositeOp,tile_image,
1247 destination.left,destination.top);
1248 tile_image=DestroyImage(tile_image);
1262 type=ReadBlobMSBShort(image);
1263 length=ReadBlobMSBShort(image);
1266 (void) ReadBlobMSBLong(image);
1270 info=(unsigned char *) AcquireQuantumMemory(length,sizeof(*info));
1271 if (info == (unsigned char *) NULL)
1273 count=ReadBlob(image,length,info);
1281 profile=AcquireStringInfo(length);
1282 SetStringInfoDatum(profile,info);
1283 status=SetImageProfile(image,"icc",profile);
1284 profile=DestroyStringInfo(profile);
1285 if (status == MagickFalse)
1286 ThrowReaderException(ResourceLimitError,
1287 "MemoryAllocationFailed");
1294 profile=AcquireStringInfo(length);
1295 SetStringInfoDatum(profile,info);
1296 status=SetImageProfile(image,"iptc",profile);
1297 if (status == MagickFalse)
1298 ThrowReaderException(ResourceLimitError,
1299 "MemoryAllocationFailed");
1300 profile=DestroyStringInfo(profile);
1306 info=(unsigned char *) RelinquishMagickMemory(info);
1312 Skip to next op code.
1314 if (codes[code].length == -1)
1315 (void) ReadBlobMSBShort(image);
1317 for (i=0; i < (ssize_t) codes[code].length; i++)
1318 (void) ReadBlobByte(image);
1327 for (i=0; i < 24; i++)
1328 (void) ReadBlobByte(image);
1331 if (((code >= 0xb0) && (code <= 0xcf)) ||
1332 ((code >= 0x8000) && (code <= 0x80ff)))
1352 read_info=CloneImageInfo(image_info);
1353 SetImageInfoBlob(read_info,(void *) NULL,0);
1355 unique_file=AcquireUniqueFileResource(read_info->filename);
1356 if (unique_file != -1)
1357 file=fdopen(unique_file,"wb");
1358 if ((unique_file == -1) || (file == (FILE *) NULL))
1360 (void) CopyMagickString(image->filename,read_info->filename,
1362 ThrowFileException(exception,FileOpenError,
1363 "UnableToCreateTemporaryFile",image->filename);
1364 image=DestroyImageList(image);
1365 return((Image *) NULL);
1367 length=ReadBlobMSBLong(image);
1368 for (i=0; i < 6; i++)
1369 (void) ReadBlobMSBLong(image);
1370 ReadRectangle(image,frame);
1371 for (i=0; i < 122; i++)
1372 (void) ReadBlobByte(image);
1373 for (i=0; i < (ssize_t) (length-154); i++)
1375 c=ReadBlobByte(image);
1376 (void) fputc(c,file);
1378 (void) fclose(file);
1379 tile_image=ReadImage(read_info,exception);
1380 (void) RelinquishUniqueFileResource(read_info->filename);
1381 read_info=DestroyImageInfo(read_info);
1382 if (tile_image == (Image *) NULL)
1384 (void) FormatMagickString(geometry,MaxTextExtent,"%.20gx%.20g",
1385 (double) MagickMax(image->columns,tile_image->columns),
1386 (double) MagickMax(image->rows,tile_image->rows));
1387 (void) SetImageExtent(image,
1388 MagickMax(image->columns,tile_image->columns),
1389 MagickMax(image->rows,tile_image->rows));
1390 if (image->colorspace != RGBColorspace)
1391 (void) TransformImageColorspace(image,tile_image->colorspace);
1392 (void) CompositeImage(image,CopyCompositeOp,tile_image,frame.left,
1394 image->compression=tile_image->compression;
1395 tile_image=DestroyImage(tile_image);
1398 if ((code == 0xff) || (code == 0xffff))
1400 if (((code >= 0xd0) && (code <= 0xfe)) ||
1401 ((code >= 0x8100) && (code <= 0xffff)))
1406 length=ReadBlobMSBShort(image);
1407 for (i=0; i < (ssize_t) length; i++)
1408 (void) ReadBlobByte(image);
1411 if ((code >= 0x100) && (code <= 0x7fff))
1416 length=(size_t) ((code >> 7) & 0xff);
1417 for (i=0; i < (ssize_t) length; i++)
1418 (void) ReadBlobByte(image);
1422 (void) CloseBlob(image);
1423 return(GetFirstImageInList(image));
1428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1432 % R e g i s t e r P I C T I m a g e %
1436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1438 % RegisterPICTImage() adds attributes for the PICT image format to
1439 % the list of supported formats. The attributes include the image format
1440 % tag, a method to read and/or write the format, whether the format
1441 % supports the saving of more than one frame to the same file or blob,
1442 % whether the format supports native in-memory I/O, and a brief
1443 % description of the format.
1445 % The format of the RegisterPICTImage method is:
1447 % size_t RegisterPICTImage(void)
1450 ModuleExport size_t RegisterPICTImage(void)
1455 entry=SetMagickInfo("PCT");
1456 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1457 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1458 entry->adjoin=MagickFalse;
1459 entry->seekable_stream=MagickTrue;
1460 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1461 entry->magick=(IsImageFormatHandler *) IsPICT;
1462 entry->module=ConstantString("PICT");
1463 (void) RegisterMagickInfo(entry);
1464 entry=SetMagickInfo("PICT");
1465 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1466 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1467 entry->adjoin=MagickFalse;
1468 entry->seekable_stream=MagickTrue;
1469 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1470 entry->magick=(IsImageFormatHandler *) IsPICT;
1471 entry->module=ConstantString("PICT");
1472 (void) RegisterMagickInfo(entry);
1473 return(MagickImageCoderSignature);
1477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1481 % U n r e g i s t e r P I C T I m a g e %
1485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1487 % UnregisterPICTImage() removes format registrations made by the
1488 % PICT module from the list of supported formats.
1490 % The format of the UnregisterPICTImage method is:
1492 % UnregisterPICTImage(void)
1495 ModuleExport void UnregisterPICTImage(void)
1497 (void) UnregisterMagickInfo("PCT");
1498 (void) UnregisterMagickInfo("PICT");
1502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1506 % W r i t e P I C T I m a g e %
1510 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1512 % WritePICTImage() writes an image to a file in the Apple Macintosh
1513 % QuickDraw/PICT image format.
1515 % The format of the WritePICTImage method is:
1517 % MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1520 % A description of each parameter follows.
1522 % o image_info: the image info.
1524 % o image: The image.
1527 static MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1530 #define MaxCount 128
1531 #define PictCropRegionOp 0x01
1532 #define PictEndOfPictureOp 0xff
1533 #define PictJPEGOp 0x8200
1534 #define PictInfoOp 0x0C00
1535 #define PictInfoSize 512
1536 #define PictPixmapOp 0x9A
1537 #define PictPICTOp 0x98
1538 #define PictVersion 0x11
1559 destination_rectangle,
1564 register const IndexPacket
1567 register const PixelPacket
1594 Open output image file.
1596 assert(image_info != (const ImageInfo *) NULL);
1597 assert(image_info->signature == MagickSignature);
1598 assert(image != (Image *) NULL);
1599 assert(image->signature == MagickSignature);
1600 if (image->debug != MagickFalse)
1601 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1602 if ((image->columns > 65535L) || (image->rows > 65535L))
1603 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1604 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1605 if (status == MagickFalse)
1607 if (image->colorspace != RGBColorspace)
1608 (void) TransformImageColorspace(image,RGBColorspace);
1610 Initialize image info.
1612 size_rectangle.top=0;
1613 size_rectangle.left=0;
1614 size_rectangle.bottom=(short) image->rows;
1615 size_rectangle.right=(short) image->columns;
1616 frame_rectangle=size_rectangle;
1617 crop_rectangle=size_rectangle;
1618 source_rectangle=size_rectangle;
1619 destination_rectangle=size_rectangle;
1621 row_bytes=(unsigned short) (image->columns | 0x8000);
1624 bounds.bottom=(short) image->rows;
1625 bounds.right=(short) image->columns;
1629 pixmap.pixel_type=0;
1630 pixmap.bits_per_pixel=8;
1631 pixmap.component_count=1;
1632 pixmap.component_size=8;
1633 pixmap.plane_bytes=0;
1637 x_resolution=image->x_resolution != 0.0 ? image->x_resolution :
1639 y_resolution=image->y_resolution != 0.0 ? image->y_resolution :
1641 storage_class=image->storage_class;
1642 if (image_info->compression == JPEGCompression)
1643 storage_class=DirectClass;
1644 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1646 pixmap.component_count=image->matte ? 4 : 3;
1647 pixmap.pixel_type=16;
1648 pixmap.bits_per_pixel=32;
1649 pixmap.pack_type=0x04;
1651 row_bytes=(unsigned short) ((4*image->columns) | 0x8000);
1656 bytes_per_line=image->columns;
1657 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1658 bytes_per_line*=image->matte ? 4 : 3;
1659 buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer));
1660 packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t)
1661 (row_bytes+MaxCount),sizeof(*packed_scanline));
1662 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
1663 if ((buffer == (unsigned char *) NULL) ||
1664 (packed_scanline == (unsigned char *) NULL) ||
1665 (scanline == (unsigned char *) NULL))
1666 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1667 (void) ResetMagickMemory(scanline,0,row_bytes);
1668 (void) ResetMagickMemory(packed_scanline,0,(size_t) (row_bytes+MaxCount));
1670 Write header, header size, size bounding box, version, and reserved.
1672 (void) ResetMagickMemory(buffer,0,PictInfoSize);
1673 (void) WriteBlob(image,PictInfoSize,buffer);
1674 (void) WriteBlobMSBShort(image,0);
1675 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top);
1676 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left);
1677 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom);
1678 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right);
1679 (void) WriteBlobMSBShort(image,PictVersion);
1680 (void) WriteBlobMSBShort(image,0x02ff); /* version #2 */
1681 (void) WriteBlobMSBShort(image,PictInfoOp);
1682 (void) WriteBlobMSBLong(image,0xFFFE0000UL);
1684 Write full size of the file, resolution, frame bounding box, and reserved.
1686 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1687 (void) WriteBlobMSBShort(image,0x0000);
1688 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1689 (void) WriteBlobMSBShort(image,0x0000);
1690 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top);
1691 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left);
1692 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom);
1693 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right);
1694 (void) WriteBlobMSBLong(image,0x00000000L);
1695 profile=GetImageProfile(image,"iptc");
1696 if (profile != (StringInfo *) NULL)
1698 (void) WriteBlobMSBShort(image,0xa1);
1699 (void) WriteBlobMSBShort(image,0x1f2);
1700 (void) WriteBlobMSBShort(image,(unsigned short)
1701 (GetStringInfoLength(profile)+4));
1702 (void) WriteBlobString(image,"8BIM");
1703 (void) WriteBlob(image,GetStringInfoLength(profile),
1704 GetStringInfoDatum(profile));
1706 profile=GetImageProfile(image,"icc");
1707 if (profile != (StringInfo *) NULL)
1709 (void) WriteBlobMSBShort(image,0xa1);
1710 (void) WriteBlobMSBShort(image,0xe0);
1711 (void) WriteBlobMSBShort(image,(unsigned short)
1712 (GetStringInfoLength(profile)+4));
1713 (void) WriteBlobMSBLong(image,0x00000000UL);
1714 (void) WriteBlob(image,GetStringInfoLength(profile),
1715 GetStringInfoDatum(profile));
1716 (void) WriteBlobMSBShort(image,0xa1);
1717 (void) WriteBlobMSBShort(image,0xe0);
1718 (void) WriteBlobMSBShort(image,4);
1719 (void) WriteBlobMSBLong(image,0x00000002UL);
1722 Write crop region opcode and crop bounding box.
1724 (void) WriteBlobMSBShort(image,PictCropRegionOp);
1725 (void) WriteBlobMSBShort(image,0xa);
1726 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top);
1727 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left);
1728 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom);
1729 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right);
1730 if (image_info->compression == JPEGCompression)
1744 jpeg_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1745 if (jpeg_image == (Image *) NULL)
1747 (void) CloseBlob(image);
1748 return(MagickFalse);
1750 jpeg_info=CloneImageInfo(image_info);
1751 (void) CopyMagickString(jpeg_info->magick,"JPEG",MaxTextExtent);
1753 blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length,
1755 jpeg_info=DestroyImageInfo(jpeg_info);
1756 if (blob == (unsigned char *) NULL)
1757 return(MagickFalse);
1758 jpeg_image=DestroyImage(jpeg_image);
1759 (void) WriteBlobMSBShort(image,PictJPEGOp);
1760 (void) WriteBlobMSBLong(image,(unsigned int) length+154);
1761 (void) WriteBlobMSBShort(image,0x0000);
1762 (void) WriteBlobMSBLong(image,0x00010000UL);
1763 (void) WriteBlobMSBLong(image,0x00000000UL);
1764 (void) WriteBlobMSBLong(image,0x00000000UL);
1765 (void) WriteBlobMSBLong(image,0x00000000UL);
1766 (void) WriteBlobMSBLong(image,0x00010000UL);
1767 (void) WriteBlobMSBLong(image,0x00000000UL);
1768 (void) WriteBlobMSBLong(image,0x00000000UL);
1769 (void) WriteBlobMSBLong(image,0x00000000UL);
1770 (void) WriteBlobMSBLong(image,0x40000000UL);
1771 (void) WriteBlobMSBLong(image,0x00000000UL);
1772 (void) WriteBlobMSBLong(image,0x00000000UL);
1773 (void) WriteBlobMSBLong(image,0x00000000UL);
1774 (void) WriteBlobMSBLong(image,0x00400000UL);
1775 (void) WriteBlobMSBShort(image,0x0000);
1776 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1777 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1778 (void) WriteBlobMSBShort(image,0x0000);
1779 (void) WriteBlobMSBShort(image,768);
1780 (void) WriteBlobMSBShort(image,0x0000);
1781 (void) WriteBlobMSBLong(image,0x00000000UL);
1782 (void) WriteBlobMSBLong(image,0x00566A70UL);
1783 (void) WriteBlobMSBLong(image,0x65670000UL);
1784 (void) WriteBlobMSBLong(image,0x00000000UL);
1785 (void) WriteBlobMSBLong(image,0x00000001UL);
1786 (void) WriteBlobMSBLong(image,0x00016170UL);
1787 (void) WriteBlobMSBLong(image,0x706C0000UL);
1788 (void) WriteBlobMSBLong(image,0x00000000UL);
1789 (void) WriteBlobMSBShort(image,768);
1790 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1791 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1792 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1793 (void) WriteBlobMSBShort(image,0x0000);
1794 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1795 (void) WriteBlobMSBLong(image,0x00000000UL);
1796 (void) WriteBlobMSBLong(image,0x87AC0001UL);
1797 (void) WriteBlobMSBLong(image,0x0B466F74UL);
1798 (void) WriteBlobMSBLong(image,0x6F202D20UL);
1799 (void) WriteBlobMSBLong(image,0x4A504547UL);
1800 (void) WriteBlobMSBLong(image,0x00000000UL);
1801 (void) WriteBlobMSBLong(image,0x00000000UL);
1802 (void) WriteBlobMSBLong(image,0x00000000UL);
1803 (void) WriteBlobMSBLong(image,0x00000000UL);
1804 (void) WriteBlobMSBLong(image,0x00000000UL);
1805 (void) WriteBlobMSBLong(image,0x0018FFFFUL);
1806 (void) WriteBlob(image,length,blob);
1807 if ((length & 0x01) != 0)
1808 (void) WriteBlobByte(image,'\0');
1809 blob=(unsigned char *) RelinquishMagickMemory(blob);
1812 Write picture opcode, row bytes, and picture bounding box, and version.
1814 if (storage_class == PseudoClass)
1815 (void) WriteBlobMSBShort(image,PictPICTOp);
1818 (void) WriteBlobMSBShort(image,PictPixmapOp);
1819 (void) WriteBlobMSBLong(image,(size_t) base_address);
1821 (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000));
1822 (void) WriteBlobMSBShort(image,(unsigned short) bounds.top);
1823 (void) WriteBlobMSBShort(image,(unsigned short) bounds.left);
1824 (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom);
1825 (void) WriteBlobMSBShort(image,(unsigned short) bounds.right);
1827 Write pack type, pack size, resolution, pixel type, and pixel size.
1829 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version);
1830 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type);
1831 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.pack_size);
1832 (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5));
1833 (void) WriteBlobMSBShort(image,0x0000);
1834 (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5));
1835 (void) WriteBlobMSBShort(image,0x0000);
1836 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type);
1837 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel);
1839 Write component count, size, plane bytes, table size, and reserved.
1841 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count);
1842 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size);
1843 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.plane_bytes);
1844 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.table);
1845 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.reserved);
1846 if (storage_class == PseudoClass)
1849 Write image colormap.
1851 (void) WriteBlobMSBLong(image,0x00000000L); /* color seed */
1852 (void) WriteBlobMSBShort(image,0L); /* color flags */
1853 (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1));
1854 for (i=0; i < (ssize_t) image->colors; i++)
1856 (void) WriteBlobMSBShort(image,(unsigned short) i);
1857 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1858 image->colormap[i].red));
1859 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1860 image->colormap[i].green));
1861 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1862 image->colormap[i].blue));
1866 Write source and destination rectangle.
1868 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top);
1869 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left);
1870 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom);
1871 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right);
1872 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top);
1873 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left);
1874 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom);
1875 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right);
1876 (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode);
1881 if ((storage_class == PseudoClass) && (image->matte == MagickFalse))
1882 for (y=0; y < (ssize_t) image->rows; y++)
1884 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1885 if (p == (const PixelPacket *) NULL)
1887 indexes=GetVirtualIndexQueue(image);
1888 for (x=0; x < (ssize_t) image->columns; x++)
1889 scanline[x]=(unsigned char) GetIndexPixelComponent(indexes+x);
1890 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
1892 if (image->previous == (Image *) NULL)
1894 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1896 if (status == MagickFalse)
1901 if (image_info->compression == JPEGCompression)
1903 (void) ResetMagickMemory(scanline,0,row_bytes);
1904 for (y=0; y < (ssize_t) image->rows; y++)
1905 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
1910 register unsigned char
1917 green=scanline+image->columns;
1918 blue=scanline+2*image->columns;
1919 opacity=scanline+3*image->columns;
1920 for (y=0; y < (ssize_t) image->rows; y++)
1922 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1923 if (p == (const PixelPacket *) NULL)
1926 green=scanline+image->columns;
1927 blue=scanline+2*image->columns;
1928 if (image->matte != MagickFalse)
1931 red=scanline+image->columns;
1932 green=scanline+2*image->columns;
1933 blue=scanline+3*image->columns;
1935 for (x=0; x < (ssize_t) image->columns; x++)
1937 *red++=ScaleQuantumToChar(GetRedPixelComponent(p));
1938 *green++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1939 *blue++=ScaleQuantumToChar(GetBluePixelComponent(p));
1940 if (image->matte != MagickFalse)
1941 *opacity++=ScaleQuantumToChar((Quantum)
1942 (GetAlphaPixelComponent(p)));
1945 count+=EncodeImage(image,scanline,bytes_per_line & 0x7FFF,
1947 if (image->previous == (Image *) NULL)
1949 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1951 if (status == MagickFalse)
1956 if ((count & 0x01) != 0)
1957 (void) WriteBlobByte(image,'\0');
1958 (void) WriteBlobMSBShort(image,PictEndOfPictureOp);
1959 offset=TellBlob(image);
1960 offset=SeekBlob(image,512,SEEK_SET);
1961 (void) WriteBlobMSBShort(image,(unsigned short) offset);
1962 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
1963 packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline);
1964 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1965 (void) CloseBlob(image);