blob: 4e8040bea8a7382f0dc6b653b15a6cec057331ed [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 %
16% John Cristy %
17% Leonard Rosenthol %
18% July 1992 %
19% %
20% %
cristy1454be72011-12-19 01:52:48 +000021% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000022% dedicated to making software imaging solutions freely available. %
23% %
24% You may not use this file except in compliance with the License. You may %
25% obtain a copy of the License at %
26% %
27% http://www.imagemagick.org/script/license.php %
28% %
29% Unless required by applicable law or agreed to in writing, software %
30% distributed under the License is distributed on an "AS IS" BASIS, %
31% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32% See the License for the specific language governing permissions and %
33% limitations under the License. %
34% %
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
44#include "MagickCore/artifact.h"
45#include "MagickCore/attribute.h"
46#include "MagickCore/blob.h"
47#include "MagickCore/blob-private.h"
48#include "MagickCore/cache.h"
49#include "MagickCore/colormap.h"
50#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/constitute.h"
53#include "MagickCore/enhance.h"
54#include "MagickCore/exception.h"
55#include "MagickCore/exception-private.h"
56#include "MagickCore/image.h"
57#include "MagickCore/image-private.h"
58#include "MagickCore/list.h"
59#include "MagickCore/log.h"
60#include "MagickCore/magick.h"
61#include "MagickCore/memory_.h"
62#include "MagickCore/module.h"
63#include "MagickCore/monitor-private.h"
64#include "MagickCore/pixel.h"
65#include "MagickCore/pixel-accessor.h"
66#include "MagickCore/profile.h"
67#include "MagickCore/property.h"
68#include "MagickCore/quantum-private.h"
69#include "MagickCore/static.h"
70#include "MagickCore/string_.h"
cristy3ed852e2009-09-05 21:47:34 +000071
72/*
cristy2d3d87f2010-03-01 00:23:08 +000073 Define declaractions.
74*/
cristyaca3ec52010-03-02 14:55:01 +000075#define MaxPSDChannels 56
cristybb503372010-05-27 20:51:26 +000076#define PSDQuantum(x) (((ssize_t) (x)+1) & -2)
cristy2d3d87f2010-03-01 00:23:08 +000077
78/*
79 Enumerated declaractions.
80*/
81typedef enum
82{
83 BitmapMode = 0,
84 GrayscaleMode = 1,
85 IndexedMode = 2,
86 RGBMode = 3,
87 CMYKMode = 4,
88 MultichannelMode = 7,
89 DuotoneMode = 8,
90 LabMode = 9
91} PSDImageType;
92
93/*
94 Typedef declaractions.
95*/
96typedef struct _ChannelInfo
97{
98 short int
99 type;
100
cristybb503372010-05-27 20:51:26 +0000101 size_t
cristy2d3d87f2010-03-01 00:23:08 +0000102 size;
103} ChannelInfo;
104
105typedef struct _LayerInfo
106{
107 RectangleInfo
108 page,
109 mask;
110
111 unsigned short
112 channels;
113
114 ChannelInfo
115 channel_info[MaxPSDChannels];
116
117 char
118 blendkey[4];
119
120 Quantum
121 opacity;
122
123 unsigned char
124 clipping,
125 visible,
126 flags;
127
cristybb503372010-05-27 20:51:26 +0000128 size_t
cristy2d3d87f2010-03-01 00:23:08 +0000129 offset_x,
130 offset_y;
131
132 unsigned char
133 name[256];
134
135 Image
136 *image;
137} LayerInfo;
138
139typedef struct _PSDInfo
140{
141 char
142 signature[4];
143
144 unsigned short
145 channels,
cristy374b8ad2012-02-01 20:55:21 +0000146 color_channels,
cristy2d3d87f2010-03-01 00:23:08 +0000147 version;
148
149 unsigned char
150 reserved[6];
151
cristybb503372010-05-27 20:51:26 +0000152 size_t
cristy2d3d87f2010-03-01 00:23:08 +0000153 rows,
154 columns;
155
156 unsigned short
157 depth,
158 mode;
159} PSDInfo;
160
161/*
cristy3ed852e2009-09-05 21:47:34 +0000162 Forward declarations.
163*/
164static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +0000165 WritePSDImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000166
167/*
168%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169% %
170% %
171% %
cristy3ed852e2009-09-05 21:47:34 +0000172% I s P S D %
173% %
174% %
175% %
176%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177%
178% IsPSD()() returns MagickTrue if the image format type, identified by the
179% magick string, is PSD.
180%
181% The format of the IsPSD method is:
182%
183% MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
184%
185% A description of each parameter follows:
186%
187% o magick: compare image format pattern against these bytes.
188%
189% o length: Specifies the length of the magick string.
190%
191*/
192static MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
193{
194 if (length < 4)
195 return(MagickFalse);
196 if (LocaleNCompare((const char *) magick,"8BPS",4) == 0)
197 return(MagickTrue);
198 return(MagickFalse);
199}
200
201/*
202%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203% %
204% %
205% %
206% R e a d P S D I m a g e %
207% %
208% %
209% %
210%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211%
212% ReadPSDImage() reads an Adobe Photoshop image file and returns it. It
213% allocates the memory necessary for the new Image structure and returns a
214% pointer to the new image.
215%
216% The format of the ReadPSDImage method is:
217%
cristycd081772010-03-22 17:19:12 +0000218% Image *ReadPSDImage(image_info)
cristy3ed852e2009-09-05 21:47:34 +0000219%
220% A description of each parameter follows:
221%
222% o image_info: the image info.
223%
224% o exception: return any errors or warnings in this structure.
225%
226*/
227
cristy19eb6412010-04-23 14:42:29 +0000228static const char *CompositeOperatorToPSDBlendMode(CompositeOperator op)
cristycd081772010-03-22 17:19:12 +0000229{
230 const char
231 *blend_mode;
232
cristy19eb6412010-04-23 14:42:29 +0000233 switch (op)
cristycd081772010-03-22 17:19:12 +0000234 {
235 case OverCompositeOp: blend_mode = "norm"; break;
236 case MultiplyCompositeOp: blend_mode = "mul "; break;
237 case DissolveCompositeOp: blend_mode = "diss"; break;
238 case DifferenceCompositeOp: blend_mode = "diff"; break;
239 case DarkenCompositeOp: blend_mode = "dark"; break;
240 case LightenCompositeOp: blend_mode = "lite"; break;
241 case HueCompositeOp: blend_mode = "hue "; break;
242 case SaturateCompositeOp: blend_mode = "sat "; break;
243 case ColorizeCompositeOp: blend_mode = "colr"; break;
244 case LuminizeCompositeOp: blend_mode = "lum "; break;
245 case ScreenCompositeOp: blend_mode = "scrn"; break;
246 case OverlayCompositeOp: blend_mode = "over"; break;
247 default:
248 blend_mode = "norm";
249 }
250 return(blend_mode);
251}
252
253static ssize_t DecodePSDPixels(const size_t number_compact_pixels,
cristybb503372010-05-27 20:51:26 +0000254 const unsigned char *compact_pixels,const ssize_t depth,
cristycd081772010-03-22 17:19:12 +0000255 const size_t number_pixels,unsigned char *pixels)
256{
257 int
258 pixel;
259
260 register ssize_t
261 i,
262 j;
263
cristycd081772010-03-22 17:19:12 +0000264 size_t
265 length;
266
cristy802d3642011-04-27 02:02:41 +0000267 ssize_t
268 packets;
269
cristycd081772010-03-22 17:19:12 +0000270 packets=(ssize_t) number_compact_pixels;
271 for (i=0; (packets > 1) && (i < (ssize_t) number_pixels); )
272 {
273 length=(*compact_pixels++);
274 packets--;
275 if (length == 128)
276 continue;
277 if (length > 128)
278 {
279 length=256-length+1;
280 pixel=(*compact_pixels++);
281 packets--;
cristy284c7d82010-04-24 00:19:14 +0000282 for (j=0; j < (ssize_t) length; j++)
cristycd081772010-03-22 17:19:12 +0000283 {
284 switch (depth)
285 {
286 case 1:
287 {
cristy284c7d82010-04-24 00:19:14 +0000288 *pixels++=(pixel >> 7) & 0x01 ? 0U : 255U;
289 *pixels++=(pixel >> 6) & 0x01 ? 0U : 255U;
290 *pixels++=(pixel >> 5) & 0x01 ? 0U : 255U;
291 *pixels++=(pixel >> 4) & 0x01 ? 0U : 255U;
292 *pixels++=(pixel >> 3) & 0x01 ? 0U : 255U;
293 *pixels++=(pixel >> 2) & 0x01 ? 0U : 255U;
294 *pixels++=(pixel >> 1) & 0x01 ? 0U : 255U;
295 *pixels++=(pixel >> 0) & 0x01 ? 0U : 255U;
cristycd081772010-03-22 17:19:12 +0000296 i+=8;
297 break;
298 }
299 case 4:
300 {
cristy284c7d82010-04-24 00:19:14 +0000301 *pixels++=(unsigned char) ((pixel >> 4) & 0xff);
302 *pixels++=(unsigned char) ((pixel & 0x0f) & 0xff);
cristycd081772010-03-22 17:19:12 +0000303 i+=2;
304 break;
305 }
306 case 2:
307 {
cristy284c7d82010-04-24 00:19:14 +0000308 *pixels++=(unsigned char) ((pixel >> 6) & 0x03);
309 *pixels++=(unsigned char) ((pixel >> 4) & 0x03);
310 *pixels++=(unsigned char) ((pixel >> 2) & 0x03);
311 *pixels++=(unsigned char) ((pixel & 0x03) & 0x03);
cristycd081772010-03-22 17:19:12 +0000312 i+=4;
313 break;
314 }
315 default:
316 {
cristy284c7d82010-04-24 00:19:14 +0000317 *pixels++=(unsigned char) pixel;
cristycd081772010-03-22 17:19:12 +0000318 i++;
319 break;
320 }
321 }
322 }
323 continue;
324 }
325 length++;
cristy284c7d82010-04-24 00:19:14 +0000326 for (j=0; j < (ssize_t) length; j++)
cristycd081772010-03-22 17:19:12 +0000327 {
328 switch (depth)
329 {
330 case 1:
331 {
cristy284c7d82010-04-24 00:19:14 +0000332 *pixels++=(*compact_pixels >> 7) & 0x01 ? 0U : 255U;
333 *pixels++=(*compact_pixels >> 6) & 0x01 ? 0U : 255U;
334 *pixels++=(*compact_pixels >> 5) & 0x01 ? 0U : 255U;
335 *pixels++=(*compact_pixels >> 4) & 0x01 ? 0U : 255U;
336 *pixels++=(*compact_pixels >> 3) & 0x01 ? 0U : 255U;
337 *pixels++=(*compact_pixels >> 2) & 0x01 ? 0U : 255U;
338 *pixels++=(*compact_pixels >> 1) & 0x01 ? 0U : 255U;
339 *pixels++=(*compact_pixels >> 0) & 0x01 ? 0U : 255U;
cristycd081772010-03-22 17:19:12 +0000340 i+=8;
341 break;
342 }
343 case 4:
344 {
cristy618a9662010-03-23 13:59:54 +0000345 *pixels++=(*compact_pixels >> 4) & 0xff;
346 *pixels++=(*compact_pixels & 0x0f) & 0xff;
cristycd081772010-03-22 17:19:12 +0000347 i+=2;
348 break;
349 }
350 case 2:
351 {
cristy618a9662010-03-23 13:59:54 +0000352 *pixels++=(*compact_pixels >> 6) & 0x03;
353 *pixels++=(*compact_pixels >> 4) & 0x03;
354 *pixels++=(*compact_pixels >> 2) & 0x03;
355 *pixels++=(*compact_pixels & 0x03) & 0x03;
cristycd081772010-03-22 17:19:12 +0000356 i+=4;
357 break;
358 }
359 default:
360 {
361 *pixels++=(*compact_pixels);
362 i++;
363 break;
364 }
365 }
366 compact_pixels++;
367 }
368 }
369 return(i);
370}
371
cristy2d3d87f2010-03-01 00:23:08 +0000372static inline MagickOffsetType GetPSDOffset(PSDInfo *psd_info,Image *image)
373{
374 if (psd_info->version == 1)
375 return((MagickOffsetType) ReadBlobMSBShort(image));
376 return((MagickOffsetType) ReadBlobMSBLong(image));
377}
378
379static inline MagickSizeType GetPSDSize(PSDInfo *psd_info,Image *image)
380{
381 if (psd_info->version == 1)
382 return((MagickSizeType) ReadBlobMSBLong(image));
383 return((MagickSizeType) ReadBlobMSBLongLong(image));
384}
385
cristybb503372010-05-27 20:51:26 +0000386static inline ssize_t MagickAbsoluteValue(const ssize_t x)
cristy3ed852e2009-09-05 21:47:34 +0000387{
388 if (x < 0)
389 return(-x);
390 return(x);
391}
392
cristycd081772010-03-22 17:19:12 +0000393static const char *ModeToString(PSDImageType type)
cristy3ed852e2009-09-05 21:47:34 +0000394{
cristycd081772010-03-22 17:19:12 +0000395 switch (type)
cristy3ed852e2009-09-05 21:47:34 +0000396 {
397 case BitmapMode: return "Bitmap";
398 case GrayscaleMode: return "Grayscale";
399 case IndexedMode: return "Indexed";
400 case RGBMode: return "RGB";
401 case CMYKMode: return "CMYK";
402 case MultichannelMode: return "Multichannel";
403 case DuotoneMode: return "Duotone";
404 case LabMode: return "L*A*B";
405 default: return "unknown";
406 }
407}
408
409static MagickBooleanType ParseImageResourceBlocks(Image *image,
cristyd15e6592011-10-15 00:13:06 +0000410 const unsigned char *blocks,size_t length,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000411{
412 const unsigned char
413 *p;
414
415 StringInfo
416 *profile;
417
cristy6befb0f2010-05-31 14:33:15 +0000418 unsigned int
cristy3ed852e2009-09-05 21:47:34 +0000419 count,
cristy6befb0f2010-05-31 14:33:15 +0000420 long_sans;
cristy3ed852e2009-09-05 21:47:34 +0000421
422 unsigned short
423 id,
424 short_sans;
425
426 if (length < 16)
427 return(MagickFalse);
cristy8723e4b2011-09-01 13:11:19 +0000428 profile=BlobToStringInfo((const void *) NULL,length);
cristy3ed852e2009-09-05 21:47:34 +0000429 SetStringInfoDatum(profile,blocks);
cristyd15e6592011-10-15 00:13:06 +0000430 (void) SetImageProfile(image,"8bim",profile,exception);
cristy3ed852e2009-09-05 21:47:34 +0000431 profile=DestroyStringInfo(profile);
432 for (p=blocks; (p >= blocks) && (p < (blocks+length-16)); )
433 {
434 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
435 break;
cristy6befb0f2010-05-31 14:33:15 +0000436 p=PushLongPixel(MSBEndian,p,&long_sans);
cristyf11065e2010-05-14 13:26:59 +0000437 p=PushShortPixel(MSBEndian,p,&id);
438 p=PushShortPixel(MSBEndian,p,&short_sans);
439 p=PushLongPixel(MSBEndian,p,&count);
cristy3ed852e2009-09-05 21:47:34 +0000440 switch (id)
441 {
442 case 0x03ed:
443 {
cristy1e4a80b2010-05-14 16:18:26 +0000444 char
445 value[MaxTextExtent];
446
cristy3ed852e2009-09-05 21:47:34 +0000447 unsigned short
448 resolution;
449
450 /*
451 Resolution info.
452 */
cristyf11065e2010-05-14 13:26:59 +0000453 p=PushShortPixel(MSBEndian,p,&resolution);
cristy2a11bef2011-10-28 18:33:11 +0000454 image->resolution.x=(double) resolution;
455 (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.x);
cristyd15e6592011-10-15 00:13:06 +0000456 (void) SetImageProperty(image,"tiff:XResolution",value,exception);
cristyf11065e2010-05-14 13:26:59 +0000457 p=PushShortPixel(MSBEndian,p,&short_sans);
458 p=PushShortPixel(MSBEndian,p,&short_sans);
459 p=PushShortPixel(MSBEndian,p,&short_sans);
460 p=PushShortPixel(MSBEndian,p,&resolution);
cristy2a11bef2011-10-28 18:33:11 +0000461 image->resolution.y=(double) resolution;
462 (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.y);
cristyd15e6592011-10-15 00:13:06 +0000463 (void) SetImageProperty(image,"tiff:YResolution",value,exception);
cristyf11065e2010-05-14 13:26:59 +0000464 p=PushShortPixel(MSBEndian,p,&short_sans);
465 p=PushShortPixel(MSBEndian,p,&short_sans);
466 p=PushShortPixel(MSBEndian,p,&short_sans);
cristy3ed852e2009-09-05 21:47:34 +0000467 break;
468 }
469 default:
470 {
471 p+=count;
472 break;
473 }
474 }
475 if ((count & 0x01) != 0)
476 p++;
477 }
478 return(MagickTrue);
479}
480
cristycd081772010-03-22 17:19:12 +0000481static CompositeOperator PSDBlendModeToCompositeOperator(const char *mode)
cristy56ed31c2010-03-22 00:46:21 +0000482{
cristycd081772010-03-22 17:19:12 +0000483 if (mode == (const char *) NULL)
484 return(OverCompositeOp);
485 if (LocaleNCompare(mode,"norm",4) == 0)
486 return(OverCompositeOp);
487 if (LocaleNCompare(mode,"mul ",4) == 0)
488 return(MultiplyCompositeOp);
489 if (LocaleNCompare(mode,"diss",4) == 0)
490 return(DissolveCompositeOp);
491 if (LocaleNCompare(mode,"diff",4) == 0)
492 return(DifferenceCompositeOp);
493 if (LocaleNCompare(mode,"dark",4) == 0)
494 return(DarkenCompositeOp);
495 if (LocaleNCompare(mode,"lite",4) == 0)
496 return(LightenCompositeOp);
497 if (LocaleNCompare(mode,"hue ",4) == 0)
498 return(HueCompositeOp);
499 if (LocaleNCompare(mode,"sat ",4) == 0)
500 return(SaturateCompositeOp);
501 if (LocaleNCompare(mode,"colr",4) == 0)
502 return(ColorizeCompositeOp);
503 if (LocaleNCompare(mode,"lum ",4) == 0)
504 return(LuminizeCompositeOp);
505 if (LocaleNCompare(mode,"scrn",4) == 0)
506 return(ScreenCompositeOp);
507 if (LocaleNCompare(mode,"over",4) == 0)
508 return(OverlayCompositeOp);
509 if (LocaleNCompare(mode,"hLit",4) == 0)
510 return(OverCompositeOp);
511 if (LocaleNCompare(mode,"sLit",4) == 0)
512 return(OverCompositeOp);
513 if (LocaleNCompare(mode,"smud",4) == 0)
514 return(OverCompositeOp);
515 if (LocaleNCompare(mode,"div ",4) == 0)
516 return(OverCompositeOp);
517 if (LocaleNCompare(mode,"idiv",4) == 0)
518 return(OverCompositeOp);
519 return(OverCompositeOp);
cristy56ed31c2010-03-22 00:46:21 +0000520}
521
cristybb503372010-05-27 20:51:26 +0000522static MagickBooleanType ReadPSDLayer(Image *image,const size_t channels,
523 const ssize_t type,const MagickOffsetType *offsets,ExceptionInfo *exception)
cristy56ed31c2010-03-22 00:46:21 +0000524{
cristyd05dca12010-07-25 02:32:32 +0000525 ColorspaceType
526 colorspace;
cristy56ed31c2010-03-22 00:46:21 +0000527
528 Quantum
529 pixel;
530
cristy7753b2a2011-02-19 18:36:52 +0000531 register const unsigned char
532 *p;
533
cristy4c08aed2011-07-01 19:47:50 +0000534 register Quantum
cristy56ed31c2010-03-22 00:46:21 +0000535 *q;
536
cristy7753b2a2011-02-19 18:36:52 +0000537 register ssize_t
538 x;
cristy56ed31c2010-03-22 00:46:21 +0000539
540 size_t
541 packet_size;
542
543 ssize_t
cristyd05dca12010-07-25 02:32:32 +0000544 count,
545 y;
cristy56ed31c2010-03-22 00:46:21 +0000546
547 unsigned char
548 *compact_pixels,
549 *pixels;
550
551 unsigned short
552 nibble;
553
554 packet_size=1;
555 if (image->storage_class == PseudoClass)
556 {
557 if (image->colors > 256)
558 packet_size++;
559 else
560 if (image->depth > 8)
561 packet_size++;
562 }
563 else
564 if (image->depth > 8)
565 packet_size++;
cristycd081772010-03-22 17:19:12 +0000566 pixels=(unsigned char *) AcquireQuantumMemory(image->columns+256,packet_size*
cristy56ed31c2010-03-22 00:46:21 +0000567 sizeof(*pixels));
568 if (pixels == (unsigned char *) NULL)
569 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
570 image->filename);
cristycd081772010-03-22 17:19:12 +0000571 (void) ResetMagickMemory(pixels,0,image->columns*packet_size*sizeof(*pixels));
572 compact_pixels=(unsigned char *) NULL;
cristy56ed31c2010-03-22 00:46:21 +0000573 if (image->compression == RLECompression)
574 {
cristye195f262010-04-16 18:12:35 +0000575 size_t
576 length;
577
578 length=0;
cristybb503372010-05-27 20:51:26 +0000579 for (y=0; y < (ssize_t) image->rows; y++)
cristye195f262010-04-16 18:12:35 +0000580 if ((MagickOffsetType) length < offsets[y])
cristy284c7d82010-04-24 00:19:14 +0000581 length=(size_t) offsets[y];
cristye195f262010-04-16 18:12:35 +0000582 compact_pixels=(unsigned char *) AcquireQuantumMemory(length,
583 sizeof(*pixels));
cristy56ed31c2010-03-22 00:46:21 +0000584 if (compact_pixels == (unsigned char *) NULL)
585 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
586 image->filename);
cristy0132ba82010-04-16 23:34:02 +0000587 (void) ResetMagickMemory(compact_pixels,0,length*sizeof(*compact_pixels));
cristy56ed31c2010-03-22 00:46:21 +0000588 }
cristyd05dca12010-07-25 02:32:32 +0000589 colorspace=image->colorspace;
cristybb503372010-05-27 20:51:26 +0000590 for (y=0; y < (ssize_t) image->rows; y++)
cristy56ed31c2010-03-22 00:46:21 +0000591 {
cristy3b004082010-08-12 19:56:38 +0000592 if (image->depth == 1)
593 {
cristyb6cabe82011-03-18 13:25:59 +0000594 if (image->compression != RLECompression)
595 count=ReadBlob(image,(image->columns+7)/8,pixels);
596 else
597 {
598 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
599 if (count != (ssize_t) offsets[y])
600 break;
601 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
602 (ssize_t) 123456,(size_t) ((image->columns+7)/8),pixels);
603 }
cristy3b004082010-08-12 19:56:38 +0000604 if (count < (ssize_t) ((image->columns+7)/8))
605 break;
606 }
cristy56ed31c2010-03-22 00:46:21 +0000607 else
608 {
cristy3b004082010-08-12 19:56:38 +0000609 if (image->compression != RLECompression)
610 count=ReadBlob(image,packet_size*image->columns,pixels);
611 else
612 {
613 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
614 if (count != (ssize_t) offsets[y])
615 break;
616 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
617 (ssize_t) image->depth,packet_size*image->columns,pixels);
618 }
619 if (count < (ssize_t) (packet_size*image->columns))
cristy56ed31c2010-03-22 00:46:21 +0000620 break;
cristy56ed31c2010-03-22 00:46:21 +0000621 }
cristy56ed31c2010-03-22 00:46:21 +0000622 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000623 if (q == (Quantum *) NULL)
cristy56ed31c2010-03-22 00:46:21 +0000624 break;
cristy56ed31c2010-03-22 00:46:21 +0000625 p=pixels;
cristybb503372010-05-27 20:51:26 +0000626 for (x=0; x < (ssize_t) image->columns; x++)
cristy56ed31c2010-03-22 00:46:21 +0000627 {
628 if (packet_size == 1)
629 pixel=ScaleCharToQuantum(*p++);
630 else
631 {
632 p=PushShortPixel(MSBEndian,p,&nibble);
633 pixel=ScaleShortToQuantum(nibble);
634 }
635 switch (type)
636 {
637 case -1:
638 {
cristy4c08aed2011-07-01 19:47:50 +0000639 SetPixelAlpha(image,pixel,q);
cristy56ed31c2010-03-22 00:46:21 +0000640 break;
641 }
642 case 0:
643 {
cristy4c08aed2011-07-01 19:47:50 +0000644 SetPixelRed(image,pixel,q);
cristy56ed31c2010-03-22 00:46:21 +0000645 if (channels == 1)
cristyaf6eb932012-01-01 01:22:59 +0000646 SetPixelGray(image,pixel,q);
647 else
648 SetPixelRed(image,pixel,q);
cristy56ed31c2010-03-22 00:46:21 +0000649 if (image->storage_class == PseudoClass)
650 {
651 if (packet_size == 1)
cristy4c08aed2011-07-01 19:47:50 +0000652 SetPixelIndex(image,ScaleQuantumToChar(pixel),q);
cristy56ed31c2010-03-22 00:46:21 +0000653 else
cristy4c08aed2011-07-01 19:47:50 +0000654 SetPixelIndex(image,ScaleQuantumToShort(pixel),q);
cristy803640d2011-11-17 02:11:32 +0000655 SetPixelInfoPixel(image,image->colormap+(ssize_t)
cristy4c08aed2011-07-01 19:47:50 +0000656 GetPixelIndex(image,q),q);
cristy3b004082010-08-12 19:56:38 +0000657 if (image->depth == 1)
658 {
659 ssize_t
cristyb6cabe82011-03-18 13:25:59 +0000660 bit,
661 number_bits;
cristy3b004082010-08-12 19:56:38 +0000662
cristyb6cabe82011-03-18 13:25:59 +0000663 number_bits=image->columns-x;
664 if (number_bits > 8)
665 number_bits=8;
666 for (bit=0; bit < number_bits; bit++)
cristy3b004082010-08-12 19:56:38 +0000667 {
cristy4c08aed2011-07-01 19:47:50 +0000668 SetPixelIndex(image,(((unsigned char) pixel) &
669 (0x01 << (7-bit))) != 0 ? 0 : 255,q);
cristy803640d2011-11-17 02:11:32 +0000670 SetPixelInfoPixel(image,image->colormap+(ssize_t)
cristy4c08aed2011-07-01 19:47:50 +0000671 GetPixelIndex(image,q),q);
cristyed231572011-07-14 02:18:59 +0000672 q+=GetPixelChannels(image);
cristy3b004082010-08-12 19:56:38 +0000673 x++;
674 }
cristy3b004082010-08-12 19:56:38 +0000675 }
cristy56ed31c2010-03-22 00:46:21 +0000676 }
677 break;
678 }
679 case 1:
680 {
681 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000682 SetPixelAlpha(image,pixel,q);
cristy56ed31c2010-03-22 00:46:21 +0000683 else
cristy4c08aed2011-07-01 19:47:50 +0000684 SetPixelGreen(image,pixel,q);
cristy56ed31c2010-03-22 00:46:21 +0000685 break;
686 }
687 case 2:
688 {
cristydede49f2010-08-20 20:26:26 +0000689 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000690 SetPixelAlpha(image,pixel,q);
cristydede49f2010-08-20 20:26:26 +0000691 else
cristy4c08aed2011-07-01 19:47:50 +0000692 SetPixelBlue(image,pixel,q);
cristy56ed31c2010-03-22 00:46:21 +0000693 break;
694 }
695 case 3:
696 {
697 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000698 SetPixelBlack(image,pixel,q);
cristy56ed31c2010-03-22 00:46:21 +0000699 else
cristy8a46d822012-08-28 23:32:39 +0000700 if (image->alpha_trait == BlendPixelTrait)
cristy374b8ad2012-02-01 20:55:21 +0000701 SetPixelAlpha(image,pixel,q);
cristy56ed31c2010-03-22 00:46:21 +0000702 break;
703 }
704 case 4:
705 {
cristy3d9f5ba2012-06-26 13:37:31 +0000706 if ((IssRGBCompatibleColorspace(image->colorspace) != MagickFalse) &&
cristy374b8ad2012-02-01 20:55:21 +0000707 (channels > 3))
cristya60c0202010-07-31 21:36:45 +0000708 break;
cristy8a46d822012-08-28 23:32:39 +0000709 if (image->alpha_trait == BlendPixelTrait)
cristy374b8ad2012-02-01 20:55:21 +0000710 SetPixelAlpha(image,pixel,q);
cristy56ed31c2010-03-22 00:46:21 +0000711 break;
712 }
713 default:
714 break;
715 }
cristyed231572011-07-14 02:18:59 +0000716 q+=GetPixelChannels(image);
cristy56ed31c2010-03-22 00:46:21 +0000717 }
718 if (SyncAuthenticPixels(image,exception) == MagickFalse)
719 break;
720 }
cristyd05dca12010-07-25 02:32:32 +0000721 image->colorspace=colorspace;
cristy56ed31c2010-03-22 00:46:21 +0000722 if (image->compression == RLECompression)
723 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
724 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
725 return(MagickTrue);
726}
727
cristy3ed852e2009-09-05 21:47:34 +0000728static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception)
729{
cristy3ed852e2009-09-05 21:47:34 +0000730 char
cristy56ed31c2010-03-22 00:46:21 +0000731 message[MaxTextExtent],
cristy3ed852e2009-09-05 21:47:34 +0000732 type[4];
733
734 Image
735 *image;
736
cristy3ed852e2009-09-05 21:47:34 +0000737 LayerInfo
738 *layer_info;
739
cristy56ed31c2010-03-22 00:46:21 +0000740 MagickBooleanType
cristy374b8ad2012-02-01 20:55:21 +0000741 check_background,
cristy56ed31c2010-03-22 00:46:21 +0000742 status;
743
744 MagickOffsetType
745 offset,
746 *offsets;
747
748 MagickSizeType
749 combinedlength,
750 length,
751 size;
752
cristy3ed852e2009-09-05 21:47:34 +0000753 PSDInfo
754 psd_info;
755
cristy4c08aed2011-07-01 19:47:50 +0000756 register Quantum
cristydede49f2010-08-20 20:26:26 +0000757 *q;
758
cristybb503372010-05-27 20:51:26 +0000759 register ssize_t
cristy56ed31c2010-03-22 00:46:21 +0000760 i,
cristy3ed852e2009-09-05 21:47:34 +0000761 x;
762
cristydc05fc22011-01-22 19:43:15 +0000763 size_t
764 mask_size,
765 skip_first_alpha = 0;
766
cristy3ed852e2009-09-05 21:47:34 +0000767 ssize_t
cristydc05fc22011-01-22 19:43:15 +0000768 count,
769 j,
770 number_layers,
771 y;
cristy3ed852e2009-09-05 21:47:34 +0000772
cristy3ed852e2009-09-05 21:47:34 +0000773 unsigned char
774 *data;
775
776 unsigned short
777 compression;
778
cristy3ed852e2009-09-05 21:47:34 +0000779 /*
780 Open image file.
781 */
782 assert(image_info != (const ImageInfo *) NULL);
783 assert(image_info->signature == MagickSignature);
784 if (image_info->debug != MagickFalse)
785 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
786 image_info->filename);
787 assert(exception != (ExceptionInfo *) NULL);
788 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000789 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000790 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
791 if (status == MagickFalse)
792 {
793 image=DestroyImageList(image);
794 return((Image *) NULL);
795 }
796 /*
797 Read image header.
798 */
799 count=ReadBlob(image,4,(unsigned char *) psd_info.signature);
800 psd_info.version=ReadBlobMSBShort(image);
cristy50aea4a2010-03-09 17:37:44 +0000801 if ((count == 0) || (LocaleNCompare(psd_info.signature,"8BPS",4) != 0) ||
cristy2d3d87f2010-03-01 00:23:08 +0000802 ((psd_info.version != 1) && (psd_info.version != 2)))
cristy3ed852e2009-09-05 21:47:34 +0000803 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
804 count=ReadBlob(image,6,psd_info.reserved);
805 psd_info.channels=ReadBlobMSBShort(image);
cristy374b8ad2012-02-01 20:55:21 +0000806 psd_info.color_channels=psd_info.channels;
cristy3ed852e2009-09-05 21:47:34 +0000807 if (psd_info.channels > MaxPSDChannels)
808 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
809 psd_info.rows=ReadBlobMSBLong(image);
810 psd_info.columns=ReadBlobMSBLong(image);
cristy2d3d87f2010-03-01 00:23:08 +0000811 if ((psd_info.version == 1) && ((psd_info.rows > 30000) ||
812 (psd_info.columns > 30000)))
813 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000814 psd_info.depth=ReadBlobMSBShort(image);
cristy2d3d87f2010-03-01 00:23:08 +0000815 if ((psd_info.depth != 1) && (psd_info.depth != 8) && (psd_info.depth != 16))
816 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000817 psd_info.mode=ReadBlobMSBShort(image);
818 if (image->debug != MagickFalse)
819 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000820 " Image is %.20g x %.20g with channels=%.20g, depth=%.20g, mode=%s",
821 (double) psd_info.columns,(double) psd_info.rows,(double)
822 psd_info.channels,(double) psd_info.depth,ModeToString((PSDImageType)
823 psd_info.mode));
cristy3ed852e2009-09-05 21:47:34 +0000824 /*
825 Initialize image.
826 */
827 image->depth=psd_info.depth;
828 image->columns=psd_info.columns;
829 image->rows=psd_info.rows;
cristyea1a8aa2011-10-20 13:24:06 +0000830 if (SetImageBackgroundColor(image,exception) == MagickFalse)
cristy95524f92010-02-16 18:44:34 +0000831 {
cristy95524f92010-02-16 18:44:34 +0000832 image=DestroyImageList(image);
833 return((Image *) NULL);
834 }
cristy8a46d822012-08-28 23:32:39 +0000835 image->alpha_trait=psd_info.channels >= 4 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000836 if (psd_info.mode == LabMode)
cristye2c4f182012-05-12 14:11:53 +0000837 SetImageColorspace(image,LabColorspace,exception);
cristy374b8ad2012-02-01 20:55:21 +0000838 psd_info.color_channels=3;
cristy3ed852e2009-09-05 21:47:34 +0000839 if (psd_info.mode == CMYKMode)
840 {
cristy374b8ad2012-02-01 20:55:21 +0000841 psd_info.color_channels=4;
cristye2c4f182012-05-12 14:11:53 +0000842 SetImageColorspace(image,CMYKColorspace,exception);
cristy8a46d822012-08-28 23:32:39 +0000843 image->alpha_trait=psd_info.channels >= 5 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000844 }
845 if ((psd_info.mode == BitmapMode) || (psd_info.mode == GrayscaleMode) ||
846 (psd_info.mode == DuotoneMode))
847 {
cristy374b8ad2012-02-01 20:55:21 +0000848 psd_info.color_channels=1;
cristy018f07f2011-09-04 21:15:19 +0000849 if (AcquireImageColormap(image,256,exception) == MagickFalse)
cristy52cf7f12010-02-07 18:07:56 +0000850 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy8a46d822012-08-28 23:32:39 +0000851 image->alpha_trait=psd_info.channels >= 2 ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000852 if (image->debug != MagickFalse)
853 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristydede49f2010-08-20 20:26:26 +0000854 " Image colormap allocated");
cristye2c4f182012-05-12 14:11:53 +0000855 SetImageColorspace(image,GRAYColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000856 }
cristy8a46d822012-08-28 23:32:39 +0000857 image->alpha_trait=UndefinedPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +0000858 /*
859 Read PSD raster colormap only present for indexed and duotone images.
860 */
861 length=ReadBlobMSBLong(image);
862 if (length != 0)
863 {
864 if (image->debug != MagickFalse)
865 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
866 " reading colormap");
867 if (psd_info.mode == DuotoneMode)
868 {
869 /*
870 Duotone image data; the format of this data is undocumented.
871 */
cristy56ed31c2010-03-22 00:46:21 +0000872 data=(unsigned char *) AcquireQuantumMemory((size_t) length,
873 sizeof(*data));
cristy3ed852e2009-09-05 21:47:34 +0000874 if (data == (unsigned char *) NULL)
875 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy56ed31c2010-03-22 00:46:21 +0000876 count=ReadBlob(image,(size_t) length,data);
cristy3ed852e2009-09-05 21:47:34 +0000877 data=(unsigned char *) RelinquishMagickMemory(data);
878 }
879 else
880 {
881 /*
882 Read PSD raster colormap.
883 */
cristy018f07f2011-09-04 21:15:19 +0000884 if (AcquireImageColormap(image,(size_t) (length/3),exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000885 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000886 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000887 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
888 ReadBlobByte(image));
cristybb503372010-05-27 20:51:26 +0000889 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000890 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
891 ReadBlobByte(image));
cristybb503372010-05-27 20:51:26 +0000892 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000893 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
894 ReadBlobByte(image));
cristy8a46d822012-08-28 23:32:39 +0000895 image->alpha_trait=UndefinedPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +0000896 }
897 }
898 length=ReadBlobMSBLong(image);
899 if (length != 0)
900 {
901 unsigned char
902 *blocks;
903
904 /*
905 Image resources block.
906 */
907 if (image->debug != MagickFalse)
908 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy2b9582a2011-07-04 17:38:56 +0000909 " reading image resource blocks - %.20g bytes",(double)
910 ((MagickOffsetType) length));
cristy56ed31c2010-03-22 00:46:21 +0000911 blocks=(unsigned char *) AcquireQuantumMemory((size_t) length,
912 sizeof(*blocks));
cristy3ed852e2009-09-05 21:47:34 +0000913 if (blocks == (unsigned char *) NULL)
914 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy56ed31c2010-03-22 00:46:21 +0000915 count=ReadBlob(image,(size_t) length,blocks);
916 if ((count != (ssize_t) length) ||
cristy3ed852e2009-09-05 21:47:34 +0000917 (LocaleNCompare((char *) blocks,"8BIM",4) != 0))
918 {
919 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
920 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
921 }
cristyd15e6592011-10-15 00:13:06 +0000922 (void) ParseImageResourceBlocks(image,blocks,(size_t) length,
923 exception);
cristy3ed852e2009-09-05 21:47:34 +0000924 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
925 }
926 /*
cristy3ed852e2009-09-05 21:47:34 +0000927 Layer and mask block.
928 */
929 layer_info=(LayerInfo *) NULL;
cristy4689cf02010-02-17 21:15:45 +0000930 number_layers=1;
cristy2d3d87f2010-03-01 00:23:08 +0000931 length=GetPSDSize(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +0000932 if (length == 8)
933 {
934 length=ReadBlobMSBLong(image);
935 length=ReadBlobMSBLong(image);
936 }
cristy374b8ad2012-02-01 20:55:21 +0000937 check_background=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000938 if ((image_info->number_scenes == 1) && (image_info->scene == 0))
cristyd4297022010-09-16 22:59:09 +0000939 {
cristy374b8ad2012-02-01 20:55:21 +0000940 if (image->debug != MagickFalse)
941 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
942 " read composite only");
943 check_background=MagickTrue;
cristyd4297022010-09-16 22:59:09 +0000944 }
cristy3ed852e2009-09-05 21:47:34 +0000945 if (length == 0)
946 {
947 if (image->debug != MagickFalse)
948 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
949 " image has no layers");
950 }
951 else
952 {
953 offset=TellBlob(image);
cristy2d3d87f2010-03-01 00:23:08 +0000954 size=GetPSDSize(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +0000955 if (size == 0)
956 {
cristybb503372010-05-27 20:51:26 +0000957 size_t
cristy2d3d87f2010-03-01 00:23:08 +0000958 quantum;
959
cristy9fb55e82011-02-27 23:12:47 +0000960 unsigned long
961 tag;
962
cristy3ed852e2009-09-05 21:47:34 +0000963 /*
964 Skip layers & masks.
965 */
cristy56ed31c2010-03-22 00:46:21 +0000966 quantum=psd_info.version == 1 ? 4UL : 8UL;
cristy9fb55e82011-02-27 23:12:47 +0000967 tag=ReadBlobMSBLong(image);
cristy6c70aec2011-03-04 20:48:09 +0000968 (void) tag;
969 count=ReadBlob(image,4,(unsigned char *) type);
970 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
cristy9fb55e82011-02-27 23:12:47 +0000971 {
972 if (DiscardBlobBytes(image,length-quantum-8) == MagickFalse)
973 ThrowFileException(exception,CorruptImageError,
974 "UnexpectedEndOfFile",image->filename);
975 }
976 else
977 {
cristy6c70aec2011-03-04 20:48:09 +0000978 count=ReadBlob(image,4,(unsigned char *) type);
979 if ((count != 0) && (LocaleNCompare(type,"Lr16",4) == 0))
cristy9fb55e82011-02-27 23:12:47 +0000980 size=GetPSDSize(&psd_info,image);
981 else
982 if (DiscardBlobBytes(image,length-quantum-12) == MagickFalse)
983 ThrowFileException(exception,CorruptImageError,
984 "UnexpectedEndOfFile",image->filename);
985 }
cristy3ed852e2009-09-05 21:47:34 +0000986 }
cristy9fb55e82011-02-27 23:12:47 +0000987 if (size != 0)
cristy3ed852e2009-09-05 21:47:34 +0000988 {
cristyb541bd72012-02-20 14:55:11 +0000989 MagickOffsetType
990 layer_offset;
991
cristy8a46d822012-08-28 23:32:39 +0000992 image->alpha_trait=psd_info.channels > psd_info.color_channels ? MagickTrue : MagickFalse;
cristyb541bd72012-02-20 14:55:11 +0000993
cristy374b8ad2012-02-01 20:55:21 +0000994 if (image->debug != MagickFalse)
995 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy8a46d822012-08-28 23:32:39 +0000996 image->alpha_trait ? " image has matte" : " image has no matte");
cristy3ed852e2009-09-05 21:47:34 +0000997
998 layer_offset=offset+length;
999 number_layers=(short) ReadBlobMSBShort(image);
1000 if (image->debug != MagickFalse)
1001 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001002 " image contains %.20g layers",(double) number_layers);
cristy3ed852e2009-09-05 21:47:34 +00001003 if (number_layers < 0)
1004 {
1005 /*
1006 Weird hack in PSD format to ignore first alpha channel.
1007 */
1008 skip_first_alpha=1;
cristyda16f162011-02-19 23:52:17 +00001009 (void) skip_first_alpha;
cristy3ed852e2009-09-05 21:47:34 +00001010 number_layers=MagickAbsoluteValue(number_layers);
1011 if (image->debug != MagickFalse)
1012 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1013 " negative layer count corrected for");
1014 }
1015 layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers,
1016 sizeof(*layer_info));
1017 if (layer_info == (LayerInfo *) NULL)
1018 {
1019 if (image->debug != MagickFalse)
1020 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1021 " allocation of LayerInfo failed");
1022 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1023 }
1024 (void) ResetMagickMemory(layer_info,0,(size_t) number_layers*
1025 sizeof(*layer_info));
1026 for (i=0; i < number_layers; i++)
1027 {
cristy4484e332010-10-08 23:46:21 +00001028 int
1029 x,
1030 y;
1031
cristy3ed852e2009-09-05 21:47:34 +00001032 if (image->debug != MagickFalse)
1033 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001034 " reading layer #%.20g",(double) i+1);
cristy6cff05d2010-09-02 11:22:46 +00001035 layer_info[i].page.y=(int) ReadBlobMSBLong(image);
1036 layer_info[i].page.x=(int) ReadBlobMSBLong(image);
cristy4484e332010-10-08 23:46:21 +00001037 y=(int) ReadBlobMSBLong(image);
1038 x=(int) ReadBlobMSBLong(image);
1039 layer_info[i].page.width=(ssize_t) (x-layer_info[i].page.x);
1040 layer_info[i].page.height=(ssize_t) (y-layer_info[i].page.y);
cristy3ed852e2009-09-05 21:47:34 +00001041 layer_info[i].channels=ReadBlobMSBShort(image);
cristy374b8ad2012-02-01 20:55:21 +00001042 if (check_background == MagickTrue)
1043 {
1044 size_t
1045 quantum;
1046
1047 if (layer_info[i].channels == psd_info.color_channels)
cristy8a46d822012-08-28 23:32:39 +00001048 image->alpha_trait=UndefinedPixelTrait;
cristy374b8ad2012-02-01 20:55:21 +00001049 quantum=psd_info.version == 1 ? 4UL : 8UL;
1050 if (DiscardBlobBytes(image,length-20-quantum) == MagickFalse)
1051 ThrowFileException(exception,CorruptImageError,
1052 "UnexpectedEndOfFile",image->filename);
1053 break;
1054 }
cristy3ed852e2009-09-05 21:47:34 +00001055 if (layer_info[i].channels > MaxPSDChannels)
1056 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
1057 if (image->debug != MagickFalse)
1058 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001059 " offset(%.20g,%.20g), size(%.20g,%.20g), channels=%.20g",
1060 (double) layer_info[i].page.x,(double) layer_info[i].page.y,
1061 (double) layer_info[i].page.height,(double)
1062 layer_info[i].page.width,(double) layer_info[i].channels);
cristybb503372010-05-27 20:51:26 +00001063 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
cristy3ed852e2009-09-05 21:47:34 +00001064 {
cristy56ed31c2010-03-22 00:46:21 +00001065 layer_info[i].channel_info[j].type=(short)
1066 ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001067 layer_info[i].channel_info[j].size=(size_t)
cristy56ed31c2010-03-22 00:46:21 +00001068 GetPSDSize(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +00001069 if (image->debug != MagickFalse)
1070 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001071 " channel[%.20g]: type=%.20g, size=%.20g",(double) j,
1072 (double) layer_info[i].channel_info[j].type,
1073 (double) layer_info[i].channel_info[j].size);
cristy3ed852e2009-09-05 21:47:34 +00001074 }
1075 count=ReadBlob(image,4,(unsigned char *) type);
1076 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1077 {
1078 if (image->debug != MagickFalse)
1079 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1080 " layer type was %.4s instead of 8BIM", type);
1081 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1082 }
1083 count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey);
cristy4c08aed2011-07-01 19:47:50 +00001084 layer_info[i].opacity=(Quantum) ScaleCharToQuantum((unsigned char)
1085 ReadBlobByte(image));
cristy3ed852e2009-09-05 21:47:34 +00001086 layer_info[i].clipping=(unsigned char) ReadBlobByte(image);
1087 layer_info[i].flags=(unsigned char) ReadBlobByte(image);
1088 layer_info[i].visible=!(layer_info[i].flags & 0x02);
1089 if (image->debug != MagickFalse)
1090 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001091 " blend=%.4s, opacity=%.20g, clipping=%s, flags=%d, visible=%s",
1092 layer_info[i].blendkey,(double) layer_info[i].opacity,
cristy3ed852e2009-09-05 21:47:34 +00001093 layer_info[i].clipping ? "true" : "false",layer_info[i].flags,
1094 layer_info[i].visible ? "true" : "false");
1095 (void) ReadBlobByte(image); /* filler */
1096 combinedlength=0;
1097 size=ReadBlobMSBLong(image);
1098 if (size != 0)
1099 {
1100 if (image->debug != MagickFalse)
1101 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1102 " layer contains additional info");
1103 length=ReadBlobMSBLong(image);
1104 if (length != 0)
1105 {
1106 /*
1107 Layer mask info.
1108 */
cristy6cff05d2010-09-02 11:22:46 +00001109 layer_info[i].mask.y=(int) ReadBlobMSBLong(image);
1110 layer_info[i].mask.x=(int) ReadBlobMSBLong(image);
cristybb503372010-05-27 20:51:26 +00001111 layer_info[i].mask.height=(size_t)
cristy3ed852e2009-09-05 21:47:34 +00001112 (ReadBlobMSBLong(image)-layer_info[i].mask.y);
cristybb503372010-05-27 20:51:26 +00001113 layer_info[i].mask.width=(size_t)
cristy3ed852e2009-09-05 21:47:34 +00001114 (ReadBlobMSBLong(image)-layer_info[i].mask.x);
1115 if (image->debug != MagickFalse)
1116 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001117 " layer mask: offset(%.20g,%.20g), size(%.20g,%.20g), length=%.20g",
cristyd4297022010-09-16 22:59:09 +00001118 (double) layer_info[i].mask.x,(double) layer_info[i].mask.y,
cristye8c25f92010-06-03 00:53:06 +00001119 (double) layer_info[i].mask.width,(double)
cristy2b9582a2011-07-04 17:38:56 +00001120 layer_info[i].mask.height,(double)
1121 ((MagickOffsetType) length-16));
cristy3ed852e2009-09-05 21:47:34 +00001122 /*
1123 Skip over the rest of the layer mask information.
1124 */
cristyc1af14f2010-09-16 20:01:21 +00001125 if (DiscardBlobBytes(image,length-16) == MagickFalse)
cristyd4297022010-09-16 22:59:09 +00001126 ThrowFileException(exception,CorruptImageError,
1127 "UnexpectedEndOfFile",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001128 }
1129 combinedlength+=length+4; /* +4 for length */
1130 length=ReadBlobMSBLong(image);
1131 if (length != 0)
1132 {
1133 /*
1134 Layer blending ranges info.
1135 */
1136 if (image->debug != MagickFalse)
1137 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001138 " layer blending ranges: length=%.20g",(double)
cristy2b9582a2011-07-04 17:38:56 +00001139 ((MagickOffsetType) length));
cristy3ed852e2009-09-05 21:47:34 +00001140 /*
1141 We read it, but don't use it...
1142 */
cristybb503372010-05-27 20:51:26 +00001143 for (j=0; j < (ssize_t) (length); j+=8)
cristy3ed852e2009-09-05 21:47:34 +00001144 {
1145 size_t blend_source=ReadBlobMSBLong(image);
1146 size_t blend_dest=ReadBlobMSBLong(image);
1147 if (image->debug != MagickFalse)
1148 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1149 " source(%x), dest(%x)",(unsigned int)
1150 blend_source,(unsigned int) blend_dest);
1151 }
1152 }
cristy2cc39842009-10-10 20:04:02 +00001153 combinedlength+=length+4;
1154 /*
1155 Layer name.
1156 */
cristy3ed852e2009-09-05 21:47:34 +00001157 length=(size_t) ReadBlobByte(image);
cristybb503372010-05-27 20:51:26 +00001158 for (j=0; j < (ssize_t) length; j++)
cristy2cc39842009-10-10 20:04:02 +00001159 layer_info[i].name[j]=(unsigned char) ReadBlobByte(image);
1160 layer_info[i].name[j]='\0';
1161 if (image->debug != MagickFalse)
1162 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1163 " layer name: %s",layer_info[i].name);
1164 combinedlength+=length+1;
cristy3ed852e2009-09-05 21:47:34 +00001165
1166#if 0 /* still in development */
1167 /*
1168 Adjustment layers and other stuff...
1169 */
1170 {
cristy802d3642011-04-27 02:02:41 +00001171 char alsig[4], alkey[4];
cristy3ed852e2009-09-05 21:47:34 +00001172
1173 count=ReadBlob(image,4,alsig);
1174 if ((count == 0) || (LocaleNCompare(alsig,"8BIM",4) != 0)) {
1175 if (debug != MagickFalse)
1176 {
cristy802d3642011-04-27 02:02:41 +00001177 if (image->debug != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001178 (void) LogMagickEvent(CoderEvent,GetMagickModule()," adjustment layer type was %.4s instead of 8BIM", alsig);
1179 }
1180 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1181 }
1182 count=ReadBlob(image,4,alkey);
1183 length=ReadBlobMSBLong(image);
1184 if (debug != MagickFalse)
1185 {
cristy802d3642011-04-27 02:02:41 +00001186 if (image->debug != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001187 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001188 " adjustment layer key: %.4s, data length=%.20g",
1189 alkey, (double) length);
cristy3ed852e2009-09-05 21:47:34 +00001190 }
1191
1192 if ( length ) {
cristybb503372010-05-27 20:51:26 +00001193 for (j=0; j < (ssize_t) (length); j++)
cristy3ed852e2009-09-05 21:47:34 +00001194 (void) ReadBlobByte(image);
1195 }
1196
1197 }
1198 combinedlength += 12 + length; /* sig, key, length + the actual length*/
1199#endif
1200
1201 /*
1202 Skip the rest of the variable data until we support it.
1203 */
1204 if (image->debug != MagickFalse)
1205 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001206 " unsupported data: length=%.20g",(double)
cristy2b9582a2011-07-04 17:38:56 +00001207 ((MagickOffsetType) (size-combinedlength)));
cristyc1af14f2010-09-16 20:01:21 +00001208 if (DiscardBlobBytes(image,size-combinedlength) == MagickFalse)
cristyd4297022010-09-16 22:59:09 +00001209 ThrowFileException(exception,CorruptImageError,
1210 "UnexpectedEndOfFile",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001211 }
1212 /*
1213 Allocate layered image.
1214 */
1215 layer_info[i].image=CloneImage(image,layer_info[i].page.width,
cristy390c39f2010-10-05 23:41:44 +00001216 layer_info[i].page.height == ~0U ? 1 : layer_info[i].page.height,
cristyc82a27b2011-10-21 01:07:16 +00001217 MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001218 if (layer_info[i].image == (Image *) NULL)
1219 {
1220 for (j=0; j < i; j++)
1221 layer_info[j].image=DestroyImage(layer_info[j].image);
1222 if (image->debug != MagickFalse)
1223 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001224 " allocation of image for layer %.20g failed",(double) i);
cristy3ed852e2009-09-05 21:47:34 +00001225 ThrowReaderException(ResourceLimitError,
1226 "MemoryAllocationFailed");
1227 }
1228 if (image->debug != MagickFalse)
1229 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1230 " setting up new layer image");
cristy2e2e46f2010-10-12 18:55:20 +00001231 if (image_info->ping != MagickFalse)
cristyea1a8aa2011-10-20 13:24:06 +00001232 (void) SetImageBackgroundColor(layer_info[i].image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001233 layer_info[i].image->compose=
1234 PSDBlendModeToCompositeOperator(layer_info[i].blendkey);
1235 if (layer_info[i].visible == MagickFalse)
1236 layer_info[i].image->compose=NoCompositeOp;
1237 if (psd_info.mode == CMYKMode)
cristye2c4f182012-05-12 14:11:53 +00001238 SetImageColorspace(layer_info[i].image,CMYKColorspace,exception);
cristy0132ba82010-04-16 23:34:02 +00001239 if ((psd_info.mode == BitmapMode) ||
1240 (psd_info.mode == GrayscaleMode) ||
1241 (psd_info.mode == DuotoneMode))
cristye2c4f182012-05-12 14:11:53 +00001242 SetImageColorspace(layer_info[i].image,GRAYColorspace,exception);
cristybb503372010-05-27 20:51:26 +00001243 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
cristy3ed852e2009-09-05 21:47:34 +00001244 if (layer_info[i].channel_info[j].type == -1)
cristy8a46d822012-08-28 23:32:39 +00001245 layer_info[i].image->alpha_trait=BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +00001246 /*
1247 Set up some hidden attributes for folks that need them.
1248 */
cristyb51dff52011-05-19 16:55:47 +00001249 (void) FormatLocaleString(message,MaxTextExtent,"%.20gld",
cristye8c25f92010-06-03 00:53:06 +00001250 (double) layer_info[i].page.x);
cristy56ed31c2010-03-22 00:46:21 +00001251 (void) SetImageArtifact(layer_info[i].image,"psd:layer.x",message);
cristyb51dff52011-05-19 16:55:47 +00001252 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",
cristye8c25f92010-06-03 00:53:06 +00001253 (double) layer_info[i].page.y);
cristy56ed31c2010-03-22 00:46:21 +00001254 (void) SetImageArtifact(layer_info[i].image,"psd:layer.y",message);
cristyb51dff52011-05-19 16:55:47 +00001255 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",
cristye8c25f92010-06-03 00:53:06 +00001256 (double) layer_info[i].opacity);
cristy56ed31c2010-03-22 00:46:21 +00001257 (void) SetImageArtifact(layer_info[i].image,"psd:layer.opacity",
1258 message);
cristy3ed852e2009-09-05 21:47:34 +00001259 (void) SetImageProperty(layer_info[i].image,"label",(char *)
cristyd15e6592011-10-15 00:13:06 +00001260 layer_info[i].name,exception);
cristy3ed852e2009-09-05 21:47:34 +00001261 }
cristy374b8ad2012-02-01 20:55:21 +00001262 if (check_background == MagickFalse)
cristy56ed31c2010-03-22 00:46:21 +00001263 {
cristy374b8ad2012-02-01 20:55:21 +00001264 if (image->debug != MagickFalse)
1265 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1266 " reading image data for layers");
1267 /*
1268 Read pixel data for each layer.
1269 */
1270 for (i=0; i < number_layers; i++)
1271 {
1272 if (image->debug != MagickFalse)
1273 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1274 " reading data for layer %.20g",(double) i);
1275 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1276 {
1277 if (image->debug != MagickFalse)
1278 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1279 " reading data for channel %.20g",(double) j);
1280#if 1
1281 if (layer_info[i].channel_info[j].size <= (2*layer_info[i].image->rows))
1282 {
1283 ssize_t
1284 k;
1285
1286 if (image->debug != MagickFalse)
1287 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1288 " layer data is empty");
1289 /*
1290 A layer without data.
1291 */
1292 for (k=0; k < (ssize_t) layer_info[i].channel_info[j].size; k++)
1293 (void) ReadBlobByte(layer_info[i].image);
1294 continue;
1295 }
1296#endif
1297 offsets=(MagickOffsetType *) NULL;
1298 layer_info[i].image->compression=NoCompression;
1299 compression=ReadBlobMSBShort(layer_info[i].image);
1300 if ((layer_info[i].page.height != 0) &&
1301 (layer_info[i].page.width != 0))
1302 {
1303 if (compression == 1)
1304 {
1305 /*
1306 Read RLE compressed data.
1307 */
1308 layer_info[i].image->compression=RLECompression;
1309 if (image->debug != MagickFalse)
1310 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1311 " layer data is RLE compressed");
1312 offsets=(MagickOffsetType *) AcquireQuantumMemory(
1313 layer_info[i].image->rows,sizeof(*offsets));
1314 if (offsets == (MagickOffsetType *) NULL)
1315 ThrowReaderException(ResourceLimitError,
1316 "MemoryAllocationFailed");
1317 for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
1318 offsets[y]=GetPSDOffset(&psd_info,
1319 layer_info[i].image);
1320 }
1321 status=ReadPSDLayer(layer_info[i].image,
1322 layer_info[i].channels,
1323 layer_info[i].channel_info[j].type,offsets,exception);
1324 if (compression == 1)
1325 offsets=(MagickOffsetType *) RelinquishMagickMemory(
1326 offsets);
1327 if (status == MagickFalse)
1328 break;
1329 }
1330 }
1331 if (layer_info[i].opacity != OpaqueAlpha)
1332 {
1333 /*
1334 Correct for opacity level.
1335 */
1336 for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
1337 {
1338 q=GetAuthenticPixels(layer_info[i].image,0,y,
1339 layer_info[i].image->columns,1,exception);
1340 if (q == (Quantum *) NULL)
1341 break;
1342 for (x=0; x < (ssize_t) layer_info[i].image->columns; x++)
1343 {
1344 SetPixelAlpha(layer_info[i].image,(Quantum)
1345 (QuantumScale*(GetPixelAlpha(layer_info[i].image,q))*
1346 layer_info[i].opacity),q);
1347 q+=GetPixelChannels(layer_info[i].image);
1348 }
1349 if (SyncAuthenticPixels(layer_info[i].image,exception) == MagickFalse)
1350 break;
1351 }
1352 }
1353 if (layer_info[i].image->colorspace == CMYKColorspace)
1354 (void) NegateImage(layer_info[i].image,MagickFalse,exception);
1355 status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType)
1356 number_layers);
1357 if (status == MagickFalse)
1358 break;
1359 }
1360 /* added by palf -> invisible group layer make layer of this group
1361 invisible I consider that all layer with width and height null are
1362 layer for group layer */
1363 {
1364 short inside_layer = 0;
1365 short layer_visible = 0;
1366 for (i=number_layers-1; i >=0; i--)
cristy3ed852e2009-09-05 21:47:34 +00001367 {
cristy374b8ad2012-02-01 20:55:21 +00001368 if ((layer_info[i].page.width == 0) ||
1369 (layer_info[i].page.height == 0))
1370 {
1371 if (inside_layer == 0)
1372 {
1373 inside_layer=1;
1374 layer_visible=(short int) layer_info[i].visible;
1375 }
1376 else
1377 {
1378 inside_layer = 0;
1379 }
1380 }
1381 else
1382 if ((inside_layer == 1) && (layer_visible == 0))
1383 {
1384 layer_info[i].visible=(unsigned char) layer_visible;
1385 layer_info[i].image->compose=NoCompositeOp;
1386 }
cristy56ed31c2010-03-22 00:46:21 +00001387 }
cristy56ed31c2010-03-22 00:46:21 +00001388 }
cristy374b8ad2012-02-01 20:55:21 +00001389 /* added by palf -> suppression of empty layer */
1390 /* I consider that all layer with width and height null are layer for group layer */
1391 for (i=0; i < number_layers; i++)
1392 {
1393 if ((layer_info[i].page.width == 0) ||
1394 (layer_info[i].page.height == 0))
1395 {
1396 if (layer_info[i].image != (Image *) NULL)
1397 layer_info[i].image=DestroyImage(layer_info[i].image);
1398 for (j=i; j < number_layers - 1; j++)
1399 layer_info[j] = layer_info[j+1];
1400 number_layers--;
1401 i--;
1402 }
1403 }
1404 mask_size = ReadBlobMSBLong(image); /* global mask size: currently ignored */
1405 (void) mask_size;
1406 if (number_layers > 0)
1407 {
1408 if (image->debug != MagickFalse)
1409 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1410 " putting layers into image list");
1411 for (i=0; i < number_layers; i++)
1412 {
1413 if (i > 0)
1414 layer_info[i].image->previous=layer_info[i-1].image;
1415 if (i < (number_layers-1))
1416 layer_info[i].image->next=layer_info[i+1].image;
1417 layer_info[i].image->page=layer_info[i].page;
1418 }
1419 image->next=layer_info[0].image;
1420 layer_info[0].image->previous=image;
1421 layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info);
1422 }
1423 layer_offset-=TellBlob(image);
1424 offset=SeekBlob(image,layer_offset,SEEK_CUR);
1425 }
cristy56ed31c2010-03-22 00:46:21 +00001426 }
cristy3ed852e2009-09-05 21:47:34 +00001427 }
1428 /*
1429 Read the precombined layer, present for PSD < 4 compatibility
1430 */
1431 if (image->debug != MagickFalse)
1432 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1433 " reading the precombined layer");
cristy56ed31c2010-03-22 00:46:21 +00001434 offsets=(MagickOffsetType *) NULL;
1435 image->compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00001436 compression=ReadBlobMSBShort(image);
1437 if (compression == 1)
1438 {
1439 /*
cristy3f938c62010-03-07 00:53:52 +00001440 Read Packbit encoded pixel data as separate planes.
cristy3ed852e2009-09-05 21:47:34 +00001441 */
cristy56ed31c2010-03-22 00:46:21 +00001442 image->compression=RLECompression;
1443 offsets=(MagickOffsetType *) AcquireQuantumMemory(image->rows,
1444 psd_info.channels*sizeof(*offsets));
1445 if (offsets == (MagickOffsetType *) NULL)
1446 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00001447 for (i=0; i < (ssize_t) (image->rows*psd_info.channels); i++)
cristy56ed31c2010-03-22 00:46:21 +00001448 offsets[i]=GetPSDOffset(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +00001449 }
cristybb503372010-05-27 20:51:26 +00001450 for (i=0; i < (ssize_t) psd_info.channels; i++)
cristy56ed31c2010-03-22 00:46:21 +00001451 {
cristye3038982010-05-17 02:20:52 +00001452 status=ReadPSDLayer(image,psd_info.channels,i,offsets+i*image->rows,
1453 exception);
cristy56ed31c2010-03-22 00:46:21 +00001454 if (status == MagickFalse)
1455 break;
1456 status=SetImageProgress(image,LoadImagesTag,i,psd_info.channels);
1457 if (status == MagickFalse)
1458 break;
1459 }
1460 if (compression == 1)
1461 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
cristy3ed852e2009-09-05 21:47:34 +00001462 if (image->colorspace == CMYKColorspace)
cristyb3e7c6c2011-07-24 01:43:55 +00001463 (void) NegateImage(image,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001464 (void) CloseBlob(image);
1465 return(GetFirstImageInList(image));
1466}
1467
1468/*
1469%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1470% %
1471% %
1472% %
1473% R e g i s t e r P S D I m a g e %
1474% %
1475% %
1476% %
1477%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1478%
1479% RegisterPSDImage() adds properties for the PSD image format to
1480% the list of supported formats. The properties include the image format
1481% tag, a method to read and/or write the format, whether the format
1482% supports the saving of more than one frame to the same file or blob,
1483% whether the format supports native in-memory I/O, and a brief
1484% description of the format.
1485%
1486% The format of the RegisterPSDImage method is:
1487%
cristybb503372010-05-27 20:51:26 +00001488% size_t RegisterPSDImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001489%
1490*/
cristybb503372010-05-27 20:51:26 +00001491ModuleExport size_t RegisterPSDImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001492{
1493 MagickInfo
1494 *entry;
1495
cristyb4233012010-02-28 20:09:14 +00001496 entry=SetMagickInfo("PSB");
1497 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1498 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1499 entry->magick=(IsImageFormatHandler *) IsPSD;
cristyffaf9782011-04-13 19:50:51 +00001500 entry->seekable_stream=MagickTrue;
cristyb4233012010-02-28 20:09:14 +00001501 entry->description=ConstantString("Adobe Large Document Format");
1502 entry->module=ConstantString("PSD");
1503 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001504 entry=SetMagickInfo("PSD");
1505 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1506 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1507 entry->magick=(IsImageFormatHandler *) IsPSD;
cristyffaf9782011-04-13 19:50:51 +00001508 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001509 entry->description=ConstantString("Adobe Photoshop bitmap");
1510 entry->module=ConstantString("PSD");
1511 (void) RegisterMagickInfo(entry);
1512 return(MagickImageCoderSignature);
1513}
1514
1515/*
1516%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1517% %
1518% %
1519% %
1520% U n r e g i s t e r P S D I m a g e %
1521% %
1522% %
1523% %
1524%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1525%
1526% UnregisterPSDImage() removes format registrations made by the
1527% PSD module from the list of supported formats.
1528%
1529% The format of the UnregisterPSDImage method is:
1530%
1531% UnregisterPSDImage(void)
1532%
1533*/
1534ModuleExport void UnregisterPSDImage(void)
1535{
cristyb4233012010-02-28 20:09:14 +00001536 (void) UnregisterMagickInfo("PSB");
cristy3ed852e2009-09-05 21:47:34 +00001537 (void) UnregisterMagickInfo("PSD");
1538}
1539
1540/*
1541%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1542% %
1543% %
1544% %
1545% W r i t e P S D I m a g e %
1546% %
1547% %
1548% %
1549%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1550%
cristyb1459bc2010-03-23 21:41:43 +00001551% WritePSDImage() writes an image in the Adobe Photoshop encoded image format.
cristy3ed852e2009-09-05 21:47:34 +00001552%
1553% The format of the WritePSDImage method is:
1554%
cristy3a37efd2011-08-28 20:31:03 +00001555% MagickBooleanType WritePSDImage(const ImageInfo *image_info,
1556% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001557%
1558% A description of each parameter follows.
1559%
1560% o image_info: the image info.
1561%
1562% o image: The image.
1563%
cristy3a37efd2011-08-28 20:31:03 +00001564% o exception: return any errors or warnings in this structure.
1565%
cristy3ed852e2009-09-05 21:47:34 +00001566*/
1567
cristy50aea4a2010-03-09 17:37:44 +00001568static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image,
cristyf0460ee2010-03-06 02:55:11 +00001569 const size_t offset)
1570{
1571 if (psd_info->version == 1)
cristy56ed31c2010-03-22 00:46:21 +00001572 return(WriteBlobMSBShort(image,(unsigned short) offset));
1573 return(WriteBlobMSBLong(image,(unsigned short) offset));
cristyf0460ee2010-03-06 02:55:11 +00001574}
1575
cristy50aea4a2010-03-09 17:37:44 +00001576static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image,
cristyf0460ee2010-03-06 02:55:11 +00001577 const MagickSizeType size)
1578{
1579 if (psd_info->version == 1)
cristy56ed31c2010-03-22 00:46:21 +00001580 return(WriteBlobMSBLong(image,(unsigned int) size));
cristyf0460ee2010-03-06 02:55:11 +00001581 return(WriteBlobMSBLongLong(image,size));
1582}
1583
cristy4aff1572010-02-15 13:34:01 +00001584static size_t PSDPackbitsEncodeImage(Image *image,const size_t length,
cristy018f07f2011-09-04 21:15:19 +00001585 const unsigned char *pixels,unsigned char *compact_pixels,
1586 ExceptionInfo *exception)
cristy4aff1572010-02-15 13:34:01 +00001587{
1588 int
1589 count;
1590
cristybb503372010-05-27 20:51:26 +00001591 register ssize_t
cristy4aff1572010-02-15 13:34:01 +00001592 i,
1593 j;
1594
1595 register unsigned char
1596 *q;
1597
1598 unsigned char
1599 *packbits;
1600
1601 /*
1602 Compress pixels with Packbits encoding.
1603 */
1604 assert(image != (Image *) NULL);
1605 assert(image->signature == MagickSignature);
1606 if (image->debug != MagickFalse)
1607 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1608 assert(pixels != (unsigned char *) NULL);
1609 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1610 if (packbits == (unsigned char *) NULL)
1611 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1612 image->filename);
cristyb1459bc2010-03-23 21:41:43 +00001613 q=compact_pixels;
cristybb503372010-05-27 20:51:26 +00001614 for (i=(ssize_t) length; i != 0; )
cristy4aff1572010-02-15 13:34:01 +00001615 {
1616 switch (i)
1617 {
1618 case 1:
1619 {
1620 i--;
1621 *q++=(unsigned char) 0;
1622 *q++=(*pixels);
1623 break;
1624 }
1625 case 2:
1626 {
1627 i-=2;
1628 *q++=(unsigned char) 1;
1629 *q++=(*pixels);
1630 *q++=pixels[1];
1631 break;
1632 }
1633 case 3:
1634 {
1635 i-=3;
1636 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1637 {
1638 *q++=(unsigned char) ((256-3)+1);
1639 *q++=(*pixels);
1640 break;
1641 }
1642 *q++=(unsigned char) 2;
1643 *q++=(*pixels);
1644 *q++=pixels[1];
1645 *q++=pixels[2];
1646 break;
1647 }
1648 default:
1649 {
1650 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1651 {
1652 /*
1653 Packed run.
1654 */
1655 count=3;
cristybb503372010-05-27 20:51:26 +00001656 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
cristy4aff1572010-02-15 13:34:01 +00001657 {
1658 count++;
1659 if (count >= 127)
1660 break;
1661 }
1662 i-=count;
1663 *q++=(unsigned char) ((256-count)+1);
1664 *q++=(*pixels);
1665 pixels+=count;
1666 break;
1667 }
1668 /*
1669 Literal run.
1670 */
1671 count=0;
1672 while ((*(pixels+count) != *(pixels+count+1)) ||
1673 (*(pixels+count+1) != *(pixels+count+2)))
1674 {
1675 packbits[count+1]=pixels[count];
1676 count++;
cristybb503372010-05-27 20:51:26 +00001677 if (((ssize_t) count >= (i-3)) || (count >= 127))
cristy4aff1572010-02-15 13:34:01 +00001678 break;
1679 }
1680 i-=count;
1681 *packbits=(unsigned char) (count-1);
cristybb503372010-05-27 20:51:26 +00001682 for (j=0; j <= (ssize_t) count; j++)
cristy4aff1572010-02-15 13:34:01 +00001683 *q++=packbits[j];
1684 pixels+=count;
1685 break;
1686 }
1687 }
1688 }
1689 *q++=(unsigned char) 128; /* EOD marker */
1690 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
cristyb1459bc2010-03-23 21:41:43 +00001691 return((size_t) (q-compact_pixels));
cristy4aff1572010-02-15 13:34:01 +00001692}
1693
cristy875e28a2010-03-06 19:46:55 +00001694static void WritePackbitsLength(const PSDInfo *psd_info,
cristya20214e2010-08-28 16:52:13 +00001695 const ImageInfo *image_info,Image *image,Image *next_image,
cristy018f07f2011-09-04 21:15:19 +00001696 unsigned char *compact_pixels,const QuantumType quantum_type,
1697 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001698{
cristy3ed852e2009-09-05 21:47:34 +00001699 QuantumInfo
1700 *quantum_info;
1701
cristy4c08aed2011-07-01 19:47:50 +00001702 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001703 *p;
1704
1705 size_t
cristy4aff1572010-02-15 13:34:01 +00001706 length,
cristy3ed852e2009-09-05 21:47:34 +00001707 packet_size;
1708
cristy75f85ae2010-09-25 03:01:06 +00001709 ssize_t
1710 y;
1711
cristya20214e2010-08-28 16:52:13 +00001712 unsigned char
1713 *pixels;
1714
1715 if (next_image->depth > 8)
1716 next_image->depth=16;
1717 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
cristyda16f162011-02-19 23:52:17 +00001718 (void) packet_size;
cristy4aff1572010-02-15 13:34:01 +00001719 quantum_info=AcquireQuantumInfo(image_info,image);
cristya20214e2010-08-28 16:52:13 +00001720 pixels=GetQuantumPixels(quantum_info);
1721 for (y=0; y < (ssize_t) next_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001722 {
cristyc82a27b2011-10-21 01:07:16 +00001723 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001724 if (p == (const Quantum *) NULL)
cristy4aff1572010-02-15 13:34:01 +00001725 break;
cristya20214e2010-08-28 16:52:13 +00001726 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
cristyc82a27b2011-10-21 01:07:16 +00001727 quantum_type,pixels,exception);
cristy018f07f2011-09-04 21:15:19 +00001728 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
1729 exception);
cristy875e28a2010-03-06 19:46:55 +00001730 (void) SetPSDOffset(psd_info,image,length);
cristy4aff1572010-02-15 13:34:01 +00001731 }
1732 quantum_info=DestroyQuantumInfo(quantum_info);
1733}
1734
cristy875e28a2010-03-06 19:46:55 +00001735static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info,
cristya20214e2010-08-28 16:52:13 +00001736 Image *image,Image *next_image,unsigned char *compact_pixels,
cristy018f07f2011-09-04 21:15:19 +00001737 const QuantumType quantum_type,const MagickBooleanType compression_flag,
1738 ExceptionInfo *exception)
cristy4aff1572010-02-15 13:34:01 +00001739{
1740 int
1741 y;
1742
cristy0910f242010-04-01 18:55:09 +00001743 MagickBooleanType
1744 monochrome;
1745
cristy4aff1572010-02-15 13:34:01 +00001746 QuantumInfo
1747 *quantum_info;
1748
cristy4c08aed2011-07-01 19:47:50 +00001749 register const Quantum
cristy4aff1572010-02-15 13:34:01 +00001750 *p;
1751
cristybb503372010-05-27 20:51:26 +00001752 register ssize_t
cristy0910f242010-04-01 18:55:09 +00001753 i;
1754
cristy4aff1572010-02-15 13:34:01 +00001755 size_t
1756 length,
1757 packet_size;
1758
cristya20214e2010-08-28 16:52:13 +00001759 unsigned char
1760 *pixels;
1761
cristy50aea4a2010-03-09 17:37:44 +00001762 (void) psd_info;
cristy4aff1572010-02-15 13:34:01 +00001763 if ((compression_flag != MagickFalse) &&
cristya20214e2010-08-28 16:52:13 +00001764 (next_image->compression != RLECompression))
cristy4aff1572010-02-15 13:34:01 +00001765 (void) WriteBlobMSBShort(image,0);
cristya20214e2010-08-28 16:52:13 +00001766 if (next_image->depth > 8)
1767 next_image->depth=16;
cristyc82a27b2011-10-21 01:07:16 +00001768 monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
1769 MagickTrue : MagickFalse;
cristya20214e2010-08-28 16:52:13 +00001770 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
cristyda16f162011-02-19 23:52:17 +00001771 (void) packet_size;
cristy4aff1572010-02-15 13:34:01 +00001772 quantum_info=AcquireQuantumInfo(image_info,image);
cristya20214e2010-08-28 16:52:13 +00001773 pixels=GetQuantumPixels(quantum_info);
1774 for (y=0; y < (ssize_t) next_image->rows; y++)
cristy4aff1572010-02-15 13:34:01 +00001775 {
cristyc82a27b2011-10-21 01:07:16 +00001776 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001777 if (p == (const Quantum *) NULL)
cristy4aff1572010-02-15 13:34:01 +00001778 break;
cristya20214e2010-08-28 16:52:13 +00001779 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
cristyc82a27b2011-10-21 01:07:16 +00001780 quantum_type,pixels,exception);
cristy0910f242010-04-01 18:55:09 +00001781 if (monochrome != MagickFalse)
cristybb503372010-05-27 20:51:26 +00001782 for (i=0; i < (ssize_t) length; i++)
cristy0910f242010-04-01 18:55:09 +00001783 pixels[i]=(~pixels[i]);
cristya20214e2010-08-28 16:52:13 +00001784 if (next_image->compression != RLECompression)
cristy4aff1572010-02-15 13:34:01 +00001785 (void) WriteBlob(image,length,pixels);
1786 else
1787 {
cristy018f07f2011-09-04 21:15:19 +00001788 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
1789 exception);
cristyb1459bc2010-03-23 21:41:43 +00001790 (void) WriteBlob(image,length,compact_pixels);
cristy4aff1572010-02-15 13:34:01 +00001791 }
cristy3ed852e2009-09-05 21:47:34 +00001792 }
1793 quantum_info=DestroyQuantumInfo(quantum_info);
1794}
1795
cristy875e28a2010-03-06 19:46:55 +00001796static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info,
cristya20214e2010-08-28 16:52:13 +00001797 const ImageInfo *image_info,Image *image,Image *next_image,
cristy018f07f2011-09-04 21:15:19 +00001798 const MagickBooleanType separate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001799{
1800 int
1801 i;
1802
1803 size_t
1804 channels,
1805 packet_size;
1806
1807 unsigned char
cristya20214e2010-08-28 16:52:13 +00001808 *compact_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001809
1810 /*
cristy875e28a2010-03-06 19:46:55 +00001811 Write uncompressed pixels as separate planes.
cristy3ed852e2009-09-05 21:47:34 +00001812 */
1813 channels=1;
cristya20214e2010-08-28 16:52:13 +00001814 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1815 compact_pixels=(unsigned char *) NULL;
1816 if (next_image->compression == RLECompression)
cristy4aff1572010-02-15 13:34:01 +00001817 {
cristya20214e2010-08-28 16:52:13 +00001818 compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels*
1819 next_image->columns,packet_size*sizeof(*compact_pixels));
1820 if (compact_pixels == (unsigned char *) NULL)
1821 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy4aff1572010-02-15 13:34:01 +00001822 }
cristy3ed852e2009-09-05 21:47:34 +00001823 i=0;
cristyc82a27b2011-10-21 01:07:16 +00001824 if (IsImageGray(next_image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001825 {
cristya20214e2010-08-28 16:52:13 +00001826 if (next_image->compression == RLECompression)
cristy4aff1572010-02-15 13:34:01 +00001827 {
1828 /*
1829 Packbits compression.
1830 */
1831 (void) WriteBlobMSBShort(image,1);
cristya20214e2010-08-28 16:52:13 +00001832 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00001833 compact_pixels,GrayQuantum,exception);
cristy8a46d822012-08-28 23:32:39 +00001834 if (next_image->alpha_trait == BlendPixelTrait)
cristya20214e2010-08-28 16:52:13 +00001835 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00001836 compact_pixels,AlphaQuantum,exception);
cristy4aff1572010-02-15 13:34:01 +00001837 }
cristya20214e2010-08-28 16:52:13 +00001838 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1839 GrayQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00001840 MagickFalse,exception);
cristy8a46d822012-08-28 23:32:39 +00001841 if (next_image->alpha_trait == BlendPixelTrait)
cristya20214e2010-08-28 16:52:13 +00001842 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1843 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00001844 MagickFalse,exception);
cristy6886a752010-04-23 18:23:20 +00001845 (void) SetImageProgress(image,SaveImagesTag,0,1);
cristy3ed852e2009-09-05 21:47:34 +00001846 }
cristy0910f242010-04-01 18:55:09 +00001847 else
cristya20214e2010-08-28 16:52:13 +00001848 if (next_image->storage_class == PseudoClass)
cristy0910f242010-04-01 18:55:09 +00001849 {
cristya20214e2010-08-28 16:52:13 +00001850 if (next_image->compression == RLECompression)
cristy0910f242010-04-01 18:55:09 +00001851 {
1852 /*
1853 Packbits compression.
1854 */
1855 (void) WriteBlobMSBShort(image,1);
cristya20214e2010-08-28 16:52:13 +00001856 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00001857 compact_pixels,IndexQuantum,exception);
cristy8a46d822012-08-28 23:32:39 +00001858 if (next_image->alpha_trait == BlendPixelTrait)
cristya20214e2010-08-28 16:52:13 +00001859 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00001860 compact_pixels,AlphaQuantum,exception);
cristy0910f242010-04-01 18:55:09 +00001861 }
cristya20214e2010-08-28 16:52:13 +00001862 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1863 IndexQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00001864 MagickFalse,exception);
cristy8a46d822012-08-28 23:32:39 +00001865 if (next_image->alpha_trait == BlendPixelTrait)
cristya20214e2010-08-28 16:52:13 +00001866 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1867 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00001868 MagickFalse,exception);
cristy0910f242010-04-01 18:55:09 +00001869 (void) SetImageProgress(image,SaveImagesTag,0,1);
1870 }
1871 else
1872 {
cristya20214e2010-08-28 16:52:13 +00001873 if (next_image->colorspace == CMYKColorspace)
cristyc82a27b2011-10-21 01:07:16 +00001874 (void) NegateImage(next_image,MagickFalse,exception);
cristya20214e2010-08-28 16:52:13 +00001875 if (next_image->compression == RLECompression)
cristy0910f242010-04-01 18:55:09 +00001876 {
1877 /*
1878 Packbits compression.
1879 */
1880 (void) WriteBlobMSBShort(image,1);
cristya20214e2010-08-28 16:52:13 +00001881 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00001882 compact_pixels,RedQuantum,exception);
cristya20214e2010-08-28 16:52:13 +00001883 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00001884 compact_pixels,GreenQuantum,exception);
cristya20214e2010-08-28 16:52:13 +00001885 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00001886 compact_pixels,BlueQuantum,exception);
cristya20214e2010-08-28 16:52:13 +00001887 if (next_image->colorspace == CMYKColorspace)
1888 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00001889 compact_pixels,BlackQuantum,exception);
cristy8a46d822012-08-28 23:32:39 +00001890 if (next_image->alpha_trait == BlendPixelTrait)
cristya20214e2010-08-28 16:52:13 +00001891 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00001892 compact_pixels,AlphaQuantum,exception);
cristy0910f242010-04-01 18:55:09 +00001893 }
1894 (void) SetImageProgress(image,SaveImagesTag,0,6);
cristya20214e2010-08-28 16:52:13 +00001895 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1896 RedQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00001897 MagickFalse,exception);
cristy860cc732010-05-19 16:38:37 +00001898 (void) SetImageProgress(image,SaveImagesTag,1,6);
cristya20214e2010-08-28 16:52:13 +00001899 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1900 GreenQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00001901 MagickFalse,exception);
cristy860cc732010-05-19 16:38:37 +00001902 (void) SetImageProgress(image,SaveImagesTag,2,6);
cristya20214e2010-08-28 16:52:13 +00001903 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1904 BlueQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00001905 MagickFalse,exception);
cristy860cc732010-05-19 16:38:37 +00001906 (void) SetImageProgress(image,SaveImagesTag,3,6);
cristya20214e2010-08-28 16:52:13 +00001907 if (next_image->colorspace == CMYKColorspace)
1908 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1909 BlackQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00001910 MagickFalse,exception);
cristy860cc732010-05-19 16:38:37 +00001911 (void) SetImageProgress(image,SaveImagesTag,4,6);
cristy8a46d822012-08-28 23:32:39 +00001912 if (next_image->alpha_trait == BlendPixelTrait)
cristya20214e2010-08-28 16:52:13 +00001913 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1914 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
cristy018f07f2011-09-04 21:15:19 +00001915 MagickFalse,exception);
cristy0910f242010-04-01 18:55:09 +00001916 (void) SetImageProgress(image,SaveImagesTag,5,6);
cristya20214e2010-08-28 16:52:13 +00001917 if (next_image->colorspace == CMYKColorspace)
cristyc82a27b2011-10-21 01:07:16 +00001918 (void) NegateImage(next_image,MagickFalse,exception);
cristy0910f242010-04-01 18:55:09 +00001919 }
cristya20214e2010-08-28 16:52:13 +00001920 if (next_image->compression == RLECompression)
1921 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001922 return(MagickTrue);
1923}
1924
cristy3ed852e2009-09-05 21:47:34 +00001925static void WritePascalString(Image* inImage,const char *inString,int inPad)
1926{
1927 size_t
cristya20214e2010-08-28 16:52:13 +00001928 length;
cristy3ed852e2009-09-05 21:47:34 +00001929
cristya20214e2010-08-28 16:52:13 +00001930 register ssize_t
1931 i;
cristy3ed852e2009-09-05 21:47:34 +00001932
cristya20214e2010-08-28 16:52:13 +00001933 /*
1934 Max length is 255.
1935 */
1936 length=(strlen(inString) > 255UL ) ? 255UL : strlen(inString);
1937 if (length == 0)
1938 (void) WriteBlobByte(inImage,0);
cristy3ed852e2009-09-05 21:47:34 +00001939 else
cristya20214e2010-08-28 16:52:13 +00001940 {
1941 (void) WriteBlobByte(inImage,(unsigned char) length);
1942 (void) WriteBlob(inImage, length, (const unsigned char *) inString);
1943 }
1944 length++;
1945 if ((length % inPad) == 0)
cristy3ed852e2009-09-05 21:47:34 +00001946 return;
cristya20214e2010-08-28 16:52:13 +00001947 for (i=0; i < (ssize_t) (inPad-(length % inPad)); i++)
cristy3ed852e2009-09-05 21:47:34 +00001948 (void) WriteBlobByte(inImage,0);
1949}
1950
1951static void WriteResolutionResourceBlock(Image *image)
1952{
cristy56ed31c2010-03-22 00:46:21 +00001953 double
1954 x_resolution,
1955 y_resolution;
cristy3ed852e2009-09-05 21:47:34 +00001956
1957 unsigned short
1958 units;
1959
cristy2a11bef2011-10-28 18:33:11 +00001960 x_resolution=65536.0*image->resolution.x+0.5;
1961 y_resolution=65536.0*image->resolution.y+0.5;
cristy3ed852e2009-09-05 21:47:34 +00001962 units=1;
1963 if (image->units == PixelsPerCentimeterResolution)
1964 {
cristy2a11bef2011-10-28 18:33:11 +00001965 x_resolution=2.54*65536.0*image->resolution.x*0.5;
1966 y_resolution=2.54*65536.0*image->resolution.y+0.5;
cristy3ed852e2009-09-05 21:47:34 +00001967 units=2;
1968 }
1969 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
1970 (void) WriteBlobMSBShort(image,0x03ED);
1971 (void) WriteBlobMSBShort(image,0);
1972 (void) WriteBlobMSBLong(image,16); /* resource size */
cristy56ed31c2010-03-22 00:46:21 +00001973 (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001974 (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */
1975 (void) WriteBlobMSBShort(image,units); /* width unit */
cristy56ed31c2010-03-22 00:46:21 +00001976 (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001977 (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */
1978 (void) WriteBlobMSBShort(image,units); /* height unit */
1979}
1980
cristyd4d3f742010-04-25 20:36:50 +00001981static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile)
1982{
cristy0b796e62010-04-29 00:38:45 +00001983 register const unsigned char
cristyd4d3f742010-04-25 20:36:50 +00001984 *p;
1985
1986 size_t
1987 length;
1988
1989 unsigned char
1990 *datum;
1991
cristy6befb0f2010-05-31 14:33:15 +00001992 unsigned int
cristyd4d3f742010-04-25 20:36:50 +00001993 count,
cristy6befb0f2010-05-31 14:33:15 +00001994 long_sans;
cristyd4d3f742010-04-25 20:36:50 +00001995
1996 unsigned short
1997 id,
1998 short_sans;
1999
2000 length=GetStringInfoLength(bim_profile);
2001 if (length < 16)
cristy0b796e62010-04-29 00:38:45 +00002002 return;
cristyd4d3f742010-04-25 20:36:50 +00002003 datum=GetStringInfoDatum(bim_profile);
2004 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2005 {
cristy4176bb22010-05-01 16:29:09 +00002006 register unsigned char
2007 *q;
2008
2009 q=(unsigned char *) p;
cristyd4d3f742010-04-25 20:36:50 +00002010 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2011 break;
cristy6befb0f2010-05-31 14:33:15 +00002012 p=PushLongPixel(MSBEndian,p,&long_sans);
cristyd4d3f742010-04-25 20:36:50 +00002013 p=PushShortPixel(MSBEndian,p,&id);
2014 p=PushShortPixel(MSBEndian,p,&short_sans);
2015 p=PushLongPixel(MSBEndian,p,&count);
2016 if (id == 0x0000040f)
2017 {
cristy4176bb22010-05-01 16:29:09 +00002018 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2019 (PSDQuantum(count)+12)-(q-datum));
2020 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
cristyd4d3f742010-04-25 20:36:50 +00002021 break;
2022 }
2023 p+=count;
2024 if ((count & 0x01) != 0)
2025 p++;
2026 }
2027}
2028
cristyf11065e2010-05-14 13:26:59 +00002029static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile)
2030{
2031 register const unsigned char
2032 *p;
2033
2034 size_t
2035 length;
2036
2037 unsigned char
2038 *datum;
2039
cristy6befb0f2010-05-31 14:33:15 +00002040 unsigned int
cristyf11065e2010-05-14 13:26:59 +00002041 count,
cristy6befb0f2010-05-31 14:33:15 +00002042 long_sans;
cristyf11065e2010-05-14 13:26:59 +00002043
2044 unsigned short
2045 id,
2046 short_sans;
2047
2048 length=GetStringInfoLength(bim_profile);
2049 if (length < 16)
2050 return;
2051 datum=GetStringInfoDatum(bim_profile);
2052 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2053 {
2054 register unsigned char
2055 *q;
2056
2057 q=(unsigned char *) p;
2058 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2059 break;
cristy6befb0f2010-05-31 14:33:15 +00002060 p=PushLongPixel(MSBEndian,p,&long_sans);
cristyf11065e2010-05-14 13:26:59 +00002061 p=PushShortPixel(MSBEndian,p,&id);
2062 p=PushShortPixel(MSBEndian,p,&short_sans);
2063 p=PushLongPixel(MSBEndian,p,&count);
cristy94b11832011-09-08 19:46:03 +00002064 if ((id == 0x000003ed) && (PSDQuantum(count) < (ssize_t) (length-12)))
cristyf11065e2010-05-14 13:26:59 +00002065 {
2066 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2067 (PSDQuantum(count)+12)-(q-datum));
2068 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2069 break;
2070 }
2071 p+=count;
2072 if ((count & 0x01) != 0)
2073 p++;
2074 }
2075}
2076
cristy3a37efd2011-08-28 20:31:03 +00002077static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
2078 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002079{
2080 const char
cristya20214e2010-08-28 16:52:13 +00002081 *property;
cristy3ed852e2009-09-05 21:47:34 +00002082
2083 const StringInfo
cristy749d2152010-04-04 23:47:33 +00002084 *icc_profile;
cristy3ed852e2009-09-05 21:47:34 +00002085
cristya20214e2010-08-28 16:52:13 +00002086 Image
2087 *base_image,
2088 *next_image;
2089
cristy3ed852e2009-09-05 21:47:34 +00002090 MagickBooleanType
cristy3ed852e2009-09-05 21:47:34 +00002091 status;
2092
cristy875e28a2010-03-06 19:46:55 +00002093 PSDInfo
2094 psd_info;
2095
cristybb503372010-05-27 20:51:26 +00002096 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002097 i;
2098
2099 size_t
cristya20214e2010-08-28 16:52:13 +00002100 channel_size,
2101 channelLength,
2102 layer_count,
2103 layer_info_size,
cristy749d2152010-04-04 23:47:33 +00002104 length,
cristy3ed852e2009-09-05 21:47:34 +00002105 num_channels,
cristya20214e2010-08-28 16:52:13 +00002106 packet_size,
2107 rounded_layer_info_size;
cristy3ed852e2009-09-05 21:47:34 +00002108
cristy0b796e62010-04-29 00:38:45 +00002109 StringInfo
2110 *bim_profile;
2111
cristy3ed852e2009-09-05 21:47:34 +00002112 unsigned char
2113 layer_name[4];
2114
cristy3ed852e2009-09-05 21:47:34 +00002115 /*
cristy56ed31c2010-03-22 00:46:21 +00002116 Open image file.
cristy3ed852e2009-09-05 21:47:34 +00002117 */
2118 assert(image_info != (const ImageInfo *) NULL);
2119 assert(image_info->signature == MagickSignature);
2120 assert(image != (Image *) NULL);
2121 assert(image->signature == MagickSignature);
2122 if (image->debug != MagickFalse)
2123 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00002124 assert(exception != (ExceptionInfo *) NULL);
2125 assert(exception->signature == MagickSignature);
2126 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00002127 if (status == MagickFalse)
2128 return(status);
2129 packet_size=(size_t) (image->depth > 8 ? 6 : 3);
cristy8a46d822012-08-28 23:32:39 +00002130 if (image->alpha_trait == BlendPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00002131 packet_size+=image->depth > 8 ? 2 : 1;
cristy875e28a2010-03-06 19:46:55 +00002132 psd_info.version=1;
2133 if ((LocaleCompare(image_info->magick,"PSB") == 0) ||
2134 (image->columns > 30000) || (image->rows > 30000))
2135 psd_info.version=2;
cristy50aea4a2010-03-09 17:37:44 +00002136 (void) WriteBlob(image,4,(const unsigned char *) "8BPS");
cristy875e28a2010-03-06 19:46:55 +00002137 (void) WriteBlobMSBShort(image,psd_info.version); /* version */
2138 for (i=1; i <= 6; i++)
2139 (void) WriteBlobByte(image, 0); /* 6 bytes of reserved */
cristy3a37efd2011-08-28 20:31:03 +00002140 if (IsImageGray(image,exception) != MagickFalse)
cristy8a46d822012-08-28 23:32:39 +00002141 num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
cristy3ed852e2009-09-05 21:47:34 +00002142 else
cristy0910f242010-04-01 18:55:09 +00002143 if (image->storage_class == PseudoClass)
cristy8a46d822012-08-28 23:32:39 +00002144 num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
cristy0910f242010-04-01 18:55:09 +00002145 else
2146 {
2147 if (image->colorspace != CMYKColorspace)
cristy8a46d822012-08-28 23:32:39 +00002148 num_channels=(image->alpha_trait == BlendPixelTrait ? 4UL : 3UL);
cristy0910f242010-04-01 18:55:09 +00002149 else
cristy8a46d822012-08-28 23:32:39 +00002150 num_channels=(image->alpha_trait == BlendPixelTrait ? 5UL : 4UL);
cristy0910f242010-04-01 18:55:09 +00002151 }
cristy3ed852e2009-09-05 21:47:34 +00002152 (void) WriteBlobMSBShort(image,(unsigned short) num_channels);
cristy56ed31c2010-03-22 00:46:21 +00002153 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
2154 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
cristy3a37efd2011-08-28 20:31:03 +00002155 if (IsImageGray(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002156 {
cristy2045da32010-04-16 00:59:35 +00002157 MagickBooleanType
2158 monochrome;
2159
cristy0910f242010-04-01 18:55:09 +00002160 /*
2161 Write depth & mode.
2162 */
cristy3a37efd2011-08-28 20:31:03 +00002163 monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
2164 MagickTrue : MagickFalse;
cristy284c7d82010-04-24 00:19:14 +00002165 (void) WriteBlobMSBShort(image,(unsigned short)
2166 (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8));
cristy2b9582a2011-07-04 17:38:56 +00002167 (void) WriteBlobMSBShort(image,(unsigned short)
2168 (monochrome != MagickFalse ? BitmapMode : GrayscaleMode));
cristy3ed852e2009-09-05 21:47:34 +00002169 }
2170 else
2171 {
cristya20214e2010-08-28 16:52:13 +00002172 (void) WriteBlobMSBShort(image,(unsigned short) (image->storage_class ==
2173 PseudoClass ? 8 : image->depth > 8 ? 16 : 8));
cristy48845392010-06-02 01:19:17 +00002174 if (((image_info->colorspace != UndefinedColorspace) ||
cristy0910f242010-04-01 18:55:09 +00002175 (image->colorspace != CMYKColorspace)) &&
cristy48845392010-06-02 01:19:17 +00002176 (image_info->colorspace != CMYKColorspace))
cristy0910f242010-04-01 18:55:09 +00002177 {
cristy3d9f5ba2012-06-26 13:37:31 +00002178 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
cristy8d951092012-02-08 18:54:56 +00002179 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy0910f242010-04-01 18:55:09 +00002180 (void) WriteBlobMSBShort(image,(unsigned short)
cristy2045da32010-04-16 00:59:35 +00002181 (image->storage_class == PseudoClass ? IndexedMode : RGBMode));
cristy0910f242010-04-01 18:55:09 +00002182 }
2183 else
2184 {
cristy48845392010-06-02 01:19:17 +00002185 if (image->colorspace != CMYKColorspace)
cristye941a752011-10-15 01:52:48 +00002186 (void) TransformImageColorspace(image,CMYKColorspace,exception);
cristy2045da32010-04-16 00:59:35 +00002187 (void) WriteBlobMSBShort(image,CMYKMode);
cristy0910f242010-04-01 18:55:09 +00002188 }
cristy3ed852e2009-09-05 21:47:34 +00002189 }
cristy3a37efd2011-08-28 20:31:03 +00002190 if ((IsImageGray(image,exception) != MagickFalse) ||
cristyca9ddb02010-04-16 01:01:18 +00002191 (image->storage_class == DirectClass) || (image->colors > 256))
cristy3ed852e2009-09-05 21:47:34 +00002192 (void) WriteBlobMSBLong(image,0);
2193 else
2194 {
2195 /*
2196 Write PSD raster colormap.
2197 */
2198 (void) WriteBlobMSBLong(image,768);
cristybb503372010-05-27 20:51:26 +00002199 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002200 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red));
2201 for ( ; i < 256; i++)
2202 (void) WriteBlobByte(image,0);
cristybb503372010-05-27 20:51:26 +00002203 for (i=0; i < (ssize_t) image->colors; i++)
cristya20214e2010-08-28 16:52:13 +00002204 (void) WriteBlobByte(image,ScaleQuantumToChar(
2205 image->colormap[i].green));
cristy3ed852e2009-09-05 21:47:34 +00002206 for ( ; i < 256; i++)
2207 (void) WriteBlobByte(image,0);
cristybb503372010-05-27 20:51:26 +00002208 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002209 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue));
2210 for ( ; i < 256; i++)
2211 (void) WriteBlobByte(image,0);
2212 }
2213 /*
2214 Image resource block.
2215 */
cristy749d2152010-04-04 23:47:33 +00002216 length=28; /* 0x03EB */
cristy0b796e62010-04-29 00:38:45 +00002217 bim_profile=(StringInfo *) GetImageProfile(image,"8bim");
cristy749d2152010-04-04 23:47:33 +00002218 icc_profile=GetImageProfile(image,"icc");
cristyd4d3f742010-04-25 20:36:50 +00002219 if (bim_profile != (StringInfo *) NULL)
2220 {
cristy0b796e62010-04-29 00:38:45 +00002221 bim_profile=CloneStringInfo(bim_profile);
cristyd4d3f742010-04-25 20:36:50 +00002222 if (icc_profile != (StringInfo *) NULL)
2223 RemoveICCProfileFromResourceBlock(bim_profile);
cristyf11065e2010-05-14 13:26:59 +00002224 RemoveResolutionFromResourceBlock(bim_profile);
cristyd4d3f742010-04-25 20:36:50 +00002225 length+=PSDQuantum(GetStringInfoLength(bim_profile));
2226 }
cristy0b796e62010-04-29 00:38:45 +00002227 if (icc_profile != (const StringInfo *) NULL)
cristy749d2152010-04-04 23:47:33 +00002228 length+=PSDQuantum(GetStringInfoLength(icc_profile))+12;
cristy284c7d82010-04-24 00:19:14 +00002229 (void) WriteBlobMSBLong(image,(unsigned int) length);
cristyf11065e2010-05-14 13:26:59 +00002230 WriteResolutionResourceBlock(image);
cristy4176bb22010-05-01 16:29:09 +00002231 if (bim_profile != (StringInfo *) NULL)
2232 {
2233 (void) WriteBlob(image,GetStringInfoLength(bim_profile),
2234 GetStringInfoDatum(bim_profile));
2235 bim_profile=DestroyStringInfo(bim_profile);
2236 }
cristy749d2152010-04-04 23:47:33 +00002237 if (icc_profile != (StringInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002238 {
cristy749d2152010-04-04 23:47:33 +00002239 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
cristy4176bb22010-05-01 16:29:09 +00002240 (void) WriteBlobMSBShort(image,0x0000040F);
cristy749d2152010-04-04 23:47:33 +00002241 (void) WriteBlobMSBShort(image,0);
cristy284c7d82010-04-24 00:19:14 +00002242 (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength(
2243 icc_profile));
cristy749d2152010-04-04 23:47:33 +00002244 (void) WriteBlob(image,GetStringInfoLength(icc_profile),
2245 GetStringInfoDatum(icc_profile));
cristye195f262010-04-16 18:12:35 +00002246 if ((MagickOffsetType) GetStringInfoLength(icc_profile) !=
cristy2045da32010-04-16 00:59:35 +00002247 PSDQuantum(GetStringInfoLength(icc_profile)))
cristy749d2152010-04-04 23:47:33 +00002248 (void) WriteBlobByte(image,0);
cristye6365592010-04-02 17:31:23 +00002249 }
cristy48845392010-06-02 01:19:17 +00002250 layer_count=0;
2251 layer_info_size=2;
cristy2837bcc2010-08-07 23:57:39 +00002252 base_image=GetNextImageInList(image);
cristy8a46d822012-08-28 23:32:39 +00002253 if ((image->alpha_trait == BlendPixelTrait) && (base_image == (Image *) NULL))
cristy2837bcc2010-08-07 23:57:39 +00002254 base_image=image;
cristya20214e2010-08-28 16:52:13 +00002255 next_image=base_image;
2256 while ( next_image != NULL )
2257 {
2258 packet_size=next_image->depth > 8 ? 2UL : 1UL;
cristy3a37efd2011-08-28 20:31:03 +00002259 if (IsImageGray(next_image,exception) != MagickFalse)
cristy8a46d822012-08-28 23:32:39 +00002260 num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
cristy3ed852e2009-09-05 21:47:34 +00002261 else
cristya20214e2010-08-28 16:52:13 +00002262 if (next_image->storage_class == PseudoClass)
cristy8a46d822012-08-28 23:32:39 +00002263 num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
cristy2045da32010-04-16 00:59:35 +00002264 else
cristya20214e2010-08-28 16:52:13 +00002265 if (next_image->colorspace != CMYKColorspace)
cristy8a46d822012-08-28 23:32:39 +00002266 num_channels=next_image->alpha_trait == BlendPixelTrait ? 4UL : 3UL;
cristy2045da32010-04-16 00:59:35 +00002267 else
cristy8a46d822012-08-28 23:32:39 +00002268 num_channels=next_image->alpha_trait == BlendPixelTrait ? 5UL : 4UL;
cristya20214e2010-08-28 16:52:13 +00002269 channelLength=(size_t) (next_image->columns*next_image->rows*packet_size+2);
cristy48845392010-06-02 01:19:17 +00002270 layer_info_size+=(size_t) (4*4+2+num_channels*6+(psd_info.version == 1 ? 8 :
2271 16)+4*1+4+num_channels*channelLength);
cristyd15e6592011-10-15 00:13:06 +00002272 property=(const char *) GetImageProperty(next_image,"label",exception);
cristya20214e2010-08-28 16:52:13 +00002273 if (property == (const char *) NULL)
2274 layer_info_size+=16;
cristyde9b8f52010-03-12 01:17:00 +00002275 else
2276 {
cristya20214e2010-08-28 16:52:13 +00002277 size_t
2278 length;
2279
2280 length=strlen(property);
2281 layer_info_size+=8+length+(4-(length % 4));
cristyde9b8f52010-03-12 01:17:00 +00002282 }
cristy4aff1572010-02-15 13:34:01 +00002283 layer_count++;
cristya20214e2010-08-28 16:52:13 +00002284 next_image=GetNextImageInList(next_image);
cristy3ed852e2009-09-05 21:47:34 +00002285 }
cristy144f1b62010-05-18 00:52:09 +00002286 if (layer_count == 0)
cristy875e28a2010-03-06 19:46:55 +00002287 (void) SetPSDSize(&psd_info,image,0);
cristy3ed852e2009-09-05 21:47:34 +00002288 else
cristya20214e2010-08-28 16:52:13 +00002289 {
cristy8e9bd3e2010-08-29 00:03:56 +00002290 CompressionType
2291 compression;
2292
2293 (void) SetPSDSize(&psd_info,image,layer_info_size+
2294 (psd_info.version == 1 ? 8 : 16));
2295 if ((layer_info_size/2) != ((layer_info_size+1)/2))
2296 rounded_layer_info_size=layer_info_size+1;
cristya20214e2010-08-28 16:52:13 +00002297 else
cristy8e9bd3e2010-08-29 00:03:56 +00002298 rounded_layer_info_size=layer_info_size;
2299 (void) SetPSDSize(&psd_info,image,rounded_layer_info_size);
2300 (void) WriteBlobMSBShort(image,(unsigned short) layer_count);
2301 layer_count=1;
2302 compression=base_image->compression;
2303 next_image=base_image;
2304 while (next_image != NULL)
2305 {
2306 next_image->compression=NoCompression;
cristya4e5f472011-11-09 00:42:46 +00002307 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y);
2308 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x);
2309 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y+
2310 next_image->rows);
2311 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x+
2312 next_image->columns);
cristy8e9bd3e2010-08-29 00:03:56 +00002313 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2314 channel_size=(unsigned int) ((packet_size*next_image->rows*
2315 next_image->columns)+2);
cristy3a37efd2011-08-28 20:31:03 +00002316 if ((IsImageGray(next_image,exception) != MagickFalse) ||
cristy8e9bd3e2010-08-29 00:03:56 +00002317 (next_image->storage_class == PseudoClass))
2318 {
2319 (void) WriteBlobMSBShort(image,(unsigned short)
cristy8a46d822012-08-28 23:32:39 +00002320 (next_image->alpha_trait == BlendPixelTrait ? 2 : 1));
cristy8e9bd3e2010-08-29 00:03:56 +00002321 (void) WriteBlobMSBShort(image,0);
2322 (void) SetPSDSize(&psd_info,image,channel_size);
cristy8a46d822012-08-28 23:32:39 +00002323 if (next_image->alpha_trait == BlendPixelTrait)
cristy8e9bd3e2010-08-29 00:03:56 +00002324 {
2325 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2326 (void) SetPSDSize(&psd_info,image,channel_size);
2327 }
2328 }
2329 else
2330 if (next_image->colorspace != CMYKColorspace)
2331 {
2332 (void) WriteBlobMSBShort(image,(unsigned short)
cristy8a46d822012-08-28 23:32:39 +00002333 (next_image->alpha_trait == BlendPixelTrait ? 4 : 3));
cristy8e9bd3e2010-08-29 00:03:56 +00002334 (void) WriteBlobMSBShort(image,0);
2335 (void) SetPSDSize(&psd_info,image,channel_size);
2336 (void) WriteBlobMSBShort(image,1);
2337 (void) SetPSDSize(&psd_info,image,channel_size);
2338 (void) WriteBlobMSBShort(image,2);
2339 (void) SetPSDSize(&psd_info,image,channel_size);
cristy8a46d822012-08-28 23:32:39 +00002340 if (next_image->alpha_trait == BlendPixelTrait)
cristy8e9bd3e2010-08-29 00:03:56 +00002341 {
2342 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2343 (void) SetPSDSize(&psd_info,image,channel_size);
2344 }
2345 }
2346 else
2347 {
2348 (void) WriteBlobMSBShort(image,(unsigned short)
cristy8a46d822012-08-28 23:32:39 +00002349 (next_image->alpha_trait ? 5 : 4));
cristy8e9bd3e2010-08-29 00:03:56 +00002350 (void) WriteBlobMSBShort(image,0);
2351 (void) SetPSDSize(&psd_info,image,channel_size);
2352 (void) WriteBlobMSBShort(image,1);
2353 (void) SetPSDSize(&psd_info,image,channel_size);
2354 (void) WriteBlobMSBShort(image,2);
2355 (void) SetPSDSize(&psd_info,image,channel_size);
2356 (void) WriteBlobMSBShort(image,3);
2357 (void) SetPSDSize(&psd_info,image,channel_size);
cristy8a46d822012-08-28 23:32:39 +00002358 if (next_image->alpha_trait)
cristy8e9bd3e2010-08-29 00:03:56 +00002359 {
2360 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2361 (void) SetPSDSize(&psd_info,image,channel_size);
2362 }
2363 }
2364 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2365 (void) WriteBlob(image,4,(const unsigned char *)
2366 CompositeOperatorToPSDBlendMode(next_image->compose));
2367 (void) WriteBlobByte(image,255); /* layer opacity */
2368 (void) WriteBlobByte(image,0);
2369 (void) WriteBlobByte(image,1); /* layer propertys - visible, etc. */
2370 (void) WriteBlobByte(image,0);
cristyd15e6592011-10-15 00:13:06 +00002371 property=(const char *) GetImageProperty(next_image,"label",exception);
cristy8e9bd3e2010-08-29 00:03:56 +00002372 if (property == (const char *) NULL)
2373 {
2374 (void) WriteBlobMSBLong(image,16);
2375 (void) WriteBlobMSBLong(image,0);
2376 (void) WriteBlobMSBLong(image,0);
cristyb51dff52011-05-19 16:55:47 +00002377 (void) FormatLocaleString((char *) layer_name,MaxTextExtent,
cristy8e9bd3e2010-08-29 00:03:56 +00002378 "L%06ld",(long) layer_count++);
2379 WritePascalString( image, (char*)layer_name, 4 );
2380 }
2381 else
2382 {
2383 size_t
2384 length;
cristy3ed852e2009-09-05 21:47:34 +00002385
cristy8e9bd3e2010-08-29 00:03:56 +00002386 length=strlen(property);
2387 (void) WriteBlobMSBLong(image,(unsigned int) (length+(4-
2388 (length % 4))+8));
2389 (void) WriteBlobMSBLong(image,0);
2390 (void) WriteBlobMSBLong(image,0);
2391 WritePascalString(image,property,4);
2392 }
2393 next_image=GetNextImageInList(next_image);
2394 }
2395 /*
2396 Now the image data!
2397 */
2398 next_image=base_image;
2399 while (next_image != NULL)
2400 {
2401 status=WriteImageChannels(&psd_info,image_info,image,next_image,
cristy018f07f2011-09-04 21:15:19 +00002402 MagickTrue,exception);
cristy8e9bd3e2010-08-29 00:03:56 +00002403 next_image=GetNextImageInList(next_image);
2404 }
2405 (void) WriteBlobMSBLong(image,0); /* user mask data */
2406 base_image->compression=compression;
cristy144f1b62010-05-18 00:52:09 +00002407 }
cristy144f1b62010-05-18 00:52:09 +00002408 /*
2409 Write composite image.
2410 */
cristy018f07f2011-09-04 21:15:19 +00002411 status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse,
2412 exception);
cristy3ed852e2009-09-05 21:47:34 +00002413 (void) CloseBlob(image);
2414 return(status);
2415}