blob: 027119d81506fe8e71fa7602e25fa1686e716b56 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% PPPP IIIII CCCC TTTTT %
7% P P I C T %
8% PPPP I C T %
9% P I C T %
10% P IIIII CCCC T %
11% %
12% %
13% Read/Write Apple Macintosh QuickDraw/PICT Format %
14% %
15% Software Design %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
cristyb56bb242014-11-25 17:12:48 +000020% Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#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"
cristy510d06a2011-07-06 23:43:54 +000050#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000051#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"
cristy3ed852e2009-09-05 21:47:34 +000072
73/*
74 ImageMagick Macintosh PICT Methods.
75*/
76#define ReadPixmap(pixmap) \
77{ \
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); \
cristy6d62e4b2015-01-11 14:07:17 +000092 if ((EOFBlob(image) != MagickFalse) || (pixmap.bits_per_pixel <= 0) || \
93 (pixmap.bits_per_pixel > 32) || (pixmap.component_count <= 0) || \
94 (pixmap.component_count > 4) || (pixmap.component_size <= 0)) \
cristy3ed852e2009-09-05 21:47:34 +000095 ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
96}
97
cristy3ed852e2009-09-05 21:47:34 +000098typedef struct _PICTCode
99{
100 const char
101 *name;
102
cristybb503372010-05-27 20:51:26 +0000103 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000104 length;
105
106 const char
107 *description;
108} PICTCode;
109
110typedef struct _PICTPixmap
111{
112 short
113 version,
114 pack_type;
115
cristybb503372010-05-27 20:51:26 +0000116 size_t
cristy3ed852e2009-09-05 21:47:34 +0000117 pack_size,
118 horizontal_resolution,
119 vertical_resolution;
120
121 short
122 pixel_type,
123 bits_per_pixel,
124 component_count,
125 component_size;
126
cristybb503372010-05-27 20:51:26 +0000127 size_t
cristy3ed852e2009-09-05 21:47:34 +0000128 plane_bytes,
129 table,
130 reserved;
131} PICTPixmap;
132
133typedef struct _PICTRectangle
134{
135 short
136 top,
137 left,
138 bottom,
139 right;
140} PICTRectangle;
141
142static const PICTCode
143 codes[] =
144 {
145 /* 0x00 */ { "NOP", 0, "nop" },
146 /* 0x01 */ { "Clip", 0, "clip" },
147 /* 0x02 */ { "BkPat", 8, "background pattern" },
148 /* 0x03 */ { "TxFont", 2, "text font (word)" },
149 /* 0x04 */ { "TxFace", 1, "text face (byte)" },
150 /* 0x05 */ { "TxMode", 2, "text mode (word)" },
151 /* 0x06 */ { "SpExtra", 4, "space extra (fixed point)" },
152 /* 0x07 */ { "PnSize", 4, "pen size (point)" },
153 /* 0x08 */ { "PnMode", 2, "pen mode (word)" },
154 /* 0x09 */ { "PnPat", 8, "pen pattern" },
155 /* 0x0a */ { "FillPat", 8, "fill pattern" },
156 /* 0x0b */ { "OvSize", 4, "oval size (point)" },
157 /* 0x0c */ { "Origin", 4, "dh, dv (word)" },
158 /* 0x0d */ { "TxSize", 2, "text size (word)" },
cristybb503372010-05-27 20:51:26 +0000159 /* 0x0e */ { "FgColor", 4, "foreground color (ssize_tword)" },
160 /* 0x0f */ { "BkColor", 4, "background color (ssize_tword)" },
cristy3ed852e2009-09-05 21:47:34 +0000161 /* 0x10 */ { "TxRatio", 8, "numerator (point), denominator (point)" },
162 /* 0x11 */ { "Version", 1, "version (byte)" },
163 /* 0x12 */ { "BkPixPat", 0, "color background pattern" },
164 /* 0x13 */ { "PnPixPat", 0, "color pen pattern" },
165 /* 0x14 */ { "FillPixPat", 0, "color fill pattern" },
166 /* 0x15 */ { "PnLocHFrac", 2, "fractional pen position" },
167 /* 0x16 */ { "ChExtra", 2, "extra for each character" },
168 /* 0x17 */ { "reserved", 0, "reserved for Apple use" },
169 /* 0x18 */ { "reserved", 0, "reserved for Apple use" },
170 /* 0x19 */ { "reserved", 0, "reserved for Apple use" },
171 /* 0x1a */ { "RGBFgCol", 6, "RGB foreColor" },
172 /* 0x1b */ { "RGBBkCol", 6, "RGB backColor" },
173 /* 0x1c */ { "HiliteMode", 0, "hilite mode flag" },
174 /* 0x1d */ { "HiliteColor", 6, "RGB hilite color" },
175 /* 0x1e */ { "DefHilite", 0, "Use default hilite color" },
176 /* 0x1f */ { "OpColor", 6, "RGB OpColor for arithmetic modes" },
177 /* 0x20 */ { "Line", 8, "pnLoc (point), newPt (point)" },
178 /* 0x21 */ { "LineFrom", 4, "newPt (point)" },
179 /* 0x22 */ { "ShortLine", 6, "pnLoc (point, dh, dv (-128 .. 127))" },
180 /* 0x23 */ { "ShortLineFrom", 2, "dh, dv (-128 .. 127)" },
181 /* 0x24 */ { "reserved", -1, "reserved for Apple use" },
182 /* 0x25 */ { "reserved", -1, "reserved for Apple use" },
183 /* 0x26 */ { "reserved", -1, "reserved for Apple use" },
184 /* 0x27 */ { "reserved", -1, "reserved for Apple use" },
185 /* 0x28 */ { "LongText", 0, "txLoc (point), count (0..255), text" },
186 /* 0x29 */ { "DHText", 0, "dh (0..255), count (0..255), text" },
187 /* 0x2a */ { "DVText", 0, "dv (0..255), count (0..255), text" },
188 /* 0x2b */ { "DHDVText", 0, "dh, dv (0..255), count (0..255), text" },
189 /* 0x2c */ { "reserved", -1, "reserved for Apple use" },
190 /* 0x2d */ { "reserved", -1, "reserved for Apple use" },
191 /* 0x2e */ { "reserved", -1, "reserved for Apple use" },
192 /* 0x2f */ { "reserved", -1, "reserved for Apple use" },
193 /* 0x30 */ { "frameRect", 8, "rect" },
194 /* 0x31 */ { "paintRect", 8, "rect" },
195 /* 0x32 */ { "eraseRect", 8, "rect" },
196 /* 0x33 */ { "invertRect", 8, "rect" },
197 /* 0x34 */ { "fillRect", 8, "rect" },
198 /* 0x35 */ { "reserved", 8, "reserved for Apple use" },
199 /* 0x36 */ { "reserved", 8, "reserved for Apple use" },
200 /* 0x37 */ { "reserved", 8, "reserved for Apple use" },
201 /* 0x38 */ { "frameSameRect", 0, "rect" },
202 /* 0x39 */ { "paintSameRect", 0, "rect" },
203 /* 0x3a */ { "eraseSameRect", 0, "rect" },
204 /* 0x3b */ { "invertSameRect", 0, "rect" },
205 /* 0x3c */ { "fillSameRect", 0, "rect" },
206 /* 0x3d */ { "reserved", 0, "reserved for Apple use" },
207 /* 0x3e */ { "reserved", 0, "reserved for Apple use" },
208 /* 0x3f */ { "reserved", 0, "reserved for Apple use" },
209 /* 0x40 */ { "frameRRect", 8, "rect" },
210 /* 0x41 */ { "paintRRect", 8, "rect" },
211 /* 0x42 */ { "eraseRRect", 8, "rect" },
212 /* 0x43 */ { "invertRRect", 8, "rect" },
213 /* 0x44 */ { "fillRRrect", 8, "rect" },
214 /* 0x45 */ { "reserved", 8, "reserved for Apple use" },
215 /* 0x46 */ { "reserved", 8, "reserved for Apple use" },
216 /* 0x47 */ { "reserved", 8, "reserved for Apple use" },
217 /* 0x48 */ { "frameSameRRect", 0, "rect" },
218 /* 0x49 */ { "paintSameRRect", 0, "rect" },
219 /* 0x4a */ { "eraseSameRRect", 0, "rect" },
220 /* 0x4b */ { "invertSameRRect", 0, "rect" },
221 /* 0x4c */ { "fillSameRRect", 0, "rect" },
222 /* 0x4d */ { "reserved", 0, "reserved for Apple use" },
223 /* 0x4e */ { "reserved", 0, "reserved for Apple use" },
224 /* 0x4f */ { "reserved", 0, "reserved for Apple use" },
225 /* 0x50 */ { "frameOval", 8, "rect" },
226 /* 0x51 */ { "paintOval", 8, "rect" },
227 /* 0x52 */ { "eraseOval", 8, "rect" },
228 /* 0x53 */ { "invertOval", 8, "rect" },
229 /* 0x54 */ { "fillOval", 8, "rect" },
230 /* 0x55 */ { "reserved", 8, "reserved for Apple use" },
231 /* 0x56 */ { "reserved", 8, "reserved for Apple use" },
232 /* 0x57 */ { "reserved", 8, "reserved for Apple use" },
233 /* 0x58 */ { "frameSameOval", 0, "rect" },
234 /* 0x59 */ { "paintSameOval", 0, "rect" },
235 /* 0x5a */ { "eraseSameOval", 0, "rect" },
236 /* 0x5b */ { "invertSameOval", 0, "rect" },
237 /* 0x5c */ { "fillSameOval", 0, "rect" },
238 /* 0x5d */ { "reserved", 0, "reserved for Apple use" },
239 /* 0x5e */ { "reserved", 0, "reserved for Apple use" },
240 /* 0x5f */ { "reserved", 0, "reserved for Apple use" },
241 /* 0x60 */ { "frameArc", 12, "rect, startAngle, arcAngle" },
242 /* 0x61 */ { "paintArc", 12, "rect, startAngle, arcAngle" },
243 /* 0x62 */ { "eraseArc", 12, "rect, startAngle, arcAngle" },
244 /* 0x63 */ { "invertArc", 12, "rect, startAngle, arcAngle" },
245 /* 0x64 */ { "fillArc", 12, "rect, startAngle, arcAngle" },
246 /* 0x65 */ { "reserved", 12, "reserved for Apple use" },
247 /* 0x66 */ { "reserved", 12, "reserved for Apple use" },
248 /* 0x67 */ { "reserved", 12, "reserved for Apple use" },
249 /* 0x68 */ { "frameSameArc", 4, "rect, startAngle, arcAngle" },
250 /* 0x69 */ { "paintSameArc", 4, "rect, startAngle, arcAngle" },
251 /* 0x6a */ { "eraseSameArc", 4, "rect, startAngle, arcAngle" },
252 /* 0x6b */ { "invertSameArc", 4, "rect, startAngle, arcAngle" },
253 /* 0x6c */ { "fillSameArc", 4, "rect, startAngle, arcAngle" },
254 /* 0x6d */ { "reserved", 4, "reserved for Apple use" },
255 /* 0x6e */ { "reserved", 4, "reserved for Apple use" },
256 /* 0x6f */ { "reserved", 4, "reserved for Apple use" },
257 /* 0x70 */ { "framePoly", 0, "poly" },
258 /* 0x71 */ { "paintPoly", 0, "poly" },
259 /* 0x72 */ { "erasePoly", 0, "poly" },
260 /* 0x73 */ { "invertPoly", 0, "poly" },
261 /* 0x74 */ { "fillPoly", 0, "poly" },
262 /* 0x75 */ { "reserved", 0, "reserved for Apple use" },
263 /* 0x76 */ { "reserved", 0, "reserved for Apple use" },
264 /* 0x77 */ { "reserved", 0, "reserved for Apple use" },
265 /* 0x78 */ { "frameSamePoly", 0, "poly (NYI)" },
266 /* 0x79 */ { "paintSamePoly", 0, "poly (NYI)" },
267 /* 0x7a */ { "eraseSamePoly", 0, "poly (NYI)" },
268 /* 0x7b */ { "invertSamePoly", 0, "poly (NYI)" },
269 /* 0x7c */ { "fillSamePoly", 0, "poly (NYI)" },
270 /* 0x7d */ { "reserved", 0, "reserved for Apple use" },
271 /* 0x7e */ { "reserved", 0, "reserved for Apple use" },
272 /* 0x7f */ { "reserved", 0, "reserved for Apple use" },
273 /* 0x80 */ { "frameRgn", 0, "region" },
274 /* 0x81 */ { "paintRgn", 0, "region" },
275 /* 0x82 */ { "eraseRgn", 0, "region" },
276 /* 0x83 */ { "invertRgn", 0, "region" },
277 /* 0x84 */ { "fillRgn", 0, "region" },
278 /* 0x85 */ { "reserved", 0, "reserved for Apple use" },
279 /* 0x86 */ { "reserved", 0, "reserved for Apple use" },
280 /* 0x87 */ { "reserved", 0, "reserved for Apple use" },
281 /* 0x88 */ { "frameSameRgn", 0, "region (NYI)" },
282 /* 0x89 */ { "paintSameRgn", 0, "region (NYI)" },
283 /* 0x8a */ { "eraseSameRgn", 0, "region (NYI)" },
284 /* 0x8b */ { "invertSameRgn", 0, "region (NYI)" },
285 /* 0x8c */ { "fillSameRgn", 0, "region (NYI)" },
286 /* 0x8d */ { "reserved", 0, "reserved for Apple use" },
287 /* 0x8e */ { "reserved", 0, "reserved for Apple use" },
288 /* 0x8f */ { "reserved", 0, "reserved for Apple use" },
289 /* 0x90 */ { "BitsRect", 0, "copybits, rect clipped" },
290 /* 0x91 */ { "BitsRgn", 0, "copybits, rgn clipped" },
291 /* 0x92 */ { "reserved", -1, "reserved for Apple use" },
292 /* 0x93 */ { "reserved", -1, "reserved for Apple use" },
293 /* 0x94 */ { "reserved", -1, "reserved for Apple use" },
294 /* 0x95 */ { "reserved", -1, "reserved for Apple use" },
295 /* 0x96 */ { "reserved", -1, "reserved for Apple use" },
296 /* 0x97 */ { "reserved", -1, "reserved for Apple use" },
297 /* 0x98 */ { "PackBitsRect", 0, "packed copybits, rect clipped" },
298 /* 0x99 */ { "PackBitsRgn", 0, "packed copybits, rgn clipped" },
299 /* 0x9a */ { "DirectBitsRect", 0, "PixMap, srcRect, dstRect, mode, PixData" },
300 /* 0x9b */ { "DirectBitsRgn", 0, "PixMap, srcRect, dstRect, mode, maskRgn, PixData" },
301 /* 0x9c */ { "reserved", -1, "reserved for Apple use" },
302 /* 0x9d */ { "reserved", -1, "reserved for Apple use" },
303 /* 0x9e */ { "reserved", -1, "reserved for Apple use" },
304 /* 0x9f */ { "reserved", -1, "reserved for Apple use" },
305 /* 0xa0 */ { "ShortComment", 2, "kind (word)" },
306 /* 0xa1 */ { "LongComment", 0, "kind (word), size (word), data" }
307 };
308
309/*
310 Forward declarations.
311*/
312static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +0000313 WritePICTImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000314
315/*
316%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317% %
318% %
319% %
320% D e c o d e I m a g e %
321% %
322% %
323% %
324%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325%
326% DecodeImage decompresses an image via Macintosh pack bits decoding for
327% Macintosh PICT images.
328%
329% The format of the DecodeImage method is:
330%
331% unsigned char *DecodeImage(Image *blob,Image *image,
cristybb503372010-05-27 20:51:26 +0000332% size_t bytes_per_line,const int bits_per_pixel,
cristy3ed852e2009-09-05 21:47:34 +0000333% unsigned size_t extent)
334%
335% A description of each parameter follows:
336%
337% o image_info: the image info.
338%
339% o blob,image: the address of a structure of type Image.
340%
341% o bytes_per_line: This integer identifies the number of bytes in a
342% scanline.
343%
344% o bits_per_pixel: the number of bits in a pixel.
345%
346% o extent: the number of pixels allocated.
347%
348*/
349
350static unsigned char *ExpandBuffer(unsigned char *pixels,
351 MagickSizeType *bytes_per_line,const unsigned int bits_per_pixel)
352{
cristybb503372010-05-27 20:51:26 +0000353 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000354 i;
355
356 register unsigned char
357 *p,
358 *q;
359
360 static unsigned char
361 scanline[8*256];
362
363 p=pixels;
364 q=scanline;
365 switch (bits_per_pixel)
366 {
367 case 8:
368 case 16:
369 case 32:
370 return(pixels);
371 case 4:
372 {
cristybb503372010-05-27 20:51:26 +0000373 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000374 {
375 *q++=(*p >> 4) & 0xff;
376 *q++=(*p & 15);
377 p++;
378 }
379 *bytes_per_line*=2;
380 break;
381 }
382 case 2:
383 {
cristybb503372010-05-27 20:51:26 +0000384 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000385 {
386 *q++=(*p >> 6) & 0x03;
387 *q++=(*p >> 4) & 0x03;
388 *q++=(*p >> 2) & 0x03;
389 *q++=(*p & 3);
390 p++;
391 }
392 *bytes_per_line*=4;
393 break;
394 }
395 case 1:
396 {
cristybb503372010-05-27 20:51:26 +0000397 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000398 {
399 *q++=(*p >> 7) & 0x01;
400 *q++=(*p >> 6) & 0x01;
401 *q++=(*p >> 5) & 0x01;
402 *q++=(*p >> 4) & 0x01;
403 *q++=(*p >> 3) & 0x01;
404 *q++=(*p >> 2) & 0x01;
405 *q++=(*p >> 1) & 0x01;
406 *q++=(*p & 0x01);
407 p++;
408 }
409 *bytes_per_line*=8;
410 break;
411 }
412 default:
413 break;
414 }
415 return(scanline);
416}
417
418static unsigned char *DecodeImage(Image *blob,Image *image,
cristyc82a27b2011-10-21 01:07:16 +0000419 size_t bytes_per_line,const unsigned int bits_per_pixel,size_t *extent,
420 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000421{
cristy3ed852e2009-09-05 21:47:34 +0000422 MagickSizeType
423 number_pixels;
424
cristybb503372010-05-27 20:51:26 +0000425 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000426 i;
427
428 register unsigned char
429 *p,
430 *q;
431
432 size_t
cristyaff6d802011-04-26 01:46:31 +0000433 bytes_per_pixel,
cristy3ed852e2009-09-05 21:47:34 +0000434 length,
cristyaff6d802011-04-26 01:46:31 +0000435 row_bytes,
436 scanline_length,
437 width;
cristy3ed852e2009-09-05 21:47:34 +0000438
439 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000440 count,
441 j,
442 y;
cristy3ed852e2009-09-05 21:47:34 +0000443
444 unsigned char
445 *pixels,
446 *scanline;
447
cristy3ed852e2009-09-05 21:47:34 +0000448 /*
449 Determine pixel buffer size.
450 */
451 if (bits_per_pixel <= 8)
452 bytes_per_line&=0x7fff;
453 width=image->columns;
454 bytes_per_pixel=1;
455 if (bits_per_pixel == 16)
456 {
457 bytes_per_pixel=2;
458 width*=2;
459 }
460 else
461 if (bits_per_pixel == 32)
cristy8a46d822012-08-28 23:32:39 +0000462 width*=image->alpha_trait ? 4 : 3;
cristy3ed852e2009-09-05 21:47:34 +0000463 if (bytes_per_line == 0)
464 bytes_per_line=width;
465 row_bytes=(size_t) (image->columns | 0x8000);
466 if (image->storage_class == DirectClass)
467 row_bytes=(size_t) ((4*image->columns) | 0x8000);
468 /*
469 Allocate pixel and scanline buffer.
470 */
471 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,row_bytes*
472 sizeof(*pixels));
473 if (pixels == (unsigned char *) NULL)
474 return((unsigned char *) NULL);
475 *extent=row_bytes*image->rows*sizeof(*pixels);
476 (void) ResetMagickMemory(pixels,0,*extent);
cristyf432c632014-12-07 15:11:28 +0000477 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,2*
478 sizeof(*scanline));
cristy3ed852e2009-09-05 21:47:34 +0000479 if (scanline == (unsigned char *) NULL)
480 return((unsigned char *) NULL);
481 if (bytes_per_line < 8)
482 {
483 /*
484 Pixels are already uncompressed.
485 */
cristybb503372010-05-27 20:51:26 +0000486 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000487 {
cristyed231572011-07-14 02:18:59 +0000488 q=pixels+y*width*GetPixelChannels(image);;
cristy3ed852e2009-09-05 21:47:34 +0000489 number_pixels=bytes_per_line;
490 count=ReadBlob(blob,(size_t) number_pixels,scanline);
cristy6d62e4b2015-01-11 14:07:17 +0000491 if (count != (ssize_t) number_pixels)
492 {
493 (void) ThrowMagickException(exception,GetMagickModule(),
494 CorruptImageError,"UnableToUncompressImage","`%s'",
495 image->filename);
496 break;
497 }
cristy3ed852e2009-09-05 21:47:34 +0000498 p=ExpandBuffer(scanline,&number_pixels,bits_per_pixel);
499 if ((q+number_pixels) > (pixels+(*extent)))
500 {
cristyc82a27b2011-10-21 01:07:16 +0000501 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000502 CorruptImageError,"UnableToUncompressImage","`%s'",
503 image->filename);
504 break;
505 }
506 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
507 }
508 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
509 return(pixels);
510 }
511 /*
512 Uncompress RLE pixels into uncompressed pixel buffer.
513 */
cristybb503372010-05-27 20:51:26 +0000514 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000515 {
516 q=pixels+y*width;
517 if (bytes_per_line > 200)
518 scanline_length=ReadBlobMSBShort(blob);
519 else
520 scanline_length=1UL*ReadBlobByte(blob);
521 if (scanline_length >= row_bytes)
522 {
cristyc82a27b2011-10-21 01:07:16 +0000523 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000524 CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
525 break;
526 }
527 count=ReadBlob(blob,scanline_length,scanline);
cristy6d62e4b2015-01-11 14:07:17 +0000528 if (count != (ssize_t) scanline_length)
529 {
530 (void) ThrowMagickException(exception,GetMagickModule(),
531 CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
532 break;
533 }
cristybb503372010-05-27 20:51:26 +0000534 for (j=0; j < (ssize_t) scanline_length; )
cristy3ed852e2009-09-05 21:47:34 +0000535 if ((scanline[j] & 0x80) == 0)
536 {
537 length=(size_t) ((scanline[j] & 0xff)+1);
538 number_pixels=length*bytes_per_pixel;
539 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
540 if ((q-pixels+number_pixels) <= *extent)
541 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
542 q+=number_pixels;
cristybb503372010-05-27 20:51:26 +0000543 j+=(ssize_t) (length*bytes_per_pixel+1);
cristy3ed852e2009-09-05 21:47:34 +0000544 }
545 else
546 {
547 length=(size_t) (((scanline[j] ^ 0xff) & 0xff)+2);
548 number_pixels=bytes_per_pixel;
549 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
cristybb503372010-05-27 20:51:26 +0000550 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000551 {
552 if ((q-pixels+number_pixels) <= *extent)
553 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
554 q+=number_pixels;
555 }
cristyeaedf062010-05-29 22:36:02 +0000556 j+=(ssize_t) bytes_per_pixel+1;
cristy3ed852e2009-09-05 21:47:34 +0000557 }
558 }
559 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
560 return(pixels);
561}
562
563/*
564%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565% %
566% %
567% %
568% E n c o d e I m a g e %
569% %
570% %
571% %
572%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573%
574% EncodeImage compresses an image via Macintosh pack bits encoding
575% for Macintosh PICT images.
576%
577% The format of the EncodeImage method is:
578%
579% size_t EncodeImage(Image *image,const unsigned char *scanline,
cristybb503372010-05-27 20:51:26 +0000580% const size_t bytes_per_line,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000581%
582% A description of each parameter follows:
583%
584% o image: the address of a structure of type Image.
585%
586% o scanline: A pointer to an array of characters to pack.
587%
588% o bytes_per_line: the number of bytes in a scanline.
589%
590% o pixels: A pointer to an array of characters where the packed
591% characters are stored.
592%
cristy3ed852e2009-09-05 21:47:34 +0000593*/
594static size_t EncodeImage(Image *image,const unsigned char *scanline,
cristybb503372010-05-27 20:51:26 +0000595 const size_t bytes_per_line,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000596{
597#define MaxCount 128
598#define MaxPackbitsRunlength 128
599
cristy3ed852e2009-09-05 21:47:34 +0000600 register const unsigned char
601 *p;
602
cristybb503372010-05-27 20:51:26 +0000603 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000604 i;
605
606 register unsigned char
607 *q;
608
609 size_t
610 length;
611
cristyaff6d802011-04-26 01:46:31 +0000612 ssize_t
613 count,
614 repeat_count,
615 runlength;
616
cristy3ed852e2009-09-05 21:47:34 +0000617 unsigned char
618 index;
619
620 /*
621 Pack scanline.
622 */
623 assert(image != (Image *) NULL);
624 assert(image->signature == MagickSignature);
625 if (image->debug != MagickFalse)
626 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
627 assert(scanline != (unsigned char *) NULL);
628 assert(pixels != (unsigned char *) NULL);
629 count=0;
630 runlength=0;
631 p=scanline+(bytes_per_line-1);
632 q=pixels;
633 index=(*p);
cristybb503372010-05-27 20:51:26 +0000634 for (i=(ssize_t) bytes_per_line-1; i >= 0; i--)
cristy3ed852e2009-09-05 21:47:34 +0000635 {
636 if (index == *p)
637 runlength++;
638 else
639 {
640 if (runlength < 3)
641 while (runlength > 0)
642 {
643 *q++=(unsigned char) index;
644 runlength--;
645 count++;
646 if (count == MaxCount)
647 {
648 *q++=(unsigned char) (MaxCount-1);
649 count-=MaxCount;
650 }
651 }
652 else
653 {
654 if (count > 0)
655 *q++=(unsigned char) (count-1);
656 count=0;
657 while (runlength > 0)
658 {
659 repeat_count=runlength;
660 if (repeat_count > MaxPackbitsRunlength)
661 repeat_count=MaxPackbitsRunlength;
662 *q++=(unsigned char) index;
663 *q++=(unsigned char) (257-repeat_count);
664 runlength-=repeat_count;
665 }
666 }
667 runlength=1;
668 }
669 index=(*p);
670 p--;
671 }
672 if (runlength < 3)
673 while (runlength > 0)
674 {
675 *q++=(unsigned char) index;
676 runlength--;
677 count++;
678 if (count == MaxCount)
679 {
680 *q++=(unsigned char) (MaxCount-1);
681 count-=MaxCount;
682 }
683 }
684 else
685 {
686 if (count > 0)
687 *q++=(unsigned char) (count-1);
688 count=0;
689 while (runlength > 0)
690 {
691 repeat_count=runlength;
692 if (repeat_count > MaxPackbitsRunlength)
693 repeat_count=MaxPackbitsRunlength;
694 *q++=(unsigned char) index;
695 *q++=(unsigned char) (257-repeat_count);
696 runlength-=repeat_count;
697 }
698 }
699 if (count > 0)
700 *q++=(unsigned char) (count-1);
701 /*
702 Write the number of and the packed length.
703 */
704 length=(size_t) (q-pixels);
705 if (bytes_per_line > 200)
706 {
707 (void) WriteBlobMSBShort(image,(unsigned short) length);
708 length+=2;
709 }
710 else
711 {
712 (void) WriteBlobByte(image,(unsigned char) length);
713 length++;
714 }
715 while (q != pixels)
716 {
717 q--;
718 (void) WriteBlobByte(image,*q);
719 }
720 return(length);
721}
722
723/*
724%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
725% %
726% %
727% %
728% I s P I C T %
729% %
730% %
731% %
732%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733%
734% IsPICT()() returns MagickTrue if the image format type, identified by the
735% magick string, is PICT.
736%
737% The format of the ReadPICTImage method is:
738%
739% MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
740%
741% A description of each parameter follows:
742%
743% o magick: compare image format pattern against these bytes.
744%
745% o length: Specifies the length of the magick string.
746%
cristy3ed852e2009-09-05 21:47:34 +0000747*/
748static MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
749{
cristy80af6b62012-11-12 12:48:16 +0000750 if (length < 12)
751 return(MagickFalse);
752 /*
753 Embedded OLE2 macintosh have "PICT" instead of 512 platform header.
754 */
755 if (memcmp(magick,"PICT",4) == 0)
756 return(MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000757 if (length < 528)
758 return(MagickFalse);
759 if (memcmp(magick+522,"\000\021\002\377\014\000",6) == 0)
760 return(MagickTrue);
761 return(MagickFalse);
762}
763
764#if !defined(macintosh)
765/*
766%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
767% %
768% %
769% %
770% R e a d P I C T I m a g e %
771% %
772% %
773% %
774%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
775%
776% ReadPICTImage() reads an Apple Macintosh QuickDraw/PICT image file
777% and returns it. It allocates the memory necessary for the new Image
778% structure and returns a pointer to the new image.
779%
780% The format of the ReadPICTImage method is:
781%
782% Image *ReadPICTImage(const ImageInfo *image_info,
783% ExceptionInfo *exception)
784%
785% A description of each parameter follows:
786%
787% o image_info: the image info.
788%
789% o exception: return any errors or warnings in this structure.
790%
791*/
792
cristyc944c572014-05-12 00:19:16 +0000793static MagickBooleanType ReadRectangle(Image *image,PICTRectangle *rectangle)
794{
795 rectangle->top=(short) ReadBlobMSBShort(image);
796 rectangle->left=(short) ReadBlobMSBShort(image);
797 rectangle->bottom=(short) ReadBlobMSBShort(image);
798 rectangle->right=(short) ReadBlobMSBShort(image);
cristy6d62e4b2015-01-11 14:07:17 +0000799 if ((EOFBlob(image) != MagickFalse) || (rectangle->left > rectangle->right) ||
cristyc944c572014-05-12 00:19:16 +0000800 (rectangle->top > rectangle->bottom))
801 return(MagickFalse);
802 return(MagickTrue);
803}
804
cristy3ed852e2009-09-05 21:47:34 +0000805static Image *ReadPICTImage(const ImageInfo *image_info,
806 ExceptionInfo *exception)
807{
808 char
cristy151b66d2015-04-15 10:50:31 +0000809 geometry[MagickPathExtent],
cristy80af6b62012-11-12 12:48:16 +0000810 header_ole[4];
cristy3ed852e2009-09-05 21:47:34 +0000811
812 Image
813 *image;
814
cristy3ed852e2009-09-05 21:47:34 +0000815 int
816 c,
817 code;
818
cristy3ed852e2009-09-05 21:47:34 +0000819 MagickBooleanType
820 jpeg,
821 status;
822
823 PICTRectangle
824 frame;
825
826 PICTPixmap
827 pixmap;
828
cristy4c08aed2011-07-01 19:47:50 +0000829 Quantum
830 index;
cristy3ed852e2009-09-05 21:47:34 +0000831
cristy4c08aed2011-07-01 19:47:50 +0000832 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000833 *q;
834
cristybb503372010-05-27 20:51:26 +0000835 register ssize_t
cristy4c08aed2011-07-01 19:47:50 +0000836 i,
837 x;
cristy3ed852e2009-09-05 21:47:34 +0000838
839 size_t
840 extent,
841 length;
842
843 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000844 count,
845 flags,
846 j,
847 version,
848 y;
cristy3ed852e2009-09-05 21:47:34 +0000849
850 StringInfo
851 *profile;
852
853 /*
854 Open image file.
855 */
856 assert(image_info != (const ImageInfo *) NULL);
857 assert(image_info->signature == MagickSignature);
858 if (image_info->debug != MagickFalse)
859 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
860 image_info->filename);
861 assert(exception != (ExceptionInfo *) NULL);
862 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000863 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000864 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
865 if (status == MagickFalse)
866 {
867 image=DestroyImageList(image);
868 return((Image *) NULL);
869 }
870 /*
871 Read PICT header.
872 */
873 pixmap.bits_per_pixel=0;
874 pixmap.component_count=0;
cristy80af6b62012-11-12 12:48:16 +0000875 /*
876 Skip header : 512 for standard PICT and 4, ie "PICT" for OLE2.
877 */
878 header_ole[0]=ReadBlobByte(image);
879 header_ole[1]=ReadBlobByte(image);
880 header_ole[2]=ReadBlobByte(image);
881 header_ole[3]=ReadBlobByte(image);
882 if (!((header_ole[0] == 0x50) && (header_ole[1] == 0x49) &&
883 (header_ole[2] == 0x43) && (header_ole[3] == 0x54 )))
884 for (i=0; i < 508; i++)
cristye11ddbd2015-03-20 14:34:55 +0000885 if (ReadBlobByte(image) == EOF)
886 break;
cristy3ed852e2009-09-05 21:47:34 +0000887 (void) ReadBlobMSBShort(image); /* skip picture size */
cristyc944c572014-05-12 00:19:16 +0000888 if (ReadRectangle(image,&frame) == MagickFalse)
889 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000890 while ((c=ReadBlobByte(image)) == 0) ;
891 if (c != 0x11)
892 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
893 version=ReadBlobByte(image);
894 if (version == 2)
895 {
896 c=ReadBlobByte(image);
897 if (c != 0xff)
898 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
899 }
900 else
901 if (version != 1)
902 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
903 if ((frame.left < 0) || (frame.right < 0) || (frame.top < 0) ||
904 (frame.bottom < 0) || (frame.left >= frame.right) ||
905 (frame.top >= frame.bottom))
906 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
907 /*
908 Create black canvas.
909 */
910 flags=0;
cristy65cb9632012-12-08 00:34:29 +0000911 image->depth=8;
cristy3ed852e2009-09-05 21:47:34 +0000912 image->columns=1UL*(frame.right-frame.left);
913 image->rows=1UL*(frame.bottom-frame.top);
cristy2a11bef2011-10-28 18:33:11 +0000914 image->resolution.x=DefaultResolution;
915 image->resolution.y=DefaultResolution;
cristy3ed852e2009-09-05 21:47:34 +0000916 image->units=UndefinedResolution;
cristyd6d3b0d2015-01-01 17:46:31 +0000917 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
918 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
919 {
920 (void) CloseBlob(image);
921 return(GetFirstImageInList(image));
922 }
923 status=SetImageExtent(image,image->columns,image->rows,exception);
924 if (status == MagickFalse)
925 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +0000926 /*
927 Interpret PICT opcodes.
928 */
929 jpeg=MagickFalse;
930 for (code=0; EOFBlob(image) == MagickFalse; )
931 {
932 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
933 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
934 break;
935 if ((version == 1) || ((TellBlob(image) % 2) != 0))
936 code=ReadBlobByte(image);
937 if (version == 2)
938 code=(int) ReadBlobMSBShort(image);
cristyf9f85022014-05-18 12:46:29 +0000939 if (code < 0)
940 break;
cristy3ed852e2009-09-05 21:47:34 +0000941 if (code > 0xa1)
942 {
943 if (image->debug != MagickFalse)
944 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%04X:",code);
945 }
946 else
947 {
948 if (image->debug != MagickFalse)
949 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
950 " %04X %s: %s",code,codes[code].name,codes[code].description);
951 switch (code)
952 {
953 case 0x01:
954 {
955 /*
956 Clipping rectangle.
957 */
958 length=ReadBlobMSBShort(image);
959 if (length != 0x000a)
960 {
cristybb503372010-05-27 20:51:26 +0000961 for (i=0; i < (ssize_t) (length-2); i++)
cristye11ddbd2015-03-20 14:34:55 +0000962 if (ReadBlobByte(image) == EOF)
963 break;
cristy3ed852e2009-09-05 21:47:34 +0000964 break;
965 }
cristyc944c572014-05-12 00:19:16 +0000966 if (ReadRectangle(image,&frame) == MagickFalse)
967 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000968 if (((frame.left & 0x8000) != 0) || ((frame.top & 0x8000) != 0))
969 break;
970 image->columns=1UL*(frame.right-frame.left);
971 image->rows=1UL*(frame.bottom-frame.top);
cristyea1a8aa2011-10-20 13:24:06 +0000972 (void) SetImageBackgroundColor(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000973 break;
974 }
975 case 0x12:
976 case 0x13:
977 case 0x14:
978 {
cristybb503372010-05-27 20:51:26 +0000979 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000980 pattern;
981
cristybb503372010-05-27 20:51:26 +0000982 size_t
cristy3ed852e2009-09-05 21:47:34 +0000983 height,
984 width;
985
986 /*
987 Skip pattern definition.
988 */
989 pattern=1L*ReadBlobMSBShort(image);
990 for (i=0; i < 8; i++)
cristye11ddbd2015-03-20 14:34:55 +0000991 if (ReadBlobByte(image) == EOF)
992 break;
cristy3ed852e2009-09-05 21:47:34 +0000993 if (pattern == 2)
994 {
995 for (i=0; i < 5; i++)
cristye11ddbd2015-03-20 14:34:55 +0000996 if (ReadBlobByte(image) == EOF)
997 break;
cristy3ed852e2009-09-05 21:47:34 +0000998 break;
999 }
1000 if (pattern != 1)
1001 ThrowReaderException(CorruptImageError,"UnknownPatternType");
1002 length=ReadBlobMSBShort(image);
cristyc944c572014-05-12 00:19:16 +00001003 if (ReadRectangle(image,&frame) == MagickFalse)
1004 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +00001005 ReadPixmap(pixmap);
1006 image->depth=1UL*pixmap.component_size;
cristy2a11bef2011-10-28 18:33:11 +00001007 image->resolution.x=1.0*pixmap.horizontal_resolution;
1008 image->resolution.y=1.0*pixmap.vertical_resolution;
cristy3ed852e2009-09-05 21:47:34 +00001009 image->units=PixelsPerInchResolution;
1010 (void) ReadBlobMSBLong(image);
1011 flags=1L*ReadBlobMSBShort(image);
1012 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001013 for (i=0; i <= (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001014 (void) ReadBlobMSBLong(image);
1015 width=1UL*(frame.bottom-frame.top);
1016 height=1UL*(frame.right-frame.left);
1017 if (pixmap.bits_per_pixel <= 8)
1018 length&=0x7fff;
1019 if (pixmap.bits_per_pixel == 16)
1020 width<<=1;
1021 if (length == 0)
1022 length=width;
1023 if (length < 8)
1024 {
cristybb503372010-05-27 20:51:26 +00001025 for (i=0; i < (ssize_t) (length*height); i++)
cristye11ddbd2015-03-20 14:34:55 +00001026 if (ReadBlobByte(image) == EOF)
1027 break;
cristy3ed852e2009-09-05 21:47:34 +00001028 }
1029 else
1030 for (j=0; j < (int) height; j++)
1031 if (length > 200)
cristy46a54342015-03-29 00:16:16 +00001032 {
1033 for (j=0; j < (ssize_t) ReadBlobMSBShort(image); j++)
1034 if (ReadBlobByte(image) == EOF)
1035 break;
1036 }
cristy3ed852e2009-09-05 21:47:34 +00001037 else
cristybb503372010-05-27 20:51:26 +00001038 for (j=0; j < (ssize_t) ReadBlobByte(image); j++)
cristye11ddbd2015-03-20 14:34:55 +00001039 if (ReadBlobByte(image) == EOF)
1040 break;
cristy3ed852e2009-09-05 21:47:34 +00001041 break;
1042 }
1043 case 0x1b:
1044 {
1045 /*
1046 Initialize image background color.
1047 */
1048 image->background_color.red=(Quantum)
1049 ScaleShortToQuantum(ReadBlobMSBShort(image));
1050 image->background_color.green=(Quantum)
1051 ScaleShortToQuantum(ReadBlobMSBShort(image));
1052 image->background_color.blue=(Quantum)
1053 ScaleShortToQuantum(ReadBlobMSBShort(image));
1054 break;
1055 }
1056 case 0x70:
1057 case 0x71:
1058 case 0x72:
1059 case 0x73:
1060 case 0x74:
1061 case 0x75:
1062 case 0x76:
1063 case 0x77:
1064 {
1065 /*
1066 Skip polygon or region.
1067 */
1068 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001069 for (i=0; i < (ssize_t) (length-2); i++)
cristye11ddbd2015-03-20 14:34:55 +00001070 if (ReadBlobByte(image) == EOF)
1071 break;
cristy3ed852e2009-09-05 21:47:34 +00001072 break;
1073 }
1074 case 0x90:
1075 case 0x91:
1076 case 0x98:
1077 case 0x99:
1078 case 0x9a:
1079 case 0x9b:
1080 {
cristy4c08aed2011-07-01 19:47:50 +00001081 Image
1082 *tile_image;
cristy3ed852e2009-09-05 21:47:34 +00001083
1084 PICTRectangle
1085 source,
1086 destination;
1087
1088 register unsigned char
1089 *p;
1090
1091 size_t
1092 j;
1093
cristy4c08aed2011-07-01 19:47:50 +00001094 ssize_t
1095 bytes_per_line;
1096
cristy3ed852e2009-09-05 21:47:34 +00001097 unsigned char
1098 *pixels;
1099
cristy3ed852e2009-09-05 21:47:34 +00001100 /*
1101 Pixmap clipped by a rectangle.
1102 */
1103 bytes_per_line=0;
1104 if ((code != 0x9a) && (code != 0x9b))
1105 bytes_per_line=1L*ReadBlobMSBShort(image);
1106 else
1107 {
1108 (void) ReadBlobMSBShort(image);
1109 (void) ReadBlobMSBShort(image);
1110 (void) ReadBlobMSBShort(image);
1111 }
cristyc944c572014-05-12 00:19:16 +00001112 if (ReadRectangle(image,&frame) == MagickFalse)
1113 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +00001114 /*
1115 Initialize tile image.
1116 */
1117 tile_image=CloneImage(image,1UL*(frame.right-frame.left),
1118 1UL*(frame.bottom-frame.top),MagickTrue,exception);
1119 if (tile_image == (Image *) NULL)
1120 return((Image *) NULL);
1121 if ((code == 0x9a) || (code == 0x9b) ||
1122 ((bytes_per_line & 0x8000) != 0))
1123 {
1124 ReadPixmap(pixmap);
1125 tile_image->depth=1UL*pixmap.component_size;
cristy8a46d822012-08-28 23:32:39 +00001126 tile_image->alpha_trait=pixmap.component_count == 4 ?
cristyb0a657e2012-08-29 00:45:37 +00001127 BlendPixelTrait : UndefinedPixelTrait;
cristy2a11bef2011-10-28 18:33:11 +00001128 tile_image->resolution.x=(double) pixmap.horizontal_resolution;
1129 tile_image->resolution.y=(double) pixmap.vertical_resolution;
cristy3ed852e2009-09-05 21:47:34 +00001130 tile_image->units=PixelsPerInchResolution;
cristy17f11b02014-12-20 19:37:04 +00001131 if (tile_image->alpha_trait != UndefinedPixelTrait)
cristy8a46d822012-08-28 23:32:39 +00001132 image->alpha_trait=tile_image->alpha_trait;
cristy3ed852e2009-09-05 21:47:34 +00001133 }
1134 if ((code != 0x9a) && (code != 0x9b))
1135 {
1136 /*
1137 Initialize colormap.
1138 */
1139 tile_image->colors=2;
1140 if ((bytes_per_line & 0x8000) != 0)
1141 {
1142 (void) ReadBlobMSBLong(image);
1143 flags=1L*ReadBlobMSBShort(image);
1144 tile_image->colors=1UL*ReadBlobMSBShort(image)+1;
1145 }
cristy018f07f2011-09-04 21:15:19 +00001146 status=AcquireImageColormap(tile_image,tile_image->colors,
1147 exception);
cristy3ed852e2009-09-05 21:47:34 +00001148 if (status == MagickFalse)
1149 {
1150 tile_image=DestroyImage(tile_image);
1151 ThrowReaderException(ResourceLimitError,
1152 "MemoryAllocationFailed");
1153 }
1154 if ((bytes_per_line & 0x8000) != 0)
1155 {
cristybb503372010-05-27 20:51:26 +00001156 for (i=0; i < (ssize_t) tile_image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001157 {
1158 j=ReadBlobMSBShort(image) % tile_image->colors;
1159 if ((flags & 0x8000) != 0)
1160 j=(size_t) i;
1161 tile_image->colormap[j].red=(Quantum)
1162 ScaleShortToQuantum(ReadBlobMSBShort(image));
1163 tile_image->colormap[j].green=(Quantum)
1164 ScaleShortToQuantum(ReadBlobMSBShort(image));
1165 tile_image->colormap[j].blue=(Quantum)
1166 ScaleShortToQuantum(ReadBlobMSBShort(image));
1167 }
1168 }
1169 else
1170 {
cristybb503372010-05-27 20:51:26 +00001171 for (i=0; i < (ssize_t) tile_image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001172 {
1173 tile_image->colormap[i].red=(Quantum) (QuantumRange-
1174 tile_image->colormap[i].red);
1175 tile_image->colormap[i].green=(Quantum) (QuantumRange-
1176 tile_image->colormap[i].green);
1177 tile_image->colormap[i].blue=(Quantum) (QuantumRange-
1178 tile_image->colormap[i].blue);
1179 }
1180 }
1181 }
cristyc944c572014-05-12 00:19:16 +00001182 if (ReadRectangle(image,&source) == MagickFalse)
1183 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1184 if (ReadRectangle(image,&destination) == MagickFalse)
1185 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +00001186 (void) ReadBlobMSBShort(image);
1187 if ((code == 0x91) || (code == 0x99) || (code == 0x9b))
1188 {
1189 /*
1190 Skip region.
1191 */
1192 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001193 for (i=0; i < (ssize_t) (length-2); i++)
cristye11ddbd2015-03-20 14:34:55 +00001194 if (ReadBlobByte(image) == EOF)
1195 break;
cristy3ed852e2009-09-05 21:47:34 +00001196 }
1197 if ((code != 0x9a) && (code != 0x9b) &&
1198 (bytes_per_line & 0x8000) == 0)
cristyc82a27b2011-10-21 01:07:16 +00001199 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1,&extent,
1200 exception);
cristy3ed852e2009-09-05 21:47:34 +00001201 else
1202 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1U*
cristyc82a27b2011-10-21 01:07:16 +00001203 pixmap.bits_per_pixel,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +00001204 if (pixels == (unsigned char *) NULL)
1205 {
1206 tile_image=DestroyImage(tile_image);
1207 ThrowReaderException(ResourceLimitError,
1208 "MemoryAllocationFailed");
1209 }
1210 /*
1211 Convert PICT tile image to pixel packets.
1212 */
1213 p=pixels;
cristybb503372010-05-27 20:51:26 +00001214 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001215 {
1216 if (p > (pixels+extent+image->columns))
1217 ThrowReaderException(CorruptImageError,"NotEnoughPixelData");
cristy3669d042009-10-16 01:36:31 +00001218 q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,
1219 exception);
cristyacd2ed22011-08-30 01:44:23 +00001220 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001221 break;
cristybb503372010-05-27 20:51:26 +00001222 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001223 {
1224 if (tile_image->storage_class == PseudoClass)
1225 {
cristyc82a27b2011-10-21 01:07:16 +00001226 index=ConstrainColormapIndex(tile_image,*p,exception);
cristy4c08aed2011-07-01 19:47:50 +00001227 SetPixelIndex(tile_image,index,q);
1228 SetPixelRed(tile_image,
1229 tile_image->colormap[(ssize_t) index].red,q);
1230 SetPixelGreen(tile_image,
1231 tile_image->colormap[(ssize_t) index].green,q);
1232 SetPixelBlue(tile_image,
1233 tile_image->colormap[(ssize_t) index].blue,q);
cristy3ed852e2009-09-05 21:47:34 +00001234 }
1235 else
1236 {
1237 if (pixmap.bits_per_pixel == 16)
1238 {
1239 i=(*p++);
1240 j=(*p);
cristy4c08aed2011-07-01 19:47:50 +00001241 SetPixelRed(tile_image,ScaleCharToQuantum(
1242 (unsigned char) ((i & 0x7c) << 1)),q);
1243 SetPixelGreen(tile_image,ScaleCharToQuantum(
cristyaff6d802011-04-26 01:46:31 +00001244 (unsigned char) (((i & 0x03) << 6) |
cristy4c08aed2011-07-01 19:47:50 +00001245 ((j & 0xe0) >> 2))),q);
1246 SetPixelBlue(tile_image,ScaleCharToQuantum(
1247 (unsigned char) ((j & 0x1f) << 3)),q);
cristy3ed852e2009-09-05 21:47:34 +00001248 }
1249 else
cristy17f11b02014-12-20 19:37:04 +00001250 if (tile_image->alpha_trait == UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00001251 {
1252 if (p > (pixels+extent+2*image->columns))
1253 ThrowReaderException(CorruptImageError,
1254 "NotEnoughPixelData");
cristy4c08aed2011-07-01 19:47:50 +00001255 SetPixelRed(tile_image,ScaleCharToQuantum(*p),q);
1256 SetPixelGreen(tile_image,ScaleCharToQuantum(
1257 *(p+tile_image->columns)),q);
1258 SetPixelBlue(tile_image,ScaleCharToQuantum(
1259 *(p+2*tile_image->columns)),q);
cristy3ed852e2009-09-05 21:47:34 +00001260 }
1261 else
1262 {
1263 if (p > (pixels+extent+3*image->columns))
1264 ThrowReaderException(CorruptImageError,
1265 "NotEnoughPixelData");
cristy4c08aed2011-07-01 19:47:50 +00001266 SetPixelAlpha(tile_image,ScaleCharToQuantum(*p),q);
1267 SetPixelRed(tile_image,ScaleCharToQuantum(
1268 *(p+tile_image->columns)),q);
1269 SetPixelGreen(tile_image,ScaleCharToQuantum(
1270 *(p+2*tile_image->columns)),q);
1271 SetPixelBlue(tile_image,ScaleCharToQuantum(
1272 *(p+3*tile_image->columns)),q);
cristy3ed852e2009-09-05 21:47:34 +00001273 }
1274 }
1275 p++;
cristyed231572011-07-14 02:18:59 +00001276 q+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00001277 }
1278 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
1279 break;
1280 if ((tile_image->storage_class == DirectClass) &&
1281 (pixmap.bits_per_pixel != 16))
1282 {
1283 p+=(pixmap.component_count-1)*tile_image->columns;
1284 if (p < pixels)
1285 break;
1286 }
1287 status=SetImageProgress(image,LoadImageTag,y,tile_image->rows);
1288 if (status == MagickFalse)
1289 break;
1290 }
1291 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1292 if (jpeg == MagickFalse)
1293 if ((code == 0x9a) || (code == 0x9b) ||
1294 ((bytes_per_line & 0x8000) != 0))
cristyfeb3e962012-03-29 17:25:55 +00001295 (void) CompositeImage(image,tile_image,CopyCompositeOp,
cristy39172402012-03-30 13:04:39 +00001296 MagickTrue,destination.left,destination.top,exception);
cristy3ed852e2009-09-05 21:47:34 +00001297 tile_image=DestroyImage(tile_image);
1298 break;
1299 }
1300 case 0xa1:
1301 {
1302 unsigned char
1303 *info;
1304
cristybb503372010-05-27 20:51:26 +00001305 size_t
cristy3ed852e2009-09-05 21:47:34 +00001306 type;
1307
1308 /*
1309 Comment.
1310 */
1311 type=ReadBlobMSBShort(image);
1312 length=ReadBlobMSBShort(image);
1313 if (length == 0)
1314 break;
1315 (void) ReadBlobMSBLong(image);
1316 length-=4;
1317 if (length == 0)
1318 break;
1319 info=(unsigned char *) AcquireQuantumMemory(length,sizeof(*info));
1320 if (info == (unsigned char *) NULL)
1321 break;
1322 count=ReadBlob(image,length,info);
cristy6d62e4b2015-01-11 14:07:17 +00001323 if (count != (ssize_t) length)
1324 ThrowReaderException(ResourceLimitError,"UnableToReadImageData");
cristy3ed852e2009-09-05 21:47:34 +00001325 switch (type)
1326 {
1327 case 0xe0:
1328 {
1329 if (length == 0)
1330 break;
cristy8723e4b2011-09-01 13:11:19 +00001331 profile=BlobToStringInfo((const void *) NULL,length);
cristy3ed852e2009-09-05 21:47:34 +00001332 SetStringInfoDatum(profile,info);
cristyd15e6592011-10-15 00:13:06 +00001333 status=SetImageProfile(image,"icc",profile,exception);
cristy3ed852e2009-09-05 21:47:34 +00001334 profile=DestroyStringInfo(profile);
1335 if (status == MagickFalse)
1336 ThrowReaderException(ResourceLimitError,
1337 "MemoryAllocationFailed");
1338 break;
1339 }
1340 case 0x1f2:
1341 {
1342 if (length == 0)
1343 break;
cristy8723e4b2011-09-01 13:11:19 +00001344 profile=BlobToStringInfo((const void *) NULL,length);
cristy3ed852e2009-09-05 21:47:34 +00001345 SetStringInfoDatum(profile,info);
cristyd15e6592011-10-15 00:13:06 +00001346 status=SetImageProfile(image,"iptc",profile,exception);
cristy3ed852e2009-09-05 21:47:34 +00001347 if (status == MagickFalse)
1348 ThrowReaderException(ResourceLimitError,
1349 "MemoryAllocationFailed");
1350 profile=DestroyStringInfo(profile);
1351 break;
1352 }
1353 default:
1354 break;
1355 }
1356 info=(unsigned char *) RelinquishMagickMemory(info);
1357 break;
1358 }
1359 default:
1360 {
1361 /*
1362 Skip to next op code.
1363 */
cristy7163b3e2014-05-24 22:18:20 +00001364 if (code < 0)
1365 break;
cristy3ed852e2009-09-05 21:47:34 +00001366 if (codes[code].length == -1)
1367 (void) ReadBlobMSBShort(image);
1368 else
cristybb503372010-05-27 20:51:26 +00001369 for (i=0; i < (ssize_t) codes[code].length; i++)
cristye11ddbd2015-03-20 14:34:55 +00001370 if (ReadBlobByte(image) == EOF)
1371 break;
cristy3ed852e2009-09-05 21:47:34 +00001372 }
1373 }
1374 }
1375 if (code == 0xc00)
1376 {
1377 /*
1378 Skip header.
1379 */
1380 for (i=0; i < 24; i++)
cristye11ddbd2015-03-20 14:34:55 +00001381 if (ReadBlobByte(image) == EOF)
1382 break;
cristy3ed852e2009-09-05 21:47:34 +00001383 continue;
1384 }
1385 if (((code >= 0xb0) && (code <= 0xcf)) ||
1386 ((code >= 0x8000) && (code <= 0x80ff)))
1387 continue;
1388 if (code == 0x8200)
1389 {
1390 FILE
1391 *file;
1392
1393 Image
1394 *tile_image;
1395
1396 ImageInfo
1397 *read_info;
1398
1399 int
1400 unique_file;
1401
1402 /*
1403 Embedded JPEG.
1404 */
1405 jpeg=MagickTrue;
1406 read_info=CloneImageInfo(image_info);
1407 SetImageInfoBlob(read_info,(void *) NULL,0);
1408 file=(FILE *) NULL;
1409 unique_file=AcquireUniqueFileResource(read_info->filename);
1410 if (unique_file != -1)
cristyd0305b92009-10-19 13:12:21 +00001411 file=fdopen(unique_file,"wb");
cristy3ed852e2009-09-05 21:47:34 +00001412 if ((unique_file == -1) || (file == (FILE *) NULL))
1413 {
dirkce6b1222014-11-06 21:09:46 +00001414 (void) RelinquishUniqueFileResource(read_info->filename);
cristy3ed852e2009-09-05 21:47:34 +00001415 (void) CopyMagickString(image->filename,read_info->filename,
cristy151b66d2015-04-15 10:50:31 +00001416 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001417 ThrowFileException(exception,FileOpenError,
1418 "UnableToCreateTemporaryFile",image->filename);
1419 image=DestroyImageList(image);
1420 return((Image *) NULL);
1421 }
1422 length=ReadBlobMSBLong(image);
cristye11ddbd2015-03-20 14:34:55 +00001423 if (length > 154)
cristyc944c572014-05-12 00:19:16 +00001424 {
cristye11ddbd2015-03-20 14:34:55 +00001425 for (i=0; i < 6; i++)
1426 (void) ReadBlobMSBLong(image);
1427 if (ReadRectangle(image,&frame) == MagickFalse)
1428 {
1429 (void) fclose(file);
1430 (void) RelinquishUniqueFileResource(read_info->filename);
1431 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1432 }
1433 for (i=0; i < 122; i++)
1434 if (ReadBlobByte(image) == EOF)
1435 break;
1436 for (i=0; i < (ssize_t) (length-154); i++)
1437 {
1438 c=ReadBlobByte(image);
1439 if (c == EOF)
1440 break;
1441 (void) fputc(c,file);
1442 }
cristyc944c572014-05-12 00:19:16 +00001443 }
cristy3ed852e2009-09-05 21:47:34 +00001444 (void) fclose(file);
cristy100a0562014-04-18 01:27:37 +00001445 (void) close(unique_file);
cristy3ed852e2009-09-05 21:47:34 +00001446 tile_image=ReadImage(read_info,exception);
1447 (void) RelinquishUniqueFileResource(read_info->filename);
1448 read_info=DestroyImageInfo(read_info);
1449 if (tile_image == (Image *) NULL)
1450 continue;
cristy151b66d2015-04-15 10:50:31 +00001451 (void) FormatLocaleString(geometry,MagickPathExtent,"%.20gx%.20g",
cristye8c25f92010-06-03 00:53:06 +00001452 (double) MagickMax(image->columns,tile_image->columns),
1453 (double) MagickMax(image->rows,tile_image->rows));
cristya0511732010-02-17 02:38:35 +00001454 (void) SetImageExtent(image,
1455 MagickMax(image->columns,tile_image->columns),
cristy63240882011-08-05 19:05:27 +00001456 MagickMax(image->rows,tile_image->rows),exception);
cristyaf8d3912014-02-21 14:50:33 +00001457 (void) TransformImageColorspace(image,tile_image->colorspace,exception);
cristy39172402012-03-30 13:04:39 +00001458 (void) CompositeImage(image,tile_image,CopyCompositeOp,MagickTrue,
cristyfeb3e962012-03-29 17:25:55 +00001459 frame.left,frame.right,exception);
cristy3ed852e2009-09-05 21:47:34 +00001460 image->compression=tile_image->compression;
1461 tile_image=DestroyImage(tile_image);
1462 continue;
1463 }
1464 if ((code == 0xff) || (code == 0xffff))
1465 break;
1466 if (((code >= 0xd0) && (code <= 0xfe)) ||
1467 ((code >= 0x8100) && (code <= 0xffff)))
1468 {
1469 /*
1470 Skip reserved.
1471 */
1472 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001473 for (i=0; i < (ssize_t) length; i++)
cristye11ddbd2015-03-20 14:34:55 +00001474 if (ReadBlobByte(image) == EOF)
1475 break;
cristy3ed852e2009-09-05 21:47:34 +00001476 continue;
1477 }
1478 if ((code >= 0x100) && (code <= 0x7fff))
1479 {
1480 /*
1481 Skip reserved.
1482 */
1483 length=(size_t) ((code >> 7) & 0xff);
cristybb503372010-05-27 20:51:26 +00001484 for (i=0; i < (ssize_t) length; i++)
cristye11ddbd2015-03-20 14:34:55 +00001485 if (ReadBlobByte(image) == EOF)
1486 break;
cristy3ed852e2009-09-05 21:47:34 +00001487 continue;
1488 }
1489 }
1490 (void) CloseBlob(image);
1491 return(GetFirstImageInList(image));
1492}
1493#endif
1494
1495/*
1496%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1497% %
1498% %
1499% %
1500% R e g i s t e r P I C T I m a g e %
1501% %
1502% %
1503% %
1504%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1505%
1506% RegisterPICTImage() adds attributes for the PICT image format to
1507% the list of supported formats. The attributes include the image format
1508% tag, a method to read and/or write the format, whether the format
1509% supports the saving of more than one frame to the same file or blob,
1510% whether the format supports native in-memory I/O, and a brief
1511% description of the format.
1512%
1513% The format of the RegisterPICTImage method is:
1514%
cristybb503372010-05-27 20:51:26 +00001515% size_t RegisterPICTImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001516%
1517*/
cristybb503372010-05-27 20:51:26 +00001518ModuleExport size_t RegisterPICTImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001519{
1520 MagickInfo
1521 *entry;
1522
dirk06b627a2015-04-06 18:59:17 +00001523 entry=AcquireMagickInfo("PICT","PCT","Apple Macintosh QuickDraw/PICT");
cristy3ed852e2009-09-05 21:47:34 +00001524 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1525 entry->encoder=(EncodeImageHandler *) WritePICTImage;
dirk08e9a112015-02-22 01:51:41 +00001526 entry->flags^=CoderAdjoinFlag;
1527 entry->flags|=CoderSeekableStreamFlag;
cristy3ed852e2009-09-05 21:47:34 +00001528 entry->magick=(IsImageFormatHandler *) IsPICT;
cristy3ed852e2009-09-05 21:47:34 +00001529 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +00001530 entry=AcquireMagickInfo("PICT","PICT","Apple Macintosh QuickDraw/PICT");
cristy3ed852e2009-09-05 21:47:34 +00001531 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1532 entry->encoder=(EncodeImageHandler *) WritePICTImage;
dirk08e9a112015-02-22 01:51:41 +00001533 entry->flags^=CoderAdjoinFlag;
1534 entry->flags|=CoderSeekableStreamFlag;
cristy3ed852e2009-09-05 21:47:34 +00001535 entry->magick=(IsImageFormatHandler *) IsPICT;
cristy3ed852e2009-09-05 21:47:34 +00001536 (void) RegisterMagickInfo(entry);
1537 return(MagickImageCoderSignature);
1538}
1539
1540/*
1541%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1542% %
1543% %
1544% %
1545% U n r e g i s t e r P I C T I m a g e %
1546% %
1547% %
1548% %
1549%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1550%
1551% UnregisterPICTImage() removes format registrations made by the
1552% PICT module from the list of supported formats.
1553%
1554% The format of the UnregisterPICTImage method is:
1555%
1556% UnregisterPICTImage(void)
1557%
1558*/
1559ModuleExport void UnregisterPICTImage(void)
1560{
1561 (void) UnregisterMagickInfo("PCT");
1562 (void) UnregisterMagickInfo("PICT");
1563}
1564
1565/*
1566%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1567% %
1568% %
1569% %
1570% W r i t e P I C T I m a g e %
1571% %
1572% %
1573% %
1574%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1575%
1576% WritePICTImage() writes an image to a file in the Apple Macintosh
1577% QuickDraw/PICT image format.
1578%
1579% The format of the WritePICTImage method is:
1580%
cristy3669d042009-10-16 01:36:31 +00001581% MagickBooleanType WritePICTImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +00001582% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001583%
1584% A description of each parameter follows.
1585%
1586% o image_info: the image info.
1587%
1588% o image: The image.
1589%
cristy1e178e72011-08-28 19:44:34 +00001590% o exception: return any errors or warnings in this structure.
1591%
cristy3ed852e2009-09-05 21:47:34 +00001592*/
1593static MagickBooleanType WritePICTImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +00001594 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001595{
1596#define MaxCount 128
1597#define PictCropRegionOp 0x01
1598#define PictEndOfPictureOp 0xff
1599#define PictJPEGOp 0x8200
1600#define PictInfoOp 0x0C00
1601#define PictInfoSize 512
1602#define PictPixmapOp 0x9A
1603#define PictPICTOp 0x98
1604#define PictVersion 0x11
1605
1606 const StringInfo
1607 *profile;
1608
1609 double
1610 x_resolution,
1611 y_resolution;
1612
cristy3ed852e2009-09-05 21:47:34 +00001613 MagickBooleanType
1614 status;
1615
1616 MagickOffsetType
1617 offset;
1618
1619 PICTPixmap
1620 pixmap;
1621
1622 PICTRectangle
1623 bounds,
1624 crop_rectangle,
1625 destination_rectangle,
1626 frame_rectangle,
1627 size_rectangle,
1628 source_rectangle;
1629
cristy4c08aed2011-07-01 19:47:50 +00001630 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001631 *p;
1632
cristybb503372010-05-27 20:51:26 +00001633 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001634 i,
1635 x;
1636
1637 size_t
cristyaff6d802011-04-26 01:46:31 +00001638 bytes_per_line,
1639 count,
cristy0f6fc2d2015-05-30 00:49:11 +00001640 row_bytes,
cristyaff6d802011-04-26 01:46:31 +00001641 storage_class;
1642
1643 ssize_t
1644 y;
cristy3ed852e2009-09-05 21:47:34 +00001645
1646 unsigned char
1647 *buffer,
1648 *packed_scanline,
1649 *scanline;
1650
cristy3ed852e2009-09-05 21:47:34 +00001651 unsigned short
1652 base_address,
cristy3ed852e2009-09-05 21:47:34 +00001653 transfer_mode;
1654
1655 /*
1656 Open output image file.
1657 */
1658 assert(image_info != (const ImageInfo *) NULL);
1659 assert(image_info->signature == MagickSignature);
1660 assert(image != (Image *) NULL);
1661 assert(image->signature == MagickSignature);
1662 if (image->debug != MagickFalse)
1663 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1664 if ((image->columns > 65535L) || (image->rows > 65535L))
1665 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
cristy3a37efd2011-08-28 20:31:03 +00001666 assert(exception != (ExceptionInfo *) NULL);
1667 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +00001668 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001669 if (status == MagickFalse)
1670 return(status);
cristyaf8d3912014-02-21 14:50:33 +00001671 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001672 /*
1673 Initialize image info.
1674 */
1675 size_rectangle.top=0;
1676 size_rectangle.left=0;
1677 size_rectangle.bottom=(short) image->rows;
1678 size_rectangle.right=(short) image->columns;
1679 frame_rectangle=size_rectangle;
1680 crop_rectangle=size_rectangle;
1681 source_rectangle=size_rectangle;
1682 destination_rectangle=size_rectangle;
1683 base_address=0xff;
cristy0f6fc2d2015-05-30 00:49:11 +00001684 row_bytes=image->columns;
cristy3ed852e2009-09-05 21:47:34 +00001685 bounds.top=0;
1686 bounds.left=0;
1687 bounds.bottom=(short) image->rows;
1688 bounds.right=(short) image->columns;
1689 pixmap.version=0;
1690 pixmap.pack_type=0;
1691 pixmap.pack_size=0;
1692 pixmap.pixel_type=0;
1693 pixmap.bits_per_pixel=8;
1694 pixmap.component_count=1;
1695 pixmap.component_size=8;
1696 pixmap.plane_bytes=0;
1697 pixmap.table=0;
1698 pixmap.reserved=0;
1699 transfer_mode=0;
cristy2a11bef2011-10-28 18:33:11 +00001700 x_resolution=image->resolution.x != 0.0 ? image->resolution.x :
cristy3ed852e2009-09-05 21:47:34 +00001701 DefaultResolution;
cristy2a11bef2011-10-28 18:33:11 +00001702 y_resolution=image->resolution.y != 0.0 ? image->resolution.y :
cristy3ed852e2009-09-05 21:47:34 +00001703 DefaultResolution;
1704 storage_class=image->storage_class;
1705 if (image_info->compression == JPEGCompression)
1706 storage_class=DirectClass;
cristy213770a2014-09-27 00:12:23 +00001707 if (storage_class == DirectClass)
cristy3ed852e2009-09-05 21:47:34 +00001708 {
cristy17f11b02014-12-20 19:37:04 +00001709 pixmap.component_count=image->alpha_trait != UndefinedPixelTrait ? 4 : 3;
cristy3ed852e2009-09-05 21:47:34 +00001710 pixmap.pixel_type=16;
1711 pixmap.bits_per_pixel=32;
1712 pixmap.pack_type=0x04;
1713 transfer_mode=0x40;
cristy0f6fc2d2015-05-30 00:49:11 +00001714 row_bytes=4*image->columns;
cristy3ed852e2009-09-05 21:47:34 +00001715 }
1716 /*
1717 Allocate memory.
1718 */
1719 bytes_per_line=image->columns;
cristy213770a2014-09-27 00:12:23 +00001720 if (storage_class == DirectClass)
cristy17f11b02014-12-20 19:37:04 +00001721 bytes_per_line*=image->alpha_trait != UndefinedPixelTrait ? 4 : 3;
cristy3ed852e2009-09-05 21:47:34 +00001722 buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer));
1723 packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t)
1724 (row_bytes+MaxCount),sizeof(*packed_scanline));
1725 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
1726 if ((buffer == (unsigned char *) NULL) ||
1727 (packed_scanline == (unsigned char *) NULL) ||
1728 (scanline == (unsigned char *) NULL))
1729 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1730 (void) ResetMagickMemory(scanline,0,row_bytes);
1731 (void) ResetMagickMemory(packed_scanline,0,(size_t) (row_bytes+MaxCount));
1732 /*
1733 Write header, header size, size bounding box, version, and reserved.
1734 */
1735 (void) ResetMagickMemory(buffer,0,PictInfoSize);
1736 (void) WriteBlob(image,PictInfoSize,buffer);
1737 (void) WriteBlobMSBShort(image,0);
1738 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top);
1739 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left);
1740 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom);
1741 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right);
1742 (void) WriteBlobMSBShort(image,PictVersion);
1743 (void) WriteBlobMSBShort(image,0x02ff); /* version #2 */
1744 (void) WriteBlobMSBShort(image,PictInfoOp);
1745 (void) WriteBlobMSBLong(image,0xFFFE0000UL);
1746 /*
1747 Write full size of the file, resolution, frame bounding box, and reserved.
1748 */
1749 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1750 (void) WriteBlobMSBShort(image,0x0000);
1751 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1752 (void) WriteBlobMSBShort(image,0x0000);
1753 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top);
1754 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left);
1755 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom);
1756 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right);
1757 (void) WriteBlobMSBLong(image,0x00000000L);
1758 profile=GetImageProfile(image,"iptc");
1759 if (profile != (StringInfo *) NULL)
1760 {
1761 (void) WriteBlobMSBShort(image,0xa1);
1762 (void) WriteBlobMSBShort(image,0x1f2);
cristyed552522009-10-16 14:04:35 +00001763 (void) WriteBlobMSBShort(image,(unsigned short)
1764 (GetStringInfoLength(profile)+4));
cristy3ed852e2009-09-05 21:47:34 +00001765 (void) WriteBlobString(image,"8BIM");
cristyed552522009-10-16 14:04:35 +00001766 (void) WriteBlob(image,GetStringInfoLength(profile),
1767 GetStringInfoDatum(profile));
cristy3ed852e2009-09-05 21:47:34 +00001768 }
1769 profile=GetImageProfile(image,"icc");
1770 if (profile != (StringInfo *) NULL)
1771 {
1772 (void) WriteBlobMSBShort(image,0xa1);
1773 (void) WriteBlobMSBShort(image,0xe0);
cristyed552522009-10-16 14:04:35 +00001774 (void) WriteBlobMSBShort(image,(unsigned short)
1775 (GetStringInfoLength(profile)+4));
cristy3ed852e2009-09-05 21:47:34 +00001776 (void) WriteBlobMSBLong(image,0x00000000UL);
cristyed552522009-10-16 14:04:35 +00001777 (void) WriteBlob(image,GetStringInfoLength(profile),
1778 GetStringInfoDatum(profile));
cristy3ed852e2009-09-05 21:47:34 +00001779 (void) WriteBlobMSBShort(image,0xa1);
1780 (void) WriteBlobMSBShort(image,0xe0);
1781 (void) WriteBlobMSBShort(image,4);
1782 (void) WriteBlobMSBLong(image,0x00000002UL);
1783 }
1784 /*
1785 Write crop region opcode and crop bounding box.
1786 */
1787 (void) WriteBlobMSBShort(image,PictCropRegionOp);
1788 (void) WriteBlobMSBShort(image,0xa);
1789 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top);
1790 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left);
1791 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom);
1792 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right);
1793 if (image_info->compression == JPEGCompression)
1794 {
1795 Image
1796 *jpeg_image;
1797
1798 ImageInfo
1799 *jpeg_info;
1800
1801 size_t
1802 length;
1803
1804 unsigned char
1805 *blob;
1806
cristy1e178e72011-08-28 19:44:34 +00001807 jpeg_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001808 if (jpeg_image == (Image *) NULL)
1809 {
1810 (void) CloseBlob(image);
1811 return(MagickFalse);
1812 }
1813 jpeg_info=CloneImageInfo(image_info);
cristy151b66d2015-04-15 10:50:31 +00001814 (void) CopyMagickString(jpeg_info->magick,"JPEG",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001815 length=0;
1816 blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length,
cristy1e178e72011-08-28 19:44:34 +00001817 exception);
cristy3ed852e2009-09-05 21:47:34 +00001818 jpeg_info=DestroyImageInfo(jpeg_info);
1819 if (blob == (unsigned char *) NULL)
1820 return(MagickFalse);
1821 jpeg_image=DestroyImage(jpeg_image);
1822 (void) WriteBlobMSBShort(image,PictJPEGOp);
cristyeaedf062010-05-29 22:36:02 +00001823 (void) WriteBlobMSBLong(image,(unsigned int) length+154);
cristy3ed852e2009-09-05 21:47:34 +00001824 (void) WriteBlobMSBShort(image,0x0000);
1825 (void) WriteBlobMSBLong(image,0x00010000UL);
1826 (void) WriteBlobMSBLong(image,0x00000000UL);
1827 (void) WriteBlobMSBLong(image,0x00000000UL);
1828 (void) WriteBlobMSBLong(image,0x00000000UL);
1829 (void) WriteBlobMSBLong(image,0x00010000UL);
1830 (void) WriteBlobMSBLong(image,0x00000000UL);
1831 (void) WriteBlobMSBLong(image,0x00000000UL);
1832 (void) WriteBlobMSBLong(image,0x00000000UL);
1833 (void) WriteBlobMSBLong(image,0x40000000UL);
1834 (void) WriteBlobMSBLong(image,0x00000000UL);
1835 (void) WriteBlobMSBLong(image,0x00000000UL);
1836 (void) WriteBlobMSBLong(image,0x00000000UL);
1837 (void) WriteBlobMSBLong(image,0x00400000UL);
1838 (void) WriteBlobMSBShort(image,0x0000);
1839 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1840 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1841 (void) WriteBlobMSBShort(image,0x0000);
1842 (void) WriteBlobMSBShort(image,768);
1843 (void) WriteBlobMSBShort(image,0x0000);
1844 (void) WriteBlobMSBLong(image,0x00000000UL);
1845 (void) WriteBlobMSBLong(image,0x00566A70UL);
1846 (void) WriteBlobMSBLong(image,0x65670000UL);
1847 (void) WriteBlobMSBLong(image,0x00000000UL);
1848 (void) WriteBlobMSBLong(image,0x00000001UL);
1849 (void) WriteBlobMSBLong(image,0x00016170UL);
1850 (void) WriteBlobMSBLong(image,0x706C0000UL);
1851 (void) WriteBlobMSBLong(image,0x00000000UL);
1852 (void) WriteBlobMSBShort(image,768);
1853 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1854 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1855 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1856 (void) WriteBlobMSBShort(image,0x0000);
1857 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1858 (void) WriteBlobMSBLong(image,0x00000000UL);
1859 (void) WriteBlobMSBLong(image,0x87AC0001UL);
1860 (void) WriteBlobMSBLong(image,0x0B466F74UL);
1861 (void) WriteBlobMSBLong(image,0x6F202D20UL);
1862 (void) WriteBlobMSBLong(image,0x4A504547UL);
1863 (void) WriteBlobMSBLong(image,0x00000000UL);
1864 (void) WriteBlobMSBLong(image,0x00000000UL);
1865 (void) WriteBlobMSBLong(image,0x00000000UL);
1866 (void) WriteBlobMSBLong(image,0x00000000UL);
1867 (void) WriteBlobMSBLong(image,0x00000000UL);
1868 (void) WriteBlobMSBLong(image,0x0018FFFFUL);
1869 (void) WriteBlob(image,length,blob);
1870 if ((length & 0x01) != 0)
1871 (void) WriteBlobByte(image,'\0');
1872 blob=(unsigned char *) RelinquishMagickMemory(blob);
1873 }
1874 /*
1875 Write picture opcode, row bytes, and picture bounding box, and version.
1876 */
1877 if (storage_class == PseudoClass)
1878 (void) WriteBlobMSBShort(image,PictPICTOp);
1879 else
1880 {
1881 (void) WriteBlobMSBShort(image,PictPixmapOp);
cristybb503372010-05-27 20:51:26 +00001882 (void) WriteBlobMSBLong(image,(size_t) base_address);
cristy3ed852e2009-09-05 21:47:34 +00001883 }
1884 (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000));
1885 (void) WriteBlobMSBShort(image,(unsigned short) bounds.top);
1886 (void) WriteBlobMSBShort(image,(unsigned short) bounds.left);
1887 (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom);
1888 (void) WriteBlobMSBShort(image,(unsigned short) bounds.right);
1889 /*
1890 Write pack type, pack size, resolution, pixel type, and pixel size.
1891 */
1892 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version);
1893 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type);
cristyeaedf062010-05-29 22:36:02 +00001894 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.pack_size);
cristy3ed852e2009-09-05 21:47:34 +00001895 (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5));
1896 (void) WriteBlobMSBShort(image,0x0000);
1897 (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5));
1898 (void) WriteBlobMSBShort(image,0x0000);
1899 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type);
1900 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel);
1901 /*
1902 Write component count, size, plane bytes, table size, and reserved.
1903 */
1904 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count);
1905 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size);
cristyeaedf062010-05-29 22:36:02 +00001906 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.plane_bytes);
1907 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.table);
1908 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.reserved);
cristy3ed852e2009-09-05 21:47:34 +00001909 if (storage_class == PseudoClass)
1910 {
1911 /*
1912 Write image colormap.
1913 */
1914 (void) WriteBlobMSBLong(image,0x00000000L); /* color seed */
1915 (void) WriteBlobMSBShort(image,0L); /* color flags */
1916 (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1));
cristybb503372010-05-27 20:51:26 +00001917 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001918 {
1919 (void) WriteBlobMSBShort(image,(unsigned short) i);
1920 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1921 image->colormap[i].red));
1922 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1923 image->colormap[i].green));
1924 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1925 image->colormap[i].blue));
1926 }
1927 }
1928 /*
1929 Write source and destination rectangle.
1930 */
1931 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top);
1932 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left);
1933 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom);
1934 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right);
1935 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top);
1936 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left);
1937 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom);
1938 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right);
1939 (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode);
1940 /*
1941 Write picture data.
1942 */
1943 count=0;
cristy213770a2014-09-27 00:12:23 +00001944 if (storage_class == PseudoClass)
cristybb503372010-05-27 20:51:26 +00001945 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001946 {
cristy1e178e72011-08-28 19:44:34 +00001947 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001948 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001949 break;
cristybb503372010-05-27 20:51:26 +00001950 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001951 {
1952 scanline[x]=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001953 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001954 }
cristybb503372010-05-27 20:51:26 +00001955 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
cristy3ed852e2009-09-05 21:47:34 +00001956 packed_scanline);
cristy8b27a6d2010-02-14 03:31:15 +00001957 if (image->previous == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001958 {
cristycee97112010-05-28 00:44:52 +00001959 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +00001960 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001961 if (status == MagickFalse)
1962 break;
1963 }
1964 }
1965 else
1966 if (image_info->compression == JPEGCompression)
1967 {
1968 (void) ResetMagickMemory(scanline,0,row_bytes);
cristybb503372010-05-27 20:51:26 +00001969 for (y=0; y < (ssize_t) image->rows; y++)
cristyaff6d802011-04-26 01:46:31 +00001970 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
1971 packed_scanline);
cristy3ed852e2009-09-05 21:47:34 +00001972 }
1973 else
1974 {
1975 register unsigned char
1976 *blue,
1977 *green,
1978 *opacity,
1979 *red;
1980
1981 red=scanline;
1982 green=scanline+image->columns;
1983 blue=scanline+2*image->columns;
1984 opacity=scanline+3*image->columns;
cristybb503372010-05-27 20:51:26 +00001985 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001986 {
cristy1e178e72011-08-28 19:44:34 +00001987 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001988 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001989 break;
1990 red=scanline;
1991 green=scanline+image->columns;
1992 blue=scanline+2*image->columns;
cristy17f11b02014-12-20 19:37:04 +00001993 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00001994 {
1995 opacity=scanline;
1996 red=scanline+image->columns;
1997 green=scanline+2*image->columns;
1998 blue=scanline+3*image->columns;
1999 }
cristybb503372010-05-27 20:51:26 +00002000 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002001 {
cristy4c08aed2011-07-01 19:47:50 +00002002 *red++=ScaleQuantumToChar(GetPixelRed(image,p));
2003 *green++=ScaleQuantumToChar(GetPixelGreen(image,p));
2004 *blue++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy17f11b02014-12-20 19:37:04 +00002005 if (image->alpha_trait != UndefinedPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +00002006 *opacity++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
cristyed231572011-07-14 02:18:59 +00002007 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002008 }
2009 count+=EncodeImage(image,scanline,bytes_per_line & 0x7FFF,
2010 packed_scanline);
cristy8b27a6d2010-02-14 03:31:15 +00002011 if (image->previous == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002012 {
cristycee97112010-05-28 00:44:52 +00002013 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2014 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002015 if (status == MagickFalse)
2016 break;
2017 }
2018 }
2019 }
2020 if ((count & 0x01) != 0)
2021 (void) WriteBlobByte(image,'\0');
2022 (void) WriteBlobMSBShort(image,PictEndOfPictureOp);
2023 offset=TellBlob(image);
2024 offset=SeekBlob(image,512,SEEK_SET);
2025 (void) WriteBlobMSBShort(image,(unsigned short) offset);
2026 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
2027 packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline);
2028 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2029 (void) CloseBlob(image);
2030 return(MagickTrue);
2031}