blob: d514d69a05eee2e986ffc230cc2d954c952c711f [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"
50#include "MagickCore/composite.h"
51#include "MagickCore/constitute.h"
52#include "MagickCore/exception.h"
53#include "MagickCore/exception-private.h"
54#include "MagickCore/image.h"
55#include "MagickCore/image-private.h"
56#include "MagickCore/list.h"
57#include "MagickCore/log.h"
58#include "MagickCore/magick.h"
59#include "MagickCore/memory_.h"
60#include "MagickCore/monitor.h"
61#include "MagickCore/monitor-private.h"
62#include "MagickCore/pixel-accessor.h"
63#include "MagickCore/profile.h"
64#include "MagickCore/resource_.h"
65#include "MagickCore/quantum-private.h"
66#include "MagickCore/static.h"
67#include "MagickCore/string_.h"
68#include "MagickCore/module.h"
69#include "MagickCore/transform.h"
70#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000071
72/*
73 ImageMagick Macintosh PICT Methods.
74*/
75#define ReadPixmap(pixmap) \
76{ \
77 pixmap.version=(short) ReadBlobMSBShort(image); \
78 pixmap.pack_type=(short) ReadBlobMSBShort(image); \
79 pixmap.pack_size=ReadBlobMSBLong(image); \
80 pixmap.horizontal_resolution=1UL*ReadBlobMSBShort(image); \
81 (void) ReadBlobMSBShort(image); \
82 pixmap.vertical_resolution=1UL*ReadBlobMSBShort(image); \
83 (void) ReadBlobMSBShort(image); \
84 pixmap.pixel_type=(short) ReadBlobMSBShort(image); \
85 pixmap.bits_per_pixel=(short) ReadBlobMSBShort(image); \
86 pixmap.component_count=(short) ReadBlobMSBShort(image); \
87 pixmap.component_size=(short) ReadBlobMSBShort(image); \
88 pixmap.plane_bytes=ReadBlobMSBLong(image); \
89 pixmap.table=ReadBlobMSBLong(image); \
90 pixmap.reserved=ReadBlobMSBLong(image); \
91 if ((pixmap.bits_per_pixel <= 0) || (pixmap.bits_per_pixel > 32) || \
92 (pixmap.component_count <= 0) || (pixmap.component_count > 4) || \
93 (pixmap.component_size <= 0)) \
94 ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
95}
96
97#define ReadRectangle(image,rectangle) \
98{ \
99 rectangle.top=(short) ReadBlobMSBShort(image); \
100 rectangle.left=(short) ReadBlobMSBShort(image); \
101 rectangle.bottom=(short) ReadBlobMSBShort(image); \
102 rectangle.right=(short) ReadBlobMSBShort(image); \
103 if ((rectangle.left > rectangle.right) || \
104 (rectangle.top > rectangle.bottom)) \
105 ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
106}
107
108typedef struct _PICTCode
109{
110 const char
111 *name;
112
cristybb503372010-05-27 20:51:26 +0000113 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000114 length;
115
116 const char
117 *description;
118} PICTCode;
119
120typedef struct _PICTPixmap
121{
122 short
123 version,
124 pack_type;
125
cristybb503372010-05-27 20:51:26 +0000126 size_t
cristy3ed852e2009-09-05 21:47:34 +0000127 pack_size,
128 horizontal_resolution,
129 vertical_resolution;
130
131 short
132 pixel_type,
133 bits_per_pixel,
134 component_count,
135 component_size;
136
cristybb503372010-05-27 20:51:26 +0000137 size_t
cristy3ed852e2009-09-05 21:47:34 +0000138 plane_bytes,
139 table,
140 reserved;
141} PICTPixmap;
142
143typedef struct _PICTRectangle
144{
145 short
146 top,
147 left,
148 bottom,
149 right;
150} PICTRectangle;
151
152static const PICTCode
153 codes[] =
154 {
155 /* 0x00 */ { "NOP", 0, "nop" },
156 /* 0x01 */ { "Clip", 0, "clip" },
157 /* 0x02 */ { "BkPat", 8, "background pattern" },
158 /* 0x03 */ { "TxFont", 2, "text font (word)" },
159 /* 0x04 */ { "TxFace", 1, "text face (byte)" },
160 /* 0x05 */ { "TxMode", 2, "text mode (word)" },
161 /* 0x06 */ { "SpExtra", 4, "space extra (fixed point)" },
162 /* 0x07 */ { "PnSize", 4, "pen size (point)" },
163 /* 0x08 */ { "PnMode", 2, "pen mode (word)" },
164 /* 0x09 */ { "PnPat", 8, "pen pattern" },
165 /* 0x0a */ { "FillPat", 8, "fill pattern" },
166 /* 0x0b */ { "OvSize", 4, "oval size (point)" },
167 /* 0x0c */ { "Origin", 4, "dh, dv (word)" },
168 /* 0x0d */ { "TxSize", 2, "text size (word)" },
cristybb503372010-05-27 20:51:26 +0000169 /* 0x0e */ { "FgColor", 4, "foreground color (ssize_tword)" },
170 /* 0x0f */ { "BkColor", 4, "background color (ssize_tword)" },
cristy3ed852e2009-09-05 21:47:34 +0000171 /* 0x10 */ { "TxRatio", 8, "numerator (point), denominator (point)" },
172 /* 0x11 */ { "Version", 1, "version (byte)" },
173 /* 0x12 */ { "BkPixPat", 0, "color background pattern" },
174 /* 0x13 */ { "PnPixPat", 0, "color pen pattern" },
175 /* 0x14 */ { "FillPixPat", 0, "color fill pattern" },
176 /* 0x15 */ { "PnLocHFrac", 2, "fractional pen position" },
177 /* 0x16 */ { "ChExtra", 2, "extra for each character" },
178 /* 0x17 */ { "reserved", 0, "reserved for Apple use" },
179 /* 0x18 */ { "reserved", 0, "reserved for Apple use" },
180 /* 0x19 */ { "reserved", 0, "reserved for Apple use" },
181 /* 0x1a */ { "RGBFgCol", 6, "RGB foreColor" },
182 /* 0x1b */ { "RGBBkCol", 6, "RGB backColor" },
183 /* 0x1c */ { "HiliteMode", 0, "hilite mode flag" },
184 /* 0x1d */ { "HiliteColor", 6, "RGB hilite color" },
185 /* 0x1e */ { "DefHilite", 0, "Use default hilite color" },
186 /* 0x1f */ { "OpColor", 6, "RGB OpColor for arithmetic modes" },
187 /* 0x20 */ { "Line", 8, "pnLoc (point), newPt (point)" },
188 /* 0x21 */ { "LineFrom", 4, "newPt (point)" },
189 /* 0x22 */ { "ShortLine", 6, "pnLoc (point, dh, dv (-128 .. 127))" },
190 /* 0x23 */ { "ShortLineFrom", 2, "dh, dv (-128 .. 127)" },
191 /* 0x24 */ { "reserved", -1, "reserved for Apple use" },
192 /* 0x25 */ { "reserved", -1, "reserved for Apple use" },
193 /* 0x26 */ { "reserved", -1, "reserved for Apple use" },
194 /* 0x27 */ { "reserved", -1, "reserved for Apple use" },
195 /* 0x28 */ { "LongText", 0, "txLoc (point), count (0..255), text" },
196 /* 0x29 */ { "DHText", 0, "dh (0..255), count (0..255), text" },
197 /* 0x2a */ { "DVText", 0, "dv (0..255), count (0..255), text" },
198 /* 0x2b */ { "DHDVText", 0, "dh, dv (0..255), count (0..255), text" },
199 /* 0x2c */ { "reserved", -1, "reserved for Apple use" },
200 /* 0x2d */ { "reserved", -1, "reserved for Apple use" },
201 /* 0x2e */ { "reserved", -1, "reserved for Apple use" },
202 /* 0x2f */ { "reserved", -1, "reserved for Apple use" },
203 /* 0x30 */ { "frameRect", 8, "rect" },
204 /* 0x31 */ { "paintRect", 8, "rect" },
205 /* 0x32 */ { "eraseRect", 8, "rect" },
206 /* 0x33 */ { "invertRect", 8, "rect" },
207 /* 0x34 */ { "fillRect", 8, "rect" },
208 /* 0x35 */ { "reserved", 8, "reserved for Apple use" },
209 /* 0x36 */ { "reserved", 8, "reserved for Apple use" },
210 /* 0x37 */ { "reserved", 8, "reserved for Apple use" },
211 /* 0x38 */ { "frameSameRect", 0, "rect" },
212 /* 0x39 */ { "paintSameRect", 0, "rect" },
213 /* 0x3a */ { "eraseSameRect", 0, "rect" },
214 /* 0x3b */ { "invertSameRect", 0, "rect" },
215 /* 0x3c */ { "fillSameRect", 0, "rect" },
216 /* 0x3d */ { "reserved", 0, "reserved for Apple use" },
217 /* 0x3e */ { "reserved", 0, "reserved for Apple use" },
218 /* 0x3f */ { "reserved", 0, "reserved for Apple use" },
219 /* 0x40 */ { "frameRRect", 8, "rect" },
220 /* 0x41 */ { "paintRRect", 8, "rect" },
221 /* 0x42 */ { "eraseRRect", 8, "rect" },
222 /* 0x43 */ { "invertRRect", 8, "rect" },
223 /* 0x44 */ { "fillRRrect", 8, "rect" },
224 /* 0x45 */ { "reserved", 8, "reserved for Apple use" },
225 /* 0x46 */ { "reserved", 8, "reserved for Apple use" },
226 /* 0x47 */ { "reserved", 8, "reserved for Apple use" },
227 /* 0x48 */ { "frameSameRRect", 0, "rect" },
228 /* 0x49 */ { "paintSameRRect", 0, "rect" },
229 /* 0x4a */ { "eraseSameRRect", 0, "rect" },
230 /* 0x4b */ { "invertSameRRect", 0, "rect" },
231 /* 0x4c */ { "fillSameRRect", 0, "rect" },
232 /* 0x4d */ { "reserved", 0, "reserved for Apple use" },
233 /* 0x4e */ { "reserved", 0, "reserved for Apple use" },
234 /* 0x4f */ { "reserved", 0, "reserved for Apple use" },
235 /* 0x50 */ { "frameOval", 8, "rect" },
236 /* 0x51 */ { "paintOval", 8, "rect" },
237 /* 0x52 */ { "eraseOval", 8, "rect" },
238 /* 0x53 */ { "invertOval", 8, "rect" },
239 /* 0x54 */ { "fillOval", 8, "rect" },
240 /* 0x55 */ { "reserved", 8, "reserved for Apple use" },
241 /* 0x56 */ { "reserved", 8, "reserved for Apple use" },
242 /* 0x57 */ { "reserved", 8, "reserved for Apple use" },
243 /* 0x58 */ { "frameSameOval", 0, "rect" },
244 /* 0x59 */ { "paintSameOval", 0, "rect" },
245 /* 0x5a */ { "eraseSameOval", 0, "rect" },
246 /* 0x5b */ { "invertSameOval", 0, "rect" },
247 /* 0x5c */ { "fillSameOval", 0, "rect" },
248 /* 0x5d */ { "reserved", 0, "reserved for Apple use" },
249 /* 0x5e */ { "reserved", 0, "reserved for Apple use" },
250 /* 0x5f */ { "reserved", 0, "reserved for Apple use" },
251 /* 0x60 */ { "frameArc", 12, "rect, startAngle, arcAngle" },
252 /* 0x61 */ { "paintArc", 12, "rect, startAngle, arcAngle" },
253 /* 0x62 */ { "eraseArc", 12, "rect, startAngle, arcAngle" },
254 /* 0x63 */ { "invertArc", 12, "rect, startAngle, arcAngle" },
255 /* 0x64 */ { "fillArc", 12, "rect, startAngle, arcAngle" },
256 /* 0x65 */ { "reserved", 12, "reserved for Apple use" },
257 /* 0x66 */ { "reserved", 12, "reserved for Apple use" },
258 /* 0x67 */ { "reserved", 12, "reserved for Apple use" },
259 /* 0x68 */ { "frameSameArc", 4, "rect, startAngle, arcAngle" },
260 /* 0x69 */ { "paintSameArc", 4, "rect, startAngle, arcAngle" },
261 /* 0x6a */ { "eraseSameArc", 4, "rect, startAngle, arcAngle" },
262 /* 0x6b */ { "invertSameArc", 4, "rect, startAngle, arcAngle" },
263 /* 0x6c */ { "fillSameArc", 4, "rect, startAngle, arcAngle" },
264 /* 0x6d */ { "reserved", 4, "reserved for Apple use" },
265 /* 0x6e */ { "reserved", 4, "reserved for Apple use" },
266 /* 0x6f */ { "reserved", 4, "reserved for Apple use" },
267 /* 0x70 */ { "framePoly", 0, "poly" },
268 /* 0x71 */ { "paintPoly", 0, "poly" },
269 /* 0x72 */ { "erasePoly", 0, "poly" },
270 /* 0x73 */ { "invertPoly", 0, "poly" },
271 /* 0x74 */ { "fillPoly", 0, "poly" },
272 /* 0x75 */ { "reserved", 0, "reserved for Apple use" },
273 /* 0x76 */ { "reserved", 0, "reserved for Apple use" },
274 /* 0x77 */ { "reserved", 0, "reserved for Apple use" },
275 /* 0x78 */ { "frameSamePoly", 0, "poly (NYI)" },
276 /* 0x79 */ { "paintSamePoly", 0, "poly (NYI)" },
277 /* 0x7a */ { "eraseSamePoly", 0, "poly (NYI)" },
278 /* 0x7b */ { "invertSamePoly", 0, "poly (NYI)" },
279 /* 0x7c */ { "fillSamePoly", 0, "poly (NYI)" },
280 /* 0x7d */ { "reserved", 0, "reserved for Apple use" },
281 /* 0x7e */ { "reserved", 0, "reserved for Apple use" },
282 /* 0x7f */ { "reserved", 0, "reserved for Apple use" },
283 /* 0x80 */ { "frameRgn", 0, "region" },
284 /* 0x81 */ { "paintRgn", 0, "region" },
285 /* 0x82 */ { "eraseRgn", 0, "region" },
286 /* 0x83 */ { "invertRgn", 0, "region" },
287 /* 0x84 */ { "fillRgn", 0, "region" },
288 /* 0x85 */ { "reserved", 0, "reserved for Apple use" },
289 /* 0x86 */ { "reserved", 0, "reserved for Apple use" },
290 /* 0x87 */ { "reserved", 0, "reserved for Apple use" },
291 /* 0x88 */ { "frameSameRgn", 0, "region (NYI)" },
292 /* 0x89 */ { "paintSameRgn", 0, "region (NYI)" },
293 /* 0x8a */ { "eraseSameRgn", 0, "region (NYI)" },
294 /* 0x8b */ { "invertSameRgn", 0, "region (NYI)" },
295 /* 0x8c */ { "fillSameRgn", 0, "region (NYI)" },
296 /* 0x8d */ { "reserved", 0, "reserved for Apple use" },
297 /* 0x8e */ { "reserved", 0, "reserved for Apple use" },
298 /* 0x8f */ { "reserved", 0, "reserved for Apple use" },
299 /* 0x90 */ { "BitsRect", 0, "copybits, rect clipped" },
300 /* 0x91 */ { "BitsRgn", 0, "copybits, rgn clipped" },
301 /* 0x92 */ { "reserved", -1, "reserved for Apple use" },
302 /* 0x93 */ { "reserved", -1, "reserved for Apple use" },
303 /* 0x94 */ { "reserved", -1, "reserved for Apple use" },
304 /* 0x95 */ { "reserved", -1, "reserved for Apple use" },
305 /* 0x96 */ { "reserved", -1, "reserved for Apple use" },
306 /* 0x97 */ { "reserved", -1, "reserved for Apple use" },
307 /* 0x98 */ { "PackBitsRect", 0, "packed copybits, rect clipped" },
308 /* 0x99 */ { "PackBitsRgn", 0, "packed copybits, rgn clipped" },
309 /* 0x9a */ { "DirectBitsRect", 0, "PixMap, srcRect, dstRect, mode, PixData" },
310 /* 0x9b */ { "DirectBitsRgn", 0, "PixMap, srcRect, dstRect, mode, maskRgn, PixData" },
311 /* 0x9c */ { "reserved", -1, "reserved for Apple use" },
312 /* 0x9d */ { "reserved", -1, "reserved for Apple use" },
313 /* 0x9e */ { "reserved", -1, "reserved for Apple use" },
314 /* 0x9f */ { "reserved", -1, "reserved for Apple use" },
315 /* 0xa0 */ { "ShortComment", 2, "kind (word)" },
316 /* 0xa1 */ { "LongComment", 0, "kind (word), size (word), data" }
317 };
318
319/*
320 Forward declarations.
321*/
322static MagickBooleanType
323 WritePICTImage(const ImageInfo *,Image *);
324
325/*
326%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327% %
328% %
329% %
330% D e c o d e I m a g e %
331% %
332% %
333% %
334%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335%
336% DecodeImage decompresses an image via Macintosh pack bits decoding for
337% Macintosh PICT images.
338%
339% The format of the DecodeImage method is:
340%
341% unsigned char *DecodeImage(Image *blob,Image *image,
cristybb503372010-05-27 20:51:26 +0000342% size_t bytes_per_line,const int bits_per_pixel,
cristy3ed852e2009-09-05 21:47:34 +0000343% unsigned size_t extent)
344%
345% A description of each parameter follows:
346%
347% o image_info: the image info.
348%
349% o blob,image: the address of a structure of type Image.
350%
351% o bytes_per_line: This integer identifies the number of bytes in a
352% scanline.
353%
354% o bits_per_pixel: the number of bits in a pixel.
355%
356% o extent: the number of pixels allocated.
357%
358*/
359
360static unsigned char *ExpandBuffer(unsigned char *pixels,
361 MagickSizeType *bytes_per_line,const unsigned int bits_per_pixel)
362{
cristybb503372010-05-27 20:51:26 +0000363 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000364 i;
365
366 register unsigned char
367 *p,
368 *q;
369
370 static unsigned char
371 scanline[8*256];
372
373 p=pixels;
374 q=scanline;
375 switch (bits_per_pixel)
376 {
377 case 8:
378 case 16:
379 case 32:
380 return(pixels);
381 case 4:
382 {
cristybb503372010-05-27 20:51:26 +0000383 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000384 {
385 *q++=(*p >> 4) & 0xff;
386 *q++=(*p & 15);
387 p++;
388 }
389 *bytes_per_line*=2;
390 break;
391 }
392 case 2:
393 {
cristybb503372010-05-27 20:51:26 +0000394 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000395 {
396 *q++=(*p >> 6) & 0x03;
397 *q++=(*p >> 4) & 0x03;
398 *q++=(*p >> 2) & 0x03;
399 *q++=(*p & 3);
400 p++;
401 }
402 *bytes_per_line*=4;
403 break;
404 }
405 case 1:
406 {
cristybb503372010-05-27 20:51:26 +0000407 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000408 {
409 *q++=(*p >> 7) & 0x01;
410 *q++=(*p >> 6) & 0x01;
411 *q++=(*p >> 5) & 0x01;
412 *q++=(*p >> 4) & 0x01;
413 *q++=(*p >> 3) & 0x01;
414 *q++=(*p >> 2) & 0x01;
415 *q++=(*p >> 1) & 0x01;
416 *q++=(*p & 0x01);
417 p++;
418 }
419 *bytes_per_line*=8;
420 break;
421 }
422 default:
423 break;
424 }
425 return(scanline);
426}
427
428static unsigned char *DecodeImage(Image *blob,Image *image,
cristybb503372010-05-27 20:51:26 +0000429 size_t bytes_per_line,const unsigned int bits_per_pixel,size_t *extent)
cristy3ed852e2009-09-05 21:47:34 +0000430{
cristy3ed852e2009-09-05 21:47:34 +0000431 MagickSizeType
432 number_pixels;
433
cristybb503372010-05-27 20:51:26 +0000434 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000435 i;
436
437 register unsigned char
438 *p,
439 *q;
440
441 size_t
cristyaff6d802011-04-26 01:46:31 +0000442 bytes_per_pixel,
cristy3ed852e2009-09-05 21:47:34 +0000443 length,
cristyaff6d802011-04-26 01:46:31 +0000444 row_bytes,
445 scanline_length,
446 width;
cristy3ed852e2009-09-05 21:47:34 +0000447
448 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000449 count,
450 j,
451 y;
cristy3ed852e2009-09-05 21:47:34 +0000452
453 unsigned char
454 *pixels,
455 *scanline;
456
cristy3ed852e2009-09-05 21:47:34 +0000457 /*
458 Determine pixel buffer size.
459 */
460 if (bits_per_pixel <= 8)
461 bytes_per_line&=0x7fff;
462 width=image->columns;
463 bytes_per_pixel=1;
464 if (bits_per_pixel == 16)
465 {
466 bytes_per_pixel=2;
467 width*=2;
468 }
469 else
470 if (bits_per_pixel == 32)
471 width*=image->matte ? 4 : 3;
472 if (bytes_per_line == 0)
473 bytes_per_line=width;
474 row_bytes=(size_t) (image->columns | 0x8000);
475 if (image->storage_class == DirectClass)
476 row_bytes=(size_t) ((4*image->columns) | 0x8000);
477 /*
478 Allocate pixel and scanline buffer.
479 */
480 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,row_bytes*
481 sizeof(*pixels));
482 if (pixels == (unsigned char *) NULL)
483 return((unsigned char *) NULL);
484 *extent=row_bytes*image->rows*sizeof(*pixels);
485 (void) ResetMagickMemory(pixels,0,*extent);
486 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
487 if (scanline == (unsigned char *) NULL)
488 return((unsigned char *) NULL);
489 if (bytes_per_line < 8)
490 {
491 /*
492 Pixels are already uncompressed.
493 */
cristybb503372010-05-27 20:51:26 +0000494 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000495 {
cristy4c08aed2011-07-01 19:47:50 +0000496 q=pixels+y*width*GetPixelChannels(image);;
cristy3ed852e2009-09-05 21:47:34 +0000497 number_pixels=bytes_per_line;
498 count=ReadBlob(blob,(size_t) number_pixels,scanline);
cristyda16f162011-02-19 23:52:17 +0000499 (void) count;
cristy3ed852e2009-09-05 21:47:34 +0000500 p=ExpandBuffer(scanline,&number_pixels,bits_per_pixel);
501 if ((q+number_pixels) > (pixels+(*extent)))
502 {
503 (void) ThrowMagickException(&image->exception,GetMagickModule(),
504 CorruptImageError,"UnableToUncompressImage","`%s'",
505 image->filename);
506 break;
507 }
508 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
509 }
510 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
511 return(pixels);
512 }
513 /*
514 Uncompress RLE pixels into uncompressed pixel buffer.
515 */
cristybb503372010-05-27 20:51:26 +0000516 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000517 {
518 q=pixels+y*width;
519 if (bytes_per_line > 200)
520 scanline_length=ReadBlobMSBShort(blob);
521 else
522 scanline_length=1UL*ReadBlobByte(blob);
523 if (scanline_length >= row_bytes)
524 {
525 (void) ThrowMagickException(&image->exception,GetMagickModule(),
526 CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
527 break;
528 }
529 count=ReadBlob(blob,scanline_length,scanline);
cristybb503372010-05-27 20:51:26 +0000530 for (j=0; j < (ssize_t) scanline_length; )
cristy3ed852e2009-09-05 21:47:34 +0000531 if ((scanline[j] & 0x80) == 0)
532 {
533 length=(size_t) ((scanline[j] & 0xff)+1);
534 number_pixels=length*bytes_per_pixel;
535 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
536 if ((q-pixels+number_pixels) <= *extent)
537 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
538 q+=number_pixels;
cristybb503372010-05-27 20:51:26 +0000539 j+=(ssize_t) (length*bytes_per_pixel+1);
cristy3ed852e2009-09-05 21:47:34 +0000540 }
541 else
542 {
543 length=(size_t) (((scanline[j] ^ 0xff) & 0xff)+2);
544 number_pixels=bytes_per_pixel;
545 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
cristybb503372010-05-27 20:51:26 +0000546 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000547 {
548 if ((q-pixels+number_pixels) <= *extent)
549 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
550 q+=number_pixels;
551 }
cristyeaedf062010-05-29 22:36:02 +0000552 j+=(ssize_t) bytes_per_pixel+1;
cristy3ed852e2009-09-05 21:47:34 +0000553 }
554 }
555 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
556 return(pixels);
557}
558
559/*
560%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
561% %
562% %
563% %
564% E n c o d e I m a g e %
565% %
566% %
567% %
568%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
569%
570% EncodeImage compresses an image via Macintosh pack bits encoding
571% for Macintosh PICT images.
572%
573% The format of the EncodeImage method is:
574%
575% size_t EncodeImage(Image *image,const unsigned char *scanline,
cristybb503372010-05-27 20:51:26 +0000576% const size_t bytes_per_line,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000577%
578% A description of each parameter follows:
579%
580% o image: the address of a structure of type Image.
581%
582% o scanline: A pointer to an array of characters to pack.
583%
584% o bytes_per_line: the number of bytes in a scanline.
585%
586% o pixels: A pointer to an array of characters where the packed
587% characters are stored.
588%
cristy3ed852e2009-09-05 21:47:34 +0000589*/
590static size_t EncodeImage(Image *image,const unsigned char *scanline,
cristybb503372010-05-27 20:51:26 +0000591 const size_t bytes_per_line,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000592{
593#define MaxCount 128
594#define MaxPackbitsRunlength 128
595
cristy3ed852e2009-09-05 21:47:34 +0000596 register const unsigned char
597 *p;
598
cristybb503372010-05-27 20:51:26 +0000599 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000600 i;
601
602 register unsigned char
603 *q;
604
605 size_t
606 length;
607
cristyaff6d802011-04-26 01:46:31 +0000608 ssize_t
609 count,
610 repeat_count,
611 runlength;
612
cristy3ed852e2009-09-05 21:47:34 +0000613 unsigned char
614 index;
615
616 /*
617 Pack scanline.
618 */
619 assert(image != (Image *) NULL);
620 assert(image->signature == MagickSignature);
621 if (image->debug != MagickFalse)
622 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
623 assert(scanline != (unsigned char *) NULL);
624 assert(pixels != (unsigned char *) NULL);
625 count=0;
626 runlength=0;
627 p=scanline+(bytes_per_line-1);
628 q=pixels;
629 index=(*p);
cristybb503372010-05-27 20:51:26 +0000630 for (i=(ssize_t) bytes_per_line-1; i >= 0; i--)
cristy3ed852e2009-09-05 21:47:34 +0000631 {
632 if (index == *p)
633 runlength++;
634 else
635 {
636 if (runlength < 3)
637 while (runlength > 0)
638 {
639 *q++=(unsigned char) index;
640 runlength--;
641 count++;
642 if (count == MaxCount)
643 {
644 *q++=(unsigned char) (MaxCount-1);
645 count-=MaxCount;
646 }
647 }
648 else
649 {
650 if (count > 0)
651 *q++=(unsigned char) (count-1);
652 count=0;
653 while (runlength > 0)
654 {
655 repeat_count=runlength;
656 if (repeat_count > MaxPackbitsRunlength)
657 repeat_count=MaxPackbitsRunlength;
658 *q++=(unsigned char) index;
659 *q++=(unsigned char) (257-repeat_count);
660 runlength-=repeat_count;
661 }
662 }
663 runlength=1;
664 }
665 index=(*p);
666 p--;
667 }
668 if (runlength < 3)
669 while (runlength > 0)
670 {
671 *q++=(unsigned char) index;
672 runlength--;
673 count++;
674 if (count == MaxCount)
675 {
676 *q++=(unsigned char) (MaxCount-1);
677 count-=MaxCount;
678 }
679 }
680 else
681 {
682 if (count > 0)
683 *q++=(unsigned char) (count-1);
684 count=0;
685 while (runlength > 0)
686 {
687 repeat_count=runlength;
688 if (repeat_count > MaxPackbitsRunlength)
689 repeat_count=MaxPackbitsRunlength;
690 *q++=(unsigned char) index;
691 *q++=(unsigned char) (257-repeat_count);
692 runlength-=repeat_count;
693 }
694 }
695 if (count > 0)
696 *q++=(unsigned char) (count-1);
697 /*
698 Write the number of and the packed length.
699 */
700 length=(size_t) (q-pixels);
701 if (bytes_per_line > 200)
702 {
703 (void) WriteBlobMSBShort(image,(unsigned short) length);
704 length+=2;
705 }
706 else
707 {
708 (void) WriteBlobByte(image,(unsigned char) length);
709 length++;
710 }
711 while (q != pixels)
712 {
713 q--;
714 (void) WriteBlobByte(image,*q);
715 }
716 return(length);
717}
718
719/*
720%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
721% %
722% %
723% %
724% I s P I C T %
725% %
726% %
727% %
728%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
729%
730% IsPICT()() returns MagickTrue if the image format type, identified by the
731% magick string, is PICT.
732%
733% The format of the ReadPICTImage method is:
734%
735% MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
736%
737% A description of each parameter follows:
738%
739% o magick: compare image format pattern against these bytes.
740%
741% o length: Specifies the length of the magick string.
742%
cristy3ed852e2009-09-05 21:47:34 +0000743*/
744static MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
745{
746 if (length < 528)
747 return(MagickFalse);
748 if (memcmp(magick+522,"\000\021\002\377\014\000",6) == 0)
749 return(MagickTrue);
750 return(MagickFalse);
751}
752
753#if !defined(macintosh)
754/*
755%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756% %
757% %
758% %
759% R e a d P I C T I m a g e %
760% %
761% %
762% %
763%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
764%
765% ReadPICTImage() reads an Apple Macintosh QuickDraw/PICT image file
766% and returns it. It allocates the memory necessary for the new Image
767% structure and returns a pointer to the new image.
768%
769% The format of the ReadPICTImage method is:
770%
771% Image *ReadPICTImage(const ImageInfo *image_info,
772% ExceptionInfo *exception)
773%
774% A description of each parameter follows:
775%
776% o image_info: the image info.
777%
778% o exception: return any errors or warnings in this structure.
779%
780*/
781
cristybb503372010-05-27 20:51:26 +0000782static inline size_t MagickMax(const size_t x,
783 const size_t y)
cristy3ed852e2009-09-05 21:47:34 +0000784{
785 if (x > y)
786 return(x);
787 return(y);
788}
789
790static Image *ReadPICTImage(const ImageInfo *image_info,
791 ExceptionInfo *exception)
792{
793 char
794 geometry[MaxTextExtent];
795
796 Image
797 *image;
798
cristy3ed852e2009-09-05 21:47:34 +0000799 int
800 c,
801 code;
802
cristy3ed852e2009-09-05 21:47:34 +0000803 MagickBooleanType
804 jpeg,
805 status;
806
807 PICTRectangle
808 frame;
809
810 PICTPixmap
811 pixmap;
812
cristy4c08aed2011-07-01 19:47:50 +0000813 Quantum
814 index;
cristy3ed852e2009-09-05 21:47:34 +0000815
cristy4c08aed2011-07-01 19:47:50 +0000816 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000817 *q;
818
cristybb503372010-05-27 20:51:26 +0000819 register ssize_t
cristy4c08aed2011-07-01 19:47:50 +0000820 i,
821 x;
cristy3ed852e2009-09-05 21:47:34 +0000822
823 size_t
824 extent,
825 length;
826
827 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000828 count,
829 flags,
830 j,
831 version,
832 y;
cristy3ed852e2009-09-05 21:47:34 +0000833
834 StringInfo
835 *profile;
836
837 /*
838 Open image file.
839 */
840 assert(image_info != (const ImageInfo *) NULL);
841 assert(image_info->signature == MagickSignature);
842 if (image_info->debug != MagickFalse)
843 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
844 image_info->filename);
845 assert(exception != (ExceptionInfo *) NULL);
846 assert(exception->signature == MagickSignature);
847 image=AcquireImage(image_info);
848 image->depth=8;
849 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
850 if (status == MagickFalse)
851 {
852 image=DestroyImageList(image);
853 return((Image *) NULL);
854 }
855 /*
856 Read PICT header.
857 */
858 pixmap.bits_per_pixel=0;
859 pixmap.component_count=0;
860 for (i=0; i < 512; i++)
861 (void) ReadBlobByte(image); /* skip header */
862 (void) ReadBlobMSBShort(image); /* skip picture size */
863 ReadRectangle(image,frame);
864 while ((c=ReadBlobByte(image)) == 0) ;
865 if (c != 0x11)
866 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
867 version=ReadBlobByte(image);
868 if (version == 2)
869 {
870 c=ReadBlobByte(image);
871 if (c != 0xff)
872 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
873 }
874 else
875 if (version != 1)
876 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
877 if ((frame.left < 0) || (frame.right < 0) || (frame.top < 0) ||
878 (frame.bottom < 0) || (frame.left >= frame.right) ||
879 (frame.top >= frame.bottom))
880 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
881 /*
882 Create black canvas.
883 */
884 flags=0;
885 image->columns=1UL*(frame.right-frame.left);
886 image->rows=1UL*(frame.bottom-frame.top);
887 image->x_resolution=DefaultResolution;
888 image->y_resolution=DefaultResolution;
889 image->units=UndefinedResolution;
890 /*
891 Interpret PICT opcodes.
892 */
893 jpeg=MagickFalse;
894 for (code=0; EOFBlob(image) == MagickFalse; )
895 {
896 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
897 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
898 break;
899 if ((version == 1) || ((TellBlob(image) % 2) != 0))
900 code=ReadBlobByte(image);
901 if (version == 2)
902 code=(int) ReadBlobMSBShort(image);
903 if (code > 0xa1)
904 {
905 if (image->debug != MagickFalse)
906 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%04X:",code);
907 }
908 else
909 {
910 if (image->debug != MagickFalse)
911 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
912 " %04X %s: %s",code,codes[code].name,codes[code].description);
913 switch (code)
914 {
915 case 0x01:
916 {
917 /*
918 Clipping rectangle.
919 */
920 length=ReadBlobMSBShort(image);
921 if (length != 0x000a)
922 {
cristybb503372010-05-27 20:51:26 +0000923 for (i=0; i < (ssize_t) (length-2); i++)
cristy3ed852e2009-09-05 21:47:34 +0000924 (void) ReadBlobByte(image);
925 break;
926 }
927 ReadRectangle(image,frame);
928 if (((frame.left & 0x8000) != 0) || ((frame.top & 0x8000) != 0))
929 break;
930 image->columns=1UL*(frame.right-frame.left);
931 image->rows=1UL*(frame.bottom-frame.top);
932 (void) SetImageBackgroundColor(image);
933 break;
934 }
935 case 0x12:
936 case 0x13:
937 case 0x14:
938 {
cristybb503372010-05-27 20:51:26 +0000939 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000940 pattern;
941
cristybb503372010-05-27 20:51:26 +0000942 size_t
cristy3ed852e2009-09-05 21:47:34 +0000943 height,
944 width;
945
946 /*
947 Skip pattern definition.
948 */
949 pattern=1L*ReadBlobMSBShort(image);
950 for (i=0; i < 8; i++)
951 (void) ReadBlobByte(image);
952 if (pattern == 2)
953 {
954 for (i=0; i < 5; i++)
955 (void) ReadBlobByte(image);
956 break;
957 }
958 if (pattern != 1)
959 ThrowReaderException(CorruptImageError,"UnknownPatternType");
960 length=ReadBlobMSBShort(image);
961 ReadRectangle(image,frame);
962 ReadPixmap(pixmap);
963 image->depth=1UL*pixmap.component_size;
964 image->x_resolution=1.0*pixmap.horizontal_resolution;
965 image->y_resolution=1.0*pixmap.vertical_resolution;
966 image->units=PixelsPerInchResolution;
967 (void) ReadBlobMSBLong(image);
968 flags=1L*ReadBlobMSBShort(image);
969 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +0000970 for (i=0; i <= (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000971 (void) ReadBlobMSBLong(image);
972 width=1UL*(frame.bottom-frame.top);
973 height=1UL*(frame.right-frame.left);
974 if (pixmap.bits_per_pixel <= 8)
975 length&=0x7fff;
976 if (pixmap.bits_per_pixel == 16)
977 width<<=1;
978 if (length == 0)
979 length=width;
980 if (length < 8)
981 {
cristybb503372010-05-27 20:51:26 +0000982 for (i=0; i < (ssize_t) (length*height); i++)
cristy3ed852e2009-09-05 21:47:34 +0000983 (void) ReadBlobByte(image);
984 }
985 else
986 for (j=0; j < (int) height; j++)
987 if (length > 200)
cristybb503372010-05-27 20:51:26 +0000988 for (j=0; j < (ssize_t) ReadBlobMSBShort(image); j++)
cristy3ed852e2009-09-05 21:47:34 +0000989 (void) ReadBlobByte(image);
990 else
cristybb503372010-05-27 20:51:26 +0000991 for (j=0; j < (ssize_t) ReadBlobByte(image); j++)
cristy3ed852e2009-09-05 21:47:34 +0000992 (void) ReadBlobByte(image);
993 break;
994 }
995 case 0x1b:
996 {
997 /*
998 Initialize image background color.
999 */
1000 image->background_color.red=(Quantum)
1001 ScaleShortToQuantum(ReadBlobMSBShort(image));
1002 image->background_color.green=(Quantum)
1003 ScaleShortToQuantum(ReadBlobMSBShort(image));
1004 image->background_color.blue=(Quantum)
1005 ScaleShortToQuantum(ReadBlobMSBShort(image));
1006 break;
1007 }
1008 case 0x70:
1009 case 0x71:
1010 case 0x72:
1011 case 0x73:
1012 case 0x74:
1013 case 0x75:
1014 case 0x76:
1015 case 0x77:
1016 {
1017 /*
1018 Skip polygon or region.
1019 */
1020 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001021 for (i=0; i < (ssize_t) (length-2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001022 (void) ReadBlobByte(image);
1023 break;
1024 }
1025 case 0x90:
1026 case 0x91:
1027 case 0x98:
1028 case 0x99:
1029 case 0x9a:
1030 case 0x9b:
1031 {
cristy4c08aed2011-07-01 19:47:50 +00001032 Image
1033 *tile_image;
cristy3ed852e2009-09-05 21:47:34 +00001034
1035 PICTRectangle
1036 source,
1037 destination;
1038
1039 register unsigned char
1040 *p;
1041
1042 size_t
1043 j;
1044
cristy4c08aed2011-07-01 19:47:50 +00001045 ssize_t
1046 bytes_per_line;
1047
cristy3ed852e2009-09-05 21:47:34 +00001048 unsigned char
1049 *pixels;
1050
cristy3ed852e2009-09-05 21:47:34 +00001051 /*
1052 Pixmap clipped by a rectangle.
1053 */
1054 bytes_per_line=0;
1055 if ((code != 0x9a) && (code != 0x9b))
1056 bytes_per_line=1L*ReadBlobMSBShort(image);
1057 else
1058 {
1059 (void) ReadBlobMSBShort(image);
1060 (void) ReadBlobMSBShort(image);
1061 (void) ReadBlobMSBShort(image);
1062 }
1063 ReadRectangle(image,frame);
1064 /*
1065 Initialize tile image.
1066 */
1067 tile_image=CloneImage(image,1UL*(frame.right-frame.left),
1068 1UL*(frame.bottom-frame.top),MagickTrue,exception);
1069 if (tile_image == (Image *) NULL)
1070 return((Image *) NULL);
1071 if ((code == 0x9a) || (code == 0x9b) ||
1072 ((bytes_per_line & 0x8000) != 0))
1073 {
1074 ReadPixmap(pixmap);
1075 tile_image->depth=1UL*pixmap.component_size;
1076 tile_image->matte=pixmap.component_count == 4 ?
1077 MagickTrue : MagickFalse;
1078 tile_image->x_resolution=(double) pixmap.horizontal_resolution;
1079 tile_image->y_resolution=(double) pixmap.vertical_resolution;
1080 tile_image->units=PixelsPerInchResolution;
1081 if (tile_image->matte != MagickFalse)
1082 image->matte=tile_image->matte;
1083 }
1084 if ((code != 0x9a) && (code != 0x9b))
1085 {
1086 /*
1087 Initialize colormap.
1088 */
1089 tile_image->colors=2;
1090 if ((bytes_per_line & 0x8000) != 0)
1091 {
1092 (void) ReadBlobMSBLong(image);
1093 flags=1L*ReadBlobMSBShort(image);
1094 tile_image->colors=1UL*ReadBlobMSBShort(image)+1;
1095 }
1096 status=AcquireImageColormap(tile_image,tile_image->colors);
1097 if (status == MagickFalse)
1098 {
1099 tile_image=DestroyImage(tile_image);
1100 ThrowReaderException(ResourceLimitError,
1101 "MemoryAllocationFailed");
1102 }
1103 if ((bytes_per_line & 0x8000) != 0)
1104 {
cristybb503372010-05-27 20:51:26 +00001105 for (i=0; i < (ssize_t) tile_image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001106 {
1107 j=ReadBlobMSBShort(image) % tile_image->colors;
1108 if ((flags & 0x8000) != 0)
1109 j=(size_t) i;
1110 tile_image->colormap[j].red=(Quantum)
1111 ScaleShortToQuantum(ReadBlobMSBShort(image));
1112 tile_image->colormap[j].green=(Quantum)
1113 ScaleShortToQuantum(ReadBlobMSBShort(image));
1114 tile_image->colormap[j].blue=(Quantum)
1115 ScaleShortToQuantum(ReadBlobMSBShort(image));
1116 }
1117 }
1118 else
1119 {
cristybb503372010-05-27 20:51:26 +00001120 for (i=0; i < (ssize_t) tile_image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001121 {
1122 tile_image->colormap[i].red=(Quantum) (QuantumRange-
1123 tile_image->colormap[i].red);
1124 tile_image->colormap[i].green=(Quantum) (QuantumRange-
1125 tile_image->colormap[i].green);
1126 tile_image->colormap[i].blue=(Quantum) (QuantumRange-
1127 tile_image->colormap[i].blue);
1128 }
1129 }
1130 }
1131 ReadRectangle(image,source);
1132 ReadRectangle(image,destination);
1133 (void) ReadBlobMSBShort(image);
1134 if ((code == 0x91) || (code == 0x99) || (code == 0x9b))
1135 {
1136 /*
1137 Skip region.
1138 */
1139 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001140 for (i=0; i < (ssize_t) (length-2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001141 (void) ReadBlobByte(image);
1142 }
1143 if ((code != 0x9a) && (code != 0x9b) &&
1144 (bytes_per_line & 0x8000) == 0)
1145 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1,&extent);
1146 else
1147 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1U*
1148 pixmap.bits_per_pixel,&extent);
1149 if (pixels == (unsigned char *) NULL)
1150 {
1151 tile_image=DestroyImage(tile_image);
1152 ThrowReaderException(ResourceLimitError,
1153 "MemoryAllocationFailed");
1154 }
1155 /*
1156 Convert PICT tile image to pixel packets.
1157 */
1158 p=pixels;
cristybb503372010-05-27 20:51:26 +00001159 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001160 {
1161 if (p > (pixels+extent+image->columns))
1162 ThrowReaderException(CorruptImageError,"NotEnoughPixelData");
cristy3669d042009-10-16 01:36:31 +00001163 q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,
1164 exception);
cristy4c08aed2011-07-01 19:47:50 +00001165 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001166 break;
cristybb503372010-05-27 20:51:26 +00001167 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001168 {
1169 if (tile_image->storage_class == PseudoClass)
1170 {
1171 index=ConstrainColormapIndex(tile_image,*p);
cristy4c08aed2011-07-01 19:47:50 +00001172 SetPixelIndex(tile_image,index,q);
1173 SetPixelRed(tile_image,
1174 tile_image->colormap[(ssize_t) index].red,q);
1175 SetPixelGreen(tile_image,
1176 tile_image->colormap[(ssize_t) index].green,q);
1177 SetPixelBlue(tile_image,
1178 tile_image->colormap[(ssize_t) index].blue,q);
cristy3ed852e2009-09-05 21:47:34 +00001179 }
1180 else
1181 {
1182 if (pixmap.bits_per_pixel == 16)
1183 {
1184 i=(*p++);
1185 j=(*p);
cristy4c08aed2011-07-01 19:47:50 +00001186 SetPixelRed(tile_image,ScaleCharToQuantum(
1187 (unsigned char) ((i & 0x7c) << 1)),q);
1188 SetPixelGreen(tile_image,ScaleCharToQuantum(
cristyaff6d802011-04-26 01:46:31 +00001189 (unsigned char) (((i & 0x03) << 6) |
cristy4c08aed2011-07-01 19:47:50 +00001190 ((j & 0xe0) >> 2))),q);
1191 SetPixelBlue(tile_image,ScaleCharToQuantum(
1192 (unsigned char) ((j & 0x1f) << 3)),q);
cristy3ed852e2009-09-05 21:47:34 +00001193 }
1194 else
1195 if (tile_image->matte == MagickFalse)
1196 {
1197 if (p > (pixels+extent+2*image->columns))
1198 ThrowReaderException(CorruptImageError,
1199 "NotEnoughPixelData");
cristy4c08aed2011-07-01 19:47:50 +00001200 SetPixelRed(tile_image,ScaleCharToQuantum(*p),q);
1201 SetPixelGreen(tile_image,ScaleCharToQuantum(
1202 *(p+tile_image->columns)),q);
1203 SetPixelBlue(tile_image,ScaleCharToQuantum(
1204 *(p+2*tile_image->columns)),q);
cristy3ed852e2009-09-05 21:47:34 +00001205 }
1206 else
1207 {
1208 if (p > (pixels+extent+3*image->columns))
1209 ThrowReaderException(CorruptImageError,
1210 "NotEnoughPixelData");
cristy4c08aed2011-07-01 19:47:50 +00001211 SetPixelAlpha(tile_image,ScaleCharToQuantum(*p),q);
1212 SetPixelRed(tile_image,ScaleCharToQuantum(
1213 *(p+tile_image->columns)),q);
1214 SetPixelGreen(tile_image,ScaleCharToQuantum(
1215 *(p+2*tile_image->columns)),q);
1216 SetPixelBlue(tile_image,ScaleCharToQuantum(
1217 *(p+3*tile_image->columns)),q);
cristy3ed852e2009-09-05 21:47:34 +00001218 }
1219 }
1220 p++;
cristy4c08aed2011-07-01 19:47:50 +00001221 q+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00001222 }
1223 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
1224 break;
1225 if ((tile_image->storage_class == DirectClass) &&
1226 (pixmap.bits_per_pixel != 16))
1227 {
1228 p+=(pixmap.component_count-1)*tile_image->columns;
1229 if (p < pixels)
1230 break;
1231 }
1232 status=SetImageProgress(image,LoadImageTag,y,tile_image->rows);
1233 if (status == MagickFalse)
1234 break;
1235 }
1236 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1237 if (jpeg == MagickFalse)
1238 if ((code == 0x9a) || (code == 0x9b) ||
1239 ((bytes_per_line & 0x8000) != 0))
1240 (void) CompositeImage(image,CopyCompositeOp,tile_image,
1241 destination.left,destination.top);
1242 tile_image=DestroyImage(tile_image);
1243 break;
1244 }
1245 case 0xa1:
1246 {
1247 unsigned char
1248 *info;
1249
cristybb503372010-05-27 20:51:26 +00001250 size_t
cristy3ed852e2009-09-05 21:47:34 +00001251 type;
1252
1253 /*
1254 Comment.
1255 */
1256 type=ReadBlobMSBShort(image);
1257 length=ReadBlobMSBShort(image);
1258 if (length == 0)
1259 break;
1260 (void) ReadBlobMSBLong(image);
1261 length-=4;
1262 if (length == 0)
1263 break;
1264 info=(unsigned char *) AcquireQuantumMemory(length,sizeof(*info));
1265 if (info == (unsigned char *) NULL)
1266 break;
1267 count=ReadBlob(image,length,info);
cristyda16f162011-02-19 23:52:17 +00001268 (void) count;
cristy3ed852e2009-09-05 21:47:34 +00001269 switch (type)
1270 {
1271 case 0xe0:
1272 {
1273 if (length == 0)
1274 break;
1275 profile=AcquireStringInfo(length);
1276 SetStringInfoDatum(profile,info);
1277 status=SetImageProfile(image,"icc",profile);
1278 profile=DestroyStringInfo(profile);
1279 if (status == MagickFalse)
1280 ThrowReaderException(ResourceLimitError,
1281 "MemoryAllocationFailed");
1282 break;
1283 }
1284 case 0x1f2:
1285 {
1286 if (length == 0)
1287 break;
1288 profile=AcquireStringInfo(length);
1289 SetStringInfoDatum(profile,info);
1290 status=SetImageProfile(image,"iptc",profile);
1291 if (status == MagickFalse)
1292 ThrowReaderException(ResourceLimitError,
1293 "MemoryAllocationFailed");
1294 profile=DestroyStringInfo(profile);
1295 break;
1296 }
1297 default:
1298 break;
1299 }
1300 info=(unsigned char *) RelinquishMagickMemory(info);
1301 break;
1302 }
1303 default:
1304 {
1305 /*
1306 Skip to next op code.
1307 */
1308 if (codes[code].length == -1)
1309 (void) ReadBlobMSBShort(image);
1310 else
cristybb503372010-05-27 20:51:26 +00001311 for (i=0; i < (ssize_t) codes[code].length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001312 (void) ReadBlobByte(image);
1313 }
1314 }
1315 }
1316 if (code == 0xc00)
1317 {
1318 /*
1319 Skip header.
1320 */
1321 for (i=0; i < 24; i++)
1322 (void) ReadBlobByte(image);
1323 continue;
1324 }
1325 if (((code >= 0xb0) && (code <= 0xcf)) ||
1326 ((code >= 0x8000) && (code <= 0x80ff)))
1327 continue;
1328 if (code == 0x8200)
1329 {
1330 FILE
1331 *file;
1332
1333 Image
1334 *tile_image;
1335
1336 ImageInfo
1337 *read_info;
1338
1339 int
1340 unique_file;
1341
1342 /*
1343 Embedded JPEG.
1344 */
1345 jpeg=MagickTrue;
1346 read_info=CloneImageInfo(image_info);
1347 SetImageInfoBlob(read_info,(void *) NULL,0);
1348 file=(FILE *) NULL;
1349 unique_file=AcquireUniqueFileResource(read_info->filename);
1350 if (unique_file != -1)
cristyd0305b92009-10-19 13:12:21 +00001351 file=fdopen(unique_file,"wb");
cristy3ed852e2009-09-05 21:47:34 +00001352 if ((unique_file == -1) || (file == (FILE *) NULL))
1353 {
1354 (void) CopyMagickString(image->filename,read_info->filename,
1355 MaxTextExtent);
1356 ThrowFileException(exception,FileOpenError,
1357 "UnableToCreateTemporaryFile",image->filename);
1358 image=DestroyImageList(image);
1359 return((Image *) NULL);
1360 }
1361 length=ReadBlobMSBLong(image);
1362 for (i=0; i < 6; i++)
1363 (void) ReadBlobMSBLong(image);
1364 ReadRectangle(image,frame);
1365 for (i=0; i < 122; i++)
1366 (void) ReadBlobByte(image);
cristybb503372010-05-27 20:51:26 +00001367 for (i=0; i < (ssize_t) (length-154); i++)
cristy3ed852e2009-09-05 21:47:34 +00001368 {
1369 c=ReadBlobByte(image);
1370 (void) fputc(c,file);
1371 }
1372 (void) fclose(file);
1373 tile_image=ReadImage(read_info,exception);
1374 (void) RelinquishUniqueFileResource(read_info->filename);
1375 read_info=DestroyImageInfo(read_info);
1376 if (tile_image == (Image *) NULL)
1377 continue;
cristyb51dff52011-05-19 16:55:47 +00001378 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g",
cristye8c25f92010-06-03 00:53:06 +00001379 (double) MagickMax(image->columns,tile_image->columns),
1380 (double) MagickMax(image->rows,tile_image->rows));
cristya0511732010-02-17 02:38:35 +00001381 (void) SetImageExtent(image,
1382 MagickMax(image->columns,tile_image->columns),
1383 MagickMax(image->rows,tile_image->rows));
cristy3ed852e2009-09-05 21:47:34 +00001384 if (image->colorspace != RGBColorspace)
1385 (void) TransformImageColorspace(image,tile_image->colorspace);
1386 (void) CompositeImage(image,CopyCompositeOp,tile_image,frame.left,
1387 frame.right);
1388 image->compression=tile_image->compression;
1389 tile_image=DestroyImage(tile_image);
1390 continue;
1391 }
1392 if ((code == 0xff) || (code == 0xffff))
1393 break;
1394 if (((code >= 0xd0) && (code <= 0xfe)) ||
1395 ((code >= 0x8100) && (code <= 0xffff)))
1396 {
1397 /*
1398 Skip reserved.
1399 */
1400 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001401 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001402 (void) ReadBlobByte(image);
1403 continue;
1404 }
1405 if ((code >= 0x100) && (code <= 0x7fff))
1406 {
1407 /*
1408 Skip reserved.
1409 */
1410 length=(size_t) ((code >> 7) & 0xff);
cristybb503372010-05-27 20:51:26 +00001411 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001412 (void) ReadBlobByte(image);
1413 continue;
1414 }
1415 }
1416 (void) CloseBlob(image);
1417 return(GetFirstImageInList(image));
1418}
1419#endif
1420
1421/*
1422%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1423% %
1424% %
1425% %
1426% R e g i s t e r P I C T I m a g e %
1427% %
1428% %
1429% %
1430%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1431%
1432% RegisterPICTImage() adds attributes for the PICT image format to
1433% the list of supported formats. The attributes include the image format
1434% tag, a method to read and/or write the format, whether the format
1435% supports the saving of more than one frame to the same file or blob,
1436% whether the format supports native in-memory I/O, and a brief
1437% description of the format.
1438%
1439% The format of the RegisterPICTImage method is:
1440%
cristybb503372010-05-27 20:51:26 +00001441% size_t RegisterPICTImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001442%
1443*/
cristybb503372010-05-27 20:51:26 +00001444ModuleExport size_t RegisterPICTImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001445{
1446 MagickInfo
1447 *entry;
1448
1449 entry=SetMagickInfo("PCT");
1450 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1451 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1452 entry->adjoin=MagickFalse;
cristyffaf9782011-04-13 19:50:51 +00001453 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001454 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1455 entry->magick=(IsImageFormatHandler *) IsPICT;
1456 entry->module=ConstantString("PICT");
1457 (void) RegisterMagickInfo(entry);
1458 entry=SetMagickInfo("PICT");
1459 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1460 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1461 entry->adjoin=MagickFalse;
cristyffaf9782011-04-13 19:50:51 +00001462 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001463 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1464 entry->magick=(IsImageFormatHandler *) IsPICT;
1465 entry->module=ConstantString("PICT");
1466 (void) RegisterMagickInfo(entry);
1467 return(MagickImageCoderSignature);
1468}
1469
1470/*
1471%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1472% %
1473% %
1474% %
1475% U n r e g i s t e r P I C T I m a g e %
1476% %
1477% %
1478% %
1479%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1480%
1481% UnregisterPICTImage() removes format registrations made by the
1482% PICT module from the list of supported formats.
1483%
1484% The format of the UnregisterPICTImage method is:
1485%
1486% UnregisterPICTImage(void)
1487%
1488*/
1489ModuleExport void UnregisterPICTImage(void)
1490{
1491 (void) UnregisterMagickInfo("PCT");
1492 (void) UnregisterMagickInfo("PICT");
1493}
1494
1495/*
1496%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1497% %
1498% %
1499% %
1500% W r i t e P I C T I m a g e %
1501% %
1502% %
1503% %
1504%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1505%
1506% WritePICTImage() writes an image to a file in the Apple Macintosh
1507% QuickDraw/PICT image format.
1508%
1509% The format of the WritePICTImage method is:
1510%
cristy3669d042009-10-16 01:36:31 +00001511% MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1512% Image *image)
cristy3ed852e2009-09-05 21:47:34 +00001513%
1514% A description of each parameter follows.
1515%
1516% o image_info: the image info.
1517%
1518% o image: The image.
1519%
cristy3ed852e2009-09-05 21:47:34 +00001520*/
1521static MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1522 Image *image)
1523{
1524#define MaxCount 128
1525#define PictCropRegionOp 0x01
1526#define PictEndOfPictureOp 0xff
1527#define PictJPEGOp 0x8200
1528#define PictInfoOp 0x0C00
1529#define PictInfoSize 512
1530#define PictPixmapOp 0x9A
1531#define PictPICTOp 0x98
1532#define PictVersion 0x11
1533
1534 const StringInfo
1535 *profile;
1536
1537 double
1538 x_resolution,
1539 y_resolution;
1540
cristy3ed852e2009-09-05 21:47:34 +00001541 MagickBooleanType
1542 status;
1543
1544 MagickOffsetType
1545 offset;
1546
1547 PICTPixmap
1548 pixmap;
1549
1550 PICTRectangle
1551 bounds,
1552 crop_rectangle,
1553 destination_rectangle,
1554 frame_rectangle,
1555 size_rectangle,
1556 source_rectangle;
1557
cristy4c08aed2011-07-01 19:47:50 +00001558 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001559 *p;
1560
cristybb503372010-05-27 20:51:26 +00001561 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001562 i,
1563 x;
1564
1565 size_t
cristyaff6d802011-04-26 01:46:31 +00001566 bytes_per_line,
1567 count,
1568 storage_class;
1569
1570 ssize_t
1571 y;
cristy3ed852e2009-09-05 21:47:34 +00001572
1573 unsigned char
1574 *buffer,
1575 *packed_scanline,
1576 *scanline;
1577
cristy3ed852e2009-09-05 21:47:34 +00001578 unsigned short
1579 base_address,
1580 row_bytes,
1581 transfer_mode;
1582
1583 /*
1584 Open output image file.
1585 */
1586 assert(image_info != (const ImageInfo *) NULL);
1587 assert(image_info->signature == MagickSignature);
1588 assert(image != (Image *) NULL);
1589 assert(image->signature == MagickSignature);
1590 if (image->debug != MagickFalse)
1591 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1592 if ((image->columns > 65535L) || (image->rows > 65535L))
1593 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1594 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1595 if (status == MagickFalse)
1596 return(status);
1597 if (image->colorspace != RGBColorspace)
1598 (void) TransformImageColorspace(image,RGBColorspace);
1599 /*
1600 Initialize image info.
1601 */
1602 size_rectangle.top=0;
1603 size_rectangle.left=0;
1604 size_rectangle.bottom=(short) image->rows;
1605 size_rectangle.right=(short) image->columns;
1606 frame_rectangle=size_rectangle;
1607 crop_rectangle=size_rectangle;
1608 source_rectangle=size_rectangle;
1609 destination_rectangle=size_rectangle;
1610 base_address=0xff;
1611 row_bytes=(unsigned short) (image->columns | 0x8000);
1612 bounds.top=0;
1613 bounds.left=0;
1614 bounds.bottom=(short) image->rows;
1615 bounds.right=(short) image->columns;
1616 pixmap.version=0;
1617 pixmap.pack_type=0;
1618 pixmap.pack_size=0;
1619 pixmap.pixel_type=0;
1620 pixmap.bits_per_pixel=8;
1621 pixmap.component_count=1;
1622 pixmap.component_size=8;
1623 pixmap.plane_bytes=0;
1624 pixmap.table=0;
1625 pixmap.reserved=0;
1626 transfer_mode=0;
1627 x_resolution=image->x_resolution != 0.0 ? image->x_resolution :
1628 DefaultResolution;
1629 y_resolution=image->y_resolution != 0.0 ? image->y_resolution :
1630 DefaultResolution;
1631 storage_class=image->storage_class;
1632 if (image_info->compression == JPEGCompression)
1633 storage_class=DirectClass;
1634 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1635 {
1636 pixmap.component_count=image->matte ? 4 : 3;
1637 pixmap.pixel_type=16;
1638 pixmap.bits_per_pixel=32;
1639 pixmap.pack_type=0x04;
1640 transfer_mode=0x40;
1641 row_bytes=(unsigned short) ((4*image->columns) | 0x8000);
1642 }
1643 /*
1644 Allocate memory.
1645 */
1646 bytes_per_line=image->columns;
1647 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1648 bytes_per_line*=image->matte ? 4 : 3;
1649 buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer));
1650 packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t)
1651 (row_bytes+MaxCount),sizeof(*packed_scanline));
1652 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
1653 if ((buffer == (unsigned char *) NULL) ||
1654 (packed_scanline == (unsigned char *) NULL) ||
1655 (scanline == (unsigned char *) NULL))
1656 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1657 (void) ResetMagickMemory(scanline,0,row_bytes);
1658 (void) ResetMagickMemory(packed_scanline,0,(size_t) (row_bytes+MaxCount));
1659 /*
1660 Write header, header size, size bounding box, version, and reserved.
1661 */
1662 (void) ResetMagickMemory(buffer,0,PictInfoSize);
1663 (void) WriteBlob(image,PictInfoSize,buffer);
1664 (void) WriteBlobMSBShort(image,0);
1665 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top);
1666 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left);
1667 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom);
1668 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right);
1669 (void) WriteBlobMSBShort(image,PictVersion);
1670 (void) WriteBlobMSBShort(image,0x02ff); /* version #2 */
1671 (void) WriteBlobMSBShort(image,PictInfoOp);
1672 (void) WriteBlobMSBLong(image,0xFFFE0000UL);
1673 /*
1674 Write full size of the file, resolution, frame bounding box, and reserved.
1675 */
1676 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1677 (void) WriteBlobMSBShort(image,0x0000);
1678 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1679 (void) WriteBlobMSBShort(image,0x0000);
1680 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top);
1681 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left);
1682 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom);
1683 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right);
1684 (void) WriteBlobMSBLong(image,0x00000000L);
1685 profile=GetImageProfile(image,"iptc");
1686 if (profile != (StringInfo *) NULL)
1687 {
1688 (void) WriteBlobMSBShort(image,0xa1);
1689 (void) WriteBlobMSBShort(image,0x1f2);
cristyed552522009-10-16 14:04:35 +00001690 (void) WriteBlobMSBShort(image,(unsigned short)
1691 (GetStringInfoLength(profile)+4));
cristy3ed852e2009-09-05 21:47:34 +00001692 (void) WriteBlobString(image,"8BIM");
cristyed552522009-10-16 14:04:35 +00001693 (void) WriteBlob(image,GetStringInfoLength(profile),
1694 GetStringInfoDatum(profile));
cristy3ed852e2009-09-05 21:47:34 +00001695 }
1696 profile=GetImageProfile(image,"icc");
1697 if (profile != (StringInfo *) NULL)
1698 {
1699 (void) WriteBlobMSBShort(image,0xa1);
1700 (void) WriteBlobMSBShort(image,0xe0);
cristyed552522009-10-16 14:04:35 +00001701 (void) WriteBlobMSBShort(image,(unsigned short)
1702 (GetStringInfoLength(profile)+4));
cristy3ed852e2009-09-05 21:47:34 +00001703 (void) WriteBlobMSBLong(image,0x00000000UL);
cristyed552522009-10-16 14:04:35 +00001704 (void) WriteBlob(image,GetStringInfoLength(profile),
1705 GetStringInfoDatum(profile));
cristy3ed852e2009-09-05 21:47:34 +00001706 (void) WriteBlobMSBShort(image,0xa1);
1707 (void) WriteBlobMSBShort(image,0xe0);
1708 (void) WriteBlobMSBShort(image,4);
1709 (void) WriteBlobMSBLong(image,0x00000002UL);
1710 }
1711 /*
1712 Write crop region opcode and crop bounding box.
1713 */
1714 (void) WriteBlobMSBShort(image,PictCropRegionOp);
1715 (void) WriteBlobMSBShort(image,0xa);
1716 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top);
1717 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left);
1718 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom);
1719 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right);
1720 if (image_info->compression == JPEGCompression)
1721 {
1722 Image
1723 *jpeg_image;
1724
1725 ImageInfo
1726 *jpeg_info;
1727
1728 size_t
1729 length;
1730
1731 unsigned char
1732 *blob;
1733
1734 jpeg_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1735 if (jpeg_image == (Image *) NULL)
1736 {
1737 (void) CloseBlob(image);
1738 return(MagickFalse);
1739 }
1740 jpeg_info=CloneImageInfo(image_info);
1741 (void) CopyMagickString(jpeg_info->magick,"JPEG",MaxTextExtent);
1742 length=0;
1743 blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length,
1744 &image->exception);
1745 jpeg_info=DestroyImageInfo(jpeg_info);
1746 if (blob == (unsigned char *) NULL)
1747 return(MagickFalse);
1748 jpeg_image=DestroyImage(jpeg_image);
1749 (void) WriteBlobMSBShort(image,PictJPEGOp);
cristyeaedf062010-05-29 22:36:02 +00001750 (void) WriteBlobMSBLong(image,(unsigned int) length+154);
cristy3ed852e2009-09-05 21:47:34 +00001751 (void) WriteBlobMSBShort(image,0x0000);
1752 (void) WriteBlobMSBLong(image,0x00010000UL);
1753 (void) WriteBlobMSBLong(image,0x00000000UL);
1754 (void) WriteBlobMSBLong(image,0x00000000UL);
1755 (void) WriteBlobMSBLong(image,0x00000000UL);
1756 (void) WriteBlobMSBLong(image,0x00010000UL);
1757 (void) WriteBlobMSBLong(image,0x00000000UL);
1758 (void) WriteBlobMSBLong(image,0x00000000UL);
1759 (void) WriteBlobMSBLong(image,0x00000000UL);
1760 (void) WriteBlobMSBLong(image,0x40000000UL);
1761 (void) WriteBlobMSBLong(image,0x00000000UL);
1762 (void) WriteBlobMSBLong(image,0x00000000UL);
1763 (void) WriteBlobMSBLong(image,0x00000000UL);
1764 (void) WriteBlobMSBLong(image,0x00400000UL);
1765 (void) WriteBlobMSBShort(image,0x0000);
1766 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1767 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1768 (void) WriteBlobMSBShort(image,0x0000);
1769 (void) WriteBlobMSBShort(image,768);
1770 (void) WriteBlobMSBShort(image,0x0000);
1771 (void) WriteBlobMSBLong(image,0x00000000UL);
1772 (void) WriteBlobMSBLong(image,0x00566A70UL);
1773 (void) WriteBlobMSBLong(image,0x65670000UL);
1774 (void) WriteBlobMSBLong(image,0x00000000UL);
1775 (void) WriteBlobMSBLong(image,0x00000001UL);
1776 (void) WriteBlobMSBLong(image,0x00016170UL);
1777 (void) WriteBlobMSBLong(image,0x706C0000UL);
1778 (void) WriteBlobMSBLong(image,0x00000000UL);
1779 (void) WriteBlobMSBShort(image,768);
1780 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1781 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1782 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1783 (void) WriteBlobMSBShort(image,0x0000);
1784 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1785 (void) WriteBlobMSBLong(image,0x00000000UL);
1786 (void) WriteBlobMSBLong(image,0x87AC0001UL);
1787 (void) WriteBlobMSBLong(image,0x0B466F74UL);
1788 (void) WriteBlobMSBLong(image,0x6F202D20UL);
1789 (void) WriteBlobMSBLong(image,0x4A504547UL);
1790 (void) WriteBlobMSBLong(image,0x00000000UL);
1791 (void) WriteBlobMSBLong(image,0x00000000UL);
1792 (void) WriteBlobMSBLong(image,0x00000000UL);
1793 (void) WriteBlobMSBLong(image,0x00000000UL);
1794 (void) WriteBlobMSBLong(image,0x00000000UL);
1795 (void) WriteBlobMSBLong(image,0x0018FFFFUL);
1796 (void) WriteBlob(image,length,blob);
1797 if ((length & 0x01) != 0)
1798 (void) WriteBlobByte(image,'\0');
1799 blob=(unsigned char *) RelinquishMagickMemory(blob);
1800 }
1801 /*
1802 Write picture opcode, row bytes, and picture bounding box, and version.
1803 */
1804 if (storage_class == PseudoClass)
1805 (void) WriteBlobMSBShort(image,PictPICTOp);
1806 else
1807 {
1808 (void) WriteBlobMSBShort(image,PictPixmapOp);
cristybb503372010-05-27 20:51:26 +00001809 (void) WriteBlobMSBLong(image,(size_t) base_address);
cristy3ed852e2009-09-05 21:47:34 +00001810 }
1811 (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000));
1812 (void) WriteBlobMSBShort(image,(unsigned short) bounds.top);
1813 (void) WriteBlobMSBShort(image,(unsigned short) bounds.left);
1814 (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom);
1815 (void) WriteBlobMSBShort(image,(unsigned short) bounds.right);
1816 /*
1817 Write pack type, pack size, resolution, pixel type, and pixel size.
1818 */
1819 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version);
1820 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type);
cristyeaedf062010-05-29 22:36:02 +00001821 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.pack_size);
cristy3ed852e2009-09-05 21:47:34 +00001822 (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5));
1823 (void) WriteBlobMSBShort(image,0x0000);
1824 (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5));
1825 (void) WriteBlobMSBShort(image,0x0000);
1826 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type);
1827 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel);
1828 /*
1829 Write component count, size, plane bytes, table size, and reserved.
1830 */
1831 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count);
1832 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size);
cristyeaedf062010-05-29 22:36:02 +00001833 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.plane_bytes);
1834 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.table);
1835 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.reserved);
cristy3ed852e2009-09-05 21:47:34 +00001836 if (storage_class == PseudoClass)
1837 {
1838 /*
1839 Write image colormap.
1840 */
1841 (void) WriteBlobMSBLong(image,0x00000000L); /* color seed */
1842 (void) WriteBlobMSBShort(image,0L); /* color flags */
1843 (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1));
cristybb503372010-05-27 20:51:26 +00001844 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001845 {
1846 (void) WriteBlobMSBShort(image,(unsigned short) i);
1847 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1848 image->colormap[i].red));
1849 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1850 image->colormap[i].green));
1851 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1852 image->colormap[i].blue));
1853 }
1854 }
1855 /*
1856 Write source and destination rectangle.
1857 */
1858 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top);
1859 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left);
1860 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom);
1861 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right);
1862 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top);
1863 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left);
1864 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom);
1865 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right);
1866 (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode);
1867 /*
1868 Write picture data.
1869 */
1870 count=0;
1871 if ((storage_class == PseudoClass) && (image->matte == MagickFalse))
cristybb503372010-05-27 20:51:26 +00001872 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001873 {
1874 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +00001875 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001876 break;
cristybb503372010-05-27 20:51:26 +00001877 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001878 {
1879 scanline[x]=(unsigned char) GetPixelIndex(image,p);
1880 p+=GetPixelChannels(image);
1881 }
cristybb503372010-05-27 20:51:26 +00001882 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
cristy3ed852e2009-09-05 21:47:34 +00001883 packed_scanline);
cristy8b27a6d2010-02-14 03:31:15 +00001884 if (image->previous == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001885 {
cristycee97112010-05-28 00:44:52 +00001886 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +00001887 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001888 if (status == MagickFalse)
1889 break;
1890 }
1891 }
1892 else
1893 if (image_info->compression == JPEGCompression)
1894 {
1895 (void) ResetMagickMemory(scanline,0,row_bytes);
cristybb503372010-05-27 20:51:26 +00001896 for (y=0; y < (ssize_t) image->rows; y++)
cristyaff6d802011-04-26 01:46:31 +00001897 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
1898 packed_scanline);
cristy3ed852e2009-09-05 21:47:34 +00001899 }
1900 else
1901 {
1902 register unsigned char
1903 *blue,
1904 *green,
1905 *opacity,
1906 *red;
1907
1908 red=scanline;
1909 green=scanline+image->columns;
1910 blue=scanline+2*image->columns;
1911 opacity=scanline+3*image->columns;
cristybb503372010-05-27 20:51:26 +00001912 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001913 {
1914 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
cristy4c08aed2011-07-01 19:47:50 +00001915 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001916 break;
1917 red=scanline;
1918 green=scanline+image->columns;
1919 blue=scanline+2*image->columns;
1920 if (image->matte != MagickFalse)
1921 {
1922 opacity=scanline;
1923 red=scanline+image->columns;
1924 green=scanline+2*image->columns;
1925 blue=scanline+3*image->columns;
1926 }
cristybb503372010-05-27 20:51:26 +00001927 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001928 {
cristy4c08aed2011-07-01 19:47:50 +00001929 *red++=ScaleQuantumToChar(GetPixelRed(image,p));
1930 *green++=ScaleQuantumToChar(GetPixelGreen(image,p));
1931 *blue++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001932 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001933 *opacity++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
1934 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001935 }
1936 count+=EncodeImage(image,scanline,bytes_per_line & 0x7FFF,
1937 packed_scanline);
cristy8b27a6d2010-02-14 03:31:15 +00001938 if (image->previous == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001939 {
cristycee97112010-05-28 00:44:52 +00001940 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1941 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001942 if (status == MagickFalse)
1943 break;
1944 }
1945 }
1946 }
1947 if ((count & 0x01) != 0)
1948 (void) WriteBlobByte(image,'\0');
1949 (void) WriteBlobMSBShort(image,PictEndOfPictureOp);
1950 offset=TellBlob(image);
1951 offset=SeekBlob(image,512,SEEK_SET);
1952 (void) WriteBlobMSBShort(image,(unsigned short) offset);
1953 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
1954 packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline);
1955 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1956 (void) CloseBlob(image);
1957 return(MagickTrue);
1958}