2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % PPPP IIIII CCCC TTTTT %
13 % Read/Write Apple Macintosh QuickDraw/PICT Format %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/color-private.h"
47 #include "MagickCore/colormap.h"
48 #include "MagickCore/colormap-private.h"
49 #include "MagickCore/colorspace.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/composite.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/image.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/log.h"
59 #include "MagickCore/magick.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/monitor.h"
62 #include "MagickCore/monitor-private.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/profile.h"
65 #include "MagickCore/resource_.h"
66 #include "MagickCore/quantum-private.h"
67 #include "MagickCore/static.h"
68 #include "MagickCore/string_.h"
69 #include "MagickCore/module.h"
70 #include "MagickCore/transform.h"
71 #include "MagickCore/utility.h"
74 ImageMagick Macintosh PICT Methods.
76 #define ReadPixmap(pixmap) \
78 pixmap.version=(short) ReadBlobMSBShort(image); \
79 pixmap.pack_type=(short) ReadBlobMSBShort(image); \
80 pixmap.pack_size=ReadBlobMSBLong(image); \
81 pixmap.horizontal_resolution=1UL*ReadBlobMSBShort(image); \
82 (void) ReadBlobMSBShort(image); \
83 pixmap.vertical_resolution=1UL*ReadBlobMSBShort(image); \
84 (void) ReadBlobMSBShort(image); \
85 pixmap.pixel_type=(short) ReadBlobMSBShort(image); \
86 pixmap.bits_per_pixel=(short) ReadBlobMSBShort(image); \
87 pixmap.component_count=(short) ReadBlobMSBShort(image); \
88 pixmap.component_size=(short) ReadBlobMSBShort(image); \
89 pixmap.plane_bytes=ReadBlobMSBLong(image); \
90 pixmap.table=ReadBlobMSBLong(image); \
91 pixmap.reserved=ReadBlobMSBLong(image); \
92 if ((pixmap.bits_per_pixel <= 0) || (pixmap.bits_per_pixel > 32) || \
93 (pixmap.component_count <= 0) || (pixmap.component_count > 4) || \
94 (pixmap.component_size <= 0)) \
95 ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
98 #define ReadRectangle(image,rectangle) \
100 rectangle.top=(short) ReadBlobMSBShort(image); \
101 rectangle.left=(short) ReadBlobMSBShort(image); \
102 rectangle.bottom=(short) ReadBlobMSBShort(image); \
103 rectangle.right=(short) ReadBlobMSBShort(image); \
104 if ((rectangle.left > rectangle.right) || \
105 (rectangle.top > rectangle.bottom)) \
106 ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
109 typedef struct _PICTCode
121 typedef struct _PICTPixmap
129 horizontal_resolution,
144 typedef struct _PICTRectangle
153 static const PICTCode
156 /* 0x00 */ { "NOP", 0, "nop" },
157 /* 0x01 */ { "Clip", 0, "clip" },
158 /* 0x02 */ { "BkPat", 8, "background pattern" },
159 /* 0x03 */ { "TxFont", 2, "text font (word)" },
160 /* 0x04 */ { "TxFace", 1, "text face (byte)" },
161 /* 0x05 */ { "TxMode", 2, "text mode (word)" },
162 /* 0x06 */ { "SpExtra", 4, "space extra (fixed point)" },
163 /* 0x07 */ { "PnSize", 4, "pen size (point)" },
164 /* 0x08 */ { "PnMode", 2, "pen mode (word)" },
165 /* 0x09 */ { "PnPat", 8, "pen pattern" },
166 /* 0x0a */ { "FillPat", 8, "fill pattern" },
167 /* 0x0b */ { "OvSize", 4, "oval size (point)" },
168 /* 0x0c */ { "Origin", 4, "dh, dv (word)" },
169 /* 0x0d */ { "TxSize", 2, "text size (word)" },
170 /* 0x0e */ { "FgColor", 4, "foreground color (ssize_tword)" },
171 /* 0x0f */ { "BkColor", 4, "background color (ssize_tword)" },
172 /* 0x10 */ { "TxRatio", 8, "numerator (point), denominator (point)" },
173 /* 0x11 */ { "Version", 1, "version (byte)" },
174 /* 0x12 */ { "BkPixPat", 0, "color background pattern" },
175 /* 0x13 */ { "PnPixPat", 0, "color pen pattern" },
176 /* 0x14 */ { "FillPixPat", 0, "color fill pattern" },
177 /* 0x15 */ { "PnLocHFrac", 2, "fractional pen position" },
178 /* 0x16 */ { "ChExtra", 2, "extra for each character" },
179 /* 0x17 */ { "reserved", 0, "reserved for Apple use" },
180 /* 0x18 */ { "reserved", 0, "reserved for Apple use" },
181 /* 0x19 */ { "reserved", 0, "reserved for Apple use" },
182 /* 0x1a */ { "RGBFgCol", 6, "RGB foreColor" },
183 /* 0x1b */ { "RGBBkCol", 6, "RGB backColor" },
184 /* 0x1c */ { "HiliteMode", 0, "hilite mode flag" },
185 /* 0x1d */ { "HiliteColor", 6, "RGB hilite color" },
186 /* 0x1e */ { "DefHilite", 0, "Use default hilite color" },
187 /* 0x1f */ { "OpColor", 6, "RGB OpColor for arithmetic modes" },
188 /* 0x20 */ { "Line", 8, "pnLoc (point), newPt (point)" },
189 /* 0x21 */ { "LineFrom", 4, "newPt (point)" },
190 /* 0x22 */ { "ShortLine", 6, "pnLoc (point, dh, dv (-128 .. 127))" },
191 /* 0x23 */ { "ShortLineFrom", 2, "dh, dv (-128 .. 127)" },
192 /* 0x24 */ { "reserved", -1, "reserved for Apple use" },
193 /* 0x25 */ { "reserved", -1, "reserved for Apple use" },
194 /* 0x26 */ { "reserved", -1, "reserved for Apple use" },
195 /* 0x27 */ { "reserved", -1, "reserved for Apple use" },
196 /* 0x28 */ { "LongText", 0, "txLoc (point), count (0..255), text" },
197 /* 0x29 */ { "DHText", 0, "dh (0..255), count (0..255), text" },
198 /* 0x2a */ { "DVText", 0, "dv (0..255), count (0..255), text" },
199 /* 0x2b */ { "DHDVText", 0, "dh, dv (0..255), count (0..255), text" },
200 /* 0x2c */ { "reserved", -1, "reserved for Apple use" },
201 /* 0x2d */ { "reserved", -1, "reserved for Apple use" },
202 /* 0x2e */ { "reserved", -1, "reserved for Apple use" },
203 /* 0x2f */ { "reserved", -1, "reserved for Apple use" },
204 /* 0x30 */ { "frameRect", 8, "rect" },
205 /* 0x31 */ { "paintRect", 8, "rect" },
206 /* 0x32 */ { "eraseRect", 8, "rect" },
207 /* 0x33 */ { "invertRect", 8, "rect" },
208 /* 0x34 */ { "fillRect", 8, "rect" },
209 /* 0x35 */ { "reserved", 8, "reserved for Apple use" },
210 /* 0x36 */ { "reserved", 8, "reserved for Apple use" },
211 /* 0x37 */ { "reserved", 8, "reserved for Apple use" },
212 /* 0x38 */ { "frameSameRect", 0, "rect" },
213 /* 0x39 */ { "paintSameRect", 0, "rect" },
214 /* 0x3a */ { "eraseSameRect", 0, "rect" },
215 /* 0x3b */ { "invertSameRect", 0, "rect" },
216 /* 0x3c */ { "fillSameRect", 0, "rect" },
217 /* 0x3d */ { "reserved", 0, "reserved for Apple use" },
218 /* 0x3e */ { "reserved", 0, "reserved for Apple use" },
219 /* 0x3f */ { "reserved", 0, "reserved for Apple use" },
220 /* 0x40 */ { "frameRRect", 8, "rect" },
221 /* 0x41 */ { "paintRRect", 8, "rect" },
222 /* 0x42 */ { "eraseRRect", 8, "rect" },
223 /* 0x43 */ { "invertRRect", 8, "rect" },
224 /* 0x44 */ { "fillRRrect", 8, "rect" },
225 /* 0x45 */ { "reserved", 8, "reserved for Apple use" },
226 /* 0x46 */ { "reserved", 8, "reserved for Apple use" },
227 /* 0x47 */ { "reserved", 8, "reserved for Apple use" },
228 /* 0x48 */ { "frameSameRRect", 0, "rect" },
229 /* 0x49 */ { "paintSameRRect", 0, "rect" },
230 /* 0x4a */ { "eraseSameRRect", 0, "rect" },
231 /* 0x4b */ { "invertSameRRect", 0, "rect" },
232 /* 0x4c */ { "fillSameRRect", 0, "rect" },
233 /* 0x4d */ { "reserved", 0, "reserved for Apple use" },
234 /* 0x4e */ { "reserved", 0, "reserved for Apple use" },
235 /* 0x4f */ { "reserved", 0, "reserved for Apple use" },
236 /* 0x50 */ { "frameOval", 8, "rect" },
237 /* 0x51 */ { "paintOval", 8, "rect" },
238 /* 0x52 */ { "eraseOval", 8, "rect" },
239 /* 0x53 */ { "invertOval", 8, "rect" },
240 /* 0x54 */ { "fillOval", 8, "rect" },
241 /* 0x55 */ { "reserved", 8, "reserved for Apple use" },
242 /* 0x56 */ { "reserved", 8, "reserved for Apple use" },
243 /* 0x57 */ { "reserved", 8, "reserved for Apple use" },
244 /* 0x58 */ { "frameSameOval", 0, "rect" },
245 /* 0x59 */ { "paintSameOval", 0, "rect" },
246 /* 0x5a */ { "eraseSameOval", 0, "rect" },
247 /* 0x5b */ { "invertSameOval", 0, "rect" },
248 /* 0x5c */ { "fillSameOval", 0, "rect" },
249 /* 0x5d */ { "reserved", 0, "reserved for Apple use" },
250 /* 0x5e */ { "reserved", 0, "reserved for Apple use" },
251 /* 0x5f */ { "reserved", 0, "reserved for Apple use" },
252 /* 0x60 */ { "frameArc", 12, "rect, startAngle, arcAngle" },
253 /* 0x61 */ { "paintArc", 12, "rect, startAngle, arcAngle" },
254 /* 0x62 */ { "eraseArc", 12, "rect, startAngle, arcAngle" },
255 /* 0x63 */ { "invertArc", 12, "rect, startAngle, arcAngle" },
256 /* 0x64 */ { "fillArc", 12, "rect, startAngle, arcAngle" },
257 /* 0x65 */ { "reserved", 12, "reserved for Apple use" },
258 /* 0x66 */ { "reserved", 12, "reserved for Apple use" },
259 /* 0x67 */ { "reserved", 12, "reserved for Apple use" },
260 /* 0x68 */ { "frameSameArc", 4, "rect, startAngle, arcAngle" },
261 /* 0x69 */ { "paintSameArc", 4, "rect, startAngle, arcAngle" },
262 /* 0x6a */ { "eraseSameArc", 4, "rect, startAngle, arcAngle" },
263 /* 0x6b */ { "invertSameArc", 4, "rect, startAngle, arcAngle" },
264 /* 0x6c */ { "fillSameArc", 4, "rect, startAngle, arcAngle" },
265 /* 0x6d */ { "reserved", 4, "reserved for Apple use" },
266 /* 0x6e */ { "reserved", 4, "reserved for Apple use" },
267 /* 0x6f */ { "reserved", 4, "reserved for Apple use" },
268 /* 0x70 */ { "framePoly", 0, "poly" },
269 /* 0x71 */ { "paintPoly", 0, "poly" },
270 /* 0x72 */ { "erasePoly", 0, "poly" },
271 /* 0x73 */ { "invertPoly", 0, "poly" },
272 /* 0x74 */ { "fillPoly", 0, "poly" },
273 /* 0x75 */ { "reserved", 0, "reserved for Apple use" },
274 /* 0x76 */ { "reserved", 0, "reserved for Apple use" },
275 /* 0x77 */ { "reserved", 0, "reserved for Apple use" },
276 /* 0x78 */ { "frameSamePoly", 0, "poly (NYI)" },
277 /* 0x79 */ { "paintSamePoly", 0, "poly (NYI)" },
278 /* 0x7a */ { "eraseSamePoly", 0, "poly (NYI)" },
279 /* 0x7b */ { "invertSamePoly", 0, "poly (NYI)" },
280 /* 0x7c */ { "fillSamePoly", 0, "poly (NYI)" },
281 /* 0x7d */ { "reserved", 0, "reserved for Apple use" },
282 /* 0x7e */ { "reserved", 0, "reserved for Apple use" },
283 /* 0x7f */ { "reserved", 0, "reserved for Apple use" },
284 /* 0x80 */ { "frameRgn", 0, "region" },
285 /* 0x81 */ { "paintRgn", 0, "region" },
286 /* 0x82 */ { "eraseRgn", 0, "region" },
287 /* 0x83 */ { "invertRgn", 0, "region" },
288 /* 0x84 */ { "fillRgn", 0, "region" },
289 /* 0x85 */ { "reserved", 0, "reserved for Apple use" },
290 /* 0x86 */ { "reserved", 0, "reserved for Apple use" },
291 /* 0x87 */ { "reserved", 0, "reserved for Apple use" },
292 /* 0x88 */ { "frameSameRgn", 0, "region (NYI)" },
293 /* 0x89 */ { "paintSameRgn", 0, "region (NYI)" },
294 /* 0x8a */ { "eraseSameRgn", 0, "region (NYI)" },
295 /* 0x8b */ { "invertSameRgn", 0, "region (NYI)" },
296 /* 0x8c */ { "fillSameRgn", 0, "region (NYI)" },
297 /* 0x8d */ { "reserved", 0, "reserved for Apple use" },
298 /* 0x8e */ { "reserved", 0, "reserved for Apple use" },
299 /* 0x8f */ { "reserved", 0, "reserved for Apple use" },
300 /* 0x90 */ { "BitsRect", 0, "copybits, rect clipped" },
301 /* 0x91 */ { "BitsRgn", 0, "copybits, rgn clipped" },
302 /* 0x92 */ { "reserved", -1, "reserved for Apple use" },
303 /* 0x93 */ { "reserved", -1, "reserved for Apple use" },
304 /* 0x94 */ { "reserved", -1, "reserved for Apple use" },
305 /* 0x95 */ { "reserved", -1, "reserved for Apple use" },
306 /* 0x96 */ { "reserved", -1, "reserved for Apple use" },
307 /* 0x97 */ { "reserved", -1, "reserved for Apple use" },
308 /* 0x98 */ { "PackBitsRect", 0, "packed copybits, rect clipped" },
309 /* 0x99 */ { "PackBitsRgn", 0, "packed copybits, rgn clipped" },
310 /* 0x9a */ { "DirectBitsRect", 0, "PixMap, srcRect, dstRect, mode, PixData" },
311 /* 0x9b */ { "DirectBitsRgn", 0, "PixMap, srcRect, dstRect, mode, maskRgn, PixData" },
312 /* 0x9c */ { "reserved", -1, "reserved for Apple use" },
313 /* 0x9d */ { "reserved", -1, "reserved for Apple use" },
314 /* 0x9e */ { "reserved", -1, "reserved for Apple use" },
315 /* 0x9f */ { "reserved", -1, "reserved for Apple use" },
316 /* 0xa0 */ { "ShortComment", 2, "kind (word)" },
317 /* 0xa1 */ { "LongComment", 0, "kind (word), size (word), data" }
321 Forward declarations.
323 static MagickBooleanType
324 WritePICTImage(const ImageInfo *,Image *,ExceptionInfo *);
327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331 % D e c o d e I m a g e %
335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337 % DecodeImage decompresses an image via Macintosh pack bits decoding for
338 % Macintosh PICT images.
340 % The format of the DecodeImage method is:
342 % unsigned char *DecodeImage(Image *blob,Image *image,
343 % size_t bytes_per_line,const int bits_per_pixel,
344 % unsigned size_t extent)
346 % A description of each parameter follows:
348 % o image_info: the image info.
350 % o blob,image: the address of a structure of type Image.
352 % o bytes_per_line: This integer identifies the number of bytes in a
355 % o bits_per_pixel: the number of bits in a pixel.
357 % o extent: the number of pixels allocated.
361 static unsigned char *ExpandBuffer(unsigned char *pixels,
362 MagickSizeType *bytes_per_line,const unsigned int bits_per_pixel)
367 register unsigned char
376 switch (bits_per_pixel)
384 for (i=0; i < (ssize_t) *bytes_per_line; i++)
386 *q++=(*p >> 4) & 0xff;
395 for (i=0; i < (ssize_t) *bytes_per_line; i++)
397 *q++=(*p >> 6) & 0x03;
398 *q++=(*p >> 4) & 0x03;
399 *q++=(*p >> 2) & 0x03;
408 for (i=0; i < (ssize_t) *bytes_per_line; i++)
410 *q++=(*p >> 7) & 0x01;
411 *q++=(*p >> 6) & 0x01;
412 *q++=(*p >> 5) & 0x01;
413 *q++=(*p >> 4) & 0x01;
414 *q++=(*p >> 3) & 0x01;
415 *q++=(*p >> 2) & 0x01;
416 *q++=(*p >> 1) & 0x01;
429 static unsigned char *DecodeImage(Image *blob,Image *image,
430 size_t bytes_per_line,const unsigned int bits_per_pixel,size_t *extent)
438 register unsigned char
459 Determine pixel buffer size.
461 if (bits_per_pixel <= 8)
462 bytes_per_line&=0x7fff;
463 width=image->columns;
465 if (bits_per_pixel == 16)
471 if (bits_per_pixel == 32)
472 width*=image->matte ? 4 : 3;
473 if (bytes_per_line == 0)
474 bytes_per_line=width;
475 row_bytes=(size_t) (image->columns | 0x8000);
476 if (image->storage_class == DirectClass)
477 row_bytes=(size_t) ((4*image->columns) | 0x8000);
479 Allocate pixel and scanline buffer.
481 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,row_bytes*
483 if (pixels == (unsigned char *) NULL)
484 return((unsigned char *) NULL);
485 *extent=row_bytes*image->rows*sizeof(*pixels);
486 (void) ResetMagickMemory(pixels,0,*extent);
487 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
488 if (scanline == (unsigned char *) NULL)
489 return((unsigned char *) NULL);
490 if (bytes_per_line < 8)
493 Pixels are already uncompressed.
495 for (y=0; y < (ssize_t) image->rows; y++)
497 q=pixels+y*width*GetPixelChannels(image);;
498 number_pixels=bytes_per_line;
499 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 < (ssize_t) 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 < (ssize_t) 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+=(ssize_t) (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 < (ssize_t) length; i++)
549 if ((q-pixels+number_pixels) <= *extent)
550 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
553 j+=(ssize_t) 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 size_t 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 size_t bytes_per_line,unsigned char *pixels)
595 #define MaxPackbitsRunlength 128
597 register const unsigned char
603 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=(ssize_t) 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 size_t MagickMax(const size_t x,
791 static Image *ReadPICTImage(const ImageInfo *image_info,
792 ExceptionInfo *exception)
795 geometry[MaxTextExtent];
841 assert(image_info != (const ImageInfo *) NULL);
842 assert(image_info->signature == MagickSignature);
843 if (image_info->debug != MagickFalse)
844 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
845 image_info->filename);
846 assert(exception != (ExceptionInfo *) NULL);
847 assert(exception->signature == MagickSignature);
848 image=AcquireImage(image_info,exception);
850 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
851 if (status == MagickFalse)
853 image=DestroyImageList(image);
854 return((Image *) NULL);
859 pixmap.bits_per_pixel=0;
860 pixmap.component_count=0;
861 for (i=0; i < 512; i++)
862 (void) ReadBlobByte(image); /* skip header */
863 (void) ReadBlobMSBShort(image); /* skip picture size */
864 ReadRectangle(image,frame);
865 while ((c=ReadBlobByte(image)) == 0) ;
867 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
868 version=ReadBlobByte(image);
871 c=ReadBlobByte(image);
873 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
877 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
878 if ((frame.left < 0) || (frame.right < 0) || (frame.top < 0) ||
879 (frame.bottom < 0) || (frame.left >= frame.right) ||
880 (frame.top >= frame.bottom))
881 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
886 image->columns=1UL*(frame.right-frame.left);
887 image->rows=1UL*(frame.bottom-frame.top);
888 image->x_resolution=DefaultResolution;
889 image->y_resolution=DefaultResolution;
890 image->units=UndefinedResolution;
892 Interpret PICT opcodes.
895 for (code=0; EOFBlob(image) == MagickFalse; )
897 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
898 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
900 if ((version == 1) || ((TellBlob(image) % 2) != 0))
901 code=ReadBlobByte(image);
903 code=(int) ReadBlobMSBShort(image);
906 if (image->debug != MagickFalse)
907 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%04X:",code);
911 if (image->debug != MagickFalse)
912 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
913 " %04X %s: %s",code,codes[code].name,codes[code].description);
921 length=ReadBlobMSBShort(image);
922 if (length != 0x000a)
924 for (i=0; i < (ssize_t) (length-2); i++)
925 (void) ReadBlobByte(image);
928 ReadRectangle(image,frame);
929 if (((frame.left & 0x8000) != 0) || ((frame.top & 0x8000) != 0))
931 image->columns=1UL*(frame.right-frame.left);
932 image->rows=1UL*(frame.bottom-frame.top);
933 (void) SetImageBackgroundColor(image);
948 Skip pattern definition.
950 pattern=1L*ReadBlobMSBShort(image);
951 for (i=0; i < 8; i++)
952 (void) ReadBlobByte(image);
955 for (i=0; i < 5; i++)
956 (void) ReadBlobByte(image);
960 ThrowReaderException(CorruptImageError,"UnknownPatternType");
961 length=ReadBlobMSBShort(image);
962 ReadRectangle(image,frame);
964 image->depth=1UL*pixmap.component_size;
965 image->x_resolution=1.0*pixmap.horizontal_resolution;
966 image->y_resolution=1.0*pixmap.vertical_resolution;
967 image->units=PixelsPerInchResolution;
968 (void) ReadBlobMSBLong(image);
969 flags=1L*ReadBlobMSBShort(image);
970 length=ReadBlobMSBShort(image);
971 for (i=0; i <= (ssize_t) length; i++)
972 (void) ReadBlobMSBLong(image);
973 width=1UL*(frame.bottom-frame.top);
974 height=1UL*(frame.right-frame.left);
975 if (pixmap.bits_per_pixel <= 8)
977 if (pixmap.bits_per_pixel == 16)
983 for (i=0; i < (ssize_t) (length*height); i++)
984 (void) ReadBlobByte(image);
987 for (j=0; j < (int) height; j++)
989 for (j=0; j < (ssize_t) ReadBlobMSBShort(image); j++)
990 (void) ReadBlobByte(image);
992 for (j=0; j < (ssize_t) ReadBlobByte(image); j++)
993 (void) ReadBlobByte(image);
999 Initialize image background color.
1001 image->background_color.red=(Quantum)
1002 ScaleShortToQuantum(ReadBlobMSBShort(image));
1003 image->background_color.green=(Quantum)
1004 ScaleShortToQuantum(ReadBlobMSBShort(image));
1005 image->background_color.blue=(Quantum)
1006 ScaleShortToQuantum(ReadBlobMSBShort(image));
1019 Skip polygon or region.
1021 length=ReadBlobMSBShort(image);
1022 for (i=0; i < (ssize_t) (length-2); i++)
1023 (void) ReadBlobByte(image);
1040 register unsigned char
1053 Pixmap clipped by a rectangle.
1056 if ((code != 0x9a) && (code != 0x9b))
1057 bytes_per_line=1L*ReadBlobMSBShort(image);
1060 (void) ReadBlobMSBShort(image);
1061 (void) ReadBlobMSBShort(image);
1062 (void) ReadBlobMSBShort(image);
1064 ReadRectangle(image,frame);
1066 Initialize tile image.
1068 tile_image=CloneImage(image,1UL*(frame.right-frame.left),
1069 1UL*(frame.bottom-frame.top),MagickTrue,exception);
1070 if (tile_image == (Image *) NULL)
1071 return((Image *) NULL);
1072 if ((code == 0x9a) || (code == 0x9b) ||
1073 ((bytes_per_line & 0x8000) != 0))
1076 tile_image->depth=1UL*pixmap.component_size;
1077 tile_image->matte=pixmap.component_count == 4 ?
1078 MagickTrue : MagickFalse;
1079 tile_image->x_resolution=(double) pixmap.horizontal_resolution;
1080 tile_image->y_resolution=(double) pixmap.vertical_resolution;
1081 tile_image->units=PixelsPerInchResolution;
1082 if (tile_image->matte != MagickFalse)
1083 image->matte=tile_image->matte;
1085 if ((code != 0x9a) && (code != 0x9b))
1088 Initialize colormap.
1090 tile_image->colors=2;
1091 if ((bytes_per_line & 0x8000) != 0)
1093 (void) ReadBlobMSBLong(image);
1094 flags=1L*ReadBlobMSBShort(image);
1095 tile_image->colors=1UL*ReadBlobMSBShort(image)+1;
1097 status=AcquireImageColormap(tile_image,tile_image->colors,
1099 if (status == MagickFalse)
1101 tile_image=DestroyImage(tile_image);
1102 ThrowReaderException(ResourceLimitError,
1103 "MemoryAllocationFailed");
1105 if ((bytes_per_line & 0x8000) != 0)
1107 for (i=0; i < (ssize_t) tile_image->colors; i++)
1109 j=ReadBlobMSBShort(image) % tile_image->colors;
1110 if ((flags & 0x8000) != 0)
1112 tile_image->colormap[j].red=(Quantum)
1113 ScaleShortToQuantum(ReadBlobMSBShort(image));
1114 tile_image->colormap[j].green=(Quantum)
1115 ScaleShortToQuantum(ReadBlobMSBShort(image));
1116 tile_image->colormap[j].blue=(Quantum)
1117 ScaleShortToQuantum(ReadBlobMSBShort(image));
1122 for (i=0; i < (ssize_t) tile_image->colors; i++)
1124 tile_image->colormap[i].red=(Quantum) (QuantumRange-
1125 tile_image->colormap[i].red);
1126 tile_image->colormap[i].green=(Quantum) (QuantumRange-
1127 tile_image->colormap[i].green);
1128 tile_image->colormap[i].blue=(Quantum) (QuantumRange-
1129 tile_image->colormap[i].blue);
1133 ReadRectangle(image,source);
1134 ReadRectangle(image,destination);
1135 (void) ReadBlobMSBShort(image);
1136 if ((code == 0x91) || (code == 0x99) || (code == 0x9b))
1141 length=ReadBlobMSBShort(image);
1142 for (i=0; i < (ssize_t) (length-2); i++)
1143 (void) ReadBlobByte(image);
1145 if ((code != 0x9a) && (code != 0x9b) &&
1146 (bytes_per_line & 0x8000) == 0)
1147 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1,&extent);
1149 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1U*
1150 pixmap.bits_per_pixel,&extent);
1151 if (pixels == (unsigned char *) NULL)
1153 tile_image=DestroyImage(tile_image);
1154 ThrowReaderException(ResourceLimitError,
1155 "MemoryAllocationFailed");
1158 Convert PICT tile image to pixel packets.
1161 for (y=0; y < (ssize_t) tile_image->rows; y++)
1163 if (p > (pixels+extent+image->columns))
1164 ThrowReaderException(CorruptImageError,"NotEnoughPixelData");
1165 q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,
1167 if (q == (Quantum *) NULL)
1169 for (x=0; x < (ssize_t) tile_image->columns; x++)
1171 if (tile_image->storage_class == PseudoClass)
1173 index=ConstrainColormapIndex(tile_image,*p);
1174 SetPixelIndex(tile_image,index,q);
1175 SetPixelRed(tile_image,
1176 tile_image->colormap[(ssize_t) index].red,q);
1177 SetPixelGreen(tile_image,
1178 tile_image->colormap[(ssize_t) index].green,q);
1179 SetPixelBlue(tile_image,
1180 tile_image->colormap[(ssize_t) index].blue,q);
1184 if (pixmap.bits_per_pixel == 16)
1188 SetPixelRed(tile_image,ScaleCharToQuantum(
1189 (unsigned char) ((i & 0x7c) << 1)),q);
1190 SetPixelGreen(tile_image,ScaleCharToQuantum(
1191 (unsigned char) (((i & 0x03) << 6) |
1192 ((j & 0xe0) >> 2))),q);
1193 SetPixelBlue(tile_image,ScaleCharToQuantum(
1194 (unsigned char) ((j & 0x1f) << 3)),q);
1197 if (tile_image->matte == MagickFalse)
1199 if (p > (pixels+extent+2*image->columns))
1200 ThrowReaderException(CorruptImageError,
1201 "NotEnoughPixelData");
1202 SetPixelRed(tile_image,ScaleCharToQuantum(*p),q);
1203 SetPixelGreen(tile_image,ScaleCharToQuantum(
1204 *(p+tile_image->columns)),q);
1205 SetPixelBlue(tile_image,ScaleCharToQuantum(
1206 *(p+2*tile_image->columns)),q);
1210 if (p > (pixels+extent+3*image->columns))
1211 ThrowReaderException(CorruptImageError,
1212 "NotEnoughPixelData");
1213 SetPixelAlpha(tile_image,ScaleCharToQuantum(*p),q);
1214 SetPixelRed(tile_image,ScaleCharToQuantum(
1215 *(p+tile_image->columns)),q);
1216 SetPixelGreen(tile_image,ScaleCharToQuantum(
1217 *(p+2*tile_image->columns)),q);
1218 SetPixelBlue(tile_image,ScaleCharToQuantum(
1219 *(p+3*tile_image->columns)),q);
1223 q+=GetPixelChannels(tile_image);
1225 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
1227 if ((tile_image->storage_class == DirectClass) &&
1228 (pixmap.bits_per_pixel != 16))
1230 p+=(pixmap.component_count-1)*tile_image->columns;
1234 status=SetImageProgress(image,LoadImageTag,y,tile_image->rows);
1235 if (status == MagickFalse)
1238 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1239 if (jpeg == MagickFalse)
1240 if ((code == 0x9a) || (code == 0x9b) ||
1241 ((bytes_per_line & 0x8000) != 0))
1242 (void) CompositeImage(image,CopyCompositeOp,tile_image,
1243 destination.left,destination.top);
1244 tile_image=DestroyImage(tile_image);
1258 type=ReadBlobMSBShort(image);
1259 length=ReadBlobMSBShort(image);
1262 (void) ReadBlobMSBLong(image);
1266 info=(unsigned char *) AcquireQuantumMemory(length,sizeof(*info));
1267 if (info == (unsigned char *) NULL)
1269 count=ReadBlob(image,length,info);
1277 profile=BlobToStringInfo((const void *) NULL,length);
1278 SetStringInfoDatum(profile,info);
1279 status=SetImageProfile(image,"icc",profile);
1280 profile=DestroyStringInfo(profile);
1281 if (status == MagickFalse)
1282 ThrowReaderException(ResourceLimitError,
1283 "MemoryAllocationFailed");
1290 profile=BlobToStringInfo((const void *) NULL,length);
1291 SetStringInfoDatum(profile,info);
1292 status=SetImageProfile(image,"iptc",profile);
1293 if (status == MagickFalse)
1294 ThrowReaderException(ResourceLimitError,
1295 "MemoryAllocationFailed");
1296 profile=DestroyStringInfo(profile);
1302 info=(unsigned char *) RelinquishMagickMemory(info);
1308 Skip to next op code.
1310 if (codes[code].length == -1)
1311 (void) ReadBlobMSBShort(image);
1313 for (i=0; i < (ssize_t) codes[code].length; i++)
1314 (void) ReadBlobByte(image);
1323 for (i=0; i < 24; i++)
1324 (void) ReadBlobByte(image);
1327 if (((code >= 0xb0) && (code <= 0xcf)) ||
1328 ((code >= 0x8000) && (code <= 0x80ff)))
1348 read_info=CloneImageInfo(image_info);
1349 SetImageInfoBlob(read_info,(void *) NULL,0);
1351 unique_file=AcquireUniqueFileResource(read_info->filename);
1352 if (unique_file != -1)
1353 file=fdopen(unique_file,"wb");
1354 if ((unique_file == -1) || (file == (FILE *) NULL))
1356 (void) CopyMagickString(image->filename,read_info->filename,
1358 ThrowFileException(exception,FileOpenError,
1359 "UnableToCreateTemporaryFile",image->filename);
1360 image=DestroyImageList(image);
1361 return((Image *) NULL);
1363 length=ReadBlobMSBLong(image);
1364 for (i=0; i < 6; i++)
1365 (void) ReadBlobMSBLong(image);
1366 ReadRectangle(image,frame);
1367 for (i=0; i < 122; i++)
1368 (void) ReadBlobByte(image);
1369 for (i=0; i < (ssize_t) (length-154); i++)
1371 c=ReadBlobByte(image);
1372 (void) fputc(c,file);
1374 (void) fclose(file);
1375 tile_image=ReadImage(read_info,exception);
1376 (void) RelinquishUniqueFileResource(read_info->filename);
1377 read_info=DestroyImageInfo(read_info);
1378 if (tile_image == (Image *) NULL)
1380 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g",
1381 (double) MagickMax(image->columns,tile_image->columns),
1382 (double) MagickMax(image->rows,tile_image->rows));
1383 (void) SetImageExtent(image,
1384 MagickMax(image->columns,tile_image->columns),
1385 MagickMax(image->rows,tile_image->rows),exception);
1386 if (IsRGBColorspace(image->colorspace) == MagickFalse)
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 < (ssize_t) length; i++)
1404 (void) ReadBlobByte(image);
1407 if ((code >= 0x100) && (code <= 0x7fff))
1412 length=(size_t) ((code >> 7) & 0xff);
1413 for (i=0; i < (ssize_t) 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 % size_t RegisterPICTImage(void)
1446 ModuleExport size_t RegisterPICTImage(void)
1451 entry=SetMagickInfo("PCT");
1452 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1453 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1454 entry->adjoin=MagickFalse;
1455 entry->seekable_stream=MagickTrue;
1456 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1457 entry->magick=(IsImageFormatHandler *) IsPICT;
1458 entry->module=ConstantString("PICT");
1459 (void) RegisterMagickInfo(entry);
1460 entry=SetMagickInfo("PICT");
1461 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1462 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1463 entry->adjoin=MagickFalse;
1464 entry->seekable_stream=MagickTrue;
1465 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1466 entry->magick=(IsImageFormatHandler *) IsPICT;
1467 entry->module=ConstantString("PICT");
1468 (void) RegisterMagickInfo(entry);
1469 return(MagickImageCoderSignature);
1473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1477 % U n r e g i s t e r P I C T I m a g e %
1481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1483 % UnregisterPICTImage() removes format registrations made by the
1484 % PICT module from the list of supported formats.
1486 % The format of the UnregisterPICTImage method is:
1488 % UnregisterPICTImage(void)
1491 ModuleExport void UnregisterPICTImage(void)
1493 (void) UnregisterMagickInfo("PCT");
1494 (void) UnregisterMagickInfo("PICT");
1498 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1502 % W r i t e P I C T I m a g e %
1506 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1508 % WritePICTImage() writes an image to a file in the Apple Macintosh
1509 % QuickDraw/PICT image format.
1511 % The format of the WritePICTImage method is:
1513 % MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1514 % Image *image,ExceptionInfo *exception)
1516 % A description of each parameter follows.
1518 % o image_info: the image info.
1520 % o image: The image.
1522 % o exception: return any errors or warnings in this structure.
1525 static MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1526 Image *image,ExceptionInfo *exception)
1528 #define MaxCount 128
1529 #define PictCropRegionOp 0x01
1530 #define PictEndOfPictureOp 0xff
1531 #define PictJPEGOp 0x8200
1532 #define PictInfoOp 0x0C00
1533 #define PictInfoSize 512
1534 #define PictPixmapOp 0x9A
1535 #define PictPICTOp 0x98
1536 #define PictVersion 0x11
1557 destination_rectangle,
1562 register const Quantum
1588 Open output image file.
1590 assert(image_info != (const ImageInfo *) NULL);
1591 assert(image_info->signature == MagickSignature);
1592 assert(image != (Image *) NULL);
1593 assert(image->signature == MagickSignature);
1594 if (image->debug != MagickFalse)
1595 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1596 if ((image->columns > 65535L) || (image->rows > 65535L))
1597 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1598 assert(exception != (ExceptionInfo *) NULL);
1599 assert(exception->signature == MagickSignature);
1600 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1601 if (status == MagickFalse)
1603 if (IsRGBColorspace(image->colorspace) == MagickFalse)
1604 (void) TransformImageColorspace(image,RGBColorspace);
1606 Initialize image info.
1608 size_rectangle.top=0;
1609 size_rectangle.left=0;
1610 size_rectangle.bottom=(short) image->rows;
1611 size_rectangle.right=(short) image->columns;
1612 frame_rectangle=size_rectangle;
1613 crop_rectangle=size_rectangle;
1614 source_rectangle=size_rectangle;
1615 destination_rectangle=size_rectangle;
1617 row_bytes=(unsigned short) (image->columns | 0x8000);
1620 bounds.bottom=(short) image->rows;
1621 bounds.right=(short) image->columns;
1625 pixmap.pixel_type=0;
1626 pixmap.bits_per_pixel=8;
1627 pixmap.component_count=1;
1628 pixmap.component_size=8;
1629 pixmap.plane_bytes=0;
1633 x_resolution=image->x_resolution != 0.0 ? image->x_resolution :
1635 y_resolution=image->y_resolution != 0.0 ? image->y_resolution :
1637 storage_class=image->storage_class;
1638 if (image_info->compression == JPEGCompression)
1639 storage_class=DirectClass;
1640 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1642 pixmap.component_count=image->matte ? 4 : 3;
1643 pixmap.pixel_type=16;
1644 pixmap.bits_per_pixel=32;
1645 pixmap.pack_type=0x04;
1647 row_bytes=(unsigned short) ((4*image->columns) | 0x8000);
1652 bytes_per_line=image->columns;
1653 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1654 bytes_per_line*=image->matte ? 4 : 3;
1655 buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer));
1656 packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t)
1657 (row_bytes+MaxCount),sizeof(*packed_scanline));
1658 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
1659 if ((buffer == (unsigned char *) NULL) ||
1660 (packed_scanline == (unsigned char *) NULL) ||
1661 (scanline == (unsigned char *) NULL))
1662 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1663 (void) ResetMagickMemory(scanline,0,row_bytes);
1664 (void) ResetMagickMemory(packed_scanline,0,(size_t) (row_bytes+MaxCount));
1666 Write header, header size, size bounding box, version, and reserved.
1668 (void) ResetMagickMemory(buffer,0,PictInfoSize);
1669 (void) WriteBlob(image,PictInfoSize,buffer);
1670 (void) WriteBlobMSBShort(image,0);
1671 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top);
1672 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left);
1673 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom);
1674 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right);
1675 (void) WriteBlobMSBShort(image,PictVersion);
1676 (void) WriteBlobMSBShort(image,0x02ff); /* version #2 */
1677 (void) WriteBlobMSBShort(image,PictInfoOp);
1678 (void) WriteBlobMSBLong(image,0xFFFE0000UL);
1680 Write full size of the file, resolution, frame bounding box, and reserved.
1682 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1683 (void) WriteBlobMSBShort(image,0x0000);
1684 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1685 (void) WriteBlobMSBShort(image,0x0000);
1686 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top);
1687 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left);
1688 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom);
1689 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right);
1690 (void) WriteBlobMSBLong(image,0x00000000L);
1691 profile=GetImageProfile(image,"iptc");
1692 if (profile != (StringInfo *) NULL)
1694 (void) WriteBlobMSBShort(image,0xa1);
1695 (void) WriteBlobMSBShort(image,0x1f2);
1696 (void) WriteBlobMSBShort(image,(unsigned short)
1697 (GetStringInfoLength(profile)+4));
1698 (void) WriteBlobString(image,"8BIM");
1699 (void) WriteBlob(image,GetStringInfoLength(profile),
1700 GetStringInfoDatum(profile));
1702 profile=GetImageProfile(image,"icc");
1703 if (profile != (StringInfo *) NULL)
1705 (void) WriteBlobMSBShort(image,0xa1);
1706 (void) WriteBlobMSBShort(image,0xe0);
1707 (void) WriteBlobMSBShort(image,(unsigned short)
1708 (GetStringInfoLength(profile)+4));
1709 (void) WriteBlobMSBLong(image,0x00000000UL);
1710 (void) WriteBlob(image,GetStringInfoLength(profile),
1711 GetStringInfoDatum(profile));
1712 (void) WriteBlobMSBShort(image,0xa1);
1713 (void) WriteBlobMSBShort(image,0xe0);
1714 (void) WriteBlobMSBShort(image,4);
1715 (void) WriteBlobMSBLong(image,0x00000002UL);
1718 Write crop region opcode and crop bounding box.
1720 (void) WriteBlobMSBShort(image,PictCropRegionOp);
1721 (void) WriteBlobMSBShort(image,0xa);
1722 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top);
1723 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left);
1724 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom);
1725 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right);
1726 if (image_info->compression == JPEGCompression)
1740 jpeg_image=CloneImage(image,0,0,MagickTrue,exception);
1741 if (jpeg_image == (Image *) NULL)
1743 (void) CloseBlob(image);
1744 return(MagickFalse);
1746 jpeg_info=CloneImageInfo(image_info);
1747 (void) CopyMagickString(jpeg_info->magick,"JPEG",MaxTextExtent);
1749 blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length,
1751 jpeg_info=DestroyImageInfo(jpeg_info);
1752 if (blob == (unsigned char *) NULL)
1753 return(MagickFalse);
1754 jpeg_image=DestroyImage(jpeg_image);
1755 (void) WriteBlobMSBShort(image,PictJPEGOp);
1756 (void) WriteBlobMSBLong(image,(unsigned int) length+154);
1757 (void) WriteBlobMSBShort(image,0x0000);
1758 (void) WriteBlobMSBLong(image,0x00010000UL);
1759 (void) WriteBlobMSBLong(image,0x00000000UL);
1760 (void) WriteBlobMSBLong(image,0x00000000UL);
1761 (void) WriteBlobMSBLong(image,0x00000000UL);
1762 (void) WriteBlobMSBLong(image,0x00010000UL);
1763 (void) WriteBlobMSBLong(image,0x00000000UL);
1764 (void) WriteBlobMSBLong(image,0x00000000UL);
1765 (void) WriteBlobMSBLong(image,0x00000000UL);
1766 (void) WriteBlobMSBLong(image,0x40000000UL);
1767 (void) WriteBlobMSBLong(image,0x00000000UL);
1768 (void) WriteBlobMSBLong(image,0x00000000UL);
1769 (void) WriteBlobMSBLong(image,0x00000000UL);
1770 (void) WriteBlobMSBLong(image,0x00400000UL);
1771 (void) WriteBlobMSBShort(image,0x0000);
1772 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1773 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1774 (void) WriteBlobMSBShort(image,0x0000);
1775 (void) WriteBlobMSBShort(image,768);
1776 (void) WriteBlobMSBShort(image,0x0000);
1777 (void) WriteBlobMSBLong(image,0x00000000UL);
1778 (void) WriteBlobMSBLong(image,0x00566A70UL);
1779 (void) WriteBlobMSBLong(image,0x65670000UL);
1780 (void) WriteBlobMSBLong(image,0x00000000UL);
1781 (void) WriteBlobMSBLong(image,0x00000001UL);
1782 (void) WriteBlobMSBLong(image,0x00016170UL);
1783 (void) WriteBlobMSBLong(image,0x706C0000UL);
1784 (void) WriteBlobMSBLong(image,0x00000000UL);
1785 (void) WriteBlobMSBShort(image,768);
1786 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1787 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1788 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1789 (void) WriteBlobMSBShort(image,0x0000);
1790 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1791 (void) WriteBlobMSBLong(image,0x00000000UL);
1792 (void) WriteBlobMSBLong(image,0x87AC0001UL);
1793 (void) WriteBlobMSBLong(image,0x0B466F74UL);
1794 (void) WriteBlobMSBLong(image,0x6F202D20UL);
1795 (void) WriteBlobMSBLong(image,0x4A504547UL);
1796 (void) WriteBlobMSBLong(image,0x00000000UL);
1797 (void) WriteBlobMSBLong(image,0x00000000UL);
1798 (void) WriteBlobMSBLong(image,0x00000000UL);
1799 (void) WriteBlobMSBLong(image,0x00000000UL);
1800 (void) WriteBlobMSBLong(image,0x00000000UL);
1801 (void) WriteBlobMSBLong(image,0x0018FFFFUL);
1802 (void) WriteBlob(image,length,blob);
1803 if ((length & 0x01) != 0)
1804 (void) WriteBlobByte(image,'\0');
1805 blob=(unsigned char *) RelinquishMagickMemory(blob);
1808 Write picture opcode, row bytes, and picture bounding box, and version.
1810 if (storage_class == PseudoClass)
1811 (void) WriteBlobMSBShort(image,PictPICTOp);
1814 (void) WriteBlobMSBShort(image,PictPixmapOp);
1815 (void) WriteBlobMSBLong(image,(size_t) base_address);
1817 (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000));
1818 (void) WriteBlobMSBShort(image,(unsigned short) bounds.top);
1819 (void) WriteBlobMSBShort(image,(unsigned short) bounds.left);
1820 (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom);
1821 (void) WriteBlobMSBShort(image,(unsigned short) bounds.right);
1823 Write pack type, pack size, resolution, pixel type, and pixel size.
1825 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version);
1826 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type);
1827 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.pack_size);
1828 (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5));
1829 (void) WriteBlobMSBShort(image,0x0000);
1830 (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5));
1831 (void) WriteBlobMSBShort(image,0x0000);
1832 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type);
1833 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel);
1835 Write component count, size, plane bytes, table size, and reserved.
1837 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count);
1838 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size);
1839 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.plane_bytes);
1840 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.table);
1841 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.reserved);
1842 if (storage_class == PseudoClass)
1845 Write image colormap.
1847 (void) WriteBlobMSBLong(image,0x00000000L); /* color seed */
1848 (void) WriteBlobMSBShort(image,0L); /* color flags */
1849 (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1));
1850 for (i=0; i < (ssize_t) image->colors; i++)
1852 (void) WriteBlobMSBShort(image,(unsigned short) i);
1853 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1854 image->colormap[i].red));
1855 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1856 image->colormap[i].green));
1857 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1858 image->colormap[i].blue));
1862 Write source and destination rectangle.
1864 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top);
1865 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left);
1866 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom);
1867 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right);
1868 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top);
1869 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left);
1870 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom);
1871 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right);
1872 (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode);
1877 if ((storage_class == PseudoClass) && (image->matte == MagickFalse))
1878 for (y=0; y < (ssize_t) image->rows; y++)
1880 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1881 if (p == (const Quantum *) NULL)
1883 for (x=0; x < (ssize_t) image->columns; x++)
1885 scanline[x]=(unsigned char) GetPixelIndex(image,p);
1886 p+=GetPixelChannels(image);
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) (row_bytes & 0x7FFF),
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,exception);
1921 if (p == (const Quantum *) 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(GetPixelRed(image,p));
1936 *green++=ScaleQuantumToChar(GetPixelGreen(image,p));
1937 *blue++=ScaleQuantumToChar(GetPixelBlue(image,p));
1938 if (image->matte != MagickFalse)
1939 *opacity++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
1940 p+=GetPixelChannels(image);
1942 count+=EncodeImage(image,scanline,bytes_per_line & 0x7FFF,
1944 if (image->previous == (Image *) NULL)
1946 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1948 if (status == MagickFalse)
1953 if ((count & 0x01) != 0)
1954 (void) WriteBlobByte(image,'\0');
1955 (void) WriteBlobMSBShort(image,PictEndOfPictureOp);
1956 offset=TellBlob(image);
1957 offset=SeekBlob(image,512,SEEK_SET);
1958 (void) WriteBlobMSBShort(image,(unsigned short) offset);
1959 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
1960 packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline);
1961 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1962 (void) CloseBlob(image);