blob: 0afe07abfb4eaceccda53eeb343fbcd9feaf988f [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% %
cristyfe676ee2013-11-18 13:03:38 +000023% Copyright 1999-2014 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"
53#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000054#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000055#include "MagickCore/constitute.h"
56#include "MagickCore/enhance.h"
57#include "MagickCore/exception.h"
58#include "MagickCore/exception-private.h"
59#include "MagickCore/image.h"
60#include "MagickCore/image-private.h"
61#include "MagickCore/list.h"
62#include "MagickCore/log.h"
63#include "MagickCore/magick.h"
64#include "MagickCore/memory_.h"
65#include "MagickCore/module.h"
66#include "MagickCore/monitor-private.h"
67#include "MagickCore/pixel.h"
68#include "MagickCore/pixel-accessor.h"
69#include "MagickCore/profile.h"
70#include "MagickCore/property.h"
71#include "MagickCore/quantum-private.h"
72#include "MagickCore/static.h"
73#include "MagickCore/string_.h"
dirkb41f0802013-12-26 16:47:58 +000074#ifdef MAGICKCORE_ZLIB_DELEGATE
75#include <zlib.h>
76#endif
cristy3ed852e2009-09-05 21:47:34 +000077
78/*
cristy2d3d87f2010-03-01 00:23:08 +000079 Define declaractions.
80*/
cristyaca3ec52010-03-02 14:55:01 +000081#define MaxPSDChannels 56
cristybb503372010-05-27 20:51:26 +000082#define PSDQuantum(x) (((ssize_t) (x)+1) & -2)
cristy2d3d87f2010-03-01 00:23:08 +000083
84/*
85 Enumerated declaractions.
86*/
87typedef enum
88{
dirkb41f0802013-12-26 16:47:58 +000089 Raw = 0,
90 RLE = 1,
91 ZipWithoutPrediction = 2,
92 ZipWithPrediction = 3
93} PSDCompressionType;
94
95typedef enum
96{
cristy2d3d87f2010-03-01 00:23:08 +000097 BitmapMode = 0,
98 GrayscaleMode = 1,
99 IndexedMode = 2,
100 RGBMode = 3,
101 CMYKMode = 4,
102 MultichannelMode = 7,
103 DuotoneMode = 8,
104 LabMode = 9
105} PSDImageType;
106
107/*
108 Typedef declaractions.
109*/
110typedef struct _ChannelInfo
111{
112 short int
113 type;
114
cristybb503372010-05-27 20:51:26 +0000115 size_t
cristy2d3d87f2010-03-01 00:23:08 +0000116 size;
117} ChannelInfo;
118
119typedef struct _LayerInfo
120{
121 RectangleInfo
122 page,
123 mask;
124
125 unsigned short
126 channels;
127
128 ChannelInfo
129 channel_info[MaxPSDChannels];
130
131 char
132 blendkey[4];
133
134 Quantum
135 opacity;
136
137 unsigned char
138 clipping,
139 visible,
140 flags;
141
cristybb503372010-05-27 20:51:26 +0000142 size_t
cristy2d3d87f2010-03-01 00:23:08 +0000143 offset_x,
144 offset_y;
145
146 unsigned char
147 name[256];
148
149 Image
150 *image;
151} LayerInfo;
152
153typedef struct _PSDInfo
154{
155 char
156 signature[4];
157
158 unsigned short
159 channels,
160 version;
161
162 unsigned char
163 reserved[6];
164
cristybb503372010-05-27 20:51:26 +0000165 size_t
cristy2d3d87f2010-03-01 00:23:08 +0000166 rows,
167 columns;
168
169 unsigned short
170 depth,
171 mode;
172} PSDInfo;
173
174/*
cristy3ed852e2009-09-05 21:47:34 +0000175 Forward declarations.
176*/
177static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +0000178 WritePSDImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000179
180/*
181%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182% %
183% %
184% %
cristy3ed852e2009-09-05 21:47:34 +0000185% I s P S D %
186% %
187% %
188% %
189%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190%
191% IsPSD()() returns MagickTrue if the image format type, identified by the
192% magick string, is PSD.
193%
194% The format of the IsPSD method is:
195%
196% MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
197%
198% A description of each parameter follows:
199%
200% o magick: compare image format pattern against these bytes.
201%
202% o length: Specifies the length of the magick string.
203%
204*/
205static MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
206{
207 if (length < 4)
208 return(MagickFalse);
209 if (LocaleNCompare((const char *) magick,"8BPS",4) == 0)
210 return(MagickTrue);
211 return(MagickFalse);
212}
213
214/*
215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216% %
217% %
218% %
219% R e a d P S D I m a g e %
220% %
221% %
222% %
223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224%
225% ReadPSDImage() reads an Adobe Photoshop image file and returns it. It
226% allocates the memory necessary for the new Image structure and returns a
227% pointer to the new image.
228%
229% The format of the ReadPSDImage method is:
230%
cristycd081772010-03-22 17:19:12 +0000231% Image *ReadPSDImage(image_info)
cristy3ed852e2009-09-05 21:47:34 +0000232%
233% A description of each parameter follows:
234%
235% o image_info: the image info.
236%
237% o exception: return any errors or warnings in this structure.
238%
239*/
240
cristy19eb6412010-04-23 14:42:29 +0000241static const char *CompositeOperatorToPSDBlendMode(CompositeOperator op)
cristycd081772010-03-22 17:19:12 +0000242{
243 const char
244 *blend_mode;
245
cristy19eb6412010-04-23 14:42:29 +0000246 switch (op)
cristycd081772010-03-22 17:19:12 +0000247 {
cristy216b90f2014-04-12 22:59:20 +0000248 case ColorizeCompositeOp: blend_mode = "colr"; break;
249 case DarkenCompositeOp: blend_mode = "dark"; break;
250 case DifferenceCompositeOp: blend_mode = "diff"; break;
251 case DissolveCompositeOp: blend_mode = "diss"; break;
252 case HardLightCompositeOp: blend_mode = "hLit"; break;
253 case HueCompositeOp: blend_mode = "hue "; break;
254 case LightenCompositeOp: blend_mode = "lite"; break;
255 case LuminizeCompositeOp: blend_mode = "lum "; break;
256 case MultiplyCompositeOp: blend_mode = "mul "; break;
257 case OverCompositeOp: blend_mode = "norm"; break;
258 case OverlayCompositeOp: blend_mode = "over"; break;
259 case SaturateCompositeOp: blend_mode = "sat "; break;
260 case ScreenCompositeOp: blend_mode = "scrn"; break;
261 case SoftLightCompositeOp: blend_mode = "sLit"; break;
262 default: blend_mode = "norm";
cristycd081772010-03-22 17:19:12 +0000263 }
264 return(blend_mode);
265}
266
dirkb41f0802013-12-26 16:47:58 +0000267static inline CompressionType ConvertPSDCompression(
268 PSDCompressionType compression)
269{
270 switch (compression)
271 {
272 case RLE:
273 return RLECompression;
274 case ZipWithPrediction:
275 case ZipWithoutPrediction:
276 return ZipCompression;
277 default:
278 return NoCompression;
279 }
280}
281
282static MagickStatusType CorrectPSDOpacity(LayerInfo* layer_info,
283 ExceptionInfo *exception)
284{
285 register Quantum
286 *q;
287
288 register ssize_t
289 x;
290
291 ssize_t
292 y;
293
294 if (layer_info->opacity == OpaqueAlpha)
295 return(MagickTrue);
296
297 layer_info->image->alpha_trait=BlendPixelTrait;
298 for (y=0; y < (ssize_t) layer_info->image->rows; y++)
299 {
cristyd8083a62014-02-01 13:53:44 +0000300 q=GetAuthenticPixels(layer_info->image,0,y,layer_info->image->columns,1,
301 exception);
dirkb41f0802013-12-26 16:47:58 +0000302 if (q == (Quantum *) NULL)
303 break;
304 for (x=0; x < (ssize_t) layer_info->image->columns; x++)
305 {
306 SetPixelAlpha(layer_info->image,(Quantum) (QuantumScale*(GetPixelAlpha(
307 layer_info->image,q))*layer_info->opacity),q);
308 q+=GetPixelChannels(layer_info->image);
309 }
310 if (SyncAuthenticPixels(layer_info->image,exception) == MagickFalse)
311 return(MagickFalse);
312 }
313
314 return(MagickTrue);
315}
316
cristycd081772010-03-22 17:19:12 +0000317static ssize_t DecodePSDPixels(const size_t number_compact_pixels,
cristybb503372010-05-27 20:51:26 +0000318 const unsigned char *compact_pixels,const ssize_t depth,
cristycd081772010-03-22 17:19:12 +0000319 const size_t number_pixels,unsigned char *pixels)
320{
321 int
322 pixel;
323
324 register ssize_t
325 i,
326 j;
327
cristycd081772010-03-22 17:19:12 +0000328 size_t
329 length;
330
cristy802d3642011-04-27 02:02:41 +0000331 ssize_t
332 packets;
333
cristycd081772010-03-22 17:19:12 +0000334 packets=(ssize_t) number_compact_pixels;
335 for (i=0; (packets > 1) && (i < (ssize_t) number_pixels); )
336 {
dirk40084fa2014-02-03 21:29:38 +0000337 length=(size_t) (*compact_pixels++);
cristycd081772010-03-22 17:19:12 +0000338 packets--;
339 if (length == 128)
340 continue;
341 if (length > 128)
342 {
343 length=256-length+1;
cristy67fc7682014-02-10 15:23:55 +0000344 if (((ssize_t) length+i) > (ssize_t) number_pixels)
dirk40084fa2014-02-03 21:29:38 +0000345 length=number_pixels-(size_t) i;
cristycd081772010-03-22 17:19:12 +0000346 pixel=(*compact_pixels++);
347 packets--;
cristy284c7d82010-04-24 00:19:14 +0000348 for (j=0; j < (ssize_t) length; j++)
cristycd081772010-03-22 17:19:12 +0000349 {
350 switch (depth)
351 {
352 case 1:
353 {
cristy284c7d82010-04-24 00:19:14 +0000354 *pixels++=(pixel >> 7) & 0x01 ? 0U : 255U;
355 *pixels++=(pixel >> 6) & 0x01 ? 0U : 255U;
356 *pixels++=(pixel >> 5) & 0x01 ? 0U : 255U;
357 *pixels++=(pixel >> 4) & 0x01 ? 0U : 255U;
358 *pixels++=(pixel >> 3) & 0x01 ? 0U : 255U;
359 *pixels++=(pixel >> 2) & 0x01 ? 0U : 255U;
360 *pixels++=(pixel >> 1) & 0x01 ? 0U : 255U;
361 *pixels++=(pixel >> 0) & 0x01 ? 0U : 255U;
cristycd081772010-03-22 17:19:12 +0000362 i+=8;
363 break;
364 }
365 case 4:
366 {
cristy284c7d82010-04-24 00:19:14 +0000367 *pixels++=(unsigned char) ((pixel >> 4) & 0xff);
368 *pixels++=(unsigned char) ((pixel & 0x0f) & 0xff);
cristycd081772010-03-22 17:19:12 +0000369 i+=2;
370 break;
371 }
372 case 2:
373 {
cristy284c7d82010-04-24 00:19:14 +0000374 *pixels++=(unsigned char) ((pixel >> 6) & 0x03);
375 *pixels++=(unsigned char) ((pixel >> 4) & 0x03);
376 *pixels++=(unsigned char) ((pixel >> 2) & 0x03);
377 *pixels++=(unsigned char) ((pixel & 0x03) & 0x03);
cristycd081772010-03-22 17:19:12 +0000378 i+=4;
379 break;
380 }
381 default:
382 {
cristy284c7d82010-04-24 00:19:14 +0000383 *pixels++=(unsigned char) pixel;
cristycd081772010-03-22 17:19:12 +0000384 i++;
385 break;
386 }
387 }
388 }
389 continue;
390 }
391 length++;
cristy67fc7682014-02-10 15:23:55 +0000392 if (((ssize_t) length+i) > (ssize_t) number_pixels)
dirk40084fa2014-02-03 21:29:38 +0000393 length=number_pixels-(size_t) i;
cristy284c7d82010-04-24 00:19:14 +0000394 for (j=0; j < (ssize_t) length; j++)
cristycd081772010-03-22 17:19:12 +0000395 {
396 switch (depth)
397 {
398 case 1:
399 {
cristy284c7d82010-04-24 00:19:14 +0000400 *pixels++=(*compact_pixels >> 7) & 0x01 ? 0U : 255U;
401 *pixels++=(*compact_pixels >> 6) & 0x01 ? 0U : 255U;
402 *pixels++=(*compact_pixels >> 5) & 0x01 ? 0U : 255U;
403 *pixels++=(*compact_pixels >> 4) & 0x01 ? 0U : 255U;
404 *pixels++=(*compact_pixels >> 3) & 0x01 ? 0U : 255U;
405 *pixels++=(*compact_pixels >> 2) & 0x01 ? 0U : 255U;
406 *pixels++=(*compact_pixels >> 1) & 0x01 ? 0U : 255U;
407 *pixels++=(*compact_pixels >> 0) & 0x01 ? 0U : 255U;
cristycd081772010-03-22 17:19:12 +0000408 i+=8;
409 break;
410 }
411 case 4:
412 {
cristy618a9662010-03-23 13:59:54 +0000413 *pixels++=(*compact_pixels >> 4) & 0xff;
414 *pixels++=(*compact_pixels & 0x0f) & 0xff;
cristycd081772010-03-22 17:19:12 +0000415 i+=2;
416 break;
417 }
418 case 2:
419 {
cristy618a9662010-03-23 13:59:54 +0000420 *pixels++=(*compact_pixels >> 6) & 0x03;
421 *pixels++=(*compact_pixels >> 4) & 0x03;
422 *pixels++=(*compact_pixels >> 2) & 0x03;
423 *pixels++=(*compact_pixels & 0x03) & 0x03;
cristycd081772010-03-22 17:19:12 +0000424 i+=4;
425 break;
426 }
427 default:
428 {
429 *pixels++=(*compact_pixels);
430 i++;
431 break;
432 }
433 }
434 compact_pixels++;
435 }
436 }
437 return(i);
438}
439
dirkbd8bd852013-12-28 22:55:19 +0000440static inline LayerInfo *DestroyLayerInfo(LayerInfo *layer_info,
441 const ssize_t number_layers)
442{
443 ssize_t
444 i;
445
446 for (i=0; i<number_layers; i++)
447 {
448 if (layer_info[i].image != (Image *) NULL)
449 layer_info[i].image=DestroyImage(layer_info[i].image);
450 }
451
452 return (LayerInfo *) RelinquishMagickMemory(layer_info);
453}
454
dirkb41f0802013-12-26 16:47:58 +0000455static inline size_t GetPSDPacketSize(Image *image)
cristy2d3d87f2010-03-01 00:23:08 +0000456{
dirkb41f0802013-12-26 16:47:58 +0000457 if (image->storage_class == PseudoClass)
458 {
459 if (image->colors > 256)
460 return(2);
461 else if (image->depth > 8)
462 return(2);
463 }
464 else
465 if (image->depth > 8)
466 return(2);
467
468 return(1);
cristy2d3d87f2010-03-01 00:23:08 +0000469}
470
dirk4d9863c2014-03-28 19:48:49 +0000471static inline MagickSizeType GetPSDSize(const PSDInfo *psd_info,Image *image)
cristy2d3d87f2010-03-01 00:23:08 +0000472{
473 if (psd_info->version == 1)
474 return((MagickSizeType) ReadBlobMSBLong(image));
475 return((MagickSizeType) ReadBlobMSBLongLong(image));
476}
477
dirkb41f0802013-12-26 16:47:58 +0000478static inline size_t GetPSDRowSize(Image *image)
479{
480 if (image->depth == 1)
481 return((image->columns+7)/8);
482 else
483 return(image->columns*GetPSDPacketSize(image));
484}
485
cristybb503372010-05-27 20:51:26 +0000486static inline ssize_t MagickAbsoluteValue(const ssize_t x)
cristy3ed852e2009-09-05 21:47:34 +0000487{
488 if (x < 0)
489 return(-x);
490 return(x);
491}
492
cristycd081772010-03-22 17:19:12 +0000493static const char *ModeToString(PSDImageType type)
cristy3ed852e2009-09-05 21:47:34 +0000494{
cristycd081772010-03-22 17:19:12 +0000495 switch (type)
cristy3ed852e2009-09-05 21:47:34 +0000496 {
497 case BitmapMode: return "Bitmap";
498 case GrayscaleMode: return "Grayscale";
499 case IndexedMode: return "Indexed";
500 case RGBMode: return "RGB";
501 case CMYKMode: return "CMYK";
502 case MultichannelMode: return "Multichannel";
503 case DuotoneMode: return "Duotone";
504 case LabMode: return "L*A*B";
505 default: return "unknown";
506 }
507}
508
509static MagickBooleanType ParseImageResourceBlocks(Image *image,
dirk18c0e4d2014-02-22 22:24:05 +0000510 const unsigned char *blocks,size_t length,
511 MagickBooleanType *has_merged_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000512{
513 const unsigned char
514 *p;
515
516 StringInfo
517 *profile;
518
cristy6befb0f2010-05-31 14:33:15 +0000519 unsigned int
cristy3ed852e2009-09-05 21:47:34 +0000520 count,
cristy6befb0f2010-05-31 14:33:15 +0000521 long_sans;
cristy3ed852e2009-09-05 21:47:34 +0000522
523 unsigned short
524 id,
525 short_sans;
526
527 if (length < 16)
528 return(MagickFalse);
cristy8723e4b2011-09-01 13:11:19 +0000529 profile=BlobToStringInfo((const void *) NULL,length);
cristy3ed852e2009-09-05 21:47:34 +0000530 SetStringInfoDatum(profile,blocks);
cristyd15e6592011-10-15 00:13:06 +0000531 (void) SetImageProfile(image,"8bim",profile,exception);
cristy3ed852e2009-09-05 21:47:34 +0000532 profile=DestroyStringInfo(profile);
533 for (p=blocks; (p >= blocks) && (p < (blocks+length-16)); )
534 {
535 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
536 break;
cristy6befb0f2010-05-31 14:33:15 +0000537 p=PushLongPixel(MSBEndian,p,&long_sans);
cristyf11065e2010-05-14 13:26:59 +0000538 p=PushShortPixel(MSBEndian,p,&id);
539 p=PushShortPixel(MSBEndian,p,&short_sans);
540 p=PushLongPixel(MSBEndian,p,&count);
cristy3ed852e2009-09-05 21:47:34 +0000541 switch (id)
542 {
543 case 0x03ed:
544 {
cristy1e4a80b2010-05-14 16:18:26 +0000545 char
546 value[MaxTextExtent];
547
cristy3ed852e2009-09-05 21:47:34 +0000548 unsigned short
549 resolution;
550
551 /*
552 Resolution info.
553 */
cristyf11065e2010-05-14 13:26:59 +0000554 p=PushShortPixel(MSBEndian,p,&resolution);
cristy2a11bef2011-10-28 18:33:11 +0000555 image->resolution.x=(double) resolution;
556 (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.x);
cristyd15e6592011-10-15 00:13:06 +0000557 (void) SetImageProperty(image,"tiff:XResolution",value,exception);
cristyf11065e2010-05-14 13:26:59 +0000558 p=PushShortPixel(MSBEndian,p,&short_sans);
559 p=PushShortPixel(MSBEndian,p,&short_sans);
560 p=PushShortPixel(MSBEndian,p,&short_sans);
561 p=PushShortPixel(MSBEndian,p,&resolution);
cristy2a11bef2011-10-28 18:33:11 +0000562 image->resolution.y=(double) resolution;
563 (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.y);
cristyd15e6592011-10-15 00:13:06 +0000564 (void) SetImageProperty(image,"tiff:YResolution",value,exception);
cristyf11065e2010-05-14 13:26:59 +0000565 p=PushShortPixel(MSBEndian,p,&short_sans);
566 p=PushShortPixel(MSBEndian,p,&short_sans);
567 p=PushShortPixel(MSBEndian,p,&short_sans);
cristy15893bd2012-12-19 14:40:13 +0000568 image->units=PixelsPerInchResolution;
cristy3ed852e2009-09-05 21:47:34 +0000569 break;
570 }
dirk18c0e4d2014-02-22 22:24:05 +0000571 case 0x0421:
572 {
573 if (*(p+4) == 0)
574 *has_merged_image=MagickFalse;
dirkc3378432014-05-18 06:50:37 +0000575 p+=count;
576 break;
dirk18c0e4d2014-02-22 22:24:05 +0000577 }
cristy3ed852e2009-09-05 21:47:34 +0000578 default:
579 {
580 p+=count;
581 break;
582 }
583 }
584 if ((count & 0x01) != 0)
585 p++;
586 }
587 return(MagickTrue);
588}
589
cristycd081772010-03-22 17:19:12 +0000590static CompositeOperator PSDBlendModeToCompositeOperator(const char *mode)
cristy56ed31c2010-03-22 00:46:21 +0000591{
cristycd081772010-03-22 17:19:12 +0000592 if (mode == (const char *) NULL)
593 return(OverCompositeOp);
594 if (LocaleNCompare(mode,"norm",4) == 0)
595 return(OverCompositeOp);
596 if (LocaleNCompare(mode,"mul ",4) == 0)
597 return(MultiplyCompositeOp);
598 if (LocaleNCompare(mode,"diss",4) == 0)
599 return(DissolveCompositeOp);
600 if (LocaleNCompare(mode,"diff",4) == 0)
601 return(DifferenceCompositeOp);
602 if (LocaleNCompare(mode,"dark",4) == 0)
603 return(DarkenCompositeOp);
604 if (LocaleNCompare(mode,"lite",4) == 0)
605 return(LightenCompositeOp);
606 if (LocaleNCompare(mode,"hue ",4) == 0)
607 return(HueCompositeOp);
608 if (LocaleNCompare(mode,"sat ",4) == 0)
609 return(SaturateCompositeOp);
610 if (LocaleNCompare(mode,"colr",4) == 0)
611 return(ColorizeCompositeOp);
612 if (LocaleNCompare(mode,"lum ",4) == 0)
613 return(LuminizeCompositeOp);
614 if (LocaleNCompare(mode,"scrn",4) == 0)
615 return(ScreenCompositeOp);
616 if (LocaleNCompare(mode,"over",4) == 0)
617 return(OverlayCompositeOp);
618 if (LocaleNCompare(mode,"hLit",4) == 0)
cristy216b90f2014-04-12 22:59:20 +0000619 return(HardLightCompositeOp);
cristycd081772010-03-22 17:19:12 +0000620 if (LocaleNCompare(mode,"sLit",4) == 0)
cristy216b90f2014-04-12 22:59:20 +0000621 return(SoftLightCompositeOp);
cristycd081772010-03-22 17:19:12 +0000622 if (LocaleNCompare(mode,"smud",4) == 0)
623 return(OverCompositeOp);
624 if (LocaleNCompare(mode,"div ",4) == 0)
625 return(OverCompositeOp);
626 if (LocaleNCompare(mode,"idiv",4) == 0)
627 return(OverCompositeOp);
628 return(OverCompositeOp);
cristy56ed31c2010-03-22 00:46:21 +0000629}
630
dirkb41f0802013-12-26 16:47:58 +0000631static MagickStatusType ReadPSDChannelPixels(Image *image,
632 const size_t channels,const size_t row,const ssize_t type,
633 const unsigned char *pixels,ExceptionInfo *exception)
cristy56ed31c2010-03-22 00:46:21 +0000634{
cristy56ed31c2010-03-22 00:46:21 +0000635 Quantum
636 pixel;
637
cristy7753b2a2011-02-19 18:36:52 +0000638 register const unsigned char
639 *p;
640
cristy4c08aed2011-07-01 19:47:50 +0000641 register Quantum
cristy56ed31c2010-03-22 00:46:21 +0000642 *q;
643
cristy7753b2a2011-02-19 18:36:52 +0000644 register ssize_t
645 x;
cristy56ed31c2010-03-22 00:46:21 +0000646
647 size_t
648 packet_size;
649
dirkb41f0802013-12-26 16:47:58 +0000650 unsigned short
651 nibble;
652
653 p=pixels;
654 q=GetAuthenticPixels(image,0,row,image->columns,1,exception);
655 if (q == (Quantum *) NULL)
656 return MagickFalse;
657 packet_size=GetPSDPacketSize(image);
658 for (x=0; x < (ssize_t) image->columns; x++)
659 {
660 if (packet_size == 1)
661 pixel=ScaleCharToQuantum(*p++);
662 else
663 {
664 p=PushShortPixel(MSBEndian,p,&nibble);
665 pixel=ScaleShortToQuantum(nibble);
666 }
667 switch (type)
668 {
669 case -1:
670 {
671 SetPixelAlpha(image,pixel,q);
672 break;
673 }
674 case 0:
675 {
676 SetPixelRed(image,pixel,q);
677 if (channels == 1)
678 SetPixelGray(image,pixel,q);
679 else
680 SetPixelRed(image,pixel,q);
681 if (image->storage_class == PseudoClass)
682 {
683 if (packet_size == 1)
684 SetPixelIndex(image,ScaleQuantumToChar(pixel),q);
685 else
686 SetPixelIndex(image,ScaleQuantumToShort(pixel),q);
687 SetPixelInfoPixel(image,image->colormap+(ssize_t)
688 GetPixelIndex(image,q),q);
689 if (image->depth == 1)
690 {
691 ssize_t
692 bit,
693 number_bits;
694
695 number_bits=image->columns-x;
696 if (number_bits > 8)
697 number_bits=8;
698 for (bit=0; bit < number_bits; bit++)
699 {
700 SetPixelIndex(image,(((unsigned char) pixel) &
701 (0x01 << (7-bit))) != 0 ? 0 : 255,q);
702 SetPixelInfoPixel(image,image->colormap+(ssize_t)
703 GetPixelIndex(image,q),q);
704 q+=GetPixelChannels(image);
705 x++;
706 }
707 }
708 }
709 break;
710 }
711 case 1:
712 {
713 if (image->storage_class == PseudoClass)
714 SetPixelAlpha(image,pixel,q);
715 else
716 SetPixelGreen(image,pixel,q);
717 break;
718 }
719 case 2:
720 {
721 if (image->storage_class == PseudoClass)
722 SetPixelAlpha(image,pixel,q);
723 else
724 SetPixelBlue(image,pixel,q);
725 break;
726 }
727 case 3:
728 {
729 if (image->colorspace == CMYKColorspace)
730 SetPixelBlack(image,pixel,q);
731 else
732 if (image->alpha_trait == BlendPixelTrait)
733 SetPixelAlpha(image,pixel,q);
734 break;
735 }
736 case 4:
737 {
738 if ((IssRGBCompatibleColorspace(image->colorspace) != MagickFalse) &&
739 (channels > 3))
740 break;
741 if (image->alpha_trait == BlendPixelTrait)
742 SetPixelAlpha(image,pixel,q);
743 break;
744 }
745 default:
746 break;
747 }
748 q+=GetPixelChannels(image);
749 }
750 return(SyncAuthenticPixels(image,exception));
751}
752
753static MagickStatusType ReadPSDChannelRaw(Image *image,const size_t channels,
754 const ssize_t type,ExceptionInfo *exception)
755{
756 MagickStatusType
757 status;
758
759 size_t
760 count,
761 row_size;
762
763 ssize_t
764 y;
765
766 unsigned char
767 *pixels;
768
769 if (image->debug != MagickFalse)
770 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
771 " layer data is RAW");
772
773 row_size=GetPSDRowSize(image);
774 pixels=(unsigned char *) AcquireQuantumMemory(row_size,sizeof(*pixels));
775 if (pixels == (unsigned char *) NULL)
776 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
777 image->filename);
778
779 status=MagickTrue;
780 for (y=0; y < (ssize_t) image->rows; y++)
781 {
782 status=MagickFalse;
783
784 count=ReadBlob(image,row_size,pixels);
785 if (count != row_size)
786 break;
787
788 status=ReadPSDChannelPixels(image,channels,y,type,pixels,exception);
789 if (status == MagickFalse)
790 break;
791 }
792
793 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
794 return(status);
795}
796
797static inline MagickOffsetType *ReadPSDRLEOffsets(Image *image,
dirk4d9863c2014-03-28 19:48:49 +0000798 const PSDInfo *psd_info,const size_t size)
dirkb41f0802013-12-26 16:47:58 +0000799{
800 MagickOffsetType
801 *offsets;
802
803 ssize_t
804 y;
805
806 offsets=(MagickOffsetType *) AcquireQuantumMemory(size,sizeof(*offsets));
807 if(offsets != (MagickOffsetType *) NULL)
808 {
809 for (y=0; y < (ssize_t) size; y++)
810 {
811 if (psd_info->version == 1)
812 offsets[y]=(MagickOffsetType) ReadBlobMSBShort(image);
813 else
814 offsets[y]=(MagickOffsetType) ReadBlobMSBLong(image);
815 }
816 }
817 return offsets;
818}
819
dirk4d9863c2014-03-28 19:48:49 +0000820static MagickStatusType ReadPSDChannelRLE(Image *image,const PSDInfo *psd_info,
dirkb41f0802013-12-26 16:47:58 +0000821 const ssize_t type,MagickOffsetType *offsets,ExceptionInfo *exception)
822{
823 MagickStatusType
824 status;
825
826 size_t
827 length,
828 row_size;
829
cristy56ed31c2010-03-22 00:46:21 +0000830 ssize_t
cristyd05dca12010-07-25 02:32:32 +0000831 count,
832 y;
cristy56ed31c2010-03-22 00:46:21 +0000833
834 unsigned char
835 *compact_pixels,
836 *pixels;
837
dirkb41f0802013-12-26 16:47:58 +0000838 if (image->debug != MagickFalse)
839 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
840 " layer data is RLE compressed");
cristy56ed31c2010-03-22 00:46:21 +0000841
dirkb41f0802013-12-26 16:47:58 +0000842 row_size=GetPSDRowSize(image);
843 pixels=(unsigned char *) AcquireQuantumMemory(row_size,sizeof(*pixels));
cristy56ed31c2010-03-22 00:46:21 +0000844 if (pixels == (unsigned char *) NULL)
845 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
846 image->filename);
cristye195f262010-04-16 18:12:35 +0000847
dirkb41f0802013-12-26 16:47:58 +0000848 length=0;
849 for (y=0; y < (ssize_t) image->rows; y++)
850 if ((MagickOffsetType) length < offsets[y])
851 length=(size_t) offsets[y];
852
853 if (length > row_size + 256) // arbitrary number
854 {
855 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
856 ThrowBinaryException(ResourceLimitError,"InvalidLength",
857 image->filename);
cristy56ed31c2010-03-22 00:46:21 +0000858 }
dirkb41f0802013-12-26 16:47:58 +0000859
cristy39037ae2014-02-10 15:31:12 +0000860 compact_pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
dirkb41f0802013-12-26 16:47:58 +0000861 if (compact_pixels == (unsigned char *) NULL)
862 {
863 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
864 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
865 image->filename);
866 }
867
868 (void) ResetMagickMemory(compact_pixels,0,length*sizeof(*compact_pixels));
869
870 status=MagickTrue;
cristybb503372010-05-27 20:51:26 +0000871 for (y=0; y < (ssize_t) image->rows; y++)
cristy56ed31c2010-03-22 00:46:21 +0000872 {
dirkb41f0802013-12-26 16:47:58 +0000873 status=MagickFalse;
cristy3b004082010-08-12 19:56:38 +0000874
dirkb41f0802013-12-26 16:47:58 +0000875 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
876 if (count != (ssize_t) offsets[y])
877 break;
878
879 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
880 (ssize_t) (image->depth == 1 ? 123456 : image->depth),row_size,pixels);
881 if (count != (ssize_t) row_size)
882 break;
883
884 status=ReadPSDChannelPixels(image,psd_info->channels,y,type,pixels,
885 exception);
886 if (status == MagickFalse)
cristy56ed31c2010-03-22 00:46:21 +0000887 break;
888 }
dirkb41f0802013-12-26 16:47:58 +0000889
890 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
cristy56ed31c2010-03-22 00:46:21 +0000891 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
dirkb41f0802013-12-26 16:47:58 +0000892 return(status);
cristy56ed31c2010-03-22 00:46:21 +0000893}
894
dirkb41f0802013-12-26 16:47:58 +0000895static MagickStatusType ReadPSDChannelZip(Image *image,
896 const size_t channels,const ssize_t type,
897 const PSDCompressionType compression,const size_t compact_size,
898 ExceptionInfo *exception)
899{
900#ifdef MAGICKCORE_ZLIB_DELEGATE
901 MagickStatusType
902 status;
903
904 register unsigned char
905 *p;
906
907 size_t
908 count,
909 length,
910 packet_size,
911 row_size;
912
913 ssize_t
914 y;
915
916 unsigned char
917 *compact_pixels,
918 *pixels;
919
920 z_stream
921 stream;
922
923 if (image->debug != MagickFalse)
924 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
dirk245cc752014-01-16 18:31:58 +0000925 " layer data is ZIP compressed");
dirkb41f0802013-12-26 16:47:58 +0000926
927 compact_pixels=(unsigned char *) AcquireQuantumMemory(compact_size,
928 sizeof(*compact_pixels));
929 if (compact_pixels == (unsigned char *) NULL)
930 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
931 image->filename);
932
933 packet_size=GetPSDPacketSize(image);
934 row_size=image->columns*packet_size;
935 count=image->rows*row_size;
936
937 pixels=(unsigned char *) AcquireQuantumMemory(count,sizeof(*pixels));
938 if (pixels == (unsigned char *) NULL)
939 {
940 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
941 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
942 image->filename);
943 }
944
945 ResetMagickMemory(&stream, 0, sizeof(z_stream));
946 stream.data_type=Z_BINARY;
947 (void) ReadBlob(image,compact_size,compact_pixels);
948
949 stream.next_in=(Bytef *)compact_pixels;
cristy0e274112014-03-25 14:09:19 +0000950 stream.avail_in=(unsigned int) compact_size;
dirkb41f0802013-12-26 16:47:58 +0000951 stream.next_out=(Bytef *)pixels;
cristy0e274112014-03-25 14:09:19 +0000952 stream.avail_out=(unsigned int) count;
dirkb41f0802013-12-26 16:47:58 +0000953
954 if(inflateInit(&stream) == Z_OK)
955 {
956 int
957 ret;
958
959 while (stream.avail_out > 0)
960 {
961 ret=inflate(&stream, Z_SYNC_FLUSH);
962 if (ret != Z_OK && ret != Z_STREAM_END)
963 {
964 compact_pixels=(unsigned char *) RelinquishMagickMemory(
965 compact_pixels);
966 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
967 return(MagickFalse);
968 }
969 }
970 }
971
972 if (compression == ZipWithPrediction)
973 {
974 p=pixels;
975 while(count > 0)
976 {
977 length=image->columns;
978 while(--length)
979 {
980 if (packet_size == 2)
981 {
982 p[2]+=p[0]+((p[1]+p[3]) >> 8);
983 p[3]+=p[1];
984 }
985 else
986 *(p+1)+=*p;
987 p+=packet_size;
988 }
989 p+=packet_size;
990 count-=row_size;
991 }
992 }
993
994 status=MagickTrue;
995 p=pixels;
996 for (y=0; y < (ssize_t) image->rows; y++)
997 {
998 status=ReadPSDChannelPixels(image,channels,y,type,p,exception);
999 if (status == MagickFalse)
1000 break;
1001
1002 p+=row_size;
1003 }
1004
1005 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
1006 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1007 return(status);
1008#else
1009 magick_unreferenced(image);
1010 magick_unreferenced(channels);
1011 magick_unreferenced(type);
1012 magick_unreferenced(compression);
1013 magick_unreferenced(compact_size);
1014 magick_unreferenced(exception);
1015 return(MagickFalse);
1016#endif
1017}
1018
dirk4d9863c2014-03-28 19:48:49 +00001019static MagickStatusType ReadPSDChannel(Image *image,const PSDInfo *psd_info,
dirkb41f0802013-12-26 16:47:58 +00001020 const LayerInfo* layer_info,const size_t channel,
1021 const PSDCompressionType compression,ExceptionInfo *exception)
1022{
1023 MagickOffsetType
1024 offset;
1025
1026 MagickStatusType
1027 status;
1028
dirk245cc752014-01-16 18:31:58 +00001029 if (layer_info->channel_info[channel].type < -1)
1030 {
1031 /* ignore user supplied layer mask */
1032 SeekBlob(image,layer_info->channel_info[channel].size-2,SEEK_CUR);
1033 return(MagickTrue);
1034 }
1035
dirkb41f0802013-12-26 16:47:58 +00001036 offset=TellBlob(image);
1037 status=MagickTrue;
1038 switch(compression)
1039 {
1040 case Raw:
1041 return(ReadPSDChannelRaw(image,psd_info->channels,
1042 layer_info->channel_info[channel].type,exception));
1043 case RLE:
1044 {
1045 MagickOffsetType
1046 *offsets;
1047
1048 offsets=ReadPSDRLEOffsets(image,psd_info,image->rows);
1049 if (offsets == (MagickOffsetType *) NULL)
1050 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1051 image->filename);
1052 status=ReadPSDChannelRLE(image,psd_info,
1053 layer_info->channel_info[channel].type,offsets,exception);
1054 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1055 }
1056 break;
1057 case ZipWithPrediction:
1058 case ZipWithoutPrediction:
1059#ifdef MAGICKCORE_ZLIB_DELEGATE
1060 status=ReadPSDChannelZip(image,layer_info->channels,
1061 layer_info->channel_info[channel].type,compression,
1062 layer_info->channel_info[channel].size-2,exception);
1063#else
1064 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1065 (void) ThrowMagickException(exception,GetMagickModule(),
1066 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn",
1067 "'%s' (ZLIB)",image->filename);
1068#endif
1069 break;
1070 default:
1071 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1072 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
1073 "CompressionNotSupported","'%.20g'",(double) compression);
1074 break;
1075 }
1076
1077 if (status == MagickFalse)
1078 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1079
1080 return(status);
1081}
1082
dirk4d9863c2014-03-28 19:48:49 +00001083static MagickStatusType ReadPSDLayer(Image *image,const PSDInfo *psd_info,
dirkb41f0802013-12-26 16:47:58 +00001084 LayerInfo* layer_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001085{
cristy3ed852e2009-09-05 21:47:34 +00001086 char
dirkb41f0802013-12-26 16:47:58 +00001087 message[MaxTextExtent];
cristy3ed852e2009-09-05 21:47:34 +00001088
dirkb41f0802013-12-26 16:47:58 +00001089 MagickStatusType
1090 status;
1091
1092 PSDCompressionType
1093 compression;
1094
1095 ssize_t
1096 j;
1097
1098 if (image->debug != MagickFalse)
1099 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1100 " setting up new layer image");
1101 (void) SetImageBackgroundColor(layer_info->image,exception);
1102 layer_info->image->compose=PSDBlendModeToCompositeOperator(
1103 layer_info->blendkey);
1104 if (layer_info->visible == MagickFalse)
1105 layer_info->image->compose=NoCompositeOp;
1106 if (psd_info->mode == CMYKMode)
1107 SetImageColorspace(layer_info->image,CMYKColorspace,exception);
cristyd8083a62014-02-01 13:53:44 +00001108 if ((psd_info->mode == BitmapMode) || (psd_info->mode == GrayscaleMode) ||
dirkb41f0802013-12-26 16:47:58 +00001109 (psd_info->mode == DuotoneMode))
1110 SetImageColorspace(layer_info->image,GRAYColorspace,exception);
1111 /*
1112 Set up some hidden attributes for folks that need them.
1113 */
1114 (void) FormatLocaleString(message,MaxTextExtent,"%.20gld",
1115 (double) layer_info->page.x);
1116 (void) SetImageArtifact(layer_info->image,"psd:layer.x",message);
1117 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",
1118 (double) layer_info->page.y);
1119 (void) SetImageArtifact(layer_info->image,"psd:layer.y",message);
1120 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",(double)
1121 layer_info->opacity);
1122 (void) SetImageArtifact(layer_info->image,"psd:layer.opacity",message);
1123 (void) SetImageProperty(layer_info->image,"label",(char *) layer_info->name,
1124 exception);
1125
1126 status=MagickTrue;
dirkb41f0802013-12-26 16:47:58 +00001127 for (j=0; j < (ssize_t) layer_info->channels; j++)
1128 {
1129 if (image->debug != MagickFalse)
1130 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1131 " reading data for channel %.20g",(double) j);
1132
1133 compression=(PSDCompressionType) ReadBlobMSBShort(layer_info->image);
1134 layer_info->image->compression=ConvertPSDCompression(compression);
dirk6f205312014-01-31 22:25:10 +00001135 if (layer_info->channel_info[j].type == -1)
1136 layer_info->image->alpha_trait=BlendPixelTrait;
1137 status=ReadPSDChannel(layer_info->image,psd_info,layer_info,j,
1138 compression,exception);
dirkb41f0802013-12-26 16:47:58 +00001139
1140 if (status == MagickFalse)
1141 break;
1142 }
1143
dirk6f205312014-01-31 22:25:10 +00001144 if (status != MagickFalse)
dirkb41f0802013-12-26 16:47:58 +00001145 status=CorrectPSDOpacity(layer_info,exception);
1146
1147 if (status != MagickFalse && layer_info->image->colorspace == CMYKColorspace)
1148 (void) NegateImage(layer_info->image,MagickFalse,exception);
1149
1150 return(status);
1151}
1152
dirk4d9863c2014-03-28 19:48:49 +00001153static MagickStatusType ReadPSDLayers(Image *image,const ImageInfo *image_info,
1154 const PSDInfo *psd_info,const MagickBooleanType skip_layers,
1155 ExceptionInfo *exception)
dirkb41f0802013-12-26 16:47:58 +00001156{
1157 char
1158 type[4];
cristy3ed852e2009-09-05 21:47:34 +00001159
cristy3ed852e2009-09-05 21:47:34 +00001160 LayerInfo
1161 *layer_info;
1162
cristy56ed31c2010-03-22 00:46:21 +00001163 MagickSizeType
cristy56ed31c2010-03-22 00:46:21 +00001164 size;
1165
dirkb41f0802013-12-26 16:47:58 +00001166 MagickStatusType
1167 status;
cristydede49f2010-08-20 20:26:26 +00001168
cristybb503372010-05-27 20:51:26 +00001169 register ssize_t
dirkb41f0802013-12-26 16:47:58 +00001170 i;
cristydc05fc22011-01-22 19:43:15 +00001171
cristy3ed852e2009-09-05 21:47:34 +00001172 ssize_t
cristydc05fc22011-01-22 19:43:15 +00001173 count,
1174 j,
dirkb41f0802013-12-26 16:47:58 +00001175 number_layers;
1176
1177 size=GetPSDSize(psd_info,image);
1178 if (size == 0)
1179 {
1180 size_t
1181 quantum;
1182
1183 /*
1184 Skip layers & masks.
1185 */
1186 quantum=psd_info->version == 1 ? 4UL : 8UL;
1187 (void) ReadBlobMSBLong(image);
1188 count=ReadBlob(image,4,(unsigned char *) type);
1189 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1190 {
cristy1b875d22014-05-28 14:27:23 +00001191 if (DiscardBlobBytes(image,(MagickSizeType) (size-quantum-8)) == MagickFalse)
dirkb41f0802013-12-26 16:47:58 +00001192 ThrowFileException(exception,CorruptImageError,
1193 "UnexpectedEndOfFile",image->filename);
1194 }
1195 else
1196 {
1197 count=ReadBlob(image,4,(unsigned char *) type);
1198 if ((count != 0) && (LocaleNCompare(type,"Lr16",4) == 0))
1199 size=GetPSDSize(psd_info,image);
1200 else
cristy1fda43d2014-05-28 14:24:51 +00001201 if (DiscardBlobBytes(image,(MagickSizeType) (size-quantum-12)) == MagickFalse)
dirkb41f0802013-12-26 16:47:58 +00001202 ThrowFileException(exception,CorruptImageError,
1203 "UnexpectedEndOfFile",image->filename);
1204 }
1205 }
dirkb41f0802013-12-26 16:47:58 +00001206 status=MagickTrue;
1207 if (size != 0)
1208 {
1209 layer_info=(LayerInfo *) NULL;
1210 number_layers=(short) ReadBlobMSBShort(image);
1211
dirkb41f0802013-12-26 16:47:58 +00001212 if (number_layers < 0)
1213 {
1214 /*
dirk6f205312014-01-31 22:25:10 +00001215 The first alpha channel in the merged result contains the
1216 transparency data for the merged result.
dirkb41f0802013-12-26 16:47:58 +00001217 */
1218 number_layers=MagickAbsoluteValue(number_layers);
1219 if (image->debug != MagickFalse)
1220 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1221 " negative layer count corrected for");
dirk6f205312014-01-31 22:25:10 +00001222 image->alpha_trait=BlendPixelTrait;
dirkb41f0802013-12-26 16:47:58 +00001223 }
1224
dirkfcaec622014-01-31 23:15:51 +00001225 if (skip_layers != MagickFalse)
1226 return(MagickTrue);
1227
dirkb41f0802013-12-26 16:47:58 +00001228 if (image->debug != MagickFalse)
1229 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1230 " image contains %.20g layers",(double) number_layers);
1231
1232 if (number_layers == 0)
1233 return(MagickFalse);
1234
1235 layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers,
1236 sizeof(*layer_info));
1237 if (layer_info == (LayerInfo *) NULL)
1238 {
1239 if (image->debug != MagickFalse)
1240 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1241 " allocation of LayerInfo failed");
1242 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1243 image->filename);
1244 }
1245 (void) ResetMagickMemory(layer_info,0,(size_t) number_layers*
1246 sizeof(*layer_info));
1247
1248 for (i=0; i < number_layers; i++)
1249 {
1250 int
1251 x,
1252 y;
1253
1254 if (image->debug != MagickFalse)
1255 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1256 " reading layer #%.20g",(double) i+1);
1257 layer_info[i].page.y=(int) ReadBlobMSBLong(image);
1258 layer_info[i].page.x=(int) ReadBlobMSBLong(image);
1259 y=(int) ReadBlobMSBLong(image);
1260 x=(int) ReadBlobMSBLong(image);
1261 layer_info[i].page.width=(ssize_t) (x-layer_info[i].page.x);
1262 layer_info[i].page.height=(ssize_t) (y-layer_info[i].page.y);
1263 layer_info[i].channels=ReadBlobMSBShort(image);
1264 if (layer_info[i].channels > MaxPSDChannels)
1265 {
dirkbd8bd852013-12-28 22:55:19 +00001266 layer_info=DestroyLayerInfo(layer_info,number_layers);
dirkb41f0802013-12-26 16:47:58 +00001267 ThrowBinaryException(CorruptImageError,"MaximumChannelsExceeded",
1268 image->filename);
1269 }
1270 if (image->debug != MagickFalse)
1271 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1272 " offset(%.20g,%.20g), size(%.20g,%.20g), channels=%.20g",
1273 (double) layer_info[i].page.x,(double) layer_info[i].page.y,
1274 (double) layer_info[i].page.height,(double)
1275 layer_info[i].page.width,(double) layer_info[i].channels);
1276 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1277 {
1278 layer_info[i].channel_info[j].type=(short) ReadBlobMSBShort(image);
1279 layer_info[i].channel_info[j].size=(size_t) GetPSDSize(psd_info,
1280 image);
1281 if (image->debug != MagickFalse)
1282 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1283 " channel[%.20g]: type=%.20g, size=%.20g",(double) j,
1284 (double) layer_info[i].channel_info[j].type,
1285 (double) layer_info[i].channel_info[j].size);
1286 }
1287 count=ReadBlob(image,4,(unsigned char *) type);
1288 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1289 {
1290 if (image->debug != MagickFalse)
1291 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1292 " layer type was %.4s instead of 8BIM", type);
dirkbd8bd852013-12-28 22:55:19 +00001293 layer_info=DestroyLayerInfo(layer_info,number_layers);
dirkb41f0802013-12-26 16:47:58 +00001294 ThrowBinaryException(CorruptImageError,"ImproperImageHeader",
1295 image->filename);
1296 }
1297 count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey);
dirkb52a4b72013-12-26 16:55:20 +00001298 layer_info[i].opacity=(Quantum) ScaleCharToQuantum((unsigned char)
cristyd8083a62014-02-01 13:53:44 +00001299 ReadBlobByte(image));
dirkb41f0802013-12-26 16:47:58 +00001300 layer_info[i].clipping=(unsigned char) ReadBlobByte(image);
1301 layer_info[i].flags=(unsigned char) ReadBlobByte(image);
1302 layer_info[i].visible=!(layer_info[i].flags & 0x02);
1303 if (image->debug != MagickFalse)
1304 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1305 " blend=%.4s, opacity=%.20g, clipping=%s, flags=%d, visible=%s",
1306 layer_info[i].blendkey,(double) layer_info[i].opacity,
1307 layer_info[i].clipping ? "true" : "false",layer_info[i].flags,
1308 layer_info[i].visible ? "true" : "false");
1309 (void) ReadBlobByte(image); /* filler */
1310
1311 size=ReadBlobMSBLong(image);
1312 if (size != 0)
1313 {
1314 MagickSizeType
1315 combined_length,
1316 length;
1317
1318 if (image->debug != MagickFalse)
1319 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1320 " layer contains additional info");
1321 length=ReadBlobMSBLong(image);
1322 combined_length=length+4;
1323 if (length != 0)
1324 {
1325 /*
1326 Layer mask info.
1327 */
1328 layer_info[i].mask.y=(int) ReadBlobMSBLong(image);
1329 layer_info[i].mask.x=(int) ReadBlobMSBLong(image);
1330 layer_info[i].mask.height=(size_t)
1331 (ReadBlobMSBLong(image)-layer_info[i].mask.y);
1332 layer_info[i].mask.width=(size_t)
1333 (ReadBlobMSBLong(image)-layer_info[i].mask.x);
1334 if (image->debug != MagickFalse)
1335 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1336 " layer mask: offset(%.20g,%.20g), size(%.20g,%.20g), length=%.20g",
1337 (double) layer_info[i].mask.x,(double)
1338 layer_info[i].mask.y,(double) layer_info[i].mask.width,
1339 (double) layer_info[i].mask.height,(double)
1340 ((MagickOffsetType) length)-16);
1341 /*
1342 Skip over the rest of the layer mask information.
1343 */
cristy1b875d22014-05-28 14:27:23 +00001344 if (DiscardBlobBytes(image,(MagickSizeType) (length-16)) == MagickFalse)
dirkb41f0802013-12-26 16:47:58 +00001345 {
dirkbd8bd852013-12-28 22:55:19 +00001346 layer_info=DestroyLayerInfo(layer_info,number_layers);
dirkb41f0802013-12-26 16:47:58 +00001347 ThrowFileException(exception,CorruptImageError,
1348 "UnexpectedEndOfFile",image->filename);
1349 }
1350 }
1351 length=ReadBlobMSBLong(image);
1352 combined_length+=length+4;
1353 if (length != 0)
1354 {
1355 /*
1356 Layer blending ranges info.
1357 */
1358 if (image->debug != MagickFalse)
1359 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1360 " layer blending ranges: length=%.20g",(double)
1361 ((MagickOffsetType) length));
1362 /*
1363 We read it, but don't use it...
1364 */
1365 for (j=0; j < (ssize_t) (length); j+=8)
1366 {
1367 size_t blend_source=ReadBlobMSBLong(image);
1368 size_t blend_dest=ReadBlobMSBLong(image);
1369 if (image->debug != MagickFalse)
1370 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1371 " source(%x), dest(%x)",(unsigned int)
1372 blend_source,(unsigned int) blend_dest);
1373 }
1374 }
1375 /*
1376 Layer name.
1377 */
1378 length=(size_t) ReadBlobByte(image);
1379 combined_length+=length+1;
1380 for (j=0; j < (ssize_t) length; j++)
1381 layer_info[i].name[j]=(unsigned char) ReadBlobByte(image);
1382 layer_info[i].name[j]='\0';
1383 if (image->debug != MagickFalse)
1384 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1385 " layer name: %s",layer_info[i].name);
1386 /*
1387 Skip the rest of the variable data until we support it.
1388 */
1389 if (image->debug != MagickFalse)
1390 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1391 " unsupported data: length=%.20g",(double)
1392 ((MagickOffsetType) (size-combined_length)));
cristy1b875d22014-05-28 14:27:23 +00001393 if (DiscardBlobBytes(image,(MagickSizeType) (size-combined_length)) == MagickFalse)
dirkb41f0802013-12-26 16:47:58 +00001394 {
dirkbd8bd852013-12-28 22:55:19 +00001395 layer_info=DestroyLayerInfo(layer_info,number_layers);
dirkb41f0802013-12-26 16:47:58 +00001396 ThrowBinaryException(CorruptImageError,
1397 "UnexpectedEndOfFile",image->filename);
1398 }
1399 }
1400 }
1401
1402 for (i=0; i < number_layers; i++)
1403 {
dirkb41f0802013-12-26 16:47:58 +00001404 if ((layer_info[i].page.width == 0) ||
1405 (layer_info[i].page.height == 0))
1406 {
1407 if (image->debug != MagickFalse)
1408 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1409 " layer data is empty");
1410 continue;
1411 }
1412
dirkb41f0802013-12-26 16:47:58 +00001413 /*
1414 Allocate layered image.
1415 */
1416 layer_info[i].image=CloneImage(image,layer_info[i].page.width,
1417 layer_info[i].page.height,MagickFalse,exception);
1418 if (layer_info[i].image == (Image *) NULL)
1419 {
dirkbd8bd852013-12-28 22:55:19 +00001420 layer_info=DestroyLayerInfo(layer_info,number_layers);
dirkb41f0802013-12-26 16:47:58 +00001421 if (image->debug != MagickFalse)
1422 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1423 " allocation of image for layer %.20g failed",(double) i);
1424 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1425 image->filename);
1426 }
1427 }
1428
dirk4d9863c2014-03-28 19:48:49 +00001429 if (image_info->ping == MagickFalse)
dirkb41f0802013-12-26 16:47:58 +00001430 {
dirk4d9863c2014-03-28 19:48:49 +00001431 for (i=0; i < number_layers; i++)
dirkb41f0802013-12-26 16:47:58 +00001432 {
dirk4d9863c2014-03-28 19:48:49 +00001433 if (layer_info[i].image == (Image *) NULL)
dirkb41f0802013-12-26 16:47:58 +00001434 {
dirk4d9863c2014-03-28 19:48:49 +00001435 for (j=0; j < layer_info[i].channels; j++)
1436 {
cristy1b875d22014-05-28 14:27:23 +00001437 if (DiscardBlobBytes(image,(MagickSizeType)
dirk4d9863c2014-03-28 19:48:49 +00001438 layer_info[i].channel_info[j].size) == MagickFalse)
1439 {
1440 layer_info=DestroyLayerInfo(layer_info,number_layers);
1441 ThrowBinaryException(CorruptImageError,
1442 "UnexpectedEndOfFile",image->filename);
1443 }
1444 }
1445 continue;
dirkb41f0802013-12-26 16:47:58 +00001446 }
dirk4d9863c2014-03-28 19:48:49 +00001447
1448 if (image->debug != MagickFalse)
1449 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1450 " reading data for layer %.20g",(double) i);
1451
1452 status=ReadPSDLayer(image,psd_info,&layer_info[i],exception);
1453 if (status == MagickFalse)
1454 break;
1455
1456 status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType)
1457 number_layers);
1458 if (status == MagickFalse)
1459 break;
dirkb41f0802013-12-26 16:47:58 +00001460 }
dirkb41f0802013-12-26 16:47:58 +00001461 }
1462
dirkb41f0802013-12-26 16:47:58 +00001463 if (status != MagickFalse)
1464 {
1465 for (i=0; i < number_layers; i++)
1466 {
1467 if (layer_info[i].image == (Image *) NULL)
1468 {
1469 for (j=i; j < number_layers - 1; j++)
1470 layer_info[j] = layer_info[j+1];
1471 number_layers--;
1472 i--;
1473 }
1474 }
1475
1476 if (number_layers > 0)
1477 {
1478 for (i=0; i < number_layers; i++)
1479 {
1480 if (i > 0)
1481 layer_info[i].image->previous=layer_info[i-1].image;
1482 if (i < (number_layers-1))
1483 layer_info[i].image->next=layer_info[i+1].image;
1484 layer_info[i].image->page=layer_info[i].page;
1485 }
1486 image->next=layer_info[0].image;
1487 layer_info[0].image->previous=image;
1488 }
1489 }
1490 layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info);
1491 }
1492
1493 return(status);
1494}
1495
dirk4d9863c2014-03-28 19:48:49 +00001496static MagickStatusType ReadPSDMergedImage(Image* image,
1497 const PSDInfo* psd_info,ExceptionInfo *exception)
dirkb41f0802013-12-26 16:47:58 +00001498{
1499 MagickOffsetType
1500 *offsets;
1501
1502 MagickStatusType
1503 status;
1504
1505 PSDCompressionType
1506 compression;
1507
1508 register ssize_t
1509 i;
1510
1511 compression=(PSDCompressionType) ReadBlobMSBShort(image);
1512 image->compression=ConvertPSDCompression(compression);
1513
1514 if (compression != Raw && compression != RLE)
1515 {
1516 (void) ThrowMagickException(exception,GetMagickModule(),
1517 TypeWarning,"CompressionNotSupported","'%.20g'",(double) compression);
1518 return(MagickFalse);
1519 }
1520
1521 offsets=(MagickOffsetType *) NULL;
1522 if (compression == RLE)
1523 {
1524 offsets=ReadPSDRLEOffsets(image,psd_info,image->rows*psd_info->channels);
1525 if (offsets == (MagickOffsetType *) NULL)
1526 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1527 image->filename);
1528 }
1529
1530 status=MagickTrue;
1531 for (i=0; i < (ssize_t) psd_info->channels; i++)
1532 {
1533 if (compression == RLE)
1534 status=ReadPSDChannelRLE(image,psd_info,i,offsets+(i*image->rows),
1535 exception);
1536 else
1537 status=ReadPSDChannelRaw(image,psd_info->channels,i,exception);
1538
1539 if (status == MagickFalse)
1540 break;
1541 status=SetImageProgress(image,LoadImagesTag,i,psd_info->channels);
1542 if (status == MagickFalse)
1543 break;
1544 }
1545
1546 if (image->colorspace == CMYKColorspace)
1547 (void) NegateImage(image,MagickFalse,exception);
1548
1549 if (offsets != (MagickOffsetType *) NULL)
1550 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1551
1552 return(status);
1553}
1554
1555static Image *ReadPSDImage(const ImageInfo *image_info,
1556 ExceptionInfo *exception)
1557{
1558 Image
1559 *image;
1560
1561 MagickBooleanType
dirk18c0e4d2014-02-22 22:24:05 +00001562 has_merged_image,
dirkb41f0802013-12-26 16:47:58 +00001563 skip_layers,
1564 status;
1565
1566 MagickOffsetType
1567 offset;
1568
1569 MagickSizeType
1570 length;
1571
1572 PSDInfo
1573 psd_info;
1574
1575 register ssize_t
1576 i;
1577
1578 ssize_t
1579 count;
cristy3ed852e2009-09-05 21:47:34 +00001580
cristy3ed852e2009-09-05 21:47:34 +00001581 unsigned char
1582 *data;
1583
cristy3ed852e2009-09-05 21:47:34 +00001584 /*
1585 Open image file.
1586 */
1587 assert(image_info != (const ImageInfo *) NULL);
1588 assert(image_info->signature == MagickSignature);
1589 if (image_info->debug != MagickFalse)
1590 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1591 image_info->filename);
1592 assert(exception != (ExceptionInfo *) NULL);
1593 assert(exception->signature == MagickSignature);
dirkb41f0802013-12-26 16:47:58 +00001594
cristy9950d572011-10-01 18:22:35 +00001595 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001596 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1597 if (status == MagickFalse)
1598 {
1599 image=DestroyImageList(image);
1600 return((Image *) NULL);
1601 }
1602 /*
1603 Read image header.
1604 */
1605 count=ReadBlob(image,4,(unsigned char *) psd_info.signature);
1606 psd_info.version=ReadBlobMSBShort(image);
cristy50aea4a2010-03-09 17:37:44 +00001607 if ((count == 0) || (LocaleNCompare(psd_info.signature,"8BPS",4) != 0) ||
cristy2d3d87f2010-03-01 00:23:08 +00001608 ((psd_info.version != 1) && (psd_info.version != 2)))
cristy3ed852e2009-09-05 21:47:34 +00001609 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1610 count=ReadBlob(image,6,psd_info.reserved);
1611 psd_info.channels=ReadBlobMSBShort(image);
1612 if (psd_info.channels > MaxPSDChannels)
1613 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
1614 psd_info.rows=ReadBlobMSBLong(image);
1615 psd_info.columns=ReadBlobMSBLong(image);
cristy2d3d87f2010-03-01 00:23:08 +00001616 if ((psd_info.version == 1) && ((psd_info.rows > 30000) ||
1617 (psd_info.columns > 30000)))
1618 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +00001619 psd_info.depth=ReadBlobMSBShort(image);
cristy2d3d87f2010-03-01 00:23:08 +00001620 if ((psd_info.depth != 1) && (psd_info.depth != 8) && (psd_info.depth != 16))
1621 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +00001622 psd_info.mode=ReadBlobMSBShort(image);
1623 if (image->debug != MagickFalse)
1624 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001625 " Image is %.20g x %.20g with channels=%.20g, depth=%.20g, mode=%s",
1626 (double) psd_info.columns,(double) psd_info.rows,(double)
1627 psd_info.channels,(double) psd_info.depth,ModeToString((PSDImageType)
1628 psd_info.mode));
cristy3ed852e2009-09-05 21:47:34 +00001629 /*
1630 Initialize image.
1631 */
1632 image->depth=psd_info.depth;
1633 image->columns=psd_info.columns;
1634 image->rows=psd_info.rows;
cristyea1a8aa2011-10-20 13:24:06 +00001635 if (SetImageBackgroundColor(image,exception) == MagickFalse)
cristy95524f92010-02-16 18:44:34 +00001636 {
cristy95524f92010-02-16 18:44:34 +00001637 image=DestroyImageList(image);
1638 return((Image *) NULL);
1639 }
cristy3ed852e2009-09-05 21:47:34 +00001640 if (psd_info.mode == LabMode)
cristye2c4f182012-05-12 14:11:53 +00001641 SetImageColorspace(image,LabColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001642 if (psd_info.mode == CMYKMode)
dirk84affaa2014-05-27 11:09:16 +00001643 {
1644 SetImageColorspace(image,CMYKColorspace,exception);
dirkdeca4042014-05-29 06:31:05 +00001645 image->alpha_trait=psd_info.channels > 4 ? BlendPixelTrait :
dirk84affaa2014-05-27 11:09:16 +00001646 UndefinedPixelTrait;
1647 }
1648 else if ((psd_info.mode == BitmapMode) || (psd_info.mode == GrayscaleMode) ||
cristy3ed852e2009-09-05 21:47:34 +00001649 (psd_info.mode == DuotoneMode))
1650 {
cristyafd0fd12013-09-17 12:41:18 +00001651 status=AcquireImageColormap(image,psd_info.depth != 16 ? 256 : 65536,
1652 exception);
1653 if (status == MagickFalse)
cristy52cf7f12010-02-07 18:07:56 +00001654 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00001655 if (image->debug != MagickFalse)
1656 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristydede49f2010-08-20 20:26:26 +00001657 " Image colormap allocated");
cristye2c4f182012-05-12 14:11:53 +00001658 SetImageColorspace(image,GRAYColorspace,exception);
dirkdeca4042014-05-29 06:31:05 +00001659 image->alpha_trait=psd_info.channels > 1 ? BlendPixelTrait :
dirk84affaa2014-05-27 11:09:16 +00001660 UndefinedPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00001661 }
dirk84affaa2014-05-27 11:09:16 +00001662 else
dirkdeca4042014-05-29 06:31:05 +00001663 image->alpha_trait=psd_info.channels > 3 ? BlendPixelTrait :
dirk84affaa2014-05-27 11:09:16 +00001664 UndefinedPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00001665 /*
1666 Read PSD raster colormap only present for indexed and duotone images.
1667 */
1668 length=ReadBlobMSBLong(image);
1669 if (length != 0)
1670 {
1671 if (image->debug != MagickFalse)
1672 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1673 " reading colormap");
1674 if (psd_info.mode == DuotoneMode)
1675 {
1676 /*
1677 Duotone image data; the format of this data is undocumented.
1678 */
cristy56ed31c2010-03-22 00:46:21 +00001679 data=(unsigned char *) AcquireQuantumMemory((size_t) length,
1680 sizeof(*data));
cristy3ed852e2009-09-05 21:47:34 +00001681 if (data == (unsigned char *) NULL)
1682 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy56ed31c2010-03-22 00:46:21 +00001683 count=ReadBlob(image,(size_t) length,data);
cristy3ed852e2009-09-05 21:47:34 +00001684 data=(unsigned char *) RelinquishMagickMemory(data);
1685 }
1686 else
1687 {
1688 /*
1689 Read PSD raster colormap.
1690 */
cristyd8083a62014-02-01 13:53:44 +00001691 if (AcquireImageColormap(image,(size_t) (length/3),exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001692 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00001693 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001694 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
1695 ReadBlobByte(image));
cristybb503372010-05-27 20:51:26 +00001696 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001697 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
1698 ReadBlobByte(image));
cristybb503372010-05-27 20:51:26 +00001699 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001700 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
1701 ReadBlobByte(image));
cristy8a46d822012-08-28 23:32:39 +00001702 image->alpha_trait=UndefinedPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00001703 }
1704 }
dirk18c0e4d2014-02-22 22:24:05 +00001705 has_merged_image=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001706 length=ReadBlobMSBLong(image);
1707 if (length != 0)
1708 {
1709 unsigned char
1710 *blocks;
1711
1712 /*
1713 Image resources block.
1714 */
1715 if (image->debug != MagickFalse)
1716 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy2b9582a2011-07-04 17:38:56 +00001717 " reading image resource blocks - %.20g bytes",(double)
1718 ((MagickOffsetType) length));
cristy56ed31c2010-03-22 00:46:21 +00001719 blocks=(unsigned char *) AcquireQuantumMemory((size_t) length,
1720 sizeof(*blocks));
cristy3ed852e2009-09-05 21:47:34 +00001721 if (blocks == (unsigned char *) NULL)
1722 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy56ed31c2010-03-22 00:46:21 +00001723 count=ReadBlob(image,(size_t) length,blocks);
1724 if ((count != (ssize_t) length) ||
cristy3ed852e2009-09-05 21:47:34 +00001725 (LocaleNCompare((char *) blocks,"8BIM",4) != 0))
1726 {
1727 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
1728 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1729 }
dirk18c0e4d2014-02-22 22:24:05 +00001730 (void) ParseImageResourceBlocks(image,blocks,(size_t) length,
1731 &has_merged_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001732 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
1733 }
1734 /*
cristy3ed852e2009-09-05 21:47:34 +00001735 Layer and mask block.
1736 */
cristy2d3d87f2010-03-01 00:23:08 +00001737 length=GetPSDSize(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +00001738 if (length == 8)
1739 {
1740 length=ReadBlobMSBLong(image);
1741 length=ReadBlobMSBLong(image);
1742 }
dirkb41f0802013-12-26 16:47:58 +00001743 offset=TellBlob(image);
1744 skip_layers=MagickFalse;
dirk18c0e4d2014-02-22 22:24:05 +00001745 if ((image_info->number_scenes == 1) && (image_info->scene == 0) &&
1746 (has_merged_image != MagickFalse))
cristyd4297022010-09-16 22:59:09 +00001747 {
cristy374b8ad2012-02-01 20:55:21 +00001748 if (image->debug != MagickFalse)
1749 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1750 " read composite only");
dirkb41f0802013-12-26 16:47:58 +00001751 skip_layers=MagickTrue;
cristyd4297022010-09-16 22:59:09 +00001752 }
cristy3ed852e2009-09-05 21:47:34 +00001753 if (length == 0)
1754 {
1755 if (image->debug != MagickFalse)
1756 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1757 " image has no layers");
1758 }
1759 else
1760 {
dirk4d9863c2014-03-28 19:48:49 +00001761 if (ReadPSDLayers(image,image_info,&psd_info,skip_layers,exception) !=
1762 MagickTrue)
dirk6f205312014-01-31 22:25:10 +00001763 {
1764 (void) CloseBlob(image);
1765 return((Image *) NULL);
1766 }
cristy3ed852e2009-09-05 21:47:34 +00001767
dirkb41f0802013-12-26 16:47:58 +00001768 /*
1769 Skip the rest of the layer and mask information.
1770 */
1771 SeekBlob(image,offset+length,SEEK_SET);
cristy3ed852e2009-09-05 21:47:34 +00001772 }
dirk4d9863c2014-03-28 19:48:49 +00001773 /*
1774 If we are only "pinging" the image, then we're done - so return.
1775 */
1776 if (image_info->ping != MagickFalse)
1777 {
1778 (void) CloseBlob(image);
1779 return(GetFirstImageInList(image));
1780 }
cristy3ed852e2009-09-05 21:47:34 +00001781 /*
dirkb41f0802013-12-26 16:47:58 +00001782 Read the precombined layer, present for PSD < 4 compatibility.
cristy3ed852e2009-09-05 21:47:34 +00001783 */
1784 if (image->debug != MagickFalse)
1785 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1786 " reading the precombined layer");
dirk18c0e4d2014-02-22 22:24:05 +00001787 if (has_merged_image != MagickFalse || GetImageListLength(image) == 1)
dirk2de94f52014-03-15 21:36:56 +00001788 has_merged_image=(MagickBooleanType) ReadPSDMergedImage(image,&psd_info,
1789 exception);
1790 if (has_merged_image == MagickFalse && GetImageListLength(image) == 1 &&
1791 length != 0)
1792 {
1793 SeekBlob(image,offset,SEEK_SET);
dirk4d9863c2014-03-28 19:48:49 +00001794 if (ReadPSDLayers(image,image_info,&psd_info,MagickFalse,exception) !=
1795 MagickTrue)
dirk2de94f52014-03-15 21:36:56 +00001796 {
1797 (void) CloseBlob(image);
1798 return((Image *) NULL);
1799 }
1800 }
1801 if (has_merged_image == MagickFalse && GetImageListLength(image) > 1)
dirk18c0e4d2014-02-22 22:24:05 +00001802 {
1803 Image
1804 *merged;
1805
1806 SetImageAlphaChannel(image,TransparentAlphaChannel,exception);
1807 image->background_color.alpha=TransparentAlpha;
1808 merged=MergeImageLayers(image,FlattenLayer,exception);
1809 ReplaceImageInList(&image,merged);
1810 }
cristy3ed852e2009-09-05 21:47:34 +00001811 (void) CloseBlob(image);
1812 return(GetFirstImageInList(image));
1813}
1814
1815/*
1816%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1817% %
1818% %
1819% %
1820% R e g i s t e r P S D I m a g e %
1821% %
1822% %
1823% %
1824%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1825%
1826% RegisterPSDImage() adds properties for the PSD image format to
1827% the list of supported formats. The properties include the image format
1828% tag, a method to read and/or write the format, whether the format
1829% supports the saving of more than one frame to the same file or blob,
1830% whether the format supports native in-memory I/O, and a brief
1831% description of the format.
1832%
1833% The format of the RegisterPSDImage method is:
1834%
cristybb503372010-05-27 20:51:26 +00001835% size_t RegisterPSDImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001836%
1837*/
cristybb503372010-05-27 20:51:26 +00001838ModuleExport size_t RegisterPSDImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001839{
1840 MagickInfo
1841 *entry;
1842
cristyb4233012010-02-28 20:09:14 +00001843 entry=SetMagickInfo("PSB");
1844 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1845 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1846 entry->magick=(IsImageFormatHandler *) IsPSD;
cristyffaf9782011-04-13 19:50:51 +00001847 entry->seekable_stream=MagickTrue;
cristyb4233012010-02-28 20:09:14 +00001848 entry->description=ConstantString("Adobe Large Document Format");
1849 entry->module=ConstantString("PSD");
1850 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001851 entry=SetMagickInfo("PSD");
1852 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1853 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1854 entry->magick=(IsImageFormatHandler *) IsPSD;
cristyffaf9782011-04-13 19:50:51 +00001855 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001856 entry->description=ConstantString("Adobe Photoshop bitmap");
1857 entry->module=ConstantString("PSD");
1858 (void) RegisterMagickInfo(entry);
1859 return(MagickImageCoderSignature);
1860}
1861
1862/*
1863%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1864% %
1865% %
1866% %
1867% U n r e g i s t e r P S D I m a g e %
1868% %
1869% %
1870% %
1871%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1872%
1873% UnregisterPSDImage() removes format registrations made by the
1874% PSD module from the list of supported formats.
1875%
1876% The format of the UnregisterPSDImage method is:
1877%
1878% UnregisterPSDImage(void)
1879%
1880*/
1881ModuleExport void UnregisterPSDImage(void)
1882{
cristyb4233012010-02-28 20:09:14 +00001883 (void) UnregisterMagickInfo("PSB");
cristy3ed852e2009-09-05 21:47:34 +00001884 (void) UnregisterMagickInfo("PSD");
1885}
1886
1887/*
1888%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1889% %
1890% %
1891% %
1892% W r i t e P S D I m a g e %
1893% %
1894% %
1895% %
1896%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1897%
cristyb1459bc2010-03-23 21:41:43 +00001898% WritePSDImage() writes an image in the Adobe Photoshop encoded image format.
cristy3ed852e2009-09-05 21:47:34 +00001899%
1900% The format of the WritePSDImage method is:
1901%
dirk1ab941f2014-02-01 17:35:11 +00001902% MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
1903% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001904%
1905% A description of each parameter follows.
1906%
1907% o image_info: the image info.
1908%
1909% o image: The image.
1910%
cristy3a37efd2011-08-28 20:31:03 +00001911% o exception: return any errors or warnings in this structure.
1912%
cristy3ed852e2009-09-05 21:47:34 +00001913*/
1914
cristy50aea4a2010-03-09 17:37:44 +00001915static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image,
cristyf0460ee2010-03-06 02:55:11 +00001916 const size_t offset)
1917{
1918 if (psd_info->version == 1)
cristy56ed31c2010-03-22 00:46:21 +00001919 return(WriteBlobMSBShort(image,(unsigned short) offset));
1920 return(WriteBlobMSBLong(image,(unsigned short) offset));
cristyf0460ee2010-03-06 02:55:11 +00001921}
1922
cristy50aea4a2010-03-09 17:37:44 +00001923static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image,
cristyf0460ee2010-03-06 02:55:11 +00001924 const MagickSizeType size)
1925{
1926 if (psd_info->version == 1)
cristy56ed31c2010-03-22 00:46:21 +00001927 return(WriteBlobMSBLong(image,(unsigned int) size));
cristyf0460ee2010-03-06 02:55:11 +00001928 return(WriteBlobMSBLongLong(image,size));
1929}
1930
cristy4aff1572010-02-15 13:34:01 +00001931static size_t PSDPackbitsEncodeImage(Image *image,const size_t length,
cristy018f07f2011-09-04 21:15:19 +00001932 const unsigned char *pixels,unsigned char *compact_pixels,
1933 ExceptionInfo *exception)
cristy4aff1572010-02-15 13:34:01 +00001934{
1935 int
1936 count;
1937
cristybb503372010-05-27 20:51:26 +00001938 register ssize_t
cristy4aff1572010-02-15 13:34:01 +00001939 i,
1940 j;
1941
1942 register unsigned char
1943 *q;
1944
1945 unsigned char
1946 *packbits;
1947
1948 /*
1949 Compress pixels with Packbits encoding.
1950 */
1951 assert(image != (Image *) NULL);
1952 assert(image->signature == MagickSignature);
1953 if (image->debug != MagickFalse)
1954 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1955 assert(pixels != (unsigned char *) NULL);
1956 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1957 if (packbits == (unsigned char *) NULL)
1958 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1959 image->filename);
cristyb1459bc2010-03-23 21:41:43 +00001960 q=compact_pixels;
cristybb503372010-05-27 20:51:26 +00001961 for (i=(ssize_t) length; i != 0; )
cristy4aff1572010-02-15 13:34:01 +00001962 {
1963 switch (i)
1964 {
1965 case 1:
1966 {
1967 i--;
1968 *q++=(unsigned char) 0;
1969 *q++=(*pixels);
1970 break;
1971 }
1972 case 2:
1973 {
1974 i-=2;
1975 *q++=(unsigned char) 1;
1976 *q++=(*pixels);
1977 *q++=pixels[1];
1978 break;
1979 }
1980 case 3:
1981 {
1982 i-=3;
1983 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1984 {
1985 *q++=(unsigned char) ((256-3)+1);
1986 *q++=(*pixels);
1987 break;
1988 }
1989 *q++=(unsigned char) 2;
1990 *q++=(*pixels);
1991 *q++=pixels[1];
1992 *q++=pixels[2];
1993 break;
1994 }
1995 default:
1996 {
1997 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1998 {
1999 /*
2000 Packed run.
2001 */
2002 count=3;
cristybb503372010-05-27 20:51:26 +00002003 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
cristy4aff1572010-02-15 13:34:01 +00002004 {
2005 count++;
2006 if (count >= 127)
2007 break;
2008 }
2009 i-=count;
2010 *q++=(unsigned char) ((256-count)+1);
2011 *q++=(*pixels);
2012 pixels+=count;
2013 break;
2014 }
2015 /*
2016 Literal run.
2017 */
2018 count=0;
2019 while ((*(pixels+count) != *(pixels+count+1)) ||
2020 (*(pixels+count+1) != *(pixels+count+2)))
2021 {
2022 packbits[count+1]=pixels[count];
2023 count++;
cristybb503372010-05-27 20:51:26 +00002024 if (((ssize_t) count >= (i-3)) || (count >= 127))
cristy4aff1572010-02-15 13:34:01 +00002025 break;
2026 }
2027 i-=count;
2028 *packbits=(unsigned char) (count-1);
cristybb503372010-05-27 20:51:26 +00002029 for (j=0; j <= (ssize_t) count; j++)
cristy4aff1572010-02-15 13:34:01 +00002030 *q++=packbits[j];
2031 pixels+=count;
2032 break;
2033 }
2034 }
2035 }
2036 *q++=(unsigned char) 128; /* EOD marker */
2037 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
cristyb1459bc2010-03-23 21:41:43 +00002038 return((size_t) (q-compact_pixels));
cristy4aff1572010-02-15 13:34:01 +00002039}
2040
cristy875e28a2010-03-06 19:46:55 +00002041static void WritePackbitsLength(const PSDInfo *psd_info,
cristya20214e2010-08-28 16:52:13 +00002042 const ImageInfo *image_info,Image *image,Image *next_image,
cristy018f07f2011-09-04 21:15:19 +00002043 unsigned char *compact_pixels,const QuantumType quantum_type,
2044 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002045{
cristy3ed852e2009-09-05 21:47:34 +00002046 QuantumInfo
2047 *quantum_info;
2048
cristy4c08aed2011-07-01 19:47:50 +00002049 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00002050 *p;
2051
2052 size_t
cristy4aff1572010-02-15 13:34:01 +00002053 length,
cristy3ed852e2009-09-05 21:47:34 +00002054 packet_size;
2055
cristy75f85ae2010-09-25 03:01:06 +00002056 ssize_t
2057 y;
2058
cristya20214e2010-08-28 16:52:13 +00002059 unsigned char
2060 *pixels;
2061
2062 if (next_image->depth > 8)
2063 next_image->depth=16;
2064 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
cristyda16f162011-02-19 23:52:17 +00002065 (void) packet_size;
cristy4aff1572010-02-15 13:34:01 +00002066 quantum_info=AcquireQuantumInfo(image_info,image);
cristya20214e2010-08-28 16:52:13 +00002067 pixels=GetQuantumPixels(quantum_info);
2068 for (y=0; y < (ssize_t) next_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002069 {
cristyc82a27b2011-10-21 01:07:16 +00002070 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002071 if (p == (const Quantum *) NULL)
cristy4aff1572010-02-15 13:34:01 +00002072 break;
cristya20214e2010-08-28 16:52:13 +00002073 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
cristyc82a27b2011-10-21 01:07:16 +00002074 quantum_type,pixels,exception);
cristy018f07f2011-09-04 21:15:19 +00002075 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
2076 exception);
cristy875e28a2010-03-06 19:46:55 +00002077 (void) SetPSDOffset(psd_info,image,length);
cristy4aff1572010-02-15 13:34:01 +00002078 }
2079 quantum_info=DestroyQuantumInfo(quantum_info);
2080}
2081
cristy875e28a2010-03-06 19:46:55 +00002082static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info,
cristya20214e2010-08-28 16:52:13 +00002083 Image *image,Image *next_image,unsigned char *compact_pixels,
cristy018f07f2011-09-04 21:15:19 +00002084 const QuantumType quantum_type,const MagickBooleanType compression_flag,
2085 ExceptionInfo *exception)
cristy4aff1572010-02-15 13:34:01 +00002086{
2087 int
2088 y;
2089
cristy0910f242010-04-01 18:55:09 +00002090 MagickBooleanType
2091 monochrome;
2092
cristy4aff1572010-02-15 13:34:01 +00002093 QuantumInfo
2094 *quantum_info;
2095
cristy4c08aed2011-07-01 19:47:50 +00002096 register const Quantum
cristy4aff1572010-02-15 13:34:01 +00002097 *p;
2098
cristybb503372010-05-27 20:51:26 +00002099 register ssize_t
cristy0910f242010-04-01 18:55:09 +00002100 i;
2101
cristy4aff1572010-02-15 13:34:01 +00002102 size_t
2103 length,
2104 packet_size;
2105
cristya20214e2010-08-28 16:52:13 +00002106 unsigned char
2107 *pixels;
2108
cristy50aea4a2010-03-09 17:37:44 +00002109 (void) psd_info;
cristy4aff1572010-02-15 13:34:01 +00002110 if ((compression_flag != MagickFalse) &&
cristya20214e2010-08-28 16:52:13 +00002111 (next_image->compression != RLECompression))
cristy4aff1572010-02-15 13:34:01 +00002112 (void) WriteBlobMSBShort(image,0);
cristya20214e2010-08-28 16:52:13 +00002113 if (next_image->depth > 8)
2114 next_image->depth=16;
cristyc82a27b2011-10-21 01:07:16 +00002115 monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
2116 MagickTrue : MagickFalse;
cristya20214e2010-08-28 16:52:13 +00002117 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
cristyda16f162011-02-19 23:52:17 +00002118 (void) packet_size;
cristy4aff1572010-02-15 13:34:01 +00002119 quantum_info=AcquireQuantumInfo(image_info,image);
cristya20214e2010-08-28 16:52:13 +00002120 pixels=GetQuantumPixels(quantum_info);
2121 for (y=0; y < (ssize_t) next_image->rows; y++)
cristy4aff1572010-02-15 13:34:01 +00002122 {
cristyc82a27b2011-10-21 01:07:16 +00002123 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002124 if (p == (const Quantum *) NULL)
cristy4aff1572010-02-15 13:34:01 +00002125 break;
cristya20214e2010-08-28 16:52:13 +00002126 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
cristyc82a27b2011-10-21 01:07:16 +00002127 quantum_type,pixels,exception);
cristy0910f242010-04-01 18:55:09 +00002128 if (monochrome != MagickFalse)
cristybb503372010-05-27 20:51:26 +00002129 for (i=0; i < (ssize_t) length; i++)
cristy0910f242010-04-01 18:55:09 +00002130 pixels[i]=(~pixels[i]);
cristya20214e2010-08-28 16:52:13 +00002131 if (next_image->compression != RLECompression)
cristy4aff1572010-02-15 13:34:01 +00002132 (void) WriteBlob(image,length,pixels);
2133 else
2134 {
cristy018f07f2011-09-04 21:15:19 +00002135 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
2136 exception);
cristyb1459bc2010-03-23 21:41:43 +00002137 (void) WriteBlob(image,length,compact_pixels);
cristy4aff1572010-02-15 13:34:01 +00002138 }
cristy3ed852e2009-09-05 21:47:34 +00002139 }
2140 quantum_info=DestroyQuantumInfo(quantum_info);
2141}
2142
cristy875e28a2010-03-06 19:46:55 +00002143static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info,
cristya20214e2010-08-28 16:52:13 +00002144 const ImageInfo *image_info,Image *image,Image *next_image,
cristy018f07f2011-09-04 21:15:19 +00002145 const MagickBooleanType separate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002146{
2147 int
2148 i;
2149
2150 size_t
2151 channels,
2152 packet_size;
2153
2154 unsigned char
cristya20214e2010-08-28 16:52:13 +00002155 *compact_pixels;
cristy3ed852e2009-09-05 21:47:34 +00002156
2157 /*
cristy875e28a2010-03-06 19:46:55 +00002158 Write uncompressed pixels as separate planes.
cristy3ed852e2009-09-05 21:47:34 +00002159 */
2160 channels=1;
cristya20214e2010-08-28 16:52:13 +00002161 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
2162 compact_pixels=(unsigned char *) NULL;
2163 if (next_image->compression == RLECompression)
cristy4aff1572010-02-15 13:34:01 +00002164 {
cristya20214e2010-08-28 16:52:13 +00002165 compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels*
2166 next_image->columns,packet_size*sizeof(*compact_pixels));
2167 if (compact_pixels == (unsigned char *) NULL)
2168 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy4aff1572010-02-15 13:34:01 +00002169 }
cristy3ed852e2009-09-05 21:47:34 +00002170 i=0;
cristyc82a27b2011-10-21 01:07:16 +00002171 if (IsImageGray(next_image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002172 {
cristya20214e2010-08-28 16:52:13 +00002173 if (next_image->compression == RLECompression)
cristy4aff1572010-02-15 13:34:01 +00002174 {
2175 /*
2176 Packbits compression.
2177 */
2178 (void) WriteBlobMSBShort(image,1);
cristya20214e2010-08-28 16:52:13 +00002179 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002180 compact_pixels,GrayQuantum,exception);
cristy8a46d822012-08-28 23:32:39 +00002181 if (next_image->alpha_trait == BlendPixelTrait)
cristya20214e2010-08-28 16:52:13 +00002182 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002183 compact_pixels,AlphaQuantum,exception);
cristy4aff1572010-02-15 13:34:01 +00002184 }
cristya20214e2010-08-28 16:52:13 +00002185 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2186 GrayQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00002187 MagickFalse,exception);
cristy8a46d822012-08-28 23:32:39 +00002188 if (next_image->alpha_trait == BlendPixelTrait)
cristya20214e2010-08-28 16:52:13 +00002189 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2190 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00002191 MagickFalse,exception);
cristy6886a752010-04-23 18:23:20 +00002192 (void) SetImageProgress(image,SaveImagesTag,0,1);
cristy3ed852e2009-09-05 21:47:34 +00002193 }
cristy0910f242010-04-01 18:55:09 +00002194 else
cristya20214e2010-08-28 16:52:13 +00002195 if (next_image->storage_class == PseudoClass)
cristy0910f242010-04-01 18:55:09 +00002196 {
cristya20214e2010-08-28 16:52:13 +00002197 if (next_image->compression == RLECompression)
cristy0910f242010-04-01 18:55:09 +00002198 {
2199 /*
2200 Packbits compression.
2201 */
2202 (void) WriteBlobMSBShort(image,1);
cristya20214e2010-08-28 16:52:13 +00002203 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002204 compact_pixels,IndexQuantum,exception);
cristy8a46d822012-08-28 23:32:39 +00002205 if (next_image->alpha_trait == BlendPixelTrait)
cristya20214e2010-08-28 16:52:13 +00002206 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002207 compact_pixels,AlphaQuantum,exception);
cristy0910f242010-04-01 18:55:09 +00002208 }
cristya20214e2010-08-28 16:52:13 +00002209 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2210 IndexQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00002211 MagickFalse,exception);
cristy8a46d822012-08-28 23:32:39 +00002212 if (next_image->alpha_trait == BlendPixelTrait)
cristya20214e2010-08-28 16:52:13 +00002213 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2214 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00002215 MagickFalse,exception);
cristy0910f242010-04-01 18:55:09 +00002216 (void) SetImageProgress(image,SaveImagesTag,0,1);
2217 }
2218 else
2219 {
cristya20214e2010-08-28 16:52:13 +00002220 if (next_image->colorspace == CMYKColorspace)
cristyc82a27b2011-10-21 01:07:16 +00002221 (void) NegateImage(next_image,MagickFalse,exception);
cristya20214e2010-08-28 16:52:13 +00002222 if (next_image->compression == RLECompression)
cristy0910f242010-04-01 18:55:09 +00002223 {
2224 /*
2225 Packbits compression.
2226 */
2227 (void) WriteBlobMSBShort(image,1);
cristya20214e2010-08-28 16:52:13 +00002228 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002229 compact_pixels,RedQuantum,exception);
cristya20214e2010-08-28 16:52:13 +00002230 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002231 compact_pixels,GreenQuantum,exception);
cristya20214e2010-08-28 16:52:13 +00002232 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002233 compact_pixels,BlueQuantum,exception);
cristya20214e2010-08-28 16:52:13 +00002234 if (next_image->colorspace == CMYKColorspace)
2235 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002236 compact_pixels,BlackQuantum,exception);
cristy8a46d822012-08-28 23:32:39 +00002237 if (next_image->alpha_trait == BlendPixelTrait)
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 }
2241 (void) SetImageProgress(image,SaveImagesTag,0,6);
cristya20214e2010-08-28 16:52:13 +00002242 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2243 RedQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00002244 MagickFalse,exception);
cristy860cc732010-05-19 16:38:37 +00002245 (void) SetImageProgress(image,SaveImagesTag,1,6);
cristya20214e2010-08-28 16:52:13 +00002246 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2247 GreenQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00002248 MagickFalse,exception);
cristy860cc732010-05-19 16:38:37 +00002249 (void) SetImageProgress(image,SaveImagesTag,2,6);
cristya20214e2010-08-28 16:52:13 +00002250 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2251 BlueQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00002252 MagickFalse,exception);
cristy860cc732010-05-19 16:38:37 +00002253 (void) SetImageProgress(image,SaveImagesTag,3,6);
cristya20214e2010-08-28 16:52:13 +00002254 if (next_image->colorspace == CMYKColorspace)
2255 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2256 BlackQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00002257 MagickFalse,exception);
cristy860cc732010-05-19 16:38:37 +00002258 (void) SetImageProgress(image,SaveImagesTag,4,6);
cristy8a46d822012-08-28 23:32:39 +00002259 if (next_image->alpha_trait == BlendPixelTrait)
cristya20214e2010-08-28 16:52:13 +00002260 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2261 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00002262 MagickFalse,exception);
cristy0910f242010-04-01 18:55:09 +00002263 (void) SetImageProgress(image,SaveImagesTag,5,6);
cristya20214e2010-08-28 16:52:13 +00002264 if (next_image->colorspace == CMYKColorspace)
cristyc82a27b2011-10-21 01:07:16 +00002265 (void) NegateImage(next_image,MagickFalse,exception);
cristy0910f242010-04-01 18:55:09 +00002266 }
cristya20214e2010-08-28 16:52:13 +00002267 if (next_image->compression == RLECompression)
2268 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
cristy3ed852e2009-09-05 21:47:34 +00002269 return(MagickTrue);
2270}
2271
cristy3ed852e2009-09-05 21:47:34 +00002272static void WritePascalString(Image* inImage,const char *inString,int inPad)
2273{
2274 size_t
cristya20214e2010-08-28 16:52:13 +00002275 length;
cristy3ed852e2009-09-05 21:47:34 +00002276
cristya20214e2010-08-28 16:52:13 +00002277 register ssize_t
2278 i;
cristy3ed852e2009-09-05 21:47:34 +00002279
cristya20214e2010-08-28 16:52:13 +00002280 /*
2281 Max length is 255.
2282 */
2283 length=(strlen(inString) > 255UL ) ? 255UL : strlen(inString);
2284 if (length == 0)
2285 (void) WriteBlobByte(inImage,0);
cristy3ed852e2009-09-05 21:47:34 +00002286 else
cristya20214e2010-08-28 16:52:13 +00002287 {
2288 (void) WriteBlobByte(inImage,(unsigned char) length);
2289 (void) WriteBlob(inImage, length, (const unsigned char *) inString);
2290 }
2291 length++;
2292 if ((length % inPad) == 0)
cristy3ed852e2009-09-05 21:47:34 +00002293 return;
cristya20214e2010-08-28 16:52:13 +00002294 for (i=0; i < (ssize_t) (inPad-(length % inPad)); i++)
cristy3ed852e2009-09-05 21:47:34 +00002295 (void) WriteBlobByte(inImage,0);
2296}
2297
2298static void WriteResolutionResourceBlock(Image *image)
2299{
cristy56ed31c2010-03-22 00:46:21 +00002300 double
2301 x_resolution,
2302 y_resolution;
cristy3ed852e2009-09-05 21:47:34 +00002303
2304 unsigned short
2305 units;
2306
cristy2a11bef2011-10-28 18:33:11 +00002307 x_resolution=65536.0*image->resolution.x+0.5;
2308 y_resolution=65536.0*image->resolution.y+0.5;
cristy3ed852e2009-09-05 21:47:34 +00002309 units=1;
2310 if (image->units == PixelsPerCentimeterResolution)
2311 {
cristy2a11bef2011-10-28 18:33:11 +00002312 x_resolution=2.54*65536.0*image->resolution.x*0.5;
2313 y_resolution=2.54*65536.0*image->resolution.y+0.5;
cristy3ed852e2009-09-05 21:47:34 +00002314 units=2;
2315 }
2316 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2317 (void) WriteBlobMSBShort(image,0x03ED);
2318 (void) WriteBlobMSBShort(image,0);
2319 (void) WriteBlobMSBLong(image,16); /* resource size */
cristy56ed31c2010-03-22 00:46:21 +00002320 (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002321 (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */
2322 (void) WriteBlobMSBShort(image,units); /* width unit */
cristy56ed31c2010-03-22 00:46:21 +00002323 (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5));
cristy3ed852e2009-09-05 21:47:34 +00002324 (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */
2325 (void) WriteBlobMSBShort(image,units); /* height unit */
2326}
2327
cristyd4d3f742010-04-25 20:36:50 +00002328static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile)
2329{
cristy0b796e62010-04-29 00:38:45 +00002330 register const unsigned char
cristyd4d3f742010-04-25 20:36:50 +00002331 *p;
2332
2333 size_t
2334 length;
2335
2336 unsigned char
2337 *datum;
2338
cristy6befb0f2010-05-31 14:33:15 +00002339 unsigned int
cristyd4d3f742010-04-25 20:36:50 +00002340 count,
cristy6befb0f2010-05-31 14:33:15 +00002341 long_sans;
cristyd4d3f742010-04-25 20:36:50 +00002342
2343 unsigned short
2344 id,
2345 short_sans;
2346
2347 length=GetStringInfoLength(bim_profile);
2348 if (length < 16)
cristy0b796e62010-04-29 00:38:45 +00002349 return;
cristyd4d3f742010-04-25 20:36:50 +00002350 datum=GetStringInfoDatum(bim_profile);
2351 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2352 {
cristy4176bb22010-05-01 16:29:09 +00002353 register unsigned char
2354 *q;
2355
2356 q=(unsigned char *) p;
cristyd4d3f742010-04-25 20:36:50 +00002357 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2358 break;
cristy6befb0f2010-05-31 14:33:15 +00002359 p=PushLongPixel(MSBEndian,p,&long_sans);
cristyd4d3f742010-04-25 20:36:50 +00002360 p=PushShortPixel(MSBEndian,p,&id);
2361 p=PushShortPixel(MSBEndian,p,&short_sans);
2362 p=PushLongPixel(MSBEndian,p,&count);
2363 if (id == 0x0000040f)
2364 {
cristy4176bb22010-05-01 16:29:09 +00002365 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2366 (PSDQuantum(count)+12)-(q-datum));
2367 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
cristyd4d3f742010-04-25 20:36:50 +00002368 break;
2369 }
2370 p+=count;
2371 if ((count & 0x01) != 0)
2372 p++;
2373 }
2374}
2375
cristyf11065e2010-05-14 13:26:59 +00002376static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile)
2377{
2378 register const unsigned char
2379 *p;
2380
2381 size_t
2382 length;
2383
2384 unsigned char
2385 *datum;
2386
cristy6befb0f2010-05-31 14:33:15 +00002387 unsigned int
cristyf11065e2010-05-14 13:26:59 +00002388 count,
cristy6befb0f2010-05-31 14:33:15 +00002389 long_sans;
cristyf11065e2010-05-14 13:26:59 +00002390
2391 unsigned short
2392 id,
2393 short_sans;
2394
2395 length=GetStringInfoLength(bim_profile);
2396 if (length < 16)
2397 return;
2398 datum=GetStringInfoDatum(bim_profile);
2399 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2400 {
2401 register unsigned char
2402 *q;
2403
2404 q=(unsigned char *) p;
2405 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2406 break;
cristy6befb0f2010-05-31 14:33:15 +00002407 p=PushLongPixel(MSBEndian,p,&long_sans);
cristyf11065e2010-05-14 13:26:59 +00002408 p=PushShortPixel(MSBEndian,p,&id);
2409 p=PushShortPixel(MSBEndian,p,&short_sans);
2410 p=PushLongPixel(MSBEndian,p,&count);
cristy94b11832011-09-08 19:46:03 +00002411 if ((id == 0x000003ed) && (PSDQuantum(count) < (ssize_t) (length-12)))
cristyf11065e2010-05-14 13:26:59 +00002412 {
2413 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2414 (PSDQuantum(count)+12)-(q-datum));
2415 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2416 break;
2417 }
2418 p+=count;
2419 if ((count & 0x01) != 0)
2420 p++;
2421 }
2422}
2423
cristy3a37efd2011-08-28 20:31:03 +00002424static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
2425 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002426{
2427 const char
cristya20214e2010-08-28 16:52:13 +00002428 *property;
cristy3ed852e2009-09-05 21:47:34 +00002429
2430 const StringInfo
cristy749d2152010-04-04 23:47:33 +00002431 *icc_profile;
cristy3ed852e2009-09-05 21:47:34 +00002432
cristya20214e2010-08-28 16:52:13 +00002433 Image
2434 *base_image,
2435 *next_image;
2436
cristy3ed852e2009-09-05 21:47:34 +00002437 MagickBooleanType
cristy3ed852e2009-09-05 21:47:34 +00002438 status;
2439
cristy875e28a2010-03-06 19:46:55 +00002440 PSDInfo
2441 psd_info;
2442
cristybb503372010-05-27 20:51:26 +00002443 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002444 i;
2445
2446 size_t
cristya20214e2010-08-28 16:52:13 +00002447 channel_size,
2448 channelLength,
2449 layer_count,
2450 layer_info_size,
cristy749d2152010-04-04 23:47:33 +00002451 length,
cristy3ed852e2009-09-05 21:47:34 +00002452 num_channels,
cristya20214e2010-08-28 16:52:13 +00002453 packet_size,
2454 rounded_layer_info_size;
cristy3ed852e2009-09-05 21:47:34 +00002455
cristy0b796e62010-04-29 00:38:45 +00002456 StringInfo
2457 *bim_profile;
2458
cristy3ed852e2009-09-05 21:47:34 +00002459 /*
cristy56ed31c2010-03-22 00:46:21 +00002460 Open image file.
cristy3ed852e2009-09-05 21:47:34 +00002461 */
2462 assert(image_info != (const ImageInfo *) NULL);
2463 assert(image_info->signature == MagickSignature);
2464 assert(image != (Image *) NULL);
2465 assert(image->signature == MagickSignature);
2466 if (image->debug != MagickFalse)
2467 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00002468 assert(exception != (ExceptionInfo *) NULL);
2469 assert(exception->signature == MagickSignature);
2470 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00002471 if (status == MagickFalse)
2472 return(status);
2473 packet_size=(size_t) (image->depth > 8 ? 6 : 3);
cristy8a46d822012-08-28 23:32:39 +00002474 if (image->alpha_trait == BlendPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00002475 packet_size+=image->depth > 8 ? 2 : 1;
cristy875e28a2010-03-06 19:46:55 +00002476 psd_info.version=1;
2477 if ((LocaleCompare(image_info->magick,"PSB") == 0) ||
2478 (image->columns > 30000) || (image->rows > 30000))
2479 psd_info.version=2;
cristy50aea4a2010-03-09 17:37:44 +00002480 (void) WriteBlob(image,4,(const unsigned char *) "8BPS");
cristy875e28a2010-03-06 19:46:55 +00002481 (void) WriteBlobMSBShort(image,psd_info.version); /* version */
2482 for (i=1; i <= 6; i++)
2483 (void) WriteBlobByte(image, 0); /* 6 bytes of reserved */
cristy3a37efd2011-08-28 20:31:03 +00002484 if (IsImageGray(image,exception) != MagickFalse)
cristy8a46d822012-08-28 23:32:39 +00002485 num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
cristy3ed852e2009-09-05 21:47:34 +00002486 else
cristy0910f242010-04-01 18:55:09 +00002487 if (image->storage_class == PseudoClass)
cristy8a46d822012-08-28 23:32:39 +00002488 num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
cristy0910f242010-04-01 18:55:09 +00002489 else
2490 {
2491 if (image->colorspace != CMYKColorspace)
cristy8a46d822012-08-28 23:32:39 +00002492 num_channels=(image->alpha_trait == BlendPixelTrait ? 4UL : 3UL);
cristy0910f242010-04-01 18:55:09 +00002493 else
cristy8a46d822012-08-28 23:32:39 +00002494 num_channels=(image->alpha_trait == BlendPixelTrait ? 5UL : 4UL);
cristy0910f242010-04-01 18:55:09 +00002495 }
cristy3ed852e2009-09-05 21:47:34 +00002496 (void) WriteBlobMSBShort(image,(unsigned short) num_channels);
cristy56ed31c2010-03-22 00:46:21 +00002497 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
2498 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
cristy3a37efd2011-08-28 20:31:03 +00002499 if (IsImageGray(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002500 {
cristy2045da32010-04-16 00:59:35 +00002501 MagickBooleanType
2502 monochrome;
2503
cristy0910f242010-04-01 18:55:09 +00002504 /*
2505 Write depth & mode.
2506 */
cristy3a37efd2011-08-28 20:31:03 +00002507 monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
2508 MagickTrue : MagickFalse;
cristy284c7d82010-04-24 00:19:14 +00002509 (void) WriteBlobMSBShort(image,(unsigned short)
2510 (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8));
cristy2b9582a2011-07-04 17:38:56 +00002511 (void) WriteBlobMSBShort(image,(unsigned short)
2512 (monochrome != MagickFalse ? BitmapMode : GrayscaleMode));
cristy3ed852e2009-09-05 21:47:34 +00002513 }
2514 else
2515 {
cristya20214e2010-08-28 16:52:13 +00002516 (void) WriteBlobMSBShort(image,(unsigned short) (image->storage_class ==
2517 PseudoClass ? 8 : image->depth > 8 ? 16 : 8));
cristy48845392010-06-02 01:19:17 +00002518 if (((image_info->colorspace != UndefinedColorspace) ||
cristy0910f242010-04-01 18:55:09 +00002519 (image->colorspace != CMYKColorspace)) &&
cristy48845392010-06-02 01:19:17 +00002520 (image_info->colorspace != CMYKColorspace))
cristy0910f242010-04-01 18:55:09 +00002521 {
cristyaf8d3912014-02-21 14:50:33 +00002522 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy0910f242010-04-01 18:55:09 +00002523 (void) WriteBlobMSBShort(image,(unsigned short)
cristy2045da32010-04-16 00:59:35 +00002524 (image->storage_class == PseudoClass ? IndexedMode : RGBMode));
cristy0910f242010-04-01 18:55:09 +00002525 }
2526 else
2527 {
cristy48845392010-06-02 01:19:17 +00002528 if (image->colorspace != CMYKColorspace)
cristye941a752011-10-15 01:52:48 +00002529 (void) TransformImageColorspace(image,CMYKColorspace,exception);
cristy2045da32010-04-16 00:59:35 +00002530 (void) WriteBlobMSBShort(image,CMYKMode);
cristy0910f242010-04-01 18:55:09 +00002531 }
cristy3ed852e2009-09-05 21:47:34 +00002532 }
cristy3a37efd2011-08-28 20:31:03 +00002533 if ((IsImageGray(image,exception) != MagickFalse) ||
cristyca9ddb02010-04-16 01:01:18 +00002534 (image->storage_class == DirectClass) || (image->colors > 256))
cristy3ed852e2009-09-05 21:47:34 +00002535 (void) WriteBlobMSBLong(image,0);
2536 else
2537 {
2538 /*
2539 Write PSD raster colormap.
2540 */
2541 (void) WriteBlobMSBLong(image,768);
cristybb503372010-05-27 20:51:26 +00002542 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002543 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red));
2544 for ( ; i < 256; i++)
2545 (void) WriteBlobByte(image,0);
cristybb503372010-05-27 20:51:26 +00002546 for (i=0; i < (ssize_t) image->colors; i++)
cristya20214e2010-08-28 16:52:13 +00002547 (void) WriteBlobByte(image,ScaleQuantumToChar(
2548 image->colormap[i].green));
cristy3ed852e2009-09-05 21:47:34 +00002549 for ( ; i < 256; i++)
2550 (void) WriteBlobByte(image,0);
cristybb503372010-05-27 20:51:26 +00002551 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002552 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue));
2553 for ( ; i < 256; i++)
2554 (void) WriteBlobByte(image,0);
2555 }
2556 /*
2557 Image resource block.
2558 */
cristy749d2152010-04-04 23:47:33 +00002559 length=28; /* 0x03EB */
cristy0b796e62010-04-29 00:38:45 +00002560 bim_profile=(StringInfo *) GetImageProfile(image,"8bim");
cristy749d2152010-04-04 23:47:33 +00002561 icc_profile=GetImageProfile(image,"icc");
cristyd4d3f742010-04-25 20:36:50 +00002562 if (bim_profile != (StringInfo *) NULL)
2563 {
cristy0b796e62010-04-29 00:38:45 +00002564 bim_profile=CloneStringInfo(bim_profile);
cristyd4d3f742010-04-25 20:36:50 +00002565 if (icc_profile != (StringInfo *) NULL)
2566 RemoveICCProfileFromResourceBlock(bim_profile);
cristyf11065e2010-05-14 13:26:59 +00002567 RemoveResolutionFromResourceBlock(bim_profile);
cristyd4d3f742010-04-25 20:36:50 +00002568 length+=PSDQuantum(GetStringInfoLength(bim_profile));
2569 }
cristy0b796e62010-04-29 00:38:45 +00002570 if (icc_profile != (const StringInfo *) NULL)
cristy749d2152010-04-04 23:47:33 +00002571 length+=PSDQuantum(GetStringInfoLength(icc_profile))+12;
cristy284c7d82010-04-24 00:19:14 +00002572 (void) WriteBlobMSBLong(image,(unsigned int) length);
cristyf11065e2010-05-14 13:26:59 +00002573 WriteResolutionResourceBlock(image);
cristy4176bb22010-05-01 16:29:09 +00002574 if (bim_profile != (StringInfo *) NULL)
2575 {
2576 (void) WriteBlob(image,GetStringInfoLength(bim_profile),
2577 GetStringInfoDatum(bim_profile));
2578 bim_profile=DestroyStringInfo(bim_profile);
2579 }
cristy749d2152010-04-04 23:47:33 +00002580 if (icc_profile != (StringInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002581 {
cristy749d2152010-04-04 23:47:33 +00002582 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
cristy4176bb22010-05-01 16:29:09 +00002583 (void) WriteBlobMSBShort(image,0x0000040F);
cristy749d2152010-04-04 23:47:33 +00002584 (void) WriteBlobMSBShort(image,0);
cristy284c7d82010-04-24 00:19:14 +00002585 (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength(
2586 icc_profile));
cristy749d2152010-04-04 23:47:33 +00002587 (void) WriteBlob(image,GetStringInfoLength(icc_profile),
2588 GetStringInfoDatum(icc_profile));
cristye195f262010-04-16 18:12:35 +00002589 if ((MagickOffsetType) GetStringInfoLength(icc_profile) !=
cristy2045da32010-04-16 00:59:35 +00002590 PSDQuantum(GetStringInfoLength(icc_profile)))
cristy749d2152010-04-04 23:47:33 +00002591 (void) WriteBlobByte(image,0);
cristye6365592010-04-02 17:31:23 +00002592 }
cristy48845392010-06-02 01:19:17 +00002593 layer_count=0;
2594 layer_info_size=2;
cristy2837bcc2010-08-07 23:57:39 +00002595 base_image=GetNextImageInList(image);
cristy8a46d822012-08-28 23:32:39 +00002596 if ((image->alpha_trait == BlendPixelTrait) && (base_image == (Image *) NULL))
cristy2837bcc2010-08-07 23:57:39 +00002597 base_image=image;
cristya20214e2010-08-28 16:52:13 +00002598 next_image=base_image;
2599 while ( next_image != NULL )
2600 {
2601 packet_size=next_image->depth > 8 ? 2UL : 1UL;
cristy3a37efd2011-08-28 20:31:03 +00002602 if (IsImageGray(next_image,exception) != MagickFalse)
cristy8a46d822012-08-28 23:32:39 +00002603 num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
cristy3ed852e2009-09-05 21:47:34 +00002604 else
cristya20214e2010-08-28 16:52:13 +00002605 if (next_image->storage_class == PseudoClass)
cristy8a46d822012-08-28 23:32:39 +00002606 num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
cristy2045da32010-04-16 00:59:35 +00002607 else
cristya20214e2010-08-28 16:52:13 +00002608 if (next_image->colorspace != CMYKColorspace)
cristy8a46d822012-08-28 23:32:39 +00002609 num_channels=next_image->alpha_trait == BlendPixelTrait ? 4UL : 3UL;
cristy2045da32010-04-16 00:59:35 +00002610 else
cristy8a46d822012-08-28 23:32:39 +00002611 num_channels=next_image->alpha_trait == BlendPixelTrait ? 5UL : 4UL;
cristya20214e2010-08-28 16:52:13 +00002612 channelLength=(size_t) (next_image->columns*next_image->rows*packet_size+2);
cristy48845392010-06-02 01:19:17 +00002613 layer_info_size+=(size_t) (4*4+2+num_channels*6+(psd_info.version == 1 ? 8 :
2614 16)+4*1+4+num_channels*channelLength);
cristyd15e6592011-10-15 00:13:06 +00002615 property=(const char *) GetImageProperty(next_image,"label",exception);
cristya20214e2010-08-28 16:52:13 +00002616 if (property == (const char *) NULL)
2617 layer_info_size+=16;
cristyde9b8f52010-03-12 01:17:00 +00002618 else
2619 {
cristya20214e2010-08-28 16:52:13 +00002620 size_t
2621 length;
2622
2623 length=strlen(property);
2624 layer_info_size+=8+length+(4-(length % 4));
cristyde9b8f52010-03-12 01:17:00 +00002625 }
cristy4aff1572010-02-15 13:34:01 +00002626 layer_count++;
cristya20214e2010-08-28 16:52:13 +00002627 next_image=GetNextImageInList(next_image);
cristy3ed852e2009-09-05 21:47:34 +00002628 }
cristy144f1b62010-05-18 00:52:09 +00002629 if (layer_count == 0)
cristy875e28a2010-03-06 19:46:55 +00002630 (void) SetPSDSize(&psd_info,image,0);
cristy3ed852e2009-09-05 21:47:34 +00002631 else
cristya20214e2010-08-28 16:52:13 +00002632 {
cristy8e9bd3e2010-08-29 00:03:56 +00002633 CompressionType
2634 compression;
2635
2636 (void) SetPSDSize(&psd_info,image,layer_info_size+
2637 (psd_info.version == 1 ? 8 : 16));
2638 if ((layer_info_size/2) != ((layer_info_size+1)/2))
2639 rounded_layer_info_size=layer_info_size+1;
cristya20214e2010-08-28 16:52:13 +00002640 else
cristy8e9bd3e2010-08-29 00:03:56 +00002641 rounded_layer_info_size=layer_info_size;
2642 (void) SetPSDSize(&psd_info,image,rounded_layer_info_size);
cristyf2a82ee2014-05-26 17:49:54 +00002643 if (base_image->alpha_trait == BlendPixelTrait)
dirk8fb7ed52014-02-11 12:07:01 +00002644 (void) WriteBlobMSBShort(image,-(unsigned short) layer_count);
2645 else
2646 (void) WriteBlobMSBShort(image,(unsigned short) layer_count);
cristy8e9bd3e2010-08-29 00:03:56 +00002647 layer_count=1;
2648 compression=base_image->compression;
cristyf2a82ee2014-05-26 17:49:54 +00002649 for (next_image=base_image; next_image != NULL; )
cristy8e9bd3e2010-08-29 00:03:56 +00002650 {
2651 next_image->compression=NoCompression;
cristya4e5f472011-11-09 00:42:46 +00002652 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y);
2653 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x);
cristy0e274112014-03-25 14:09:19 +00002654 (void) WriteBlobMSBLong(image,(unsigned int) (next_image->page.y+
2655 next_image->rows));
2656 (void) WriteBlobMSBLong(image,(unsigned int) (next_image->page.x+
2657 next_image->columns));
cristy8e9bd3e2010-08-29 00:03:56 +00002658 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2659 channel_size=(unsigned int) ((packet_size*next_image->rows*
2660 next_image->columns)+2);
cristy3a37efd2011-08-28 20:31:03 +00002661 if ((IsImageGray(next_image,exception) != MagickFalse) ||
cristy8e9bd3e2010-08-29 00:03:56 +00002662 (next_image->storage_class == PseudoClass))
2663 {
2664 (void) WriteBlobMSBShort(image,(unsigned short)
cristy8a46d822012-08-28 23:32:39 +00002665 (next_image->alpha_trait == BlendPixelTrait ? 2 : 1));
cristy8e9bd3e2010-08-29 00:03:56 +00002666 (void) WriteBlobMSBShort(image,0);
2667 (void) SetPSDSize(&psd_info,image,channel_size);
cristy8a46d822012-08-28 23:32:39 +00002668 if (next_image->alpha_trait == BlendPixelTrait)
cristy8e9bd3e2010-08-29 00:03:56 +00002669 {
2670 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2671 (void) SetPSDSize(&psd_info,image,channel_size);
2672 }
2673 }
2674 else
2675 if (next_image->colorspace != CMYKColorspace)
2676 {
2677 (void) WriteBlobMSBShort(image,(unsigned short)
cristy8a46d822012-08-28 23:32:39 +00002678 (next_image->alpha_trait == BlendPixelTrait ? 4 : 3));
cristy8e9bd3e2010-08-29 00:03:56 +00002679 (void) WriteBlobMSBShort(image,0);
2680 (void) SetPSDSize(&psd_info,image,channel_size);
2681 (void) WriteBlobMSBShort(image,1);
2682 (void) SetPSDSize(&psd_info,image,channel_size);
2683 (void) WriteBlobMSBShort(image,2);
2684 (void) SetPSDSize(&psd_info,image,channel_size);
cristy8a46d822012-08-28 23:32:39 +00002685 if (next_image->alpha_trait == BlendPixelTrait)
cristy8e9bd3e2010-08-29 00:03:56 +00002686 {
2687 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2688 (void) SetPSDSize(&psd_info,image,channel_size);
2689 }
2690 }
2691 else
2692 {
2693 (void) WriteBlobMSBShort(image,(unsigned short)
cristy8a46d822012-08-28 23:32:39 +00002694 (next_image->alpha_trait ? 5 : 4));
cristy8e9bd3e2010-08-29 00:03:56 +00002695 (void) WriteBlobMSBShort(image,0);
2696 (void) SetPSDSize(&psd_info,image,channel_size);
2697 (void) WriteBlobMSBShort(image,1);
2698 (void) SetPSDSize(&psd_info,image,channel_size);
2699 (void) WriteBlobMSBShort(image,2);
2700 (void) SetPSDSize(&psd_info,image,channel_size);
2701 (void) WriteBlobMSBShort(image,3);
2702 (void) SetPSDSize(&psd_info,image,channel_size);
cristy8a46d822012-08-28 23:32:39 +00002703 if (next_image->alpha_trait)
cristy8e9bd3e2010-08-29 00:03:56 +00002704 {
2705 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2706 (void) SetPSDSize(&psd_info,image,channel_size);
2707 }
2708 }
2709 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2710 (void) WriteBlob(image,4,(const unsigned char *)
2711 CompositeOperatorToPSDBlendMode(next_image->compose));
2712 (void) WriteBlobByte(image,255); /* layer opacity */
2713 (void) WriteBlobByte(image,0);
dirk48febd62014-04-08 04:26:07 +00002714 (void) WriteBlobByte(image,next_image->compose==NoCompositeOp ?
2715 1 << 0x02 : 1); /* layer properties - visible, etc. */
cristy8e9bd3e2010-08-29 00:03:56 +00002716 (void) WriteBlobByte(image,0);
cristyd15e6592011-10-15 00:13:06 +00002717 property=(const char *) GetImageProperty(next_image,"label",exception);
cristy8e9bd3e2010-08-29 00:03:56 +00002718 if (property == (const char *) NULL)
2719 {
dirk93b02b72013-11-16 16:03:36 +00002720 char
cristy7a1ce132013-11-14 12:38:14 +00002721 layer_name[MaxTextExtent];
2722
cristy8e9bd3e2010-08-29 00:03:56 +00002723 (void) WriteBlobMSBLong(image,16);
2724 (void) WriteBlobMSBLong(image,0);
2725 (void) WriteBlobMSBLong(image,0);
cristyaacbfdb2014-03-04 12:39:39 +00002726 (void) FormatLocaleString(layer_name,MaxTextExtent,"L%03ld",(long)
cristy7a1ce132013-11-14 12:38:14 +00002727 layer_count++);
2728 WritePascalString(image,layer_name,4);
cristy8e9bd3e2010-08-29 00:03:56 +00002729 }
2730 else
2731 {
2732 size_t
2733 length;
cristy3ed852e2009-09-05 21:47:34 +00002734
cristy8e9bd3e2010-08-29 00:03:56 +00002735 length=strlen(property);
2736 (void) WriteBlobMSBLong(image,(unsigned int) (length+(4-
2737 (length % 4))+8));
2738 (void) WriteBlobMSBLong(image,0);
2739 (void) WriteBlobMSBLong(image,0);
2740 WritePascalString(image,property,4);
2741 }
2742 next_image=GetNextImageInList(next_image);
2743 }
2744 /*
2745 Now the image data!
2746 */
2747 next_image=base_image;
2748 while (next_image != NULL)
2749 {
2750 status=WriteImageChannels(&psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002751 MagickTrue,exception);
cristy8e9bd3e2010-08-29 00:03:56 +00002752 next_image=GetNextImageInList(next_image);
2753 }
2754 (void) WriteBlobMSBLong(image,0); /* user mask data */
2755 base_image->compression=compression;
cristy144f1b62010-05-18 00:52:09 +00002756 }
cristy144f1b62010-05-18 00:52:09 +00002757 /*
2758 Write composite image.
2759 */
cristy018f07f2011-09-04 21:15:19 +00002760 status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse,
2761 exception);
cristy3ed852e2009-09-05 21:47:34 +00002762 (void) CloseBlob(image);
2763 return(status);
2764}