2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % PPPP IIIII CCCC TTTTT %
13 % Read/Write Apple Macintosh QuickDraw/PICT Format %
20 % Copyright 1999-2010 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)
440 register unsigned char
461 Determine pixel buffer size.
463 if (bits_per_pixel <= 8)
464 bytes_per_line&=0x7fff;
465 width=image->columns;
467 if (bits_per_pixel == 16)
473 if (bits_per_pixel == 32)
474 width*=image->matte ? 4 : 3;
475 if (bytes_per_line == 0)
476 bytes_per_line=width;
477 row_bytes=(size_t) (image->columns | 0x8000);
478 if (image->storage_class == DirectClass)
479 row_bytes=(size_t) ((4*image->columns) | 0x8000);
481 Allocate pixel and scanline buffer.
483 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,row_bytes*
485 if (pixels == (unsigned char *) NULL)
486 return((unsigned char *) NULL);
487 *extent=row_bytes*image->rows*sizeof(*pixels);
488 (void) ResetMagickMemory(pixels,0,*extent);
489 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
490 if (scanline == (unsigned char *) NULL)
491 return((unsigned char *) NULL);
492 if (bytes_per_line < 8)
495 Pixels are already uncompressed.
497 for (y=0; y < (ssize_t) image->rows; y++)
500 number_pixels=bytes_per_line;
501 count=ReadBlob(blob,(size_t) number_pixels,scanline);
502 p=ExpandBuffer(scanline,&number_pixels,bits_per_pixel);
503 if ((q+number_pixels) > (pixels+(*extent)))
505 (void) ThrowMagickException(&image->exception,GetMagickModule(),
506 CorruptImageError,"UnableToUncompressImage","`%s'",
510 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
512 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
516 Uncompress RLE pixels into uncompressed pixel buffer.
518 for (y=0; y < (ssize_t) image->rows; y++)
521 if (bytes_per_line > 200)
522 scanline_length=ReadBlobMSBShort(blob);
524 scanline_length=1UL*ReadBlobByte(blob);
525 if (scanline_length >= row_bytes)
527 (void) ThrowMagickException(&image->exception,GetMagickModule(),
528 CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
531 count=ReadBlob(blob,scanline_length,scanline);
532 for (j=0; j < (ssize_t) scanline_length; )
533 if ((scanline[j] & 0x80) == 0)
535 length=(size_t) ((scanline[j] & 0xff)+1);
536 number_pixels=length*bytes_per_pixel;
537 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
538 if ((q-pixels+number_pixels) <= *extent)
539 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
541 j+=(ssize_t) (length*bytes_per_pixel+1);
545 length=(size_t) (((scanline[j] ^ 0xff) & 0xff)+2);
546 number_pixels=bytes_per_pixel;
547 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
548 for (i=0; i < (ssize_t) length; i++)
550 if ((q-pixels+number_pixels) <= *extent)
551 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
554 j+=(ssize_t) bytes_per_pixel+1;
557 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % E n c o d e I m a g e %
570 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572 % EncodeImage compresses an image via Macintosh pack bits encoding
573 % for Macintosh PICT images.
575 % The format of the EncodeImage method is:
577 % size_t EncodeImage(Image *image,const unsigned char *scanline,
578 % const size_t bytes_per_line,unsigned char *pixels)
580 % A description of each parameter follows:
582 % o image: the address of a structure of type Image.
584 % o scanline: A pointer to an array of characters to pack.
586 % o bytes_per_line: the number of bytes in a scanline.
588 % o pixels: A pointer to an array of characters where the packed
589 % characters are stored.
592 static size_t EncodeImage(Image *image,const unsigned char *scanline,
593 const size_t bytes_per_line,unsigned char *pixels)
596 #define MaxPackbitsRunlength 128
603 register const unsigned char
609 register unsigned char
621 assert(image != (Image *) NULL);
622 assert(image->signature == MagickSignature);
623 if (image->debug != MagickFalse)
624 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
625 assert(scanline != (unsigned char *) NULL);
626 assert(pixels != (unsigned char *) NULL);
629 p=scanline+(bytes_per_line-1);
632 for (i=(ssize_t) bytes_per_line-1; i >= 0; i--)
639 while (runlength > 0)
641 *q++=(unsigned char) index;
644 if (count == MaxCount)
646 *q++=(unsigned char) (MaxCount-1);
653 *q++=(unsigned char) (count-1);
655 while (runlength > 0)
657 repeat_count=runlength;
658 if (repeat_count > MaxPackbitsRunlength)
659 repeat_count=MaxPackbitsRunlength;
660 *q++=(unsigned char) index;
661 *q++=(unsigned char) (257-repeat_count);
662 runlength-=repeat_count;
671 while (runlength > 0)
673 *q++=(unsigned char) index;
676 if (count == MaxCount)
678 *q++=(unsigned char) (MaxCount-1);
685 *q++=(unsigned char) (count-1);
687 while (runlength > 0)
689 repeat_count=runlength;
690 if (repeat_count > MaxPackbitsRunlength)
691 repeat_count=MaxPackbitsRunlength;
692 *q++=(unsigned char) index;
693 *q++=(unsigned char) (257-repeat_count);
694 runlength-=repeat_count;
698 *q++=(unsigned char) (count-1);
700 Write the number of and the packed length.
702 length=(size_t) (q-pixels);
703 if (bytes_per_line > 200)
705 (void) WriteBlobMSBShort(image,(unsigned short) length);
710 (void) WriteBlobByte(image,(unsigned char) length);
716 (void) WriteBlobByte(image,*q);
722 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732 % IsPICT()() returns MagickTrue if the image format type, identified by the
733 % magick string, is PICT.
735 % The format of the ReadPICTImage method is:
737 % MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
739 % A description of each parameter follows:
741 % o magick: compare image format pattern against these bytes.
743 % o length: Specifies the length of the magick string.
746 static MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
750 if (memcmp(magick+522,"\000\021\002\377\014\000",6) == 0)
755 #if !defined(macintosh)
757 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761 % R e a d P I C T I m a g e %
765 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
767 % ReadPICTImage() reads an Apple Macintosh QuickDraw/PICT image file
768 % and returns it. It allocates the memory necessary for the new Image
769 % structure and returns a pointer to the new image.
771 % The format of the ReadPICTImage method is:
773 % Image *ReadPICTImage(const ImageInfo *image_info,
774 % ExceptionInfo *exception)
776 % A description of each parameter follows:
778 % o image_info: the image info.
780 % o exception: return any errors or warnings in this structure.
784 static inline size_t MagickMax(const size_t x,
792 static Image *ReadPICTImage(const ImageInfo *image_info,
793 ExceptionInfo *exception)
796 geometry[MaxTextExtent];
849 assert(image_info != (const ImageInfo *) NULL);
850 assert(image_info->signature == MagickSignature);
851 if (image_info->debug != MagickFalse)
852 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
853 image_info->filename);
854 assert(exception != (ExceptionInfo *) NULL);
855 assert(exception->signature == MagickSignature);
856 image=AcquireImage(image_info);
858 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
859 if (status == MagickFalse)
861 image=DestroyImageList(image);
862 return((Image *) NULL);
867 pixmap.bits_per_pixel=0;
868 pixmap.component_count=0;
869 for (i=0; i < 512; i++)
870 (void) ReadBlobByte(image); /* skip header */
871 (void) ReadBlobMSBShort(image); /* skip picture size */
872 ReadRectangle(image,frame);
873 while ((c=ReadBlobByte(image)) == 0) ;
875 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
876 version=ReadBlobByte(image);
879 c=ReadBlobByte(image);
881 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
885 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
886 if ((frame.left < 0) || (frame.right < 0) || (frame.top < 0) ||
887 (frame.bottom < 0) || (frame.left >= frame.right) ||
888 (frame.top >= frame.bottom))
889 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
894 image->columns=1UL*(frame.right-frame.left);
895 image->rows=1UL*(frame.bottom-frame.top);
896 image->x_resolution=DefaultResolution;
897 image->y_resolution=DefaultResolution;
898 image->units=UndefinedResolution;
900 Interpret PICT opcodes.
903 for (code=0; EOFBlob(image) == MagickFalse; )
905 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
906 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
908 if ((version == 1) || ((TellBlob(image) % 2) != 0))
909 code=ReadBlobByte(image);
911 code=(int) ReadBlobMSBShort(image);
914 if (image->debug != MagickFalse)
915 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%04X:",code);
919 if (image->debug != MagickFalse)
920 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
921 " %04X %s: %s",code,codes[code].name,codes[code].description);
929 length=ReadBlobMSBShort(image);
930 if (length != 0x000a)
932 for (i=0; i < (ssize_t) (length-2); i++)
933 (void) ReadBlobByte(image);
936 ReadRectangle(image,frame);
937 if (((frame.left & 0x8000) != 0) || ((frame.top & 0x8000) != 0))
939 image->columns=1UL*(frame.right-frame.left);
940 image->rows=1UL*(frame.bottom-frame.top);
941 (void) SetImageBackgroundColor(image);
956 Skip pattern definition.
958 pattern=1L*ReadBlobMSBShort(image);
959 for (i=0; i < 8; i++)
960 (void) ReadBlobByte(image);
963 for (i=0; i < 5; i++)
964 (void) ReadBlobByte(image);
968 ThrowReaderException(CorruptImageError,"UnknownPatternType");
969 length=ReadBlobMSBShort(image);
970 ReadRectangle(image,frame);
972 image->depth=1UL*pixmap.component_size;
973 image->x_resolution=1.0*pixmap.horizontal_resolution;
974 image->y_resolution=1.0*pixmap.vertical_resolution;
975 image->units=PixelsPerInchResolution;
976 (void) ReadBlobMSBLong(image);
977 flags=1L*ReadBlobMSBShort(image);
978 length=ReadBlobMSBShort(image);
979 for (i=0; i <= (ssize_t) length; i++)
980 (void) ReadBlobMSBLong(image);
981 width=1UL*(frame.bottom-frame.top);
982 height=1UL*(frame.right-frame.left);
983 if (pixmap.bits_per_pixel <= 8)
985 if (pixmap.bits_per_pixel == 16)
991 for (i=0; i < (ssize_t) (length*height); i++)
992 (void) ReadBlobByte(image);
995 for (j=0; j < (int) height; j++)
997 for (j=0; j < (ssize_t) ReadBlobMSBShort(image); j++)
998 (void) ReadBlobByte(image);
1000 for (j=0; j < (ssize_t) ReadBlobByte(image); j++)
1001 (void) ReadBlobByte(image);
1007 Initialize image background color.
1009 image->background_color.red=(Quantum)
1010 ScaleShortToQuantum(ReadBlobMSBShort(image));
1011 image->background_color.green=(Quantum)
1012 ScaleShortToQuantum(ReadBlobMSBShort(image));
1013 image->background_color.blue=(Quantum)
1014 ScaleShortToQuantum(ReadBlobMSBShort(image));
1027 Skip polygon or region.
1029 length=ReadBlobMSBShort(image);
1030 for (i=0; i < (ssize_t) (length-2); i++)
1031 (void) ReadBlobByte(image);
1048 register unsigned char
1061 Pixmap clipped by a rectangle.
1064 if ((code != 0x9a) && (code != 0x9b))
1065 bytes_per_line=1L*ReadBlobMSBShort(image);
1068 (void) ReadBlobMSBShort(image);
1069 (void) ReadBlobMSBShort(image);
1070 (void) ReadBlobMSBShort(image);
1072 ReadRectangle(image,frame);
1074 Initialize tile image.
1076 tile_image=CloneImage(image,1UL*(frame.right-frame.left),
1077 1UL*(frame.bottom-frame.top),MagickTrue,exception);
1078 if (tile_image == (Image *) NULL)
1079 return((Image *) NULL);
1080 if ((code == 0x9a) || (code == 0x9b) ||
1081 ((bytes_per_line & 0x8000) != 0))
1084 tile_image->depth=1UL*pixmap.component_size;
1085 tile_image->matte=pixmap.component_count == 4 ?
1086 MagickTrue : MagickFalse;
1087 tile_image->x_resolution=(double) pixmap.horizontal_resolution;
1088 tile_image->y_resolution=(double) pixmap.vertical_resolution;
1089 tile_image->units=PixelsPerInchResolution;
1090 if (tile_image->matte != MagickFalse)
1091 image->matte=tile_image->matte;
1093 if ((code != 0x9a) && (code != 0x9b))
1096 Initialize colormap.
1098 tile_image->colors=2;
1099 if ((bytes_per_line & 0x8000) != 0)
1101 (void) ReadBlobMSBLong(image);
1102 flags=1L*ReadBlobMSBShort(image);
1103 tile_image->colors=1UL*ReadBlobMSBShort(image)+1;
1105 status=AcquireImageColormap(tile_image,tile_image->colors);
1106 if (status == MagickFalse)
1108 tile_image=DestroyImage(tile_image);
1109 ThrowReaderException(ResourceLimitError,
1110 "MemoryAllocationFailed");
1112 if ((bytes_per_line & 0x8000) != 0)
1114 for (i=0; i < (ssize_t) tile_image->colors; i++)
1116 j=ReadBlobMSBShort(image) % tile_image->colors;
1117 if ((flags & 0x8000) != 0)
1119 tile_image->colormap[j].red=(Quantum)
1120 ScaleShortToQuantum(ReadBlobMSBShort(image));
1121 tile_image->colormap[j].green=(Quantum)
1122 ScaleShortToQuantum(ReadBlobMSBShort(image));
1123 tile_image->colormap[j].blue=(Quantum)
1124 ScaleShortToQuantum(ReadBlobMSBShort(image));
1129 for (i=0; i < (ssize_t) tile_image->colors; i++)
1131 tile_image->colormap[i].red=(Quantum) (QuantumRange-
1132 tile_image->colormap[i].red);
1133 tile_image->colormap[i].green=(Quantum) (QuantumRange-
1134 tile_image->colormap[i].green);
1135 tile_image->colormap[i].blue=(Quantum) (QuantumRange-
1136 tile_image->colormap[i].blue);
1140 ReadRectangle(image,source);
1141 ReadRectangle(image,destination);
1142 (void) ReadBlobMSBShort(image);
1143 if ((code == 0x91) || (code == 0x99) || (code == 0x9b))
1148 length=ReadBlobMSBShort(image);
1149 for (i=0; i < (ssize_t) (length-2); i++)
1150 (void) ReadBlobByte(image);
1152 if ((code != 0x9a) && (code != 0x9b) &&
1153 (bytes_per_line & 0x8000) == 0)
1154 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1,&extent);
1156 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1U*
1157 pixmap.bits_per_pixel,&extent);
1158 if (pixels == (unsigned char *) NULL)
1160 tile_image=DestroyImage(tile_image);
1161 ThrowReaderException(ResourceLimitError,
1162 "MemoryAllocationFailed");
1165 Convert PICT tile image to pixel packets.
1168 for (y=0; y < (ssize_t) tile_image->rows; y++)
1170 if (p > (pixels+extent+image->columns))
1171 ThrowReaderException(CorruptImageError,"NotEnoughPixelData");
1172 q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,
1174 if (q == (PixelPacket *) NULL)
1176 indexes=GetAuthenticIndexQueue(tile_image);
1177 for (x=0; x < (ssize_t) tile_image->columns; x++)
1179 if (tile_image->storage_class == PseudoClass)
1181 index=ConstrainColormapIndex(tile_image,*p);
1183 q->red=tile_image->colormap[(ssize_t) index].red;
1184 q->green=tile_image->colormap[(ssize_t) index].green;
1185 q->blue=tile_image->colormap[(ssize_t) index].blue;
1189 if (pixmap.bits_per_pixel == 16)
1193 q->red=ScaleCharToQuantum((unsigned char)
1195 q->green=ScaleCharToQuantum((unsigned char)
1196 (((i & 0x03) << 6) | ((j & 0xe0) >> 2)));
1197 q->blue=ScaleCharToQuantum((unsigned char)
1201 if (tile_image->matte == MagickFalse)
1203 if (p > (pixels+extent+2*image->columns))
1204 ThrowReaderException(CorruptImageError,
1205 "NotEnoughPixelData");
1206 q->red=ScaleCharToQuantum(*p);
1207 q->green=ScaleCharToQuantum(
1208 *(p+tile_image->columns));
1209 q->blue=ScaleCharToQuantum(
1210 *(p+2*tile_image->columns));
1214 if (p > (pixels+extent+3*image->columns))
1215 ThrowReaderException(CorruptImageError,
1216 "NotEnoughPixelData");
1217 q->opacity=(Quantum) (QuantumRange-
1218 ScaleCharToQuantum(*p));
1219 q->red=ScaleCharToQuantum(*(p+tile_image->columns));
1220 q->green=(Quantum) ScaleCharToQuantum(
1221 *(p+2*tile_image->columns));
1222 q->blue=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);
1280 profile=AcquireStringInfo(length);
1281 SetStringInfoDatum(profile,info);
1282 status=SetImageProfile(image,"icc",profile);
1283 profile=DestroyStringInfo(profile);
1284 if (status == MagickFalse)
1285 ThrowReaderException(ResourceLimitError,
1286 "MemoryAllocationFailed");
1293 profile=AcquireStringInfo(length);
1294 SetStringInfoDatum(profile,info);
1295 status=SetImageProfile(image,"iptc",profile);
1296 if (status == MagickFalse)
1297 ThrowReaderException(ResourceLimitError,
1298 "MemoryAllocationFailed");
1299 profile=DestroyStringInfo(profile);
1305 info=(unsigned char *) RelinquishMagickMemory(info);
1311 Skip to next op code.
1313 if (codes[code].length == -1)
1314 (void) ReadBlobMSBShort(image);
1316 for (i=0; i < (ssize_t) codes[code].length; i++)
1317 (void) ReadBlobByte(image);
1326 for (i=0; i < 24; i++)
1327 (void) ReadBlobByte(image);
1330 if (((code >= 0xb0) && (code <= 0xcf)) ||
1331 ((code >= 0x8000) && (code <= 0x80ff)))
1351 read_info=CloneImageInfo(image_info);
1352 SetImageInfoBlob(read_info,(void *) NULL,0);
1354 unique_file=AcquireUniqueFileResource(read_info->filename);
1355 if (unique_file != -1)
1356 file=fdopen(unique_file,"wb");
1357 if ((unique_file == -1) || (file == (FILE *) NULL))
1359 (void) CopyMagickString(image->filename,read_info->filename,
1361 ThrowFileException(exception,FileOpenError,
1362 "UnableToCreateTemporaryFile",image->filename);
1363 image=DestroyImageList(image);
1364 return((Image *) NULL);
1366 length=ReadBlobMSBLong(image);
1367 for (i=0; i < 6; i++)
1368 (void) ReadBlobMSBLong(image);
1369 ReadRectangle(image,frame);
1370 for (i=0; i < 122; i++)
1371 (void) ReadBlobByte(image);
1372 for (i=0; i < (ssize_t) (length-154); i++)
1374 c=ReadBlobByte(image);
1375 (void) fputc(c,file);
1377 (void) fclose(file);
1378 tile_image=ReadImage(read_info,exception);
1379 (void) RelinquishUniqueFileResource(read_info->filename);
1380 read_info=DestroyImageInfo(read_info);
1381 if (tile_image == (Image *) NULL)
1383 (void) FormatMagickString(geometry,MaxTextExtent,"%.20gx%.20g",
1384 (double) MagickMax(image->columns,tile_image->columns),
1385 (double) MagickMax(image->rows,tile_image->rows));
1386 (void) SetImageExtent(image,
1387 MagickMax(image->columns,tile_image->columns),
1388 MagickMax(image->rows,tile_image->rows));
1389 if (image->colorspace != RGBColorspace)
1390 (void) TransformImageColorspace(image,tile_image->colorspace);
1391 (void) CompositeImage(image,CopyCompositeOp,tile_image,frame.left,
1393 image->compression=tile_image->compression;
1394 tile_image=DestroyImage(tile_image);
1397 if ((code == 0xff) || (code == 0xffff))
1399 if (((code >= 0xd0) && (code <= 0xfe)) ||
1400 ((code >= 0x8100) && (code <= 0xffff)))
1405 length=ReadBlobMSBShort(image);
1406 for (i=0; i < (ssize_t) length; i++)
1407 (void) ReadBlobByte(image);
1410 if ((code >= 0x100) && (code <= 0x7fff))
1415 length=(size_t) ((code >> 7) & 0xff);
1416 for (i=0; i < (ssize_t) length; i++)
1417 (void) ReadBlobByte(image);
1421 (void) CloseBlob(image);
1422 return(GetFirstImageInList(image));
1427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1431 % R e g i s t e r P I C T I m a g e %
1435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1437 % RegisterPICTImage() adds attributes for the PICT image format to
1438 % the list of supported formats. The attributes include the image format
1439 % tag, a method to read and/or write the format, whether the format
1440 % supports the saving of more than one frame to the same file or blob,
1441 % whether the format supports native in-memory I/O, and a brief
1442 % description of the format.
1444 % The format of the RegisterPICTImage method is:
1446 % size_t RegisterPICTImage(void)
1449 ModuleExport size_t RegisterPICTImage(void)
1454 entry=SetMagickInfo("PCT");
1455 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1456 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1457 entry->adjoin=MagickFalse;
1458 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1459 entry->magick=(IsImageFormatHandler *) IsPICT;
1460 entry->module=ConstantString("PICT");
1461 (void) RegisterMagickInfo(entry);
1462 entry=SetMagickInfo("PICT");
1463 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1464 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1465 entry->adjoin=MagickFalse;
1466 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1467 entry->magick=(IsImageFormatHandler *) IsPICT;
1468 entry->module=ConstantString("PICT");
1469 (void) RegisterMagickInfo(entry);
1470 return(MagickImageCoderSignature);
1474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1478 % U n r e g i s t e r P I C T I m a g e %
1482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1484 % UnregisterPICTImage() removes format registrations made by the
1485 % PICT module from the list of supported formats.
1487 % The format of the UnregisterPICTImage method is:
1489 % UnregisterPICTImage(void)
1492 ModuleExport void UnregisterPICTImage(void)
1494 (void) UnregisterMagickInfo("PCT");
1495 (void) UnregisterMagickInfo("PICT");
1499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1503 % W r i t e P I C T I m a g e %
1507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1509 % WritePICTImage() writes an image to a file in the Apple Macintosh
1510 % QuickDraw/PICT image format.
1512 % The format of the WritePICTImage method is:
1514 % MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1517 % A description of each parameter follows.
1519 % o image_info: the image info.
1521 % o image: The image.
1524 static MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1527 #define MaxCount 128
1528 #define PictCropRegionOp 0x01
1529 #define PictEndOfPictureOp 0xff
1530 #define PictJPEGOp 0x8200
1531 #define PictInfoOp 0x0C00
1532 #define PictInfoSize 512
1533 #define PictPixmapOp 0x9A
1534 #define PictPICTOp 0x98
1535 #define PictVersion 0x11
1559 destination_rectangle,
1564 register const IndexPacket
1567 register const PixelPacket
1592 Open output image file.
1594 assert(image_info != (const ImageInfo *) NULL);
1595 assert(image_info->signature == MagickSignature);
1596 assert(image != (Image *) NULL);
1597 assert(image->signature == MagickSignature);
1598 if (image->debug != MagickFalse)
1599 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1600 if ((image->columns > 65535L) || (image->rows > 65535L))
1601 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1602 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1603 if (status == MagickFalse)
1605 if (image->colorspace != RGBColorspace)
1606 (void) TransformImageColorspace(image,RGBColorspace);
1608 Initialize image info.
1610 size_rectangle.top=0;
1611 size_rectangle.left=0;
1612 size_rectangle.bottom=(short) image->rows;
1613 size_rectangle.right=(short) image->columns;
1614 frame_rectangle=size_rectangle;
1615 crop_rectangle=size_rectangle;
1616 source_rectangle=size_rectangle;
1617 destination_rectangle=size_rectangle;
1619 row_bytes=(unsigned short) (image->columns | 0x8000);
1622 bounds.bottom=(short) image->rows;
1623 bounds.right=(short) image->columns;
1627 pixmap.pixel_type=0;
1628 pixmap.bits_per_pixel=8;
1629 pixmap.component_count=1;
1630 pixmap.component_size=8;
1631 pixmap.plane_bytes=0;
1635 x_resolution=image->x_resolution != 0.0 ? image->x_resolution :
1637 y_resolution=image->y_resolution != 0.0 ? image->y_resolution :
1639 storage_class=image->storage_class;
1640 if (image_info->compression == JPEGCompression)
1641 storage_class=DirectClass;
1642 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1644 pixmap.component_count=image->matte ? 4 : 3;
1645 pixmap.pixel_type=16;
1646 pixmap.bits_per_pixel=32;
1647 pixmap.pack_type=0x04;
1649 row_bytes=(unsigned short) ((4*image->columns) | 0x8000);
1654 bytes_per_line=image->columns;
1655 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1656 bytes_per_line*=image->matte ? 4 : 3;
1657 buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer));
1658 packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t)
1659 (row_bytes+MaxCount),sizeof(*packed_scanline));
1660 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
1661 if ((buffer == (unsigned char *) NULL) ||
1662 (packed_scanline == (unsigned char *) NULL) ||
1663 (scanline == (unsigned char *) NULL))
1664 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1665 (void) ResetMagickMemory(scanline,0,row_bytes);
1666 (void) ResetMagickMemory(packed_scanline,0,(size_t) (row_bytes+MaxCount));
1668 Write header, header size, size bounding box, version, and reserved.
1670 (void) ResetMagickMemory(buffer,0,PictInfoSize);
1671 (void) WriteBlob(image,PictInfoSize,buffer);
1672 (void) WriteBlobMSBShort(image,0);
1673 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top);
1674 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left);
1675 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom);
1676 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right);
1677 (void) WriteBlobMSBShort(image,PictVersion);
1678 (void) WriteBlobMSBShort(image,0x02ff); /* version #2 */
1679 (void) WriteBlobMSBShort(image,PictInfoOp);
1680 (void) WriteBlobMSBLong(image,0xFFFE0000UL);
1682 Write full size of the file, resolution, frame bounding box, and reserved.
1684 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1685 (void) WriteBlobMSBShort(image,0x0000);
1686 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1687 (void) WriteBlobMSBShort(image,0x0000);
1688 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top);
1689 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left);
1690 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom);
1691 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right);
1692 (void) WriteBlobMSBLong(image,0x00000000L);
1693 profile=GetImageProfile(image,"iptc");
1694 if (profile != (StringInfo *) NULL)
1696 (void) WriteBlobMSBShort(image,0xa1);
1697 (void) WriteBlobMSBShort(image,0x1f2);
1698 (void) WriteBlobMSBShort(image,(unsigned short)
1699 (GetStringInfoLength(profile)+4));
1700 (void) WriteBlobString(image,"8BIM");
1701 (void) WriteBlob(image,GetStringInfoLength(profile),
1702 GetStringInfoDatum(profile));
1704 profile=GetImageProfile(image,"icc");
1705 if (profile != (StringInfo *) NULL)
1707 (void) WriteBlobMSBShort(image,0xa1);
1708 (void) WriteBlobMSBShort(image,0xe0);
1709 (void) WriteBlobMSBShort(image,(unsigned short)
1710 (GetStringInfoLength(profile)+4));
1711 (void) WriteBlobMSBLong(image,0x00000000UL);
1712 (void) WriteBlob(image,GetStringInfoLength(profile),
1713 GetStringInfoDatum(profile));
1714 (void) WriteBlobMSBShort(image,0xa1);
1715 (void) WriteBlobMSBShort(image,0xe0);
1716 (void) WriteBlobMSBShort(image,4);
1717 (void) WriteBlobMSBLong(image,0x00000002UL);
1720 Write crop region opcode and crop bounding box.
1722 (void) WriteBlobMSBShort(image,PictCropRegionOp);
1723 (void) WriteBlobMSBShort(image,0xa);
1724 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top);
1725 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left);
1726 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom);
1727 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right);
1728 if (image_info->compression == JPEGCompression)
1742 jpeg_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1743 if (jpeg_image == (Image *) NULL)
1745 (void) CloseBlob(image);
1746 return(MagickFalse);
1748 jpeg_info=CloneImageInfo(image_info);
1749 (void) CopyMagickString(jpeg_info->magick,"JPEG",MaxTextExtent);
1751 blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length,
1753 jpeg_info=DestroyImageInfo(jpeg_info);
1754 if (blob == (unsigned char *) NULL)
1755 return(MagickFalse);
1756 jpeg_image=DestroyImage(jpeg_image);
1757 (void) WriteBlobMSBShort(image,PictJPEGOp);
1758 (void) WriteBlobMSBLong(image,(unsigned int) length+154);
1759 (void) WriteBlobMSBShort(image,0x0000);
1760 (void) WriteBlobMSBLong(image,0x00010000UL);
1761 (void) WriteBlobMSBLong(image,0x00000000UL);
1762 (void) WriteBlobMSBLong(image,0x00000000UL);
1763 (void) WriteBlobMSBLong(image,0x00000000UL);
1764 (void) WriteBlobMSBLong(image,0x00010000UL);
1765 (void) WriteBlobMSBLong(image,0x00000000UL);
1766 (void) WriteBlobMSBLong(image,0x00000000UL);
1767 (void) WriteBlobMSBLong(image,0x00000000UL);
1768 (void) WriteBlobMSBLong(image,0x40000000UL);
1769 (void) WriteBlobMSBLong(image,0x00000000UL);
1770 (void) WriteBlobMSBLong(image,0x00000000UL);
1771 (void) WriteBlobMSBLong(image,0x00000000UL);
1772 (void) WriteBlobMSBLong(image,0x00400000UL);
1773 (void) WriteBlobMSBShort(image,0x0000);
1774 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1775 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1776 (void) WriteBlobMSBShort(image,0x0000);
1777 (void) WriteBlobMSBShort(image,768);
1778 (void) WriteBlobMSBShort(image,0x0000);
1779 (void) WriteBlobMSBLong(image,0x00000000UL);
1780 (void) WriteBlobMSBLong(image,0x00566A70UL);
1781 (void) WriteBlobMSBLong(image,0x65670000UL);
1782 (void) WriteBlobMSBLong(image,0x00000000UL);
1783 (void) WriteBlobMSBLong(image,0x00000001UL);
1784 (void) WriteBlobMSBLong(image,0x00016170UL);
1785 (void) WriteBlobMSBLong(image,0x706C0000UL);
1786 (void) WriteBlobMSBLong(image,0x00000000UL);
1787 (void) WriteBlobMSBShort(image,768);
1788 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1789 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1790 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1791 (void) WriteBlobMSBShort(image,0x0000);
1792 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1793 (void) WriteBlobMSBLong(image,0x00000000UL);
1794 (void) WriteBlobMSBLong(image,0x87AC0001UL);
1795 (void) WriteBlobMSBLong(image,0x0B466F74UL);
1796 (void) WriteBlobMSBLong(image,0x6F202D20UL);
1797 (void) WriteBlobMSBLong(image,0x4A504547UL);
1798 (void) WriteBlobMSBLong(image,0x00000000UL);
1799 (void) WriteBlobMSBLong(image,0x00000000UL);
1800 (void) WriteBlobMSBLong(image,0x00000000UL);
1801 (void) WriteBlobMSBLong(image,0x00000000UL);
1802 (void) WriteBlobMSBLong(image,0x00000000UL);
1803 (void) WriteBlobMSBLong(image,0x0018FFFFUL);
1804 (void) WriteBlob(image,length,blob);
1805 if ((length & 0x01) != 0)
1806 (void) WriteBlobByte(image,'\0');
1807 blob=(unsigned char *) RelinquishMagickMemory(blob);
1810 Write picture opcode, row bytes, and picture bounding box, and version.
1812 if (storage_class == PseudoClass)
1813 (void) WriteBlobMSBShort(image,PictPICTOp);
1816 (void) WriteBlobMSBShort(image,PictPixmapOp);
1817 (void) WriteBlobMSBLong(image,(size_t) base_address);
1819 (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000));
1820 (void) WriteBlobMSBShort(image,(unsigned short) bounds.top);
1821 (void) WriteBlobMSBShort(image,(unsigned short) bounds.left);
1822 (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom);
1823 (void) WriteBlobMSBShort(image,(unsigned short) bounds.right);
1825 Write pack type, pack size, resolution, pixel type, and pixel size.
1827 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version);
1828 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type);
1829 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.pack_size);
1830 (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5));
1831 (void) WriteBlobMSBShort(image,0x0000);
1832 (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5));
1833 (void) WriteBlobMSBShort(image,0x0000);
1834 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type);
1835 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel);
1837 Write component count, size, plane bytes, table size, and reserved.
1839 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count);
1840 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size);
1841 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.plane_bytes);
1842 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.table);
1843 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.reserved);
1844 if (storage_class == PseudoClass)
1847 Write image colormap.
1849 (void) WriteBlobMSBLong(image,0x00000000L); /* color seed */
1850 (void) WriteBlobMSBShort(image,0L); /* color flags */
1851 (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1));
1852 for (i=0; i < (ssize_t) image->colors; i++)
1854 (void) WriteBlobMSBShort(image,(unsigned short) i);
1855 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1856 image->colormap[i].red));
1857 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1858 image->colormap[i].green));
1859 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1860 image->colormap[i].blue));
1864 Write source and destination rectangle.
1866 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top);
1867 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left);
1868 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom);
1869 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right);
1870 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top);
1871 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left);
1872 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom);
1873 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right);
1874 (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode);
1879 if ((storage_class == PseudoClass) && (image->matte == MagickFalse))
1880 for (y=0; y < (ssize_t) image->rows; y++)
1882 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1883 if (p == (const PixelPacket *) NULL)
1885 indexes=GetVirtualIndexQueue(image);
1886 for (x=0; x < (ssize_t) image->columns; x++)
1887 scanline[x]=(unsigned char) indexes[x];
1888 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
1890 if (image->previous == (Image *) NULL)
1892 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1894 if (status == MagickFalse)
1899 if (image_info->compression == JPEGCompression)
1901 (void) ResetMagickMemory(scanline,0,row_bytes);
1902 for (y=0; y < (ssize_t) image->rows; y++)
1903 count+=EncodeImage(image,scanline,(size_t)
1904 (row_bytes & 0x7FFF),packed_scanline);
1908 register unsigned char
1915 green=scanline+image->columns;
1916 blue=scanline+2*image->columns;
1917 opacity=scanline+3*image->columns;
1918 for (y=0; y < (ssize_t) image->rows; y++)
1920 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1921 if (p == (const PixelPacket *) NULL)
1924 green=scanline+image->columns;
1925 blue=scanline+2*image->columns;
1926 if (image->matte != MagickFalse)
1929 red=scanline+image->columns;
1930 green=scanline+2*image->columns;
1931 blue=scanline+3*image->columns;
1933 for (x=0; x < (ssize_t) image->columns; x++)
1935 *red++=ScaleQuantumToChar(GetRedPixelComponent(p));
1936 *green++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1937 *blue++=ScaleQuantumToChar(GetBluePixelComponent(p));
1938 if (image->matte != MagickFalse)
1939 *opacity++=ScaleQuantumToChar((Quantum)
1940 (GetAlphaPixelComponent(p)));
1943 count+=EncodeImage(image,scanline,bytes_per_line & 0x7FFF,
1945 if (image->previous == (Image *) NULL)
1947 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1949 if (status == MagickFalse)
1954 if ((count & 0x01) != 0)
1955 (void) WriteBlobByte(image,'\0');
1956 (void) WriteBlobMSBShort(image,PictEndOfPictureOp);
1957 offset=TellBlob(image);
1958 offset=SeekBlob(image,512,SEEK_SET);
1959 (void) WriteBlobMSBShort(image,(unsigned short) offset);
1960 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
1961 packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline);
1962 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1963 (void) CloseBlob(image);