blob: 6475c352c2c5ff387399bbb2214d467a175912d9 [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*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/blob-private.h"
45#include "magick/cache.h"
46#include "magick/color-private.h"
cristye7e40552010-04-24 21:34:22 +000047#include "magick/colormap.h"
cristy316d5172009-09-17 19:31:25 +000048#include "magick/colormap-private.h"
cristy3ed852e2009-09-05 21:47:34 +000049#include "magick/colorspace.h"
50#include "magick/composite.h"
51#include "magick/constitute.h"
52#include "magick/exception.h"
53#include "magick/exception-private.h"
54#include "magick/image.h"
55#include "magick/image-private.h"
56#include "magick/list.h"
57#include "magick/log.h"
58#include "magick/magick.h"
59#include "magick/memory_.h"
60#include "magick/monitor.h"
61#include "magick/monitor-private.h"
62#include "magick/profile.h"
63#include "magick/resource_.h"
64#include "magick/quantum-private.h"
65#include "magick/static.h"
66#include "magick/string_.h"
67#include "magick/module.h"
68#include "magick/transform.h"
69#include "magick/utility.h"
70
71/*
72 ImageMagick Macintosh PICT Methods.
73*/
74#define ReadPixmap(pixmap) \
75{ \
76 pixmap.version=(short) ReadBlobMSBShort(image); \
77 pixmap.pack_type=(short) ReadBlobMSBShort(image); \
78 pixmap.pack_size=ReadBlobMSBLong(image); \
79 pixmap.horizontal_resolution=1UL*ReadBlobMSBShort(image); \
80 (void) ReadBlobMSBShort(image); \
81 pixmap.vertical_resolution=1UL*ReadBlobMSBShort(image); \
82 (void) ReadBlobMSBShort(image); \
83 pixmap.pixel_type=(short) ReadBlobMSBShort(image); \
84 pixmap.bits_per_pixel=(short) ReadBlobMSBShort(image); \
85 pixmap.component_count=(short) ReadBlobMSBShort(image); \
86 pixmap.component_size=(short) ReadBlobMSBShort(image); \
87 pixmap.plane_bytes=ReadBlobMSBLong(image); \
88 pixmap.table=ReadBlobMSBLong(image); \
89 pixmap.reserved=ReadBlobMSBLong(image); \
90 if ((pixmap.bits_per_pixel <= 0) || (pixmap.bits_per_pixel > 32) || \
91 (pixmap.component_count <= 0) || (pixmap.component_count > 4) || \
92 (pixmap.component_size <= 0)) \
93 ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
94}
95
96#define ReadRectangle(image,rectangle) \
97{ \
98 rectangle.top=(short) ReadBlobMSBShort(image); \
99 rectangle.left=(short) ReadBlobMSBShort(image); \
100 rectangle.bottom=(short) ReadBlobMSBShort(image); \
101 rectangle.right=(short) ReadBlobMSBShort(image); \
102 if ((rectangle.left > rectangle.right) || \
103 (rectangle.top > rectangle.bottom)) \
104 ThrowReaderException(CorruptImageError,"ImproperImageHeader"); \
105}
106
107typedef struct _PICTCode
108{
109 const char
110 *name;
111
cristybb503372010-05-27 20:51:26 +0000112 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000113 length;
114
115 const char
116 *description;
117} PICTCode;
118
119typedef struct _PICTPixmap
120{
121 short
122 version,
123 pack_type;
124
cristybb503372010-05-27 20:51:26 +0000125 size_t
cristy3ed852e2009-09-05 21:47:34 +0000126 pack_size,
127 horizontal_resolution,
128 vertical_resolution;
129
130 short
131 pixel_type,
132 bits_per_pixel,
133 component_count,
134 component_size;
135
cristybb503372010-05-27 20:51:26 +0000136 size_t
cristy3ed852e2009-09-05 21:47:34 +0000137 plane_bytes,
138 table,
139 reserved;
140} PICTPixmap;
141
142typedef struct _PICTRectangle
143{
144 short
145 top,
146 left,
147 bottom,
148 right;
149} PICTRectangle;
150
151static const PICTCode
152 codes[] =
153 {
154 /* 0x00 */ { "NOP", 0, "nop" },
155 /* 0x01 */ { "Clip", 0, "clip" },
156 /* 0x02 */ { "BkPat", 8, "background pattern" },
157 /* 0x03 */ { "TxFont", 2, "text font (word)" },
158 /* 0x04 */ { "TxFace", 1, "text face (byte)" },
159 /* 0x05 */ { "TxMode", 2, "text mode (word)" },
160 /* 0x06 */ { "SpExtra", 4, "space extra (fixed point)" },
161 /* 0x07 */ { "PnSize", 4, "pen size (point)" },
162 /* 0x08 */ { "PnMode", 2, "pen mode (word)" },
163 /* 0x09 */ { "PnPat", 8, "pen pattern" },
164 /* 0x0a */ { "FillPat", 8, "fill pattern" },
165 /* 0x0b */ { "OvSize", 4, "oval size (point)" },
166 /* 0x0c */ { "Origin", 4, "dh, dv (word)" },
167 /* 0x0d */ { "TxSize", 2, "text size (word)" },
cristybb503372010-05-27 20:51:26 +0000168 /* 0x0e */ { "FgColor", 4, "foreground color (ssize_tword)" },
169 /* 0x0f */ { "BkColor", 4, "background color (ssize_tword)" },
cristy3ed852e2009-09-05 21:47:34 +0000170 /* 0x10 */ { "TxRatio", 8, "numerator (point), denominator (point)" },
171 /* 0x11 */ { "Version", 1, "version (byte)" },
172 /* 0x12 */ { "BkPixPat", 0, "color background pattern" },
173 /* 0x13 */ { "PnPixPat", 0, "color pen pattern" },
174 /* 0x14 */ { "FillPixPat", 0, "color fill pattern" },
175 /* 0x15 */ { "PnLocHFrac", 2, "fractional pen position" },
176 /* 0x16 */ { "ChExtra", 2, "extra for each character" },
177 /* 0x17 */ { "reserved", 0, "reserved for Apple use" },
178 /* 0x18 */ { "reserved", 0, "reserved for Apple use" },
179 /* 0x19 */ { "reserved", 0, "reserved for Apple use" },
180 /* 0x1a */ { "RGBFgCol", 6, "RGB foreColor" },
181 /* 0x1b */ { "RGBBkCol", 6, "RGB backColor" },
182 /* 0x1c */ { "HiliteMode", 0, "hilite mode flag" },
183 /* 0x1d */ { "HiliteColor", 6, "RGB hilite color" },
184 /* 0x1e */ { "DefHilite", 0, "Use default hilite color" },
185 /* 0x1f */ { "OpColor", 6, "RGB OpColor for arithmetic modes" },
186 /* 0x20 */ { "Line", 8, "pnLoc (point), newPt (point)" },
187 /* 0x21 */ { "LineFrom", 4, "newPt (point)" },
188 /* 0x22 */ { "ShortLine", 6, "pnLoc (point, dh, dv (-128 .. 127))" },
189 /* 0x23 */ { "ShortLineFrom", 2, "dh, dv (-128 .. 127)" },
190 /* 0x24 */ { "reserved", -1, "reserved for Apple use" },
191 /* 0x25 */ { "reserved", -1, "reserved for Apple use" },
192 /* 0x26 */ { "reserved", -1, "reserved for Apple use" },
193 /* 0x27 */ { "reserved", -1, "reserved for Apple use" },
194 /* 0x28 */ { "LongText", 0, "txLoc (point), count (0..255), text" },
195 /* 0x29 */ { "DHText", 0, "dh (0..255), count (0..255), text" },
196 /* 0x2a */ { "DVText", 0, "dv (0..255), count (0..255), text" },
197 /* 0x2b */ { "DHDVText", 0, "dh, dv (0..255), count (0..255), text" },
198 /* 0x2c */ { "reserved", -1, "reserved for Apple use" },
199 /* 0x2d */ { "reserved", -1, "reserved for Apple use" },
200 /* 0x2e */ { "reserved", -1, "reserved for Apple use" },
201 /* 0x2f */ { "reserved", -1, "reserved for Apple use" },
202 /* 0x30 */ { "frameRect", 8, "rect" },
203 /* 0x31 */ { "paintRect", 8, "rect" },
204 /* 0x32 */ { "eraseRect", 8, "rect" },
205 /* 0x33 */ { "invertRect", 8, "rect" },
206 /* 0x34 */ { "fillRect", 8, "rect" },
207 /* 0x35 */ { "reserved", 8, "reserved for Apple use" },
208 /* 0x36 */ { "reserved", 8, "reserved for Apple use" },
209 /* 0x37 */ { "reserved", 8, "reserved for Apple use" },
210 /* 0x38 */ { "frameSameRect", 0, "rect" },
211 /* 0x39 */ { "paintSameRect", 0, "rect" },
212 /* 0x3a */ { "eraseSameRect", 0, "rect" },
213 /* 0x3b */ { "invertSameRect", 0, "rect" },
214 /* 0x3c */ { "fillSameRect", 0, "rect" },
215 /* 0x3d */ { "reserved", 0, "reserved for Apple use" },
216 /* 0x3e */ { "reserved", 0, "reserved for Apple use" },
217 /* 0x3f */ { "reserved", 0, "reserved for Apple use" },
218 /* 0x40 */ { "frameRRect", 8, "rect" },
219 /* 0x41 */ { "paintRRect", 8, "rect" },
220 /* 0x42 */ { "eraseRRect", 8, "rect" },
221 /* 0x43 */ { "invertRRect", 8, "rect" },
222 /* 0x44 */ { "fillRRrect", 8, "rect" },
223 /* 0x45 */ { "reserved", 8, "reserved for Apple use" },
224 /* 0x46 */ { "reserved", 8, "reserved for Apple use" },
225 /* 0x47 */ { "reserved", 8, "reserved for Apple use" },
226 /* 0x48 */ { "frameSameRRect", 0, "rect" },
227 /* 0x49 */ { "paintSameRRect", 0, "rect" },
228 /* 0x4a */ { "eraseSameRRect", 0, "rect" },
229 /* 0x4b */ { "invertSameRRect", 0, "rect" },
230 /* 0x4c */ { "fillSameRRect", 0, "rect" },
231 /* 0x4d */ { "reserved", 0, "reserved for Apple use" },
232 /* 0x4e */ { "reserved", 0, "reserved for Apple use" },
233 /* 0x4f */ { "reserved", 0, "reserved for Apple use" },
234 /* 0x50 */ { "frameOval", 8, "rect" },
235 /* 0x51 */ { "paintOval", 8, "rect" },
236 /* 0x52 */ { "eraseOval", 8, "rect" },
237 /* 0x53 */ { "invertOval", 8, "rect" },
238 /* 0x54 */ { "fillOval", 8, "rect" },
239 /* 0x55 */ { "reserved", 8, "reserved for Apple use" },
240 /* 0x56 */ { "reserved", 8, "reserved for Apple use" },
241 /* 0x57 */ { "reserved", 8, "reserved for Apple use" },
242 /* 0x58 */ { "frameSameOval", 0, "rect" },
243 /* 0x59 */ { "paintSameOval", 0, "rect" },
244 /* 0x5a */ { "eraseSameOval", 0, "rect" },
245 /* 0x5b */ { "invertSameOval", 0, "rect" },
246 /* 0x5c */ { "fillSameOval", 0, "rect" },
247 /* 0x5d */ { "reserved", 0, "reserved for Apple use" },
248 /* 0x5e */ { "reserved", 0, "reserved for Apple use" },
249 /* 0x5f */ { "reserved", 0, "reserved for Apple use" },
250 /* 0x60 */ { "frameArc", 12, "rect, startAngle, arcAngle" },
251 /* 0x61 */ { "paintArc", 12, "rect, startAngle, arcAngle" },
252 /* 0x62 */ { "eraseArc", 12, "rect, startAngle, arcAngle" },
253 /* 0x63 */ { "invertArc", 12, "rect, startAngle, arcAngle" },
254 /* 0x64 */ { "fillArc", 12, "rect, startAngle, arcAngle" },
255 /* 0x65 */ { "reserved", 12, "reserved for Apple use" },
256 /* 0x66 */ { "reserved", 12, "reserved for Apple use" },
257 /* 0x67 */ { "reserved", 12, "reserved for Apple use" },
258 /* 0x68 */ { "frameSameArc", 4, "rect, startAngle, arcAngle" },
259 /* 0x69 */ { "paintSameArc", 4, "rect, startAngle, arcAngle" },
260 /* 0x6a */ { "eraseSameArc", 4, "rect, startAngle, arcAngle" },
261 /* 0x6b */ { "invertSameArc", 4, "rect, startAngle, arcAngle" },
262 /* 0x6c */ { "fillSameArc", 4, "rect, startAngle, arcAngle" },
263 /* 0x6d */ { "reserved", 4, "reserved for Apple use" },
264 /* 0x6e */ { "reserved", 4, "reserved for Apple use" },
265 /* 0x6f */ { "reserved", 4, "reserved for Apple use" },
266 /* 0x70 */ { "framePoly", 0, "poly" },
267 /* 0x71 */ { "paintPoly", 0, "poly" },
268 /* 0x72 */ { "erasePoly", 0, "poly" },
269 /* 0x73 */ { "invertPoly", 0, "poly" },
270 /* 0x74 */ { "fillPoly", 0, "poly" },
271 /* 0x75 */ { "reserved", 0, "reserved for Apple use" },
272 /* 0x76 */ { "reserved", 0, "reserved for Apple use" },
273 /* 0x77 */ { "reserved", 0, "reserved for Apple use" },
274 /* 0x78 */ { "frameSamePoly", 0, "poly (NYI)" },
275 /* 0x79 */ { "paintSamePoly", 0, "poly (NYI)" },
276 /* 0x7a */ { "eraseSamePoly", 0, "poly (NYI)" },
277 /* 0x7b */ { "invertSamePoly", 0, "poly (NYI)" },
278 /* 0x7c */ { "fillSamePoly", 0, "poly (NYI)" },
279 /* 0x7d */ { "reserved", 0, "reserved for Apple use" },
280 /* 0x7e */ { "reserved", 0, "reserved for Apple use" },
281 /* 0x7f */ { "reserved", 0, "reserved for Apple use" },
282 /* 0x80 */ { "frameRgn", 0, "region" },
283 /* 0x81 */ { "paintRgn", 0, "region" },
284 /* 0x82 */ { "eraseRgn", 0, "region" },
285 /* 0x83 */ { "invertRgn", 0, "region" },
286 /* 0x84 */ { "fillRgn", 0, "region" },
287 /* 0x85 */ { "reserved", 0, "reserved for Apple use" },
288 /* 0x86 */ { "reserved", 0, "reserved for Apple use" },
289 /* 0x87 */ { "reserved", 0, "reserved for Apple use" },
290 /* 0x88 */ { "frameSameRgn", 0, "region (NYI)" },
291 /* 0x89 */ { "paintSameRgn", 0, "region (NYI)" },
292 /* 0x8a */ { "eraseSameRgn", 0, "region (NYI)" },
293 /* 0x8b */ { "invertSameRgn", 0, "region (NYI)" },
294 /* 0x8c */ { "fillSameRgn", 0, "region (NYI)" },
295 /* 0x8d */ { "reserved", 0, "reserved for Apple use" },
296 /* 0x8e */ { "reserved", 0, "reserved for Apple use" },
297 /* 0x8f */ { "reserved", 0, "reserved for Apple use" },
298 /* 0x90 */ { "BitsRect", 0, "copybits, rect clipped" },
299 /* 0x91 */ { "BitsRgn", 0, "copybits, rgn clipped" },
300 /* 0x92 */ { "reserved", -1, "reserved for Apple use" },
301 /* 0x93 */ { "reserved", -1, "reserved for Apple use" },
302 /* 0x94 */ { "reserved", -1, "reserved for Apple use" },
303 /* 0x95 */ { "reserved", -1, "reserved for Apple use" },
304 /* 0x96 */ { "reserved", -1, "reserved for Apple use" },
305 /* 0x97 */ { "reserved", -1, "reserved for Apple use" },
306 /* 0x98 */ { "PackBitsRect", 0, "packed copybits, rect clipped" },
307 /* 0x99 */ { "PackBitsRgn", 0, "packed copybits, rgn clipped" },
308 /* 0x9a */ { "DirectBitsRect", 0, "PixMap, srcRect, dstRect, mode, PixData" },
309 /* 0x9b */ { "DirectBitsRgn", 0, "PixMap, srcRect, dstRect, mode, maskRgn, PixData" },
310 /* 0x9c */ { "reserved", -1, "reserved for Apple use" },
311 /* 0x9d */ { "reserved", -1, "reserved for Apple use" },
312 /* 0x9e */ { "reserved", -1, "reserved for Apple use" },
313 /* 0x9f */ { "reserved", -1, "reserved for Apple use" },
314 /* 0xa0 */ { "ShortComment", 2, "kind (word)" },
315 /* 0xa1 */ { "LongComment", 0, "kind (word), size (word), data" }
316 };
317
318/*
319 Forward declarations.
320*/
321static MagickBooleanType
322 WritePICTImage(const ImageInfo *,Image *);
323
324/*
325%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
326% %
327% %
328% %
329% D e c o d e I m a g e %
330% %
331% %
332% %
333%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334%
335% DecodeImage decompresses an image via Macintosh pack bits decoding for
336% Macintosh PICT images.
337%
338% The format of the DecodeImage method is:
339%
340% unsigned char *DecodeImage(Image *blob,Image *image,
cristybb503372010-05-27 20:51:26 +0000341% size_t bytes_per_line,const int bits_per_pixel,
cristy3ed852e2009-09-05 21:47:34 +0000342% unsigned size_t extent)
343%
344% A description of each parameter follows:
345%
346% o image_info: the image info.
347%
348% o blob,image: the address of a structure of type Image.
349%
350% o bytes_per_line: This integer identifies the number of bytes in a
351% scanline.
352%
353% o bits_per_pixel: the number of bits in a pixel.
354%
355% o extent: the number of pixels allocated.
356%
357*/
358
359static unsigned char *ExpandBuffer(unsigned char *pixels,
360 MagickSizeType *bytes_per_line,const unsigned int bits_per_pixel)
361{
cristybb503372010-05-27 20:51:26 +0000362 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000363 i;
364
365 register unsigned char
366 *p,
367 *q;
368
369 static unsigned char
370 scanline[8*256];
371
372 p=pixels;
373 q=scanline;
374 switch (bits_per_pixel)
375 {
376 case 8:
377 case 16:
378 case 32:
379 return(pixels);
380 case 4:
381 {
cristybb503372010-05-27 20:51:26 +0000382 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000383 {
384 *q++=(*p >> 4) & 0xff;
385 *q++=(*p & 15);
386 p++;
387 }
388 *bytes_per_line*=2;
389 break;
390 }
391 case 2:
392 {
cristybb503372010-05-27 20:51:26 +0000393 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000394 {
395 *q++=(*p >> 6) & 0x03;
396 *q++=(*p >> 4) & 0x03;
397 *q++=(*p >> 2) & 0x03;
398 *q++=(*p & 3);
399 p++;
400 }
401 *bytes_per_line*=4;
402 break;
403 }
404 case 1:
405 {
cristybb503372010-05-27 20:51:26 +0000406 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000407 {
408 *q++=(*p >> 7) & 0x01;
409 *q++=(*p >> 6) & 0x01;
410 *q++=(*p >> 5) & 0x01;
411 *q++=(*p >> 4) & 0x01;
412 *q++=(*p >> 3) & 0x01;
413 *q++=(*p >> 2) & 0x01;
414 *q++=(*p >> 1) & 0x01;
415 *q++=(*p & 0x01);
416 p++;
417 }
418 *bytes_per_line*=8;
419 break;
420 }
421 default:
422 break;
423 }
424 return(scanline);
425}
426
427static unsigned char *DecodeImage(Image *blob,Image *image,
cristybb503372010-05-27 20:51:26 +0000428 size_t bytes_per_line,const unsigned int bits_per_pixel,size_t *extent)
cristy3ed852e2009-09-05 21:47:34 +0000429{
cristybb503372010-05-27 20:51:26 +0000430 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000431 j,
432 y;
433
434 MagickSizeType
435 number_pixels;
436
cristybb503372010-05-27 20:51:26 +0000437 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000438 i;
439
440 register unsigned char
441 *p,
442 *q;
443
444 size_t
445 length,
446 row_bytes;
447
448 ssize_t
449 count;
450
451 unsigned char
452 *pixels,
453 *scanline;
454
cristybb503372010-05-27 20:51:26 +0000455 size_t
cristy3ed852e2009-09-05 21:47:34 +0000456 bytes_per_pixel,
457 scanline_length,
458 width;
459
460 /*
461 Determine pixel buffer size.
462 */
463 if (bits_per_pixel <= 8)
464 bytes_per_line&=0x7fff;
465 width=image->columns;
466 bytes_per_pixel=1;
467 if (bits_per_pixel == 16)
468 {
469 bytes_per_pixel=2;
470 width*=2;
471 }
472 else
473 if (bits_per_pixel == 32)
474 width*=image->matte ? 4 : 3;
475 if (bytes_per_line == 0)
476 bytes_per_line=width;
477 row_bytes=(size_t) (image->columns | 0x8000);
478 if (image->storage_class == DirectClass)
479 row_bytes=(size_t) ((4*image->columns) | 0x8000);
480 /*
481 Allocate pixel and scanline buffer.
482 */
483 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,row_bytes*
484 sizeof(*pixels));
485 if (pixels == (unsigned char *) NULL)
486 return((unsigned char *) NULL);
487 *extent=row_bytes*image->rows*sizeof(*pixels);
488 (void) ResetMagickMemory(pixels,0,*extent);
489 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
490 if (scanline == (unsigned char *) NULL)
491 return((unsigned char *) NULL);
492 if (bytes_per_line < 8)
493 {
494 /*
495 Pixels are already uncompressed.
496 */
cristybb503372010-05-27 20:51:26 +0000497 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000498 {
499 q=pixels+y*width;
500 number_pixels=bytes_per_line;
501 count=ReadBlob(blob,(size_t) number_pixels,scanline);
cristyda16f162011-02-19 23:52:17 +0000502 (void) count;
cristy3ed852e2009-09-05 21:47:34 +0000503 p=ExpandBuffer(scanline,&number_pixels,bits_per_pixel);
504 if ((q+number_pixels) > (pixels+(*extent)))
505 {
506 (void) ThrowMagickException(&image->exception,GetMagickModule(),
507 CorruptImageError,"UnableToUncompressImage","`%s'",
508 image->filename);
509 break;
510 }
511 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
512 }
513 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
514 return(pixels);
515 }
516 /*
517 Uncompress RLE pixels into uncompressed pixel buffer.
518 */
cristybb503372010-05-27 20:51:26 +0000519 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000520 {
521 q=pixels+y*width;
522 if (bytes_per_line > 200)
523 scanline_length=ReadBlobMSBShort(blob);
524 else
525 scanline_length=1UL*ReadBlobByte(blob);
526 if (scanline_length >= row_bytes)
527 {
528 (void) ThrowMagickException(&image->exception,GetMagickModule(),
529 CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
530 break;
531 }
532 count=ReadBlob(blob,scanline_length,scanline);
cristybb503372010-05-27 20:51:26 +0000533 for (j=0; j < (ssize_t) scanline_length; )
cristy3ed852e2009-09-05 21:47:34 +0000534 if ((scanline[j] & 0x80) == 0)
535 {
536 length=(size_t) ((scanline[j] & 0xff)+1);
537 number_pixels=length*bytes_per_pixel;
538 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
539 if ((q-pixels+number_pixels) <= *extent)
540 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
541 q+=number_pixels;
cristybb503372010-05-27 20:51:26 +0000542 j+=(ssize_t) (length*bytes_per_pixel+1);
cristy3ed852e2009-09-05 21:47:34 +0000543 }
544 else
545 {
546 length=(size_t) (((scanline[j] ^ 0xff) & 0xff)+2);
547 number_pixels=bytes_per_pixel;
548 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
cristybb503372010-05-27 20:51:26 +0000549 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000550 {
551 if ((q-pixels+number_pixels) <= *extent)
552 (void) CopyMagickMemory(q,p,(size_t) number_pixels);
553 q+=number_pixels;
554 }
cristyeaedf062010-05-29 22:36:02 +0000555 j+=(ssize_t) bytes_per_pixel+1;
cristy3ed852e2009-09-05 21:47:34 +0000556 }
557 }
558 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
559 return(pixels);
560}
561
562/*
563%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
564% %
565% %
566% %
567% E n c o d e I m a g e %
568% %
569% %
570% %
571%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572%
573% EncodeImage compresses an image via Macintosh pack bits encoding
574% for Macintosh PICT images.
575%
576% The format of the EncodeImage method is:
577%
578% size_t EncodeImage(Image *image,const unsigned char *scanline,
cristybb503372010-05-27 20:51:26 +0000579% const size_t bytes_per_line,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000580%
581% A description of each parameter follows:
582%
583% o image: the address of a structure of type Image.
584%
585% o scanline: A pointer to an array of characters to pack.
586%
587% o bytes_per_line: the number of bytes in a scanline.
588%
589% o pixels: A pointer to an array of characters where the packed
590% characters are stored.
591%
cristy3ed852e2009-09-05 21:47:34 +0000592*/
593static size_t EncodeImage(Image *image,const unsigned char *scanline,
cristybb503372010-05-27 20:51:26 +0000594 const size_t bytes_per_line,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000595{
596#define MaxCount 128
597#define MaxPackbitsRunlength 128
598
cristybb503372010-05-27 20:51:26 +0000599 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000600 count,
601 repeat_count,
602 runlength;
603
604 register const unsigned char
605 *p;
606
cristybb503372010-05-27 20:51:26 +0000607 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000608 i;
609
610 register unsigned char
611 *q;
612
613 size_t
614 length;
615
616 unsigned char
617 index;
618
619 /*
620 Pack scanline.
621 */
622 assert(image != (Image *) NULL);
623 assert(image->signature == MagickSignature);
624 if (image->debug != MagickFalse)
625 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
626 assert(scanline != (unsigned char *) NULL);
627 assert(pixels != (unsigned char *) NULL);
628 count=0;
629 runlength=0;
630 p=scanline+(bytes_per_line-1);
631 q=pixels;
632 index=(*p);
cristybb503372010-05-27 20:51:26 +0000633 for (i=(ssize_t) bytes_per_line-1; i >= 0; i--)
cristy3ed852e2009-09-05 21:47:34 +0000634 {
635 if (index == *p)
636 runlength++;
637 else
638 {
639 if (runlength < 3)
640 while (runlength > 0)
641 {
642 *q++=(unsigned char) index;
643 runlength--;
644 count++;
645 if (count == MaxCount)
646 {
647 *q++=(unsigned char) (MaxCount-1);
648 count-=MaxCount;
649 }
650 }
651 else
652 {
653 if (count > 0)
654 *q++=(unsigned char) (count-1);
655 count=0;
656 while (runlength > 0)
657 {
658 repeat_count=runlength;
659 if (repeat_count > MaxPackbitsRunlength)
660 repeat_count=MaxPackbitsRunlength;
661 *q++=(unsigned char) index;
662 *q++=(unsigned char) (257-repeat_count);
663 runlength-=repeat_count;
664 }
665 }
666 runlength=1;
667 }
668 index=(*p);
669 p--;
670 }
671 if (runlength < 3)
672 while (runlength > 0)
673 {
674 *q++=(unsigned char) index;
675 runlength--;
676 count++;
677 if (count == MaxCount)
678 {
679 *q++=(unsigned char) (MaxCount-1);
680 count-=MaxCount;
681 }
682 }
683 else
684 {
685 if (count > 0)
686 *q++=(unsigned char) (count-1);
687 count=0;
688 while (runlength > 0)
689 {
690 repeat_count=runlength;
691 if (repeat_count > MaxPackbitsRunlength)
692 repeat_count=MaxPackbitsRunlength;
693 *q++=(unsigned char) index;
694 *q++=(unsigned char) (257-repeat_count);
695 runlength-=repeat_count;
696 }
697 }
698 if (count > 0)
699 *q++=(unsigned char) (count-1);
700 /*
701 Write the number of and the packed length.
702 */
703 length=(size_t) (q-pixels);
704 if (bytes_per_line > 200)
705 {
706 (void) WriteBlobMSBShort(image,(unsigned short) length);
707 length+=2;
708 }
709 else
710 {
711 (void) WriteBlobByte(image,(unsigned char) length);
712 length++;
713 }
714 while (q != pixels)
715 {
716 q--;
717 (void) WriteBlobByte(image,*q);
718 }
719 return(length);
720}
721
722/*
723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724% %
725% %
726% %
727% I s P I C T %
728% %
729% %
730% %
731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732%
733% IsPICT()() returns MagickTrue if the image format type, identified by the
734% magick string, is PICT.
735%
736% The format of the ReadPICTImage method is:
737%
738% MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
739%
740% A description of each parameter follows:
741%
742% o magick: compare image format pattern against these bytes.
743%
744% o length: Specifies the length of the magick string.
745%
cristy3ed852e2009-09-05 21:47:34 +0000746*/
747static MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
748{
749 if (length < 528)
750 return(MagickFalse);
751 if (memcmp(magick+522,"\000\021\002\377\014\000",6) == 0)
752 return(MagickTrue);
753 return(MagickFalse);
754}
755
756#if !defined(macintosh)
757/*
758%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759% %
760% %
761% %
762% R e a d P I C T I m a g e %
763% %
764% %
765% %
766%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
767%
768% ReadPICTImage() reads an Apple Macintosh QuickDraw/PICT image file
769% and returns it. It allocates the memory necessary for the new Image
770% structure and returns a pointer to the new image.
771%
772% The format of the ReadPICTImage method is:
773%
774% Image *ReadPICTImage(const ImageInfo *image_info,
775% ExceptionInfo *exception)
776%
777% A description of each parameter follows:
778%
779% o image_info: the image info.
780%
781% o exception: return any errors or warnings in this structure.
782%
783*/
784
cristybb503372010-05-27 20:51:26 +0000785static inline size_t MagickMax(const size_t x,
786 const size_t y)
cristy3ed852e2009-09-05 21:47:34 +0000787{
788 if (x > y)
789 return(x);
790 return(y);
791}
792
793static Image *ReadPICTImage(const ImageInfo *image_info,
794 ExceptionInfo *exception)
795{
796 char
797 geometry[MaxTextExtent];
798
799 Image
800 *image;
801
802 IndexPacket
803 index;
804
805 int
806 c,
807 code;
808
cristybb503372010-05-27 20:51:26 +0000809 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000810 flags,
811 j,
812 version,
813 y;
814
815 MagickBooleanType
816 jpeg,
817 status;
818
819 PICTRectangle
820 frame;
821
822 PICTPixmap
823 pixmap;
824
825 register IndexPacket
826 *indexes;
827
cristybb503372010-05-27 20:51:26 +0000828 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000829 x;
830
831 register PixelPacket
832 *q;
833
cristybb503372010-05-27 20:51:26 +0000834 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000835 i;
836
837 size_t
838 extent,
839 length;
840
841 ssize_t
842 count;
843
844 StringInfo
845 *profile;
846
847 /*
848 Open image file.
849 */
850 assert(image_info != (const ImageInfo *) NULL);
851 assert(image_info->signature == MagickSignature);
852 if (image_info->debug != MagickFalse)
853 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
854 image_info->filename);
855 assert(exception != (ExceptionInfo *) NULL);
856 assert(exception->signature == MagickSignature);
857 image=AcquireImage(image_info);
858 image->depth=8;
859 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
860 if (status == MagickFalse)
861 {
862 image=DestroyImageList(image);
863 return((Image *) NULL);
864 }
865 /*
866 Read PICT header.
867 */
868 pixmap.bits_per_pixel=0;
869 pixmap.component_count=0;
870 for (i=0; i < 512; i++)
871 (void) ReadBlobByte(image); /* skip header */
872 (void) ReadBlobMSBShort(image); /* skip picture size */
873 ReadRectangle(image,frame);
874 while ((c=ReadBlobByte(image)) == 0) ;
875 if (c != 0x11)
876 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
877 version=ReadBlobByte(image);
878 if (version == 2)
879 {
880 c=ReadBlobByte(image);
881 if (c != 0xff)
882 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
883 }
884 else
885 if (version != 1)
886 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
887 if ((frame.left < 0) || (frame.right < 0) || (frame.top < 0) ||
888 (frame.bottom < 0) || (frame.left >= frame.right) ||
889 (frame.top >= frame.bottom))
890 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
891 /*
892 Create black canvas.
893 */
894 flags=0;
895 image->columns=1UL*(frame.right-frame.left);
896 image->rows=1UL*(frame.bottom-frame.top);
897 image->x_resolution=DefaultResolution;
898 image->y_resolution=DefaultResolution;
899 image->units=UndefinedResolution;
900 /*
901 Interpret PICT opcodes.
902 */
903 jpeg=MagickFalse;
904 for (code=0; EOFBlob(image) == MagickFalse; )
905 {
906 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
907 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
908 break;
909 if ((version == 1) || ((TellBlob(image) % 2) != 0))
910 code=ReadBlobByte(image);
911 if (version == 2)
912 code=(int) ReadBlobMSBShort(image);
913 if (code > 0xa1)
914 {
915 if (image->debug != MagickFalse)
916 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%04X:",code);
917 }
918 else
919 {
920 if (image->debug != MagickFalse)
921 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
922 " %04X %s: %s",code,codes[code].name,codes[code].description);
923 switch (code)
924 {
925 case 0x01:
926 {
927 /*
928 Clipping rectangle.
929 */
930 length=ReadBlobMSBShort(image);
931 if (length != 0x000a)
932 {
cristybb503372010-05-27 20:51:26 +0000933 for (i=0; i < (ssize_t) (length-2); i++)
cristy3ed852e2009-09-05 21:47:34 +0000934 (void) ReadBlobByte(image);
935 break;
936 }
937 ReadRectangle(image,frame);
938 if (((frame.left & 0x8000) != 0) || ((frame.top & 0x8000) != 0))
939 break;
940 image->columns=1UL*(frame.right-frame.left);
941 image->rows=1UL*(frame.bottom-frame.top);
942 (void) SetImageBackgroundColor(image);
943 break;
944 }
945 case 0x12:
946 case 0x13:
947 case 0x14:
948 {
cristybb503372010-05-27 20:51:26 +0000949 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000950 pattern;
951
cristybb503372010-05-27 20:51:26 +0000952 size_t
cristy3ed852e2009-09-05 21:47:34 +0000953 height,
954 width;
955
956 /*
957 Skip pattern definition.
958 */
959 pattern=1L*ReadBlobMSBShort(image);
960 for (i=0; i < 8; i++)
961 (void) ReadBlobByte(image);
962 if (pattern == 2)
963 {
964 for (i=0; i < 5; i++)
965 (void) ReadBlobByte(image);
966 break;
967 }
968 if (pattern != 1)
969 ThrowReaderException(CorruptImageError,"UnknownPatternType");
970 length=ReadBlobMSBShort(image);
971 ReadRectangle(image,frame);
972 ReadPixmap(pixmap);
973 image->depth=1UL*pixmap.component_size;
974 image->x_resolution=1.0*pixmap.horizontal_resolution;
975 image->y_resolution=1.0*pixmap.vertical_resolution;
976 image->units=PixelsPerInchResolution;
977 (void) ReadBlobMSBLong(image);
978 flags=1L*ReadBlobMSBShort(image);
979 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +0000980 for (i=0; i <= (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000981 (void) ReadBlobMSBLong(image);
982 width=1UL*(frame.bottom-frame.top);
983 height=1UL*(frame.right-frame.left);
984 if (pixmap.bits_per_pixel <= 8)
985 length&=0x7fff;
986 if (pixmap.bits_per_pixel == 16)
987 width<<=1;
988 if (length == 0)
989 length=width;
990 if (length < 8)
991 {
cristybb503372010-05-27 20:51:26 +0000992 for (i=0; i < (ssize_t) (length*height); i++)
cristy3ed852e2009-09-05 21:47:34 +0000993 (void) ReadBlobByte(image);
994 }
995 else
996 for (j=0; j < (int) height; j++)
997 if (length > 200)
cristybb503372010-05-27 20:51:26 +0000998 for (j=0; j < (ssize_t) ReadBlobMSBShort(image); j++)
cristy3ed852e2009-09-05 21:47:34 +0000999 (void) ReadBlobByte(image);
1000 else
cristybb503372010-05-27 20:51:26 +00001001 for (j=0; j < (ssize_t) ReadBlobByte(image); j++)
cristy3ed852e2009-09-05 21:47:34 +00001002 (void) ReadBlobByte(image);
1003 break;
1004 }
1005 case 0x1b:
1006 {
1007 /*
1008 Initialize image background color.
1009 */
1010 image->background_color.red=(Quantum)
1011 ScaleShortToQuantum(ReadBlobMSBShort(image));
1012 image->background_color.green=(Quantum)
1013 ScaleShortToQuantum(ReadBlobMSBShort(image));
1014 image->background_color.blue=(Quantum)
1015 ScaleShortToQuantum(ReadBlobMSBShort(image));
1016 break;
1017 }
1018 case 0x70:
1019 case 0x71:
1020 case 0x72:
1021 case 0x73:
1022 case 0x74:
1023 case 0x75:
1024 case 0x76:
1025 case 0x77:
1026 {
1027 /*
1028 Skip polygon or region.
1029 */
1030 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001031 for (i=0; i < (ssize_t) (length-2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001032 (void) ReadBlobByte(image);
1033 break;
1034 }
1035 case 0x90:
1036 case 0x91:
1037 case 0x98:
1038 case 0x99:
1039 case 0x9a:
1040 case 0x9b:
1041 {
cristybb503372010-05-27 20:51:26 +00001042 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001043 bytes_per_line;
1044
1045 PICTRectangle
1046 source,
1047 destination;
1048
1049 register unsigned char
1050 *p;
1051
1052 size_t
1053 j;
1054
1055 unsigned char
1056 *pixels;
1057
1058 Image
1059 *tile_image;
1060
1061 /*
1062 Pixmap clipped by a rectangle.
1063 */
1064 bytes_per_line=0;
1065 if ((code != 0x9a) && (code != 0x9b))
1066 bytes_per_line=1L*ReadBlobMSBShort(image);
1067 else
1068 {
1069 (void) ReadBlobMSBShort(image);
1070 (void) ReadBlobMSBShort(image);
1071 (void) ReadBlobMSBShort(image);
1072 }
1073 ReadRectangle(image,frame);
1074 /*
1075 Initialize tile image.
1076 */
1077 tile_image=CloneImage(image,1UL*(frame.right-frame.left),
1078 1UL*(frame.bottom-frame.top),MagickTrue,exception);
1079 if (tile_image == (Image *) NULL)
1080 return((Image *) NULL);
1081 if ((code == 0x9a) || (code == 0x9b) ||
1082 ((bytes_per_line & 0x8000) != 0))
1083 {
1084 ReadPixmap(pixmap);
1085 tile_image->depth=1UL*pixmap.component_size;
1086 tile_image->matte=pixmap.component_count == 4 ?
1087 MagickTrue : MagickFalse;
1088 tile_image->x_resolution=(double) pixmap.horizontal_resolution;
1089 tile_image->y_resolution=(double) pixmap.vertical_resolution;
1090 tile_image->units=PixelsPerInchResolution;
1091 if (tile_image->matte != MagickFalse)
1092 image->matte=tile_image->matte;
1093 }
1094 if ((code != 0x9a) && (code != 0x9b))
1095 {
1096 /*
1097 Initialize colormap.
1098 */
1099 tile_image->colors=2;
1100 if ((bytes_per_line & 0x8000) != 0)
1101 {
1102 (void) ReadBlobMSBLong(image);
1103 flags=1L*ReadBlobMSBShort(image);
1104 tile_image->colors=1UL*ReadBlobMSBShort(image)+1;
1105 }
1106 status=AcquireImageColormap(tile_image,tile_image->colors);
1107 if (status == MagickFalse)
1108 {
1109 tile_image=DestroyImage(tile_image);
1110 ThrowReaderException(ResourceLimitError,
1111 "MemoryAllocationFailed");
1112 }
1113 if ((bytes_per_line & 0x8000) != 0)
1114 {
cristybb503372010-05-27 20:51:26 +00001115 for (i=0; i < (ssize_t) tile_image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001116 {
1117 j=ReadBlobMSBShort(image) % tile_image->colors;
1118 if ((flags & 0x8000) != 0)
1119 j=(size_t) i;
1120 tile_image->colormap[j].red=(Quantum)
1121 ScaleShortToQuantum(ReadBlobMSBShort(image));
1122 tile_image->colormap[j].green=(Quantum)
1123 ScaleShortToQuantum(ReadBlobMSBShort(image));
1124 tile_image->colormap[j].blue=(Quantum)
1125 ScaleShortToQuantum(ReadBlobMSBShort(image));
1126 }
1127 }
1128 else
1129 {
cristybb503372010-05-27 20:51:26 +00001130 for (i=0; i < (ssize_t) tile_image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001131 {
1132 tile_image->colormap[i].red=(Quantum) (QuantumRange-
1133 tile_image->colormap[i].red);
1134 tile_image->colormap[i].green=(Quantum) (QuantumRange-
1135 tile_image->colormap[i].green);
1136 tile_image->colormap[i].blue=(Quantum) (QuantumRange-
1137 tile_image->colormap[i].blue);
1138 }
1139 }
1140 }
1141 ReadRectangle(image,source);
1142 ReadRectangle(image,destination);
1143 (void) ReadBlobMSBShort(image);
1144 if ((code == 0x91) || (code == 0x99) || (code == 0x9b))
1145 {
1146 /*
1147 Skip region.
1148 */
1149 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001150 for (i=0; i < (ssize_t) (length-2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001151 (void) ReadBlobByte(image);
1152 }
1153 if ((code != 0x9a) && (code != 0x9b) &&
1154 (bytes_per_line & 0x8000) == 0)
1155 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1,&extent);
1156 else
1157 pixels=DecodeImage(image,tile_image,1UL*bytes_per_line,1U*
1158 pixmap.bits_per_pixel,&extent);
1159 if (pixels == (unsigned char *) NULL)
1160 {
1161 tile_image=DestroyImage(tile_image);
1162 ThrowReaderException(ResourceLimitError,
1163 "MemoryAllocationFailed");
1164 }
1165 /*
1166 Convert PICT tile image to pixel packets.
1167 */
1168 p=pixels;
cristybb503372010-05-27 20:51:26 +00001169 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001170 {
1171 if (p > (pixels+extent+image->columns))
1172 ThrowReaderException(CorruptImageError,"NotEnoughPixelData");
cristy3669d042009-10-16 01:36:31 +00001173 q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,
1174 exception);
cristy3ed852e2009-09-05 21:47:34 +00001175 if (q == (PixelPacket *) NULL)
1176 break;
1177 indexes=GetAuthenticIndexQueue(tile_image);
cristybb503372010-05-27 20:51:26 +00001178 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001179 {
1180 if (tile_image->storage_class == PseudoClass)
1181 {
1182 index=ConstrainColormapIndex(tile_image,*p);
1183 indexes[x]=index;
cristybb503372010-05-27 20:51:26 +00001184 q->red=tile_image->colormap[(ssize_t) index].red;
1185 q->green=tile_image->colormap[(ssize_t) index].green;
1186 q->blue=tile_image->colormap[(ssize_t) index].blue;
cristy3ed852e2009-09-05 21:47:34 +00001187 }
1188 else
1189 {
1190 if (pixmap.bits_per_pixel == 16)
1191 {
1192 i=(*p++);
1193 j=(*p);
1194 q->red=ScaleCharToQuantum((unsigned char)
1195 ((i & 0x7c) << 1));
1196 q->green=ScaleCharToQuantum((unsigned char)
1197 (((i & 0x03) << 6) | ((j & 0xe0) >> 2)));
1198 q->blue=ScaleCharToQuantum((unsigned char)
1199 ((j & 0x1f) << 3));
1200 }
1201 else
1202 if (tile_image->matte == MagickFalse)
1203 {
1204 if (p > (pixels+extent+2*image->columns))
1205 ThrowReaderException(CorruptImageError,
1206 "NotEnoughPixelData");
1207 q->red=ScaleCharToQuantum(*p);
1208 q->green=ScaleCharToQuantum(
1209 *(p+tile_image->columns));
1210 q->blue=ScaleCharToQuantum(
1211 *(p+2*tile_image->columns));
1212 }
1213 else
1214 {
1215 if (p > (pixels+extent+3*image->columns))
1216 ThrowReaderException(CorruptImageError,
1217 "NotEnoughPixelData");
1218 q->opacity=(Quantum) (QuantumRange-
1219 ScaleCharToQuantum(*p));
1220 q->red=ScaleCharToQuantum(*(p+tile_image->columns));
1221 q->green=(Quantum) ScaleCharToQuantum(
1222 *(p+2*tile_image->columns));
1223 q->blue=ScaleCharToQuantum(
1224 *(p+3*tile_image->columns));
1225 }
1226 }
1227 p++;
1228 q++;
1229 }
1230 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
1231 break;
1232 if ((tile_image->storage_class == DirectClass) &&
1233 (pixmap.bits_per_pixel != 16))
1234 {
1235 p+=(pixmap.component_count-1)*tile_image->columns;
1236 if (p < pixels)
1237 break;
1238 }
1239 status=SetImageProgress(image,LoadImageTag,y,tile_image->rows);
1240 if (status == MagickFalse)
1241 break;
1242 }
1243 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1244 if (jpeg == MagickFalse)
1245 if ((code == 0x9a) || (code == 0x9b) ||
1246 ((bytes_per_line & 0x8000) != 0))
1247 (void) CompositeImage(image,CopyCompositeOp,tile_image,
1248 destination.left,destination.top);
1249 tile_image=DestroyImage(tile_image);
1250 break;
1251 }
1252 case 0xa1:
1253 {
1254 unsigned char
1255 *info;
1256
cristybb503372010-05-27 20:51:26 +00001257 size_t
cristy3ed852e2009-09-05 21:47:34 +00001258 type;
1259
1260 /*
1261 Comment.
1262 */
1263 type=ReadBlobMSBShort(image);
1264 length=ReadBlobMSBShort(image);
1265 if (length == 0)
1266 break;
1267 (void) ReadBlobMSBLong(image);
1268 length-=4;
1269 if (length == 0)
1270 break;
1271 info=(unsigned char *) AcquireQuantumMemory(length,sizeof(*info));
1272 if (info == (unsigned char *) NULL)
1273 break;
1274 count=ReadBlob(image,length,info);
cristyda16f162011-02-19 23:52:17 +00001275 (void) count;
cristy3ed852e2009-09-05 21:47:34 +00001276 switch (type)
1277 {
1278 case 0xe0:
1279 {
1280 if (length == 0)
1281 break;
1282 profile=AcquireStringInfo(length);
1283 SetStringInfoDatum(profile,info);
1284 status=SetImageProfile(image,"icc",profile);
1285 profile=DestroyStringInfo(profile);
1286 if (status == MagickFalse)
1287 ThrowReaderException(ResourceLimitError,
1288 "MemoryAllocationFailed");
1289 break;
1290 }
1291 case 0x1f2:
1292 {
1293 if (length == 0)
1294 break;
1295 profile=AcquireStringInfo(length);
1296 SetStringInfoDatum(profile,info);
1297 status=SetImageProfile(image,"iptc",profile);
1298 if (status == MagickFalse)
1299 ThrowReaderException(ResourceLimitError,
1300 "MemoryAllocationFailed");
1301 profile=DestroyStringInfo(profile);
1302 break;
1303 }
1304 default:
1305 break;
1306 }
1307 info=(unsigned char *) RelinquishMagickMemory(info);
1308 break;
1309 }
1310 default:
1311 {
1312 /*
1313 Skip to next op code.
1314 */
1315 if (codes[code].length == -1)
1316 (void) ReadBlobMSBShort(image);
1317 else
cristybb503372010-05-27 20:51:26 +00001318 for (i=0; i < (ssize_t) codes[code].length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001319 (void) ReadBlobByte(image);
1320 }
1321 }
1322 }
1323 if (code == 0xc00)
1324 {
1325 /*
1326 Skip header.
1327 */
1328 for (i=0; i < 24; i++)
1329 (void) ReadBlobByte(image);
1330 continue;
1331 }
1332 if (((code >= 0xb0) && (code <= 0xcf)) ||
1333 ((code >= 0x8000) && (code <= 0x80ff)))
1334 continue;
1335 if (code == 0x8200)
1336 {
1337 FILE
1338 *file;
1339
1340 Image
1341 *tile_image;
1342
1343 ImageInfo
1344 *read_info;
1345
1346 int
1347 unique_file;
1348
1349 /*
1350 Embedded JPEG.
1351 */
1352 jpeg=MagickTrue;
1353 read_info=CloneImageInfo(image_info);
1354 SetImageInfoBlob(read_info,(void *) NULL,0);
1355 file=(FILE *) NULL;
1356 unique_file=AcquireUniqueFileResource(read_info->filename);
1357 if (unique_file != -1)
cristyd0305b92009-10-19 13:12:21 +00001358 file=fdopen(unique_file,"wb");
cristy3ed852e2009-09-05 21:47:34 +00001359 if ((unique_file == -1) || (file == (FILE *) NULL))
1360 {
1361 (void) CopyMagickString(image->filename,read_info->filename,
1362 MaxTextExtent);
1363 ThrowFileException(exception,FileOpenError,
1364 "UnableToCreateTemporaryFile",image->filename);
1365 image=DestroyImageList(image);
1366 return((Image *) NULL);
1367 }
1368 length=ReadBlobMSBLong(image);
1369 for (i=0; i < 6; i++)
1370 (void) ReadBlobMSBLong(image);
1371 ReadRectangle(image,frame);
1372 for (i=0; i < 122; i++)
1373 (void) ReadBlobByte(image);
cristybb503372010-05-27 20:51:26 +00001374 for (i=0; i < (ssize_t) (length-154); i++)
cristy3ed852e2009-09-05 21:47:34 +00001375 {
1376 c=ReadBlobByte(image);
1377 (void) fputc(c,file);
1378 }
1379 (void) fclose(file);
1380 tile_image=ReadImage(read_info,exception);
1381 (void) RelinquishUniqueFileResource(read_info->filename);
1382 read_info=DestroyImageInfo(read_info);
1383 if (tile_image == (Image *) NULL)
1384 continue;
cristye8c25f92010-06-03 00:53:06 +00001385 (void) FormatMagickString(geometry,MaxTextExtent,"%.20gx%.20g",
1386 (double) MagickMax(image->columns,tile_image->columns),
1387 (double) MagickMax(image->rows,tile_image->rows));
cristya0511732010-02-17 02:38:35 +00001388 (void) SetImageExtent(image,
1389 MagickMax(image->columns,tile_image->columns),
1390 MagickMax(image->rows,tile_image->rows));
cristy3ed852e2009-09-05 21:47:34 +00001391 if (image->colorspace != RGBColorspace)
1392 (void) TransformImageColorspace(image,tile_image->colorspace);
1393 (void) CompositeImage(image,CopyCompositeOp,tile_image,frame.left,
1394 frame.right);
1395 image->compression=tile_image->compression;
1396 tile_image=DestroyImage(tile_image);
1397 continue;
1398 }
1399 if ((code == 0xff) || (code == 0xffff))
1400 break;
1401 if (((code >= 0xd0) && (code <= 0xfe)) ||
1402 ((code >= 0x8100) && (code <= 0xffff)))
1403 {
1404 /*
1405 Skip reserved.
1406 */
1407 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001408 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001409 (void) ReadBlobByte(image);
1410 continue;
1411 }
1412 if ((code >= 0x100) && (code <= 0x7fff))
1413 {
1414 /*
1415 Skip reserved.
1416 */
1417 length=(size_t) ((code >> 7) & 0xff);
cristybb503372010-05-27 20:51:26 +00001418 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001419 (void) ReadBlobByte(image);
1420 continue;
1421 }
1422 }
1423 (void) CloseBlob(image);
1424 return(GetFirstImageInList(image));
1425}
1426#endif
1427
1428/*
1429%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1430% %
1431% %
1432% %
1433% R e g i s t e r P I C T I m a g e %
1434% %
1435% %
1436% %
1437%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1438%
1439% RegisterPICTImage() adds attributes for the PICT image format to
1440% the list of supported formats. The attributes include the image format
1441% tag, a method to read and/or write the format, whether the format
1442% supports the saving of more than one frame to the same file or blob,
1443% whether the format supports native in-memory I/O, and a brief
1444% description of the format.
1445%
1446% The format of the RegisterPICTImage method is:
1447%
cristybb503372010-05-27 20:51:26 +00001448% size_t RegisterPICTImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001449%
1450*/
cristybb503372010-05-27 20:51:26 +00001451ModuleExport size_t RegisterPICTImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001452{
1453 MagickInfo
1454 *entry;
1455
1456 entry=SetMagickInfo("PCT");
1457 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1458 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1459 entry->adjoin=MagickFalse;
1460 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1461 entry->magick=(IsImageFormatHandler *) IsPICT;
1462 entry->module=ConstantString("PICT");
1463 (void) RegisterMagickInfo(entry);
1464 entry=SetMagickInfo("PICT");
1465 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1466 entry->encoder=(EncodeImageHandler *) WritePICTImage;
1467 entry->adjoin=MagickFalse;
1468 entry->description=ConstantString("Apple Macintosh QuickDraw/PICT");
1469 entry->magick=(IsImageFormatHandler *) IsPICT;
1470 entry->module=ConstantString("PICT");
1471 (void) RegisterMagickInfo(entry);
1472 return(MagickImageCoderSignature);
1473}
1474
1475/*
1476%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1477% %
1478% %
1479% %
1480% U n r e g i s t e r P I C T I m a g e %
1481% %
1482% %
1483% %
1484%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1485%
1486% UnregisterPICTImage() removes format registrations made by the
1487% PICT module from the list of supported formats.
1488%
1489% The format of the UnregisterPICTImage method is:
1490%
1491% UnregisterPICTImage(void)
1492%
1493*/
1494ModuleExport void UnregisterPICTImage(void)
1495{
1496 (void) UnregisterMagickInfo("PCT");
1497 (void) UnregisterMagickInfo("PICT");
1498}
1499
1500/*
1501%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1502% %
1503% %
1504% %
1505% W r i t e P I C T I m a g e %
1506% %
1507% %
1508% %
1509%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1510%
1511% WritePICTImage() writes an image to a file in the Apple Macintosh
1512% QuickDraw/PICT image format.
1513%
1514% The format of the WritePICTImage method is:
1515%
cristy3669d042009-10-16 01:36:31 +00001516% MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1517% Image *image)
cristy3ed852e2009-09-05 21:47:34 +00001518%
1519% A description of each parameter follows.
1520%
1521% o image_info: the image info.
1522%
1523% o image: The image.
1524%
cristy3ed852e2009-09-05 21:47:34 +00001525*/
1526static MagickBooleanType WritePICTImage(const ImageInfo *image_info,
1527 Image *image)
1528{
1529#define MaxCount 128
1530#define PictCropRegionOp 0x01
1531#define PictEndOfPictureOp 0xff
1532#define PictJPEGOp 0x8200
1533#define PictInfoOp 0x0C00
1534#define PictInfoSize 512
1535#define PictPixmapOp 0x9A
1536#define PictPICTOp 0x98
1537#define PictVersion 0x11
1538
1539 const StringInfo
1540 *profile;
1541
1542 double
1543 x_resolution,
1544 y_resolution;
1545
cristybb503372010-05-27 20:51:26 +00001546 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001547 y;
1548
1549 MagickBooleanType
1550 status;
1551
1552 MagickOffsetType
1553 offset;
1554
1555 PICTPixmap
1556 pixmap;
1557
1558 PICTRectangle
1559 bounds,
1560 crop_rectangle,
1561 destination_rectangle,
1562 frame_rectangle,
1563 size_rectangle,
1564 source_rectangle;
1565
1566 register const IndexPacket
1567 *indexes;
1568
1569 register const PixelPacket
1570 *p;
1571
cristybb503372010-05-27 20:51:26 +00001572 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001573 i,
1574 x;
1575
1576 size_t
1577 count;
1578
1579 unsigned char
1580 *buffer,
1581 *packed_scanline,
1582 *scanline;
1583
cristybb503372010-05-27 20:51:26 +00001584 size_t
cristy3ed852e2009-09-05 21:47:34 +00001585 bytes_per_line,
1586 storage_class;
1587
1588 unsigned short
1589 base_address,
1590 row_bytes,
1591 transfer_mode;
1592
1593 /*
1594 Open output image file.
1595 */
1596 assert(image_info != (const ImageInfo *) NULL);
1597 assert(image_info->signature == MagickSignature);
1598 assert(image != (Image *) NULL);
1599 assert(image->signature == MagickSignature);
1600 if (image->debug != MagickFalse)
1601 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1602 if ((image->columns > 65535L) || (image->rows > 65535L))
1603 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1604 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1605 if (status == MagickFalse)
1606 return(status);
1607 if (image->colorspace != RGBColorspace)
1608 (void) TransformImageColorspace(image,RGBColorspace);
1609 /*
1610 Initialize image info.
1611 */
1612 size_rectangle.top=0;
1613 size_rectangle.left=0;
1614 size_rectangle.bottom=(short) image->rows;
1615 size_rectangle.right=(short) image->columns;
1616 frame_rectangle=size_rectangle;
1617 crop_rectangle=size_rectangle;
1618 source_rectangle=size_rectangle;
1619 destination_rectangle=size_rectangle;
1620 base_address=0xff;
1621 row_bytes=(unsigned short) (image->columns | 0x8000);
1622 bounds.top=0;
1623 bounds.left=0;
1624 bounds.bottom=(short) image->rows;
1625 bounds.right=(short) image->columns;
1626 pixmap.version=0;
1627 pixmap.pack_type=0;
1628 pixmap.pack_size=0;
1629 pixmap.pixel_type=0;
1630 pixmap.bits_per_pixel=8;
1631 pixmap.component_count=1;
1632 pixmap.component_size=8;
1633 pixmap.plane_bytes=0;
1634 pixmap.table=0;
1635 pixmap.reserved=0;
1636 transfer_mode=0;
1637 x_resolution=image->x_resolution != 0.0 ? image->x_resolution :
1638 DefaultResolution;
1639 y_resolution=image->y_resolution != 0.0 ? image->y_resolution :
1640 DefaultResolution;
1641 storage_class=image->storage_class;
1642 if (image_info->compression == JPEGCompression)
1643 storage_class=DirectClass;
1644 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1645 {
1646 pixmap.component_count=image->matte ? 4 : 3;
1647 pixmap.pixel_type=16;
1648 pixmap.bits_per_pixel=32;
1649 pixmap.pack_type=0x04;
1650 transfer_mode=0x40;
1651 row_bytes=(unsigned short) ((4*image->columns) | 0x8000);
1652 }
1653 /*
1654 Allocate memory.
1655 */
1656 bytes_per_line=image->columns;
1657 if ((storage_class == DirectClass) || (image->matte != MagickFalse))
1658 bytes_per_line*=image->matte ? 4 : 3;
1659 buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer));
1660 packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t)
1661 (row_bytes+MaxCount),sizeof(*packed_scanline));
1662 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
1663 if ((buffer == (unsigned char *) NULL) ||
1664 (packed_scanline == (unsigned char *) NULL) ||
1665 (scanline == (unsigned char *) NULL))
1666 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1667 (void) ResetMagickMemory(scanline,0,row_bytes);
1668 (void) ResetMagickMemory(packed_scanline,0,(size_t) (row_bytes+MaxCount));
1669 /*
1670 Write header, header size, size bounding box, version, and reserved.
1671 */
1672 (void) ResetMagickMemory(buffer,0,PictInfoSize);
1673 (void) WriteBlob(image,PictInfoSize,buffer);
1674 (void) WriteBlobMSBShort(image,0);
1675 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top);
1676 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left);
1677 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom);
1678 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right);
1679 (void) WriteBlobMSBShort(image,PictVersion);
1680 (void) WriteBlobMSBShort(image,0x02ff); /* version #2 */
1681 (void) WriteBlobMSBShort(image,PictInfoOp);
1682 (void) WriteBlobMSBLong(image,0xFFFE0000UL);
1683 /*
1684 Write full size of the file, resolution, frame bounding box, and reserved.
1685 */
1686 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1687 (void) WriteBlobMSBShort(image,0x0000);
1688 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1689 (void) WriteBlobMSBShort(image,0x0000);
1690 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top);
1691 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left);
1692 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom);
1693 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right);
1694 (void) WriteBlobMSBLong(image,0x00000000L);
1695 profile=GetImageProfile(image,"iptc");
1696 if (profile != (StringInfo *) NULL)
1697 {
1698 (void) WriteBlobMSBShort(image,0xa1);
1699 (void) WriteBlobMSBShort(image,0x1f2);
cristyed552522009-10-16 14:04:35 +00001700 (void) WriteBlobMSBShort(image,(unsigned short)
1701 (GetStringInfoLength(profile)+4));
cristy3ed852e2009-09-05 21:47:34 +00001702 (void) WriteBlobString(image,"8BIM");
cristyed552522009-10-16 14:04:35 +00001703 (void) WriteBlob(image,GetStringInfoLength(profile),
1704 GetStringInfoDatum(profile));
cristy3ed852e2009-09-05 21:47:34 +00001705 }
1706 profile=GetImageProfile(image,"icc");
1707 if (profile != (StringInfo *) NULL)
1708 {
1709 (void) WriteBlobMSBShort(image,0xa1);
1710 (void) WriteBlobMSBShort(image,0xe0);
cristyed552522009-10-16 14:04:35 +00001711 (void) WriteBlobMSBShort(image,(unsigned short)
1712 (GetStringInfoLength(profile)+4));
cristy3ed852e2009-09-05 21:47:34 +00001713 (void) WriteBlobMSBLong(image,0x00000000UL);
cristyed552522009-10-16 14:04:35 +00001714 (void) WriteBlob(image,GetStringInfoLength(profile),
1715 GetStringInfoDatum(profile));
cristy3ed852e2009-09-05 21:47:34 +00001716 (void) WriteBlobMSBShort(image,0xa1);
1717 (void) WriteBlobMSBShort(image,0xe0);
1718 (void) WriteBlobMSBShort(image,4);
1719 (void) WriteBlobMSBLong(image,0x00000002UL);
1720 }
1721 /*
1722 Write crop region opcode and crop bounding box.
1723 */
1724 (void) WriteBlobMSBShort(image,PictCropRegionOp);
1725 (void) WriteBlobMSBShort(image,0xa);
1726 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top);
1727 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left);
1728 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom);
1729 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right);
1730 if (image_info->compression == JPEGCompression)
1731 {
1732 Image
1733 *jpeg_image;
1734
1735 ImageInfo
1736 *jpeg_info;
1737
1738 size_t
1739 length;
1740
1741 unsigned char
1742 *blob;
1743
1744 jpeg_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1745 if (jpeg_image == (Image *) NULL)
1746 {
1747 (void) CloseBlob(image);
1748 return(MagickFalse);
1749 }
1750 jpeg_info=CloneImageInfo(image_info);
1751 (void) CopyMagickString(jpeg_info->magick,"JPEG",MaxTextExtent);
1752 length=0;
1753 blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length,
1754 &image->exception);
1755 jpeg_info=DestroyImageInfo(jpeg_info);
1756 if (blob == (unsigned char *) NULL)
1757 return(MagickFalse);
1758 jpeg_image=DestroyImage(jpeg_image);
1759 (void) WriteBlobMSBShort(image,PictJPEGOp);
cristyeaedf062010-05-29 22:36:02 +00001760 (void) WriteBlobMSBLong(image,(unsigned int) length+154);
cristy3ed852e2009-09-05 21:47:34 +00001761 (void) WriteBlobMSBShort(image,0x0000);
1762 (void) WriteBlobMSBLong(image,0x00010000UL);
1763 (void) WriteBlobMSBLong(image,0x00000000UL);
1764 (void) WriteBlobMSBLong(image,0x00000000UL);
1765 (void) WriteBlobMSBLong(image,0x00000000UL);
1766 (void) WriteBlobMSBLong(image,0x00010000UL);
1767 (void) WriteBlobMSBLong(image,0x00000000UL);
1768 (void) WriteBlobMSBLong(image,0x00000000UL);
1769 (void) WriteBlobMSBLong(image,0x00000000UL);
1770 (void) WriteBlobMSBLong(image,0x40000000UL);
1771 (void) WriteBlobMSBLong(image,0x00000000UL);
1772 (void) WriteBlobMSBLong(image,0x00000000UL);
1773 (void) WriteBlobMSBLong(image,0x00000000UL);
1774 (void) WriteBlobMSBLong(image,0x00400000UL);
1775 (void) WriteBlobMSBShort(image,0x0000);
1776 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1777 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1778 (void) WriteBlobMSBShort(image,0x0000);
1779 (void) WriteBlobMSBShort(image,768);
1780 (void) WriteBlobMSBShort(image,0x0000);
1781 (void) WriteBlobMSBLong(image,0x00000000UL);
1782 (void) WriteBlobMSBLong(image,0x00566A70UL);
1783 (void) WriteBlobMSBLong(image,0x65670000UL);
1784 (void) WriteBlobMSBLong(image,0x00000000UL);
1785 (void) WriteBlobMSBLong(image,0x00000001UL);
1786 (void) WriteBlobMSBLong(image,0x00016170UL);
1787 (void) WriteBlobMSBLong(image,0x706C0000UL);
1788 (void) WriteBlobMSBLong(image,0x00000000UL);
1789 (void) WriteBlobMSBShort(image,768);
1790 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1791 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1792 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1793 (void) WriteBlobMSBShort(image,0x0000);
1794 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1795 (void) WriteBlobMSBLong(image,0x00000000UL);
1796 (void) WriteBlobMSBLong(image,0x87AC0001UL);
1797 (void) WriteBlobMSBLong(image,0x0B466F74UL);
1798 (void) WriteBlobMSBLong(image,0x6F202D20UL);
1799 (void) WriteBlobMSBLong(image,0x4A504547UL);
1800 (void) WriteBlobMSBLong(image,0x00000000UL);
1801 (void) WriteBlobMSBLong(image,0x00000000UL);
1802 (void) WriteBlobMSBLong(image,0x00000000UL);
1803 (void) WriteBlobMSBLong(image,0x00000000UL);
1804 (void) WriteBlobMSBLong(image,0x00000000UL);
1805 (void) WriteBlobMSBLong(image,0x0018FFFFUL);
1806 (void) WriteBlob(image,length,blob);
1807 if ((length & 0x01) != 0)
1808 (void) WriteBlobByte(image,'\0');
1809 blob=(unsigned char *) RelinquishMagickMemory(blob);
1810 }
1811 /*
1812 Write picture opcode, row bytes, and picture bounding box, and version.
1813 */
1814 if (storage_class == PseudoClass)
1815 (void) WriteBlobMSBShort(image,PictPICTOp);
1816 else
1817 {
1818 (void) WriteBlobMSBShort(image,PictPixmapOp);
cristybb503372010-05-27 20:51:26 +00001819 (void) WriteBlobMSBLong(image,(size_t) base_address);
cristy3ed852e2009-09-05 21:47:34 +00001820 }
1821 (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000));
1822 (void) WriteBlobMSBShort(image,(unsigned short) bounds.top);
1823 (void) WriteBlobMSBShort(image,(unsigned short) bounds.left);
1824 (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom);
1825 (void) WriteBlobMSBShort(image,(unsigned short) bounds.right);
1826 /*
1827 Write pack type, pack size, resolution, pixel type, and pixel size.
1828 */
1829 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version);
1830 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type);
cristyeaedf062010-05-29 22:36:02 +00001831 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.pack_size);
cristy3ed852e2009-09-05 21:47:34 +00001832 (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5));
1833 (void) WriteBlobMSBShort(image,0x0000);
1834 (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5));
1835 (void) WriteBlobMSBShort(image,0x0000);
1836 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type);
1837 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel);
1838 /*
1839 Write component count, size, plane bytes, table size, and reserved.
1840 */
1841 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count);
1842 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size);
cristyeaedf062010-05-29 22:36:02 +00001843 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.plane_bytes);
1844 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.table);
1845 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.reserved);
cristy3ed852e2009-09-05 21:47:34 +00001846 if (storage_class == PseudoClass)
1847 {
1848 /*
1849 Write image colormap.
1850 */
1851 (void) WriteBlobMSBLong(image,0x00000000L); /* color seed */
1852 (void) WriteBlobMSBShort(image,0L); /* color flags */
1853 (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1));
cristybb503372010-05-27 20:51:26 +00001854 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001855 {
1856 (void) WriteBlobMSBShort(image,(unsigned short) i);
1857 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1858 image->colormap[i].red));
1859 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1860 image->colormap[i].green));
1861 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1862 image->colormap[i].blue));
1863 }
1864 }
1865 /*
1866 Write source and destination rectangle.
1867 */
1868 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top);
1869 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left);
1870 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom);
1871 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right);
1872 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top);
1873 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left);
1874 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom);
1875 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right);
1876 (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode);
1877 /*
1878 Write picture data.
1879 */
1880 count=0;
1881 if ((storage_class == PseudoClass) && (image->matte == MagickFalse))
cristybb503372010-05-27 20:51:26 +00001882 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001883 {
1884 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1885 if (p == (const PixelPacket *) NULL)
1886 break;
1887 indexes=GetVirtualIndexQueue(image);
cristybb503372010-05-27 20:51:26 +00001888 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001889 scanline[x]=(unsigned char) indexes[x];
cristybb503372010-05-27 20:51:26 +00001890 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
cristy3ed852e2009-09-05 21:47:34 +00001891 packed_scanline);
cristy8b27a6d2010-02-14 03:31:15 +00001892 if (image->previous == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001893 {
cristycee97112010-05-28 00:44:52 +00001894 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1895 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001896 if (status == MagickFalse)
1897 break;
1898 }
1899 }
1900 else
1901 if (image_info->compression == JPEGCompression)
1902 {
1903 (void) ResetMagickMemory(scanline,0,row_bytes);
cristybb503372010-05-27 20:51:26 +00001904 for (y=0; y < (ssize_t) image->rows; y++)
1905 count+=EncodeImage(image,scanline,(size_t)
cristy3ed852e2009-09-05 21:47:34 +00001906 (row_bytes & 0x7FFF),packed_scanline);
1907 }
1908 else
1909 {
1910 register unsigned char
1911 *blue,
1912 *green,
1913 *opacity,
1914 *red;
1915
1916 red=scanline;
1917 green=scanline+image->columns;
1918 blue=scanline+2*image->columns;
1919 opacity=scanline+3*image->columns;
cristybb503372010-05-27 20:51:26 +00001920 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001921 {
1922 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1923 if (p == (const PixelPacket *) NULL)
1924 break;
1925 red=scanline;
1926 green=scanline+image->columns;
1927 blue=scanline+2*image->columns;
1928 if (image->matte != MagickFalse)
1929 {
1930 opacity=scanline;
1931 red=scanline+image->columns;
1932 green=scanline+2*image->columns;
1933 blue=scanline+3*image->columns;
1934 }
cristybb503372010-05-27 20:51:26 +00001935 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001936 {
cristyce70c172010-01-07 17:15:30 +00001937 *red++=ScaleQuantumToChar(GetRedPixelComponent(p));
1938 *green++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1939 *blue++=ScaleQuantumToChar(GetBluePixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +00001940 if (image->matte != MagickFalse)
1941 *opacity++=ScaleQuantumToChar((Quantum)
cristy46f08202010-01-10 04:04:21 +00001942 (GetAlphaPixelComponent(p)));
cristy3ed852e2009-09-05 21:47:34 +00001943 p++;
1944 }
1945 count+=EncodeImage(image,scanline,bytes_per_line & 0x7FFF,
1946 packed_scanline);
cristy8b27a6d2010-02-14 03:31:15 +00001947 if (image->previous == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001948 {
cristycee97112010-05-28 00:44:52 +00001949 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1950 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001951 if (status == MagickFalse)
1952 break;
1953 }
1954 }
1955 }
1956 if ((count & 0x01) != 0)
1957 (void) WriteBlobByte(image,'\0');
1958 (void) WriteBlobMSBShort(image,PictEndOfPictureOp);
1959 offset=TellBlob(image);
1960 offset=SeekBlob(image,512,SEEK_SET);
1961 (void) WriteBlobMSBShort(image,(unsigned short) offset);
1962 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
1963 packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline);
1964 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1965 (void) CloseBlob(image);
1966 return(MagickTrue);
1967}