2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % PPPP IIIII CCCC TTTTT %
13 % Read/Write Apple Macintosh QuickDraw/PICT Format %
20 % Copyright 1999-2009 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-private.h"
48 #include "magick/colorspace.h"
49 #include "magick/composite.h"
50 #include "magick/constitute.h"
51 #include "magick/exception.h"
52 #include "magick/exception-private.h"
53 #include "magick/image.h"
54 #include "magick/image-private.h"
55 #include "magick/list.h"
56 #include "magick/log.h"
57 #include "magick/magick.h"
58 #include "magick/memory_.h"
59 #include "magick/monitor.h"
60 #include "magick/monitor-private.h"
61 #include "magick/profile.h"
62 #include "magick/resource_.h"
63 #include "magick/quantum-private.h"
64 #include "magick/static.h"
65 #include "magick/string_.h"
66 #include "magick/module.h"
67 #include "magick/transform.h"
68 #include "magick/utility.h"
71 ImageMagick Macintosh PICT Methods.
73 #define ReadPixmap(pixmap) \
75 pixmap.version=(short) ReadBlobMSBShort(image); \
76 pixmap.pack_type=(short) ReadBlobMSBShort(image); \
77 pixmap.pack_size=ReadBlobMSBLong(image); \
78 pixmap.horizontal_resolution=1UL*ReadBlobMSBShort(image); \
79 (void) ReadBlobMSBShort(image); \
80 pixmap.vertical_resolution=1UL*ReadBlobMSBShort(image); \
81 (void) ReadBlobMSBShort(image); \
82 pixmap.pixel_type=(short) ReadBlobMSBShort(image); \
83 pixmap.bits_per_pixel=(short) ReadBlobMSBShort(image); \
84 pixmap.component_count=(short) ReadBlobMSBShort(image); \
85 pixmap.component_size=(short) ReadBlobMSBShort(image); \
86 pixmap.plane_bytes=ReadBlobMSBLong(image); \
87 pixmap.table=ReadBlobMSBLong(image); \
88 pixmap.reserved=ReadBlobMSBLong(image); \
89 if ((pixmap.bits_per_pixel <= 0) || (pixmap.bits_per_pixel > 32) || \
90 (pixmap.component_count <= 0) || (pixmap.component_count > 4) || \
91 (pixmap.component_size <= 0)) \
92 ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
95 #define ReadRectangle(image,rectangle) \
97 rectangle.top=(short) ReadBlobMSBShort(image); \
98 rectangle.left=(short) ReadBlobMSBShort(image); \
99 rectangle.bottom=(short) ReadBlobMSBShort(image); \
100 rectangle.right=(short) ReadBlobMSBShort(image); \
101 if ((rectangle.left > rectangle.right) || \
102 (rectangle.top > rectangle.bottom)) \
103 ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
106 typedef struct _PICTCode
118 typedef struct _PICTPixmap
126 horizontal_resolution,
141 typedef struct _PICTRectangle
150 static const PICTCode
153 /* 0x00 */ { "NOP", 0, "nop" },
154 /* 0x01 */ { "Clip", 0, "clip" },
155 /* 0x02 */ { "BkPat", 8, "background pattern" },
156 /* 0x03 */ { "TxFont", 2, "text font (word)" },
157 /* 0x04 */ { "TxFace", 1, "text face (byte)" },
158 /* 0x05 */ { "TxMode", 2, "text mode (word)" },
159 /* 0x06 */ { "SpExtra", 4, "space extra (fixed point)" },
160 /* 0x07 */ { "PnSize", 4, "pen size (point)" },
161 /* 0x08 */ { "PnMode", 2, "pen mode (word)" },
162 /* 0x09 */ { "PnPat", 8, "pen pattern" },
163 /* 0x0a */ { "FillPat", 8, "fill pattern" },
164 /* 0x0b */ { "OvSize", 4, "oval size (point)" },
165 /* 0x0c */ { "Origin", 4, "dh, dv (word)" },
166 /* 0x0d */ { "TxSize", 2, "text size (word)" },
167 /* 0x0e */ { "FgColor", 4, "foreground color (longword)" },
168 /* 0x0f */ { "BkColor", 4, "background color (longword)" },
169 /* 0x10 */ { "TxRatio", 8, "numerator (point), denominator (point)" },
170 /* 0x11 */ { "Version", 1, "version (byte)" },
171 /* 0x12 */ { "BkPixPat", 0, "color background pattern" },
172 /* 0x13 */ { "PnPixPat", 0, "color pen pattern" },
173 /* 0x14 */ { "FillPixPat", 0, "color fill pattern" },
174 /* 0x15 */ { "PnLocHFrac", 2, "fractional pen position" },
175 /* 0x16 */ { "ChExtra", 2, "extra for each character" },
176 /* 0x17 */ { "reserved", 0, "reserved for Apple use" },
177 /* 0x18 */ { "reserved", 0, "reserved for Apple use" },
178 /* 0x19 */ { "reserved", 0, "reserved for Apple use" },
179 /* 0x1a */ { "RGBFgCol", 6, "RGB foreColor" },
180 /* 0x1b */ { "RGBBkCol", 6, "RGB backColor" },
181 /* 0x1c */ { "HiliteMode", 0, "hilite mode flag" },
182 /* 0x1d */ { "HiliteColor", 6, "RGB hilite color" },
183 /* 0x1e */ { "DefHilite", 0, "Use default hilite color" },
184 /* 0x1f */ { "OpColor", 6, "RGB OpColor for arithmetic modes" },
185 /* 0x20 */ { "Line", 8, "pnLoc (point), newPt (point)" },
186 /* 0x21 */ { "LineFrom", 4, "newPt (point)" },
187 /* 0x22 */ { "ShortLine", 6, "pnLoc (point, dh, dv (-128 .. 127))" },
188 /* 0x23 */ { "ShortLineFrom", 2, "dh, dv (-128 .. 127)" },
189 /* 0x24 */ { "reserved", -1, "reserved for Apple use" },
190 /* 0x25 */ { "reserved", -1, "reserved for Apple use" },
191 /* 0x26 */ { "reserved", -1, "reserved for Apple use" },
192 /* 0x27 */ { "reserved", -1, "reserved for Apple use" },
193 /* 0x28 */ { "LongText", 0, "txLoc (point), count (0..255), text" },
194 /* 0x29 */ { "DHText", 0, "dh (0..255), count (0..255), text" },
195 /* 0x2a */ { "DVText", 0, "dv (0..255), count (0..255), text" },
196 /* 0x2b */ { "DHDVText", 0, "dh, dv (0..255), count (0..255), text" },
197 /* 0x2c */ { "reserved", -1, "reserved for Apple use" },
198 /* 0x2d */ { "reserved", -1, "reserved for Apple use" },
199 /* 0x2e */ { "reserved", -1, "reserved for Apple use" },
200 /* 0x2f */ { "reserved", -1, "reserved for Apple use" },
201 /* 0x30 */ { "frameRect", 8, "rect" },
202 /* 0x31 */ { "paintRect", 8, "rect" },
203 /* 0x32 */ { "eraseRect", 8, "rect" },
204 /* 0x33 */ { "invertRect", 8, "rect" },
205 /* 0x34 */ { "fillRect", 8, "rect" },
206 /* 0x35 */ { "reserved", 8, "reserved for Apple use" },
207 /* 0x36 */ { "reserved", 8, "reserved for Apple use" },
208 /* 0x37 */ { "reserved", 8, "reserved for Apple use" },
209 /* 0x38 */ { "frameSameRect", 0, "rect" },
210 /* 0x39 */ { "paintSameRect", 0, "rect" },
211 /* 0x3a */ { "eraseSameRect", 0, "rect" },
212 /* 0x3b */ { "invertSameRect", 0, "rect" },
213 /* 0x3c */ { "fillSameRect", 0, "rect" },
214 /* 0x3d */ { "reserved", 0, "reserved for Apple use" },
215 /* 0x3e */ { "reserved", 0, "reserved for Apple use" },
216 /* 0x3f */ { "reserved", 0, "reserved for Apple use" },
217 /* 0x40 */ { "frameRRect", 8, "rect" },
218 /* 0x41 */ { "paintRRect", 8, "rect" },
219 /* 0x42 */ { "eraseRRect", 8, "rect" },
220 /* 0x43 */ { "invertRRect", 8, "rect" },
221 /* 0x44 */ { "fillRRrect", 8, "rect" },
222 /* 0x45 */ { "reserved", 8, "reserved for Apple use" },
223 /* 0x46 */ { "reserved", 8, "reserved for Apple use" },
224 /* 0x47 */ { "reserved", 8, "reserved for Apple use" },
225 /* 0x48 */ { "frameSameRRect", 0, "rect" },
226 /* 0x49 */ { "paintSameRRect", 0, "rect" },
227 /* 0x4a */ { "eraseSameRRect", 0, "rect" },
228 /* 0x4b */ { "invertSameRRect", 0, "rect" },
229 /* 0x4c */ { "fillSameRRect", 0, "rect" },
230 /* 0x4d */ { "reserved", 0, "reserved for Apple use" },
231 /* 0x4e */ { "reserved", 0, "reserved for Apple use" },
232 /* 0x4f */ { "reserved", 0, "reserved for Apple use" },
233 /* 0x50 */ { "frameOval", 8, "rect" },
234 /* 0x51 */ { "paintOval", 8, "rect" },
235 /* 0x52 */ { "eraseOval", 8, "rect" },
236 /* 0x53 */ { "invertOval", 8, "rect" },
237 /* 0x54 */ { "fillOval", 8, "rect" },
238 /* 0x55 */ { "reserved", 8, "reserved for Apple use" },
239 /* 0x56 */ { "reserved", 8, "reserved for Apple use" },
240 /* 0x57 */ { "reserved", 8, "reserved for Apple use" },
241 /* 0x58 */ { "frameSameOval", 0, "rect" },
242 /* 0x59 */ { "paintSameOval", 0, "rect" },
243 /* 0x5a */ { "eraseSameOval", 0, "rect" },
244 /* 0x5b */ { "invertSameOval", 0, "rect" },
245 /* 0x5c */ { "fillSameOval", 0, "rect" },
246 /* 0x5d */ { "reserved", 0, "reserved for Apple use" },
247 /* 0x5e */ { "reserved", 0, "reserved for Apple use" },
248 /* 0x5f */ { "reserved", 0, "reserved for Apple use" },
249 /* 0x60 */ { "frameArc", 12, "rect, startAngle, arcAngle" },
250 /* 0x61 */ { "paintArc", 12, "rect, startAngle, arcAngle" },
251 /* 0x62 */ { "eraseArc", 12, "rect, startAngle, arcAngle" },
252 /* 0x63 */ { "invertArc", 12, "rect, startAngle, arcAngle" },
253 /* 0x64 */ { "fillArc", 12, "rect, startAngle, arcAngle" },
254 /* 0x65 */ { "reserved", 12, "reserved for Apple use" },
255 /* 0x66 */ { "reserved", 12, "reserved for Apple use" },
256 /* 0x67 */ { "reserved", 12, "reserved for Apple use" },
257 /* 0x68 */ { "frameSameArc", 4, "rect, startAngle, arcAngle" },
258 /* 0x69 */ { "paintSameArc", 4, "rect, startAngle, arcAngle" },
259 /* 0x6a */ { "eraseSameArc", 4, "rect, startAngle, arcAngle" },
260 /* 0x6b */ { "invertSameArc", 4, "rect, startAngle, arcAngle" },
261 /* 0x6c */ { "fillSameArc", 4, "rect, startAngle, arcAngle" },
262 /* 0x6d */ { "reserved", 4, "reserved for Apple use" },
263 /* 0x6e */ { "reserved", 4, "reserved for Apple use" },
264 /* 0x6f */ { "reserved", 4, "reserved for Apple use" },
265 /* 0x70 */ { "framePoly", 0, "poly" },
266 /* 0x71 */ { "paintPoly", 0, "poly" },
267 /* 0x72 */ { "erasePoly", 0, "poly" },
268 /* 0x73 */ { "invertPoly", 0, "poly" },
269 /* 0x74 */ { "fillPoly", 0, "poly" },
270 /* 0x75 */ { "reserved", 0, "reserved for Apple use" },
271 /* 0x76 */ { "reserved", 0, "reserved for Apple use" },
272 /* 0x77 */ { "reserved", 0, "reserved for Apple use" },
273 /* 0x78 */ { "frameSamePoly", 0, "poly (NYI)" },
274 /* 0x79 */ { "paintSamePoly", 0, "poly (NYI)" },
275 /* 0x7a */ { "eraseSamePoly", 0, "poly (NYI)" },
276 /* 0x7b */ { "invertSamePoly", 0, "poly (NYI)" },
277 /* 0x7c */ { "fillSamePoly", 0, "poly (NYI)" },
278 /* 0x7d */ { "reserved", 0, "reserved for Apple use" },
279 /* 0x7e */ { "reserved", 0, "reserved for Apple use" },
280 /* 0x7f */ { "reserved", 0, "reserved for Apple use" },
281 /* 0x80 */ { "frameRgn", 0, "region" },
282 /* 0x81 */ { "paintRgn", 0, "region" },
283 /* 0x82 */ { "eraseRgn", 0, "region" },
284 /* 0x83 */ { "invertRgn", 0, "region" },
285 /* 0x84 */ { "fillRgn", 0, "region" },
286 /* 0x85 */ { "reserved", 0, "reserved for Apple use" },
287 /* 0x86 */ { "reserved", 0, "reserved for Apple use" },
288 /* 0x87 */ { "reserved", 0, "reserved for Apple use" },
289 /* 0x88 */ { "frameSameRgn", 0, "region (NYI)" },
290 /* 0x89 */ { "paintSameRgn", 0, "region (NYI)" },
291 /* 0x8a */ { "eraseSameRgn", 0, "region (NYI)" },
292 /* 0x8b */ { "invertSameRgn", 0, "region (NYI)" },
293 /* 0x8c */ { "fillSameRgn", 0, "region (NYI)" },
294 /* 0x8d */ { "reserved", 0, "reserved for Apple use" },
295 /* 0x8e */ { "reserved", 0, "reserved for Apple use" },
296 /* 0x8f */ { "reserved", 0, "reserved for Apple use" },
297 /* 0x90 */ { "BitsRect", 0, "copybits, rect clipped" },
298 /* 0x91 */ { "BitsRgn", 0, "copybits, rgn clipped" },
299 /* 0x92 */ { "reserved", -1, "reserved for Apple use" },
300 /* 0x93 */ { "reserved", -1, "reserved for Apple use" },
301 /* 0x94 */ { "reserved", -1, "reserved for Apple use" },
302 /* 0x95 */ { "reserved", -1, "reserved for Apple use" },
303 /* 0x96 */ { "reserved", -1, "reserved for Apple use" },
304 /* 0x97 */ { "reserved", -1, "reserved for Apple use" },
305 /* 0x98 */ { "PackBitsRect", 0, "packed copybits, rect clipped" },
306 /* 0x99 */ { "PackBitsRgn", 0, "packed copybits, rgn clipped" },
307 /* 0x9a */ { "DirectBitsRect", 0, "PixMap, srcRect, dstRect, mode, PixData" },
308 /* 0x9b */ { "DirectBitsRgn", 0, "PixMap, srcRect, dstRect, mode, maskRgn, PixData" },
309 /* 0x9c */ { "reserved", -1, "reserved for Apple use" },
310 /* 0x9d */ { "reserved", -1, "reserved for Apple use" },
311 /* 0x9e */ { "reserved", -1, "reserved for Apple use" },
312 /* 0x9f */ { "reserved", -1, "reserved for Apple use" },
313 /* 0xa0 */ { "ShortComment", 2, "kind (word)" },
314 /* 0xa1 */ { "LongComment", 0, "kind (word), size (word), data" }
318 Forward declarations.
320 static MagickBooleanType
321 WritePICTImage(const ImageInfo *,Image *);
324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 % D e c o d e I m a g e %
332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 % DecodeImage decompresses an image via Macintosh pack bits decoding for
335 % Macintosh PICT images.
337 % The format of the DecodeImage method is:
339 % unsigned char *DecodeImage(Image *blob,Image *image,
340 % unsigned long bytes_per_line,const int bits_per_pixel,
341 % unsigned size_t extent)
343 % A description of each parameter follows:
345 % o image_info: the image info.
347 % o blob,image: the address of a structure of type Image.
349 % o bytes_per_line: This integer identifies the number of bytes in a
352 % o bits_per_pixel: the number of bits in a pixel.
354 % o extent: the number of pixels allocated.
358 static unsigned char *ExpandBuffer(unsigned char *pixels,
359 MagickSizeType *bytes_per_line,const unsigned int bits_per_pixel)
364 register unsigned char
373 switch (bits_per_pixel)
381 for (i=0; i < (long) *bytes_per_line; i++)
383 *q++=(*p >> 4) & 0xff;
392 for (i=0; i < (long) *bytes_per_line; i++)
394 *q++=(*p >> 6) & 0x03;
395 *q++=(*p >> 4) & 0x03;
396 *q++=(*p >> 2) & 0x03;
405 for (i=0; i < (long) *bytes_per_line; i++)
407 *q++=(*p >> 7) & 0x01;
408 *q++=(*p >> 6) & 0x01;
409 *q++=(*p >> 5) & 0x01;
410 *q++=(*p >> 4) & 0x01;
411 *q++=(*p >> 3) & 0x01;
412 *q++=(*p >> 2) & 0x01;
413 *q++=(*p >> 1) & 0x01;
426 static unsigned char *DecodeImage(Image *blob,Image *image,
427 unsigned long bytes_per_line,const unsigned int bits_per_pixel,size_t *extent)
439 register unsigned char
460 Determine pixel buffer size.
462 if (bits_per_pixel <= 8)
463 bytes_per_line&=0x7fff;
464 width=image->columns;
466 if (bits_per_pixel == 16)
472 if (bits_per_pixel == 32)
473 width*=image->matte ? 4 : 3;
474 if (bytes_per_line == 0)
475 bytes_per_line=width;
476 row_bytes=(size_t) (image->columns | 0x8000);
477 if (image->storage_class == DirectClass)
478 row_bytes=(size_t) ((4*image->columns) | 0x8000);
480 Allocate pixel and scanline buffer.
482 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,row_bytes*
484 if (pixels == (unsigned char *) NULL)
485 return((unsigned char *) NULL);
486 *extent=row_bytes*image->rows*sizeof(*pixels);
487 (void) ResetMagickMemory(pixels,0,*extent);
488 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
489 if (scanline == (unsigned char *) NULL)
490 return((unsigned char *) NULL);
491 if (bytes_per_line < 8)
494 Pixels are already uncompressed.
496 for (y=0; y < (long) image->rows; y++)
499 number_pixels=bytes_per_line;
500 count=ReadBlob(blob,(size_t) number_pixels,scanline);
501 p=ExpandBuffer(scanline,&number_pixels,bits_per_pixel);
502 if ((q+number_pixels) > (pixels+(*extent)))
504 (void) ThrowMagickException(&image->exception,GetMagickModule(),
505 CorruptImageError,"UnableToUncompressImage","`%s'",
509 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
511 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
515 Uncompress RLE pixels into uncompressed pixel buffer.
517 for (y=0; y < (long) image->rows; y++)
520 if (bytes_per_line > 200)
521 scanline_length=ReadBlobMSBShort(blob);
523 scanline_length=1UL*ReadBlobByte(blob);
524 if (scanline_length >= row_bytes)
526 (void) ThrowMagickException(&image->exception,GetMagickModule(),
527 CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
530 count=ReadBlob(blob,scanline_length,scanline);
531 for (j=0; j < (long) scanline_length; )
532 if ((scanline[j] & 0x80) == 0)
534 length=(size_t) ((scanline[j] & 0xff)+1);
535 number_pixels=length*bytes_per_pixel;
536 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
537 if ((q-pixels+number_pixels) <= *extent)
538 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
540 j+=(long) (length*bytes_per_pixel+1);
544 length=(size_t) (((scanline[j] ^ 0xff) & 0xff)+2);
545 number_pixels=bytes_per_pixel;
546 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
547 for (i=0; i < (long) length; i++)
549 if ((q-pixels+number_pixels) <= *extent)
550 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
553 j+=bytes_per_pixel+1;
556 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565 % E n c o d e I m a g e %
569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571 % EncodeImage compresses an image via Macintosh pack bits encoding
572 % for Macintosh PICT images.
574 % The format of the EncodeImage method is:
576 % size_t EncodeImage(Image *image,const unsigned char *scanline,
577 % const unsigned long bytes_per_line,unsigned char *pixels)
579 % A description of each parameter follows:
581 % o image: the address of a structure of type Image.
583 % o scanline: A pointer to an array of characters to pack.
585 % o bytes_per_line: the number of bytes in a scanline.
587 % o pixels: A pointer to an array of characters where the packed
588 % characters are stored.
591 static size_t EncodeImage(Image *image,const unsigned char *scanline,
592 const unsigned long bytes_per_line,unsigned char *pixels)
595 #define MaxPackbitsRunlength 128
602 register const unsigned char
608 register unsigned char
620 assert(image != (Image *) NULL);
621 assert(image->signature == MagickSignature);
622 if (image->debug != MagickFalse)
623 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
624 assert(scanline != (unsigned char *) NULL);
625 assert(pixels != (unsigned char *) NULL);
628 p=scanline+(bytes_per_line-1);
631 for (i=(long) bytes_per_line-1; i >= 0; i--)
638 while (runlength > 0)
640 *q++=(unsigned char) index;
643 if (count == MaxCount)
645 *q++=(unsigned char) (MaxCount-1);
652 *q++=(unsigned char) (count-1);
654 while (runlength > 0)
656 repeat_count=runlength;
657 if (repeat_count > MaxPackbitsRunlength)
658 repeat_count=MaxPackbitsRunlength;
659 *q++=(unsigned char) index;
660 *q++=(unsigned char) (257-repeat_count);
661 runlength-=repeat_count;
670 while (runlength > 0)
672 *q++=(unsigned char) index;
675 if (count == MaxCount)
677 *q++=(unsigned char) (MaxCount-1);
684 *q++=(unsigned char) (count-1);
686 while (runlength > 0)
688 repeat_count=runlength;
689 if (repeat_count > MaxPackbitsRunlength)
690 repeat_count=MaxPackbitsRunlength;
691 *q++=(unsigned char) index;
692 *q++=(unsigned char) (257-repeat_count);
693 runlength-=repeat_count;
697 *q++=(unsigned char) (count-1);
699 Write the number of and the packed length.
701 length=(size_t) (q-pixels);
702 if (bytes_per_line > 200)
704 (void) WriteBlobMSBShort(image,(unsigned short) length);
709 (void) WriteBlobByte(image,(unsigned char) length);
715 (void) WriteBlobByte(image,*q);
721 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 % IsPICT()() returns MagickTrue if the image format type, identified by the
732 % magick string, is PICT.
734 % The format of the ReadPICTImage method is:
736 % MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
738 % A description of each parameter follows:
740 % o magick: compare image format pattern against these bytes.
742 % o length: Specifies the length of the magick string.
745 static MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
749 if (memcmp(magick+522,"\000\021\002\377\014\000",6) == 0)
754 #if !defined(macintosh)
756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 % R e a d P I C T I m a g e %
764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766 % ReadPICTImage() reads an Apple Macintosh QuickDraw/PICT image file
767 % and returns it. It allocates the memory necessary for the new Image
768 % structure and returns a pointer to the new image.
770 % The format of the ReadPICTImage method is:
772 % Image *ReadPICTImage(const ImageInfo *image_info,
773 % ExceptionInfo *exception)
775 % A description of each parameter follows:
777 % o image_info: the image info.
779 % o exception: return any errors or warnings in this structure.
783 static inline unsigned long MagickMax(const unsigned long x,
784 const unsigned long y)
791 static Image *ReadPICTImage(const ImageInfo *image_info,
792 ExceptionInfo *exception)
795 geometry[MaxTextExtent];
848 assert(image_info != (const ImageInfo *) NULL);
849 assert(image_info->signature == MagickSignature);
850 if (image_info->debug != MagickFalse)
851 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
852 image_info->filename);
853 assert(exception != (ExceptionInfo *) NULL);
854 assert(exception->signature == MagickSignature);
855 image=AcquireImage(image_info);
857 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
858 if (status == MagickFalse)
860 image=DestroyImageList(image);
861 return((Image *) NULL);
866 pixmap.bits_per_pixel=0;
867 pixmap.component_count=0;
868 for (i=0; i < 512; i++)
869 (void) ReadBlobByte(image); /* skip header */
870 (void) ReadBlobMSBShort(image); /* skip picture size */
871 ReadRectangle(image,frame);
872 while ((c=ReadBlobByte(image)) == 0) ;
874 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
875 version=ReadBlobByte(image);
878 c=ReadBlobByte(image);
880 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
884 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
885 if ((frame.left < 0) || (frame.right < 0) || (frame.top < 0) ||
886 (frame.bottom < 0) || (frame.left >= frame.right) ||
887 (frame.top >= frame.bottom))
888 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
893 image->columns=1UL*(frame.right-frame.left);
894 image->rows=1UL*(frame.bottom-frame.top);
895 image->x_resolution=DefaultResolution;
896 image->y_resolution=DefaultResolution;
897 image->units=UndefinedResolution;
899 Interpret PICT opcodes.
902 for (code=0; EOFBlob(image) == MagickFalse; )
904 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
905 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
907 if ((version == 1) || ((TellBlob(image) % 2) != 0))
908 code=ReadBlobByte(image);
910 code=(int) ReadBlobMSBShort(image);
913 if (image->debug != MagickFalse)
914 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%04X:",code);
918 if (image->debug != MagickFalse)
919 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
920 " %04X %s: %s",code,codes[code].name,codes[code].description);
928 length=ReadBlobMSBShort(image);
929 if (length != 0x000a)
931 for (i=0; i < (long) (length-2); i++)
932 (void) ReadBlobByte(image);
935 ReadRectangle(image,frame);
936 if (((frame.left & 0x8000) != 0) || ((frame.top & 0x8000) != 0))
938 image->columns=1UL*(frame.right-frame.left);
939 image->rows=1UL*(frame.bottom-frame.top);
940 (void) SetImageBackgroundColor(image);
955 Skip pattern definition.
957 pattern=1L*ReadBlobMSBShort(image);
958 for (i=0; i < 8; i++)
959 (void) ReadBlobByte(image);
962 for (i=0; i < 5; i++)
963 (void) ReadBlobByte(image);
967 ThrowReaderException(CorruptImageError,"UnknownPatternType");
968 length=ReadBlobMSBShort(image);
969 ReadRectangle(image,frame);
971 image->depth=1UL*pixmap.component_size;
972 image->x_resolution=1.0*pixmap.horizontal_resolution;
973 image->y_resolution=1.0*pixmap.vertical_resolution;
974 image->units=PixelsPerInchResolution;
975 (void) ReadBlobMSBLong(image);
976 flags=1L*ReadBlobMSBShort(image);
977 length=ReadBlobMSBShort(image);
978 for (i=0; i <= (long) length; i++)
979 (void) ReadBlobMSBLong(image);
980 width=1UL*(frame.bottom-frame.top);
981 height=1UL*(frame.right-frame.left);
982 if (pixmap.bits_per_pixel <= 8)
984 if (pixmap.bits_per_pixel == 16)
990 for (i=0; i < (long) (length*height); i++)
991 (void) ReadBlobByte(image);
994 for (j=0; j < (int) height; j++)
996 for (j=0; j < (long) ReadBlobMSBShort(image); j++)
997 (void) ReadBlobByte(image);
999 for (j=0; j < (long) ReadBlobByte(image); j++)
1000 (void) ReadBlobByte(image);
1006 Initialize image background color.
1008 image->background_color.red=(Quantum)
1009 ScaleShortToQuantum(ReadBlobMSBShort(image));
1010 image->background_color.green=(Quantum)
1011 ScaleShortToQuantum(ReadBlobMSBShort(image));
1012 image->background_color.blue=(Quantum)
1013 ScaleShortToQuantum(ReadBlobMSBShort(image));
1026 Skip polygon or region.
1028 length=ReadBlobMSBShort(image);
1029 for (i=0; i < (long) (length-2); i++)
1030 (void) ReadBlobByte(image);
1047 register unsigned char
1060 Pixmap clipped by a rectangle.
1063 if ((code != 0x9a) && (code != 0x9b))
1064 bytes_per_line=1L*ReadBlobMSBShort(image);
1067 (void) ReadBlobMSBShort(image);
1068 (void) ReadBlobMSBShort(image);
1069 (void) ReadBlobMSBShort(image);
1071 ReadRectangle(image,frame);
1073 Initialize tile image.
1075 tile_image=CloneImage(image,1UL*(frame.right-frame.left),
1076 1UL*(frame.bottom-frame.top),MagickTrue,exception);
1077 if (tile_image == (Image *) NULL)
1078 return((Image *) NULL);
1079 if ((code == 0x9a) || (code == 0x9b) ||
1080 ((bytes_per_line & 0x8000) != 0))
1083 tile_image->depth=1UL*pixmap.component_size;
1084 tile_image->matte=pixmap.component_count == 4 ?
1085 MagickTrue : MagickFalse;
1086 tile_image->x_resolution=(double) pixmap.horizontal_resolution;
1087 tile_image->y_resolution=(double) pixmap.vertical_resolution;
1088 tile_image->units=PixelsPerInchResolution;
1089 if (tile_image->matte != MagickFalse)
1090 image->matte=tile_image->matte;
1092 if ((code != 0x9a) && (code != 0x9b))
1095 Initialize colormap.
1097 tile_image->colors=2;
1098 if ((bytes_per_line & 0x8000) != 0)
1100 (void) ReadBlobMSBLong(image);
1101 flags=1L*ReadBlobMSBShort(image);
1102 tile_image->colors=1UL*ReadBlobMSBShort(image)+1;
1104 status=AcquireImageColormap(tile_image,tile_image->colors);
1105 if (status == MagickFalse)
1107 tile_image=DestroyImage(tile_image);
1108 ThrowReaderException(ResourceLimitError,
1109 "MemoryAllocationFailed");
1111 if ((bytes_per_line & 0x8000) != 0)
1113 for (i=0; i < (long) tile_image->colors; i++)
1115 j=ReadBlobMSBShort(image) % tile_image->colors;
1116 if ((flags & 0x8000) != 0)
1118 tile_image->colormap[j].red=(Quantum)
1119 ScaleShortToQuantum(ReadBlobMSBShort(image));
1120 tile_image->colormap[j].green=(Quantum)
1121 ScaleShortToQuantum(ReadBlobMSBShort(image));
1122 tile_image->colormap[j].blue=(Quantum)
1123 ScaleShortToQuantum(ReadBlobMSBShort(image));
1128 for (i=0; i < (long) tile_image->colors; i++)
1130 tile_image->colormap[i].red=(Quantum) (QuantumRange-
1131 tile_image->colormap[i].red);
1132 tile_image->colormap[i].green=(Quantum) (QuantumRange-
1133 tile_image->colormap[i].green);
1134 tile_image->colormap[i].blue=(Quantum) (QuantumRange-
1135 tile_image->colormap[i].blue);
1139 ReadRectangle(image,source);
1140 ReadRectangle(image,destination);
1141 (void) ReadBlobMSBShort(image);
1142 if ((code == 0x91) || (code == 0x99) || (code == 0x9b))
1147 length=ReadBlobMSBShort(image);
1148 for (i=0; i < (long) (length-2); i++)
1149 (void) ReadBlobByte(image);
1151 if ((code != 0x9a) && (code != 0x9b) &&
1152 (bytes_per_line & 0x8000) == 0)
1153 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1,&extent);
1155 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1U*
1156 pixmap.bits_per_pixel,&extent);
1157 if (pixels == (unsigned char *) NULL)
1159 tile_image=DestroyImage(tile_image);
1160 ThrowReaderException(ResourceLimitError,
1161 "MemoryAllocationFailed");
1164 Convert PICT tile image to pixel packets.
1167 for (y=0; y < (long) tile_image->rows; y++)
1169 if (p > (pixels+extent+image->columns))
1170 ThrowReaderException(CorruptImageError,"NotEnoughPixelData");
1171 q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,
1173 if (q == (PixelPacket *) NULL)
1175 indexes=GetAuthenticIndexQueue(tile_image);
1176 for (x=0; x < (long) tile_image->columns; x++)
1178 if (tile_image->storage_class == PseudoClass)
1180 index=ConstrainColormapIndex(tile_image,*p);
1182 q->red=tile_image->colormap[(long) index].red;
1183 q->green=tile_image->colormap[(long) index].green;
1184 q->blue=tile_image->colormap[(long) index].blue;
1188 if (pixmap.bits_per_pixel == 16)
1192 q->red=ScaleCharToQuantum((unsigned char)
1194 q->green=ScaleCharToQuantum((unsigned char)
1195 (((i & 0x03) << 6) | ((j & 0xe0) >> 2)));
1196 q->blue=ScaleCharToQuantum((unsigned char)
1200 if (tile_image->matte == MagickFalse)
1202 if (p > (pixels+extent+2*image->columns))
1203 ThrowReaderException(CorruptImageError,
1204 "NotEnoughPixelData");
1205 q->red=ScaleCharToQuantum(*p);
1206 q->green=ScaleCharToQuantum(
1207 *(p+tile_image->columns));
1208 q->blue=ScaleCharToQuantum(
1209 *(p+2*tile_image->columns));
1213 if (p > (pixels+extent+3*image->columns))
1214 ThrowReaderException(CorruptImageError,
1215 "NotEnoughPixelData");
1216 q->opacity=(Quantum) (QuantumRange-
1217 ScaleCharToQuantum(*p));
1218 q->red=ScaleCharToQuantum(*(p+tile_image->columns));
1219 q->green=(Quantum) ScaleCharToQuantum(
1220 *(p+2*tile_image->columns));
1221 q->blue=ScaleCharToQuantum(
1222 *(p+3*tile_image->columns));
1228 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
1230 if ((tile_image->storage_class == DirectClass) &&
1231 (pixmap.bits_per_pixel != 16))
1233 p+=(pixmap.component_count-1)*tile_image->columns;
1237 status=SetImageProgress(image,LoadImageTag,y,tile_image->rows);
1238 if (status == MagickFalse)
1241 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1242 if (jpeg == MagickFalse)
1243 if ((code == 0x9a) || (code == 0x9b) ||
1244 ((bytes_per_line & 0x8000) != 0))
1245 (void) CompositeImage(image,CopyCompositeOp,tile_image,
1246 destination.left,destination.top);
1247 tile_image=DestroyImage(tile_image);
1261 type=ReadBlobMSBShort(image);
1262 length=ReadBlobMSBShort(image);
1265 (void) ReadBlobMSBLong(image);
1269 info=(unsigned char *) AcquireQuantumMemory(length,sizeof(*info));
1270 if (info == (unsigned char *) NULL)
1272 count=ReadBlob(image,length,info);
1279 profile=AcquireStringInfo(length);
1280 SetStringInfoDatum(profile,info);
1281 status=SetImageProfile(image,"icc",profile);
1282 profile=DestroyStringInfo(profile);
1283 if (status == MagickFalse)
1284 ThrowReaderException(ResourceLimitError,
1285 "MemoryAllocationFailed");
1292 profile=AcquireStringInfo(length);
1293 SetStringInfoDatum(profile,info);
1294 status=SetImageProfile(image,"iptc",profile);
1295 if (status == MagickFalse)
1296 ThrowReaderException(ResourceLimitError,
1297 "MemoryAllocationFailed");
1298 profile=DestroyStringInfo(profile);
1304 info=(unsigned char *) RelinquishMagickMemory(info);
1310 Skip to next op code.
1312 if (codes[code].length == -1)
1313 (void) ReadBlobMSBShort(image);
1315 for (i=0; i < (long) codes[code].length; i++)
1316 (void) ReadBlobByte(image);
1325 for (i=0; i < 24; i++)
1326 (void) ReadBlobByte(image);
1329 if (((code >= 0xb0) && (code <= 0xcf)) ||
1330 ((code >= 0x8000) && (code <= 0x80ff)))
1350 read_info=CloneImageInfo(image_info);
1351 SetImageInfoBlob(read_info,(void *) NULL,0);
1353 unique_file=AcquireUniqueFileResource(read_info->filename);
1354 if (unique_file != -1)
1355 file=OpenMagickStream(read_info->filename,"wb");
1356 if ((unique_file == -1) || (file == (FILE *) NULL))
1358 (void) CopyMagickString(image->filename,read_info->filename,
1360 ThrowFileException(exception,FileOpenError,
1361 "UnableToCreateTemporaryFile",image->filename);
1362 image=DestroyImageList(image);
1363 return((Image *) NULL);
1365 length=ReadBlobMSBLong(image);
1366 for (i=0; i < 6; i++)
1367 (void) ReadBlobMSBLong(image);
1368 ReadRectangle(image,frame);
1369 for (i=0; i < 122; i++)
1370 (void) ReadBlobByte(image);
1371 for (i=0; i < (long) (length-154); i++)
1373 c=ReadBlobByte(image);
1374 (void) fputc(c,file);
1376 (void) fclose(file);
1377 tile_image=ReadImage(read_info,exception);
1378 (void) RelinquishUniqueFileResource(read_info->filename);
1379 read_info=DestroyImageInfo(read_info);
1380 if (tile_image == (Image *) NULL)
1382 (void) FormatMagickString(geometry,MaxTextExtent,"%lux%lu",
1383 MagickMax(image->columns,tile_image->columns),
1384 MagickMax(image->rows,tile_image->rows));
1385 (void) TransformImage(&image,(char *) NULL,geometry);
1386 if (image->colorspace != RGBColorspace)
1387 (void) TransformImageColorspace(image,tile_image->colorspace);
1388 (void) CompositeImage(image,CopyCompositeOp,tile_image,frame.left,
1390 image->compression=tile_image->compression;
1391 tile_image=DestroyImage(tile_image);
1394 if ((code == 0xff) || (code == 0xffff))
1396 if (((code >= 0xd0) && (code <= 0xfe)) ||
1397 ((code >= 0x8100) && (code <= 0xffff)))
1402 length=ReadBlobMSBShort(image);
1403 for (i=0; i < (long) length; i++)
1404 (void) ReadBlobByte(image);
1407 if ((code >= 0x100) && (code <= 0x7fff))
1412 length=(size_t) ((code >> 7) & 0xff);
1413 for (i=0; i < (long) length; i++)
1414 (void) ReadBlobByte(image);
1418 (void) CloseBlob(image);
1419 return(GetFirstImageInList(image));
1424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1428 % R e g i s t e r P I C T I m a g e %
1432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1434 % RegisterPICTImage() adds attributes for the PICT image format to
1435 % the list of supported formats. The attributes include the image format
1436 % tag, a method to read and/or write the format, whether the format
1437 % supports the saving of more than one frame to the same file or blob,
1438 % whether the format supports native in-memory I/O, and a brief
1439 % description of the format.
1441 % The format of the RegisterPICTImage method is:
1443 % unsigned long RegisterPICTImage(void)
1446 ModuleExport unsigned long RegisterPICTImage(void)
1451 entry=SetMagickInfo("PCT");
1452 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1453 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1454 entry->adjoin=MagickFalse;
1455 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1456 entry->magick=(IsImageFormatHandler *) IsPICT;
1457 entry->module=ConstantString("PICT");
1458 (void) RegisterMagickInfo(entry);
1459 entry=SetMagickInfo("PICT");
1460 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1461 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1462 entry->adjoin=MagickFalse;
1463 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1464 entry->magick=(IsImageFormatHandler *) IsPICT;
1465 entry->module=ConstantString("PICT");
1466 (void) RegisterMagickInfo(entry);
1467 return(MagickImageCoderSignature);
1471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1475 % U n r e g i s t e r P I C T I m a g e %
1479 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1481 % UnregisterPICTImage() removes format registrations made by the
1482 % PICT module from the list of supported formats.
1484 % The format of the UnregisterPICTImage method is:
1486 % UnregisterPICTImage(void)
1489 ModuleExport void UnregisterPICTImage(void)
1491 (void) UnregisterMagickInfo("PCT");
1492 (void) UnregisterMagickInfo("PICT");
1496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1500 % W r i t e P I C T I m a g e %
1504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1506 % WritePICTImage() writes an image to a file in the Apple Macintosh
1507 % QuickDraw/PICT image format.
1509 % The format of the WritePICTImage method is:
1511 % MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1514 % A description of each parameter follows.
1516 % o image_info: the image info.
1518 % o image: The image.
1521 static MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1524 #define MaxCount 128
1525 #define PictCropRegionOp 0x01
1526 #define PictEndOfPictureOp 0xff
1527 #define PictJPEGOp 0x8200
1528 #define PictInfoOp 0x0C00
1529 #define PictInfoSize 512
1530 #define PictPixmapOp 0x9A
1531 #define PictPICTOp 0x98
1532 #define PictVersion 0x11
1556 destination_rectangle,
1561 register const IndexPacket
1564 register const PixelPacket
1589 Open output image file.
1591 assert(image_info != (const ImageInfo *) NULL);
1592 assert(image_info->signature == MagickSignature);
1593 assert(image != (Image *) NULL);
1594 assert(image->signature == MagickSignature);
1595 if (image->debug != MagickFalse)
1596 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1597 if ((image->columns > 65535L) || (image->rows > 65535L))
1598 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1599 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1600 if (status == MagickFalse)
1602 if (image->colorspace != RGBColorspace)
1603 (void) TransformImageColorspace(image,RGBColorspace);
1605 Initialize image info.
1607 size_rectangle.top=0;
1608 size_rectangle.left=0;
1609 size_rectangle.bottom=(short) image->rows;
1610 size_rectangle.right=(short) image->columns;
1611 frame_rectangle=size_rectangle;
1612 crop_rectangle=size_rectangle;
1613 source_rectangle=size_rectangle;
1614 destination_rectangle=size_rectangle;
1616 row_bytes=(unsigned short) (image->columns | 0x8000);
1619 bounds.bottom=(short) image->rows;
1620 bounds.right=(short) image->columns;
1624 pixmap.pixel_type=0;
1625 pixmap.bits_per_pixel=8;
1626 pixmap.component_count=1;
1627 pixmap.component_size=8;
1628 pixmap.plane_bytes=0;
1632 x_resolution=image->x_resolution != 0.0 ? image->x_resolution :
1634 y_resolution=image->y_resolution != 0.0 ? image->y_resolution :
1636 storage_class=image->storage_class;
1637 if (image_info->compression == JPEGCompression)
1638 storage_class=DirectClass;
1639 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1641 pixmap.component_count=image->matte ? 4 : 3;
1642 pixmap.pixel_type=16;
1643 pixmap.bits_per_pixel=32;
1644 pixmap.pack_type=0x04;
1646 row_bytes=(unsigned short) ((4*image->columns) | 0x8000);
1651 bytes_per_line=image->columns;
1652 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1653 bytes_per_line*=image->matte ? 4 : 3;
1654 buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer));
1655 packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t)
1656 (row_bytes+MaxCount),sizeof(*packed_scanline));
1657 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
1658 if ((buffer == (unsigned char *) NULL) ||
1659 (packed_scanline == (unsigned char *) NULL) ||
1660 (scanline == (unsigned char *) NULL))
1661 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1662 (void) ResetMagickMemory(scanline,0,row_bytes);
1663 (void) ResetMagickMemory(packed_scanline,0,(size_t) (row_bytes+MaxCount));
1665 Write header, header size, size bounding box, version, and reserved.
1667 (void) ResetMagickMemory(buffer,0,PictInfoSize);
1668 (void) WriteBlob(image,PictInfoSize,buffer);
1669 (void) WriteBlobMSBShort(image,0);
1670 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top);
1671 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left);
1672 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom);
1673 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right);
1674 (void) WriteBlobMSBShort(image,PictVersion);
1675 (void) WriteBlobMSBShort(image,0x02ff); /* version #2 */
1676 (void) WriteBlobMSBShort(image,PictInfoOp);
1677 (void) WriteBlobMSBLong(image,0xFFFE0000UL);
1679 Write full size of the file, resolution, frame bounding box, and reserved.
1681 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1682 (void) WriteBlobMSBShort(image,0x0000);
1683 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1684 (void) WriteBlobMSBShort(image,0x0000);
1685 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top);
1686 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left);
1687 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom);
1688 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right);
1689 (void) WriteBlobMSBLong(image,0x00000000L);
1690 profile=GetImageProfile(image,"iptc");
1691 if (profile != (StringInfo *) NULL)
1693 (void) WriteBlobMSBShort(image,0xa1);
1694 (void) WriteBlobMSBShort(image,0x1f2);
1695 (void) WriteBlobMSBShort(image,(unsigned short) (GetStringInfoLength(profile)+4));
1696 (void) WriteBlobString(image,"8BIM");
1697 (void) WriteBlob(image,GetStringInfoLength(profile),GetStringInfoDatum(profile));
1699 profile=GetImageProfile(image,"icc");
1700 if (profile != (StringInfo *) NULL)
1702 (void) WriteBlobMSBShort(image,0xa1);
1703 (void) WriteBlobMSBShort(image,0xe0);
1704 (void) WriteBlobMSBShort(image,(unsigned short) (GetStringInfoLength(profile)+4));
1705 (void) WriteBlobMSBLong(image,0x00000000UL);
1706 (void) WriteBlob(image,GetStringInfoLength(profile),GetStringInfoDatum(profile));
1707 (void) WriteBlobMSBShort(image,0xa1);
1708 (void) WriteBlobMSBShort(image,0xe0);
1709 (void) WriteBlobMSBShort(image,4);
1710 (void) WriteBlobMSBLong(image,0x00000002UL);
1713 Write crop region opcode and crop bounding box.
1715 (void) WriteBlobMSBShort(image,PictCropRegionOp);
1716 (void) WriteBlobMSBShort(image,0xa);
1717 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top);
1718 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left);
1719 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom);
1720 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right);
1721 if (image_info->compression == JPEGCompression)
1735 jpeg_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1736 if (jpeg_image == (Image *) NULL)
1738 (void) CloseBlob(image);
1739 return(MagickFalse);
1741 jpeg_info=CloneImageInfo(image_info);
1742 (void) CopyMagickString(jpeg_info->magick,"JPEG",MaxTextExtent);
1744 blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length,
1746 jpeg_info=DestroyImageInfo(jpeg_info);
1747 if (blob == (unsigned char *) NULL)
1748 return(MagickFalse);
1749 jpeg_image=DestroyImage(jpeg_image);
1750 (void) WriteBlobMSBShort(image,PictJPEGOp);
1751 (void) WriteBlobMSBLong(image,(unsigned long) length+154);
1752 (void) WriteBlobMSBShort(image,0x0000);
1753 (void) WriteBlobMSBLong(image,0x00010000UL);
1754 (void) WriteBlobMSBLong(image,0x00000000UL);
1755 (void) WriteBlobMSBLong(image,0x00000000UL);
1756 (void) WriteBlobMSBLong(image,0x00000000UL);
1757 (void) WriteBlobMSBLong(image,0x00010000UL);
1758 (void) WriteBlobMSBLong(image,0x00000000UL);
1759 (void) WriteBlobMSBLong(image,0x00000000UL);
1760 (void) WriteBlobMSBLong(image,0x00000000UL);
1761 (void) WriteBlobMSBLong(image,0x40000000UL);
1762 (void) WriteBlobMSBLong(image,0x00000000UL);
1763 (void) WriteBlobMSBLong(image,0x00000000UL);
1764 (void) WriteBlobMSBLong(image,0x00000000UL);
1765 (void) WriteBlobMSBLong(image,0x00400000UL);
1766 (void) WriteBlobMSBShort(image,0x0000);
1767 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1768 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1769 (void) WriteBlobMSBShort(image,0x0000);
1770 (void) WriteBlobMSBShort(image,768);
1771 (void) WriteBlobMSBShort(image,0x0000);
1772 (void) WriteBlobMSBLong(image,0x00000000UL);
1773 (void) WriteBlobMSBLong(image,0x00566A70UL);
1774 (void) WriteBlobMSBLong(image,0x65670000UL);
1775 (void) WriteBlobMSBLong(image,0x00000000UL);
1776 (void) WriteBlobMSBLong(image,0x00000001UL);
1777 (void) WriteBlobMSBLong(image,0x00016170UL);
1778 (void) WriteBlobMSBLong(image,0x706C0000UL);
1779 (void) WriteBlobMSBLong(image,0x00000000UL);
1780 (void) WriteBlobMSBShort(image,768);
1781 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1782 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1783 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1784 (void) WriteBlobMSBShort(image,0x0000);
1785 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1786 (void) WriteBlobMSBLong(image,0x00000000UL);
1787 (void) WriteBlobMSBLong(image,0x87AC0001UL);
1788 (void) WriteBlobMSBLong(image,0x0B466F74UL);
1789 (void) WriteBlobMSBLong(image,0x6F202D20UL);
1790 (void) WriteBlobMSBLong(image,0x4A504547UL);
1791 (void) WriteBlobMSBLong(image,0x00000000UL);
1792 (void) WriteBlobMSBLong(image,0x00000000UL);
1793 (void) WriteBlobMSBLong(image,0x00000000UL);
1794 (void) WriteBlobMSBLong(image,0x00000000UL);
1795 (void) WriteBlobMSBLong(image,0x00000000UL);
1796 (void) WriteBlobMSBLong(image,0x0018FFFFUL);
1797 (void) WriteBlob(image,length,blob);
1798 if ((length & 0x01) != 0)
1799 (void) WriteBlobByte(image,'\0');
1800 blob=(unsigned char *) RelinquishMagickMemory(blob);
1803 Write picture opcode, row bytes, and picture bounding box, and version.
1805 if (storage_class == PseudoClass)
1806 (void) WriteBlobMSBShort(image,PictPICTOp);
1809 (void) WriteBlobMSBShort(image,PictPixmapOp);
1810 (void) WriteBlobMSBLong(image,(unsigned long) base_address);
1812 (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000));
1813 (void) WriteBlobMSBShort(image,(unsigned short) bounds.top);
1814 (void) WriteBlobMSBShort(image,(unsigned short) bounds.left);
1815 (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom);
1816 (void) WriteBlobMSBShort(image,(unsigned short) bounds.right);
1818 Write pack type, pack size, resolution, pixel type, and pixel size.
1820 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version);
1821 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type);
1822 (void) WriteBlobMSBLong(image,pixmap.pack_size);
1823 (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5));
1824 (void) WriteBlobMSBShort(image,0x0000);
1825 (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5));
1826 (void) WriteBlobMSBShort(image,0x0000);
1827 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type);
1828 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel);
1830 Write component count, size, plane bytes, table size, and reserved.
1832 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count);
1833 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size);
1834 (void) WriteBlobMSBLong(image,(unsigned long) pixmap.plane_bytes);
1835 (void) WriteBlobMSBLong(image,(unsigned long) pixmap.table);
1836 (void) WriteBlobMSBLong(image,(unsigned long) pixmap.reserved);
1837 if (storage_class == PseudoClass)
1840 Write image colormap.
1842 (void) WriteBlobMSBLong(image,0x00000000L); /* color seed */
1843 (void) WriteBlobMSBShort(image,0L); /* color flags */
1844 (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1));
1845 for (i=0; i < (long) image->colors; i++)
1847 (void) WriteBlobMSBShort(image,(unsigned short) i);
1848 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1849 image->colormap[i].red));
1850 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1851 image->colormap[i].green));
1852 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1853 image->colormap[i].blue));
1857 Write source and destination rectangle.
1859 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top);
1860 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left);
1861 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom);
1862 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right);
1863 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top);
1864 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left);
1865 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom);
1866 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right);
1867 (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode);
1872 if ((storage_class == PseudoClass) && (image->matte == MagickFalse))
1873 for (y=0; y < (long) image->rows; y++)
1875 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1876 if (p == (const PixelPacket *) NULL)
1878 indexes=GetVirtualIndexQueue(image);
1879 for (x=0; x < (long) image->columns; x++)
1880 scanline[x]=(unsigned char) indexes[x];
1881 count+=EncodeImage(image,scanline,(unsigned long) (row_bytes & 0x7FFF),
1883 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1884 (QuantumTick(y,image->rows) != MagickFalse))
1886 status=image->progress_monitor(SaveImageTag,y,image->rows,
1887 image->client_data);
1888 if (status == MagickFalse)
1893 if (image_info->compression == JPEGCompression)
1895 (void) ResetMagickMemory(scanline,0,row_bytes);
1896 for (y=0; y < (long) image->rows; y++)
1897 count+=EncodeImage(image,scanline,(unsigned long)
1898 (row_bytes & 0x7FFF),packed_scanline);
1902 register unsigned char
1909 green=scanline+image->columns;
1910 blue=scanline+2*image->columns;
1911 opacity=scanline+3*image->columns;
1912 for (y=0; y < (long) image->rows; y++)
1914 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1915 if (p == (const PixelPacket *) NULL)
1918 green=scanline+image->columns;
1919 blue=scanline+2*image->columns;
1920 if (image->matte != MagickFalse)
1923 red=scanline+image->columns;
1924 green=scanline+2*image->columns;
1925 blue=scanline+3*image->columns;
1927 for (x=0; x < (long) image->columns; x++)
1929 *red++=ScaleQuantumToChar(p->red);
1930 *green++=ScaleQuantumToChar(p->green);
1931 *blue++=ScaleQuantumToChar(p->blue);
1932 if (image->matte != MagickFalse)
1933 *opacity++=ScaleQuantumToChar((Quantum)
1934 (QuantumRange-p->opacity));
1937 count+=EncodeImage(image,scanline,bytes_per_line & 0x7FFF,
1939 if ((image->progress_monitor != (MagickProgressMonitor) NULL) &&
1940 (QuantumTick(y,image->rows) != MagickFalse))
1942 status=image->progress_monitor(SaveImageTag,y,image->rows,
1943 image->client_data);
1944 if (status == MagickFalse)
1949 if ((count & 0x01) != 0)
1950 (void) WriteBlobByte(image,'\0');
1951 (void) WriteBlobMSBShort(image,PictEndOfPictureOp);
1952 offset=TellBlob(image);
1953 offset=SeekBlob(image,512,SEEK_SET);
1954 (void) WriteBlobMSBShort(image,(unsigned short) offset);
1955 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
1956 packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline);
1957 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1958 (void) CloseBlob(image);