blob: 418e3d54a02ff0c842756f6f3bd859e581ac4182 [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 %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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); \
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"); \
96}
97
98#define ReadRectangle(image,rectangle) \
99{ \
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"); \
107}
108
109typedef struct _PICTCode
110{
111 const char
112 *name;
113
cristybb503372010-05-27 20:51:26 +0000114 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000115 length;
116
117 const char
118 *description;
119} PICTCode;
120
121typedef struct _PICTPixmap
122{
123 short
124 version,
125 pack_type;
126
cristybb503372010-05-27 20:51:26 +0000127 size_t
cristy3ed852e2009-09-05 21:47:34 +0000128 pack_size,
129 horizontal_resolution,
130 vertical_resolution;
131
132 short
133 pixel_type,
134 bits_per_pixel,
135 component_count,
136 component_size;
137
cristybb503372010-05-27 20:51:26 +0000138 size_t
cristy3ed852e2009-09-05 21:47:34 +0000139 plane_bytes,
140 table,
141 reserved;
142} PICTPixmap;
143
144typedef struct _PICTRectangle
145{
146 short
147 top,
148 left,
149 bottom,
150 right;
151} PICTRectangle;
152
153static const PICTCode
154 codes[] =
155 {
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)" },
cristybb503372010-05-27 20:51:26 +0000170 /* 0x0e */ { "FgColor", 4, "foreground color (ssize_tword)" },
171 /* 0x0f */ { "BkColor", 4, "background color (ssize_tword)" },
cristy3ed852e2009-09-05 21:47:34 +0000172 /* 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" }
318 };
319
320/*
321 Forward declarations.
322*/
323static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +0000324 WritePICTImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000325
326/*
327%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328% %
329% %
330% %
331% D e c o d e I m a g e %
332% %
333% %
334% %
335%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336%
337% DecodeImage decompresses an image via Macintosh pack bits decoding for
338% Macintosh PICT images.
339%
340% The format of the DecodeImage method is:
341%
342% unsigned char *DecodeImage(Image *blob,Image *image,
cristybb503372010-05-27 20:51:26 +0000343% size_t bytes_per_line,const int bits_per_pixel,
cristy3ed852e2009-09-05 21:47:34 +0000344% unsigned size_t extent)
345%
346% A description of each parameter follows:
347%
348% o image_info: the image info.
349%
350% o blob,image: the address of a structure of type Image.
351%
352% o bytes_per_line: This integer identifies the number of bytes in a
353% scanline.
354%
355% o bits_per_pixel: the number of bits in a pixel.
356%
357% o extent: the number of pixels allocated.
358%
359*/
360
361static unsigned char *ExpandBuffer(unsigned char *pixels,
362 MagickSizeType *bytes_per_line,const unsigned int bits_per_pixel)
363{
cristybb503372010-05-27 20:51:26 +0000364 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000365 i;
366
367 register unsigned char
368 *p,
369 *q;
370
371 static unsigned char
372 scanline[8*256];
373
374 p=pixels;
375 q=scanline;
376 switch (bits_per_pixel)
377 {
378 case 8:
379 case 16:
380 case 32:
381 return(pixels);
382 case 4:
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 >> 4) & 0xff;
387 *q++=(*p & 15);
388 p++;
389 }
390 *bytes_per_line*=2;
391 break;
392 }
393 case 2:
394 {
cristybb503372010-05-27 20:51:26 +0000395 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000396 {
397 *q++=(*p >> 6) & 0x03;
398 *q++=(*p >> 4) & 0x03;
399 *q++=(*p >> 2) & 0x03;
400 *q++=(*p & 3);
401 p++;
402 }
403 *bytes_per_line*=4;
404 break;
405 }
406 case 1:
407 {
cristybb503372010-05-27 20:51:26 +0000408 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000409 {
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;
417 *q++=(*p & 0x01);
418 p++;
419 }
420 *bytes_per_line*=8;
421 break;
422 }
423 default:
424 break;
425 }
426 return(scanline);
427}
428
429static unsigned char *DecodeImage(Image *blob,Image *image,
cristyc82a27b2011-10-21 01:07:16 +0000430 size_t bytes_per_line,const unsigned int bits_per_pixel,size_t *extent,
431 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000432{
cristy3ed852e2009-09-05 21:47:34 +0000433 MagickSizeType
434 number_pixels;
435
cristybb503372010-05-27 20:51:26 +0000436 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000437 i;
438
439 register unsigned char
440 *p,
441 *q;
442
443 size_t
cristyaff6d802011-04-26 01:46:31 +0000444 bytes_per_pixel,
cristy3ed852e2009-09-05 21:47:34 +0000445 length,
cristyaff6d802011-04-26 01:46:31 +0000446 row_bytes,
447 scanline_length,
448 width;
cristy3ed852e2009-09-05 21:47:34 +0000449
450 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000451 count,
452 j,
453 y;
cristy3ed852e2009-09-05 21:47:34 +0000454
455 unsigned char
456 *pixels,
457 *scanline;
458
cristy3ed852e2009-09-05 21:47:34 +0000459 /*
460 Determine pixel buffer size.
461 */
462 if (bits_per_pixel <= 8)
463 bytes_per_line&=0x7fff;
464 width=image->columns;
465 bytes_per_pixel=1;
466 if (bits_per_pixel == 16)
467 {
468 bytes_per_pixel=2;
469 width*=2;
470 }
471 else
472 if (bits_per_pixel == 32)
473 width*=image->matte ? 4 : 3;
474 if (bytes_per_line == 0)
475 bytes_per_line=width;
476 row_bytes=(size_t) (image->columns | 0x8000);
477 if (image->storage_class == DirectClass)
478 row_bytes=(size_t) ((4*image->columns) | 0x8000);
479 /*
480 Allocate pixel and scanline buffer.
481 */
482 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,row_bytes*
483 sizeof(*pixels));
484 if (pixels == (unsigned char *) NULL)
485 return((unsigned char *) NULL);
486 *extent=row_bytes*image->rows*sizeof(*pixels);
487 (void) ResetMagickMemory(pixels,0,*extent);
488 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
489 if (scanline == (unsigned char *) NULL)
490 return((unsigned char *) NULL);
491 if (bytes_per_line < 8)
492 {
493 /*
494 Pixels are already uncompressed.
495 */
cristybb503372010-05-27 20:51:26 +0000496 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000497 {
cristyed231572011-07-14 02:18:59 +0000498 q=pixels+y*width*GetPixelChannels(image);;
cristy3ed852e2009-09-05 21:47:34 +0000499 number_pixels=bytes_per_line;
500 count=ReadBlob(blob,(size_t) number_pixels,scanline);
cristyda16f162011-02-19 23:52:17 +0000501 (void) count;
cristy3ed852e2009-09-05 21:47:34 +0000502 p=ExpandBuffer(scanline,&number_pixels,bits_per_pixel);
503 if ((q+number_pixels) > (pixels+(*extent)))
504 {
cristyc82a27b2011-10-21 01:07:16 +0000505 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000506 CorruptImageError,"UnableToUncompressImage","`%s'",
507 image->filename);
508 break;
509 }
510 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
511 }
512 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
513 return(pixels);
514 }
515 /*
516 Uncompress RLE pixels into uncompressed pixel buffer.
517 */
cristybb503372010-05-27 20:51:26 +0000518 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000519 {
520 q=pixels+y*width;
521 if (bytes_per_line > 200)
522 scanline_length=ReadBlobMSBShort(blob);
523 else
524 scanline_length=1UL*ReadBlobByte(blob);
525 if (scanline_length >= row_bytes)
526 {
cristyc82a27b2011-10-21 01:07:16 +0000527 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000528 CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
529 break;
530 }
531 count=ReadBlob(blob,scanline_length,scanline);
cristybb503372010-05-27 20:51:26 +0000532 for (j=0; j < (ssize_t) scanline_length; )
cristy3ed852e2009-09-05 21:47:34 +0000533 if ((scanline[j] & 0x80) == 0)
534 {
535 length=(size_t) ((scanline[j] & 0xff)+1);
536 number_pixels=length*bytes_per_pixel;
537 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
538 if ((q-pixels+number_pixels) <= *extent)
539 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
540 q+=number_pixels;
cristybb503372010-05-27 20:51:26 +0000541 j+=(ssize_t) (length*bytes_per_pixel+1);
cristy3ed852e2009-09-05 21:47:34 +0000542 }
543 else
544 {
545 length=(size_t) (((scanline[j] ^ 0xff) & 0xff)+2);
546 number_pixels=bytes_per_pixel;
547 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
cristybb503372010-05-27 20:51:26 +0000548 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000549 {
550 if ((q-pixels+number_pixels) <= *extent)
551 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
552 q+=number_pixels;
553 }
cristyeaedf062010-05-29 22:36:02 +0000554 j+=(ssize_t) bytes_per_pixel+1;
cristy3ed852e2009-09-05 21:47:34 +0000555 }
556 }
557 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
558 return(pixels);
559}
560
561/*
562%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563% %
564% %
565% %
566% E n c o d e I m a g e %
567% %
568% %
569% %
570%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571%
572% EncodeImage compresses an image via Macintosh pack bits encoding
573% for Macintosh PICT images.
574%
575% The format of the EncodeImage method is:
576%
577% size_t EncodeImage(Image *image,const unsigned char *scanline,
cristybb503372010-05-27 20:51:26 +0000578% const size_t bytes_per_line,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000579%
580% A description of each parameter follows:
581%
582% o image: the address of a structure of type Image.
583%
584% o scanline: A pointer to an array of characters to pack.
585%
586% o bytes_per_line: the number of bytes in a scanline.
587%
588% o pixels: A pointer to an array of characters where the packed
589% characters are stored.
590%
cristy3ed852e2009-09-05 21:47:34 +0000591*/
592static size_t EncodeImage(Image *image,const unsigned char *scanline,
cristybb503372010-05-27 20:51:26 +0000593 const size_t bytes_per_line,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000594{
595#define MaxCount 128
596#define MaxPackbitsRunlength 128
597
cristy3ed852e2009-09-05 21:47:34 +0000598 register const unsigned char
599 *p;
600
cristybb503372010-05-27 20:51:26 +0000601 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000602 i;
603
604 register unsigned char
605 *q;
606
607 size_t
608 length;
609
cristyaff6d802011-04-26 01:46:31 +0000610 ssize_t
611 count,
612 repeat_count,
613 runlength;
614
cristy3ed852e2009-09-05 21:47:34 +0000615 unsigned char
616 index;
617
618 /*
619 Pack scanline.
620 */
621 assert(image != (Image *) NULL);
622 assert(image->signature == MagickSignature);
623 if (image->debug != MagickFalse)
624 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
625 assert(scanline != (unsigned char *) NULL);
626 assert(pixels != (unsigned char *) NULL);
627 count=0;
628 runlength=0;
629 p=scanline+(bytes_per_line-1);
630 q=pixels;
631 index=(*p);
cristybb503372010-05-27 20:51:26 +0000632 for (i=(ssize_t) bytes_per_line-1; i >= 0; i--)
cristy3ed852e2009-09-05 21:47:34 +0000633 {
634 if (index == *p)
635 runlength++;
636 else
637 {
638 if (runlength < 3)
639 while (runlength > 0)
640 {
641 *q++=(unsigned char) index;
642 runlength--;
643 count++;
644 if (count == MaxCount)
645 {
646 *q++=(unsigned char) (MaxCount-1);
647 count-=MaxCount;
648 }
649 }
650 else
651 {
652 if (count > 0)
653 *q++=(unsigned char) (count-1);
654 count=0;
655 while (runlength > 0)
656 {
657 repeat_count=runlength;
658 if (repeat_count > MaxPackbitsRunlength)
659 repeat_count=MaxPackbitsRunlength;
660 *q++=(unsigned char) index;
661 *q++=(unsigned char) (257-repeat_count);
662 runlength-=repeat_count;
663 }
664 }
665 runlength=1;
666 }
667 index=(*p);
668 p--;
669 }
670 if (runlength < 3)
671 while (runlength > 0)
672 {
673 *q++=(unsigned char) index;
674 runlength--;
675 count++;
676 if (count == MaxCount)
677 {
678 *q++=(unsigned char) (MaxCount-1);
679 count-=MaxCount;
680 }
681 }
682 else
683 {
684 if (count > 0)
685 *q++=(unsigned char) (count-1);
686 count=0;
687 while (runlength > 0)
688 {
689 repeat_count=runlength;
690 if (repeat_count > MaxPackbitsRunlength)
691 repeat_count=MaxPackbitsRunlength;
692 *q++=(unsigned char) index;
693 *q++=(unsigned char) (257-repeat_count);
694 runlength-=repeat_count;
695 }
696 }
697 if (count > 0)
698 *q++=(unsigned char) (count-1);
699 /*
700 Write the number of and the packed length.
701 */
702 length=(size_t) (q-pixels);
703 if (bytes_per_line > 200)
704 {
705 (void) WriteBlobMSBShort(image,(unsigned short) length);
706 length+=2;
707 }
708 else
709 {
710 (void) WriteBlobByte(image,(unsigned char) length);
711 length++;
712 }
713 while (q != pixels)
714 {
715 q--;
716 (void) WriteBlobByte(image,*q);
717 }
718 return(length);
719}
720
721/*
722%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723% %
724% %
725% %
726% I s P I C T %
727% %
728% %
729% %
730%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731%
732% IsPICT()() returns MagickTrue if the image format type, identified by the
733% magick string, is PICT.
734%
735% The format of the ReadPICTImage method is:
736%
737% MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
738%
739% A description of each parameter follows:
740%
741% o magick: compare image format pattern against these bytes.
742%
743% o length: Specifies the length of the magick string.
744%
cristy3ed852e2009-09-05 21:47:34 +0000745*/
746static MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
747{
748 if (length < 528)
749 return(MagickFalse);
750 if (memcmp(magick+522,"\000\021\002\377\014\000",6) == 0)
751 return(MagickTrue);
752 return(MagickFalse);
753}
754
755#if !defined(macintosh)
756/*
757%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758% %
759% %
760% %
761% R e a d P I C T I m a g e %
762% %
763% %
764% %
765%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766%
767% ReadPICTImage() reads an Apple Macintosh QuickDraw/PICT image file
768% and returns it. It allocates the memory necessary for the new Image
769% structure and returns a pointer to the new image.
770%
771% The format of the ReadPICTImage method is:
772%
773% Image *ReadPICTImage(const ImageInfo *image_info,
774% ExceptionInfo *exception)
775%
776% A description of each parameter follows:
777%
778% o image_info: the image info.
779%
780% o exception: return any errors or warnings in this structure.
781%
782*/
783
cristybb503372010-05-27 20:51:26 +0000784static inline size_t MagickMax(const size_t x,
785 const size_t y)
cristy3ed852e2009-09-05 21:47:34 +0000786{
787 if (x > y)
788 return(x);
789 return(y);
790}
791
792static Image *ReadPICTImage(const ImageInfo *image_info,
793 ExceptionInfo *exception)
794{
795 char
796 geometry[MaxTextExtent];
797
798 Image
799 *image;
800
cristy3ed852e2009-09-05 21:47:34 +0000801 int
802 c,
803 code;
804
cristy3ed852e2009-09-05 21:47:34 +0000805 MagickBooleanType
806 jpeg,
807 status;
808
809 PICTRectangle
810 frame;
811
812 PICTPixmap
813 pixmap;
814
cristy4c08aed2011-07-01 19:47:50 +0000815 Quantum
816 index;
cristy3ed852e2009-09-05 21:47:34 +0000817
cristy4c08aed2011-07-01 19:47:50 +0000818 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000819 *q;
820
cristybb503372010-05-27 20:51:26 +0000821 register ssize_t
cristy4c08aed2011-07-01 19:47:50 +0000822 i,
823 x;
cristy3ed852e2009-09-05 21:47:34 +0000824
825 size_t
826 extent,
827 length;
828
829 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000830 count,
831 flags,
832 j,
833 version,
834 y;
cristy3ed852e2009-09-05 21:47:34 +0000835
836 StringInfo
837 *profile;
838
839 /*
840 Open image file.
841 */
842 assert(image_info != (const ImageInfo *) NULL);
843 assert(image_info->signature == MagickSignature);
844 if (image_info->debug != MagickFalse)
845 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
846 image_info->filename);
847 assert(exception != (ExceptionInfo *) NULL);
848 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000849 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000850 image->depth=8;
851 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
852 if (status == MagickFalse)
853 {
854 image=DestroyImageList(image);
855 return((Image *) NULL);
856 }
857 /*
858 Read PICT header.
859 */
860 pixmap.bits_per_pixel=0;
861 pixmap.component_count=0;
862 for (i=0; i < 512; i++)
863 (void) ReadBlobByte(image); /* skip header */
864 (void) ReadBlobMSBShort(image); /* skip picture size */
865 ReadRectangle(image,frame);
866 while ((c=ReadBlobByte(image)) == 0) ;
867 if (c != 0x11)
868 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
869 version=ReadBlobByte(image);
870 if (version == 2)
871 {
872 c=ReadBlobByte(image);
873 if (c != 0xff)
874 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
875 }
876 else
877 if (version != 1)
878 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
879 if ((frame.left < 0) || (frame.right < 0) || (frame.top < 0) ||
880 (frame.bottom < 0) || (frame.left >= frame.right) ||
881 (frame.top >= frame.bottom))
882 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
883 /*
884 Create black canvas.
885 */
886 flags=0;
887 image->columns=1UL*(frame.right-frame.left);
888 image->rows=1UL*(frame.bottom-frame.top);
cristy2a11bef2011-10-28 18:33:11 +0000889 image->resolution.x=DefaultResolution;
890 image->resolution.y=DefaultResolution;
cristy3ed852e2009-09-05 21:47:34 +0000891 image->units=UndefinedResolution;
892 /*
893 Interpret PICT opcodes.
894 */
895 jpeg=MagickFalse;
896 for (code=0; EOFBlob(image) == MagickFalse; )
897 {
898 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
899 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
900 break;
901 if ((version == 1) || ((TellBlob(image) % 2) != 0))
902 code=ReadBlobByte(image);
903 if (version == 2)
904 code=(int) ReadBlobMSBShort(image);
905 if (code > 0xa1)
906 {
907 if (image->debug != MagickFalse)
908 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%04X:",code);
909 }
910 else
911 {
912 if (image->debug != MagickFalse)
913 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
914 " %04X %s: %s",code,codes[code].name,codes[code].description);
915 switch (code)
916 {
917 case 0x01:
918 {
919 /*
920 Clipping rectangle.
921 */
922 length=ReadBlobMSBShort(image);
923 if (length != 0x000a)
924 {
cristybb503372010-05-27 20:51:26 +0000925 for (i=0; i < (ssize_t) (length-2); i++)
cristy3ed852e2009-09-05 21:47:34 +0000926 (void) ReadBlobByte(image);
927 break;
928 }
929 ReadRectangle(image,frame);
930 if (((frame.left & 0x8000) != 0) || ((frame.top & 0x8000) != 0))
931 break;
932 image->columns=1UL*(frame.right-frame.left);
933 image->rows=1UL*(frame.bottom-frame.top);
cristyea1a8aa2011-10-20 13:24:06 +0000934 (void) SetImageBackgroundColor(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000935 break;
936 }
937 case 0x12:
938 case 0x13:
939 case 0x14:
940 {
cristybb503372010-05-27 20:51:26 +0000941 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000942 pattern;
943
cristybb503372010-05-27 20:51:26 +0000944 size_t
cristy3ed852e2009-09-05 21:47:34 +0000945 height,
946 width;
947
948 /*
949 Skip pattern definition.
950 */
951 pattern=1L*ReadBlobMSBShort(image);
952 for (i=0; i < 8; i++)
953 (void) ReadBlobByte(image);
954 if (pattern == 2)
955 {
956 for (i=0; i < 5; i++)
957 (void) ReadBlobByte(image);
958 break;
959 }
960 if (pattern != 1)
961 ThrowReaderException(CorruptImageError,"UnknownPatternType");
962 length=ReadBlobMSBShort(image);
963 ReadRectangle(image,frame);
964 ReadPixmap(pixmap);
965 image->depth=1UL*pixmap.component_size;
cristy2a11bef2011-10-28 18:33:11 +0000966 image->resolution.x=1.0*pixmap.horizontal_resolution;
967 image->resolution.y=1.0*pixmap.vertical_resolution;
cristy3ed852e2009-09-05 21:47:34 +0000968 image->units=PixelsPerInchResolution;
969 (void) ReadBlobMSBLong(image);
970 flags=1L*ReadBlobMSBShort(image);
971 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +0000972 for (i=0; i <= (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000973 (void) ReadBlobMSBLong(image);
974 width=1UL*(frame.bottom-frame.top);
975 height=1UL*(frame.right-frame.left);
976 if (pixmap.bits_per_pixel <= 8)
977 length&=0x7fff;
978 if (pixmap.bits_per_pixel == 16)
979 width<<=1;
980 if (length == 0)
981 length=width;
982 if (length < 8)
983 {
cristybb503372010-05-27 20:51:26 +0000984 for (i=0; i < (ssize_t) (length*height); i++)
cristy3ed852e2009-09-05 21:47:34 +0000985 (void) ReadBlobByte(image);
986 }
987 else
988 for (j=0; j < (int) height; j++)
989 if (length > 200)
cristybb503372010-05-27 20:51:26 +0000990 for (j=0; j < (ssize_t) ReadBlobMSBShort(image); j++)
cristy3ed852e2009-09-05 21:47:34 +0000991 (void) ReadBlobByte(image);
992 else
cristybb503372010-05-27 20:51:26 +0000993 for (j=0; j < (ssize_t) ReadBlobByte(image); j++)
cristy3ed852e2009-09-05 21:47:34 +0000994 (void) ReadBlobByte(image);
995 break;
996 }
997 case 0x1b:
998 {
999 /*
1000 Initialize image background color.
1001 */
1002 image->background_color.red=(Quantum)
1003 ScaleShortToQuantum(ReadBlobMSBShort(image));
1004 image->background_color.green=(Quantum)
1005 ScaleShortToQuantum(ReadBlobMSBShort(image));
1006 image->background_color.blue=(Quantum)
1007 ScaleShortToQuantum(ReadBlobMSBShort(image));
1008 break;
1009 }
1010 case 0x70:
1011 case 0x71:
1012 case 0x72:
1013 case 0x73:
1014 case 0x74:
1015 case 0x75:
1016 case 0x76:
1017 case 0x77:
1018 {
1019 /*
1020 Skip polygon or region.
1021 */
1022 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001023 for (i=0; i < (ssize_t) (length-2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001024 (void) ReadBlobByte(image);
1025 break;
1026 }
1027 case 0x90:
1028 case 0x91:
1029 case 0x98:
1030 case 0x99:
1031 case 0x9a:
1032 case 0x9b:
1033 {
cristy4c08aed2011-07-01 19:47:50 +00001034 Image
1035 *tile_image;
cristy3ed852e2009-09-05 21:47:34 +00001036
1037 PICTRectangle
1038 source,
1039 destination;
1040
1041 register unsigned char
1042 *p;
1043
1044 size_t
1045 j;
1046
cristy4c08aed2011-07-01 19:47:50 +00001047 ssize_t
1048 bytes_per_line;
1049
cristy3ed852e2009-09-05 21:47:34 +00001050 unsigned char
1051 *pixels;
1052
cristy3ed852e2009-09-05 21:47:34 +00001053 /*
1054 Pixmap clipped by a rectangle.
1055 */
1056 bytes_per_line=0;
1057 if ((code != 0x9a) && (code != 0x9b))
1058 bytes_per_line=1L*ReadBlobMSBShort(image);
1059 else
1060 {
1061 (void) ReadBlobMSBShort(image);
1062 (void) ReadBlobMSBShort(image);
1063 (void) ReadBlobMSBShort(image);
1064 }
1065 ReadRectangle(image,frame);
1066 /*
1067 Initialize tile image.
1068 */
1069 tile_image=CloneImage(image,1UL*(frame.right-frame.left),
1070 1UL*(frame.bottom-frame.top),MagickTrue,exception);
1071 if (tile_image == (Image *) NULL)
1072 return((Image *) NULL);
1073 if ((code == 0x9a) || (code == 0x9b) ||
1074 ((bytes_per_line & 0x8000) != 0))
1075 {
1076 ReadPixmap(pixmap);
1077 tile_image->depth=1UL*pixmap.component_size;
1078 tile_image->matte=pixmap.component_count == 4 ?
1079 MagickTrue : MagickFalse;
cristy2a11bef2011-10-28 18:33:11 +00001080 tile_image->resolution.x=(double) pixmap.horizontal_resolution;
1081 tile_image->resolution.y=(double) pixmap.vertical_resolution;
cristy3ed852e2009-09-05 21:47:34 +00001082 tile_image->units=PixelsPerInchResolution;
1083 if (tile_image->matte != MagickFalse)
1084 image->matte=tile_image->matte;
1085 }
1086 if ((code != 0x9a) && (code != 0x9b))
1087 {
1088 /*
1089 Initialize colormap.
1090 */
1091 tile_image->colors=2;
1092 if ((bytes_per_line & 0x8000) != 0)
1093 {
1094 (void) ReadBlobMSBLong(image);
1095 flags=1L*ReadBlobMSBShort(image);
1096 tile_image->colors=1UL*ReadBlobMSBShort(image)+1;
1097 }
cristy018f07f2011-09-04 21:15:19 +00001098 status=AcquireImageColormap(tile_image,tile_image->colors,
1099 exception);
cristy3ed852e2009-09-05 21:47:34 +00001100 if (status == MagickFalse)
1101 {
1102 tile_image=DestroyImage(tile_image);
1103 ThrowReaderException(ResourceLimitError,
1104 "MemoryAllocationFailed");
1105 }
1106 if ((bytes_per_line & 0x8000) != 0)
1107 {
cristybb503372010-05-27 20:51:26 +00001108 for (i=0; i < (ssize_t) tile_image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001109 {
1110 j=ReadBlobMSBShort(image) % tile_image->colors;
1111 if ((flags & 0x8000) != 0)
1112 j=(size_t) i;
1113 tile_image->colormap[j].red=(Quantum)
1114 ScaleShortToQuantum(ReadBlobMSBShort(image));
1115 tile_image->colormap[j].green=(Quantum)
1116 ScaleShortToQuantum(ReadBlobMSBShort(image));
1117 tile_image->colormap[j].blue=(Quantum)
1118 ScaleShortToQuantum(ReadBlobMSBShort(image));
1119 }
1120 }
1121 else
1122 {
cristybb503372010-05-27 20:51:26 +00001123 for (i=0; i < (ssize_t) tile_image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001124 {
1125 tile_image->colormap[i].red=(Quantum) (QuantumRange-
1126 tile_image->colormap[i].red);
1127 tile_image->colormap[i].green=(Quantum) (QuantumRange-
1128 tile_image->colormap[i].green);
1129 tile_image->colormap[i].blue=(Quantum) (QuantumRange-
1130 tile_image->colormap[i].blue);
1131 }
1132 }
1133 }
1134 ReadRectangle(image,source);
1135 ReadRectangle(image,destination);
1136 (void) ReadBlobMSBShort(image);
1137 if ((code == 0x91) || (code == 0x99) || (code == 0x9b))
1138 {
1139 /*
1140 Skip region.
1141 */
1142 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001143 for (i=0; i < (ssize_t) (length-2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001144 (void) ReadBlobByte(image);
1145 }
1146 if ((code != 0x9a) && (code != 0x9b) &&
1147 (bytes_per_line & 0x8000) == 0)
cristyc82a27b2011-10-21 01:07:16 +00001148 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1,&extent,
1149 exception);
cristy3ed852e2009-09-05 21:47:34 +00001150 else
1151 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1U*
cristyc82a27b2011-10-21 01:07:16 +00001152 pixmap.bits_per_pixel,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +00001153 if (pixels == (unsigned char *) NULL)
1154 {
1155 tile_image=DestroyImage(tile_image);
1156 ThrowReaderException(ResourceLimitError,
1157 "MemoryAllocationFailed");
1158 }
1159 /*
1160 Convert PICT tile image to pixel packets.
1161 */
1162 p=pixels;
cristybb503372010-05-27 20:51:26 +00001163 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001164 {
1165 if (p > (pixels+extent+image->columns))
1166 ThrowReaderException(CorruptImageError,"NotEnoughPixelData");
cristy3669d042009-10-16 01:36:31 +00001167 q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,
1168 exception);
cristyacd2ed22011-08-30 01:44:23 +00001169 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001170 break;
cristybb503372010-05-27 20:51:26 +00001171 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001172 {
1173 if (tile_image->storage_class == PseudoClass)
1174 {
cristyc82a27b2011-10-21 01:07:16 +00001175 index=ConstrainColormapIndex(tile_image,*p,exception);
cristy4c08aed2011-07-01 19:47:50 +00001176 SetPixelIndex(tile_image,index,q);
1177 SetPixelRed(tile_image,
1178 tile_image->colormap[(ssize_t) index].red,q);
1179 SetPixelGreen(tile_image,
1180 tile_image->colormap[(ssize_t) index].green,q);
1181 SetPixelBlue(tile_image,
1182 tile_image->colormap[(ssize_t) index].blue,q);
cristy3ed852e2009-09-05 21:47:34 +00001183 }
1184 else
1185 {
1186 if (pixmap.bits_per_pixel == 16)
1187 {
1188 i=(*p++);
1189 j=(*p);
cristy4c08aed2011-07-01 19:47:50 +00001190 SetPixelRed(tile_image,ScaleCharToQuantum(
1191 (unsigned char) ((i & 0x7c) << 1)),q);
1192 SetPixelGreen(tile_image,ScaleCharToQuantum(
cristyaff6d802011-04-26 01:46:31 +00001193 (unsigned char) (((i & 0x03) << 6) |
cristy4c08aed2011-07-01 19:47:50 +00001194 ((j & 0xe0) >> 2))),q);
1195 SetPixelBlue(tile_image,ScaleCharToQuantum(
1196 (unsigned char) ((j & 0x1f) << 3)),q);
cristy3ed852e2009-09-05 21:47:34 +00001197 }
1198 else
1199 if (tile_image->matte == MagickFalse)
1200 {
1201 if (p > (pixels+extent+2*image->columns))
1202 ThrowReaderException(CorruptImageError,
1203 "NotEnoughPixelData");
cristy4c08aed2011-07-01 19:47:50 +00001204 SetPixelRed(tile_image,ScaleCharToQuantum(*p),q);
1205 SetPixelGreen(tile_image,ScaleCharToQuantum(
1206 *(p+tile_image->columns)),q);
1207 SetPixelBlue(tile_image,ScaleCharToQuantum(
1208 *(p+2*tile_image->columns)),q);
cristy3ed852e2009-09-05 21:47:34 +00001209 }
1210 else
1211 {
1212 if (p > (pixels+extent+3*image->columns))
1213 ThrowReaderException(CorruptImageError,
1214 "NotEnoughPixelData");
cristy4c08aed2011-07-01 19:47:50 +00001215 SetPixelAlpha(tile_image,ScaleCharToQuantum(*p),q);
1216 SetPixelRed(tile_image,ScaleCharToQuantum(
1217 *(p+tile_image->columns)),q);
1218 SetPixelGreen(tile_image,ScaleCharToQuantum(
1219 *(p+2*tile_image->columns)),q);
1220 SetPixelBlue(tile_image,ScaleCharToQuantum(
1221 *(p+3*tile_image->columns)),q);
cristy3ed852e2009-09-05 21:47:34 +00001222 }
1223 }
1224 p++;
cristyed231572011-07-14 02:18:59 +00001225 q+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00001226 }
1227 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
1228 break;
1229 if ((tile_image->storage_class == DirectClass) &&
1230 (pixmap.bits_per_pixel != 16))
1231 {
1232 p+=(pixmap.component_count-1)*tile_image->columns;
1233 if (p < pixels)
1234 break;
1235 }
1236 status=SetImageProgress(image,LoadImageTag,y,tile_image->rows);
1237 if (status == MagickFalse)
1238 break;
1239 }
1240 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1241 if (jpeg == MagickFalse)
1242 if ((code == 0x9a) || (code == 0x9b) ||
1243 ((bytes_per_line & 0x8000) != 0))
1244 (void) CompositeImage(image,CopyCompositeOp,tile_image,
cristye941a752011-10-15 01:52:48 +00001245 destination.left,destination.top,exception);
cristy3ed852e2009-09-05 21:47:34 +00001246 tile_image=DestroyImage(tile_image);
1247 break;
1248 }
1249 case 0xa1:
1250 {
1251 unsigned char
1252 *info;
1253
cristybb503372010-05-27 20:51:26 +00001254 size_t
cristy3ed852e2009-09-05 21:47:34 +00001255 type;
1256
1257 /*
1258 Comment.
1259 */
1260 type=ReadBlobMSBShort(image);
1261 length=ReadBlobMSBShort(image);
1262 if (length == 0)
1263 break;
1264 (void) ReadBlobMSBLong(image);
1265 length-=4;
1266 if (length == 0)
1267 break;
1268 info=(unsigned char *) AcquireQuantumMemory(length,sizeof(*info));
1269 if (info == (unsigned char *) NULL)
1270 break;
1271 count=ReadBlob(image,length,info);
cristyda16f162011-02-19 23:52:17 +00001272 (void) count;
cristy3ed852e2009-09-05 21:47:34 +00001273 switch (type)
1274 {
1275 case 0xe0:
1276 {
1277 if (length == 0)
1278 break;
cristy8723e4b2011-09-01 13:11:19 +00001279 profile=BlobToStringInfo((const void *) NULL,length);
cristy3ed852e2009-09-05 21:47:34 +00001280 SetStringInfoDatum(profile,info);
cristyd15e6592011-10-15 00:13:06 +00001281 status=SetImageProfile(image,"icc",profile,exception);
cristy3ed852e2009-09-05 21:47:34 +00001282 profile=DestroyStringInfo(profile);
1283 if (status == MagickFalse)
1284 ThrowReaderException(ResourceLimitError,
1285 "MemoryAllocationFailed");
1286 break;
1287 }
1288 case 0x1f2:
1289 {
1290 if (length == 0)
1291 break;
cristy8723e4b2011-09-01 13:11:19 +00001292 profile=BlobToStringInfo((const void *) NULL,length);
cristy3ed852e2009-09-05 21:47:34 +00001293 SetStringInfoDatum(profile,info);
cristyd15e6592011-10-15 00:13:06 +00001294 status=SetImageProfile(image,"iptc",profile,exception);
cristy3ed852e2009-09-05 21:47:34 +00001295 if (status == MagickFalse)
1296 ThrowReaderException(ResourceLimitError,
1297 "MemoryAllocationFailed");
1298 profile=DestroyStringInfo(profile);
1299 break;
1300 }
1301 default:
1302 break;
1303 }
1304 info=(unsigned char *) RelinquishMagickMemory(info);
1305 break;
1306 }
1307 default:
1308 {
1309 /*
1310 Skip to next op code.
1311 */
1312 if (codes[code].length == -1)
1313 (void) ReadBlobMSBShort(image);
1314 else
cristybb503372010-05-27 20:51:26 +00001315 for (i=0; i < (ssize_t) codes[code].length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001316 (void) ReadBlobByte(image);
1317 }
1318 }
1319 }
1320 if (code == 0xc00)
1321 {
1322 /*
1323 Skip header.
1324 */
1325 for (i=0; i < 24; i++)
1326 (void) ReadBlobByte(image);
1327 continue;
1328 }
1329 if (((code >= 0xb0) && (code <= 0xcf)) ||
1330 ((code >= 0x8000) && (code <= 0x80ff)))
1331 continue;
1332 if (code == 0x8200)
1333 {
1334 FILE
1335 *file;
1336
1337 Image
1338 *tile_image;
1339
1340 ImageInfo
1341 *read_info;
1342
1343 int
1344 unique_file;
1345
1346 /*
1347 Embedded JPEG.
1348 */
1349 jpeg=MagickTrue;
1350 read_info=CloneImageInfo(image_info);
1351 SetImageInfoBlob(read_info,(void *) NULL,0);
1352 file=(FILE *) NULL;
1353 unique_file=AcquireUniqueFileResource(read_info->filename);
1354 if (unique_file != -1)
cristyd0305b92009-10-19 13:12:21 +00001355 file=fdopen(unique_file,"wb");
cristy3ed852e2009-09-05 21:47:34 +00001356 if ((unique_file == -1) || (file == (FILE *) NULL))
1357 {
1358 (void) CopyMagickString(image->filename,read_info->filename,
1359 MaxTextExtent);
1360 ThrowFileException(exception,FileOpenError,
1361 "UnableToCreateTemporaryFile",image->filename);
1362 image=DestroyImageList(image);
1363 return((Image *) NULL);
1364 }
1365 length=ReadBlobMSBLong(image);
1366 for (i=0; i < 6; i++)
1367 (void) ReadBlobMSBLong(image);
1368 ReadRectangle(image,frame);
1369 for (i=0; i < 122; i++)
1370 (void) ReadBlobByte(image);
cristybb503372010-05-27 20:51:26 +00001371 for (i=0; i < (ssize_t) (length-154); i++)
cristy3ed852e2009-09-05 21:47:34 +00001372 {
1373 c=ReadBlobByte(image);
1374 (void) fputc(c,file);
1375 }
1376 (void) fclose(file);
1377 tile_image=ReadImage(read_info,exception);
1378 (void) RelinquishUniqueFileResource(read_info->filename);
1379 read_info=DestroyImageInfo(read_info);
1380 if (tile_image == (Image *) NULL)
1381 continue;
cristyb51dff52011-05-19 16:55:47 +00001382 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g",
cristye8c25f92010-06-03 00:53:06 +00001383 (double) MagickMax(image->columns,tile_image->columns),
1384 (double) MagickMax(image->rows,tile_image->rows));
cristya0511732010-02-17 02:38:35 +00001385 (void) SetImageExtent(image,
1386 MagickMax(image->columns,tile_image->columns),
cristy63240882011-08-05 19:05:27 +00001387 MagickMax(image->rows,tile_image->rows),exception);
cristy510d06a2011-07-06 23:43:54 +00001388 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +00001389 (void) TransformImageColorspace(image,tile_image->colorspace,
1390 exception);
cristy3ed852e2009-09-05 21:47:34 +00001391 (void) CompositeImage(image,CopyCompositeOp,tile_image,frame.left,
cristye941a752011-10-15 01:52:48 +00001392 frame.right,exception);
cristy3ed852e2009-09-05 21:47:34 +00001393 image->compression=tile_image->compression;
1394 tile_image=DestroyImage(tile_image);
1395 continue;
1396 }
1397 if ((code == 0xff) || (code == 0xffff))
1398 break;
1399 if (((code >= 0xd0) && (code <= 0xfe)) ||
1400 ((code >= 0x8100) && (code <= 0xffff)))
1401 {
1402 /*
1403 Skip reserved.
1404 */
1405 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001406 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001407 (void) ReadBlobByte(image);
1408 continue;
1409 }
1410 if ((code >= 0x100) && (code <= 0x7fff))
1411 {
1412 /*
1413 Skip reserved.
1414 */
1415 length=(size_t) ((code >> 7) & 0xff);
cristybb503372010-05-27 20:51:26 +00001416 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001417 (void) ReadBlobByte(image);
1418 continue;
1419 }
1420 }
1421 (void) CloseBlob(image);
1422 return(GetFirstImageInList(image));
1423}
1424#endif
1425
1426/*
1427%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1428% %
1429% %
1430% %
1431% R e g i s t e r P I C T I m a g e %
1432% %
1433% %
1434% %
1435%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1436%
1437% RegisterPICTImage() adds attributes for the PICT image format to
1438% the list of supported formats. The attributes include the image format
1439% tag, a method to read and/or write the format, whether the format
1440% supports the saving of more than one frame to the same file or blob,
1441% whether the format supports native in-memory I/O, and a brief
1442% description of the format.
1443%
1444% The format of the RegisterPICTImage method is:
1445%
cristybb503372010-05-27 20:51:26 +00001446% size_t RegisterPICTImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001447%
1448*/
cristybb503372010-05-27 20:51:26 +00001449ModuleExport size_t RegisterPICTImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001450{
1451 MagickInfo
1452 *entry;
1453
1454 entry=SetMagickInfo("PCT");
1455 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1456 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1457 entry->adjoin=MagickFalse;
cristyffaf9782011-04-13 19:50:51 +00001458 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001459 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1460 entry->magick=(IsImageFormatHandler *) IsPICT;
1461 entry->module=ConstantString("PICT");
1462 (void) RegisterMagickInfo(entry);
1463 entry=SetMagickInfo("PICT");
1464 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1465 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1466 entry->adjoin=MagickFalse;
cristyffaf9782011-04-13 19:50:51 +00001467 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001468 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1469 entry->magick=(IsImageFormatHandler *) IsPICT;
1470 entry->module=ConstantString("PICT");
1471 (void) RegisterMagickInfo(entry);
1472 return(MagickImageCoderSignature);
1473}
1474
1475/*
1476%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1477% %
1478% %
1479% %
1480% U n r e g i s t e r P I C T I m a g e %
1481% %
1482% %
1483% %
1484%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1485%
1486% UnregisterPICTImage() removes format registrations made by the
1487% PICT module from the list of supported formats.
1488%
1489% The format of the UnregisterPICTImage method is:
1490%
1491% UnregisterPICTImage(void)
1492%
1493*/
1494ModuleExport void UnregisterPICTImage(void)
1495{
1496 (void) UnregisterMagickInfo("PCT");
1497 (void) UnregisterMagickInfo("PICT");
1498}
1499
1500/*
1501%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1502% %
1503% %
1504% %
1505% W r i t e P I C T I m a g e %
1506% %
1507% %
1508% %
1509%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1510%
1511% WritePICTImage() writes an image to a file in the Apple Macintosh
1512% QuickDraw/PICT image format.
1513%
1514% The format of the WritePICTImage method is:
1515%
cristy3669d042009-10-16 01:36:31 +00001516% MagickBooleanType WritePICTImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +00001517% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001518%
1519% A description of each parameter follows.
1520%
1521% o image_info: the image info.
1522%
1523% o image: The image.
1524%
cristy1e178e72011-08-28 19:44:34 +00001525% o exception: return any errors or warnings in this structure.
1526%
cristy3ed852e2009-09-05 21:47:34 +00001527*/
1528static MagickBooleanType WritePICTImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +00001529 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001530{
1531#define MaxCount 128
1532#define PictCropRegionOp 0x01
1533#define PictEndOfPictureOp 0xff
1534#define PictJPEGOp 0x8200
1535#define PictInfoOp 0x0C00
1536#define PictInfoSize 512
1537#define PictPixmapOp 0x9A
1538#define PictPICTOp 0x98
1539#define PictVersion 0x11
1540
1541 const StringInfo
1542 *profile;
1543
1544 double
1545 x_resolution,
1546 y_resolution;
1547
cristy3ed852e2009-09-05 21:47:34 +00001548 MagickBooleanType
1549 status;
1550
1551 MagickOffsetType
1552 offset;
1553
1554 PICTPixmap
1555 pixmap;
1556
1557 PICTRectangle
1558 bounds,
1559 crop_rectangle,
1560 destination_rectangle,
1561 frame_rectangle,
1562 size_rectangle,
1563 source_rectangle;
1564
cristy4c08aed2011-07-01 19:47:50 +00001565 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001566 *p;
1567
cristybb503372010-05-27 20:51:26 +00001568 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001569 i,
1570 x;
1571
1572 size_t
cristyaff6d802011-04-26 01:46:31 +00001573 bytes_per_line,
1574 count,
1575 storage_class;
1576
1577 ssize_t
1578 y;
cristy3ed852e2009-09-05 21:47:34 +00001579
1580 unsigned char
1581 *buffer,
1582 *packed_scanline,
1583 *scanline;
1584
cristy3ed852e2009-09-05 21:47:34 +00001585 unsigned short
1586 base_address,
1587 row_bytes,
1588 transfer_mode;
1589
1590 /*
1591 Open output image file.
1592 */
1593 assert(image_info != (const ImageInfo *) NULL);
1594 assert(image_info->signature == MagickSignature);
1595 assert(image != (Image *) NULL);
1596 assert(image->signature == MagickSignature);
1597 if (image->debug != MagickFalse)
1598 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1599 if ((image->columns > 65535L) || (image->rows > 65535L))
1600 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
cristy3a37efd2011-08-28 20:31:03 +00001601 assert(exception != (ExceptionInfo *) NULL);
1602 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +00001603 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001604 if (status == MagickFalse)
1605 return(status);
cristy510d06a2011-07-06 23:43:54 +00001606 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +00001607 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001608 /*
1609 Initialize image info.
1610 */
1611 size_rectangle.top=0;
1612 size_rectangle.left=0;
1613 size_rectangle.bottom=(short) image->rows;
1614 size_rectangle.right=(short) image->columns;
1615 frame_rectangle=size_rectangle;
1616 crop_rectangle=size_rectangle;
1617 source_rectangle=size_rectangle;
1618 destination_rectangle=size_rectangle;
1619 base_address=0xff;
1620 row_bytes=(unsigned short) (image->columns | 0x8000);
1621 bounds.top=0;
1622 bounds.left=0;
1623 bounds.bottom=(short) image->rows;
1624 bounds.right=(short) image->columns;
1625 pixmap.version=0;
1626 pixmap.pack_type=0;
1627 pixmap.pack_size=0;
1628 pixmap.pixel_type=0;
1629 pixmap.bits_per_pixel=8;
1630 pixmap.component_count=1;
1631 pixmap.component_size=8;
1632 pixmap.plane_bytes=0;
1633 pixmap.table=0;
1634 pixmap.reserved=0;
1635 transfer_mode=0;
cristy2a11bef2011-10-28 18:33:11 +00001636 x_resolution=image->resolution.x != 0.0 ? image->resolution.x :
cristy3ed852e2009-09-05 21:47:34 +00001637 DefaultResolution;
cristy2a11bef2011-10-28 18:33:11 +00001638 y_resolution=image->resolution.y != 0.0 ? image->resolution.y :
cristy3ed852e2009-09-05 21:47:34 +00001639 DefaultResolution;
1640 storage_class=image->storage_class;
1641 if (image_info->compression == JPEGCompression)
1642 storage_class=DirectClass;
1643 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1644 {
1645 pixmap.component_count=image->matte ? 4 : 3;
1646 pixmap.pixel_type=16;
1647 pixmap.bits_per_pixel=32;
1648 pixmap.pack_type=0x04;
1649 transfer_mode=0x40;
1650 row_bytes=(unsigned short) ((4*image->columns) | 0x8000);
1651 }
1652 /*
1653 Allocate memory.
1654 */
1655 bytes_per_line=image->columns;
1656 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1657 bytes_per_line*=image->matte ? 4 : 3;
1658 buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer));
1659 packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t)
1660 (row_bytes+MaxCount),sizeof(*packed_scanline));
1661 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
1662 if ((buffer == (unsigned char *) NULL) ||
1663 (packed_scanline == (unsigned char *) NULL) ||
1664 (scanline == (unsigned char *) NULL))
1665 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1666 (void) ResetMagickMemory(scanline,0,row_bytes);
1667 (void) ResetMagickMemory(packed_scanline,0,(size_t) (row_bytes+MaxCount));
1668 /*
1669 Write header, header size, size bounding box, version, and reserved.
1670 */
1671 (void) ResetMagickMemory(buffer,0,PictInfoSize);
1672 (void) WriteBlob(image,PictInfoSize,buffer);
1673 (void) WriteBlobMSBShort(image,0);
1674 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top);
1675 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left);
1676 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom);
1677 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right);
1678 (void) WriteBlobMSBShort(image,PictVersion);
1679 (void) WriteBlobMSBShort(image,0x02ff); /* version #2 */
1680 (void) WriteBlobMSBShort(image,PictInfoOp);
1681 (void) WriteBlobMSBLong(image,0xFFFE0000UL);
1682 /*
1683 Write full size of the file, resolution, frame bounding box, and reserved.
1684 */
1685 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1686 (void) WriteBlobMSBShort(image,0x0000);
1687 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1688 (void) WriteBlobMSBShort(image,0x0000);
1689 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top);
1690 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left);
1691 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom);
1692 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right);
1693 (void) WriteBlobMSBLong(image,0x00000000L);
1694 profile=GetImageProfile(image,"iptc");
1695 if (profile != (StringInfo *) NULL)
1696 {
1697 (void) WriteBlobMSBShort(image,0xa1);
1698 (void) WriteBlobMSBShort(image,0x1f2);
cristyed552522009-10-16 14:04:35 +00001699 (void) WriteBlobMSBShort(image,(unsigned short)
1700 (GetStringInfoLength(profile)+4));
cristy3ed852e2009-09-05 21:47:34 +00001701 (void) WriteBlobString(image,"8BIM");
cristyed552522009-10-16 14:04:35 +00001702 (void) WriteBlob(image,GetStringInfoLength(profile),
1703 GetStringInfoDatum(profile));
cristy3ed852e2009-09-05 21:47:34 +00001704 }
1705 profile=GetImageProfile(image,"icc");
1706 if (profile != (StringInfo *) NULL)
1707 {
1708 (void) WriteBlobMSBShort(image,0xa1);
1709 (void) WriteBlobMSBShort(image,0xe0);
cristyed552522009-10-16 14:04:35 +00001710 (void) WriteBlobMSBShort(image,(unsigned short)
1711 (GetStringInfoLength(profile)+4));
cristy3ed852e2009-09-05 21:47:34 +00001712 (void) WriteBlobMSBLong(image,0x00000000UL);
cristyed552522009-10-16 14:04:35 +00001713 (void) WriteBlob(image,GetStringInfoLength(profile),
1714 GetStringInfoDatum(profile));
cristy3ed852e2009-09-05 21:47:34 +00001715 (void) WriteBlobMSBShort(image,0xa1);
1716 (void) WriteBlobMSBShort(image,0xe0);
1717 (void) WriteBlobMSBShort(image,4);
1718 (void) WriteBlobMSBLong(image,0x00000002UL);
1719 }
1720 /*
1721 Write crop region opcode and crop bounding box.
1722 */
1723 (void) WriteBlobMSBShort(image,PictCropRegionOp);
1724 (void) WriteBlobMSBShort(image,0xa);
1725 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top);
1726 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left);
1727 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom);
1728 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right);
1729 if (image_info->compression == JPEGCompression)
1730 {
1731 Image
1732 *jpeg_image;
1733
1734 ImageInfo
1735 *jpeg_info;
1736
1737 size_t
1738 length;
1739
1740 unsigned char
1741 *blob;
1742
cristy1e178e72011-08-28 19:44:34 +00001743 jpeg_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001744 if (jpeg_image == (Image *) NULL)
1745 {
1746 (void) CloseBlob(image);
1747 return(MagickFalse);
1748 }
1749 jpeg_info=CloneImageInfo(image_info);
1750 (void) CopyMagickString(jpeg_info->magick,"JPEG",MaxTextExtent);
1751 length=0;
1752 blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length,
cristy1e178e72011-08-28 19:44:34 +00001753 exception);
cristy3ed852e2009-09-05 21:47:34 +00001754 jpeg_info=DestroyImageInfo(jpeg_info);
1755 if (blob == (unsigned char *) NULL)
1756 return(MagickFalse);
1757 jpeg_image=DestroyImage(jpeg_image);
1758 (void) WriteBlobMSBShort(image,PictJPEGOp);
cristyeaedf062010-05-29 22:36:02 +00001759 (void) WriteBlobMSBLong(image,(unsigned int) length+154);
cristy3ed852e2009-09-05 21:47:34 +00001760 (void) WriteBlobMSBShort(image,0x0000);
1761 (void) WriteBlobMSBLong(image,0x00010000UL);
1762 (void) WriteBlobMSBLong(image,0x00000000UL);
1763 (void) WriteBlobMSBLong(image,0x00000000UL);
1764 (void) WriteBlobMSBLong(image,0x00000000UL);
1765 (void) WriteBlobMSBLong(image,0x00010000UL);
1766 (void) WriteBlobMSBLong(image,0x00000000UL);
1767 (void) WriteBlobMSBLong(image,0x00000000UL);
1768 (void) WriteBlobMSBLong(image,0x00000000UL);
1769 (void) WriteBlobMSBLong(image,0x40000000UL);
1770 (void) WriteBlobMSBLong(image,0x00000000UL);
1771 (void) WriteBlobMSBLong(image,0x00000000UL);
1772 (void) WriteBlobMSBLong(image,0x00000000UL);
1773 (void) WriteBlobMSBLong(image,0x00400000UL);
1774 (void) WriteBlobMSBShort(image,0x0000);
1775 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1776 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1777 (void) WriteBlobMSBShort(image,0x0000);
1778 (void) WriteBlobMSBShort(image,768);
1779 (void) WriteBlobMSBShort(image,0x0000);
1780 (void) WriteBlobMSBLong(image,0x00000000UL);
1781 (void) WriteBlobMSBLong(image,0x00566A70UL);
1782 (void) WriteBlobMSBLong(image,0x65670000UL);
1783 (void) WriteBlobMSBLong(image,0x00000000UL);
1784 (void) WriteBlobMSBLong(image,0x00000001UL);
1785 (void) WriteBlobMSBLong(image,0x00016170UL);
1786 (void) WriteBlobMSBLong(image,0x706C0000UL);
1787 (void) WriteBlobMSBLong(image,0x00000000UL);
1788 (void) WriteBlobMSBShort(image,768);
1789 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1790 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1791 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1792 (void) WriteBlobMSBShort(image,0x0000);
1793 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1794 (void) WriteBlobMSBLong(image,0x00000000UL);
1795 (void) WriteBlobMSBLong(image,0x87AC0001UL);
1796 (void) WriteBlobMSBLong(image,0x0B466F74UL);
1797 (void) WriteBlobMSBLong(image,0x6F202D20UL);
1798 (void) WriteBlobMSBLong(image,0x4A504547UL);
1799 (void) WriteBlobMSBLong(image,0x00000000UL);
1800 (void) WriteBlobMSBLong(image,0x00000000UL);
1801 (void) WriteBlobMSBLong(image,0x00000000UL);
1802 (void) WriteBlobMSBLong(image,0x00000000UL);
1803 (void) WriteBlobMSBLong(image,0x00000000UL);
1804 (void) WriteBlobMSBLong(image,0x0018FFFFUL);
1805 (void) WriteBlob(image,length,blob);
1806 if ((length & 0x01) != 0)
1807 (void) WriteBlobByte(image,'\0');
1808 blob=(unsigned char *) RelinquishMagickMemory(blob);
1809 }
1810 /*
1811 Write picture opcode, row bytes, and picture bounding box, and version.
1812 */
1813 if (storage_class == PseudoClass)
1814 (void) WriteBlobMSBShort(image,PictPICTOp);
1815 else
1816 {
1817 (void) WriteBlobMSBShort(image,PictPixmapOp);
cristybb503372010-05-27 20:51:26 +00001818 (void) WriteBlobMSBLong(image,(size_t) base_address);
cristy3ed852e2009-09-05 21:47:34 +00001819 }
1820 (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000));
1821 (void) WriteBlobMSBShort(image,(unsigned short) bounds.top);
1822 (void) WriteBlobMSBShort(image,(unsigned short) bounds.left);
1823 (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom);
1824 (void) WriteBlobMSBShort(image,(unsigned short) bounds.right);
1825 /*
1826 Write pack type, pack size, resolution, pixel type, and pixel size.
1827 */
1828 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version);
1829 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type);
cristyeaedf062010-05-29 22:36:02 +00001830 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.pack_size);
cristy3ed852e2009-09-05 21:47:34 +00001831 (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5));
1832 (void) WriteBlobMSBShort(image,0x0000);
1833 (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5));
1834 (void) WriteBlobMSBShort(image,0x0000);
1835 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type);
1836 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel);
1837 /*
1838 Write component count, size, plane bytes, table size, and reserved.
1839 */
1840 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count);
1841 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size);
cristyeaedf062010-05-29 22:36:02 +00001842 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.plane_bytes);
1843 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.table);
1844 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.reserved);
cristy3ed852e2009-09-05 21:47:34 +00001845 if (storage_class == PseudoClass)
1846 {
1847 /*
1848 Write image colormap.
1849 */
1850 (void) WriteBlobMSBLong(image,0x00000000L); /* color seed */
1851 (void) WriteBlobMSBShort(image,0L); /* color flags */
1852 (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1));
cristybb503372010-05-27 20:51:26 +00001853 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001854 {
1855 (void) WriteBlobMSBShort(image,(unsigned short) i);
1856 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1857 image->colormap[i].red));
1858 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1859 image->colormap[i].green));
1860 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1861 image->colormap[i].blue));
1862 }
1863 }
1864 /*
1865 Write source and destination rectangle.
1866 */
1867 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top);
1868 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left);
1869 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom);
1870 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right);
1871 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top);
1872 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left);
1873 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom);
1874 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right);
1875 (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode);
1876 /*
1877 Write picture data.
1878 */
1879 count=0;
1880 if ((storage_class == PseudoClass) && (image->matte == MagickFalse))
cristybb503372010-05-27 20:51:26 +00001881 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001882 {
cristy1e178e72011-08-28 19:44:34 +00001883 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001884 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001885 break;
cristybb503372010-05-27 20:51:26 +00001886 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001887 {
1888 scanline[x]=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001889 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001890 }
cristybb503372010-05-27 20:51:26 +00001891 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
cristy3ed852e2009-09-05 21:47:34 +00001892 packed_scanline);
cristy8b27a6d2010-02-14 03:31:15 +00001893 if (image->previous == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001894 {
cristycee97112010-05-28 00:44:52 +00001895 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +00001896 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001897 if (status == MagickFalse)
1898 break;
1899 }
1900 }
1901 else
1902 if (image_info->compression == JPEGCompression)
1903 {
1904 (void) ResetMagickMemory(scanline,0,row_bytes);
cristybb503372010-05-27 20:51:26 +00001905 for (y=0; y < (ssize_t) image->rows; y++)
cristyaff6d802011-04-26 01:46:31 +00001906 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
1907 packed_scanline);
cristy3ed852e2009-09-05 21:47:34 +00001908 }
1909 else
1910 {
1911 register unsigned char
1912 *blue,
1913 *green,
1914 *opacity,
1915 *red;
1916
1917 red=scanline;
1918 green=scanline+image->columns;
1919 blue=scanline+2*image->columns;
1920 opacity=scanline+3*image->columns;
cristybb503372010-05-27 20:51:26 +00001921 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001922 {
cristy1e178e72011-08-28 19:44:34 +00001923 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001924 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001925 break;
1926 red=scanline;
1927 green=scanline+image->columns;
1928 blue=scanline+2*image->columns;
1929 if (image->matte != MagickFalse)
1930 {
1931 opacity=scanline;
1932 red=scanline+image->columns;
1933 green=scanline+2*image->columns;
1934 blue=scanline+3*image->columns;
1935 }
cristybb503372010-05-27 20:51:26 +00001936 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001937 {
cristy4c08aed2011-07-01 19:47:50 +00001938 *red++=ScaleQuantumToChar(GetPixelRed(image,p));
1939 *green++=ScaleQuantumToChar(GetPixelGreen(image,p));
1940 *blue++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001941 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001942 *opacity++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
cristyed231572011-07-14 02:18:59 +00001943 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001944 }
1945 count+=EncodeImage(image,scanline,bytes_per_line & 0x7FFF,
1946 packed_scanline);
cristy8b27a6d2010-02-14 03:31:15 +00001947 if (image->previous == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001948 {
cristycee97112010-05-28 00:44:52 +00001949 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1950 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001951 if (status == MagickFalse)
1952 break;
1953 }
1954 }
1955 }
1956 if ((count & 0x01) != 0)
1957 (void) WriteBlobByte(image,'\0');
1958 (void) WriteBlobMSBShort(image,PictEndOfPictureOp);
1959 offset=TellBlob(image);
1960 offset=SeekBlob(image,512,SEEK_SET);
1961 (void) WriteBlobMSBShort(image,(unsigned short) offset);
1962 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
1963 packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline);
1964 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1965 (void) CloseBlob(image);
1966 return(MagickTrue);
1967}