blob: 143fca6c76794a71cad8c7efde9ef6833e647873 [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% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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,
cristybb503372010-05-27 20:51:26 +0000430 size_t bytes_per_line,const unsigned int bits_per_pixel,size_t *extent)
cristy3ed852e2009-09-05 21:47:34 +0000431{
cristy3ed852e2009-09-05 21:47:34 +0000432 MagickSizeType
433 number_pixels;
434
cristybb503372010-05-27 20:51:26 +0000435 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000436 i;
437
438 register unsigned char
439 *p,
440 *q;
441
442 size_t
cristyaff6d802011-04-26 01:46:31 +0000443 bytes_per_pixel,
cristy3ed852e2009-09-05 21:47:34 +0000444 length,
cristyaff6d802011-04-26 01:46:31 +0000445 row_bytes,
446 scanline_length,
447 width;
cristy3ed852e2009-09-05 21:47:34 +0000448
449 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000450 count,
451 j,
452 y;
cristy3ed852e2009-09-05 21:47:34 +0000453
454 unsigned char
455 *pixels,
456 *scanline;
457
cristy3ed852e2009-09-05 21:47:34 +0000458 /*
459 Determine pixel buffer size.
460 */
461 if (bits_per_pixel <= 8)
462 bytes_per_line&=0x7fff;
463 width=image->columns;
464 bytes_per_pixel=1;
465 if (bits_per_pixel == 16)
466 {
467 bytes_per_pixel=2;
468 width*=2;
469 }
470 else
471 if (bits_per_pixel == 32)
472 width*=image->matte ? 4 : 3;
473 if (bytes_per_line == 0)
474 bytes_per_line=width;
475 row_bytes=(size_t) (image->columns | 0x8000);
476 if (image->storage_class == DirectClass)
477 row_bytes=(size_t) ((4*image->columns) | 0x8000);
478 /*
479 Allocate pixel and scanline buffer.
480 */
481 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,row_bytes*
482 sizeof(*pixels));
483 if (pixels == (unsigned char *) NULL)
484 return((unsigned char *) NULL);
485 *extent=row_bytes*image->rows*sizeof(*pixels);
486 (void) ResetMagickMemory(pixels,0,*extent);
487 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
488 if (scanline == (unsigned char *) NULL)
489 return((unsigned char *) NULL);
490 if (bytes_per_line < 8)
491 {
492 /*
493 Pixels are already uncompressed.
494 */
cristybb503372010-05-27 20:51:26 +0000495 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000496 {
cristyed231572011-07-14 02:18:59 +0000497 q=pixels+y*width*GetPixelChannels(image);;
cristy3ed852e2009-09-05 21:47:34 +0000498 number_pixels=bytes_per_line;
499 count=ReadBlob(blob,(size_t) number_pixels,scanline);
cristyda16f162011-02-19 23:52:17 +0000500 (void) count;
cristy3ed852e2009-09-05 21:47:34 +0000501 p=ExpandBuffer(scanline,&number_pixels,bits_per_pixel);
502 if ((q+number_pixels) > (pixels+(*extent)))
503 {
504 (void) ThrowMagickException(&image->exception,GetMagickModule(),
505 CorruptImageError,"UnableToUncompressImage","`%s'",
506 image->filename);
507 break;
508 }
509 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
510 }
511 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
512 return(pixels);
513 }
514 /*
515 Uncompress RLE pixels into uncompressed pixel buffer.
516 */
cristybb503372010-05-27 20:51:26 +0000517 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000518 {
519 q=pixels+y*width;
520 if (bytes_per_line > 200)
521 scanline_length=ReadBlobMSBShort(blob);
522 else
523 scanline_length=1UL*ReadBlobByte(blob);
524 if (scanline_length >= row_bytes)
525 {
526 (void) ThrowMagickException(&image->exception,GetMagickModule(),
527 CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
528 break;
529 }
530 count=ReadBlob(blob,scanline_length,scanline);
cristybb503372010-05-27 20:51:26 +0000531 for (j=0; j < (ssize_t) scanline_length; )
cristy3ed852e2009-09-05 21:47:34 +0000532 if ((scanline[j] & 0x80) == 0)
533 {
534 length=(size_t) ((scanline[j] & 0xff)+1);
535 number_pixels=length*bytes_per_pixel;
536 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
537 if ((q-pixels+number_pixels) <= *extent)
538 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
539 q+=number_pixels;
cristybb503372010-05-27 20:51:26 +0000540 j+=(ssize_t) (length*bytes_per_pixel+1);
cristy3ed852e2009-09-05 21:47:34 +0000541 }
542 else
543 {
544 length=(size_t) (((scanline[j] ^ 0xff) & 0xff)+2);
545 number_pixels=bytes_per_pixel;
546 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
cristybb503372010-05-27 20:51:26 +0000547 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000548 {
549 if ((q-pixels+number_pixels) <= *extent)
550 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
551 q+=number_pixels;
552 }
cristyeaedf062010-05-29 22:36:02 +0000553 j+=(ssize_t) bytes_per_pixel+1;
cristy3ed852e2009-09-05 21:47:34 +0000554 }
555 }
556 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
557 return(pixels);
558}
559
560/*
561%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562% %
563% %
564% %
565% E n c o d e I m a g e %
566% %
567% %
568% %
569%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570%
571% EncodeImage compresses an image via Macintosh pack bits encoding
572% for Macintosh PICT images.
573%
574% The format of the EncodeImage method is:
575%
576% size_t EncodeImage(Image *image,const unsigned char *scanline,
cristybb503372010-05-27 20:51:26 +0000577% const size_t bytes_per_line,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000578%
579% A description of each parameter follows:
580%
581% o image: the address of a structure of type Image.
582%
583% o scanline: A pointer to an array of characters to pack.
584%
585% o bytes_per_line: the number of bytes in a scanline.
586%
587% o pixels: A pointer to an array of characters where the packed
588% characters are stored.
589%
cristy3ed852e2009-09-05 21:47:34 +0000590*/
591static size_t EncodeImage(Image *image,const unsigned char *scanline,
cristybb503372010-05-27 20:51:26 +0000592 const size_t bytes_per_line,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000593{
594#define MaxCount 128
595#define MaxPackbitsRunlength 128
596
cristy3ed852e2009-09-05 21:47:34 +0000597 register const unsigned char
598 *p;
599
cristybb503372010-05-27 20:51:26 +0000600 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000601 i;
602
603 register unsigned char
604 *q;
605
606 size_t
607 length;
608
cristyaff6d802011-04-26 01:46:31 +0000609 ssize_t
610 count,
611 repeat_count,
612 runlength;
613
cristy3ed852e2009-09-05 21:47:34 +0000614 unsigned char
615 index;
616
617 /*
618 Pack scanline.
619 */
620 assert(image != (Image *) NULL);
621 assert(image->signature == MagickSignature);
622 if (image->debug != MagickFalse)
623 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
624 assert(scanline != (unsigned char *) NULL);
625 assert(pixels != (unsigned char *) NULL);
626 count=0;
627 runlength=0;
628 p=scanline+(bytes_per_line-1);
629 q=pixels;
630 index=(*p);
cristybb503372010-05-27 20:51:26 +0000631 for (i=(ssize_t) bytes_per_line-1; i >= 0; i--)
cristy3ed852e2009-09-05 21:47:34 +0000632 {
633 if (index == *p)
634 runlength++;
635 else
636 {
637 if (runlength < 3)
638 while (runlength > 0)
639 {
640 *q++=(unsigned char) index;
641 runlength--;
642 count++;
643 if (count == MaxCount)
644 {
645 *q++=(unsigned char) (MaxCount-1);
646 count-=MaxCount;
647 }
648 }
649 else
650 {
651 if (count > 0)
652 *q++=(unsigned char) (count-1);
653 count=0;
654 while (runlength > 0)
655 {
656 repeat_count=runlength;
657 if (repeat_count > MaxPackbitsRunlength)
658 repeat_count=MaxPackbitsRunlength;
659 *q++=(unsigned char) index;
660 *q++=(unsigned char) (257-repeat_count);
661 runlength-=repeat_count;
662 }
663 }
664 runlength=1;
665 }
666 index=(*p);
667 p--;
668 }
669 if (runlength < 3)
670 while (runlength > 0)
671 {
672 *q++=(unsigned char) index;
673 runlength--;
674 count++;
675 if (count == MaxCount)
676 {
677 *q++=(unsigned char) (MaxCount-1);
678 count-=MaxCount;
679 }
680 }
681 else
682 {
683 if (count > 0)
684 *q++=(unsigned char) (count-1);
685 count=0;
686 while (runlength > 0)
687 {
688 repeat_count=runlength;
689 if (repeat_count > MaxPackbitsRunlength)
690 repeat_count=MaxPackbitsRunlength;
691 *q++=(unsigned char) index;
692 *q++=(unsigned char) (257-repeat_count);
693 runlength-=repeat_count;
694 }
695 }
696 if (count > 0)
697 *q++=(unsigned char) (count-1);
698 /*
699 Write the number of and the packed length.
700 */
701 length=(size_t) (q-pixels);
702 if (bytes_per_line > 200)
703 {
704 (void) WriteBlobMSBShort(image,(unsigned short) length);
705 length+=2;
706 }
707 else
708 {
709 (void) WriteBlobByte(image,(unsigned char) length);
710 length++;
711 }
712 while (q != pixels)
713 {
714 q--;
715 (void) WriteBlobByte(image,*q);
716 }
717 return(length);
718}
719
720/*
721%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722% %
723% %
724% %
725% I s P I C T %
726% %
727% %
728% %
729%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730%
731% IsPICT()() returns MagickTrue if the image format type, identified by the
732% magick string, is PICT.
733%
734% The format of the ReadPICTImage method is:
735%
736% MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
737%
738% A description of each parameter follows:
739%
740% o magick: compare image format pattern against these bytes.
741%
742% o length: Specifies the length of the magick string.
743%
cristy3ed852e2009-09-05 21:47:34 +0000744*/
745static MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
746{
747 if (length < 528)
748 return(MagickFalse);
749 if (memcmp(magick+522,"\000\021\002\377\014\000",6) == 0)
750 return(MagickTrue);
751 return(MagickFalse);
752}
753
754#if !defined(macintosh)
755/*
756%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757% %
758% %
759% %
760% R e a d P I C T I m a g e %
761% %
762% %
763% %
764%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765%
766% ReadPICTImage() reads an Apple Macintosh QuickDraw/PICT image file
767% and returns it. It allocates the memory necessary for the new Image
768% structure and returns a pointer to the new image.
769%
770% The format of the ReadPICTImage method is:
771%
772% Image *ReadPICTImage(const ImageInfo *image_info,
773% ExceptionInfo *exception)
774%
775% A description of each parameter follows:
776%
777% o image_info: the image info.
778%
779% o exception: return any errors or warnings in this structure.
780%
781*/
782
cristybb503372010-05-27 20:51:26 +0000783static inline size_t MagickMax(const size_t x,
784 const size_t y)
cristy3ed852e2009-09-05 21:47:34 +0000785{
786 if (x > y)
787 return(x);
788 return(y);
789}
790
791static Image *ReadPICTImage(const ImageInfo *image_info,
792 ExceptionInfo *exception)
793{
794 char
795 geometry[MaxTextExtent];
796
797 Image
798 *image;
799
cristy3ed852e2009-09-05 21:47:34 +0000800 int
801 c,
802 code;
803
cristy3ed852e2009-09-05 21:47:34 +0000804 MagickBooleanType
805 jpeg,
806 status;
807
808 PICTRectangle
809 frame;
810
811 PICTPixmap
812 pixmap;
813
cristy4c08aed2011-07-01 19:47:50 +0000814 Quantum
815 index;
cristy3ed852e2009-09-05 21:47:34 +0000816
cristy4c08aed2011-07-01 19:47:50 +0000817 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000818 *q;
819
cristybb503372010-05-27 20:51:26 +0000820 register ssize_t
cristy4c08aed2011-07-01 19:47:50 +0000821 i,
822 x;
cristy3ed852e2009-09-05 21:47:34 +0000823
824 size_t
825 extent,
826 length;
827
828 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000829 count,
830 flags,
831 j,
832 version,
833 y;
cristy3ed852e2009-09-05 21:47:34 +0000834
835 StringInfo
836 *profile;
837
838 /*
839 Open image file.
840 */
841 assert(image_info != (const ImageInfo *) NULL);
842 assert(image_info->signature == MagickSignature);
843 if (image_info->debug != MagickFalse)
844 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
845 image_info->filename);
846 assert(exception != (ExceptionInfo *) NULL);
847 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000848 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000849 image->depth=8;
850 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
851 if (status == MagickFalse)
852 {
853 image=DestroyImageList(image);
854 return((Image *) NULL);
855 }
856 /*
857 Read PICT header.
858 */
859 pixmap.bits_per_pixel=0;
860 pixmap.component_count=0;
861 for (i=0; i < 512; i++)
862 (void) ReadBlobByte(image); /* skip header */
863 (void) ReadBlobMSBShort(image); /* skip picture size */
864 ReadRectangle(image,frame);
865 while ((c=ReadBlobByte(image)) == 0) ;
866 if (c != 0x11)
867 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
868 version=ReadBlobByte(image);
869 if (version == 2)
870 {
871 c=ReadBlobByte(image);
872 if (c != 0xff)
873 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
874 }
875 else
876 if (version != 1)
877 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
878 if ((frame.left < 0) || (frame.right < 0) || (frame.top < 0) ||
879 (frame.bottom < 0) || (frame.left >= frame.right) ||
880 (frame.top >= frame.bottom))
881 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
882 /*
883 Create black canvas.
884 */
885 flags=0;
886 image->columns=1UL*(frame.right-frame.left);
887 image->rows=1UL*(frame.bottom-frame.top);
888 image->x_resolution=DefaultResolution;
889 image->y_resolution=DefaultResolution;
890 image->units=UndefinedResolution;
891 /*
892 Interpret PICT opcodes.
893 */
894 jpeg=MagickFalse;
895 for (code=0; EOFBlob(image) == MagickFalse; )
896 {
897 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
898 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
899 break;
900 if ((version == 1) || ((TellBlob(image) % 2) != 0))
901 code=ReadBlobByte(image);
902 if (version == 2)
903 code=(int) ReadBlobMSBShort(image);
904 if (code > 0xa1)
905 {
906 if (image->debug != MagickFalse)
907 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%04X:",code);
908 }
909 else
910 {
911 if (image->debug != MagickFalse)
912 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
913 " %04X %s: %s",code,codes[code].name,codes[code].description);
914 switch (code)
915 {
916 case 0x01:
917 {
918 /*
919 Clipping rectangle.
920 */
921 length=ReadBlobMSBShort(image);
922 if (length != 0x000a)
923 {
cristybb503372010-05-27 20:51:26 +0000924 for (i=0; i < (ssize_t) (length-2); i++)
cristy3ed852e2009-09-05 21:47:34 +0000925 (void) ReadBlobByte(image);
926 break;
927 }
928 ReadRectangle(image,frame);
929 if (((frame.left & 0x8000) != 0) || ((frame.top & 0x8000) != 0))
930 break;
931 image->columns=1UL*(frame.right-frame.left);
932 image->rows=1UL*(frame.bottom-frame.top);
933 (void) SetImageBackgroundColor(image);
934 break;
935 }
936 case 0x12:
937 case 0x13:
938 case 0x14:
939 {
cristybb503372010-05-27 20:51:26 +0000940 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000941 pattern;
942
cristybb503372010-05-27 20:51:26 +0000943 size_t
cristy3ed852e2009-09-05 21:47:34 +0000944 height,
945 width;
946
947 /*
948 Skip pattern definition.
949 */
950 pattern=1L*ReadBlobMSBShort(image);
951 for (i=0; i < 8; i++)
952 (void) ReadBlobByte(image);
953 if (pattern == 2)
954 {
955 for (i=0; i < 5; i++)
956 (void) ReadBlobByte(image);
957 break;
958 }
959 if (pattern != 1)
960 ThrowReaderException(CorruptImageError,"UnknownPatternType");
961 length=ReadBlobMSBShort(image);
962 ReadRectangle(image,frame);
963 ReadPixmap(pixmap);
964 image->depth=1UL*pixmap.component_size;
965 image->x_resolution=1.0*pixmap.horizontal_resolution;
966 image->y_resolution=1.0*pixmap.vertical_resolution;
967 image->units=PixelsPerInchResolution;
968 (void) ReadBlobMSBLong(image);
969 flags=1L*ReadBlobMSBShort(image);
970 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +0000971 for (i=0; i <= (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000972 (void) ReadBlobMSBLong(image);
973 width=1UL*(frame.bottom-frame.top);
974 height=1UL*(frame.right-frame.left);
975 if (pixmap.bits_per_pixel <= 8)
976 length&=0x7fff;
977 if (pixmap.bits_per_pixel == 16)
978 width<<=1;
979 if (length == 0)
980 length=width;
981 if (length < 8)
982 {
cristybb503372010-05-27 20:51:26 +0000983 for (i=0; i < (ssize_t) (length*height); i++)
cristy3ed852e2009-09-05 21:47:34 +0000984 (void) ReadBlobByte(image);
985 }
986 else
987 for (j=0; j < (int) height; j++)
988 if (length > 200)
cristybb503372010-05-27 20:51:26 +0000989 for (j=0; j < (ssize_t) ReadBlobMSBShort(image); j++)
cristy3ed852e2009-09-05 21:47:34 +0000990 (void) ReadBlobByte(image);
991 else
cristybb503372010-05-27 20:51:26 +0000992 for (j=0; j < (ssize_t) ReadBlobByte(image); j++)
cristy3ed852e2009-09-05 21:47:34 +0000993 (void) ReadBlobByte(image);
994 break;
995 }
996 case 0x1b:
997 {
998 /*
999 Initialize image background color.
1000 */
1001 image->background_color.red=(Quantum)
1002 ScaleShortToQuantum(ReadBlobMSBShort(image));
1003 image->background_color.green=(Quantum)
1004 ScaleShortToQuantum(ReadBlobMSBShort(image));
1005 image->background_color.blue=(Quantum)
1006 ScaleShortToQuantum(ReadBlobMSBShort(image));
1007 break;
1008 }
1009 case 0x70:
1010 case 0x71:
1011 case 0x72:
1012 case 0x73:
1013 case 0x74:
1014 case 0x75:
1015 case 0x76:
1016 case 0x77:
1017 {
1018 /*
1019 Skip polygon or region.
1020 */
1021 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001022 for (i=0; i < (ssize_t) (length-2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001023 (void) ReadBlobByte(image);
1024 break;
1025 }
1026 case 0x90:
1027 case 0x91:
1028 case 0x98:
1029 case 0x99:
1030 case 0x9a:
1031 case 0x9b:
1032 {
cristy4c08aed2011-07-01 19:47:50 +00001033 Image
1034 *tile_image;
cristy3ed852e2009-09-05 21:47:34 +00001035
1036 PICTRectangle
1037 source,
1038 destination;
1039
1040 register unsigned char
1041 *p;
1042
1043 size_t
1044 j;
1045
cristy4c08aed2011-07-01 19:47:50 +00001046 ssize_t
1047 bytes_per_line;
1048
cristy3ed852e2009-09-05 21:47:34 +00001049 unsigned char
1050 *pixels;
1051
cristy3ed852e2009-09-05 21:47:34 +00001052 /*
1053 Pixmap clipped by a rectangle.
1054 */
1055 bytes_per_line=0;
1056 if ((code != 0x9a) && (code != 0x9b))
1057 bytes_per_line=1L*ReadBlobMSBShort(image);
1058 else
1059 {
1060 (void) ReadBlobMSBShort(image);
1061 (void) ReadBlobMSBShort(image);
1062 (void) ReadBlobMSBShort(image);
1063 }
1064 ReadRectangle(image,frame);
1065 /*
1066 Initialize tile image.
1067 */
1068 tile_image=CloneImage(image,1UL*(frame.right-frame.left),
1069 1UL*(frame.bottom-frame.top),MagickTrue,exception);
1070 if (tile_image == (Image *) NULL)
1071 return((Image *) NULL);
1072 if ((code == 0x9a) || (code == 0x9b) ||
1073 ((bytes_per_line & 0x8000) != 0))
1074 {
1075 ReadPixmap(pixmap);
1076 tile_image->depth=1UL*pixmap.component_size;
1077 tile_image->matte=pixmap.component_count == 4 ?
1078 MagickTrue : MagickFalse;
1079 tile_image->x_resolution=(double) pixmap.horizontal_resolution;
1080 tile_image->y_resolution=(double) pixmap.vertical_resolution;
1081 tile_image->units=PixelsPerInchResolution;
1082 if (tile_image->matte != MagickFalse)
1083 image->matte=tile_image->matte;
1084 }
1085 if ((code != 0x9a) && (code != 0x9b))
1086 {
1087 /*
1088 Initialize colormap.
1089 */
1090 tile_image->colors=2;
1091 if ((bytes_per_line & 0x8000) != 0)
1092 {
1093 (void) ReadBlobMSBLong(image);
1094 flags=1L*ReadBlobMSBShort(image);
1095 tile_image->colors=1UL*ReadBlobMSBShort(image)+1;
1096 }
cristy018f07f2011-09-04 21:15:19 +00001097 status=AcquireImageColormap(tile_image,tile_image->colors,
1098 exception);
cristy3ed852e2009-09-05 21:47:34 +00001099 if (status == MagickFalse)
1100 {
1101 tile_image=DestroyImage(tile_image);
1102 ThrowReaderException(ResourceLimitError,
1103 "MemoryAllocationFailed");
1104 }
1105 if ((bytes_per_line & 0x8000) != 0)
1106 {
cristybb503372010-05-27 20:51:26 +00001107 for (i=0; i < (ssize_t) tile_image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001108 {
1109 j=ReadBlobMSBShort(image) % tile_image->colors;
1110 if ((flags & 0x8000) != 0)
1111 j=(size_t) i;
1112 tile_image->colormap[j].red=(Quantum)
1113 ScaleShortToQuantum(ReadBlobMSBShort(image));
1114 tile_image->colormap[j].green=(Quantum)
1115 ScaleShortToQuantum(ReadBlobMSBShort(image));
1116 tile_image->colormap[j].blue=(Quantum)
1117 ScaleShortToQuantum(ReadBlobMSBShort(image));
1118 }
1119 }
1120 else
1121 {
cristybb503372010-05-27 20:51:26 +00001122 for (i=0; i < (ssize_t) tile_image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001123 {
1124 tile_image->colormap[i].red=(Quantum) (QuantumRange-
1125 tile_image->colormap[i].red);
1126 tile_image->colormap[i].green=(Quantum) (QuantumRange-
1127 tile_image->colormap[i].green);
1128 tile_image->colormap[i].blue=(Quantum) (QuantumRange-
1129 tile_image->colormap[i].blue);
1130 }
1131 }
1132 }
1133 ReadRectangle(image,source);
1134 ReadRectangle(image,destination);
1135 (void) ReadBlobMSBShort(image);
1136 if ((code == 0x91) || (code == 0x99) || (code == 0x9b))
1137 {
1138 /*
1139 Skip region.
1140 */
1141 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001142 for (i=0; i < (ssize_t) (length-2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001143 (void) ReadBlobByte(image);
1144 }
1145 if ((code != 0x9a) && (code != 0x9b) &&
1146 (bytes_per_line & 0x8000) == 0)
1147 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1,&extent);
1148 else
1149 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1U*
1150 pixmap.bits_per_pixel,&extent);
1151 if (pixels == (unsigned char *) NULL)
1152 {
1153 tile_image=DestroyImage(tile_image);
1154 ThrowReaderException(ResourceLimitError,
1155 "MemoryAllocationFailed");
1156 }
1157 /*
1158 Convert PICT tile image to pixel packets.
1159 */
1160 p=pixels;
cristybb503372010-05-27 20:51:26 +00001161 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001162 {
1163 if (p > (pixels+extent+image->columns))
1164 ThrowReaderException(CorruptImageError,"NotEnoughPixelData");
cristy3669d042009-10-16 01:36:31 +00001165 q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,
1166 exception);
cristyacd2ed22011-08-30 01:44:23 +00001167 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001168 break;
cristybb503372010-05-27 20:51:26 +00001169 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001170 {
1171 if (tile_image->storage_class == PseudoClass)
1172 {
1173 index=ConstrainColormapIndex(tile_image,*p);
cristy4c08aed2011-07-01 19:47:50 +00001174 SetPixelIndex(tile_image,index,q);
1175 SetPixelRed(tile_image,
1176 tile_image->colormap[(ssize_t) index].red,q);
1177 SetPixelGreen(tile_image,
1178 tile_image->colormap[(ssize_t) index].green,q);
1179 SetPixelBlue(tile_image,
1180 tile_image->colormap[(ssize_t) index].blue,q);
cristy3ed852e2009-09-05 21:47:34 +00001181 }
1182 else
1183 {
1184 if (pixmap.bits_per_pixel == 16)
1185 {
1186 i=(*p++);
1187 j=(*p);
cristy4c08aed2011-07-01 19:47:50 +00001188 SetPixelRed(tile_image,ScaleCharToQuantum(
1189 (unsigned char) ((i & 0x7c) << 1)),q);
1190 SetPixelGreen(tile_image,ScaleCharToQuantum(
cristyaff6d802011-04-26 01:46:31 +00001191 (unsigned char) (((i & 0x03) << 6) |
cristy4c08aed2011-07-01 19:47:50 +00001192 ((j & 0xe0) >> 2))),q);
1193 SetPixelBlue(tile_image,ScaleCharToQuantum(
1194 (unsigned char) ((j & 0x1f) << 3)),q);
cristy3ed852e2009-09-05 21:47:34 +00001195 }
1196 else
1197 if (tile_image->matte == MagickFalse)
1198 {
1199 if (p > (pixels+extent+2*image->columns))
1200 ThrowReaderException(CorruptImageError,
1201 "NotEnoughPixelData");
cristy4c08aed2011-07-01 19:47:50 +00001202 SetPixelRed(tile_image,ScaleCharToQuantum(*p),q);
1203 SetPixelGreen(tile_image,ScaleCharToQuantum(
1204 *(p+tile_image->columns)),q);
1205 SetPixelBlue(tile_image,ScaleCharToQuantum(
1206 *(p+2*tile_image->columns)),q);
cristy3ed852e2009-09-05 21:47:34 +00001207 }
1208 else
1209 {
1210 if (p > (pixels+extent+3*image->columns))
1211 ThrowReaderException(CorruptImageError,
1212 "NotEnoughPixelData");
cristy4c08aed2011-07-01 19:47:50 +00001213 SetPixelAlpha(tile_image,ScaleCharToQuantum(*p),q);
1214 SetPixelRed(tile_image,ScaleCharToQuantum(
1215 *(p+tile_image->columns)),q);
1216 SetPixelGreen(tile_image,ScaleCharToQuantum(
1217 *(p+2*tile_image->columns)),q);
1218 SetPixelBlue(tile_image,ScaleCharToQuantum(
1219 *(p+3*tile_image->columns)),q);
cristy3ed852e2009-09-05 21:47:34 +00001220 }
1221 }
1222 p++;
cristyed231572011-07-14 02:18:59 +00001223 q+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00001224 }
1225 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
1226 break;
1227 if ((tile_image->storage_class == DirectClass) &&
1228 (pixmap.bits_per_pixel != 16))
1229 {
1230 p+=(pixmap.component_count-1)*tile_image->columns;
1231 if (p < pixels)
1232 break;
1233 }
1234 status=SetImageProgress(image,LoadImageTag,y,tile_image->rows);
1235 if (status == MagickFalse)
1236 break;
1237 }
1238 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1239 if (jpeg == MagickFalse)
1240 if ((code == 0x9a) || (code == 0x9b) ||
1241 ((bytes_per_line & 0x8000) != 0))
1242 (void) CompositeImage(image,CopyCompositeOp,tile_image,
1243 destination.left,destination.top);
1244 tile_image=DestroyImage(tile_image);
1245 break;
1246 }
1247 case 0xa1:
1248 {
1249 unsigned char
1250 *info;
1251
cristybb503372010-05-27 20:51:26 +00001252 size_t
cristy3ed852e2009-09-05 21:47:34 +00001253 type;
1254
1255 /*
1256 Comment.
1257 */
1258 type=ReadBlobMSBShort(image);
1259 length=ReadBlobMSBShort(image);
1260 if (length == 0)
1261 break;
1262 (void) ReadBlobMSBLong(image);
1263 length-=4;
1264 if (length == 0)
1265 break;
1266 info=(unsigned char *) AcquireQuantumMemory(length,sizeof(*info));
1267 if (info == (unsigned char *) NULL)
1268 break;
1269 count=ReadBlob(image,length,info);
cristyda16f162011-02-19 23:52:17 +00001270 (void) count;
cristy3ed852e2009-09-05 21:47:34 +00001271 switch (type)
1272 {
1273 case 0xe0:
1274 {
1275 if (length == 0)
1276 break;
cristy8723e4b2011-09-01 13:11:19 +00001277 profile=BlobToStringInfo((const void *) NULL,length);
cristy3ed852e2009-09-05 21:47:34 +00001278 SetStringInfoDatum(profile,info);
1279 status=SetImageProfile(image,"icc",profile);
1280 profile=DestroyStringInfo(profile);
1281 if (status == MagickFalse)
1282 ThrowReaderException(ResourceLimitError,
1283 "MemoryAllocationFailed");
1284 break;
1285 }
1286 case 0x1f2:
1287 {
1288 if (length == 0)
1289 break;
cristy8723e4b2011-09-01 13:11:19 +00001290 profile=BlobToStringInfo((const void *) NULL,length);
cristy3ed852e2009-09-05 21:47:34 +00001291 SetStringInfoDatum(profile,info);
1292 status=SetImageProfile(image,"iptc",profile);
1293 if (status == MagickFalse)
1294 ThrowReaderException(ResourceLimitError,
1295 "MemoryAllocationFailed");
1296 profile=DestroyStringInfo(profile);
1297 break;
1298 }
1299 default:
1300 break;
1301 }
1302 info=(unsigned char *) RelinquishMagickMemory(info);
1303 break;
1304 }
1305 default:
1306 {
1307 /*
1308 Skip to next op code.
1309 */
1310 if (codes[code].length == -1)
1311 (void) ReadBlobMSBShort(image);
1312 else
cristybb503372010-05-27 20:51:26 +00001313 for (i=0; i < (ssize_t) codes[code].length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001314 (void) ReadBlobByte(image);
1315 }
1316 }
1317 }
1318 if (code == 0xc00)
1319 {
1320 /*
1321 Skip header.
1322 */
1323 for (i=0; i < 24; i++)
1324 (void) ReadBlobByte(image);
1325 continue;
1326 }
1327 if (((code >= 0xb0) && (code <= 0xcf)) ||
1328 ((code >= 0x8000) && (code <= 0x80ff)))
1329 continue;
1330 if (code == 0x8200)
1331 {
1332 FILE
1333 *file;
1334
1335 Image
1336 *tile_image;
1337
1338 ImageInfo
1339 *read_info;
1340
1341 int
1342 unique_file;
1343
1344 /*
1345 Embedded JPEG.
1346 */
1347 jpeg=MagickTrue;
1348 read_info=CloneImageInfo(image_info);
1349 SetImageInfoBlob(read_info,(void *) NULL,0);
1350 file=(FILE *) NULL;
1351 unique_file=AcquireUniqueFileResource(read_info->filename);
1352 if (unique_file != -1)
cristyd0305b92009-10-19 13:12:21 +00001353 file=fdopen(unique_file,"wb");
cristy3ed852e2009-09-05 21:47:34 +00001354 if ((unique_file == -1) || (file == (FILE *) NULL))
1355 {
1356 (void) CopyMagickString(image->filename,read_info->filename,
1357 MaxTextExtent);
1358 ThrowFileException(exception,FileOpenError,
1359 "UnableToCreateTemporaryFile",image->filename);
1360 image=DestroyImageList(image);
1361 return((Image *) NULL);
1362 }
1363 length=ReadBlobMSBLong(image);
1364 for (i=0; i < 6; i++)
1365 (void) ReadBlobMSBLong(image);
1366 ReadRectangle(image,frame);
1367 for (i=0; i < 122; i++)
1368 (void) ReadBlobByte(image);
cristybb503372010-05-27 20:51:26 +00001369 for (i=0; i < (ssize_t) (length-154); i++)
cristy3ed852e2009-09-05 21:47:34 +00001370 {
1371 c=ReadBlobByte(image);
1372 (void) fputc(c,file);
1373 }
1374 (void) fclose(file);
1375 tile_image=ReadImage(read_info,exception);
1376 (void) RelinquishUniqueFileResource(read_info->filename);
1377 read_info=DestroyImageInfo(read_info);
1378 if (tile_image == (Image *) NULL)
1379 continue;
cristyb51dff52011-05-19 16:55:47 +00001380 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g",
cristye8c25f92010-06-03 00:53:06 +00001381 (double) MagickMax(image->columns,tile_image->columns),
1382 (double) MagickMax(image->rows,tile_image->rows));
cristya0511732010-02-17 02:38:35 +00001383 (void) SetImageExtent(image,
1384 MagickMax(image->columns,tile_image->columns),
cristy63240882011-08-05 19:05:27 +00001385 MagickMax(image->rows,tile_image->rows),exception);
cristy510d06a2011-07-06 23:43:54 +00001386 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001387 (void) TransformImageColorspace(image,tile_image->colorspace);
1388 (void) CompositeImage(image,CopyCompositeOp,tile_image,frame.left,
1389 frame.right);
1390 image->compression=tile_image->compression;
1391 tile_image=DestroyImage(tile_image);
1392 continue;
1393 }
1394 if ((code == 0xff) || (code == 0xffff))
1395 break;
1396 if (((code >= 0xd0) && (code <= 0xfe)) ||
1397 ((code >= 0x8100) && (code <= 0xffff)))
1398 {
1399 /*
1400 Skip reserved.
1401 */
1402 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001403 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001404 (void) ReadBlobByte(image);
1405 continue;
1406 }
1407 if ((code >= 0x100) && (code <= 0x7fff))
1408 {
1409 /*
1410 Skip reserved.
1411 */
1412 length=(size_t) ((code >> 7) & 0xff);
cristybb503372010-05-27 20:51:26 +00001413 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001414 (void) ReadBlobByte(image);
1415 continue;
1416 }
1417 }
1418 (void) CloseBlob(image);
1419 return(GetFirstImageInList(image));
1420}
1421#endif
1422
1423/*
1424%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1425% %
1426% %
1427% %
1428% R e g i s t e r P I C T I m a g e %
1429% %
1430% %
1431% %
1432%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1433%
1434% RegisterPICTImage() adds attributes for the PICT image format to
1435% the list of supported formats. The attributes include the image format
1436% tag, a method to read and/or write the format, whether the format
1437% supports the saving of more than one frame to the same file or blob,
1438% whether the format supports native in-memory I/O, and a brief
1439% description of the format.
1440%
1441% The format of the RegisterPICTImage method is:
1442%
cristybb503372010-05-27 20:51:26 +00001443% size_t RegisterPICTImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001444%
1445*/
cristybb503372010-05-27 20:51:26 +00001446ModuleExport size_t RegisterPICTImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001447{
1448 MagickInfo
1449 *entry;
1450
1451 entry=SetMagickInfo("PCT");
1452 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1453 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1454 entry->adjoin=MagickFalse;
cristyffaf9782011-04-13 19:50:51 +00001455 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001456 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1457 entry->magick=(IsImageFormatHandler *) IsPICT;
1458 entry->module=ConstantString("PICT");
1459 (void) RegisterMagickInfo(entry);
1460 entry=SetMagickInfo("PICT");
1461 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1462 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1463 entry->adjoin=MagickFalse;
cristyffaf9782011-04-13 19:50:51 +00001464 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001465 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1466 entry->magick=(IsImageFormatHandler *) IsPICT;
1467 entry->module=ConstantString("PICT");
1468 (void) RegisterMagickInfo(entry);
1469 return(MagickImageCoderSignature);
1470}
1471
1472/*
1473%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1474% %
1475% %
1476% %
1477% U n r e g i s t e r P I C T I m a g e %
1478% %
1479% %
1480% %
1481%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1482%
1483% UnregisterPICTImage() removes format registrations made by the
1484% PICT module from the list of supported formats.
1485%
1486% The format of the UnregisterPICTImage method is:
1487%
1488% UnregisterPICTImage(void)
1489%
1490*/
1491ModuleExport void UnregisterPICTImage(void)
1492{
1493 (void) UnregisterMagickInfo("PCT");
1494 (void) UnregisterMagickInfo("PICT");
1495}
1496
1497/*
1498%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1499% %
1500% %
1501% %
1502% W r i t e P I C T I m a g e %
1503% %
1504% %
1505% %
1506%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1507%
1508% WritePICTImage() writes an image to a file in the Apple Macintosh
1509% QuickDraw/PICT image format.
1510%
1511% The format of the WritePICTImage method is:
1512%
cristy3669d042009-10-16 01:36:31 +00001513% MagickBooleanType WritePICTImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +00001514% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001515%
1516% A description of each parameter follows.
1517%
1518% o image_info: the image info.
1519%
1520% o image: The image.
1521%
cristy1e178e72011-08-28 19:44:34 +00001522% o exception: return any errors or warnings in this structure.
1523%
cristy3ed852e2009-09-05 21:47:34 +00001524*/
1525static MagickBooleanType WritePICTImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +00001526 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001527{
1528#define MaxCount 128
1529#define PictCropRegionOp 0x01
1530#define PictEndOfPictureOp 0xff
1531#define PictJPEGOp 0x8200
1532#define PictInfoOp 0x0C00
1533#define PictInfoSize 512
1534#define PictPixmapOp 0x9A
1535#define PictPICTOp 0x98
1536#define PictVersion 0x11
1537
1538 const StringInfo
1539 *profile;
1540
1541 double
1542 x_resolution,
1543 y_resolution;
1544
cristy3ed852e2009-09-05 21:47:34 +00001545 MagickBooleanType
1546 status;
1547
1548 MagickOffsetType
1549 offset;
1550
1551 PICTPixmap
1552 pixmap;
1553
1554 PICTRectangle
1555 bounds,
1556 crop_rectangle,
1557 destination_rectangle,
1558 frame_rectangle,
1559 size_rectangle,
1560 source_rectangle;
1561
cristy4c08aed2011-07-01 19:47:50 +00001562 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001563 *p;
1564
cristybb503372010-05-27 20:51:26 +00001565 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001566 i,
1567 x;
1568
1569 size_t
cristyaff6d802011-04-26 01:46:31 +00001570 bytes_per_line,
1571 count,
1572 storage_class;
1573
1574 ssize_t
1575 y;
cristy3ed852e2009-09-05 21:47:34 +00001576
1577 unsigned char
1578 *buffer,
1579 *packed_scanline,
1580 *scanline;
1581
cristy3ed852e2009-09-05 21:47:34 +00001582 unsigned short
1583 base_address,
1584 row_bytes,
1585 transfer_mode;
1586
1587 /*
1588 Open output image file.
1589 */
1590 assert(image_info != (const ImageInfo *) NULL);
1591 assert(image_info->signature == MagickSignature);
1592 assert(image != (Image *) NULL);
1593 assert(image->signature == MagickSignature);
1594 if (image->debug != MagickFalse)
1595 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1596 if ((image->columns > 65535L) || (image->rows > 65535L))
1597 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
cristy3a37efd2011-08-28 20:31:03 +00001598 assert(exception != (ExceptionInfo *) NULL);
1599 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +00001600 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001601 if (status == MagickFalse)
1602 return(status);
cristy510d06a2011-07-06 23:43:54 +00001603 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001604 (void) TransformImageColorspace(image,RGBColorspace);
1605 /*
1606 Initialize image info.
1607 */
1608 size_rectangle.top=0;
1609 size_rectangle.left=0;
1610 size_rectangle.bottom=(short) image->rows;
1611 size_rectangle.right=(short) image->columns;
1612 frame_rectangle=size_rectangle;
1613 crop_rectangle=size_rectangle;
1614 source_rectangle=size_rectangle;
1615 destination_rectangle=size_rectangle;
1616 base_address=0xff;
1617 row_bytes=(unsigned short) (image->columns | 0x8000);
1618 bounds.top=0;
1619 bounds.left=0;
1620 bounds.bottom=(short) image->rows;
1621 bounds.right=(short) image->columns;
1622 pixmap.version=0;
1623 pixmap.pack_type=0;
1624 pixmap.pack_size=0;
1625 pixmap.pixel_type=0;
1626 pixmap.bits_per_pixel=8;
1627 pixmap.component_count=1;
1628 pixmap.component_size=8;
1629 pixmap.plane_bytes=0;
1630 pixmap.table=0;
1631 pixmap.reserved=0;
1632 transfer_mode=0;
1633 x_resolution=image->x_resolution != 0.0 ? image->x_resolution :
1634 DefaultResolution;
1635 y_resolution=image->y_resolution != 0.0 ? image->y_resolution :
1636 DefaultResolution;
1637 storage_class=image->storage_class;
1638 if (image_info->compression == JPEGCompression)
1639 storage_class=DirectClass;
1640 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1641 {
1642 pixmap.component_count=image->matte ? 4 : 3;
1643 pixmap.pixel_type=16;
1644 pixmap.bits_per_pixel=32;
1645 pixmap.pack_type=0x04;
1646 transfer_mode=0x40;
1647 row_bytes=(unsigned short) ((4*image->columns) | 0x8000);
1648 }
1649 /*
1650 Allocate memory.
1651 */
1652 bytes_per_line=image->columns;
1653 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1654 bytes_per_line*=image->matte ? 4 : 3;
1655 buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer));
1656 packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t)
1657 (row_bytes+MaxCount),sizeof(*packed_scanline));
1658 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
1659 if ((buffer == (unsigned char *) NULL) ||
1660 (packed_scanline == (unsigned char *) NULL) ||
1661 (scanline == (unsigned char *) NULL))
1662 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1663 (void) ResetMagickMemory(scanline,0,row_bytes);
1664 (void) ResetMagickMemory(packed_scanline,0,(size_t) (row_bytes+MaxCount));
1665 /*
1666 Write header, header size, size bounding box, version, and reserved.
1667 */
1668 (void) ResetMagickMemory(buffer,0,PictInfoSize);
1669 (void) WriteBlob(image,PictInfoSize,buffer);
1670 (void) WriteBlobMSBShort(image,0);
1671 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top);
1672 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left);
1673 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom);
1674 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right);
1675 (void) WriteBlobMSBShort(image,PictVersion);
1676 (void) WriteBlobMSBShort(image,0x02ff); /* version #2 */
1677 (void) WriteBlobMSBShort(image,PictInfoOp);
1678 (void) WriteBlobMSBLong(image,0xFFFE0000UL);
1679 /*
1680 Write full size of the file, resolution, frame bounding box, and reserved.
1681 */
1682 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1683 (void) WriteBlobMSBShort(image,0x0000);
1684 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1685 (void) WriteBlobMSBShort(image,0x0000);
1686 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top);
1687 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left);
1688 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom);
1689 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right);
1690 (void) WriteBlobMSBLong(image,0x00000000L);
1691 profile=GetImageProfile(image,"iptc");
1692 if (profile != (StringInfo *) NULL)
1693 {
1694 (void) WriteBlobMSBShort(image,0xa1);
1695 (void) WriteBlobMSBShort(image,0x1f2);
cristyed552522009-10-16 14:04:35 +00001696 (void) WriteBlobMSBShort(image,(unsigned short)
1697 (GetStringInfoLength(profile)+4));
cristy3ed852e2009-09-05 21:47:34 +00001698 (void) WriteBlobString(image,"8BIM");
cristyed552522009-10-16 14:04:35 +00001699 (void) WriteBlob(image,GetStringInfoLength(profile),
1700 GetStringInfoDatum(profile));
cristy3ed852e2009-09-05 21:47:34 +00001701 }
1702 profile=GetImageProfile(image,"icc");
1703 if (profile != (StringInfo *) NULL)
1704 {
1705 (void) WriteBlobMSBShort(image,0xa1);
1706 (void) WriteBlobMSBShort(image,0xe0);
cristyed552522009-10-16 14:04:35 +00001707 (void) WriteBlobMSBShort(image,(unsigned short)
1708 (GetStringInfoLength(profile)+4));
cristy3ed852e2009-09-05 21:47:34 +00001709 (void) WriteBlobMSBLong(image,0x00000000UL);
cristyed552522009-10-16 14:04:35 +00001710 (void) WriteBlob(image,GetStringInfoLength(profile),
1711 GetStringInfoDatum(profile));
cristy3ed852e2009-09-05 21:47:34 +00001712 (void) WriteBlobMSBShort(image,0xa1);
1713 (void) WriteBlobMSBShort(image,0xe0);
1714 (void) WriteBlobMSBShort(image,4);
1715 (void) WriteBlobMSBLong(image,0x00000002UL);
1716 }
1717 /*
1718 Write crop region opcode and crop bounding box.
1719 */
1720 (void) WriteBlobMSBShort(image,PictCropRegionOp);
1721 (void) WriteBlobMSBShort(image,0xa);
1722 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top);
1723 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left);
1724 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom);
1725 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right);
1726 if (image_info->compression == JPEGCompression)
1727 {
1728 Image
1729 *jpeg_image;
1730
1731 ImageInfo
1732 *jpeg_info;
1733
1734 size_t
1735 length;
1736
1737 unsigned char
1738 *blob;
1739
cristy1e178e72011-08-28 19:44:34 +00001740 jpeg_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001741 if (jpeg_image == (Image *) NULL)
1742 {
1743 (void) CloseBlob(image);
1744 return(MagickFalse);
1745 }
1746 jpeg_info=CloneImageInfo(image_info);
1747 (void) CopyMagickString(jpeg_info->magick,"JPEG",MaxTextExtent);
1748 length=0;
1749 blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length,
cristy1e178e72011-08-28 19:44:34 +00001750 exception);
cristy3ed852e2009-09-05 21:47:34 +00001751 jpeg_info=DestroyImageInfo(jpeg_info);
1752 if (blob == (unsigned char *) NULL)
1753 return(MagickFalse);
1754 jpeg_image=DestroyImage(jpeg_image);
1755 (void) WriteBlobMSBShort(image,PictJPEGOp);
cristyeaedf062010-05-29 22:36:02 +00001756 (void) WriteBlobMSBLong(image,(unsigned int) length+154);
cristy3ed852e2009-09-05 21:47:34 +00001757 (void) WriteBlobMSBShort(image,0x0000);
1758 (void) WriteBlobMSBLong(image,0x00010000UL);
1759 (void) WriteBlobMSBLong(image,0x00000000UL);
1760 (void) WriteBlobMSBLong(image,0x00000000UL);
1761 (void) WriteBlobMSBLong(image,0x00000000UL);
1762 (void) WriteBlobMSBLong(image,0x00010000UL);
1763 (void) WriteBlobMSBLong(image,0x00000000UL);
1764 (void) WriteBlobMSBLong(image,0x00000000UL);
1765 (void) WriteBlobMSBLong(image,0x00000000UL);
1766 (void) WriteBlobMSBLong(image,0x40000000UL);
1767 (void) WriteBlobMSBLong(image,0x00000000UL);
1768 (void) WriteBlobMSBLong(image,0x00000000UL);
1769 (void) WriteBlobMSBLong(image,0x00000000UL);
1770 (void) WriteBlobMSBLong(image,0x00400000UL);
1771 (void) WriteBlobMSBShort(image,0x0000);
1772 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1773 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1774 (void) WriteBlobMSBShort(image,0x0000);
1775 (void) WriteBlobMSBShort(image,768);
1776 (void) WriteBlobMSBShort(image,0x0000);
1777 (void) WriteBlobMSBLong(image,0x00000000UL);
1778 (void) WriteBlobMSBLong(image,0x00566A70UL);
1779 (void) WriteBlobMSBLong(image,0x65670000UL);
1780 (void) WriteBlobMSBLong(image,0x00000000UL);
1781 (void) WriteBlobMSBLong(image,0x00000001UL);
1782 (void) WriteBlobMSBLong(image,0x00016170UL);
1783 (void) WriteBlobMSBLong(image,0x706C0000UL);
1784 (void) WriteBlobMSBLong(image,0x00000000UL);
1785 (void) WriteBlobMSBShort(image,768);
1786 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1787 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1788 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1789 (void) WriteBlobMSBShort(image,0x0000);
1790 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1791 (void) WriteBlobMSBLong(image,0x00000000UL);
1792 (void) WriteBlobMSBLong(image,0x87AC0001UL);
1793 (void) WriteBlobMSBLong(image,0x0B466F74UL);
1794 (void) WriteBlobMSBLong(image,0x6F202D20UL);
1795 (void) WriteBlobMSBLong(image,0x4A504547UL);
1796 (void) WriteBlobMSBLong(image,0x00000000UL);
1797 (void) WriteBlobMSBLong(image,0x00000000UL);
1798 (void) WriteBlobMSBLong(image,0x00000000UL);
1799 (void) WriteBlobMSBLong(image,0x00000000UL);
1800 (void) WriteBlobMSBLong(image,0x00000000UL);
1801 (void) WriteBlobMSBLong(image,0x0018FFFFUL);
1802 (void) WriteBlob(image,length,blob);
1803 if ((length & 0x01) != 0)
1804 (void) WriteBlobByte(image,'\0');
1805 blob=(unsigned char *) RelinquishMagickMemory(blob);
1806 }
1807 /*
1808 Write picture opcode, row bytes, and picture bounding box, and version.
1809 */
1810 if (storage_class == PseudoClass)
1811 (void) WriteBlobMSBShort(image,PictPICTOp);
1812 else
1813 {
1814 (void) WriteBlobMSBShort(image,PictPixmapOp);
cristybb503372010-05-27 20:51:26 +00001815 (void) WriteBlobMSBLong(image,(size_t) base_address);
cristy3ed852e2009-09-05 21:47:34 +00001816 }
1817 (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000));
1818 (void) WriteBlobMSBShort(image,(unsigned short) bounds.top);
1819 (void) WriteBlobMSBShort(image,(unsigned short) bounds.left);
1820 (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom);
1821 (void) WriteBlobMSBShort(image,(unsigned short) bounds.right);
1822 /*
1823 Write pack type, pack size, resolution, pixel type, and pixel size.
1824 */
1825 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version);
1826 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type);
cristyeaedf062010-05-29 22:36:02 +00001827 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.pack_size);
cristy3ed852e2009-09-05 21:47:34 +00001828 (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5));
1829 (void) WriteBlobMSBShort(image,0x0000);
1830 (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5));
1831 (void) WriteBlobMSBShort(image,0x0000);
1832 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type);
1833 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel);
1834 /*
1835 Write component count, size, plane bytes, table size, and reserved.
1836 */
1837 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count);
1838 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size);
cristyeaedf062010-05-29 22:36:02 +00001839 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.plane_bytes);
1840 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.table);
1841 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.reserved);
cristy3ed852e2009-09-05 21:47:34 +00001842 if (storage_class == PseudoClass)
1843 {
1844 /*
1845 Write image colormap.
1846 */
1847 (void) WriteBlobMSBLong(image,0x00000000L); /* color seed */
1848 (void) WriteBlobMSBShort(image,0L); /* color flags */
1849 (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1));
cristybb503372010-05-27 20:51:26 +00001850 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001851 {
1852 (void) WriteBlobMSBShort(image,(unsigned short) i);
1853 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1854 image->colormap[i].red));
1855 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1856 image->colormap[i].green));
1857 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1858 image->colormap[i].blue));
1859 }
1860 }
1861 /*
1862 Write source and destination rectangle.
1863 */
1864 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top);
1865 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left);
1866 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom);
1867 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right);
1868 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top);
1869 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left);
1870 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom);
1871 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right);
1872 (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode);
1873 /*
1874 Write picture data.
1875 */
1876 count=0;
1877 if ((storage_class == PseudoClass) && (image->matte == MagickFalse))
cristybb503372010-05-27 20:51:26 +00001878 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001879 {
cristy1e178e72011-08-28 19:44:34 +00001880 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001881 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001882 break;
cristybb503372010-05-27 20:51:26 +00001883 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001884 {
1885 scanline[x]=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001886 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001887 }
cristybb503372010-05-27 20:51:26 +00001888 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
cristy3ed852e2009-09-05 21:47:34 +00001889 packed_scanline);
cristy8b27a6d2010-02-14 03:31:15 +00001890 if (image->previous == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001891 {
cristycee97112010-05-28 00:44:52 +00001892 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +00001893 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001894 if (status == MagickFalse)
1895 break;
1896 }
1897 }
1898 else
1899 if (image_info->compression == JPEGCompression)
1900 {
1901 (void) ResetMagickMemory(scanline,0,row_bytes);
cristybb503372010-05-27 20:51:26 +00001902 for (y=0; y < (ssize_t) image->rows; y++)
cristyaff6d802011-04-26 01:46:31 +00001903 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
1904 packed_scanline);
cristy3ed852e2009-09-05 21:47:34 +00001905 }
1906 else
1907 {
1908 register unsigned char
1909 *blue,
1910 *green,
1911 *opacity,
1912 *red;
1913
1914 red=scanline;
1915 green=scanline+image->columns;
1916 blue=scanline+2*image->columns;
1917 opacity=scanline+3*image->columns;
cristybb503372010-05-27 20:51:26 +00001918 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001919 {
cristy1e178e72011-08-28 19:44:34 +00001920 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001921 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001922 break;
1923 red=scanline;
1924 green=scanline+image->columns;
1925 blue=scanline+2*image->columns;
1926 if (image->matte != MagickFalse)
1927 {
1928 opacity=scanline;
1929 red=scanline+image->columns;
1930 green=scanline+2*image->columns;
1931 blue=scanline+3*image->columns;
1932 }
cristybb503372010-05-27 20:51:26 +00001933 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001934 {
cristy4c08aed2011-07-01 19:47:50 +00001935 *red++=ScaleQuantumToChar(GetPixelRed(image,p));
1936 *green++=ScaleQuantumToChar(GetPixelGreen(image,p));
1937 *blue++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001938 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001939 *opacity++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
cristyed231572011-07-14 02:18:59 +00001940 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001941 }
1942 count+=EncodeImage(image,scanline,bytes_per_line & 0x7FFF,
1943 packed_scanline);
cristy8b27a6d2010-02-14 03:31:15 +00001944 if (image->previous == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001945 {
cristycee97112010-05-28 00:44:52 +00001946 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1947 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001948 if (status == MagickFalse)
1949 break;
1950 }
1951 }
1952 }
1953 if ((count & 0x01) != 0)
1954 (void) WriteBlobByte(image,'\0');
1955 (void) WriteBlobMSBShort(image,PictEndOfPictureOp);
1956 offset=TellBlob(image);
1957 offset=SeekBlob(image,512,SEEK_SET);
1958 (void) WriteBlobMSBShort(image,(unsigned short) offset);
1959 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
1960 packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline);
1961 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1962 (void) CloseBlob(image);
1963 return(MagickTrue);
1964}