2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % PPPP IIIII CCCC TTTTT %
13 % Read/Write Apple Macintosh QuickDraw/PICT Format %
20 % Copyright 1999-2017 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 % https://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 "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"
74 ImageMagick Macintosh PICT Methods.
76 #define ReadPixmap(pixmap) \
78 pixmap.version=(short) ReadBlobMSBShort(image); \
79 pixmap.pack_type=(short) ReadBlobMSBShort(image); \
80 pixmap.pack_size=ReadBlobMSBLong(image); \
81 pixmap.horizontal_resolution=1UL*ReadBlobMSBShort(image); \
82 (void) ReadBlobMSBShort(image); \
83 pixmap.vertical_resolution=1UL*ReadBlobMSBShort(image); \
84 (void) ReadBlobMSBShort(image); \
85 pixmap.pixel_type=(short) ReadBlobMSBShort(image); \
86 pixmap.bits_per_pixel=(short) ReadBlobMSBShort(image); \
87 pixmap.component_count=(short) ReadBlobMSBShort(image); \
88 pixmap.component_size=(short) ReadBlobMSBShort(image); \
89 pixmap.plane_bytes=ReadBlobMSBLong(image); \
90 pixmap.table=ReadBlobMSBLong(image); \
91 pixmap.reserved=ReadBlobMSBLong(image); \
92 if ((EOFBlob(image) != MagickFalse) || (pixmap.bits_per_pixel <= 0) || \
93 (pixmap.bits_per_pixel > 32) || (pixmap.component_count <= 0) || \
94 (pixmap.component_count > 4) || (pixmap.component_size <= 0)) \
95 ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
98 typedef struct _PICTCode
110 typedef struct _PICTPixmap
118 horizontal_resolution,
133 typedef struct _PICTRectangle
142 static const PICTCode
145 /* 0x00 */ { "NOP", 0, "nop" },
146 /* 0x01 */ { "Clip", 0, "clip" },
147 /* 0x02 */ { "BkPat", 8, "background pattern" },
148 /* 0x03 */ { "TxFont", 2, "text font (word)" },
149 /* 0x04 */ { "TxFace", 1, "text face (byte)" },
150 /* 0x05 */ { "TxMode", 2, "text mode (word)" },
151 /* 0x06 */ { "SpExtra", 4, "space extra (fixed point)" },
152 /* 0x07 */ { "PnSize", 4, "pen size (point)" },
153 /* 0x08 */ { "PnMode", 2, "pen mode (word)" },
154 /* 0x09 */ { "PnPat", 8, "pen pattern" },
155 /* 0x0a */ { "FillPat", 8, "fill pattern" },
156 /* 0x0b */ { "OvSize", 4, "oval size (point)" },
157 /* 0x0c */ { "Origin", 4, "dh, dv (word)" },
158 /* 0x0d */ { "TxSize", 2, "text size (word)" },
159 /* 0x0e */ { "FgColor", 4, "foreground color (ssize_tword)" },
160 /* 0x0f */ { "BkColor", 4, "background color (ssize_tword)" },
161 /* 0x10 */ { "TxRatio", 8, "numerator (point), denominator (point)" },
162 /* 0x11 */ { "Version", 1, "version (byte)" },
163 /* 0x12 */ { "BkPixPat", 0, "color background pattern" },
164 /* 0x13 */ { "PnPixPat", 0, "color pen pattern" },
165 /* 0x14 */ { "FillPixPat", 0, "color fill pattern" },
166 /* 0x15 */ { "PnLocHFrac", 2, "fractional pen position" },
167 /* 0x16 */ { "ChExtra", 2, "extra for each character" },
168 /* 0x17 */ { "reserved", 0, "reserved for Apple use" },
169 /* 0x18 */ { "reserved", 0, "reserved for Apple use" },
170 /* 0x19 */ { "reserved", 0, "reserved for Apple use" },
171 /* 0x1a */ { "RGBFgCol", 6, "RGB foreColor" },
172 /* 0x1b */ { "RGBBkCol", 6, "RGB backColor" },
173 /* 0x1c */ { "HiliteMode", 0, "hilite mode flag" },
174 /* 0x1d */ { "HiliteColor", 6, "RGB hilite color" },
175 /* 0x1e */ { "DefHilite", 0, "Use default hilite color" },
176 /* 0x1f */ { "OpColor", 6, "RGB OpColor for arithmetic modes" },
177 /* 0x20 */ { "Line", 8, "pnLoc (point), newPt (point)" },
178 /* 0x21 */ { "LineFrom", 4, "newPt (point)" },
179 /* 0x22 */ { "ShortLine", 6, "pnLoc (point, dh, dv (-128 .. 127))" },
180 /* 0x23 */ { "ShortLineFrom", 2, "dh, dv (-128 .. 127)" },
181 /* 0x24 */ { "reserved", -1, "reserved for Apple use" },
182 /* 0x25 */ { "reserved", -1, "reserved for Apple use" },
183 /* 0x26 */ { "reserved", -1, "reserved for Apple use" },
184 /* 0x27 */ { "reserved", -1, "reserved for Apple use" },
185 /* 0x28 */ { "LongText", 0, "txLoc (point), count (0..255), text" },
186 /* 0x29 */ { "DHText", 0, "dh (0..255), count (0..255), text" },
187 /* 0x2a */ { "DVText", 0, "dv (0..255), count (0..255), text" },
188 /* 0x2b */ { "DHDVText", 0, "dh, dv (0..255), count (0..255), text" },
189 /* 0x2c */ { "reserved", -1, "reserved for Apple use" },
190 /* 0x2d */ { "reserved", -1, "reserved for Apple use" },
191 /* 0x2e */ { "reserved", -1, "reserved for Apple use" },
192 /* 0x2f */ { "reserved", -1, "reserved for Apple use" },
193 /* 0x30 */ { "frameRect", 8, "rect" },
194 /* 0x31 */ { "paintRect", 8, "rect" },
195 /* 0x32 */ { "eraseRect", 8, "rect" },
196 /* 0x33 */ { "invertRect", 8, "rect" },
197 /* 0x34 */ { "fillRect", 8, "rect" },
198 /* 0x35 */ { "reserved", 8, "reserved for Apple use" },
199 /* 0x36 */ { "reserved", 8, "reserved for Apple use" },
200 /* 0x37 */ { "reserved", 8, "reserved for Apple use" },
201 /* 0x38 */ { "frameSameRect", 0, "rect" },
202 /* 0x39 */ { "paintSameRect", 0, "rect" },
203 /* 0x3a */ { "eraseSameRect", 0, "rect" },
204 /* 0x3b */ { "invertSameRect", 0, "rect" },
205 /* 0x3c */ { "fillSameRect", 0, "rect" },
206 /* 0x3d */ { "reserved", 0, "reserved for Apple use" },
207 /* 0x3e */ { "reserved", 0, "reserved for Apple use" },
208 /* 0x3f */ { "reserved", 0, "reserved for Apple use" },
209 /* 0x40 */ { "frameRRect", 8, "rect" },
210 /* 0x41 */ { "paintRRect", 8, "rect" },
211 /* 0x42 */ { "eraseRRect", 8, "rect" },
212 /* 0x43 */ { "invertRRect", 8, "rect" },
213 /* 0x44 */ { "fillRRrect", 8, "rect" },
214 /* 0x45 */ { "reserved", 8, "reserved for Apple use" },
215 /* 0x46 */ { "reserved", 8, "reserved for Apple use" },
216 /* 0x47 */ { "reserved", 8, "reserved for Apple use" },
217 /* 0x48 */ { "frameSameRRect", 0, "rect" },
218 /* 0x49 */ { "paintSameRRect", 0, "rect" },
219 /* 0x4a */ { "eraseSameRRect", 0, "rect" },
220 /* 0x4b */ { "invertSameRRect", 0, "rect" },
221 /* 0x4c */ { "fillSameRRect", 0, "rect" },
222 /* 0x4d */ { "reserved", 0, "reserved for Apple use" },
223 /* 0x4e */ { "reserved", 0, "reserved for Apple use" },
224 /* 0x4f */ { "reserved", 0, "reserved for Apple use" },
225 /* 0x50 */ { "frameOval", 8, "rect" },
226 /* 0x51 */ { "paintOval", 8, "rect" },
227 /* 0x52 */ { "eraseOval", 8, "rect" },
228 /* 0x53 */ { "invertOval", 8, "rect" },
229 /* 0x54 */ { "fillOval", 8, "rect" },
230 /* 0x55 */ { "reserved", 8, "reserved for Apple use" },
231 /* 0x56 */ { "reserved", 8, "reserved for Apple use" },
232 /* 0x57 */ { "reserved", 8, "reserved for Apple use" },
233 /* 0x58 */ { "frameSameOval", 0, "rect" },
234 /* 0x59 */ { "paintSameOval", 0, "rect" },
235 /* 0x5a */ { "eraseSameOval", 0, "rect" },
236 /* 0x5b */ { "invertSameOval", 0, "rect" },
237 /* 0x5c */ { "fillSameOval", 0, "rect" },
238 /* 0x5d */ { "reserved", 0, "reserved for Apple use" },
239 /* 0x5e */ { "reserved", 0, "reserved for Apple use" },
240 /* 0x5f */ { "reserved", 0, "reserved for Apple use" },
241 /* 0x60 */ { "frameArc", 12, "rect, startAngle, arcAngle" },
242 /* 0x61 */ { "paintArc", 12, "rect, startAngle, arcAngle" },
243 /* 0x62 */ { "eraseArc", 12, "rect, startAngle, arcAngle" },
244 /* 0x63 */ { "invertArc", 12, "rect, startAngle, arcAngle" },
245 /* 0x64 */ { "fillArc", 12, "rect, startAngle, arcAngle" },
246 /* 0x65 */ { "reserved", 12, "reserved for Apple use" },
247 /* 0x66 */ { "reserved", 12, "reserved for Apple use" },
248 /* 0x67 */ { "reserved", 12, "reserved for Apple use" },
249 /* 0x68 */ { "frameSameArc", 4, "rect, startAngle, arcAngle" },
250 /* 0x69 */ { "paintSameArc", 4, "rect, startAngle, arcAngle" },
251 /* 0x6a */ { "eraseSameArc", 4, "rect, startAngle, arcAngle" },
252 /* 0x6b */ { "invertSameArc", 4, "rect, startAngle, arcAngle" },
253 /* 0x6c */ { "fillSameArc", 4, "rect, startAngle, arcAngle" },
254 /* 0x6d */ { "reserved", 4, "reserved for Apple use" },
255 /* 0x6e */ { "reserved", 4, "reserved for Apple use" },
256 /* 0x6f */ { "reserved", 4, "reserved for Apple use" },
257 /* 0x70 */ { "framePoly", 0, "poly" },
258 /* 0x71 */ { "paintPoly", 0, "poly" },
259 /* 0x72 */ { "erasePoly", 0, "poly" },
260 /* 0x73 */ { "invertPoly", 0, "poly" },
261 /* 0x74 */ { "fillPoly", 0, "poly" },
262 /* 0x75 */ { "reserved", 0, "reserved for Apple use" },
263 /* 0x76 */ { "reserved", 0, "reserved for Apple use" },
264 /* 0x77 */ { "reserved", 0, "reserved for Apple use" },
265 /* 0x78 */ { "frameSamePoly", 0, "poly (NYI)" },
266 /* 0x79 */ { "paintSamePoly", 0, "poly (NYI)" },
267 /* 0x7a */ { "eraseSamePoly", 0, "poly (NYI)" },
268 /* 0x7b */ { "invertSamePoly", 0, "poly (NYI)" },
269 /* 0x7c */ { "fillSamePoly", 0, "poly (NYI)" },
270 /* 0x7d */ { "reserved", 0, "reserved for Apple use" },
271 /* 0x7e */ { "reserved", 0, "reserved for Apple use" },
272 /* 0x7f */ { "reserved", 0, "reserved for Apple use" },
273 /* 0x80 */ { "frameRgn", 0, "region" },
274 /* 0x81 */ { "paintRgn", 0, "region" },
275 /* 0x82 */ { "eraseRgn", 0, "region" },
276 /* 0x83 */ { "invertRgn", 0, "region" },
277 /* 0x84 */ { "fillRgn", 0, "region" },
278 /* 0x85 */ { "reserved", 0, "reserved for Apple use" },
279 /* 0x86 */ { "reserved", 0, "reserved for Apple use" },
280 /* 0x87 */ { "reserved", 0, "reserved for Apple use" },
281 /* 0x88 */ { "frameSameRgn", 0, "region (NYI)" },
282 /* 0x89 */ { "paintSameRgn", 0, "region (NYI)" },
283 /* 0x8a */ { "eraseSameRgn", 0, "region (NYI)" },
284 /* 0x8b */ { "invertSameRgn", 0, "region (NYI)" },
285 /* 0x8c */ { "fillSameRgn", 0, "region (NYI)" },
286 /* 0x8d */ { "reserved", 0, "reserved for Apple use" },
287 /* 0x8e */ { "reserved", 0, "reserved for Apple use" },
288 /* 0x8f */ { "reserved", 0, "reserved for Apple use" },
289 /* 0x90 */ { "BitsRect", 0, "copybits, rect clipped" },
290 /* 0x91 */ { "BitsRgn", 0, "copybits, rgn clipped" },
291 /* 0x92 */ { "reserved", -1, "reserved for Apple use" },
292 /* 0x93 */ { "reserved", -1, "reserved for Apple use" },
293 /* 0x94 */ { "reserved", -1, "reserved for Apple use" },
294 /* 0x95 */ { "reserved", -1, "reserved for Apple use" },
295 /* 0x96 */ { "reserved", -1, "reserved for Apple use" },
296 /* 0x97 */ { "reserved", -1, "reserved for Apple use" },
297 /* 0x98 */ { "PackBitsRect", 0, "packed copybits, rect clipped" },
298 /* 0x99 */ { "PackBitsRgn", 0, "packed copybits, rgn clipped" },
299 /* 0x9a */ { "DirectBitsRect", 0, "PixMap, srcRect, dstRect, mode, PixData" },
300 /* 0x9b */ { "DirectBitsRgn", 0, "PixMap, srcRect, dstRect, mode, maskRgn, PixData" },
301 /* 0x9c */ { "reserved", -1, "reserved for Apple use" },
302 /* 0x9d */ { "reserved", -1, "reserved for Apple use" },
303 /* 0x9e */ { "reserved", -1, "reserved for Apple use" },
304 /* 0x9f */ { "reserved", -1, "reserved for Apple use" },
305 /* 0xa0 */ { "ShortComment", 2, "kind (word)" },
306 /* 0xa1 */ { "LongComment", 0, "kind (word), size (word), data" }
310 Forward declarations.
312 static MagickBooleanType
313 WritePICTImage(const ImageInfo *,Image *,ExceptionInfo *);
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320 % D e c o d e I m a g e %
324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
326 % DecodeImage decompresses an image via Macintosh pack bits decoding for
327 % Macintosh PICT images.
329 % The format of the DecodeImage method is:
331 % unsigned char *DecodeImage(Image *blob,Image *image,
332 % size_t bytes_per_line,const int bits_per_pixel,
333 % unsigned size_t extent)
335 % A description of each parameter follows:
337 % o image_info: the image info.
339 % o blob,image: the address of a structure of type Image.
341 % o bytes_per_line: This integer identifies the number of bytes in a
344 % o bits_per_pixel: the number of bits in a pixel.
346 % o extent: the number of pixels allocated.
350 static unsigned char *ExpandBuffer(unsigned char *pixels,
351 MagickSizeType *bytes_per_line,const unsigned int bits_per_pixel)
356 register unsigned char
365 switch (bits_per_pixel)
373 for (i=0; i < (ssize_t) *bytes_per_line; i++)
375 *q++=(*p >> 4) & 0xff;
384 for (i=0; i < (ssize_t) *bytes_per_line; i++)
386 *q++=(*p >> 6) & 0x03;
387 *q++=(*p >> 4) & 0x03;
388 *q++=(*p >> 2) & 0x03;
397 for (i=0; i < (ssize_t) *bytes_per_line; i++)
399 *q++=(*p >> 7) & 0x01;
400 *q++=(*p >> 6) & 0x01;
401 *q++=(*p >> 5) & 0x01;
402 *q++=(*p >> 4) & 0x01;
403 *q++=(*p >> 3) & 0x01;
404 *q++=(*p >> 2) & 0x01;
405 *q++=(*p >> 1) & 0x01;
418 static unsigned char *DecodeImage(Image *blob,Image *image,
419 size_t bytes_per_line,const unsigned int bits_per_pixel,size_t *extent,
420 ExceptionInfo *exception)
428 register unsigned char
449 Determine pixel buffer size.
451 if (bits_per_pixel <= 8)
452 bytes_per_line&=0x7fff;
453 width=image->columns;
455 if (bits_per_pixel == 16)
461 if (bits_per_pixel == 32)
462 width*=image->alpha_trait ? 4 : 3;
463 if (bytes_per_line == 0)
464 bytes_per_line=width;
465 row_bytes=(size_t) (image->columns | 0x8000);
466 if (image->storage_class == DirectClass)
467 row_bytes=(size_t) ((4*image->columns) | 0x8000);
469 Allocate pixel and scanline buffer.
471 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,row_bytes*
473 if (pixels == (unsigned char *) NULL)
474 return((unsigned char *) NULL);
475 *extent=row_bytes*image->rows*sizeof(*pixels);
476 (void) ResetMagickMemory(pixels,0,*extent);
477 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,2*
479 if (scanline == (unsigned char *) NULL)
480 return((unsigned char *) NULL);
481 if (bytes_per_line < 8)
484 Pixels are already uncompressed.
486 for (y=0; y < (ssize_t) image->rows; y++)
488 q=pixels+y*width*GetPixelChannels(image);;
489 number_pixels=bytes_per_line;
490 count=ReadBlob(blob,(size_t) number_pixels,scanline);
491 if (count != (ssize_t) number_pixels)
493 (void) ThrowMagickException(exception,GetMagickModule(),
494 CorruptImageError,"UnableToUncompressImage","`%s'",
498 p=ExpandBuffer(scanline,&number_pixels,bits_per_pixel);
499 if ((q+number_pixels) > (pixels+(*extent)))
501 (void) ThrowMagickException(exception,GetMagickModule(),
502 CorruptImageError,"UnableToUncompressImage","`%s'",
506 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
508 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
512 Uncompress RLE pixels into uncompressed pixel buffer.
514 for (y=0; y < (ssize_t) image->rows; y++)
517 if (bytes_per_line > 200)
518 scanline_length=ReadBlobMSBShort(blob);
520 scanline_length=1UL*ReadBlobByte(blob);
521 if (scanline_length >= row_bytes)
523 (void) ThrowMagickException(exception,GetMagickModule(),
524 CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
527 count=ReadBlob(blob,scanline_length,scanline);
528 if (count != (ssize_t) scanline_length)
530 (void) ThrowMagickException(exception,GetMagickModule(),
531 CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
534 for (j=0; j < (ssize_t) scanline_length; )
535 if ((scanline[j] & 0x80) == 0)
537 length=(size_t) ((scanline[j] & 0xff)+1);
538 number_pixels=length*bytes_per_pixel;
539 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
540 if ((q-pixels+number_pixels) <= *extent)
541 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
543 j+=(ssize_t) (length*bytes_per_pixel+1);
547 length=(size_t) (((scanline[j] ^ 0xff) & 0xff)+2);
548 number_pixels=bytes_per_pixel;
549 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
550 for (i=0; i < (ssize_t) length; i++)
552 if ((q-pixels+number_pixels) <= *extent)
553 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
556 j+=(ssize_t) bytes_per_pixel+1;
559 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568 % E n c o d e I m a g e %
572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574 % EncodeImage compresses an image via Macintosh pack bits encoding
575 % for Macintosh PICT images.
577 % The format of the EncodeImage method is:
579 % size_t EncodeImage(Image *image,const unsigned char *scanline,
580 % const size_t bytes_per_line,unsigned char *pixels)
582 % A description of each parameter follows:
584 % o image: the address of a structure of type Image.
586 % o scanline: A pointer to an array of characters to pack.
588 % o bytes_per_line: the number of bytes in a scanline.
590 % o pixels: A pointer to an array of characters where the packed
591 % characters are stored.
594 static size_t EncodeImage(Image *image,const unsigned char *scanline,
595 const size_t bytes_per_line,unsigned char *pixels)
598 #define MaxPackbitsRunlength 128
600 register const unsigned char
606 register unsigned char
623 assert(image != (Image *) NULL);
624 assert(image->signature == MagickCoreSignature);
625 if (image->debug != MagickFalse)
626 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
627 assert(scanline != (unsigned char *) NULL);
628 assert(pixels != (unsigned char *) NULL);
631 p=scanline+(bytes_per_line-1);
634 for (i=(ssize_t) bytes_per_line-1; i >= 0; i--)
641 while (runlength > 0)
643 *q++=(unsigned char) index;
646 if (count == MaxCount)
648 *q++=(unsigned char) (MaxCount-1);
655 *q++=(unsigned char) (count-1);
657 while (runlength > 0)
659 repeat_count=runlength;
660 if (repeat_count > MaxPackbitsRunlength)
661 repeat_count=MaxPackbitsRunlength;
662 *q++=(unsigned char) index;
663 *q++=(unsigned char) (257-repeat_count);
664 runlength-=repeat_count;
673 while (runlength > 0)
675 *q++=(unsigned char) index;
678 if (count == MaxCount)
680 *q++=(unsigned char) (MaxCount-1);
687 *q++=(unsigned char) (count-1);
689 while (runlength > 0)
691 repeat_count=runlength;
692 if (repeat_count > MaxPackbitsRunlength)
693 repeat_count=MaxPackbitsRunlength;
694 *q++=(unsigned char) index;
695 *q++=(unsigned char) (257-repeat_count);
696 runlength-=repeat_count;
700 *q++=(unsigned char) (count-1);
702 Write the number of and the packed length.
704 length=(size_t) (q-pixels);
705 if (bytes_per_line > 200)
707 (void) WriteBlobMSBShort(image,(unsigned short) length);
712 (void) WriteBlobByte(image,(unsigned char) length);
718 (void) WriteBlobByte(image,*q);
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734 % IsPICT()() returns MagickTrue if the image format type, identified by the
735 % magick string, is PICT.
737 % The format of the ReadPICTImage method is:
739 % MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
741 % A description of each parameter follows:
743 % o magick: compare image format pattern against these bytes.
745 % o length: Specifies the length of the magick string.
748 static MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
753 Embedded OLE2 macintosh have "PICT" instead of 512 platform header.
755 if (memcmp(magick,"PICT",4) == 0)
759 if (memcmp(magick+522,"\000\021\002\377\014\000",6) == 0)
764 #if !defined(macintosh)
766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
770 % R e a d P I C T I m a g e %
774 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776 % ReadPICTImage() reads an Apple Macintosh QuickDraw/PICT image file
777 % and returns it. It allocates the memory necessary for the new Image
778 % structure and returns a pointer to the new image.
780 % The format of the ReadPICTImage method is:
782 % Image *ReadPICTImage(const ImageInfo *image_info,
783 % ExceptionInfo *exception)
785 % A description of each parameter follows:
787 % o image_info: the image info.
789 % o exception: return any errors or warnings in this structure.
793 static MagickBooleanType ReadRectangle(Image *image,PICTRectangle *rectangle)
795 rectangle->top=(short) ReadBlobMSBShort(image);
796 rectangle->left=(short) ReadBlobMSBShort(image);
797 rectangle->bottom=(short) ReadBlobMSBShort(image);
798 rectangle->right=(short) ReadBlobMSBShort(image);
799 if ((EOFBlob(image) != MagickFalse) || (rectangle->left > rectangle->right) ||
800 (rectangle->top > rectangle->bottom))
805 static Image *ReadPICTImage(const ImageInfo *image_info,
806 ExceptionInfo *exception)
809 geometry[MagickPathExtent],
856 assert(image_info != (const ImageInfo *) NULL);
857 assert(image_info->signature == MagickCoreSignature);
858 if (image_info->debug != MagickFalse)
859 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
860 image_info->filename);
861 assert(exception != (ExceptionInfo *) NULL);
862 assert(exception->signature == MagickCoreSignature);
863 image=AcquireImage(image_info,exception);
864 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
865 if (status == MagickFalse)
867 image=DestroyImageList(image);
868 return((Image *) NULL);
873 pixmap.bits_per_pixel=0;
874 pixmap.component_count=0;
876 Skip header : 512 for standard PICT and 4, ie "PICT" for OLE2.
878 header_ole[0]=ReadBlobByte(image);
879 header_ole[1]=ReadBlobByte(image);
880 header_ole[2]=ReadBlobByte(image);
881 header_ole[3]=ReadBlobByte(image);
882 if (!((header_ole[0] == 0x50) && (header_ole[1] == 0x49) &&
883 (header_ole[2] == 0x43) && (header_ole[3] == 0x54 )))
884 for (i=0; i < 508; i++)
885 if (ReadBlobByte(image) == EOF)
887 (void) ReadBlobMSBShort(image); /* skip picture size */
888 if (ReadRectangle(image,&frame) == MagickFalse)
889 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
890 while ((c=ReadBlobByte(image)) == 0) ;
892 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
893 version=ReadBlobByte(image);
896 c=ReadBlobByte(image);
898 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
902 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
903 if ((frame.left < 0) || (frame.right < 0) || (frame.top < 0) ||
904 (frame.bottom < 0) || (frame.left >= frame.right) ||
905 (frame.top >= frame.bottom))
906 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
912 image->columns=1UL*(frame.right-frame.left);
913 image->rows=1UL*(frame.bottom-frame.top);
914 image->resolution.x=DefaultResolution;
915 image->resolution.y=DefaultResolution;
916 image->units=UndefinedResolution;
917 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
918 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
920 (void) CloseBlob(image);
921 return(GetFirstImageInList(image));
923 status=SetImageExtent(image,image->columns,image->rows,exception);
924 if (status == MagickFalse)
925 return(DestroyImageList(image));
927 Interpret PICT opcodes.
930 for (code=0; EOFBlob(image) == MagickFalse; )
932 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
933 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
935 if ((version == 1) || ((TellBlob(image) % 2) != 0))
936 code=ReadBlobByte(image);
938 code=ReadBlobMSBSignedShort(image);
943 if (image->debug != MagickFalse)
944 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%04X:",code);
948 if (image->debug != MagickFalse)
949 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
950 " %04X %s: %s",code,codes[code].name,codes[code].description);
958 length=ReadBlobMSBShort(image);
959 if (length != 0x000a)
961 for (i=0; i < (ssize_t) (length-2); i++)
962 if (ReadBlobByte(image) == EOF)
966 if (ReadRectangle(image,&frame) == MagickFalse)
967 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
968 if (((frame.left & 0x8000) != 0) || ((frame.top & 0x8000) != 0))
970 image->columns=1UL*(frame.right-frame.left);
971 image->rows=1UL*(frame.bottom-frame.top);
972 status=SetImageExtent(image,image->columns,image->rows,exception);
973 if (status == MagickFalse)
974 return(DestroyImageList(image));
975 (void) SetImageBackgroundColor(image,exception);
990 Skip pattern definition.
992 pattern=1L*ReadBlobMSBShort(image);
993 for (i=0; i < 8; i++)
994 if (ReadBlobByte(image) == EOF)
998 for (i=0; i < 5; i++)
999 if (ReadBlobByte(image) == EOF)
1004 ThrowReaderException(CorruptImageError,"UnknownPatternType");
1005 length=ReadBlobMSBShort(image);
1006 if (ReadRectangle(image,&frame) == MagickFalse)
1007 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1009 image->depth=1UL*pixmap.component_size;
1010 image->resolution.x=1.0*pixmap.horizontal_resolution;
1011 image->resolution.y=1.0*pixmap.vertical_resolution;
1012 image->units=PixelsPerInchResolution;
1013 (void) ReadBlobMSBLong(image);
1014 flags=1L*ReadBlobMSBShort(image);
1015 length=ReadBlobMSBShort(image);
1016 for (i=0; i <= (ssize_t) length; i++)
1017 (void) ReadBlobMSBLong(image);
1018 width=1UL*(frame.bottom-frame.top);
1019 height=1UL*(frame.right-frame.left);
1020 if (pixmap.bits_per_pixel <= 8)
1022 if (pixmap.bits_per_pixel == 16)
1028 for (i=0; i < (ssize_t) (length*height); i++)
1029 if (ReadBlobByte(image) == EOF)
1033 for (j=0; j < (int) height; j++)
1036 for (j=0; j < (ssize_t) ReadBlobMSBShort(image); j++)
1037 if (ReadBlobByte(image) == EOF)
1041 for (j=0; j < (ssize_t) ReadBlobByte(image); j++)
1042 if (ReadBlobByte(image) == EOF)
1049 Initialize image background color.
1051 image->background_color.red=(Quantum)
1052 ScaleShortToQuantum(ReadBlobMSBShort(image));
1053 image->background_color.green=(Quantum)
1054 ScaleShortToQuantum(ReadBlobMSBShort(image));
1055 image->background_color.blue=(Quantum)
1056 ScaleShortToQuantum(ReadBlobMSBShort(image));
1069 Skip polygon or region.
1071 length=ReadBlobMSBShort(image);
1072 for (i=0; i < (ssize_t) (length-2); i++)
1073 if (ReadBlobByte(image) == EOF)
1091 register unsigned char
1104 Pixmap clipped by a rectangle.
1107 if ((code != 0x9a) && (code != 0x9b))
1108 bytes_per_line=1L*ReadBlobMSBShort(image);
1111 (void) ReadBlobMSBShort(image);
1112 (void) ReadBlobMSBShort(image);
1113 (void) ReadBlobMSBShort(image);
1115 if (ReadRectangle(image,&frame) == MagickFalse)
1116 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1118 Initialize tile image.
1120 tile_image=CloneImage(image,1UL*(frame.right-frame.left),
1121 1UL*(frame.bottom-frame.top),MagickTrue,exception);
1122 if (tile_image == (Image *) NULL)
1123 return((Image *) NULL);
1124 if ((code == 0x9a) || (code == 0x9b) ||
1125 ((bytes_per_line & 0x8000) != 0))
1128 tile_image->depth=1UL*pixmap.component_size;
1129 tile_image->alpha_trait=pixmap.component_count == 4 ?
1130 BlendPixelTrait : UndefinedPixelTrait;
1131 tile_image->resolution.x=(double) pixmap.horizontal_resolution;
1132 tile_image->resolution.y=(double) pixmap.vertical_resolution;
1133 tile_image->units=PixelsPerInchResolution;
1134 if (tile_image->alpha_trait != UndefinedPixelTrait)
1135 image->alpha_trait=tile_image->alpha_trait;
1137 if ((code != 0x9a) && (code != 0x9b))
1140 Initialize colormap.
1142 tile_image->colors=2;
1143 if ((bytes_per_line & 0x8000) != 0)
1145 (void) ReadBlobMSBLong(image);
1146 flags=1L*ReadBlobMSBShort(image);
1147 tile_image->colors=1UL*ReadBlobMSBShort(image)+1;
1149 status=AcquireImageColormap(tile_image,tile_image->colors,
1151 if (status == MagickFalse)
1153 tile_image=DestroyImage(tile_image);
1154 ThrowReaderException(ResourceLimitError,
1155 "MemoryAllocationFailed");
1157 if ((bytes_per_line & 0x8000) != 0)
1159 for (i=0; i < (ssize_t) tile_image->colors; i++)
1161 j=ReadBlobMSBShort(image) % tile_image->colors;
1162 if ((flags & 0x8000) != 0)
1164 tile_image->colormap[j].red=(Quantum)
1165 ScaleShortToQuantum(ReadBlobMSBShort(image));
1166 tile_image->colormap[j].green=(Quantum)
1167 ScaleShortToQuantum(ReadBlobMSBShort(image));
1168 tile_image->colormap[j].blue=(Quantum)
1169 ScaleShortToQuantum(ReadBlobMSBShort(image));
1174 for (i=0; i < (ssize_t) tile_image->colors; i++)
1176 tile_image->colormap[i].red=(Quantum) (QuantumRange-
1177 tile_image->colormap[i].red);
1178 tile_image->colormap[i].green=(Quantum) (QuantumRange-
1179 tile_image->colormap[i].green);
1180 tile_image->colormap[i].blue=(Quantum) (QuantumRange-
1181 tile_image->colormap[i].blue);
1185 if (ReadRectangle(image,&source) == MagickFalse)
1187 tile_image=DestroyImage(tile_image);
1188 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1190 if (ReadRectangle(image,&destination) == MagickFalse)
1192 tile_image=DestroyImage(tile_image);
1193 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1195 (void) ReadBlobMSBShort(image);
1196 if ((code == 0x91) || (code == 0x99) || (code == 0x9b))
1201 length=ReadBlobMSBShort(image);
1202 for (i=0; i < (ssize_t) (length-2); i++)
1203 if (ReadBlobByte(image) == EOF)
1206 if ((code != 0x9a) && (code != 0x9b) &&
1207 (bytes_per_line & 0x8000) == 0)
1208 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1,&extent,
1211 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1U*
1212 pixmap.bits_per_pixel,&extent,exception);
1213 if (pixels == (unsigned char *) NULL)
1215 tile_image=DestroyImage(tile_image);
1216 ThrowReaderException(ResourceLimitError,
1217 "MemoryAllocationFailed");
1220 Convert PICT tile image to pixel packets.
1223 for (y=0; y < (ssize_t) tile_image->rows; y++)
1225 if (p > (pixels+extent+image->columns))
1227 tile_image=DestroyImage(tile_image);
1228 ThrowReaderException(CorruptImageError,"NotEnoughPixelData");
1230 q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,
1232 if (q == (Quantum *) NULL)
1234 for (x=0; x < (ssize_t) tile_image->columns; x++)
1236 if (tile_image->storage_class == PseudoClass)
1238 index=ConstrainColormapIndex(tile_image,*p,exception);
1239 SetPixelIndex(tile_image,index,q);
1240 SetPixelRed(tile_image,
1241 tile_image->colormap[(ssize_t) index].red,q);
1242 SetPixelGreen(tile_image,
1243 tile_image->colormap[(ssize_t) index].green,q);
1244 SetPixelBlue(tile_image,
1245 tile_image->colormap[(ssize_t) index].blue,q);
1249 if (pixmap.bits_per_pixel == 16)
1253 SetPixelRed(tile_image,ScaleCharToQuantum(
1254 (unsigned char) ((i & 0x7c) << 1)),q);
1255 SetPixelGreen(tile_image,ScaleCharToQuantum(
1256 (unsigned char) (((i & 0x03) << 6) |
1257 ((j & 0xe0) >> 2))),q);
1258 SetPixelBlue(tile_image,ScaleCharToQuantum(
1259 (unsigned char) ((j & 0x1f) << 3)),q);
1262 if (tile_image->alpha_trait == UndefinedPixelTrait)
1264 if (p > (pixels+extent+2*image->columns))
1266 tile_image=DestroyImage(tile_image);
1267 ThrowReaderException(CorruptImageError,
1268 "NotEnoughPixelData");
1270 SetPixelRed(tile_image,ScaleCharToQuantum(*p),q);
1271 SetPixelGreen(tile_image,ScaleCharToQuantum(
1272 *(p+tile_image->columns)),q);
1273 SetPixelBlue(tile_image,ScaleCharToQuantum(
1274 *(p+2*tile_image->columns)),q);
1278 if (p > (pixels+extent+3*image->columns))
1280 tile_image=DestroyImage(tile_image);
1281 ThrowReaderException(CorruptImageError,
1282 "NotEnoughPixelData");
1284 SetPixelAlpha(tile_image,ScaleCharToQuantum(*p),q);
1285 SetPixelRed(tile_image,ScaleCharToQuantum(
1286 *(p+tile_image->columns)),q);
1287 SetPixelGreen(tile_image,ScaleCharToQuantum(
1288 *(p+2*tile_image->columns)),q);
1289 SetPixelBlue(tile_image,ScaleCharToQuantum(
1290 *(p+3*tile_image->columns)),q);
1294 q+=GetPixelChannels(tile_image);
1296 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
1298 if ((tile_image->storage_class == DirectClass) &&
1299 (pixmap.bits_per_pixel != 16))
1301 p+=(pixmap.component_count-1)*tile_image->columns;
1305 status=SetImageProgress(image,LoadImageTag,y,tile_image->rows);
1306 if (status == MagickFalse)
1309 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1310 if (jpeg == MagickFalse)
1311 if ((code == 0x9a) || (code == 0x9b) ||
1312 ((bytes_per_line & 0x8000) != 0))
1313 (void) CompositeImage(image,tile_image,CopyCompositeOp,
1314 MagickTrue,destination.left,destination.top,exception);
1315 tile_image=DestroyImage(tile_image);
1329 type=ReadBlobMSBShort(image);
1330 length=ReadBlobMSBShort(image);
1333 (void) ReadBlobMSBLong(image);
1337 info=(unsigned char *) AcquireQuantumMemory(length,sizeof(*info));
1338 if (info == (unsigned char *) NULL)
1340 count=ReadBlob(image,length,info);
1341 if (count != (ssize_t) length)
1342 ThrowReaderException(ResourceLimitError,"UnableToReadImageData");
1347 profile=BlobToStringInfo((const void *) NULL,length);
1348 SetStringInfoDatum(profile,info);
1349 status=SetImageProfile(image,"icc",profile,exception);
1350 profile=DestroyStringInfo(profile);
1351 if (status == MagickFalse)
1353 info=(unsigned char *) RelinquishMagickMemory(info);
1354 ThrowReaderException(ResourceLimitError,
1355 "MemoryAllocationFailed");
1361 profile=BlobToStringInfo((const void *) NULL,length);
1362 SetStringInfoDatum(profile,info);
1363 status=SetImageProfile(image,"iptc",profile,exception);
1364 if (status == MagickFalse)
1366 info=(unsigned char *) RelinquishMagickMemory(info);
1367 ThrowReaderException(ResourceLimitError,
1368 "MemoryAllocationFailed");
1370 profile=DestroyStringInfo(profile);
1376 info=(unsigned char *) RelinquishMagickMemory(info);
1382 Skip to next op code.
1384 if (codes[code].length == -1)
1385 (void) ReadBlobMSBShort(image);
1387 for (i=0; i < (ssize_t) codes[code].length; i++)
1388 if (ReadBlobByte(image) == EOF)
1398 for (i=0; i < 24; i++)
1399 if (ReadBlobByte(image) == EOF)
1403 if (((code >= 0xb0) && (code <= 0xcf)) ||
1404 ((code >= 0x8000) && (code <= 0x80ff)))
1424 read_info=CloneImageInfo(image_info);
1425 SetImageInfoBlob(read_info,(void *) NULL,0);
1427 unique_file=AcquireUniqueFileResource(read_info->filename);
1428 if (unique_file != -1)
1429 file=fdopen(unique_file,"wb");
1430 if ((unique_file == -1) || (file == (FILE *) NULL))
1432 (void) RelinquishUniqueFileResource(read_info->filename);
1433 (void) CopyMagickString(image->filename,read_info->filename,
1435 ThrowFileException(exception,FileOpenError,
1436 "UnableToCreateTemporaryFile",image->filename);
1437 image=DestroyImageList(image);
1438 return((Image *) NULL);
1440 length=ReadBlobMSBLong(image);
1443 for (i=0; i < 6; i++)
1444 (void) ReadBlobMSBLong(image);
1445 if (ReadRectangle(image,&frame) == MagickFalse)
1447 (void) fclose(file);
1448 (void) RelinquishUniqueFileResource(read_info->filename);
1449 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1451 for (i=0; i < 122; i++)
1452 if (ReadBlobByte(image) == EOF)
1454 for (i=0; i < (ssize_t) (length-154); i++)
1456 c=ReadBlobByte(image);
1459 (void) fputc(c,file);
1462 (void) fclose(file);
1463 (void) close(unique_file);
1464 tile_image=ReadImage(read_info,exception);
1465 (void) RelinquishUniqueFileResource(read_info->filename);
1466 read_info=DestroyImageInfo(read_info);
1467 if (tile_image == (Image *) NULL)
1469 (void) FormatLocaleString(geometry,MagickPathExtent,"%.20gx%.20g",
1470 (double) MagickMax(image->columns,tile_image->columns),
1471 (double) MagickMax(image->rows,tile_image->rows));
1472 (void) SetImageExtent(image,
1473 MagickMax(image->columns,tile_image->columns),
1474 MagickMax(image->rows,tile_image->rows),exception);
1475 (void) TransformImageColorspace(image,tile_image->colorspace,exception);
1476 (void) CompositeImage(image,tile_image,CopyCompositeOp,MagickTrue,
1477 frame.left,frame.right,exception);
1478 image->compression=tile_image->compression;
1479 tile_image=DestroyImage(tile_image);
1482 if ((code == 0xff) || (code == 0xffff))
1484 if (((code >= 0xd0) && (code <= 0xfe)) ||
1485 ((code >= 0x8100) && (code <= 0xffff)))
1490 length=ReadBlobMSBShort(image);
1491 for (i=0; i < (ssize_t) length; i++)
1492 if (ReadBlobByte(image) == EOF)
1496 if ((code >= 0x100) && (code <= 0x7fff))
1501 length=(size_t) ((code >> 7) & 0xff);
1502 for (i=0; i < (ssize_t) length; i++)
1503 if (ReadBlobByte(image) == EOF)
1508 (void) CloseBlob(image);
1509 return(GetFirstImageInList(image));
1514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1518 % R e g i s t e r P I C T I m a g e %
1522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1524 % RegisterPICTImage() adds attributes for the PICT image format to
1525 % the list of supported formats. The attributes include the image format
1526 % tag, a method to read and/or write the format, whether the format
1527 % supports the saving of more than one frame to the same file or blob,
1528 % whether the format supports native in-memory I/O, and a brief
1529 % description of the format.
1531 % The format of the RegisterPICTImage method is:
1533 % size_t RegisterPICTImage(void)
1536 ModuleExport size_t RegisterPICTImage(void)
1541 entry=AcquireMagickInfo("PICT","PCT","Apple Macintosh QuickDraw/PICT");
1542 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1543 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1544 entry->flags^=CoderAdjoinFlag;
1545 entry->flags|=CoderEncoderSeekableStreamFlag;
1546 entry->magick=(IsImageFormatHandler *) IsPICT;
1547 (void) RegisterMagickInfo(entry);
1548 entry=AcquireMagickInfo("PICT","PICT","Apple Macintosh QuickDraw/PICT");
1549 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1550 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1551 entry->flags^=CoderAdjoinFlag;
1552 entry->flags|=CoderEncoderSeekableStreamFlag;
1553 entry->magick=(IsImageFormatHandler *) IsPICT;
1554 (void) RegisterMagickInfo(entry);
1555 return(MagickImageCoderSignature);
1559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1563 % U n r e g i s t e r P I C T I m a g e %
1567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1569 % UnregisterPICTImage() removes format registrations made by the
1570 % PICT module from the list of supported formats.
1572 % The format of the UnregisterPICTImage method is:
1574 % UnregisterPICTImage(void)
1577 ModuleExport void UnregisterPICTImage(void)
1579 (void) UnregisterMagickInfo("PCT");
1580 (void) UnregisterMagickInfo("PICT");
1584 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1588 % W r i t e P I C T I m a g e %
1592 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1594 % WritePICTImage() writes an image to a file in the Apple Macintosh
1595 % QuickDraw/PICT image format.
1597 % The format of the WritePICTImage method is:
1599 % MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1600 % Image *image,ExceptionInfo *exception)
1602 % A description of each parameter follows.
1604 % o image_info: the image info.
1606 % o image: The image.
1608 % o exception: return any errors or warnings in this structure.
1611 static MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1612 Image *image,ExceptionInfo *exception)
1614 #define MaxCount 128
1615 #define PictCropRegionOp 0x01
1616 #define PictEndOfPictureOp 0xff
1617 #define PictJPEGOp 0x8200
1618 #define PictInfoOp 0x0C00
1619 #define PictInfoSize 512
1620 #define PictPixmapOp 0x9A
1621 #define PictPICTOp 0x98
1622 #define PictVersion 0x11
1643 destination_rectangle,
1648 register const Quantum
1674 Open output image file.
1676 assert(image_info != (const ImageInfo *) NULL);
1677 assert(image_info->signature == MagickCoreSignature);
1678 assert(image != (Image *) NULL);
1679 assert(image->signature == MagickCoreSignature);
1680 if (image->debug != MagickFalse)
1681 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1682 if ((image->columns > 65535L) || (image->rows > 65535L))
1683 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1684 assert(exception != (ExceptionInfo *) NULL);
1685 assert(exception->signature == MagickCoreSignature);
1686 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1687 if (status == MagickFalse)
1689 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1691 Initialize image info.
1693 size_rectangle.top=0;
1694 size_rectangle.left=0;
1695 size_rectangle.bottom=(short) image->rows;
1696 size_rectangle.right=(short) image->columns;
1697 frame_rectangle=size_rectangle;
1698 crop_rectangle=size_rectangle;
1699 source_rectangle=size_rectangle;
1700 destination_rectangle=size_rectangle;
1702 row_bytes=image->columns;
1705 bounds.bottom=(short) image->rows;
1706 bounds.right=(short) image->columns;
1710 pixmap.pixel_type=0;
1711 pixmap.bits_per_pixel=8;
1712 pixmap.component_count=1;
1713 pixmap.component_size=8;
1714 pixmap.plane_bytes=0;
1718 x_resolution=image->resolution.x != 0.0 ? image->resolution.x :
1720 y_resolution=image->resolution.y != 0.0 ? image->resolution.y :
1722 storage_class=image->storage_class;
1723 if (image_info->compression == JPEGCompression)
1724 storage_class=DirectClass;
1725 if (storage_class == DirectClass)
1727 pixmap.component_count=image->alpha_trait != UndefinedPixelTrait ? 4 : 3;
1728 pixmap.pixel_type=16;
1729 pixmap.bits_per_pixel=32;
1730 pixmap.pack_type=0x04;
1732 row_bytes=4*image->columns;
1737 bytes_per_line=image->columns;
1738 if (storage_class == DirectClass)
1739 bytes_per_line*=image->alpha_trait != UndefinedPixelTrait ? 4 : 3;
1740 buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer));
1741 packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t)
1742 (row_bytes+MaxCount),sizeof(*packed_scanline));
1743 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
1744 if ((buffer == (unsigned char *) NULL) ||
1745 (packed_scanline == (unsigned char *) NULL) ||
1746 (scanline == (unsigned char *) NULL))
1748 if (scanline != (unsigned char *) NULL)
1749 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
1750 if (packed_scanline != (unsigned char *) NULL)
1751 packed_scanline=(unsigned char *) RelinquishMagickMemory(
1753 if (buffer != (unsigned char *) NULL)
1754 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1755 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1757 (void) ResetMagickMemory(scanline,0,row_bytes);
1758 (void) ResetMagickMemory(packed_scanline,0,(size_t) (row_bytes+MaxCount));
1760 Write header, header size, size bounding box, version, and reserved.
1762 (void) ResetMagickMemory(buffer,0,PictInfoSize);
1763 (void) WriteBlob(image,PictInfoSize,buffer);
1764 (void) WriteBlobMSBShort(image,0);
1765 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top);
1766 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left);
1767 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom);
1768 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right);
1769 (void) WriteBlobMSBShort(image,PictVersion);
1770 (void) WriteBlobMSBShort(image,0x02ff); /* version #2 */
1771 (void) WriteBlobMSBShort(image,PictInfoOp);
1772 (void) WriteBlobMSBLong(image,0xFFFE0000UL);
1774 Write full size of the file, resolution, frame bounding box, and reserved.
1776 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1777 (void) WriteBlobMSBShort(image,0x0000);
1778 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1779 (void) WriteBlobMSBShort(image,0x0000);
1780 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top);
1781 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left);
1782 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom);
1783 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right);
1784 (void) WriteBlobMSBLong(image,0x00000000L);
1785 profile=GetImageProfile(image,"iptc");
1786 if (profile != (StringInfo *) NULL)
1788 (void) WriteBlobMSBShort(image,0xa1);
1789 (void) WriteBlobMSBShort(image,0x1f2);
1790 (void) WriteBlobMSBShort(image,(unsigned short)
1791 (GetStringInfoLength(profile)+4));
1792 (void) WriteBlobString(image,"8BIM");
1793 (void) WriteBlob(image,GetStringInfoLength(profile),
1794 GetStringInfoDatum(profile));
1796 profile=GetImageProfile(image,"icc");
1797 if (profile != (StringInfo *) NULL)
1799 (void) WriteBlobMSBShort(image,0xa1);
1800 (void) WriteBlobMSBShort(image,0xe0);
1801 (void) WriteBlobMSBShort(image,(unsigned short)
1802 (GetStringInfoLength(profile)+4));
1803 (void) WriteBlobMSBLong(image,0x00000000UL);
1804 (void) WriteBlob(image,GetStringInfoLength(profile),
1805 GetStringInfoDatum(profile));
1806 (void) WriteBlobMSBShort(image,0xa1);
1807 (void) WriteBlobMSBShort(image,0xe0);
1808 (void) WriteBlobMSBShort(image,4);
1809 (void) WriteBlobMSBLong(image,0x00000002UL);
1812 Write crop region opcode and crop bounding box.
1814 (void) WriteBlobMSBShort(image,PictCropRegionOp);
1815 (void) WriteBlobMSBShort(image,0xa);
1816 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top);
1817 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left);
1818 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom);
1819 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right);
1820 if (image_info->compression == JPEGCompression)
1834 jpeg_image=CloneImage(image,0,0,MagickTrue,exception);
1835 if (jpeg_image == (Image *) NULL)
1837 (void) CloseBlob(image);
1838 return(MagickFalse);
1840 jpeg_info=CloneImageInfo(image_info);
1841 (void) CopyMagickString(jpeg_info->magick,"JPEG",MagickPathExtent);
1843 blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length,
1845 jpeg_info=DestroyImageInfo(jpeg_info);
1846 if (blob == (unsigned char *) NULL)
1847 return(MagickFalse);
1848 jpeg_image=DestroyImage(jpeg_image);
1849 (void) WriteBlobMSBShort(image,PictJPEGOp);
1850 (void) WriteBlobMSBLong(image,(unsigned int) length+154);
1851 (void) WriteBlobMSBShort(image,0x0000);
1852 (void) WriteBlobMSBLong(image,0x00010000UL);
1853 (void) WriteBlobMSBLong(image,0x00000000UL);
1854 (void) WriteBlobMSBLong(image,0x00000000UL);
1855 (void) WriteBlobMSBLong(image,0x00000000UL);
1856 (void) WriteBlobMSBLong(image,0x00010000UL);
1857 (void) WriteBlobMSBLong(image,0x00000000UL);
1858 (void) WriteBlobMSBLong(image,0x00000000UL);
1859 (void) WriteBlobMSBLong(image,0x00000000UL);
1860 (void) WriteBlobMSBLong(image,0x40000000UL);
1861 (void) WriteBlobMSBLong(image,0x00000000UL);
1862 (void) WriteBlobMSBLong(image,0x00000000UL);
1863 (void) WriteBlobMSBLong(image,0x00000000UL);
1864 (void) WriteBlobMSBLong(image,0x00400000UL);
1865 (void) WriteBlobMSBShort(image,0x0000);
1866 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1867 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1868 (void) WriteBlobMSBShort(image,0x0000);
1869 (void) WriteBlobMSBShort(image,768);
1870 (void) WriteBlobMSBShort(image,0x0000);
1871 (void) WriteBlobMSBLong(image,0x00000000UL);
1872 (void) WriteBlobMSBLong(image,0x00566A70UL);
1873 (void) WriteBlobMSBLong(image,0x65670000UL);
1874 (void) WriteBlobMSBLong(image,0x00000000UL);
1875 (void) WriteBlobMSBLong(image,0x00000001UL);
1876 (void) WriteBlobMSBLong(image,0x00016170UL);
1877 (void) WriteBlobMSBLong(image,0x706C0000UL);
1878 (void) WriteBlobMSBLong(image,0x00000000UL);
1879 (void) WriteBlobMSBShort(image,768);
1880 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1881 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1882 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1883 (void) WriteBlobMSBShort(image,0x0000);
1884 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1885 (void) WriteBlobMSBLong(image,0x00000000UL);
1886 (void) WriteBlobMSBLong(image,0x87AC0001UL);
1887 (void) WriteBlobMSBLong(image,0x0B466F74UL);
1888 (void) WriteBlobMSBLong(image,0x6F202D20UL);
1889 (void) WriteBlobMSBLong(image,0x4A504547UL);
1890 (void) WriteBlobMSBLong(image,0x00000000UL);
1891 (void) WriteBlobMSBLong(image,0x00000000UL);
1892 (void) WriteBlobMSBLong(image,0x00000000UL);
1893 (void) WriteBlobMSBLong(image,0x00000000UL);
1894 (void) WriteBlobMSBLong(image,0x00000000UL);
1895 (void) WriteBlobMSBLong(image,0x0018FFFFUL);
1896 (void) WriteBlob(image,length,blob);
1897 if ((length & 0x01) != 0)
1898 (void) WriteBlobByte(image,'\0');
1899 blob=(unsigned char *) RelinquishMagickMemory(blob);
1902 Write picture opcode, row bytes, and picture bounding box, and version.
1904 if (storage_class == PseudoClass)
1905 (void) WriteBlobMSBShort(image,PictPICTOp);
1908 (void) WriteBlobMSBShort(image,PictPixmapOp);
1909 (void) WriteBlobMSBLong(image,(size_t) base_address);
1911 (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000));
1912 (void) WriteBlobMSBShort(image,(unsigned short) bounds.top);
1913 (void) WriteBlobMSBShort(image,(unsigned short) bounds.left);
1914 (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom);
1915 (void) WriteBlobMSBShort(image,(unsigned short) bounds.right);
1917 Write pack type, pack size, resolution, pixel type, and pixel size.
1919 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version);
1920 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type);
1921 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.pack_size);
1922 (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5));
1923 (void) WriteBlobMSBShort(image,0x0000);
1924 (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5));
1925 (void) WriteBlobMSBShort(image,0x0000);
1926 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type);
1927 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel);
1929 Write component count, size, plane bytes, table size, and reserved.
1931 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count);
1932 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size);
1933 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.plane_bytes);
1934 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.table);
1935 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.reserved);
1936 if (storage_class == PseudoClass)
1939 Write image colormap.
1941 (void) WriteBlobMSBLong(image,0x00000000L); /* color seed */
1942 (void) WriteBlobMSBShort(image,0L); /* color flags */
1943 (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1));
1944 for (i=0; i < (ssize_t) image->colors; i++)
1946 (void) WriteBlobMSBShort(image,(unsigned short) i);
1947 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1948 image->colormap[i].red));
1949 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1950 image->colormap[i].green));
1951 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1952 image->colormap[i].blue));
1956 Write source and destination rectangle.
1958 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top);
1959 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left);
1960 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom);
1961 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right);
1962 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top);
1963 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left);
1964 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom);
1965 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right);
1966 (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode);
1971 if (storage_class == PseudoClass)
1972 for (y=0; y < (ssize_t) image->rows; y++)
1974 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1975 if (p == (const Quantum *) NULL)
1977 for (x=0; x < (ssize_t) image->columns; x++)
1979 scanline[x]=(unsigned char) GetPixelIndex(image,p);
1980 p+=GetPixelChannels(image);
1982 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
1984 if (image->previous == (Image *) NULL)
1986 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1988 if (status == MagickFalse)
1993 if (image_info->compression == JPEGCompression)
1995 (void) ResetMagickMemory(scanline,0,row_bytes);
1996 for (y=0; y < (ssize_t) image->rows; y++)
1997 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
2002 register unsigned char
2009 green=scanline+image->columns;
2010 blue=scanline+2*image->columns;
2011 opacity=scanline+3*image->columns;
2012 for (y=0; y < (ssize_t) image->rows; y++)
2014 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2015 if (p == (const Quantum *) NULL)
2018 green=scanline+image->columns;
2019 blue=scanline+2*image->columns;
2020 if (image->alpha_trait != UndefinedPixelTrait)
2023 red=scanline+image->columns;
2024 green=scanline+2*image->columns;
2025 blue=scanline+3*image->columns;
2027 for (x=0; x < (ssize_t) image->columns; x++)
2029 *red++=ScaleQuantumToChar(GetPixelRed(image,p));
2030 *green++=ScaleQuantumToChar(GetPixelGreen(image,p));
2031 *blue++=ScaleQuantumToChar(GetPixelBlue(image,p));
2032 if (image->alpha_trait != UndefinedPixelTrait)
2033 *opacity++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
2034 p+=GetPixelChannels(image);
2036 count+=EncodeImage(image,scanline,bytes_per_line,packed_scanline);
2037 if (image->previous == (Image *) NULL)
2039 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2041 if (status == MagickFalse)
2046 if ((count & 0x01) != 0)
2047 (void) WriteBlobByte(image,'\0');
2048 (void) WriteBlobMSBShort(image,PictEndOfPictureOp);
2049 offset=TellBlob(image);
2050 offset=SeekBlob(image,512,SEEK_SET);
2051 (void) WriteBlobMSBShort(image,(unsigned short) offset);
2052 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
2053 packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline);
2054 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2055 (void) CloseBlob(image);