blob: 5c8554b38368231477f98bb8d97cd57f178972a9 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% PPPP IIIII CCCC TTTTT %
7% P P I C T %
8% PPPP I C T %
9% P I C T %
10% P IIIII CCCC T %
11% %
12% %
13% Read/Write Apple Macintosh QuickDraw/PICT Format %
14% %
15% Software Design %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
Cristy93b707b2017-12-06 07:05:51 -050020% Copyright 1999-2018 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% %
Cristyf19d4142017-04-24 11:34:30 -040026% https://www.imagemagick.org/script/license.php %
cristy3ed852e2009-09-05 21:47:34 +000027% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/blob.h"
44#include "MagickCore/blob-private.h"
45#include "MagickCore/cache.h"
46#include "MagickCore/color-private.h"
47#include "MagickCore/colormap.h"
48#include "MagickCore/colormap-private.h"
49#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000050#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000051#include "MagickCore/composite.h"
52#include "MagickCore/constitute.h"
53#include "MagickCore/exception.h"
54#include "MagickCore/exception-private.h"
55#include "MagickCore/image.h"
56#include "MagickCore/image-private.h"
57#include "MagickCore/list.h"
58#include "MagickCore/log.h"
59#include "MagickCore/magick.h"
60#include "MagickCore/memory_.h"
61#include "MagickCore/monitor.h"
62#include "MagickCore/monitor-private.h"
63#include "MagickCore/pixel-accessor.h"
64#include "MagickCore/profile.h"
65#include "MagickCore/resource_.h"
66#include "MagickCore/quantum-private.h"
67#include "MagickCore/static.h"
68#include "MagickCore/string_.h"
69#include "MagickCore/module.h"
70#include "MagickCore/transform.h"
71#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000072
73/*
74 ImageMagick Macintosh PICT Methods.
75*/
cristy3ed852e2009-09-05 21:47:34 +000076typedef struct _PICTCode
77{
78 const char
79 *name;
80
cristybb503372010-05-27 20:51:26 +000081 ssize_t
cristy3ed852e2009-09-05 21:47:34 +000082 length;
83
84 const char
85 *description;
86} PICTCode;
87
88typedef struct _PICTPixmap
89{
90 short
91 version,
92 pack_type;
93
cristybb503372010-05-27 20:51:26 +000094 size_t
cristy3ed852e2009-09-05 21:47:34 +000095 pack_size,
96 horizontal_resolution,
97 vertical_resolution;
98
99 short
100 pixel_type,
101 bits_per_pixel,
102 component_count,
103 component_size;
104
cristybb503372010-05-27 20:51:26 +0000105 size_t
cristy3ed852e2009-09-05 21:47:34 +0000106 plane_bytes,
107 table,
108 reserved;
109} PICTPixmap;
110
111typedef struct _PICTRectangle
112{
113 short
114 top,
115 left,
116 bottom,
117 right;
118} PICTRectangle;
119
120static const PICTCode
121 codes[] =
122 {
123 /* 0x00 */ { "NOP", 0, "nop" },
124 /* 0x01 */ { "Clip", 0, "clip" },
125 /* 0x02 */ { "BkPat", 8, "background pattern" },
126 /* 0x03 */ { "TxFont", 2, "text font (word)" },
127 /* 0x04 */ { "TxFace", 1, "text face (byte)" },
128 /* 0x05 */ { "TxMode", 2, "text mode (word)" },
129 /* 0x06 */ { "SpExtra", 4, "space extra (fixed point)" },
130 /* 0x07 */ { "PnSize", 4, "pen size (point)" },
131 /* 0x08 */ { "PnMode", 2, "pen mode (word)" },
132 /* 0x09 */ { "PnPat", 8, "pen pattern" },
133 /* 0x0a */ { "FillPat", 8, "fill pattern" },
134 /* 0x0b */ { "OvSize", 4, "oval size (point)" },
135 /* 0x0c */ { "Origin", 4, "dh, dv (word)" },
136 /* 0x0d */ { "TxSize", 2, "text size (word)" },
cristybb503372010-05-27 20:51:26 +0000137 /* 0x0e */ { "FgColor", 4, "foreground color (ssize_tword)" },
138 /* 0x0f */ { "BkColor", 4, "background color (ssize_tword)" },
cristy3ed852e2009-09-05 21:47:34 +0000139 /* 0x10 */ { "TxRatio", 8, "numerator (point), denominator (point)" },
140 /* 0x11 */ { "Version", 1, "version (byte)" },
141 /* 0x12 */ { "BkPixPat", 0, "color background pattern" },
142 /* 0x13 */ { "PnPixPat", 0, "color pen pattern" },
143 /* 0x14 */ { "FillPixPat", 0, "color fill pattern" },
144 /* 0x15 */ { "PnLocHFrac", 2, "fractional pen position" },
145 /* 0x16 */ { "ChExtra", 2, "extra for each character" },
146 /* 0x17 */ { "reserved", 0, "reserved for Apple use" },
147 /* 0x18 */ { "reserved", 0, "reserved for Apple use" },
148 /* 0x19 */ { "reserved", 0, "reserved for Apple use" },
149 /* 0x1a */ { "RGBFgCol", 6, "RGB foreColor" },
150 /* 0x1b */ { "RGBBkCol", 6, "RGB backColor" },
151 /* 0x1c */ { "HiliteMode", 0, "hilite mode flag" },
152 /* 0x1d */ { "HiliteColor", 6, "RGB hilite color" },
153 /* 0x1e */ { "DefHilite", 0, "Use default hilite color" },
154 /* 0x1f */ { "OpColor", 6, "RGB OpColor for arithmetic modes" },
155 /* 0x20 */ { "Line", 8, "pnLoc (point), newPt (point)" },
156 /* 0x21 */ { "LineFrom", 4, "newPt (point)" },
157 /* 0x22 */ { "ShortLine", 6, "pnLoc (point, dh, dv (-128 .. 127))" },
158 /* 0x23 */ { "ShortLineFrom", 2, "dh, dv (-128 .. 127)" },
159 /* 0x24 */ { "reserved", -1, "reserved for Apple use" },
160 /* 0x25 */ { "reserved", -1, "reserved for Apple use" },
161 /* 0x26 */ { "reserved", -1, "reserved for Apple use" },
162 /* 0x27 */ { "reserved", -1, "reserved for Apple use" },
163 /* 0x28 */ { "LongText", 0, "txLoc (point), count (0..255), text" },
164 /* 0x29 */ { "DHText", 0, "dh (0..255), count (0..255), text" },
165 /* 0x2a */ { "DVText", 0, "dv (0..255), count (0..255), text" },
166 /* 0x2b */ { "DHDVText", 0, "dh, dv (0..255), count (0..255), text" },
167 /* 0x2c */ { "reserved", -1, "reserved for Apple use" },
168 /* 0x2d */ { "reserved", -1, "reserved for Apple use" },
169 /* 0x2e */ { "reserved", -1, "reserved for Apple use" },
170 /* 0x2f */ { "reserved", -1, "reserved for Apple use" },
171 /* 0x30 */ { "frameRect", 8, "rect" },
172 /* 0x31 */ { "paintRect", 8, "rect" },
173 /* 0x32 */ { "eraseRect", 8, "rect" },
174 /* 0x33 */ { "invertRect", 8, "rect" },
175 /* 0x34 */ { "fillRect", 8, "rect" },
176 /* 0x35 */ { "reserved", 8, "reserved for Apple use" },
177 /* 0x36 */ { "reserved", 8, "reserved for Apple use" },
178 /* 0x37 */ { "reserved", 8, "reserved for Apple use" },
179 /* 0x38 */ { "frameSameRect", 0, "rect" },
180 /* 0x39 */ { "paintSameRect", 0, "rect" },
181 /* 0x3a */ { "eraseSameRect", 0, "rect" },
182 /* 0x3b */ { "invertSameRect", 0, "rect" },
183 /* 0x3c */ { "fillSameRect", 0, "rect" },
184 /* 0x3d */ { "reserved", 0, "reserved for Apple use" },
185 /* 0x3e */ { "reserved", 0, "reserved for Apple use" },
186 /* 0x3f */ { "reserved", 0, "reserved for Apple use" },
187 /* 0x40 */ { "frameRRect", 8, "rect" },
188 /* 0x41 */ { "paintRRect", 8, "rect" },
189 /* 0x42 */ { "eraseRRect", 8, "rect" },
190 /* 0x43 */ { "invertRRect", 8, "rect" },
191 /* 0x44 */ { "fillRRrect", 8, "rect" },
192 /* 0x45 */ { "reserved", 8, "reserved for Apple use" },
193 /* 0x46 */ { "reserved", 8, "reserved for Apple use" },
194 /* 0x47 */ { "reserved", 8, "reserved for Apple use" },
195 /* 0x48 */ { "frameSameRRect", 0, "rect" },
196 /* 0x49 */ { "paintSameRRect", 0, "rect" },
197 /* 0x4a */ { "eraseSameRRect", 0, "rect" },
198 /* 0x4b */ { "invertSameRRect", 0, "rect" },
199 /* 0x4c */ { "fillSameRRect", 0, "rect" },
200 /* 0x4d */ { "reserved", 0, "reserved for Apple use" },
201 /* 0x4e */ { "reserved", 0, "reserved for Apple use" },
202 /* 0x4f */ { "reserved", 0, "reserved for Apple use" },
203 /* 0x50 */ { "frameOval", 8, "rect" },
204 /* 0x51 */ { "paintOval", 8, "rect" },
205 /* 0x52 */ { "eraseOval", 8, "rect" },
206 /* 0x53 */ { "invertOval", 8, "rect" },
207 /* 0x54 */ { "fillOval", 8, "rect" },
208 /* 0x55 */ { "reserved", 8, "reserved for Apple use" },
209 /* 0x56 */ { "reserved", 8, "reserved for Apple use" },
210 /* 0x57 */ { "reserved", 8, "reserved for Apple use" },
211 /* 0x58 */ { "frameSameOval", 0, "rect" },
212 /* 0x59 */ { "paintSameOval", 0, "rect" },
213 /* 0x5a */ { "eraseSameOval", 0, "rect" },
214 /* 0x5b */ { "invertSameOval", 0, "rect" },
215 /* 0x5c */ { "fillSameOval", 0, "rect" },
216 /* 0x5d */ { "reserved", 0, "reserved for Apple use" },
217 /* 0x5e */ { "reserved", 0, "reserved for Apple use" },
218 /* 0x5f */ { "reserved", 0, "reserved for Apple use" },
219 /* 0x60 */ { "frameArc", 12, "rect, startAngle, arcAngle" },
220 /* 0x61 */ { "paintArc", 12, "rect, startAngle, arcAngle" },
221 /* 0x62 */ { "eraseArc", 12, "rect, startAngle, arcAngle" },
222 /* 0x63 */ { "invertArc", 12, "rect, startAngle, arcAngle" },
223 /* 0x64 */ { "fillArc", 12, "rect, startAngle, arcAngle" },
224 /* 0x65 */ { "reserved", 12, "reserved for Apple use" },
225 /* 0x66 */ { "reserved", 12, "reserved for Apple use" },
226 /* 0x67 */ { "reserved", 12, "reserved for Apple use" },
227 /* 0x68 */ { "frameSameArc", 4, "rect, startAngle, arcAngle" },
228 /* 0x69 */ { "paintSameArc", 4, "rect, startAngle, arcAngle" },
229 /* 0x6a */ { "eraseSameArc", 4, "rect, startAngle, arcAngle" },
230 /* 0x6b */ { "invertSameArc", 4, "rect, startAngle, arcAngle" },
231 /* 0x6c */ { "fillSameArc", 4, "rect, startAngle, arcAngle" },
232 /* 0x6d */ { "reserved", 4, "reserved for Apple use" },
233 /* 0x6e */ { "reserved", 4, "reserved for Apple use" },
234 /* 0x6f */ { "reserved", 4, "reserved for Apple use" },
235 /* 0x70 */ { "framePoly", 0, "poly" },
236 /* 0x71 */ { "paintPoly", 0, "poly" },
237 /* 0x72 */ { "erasePoly", 0, "poly" },
238 /* 0x73 */ { "invertPoly", 0, "poly" },
239 /* 0x74 */ { "fillPoly", 0, "poly" },
240 /* 0x75 */ { "reserved", 0, "reserved for Apple use" },
241 /* 0x76 */ { "reserved", 0, "reserved for Apple use" },
242 /* 0x77 */ { "reserved", 0, "reserved for Apple use" },
243 /* 0x78 */ { "frameSamePoly", 0, "poly (NYI)" },
244 /* 0x79 */ { "paintSamePoly", 0, "poly (NYI)" },
245 /* 0x7a */ { "eraseSamePoly", 0, "poly (NYI)" },
246 /* 0x7b */ { "invertSamePoly", 0, "poly (NYI)" },
247 /* 0x7c */ { "fillSamePoly", 0, "poly (NYI)" },
248 /* 0x7d */ { "reserved", 0, "reserved for Apple use" },
249 /* 0x7e */ { "reserved", 0, "reserved for Apple use" },
250 /* 0x7f */ { "reserved", 0, "reserved for Apple use" },
251 /* 0x80 */ { "frameRgn", 0, "region" },
252 /* 0x81 */ { "paintRgn", 0, "region" },
253 /* 0x82 */ { "eraseRgn", 0, "region" },
254 /* 0x83 */ { "invertRgn", 0, "region" },
255 /* 0x84 */ { "fillRgn", 0, "region" },
256 /* 0x85 */ { "reserved", 0, "reserved for Apple use" },
257 /* 0x86 */ { "reserved", 0, "reserved for Apple use" },
258 /* 0x87 */ { "reserved", 0, "reserved for Apple use" },
259 /* 0x88 */ { "frameSameRgn", 0, "region (NYI)" },
260 /* 0x89 */ { "paintSameRgn", 0, "region (NYI)" },
261 /* 0x8a */ { "eraseSameRgn", 0, "region (NYI)" },
262 /* 0x8b */ { "invertSameRgn", 0, "region (NYI)" },
263 /* 0x8c */ { "fillSameRgn", 0, "region (NYI)" },
264 /* 0x8d */ { "reserved", 0, "reserved for Apple use" },
265 /* 0x8e */ { "reserved", 0, "reserved for Apple use" },
266 /* 0x8f */ { "reserved", 0, "reserved for Apple use" },
267 /* 0x90 */ { "BitsRect", 0, "copybits, rect clipped" },
268 /* 0x91 */ { "BitsRgn", 0, "copybits, rgn clipped" },
269 /* 0x92 */ { "reserved", -1, "reserved for Apple use" },
270 /* 0x93 */ { "reserved", -1, "reserved for Apple use" },
271 /* 0x94 */ { "reserved", -1, "reserved for Apple use" },
272 /* 0x95 */ { "reserved", -1, "reserved for Apple use" },
273 /* 0x96 */ { "reserved", -1, "reserved for Apple use" },
274 /* 0x97 */ { "reserved", -1, "reserved for Apple use" },
275 /* 0x98 */ { "PackBitsRect", 0, "packed copybits, rect clipped" },
276 /* 0x99 */ { "PackBitsRgn", 0, "packed copybits, rgn clipped" },
277 /* 0x9a */ { "DirectBitsRect", 0, "PixMap, srcRect, dstRect, mode, PixData" },
278 /* 0x9b */ { "DirectBitsRgn", 0, "PixMap, srcRect, dstRect, mode, maskRgn, PixData" },
279 /* 0x9c */ { "reserved", -1, "reserved for Apple use" },
280 /* 0x9d */ { "reserved", -1, "reserved for Apple use" },
281 /* 0x9e */ { "reserved", -1, "reserved for Apple use" },
282 /* 0x9f */ { "reserved", -1, "reserved for Apple use" },
283 /* 0xa0 */ { "ShortComment", 2, "kind (word)" },
284 /* 0xa1 */ { "LongComment", 0, "kind (word), size (word), data" }
285 };
286
287/*
288 Forward declarations.
289*/
290static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +0000291 WritePICTImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000292
293/*
294%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295% %
296% %
297% %
298% D e c o d e I m a g e %
299% %
300% %
301% %
302%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303%
304% DecodeImage decompresses an image via Macintosh pack bits decoding for
305% Macintosh PICT images.
306%
307% The format of the DecodeImage method is:
308%
309% unsigned char *DecodeImage(Image *blob,Image *image,
cristybb503372010-05-27 20:51:26 +0000310% size_t bytes_per_line,const int bits_per_pixel,
cristy3ed852e2009-09-05 21:47:34 +0000311% unsigned size_t extent)
312%
313% A description of each parameter follows:
314%
315% o image_info: the image info.
316%
317% o blob,image: the address of a structure of type Image.
318%
319% o bytes_per_line: This integer identifies the number of bytes in a
320% scanline.
321%
322% o bits_per_pixel: the number of bits in a pixel.
323%
324% o extent: the number of pixels allocated.
325%
326*/
327
328static unsigned char *ExpandBuffer(unsigned char *pixels,
329 MagickSizeType *bytes_per_line,const unsigned int bits_per_pixel)
330{
cristybb503372010-05-27 20:51:26 +0000331 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000332 i;
333
334 register unsigned char
335 *p,
336 *q;
337
338 static unsigned char
339 scanline[8*256];
340
341 p=pixels;
342 q=scanline;
343 switch (bits_per_pixel)
344 {
345 case 8:
346 case 16:
347 case 32:
348 return(pixels);
349 case 4:
350 {
cristybb503372010-05-27 20:51:26 +0000351 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000352 {
353 *q++=(*p >> 4) & 0xff;
354 *q++=(*p & 15);
355 p++;
356 }
357 *bytes_per_line*=2;
358 break;
359 }
360 case 2:
361 {
cristybb503372010-05-27 20:51:26 +0000362 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000363 {
364 *q++=(*p >> 6) & 0x03;
365 *q++=(*p >> 4) & 0x03;
366 *q++=(*p >> 2) & 0x03;
367 *q++=(*p & 3);
368 p++;
369 }
370 *bytes_per_line*=4;
371 break;
372 }
373 case 1:
374 {
cristybb503372010-05-27 20:51:26 +0000375 for (i=0; i < (ssize_t) *bytes_per_line; i++)
cristy3ed852e2009-09-05 21:47:34 +0000376 {
377 *q++=(*p >> 7) & 0x01;
378 *q++=(*p >> 6) & 0x01;
379 *q++=(*p >> 5) & 0x01;
380 *q++=(*p >> 4) & 0x01;
381 *q++=(*p >> 3) & 0x01;
382 *q++=(*p >> 2) & 0x01;
383 *q++=(*p >> 1) & 0x01;
384 *q++=(*p & 0x01);
385 p++;
386 }
387 *bytes_per_line*=8;
388 break;
389 }
390 default:
391 break;
392 }
393 return(scanline);
394}
395
396static unsigned char *DecodeImage(Image *blob,Image *image,
cristyc82a27b2011-10-21 01:07:16 +0000397 size_t bytes_per_line,const unsigned int bits_per_pixel,size_t *extent,
398 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000399{
cristy3ed852e2009-09-05 21:47:34 +0000400 MagickSizeType
401 number_pixels;
402
cristybb503372010-05-27 20:51:26 +0000403 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000404 i;
405
406 register unsigned char
407 *p,
408 *q;
409
410 size_t
cristyaff6d802011-04-26 01:46:31 +0000411 bytes_per_pixel,
cristy3ed852e2009-09-05 21:47:34 +0000412 length,
cristyaff6d802011-04-26 01:46:31 +0000413 row_bytes,
414 scanline_length,
415 width;
cristy3ed852e2009-09-05 21:47:34 +0000416
417 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000418 count,
419 j,
420 y;
cristy3ed852e2009-09-05 21:47:34 +0000421
422 unsigned char
423 *pixels,
424 *scanline;
425
cristy3ed852e2009-09-05 21:47:34 +0000426 /*
427 Determine pixel buffer size.
428 */
429 if (bits_per_pixel <= 8)
430 bytes_per_line&=0x7fff;
431 width=image->columns;
432 bytes_per_pixel=1;
433 if (bits_per_pixel == 16)
434 {
435 bytes_per_pixel=2;
436 width*=2;
437 }
438 else
439 if (bits_per_pixel == 32)
cristy8a46d822012-08-28 23:32:39 +0000440 width*=image->alpha_trait ? 4 : 3;
cristy3ed852e2009-09-05 21:47:34 +0000441 if (bytes_per_line == 0)
442 bytes_per_line=width;
443 row_bytes=(size_t) (image->columns | 0x8000);
444 if (image->storage_class == DirectClass)
445 row_bytes=(size_t) ((4*image->columns) | 0x8000);
446 /*
447 Allocate pixel and scanline buffer.
448 */
449 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,row_bytes*
450 sizeof(*pixels));
451 if (pixels == (unsigned char *) NULL)
452 return((unsigned char *) NULL);
453 *extent=row_bytes*image->rows*sizeof(*pixels);
Cristy81bfff22018-03-10 07:58:31 -0500454 (void) memset(pixels,0,*extent);
cristyf432c632014-12-07 15:11:28 +0000455 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,2*
456 sizeof(*scanline));
cristy3ed852e2009-09-05 21:47:34 +0000457 if (scanline == (unsigned char *) NULL)
Cristy5ebe27c2017-09-30 11:30:43 -0400458 {
459 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
460 return((unsigned char *) NULL);
461 }
Cristy81bfff22018-03-10 07:58:31 -0500462 (void) memset(scanline,0,2*row_bytes*sizeof(*scanline));
cristy3ed852e2009-09-05 21:47:34 +0000463 if (bytes_per_line < 8)
464 {
465 /*
466 Pixels are already uncompressed.
467 */
cristybb503372010-05-27 20:51:26 +0000468 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000469 {
Cristy7c892c92018-03-23 20:16:36 -0400470 q=pixels+y*width*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000471 number_pixels=bytes_per_line;
472 count=ReadBlob(blob,(size_t) number_pixels,scanline);
cristy6d62e4b2015-01-11 14:07:17 +0000473 if (count != (ssize_t) number_pixels)
474 {
475 (void) ThrowMagickException(exception,GetMagickModule(),
476 CorruptImageError,"UnableToUncompressImage","`%s'",
477 image->filename);
478 break;
479 }
cristy3ed852e2009-09-05 21:47:34 +0000480 p=ExpandBuffer(scanline,&number_pixels,bits_per_pixel);
481 if ((q+number_pixels) > (pixels+(*extent)))
482 {
cristyc82a27b2011-10-21 01:07:16 +0000483 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000484 CorruptImageError,"UnableToUncompressImage","`%s'",
485 image->filename);
486 break;
487 }
Cristyfc31dc52018-03-10 10:42:46 -0500488 (void) memcpy(q,p,(size_t) number_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000489 }
490 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
491 return(pixels);
492 }
493 /*
494 Uncompress RLE pixels into uncompressed pixel buffer.
495 */
cristybb503372010-05-27 20:51:26 +0000496 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000497 {
498 q=pixels+y*width;
499 if (bytes_per_line > 200)
500 scanline_length=ReadBlobMSBShort(blob);
501 else
Cristy18718a92018-03-25 12:45:12 -0400502 scanline_length=(size_t) ReadBlobByte(blob);
Dirk Lemstrac2c16222018-03-16 12:06:03 +0100503 if ((scanline_length >= row_bytes) || (scanline_length == 0))
cristy3ed852e2009-09-05 21:47:34 +0000504 {
cristyc82a27b2011-10-21 01:07:16 +0000505 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000506 CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
507 break;
508 }
509 count=ReadBlob(blob,scanline_length,scanline);
cristy6d62e4b2015-01-11 14:07:17 +0000510 if (count != (ssize_t) scanline_length)
511 {
512 (void) ThrowMagickException(exception,GetMagickModule(),
513 CorruptImageError,"UnableToUncompressImage","`%s'",image->filename);
514 break;
515 }
cristybb503372010-05-27 20:51:26 +0000516 for (j=0; j < (ssize_t) scanline_length; )
cristy3ed852e2009-09-05 21:47:34 +0000517 if ((scanline[j] & 0x80) == 0)
518 {
519 length=(size_t) ((scanline[j] & 0xff)+1);
520 number_pixels=length*bytes_per_pixel;
521 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
522 if ((q-pixels+number_pixels) <= *extent)
Cristyfc31dc52018-03-10 10:42:46 -0500523 (void) memcpy(q,p,(size_t) number_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000524 q+=number_pixels;
cristybb503372010-05-27 20:51:26 +0000525 j+=(ssize_t) (length*bytes_per_pixel+1);
cristy3ed852e2009-09-05 21:47:34 +0000526 }
527 else
528 {
529 length=(size_t) (((scanline[j] ^ 0xff) & 0xff)+2);
530 number_pixels=bytes_per_pixel;
531 p=ExpandBuffer(scanline+j+1,&number_pixels,bits_per_pixel);
cristybb503372010-05-27 20:51:26 +0000532 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +0000533 {
534 if ((q-pixels+number_pixels) <= *extent)
Cristyfc31dc52018-03-10 10:42:46 -0500535 (void) memcpy(q,p,(size_t) number_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000536 q+=number_pixels;
537 }
cristyeaedf062010-05-29 22:36:02 +0000538 j+=(ssize_t) bytes_per_pixel+1;
cristy3ed852e2009-09-05 21:47:34 +0000539 }
540 }
541 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
542 return(pixels);
543}
544
545/*
546%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547% %
548% %
549% %
550% E n c o d e I m a g e %
551% %
552% %
553% %
554%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555%
556% EncodeImage compresses an image via Macintosh pack bits encoding
557% for Macintosh PICT images.
558%
559% The format of the EncodeImage method is:
560%
561% size_t EncodeImage(Image *image,const unsigned char *scanline,
cristybb503372010-05-27 20:51:26 +0000562% const size_t bytes_per_line,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000563%
564% A description of each parameter follows:
565%
566% o image: the address of a structure of type Image.
567%
568% o scanline: A pointer to an array of characters to pack.
569%
570% o bytes_per_line: the number of bytes in a scanline.
571%
572% o pixels: A pointer to an array of characters where the packed
573% characters are stored.
574%
cristy3ed852e2009-09-05 21:47:34 +0000575*/
576static size_t EncodeImage(Image *image,const unsigned char *scanline,
cristybb503372010-05-27 20:51:26 +0000577 const size_t bytes_per_line,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000578{
579#define MaxCount 128
580#define MaxPackbitsRunlength 128
581
cristy3ed852e2009-09-05 21:47:34 +0000582 register const unsigned char
583 *p;
584
cristybb503372010-05-27 20:51:26 +0000585 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000586 i;
587
588 register unsigned char
589 *q;
590
591 size_t
592 length;
593
cristyaff6d802011-04-26 01:46:31 +0000594 ssize_t
595 count,
596 repeat_count,
597 runlength;
598
cristy3ed852e2009-09-05 21:47:34 +0000599 unsigned char
600 index;
601
602 /*
603 Pack scanline.
604 */
605 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000606 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000607 if (image->debug != MagickFalse)
608 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
609 assert(scanline != (unsigned char *) NULL);
610 assert(pixels != (unsigned char *) NULL);
611 count=0;
612 runlength=0;
613 p=scanline+(bytes_per_line-1);
614 q=pixels;
615 index=(*p);
cristybb503372010-05-27 20:51:26 +0000616 for (i=(ssize_t) bytes_per_line-1; i >= 0; i--)
cristy3ed852e2009-09-05 21:47:34 +0000617 {
618 if (index == *p)
619 runlength++;
620 else
621 {
622 if (runlength < 3)
623 while (runlength > 0)
624 {
625 *q++=(unsigned char) index;
626 runlength--;
627 count++;
628 if (count == MaxCount)
629 {
630 *q++=(unsigned char) (MaxCount-1);
631 count-=MaxCount;
632 }
633 }
634 else
635 {
636 if (count > 0)
637 *q++=(unsigned char) (count-1);
638 count=0;
639 while (runlength > 0)
640 {
641 repeat_count=runlength;
642 if (repeat_count > MaxPackbitsRunlength)
643 repeat_count=MaxPackbitsRunlength;
644 *q++=(unsigned char) index;
645 *q++=(unsigned char) (257-repeat_count);
646 runlength-=repeat_count;
647 }
648 }
649 runlength=1;
650 }
651 index=(*p);
652 p--;
653 }
654 if (runlength < 3)
655 while (runlength > 0)
656 {
657 *q++=(unsigned char) index;
658 runlength--;
659 count++;
660 if (count == MaxCount)
661 {
662 *q++=(unsigned char) (MaxCount-1);
663 count-=MaxCount;
664 }
665 }
666 else
667 {
668 if (count > 0)
669 *q++=(unsigned char) (count-1);
670 count=0;
671 while (runlength > 0)
672 {
673 repeat_count=runlength;
674 if (repeat_count > MaxPackbitsRunlength)
675 repeat_count=MaxPackbitsRunlength;
676 *q++=(unsigned char) index;
677 *q++=(unsigned char) (257-repeat_count);
678 runlength-=repeat_count;
679 }
680 }
681 if (count > 0)
682 *q++=(unsigned char) (count-1);
683 /*
684 Write the number of and the packed length.
685 */
686 length=(size_t) (q-pixels);
687 if (bytes_per_line > 200)
688 {
689 (void) WriteBlobMSBShort(image,(unsigned short) length);
690 length+=2;
691 }
692 else
693 {
694 (void) WriteBlobByte(image,(unsigned char) length);
695 length++;
696 }
697 while (q != pixels)
698 {
699 q--;
700 (void) WriteBlobByte(image,*q);
701 }
702 return(length);
703}
704
705/*
706%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
707% %
708% %
709% %
710% I s P I C T %
711% %
712% %
713% %
714%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
715%
716% IsPICT()() returns MagickTrue if the image format type, identified by the
717% magick string, is PICT.
718%
719% The format of the ReadPICTImage method is:
720%
721% MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
722%
723% A description of each parameter follows:
724%
725% o magick: compare image format pattern against these bytes.
726%
727% o length: Specifies the length of the magick string.
728%
cristy3ed852e2009-09-05 21:47:34 +0000729*/
730static MagickBooleanType IsPICT(const unsigned char *magick,const size_t length)
731{
cristy80af6b62012-11-12 12:48:16 +0000732 if (length < 12)
733 return(MagickFalse);
734 /*
735 Embedded OLE2 macintosh have "PICT" instead of 512 platform header.
736 */
737 if (memcmp(magick,"PICT",4) == 0)
738 return(MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000739 if (length < 528)
740 return(MagickFalse);
741 if (memcmp(magick+522,"\000\021\002\377\014\000",6) == 0)
742 return(MagickTrue);
743 return(MagickFalse);
744}
745
746#if !defined(macintosh)
747/*
748%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
749% %
750% %
751% %
752% R e a d P I C T I m a g e %
753% %
754% %
755% %
756%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757%
758% ReadPICTImage() reads an Apple Macintosh QuickDraw/PICT image file
759% and returns it. It allocates the memory necessary for the new Image
760% structure and returns a pointer to the new image.
761%
762% The format of the ReadPICTImage method is:
763%
764% Image *ReadPICTImage(const ImageInfo *image_info,
765% ExceptionInfo *exception)
766%
767% A description of each parameter follows:
768%
769% o image_info: the image info.
770%
771% o exception: return any errors or warnings in this structure.
772%
773*/
774
Cristyffcb8f82017-07-25 07:46:37 -0400775static MagickBooleanType ReadPixmap(Image *image,PICTPixmap *pixmap)
776{
777 pixmap->version=(short) ReadBlobMSBShort(image);
778 pixmap->pack_type=(short) ReadBlobMSBShort(image);
779 pixmap->pack_size=ReadBlobMSBLong(image);
780 pixmap->horizontal_resolution=1UL*ReadBlobMSBShort(image);
781 (void) ReadBlobMSBShort(image);
782 pixmap->vertical_resolution=1UL*ReadBlobMSBShort(image);
783 (void) ReadBlobMSBShort(image);
784 pixmap->pixel_type=(short) ReadBlobMSBShort(image);
785 pixmap->bits_per_pixel=(short) ReadBlobMSBShort(image);
786 pixmap->component_count=(short) ReadBlobMSBShort(image);
787 pixmap->component_size=(short) ReadBlobMSBShort(image);
788 pixmap->plane_bytes=ReadBlobMSBLong(image);
789 pixmap->table=ReadBlobMSBLong(image);
790 pixmap->reserved=ReadBlobMSBLong(image);
791 if ((EOFBlob(image) != MagickFalse) || (pixmap->bits_per_pixel <= 0) ||
792 (pixmap->bits_per_pixel > 32) || (pixmap->component_count <= 0) ||
793 (pixmap->component_count > 4) || (pixmap->component_size <= 0))
794 return(MagickFalse);
795 return(MagickTrue);
796}
797
cristyc944c572014-05-12 00:19:16 +0000798static MagickBooleanType ReadRectangle(Image *image,PICTRectangle *rectangle)
799{
800 rectangle->top=(short) ReadBlobMSBShort(image);
801 rectangle->left=(short) ReadBlobMSBShort(image);
802 rectangle->bottom=(short) ReadBlobMSBShort(image);
803 rectangle->right=(short) ReadBlobMSBShort(image);
Cristy68768752018-02-26 20:32:42 -0500804 if ((EOFBlob(image) != MagickFalse) ||
Cristyd813fea2018-02-26 20:57:21 -0500805 ((rectangle->bottom-rectangle->top) <= 0) ||
806 ((rectangle->right-rectangle->left) <= 0))
cristyc944c572014-05-12 00:19:16 +0000807 return(MagickFalse);
808 return(MagickTrue);
809}
810
cristy3ed852e2009-09-05 21:47:34 +0000811static Image *ReadPICTImage(const ImageInfo *image_info,
812 ExceptionInfo *exception)
813{
Cristy18718a92018-03-25 12:45:12 -0400814#define ThrowPICTException(exception,message) \
815{ \
816 if (tile_image != (Image *) NULL) \
817 tile_image=DestroyImage(tile_image); \
818 if (read_info != (ImageInfo *) NULL) \
819 read_info=DestroyImageInfo(read_info); \
820 ThrowReaderException((exception),(message)); \
821}
822
cristy3ed852e2009-09-05 21:47:34 +0000823 char
cristy151b66d2015-04-15 10:50:31 +0000824 geometry[MagickPathExtent],
cristy80af6b62012-11-12 12:48:16 +0000825 header_ole[4];
cristy3ed852e2009-09-05 21:47:34 +0000826
827 Image
Cristy18718a92018-03-25 12:45:12 -0400828 *image,
829 *tile_image;
830
831 ImageInfo
832 *read_info;
cristy3ed852e2009-09-05 21:47:34 +0000833
cristy3ed852e2009-09-05 21:47:34 +0000834 int
835 c,
836 code;
837
cristy3ed852e2009-09-05 21:47:34 +0000838 MagickBooleanType
839 jpeg,
840 status;
841
842 PICTRectangle
843 frame;
844
845 PICTPixmap
846 pixmap;
847
cristy4c08aed2011-07-01 19:47:50 +0000848 Quantum
849 index;
cristy3ed852e2009-09-05 21:47:34 +0000850
cristy4c08aed2011-07-01 19:47:50 +0000851 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000852 *q;
853
cristybb503372010-05-27 20:51:26 +0000854 register ssize_t
cristy4c08aed2011-07-01 19:47:50 +0000855 i,
856 x;
cristy3ed852e2009-09-05 21:47:34 +0000857
858 size_t
859 extent,
860 length;
861
862 ssize_t
cristyaff6d802011-04-26 01:46:31 +0000863 count,
864 flags,
865 j,
866 version,
867 y;
cristy3ed852e2009-09-05 21:47:34 +0000868
869 StringInfo
870 *profile;
871
872 /*
873 Open image file.
874 */
875 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000876 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000877 if (image_info->debug != MagickFalse)
878 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
879 image_info->filename);
880 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000881 assert(exception->signature == MagickCoreSignature);
cristy9950d572011-10-01 18:22:35 +0000882 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000883 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
884 if (status == MagickFalse)
885 {
886 image=DestroyImageList(image);
887 return((Image *) NULL);
888 }
889 /*
890 Read PICT header.
891 */
Cristy18718a92018-03-25 12:45:12 -0400892 read_info=(ImageInfo *) NULL;
893 tile_image=(Image *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000894 pixmap.bits_per_pixel=0;
895 pixmap.component_count=0;
cristy80af6b62012-11-12 12:48:16 +0000896 /*
897 Skip header : 512 for standard PICT and 4, ie "PICT" for OLE2.
898 */
899 header_ole[0]=ReadBlobByte(image);
900 header_ole[1]=ReadBlobByte(image);
901 header_ole[2]=ReadBlobByte(image);
902 header_ole[3]=ReadBlobByte(image);
903 if (!((header_ole[0] == 0x50) && (header_ole[1] == 0x49) &&
904 (header_ole[2] == 0x43) && (header_ole[3] == 0x54 )))
905 for (i=0; i < 508; i++)
cristye11ddbd2015-03-20 14:34:55 +0000906 if (ReadBlobByte(image) == EOF)
907 break;
cristy3ed852e2009-09-05 21:47:34 +0000908 (void) ReadBlobMSBShort(image); /* skip picture size */
cristyc944c572014-05-12 00:19:16 +0000909 if (ReadRectangle(image,&frame) == MagickFalse)
Cristy18718a92018-03-25 12:45:12 -0400910 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000911 while ((c=ReadBlobByte(image)) == 0) ;
912 if (c != 0x11)
Cristy18718a92018-03-25 12:45:12 -0400913 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
914 version=(ssize_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000915 if (version == 2)
916 {
917 c=ReadBlobByte(image);
918 if (c != 0xff)
Cristy18718a92018-03-25 12:45:12 -0400919 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000920 }
921 else
922 if (version != 1)
Cristy18718a92018-03-25 12:45:12 -0400923 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000924 if ((frame.left < 0) || (frame.right < 0) || (frame.top < 0) ||
925 (frame.bottom < 0) || (frame.left >= frame.right) ||
926 (frame.top >= frame.bottom))
Cristy18718a92018-03-25 12:45:12 -0400927 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000928 /*
929 Create black canvas.
930 */
931 flags=0;
cristy65cb9632012-12-08 00:34:29 +0000932 image->depth=8;
Cristy18718a92018-03-25 12:45:12 -0400933 image->columns=(size_t) (frame.right-frame.left);
934 image->rows=(size_t) (frame.bottom-frame.top);
cristy2a11bef2011-10-28 18:33:11 +0000935 image->resolution.x=DefaultResolution;
936 image->resolution.y=DefaultResolution;
cristy3ed852e2009-09-05 21:47:34 +0000937 image->units=UndefinedResolution;
cristyd6d3b0d2015-01-01 17:46:31 +0000938 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
939 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
940 {
941 (void) CloseBlob(image);
942 return(GetFirstImageInList(image));
943 }
944 status=SetImageExtent(image,image->columns,image->rows,exception);
945 if (status == MagickFalse)
946 return(DestroyImageList(image));
Cristyd1ef38e2018-03-17 10:19:50 -0400947 status=ResetImagePixels(image,exception);
948 if (status == MagickFalse)
949 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +0000950 /*
951 Interpret PICT opcodes.
952 */
953 jpeg=MagickFalse;
954 for (code=0; EOFBlob(image) == MagickFalse; )
955 {
956 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
957 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
958 break;
959 if ((version == 1) || ((TellBlob(image) % 2) != 0))
960 code=ReadBlobByte(image);
961 if (version == 2)
Cristy1fe0b872016-04-23 10:48:31 -0400962 code=ReadBlobMSBSignedShort(image);
cristyf9f85022014-05-18 12:46:29 +0000963 if (code < 0)
964 break;
Dirk Lemstrac2c16222018-03-16 12:06:03 +0100965 if (code == 0)
966 continue;
cristy3ed852e2009-09-05 21:47:34 +0000967 if (code > 0xa1)
968 {
969 if (image->debug != MagickFalse)
970 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%04X:",code);
971 }
972 else
973 {
974 if (image->debug != MagickFalse)
975 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
976 " %04X %s: %s",code,codes[code].name,codes[code].description);
977 switch (code)
978 {
979 case 0x01:
980 {
981 /*
982 Clipping rectangle.
983 */
984 length=ReadBlobMSBShort(image);
985 if (length != 0x000a)
986 {
cristybb503372010-05-27 20:51:26 +0000987 for (i=0; i < (ssize_t) (length-2); i++)
cristye11ddbd2015-03-20 14:34:55 +0000988 if (ReadBlobByte(image) == EOF)
989 break;
cristy3ed852e2009-09-05 21:47:34 +0000990 break;
991 }
cristyc944c572014-05-12 00:19:16 +0000992 if (ReadRectangle(image,&frame) == MagickFalse)
Cristy18718a92018-03-25 12:45:12 -0400993 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000994 if (((frame.left & 0x8000) != 0) || ((frame.top & 0x8000) != 0))
995 break;
Cristy18718a92018-03-25 12:45:12 -0400996 image->columns=(size_t) (frame.right-frame.left);
997 image->rows=(size_t) (frame.bottom-frame.top);
Cristy2b008a82015-09-07 09:51:07 -0400998 status=SetImageExtent(image,image->columns,image->rows,exception);
999 if (status == MagickFalse)
1000 return(DestroyImageList(image));
Cristydf22c922018-03-20 19:34:26 -04001001 status=ResetImagePixels(image,exception);
1002 if (status == MagickFalse)
1003 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +00001004 break;
1005 }
1006 case 0x12:
1007 case 0x13:
1008 case 0x14:
1009 {
cristybb503372010-05-27 20:51:26 +00001010 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001011 pattern;
1012
cristybb503372010-05-27 20:51:26 +00001013 size_t
cristy3ed852e2009-09-05 21:47:34 +00001014 height,
1015 width;
1016
1017 /*
1018 Skip pattern definition.
1019 */
Cristy18718a92018-03-25 12:45:12 -04001020 pattern=(ssize_t) ReadBlobMSBShort(image);
cristy3ed852e2009-09-05 21:47:34 +00001021 for (i=0; i < 8; i++)
cristye11ddbd2015-03-20 14:34:55 +00001022 if (ReadBlobByte(image) == EOF)
1023 break;
cristy3ed852e2009-09-05 21:47:34 +00001024 if (pattern == 2)
1025 {
1026 for (i=0; i < 5; i++)
cristye11ddbd2015-03-20 14:34:55 +00001027 if (ReadBlobByte(image) == EOF)
1028 break;
cristy3ed852e2009-09-05 21:47:34 +00001029 break;
1030 }
1031 if (pattern != 1)
Cristy18718a92018-03-25 12:45:12 -04001032 ThrowPICTException(CorruptImageError,"UnknownPatternType");
cristy3ed852e2009-09-05 21:47:34 +00001033 length=ReadBlobMSBShort(image);
cristyc944c572014-05-12 00:19:16 +00001034 if (ReadRectangle(image,&frame) == MagickFalse)
Cristy18718a92018-03-25 12:45:12 -04001035 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
Cristyffcb8f82017-07-25 07:46:37 -04001036 if (ReadPixmap(image,&pixmap) == MagickFalse)
Cristy18718a92018-03-25 12:45:12 -04001037 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
1038 image->depth=(size_t) pixmap.component_size;
cristy2a11bef2011-10-28 18:33:11 +00001039 image->resolution.x=1.0*pixmap.horizontal_resolution;
1040 image->resolution.y=1.0*pixmap.vertical_resolution;
cristy3ed852e2009-09-05 21:47:34 +00001041 image->units=PixelsPerInchResolution;
1042 (void) ReadBlobMSBLong(image);
Cristy18718a92018-03-25 12:45:12 -04001043 flags=(ssize_t) ReadBlobMSBShort(image);
cristy3ed852e2009-09-05 21:47:34 +00001044 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001045 for (i=0; i <= (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00001046 (void) ReadBlobMSBLong(image);
Cristy18718a92018-03-25 12:45:12 -04001047 width=(size_t) (frame.bottom-frame.top);
1048 height=(size_t) (frame.right-frame.left);
cristy3ed852e2009-09-05 21:47:34 +00001049 if (pixmap.bits_per_pixel <= 8)
1050 length&=0x7fff;
1051 if (pixmap.bits_per_pixel == 16)
1052 width<<=1;
1053 if (length == 0)
1054 length=width;
1055 if (length < 8)
1056 {
cristybb503372010-05-27 20:51:26 +00001057 for (i=0; i < (ssize_t) (length*height); i++)
cristye11ddbd2015-03-20 14:34:55 +00001058 if (ReadBlobByte(image) == EOF)
1059 break;
cristy3ed852e2009-09-05 21:47:34 +00001060 }
1061 else
Cristy18718a92018-03-25 12:45:12 -04001062 for (i=0; i < (ssize_t) height; i++)
Cristy68768752018-02-26 20:32:42 -05001063 {
1064 if (EOFBlob(image) != MagickFalse)
1065 break;
cristy3ed852e2009-09-05 21:47:34 +00001066 if (length > 200)
cristy46a54342015-03-29 00:16:16 +00001067 {
1068 for (j=0; j < (ssize_t) ReadBlobMSBShort(image); j++)
1069 if (ReadBlobByte(image) == EOF)
1070 break;
1071 }
cristy3ed852e2009-09-05 21:47:34 +00001072 else
cristybb503372010-05-27 20:51:26 +00001073 for (j=0; j < (ssize_t) ReadBlobByte(image); j++)
cristye11ddbd2015-03-20 14:34:55 +00001074 if (ReadBlobByte(image) == EOF)
1075 break;
Cristy68768752018-02-26 20:32:42 -05001076 }
cristy3ed852e2009-09-05 21:47:34 +00001077 break;
1078 }
1079 case 0x1b:
1080 {
1081 /*
1082 Initialize image background color.
1083 */
1084 image->background_color.red=(Quantum)
1085 ScaleShortToQuantum(ReadBlobMSBShort(image));
1086 image->background_color.green=(Quantum)
1087 ScaleShortToQuantum(ReadBlobMSBShort(image));
1088 image->background_color.blue=(Quantum)
1089 ScaleShortToQuantum(ReadBlobMSBShort(image));
1090 break;
1091 }
1092 case 0x70:
1093 case 0x71:
1094 case 0x72:
1095 case 0x73:
1096 case 0x74:
1097 case 0x75:
1098 case 0x76:
1099 case 0x77:
1100 {
1101 /*
1102 Skip polygon or region.
1103 */
1104 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001105 for (i=0; i < (ssize_t) (length-2); i++)
cristye11ddbd2015-03-20 14:34:55 +00001106 if (ReadBlobByte(image) == EOF)
1107 break;
cristy3ed852e2009-09-05 21:47:34 +00001108 break;
1109 }
1110 case 0x90:
1111 case 0x91:
1112 case 0x98:
1113 case 0x99:
1114 case 0x9a:
1115 case 0x9b:
1116 {
cristy3ed852e2009-09-05 21:47:34 +00001117 PICTRectangle
1118 source,
1119 destination;
1120
1121 register unsigned char
1122 *p;
1123
1124 size_t
1125 j;
1126
cristy4c08aed2011-07-01 19:47:50 +00001127 ssize_t
1128 bytes_per_line;
1129
cristy3ed852e2009-09-05 21:47:34 +00001130 unsigned char
1131 *pixels;
1132
cristy3ed852e2009-09-05 21:47:34 +00001133 /*
1134 Pixmap clipped by a rectangle.
1135 */
1136 bytes_per_line=0;
1137 if ((code != 0x9a) && (code != 0x9b))
Cristy18718a92018-03-25 12:45:12 -04001138 bytes_per_line=(ssize_t) ReadBlobMSBShort(image);
cristy3ed852e2009-09-05 21:47:34 +00001139 else
1140 {
1141 (void) ReadBlobMSBShort(image);
1142 (void) ReadBlobMSBShort(image);
1143 (void) ReadBlobMSBShort(image);
1144 }
cristyc944c572014-05-12 00:19:16 +00001145 if (ReadRectangle(image,&frame) == MagickFalse)
Cristy18718a92018-03-25 12:45:12 -04001146 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +00001147 /*
1148 Initialize tile image.
1149 */
Cristy18718a92018-03-25 12:45:12 -04001150 tile_image=CloneImage(image,(size_t) (frame.right-frame.left),
1151 (size_t) (frame.bottom-frame.top),MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001152 if (tile_image == (Image *) NULL)
Cristy18718a92018-03-25 12:45:12 -04001153 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +00001154 if ((code == 0x9a) || (code == 0x9b) ||
1155 ((bytes_per_line & 0x8000) != 0))
1156 {
Cristyffcb8f82017-07-25 07:46:37 -04001157 if (ReadPixmap(image,&pixmap) == MagickFalse)
Cristy18718a92018-03-25 12:45:12 -04001158 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
1159 tile_image->depth=(size_t) pixmap.component_size;
cristy8a46d822012-08-28 23:32:39 +00001160 tile_image->alpha_trait=pixmap.component_count == 4 ?
cristyb0a657e2012-08-29 00:45:37 +00001161 BlendPixelTrait : UndefinedPixelTrait;
cristy2a11bef2011-10-28 18:33:11 +00001162 tile_image->resolution.x=(double) pixmap.horizontal_resolution;
1163 tile_image->resolution.y=(double) pixmap.vertical_resolution;
cristy3ed852e2009-09-05 21:47:34 +00001164 tile_image->units=PixelsPerInchResolution;
cristy17f11b02014-12-20 19:37:04 +00001165 if (tile_image->alpha_trait != UndefinedPixelTrait)
Cristy5fb86b22018-03-31 08:27:42 -04001166 (void) SetImageAlpha(tile_image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +00001167 }
1168 if ((code != 0x9a) && (code != 0x9b))
1169 {
1170 /*
1171 Initialize colormap.
1172 */
1173 tile_image->colors=2;
1174 if ((bytes_per_line & 0x8000) != 0)
1175 {
1176 (void) ReadBlobMSBLong(image);
Cristy18718a92018-03-25 12:45:12 -04001177 flags=(ssize_t) ReadBlobMSBShort(image);
cristy3ed852e2009-09-05 21:47:34 +00001178 tile_image->colors=1UL*ReadBlobMSBShort(image)+1;
1179 }
cristy018f07f2011-09-04 21:15:19 +00001180 status=AcquireImageColormap(tile_image,tile_image->colors,
1181 exception);
cristy3ed852e2009-09-05 21:47:34 +00001182 if (status == MagickFalse)
Cristy18718a92018-03-25 12:45:12 -04001183 ThrowPICTException(ResourceLimitError,
1184 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00001185 if ((bytes_per_line & 0x8000) != 0)
1186 {
cristybb503372010-05-27 20:51:26 +00001187 for (i=0; i < (ssize_t) tile_image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001188 {
1189 j=ReadBlobMSBShort(image) % tile_image->colors;
1190 if ((flags & 0x8000) != 0)
1191 j=(size_t) i;
1192 tile_image->colormap[j].red=(Quantum)
1193 ScaleShortToQuantum(ReadBlobMSBShort(image));
1194 tile_image->colormap[j].green=(Quantum)
1195 ScaleShortToQuantum(ReadBlobMSBShort(image));
1196 tile_image->colormap[j].blue=(Quantum)
1197 ScaleShortToQuantum(ReadBlobMSBShort(image));
1198 }
1199 }
1200 else
1201 {
cristybb503372010-05-27 20:51:26 +00001202 for (i=0; i < (ssize_t) tile_image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001203 {
1204 tile_image->colormap[i].red=(Quantum) (QuantumRange-
1205 tile_image->colormap[i].red);
1206 tile_image->colormap[i].green=(Quantum) (QuantumRange-
1207 tile_image->colormap[i].green);
1208 tile_image->colormap[i].blue=(Quantum) (QuantumRange-
1209 tile_image->colormap[i].blue);
1210 }
1211 }
1212 }
Cristy96620812018-03-31 14:30:50 -04001213 if (EOFBlob(image) != MagickFalse)
Cristyf8e684d2018-04-01 07:33:48 -04001214 ThrowPICTException(CorruptImageError,
1215 "InsufficientImageDataInFile");
cristyc944c572014-05-12 00:19:16 +00001216 if (ReadRectangle(image,&source) == MagickFalse)
Cristy18718a92018-03-25 12:45:12 -04001217 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
cristyc944c572014-05-12 00:19:16 +00001218 if (ReadRectangle(image,&destination) == MagickFalse)
Cristy18718a92018-03-25 12:45:12 -04001219 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +00001220 (void) ReadBlobMSBShort(image);
1221 if ((code == 0x91) || (code == 0x99) || (code == 0x9b))
1222 {
1223 /*
1224 Skip region.
1225 */
1226 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001227 for (i=0; i < (ssize_t) (length-2); i++)
cristye11ddbd2015-03-20 14:34:55 +00001228 if (ReadBlobByte(image) == EOF)
1229 break;
cristy3ed852e2009-09-05 21:47:34 +00001230 }
1231 if ((code != 0x9a) && (code != 0x9b) &&
1232 (bytes_per_line & 0x8000) == 0)
Cristy18718a92018-03-25 12:45:12 -04001233 pixels=DecodeImage(image,tile_image,(size_t) bytes_per_line,1,
1234 &extent,exception);
cristy3ed852e2009-09-05 21:47:34 +00001235 else
Cristy18718a92018-03-25 12:45:12 -04001236 pixels=DecodeImage(image,tile_image,(size_t) bytes_per_line,
1237 (unsigned int) pixmap.bits_per_pixel,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +00001238 if (pixels == (unsigned char *) NULL)
Cristy18718a92018-03-25 12:45:12 -04001239 ThrowPICTException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00001240 /*
1241 Convert PICT tile image to pixel packets.
1242 */
1243 p=pixels;
cristybb503372010-05-27 20:51:26 +00001244 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001245 {
1246 if (p > (pixels+extent+image->columns))
Cristy18718a92018-03-25 12:45:12 -04001247 ThrowPICTException(CorruptImageError,"NotEnoughPixelData");
cristy3669d042009-10-16 01:36:31 +00001248 q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,
1249 exception);
cristyacd2ed22011-08-30 01:44:23 +00001250 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001251 break;
cristybb503372010-05-27 20:51:26 +00001252 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001253 {
1254 if (tile_image->storage_class == PseudoClass)
1255 {
Cristy18718a92018-03-25 12:45:12 -04001256 index=(Quantum) ConstrainColormapIndex(tile_image,(ssize_t)
1257 *p,exception);
cristy4c08aed2011-07-01 19:47:50 +00001258 SetPixelIndex(tile_image,index,q);
1259 SetPixelRed(tile_image,
1260 tile_image->colormap[(ssize_t) index].red,q);
1261 SetPixelGreen(tile_image,
1262 tile_image->colormap[(ssize_t) index].green,q);
1263 SetPixelBlue(tile_image,
1264 tile_image->colormap[(ssize_t) index].blue,q);
cristy3ed852e2009-09-05 21:47:34 +00001265 }
1266 else
1267 {
1268 if (pixmap.bits_per_pixel == 16)
1269 {
Cristy18718a92018-03-25 12:45:12 -04001270 i=(ssize_t) (*p++);
1271 j=(size_t) (*p);
cristy4c08aed2011-07-01 19:47:50 +00001272 SetPixelRed(tile_image,ScaleCharToQuantum(
1273 (unsigned char) ((i & 0x7c) << 1)),q);
1274 SetPixelGreen(tile_image,ScaleCharToQuantum(
cristyaff6d802011-04-26 01:46:31 +00001275 (unsigned char) (((i & 0x03) << 6) |
cristy4c08aed2011-07-01 19:47:50 +00001276 ((j & 0xe0) >> 2))),q);
1277 SetPixelBlue(tile_image,ScaleCharToQuantum(
1278 (unsigned char) ((j & 0x1f) << 3)),q);
cristy3ed852e2009-09-05 21:47:34 +00001279 }
1280 else
cristy17f11b02014-12-20 19:37:04 +00001281 if (tile_image->alpha_trait == UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00001282 {
1283 if (p > (pixels+extent+2*image->columns))
Cristy18718a92018-03-25 12:45:12 -04001284 ThrowPICTException(CorruptImageError,
1285 "NotEnoughPixelData");
cristy4c08aed2011-07-01 19:47:50 +00001286 SetPixelRed(tile_image,ScaleCharToQuantum(*p),q);
1287 SetPixelGreen(tile_image,ScaleCharToQuantum(
1288 *(p+tile_image->columns)),q);
1289 SetPixelBlue(tile_image,ScaleCharToQuantum(
1290 *(p+2*tile_image->columns)),q);
cristy3ed852e2009-09-05 21:47:34 +00001291 }
1292 else
1293 {
1294 if (p > (pixels+extent+3*image->columns))
Cristy18718a92018-03-25 12:45:12 -04001295 ThrowPICTException(CorruptImageError,
1296 "NotEnoughPixelData");
cristy4c08aed2011-07-01 19:47:50 +00001297 SetPixelAlpha(tile_image,ScaleCharToQuantum(*p),q);
1298 SetPixelRed(tile_image,ScaleCharToQuantum(
1299 *(p+tile_image->columns)),q);
1300 SetPixelGreen(tile_image,ScaleCharToQuantum(
1301 *(p+2*tile_image->columns)),q);
1302 SetPixelBlue(tile_image,ScaleCharToQuantum(
1303 *(p+3*tile_image->columns)),q);
cristy3ed852e2009-09-05 21:47:34 +00001304 }
1305 }
1306 p++;
cristyed231572011-07-14 02:18:59 +00001307 q+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +00001308 }
1309 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
1310 break;
1311 if ((tile_image->storage_class == DirectClass) &&
1312 (pixmap.bits_per_pixel != 16))
1313 {
1314 p+=(pixmap.component_count-1)*tile_image->columns;
1315 if (p < pixels)
1316 break;
1317 }
Cristy18718a92018-03-25 12:45:12 -04001318 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1319 tile_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001320 if (status == MagickFalse)
1321 break;
1322 }
1323 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
Cristy5e75ae22018-03-20 19:25:44 -04001324 if ((jpeg == MagickFalse) && (EOFBlob(image) == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001325 if ((code == 0x9a) || (code == 0x9b) ||
1326 ((bytes_per_line & 0x8000) != 0))
cristyfeb3e962012-03-29 17:25:55 +00001327 (void) CompositeImage(image,tile_image,CopyCompositeOp,
Cristy18718a92018-03-25 12:45:12 -04001328 MagickTrue,(ssize_t) destination.left,(ssize_t)
1329 destination.top,exception);
cristy3ed852e2009-09-05 21:47:34 +00001330 tile_image=DestroyImage(tile_image);
1331 break;
1332 }
1333 case 0xa1:
1334 {
1335 unsigned char
1336 *info;
1337
cristybb503372010-05-27 20:51:26 +00001338 size_t
cristy3ed852e2009-09-05 21:47:34 +00001339 type;
1340
1341 /*
1342 Comment.
1343 */
1344 type=ReadBlobMSBShort(image);
1345 length=ReadBlobMSBShort(image);
1346 if (length == 0)
1347 break;
1348 (void) ReadBlobMSBLong(image);
Cristy70293c72017-10-01 21:03:00 -04001349 length-=MagickMin(length,4);
cristy3ed852e2009-09-05 21:47:34 +00001350 if (length == 0)
1351 break;
1352 info=(unsigned char *) AcquireQuantumMemory(length,sizeof(*info));
1353 if (info == (unsigned char *) NULL)
1354 break;
1355 count=ReadBlob(image,length,info);
cristy6d62e4b2015-01-11 14:07:17 +00001356 if (count != (ssize_t) length)
rootc94962c2017-09-15 15:19:43 +00001357 {
1358 info=(unsigned char *) RelinquishMagickMemory(info);
Cristy18718a92018-03-25 12:45:12 -04001359 ThrowPICTException(ResourceLimitError,"UnableToReadImageData");
rootc94962c2017-09-15 15:19:43 +00001360 }
cristy3ed852e2009-09-05 21:47:34 +00001361 switch (type)
1362 {
1363 case 0xe0:
1364 {
cristy8723e4b2011-09-01 13:11:19 +00001365 profile=BlobToStringInfo((const void *) NULL,length);
cristy3ed852e2009-09-05 21:47:34 +00001366 SetStringInfoDatum(profile,info);
cristyd15e6592011-10-15 00:13:06 +00001367 status=SetImageProfile(image,"icc",profile,exception);
cristy3ed852e2009-09-05 21:47:34 +00001368 profile=DestroyStringInfo(profile);
1369 if (status == MagickFalse)
Cristya537c722017-04-26 18:14:02 -04001370 {
1371 info=(unsigned char *) RelinquishMagickMemory(info);
Cristy18718a92018-03-25 12:45:12 -04001372 ThrowPICTException(ResourceLimitError,
Cristya537c722017-04-26 18:14:02 -04001373 "MemoryAllocationFailed");
1374 }
cristy3ed852e2009-09-05 21:47:34 +00001375 break;
1376 }
1377 case 0x1f2:
1378 {
cristy8723e4b2011-09-01 13:11:19 +00001379 profile=BlobToStringInfo((const void *) NULL,length);
cristy3ed852e2009-09-05 21:47:34 +00001380 SetStringInfoDatum(profile,info);
cristyd15e6592011-10-15 00:13:06 +00001381 status=SetImageProfile(image,"iptc",profile,exception);
cristy3ed852e2009-09-05 21:47:34 +00001382 if (status == MagickFalse)
Cristya537c722017-04-26 18:14:02 -04001383 {
1384 info=(unsigned char *) RelinquishMagickMemory(info);
Cristy18718a92018-03-25 12:45:12 -04001385 ThrowPICTException(ResourceLimitError,
Cristya537c722017-04-26 18:14:02 -04001386 "MemoryAllocationFailed");
1387 }
cristy3ed852e2009-09-05 21:47:34 +00001388 profile=DestroyStringInfo(profile);
1389 break;
1390 }
1391 default:
1392 break;
1393 }
1394 info=(unsigned char *) RelinquishMagickMemory(info);
1395 break;
1396 }
1397 default:
1398 {
1399 /*
1400 Skip to next op code.
1401 */
1402 if (codes[code].length == -1)
1403 (void) ReadBlobMSBShort(image);
1404 else
cristybb503372010-05-27 20:51:26 +00001405 for (i=0; i < (ssize_t) codes[code].length; i++)
cristye11ddbd2015-03-20 14:34:55 +00001406 if (ReadBlobByte(image) == EOF)
1407 break;
cristy3ed852e2009-09-05 21:47:34 +00001408 }
1409 }
1410 }
1411 if (code == 0xc00)
1412 {
1413 /*
1414 Skip header.
1415 */
1416 for (i=0; i < 24; i++)
cristye11ddbd2015-03-20 14:34:55 +00001417 if (ReadBlobByte(image) == EOF)
1418 break;
cristy3ed852e2009-09-05 21:47:34 +00001419 continue;
1420 }
1421 if (((code >= 0xb0) && (code <= 0xcf)) ||
1422 ((code >= 0x8000) && (code <= 0x80ff)))
1423 continue;
1424 if (code == 0x8200)
1425 {
Cristy68768752018-02-26 20:32:42 -05001426 char
1427 filename[MaxTextExtent];
1428
cristy3ed852e2009-09-05 21:47:34 +00001429 FILE
1430 *file;
1431
cristy3ed852e2009-09-05 21:47:34 +00001432 int
1433 unique_file;
1434
1435 /*
1436 Embedded JPEG.
1437 */
1438 jpeg=MagickTrue;
1439 read_info=CloneImageInfo(image_info);
1440 SetImageInfoBlob(read_info,(void *) NULL,0);
1441 file=(FILE *) NULL;
Cristy68768752018-02-26 20:32:42 -05001442 unique_file=AcquireUniqueFileResource(filename);
1443 (void) FormatLocaleString(read_info->filename,MaxTextExtent,"jpeg:%s",
1444 filename);
cristy3ed852e2009-09-05 21:47:34 +00001445 if (unique_file != -1)
cristyd0305b92009-10-19 13:12:21 +00001446 file=fdopen(unique_file,"wb");
cristy3ed852e2009-09-05 21:47:34 +00001447 if ((unique_file == -1) || (file == (FILE *) NULL))
1448 {
dirkce6b1222014-11-06 21:09:46 +00001449 (void) RelinquishUniqueFileResource(read_info->filename);
cristy3ed852e2009-09-05 21:47:34 +00001450 (void) CopyMagickString(image->filename,read_info->filename,
cristy151b66d2015-04-15 10:50:31 +00001451 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001452 ThrowFileException(exception,FileOpenError,
1453 "UnableToCreateTemporaryFile",image->filename);
1454 image=DestroyImageList(image);
1455 return((Image *) NULL);
1456 }
1457 length=ReadBlobMSBLong(image);
cristye11ddbd2015-03-20 14:34:55 +00001458 if (length > 154)
cristyc944c572014-05-12 00:19:16 +00001459 {
cristye11ddbd2015-03-20 14:34:55 +00001460 for (i=0; i < 6; i++)
1461 (void) ReadBlobMSBLong(image);
1462 if (ReadRectangle(image,&frame) == MagickFalse)
1463 {
1464 (void) fclose(file);
1465 (void) RelinquishUniqueFileResource(read_info->filename);
Cristy18718a92018-03-25 12:45:12 -04001466 ThrowPICTException(CorruptImageError,"ImproperImageHeader");
cristye11ddbd2015-03-20 14:34:55 +00001467 }
1468 for (i=0; i < 122; i++)
1469 if (ReadBlobByte(image) == EOF)
1470 break;
1471 for (i=0; i < (ssize_t) (length-154); i++)
1472 {
1473 c=ReadBlobByte(image);
1474 if (c == EOF)
1475 break;
1476 (void) fputc(c,file);
1477 }
cristyc944c572014-05-12 00:19:16 +00001478 }
cristy3ed852e2009-09-05 21:47:34 +00001479 (void) fclose(file);
cristy100a0562014-04-18 01:27:37 +00001480 (void) close(unique_file);
cristy3ed852e2009-09-05 21:47:34 +00001481 tile_image=ReadImage(read_info,exception);
Cristy68768752018-02-26 20:32:42 -05001482 (void) RelinquishUniqueFileResource(filename);
cristy3ed852e2009-09-05 21:47:34 +00001483 read_info=DestroyImageInfo(read_info);
1484 if (tile_image == (Image *) NULL)
1485 continue;
cristy151b66d2015-04-15 10:50:31 +00001486 (void) FormatLocaleString(geometry,MagickPathExtent,"%.20gx%.20g",
cristye8c25f92010-06-03 00:53:06 +00001487 (double) MagickMax(image->columns,tile_image->columns),
1488 (double) MagickMax(image->rows,tile_image->rows));
cristya0511732010-02-17 02:38:35 +00001489 (void) SetImageExtent(image,
1490 MagickMax(image->columns,tile_image->columns),
cristy63240882011-08-05 19:05:27 +00001491 MagickMax(image->rows,tile_image->rows),exception);
cristyaf8d3912014-02-21 14:50:33 +00001492 (void) TransformImageColorspace(image,tile_image->colorspace,exception);
cristy39172402012-03-30 13:04:39 +00001493 (void) CompositeImage(image,tile_image,CopyCompositeOp,MagickTrue,
Cristy18718a92018-03-25 12:45:12 -04001494 (ssize_t) frame.left,(ssize_t) frame.right,exception);
cristy3ed852e2009-09-05 21:47:34 +00001495 image->compression=tile_image->compression;
1496 tile_image=DestroyImage(tile_image);
1497 continue;
1498 }
1499 if ((code == 0xff) || (code == 0xffff))
1500 break;
1501 if (((code >= 0xd0) && (code <= 0xfe)) ||
1502 ((code >= 0x8100) && (code <= 0xffff)))
1503 {
1504 /*
1505 Skip reserved.
1506 */
1507 length=ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001508 for (i=0; i < (ssize_t) length; i++)
cristye11ddbd2015-03-20 14:34:55 +00001509 if (ReadBlobByte(image) == EOF)
1510 break;
cristy3ed852e2009-09-05 21:47:34 +00001511 continue;
1512 }
1513 if ((code >= 0x100) && (code <= 0x7fff))
1514 {
1515 /*
1516 Skip reserved.
1517 */
1518 length=(size_t) ((code >> 7) & 0xff);
cristybb503372010-05-27 20:51:26 +00001519 for (i=0; i < (ssize_t) length; i++)
cristye11ddbd2015-03-20 14:34:55 +00001520 if (ReadBlobByte(image) == EOF)
1521 break;
cristy3ed852e2009-09-05 21:47:34 +00001522 continue;
1523 }
1524 }
1525 (void) CloseBlob(image);
1526 return(GetFirstImageInList(image));
1527}
1528#endif
1529
1530/*
1531%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1532% %
1533% %
1534% %
1535% R e g i s t e r P I C T I m a g e %
1536% %
1537% %
1538% %
1539%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1540%
1541% RegisterPICTImage() adds attributes for the PICT image format to
1542% the list of supported formats. The attributes include the image format
1543% tag, a method to read and/or write the format, whether the format
1544% supports the saving of more than one frame to the same file or blob,
1545% whether the format supports native in-memory I/O, and a brief
1546% description of the format.
1547%
1548% The format of the RegisterPICTImage method is:
1549%
cristybb503372010-05-27 20:51:26 +00001550% size_t RegisterPICTImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001551%
1552*/
cristybb503372010-05-27 20:51:26 +00001553ModuleExport size_t RegisterPICTImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001554{
1555 MagickInfo
1556 *entry;
1557
dirk06b627a2015-04-06 18:59:17 +00001558 entry=AcquireMagickInfo("PICT","PCT","Apple Macintosh QuickDraw/PICT");
cristy3ed852e2009-09-05 21:47:34 +00001559 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1560 entry->encoder=(EncodeImageHandler *) WritePICTImage;
dirk08e9a112015-02-22 01:51:41 +00001561 entry->flags^=CoderAdjoinFlag;
Dirk Lemstra8974d772017-01-22 03:19:27 +01001562 entry->flags|=CoderEncoderSeekableStreamFlag;
cristy3ed852e2009-09-05 21:47:34 +00001563 entry->magick=(IsImageFormatHandler *) IsPICT;
cristy3ed852e2009-09-05 21:47:34 +00001564 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +00001565 entry=AcquireMagickInfo("PICT","PICT","Apple Macintosh QuickDraw/PICT");
cristy3ed852e2009-09-05 21:47:34 +00001566 entry->decoder=(DecodeImageHandler *) ReadPICTImage;
1567 entry->encoder=(EncodeImageHandler *) WritePICTImage;
dirk08e9a112015-02-22 01:51:41 +00001568 entry->flags^=CoderAdjoinFlag;
Dirk Lemstra8974d772017-01-22 03:19:27 +01001569 entry->flags|=CoderEncoderSeekableStreamFlag;
cristy3ed852e2009-09-05 21:47:34 +00001570 entry->magick=(IsImageFormatHandler *) IsPICT;
cristy3ed852e2009-09-05 21:47:34 +00001571 (void) RegisterMagickInfo(entry);
1572 return(MagickImageCoderSignature);
1573}
1574
1575/*
1576%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1577% %
1578% %
1579% %
1580% U n r e g i s t e r P I C T I m a g e %
1581% %
1582% %
1583% %
1584%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1585%
1586% UnregisterPICTImage() removes format registrations made by the
1587% PICT module from the list of supported formats.
1588%
1589% The format of the UnregisterPICTImage method is:
1590%
1591% UnregisterPICTImage(void)
1592%
1593*/
1594ModuleExport void UnregisterPICTImage(void)
1595{
1596 (void) UnregisterMagickInfo("PCT");
1597 (void) UnregisterMagickInfo("PICT");
1598}
1599
1600/*
1601%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1602% %
1603% %
1604% %
1605% W r i t e P I C T I m a g e %
1606% %
1607% %
1608% %
1609%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1610%
1611% WritePICTImage() writes an image to a file in the Apple Macintosh
1612% QuickDraw/PICT image format.
1613%
1614% The format of the WritePICTImage method is:
1615%
cristy3669d042009-10-16 01:36:31 +00001616% MagickBooleanType WritePICTImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +00001617% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001618%
1619% A description of each parameter follows.
1620%
1621% o image_info: the image info.
1622%
1623% o image: The image.
1624%
cristy1e178e72011-08-28 19:44:34 +00001625% o exception: return any errors or warnings in this structure.
1626%
cristy3ed852e2009-09-05 21:47:34 +00001627*/
1628static MagickBooleanType WritePICTImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +00001629 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001630{
1631#define MaxCount 128
1632#define PictCropRegionOp 0x01
1633#define PictEndOfPictureOp 0xff
1634#define PictJPEGOp 0x8200
1635#define PictInfoOp 0x0C00
1636#define PictInfoSize 512
1637#define PictPixmapOp 0x9A
1638#define PictPICTOp 0x98
1639#define PictVersion 0x11
1640
1641 const StringInfo
1642 *profile;
1643
1644 double
1645 x_resolution,
1646 y_resolution;
1647
cristy3ed852e2009-09-05 21:47:34 +00001648 MagickBooleanType
1649 status;
1650
1651 MagickOffsetType
1652 offset;
1653
1654 PICTPixmap
1655 pixmap;
1656
1657 PICTRectangle
1658 bounds,
1659 crop_rectangle,
1660 destination_rectangle,
1661 frame_rectangle,
1662 size_rectangle,
1663 source_rectangle;
1664
cristy4c08aed2011-07-01 19:47:50 +00001665 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001666 *p;
1667
cristybb503372010-05-27 20:51:26 +00001668 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001669 i,
1670 x;
1671
1672 size_t
cristyaff6d802011-04-26 01:46:31 +00001673 bytes_per_line,
1674 count,
cristy0f6fc2d2015-05-30 00:49:11 +00001675 row_bytes,
cristyaff6d802011-04-26 01:46:31 +00001676 storage_class;
1677
1678 ssize_t
1679 y;
cristy3ed852e2009-09-05 21:47:34 +00001680
1681 unsigned char
1682 *buffer,
1683 *packed_scanline,
1684 *scanline;
1685
cristy3ed852e2009-09-05 21:47:34 +00001686 unsigned short
1687 base_address,
cristy3ed852e2009-09-05 21:47:34 +00001688 transfer_mode;
1689
1690 /*
1691 Open output image file.
1692 */
1693 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001694 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001695 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001696 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001697 if (image->debug != MagickFalse)
1698 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1699 if ((image->columns > 65535L) || (image->rows > 65535L))
1700 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
cristy3a37efd2011-08-28 20:31:03 +00001701 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001702 assert(exception->signature == MagickCoreSignature);
cristy1e178e72011-08-28 19:44:34 +00001703 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001704 if (status == MagickFalse)
1705 return(status);
cristyaf8d3912014-02-21 14:50:33 +00001706 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001707 /*
1708 Initialize image info.
1709 */
1710 size_rectangle.top=0;
1711 size_rectangle.left=0;
1712 size_rectangle.bottom=(short) image->rows;
1713 size_rectangle.right=(short) image->columns;
1714 frame_rectangle=size_rectangle;
1715 crop_rectangle=size_rectangle;
1716 source_rectangle=size_rectangle;
1717 destination_rectangle=size_rectangle;
1718 base_address=0xff;
cristy0f6fc2d2015-05-30 00:49:11 +00001719 row_bytes=image->columns;
cristy3ed852e2009-09-05 21:47:34 +00001720 bounds.top=0;
1721 bounds.left=0;
1722 bounds.bottom=(short) image->rows;
1723 bounds.right=(short) image->columns;
1724 pixmap.version=0;
1725 pixmap.pack_type=0;
1726 pixmap.pack_size=0;
1727 pixmap.pixel_type=0;
1728 pixmap.bits_per_pixel=8;
1729 pixmap.component_count=1;
1730 pixmap.component_size=8;
1731 pixmap.plane_bytes=0;
1732 pixmap.table=0;
1733 pixmap.reserved=0;
1734 transfer_mode=0;
cristy2a11bef2011-10-28 18:33:11 +00001735 x_resolution=image->resolution.x != 0.0 ? image->resolution.x :
cristy3ed852e2009-09-05 21:47:34 +00001736 DefaultResolution;
cristy2a11bef2011-10-28 18:33:11 +00001737 y_resolution=image->resolution.y != 0.0 ? image->resolution.y :
cristy3ed852e2009-09-05 21:47:34 +00001738 DefaultResolution;
1739 storage_class=image->storage_class;
1740 if (image_info->compression == JPEGCompression)
1741 storage_class=DirectClass;
cristy213770a2014-09-27 00:12:23 +00001742 if (storage_class == DirectClass)
cristy3ed852e2009-09-05 21:47:34 +00001743 {
cristy17f11b02014-12-20 19:37:04 +00001744 pixmap.component_count=image->alpha_trait != UndefinedPixelTrait ? 4 : 3;
cristy3ed852e2009-09-05 21:47:34 +00001745 pixmap.pixel_type=16;
1746 pixmap.bits_per_pixel=32;
1747 pixmap.pack_type=0x04;
1748 transfer_mode=0x40;
cristy0f6fc2d2015-05-30 00:49:11 +00001749 row_bytes=4*image->columns;
cristy3ed852e2009-09-05 21:47:34 +00001750 }
1751 /*
1752 Allocate memory.
1753 */
1754 bytes_per_line=image->columns;
cristy213770a2014-09-27 00:12:23 +00001755 if (storage_class == DirectClass)
cristy17f11b02014-12-20 19:37:04 +00001756 bytes_per_line*=image->alpha_trait != UndefinedPixelTrait ? 4 : 3;
cristy3ed852e2009-09-05 21:47:34 +00001757 buffer=(unsigned char *) AcquireQuantumMemory(PictInfoSize,sizeof(*buffer));
1758 packed_scanline=(unsigned char *) AcquireQuantumMemory((size_t)
1759 (row_bytes+MaxCount),sizeof(*packed_scanline));
1760 scanline=(unsigned char *) AcquireQuantumMemory(row_bytes,sizeof(*scanline));
1761 if ((buffer == (unsigned char *) NULL) ||
1762 (packed_scanline == (unsigned char *) NULL) ||
1763 (scanline == (unsigned char *) NULL))
Cristyc1b09bb2017-07-17 19:20:45 -04001764 {
1765 if (scanline != (unsigned char *) NULL)
1766 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
1767 if (packed_scanline != (unsigned char *) NULL)
1768 packed_scanline=(unsigned char *) RelinquishMagickMemory(
1769 packed_scanline);
1770 if (buffer != (unsigned char *) NULL)
1771 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1772 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1773 }
Cristy81bfff22018-03-10 07:58:31 -05001774 (void) memset(scanline,0,row_bytes);
1775 (void) memset(packed_scanline,0,(size_t) (row_bytes+MaxCount));
cristy3ed852e2009-09-05 21:47:34 +00001776 /*
1777 Write header, header size, size bounding box, version, and reserved.
1778 */
Cristy81bfff22018-03-10 07:58:31 -05001779 (void) memset(buffer,0,PictInfoSize);
cristy3ed852e2009-09-05 21:47:34 +00001780 (void) WriteBlob(image,PictInfoSize,buffer);
1781 (void) WriteBlobMSBShort(image,0);
1782 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.top);
1783 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.left);
1784 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.bottom);
1785 (void) WriteBlobMSBShort(image,(unsigned short) size_rectangle.right);
1786 (void) WriteBlobMSBShort(image,PictVersion);
1787 (void) WriteBlobMSBShort(image,0x02ff); /* version #2 */
1788 (void) WriteBlobMSBShort(image,PictInfoOp);
Cristy18718a92018-03-25 12:45:12 -04001789 (void) WriteBlobMSBLong(image,0xFFFE0000U);
cristy3ed852e2009-09-05 21:47:34 +00001790 /*
1791 Write full size of the file, resolution, frame bounding box, and reserved.
1792 */
1793 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1794 (void) WriteBlobMSBShort(image,0x0000);
1795 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
1796 (void) WriteBlobMSBShort(image,0x0000);
1797 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.top);
1798 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.left);
1799 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.bottom);
1800 (void) WriteBlobMSBShort(image,(unsigned short) frame_rectangle.right);
1801 (void) WriteBlobMSBLong(image,0x00000000L);
1802 profile=GetImageProfile(image,"iptc");
1803 if (profile != (StringInfo *) NULL)
1804 {
1805 (void) WriteBlobMSBShort(image,0xa1);
1806 (void) WriteBlobMSBShort(image,0x1f2);
cristyed552522009-10-16 14:04:35 +00001807 (void) WriteBlobMSBShort(image,(unsigned short)
1808 (GetStringInfoLength(profile)+4));
cristy3ed852e2009-09-05 21:47:34 +00001809 (void) WriteBlobString(image,"8BIM");
cristyed552522009-10-16 14:04:35 +00001810 (void) WriteBlob(image,GetStringInfoLength(profile),
1811 GetStringInfoDatum(profile));
cristy3ed852e2009-09-05 21:47:34 +00001812 }
1813 profile=GetImageProfile(image,"icc");
1814 if (profile != (StringInfo *) NULL)
1815 {
1816 (void) WriteBlobMSBShort(image,0xa1);
1817 (void) WriteBlobMSBShort(image,0xe0);
cristyed552522009-10-16 14:04:35 +00001818 (void) WriteBlobMSBShort(image,(unsigned short)
1819 (GetStringInfoLength(profile)+4));
Cristy18718a92018-03-25 12:45:12 -04001820 (void) WriteBlobMSBLong(image,0x00000000U);
cristyed552522009-10-16 14:04:35 +00001821 (void) WriteBlob(image,GetStringInfoLength(profile),
1822 GetStringInfoDatum(profile));
cristy3ed852e2009-09-05 21:47:34 +00001823 (void) WriteBlobMSBShort(image,0xa1);
1824 (void) WriteBlobMSBShort(image,0xe0);
1825 (void) WriteBlobMSBShort(image,4);
Cristy18718a92018-03-25 12:45:12 -04001826 (void) WriteBlobMSBLong(image,0x00000002U);
cristy3ed852e2009-09-05 21:47:34 +00001827 }
1828 /*
1829 Write crop region opcode and crop bounding box.
1830 */
1831 (void) WriteBlobMSBShort(image,PictCropRegionOp);
1832 (void) WriteBlobMSBShort(image,0xa);
1833 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.top);
1834 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.left);
1835 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.bottom);
1836 (void) WriteBlobMSBShort(image,(unsigned short) crop_rectangle.right);
1837 if (image_info->compression == JPEGCompression)
1838 {
1839 Image
1840 *jpeg_image;
1841
1842 ImageInfo
1843 *jpeg_info;
1844
1845 size_t
1846 length;
1847
1848 unsigned char
1849 *blob;
1850
cristy1e178e72011-08-28 19:44:34 +00001851 jpeg_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +00001852 if (jpeg_image == (Image *) NULL)
1853 {
1854 (void) CloseBlob(image);
1855 return(MagickFalse);
1856 }
1857 jpeg_info=CloneImageInfo(image_info);
cristy151b66d2015-04-15 10:50:31 +00001858 (void) CopyMagickString(jpeg_info->magick,"JPEG",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001859 length=0;
1860 blob=(unsigned char *) ImageToBlob(jpeg_info,jpeg_image,&length,
cristy1e178e72011-08-28 19:44:34 +00001861 exception);
cristy3ed852e2009-09-05 21:47:34 +00001862 jpeg_info=DestroyImageInfo(jpeg_info);
1863 if (blob == (unsigned char *) NULL)
1864 return(MagickFalse);
1865 jpeg_image=DestroyImage(jpeg_image);
1866 (void) WriteBlobMSBShort(image,PictJPEGOp);
cristyeaedf062010-05-29 22:36:02 +00001867 (void) WriteBlobMSBLong(image,(unsigned int) length+154);
cristy3ed852e2009-09-05 21:47:34 +00001868 (void) WriteBlobMSBShort(image,0x0000);
Cristy18718a92018-03-25 12:45:12 -04001869 (void) WriteBlobMSBLong(image,0x00010000U);
1870 (void) WriteBlobMSBLong(image,0x00000000U);
1871 (void) WriteBlobMSBLong(image,0x00000000U);
1872 (void) WriteBlobMSBLong(image,0x00000000U);
1873 (void) WriteBlobMSBLong(image,0x00010000U);
1874 (void) WriteBlobMSBLong(image,0x00000000U);
1875 (void) WriteBlobMSBLong(image,0x00000000U);
1876 (void) WriteBlobMSBLong(image,0x00000000U);
1877 (void) WriteBlobMSBLong(image,0x40000000U);
1878 (void) WriteBlobMSBLong(image,0x00000000U);
1879 (void) WriteBlobMSBLong(image,0x00000000U);
1880 (void) WriteBlobMSBLong(image,0x00000000U);
1881 (void) WriteBlobMSBLong(image,0x00400000U);
cristy3ed852e2009-09-05 21:47:34 +00001882 (void) WriteBlobMSBShort(image,0x0000);
1883 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1884 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1885 (void) WriteBlobMSBShort(image,0x0000);
1886 (void) WriteBlobMSBShort(image,768);
1887 (void) WriteBlobMSBShort(image,0x0000);
Cristy18718a92018-03-25 12:45:12 -04001888 (void) WriteBlobMSBLong(image,0x00000000U);
1889 (void) WriteBlobMSBLong(image,0x00566A70U);
1890 (void) WriteBlobMSBLong(image,0x65670000U);
1891 (void) WriteBlobMSBLong(image,0x00000000U);
1892 (void) WriteBlobMSBLong(image,0x00000001U);
1893 (void) WriteBlobMSBLong(image,0x00016170U);
1894 (void) WriteBlobMSBLong(image,0x706C0000U);
1895 (void) WriteBlobMSBLong(image,0x00000000U);
cristy3ed852e2009-09-05 21:47:34 +00001896 (void) WriteBlobMSBShort(image,768);
1897 (void) WriteBlobMSBShort(image,(unsigned short) image->columns);
1898 (void) WriteBlobMSBShort(image,(unsigned short) image->rows);
1899 (void) WriteBlobMSBShort(image,(unsigned short) x_resolution);
1900 (void) WriteBlobMSBShort(image,0x0000);
1901 (void) WriteBlobMSBShort(image,(unsigned short) y_resolution);
Cristy18718a92018-03-25 12:45:12 -04001902 (void) WriteBlobMSBLong(image,0x00000000U);
1903 (void) WriteBlobMSBLong(image,0x87AC0001U);
1904 (void) WriteBlobMSBLong(image,0x0B466F74U);
1905 (void) WriteBlobMSBLong(image,0x6F202D20U);
1906 (void) WriteBlobMSBLong(image,0x4A504547U);
1907 (void) WriteBlobMSBLong(image,0x00000000U);
1908 (void) WriteBlobMSBLong(image,0x00000000U);
1909 (void) WriteBlobMSBLong(image,0x00000000U);
1910 (void) WriteBlobMSBLong(image,0x00000000U);
1911 (void) WriteBlobMSBLong(image,0x00000000U);
1912 (void) WriteBlobMSBLong(image,0x0018FFFFU);
cristy3ed852e2009-09-05 21:47:34 +00001913 (void) WriteBlob(image,length,blob);
1914 if ((length & 0x01) != 0)
1915 (void) WriteBlobByte(image,'\0');
1916 blob=(unsigned char *) RelinquishMagickMemory(blob);
1917 }
1918 /*
1919 Write picture opcode, row bytes, and picture bounding box, and version.
1920 */
1921 if (storage_class == PseudoClass)
1922 (void) WriteBlobMSBShort(image,PictPICTOp);
1923 else
1924 {
1925 (void) WriteBlobMSBShort(image,PictPixmapOp);
Cristy18718a92018-03-25 12:45:12 -04001926 (void) WriteBlobMSBLong(image,(unsigned int) base_address);
cristy3ed852e2009-09-05 21:47:34 +00001927 }
1928 (void) WriteBlobMSBShort(image,(unsigned short) (row_bytes | 0x8000));
1929 (void) WriteBlobMSBShort(image,(unsigned short) bounds.top);
1930 (void) WriteBlobMSBShort(image,(unsigned short) bounds.left);
1931 (void) WriteBlobMSBShort(image,(unsigned short) bounds.bottom);
1932 (void) WriteBlobMSBShort(image,(unsigned short) bounds.right);
1933 /*
1934 Write pack type, pack size, resolution, pixel type, and pixel size.
1935 */
1936 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.version);
1937 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pack_type);
cristyeaedf062010-05-29 22:36:02 +00001938 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.pack_size);
cristy3ed852e2009-09-05 21:47:34 +00001939 (void) WriteBlobMSBShort(image,(unsigned short) (x_resolution+0.5));
1940 (void) WriteBlobMSBShort(image,0x0000);
1941 (void) WriteBlobMSBShort(image,(unsigned short) (y_resolution+0.5));
1942 (void) WriteBlobMSBShort(image,0x0000);
1943 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.pixel_type);
1944 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.bits_per_pixel);
1945 /*
1946 Write component count, size, plane bytes, table size, and reserved.
1947 */
1948 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_count);
1949 (void) WriteBlobMSBShort(image,(unsigned short) pixmap.component_size);
cristyeaedf062010-05-29 22:36:02 +00001950 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.plane_bytes);
1951 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.table);
1952 (void) WriteBlobMSBLong(image,(unsigned int) pixmap.reserved);
cristy3ed852e2009-09-05 21:47:34 +00001953 if (storage_class == PseudoClass)
1954 {
1955 /*
1956 Write image colormap.
1957 */
1958 (void) WriteBlobMSBLong(image,0x00000000L); /* color seed */
1959 (void) WriteBlobMSBShort(image,0L); /* color flags */
1960 (void) WriteBlobMSBShort(image,(unsigned short) (image->colors-1));
cristybb503372010-05-27 20:51:26 +00001961 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001962 {
1963 (void) WriteBlobMSBShort(image,(unsigned short) i);
1964 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1965 image->colormap[i].red));
1966 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1967 image->colormap[i].green));
1968 (void) WriteBlobMSBShort(image,ScaleQuantumToShort(
1969 image->colormap[i].blue));
1970 }
1971 }
1972 /*
1973 Write source and destination rectangle.
1974 */
1975 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.top);
1976 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.left);
1977 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.bottom);
1978 (void) WriteBlobMSBShort(image,(unsigned short) source_rectangle.right);
1979 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.top);
1980 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.left);
1981 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.bottom);
1982 (void) WriteBlobMSBShort(image,(unsigned short) destination_rectangle.right);
1983 (void) WriteBlobMSBShort(image,(unsigned short) transfer_mode);
1984 /*
1985 Write picture data.
1986 */
1987 count=0;
cristy213770a2014-09-27 00:12:23 +00001988 if (storage_class == PseudoClass)
cristybb503372010-05-27 20:51:26 +00001989 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001990 {
cristy1e178e72011-08-28 19:44:34 +00001991 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001992 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001993 break;
cristybb503372010-05-27 20:51:26 +00001994 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001995 {
1996 scanline[x]=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001997 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001998 }
cristybb503372010-05-27 20:51:26 +00001999 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
cristy3ed852e2009-09-05 21:47:34 +00002000 packed_scanline);
cristy8b27a6d2010-02-14 03:31:15 +00002001 if (image->previous == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002002 {
cristycee97112010-05-28 00:44:52 +00002003 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyaff6d802011-04-26 01:46:31 +00002004 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002005 if (status == MagickFalse)
2006 break;
2007 }
2008 }
2009 else
2010 if (image_info->compression == JPEGCompression)
2011 {
Cristy81bfff22018-03-10 07:58:31 -05002012 (void) memset(scanline,0,row_bytes);
cristybb503372010-05-27 20:51:26 +00002013 for (y=0; y < (ssize_t) image->rows; y++)
cristyaff6d802011-04-26 01:46:31 +00002014 count+=EncodeImage(image,scanline,(size_t) (row_bytes & 0x7FFF),
2015 packed_scanline);
cristy3ed852e2009-09-05 21:47:34 +00002016 }
2017 else
2018 {
2019 register unsigned char
2020 *blue,
2021 *green,
2022 *opacity,
2023 *red;
2024
2025 red=scanline;
2026 green=scanline+image->columns;
2027 blue=scanline+2*image->columns;
2028 opacity=scanline+3*image->columns;
cristybb503372010-05-27 20:51:26 +00002029 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002030 {
cristy1e178e72011-08-28 19:44:34 +00002031 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002032 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002033 break;
2034 red=scanline;
2035 green=scanline+image->columns;
2036 blue=scanline+2*image->columns;
cristy17f11b02014-12-20 19:37:04 +00002037 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00002038 {
2039 opacity=scanline;
2040 red=scanline+image->columns;
2041 green=scanline+2*image->columns;
2042 blue=scanline+3*image->columns;
2043 }
cristybb503372010-05-27 20:51:26 +00002044 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002045 {
cristy4c08aed2011-07-01 19:47:50 +00002046 *red++=ScaleQuantumToChar(GetPixelRed(image,p));
2047 *green++=ScaleQuantumToChar(GetPixelGreen(image,p));
2048 *blue++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristy17f11b02014-12-20 19:37:04 +00002049 if (image->alpha_trait != UndefinedPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +00002050 *opacity++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
cristyed231572011-07-14 02:18:59 +00002051 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002052 }
Cristy21744842016-01-13 18:31:21 -05002053 count+=EncodeImage(image,scanline,bytes_per_line,packed_scanline);
cristy8b27a6d2010-02-14 03:31:15 +00002054 if (image->previous == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002055 {
cristycee97112010-05-28 00:44:52 +00002056 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2057 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002058 if (status == MagickFalse)
2059 break;
2060 }
2061 }
2062 }
2063 if ((count & 0x01) != 0)
2064 (void) WriteBlobByte(image,'\0');
2065 (void) WriteBlobMSBShort(image,PictEndOfPictureOp);
2066 offset=TellBlob(image);
2067 offset=SeekBlob(image,512,SEEK_SET);
2068 (void) WriteBlobMSBShort(image,(unsigned short) offset);
2069 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
2070 packed_scanline=(unsigned char *) RelinquishMagickMemory(packed_scanline);
2071 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2072 (void) CloseBlob(image);
2073 return(MagickTrue);
2074}