blob: 54dd96771f9ed6d6ca2bcd7db0c4b721f726d590 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% PPPP SSSSS DDDD %
7% P P SS D D %
8% PPPP SSS D D %
9% P SS D D %
10% P SSSSS DDDD %
11% %
12% %
13% Read/Write Adobe Photoshop Image Format %
14% %
15% Software Design %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% Leonard Rosenthol %
18% July 1992 %
dirkb41f0802013-12-26 16:47:58 +000019% Dirk Lemstra %
20% December 2013 %
cristy3ed852e2009-09-05 21:47:34 +000021% %
22% %
cristyb56bb242014-11-25 17:12:48 +000023% Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000024% dedicated to making software imaging solutions freely available. %
25% %
26% You may not use this file except in compliance with the License. You may %
27% obtain a copy of the License at %
28% %
29% http://www.imagemagick.org/script/license.php %
30% %
31% Unless required by applicable law or agreed to in writing, software %
32% distributed under the License is distributed on an "AS IS" BASIS, %
33% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
34% See the License for the specific language governing permissions and %
35% limitations under the License. %
36% %
37%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38%
39%
40*/
41
42/*
43 Include declarations.
44*/
cristy4c08aed2011-07-01 19:47:50 +000045#include "MagickCore/studio.h"
46#include "MagickCore/artifact.h"
47#include "MagickCore/attribute.h"
48#include "MagickCore/blob.h"
49#include "MagickCore/blob-private.h"
50#include "MagickCore/cache.h"
dirk18c0e4d2014-02-22 22:24:05 +000051#include "MagickCore/channel.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/colormap.h"
cristyf432c632014-12-07 15:11:28 +000053#include "MagickCore/colormap-private.h"
cristy4c08aed2011-07-01 19:47:50 +000054#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000055#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000056#include "MagickCore/constitute.h"
57#include "MagickCore/enhance.h"
58#include "MagickCore/exception.h"
59#include "MagickCore/exception-private.h"
60#include "MagickCore/image.h"
61#include "MagickCore/image-private.h"
62#include "MagickCore/list.h"
63#include "MagickCore/log.h"
64#include "MagickCore/magick.h"
65#include "MagickCore/memory_.h"
66#include "MagickCore/module.h"
67#include "MagickCore/monitor-private.h"
68#include "MagickCore/pixel.h"
69#include "MagickCore/pixel-accessor.h"
70#include "MagickCore/profile.h"
71#include "MagickCore/property.h"
72#include "MagickCore/quantum-private.h"
73#include "MagickCore/static.h"
74#include "MagickCore/string_.h"
dirkb41f0802013-12-26 16:47:58 +000075#ifdef MAGICKCORE_ZLIB_DELEGATE
76#include <zlib.h>
77#endif
cristy3ed852e2009-09-05 21:47:34 +000078
79/*
cristy2d3d87f2010-03-01 00:23:08 +000080 Define declaractions.
81*/
cristyaca3ec52010-03-02 14:55:01 +000082#define MaxPSDChannels 56
cristybb503372010-05-27 20:51:26 +000083#define PSDQuantum(x) (((ssize_t) (x)+1) & -2)
cristy2d3d87f2010-03-01 00:23:08 +000084
85/*
86 Enumerated declaractions.
87*/
88typedef enum
89{
dirkb41f0802013-12-26 16:47:58 +000090 Raw = 0,
91 RLE = 1,
92 ZipWithoutPrediction = 2,
93 ZipWithPrediction = 3
94} PSDCompressionType;
95
96typedef enum
97{
cristy2d3d87f2010-03-01 00:23:08 +000098 BitmapMode = 0,
99 GrayscaleMode = 1,
100 IndexedMode = 2,
101 RGBMode = 3,
102 CMYKMode = 4,
103 MultichannelMode = 7,
104 DuotoneMode = 8,
105 LabMode = 9
106} PSDImageType;
107
108/*
109 Typedef declaractions.
110*/
111typedef struct _ChannelInfo
112{
113 short int
114 type;
115
cristybb503372010-05-27 20:51:26 +0000116 size_t
cristy2d3d87f2010-03-01 00:23:08 +0000117 size;
118} ChannelInfo;
119
120typedef struct _LayerInfo
121{
122 RectangleInfo
123 page,
124 mask;
125
126 unsigned short
127 channels;
128
129 ChannelInfo
130 channel_info[MaxPSDChannels];
131
132 char
133 blendkey[4];
134
135 Quantum
136 opacity;
137
138 unsigned char
139 clipping,
140 visible,
141 flags;
142
cristybb503372010-05-27 20:51:26 +0000143 size_t
cristy2d3d87f2010-03-01 00:23:08 +0000144 offset_x,
145 offset_y;
146
147 unsigned char
148 name[256];
149
150 Image
151 *image;
152} LayerInfo;
153
154typedef struct _PSDInfo
155{
156 char
157 signature[4];
158
159 unsigned short
160 channels,
161 version;
162
163 unsigned char
164 reserved[6];
165
cristybb503372010-05-27 20:51:26 +0000166 size_t
cristy2d3d87f2010-03-01 00:23:08 +0000167 rows,
168 columns;
169
170 unsigned short
171 depth,
172 mode;
173} PSDInfo;
174
175/*
cristy3ed852e2009-09-05 21:47:34 +0000176 Forward declarations.
177*/
178static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +0000179 WritePSDImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000180
181/*
182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183% %
184% %
185% %
cristy3ed852e2009-09-05 21:47:34 +0000186% I s P S D %
187% %
188% %
189% %
190%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191%
192% IsPSD()() returns MagickTrue if the image format type, identified by the
193% magick string, is PSD.
194%
195% The format of the IsPSD method is:
196%
197% MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
198%
199% A description of each parameter follows:
200%
201% o magick: compare image format pattern against these bytes.
202%
203% o length: Specifies the length of the magick string.
204%
205*/
206static MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
207{
208 if (length < 4)
209 return(MagickFalse);
210 if (LocaleNCompare((const char *) magick,"8BPS",4) == 0)
211 return(MagickTrue);
212 return(MagickFalse);
213}
214
215/*
216%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217% %
218% %
219% %
220% R e a d P S D I m a g e %
221% %
222% %
223% %
224%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225%
226% ReadPSDImage() reads an Adobe Photoshop image file and returns it. It
227% allocates the memory necessary for the new Image structure and returns a
228% pointer to the new image.
229%
230% The format of the ReadPSDImage method is:
231%
cristy90de83d2015-04-08 22:04:41 +0000232% Image *ReadPSDImage(image_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000233%
234% A description of each parameter follows:
235%
236% o image_info: the image info.
237%
238% o exception: return any errors or warnings in this structure.
239%
240*/
241
cristy19eb6412010-04-23 14:42:29 +0000242static const char *CompositeOperatorToPSDBlendMode(CompositeOperator op)
cristycd081772010-03-22 17:19:12 +0000243{
244 const char
245 *blend_mode;
246
cristy19eb6412010-04-23 14:42:29 +0000247 switch (op)
cristycd081772010-03-22 17:19:12 +0000248 {
cristyfdc035a2014-06-26 11:29:29 +0000249 case ColorBurnCompositeOp: blend_mode = "idiv"; break;
250 case ColorDodgeCompositeOp: blend_mode = "div "; break;
cristy216b90f2014-04-12 22:59:20 +0000251 case ColorizeCompositeOp: blend_mode = "colr"; break;
252 case DarkenCompositeOp: blend_mode = "dark"; break;
253 case DifferenceCompositeOp: blend_mode = "diff"; break;
254 case DissolveCompositeOp: blend_mode = "diss"; break;
cristyfdc035a2014-06-26 11:29:29 +0000255 case ExclusionCompositeOp: blend_mode = "smud"; break;
cristy216b90f2014-04-12 22:59:20 +0000256 case HardLightCompositeOp: blend_mode = "hLit"; break;
cristyfdc035a2014-06-26 11:29:29 +0000257 case HardMixCompositeOp: blend_mode = "hMix"; break;
cristy216b90f2014-04-12 22:59:20 +0000258 case HueCompositeOp: blend_mode = "hue "; break;
259 case LightenCompositeOp: blend_mode = "lite"; break;
cristyfdc035a2014-06-26 11:29:29 +0000260 case LinearBurnCompositeOp: blend_mode = "lbrn"; break;
261 case LinearDodgeCompositeOp:blend_mode = "lddg"; break;
262 case LinearLightCompositeOp:blend_mode = "lLit"; break;
cristy216b90f2014-04-12 22:59:20 +0000263 case LuminizeCompositeOp: blend_mode = "lum "; break;
264 case MultiplyCompositeOp: blend_mode = "mul "; break;
265 case OverCompositeOp: blend_mode = "norm"; break;
266 case OverlayCompositeOp: blend_mode = "over"; break;
cristyfdc035a2014-06-26 11:29:29 +0000267 case PinLightCompositeOp: blend_mode = "pLit"; break;
cristy216b90f2014-04-12 22:59:20 +0000268 case SaturateCompositeOp: blend_mode = "sat "; break;
269 case ScreenCompositeOp: blend_mode = "scrn"; break;
270 case SoftLightCompositeOp: blend_mode = "sLit"; break;
cristyfdc035a2014-06-26 11:29:29 +0000271 case VividLightCompositeOp: blend_mode = "vLit"; break;
cristy216b90f2014-04-12 22:59:20 +0000272 default: blend_mode = "norm";
cristycd081772010-03-22 17:19:12 +0000273 }
274 return(blend_mode);
275}
276
dirkb41f0802013-12-26 16:47:58 +0000277static inline CompressionType ConvertPSDCompression(
278 PSDCompressionType compression)
279{
280 switch (compression)
281 {
282 case RLE:
283 return RLECompression;
284 case ZipWithPrediction:
285 case ZipWithoutPrediction:
286 return ZipCompression;
287 default:
288 return NoCompression;
289 }
290}
291
292static MagickStatusType CorrectPSDOpacity(LayerInfo* layer_info,
293 ExceptionInfo *exception)
294{
295 register Quantum
296 *q;
297
298 register ssize_t
299 x;
300
301 ssize_t
302 y;
303
304 if (layer_info->opacity == OpaqueAlpha)
305 return(MagickTrue);
306
307 layer_info->image->alpha_trait=BlendPixelTrait;
308 for (y=0; y < (ssize_t) layer_info->image->rows; y++)
309 {
cristyd8083a62014-02-01 13:53:44 +0000310 q=GetAuthenticPixels(layer_info->image,0,y,layer_info->image->columns,1,
311 exception);
dirkb41f0802013-12-26 16:47:58 +0000312 if (q == (Quantum *) NULL)
313 break;
314 for (x=0; x < (ssize_t) layer_info->image->columns; x++)
315 {
316 SetPixelAlpha(layer_info->image,(Quantum) (QuantumScale*(GetPixelAlpha(
317 layer_info->image,q))*layer_info->opacity),q);
318 q+=GetPixelChannels(layer_info->image);
319 }
320 if (SyncAuthenticPixels(layer_info->image,exception) == MagickFalse)
321 return(MagickFalse);
322 }
323
324 return(MagickTrue);
325}
326
cristycd081772010-03-22 17:19:12 +0000327static ssize_t DecodePSDPixels(const size_t number_compact_pixels,
cristybb503372010-05-27 20:51:26 +0000328 const unsigned char *compact_pixels,const ssize_t depth,
cristycd081772010-03-22 17:19:12 +0000329 const size_t number_pixels,unsigned char *pixels)
330{
dirk891df412014-12-17 07:25:25 +0000331#define CheckNumberCompactPixels \
332 if (packets == 0) \
333 return(i); \
334 packets--
335
336#define CheckNumberPixels(count) \
337 if (((ssize_t) i + count) > (ssize_t) number_pixels) \
338 return(i); \
339 i+=count
340
cristycd081772010-03-22 17:19:12 +0000341 int
342 pixel;
343
344 register ssize_t
345 i,
346 j;
347
cristycd081772010-03-22 17:19:12 +0000348 size_t
349 length;
350
cristy802d3642011-04-27 02:02:41 +0000351 ssize_t
352 packets;
353
cristycd081772010-03-22 17:19:12 +0000354 packets=(ssize_t) number_compact_pixels;
355 for (i=0; (packets > 1) && (i < (ssize_t) number_pixels); )
356 {
dirkdb435e22015-01-24 11:38:04 +0000357 packets--;
dirk40084fa2014-02-03 21:29:38 +0000358 length=(size_t) (*compact_pixels++);
cristycd081772010-03-22 17:19:12 +0000359 if (length == 128)
360 continue;
361 if (length > 128)
362 {
363 length=256-length+1;
dirk891df412014-12-17 07:25:25 +0000364 CheckNumberCompactPixels;
cristycd081772010-03-22 17:19:12 +0000365 pixel=(*compact_pixels++);
cristy284c7d82010-04-24 00:19:14 +0000366 for (j=0; j < (ssize_t) length; j++)
cristycd081772010-03-22 17:19:12 +0000367 {
368 switch (depth)
369 {
370 case 1:
371 {
dirk891df412014-12-17 07:25:25 +0000372 CheckNumberPixels(8);
cristy284c7d82010-04-24 00:19:14 +0000373 *pixels++=(pixel >> 7) & 0x01 ? 0U : 255U;
374 *pixels++=(pixel >> 6) & 0x01 ? 0U : 255U;
375 *pixels++=(pixel >> 5) & 0x01 ? 0U : 255U;
376 *pixels++=(pixel >> 4) & 0x01 ? 0U : 255U;
377 *pixels++=(pixel >> 3) & 0x01 ? 0U : 255U;
378 *pixels++=(pixel >> 2) & 0x01 ? 0U : 255U;
379 *pixels++=(pixel >> 1) & 0x01 ? 0U : 255U;
380 *pixels++=(pixel >> 0) & 0x01 ? 0U : 255U;
cristycd081772010-03-22 17:19:12 +0000381 break;
382 }
383 case 2:
384 {
dirk891df412014-12-17 07:25:25 +0000385 CheckNumberPixels(4);
cristy284c7d82010-04-24 00:19:14 +0000386 *pixels++=(unsigned char) ((pixel >> 6) & 0x03);
387 *pixels++=(unsigned char) ((pixel >> 4) & 0x03);
388 *pixels++=(unsigned char) ((pixel >> 2) & 0x03);
389 *pixels++=(unsigned char) ((pixel & 0x03) & 0x03);
dirk891df412014-12-17 07:25:25 +0000390 break;
391 }
392 case 4:
393 {
394 CheckNumberPixels(2);
395 *pixels++=(unsigned char) ((pixel >> 4) & 0xff);
396 *pixels++=(unsigned char) ((pixel & 0x0f) & 0xff);
cristycd081772010-03-22 17:19:12 +0000397 break;
398 }
399 default:
400 {
dirk891df412014-12-17 07:25:25 +0000401 CheckNumberPixels(1);
cristy284c7d82010-04-24 00:19:14 +0000402 *pixels++=(unsigned char) pixel;
cristycd081772010-03-22 17:19:12 +0000403 break;
404 }
405 }
406 }
407 continue;
408 }
409 length++;
cristy284c7d82010-04-24 00:19:14 +0000410 for (j=0; j < (ssize_t) length; j++)
cristycd081772010-03-22 17:19:12 +0000411 {
412 switch (depth)
413 {
414 case 1:
415 {
dirk891df412014-12-17 07:25:25 +0000416 CheckNumberPixels(8);
cristy284c7d82010-04-24 00:19:14 +0000417 *pixels++=(*compact_pixels >> 7) & 0x01 ? 0U : 255U;
418 *pixels++=(*compact_pixels >> 6) & 0x01 ? 0U : 255U;
419 *pixels++=(*compact_pixels >> 5) & 0x01 ? 0U : 255U;
420 *pixels++=(*compact_pixels >> 4) & 0x01 ? 0U : 255U;
421 *pixels++=(*compact_pixels >> 3) & 0x01 ? 0U : 255U;
422 *pixels++=(*compact_pixels >> 2) & 0x01 ? 0U : 255U;
423 *pixels++=(*compact_pixels >> 1) & 0x01 ? 0U : 255U;
424 *pixels++=(*compact_pixels >> 0) & 0x01 ? 0U : 255U;
cristycd081772010-03-22 17:19:12 +0000425 break;
426 }
427 case 2:
428 {
dirk891df412014-12-17 07:25:25 +0000429 CheckNumberPixels(4);
cristy618a9662010-03-23 13:59:54 +0000430 *pixels++=(*compact_pixels >> 6) & 0x03;
431 *pixels++=(*compact_pixels >> 4) & 0x03;
432 *pixels++=(*compact_pixels >> 2) & 0x03;
433 *pixels++=(*compact_pixels & 0x03) & 0x03;
dirk891df412014-12-17 07:25:25 +0000434 break;
435 }
436 case 4:
437 {
438 CheckNumberPixels(2);
439 *pixels++=(*compact_pixels >> 4) & 0xff;
440 *pixels++=(*compact_pixels & 0x0f) & 0xff;
cristycd081772010-03-22 17:19:12 +0000441 break;
442 }
443 default:
444 {
dirk891df412014-12-17 07:25:25 +0000445 CheckNumberPixels(1);
cristycd081772010-03-22 17:19:12 +0000446 *pixels++=(*compact_pixels);
cristycd081772010-03-22 17:19:12 +0000447 break;
448 }
449 }
dirk891df412014-12-17 07:25:25 +0000450 CheckNumberCompactPixels;
cristycd081772010-03-22 17:19:12 +0000451 compact_pixels++;
452 }
453 }
454 return(i);
455}
456
dirkbd8bd852013-12-28 22:55:19 +0000457static inline LayerInfo *DestroyLayerInfo(LayerInfo *layer_info,
458 const ssize_t number_layers)
459{
460 ssize_t
461 i;
462
463 for (i=0; i<number_layers; i++)
464 {
465 if (layer_info[i].image != (Image *) NULL)
466 layer_info[i].image=DestroyImage(layer_info[i].image);
467 }
468
469 return (LayerInfo *) RelinquishMagickMemory(layer_info);
470}
471
dirkb41f0802013-12-26 16:47:58 +0000472static inline size_t GetPSDPacketSize(Image *image)
cristy2d3d87f2010-03-01 00:23:08 +0000473{
dirkb41f0802013-12-26 16:47:58 +0000474 if (image->storage_class == PseudoClass)
475 {
476 if (image->colors > 256)
477 return(2);
478 else if (image->depth > 8)
479 return(2);
480 }
481 else
482 if (image->depth > 8)
483 return(2);
484
485 return(1);
cristy2d3d87f2010-03-01 00:23:08 +0000486}
487
dirk4d9863c2014-03-28 19:48:49 +0000488static inline MagickSizeType GetPSDSize(const PSDInfo *psd_info,Image *image)
cristy2d3d87f2010-03-01 00:23:08 +0000489{
490 if (psd_info->version == 1)
491 return((MagickSizeType) ReadBlobMSBLong(image));
492 return((MagickSizeType) ReadBlobMSBLongLong(image));
493}
494
dirkb41f0802013-12-26 16:47:58 +0000495static inline size_t GetPSDRowSize(Image *image)
496{
497 if (image->depth == 1)
498 return((image->columns+7)/8);
499 else
500 return(image->columns*GetPSDPacketSize(image));
501}
502
cristycd081772010-03-22 17:19:12 +0000503static const char *ModeToString(PSDImageType type)
cristy3ed852e2009-09-05 21:47:34 +0000504{
cristycd081772010-03-22 17:19:12 +0000505 switch (type)
cristy3ed852e2009-09-05 21:47:34 +0000506 {
507 case BitmapMode: return "Bitmap";
508 case GrayscaleMode: return "Grayscale";
509 case IndexedMode: return "Indexed";
510 case RGBMode: return "RGB";
511 case CMYKMode: return "CMYK";
512 case MultichannelMode: return "Multichannel";
513 case DuotoneMode: return "Duotone";
514 case LabMode: return "L*A*B";
515 default: return "unknown";
516 }
517}
518
dirkd9795a12014-11-11 22:01:21 +0000519static void NegateCMYK(Image *image,ExceptionInfo *exception)
520{
521 ChannelType
522 channel_mask;
523
524 channel_mask=SetImageChannelMask(image,(ChannelType)(AllChannels &~
525 AlphaChannel));
526 NegateImage(image,MagickFalse,exception);
527 (void) SetImageChannelMask(image,channel_mask);
528}
529
dirk7ec89322014-12-16 22:50:15 +0000530static void ParseImageResourceBlocks(Image *image,
dirk18c0e4d2014-02-22 22:24:05 +0000531 const unsigned char *blocks,size_t length,
532 MagickBooleanType *has_merged_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000533{
534 const unsigned char
535 *p;
536
537 StringInfo
538 *profile;
539
cristy6befb0f2010-05-31 14:33:15 +0000540 unsigned int
cristy3ed852e2009-09-05 21:47:34 +0000541 count,
cristy6befb0f2010-05-31 14:33:15 +0000542 long_sans;
cristy3ed852e2009-09-05 21:47:34 +0000543
544 unsigned short
545 id,
546 short_sans;
547
548 if (length < 16)
dirk7ec89322014-12-16 22:50:15 +0000549 return;
cristyb3f97ae2015-05-18 12:29:32 +0000550 profile=BlobToStringInfo((const unsigned char *) NULL,length);
cristy3ed852e2009-09-05 21:47:34 +0000551 SetStringInfoDatum(profile,blocks);
cristyd15e6592011-10-15 00:13:06 +0000552 (void) SetImageProfile(image,"8bim",profile,exception);
cristy3ed852e2009-09-05 21:47:34 +0000553 profile=DestroyStringInfo(profile);
554 for (p=blocks; (p >= blocks) && (p < (blocks+length-16)); )
555 {
556 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
557 break;
cristy6befb0f2010-05-31 14:33:15 +0000558 p=PushLongPixel(MSBEndian,p,&long_sans);
cristyf11065e2010-05-14 13:26:59 +0000559 p=PushShortPixel(MSBEndian,p,&id);
560 p=PushShortPixel(MSBEndian,p,&short_sans);
561 p=PushLongPixel(MSBEndian,p,&count);
dirk7ec89322014-12-16 22:50:15 +0000562 if (p+count > blocks+length)
563 return;
cristy3ed852e2009-09-05 21:47:34 +0000564 switch (id)
565 {
566 case 0x03ed:
567 {
cristy1e4a80b2010-05-14 16:18:26 +0000568 char
cristy151b66d2015-04-15 10:50:31 +0000569 value[MagickPathExtent];
cristy1e4a80b2010-05-14 16:18:26 +0000570
cristy3ed852e2009-09-05 21:47:34 +0000571 unsigned short
572 resolution;
573
574 /*
575 Resolution info.
576 */
cristyf11065e2010-05-14 13:26:59 +0000577 p=PushShortPixel(MSBEndian,p,&resolution);
cristy2a11bef2011-10-28 18:33:11 +0000578 image->resolution.x=(double) resolution;
cristy151b66d2015-04-15 10:50:31 +0000579 (void) FormatLocaleString(value,MagickPathExtent,"%g",image->resolution.x);
cristyd15e6592011-10-15 00:13:06 +0000580 (void) SetImageProperty(image,"tiff:XResolution",value,exception);
cristyf11065e2010-05-14 13:26:59 +0000581 p=PushShortPixel(MSBEndian,p,&short_sans);
582 p=PushShortPixel(MSBEndian,p,&short_sans);
583 p=PushShortPixel(MSBEndian,p,&short_sans);
584 p=PushShortPixel(MSBEndian,p,&resolution);
cristy2a11bef2011-10-28 18:33:11 +0000585 image->resolution.y=(double) resolution;
cristy151b66d2015-04-15 10:50:31 +0000586 (void) FormatLocaleString(value,MagickPathExtent,"%g",image->resolution.y);
cristyd15e6592011-10-15 00:13:06 +0000587 (void) SetImageProperty(image,"tiff:YResolution",value,exception);
cristyf11065e2010-05-14 13:26:59 +0000588 p=PushShortPixel(MSBEndian,p,&short_sans);
589 p=PushShortPixel(MSBEndian,p,&short_sans);
590 p=PushShortPixel(MSBEndian,p,&short_sans);
cristy15893bd2012-12-19 14:40:13 +0000591 image->units=PixelsPerInchResolution;
cristy3ed852e2009-09-05 21:47:34 +0000592 break;
593 }
dirk18c0e4d2014-02-22 22:24:05 +0000594 case 0x0421:
595 {
596 if (*(p+4) == 0)
597 *has_merged_image=MagickFalse;
dirkc3378432014-05-18 06:50:37 +0000598 p+=count;
599 break;
dirk18c0e4d2014-02-22 22:24:05 +0000600 }
cristy3ed852e2009-09-05 21:47:34 +0000601 default:
602 {
603 p+=count;
604 break;
605 }
606 }
607 if ((count & 0x01) != 0)
608 p++;
609 }
dirk7ec89322014-12-16 22:50:15 +0000610 return;
cristy3ed852e2009-09-05 21:47:34 +0000611}
612
cristycd081772010-03-22 17:19:12 +0000613static CompositeOperator PSDBlendModeToCompositeOperator(const char *mode)
cristy56ed31c2010-03-22 00:46:21 +0000614{
cristycd081772010-03-22 17:19:12 +0000615 if (mode == (const char *) NULL)
616 return(OverCompositeOp);
617 if (LocaleNCompare(mode,"norm",4) == 0)
618 return(OverCompositeOp);
619 if (LocaleNCompare(mode,"mul ",4) == 0)
620 return(MultiplyCompositeOp);
621 if (LocaleNCompare(mode,"diss",4) == 0)
622 return(DissolveCompositeOp);
623 if (LocaleNCompare(mode,"diff",4) == 0)
624 return(DifferenceCompositeOp);
625 if (LocaleNCompare(mode,"dark",4) == 0)
626 return(DarkenCompositeOp);
627 if (LocaleNCompare(mode,"lite",4) == 0)
628 return(LightenCompositeOp);
629 if (LocaleNCompare(mode,"hue ",4) == 0)
630 return(HueCompositeOp);
631 if (LocaleNCompare(mode,"sat ",4) == 0)
632 return(SaturateCompositeOp);
633 if (LocaleNCompare(mode,"colr",4) == 0)
634 return(ColorizeCompositeOp);
635 if (LocaleNCompare(mode,"lum ",4) == 0)
636 return(LuminizeCompositeOp);
637 if (LocaleNCompare(mode,"scrn",4) == 0)
638 return(ScreenCompositeOp);
639 if (LocaleNCompare(mode,"over",4) == 0)
640 return(OverlayCompositeOp);
641 if (LocaleNCompare(mode,"hLit",4) == 0)
cristy216b90f2014-04-12 22:59:20 +0000642 return(HardLightCompositeOp);
cristycd081772010-03-22 17:19:12 +0000643 if (LocaleNCompare(mode,"sLit",4) == 0)
cristy216b90f2014-04-12 22:59:20 +0000644 return(SoftLightCompositeOp);
cristycd081772010-03-22 17:19:12 +0000645 if (LocaleNCompare(mode,"smud",4) == 0)
cristyfdc035a2014-06-26 11:29:29 +0000646 return(ExclusionCompositeOp);
cristycd081772010-03-22 17:19:12 +0000647 if (LocaleNCompare(mode,"div ",4) == 0)
cristyfdc035a2014-06-26 11:29:29 +0000648 return(ColorDodgeCompositeOp);
cristycd081772010-03-22 17:19:12 +0000649 if (LocaleNCompare(mode,"idiv",4) == 0)
cristyfdc035a2014-06-26 11:29:29 +0000650 return(ColorBurnCompositeOp);
651 if (LocaleNCompare(mode,"lbrn",4) == 0)
652 return(LinearBurnCompositeOp);
653 if (LocaleNCompare(mode,"lddg",4) == 0)
654 return(LinearDodgeCompositeOp);
655 if (LocaleNCompare(mode,"lLit",4) == 0)
656 return(LinearLightCompositeOp);
657 if (LocaleNCompare(mode,"vLit",4) == 0)
658 return(VividLightCompositeOp);
659 if (LocaleNCompare(mode,"pLit",4) == 0)
660 return(PinLightCompositeOp);
661 if (LocaleNCompare(mode,"hMix",4) == 0)
662 return(HardMixCompositeOp);
cristycd081772010-03-22 17:19:12 +0000663 return(OverCompositeOp);
cristy56ed31c2010-03-22 00:46:21 +0000664}
665
dirkb41f0802013-12-26 16:47:58 +0000666static MagickStatusType ReadPSDChannelPixels(Image *image,
667 const size_t channels,const size_t row,const ssize_t type,
668 const unsigned char *pixels,ExceptionInfo *exception)
cristy56ed31c2010-03-22 00:46:21 +0000669{
cristy56ed31c2010-03-22 00:46:21 +0000670 Quantum
671 pixel;
672
cristy7753b2a2011-02-19 18:36:52 +0000673 register const unsigned char
674 *p;
675
cristy4c08aed2011-07-01 19:47:50 +0000676 register Quantum
cristy56ed31c2010-03-22 00:46:21 +0000677 *q;
678
cristy7753b2a2011-02-19 18:36:52 +0000679 register ssize_t
680 x;
cristy56ed31c2010-03-22 00:46:21 +0000681
682 size_t
683 packet_size;
684
dirkb41f0802013-12-26 16:47:58 +0000685 unsigned short
686 nibble;
687
688 p=pixels;
689 q=GetAuthenticPixels(image,0,row,image->columns,1,exception);
690 if (q == (Quantum *) NULL)
691 return MagickFalse;
692 packet_size=GetPSDPacketSize(image);
693 for (x=0; x < (ssize_t) image->columns; x++)
694 {
695 if (packet_size == 1)
696 pixel=ScaleCharToQuantum(*p++);
697 else
698 {
699 p=PushShortPixel(MSBEndian,p,&nibble);
700 pixel=ScaleShortToQuantum(nibble);
701 }
702 switch (type)
703 {
704 case -1:
705 {
dirkd9795a12014-11-11 22:01:21 +0000706 SetPixelAlpha(image,pixel,q);
dirkb41f0802013-12-26 16:47:58 +0000707 break;
708 }
709 case 0:
710 {
711 SetPixelRed(image,pixel,q);
712 if (channels == 1)
713 SetPixelGray(image,pixel,q);
dirkb41f0802013-12-26 16:47:58 +0000714 if (image->storage_class == PseudoClass)
715 {
716 if (packet_size == 1)
717 SetPixelIndex(image,ScaleQuantumToChar(pixel),q);
718 else
719 SetPixelIndex(image,ScaleQuantumToShort(pixel),q);
cristy11a06d32015-01-04 12:03:27 +0000720 SetPixelViaPixelInfo(image,image->colormap+(ssize_t)
cristyf432c632014-12-07 15:11:28 +0000721 ConstrainColormapIndex(image,GetPixelIndex(image,q),exception),q);
dirkb41f0802013-12-26 16:47:58 +0000722 if (image->depth == 1)
723 {
724 ssize_t
725 bit,
726 number_bits;
727
728 number_bits=image->columns-x;
729 if (number_bits > 8)
730 number_bits=8;
731 for (bit=0; bit < number_bits; bit++)
732 {
733 SetPixelIndex(image,(((unsigned char) pixel) &
734 (0x01 << (7-bit))) != 0 ? 0 : 255,q);
cristy11a06d32015-01-04 12:03:27 +0000735 SetPixelViaPixelInfo(image,image->colormap+(ssize_t)
dirkb41f0802013-12-26 16:47:58 +0000736 GetPixelIndex(image,q),q);
737 q+=GetPixelChannels(image);
738 x++;
739 }
dirk5a4bdff2014-07-31 20:49:41 +0000740 x--;
741 continue;
dirkb41f0802013-12-26 16:47:58 +0000742 }
743 }
744 break;
745 }
746 case 1:
747 {
748 if (image->storage_class == PseudoClass)
dirkd9795a12014-11-11 22:01:21 +0000749 SetPixelAlpha(image,pixel,q);
dirkb41f0802013-12-26 16:47:58 +0000750 else
751 SetPixelGreen(image,pixel,q);
752 break;
753 }
754 case 2:
755 {
756 if (image->storage_class == PseudoClass)
dirkd9795a12014-11-11 22:01:21 +0000757 SetPixelAlpha(image,pixel,q);
dirkb41f0802013-12-26 16:47:58 +0000758 else
759 SetPixelBlue(image,pixel,q);
760 break;
761 }
762 case 3:
763 {
764 if (image->colorspace == CMYKColorspace)
765 SetPixelBlack(image,pixel,q);
766 else
cristy17f11b02014-12-20 19:37:04 +0000767 if (image->alpha_trait != UndefinedPixelTrait)
dirkd9795a12014-11-11 22:01:21 +0000768 SetPixelAlpha(image,pixel,q);
dirkb41f0802013-12-26 16:47:58 +0000769 break;
770 }
771 case 4:
772 {
773 if ((IssRGBCompatibleColorspace(image->colorspace) != MagickFalse) &&
774 (channels > 3))
775 break;
cristy17f11b02014-12-20 19:37:04 +0000776 if (image->alpha_trait != UndefinedPixelTrait)
dirkd9795a12014-11-11 22:01:21 +0000777 SetPixelAlpha(image,pixel,q);
dirkb41f0802013-12-26 16:47:58 +0000778 break;
779 }
780 default:
781 break;
782 }
783 q+=GetPixelChannels(image);
784 }
785 return(SyncAuthenticPixels(image,exception));
786}
787
788static MagickStatusType ReadPSDChannelRaw(Image *image,const size_t channels,
789 const ssize_t type,ExceptionInfo *exception)
790{
791 MagickStatusType
792 status;
793
794 size_t
795 count,
796 row_size;
797
798 ssize_t
799 y;
800
801 unsigned char
802 *pixels;
803
804 if (image->debug != MagickFalse)
805 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
806 " layer data is RAW");
807
808 row_size=GetPSDRowSize(image);
dirk891df412014-12-17 07:25:25 +0000809 pixels=(unsigned char *) AcquireQuantumMemory(row_size,sizeof(*pixels));
dirkb41f0802013-12-26 16:47:58 +0000810 if (pixels == (unsigned char *) NULL)
811 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
812 image->filename);
813
814 status=MagickTrue;
815 for (y=0; y < (ssize_t) image->rows; y++)
816 {
817 status=MagickFalse;
818
819 count=ReadBlob(image,row_size,pixels);
820 if (count != row_size)
821 break;
822
823 status=ReadPSDChannelPixels(image,channels,y,type,pixels,exception);
824 if (status == MagickFalse)
825 break;
826 }
827
828 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
829 return(status);
830}
831
832static inline MagickOffsetType *ReadPSDRLEOffsets(Image *image,
dirk4d9863c2014-03-28 19:48:49 +0000833 const PSDInfo *psd_info,const size_t size)
dirkb41f0802013-12-26 16:47:58 +0000834{
835 MagickOffsetType
836 *offsets;
837
838 ssize_t
839 y;
840
841 offsets=(MagickOffsetType *) AcquireQuantumMemory(size,sizeof(*offsets));
842 if(offsets != (MagickOffsetType *) NULL)
843 {
844 for (y=0; y < (ssize_t) size; y++)
845 {
846 if (psd_info->version == 1)
847 offsets[y]=(MagickOffsetType) ReadBlobMSBShort(image);
848 else
849 offsets[y]=(MagickOffsetType) ReadBlobMSBLong(image);
850 }
851 }
852 return offsets;
853}
854
dirk4d9863c2014-03-28 19:48:49 +0000855static MagickStatusType ReadPSDChannelRLE(Image *image,const PSDInfo *psd_info,
dirkb41f0802013-12-26 16:47:58 +0000856 const ssize_t type,MagickOffsetType *offsets,ExceptionInfo *exception)
857{
858 MagickStatusType
859 status;
860
861 size_t
862 length,
863 row_size;
864
cristy56ed31c2010-03-22 00:46:21 +0000865 ssize_t
cristyd05dca12010-07-25 02:32:32 +0000866 count,
867 y;
cristy56ed31c2010-03-22 00:46:21 +0000868
869 unsigned char
870 *compact_pixels,
871 *pixels;
872
dirkb41f0802013-12-26 16:47:58 +0000873 if (image->debug != MagickFalse)
874 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
875 " layer data is RLE compressed");
cristy56ed31c2010-03-22 00:46:21 +0000876
dirkb41f0802013-12-26 16:47:58 +0000877 row_size=GetPSDRowSize(image);
dirk891df412014-12-17 07:25:25 +0000878 pixels=(unsigned char *) AcquireQuantumMemory(row_size,sizeof(*pixels));
cristy56ed31c2010-03-22 00:46:21 +0000879 if (pixels == (unsigned char *) NULL)
880 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
881 image->filename);
cristye195f262010-04-16 18:12:35 +0000882
dirkb41f0802013-12-26 16:47:58 +0000883 length=0;
884 for (y=0; y < (ssize_t) image->rows; y++)
885 if ((MagickOffsetType) length < offsets[y])
886 length=(size_t) offsets[y];
887
888 if (length > row_size + 256) // arbitrary number
889 {
890 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
891 ThrowBinaryException(ResourceLimitError,"InvalidLength",
892 image->filename);
cristy56ed31c2010-03-22 00:46:21 +0000893 }
dirkb41f0802013-12-26 16:47:58 +0000894
dirk891df412014-12-17 07:25:25 +0000895 compact_pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
dirkb41f0802013-12-26 16:47:58 +0000896 if (compact_pixels == (unsigned char *) NULL)
897 {
898 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
899 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
900 image->filename);
901 }
902
903 (void) ResetMagickMemory(compact_pixels,0,length*sizeof(*compact_pixels));
904
905 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +0000906 for (y=0; y < (ssize_t) image->rows; y++)
cristy56ed31c2010-03-22 00:46:21 +0000907 {
dirkb41f0802013-12-26 16:47:58 +0000908 status=MagickFalse;
cristy3b004082010-08-12 19:56:38 +0000909
dirkb41f0802013-12-26 16:47:58 +0000910 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
911 if (count != (ssize_t) offsets[y])
912 break;
913
914 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
915 (ssize_t) (image->depth == 1 ? 123456 : image->depth),row_size,pixels);
916 if (count != (ssize_t) row_size)
917 break;
918
919 status=ReadPSDChannelPixels(image,psd_info->channels,y,type,pixels,
920 exception);
921 if (status == MagickFalse)
cristy56ed31c2010-03-22 00:46:21 +0000922 break;
923 }
dirkb41f0802013-12-26 16:47:58 +0000924
925 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
cristy56ed31c2010-03-22 00:46:21 +0000926 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
dirkb41f0802013-12-26 16:47:58 +0000927 return(status);
cristy56ed31c2010-03-22 00:46:21 +0000928}
929
dirkb41f0802013-12-26 16:47:58 +0000930#ifdef MAGICKCORE_ZLIB_DELEGATE
cristyff30f832014-11-05 14:04:21 +0000931static MagickStatusType ReadPSDChannelZip(Image *image,const size_t channels,
932 const ssize_t type,const PSDCompressionType compression,
933 const size_t compact_size,ExceptionInfo *exception)
934{
dirkb41f0802013-12-26 16:47:58 +0000935 MagickStatusType
936 status;
937
938 register unsigned char
939 *p;
940
941 size_t
942 count,
943 length,
944 packet_size,
945 row_size;
946
947 ssize_t
948 y;
949
950 unsigned char
951 *compact_pixels,
952 *pixels;
953
954 z_stream
955 stream;
956
957 if (image->debug != MagickFalse)
958 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
dirk245cc752014-01-16 18:31:58 +0000959 " layer data is ZIP compressed");
dirkb41f0802013-12-26 16:47:58 +0000960
961 compact_pixels=(unsigned char *) AcquireQuantumMemory(compact_size,
962 sizeof(*compact_pixels));
963 if (compact_pixels == (unsigned char *) NULL)
964 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
965 image->filename);
966
967 packet_size=GetPSDPacketSize(image);
968 row_size=image->columns*packet_size;
969 count=image->rows*row_size;
970
971 pixels=(unsigned char *) AcquireQuantumMemory(count,sizeof(*pixels));
972 if (pixels == (unsigned char *) NULL)
973 {
974 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
975 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
976 image->filename);
977 }
978
979 ResetMagickMemory(&stream, 0, sizeof(z_stream));
980 stream.data_type=Z_BINARY;
981 (void) ReadBlob(image,compact_size,compact_pixels);
982
983 stream.next_in=(Bytef *)compact_pixels;
cristy0e274112014-03-25 14:09:19 +0000984 stream.avail_in=(unsigned int) compact_size;
dirkb41f0802013-12-26 16:47:58 +0000985 stream.next_out=(Bytef *)pixels;
cristy0e274112014-03-25 14:09:19 +0000986 stream.avail_out=(unsigned int) count;
dirkb41f0802013-12-26 16:47:58 +0000987
988 if(inflateInit(&stream) == Z_OK)
989 {
990 int
991 ret;
992
993 while (stream.avail_out > 0)
994 {
995 ret=inflate(&stream, Z_SYNC_FLUSH);
996 if (ret != Z_OK && ret != Z_STREAM_END)
997 {
998 compact_pixels=(unsigned char *) RelinquishMagickMemory(
999 compact_pixels);
1000 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1001 return(MagickFalse);
1002 }
1003 }
1004 }
1005
1006 if (compression == ZipWithPrediction)
1007 {
1008 p=pixels;
1009 while(count > 0)
1010 {
1011 length=image->columns;
1012 while(--length)
1013 {
1014 if (packet_size == 2)
1015 {
1016 p[2]+=p[0]+((p[1]+p[3]) >> 8);
1017 p[3]+=p[1];
1018 }
1019 else
1020 *(p+1)+=*p;
1021 p+=packet_size;
1022 }
1023 p+=packet_size;
1024 count-=row_size;
1025 }
1026 }
1027
1028 status=MagickTrue;
1029 p=pixels;
1030 for (y=0; y < (ssize_t) image->rows; y++)
1031 {
1032 status=ReadPSDChannelPixels(image,channels,y,type,p,exception);
1033 if (status == MagickFalse)
1034 break;
1035
1036 p+=row_size;
1037 }
1038
1039 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
1040 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1041 return(status);
dirkb41f0802013-12-26 16:47:58 +00001042}
cristy0d67b922014-11-05 19:29:56 +00001043#endif
dirkb41f0802013-12-26 16:47:58 +00001044
dirk4d9863c2014-03-28 19:48:49 +00001045static MagickStatusType ReadPSDChannel(Image *image,const PSDInfo *psd_info,
dirkb41f0802013-12-26 16:47:58 +00001046 const LayerInfo* layer_info,const size_t channel,
1047 const PSDCompressionType compression,ExceptionInfo *exception)
1048{
1049 MagickOffsetType
1050 offset;
1051
1052 MagickStatusType
1053 status;
1054
dirk245cc752014-01-16 18:31:58 +00001055 if (layer_info->channel_info[channel].type < -1)
1056 {
1057 /* ignore user supplied layer mask */
1058 SeekBlob(image,layer_info->channel_info[channel].size-2,SEEK_CUR);
1059 return(MagickTrue);
1060 }
1061
dirkb41f0802013-12-26 16:47:58 +00001062 offset=TellBlob(image);
1063 status=MagickTrue;
1064 switch(compression)
1065 {
1066 case Raw:
1067 return(ReadPSDChannelRaw(image,psd_info->channels,
1068 layer_info->channel_info[channel].type,exception));
1069 case RLE:
1070 {
1071 MagickOffsetType
1072 *offsets;
1073
1074 offsets=ReadPSDRLEOffsets(image,psd_info,image->rows);
1075 if (offsets == (MagickOffsetType *) NULL)
1076 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1077 image->filename);
1078 status=ReadPSDChannelRLE(image,psd_info,
1079 layer_info->channel_info[channel].type,offsets,exception);
1080 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1081 }
1082 break;
1083 case ZipWithPrediction:
1084 case ZipWithoutPrediction:
1085#ifdef MAGICKCORE_ZLIB_DELEGATE
1086 status=ReadPSDChannelZip(image,layer_info->channels,
1087 layer_info->channel_info[channel].type,compression,
1088 layer_info->channel_info[channel].size-2,exception);
1089#else
1090 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1091 (void) ThrowMagickException(exception,GetMagickModule(),
1092 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn",
1093 "'%s' (ZLIB)",image->filename);
1094#endif
1095 break;
1096 default:
1097 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1098 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
1099 "CompressionNotSupported","'%.20g'",(double) compression);
1100 break;
1101 }
1102
1103 if (status == MagickFalse)
1104 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1105
1106 return(status);
1107}
1108
dirk4d9863c2014-03-28 19:48:49 +00001109static MagickStatusType ReadPSDLayer(Image *image,const PSDInfo *psd_info,
dirkb41f0802013-12-26 16:47:58 +00001110 LayerInfo* layer_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001111{
cristy3ed852e2009-09-05 21:47:34 +00001112 char
cristy151b66d2015-04-15 10:50:31 +00001113 message[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001114
dirkb41f0802013-12-26 16:47:58 +00001115 MagickStatusType
1116 status;
1117
1118 PSDCompressionType
1119 compression;
1120
1121 ssize_t
1122 j;
1123
1124 if (image->debug != MagickFalse)
1125 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1126 " setting up new layer image");
1127 (void) SetImageBackgroundColor(layer_info->image,exception);
1128 layer_info->image->compose=PSDBlendModeToCompositeOperator(
1129 layer_info->blendkey);
1130 if (layer_info->visible == MagickFalse)
1131 layer_info->image->compose=NoCompositeOp;
1132 if (psd_info->mode == CMYKMode)
1133 SetImageColorspace(layer_info->image,CMYKColorspace,exception);
cristyd8083a62014-02-01 13:53:44 +00001134 if ((psd_info->mode == BitmapMode) || (psd_info->mode == GrayscaleMode) ||
dirkb41f0802013-12-26 16:47:58 +00001135 (psd_info->mode == DuotoneMode))
1136 SetImageColorspace(layer_info->image,GRAYColorspace,exception);
1137 /*
1138 Set up some hidden attributes for folks that need them.
1139 */
cristy151b66d2015-04-15 10:50:31 +00001140 (void) FormatLocaleString(message,MagickPathExtent,"%.20gld",
dirkb41f0802013-12-26 16:47:58 +00001141 (double) layer_info->page.x);
1142 (void) SetImageArtifact(layer_info->image,"psd:layer.x",message);
cristy151b66d2015-04-15 10:50:31 +00001143 (void) FormatLocaleString(message,MagickPathExtent,"%.20g",
dirkb41f0802013-12-26 16:47:58 +00001144 (double) layer_info->page.y);
1145 (void) SetImageArtifact(layer_info->image,"psd:layer.y",message);
cristy151b66d2015-04-15 10:50:31 +00001146 (void) FormatLocaleString(message,MagickPathExtent,"%.20g",(double)
dirkb41f0802013-12-26 16:47:58 +00001147 layer_info->opacity);
1148 (void) SetImageArtifact(layer_info->image,"psd:layer.opacity",message);
1149 (void) SetImageProperty(layer_info->image,"label",(char *) layer_info->name,
1150 exception);
1151
1152 status=MagickTrue;
dirkb41f0802013-12-26 16:47:58 +00001153 for (j=0; j < (ssize_t) layer_info->channels; j++)
1154 {
1155 if (image->debug != MagickFalse)
1156 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1157 " reading data for channel %.20g",(double) j);
1158
1159 compression=(PSDCompressionType) ReadBlobMSBShort(layer_info->image);
1160 layer_info->image->compression=ConvertPSDCompression(compression);
dirk6f205312014-01-31 22:25:10 +00001161 if (layer_info->channel_info[j].type == -1)
1162 layer_info->image->alpha_trait=BlendPixelTrait;
dirkd9795a12014-11-11 22:01:21 +00001163
dirk6f205312014-01-31 22:25:10 +00001164 status=ReadPSDChannel(layer_info->image,psd_info,layer_info,j,
1165 compression,exception);
dirkb41f0802013-12-26 16:47:58 +00001166
1167 if (status == MagickFalse)
1168 break;
1169 }
1170
dirk6f205312014-01-31 22:25:10 +00001171 if (status != MagickFalse)
dirkb41f0802013-12-26 16:47:58 +00001172 status=CorrectPSDOpacity(layer_info,exception);
1173
1174 if (status != MagickFalse && layer_info->image->colorspace == CMYKColorspace)
dirkd9795a12014-11-11 22:01:21 +00001175 (void) NegateCMYK(layer_info->image,exception);
dirkb41f0802013-12-26 16:47:58 +00001176
1177 return(status);
1178}
1179
dirk4d9863c2014-03-28 19:48:49 +00001180static MagickStatusType ReadPSDLayers(Image *image,const ImageInfo *image_info,
1181 const PSDInfo *psd_info,const MagickBooleanType skip_layers,
1182 ExceptionInfo *exception)
dirkb41f0802013-12-26 16:47:58 +00001183{
1184 char
1185 type[4];
cristy3ed852e2009-09-05 21:47:34 +00001186
cristy3ed852e2009-09-05 21:47:34 +00001187 LayerInfo
1188 *layer_info;
1189
cristy56ed31c2010-03-22 00:46:21 +00001190 MagickSizeType
cristy56ed31c2010-03-22 00:46:21 +00001191 size;
1192
dirkb41f0802013-12-26 16:47:58 +00001193 MagickStatusType
1194 status;
cristydede49f2010-08-20 20:26:26 +00001195
cristybb503372010-05-27 20:51:26 +00001196 register ssize_t
dirkb41f0802013-12-26 16:47:58 +00001197 i;
cristydc05fc22011-01-22 19:43:15 +00001198
cristy3ed852e2009-09-05 21:47:34 +00001199 ssize_t
cristydc05fc22011-01-22 19:43:15 +00001200 count,
1201 j,
dirkb41f0802013-12-26 16:47:58 +00001202 number_layers;
1203
1204 size=GetPSDSize(psd_info,image);
1205 if (size == 0)
1206 {
1207 size_t
1208 quantum;
1209
1210 /*
1211 Skip layers & masks.
1212 */
1213 quantum=psd_info->version == 1 ? 4UL : 8UL;
1214 (void) ReadBlobMSBLong(image);
1215 count=ReadBlob(image,4,(unsigned char *) type);
1216 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1217 {
dirk419e5ad2014-12-16 23:14:58 +00001218 if (DiscardBlobBytes(image,(MagickSizeType) (size-quantum-8)) ==
1219 MagickFalse)
1220 ThrowBinaryException(CorruptImageError,"UnexpectedEndOfFile",
1221 image->filename);
dirkb41f0802013-12-26 16:47:58 +00001222 }
1223 else
1224 {
1225 count=ReadBlob(image,4,(unsigned char *) type);
1226 if ((count != 0) && (LocaleNCompare(type,"Lr16",4) == 0))
1227 size=GetPSDSize(psd_info,image);
1228 else
cristyaf29e112015-01-21 22:56:27 +00001229 if (DiscardBlobBytes(image,(MagickSizeType) (size-quantum-12UL)) ==
dirk419e5ad2014-12-16 23:14:58 +00001230 MagickFalse)
1231 ThrowBinaryException(CorruptImageError,"UnexpectedEndOfFile",
1232 image->filename);
dirkb41f0802013-12-26 16:47:58 +00001233 }
1234 }
dirkb41f0802013-12-26 16:47:58 +00001235 status=MagickTrue;
1236 if (size != 0)
1237 {
1238 layer_info=(LayerInfo *) NULL;
1239 number_layers=(short) ReadBlobMSBShort(image);
1240
dirkb41f0802013-12-26 16:47:58 +00001241 if (number_layers < 0)
1242 {
1243 /*
dirk6f205312014-01-31 22:25:10 +00001244 The first alpha channel in the merged result contains the
1245 transparency data for the merged result.
dirkb41f0802013-12-26 16:47:58 +00001246 */
1247 number_layers=MagickAbsoluteValue(number_layers);
1248 if (image->debug != MagickFalse)
1249 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1250 " negative layer count corrected for");
dirk6f205312014-01-31 22:25:10 +00001251 image->alpha_trait=BlendPixelTrait;
dirkb41f0802013-12-26 16:47:58 +00001252 }
1253
dirkfcaec622014-01-31 23:15:51 +00001254 if (skip_layers != MagickFalse)
1255 return(MagickTrue);
1256
dirkb41f0802013-12-26 16:47:58 +00001257 if (image->debug != MagickFalse)
1258 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1259 " image contains %.20g layers",(double) number_layers);
1260
1261 if (number_layers == 0)
dirk419e5ad2014-12-16 23:14:58 +00001262 ThrowBinaryException(CorruptImageError,"InvalidNumberOfLayers",
1263 image->filename);
dirkb41f0802013-12-26 16:47:58 +00001264
1265 layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers,
1266 sizeof(*layer_info));
1267 if (layer_info == (LayerInfo *) NULL)
1268 {
1269 if (image->debug != MagickFalse)
1270 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1271 " allocation of LayerInfo failed");
1272 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1273 image->filename);
1274 }
1275 (void) ResetMagickMemory(layer_info,0,(size_t) number_layers*
1276 sizeof(*layer_info));
1277
1278 for (i=0; i < number_layers; i++)
1279 {
1280 int
1281 x,
1282 y;
1283
1284 if (image->debug != MagickFalse)
1285 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1286 " reading layer #%.20g",(double) i+1);
1287 layer_info[i].page.y=(int) ReadBlobMSBLong(image);
1288 layer_info[i].page.x=(int) ReadBlobMSBLong(image);
1289 y=(int) ReadBlobMSBLong(image);
1290 x=(int) ReadBlobMSBLong(image);
1291 layer_info[i].page.width=(ssize_t) (x-layer_info[i].page.x);
1292 layer_info[i].page.height=(ssize_t) (y-layer_info[i].page.y);
1293 layer_info[i].channels=ReadBlobMSBShort(image);
1294 if (layer_info[i].channels > MaxPSDChannels)
1295 {
dirkbd8bd852013-12-28 22:55:19 +00001296 layer_info=DestroyLayerInfo(layer_info,number_layers);
dirkb41f0802013-12-26 16:47:58 +00001297 ThrowBinaryException(CorruptImageError,"MaximumChannelsExceeded",
dirk419e5ad2014-12-16 23:14:58 +00001298 image->filename);
dirkb41f0802013-12-26 16:47:58 +00001299 }
1300 if (image->debug != MagickFalse)
1301 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1302 " offset(%.20g,%.20g), size(%.20g,%.20g), channels=%.20g",
1303 (double) layer_info[i].page.x,(double) layer_info[i].page.y,
1304 (double) layer_info[i].page.height,(double)
1305 layer_info[i].page.width,(double) layer_info[i].channels);
1306 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1307 {
1308 layer_info[i].channel_info[j].type=(short) ReadBlobMSBShort(image);
1309 layer_info[i].channel_info[j].size=(size_t) GetPSDSize(psd_info,
1310 image);
1311 if (image->debug != MagickFalse)
1312 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1313 " channel[%.20g]: type=%.20g, size=%.20g",(double) j,
1314 (double) layer_info[i].channel_info[j].type,
1315 (double) layer_info[i].channel_info[j].size);
1316 }
1317 count=ReadBlob(image,4,(unsigned char *) type);
1318 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1319 {
1320 if (image->debug != MagickFalse)
1321 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1322 " layer type was %.4s instead of 8BIM", type);
dirkbd8bd852013-12-28 22:55:19 +00001323 layer_info=DestroyLayerInfo(layer_info,number_layers);
dirkb41f0802013-12-26 16:47:58 +00001324 ThrowBinaryException(CorruptImageError,"ImproperImageHeader",
1325 image->filename);
1326 }
1327 count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey);
dirkb52a4b72013-12-26 16:55:20 +00001328 layer_info[i].opacity=(Quantum) ScaleCharToQuantum((unsigned char)
cristyd8083a62014-02-01 13:53:44 +00001329 ReadBlobByte(image));
dirkb41f0802013-12-26 16:47:58 +00001330 layer_info[i].clipping=(unsigned char) ReadBlobByte(image);
1331 layer_info[i].flags=(unsigned char) ReadBlobByte(image);
1332 layer_info[i].visible=!(layer_info[i].flags & 0x02);
1333 if (image->debug != MagickFalse)
1334 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1335 " blend=%.4s, opacity=%.20g, clipping=%s, flags=%d, visible=%s",
1336 layer_info[i].blendkey,(double) layer_info[i].opacity,
1337 layer_info[i].clipping ? "true" : "false",layer_info[i].flags,
1338 layer_info[i].visible ? "true" : "false");
1339 (void) ReadBlobByte(image); /* filler */
1340
1341 size=ReadBlobMSBLong(image);
1342 if (size != 0)
1343 {
1344 MagickSizeType
1345 combined_length,
1346 length;
1347
1348 if (image->debug != MagickFalse)
1349 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1350 " layer contains additional info");
1351 length=ReadBlobMSBLong(image);
1352 combined_length=length+4;
1353 if (length != 0)
1354 {
1355 /*
1356 Layer mask info.
1357 */
1358 layer_info[i].mask.y=(int) ReadBlobMSBLong(image);
1359 layer_info[i].mask.x=(int) ReadBlobMSBLong(image);
1360 layer_info[i].mask.height=(size_t)
1361 (ReadBlobMSBLong(image)-layer_info[i].mask.y);
1362 layer_info[i].mask.width=(size_t)
1363 (ReadBlobMSBLong(image)-layer_info[i].mask.x);
1364 if (image->debug != MagickFalse)
1365 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1366 " layer mask: offset(%.20g,%.20g), size(%.20g,%.20g), length=%.20g",
1367 (double) layer_info[i].mask.x,(double)
1368 layer_info[i].mask.y,(double) layer_info[i].mask.width,
1369 (double) layer_info[i].mask.height,(double)
1370 ((MagickOffsetType) length)-16);
1371 /*
1372 Skip over the rest of the layer mask information.
1373 */
cristy1b875d22014-05-28 14:27:23 +00001374 if (DiscardBlobBytes(image,(MagickSizeType) (length-16)) == MagickFalse)
dirkb41f0802013-12-26 16:47:58 +00001375 {
dirkbd8bd852013-12-28 22:55:19 +00001376 layer_info=DestroyLayerInfo(layer_info,number_layers);
dirk419e5ad2014-12-16 23:14:58 +00001377 ThrowBinaryException(CorruptImageError,"UnexpectedEndOfFile",
1378 image->filename);
dirkb41f0802013-12-26 16:47:58 +00001379 }
1380 }
1381 length=ReadBlobMSBLong(image);
1382 combined_length+=length+4;
1383 if (length != 0)
1384 {
1385 /*
1386 Layer blending ranges info.
1387 */
1388 if (image->debug != MagickFalse)
1389 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1390 " layer blending ranges: length=%.20g",(double)
1391 ((MagickOffsetType) length));
1392 /*
1393 We read it, but don't use it...
1394 */
1395 for (j=0; j < (ssize_t) (length); j+=8)
1396 {
1397 size_t blend_source=ReadBlobMSBLong(image);
1398 size_t blend_dest=ReadBlobMSBLong(image);
1399 if (image->debug != MagickFalse)
1400 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1401 " source(%x), dest(%x)",(unsigned int)
1402 blend_source,(unsigned int) blend_dest);
1403 }
1404 }
1405 /*
1406 Layer name.
1407 */
1408 length=(size_t) ReadBlobByte(image);
1409 combined_length+=length+1;
1410 for (j=0; j < (ssize_t) length; j++)
1411 layer_info[i].name[j]=(unsigned char) ReadBlobByte(image);
1412 layer_info[i].name[j]='\0';
1413 if (image->debug != MagickFalse)
1414 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1415 " layer name: %s",layer_info[i].name);
1416 /*
1417 Skip the rest of the variable data until we support it.
1418 */
1419 if (image->debug != MagickFalse)
1420 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1421 " unsupported data: length=%.20g",(double)
1422 ((MagickOffsetType) (size-combined_length)));
cristy1b875d22014-05-28 14:27:23 +00001423 if (DiscardBlobBytes(image,(MagickSizeType) (size-combined_length)) == MagickFalse)
dirkb41f0802013-12-26 16:47:58 +00001424 {
dirkbd8bd852013-12-28 22:55:19 +00001425 layer_info=DestroyLayerInfo(layer_info,number_layers);
dirkb41f0802013-12-26 16:47:58 +00001426 ThrowBinaryException(CorruptImageError,
1427 "UnexpectedEndOfFile",image->filename);
1428 }
1429 }
1430 }
1431
1432 for (i=0; i < number_layers; i++)
1433 {
dirkb41f0802013-12-26 16:47:58 +00001434 if ((layer_info[i].page.width == 0) ||
1435 (layer_info[i].page.height == 0))
1436 {
1437 if (image->debug != MagickFalse)
1438 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1439 " layer data is empty");
1440 continue;
1441 }
1442
dirkb41f0802013-12-26 16:47:58 +00001443 /*
1444 Allocate layered image.
1445 */
1446 layer_info[i].image=CloneImage(image,layer_info[i].page.width,
1447 layer_info[i].page.height,MagickFalse,exception);
1448 if (layer_info[i].image == (Image *) NULL)
1449 {
dirkbd8bd852013-12-28 22:55:19 +00001450 layer_info=DestroyLayerInfo(layer_info,number_layers);
dirkb41f0802013-12-26 16:47:58 +00001451 if (image->debug != MagickFalse)
1452 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1453 " allocation of image for layer %.20g failed",(double) i);
1454 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1455 image->filename);
1456 }
1457 }
1458
dirk4d9863c2014-03-28 19:48:49 +00001459 if (image_info->ping == MagickFalse)
dirkb41f0802013-12-26 16:47:58 +00001460 {
dirk4d9863c2014-03-28 19:48:49 +00001461 for (i=0; i < number_layers; i++)
dirkb41f0802013-12-26 16:47:58 +00001462 {
dirk4d9863c2014-03-28 19:48:49 +00001463 if (layer_info[i].image == (Image *) NULL)
dirkb41f0802013-12-26 16:47:58 +00001464 {
dirk4d9863c2014-03-28 19:48:49 +00001465 for (j=0; j < layer_info[i].channels; j++)
1466 {
cristy1b875d22014-05-28 14:27:23 +00001467 if (DiscardBlobBytes(image,(MagickSizeType)
dirk4d9863c2014-03-28 19:48:49 +00001468 layer_info[i].channel_info[j].size) == MagickFalse)
1469 {
1470 layer_info=DestroyLayerInfo(layer_info,number_layers);
1471 ThrowBinaryException(CorruptImageError,
1472 "UnexpectedEndOfFile",image->filename);
1473 }
1474 }
1475 continue;
dirkb41f0802013-12-26 16:47:58 +00001476 }
dirk4d9863c2014-03-28 19:48:49 +00001477
1478 if (image->debug != MagickFalse)
1479 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1480 " reading data for layer %.20g",(double) i);
1481
1482 status=ReadPSDLayer(image,psd_info,&layer_info[i],exception);
1483 if (status == MagickFalse)
1484 break;
1485
1486 status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType)
1487 number_layers);
1488 if (status == MagickFalse)
1489 break;
dirkb41f0802013-12-26 16:47:58 +00001490 }
dirkb41f0802013-12-26 16:47:58 +00001491 }
1492
dirkb41f0802013-12-26 16:47:58 +00001493 if (status != MagickFalse)
1494 {
1495 for (i=0; i < number_layers; i++)
1496 {
1497 if (layer_info[i].image == (Image *) NULL)
1498 {
1499 for (j=i; j < number_layers - 1; j++)
1500 layer_info[j] = layer_info[j+1];
1501 number_layers--;
1502 i--;
1503 }
1504 }
1505
1506 if (number_layers > 0)
1507 {
1508 for (i=0; i < number_layers; i++)
1509 {
1510 if (i > 0)
1511 layer_info[i].image->previous=layer_info[i-1].image;
1512 if (i < (number_layers-1))
1513 layer_info[i].image->next=layer_info[i+1].image;
1514 layer_info[i].image->page=layer_info[i].page;
1515 }
1516 image->next=layer_info[0].image;
1517 layer_info[0].image->previous=image;
1518 }
1519 }
1520 layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info);
1521 }
1522
1523 return(status);
1524}
1525
dirk4d9863c2014-03-28 19:48:49 +00001526static MagickStatusType ReadPSDMergedImage(Image* image,
1527 const PSDInfo* psd_info,ExceptionInfo *exception)
dirkb41f0802013-12-26 16:47:58 +00001528{
1529 MagickOffsetType
1530 *offsets;
1531
1532 MagickStatusType
1533 status;
1534
1535 PSDCompressionType
1536 compression;
1537
1538 register ssize_t
1539 i;
1540
1541 compression=(PSDCompressionType) ReadBlobMSBShort(image);
1542 image->compression=ConvertPSDCompression(compression);
1543
1544 if (compression != Raw && compression != RLE)
1545 {
1546 (void) ThrowMagickException(exception,GetMagickModule(),
1547 TypeWarning,"CompressionNotSupported","'%.20g'",(double) compression);
1548 return(MagickFalse);
1549 }
1550
1551 offsets=(MagickOffsetType *) NULL;
1552 if (compression == RLE)
1553 {
1554 offsets=ReadPSDRLEOffsets(image,psd_info,image->rows*psd_info->channels);
1555 if (offsets == (MagickOffsetType *) NULL)
1556 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1557 image->filename);
1558 }
1559
1560 status=MagickTrue;
1561 for (i=0; i < (ssize_t) psd_info->channels; i++)
1562 {
1563 if (compression == RLE)
1564 status=ReadPSDChannelRLE(image,psd_info,i,offsets+(i*image->rows),
1565 exception);
1566 else
1567 status=ReadPSDChannelRaw(image,psd_info->channels,i,exception);
1568
1569 if (status == MagickFalse)
1570 break;
1571 status=SetImageProgress(image,LoadImagesTag,i,psd_info->channels);
1572 if (status == MagickFalse)
1573 break;
1574 }
1575
1576 if (image->colorspace == CMYKColorspace)
dirkd9795a12014-11-11 22:01:21 +00001577 (void) NegateCMYK(image,exception);
dirkb41f0802013-12-26 16:47:58 +00001578
1579 if (offsets != (MagickOffsetType *) NULL)
1580 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1581
1582 return(status);
1583}
1584
cristy90de83d2015-04-08 22:04:41 +00001585static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception)
dirkb41f0802013-12-26 16:47:58 +00001586{
1587 Image
1588 *image;
1589
1590 MagickBooleanType
dirk18c0e4d2014-02-22 22:24:05 +00001591 has_merged_image,
dirkb41f0802013-12-26 16:47:58 +00001592 skip_layers,
1593 status;
1594
1595 MagickOffsetType
1596 offset;
1597
1598 MagickSizeType
1599 length;
1600
1601 PSDInfo
1602 psd_info;
1603
1604 register ssize_t
1605 i;
1606
1607 ssize_t
1608 count;
cristy3ed852e2009-09-05 21:47:34 +00001609
cristy3ed852e2009-09-05 21:47:34 +00001610 unsigned char
1611 *data;
1612
cristy3ed852e2009-09-05 21:47:34 +00001613 /*
1614 Open image file.
1615 */
1616 assert(image_info != (const ImageInfo *) NULL);
1617 assert(image_info->signature == MagickSignature);
1618 if (image_info->debug != MagickFalse)
1619 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1620 image_info->filename);
1621 assert(exception != (ExceptionInfo *) NULL);
1622 assert(exception->signature == MagickSignature);
dirkb41f0802013-12-26 16:47:58 +00001623
cristy9950d572011-10-01 18:22:35 +00001624 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001625 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1626 if (status == MagickFalse)
1627 {
1628 image=DestroyImageList(image);
1629 return((Image *) NULL);
1630 }
1631 /*
1632 Read image header.
1633 */
1634 count=ReadBlob(image,4,(unsigned char *) psd_info.signature);
1635 psd_info.version=ReadBlobMSBShort(image);
cristy50aea4a2010-03-09 17:37:44 +00001636 if ((count == 0) || (LocaleNCompare(psd_info.signature,"8BPS",4) != 0) ||
cristy2d3d87f2010-03-01 00:23:08 +00001637 ((psd_info.version != 1) && (psd_info.version != 2)))
cristy3ed852e2009-09-05 21:47:34 +00001638 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1639 count=ReadBlob(image,6,psd_info.reserved);
1640 psd_info.channels=ReadBlobMSBShort(image);
1641 if (psd_info.channels > MaxPSDChannels)
1642 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
1643 psd_info.rows=ReadBlobMSBLong(image);
1644 psd_info.columns=ReadBlobMSBLong(image);
cristy2d3d87f2010-03-01 00:23:08 +00001645 if ((psd_info.version == 1) && ((psd_info.rows > 30000) ||
1646 (psd_info.columns > 30000)))
1647 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +00001648 psd_info.depth=ReadBlobMSBShort(image);
cristy2d3d87f2010-03-01 00:23:08 +00001649 if ((psd_info.depth != 1) && (psd_info.depth != 8) && (psd_info.depth != 16))
1650 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +00001651 psd_info.mode=ReadBlobMSBShort(image);
1652 if (image->debug != MagickFalse)
1653 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001654 " Image is %.20g x %.20g with channels=%.20g, depth=%.20g, mode=%s",
1655 (double) psd_info.columns,(double) psd_info.rows,(double)
1656 psd_info.channels,(double) psd_info.depth,ModeToString((PSDImageType)
1657 psd_info.mode));
cristy3ed852e2009-09-05 21:47:34 +00001658 /*
1659 Initialize image.
1660 */
1661 image->depth=psd_info.depth;
1662 image->columns=psd_info.columns;
1663 image->rows=psd_info.rows;
cristyacabb842014-12-14 23:36:33 +00001664 status=SetImageExtent(image,image->columns,image->rows,exception);
1665 if (status == MagickFalse)
1666 return(DestroyImageList(image));
cristyea1a8aa2011-10-20 13:24:06 +00001667 if (SetImageBackgroundColor(image,exception) == MagickFalse)
cristy95524f92010-02-16 18:44:34 +00001668 {
cristy95524f92010-02-16 18:44:34 +00001669 image=DestroyImageList(image);
1670 return((Image *) NULL);
1671 }
cristy3ed852e2009-09-05 21:47:34 +00001672 if (psd_info.mode == LabMode)
cristye2c4f182012-05-12 14:11:53 +00001673 SetImageColorspace(image,LabColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001674 if (psd_info.mode == CMYKMode)
dirk84affaa2014-05-27 11:09:16 +00001675 {
1676 SetImageColorspace(image,CMYKColorspace,exception);
dirkdeca4042014-05-29 06:31:05 +00001677 image->alpha_trait=psd_info.channels > 4 ? BlendPixelTrait :
dirk84affaa2014-05-27 11:09:16 +00001678 UndefinedPixelTrait;
1679 }
1680 else if ((psd_info.mode == BitmapMode) || (psd_info.mode == GrayscaleMode) ||
cristy3ed852e2009-09-05 21:47:34 +00001681 (psd_info.mode == DuotoneMode))
1682 {
cristyafd0fd12013-09-17 12:41:18 +00001683 status=AcquireImageColormap(image,psd_info.depth != 16 ? 256 : 65536,
1684 exception);
1685 if (status == MagickFalse)
cristy52cf7f12010-02-07 18:07:56 +00001686 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00001687 if (image->debug != MagickFalse)
1688 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristydede49f2010-08-20 20:26:26 +00001689 " Image colormap allocated");
cristye2c4f182012-05-12 14:11:53 +00001690 SetImageColorspace(image,GRAYColorspace,exception);
dirkdeca4042014-05-29 06:31:05 +00001691 image->alpha_trait=psd_info.channels > 1 ? BlendPixelTrait :
dirk84affaa2014-05-27 11:09:16 +00001692 UndefinedPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00001693 }
dirk84affaa2014-05-27 11:09:16 +00001694 else
dirkdeca4042014-05-29 06:31:05 +00001695 image->alpha_trait=psd_info.channels > 3 ? BlendPixelTrait :
dirk84affaa2014-05-27 11:09:16 +00001696 UndefinedPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00001697 /*
1698 Read PSD raster colormap only present for indexed and duotone images.
1699 */
1700 length=ReadBlobMSBLong(image);
1701 if (length != 0)
1702 {
1703 if (image->debug != MagickFalse)
1704 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1705 " reading colormap");
1706 if (psd_info.mode == DuotoneMode)
1707 {
1708 /*
1709 Duotone image data; the format of this data is undocumented.
1710 */
cristy56ed31c2010-03-22 00:46:21 +00001711 data=(unsigned char *) AcquireQuantumMemory((size_t) length,
1712 sizeof(*data));
cristy3ed852e2009-09-05 21:47:34 +00001713 if (data == (unsigned char *) NULL)
1714 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy56ed31c2010-03-22 00:46:21 +00001715 count=ReadBlob(image,(size_t) length,data);
cristy3ed852e2009-09-05 21:47:34 +00001716 data=(unsigned char *) RelinquishMagickMemory(data);
1717 }
1718 else
1719 {
cristy18a9b972014-12-02 23:47:21 +00001720 size_t
1721 number_colors;
1722
cristy3ed852e2009-09-05 21:47:34 +00001723 /*
1724 Read PSD raster colormap.
1725 */
cristy18a9b972014-12-02 23:47:21 +00001726 number_colors=length/3;
1727 if (number_colors > 65536)
1728 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1729 if (AcquireImageColormap(image,number_colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001730 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00001731 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001732 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
1733 ReadBlobByte(image));
cristybb503372010-05-27 20:51:26 +00001734 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001735 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
1736 ReadBlobByte(image));
cristybb503372010-05-27 20:51:26 +00001737 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001738 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
1739 ReadBlobByte(image));
cristy8a46d822012-08-28 23:32:39 +00001740 image->alpha_trait=UndefinedPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00001741 }
1742 }
dirk18c0e4d2014-02-22 22:24:05 +00001743 has_merged_image=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001744 length=ReadBlobMSBLong(image);
1745 if (length != 0)
1746 {
1747 unsigned char
1748 *blocks;
1749
1750 /*
1751 Image resources block.
1752 */
1753 if (image->debug != MagickFalse)
1754 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy2b9582a2011-07-04 17:38:56 +00001755 " reading image resource blocks - %.20g bytes",(double)
1756 ((MagickOffsetType) length));
dirk7ec89322014-12-16 22:50:15 +00001757 blocks=(unsigned char *) AcquireQuantumMemory((size_t) length,
cristy56ed31c2010-03-22 00:46:21 +00001758 sizeof(*blocks));
cristy3ed852e2009-09-05 21:47:34 +00001759 if (blocks == (unsigned char *) NULL)
1760 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy56ed31c2010-03-22 00:46:21 +00001761 count=ReadBlob(image,(size_t) length,blocks);
1762 if ((count != (ssize_t) length) ||
cristy3ed852e2009-09-05 21:47:34 +00001763 (LocaleNCompare((char *) blocks,"8BIM",4) != 0))
1764 {
1765 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
1766 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1767 }
dirk7ec89322014-12-16 22:50:15 +00001768 ParseImageResourceBlocks(image,blocks,(size_t) length,&has_merged_image,
1769 exception);
cristy3ed852e2009-09-05 21:47:34 +00001770 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
1771 }
1772 /*
cristy3ed852e2009-09-05 21:47:34 +00001773 Layer and mask block.
1774 */
cristy2d3d87f2010-03-01 00:23:08 +00001775 length=GetPSDSize(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +00001776 if (length == 8)
1777 {
1778 length=ReadBlobMSBLong(image);
1779 length=ReadBlobMSBLong(image);
1780 }
dirkb41f0802013-12-26 16:47:58 +00001781 offset=TellBlob(image);
1782 skip_layers=MagickFalse;
dirk18c0e4d2014-02-22 22:24:05 +00001783 if ((image_info->number_scenes == 1) && (image_info->scene == 0) &&
1784 (has_merged_image != MagickFalse))
cristyd4297022010-09-16 22:59:09 +00001785 {
cristy374b8ad2012-02-01 20:55:21 +00001786 if (image->debug != MagickFalse)
1787 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1788 " read composite only");
dirkb41f0802013-12-26 16:47:58 +00001789 skip_layers=MagickTrue;
cristyd4297022010-09-16 22:59:09 +00001790 }
cristy3ed852e2009-09-05 21:47:34 +00001791 if (length == 0)
1792 {
1793 if (image->debug != MagickFalse)
1794 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1795 " image has no layers");
1796 }
1797 else
1798 {
dirk4d9863c2014-03-28 19:48:49 +00001799 if (ReadPSDLayers(image,image_info,&psd_info,skip_layers,exception) !=
1800 MagickTrue)
dirk6f205312014-01-31 22:25:10 +00001801 {
1802 (void) CloseBlob(image);
dirk419e5ad2014-12-16 23:14:58 +00001803 image=DestroyImageList(image);
dirk6f205312014-01-31 22:25:10 +00001804 return((Image *) NULL);
1805 }
cristy3ed852e2009-09-05 21:47:34 +00001806
dirkb41f0802013-12-26 16:47:58 +00001807 /*
1808 Skip the rest of the layer and mask information.
1809 */
1810 SeekBlob(image,offset+length,SEEK_SET);
cristy3ed852e2009-09-05 21:47:34 +00001811 }
dirk4d9863c2014-03-28 19:48:49 +00001812 /*
cristy89e22602015-04-08 23:34:13 +00001813 If we are only "pinging" the image, then we're done - so return.
1814 */
1815 if (image_info->ping != MagickFalse)
1816 {
1817 (void) CloseBlob(image);
1818 return(GetFirstImageInList(image));
1819 }
1820 /*
dirkb41f0802013-12-26 16:47:58 +00001821 Read the precombined layer, present for PSD < 4 compatibility.
cristy3ed852e2009-09-05 21:47:34 +00001822 */
1823 if (image->debug != MagickFalse)
1824 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1825 " reading the precombined layer");
cristy90de83d2015-04-08 22:04:41 +00001826 if ((has_merged_image != MagickFalse) || (GetImageListLength(image) == 1))
dirk2de94f52014-03-15 21:36:56 +00001827 has_merged_image=(MagickBooleanType) ReadPSDMergedImage(image,&psd_info,
1828 exception);
cristy90de83d2015-04-08 22:04:41 +00001829 if ((has_merged_image == MagickFalse) && (GetImageListLength(image) == 1) &&
1830 (length != 0))
dirk2de94f52014-03-15 21:36:56 +00001831 {
cristyb3f97ae2015-05-18 12:29:32 +00001832 MagickStatusType
1833 status;
1834
dirk2de94f52014-03-15 21:36:56 +00001835 SeekBlob(image,offset,SEEK_SET);
cristy90de83d2015-04-08 22:04:41 +00001836 status=ReadPSDLayers(image,image_info,&psd_info,MagickFalse,exception);
1837 if (status != MagickTrue)
dirk2de94f52014-03-15 21:36:56 +00001838 {
1839 (void) CloseBlob(image);
1840 return((Image *) NULL);
1841 }
1842 }
cristy90de83d2015-04-08 22:04:41 +00001843 if ((has_merged_image == MagickFalse) && (GetImageListLength(image) > 1))
dirk18c0e4d2014-02-22 22:24:05 +00001844 {
1845 Image
1846 *merged;
1847
1848 SetImageAlphaChannel(image,TransparentAlphaChannel,exception);
1849 image->background_color.alpha=TransparentAlpha;
1850 merged=MergeImageLayers(image,FlattenLayer,exception);
1851 ReplaceImageInList(&image,merged);
1852 }
cristy3ed852e2009-09-05 21:47:34 +00001853 (void) CloseBlob(image);
1854 return(GetFirstImageInList(image));
1855}
1856
1857/*
1858%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1859% %
1860% %
1861% %
1862% R e g i s t e r P S D I m a g e %
1863% %
1864% %
1865% %
1866%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1867%
1868% RegisterPSDImage() adds properties for the PSD image format to
1869% the list of supported formats. The properties include the image format
1870% tag, a method to read and/or write the format, whether the format
1871% supports the saving of more than one frame to the same file or blob,
1872% whether the format supports native in-memory I/O, and a brief
1873% description of the format.
1874%
1875% The format of the RegisterPSDImage method is:
1876%
cristybb503372010-05-27 20:51:26 +00001877% size_t RegisterPSDImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001878%
1879*/
cristybb503372010-05-27 20:51:26 +00001880ModuleExport size_t RegisterPSDImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001881{
1882 MagickInfo
1883 *entry;
1884
dirk06b627a2015-04-06 18:59:17 +00001885 entry=AcquireMagickInfo("PSD","PSB","Adobe Large Document Format");
cristyb4233012010-02-28 20:09:14 +00001886 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1887 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1888 entry->magick=(IsImageFormatHandler *) IsPSD;
dirk08e9a112015-02-22 01:51:41 +00001889 entry->flags|=CoderSeekableStreamFlag;
cristyb4233012010-02-28 20:09:14 +00001890 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +00001891 entry=AcquireMagickInfo("PSD","PSD","Adobe Photoshop bitmap");
cristy3ed852e2009-09-05 21:47:34 +00001892 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1893 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1894 entry->magick=(IsImageFormatHandler *) IsPSD;
dirk08e9a112015-02-22 01:51:41 +00001895 entry->flags|=CoderSeekableStreamFlag;
cristy3ed852e2009-09-05 21:47:34 +00001896 (void) RegisterMagickInfo(entry);
1897 return(MagickImageCoderSignature);
1898}
1899
1900/*
1901%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1902% %
1903% %
1904% %
1905% U n r e g i s t e r P S D I m a g e %
1906% %
1907% %
1908% %
1909%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1910%
1911% UnregisterPSDImage() removes format registrations made by the
1912% PSD module from the list of supported formats.
1913%
1914% The format of the UnregisterPSDImage method is:
1915%
1916% UnregisterPSDImage(void)
1917%
1918*/
1919ModuleExport void UnregisterPSDImage(void)
1920{
cristyb4233012010-02-28 20:09:14 +00001921 (void) UnregisterMagickInfo("PSB");
cristy3ed852e2009-09-05 21:47:34 +00001922 (void) UnregisterMagickInfo("PSD");
1923}
1924
1925/*
1926%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1927% %
1928% %
1929% %
1930% W r i t e P S D I m a g e %
1931% %
1932% %
1933% %
1934%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1935%
cristyb1459bc2010-03-23 21:41:43 +00001936% WritePSDImage() writes an image in the Adobe Photoshop encoded image format.
cristy3ed852e2009-09-05 21:47:34 +00001937%
1938% The format of the WritePSDImage method is:
1939%
dirk1ab941f2014-02-01 17:35:11 +00001940% MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
1941% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001942%
1943% A description of each parameter follows.
1944%
1945% o image_info: the image info.
1946%
1947% o image: The image.
1948%
cristy3a37efd2011-08-28 20:31:03 +00001949% o exception: return any errors or warnings in this structure.
1950%
cristy3ed852e2009-09-05 21:47:34 +00001951*/
1952
cristy50aea4a2010-03-09 17:37:44 +00001953static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image,
cristyf0460ee2010-03-06 02:55:11 +00001954 const size_t offset)
1955{
1956 if (psd_info->version == 1)
cristy56ed31c2010-03-22 00:46:21 +00001957 return(WriteBlobMSBShort(image,(unsigned short) offset));
1958 return(WriteBlobMSBLong(image,(unsigned short) offset));
cristyf0460ee2010-03-06 02:55:11 +00001959}
1960
cristy50aea4a2010-03-09 17:37:44 +00001961static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image,
cristyf0460ee2010-03-06 02:55:11 +00001962 const MagickSizeType size)
1963{
1964 if (psd_info->version == 1)
cristy56ed31c2010-03-22 00:46:21 +00001965 return(WriteBlobMSBLong(image,(unsigned int) size));
cristyf0460ee2010-03-06 02:55:11 +00001966 return(WriteBlobMSBLongLong(image,size));
1967}
1968
cristy4aff1572010-02-15 13:34:01 +00001969static size_t PSDPackbitsEncodeImage(Image *image,const size_t length,
cristy018f07f2011-09-04 21:15:19 +00001970 const unsigned char *pixels,unsigned char *compact_pixels,
1971 ExceptionInfo *exception)
cristy4aff1572010-02-15 13:34:01 +00001972{
1973 int
1974 count;
1975
cristybb503372010-05-27 20:51:26 +00001976 register ssize_t
cristy4aff1572010-02-15 13:34:01 +00001977 i,
1978 j;
1979
1980 register unsigned char
1981 *q;
1982
1983 unsigned char
1984 *packbits;
1985
1986 /*
1987 Compress pixels with Packbits encoding.
1988 */
1989 assert(image != (Image *) NULL);
1990 assert(image->signature == MagickSignature);
1991 if (image->debug != MagickFalse)
1992 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1993 assert(pixels != (unsigned char *) NULL);
1994 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1995 if (packbits == (unsigned char *) NULL)
1996 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1997 image->filename);
cristyb1459bc2010-03-23 21:41:43 +00001998 q=compact_pixels;
cristybb503372010-05-27 20:51:26 +00001999 for (i=(ssize_t) length; i != 0; )
cristy4aff1572010-02-15 13:34:01 +00002000 {
2001 switch (i)
2002 {
2003 case 1:
2004 {
2005 i--;
2006 *q++=(unsigned char) 0;
2007 *q++=(*pixels);
2008 break;
2009 }
2010 case 2:
2011 {
2012 i-=2;
2013 *q++=(unsigned char) 1;
2014 *q++=(*pixels);
2015 *q++=pixels[1];
2016 break;
2017 }
2018 case 3:
2019 {
2020 i-=3;
2021 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
2022 {
2023 *q++=(unsigned char) ((256-3)+1);
2024 *q++=(*pixels);
2025 break;
2026 }
2027 *q++=(unsigned char) 2;
2028 *q++=(*pixels);
2029 *q++=pixels[1];
2030 *q++=pixels[2];
2031 break;
2032 }
2033 default:
2034 {
2035 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
2036 {
2037 /*
2038 Packed run.
2039 */
2040 count=3;
cristybb503372010-05-27 20:51:26 +00002041 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
cristy4aff1572010-02-15 13:34:01 +00002042 {
2043 count++;
2044 if (count >= 127)
2045 break;
2046 }
2047 i-=count;
2048 *q++=(unsigned char) ((256-count)+1);
2049 *q++=(*pixels);
2050 pixels+=count;
2051 break;
2052 }
2053 /*
2054 Literal run.
2055 */
2056 count=0;
2057 while ((*(pixels+count) != *(pixels+count+1)) ||
2058 (*(pixels+count+1) != *(pixels+count+2)))
2059 {
2060 packbits[count+1]=pixels[count];
2061 count++;
cristybb503372010-05-27 20:51:26 +00002062 if (((ssize_t) count >= (i-3)) || (count >= 127))
cristy4aff1572010-02-15 13:34:01 +00002063 break;
2064 }
2065 i-=count;
2066 *packbits=(unsigned char) (count-1);
cristybb503372010-05-27 20:51:26 +00002067 for (j=0; j <= (ssize_t) count; j++)
cristy4aff1572010-02-15 13:34:01 +00002068 *q++=packbits[j];
2069 pixels+=count;
2070 break;
2071 }
2072 }
2073 }
2074 *q++=(unsigned char) 128; /* EOD marker */
2075 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
cristyb1459bc2010-03-23 21:41:43 +00002076 return((size_t) (q-compact_pixels));
cristy4aff1572010-02-15 13:34:01 +00002077}
2078
cristy875e28a2010-03-06 19:46:55 +00002079static void WritePackbitsLength(const PSDInfo *psd_info,
cristya20214e2010-08-28 16:52:13 +00002080 const ImageInfo *image_info,Image *image,Image *next_image,
cristy018f07f2011-09-04 21:15:19 +00002081 unsigned char *compact_pixels,const QuantumType quantum_type,
2082 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002083{
cristy3ed852e2009-09-05 21:47:34 +00002084 QuantumInfo
2085 *quantum_info;
2086
cristy4c08aed2011-07-01 19:47:50 +00002087 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00002088 *p;
2089
2090 size_t
cristy4aff1572010-02-15 13:34:01 +00002091 length,
cristy3ed852e2009-09-05 21:47:34 +00002092 packet_size;
2093
cristy75f85ae2010-09-25 03:01:06 +00002094 ssize_t
2095 y;
2096
cristya20214e2010-08-28 16:52:13 +00002097 unsigned char
2098 *pixels;
2099
2100 if (next_image->depth > 8)
2101 next_image->depth=16;
2102 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
cristyda16f162011-02-19 23:52:17 +00002103 (void) packet_size;
cristy5f766ef2014-12-14 21:12:47 +00002104 quantum_info=AcquireQuantumInfo(image_info,image);
cristyb3f97ae2015-05-18 12:29:32 +00002105 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
cristya20214e2010-08-28 16:52:13 +00002106 for (y=0; y < (ssize_t) next_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002107 {
cristyc82a27b2011-10-21 01:07:16 +00002108 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002109 if (p == (const Quantum *) NULL)
cristy4aff1572010-02-15 13:34:01 +00002110 break;
cristya20214e2010-08-28 16:52:13 +00002111 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
cristyc82a27b2011-10-21 01:07:16 +00002112 quantum_type,pixels,exception);
cristy018f07f2011-09-04 21:15:19 +00002113 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
2114 exception);
cristy875e28a2010-03-06 19:46:55 +00002115 (void) SetPSDOffset(psd_info,image,length);
cristy4aff1572010-02-15 13:34:01 +00002116 }
2117 quantum_info=DestroyQuantumInfo(quantum_info);
2118}
2119
cristy875e28a2010-03-06 19:46:55 +00002120static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info,
cristya20214e2010-08-28 16:52:13 +00002121 Image *image,Image *next_image,unsigned char *compact_pixels,
cristy018f07f2011-09-04 21:15:19 +00002122 const QuantumType quantum_type,const MagickBooleanType compression_flag,
2123 ExceptionInfo *exception)
cristy4aff1572010-02-15 13:34:01 +00002124{
2125 int
2126 y;
2127
cristy0910f242010-04-01 18:55:09 +00002128 MagickBooleanType
2129 monochrome;
2130
cristy4aff1572010-02-15 13:34:01 +00002131 QuantumInfo
2132 *quantum_info;
2133
cristy4c08aed2011-07-01 19:47:50 +00002134 register const Quantum
cristy4aff1572010-02-15 13:34:01 +00002135 *p;
2136
cristybb503372010-05-27 20:51:26 +00002137 register ssize_t
cristy0910f242010-04-01 18:55:09 +00002138 i;
2139
cristy4aff1572010-02-15 13:34:01 +00002140 size_t
2141 length,
2142 packet_size;
2143
cristya20214e2010-08-28 16:52:13 +00002144 unsigned char
2145 *pixels;
2146
cristy50aea4a2010-03-09 17:37:44 +00002147 (void) psd_info;
cristy4aff1572010-02-15 13:34:01 +00002148 if ((compression_flag != MagickFalse) &&
cristya20214e2010-08-28 16:52:13 +00002149 (next_image->compression != RLECompression))
cristy4aff1572010-02-15 13:34:01 +00002150 (void) WriteBlobMSBShort(image,0);
cristya20214e2010-08-28 16:52:13 +00002151 if (next_image->depth > 8)
2152 next_image->depth=16;
dirka7014c82015-04-06 08:43:28 +00002153 monochrome=IsImageMonochrome(image) && (image->depth == 1) ?
cristyc82a27b2011-10-21 01:07:16 +00002154 MagickTrue : MagickFalse;
cristya20214e2010-08-28 16:52:13 +00002155 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
cristyda16f162011-02-19 23:52:17 +00002156 (void) packet_size;
cristy5f766ef2014-12-14 21:12:47 +00002157 quantum_info=AcquireQuantumInfo(image_info,image);
cristyb3f97ae2015-05-18 12:29:32 +00002158 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
cristya20214e2010-08-28 16:52:13 +00002159 for (y=0; y < (ssize_t) next_image->rows; y++)
cristy4aff1572010-02-15 13:34:01 +00002160 {
cristyc82a27b2011-10-21 01:07:16 +00002161 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002162 if (p == (const Quantum *) NULL)
cristy4aff1572010-02-15 13:34:01 +00002163 break;
cristya20214e2010-08-28 16:52:13 +00002164 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
cristyc82a27b2011-10-21 01:07:16 +00002165 quantum_type,pixels,exception);
cristy0910f242010-04-01 18:55:09 +00002166 if (monochrome != MagickFalse)
cristybb503372010-05-27 20:51:26 +00002167 for (i=0; i < (ssize_t) length; i++)
cristy0910f242010-04-01 18:55:09 +00002168 pixels[i]=(~pixels[i]);
cristya20214e2010-08-28 16:52:13 +00002169 if (next_image->compression != RLECompression)
cristy4aff1572010-02-15 13:34:01 +00002170 (void) WriteBlob(image,length,pixels);
2171 else
2172 {
cristy018f07f2011-09-04 21:15:19 +00002173 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
2174 exception);
cristyb1459bc2010-03-23 21:41:43 +00002175 (void) WriteBlob(image,length,compact_pixels);
cristy4aff1572010-02-15 13:34:01 +00002176 }
cristy3ed852e2009-09-05 21:47:34 +00002177 }
2178 quantum_info=DestroyQuantumInfo(quantum_info);
2179}
2180
cristy875e28a2010-03-06 19:46:55 +00002181static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info,
cristya20214e2010-08-28 16:52:13 +00002182 const ImageInfo *image_info,Image *image,Image *next_image,
cristy018f07f2011-09-04 21:15:19 +00002183 const MagickBooleanType separate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002184{
cristy3ed852e2009-09-05 21:47:34 +00002185 size_t
2186 channels,
2187 packet_size;
2188
2189 unsigned char
cristya20214e2010-08-28 16:52:13 +00002190 *compact_pixels;
cristy3ed852e2009-09-05 21:47:34 +00002191
2192 /*
cristy875e28a2010-03-06 19:46:55 +00002193 Write uncompressed pixels as separate planes.
cristy3ed852e2009-09-05 21:47:34 +00002194 */
2195 channels=1;
cristya20214e2010-08-28 16:52:13 +00002196 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
2197 compact_pixels=(unsigned char *) NULL;
2198 if (next_image->compression == RLECompression)
cristy4aff1572010-02-15 13:34:01 +00002199 {
cristya20214e2010-08-28 16:52:13 +00002200 compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels*
2201 next_image->columns,packet_size*sizeof(*compact_pixels));
2202 if (compact_pixels == (unsigned char *) NULL)
2203 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy4aff1572010-02-15 13:34:01 +00002204 }
dirka7014c82015-04-06 08:43:28 +00002205 if (IsImageGray(next_image) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002206 {
cristya20214e2010-08-28 16:52:13 +00002207 if (next_image->compression == RLECompression)
cristy4aff1572010-02-15 13:34:01 +00002208 {
2209 /*
2210 Packbits compression.
2211 */
2212 (void) WriteBlobMSBShort(image,1);
cristya20214e2010-08-28 16:52:13 +00002213 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002214 compact_pixels,GrayQuantum,exception);
cristy17f11b02014-12-20 19:37:04 +00002215 if (next_image->alpha_trait != UndefinedPixelTrait)
cristya20214e2010-08-28 16:52:13 +00002216 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002217 compact_pixels,AlphaQuantum,exception);
cristy4aff1572010-02-15 13:34:01 +00002218 }
cristya20214e2010-08-28 16:52:13 +00002219 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
dirkc9e9ea32015-01-24 11:21:18 +00002220 GrayQuantum,MagickTrue,exception);
cristy17f11b02014-12-20 19:37:04 +00002221 if (next_image->alpha_trait != UndefinedPixelTrait)
cristya20214e2010-08-28 16:52:13 +00002222 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
dirkc9e9ea32015-01-24 11:21:18 +00002223 AlphaQuantum,separate,exception);
cristy6886a752010-04-23 18:23:20 +00002224 (void) SetImageProgress(image,SaveImagesTag,0,1);
cristy3ed852e2009-09-05 21:47:34 +00002225 }
cristy0910f242010-04-01 18:55:09 +00002226 else
cristya20214e2010-08-28 16:52:13 +00002227 if (next_image->storage_class == PseudoClass)
cristy0910f242010-04-01 18:55:09 +00002228 {
cristya20214e2010-08-28 16:52:13 +00002229 if (next_image->compression == RLECompression)
cristy0910f242010-04-01 18:55:09 +00002230 {
2231 /*
2232 Packbits compression.
2233 */
2234 (void) WriteBlobMSBShort(image,1);
cristya20214e2010-08-28 16:52:13 +00002235 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002236 compact_pixels,IndexQuantum,exception);
cristy17f11b02014-12-20 19:37:04 +00002237 if (next_image->alpha_trait != UndefinedPixelTrait)
cristya20214e2010-08-28 16:52:13 +00002238 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002239 compact_pixels,AlphaQuantum,exception);
cristy0910f242010-04-01 18:55:09 +00002240 }
cristya20214e2010-08-28 16:52:13 +00002241 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
dirkc9e9ea32015-01-24 11:21:18 +00002242 IndexQuantum,MagickTrue,exception);
cristy17f11b02014-12-20 19:37:04 +00002243 if (next_image->alpha_trait != UndefinedPixelTrait)
cristya20214e2010-08-28 16:52:13 +00002244 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
dirkc9e9ea32015-01-24 11:21:18 +00002245 AlphaQuantum,separate,exception);
cristy0910f242010-04-01 18:55:09 +00002246 (void) SetImageProgress(image,SaveImagesTag,0,1);
2247 }
2248 else
2249 {
cristya20214e2010-08-28 16:52:13 +00002250 if (next_image->colorspace == CMYKColorspace)
dirkd9795a12014-11-11 22:01:21 +00002251 (void) NegateCMYK(next_image,exception);
cristya20214e2010-08-28 16:52:13 +00002252 if (next_image->compression == RLECompression)
cristy0910f242010-04-01 18:55:09 +00002253 {
2254 /*
2255 Packbits compression.
2256 */
2257 (void) WriteBlobMSBShort(image,1);
cristya20214e2010-08-28 16:52:13 +00002258 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002259 compact_pixels,RedQuantum,exception);
cristya20214e2010-08-28 16:52:13 +00002260 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002261 compact_pixels,GreenQuantum,exception);
cristya20214e2010-08-28 16:52:13 +00002262 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002263 compact_pixels,BlueQuantum,exception);
cristya20214e2010-08-28 16:52:13 +00002264 if (next_image->colorspace == CMYKColorspace)
2265 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002266 compact_pixels,BlackQuantum,exception);
cristy17f11b02014-12-20 19:37:04 +00002267 if (next_image->alpha_trait != UndefinedPixelTrait)
cristya20214e2010-08-28 16:52:13 +00002268 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002269 compact_pixels,AlphaQuantum,exception);
cristy0910f242010-04-01 18:55:09 +00002270 }
2271 (void) SetImageProgress(image,SaveImagesTag,0,6);
cristya20214e2010-08-28 16:52:13 +00002272 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
dirkc9e9ea32015-01-24 11:21:18 +00002273 RedQuantum,MagickTrue,exception);
cristy860cc732010-05-19 16:38:37 +00002274 (void) SetImageProgress(image,SaveImagesTag,1,6);
cristya20214e2010-08-28 16:52:13 +00002275 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
dirkc9e9ea32015-01-24 11:21:18 +00002276 GreenQuantum,separate,exception);
cristy860cc732010-05-19 16:38:37 +00002277 (void) SetImageProgress(image,SaveImagesTag,2,6);
cristya20214e2010-08-28 16:52:13 +00002278 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
dirkc9e9ea32015-01-24 11:21:18 +00002279 BlueQuantum,separate,exception);
cristy860cc732010-05-19 16:38:37 +00002280 (void) SetImageProgress(image,SaveImagesTag,3,6);
cristya20214e2010-08-28 16:52:13 +00002281 if (next_image->colorspace == CMYKColorspace)
2282 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
dirkc9e9ea32015-01-24 11:21:18 +00002283 BlackQuantum,separate,exception);
cristy860cc732010-05-19 16:38:37 +00002284 (void) SetImageProgress(image,SaveImagesTag,4,6);
cristy17f11b02014-12-20 19:37:04 +00002285 if (next_image->alpha_trait != UndefinedPixelTrait)
cristya20214e2010-08-28 16:52:13 +00002286 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
dirkc9e9ea32015-01-24 11:21:18 +00002287 AlphaQuantum,separate,exception);
cristy0910f242010-04-01 18:55:09 +00002288 (void) SetImageProgress(image,SaveImagesTag,5,6);
cristya20214e2010-08-28 16:52:13 +00002289 if (next_image->colorspace == CMYKColorspace)
dirkd9795a12014-11-11 22:01:21 +00002290 (void) NegateCMYK(next_image,exception);
cristy0910f242010-04-01 18:55:09 +00002291 }
cristya20214e2010-08-28 16:52:13 +00002292 if (next_image->compression == RLECompression)
2293 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
cristy3ed852e2009-09-05 21:47:34 +00002294 return(MagickTrue);
2295}
2296
cristy3ed852e2009-09-05 21:47:34 +00002297static void WritePascalString(Image* inImage,const char *inString,int inPad)
2298{
2299 size_t
cristya20214e2010-08-28 16:52:13 +00002300 length;
cristy3ed852e2009-09-05 21:47:34 +00002301
cristya20214e2010-08-28 16:52:13 +00002302 register ssize_t
2303 i;
cristy3ed852e2009-09-05 21:47:34 +00002304
cristya20214e2010-08-28 16:52:13 +00002305 /*
2306 Max length is 255.
2307 */
2308 length=(strlen(inString) > 255UL ) ? 255UL : strlen(inString);
2309 if (length == 0)
2310 (void) WriteBlobByte(inImage,0);
cristy3ed852e2009-09-05 21:47:34 +00002311 else
cristya20214e2010-08-28 16:52:13 +00002312 {
2313 (void) WriteBlobByte(inImage,(unsigned char) length);
2314 (void) WriteBlob(inImage, length, (const unsigned char *) inString);
2315 }
2316 length++;
2317 if ((length % inPad) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002318 return;
cristya20214e2010-08-28 16:52:13 +00002319 for (i=0; i < (ssize_t) (inPad-(length % inPad)); i++)
cristy3ed852e2009-09-05 21:47:34 +00002320 (void) WriteBlobByte(inImage,0);
2321}
2322
2323static void WriteResolutionResourceBlock(Image *image)
2324{
cristy56ed31c2010-03-22 00:46:21 +00002325 double
2326 x_resolution,
2327 y_resolution;
cristy3ed852e2009-09-05 21:47:34 +00002328
2329 unsigned short
2330 units;
2331
cristy3ed852e2009-09-05 21:47:34 +00002332 if (image->units == PixelsPerCentimeterResolution)
2333 {
dirk87dabf62014-11-11 22:44:30 +00002334 x_resolution=2.54*65536.0*image->resolution.x+0.5;
cristy2a11bef2011-10-28 18:33:11 +00002335 y_resolution=2.54*65536.0*image->resolution.y+0.5;
cristy3ed852e2009-09-05 21:47:34 +00002336 units=2;
2337 }
dirk87dabf62014-11-11 22:44:30 +00002338 else
2339 {
2340 x_resolution=65536.0*image->resolution.x+0.5;
2341 y_resolution=65536.0*image->resolution.y+0.5;
2342 units=1;
2343 }
cristy3ed852e2009-09-05 21:47:34 +00002344 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2345 (void) WriteBlobMSBShort(image,0x03ED);
2346 (void) WriteBlobMSBShort(image,0);
2347 (void) WriteBlobMSBLong(image,16); /* resource size */
cristy56ed31c2010-03-22 00:46:21 +00002348 (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002349 (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */
2350 (void) WriteBlobMSBShort(image,units); /* width unit */
cristy56ed31c2010-03-22 00:46:21 +00002351 (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002352 (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */
2353 (void) WriteBlobMSBShort(image,units); /* height unit */
2354}
2355
cristyd4d3f742010-04-25 20:36:50 +00002356static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile)
2357{
cristy0b796e62010-04-29 00:38:45 +00002358 register const unsigned char
cristyd4d3f742010-04-25 20:36:50 +00002359 *p;
2360
2361 size_t
2362 length;
2363
2364 unsigned char
2365 *datum;
2366
cristy6befb0f2010-05-31 14:33:15 +00002367 unsigned int
cristyd4d3f742010-04-25 20:36:50 +00002368 count,
cristy6befb0f2010-05-31 14:33:15 +00002369 long_sans;
cristyd4d3f742010-04-25 20:36:50 +00002370
2371 unsigned short
2372 id,
2373 short_sans;
2374
2375 length=GetStringInfoLength(bim_profile);
2376 if (length < 16)
cristy0b796e62010-04-29 00:38:45 +00002377 return;
cristyd4d3f742010-04-25 20:36:50 +00002378 datum=GetStringInfoDatum(bim_profile);
2379 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2380 {
cristy4176bb22010-05-01 16:29:09 +00002381 register unsigned char
2382 *q;
2383
2384 q=(unsigned char *) p;
cristyd4d3f742010-04-25 20:36:50 +00002385 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2386 break;
cristy6befb0f2010-05-31 14:33:15 +00002387 p=PushLongPixel(MSBEndian,p,&long_sans);
cristyd4d3f742010-04-25 20:36:50 +00002388 p=PushShortPixel(MSBEndian,p,&id);
2389 p=PushShortPixel(MSBEndian,p,&short_sans);
2390 p=PushLongPixel(MSBEndian,p,&count);
2391 if (id == 0x0000040f)
2392 {
cristy4176bb22010-05-01 16:29:09 +00002393 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2394 (PSDQuantum(count)+12)-(q-datum));
2395 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
cristyd4d3f742010-04-25 20:36:50 +00002396 break;
2397 }
2398 p+=count;
2399 if ((count & 0x01) != 0)
2400 p++;
2401 }
2402}
2403
cristyf11065e2010-05-14 13:26:59 +00002404static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile)
2405{
2406 register const unsigned char
2407 *p;
2408
2409 size_t
2410 length;
2411
2412 unsigned char
2413 *datum;
2414
cristy6befb0f2010-05-31 14:33:15 +00002415 unsigned int
cristyf11065e2010-05-14 13:26:59 +00002416 count,
cristy6befb0f2010-05-31 14:33:15 +00002417 long_sans;
cristyf11065e2010-05-14 13:26:59 +00002418
2419 unsigned short
2420 id,
2421 short_sans;
2422
2423 length=GetStringInfoLength(bim_profile);
2424 if (length < 16)
2425 return;
2426 datum=GetStringInfoDatum(bim_profile);
2427 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2428 {
2429 register unsigned char
2430 *q;
2431
2432 q=(unsigned char *) p;
2433 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2434 break;
cristy6befb0f2010-05-31 14:33:15 +00002435 p=PushLongPixel(MSBEndian,p,&long_sans);
cristyf11065e2010-05-14 13:26:59 +00002436 p=PushShortPixel(MSBEndian,p,&id);
2437 p=PushShortPixel(MSBEndian,p,&short_sans);
2438 p=PushLongPixel(MSBEndian,p,&count);
cristy94b11832011-09-08 19:46:03 +00002439 if ((id == 0x000003ed) && (PSDQuantum(count) < (ssize_t) (length-12)))
cristyf11065e2010-05-14 13:26:59 +00002440 {
2441 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2442 (PSDQuantum(count)+12)-(q-datum));
2443 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2444 break;
2445 }
2446 p+=count;
2447 if ((count & 0x01) != 0)
2448 p++;
2449 }
2450}
2451
cristy3a37efd2011-08-28 20:31:03 +00002452static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
2453 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002454{
2455 const char
cristya20214e2010-08-28 16:52:13 +00002456 *property;
cristy3ed852e2009-09-05 21:47:34 +00002457
2458 const StringInfo
cristy749d2152010-04-04 23:47:33 +00002459 *icc_profile;
cristy3ed852e2009-09-05 21:47:34 +00002460
cristya20214e2010-08-28 16:52:13 +00002461 Image
2462 *base_image,
2463 *next_image;
2464
cristy3ed852e2009-09-05 21:47:34 +00002465 MagickBooleanType
cristy3ed852e2009-09-05 21:47:34 +00002466 status;
2467
cristy875e28a2010-03-06 19:46:55 +00002468 PSDInfo
2469 psd_info;
2470
cristybb503372010-05-27 20:51:26 +00002471 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002472 i;
2473
2474 size_t
cristya20214e2010-08-28 16:52:13 +00002475 channel_size,
2476 channelLength,
2477 layer_count,
2478 layer_info_size,
cristy749d2152010-04-04 23:47:33 +00002479 length,
cristy3ed852e2009-09-05 21:47:34 +00002480 num_channels,
cristya20214e2010-08-28 16:52:13 +00002481 packet_size,
2482 rounded_layer_info_size;
cristy3ed852e2009-09-05 21:47:34 +00002483
cristy0b796e62010-04-29 00:38:45 +00002484 StringInfo
2485 *bim_profile;
2486
cristy3ed852e2009-09-05 21:47:34 +00002487 /*
cristy56ed31c2010-03-22 00:46:21 +00002488 Open image file.
cristy3ed852e2009-09-05 21:47:34 +00002489 */
2490 assert(image_info != (const ImageInfo *) NULL);
2491 assert(image_info->signature == MagickSignature);
2492 assert(image != (Image *) NULL);
2493 assert(image->signature == MagickSignature);
2494 if (image->debug != MagickFalse)
2495 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00002496 assert(exception != (ExceptionInfo *) NULL);
2497 assert(exception->signature == MagickSignature);
2498 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00002499 if (status == MagickFalse)
2500 return(status);
2501 packet_size=(size_t) (image->depth > 8 ? 6 : 3);
cristy17f11b02014-12-20 19:37:04 +00002502 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00002503 packet_size+=image->depth > 8 ? 2 : 1;
cristy875e28a2010-03-06 19:46:55 +00002504 psd_info.version=1;
2505 if ((LocaleCompare(image_info->magick,"PSB") == 0) ||
2506 (image->columns > 30000) || (image->rows > 30000))
2507 psd_info.version=2;
cristy50aea4a2010-03-09 17:37:44 +00002508 (void) WriteBlob(image,4,(const unsigned char *) "8BPS");
cristy875e28a2010-03-06 19:46:55 +00002509 (void) WriteBlobMSBShort(image,psd_info.version); /* version */
2510 for (i=1; i <= 6; i++)
2511 (void) WriteBlobByte(image, 0); /* 6 bytes of reserved */
dirka7014c82015-04-06 08:43:28 +00002512 if (IsImageGray(image) != MagickFalse)
cristy17f11b02014-12-20 19:37:04 +00002513 num_channels=(image->alpha_trait != UndefinedPixelTrait ? 2UL : 1UL);
cristy3ed852e2009-09-05 21:47:34 +00002514 else
cristy0910f242010-04-01 18:55:09 +00002515 if (image->storage_class == PseudoClass)
cristy17f11b02014-12-20 19:37:04 +00002516 num_channels=(image->alpha_trait != UndefinedPixelTrait ? 2UL : 1UL);
cristy0910f242010-04-01 18:55:09 +00002517 else
2518 {
2519 if (image->colorspace != CMYKColorspace)
cristy17f11b02014-12-20 19:37:04 +00002520 num_channels=(image->alpha_trait != UndefinedPixelTrait ? 4UL : 3UL);
cristy0910f242010-04-01 18:55:09 +00002521 else
cristy17f11b02014-12-20 19:37:04 +00002522 num_channels=(image->alpha_trait != UndefinedPixelTrait ? 5UL : 4UL);
cristy0910f242010-04-01 18:55:09 +00002523 }
cristy3ed852e2009-09-05 21:47:34 +00002524 (void) WriteBlobMSBShort(image,(unsigned short) num_channels);
cristy56ed31c2010-03-22 00:46:21 +00002525 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
2526 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
dirka7014c82015-04-06 08:43:28 +00002527 if (IsImageGray(image) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002528 {
cristy2045da32010-04-16 00:59:35 +00002529 MagickBooleanType
2530 monochrome;
2531
cristy0910f242010-04-01 18:55:09 +00002532 /*
2533 Write depth & mode.
2534 */
dirka7014c82015-04-06 08:43:28 +00002535 monochrome=IsImageMonochrome(image) && (image->depth == 1) ?
cristy3a37efd2011-08-28 20:31:03 +00002536 MagickTrue : MagickFalse;
cristy284c7d82010-04-24 00:19:14 +00002537 (void) WriteBlobMSBShort(image,(unsigned short)
2538 (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8));
cristy2b9582a2011-07-04 17:38:56 +00002539 (void) WriteBlobMSBShort(image,(unsigned short)
2540 (monochrome != MagickFalse ? BitmapMode : GrayscaleMode));
cristy3ed852e2009-09-05 21:47:34 +00002541 }
2542 else
2543 {
cristya20214e2010-08-28 16:52:13 +00002544 (void) WriteBlobMSBShort(image,(unsigned short) (image->storage_class ==
2545 PseudoClass ? 8 : image->depth > 8 ? 16 : 8));
dirk87dabf62014-11-11 22:44:30 +00002546
cristy48845392010-06-02 01:19:17 +00002547 if (((image_info->colorspace != UndefinedColorspace) ||
cristy0910f242010-04-01 18:55:09 +00002548 (image->colorspace != CMYKColorspace)) &&
cristy48845392010-06-02 01:19:17 +00002549 (image_info->colorspace != CMYKColorspace))
cristy0910f242010-04-01 18:55:09 +00002550 {
cristyaf8d3912014-02-21 14:50:33 +00002551 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy0910f242010-04-01 18:55:09 +00002552 (void) WriteBlobMSBShort(image,(unsigned short)
cristy2045da32010-04-16 00:59:35 +00002553 (image->storage_class == PseudoClass ? IndexedMode : RGBMode));
cristy0910f242010-04-01 18:55:09 +00002554 }
2555 else
2556 {
cristy48845392010-06-02 01:19:17 +00002557 if (image->colorspace != CMYKColorspace)
cristye941a752011-10-15 01:52:48 +00002558 (void) TransformImageColorspace(image,CMYKColorspace,exception);
cristy2045da32010-04-16 00:59:35 +00002559 (void) WriteBlobMSBShort(image,CMYKMode);
cristy0910f242010-04-01 18:55:09 +00002560 }
cristy3ed852e2009-09-05 21:47:34 +00002561 }
dirka7014c82015-04-06 08:43:28 +00002562 if ((IsImageGray(image) != MagickFalse) ||
cristyca9ddb02010-04-16 01:01:18 +00002563 (image->storage_class == DirectClass) || (image->colors > 256))
cristy3ed852e2009-09-05 21:47:34 +00002564 (void) WriteBlobMSBLong(image,0);
2565 else
2566 {
2567 /*
2568 Write PSD raster colormap.
2569 */
2570 (void) WriteBlobMSBLong(image,768);
cristybb503372010-05-27 20:51:26 +00002571 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002572 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red));
2573 for ( ; i < 256; i++)
2574 (void) WriteBlobByte(image,0);
cristybb503372010-05-27 20:51:26 +00002575 for (i=0; i < (ssize_t) image->colors; i++)
cristya20214e2010-08-28 16:52:13 +00002576 (void) WriteBlobByte(image,ScaleQuantumToChar(
2577 image->colormap[i].green));
cristy3ed852e2009-09-05 21:47:34 +00002578 for ( ; i < 256; i++)
2579 (void) WriteBlobByte(image,0);
cristybb503372010-05-27 20:51:26 +00002580 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002581 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue));
2582 for ( ; i < 256; i++)
2583 (void) WriteBlobByte(image,0);
2584 }
2585 /*
2586 Image resource block.
2587 */
cristy749d2152010-04-04 23:47:33 +00002588 length=28; /* 0x03EB */
cristy0b796e62010-04-29 00:38:45 +00002589 bim_profile=(StringInfo *) GetImageProfile(image,"8bim");
cristy749d2152010-04-04 23:47:33 +00002590 icc_profile=GetImageProfile(image,"icc");
cristyd4d3f742010-04-25 20:36:50 +00002591 if (bim_profile != (StringInfo *) NULL)
2592 {
cristy0b796e62010-04-29 00:38:45 +00002593 bim_profile=CloneStringInfo(bim_profile);
cristyd4d3f742010-04-25 20:36:50 +00002594 if (icc_profile != (StringInfo *) NULL)
2595 RemoveICCProfileFromResourceBlock(bim_profile);
cristyf11065e2010-05-14 13:26:59 +00002596 RemoveResolutionFromResourceBlock(bim_profile);
cristyd4d3f742010-04-25 20:36:50 +00002597 length+=PSDQuantum(GetStringInfoLength(bim_profile));
2598 }
cristy0b796e62010-04-29 00:38:45 +00002599 if (icc_profile != (const StringInfo *) NULL)
cristy749d2152010-04-04 23:47:33 +00002600 length+=PSDQuantum(GetStringInfoLength(icc_profile))+12;
cristy284c7d82010-04-24 00:19:14 +00002601 (void) WriteBlobMSBLong(image,(unsigned int) length);
cristyf11065e2010-05-14 13:26:59 +00002602 WriteResolutionResourceBlock(image);
cristy4176bb22010-05-01 16:29:09 +00002603 if (bim_profile != (StringInfo *) NULL)
2604 {
2605 (void) WriteBlob(image,GetStringInfoLength(bim_profile),
2606 GetStringInfoDatum(bim_profile));
2607 bim_profile=DestroyStringInfo(bim_profile);
2608 }
cristy749d2152010-04-04 23:47:33 +00002609 if (icc_profile != (StringInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002610 {
cristy749d2152010-04-04 23:47:33 +00002611 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
cristy4176bb22010-05-01 16:29:09 +00002612 (void) WriteBlobMSBShort(image,0x0000040F);
cristy749d2152010-04-04 23:47:33 +00002613 (void) WriteBlobMSBShort(image,0);
cristy284c7d82010-04-24 00:19:14 +00002614 (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength(
2615 icc_profile));
cristy749d2152010-04-04 23:47:33 +00002616 (void) WriteBlob(image,GetStringInfoLength(icc_profile),
2617 GetStringInfoDatum(icc_profile));
cristye195f262010-04-16 18:12:35 +00002618 if ((MagickOffsetType) GetStringInfoLength(icc_profile) !=
cristy2045da32010-04-16 00:59:35 +00002619 PSDQuantum(GetStringInfoLength(icc_profile)))
cristy749d2152010-04-04 23:47:33 +00002620 (void) WriteBlobByte(image,0);
cristye6365592010-04-02 17:31:23 +00002621 }
cristy48845392010-06-02 01:19:17 +00002622 layer_count=0;
2623 layer_info_size=2;
cristy2837bcc2010-08-07 23:57:39 +00002624 base_image=GetNextImageInList(image);
cristy17f11b02014-12-20 19:37:04 +00002625 if ((image->alpha_trait != UndefinedPixelTrait) && (base_image == (Image *) NULL))
cristy2837bcc2010-08-07 23:57:39 +00002626 base_image=image;
cristya20214e2010-08-28 16:52:13 +00002627 next_image=base_image;
2628 while ( next_image != NULL )
2629 {
2630 packet_size=next_image->depth > 8 ? 2UL : 1UL;
dirka7014c82015-04-06 08:43:28 +00002631 if (IsImageGray(next_image) != MagickFalse)
cristy17f11b02014-12-20 19:37:04 +00002632 num_channels=next_image->alpha_trait != UndefinedPixelTrait ? 2UL : 1UL;
cristy3ed852e2009-09-05 21:47:34 +00002633 else
cristya20214e2010-08-28 16:52:13 +00002634 if (next_image->storage_class == PseudoClass)
cristy17f11b02014-12-20 19:37:04 +00002635 num_channels=next_image->alpha_trait != UndefinedPixelTrait ? 2UL : 1UL;
cristy2045da32010-04-16 00:59:35 +00002636 else
cristya20214e2010-08-28 16:52:13 +00002637 if (next_image->colorspace != CMYKColorspace)
cristy17f11b02014-12-20 19:37:04 +00002638 num_channels=next_image->alpha_trait != UndefinedPixelTrait ? 4UL : 3UL;
cristy2045da32010-04-16 00:59:35 +00002639 else
cristy17f11b02014-12-20 19:37:04 +00002640 num_channels=next_image->alpha_trait != UndefinedPixelTrait ? 5UL : 4UL;
cristya20214e2010-08-28 16:52:13 +00002641 channelLength=(size_t) (next_image->columns*next_image->rows*packet_size+2);
cristy48845392010-06-02 01:19:17 +00002642 layer_info_size+=(size_t) (4*4+2+num_channels*6+(psd_info.version == 1 ? 8 :
2643 16)+4*1+4+num_channels*channelLength);
cristyd15e6592011-10-15 00:13:06 +00002644 property=(const char *) GetImageProperty(next_image,"label",exception);
cristya20214e2010-08-28 16:52:13 +00002645 if (property == (const char *) NULL)
2646 layer_info_size+=16;
cristyde9b8f52010-03-12 01:17:00 +00002647 else
2648 {
cristya20214e2010-08-28 16:52:13 +00002649 size_t
2650 length;
2651
2652 length=strlen(property);
2653 layer_info_size+=8+length+(4-(length % 4));
cristyde9b8f52010-03-12 01:17:00 +00002654 }
cristy4aff1572010-02-15 13:34:01 +00002655 layer_count++;
cristya20214e2010-08-28 16:52:13 +00002656 next_image=GetNextImageInList(next_image);
cristy3ed852e2009-09-05 21:47:34 +00002657 }
cristy144f1b62010-05-18 00:52:09 +00002658 if (layer_count == 0)
cristy875e28a2010-03-06 19:46:55 +00002659 (void) SetPSDSize(&psd_info,image,0);
cristy3ed852e2009-09-05 21:47:34 +00002660 else
cristya20214e2010-08-28 16:52:13 +00002661 {
cristy8e9bd3e2010-08-29 00:03:56 +00002662 CompressionType
2663 compression;
2664
2665 (void) SetPSDSize(&psd_info,image,layer_info_size+
2666 (psd_info.version == 1 ? 8 : 16));
2667 if ((layer_info_size/2) != ((layer_info_size+1)/2))
2668 rounded_layer_info_size=layer_info_size+1;
cristya20214e2010-08-28 16:52:13 +00002669 else
cristy8e9bd3e2010-08-29 00:03:56 +00002670 rounded_layer_info_size=layer_info_size;
2671 (void) SetPSDSize(&psd_info,image,rounded_layer_info_size);
dirk17303172015-04-05 21:20:07 +00002672 if (image->alpha_trait != UndefinedPixelTrait)
dirk8fb7ed52014-02-11 12:07:01 +00002673 (void) WriteBlobMSBShort(image,-(unsigned short) layer_count);
2674 else
2675 (void) WriteBlobMSBShort(image,(unsigned short) layer_count);
cristy8e9bd3e2010-08-29 00:03:56 +00002676 layer_count=1;
2677 compression=base_image->compression;
cristyf2a82ee2014-05-26 17:49:54 +00002678 for (next_image=base_image; next_image != NULL; )
cristy8e9bd3e2010-08-29 00:03:56 +00002679 {
2680 next_image->compression=NoCompression;
cristya4e5f472011-11-09 00:42:46 +00002681 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y);
2682 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x);
cristy0e274112014-03-25 14:09:19 +00002683 (void) WriteBlobMSBLong(image,(unsigned int) (next_image->page.y+
2684 next_image->rows));
2685 (void) WriteBlobMSBLong(image,(unsigned int) (next_image->page.x+
2686 next_image->columns));
cristy8e9bd3e2010-08-29 00:03:56 +00002687 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2688 channel_size=(unsigned int) ((packet_size*next_image->rows*
2689 next_image->columns)+2);
dirka7014c82015-04-06 08:43:28 +00002690 if ((IsImageGray(next_image) != MagickFalse) ||
cristy8e9bd3e2010-08-29 00:03:56 +00002691 (next_image->storage_class == PseudoClass))
2692 {
2693 (void) WriteBlobMSBShort(image,(unsigned short)
cristy17f11b02014-12-20 19:37:04 +00002694 (next_image->alpha_trait != UndefinedPixelTrait ? 2 : 1));
cristy8e9bd3e2010-08-29 00:03:56 +00002695 (void) WriteBlobMSBShort(image,0);
2696 (void) SetPSDSize(&psd_info,image,channel_size);
cristy17f11b02014-12-20 19:37:04 +00002697 if (next_image->alpha_trait != UndefinedPixelTrait)
cristy8e9bd3e2010-08-29 00:03:56 +00002698 {
2699 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2700 (void) SetPSDSize(&psd_info,image,channel_size);
2701 }
2702 }
2703 else
2704 if (next_image->colorspace != CMYKColorspace)
2705 {
2706 (void) WriteBlobMSBShort(image,(unsigned short)
cristy17f11b02014-12-20 19:37:04 +00002707 (next_image->alpha_trait != UndefinedPixelTrait ? 4 : 3));
cristy8e9bd3e2010-08-29 00:03:56 +00002708 (void) WriteBlobMSBShort(image,0);
2709 (void) SetPSDSize(&psd_info,image,channel_size);
2710 (void) WriteBlobMSBShort(image,1);
2711 (void) SetPSDSize(&psd_info,image,channel_size);
2712 (void) WriteBlobMSBShort(image,2);
2713 (void) SetPSDSize(&psd_info,image,channel_size);
cristy17f11b02014-12-20 19:37:04 +00002714 if (next_image->alpha_trait != UndefinedPixelTrait)
cristy8e9bd3e2010-08-29 00:03:56 +00002715 {
2716 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2717 (void) SetPSDSize(&psd_info,image,channel_size);
2718 }
2719 }
2720 else
2721 {
2722 (void) WriteBlobMSBShort(image,(unsigned short)
cristy8a46d822012-08-28 23:32:39 +00002723 (next_image->alpha_trait ? 5 : 4));
cristy8e9bd3e2010-08-29 00:03:56 +00002724 (void) WriteBlobMSBShort(image,0);
2725 (void) SetPSDSize(&psd_info,image,channel_size);
2726 (void) WriteBlobMSBShort(image,1);
2727 (void) SetPSDSize(&psd_info,image,channel_size);
2728 (void) WriteBlobMSBShort(image,2);
2729 (void) SetPSDSize(&psd_info,image,channel_size);
2730 (void) WriteBlobMSBShort(image,3);
2731 (void) SetPSDSize(&psd_info,image,channel_size);
cristy8a46d822012-08-28 23:32:39 +00002732 if (next_image->alpha_trait)
cristy8e9bd3e2010-08-29 00:03:56 +00002733 {
2734 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2735 (void) SetPSDSize(&psd_info,image,channel_size);
2736 }
2737 }
2738 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2739 (void) WriteBlob(image,4,(const unsigned char *)
2740 CompositeOperatorToPSDBlendMode(next_image->compose));
2741 (void) WriteBlobByte(image,255); /* layer opacity */
2742 (void) WriteBlobByte(image,0);
dirk48febd62014-04-08 04:26:07 +00002743 (void) WriteBlobByte(image,next_image->compose==NoCompositeOp ?
2744 1 << 0x02 : 1); /* layer properties - visible, etc. */
cristy8e9bd3e2010-08-29 00:03:56 +00002745 (void) WriteBlobByte(image,0);
cristyd15e6592011-10-15 00:13:06 +00002746 property=(const char *) GetImageProperty(next_image,"label",exception);
cristy8e9bd3e2010-08-29 00:03:56 +00002747 if (property == (const char *) NULL)
2748 {
dirk93b02b72013-11-16 16:03:36 +00002749 char
cristy151b66d2015-04-15 10:50:31 +00002750 layer_name[MagickPathExtent];
cristy7a1ce132013-11-14 12:38:14 +00002751
cristy8e9bd3e2010-08-29 00:03:56 +00002752 (void) WriteBlobMSBLong(image,16);
2753 (void) WriteBlobMSBLong(image,0);
2754 (void) WriteBlobMSBLong(image,0);
cristy151b66d2015-04-15 10:50:31 +00002755 (void) FormatLocaleString(layer_name,MagickPathExtent,"L%04ld",(long)
cristy7a1ce132013-11-14 12:38:14 +00002756 layer_count++);
2757 WritePascalString(image,layer_name,4);
cristy8e9bd3e2010-08-29 00:03:56 +00002758 }
2759 else
2760 {
2761 size_t
2762 length;
cristy3ed852e2009-09-05 21:47:34 +00002763
cristy8e9bd3e2010-08-29 00:03:56 +00002764 length=strlen(property);
2765 (void) WriteBlobMSBLong(image,(unsigned int) (length+(4-
2766 (length % 4))+8));
2767 (void) WriteBlobMSBLong(image,0);
2768 (void) WriteBlobMSBLong(image,0);
2769 WritePascalString(image,property,4);
2770 }
2771 next_image=GetNextImageInList(next_image);
2772 }
2773 /*
2774 Now the image data!
2775 */
2776 next_image=base_image;
2777 while (next_image != NULL)
2778 {
2779 status=WriteImageChannels(&psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002780 MagickTrue,exception);
cristy8e9bd3e2010-08-29 00:03:56 +00002781 next_image=GetNextImageInList(next_image);
2782 }
2783 (void) WriteBlobMSBLong(image,0); /* user mask data */
2784 base_image->compression=compression;
cristy144f1b62010-05-18 00:52:09 +00002785 }
cristy144f1b62010-05-18 00:52:09 +00002786 /*
2787 Write composite image.
2788 */
cristy018f07f2011-09-04 21:15:19 +00002789 status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse,
2790 exception);
cristy3ed852e2009-09-05 21:47:34 +00002791 (void) CloseBlob(image);
2792 return(status);
2793}