blob: 58f2699a48ccdf262ad344487c23ef178f6a01f0 [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% %
cristy16af1cb2009-12-11 21:38:29 +000021% Copyright 1999-2010 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*/
43#include "magick/studio.h"
44#include "magick/artifact.h"
45#include "magick/blob.h"
46#include "magick/blob-private.h"
47#include "magick/cache.h"
cristye7e40552010-04-24 21:34:22 +000048#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000049#include "magick/colorspace.h"
50#include "magick/constitute.h"
51#include "magick/enhance.h"
52#include "magick/exception.h"
53#include "magick/exception-private.h"
54#include "magick/image.h"
55#include "magick/image-private.h"
56#include "magick/list.h"
57#include "magick/log.h"
58#include "magick/magick.h"
59#include "magick/memory_.h"
60#include "magick/module.h"
cristy4689cf02010-02-17 21:15:45 +000061#include "magick/monitor-private.h"
cristy3ed852e2009-09-05 21:47:34 +000062#include "magick/profile.h"
63#include "magick/property.h"
64#include "magick/quantum-private.h"
65#include "magick/static.h"
66#include "magick/string_.h"
67
68/*
cristy2d3d87f2010-03-01 00:23:08 +000069 Define declaractions.
70*/
cristyaca3ec52010-03-02 14:55:01 +000071#define MaxPSDChannels 56
cristye6365592010-04-02 17:31:23 +000072#define PSDQuantum(x) (((long) (x)+1) & -2)
cristy2d3d87f2010-03-01 00:23:08 +000073
74/*
75 Enumerated declaractions.
76*/
77typedef enum
78{
79 BitmapMode = 0,
80 GrayscaleMode = 1,
81 IndexedMode = 2,
82 RGBMode = 3,
83 CMYKMode = 4,
84 MultichannelMode = 7,
85 DuotoneMode = 8,
86 LabMode = 9
87} PSDImageType;
88
89/*
90 Typedef declaractions.
91*/
92typedef struct _ChannelInfo
93{
94 short int
95 type;
96
97 unsigned long
98 size;
99} ChannelInfo;
100
101typedef struct _LayerInfo
102{
103 RectangleInfo
104 page,
105 mask;
106
107 unsigned short
108 channels;
109
110 ChannelInfo
111 channel_info[MaxPSDChannels];
112
113 char
114 blendkey[4];
115
116 Quantum
117 opacity;
118
119 unsigned char
120 clipping,
121 visible,
122 flags;
123
124 unsigned long
125 offset_x,
126 offset_y;
127
128 unsigned char
129 name[256];
130
131 Image
132 *image;
133} LayerInfo;
134
135typedef struct _PSDInfo
136{
137 char
138 signature[4];
139
140 unsigned short
141 channels,
142 version;
143
144 unsigned char
145 reserved[6];
146
147 unsigned long
148 rows,
149 columns;
150
151 unsigned short
152 depth,
153 mode;
154} PSDInfo;
155
156/*
cristy3ed852e2009-09-05 21:47:34 +0000157 Forward declarations.
158*/
159static MagickBooleanType
160 WritePSDImage(const ImageInfo *,Image *);
161
162/*
163%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164% %
165% %
166% %
cristy3ed852e2009-09-05 21:47:34 +0000167% I s P S D %
168% %
169% %
170% %
171%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172%
173% IsPSD()() returns MagickTrue if the image format type, identified by the
174% magick string, is PSD.
175%
176% The format of the IsPSD method is:
177%
178% MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
179%
180% A description of each parameter follows:
181%
182% o magick: compare image format pattern against these bytes.
183%
184% o length: Specifies the length of the magick string.
185%
186*/
187static MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
188{
189 if (length < 4)
190 return(MagickFalse);
191 if (LocaleNCompare((const char *) magick,"8BPS",4) == 0)
192 return(MagickTrue);
193 return(MagickFalse);
194}
195
196/*
197%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198% %
199% %
200% %
201% R e a d P S D I m a g e %
202% %
203% %
204% %
205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206%
207% ReadPSDImage() reads an Adobe Photoshop image file and returns it. It
208% allocates the memory necessary for the new Image structure and returns a
209% pointer to the new image.
210%
211% The format of the ReadPSDImage method is:
212%
cristycd081772010-03-22 17:19:12 +0000213% Image *ReadPSDImage(image_info)
cristy3ed852e2009-09-05 21:47:34 +0000214%
215% A description of each parameter follows:
216%
217% o image_info: the image info.
218%
219% o exception: return any errors or warnings in this structure.
220%
221*/
222
cristy19eb6412010-04-23 14:42:29 +0000223static const char *CompositeOperatorToPSDBlendMode(CompositeOperator op)
cristycd081772010-03-22 17:19:12 +0000224{
225 const char
226 *blend_mode;
227
cristy19eb6412010-04-23 14:42:29 +0000228 switch (op)
cristycd081772010-03-22 17:19:12 +0000229 {
230 case OverCompositeOp: blend_mode = "norm"; break;
231 case MultiplyCompositeOp: blend_mode = "mul "; break;
232 case DissolveCompositeOp: blend_mode = "diss"; break;
233 case DifferenceCompositeOp: blend_mode = "diff"; break;
234 case DarkenCompositeOp: blend_mode = "dark"; break;
235 case LightenCompositeOp: blend_mode = "lite"; break;
236 case HueCompositeOp: blend_mode = "hue "; break;
237 case SaturateCompositeOp: blend_mode = "sat "; break;
238 case ColorizeCompositeOp: blend_mode = "colr"; break;
239 case LuminizeCompositeOp: blend_mode = "lum "; break;
240 case ScreenCompositeOp: blend_mode = "scrn"; break;
241 case OverlayCompositeOp: blend_mode = "over"; break;
242 default:
243 blend_mode = "norm";
244 }
245 return(blend_mode);
246}
247
248static ssize_t DecodePSDPixels(const size_t number_compact_pixels,
249 const unsigned char *compact_pixels,const long depth,
250 const size_t number_pixels,unsigned char *pixels)
251{
252 int
253 pixel;
254
255 register ssize_t
256 i,
257 j;
258
259 ssize_t
260 packets;
261
262 size_t
263 length;
264
265 packets=(ssize_t) number_compact_pixels;
266 for (i=0; (packets > 1) && (i < (ssize_t) number_pixels); )
267 {
268 length=(*compact_pixels++);
269 packets--;
270 if (length == 128)
271 continue;
272 if (length > 128)
273 {
274 length=256-length+1;
275 pixel=(*compact_pixels++);
276 packets--;
cristy284c7d82010-04-24 00:19:14 +0000277 for (j=0; j < (ssize_t) length; j++)
cristycd081772010-03-22 17:19:12 +0000278 {
279 switch (depth)
280 {
281 case 1:
282 {
cristy284c7d82010-04-24 00:19:14 +0000283 *pixels++=(pixel >> 7) & 0x01 ? 0U : 255U;
284 *pixels++=(pixel >> 6) & 0x01 ? 0U : 255U;
285 *pixels++=(pixel >> 5) & 0x01 ? 0U : 255U;
286 *pixels++=(pixel >> 4) & 0x01 ? 0U : 255U;
287 *pixels++=(pixel >> 3) & 0x01 ? 0U : 255U;
288 *pixels++=(pixel >> 2) & 0x01 ? 0U : 255U;
289 *pixels++=(pixel >> 1) & 0x01 ? 0U : 255U;
290 *pixels++=(pixel >> 0) & 0x01 ? 0U : 255U;
cristycd081772010-03-22 17:19:12 +0000291 i+=8;
292 break;
293 }
294 case 4:
295 {
cristy284c7d82010-04-24 00:19:14 +0000296 *pixels++=(unsigned char) ((pixel >> 4) & 0xff);
297 *pixels++=(unsigned char) ((pixel & 0x0f) & 0xff);
cristycd081772010-03-22 17:19:12 +0000298 i+=2;
299 break;
300 }
301 case 2:
302 {
cristy284c7d82010-04-24 00:19:14 +0000303 *pixels++=(unsigned char) ((pixel >> 6) & 0x03);
304 *pixels++=(unsigned char) ((pixel >> 4) & 0x03);
305 *pixels++=(unsigned char) ((pixel >> 2) & 0x03);
306 *pixels++=(unsigned char) ((pixel & 0x03) & 0x03);
cristycd081772010-03-22 17:19:12 +0000307 i+=4;
308 break;
309 }
310 default:
311 {
cristy284c7d82010-04-24 00:19:14 +0000312 *pixels++=(unsigned char) pixel;
cristycd081772010-03-22 17:19:12 +0000313 i++;
314 break;
315 }
316 }
317 }
318 continue;
319 }
320 length++;
cristy284c7d82010-04-24 00:19:14 +0000321 for (j=0; j < (ssize_t) length; j++)
cristycd081772010-03-22 17:19:12 +0000322 {
323 switch (depth)
324 {
325 case 1:
326 {
cristy284c7d82010-04-24 00:19:14 +0000327 *pixels++=(*compact_pixels >> 7) & 0x01 ? 0U : 255U;
328 *pixels++=(*compact_pixels >> 6) & 0x01 ? 0U : 255U;
329 *pixels++=(*compact_pixels >> 5) & 0x01 ? 0U : 255U;
330 *pixels++=(*compact_pixels >> 4) & 0x01 ? 0U : 255U;
331 *pixels++=(*compact_pixels >> 3) & 0x01 ? 0U : 255U;
332 *pixels++=(*compact_pixels >> 2) & 0x01 ? 0U : 255U;
333 *pixels++=(*compact_pixels >> 1) & 0x01 ? 0U : 255U;
334 *pixels++=(*compact_pixels >> 0) & 0x01 ? 0U : 255U;
cristycd081772010-03-22 17:19:12 +0000335 i+=8;
336 break;
337 }
338 case 4:
339 {
cristy618a9662010-03-23 13:59:54 +0000340 *pixels++=(*compact_pixels >> 4) & 0xff;
341 *pixels++=(*compact_pixels & 0x0f) & 0xff;
cristycd081772010-03-22 17:19:12 +0000342 i+=2;
343 break;
344 }
345 case 2:
346 {
cristy618a9662010-03-23 13:59:54 +0000347 *pixels++=(*compact_pixels >> 6) & 0x03;
348 *pixels++=(*compact_pixels >> 4) & 0x03;
349 *pixels++=(*compact_pixels >> 2) & 0x03;
350 *pixels++=(*compact_pixels & 0x03) & 0x03;
cristycd081772010-03-22 17:19:12 +0000351 i+=4;
352 break;
353 }
354 default:
355 {
356 *pixels++=(*compact_pixels);
357 i++;
358 break;
359 }
360 }
361 compact_pixels++;
362 }
363 }
364 return(i);
365}
366
cristy2d3d87f2010-03-01 00:23:08 +0000367static inline MagickOffsetType GetPSDOffset(PSDInfo *psd_info,Image *image)
368{
369 if (psd_info->version == 1)
370 return((MagickOffsetType) ReadBlobMSBShort(image));
371 return((MagickOffsetType) ReadBlobMSBLong(image));
372}
373
374static inline MagickSizeType GetPSDSize(PSDInfo *psd_info,Image *image)
375{
376 if (psd_info->version == 1)
377 return((MagickSizeType) ReadBlobMSBLong(image));
378 return((MagickSizeType) ReadBlobMSBLongLong(image));
379}
380
cristy3ed852e2009-09-05 21:47:34 +0000381static inline long MagickAbsoluteValue(const long x)
382{
383 if (x < 0)
384 return(-x);
385 return(x);
386}
387
cristycd081772010-03-22 17:19:12 +0000388static const char *ModeToString(PSDImageType type)
cristy3ed852e2009-09-05 21:47:34 +0000389{
cristycd081772010-03-22 17:19:12 +0000390 switch (type)
cristy3ed852e2009-09-05 21:47:34 +0000391 {
392 case BitmapMode: return "Bitmap";
393 case GrayscaleMode: return "Grayscale";
394 case IndexedMode: return "Indexed";
395 case RGBMode: return "RGB";
396 case CMYKMode: return "CMYK";
397 case MultichannelMode: return "Multichannel";
398 case DuotoneMode: return "Duotone";
399 case LabMode: return "L*A*B";
400 default: return "unknown";
401 }
402}
403
404static MagickBooleanType ParseImageResourceBlocks(Image *image,
405 const unsigned char *blocks,size_t length)
406{
407 const unsigned char
408 *p;
409
410 StringInfo
411 *profile;
412
413 unsigned long
414 count,
415 long_sans;
416
417 unsigned short
418 id,
419 short_sans;
420
421 if (length < 16)
422 return(MagickFalse);
423 profile=AcquireStringInfo(length);
424 SetStringInfoDatum(profile,blocks);
425 (void) SetImageProfile(image,"8bim",profile);
426 profile=DestroyStringInfo(profile);
427 for (p=blocks; (p >= blocks) && (p < (blocks+length-16)); )
428 {
429 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
430 break;
cristyf11065e2010-05-14 13:26:59 +0000431 p=PushLongPixel(MSBEndian,p,&long_sans);
432 p=PushShortPixel(MSBEndian,p,&id);
433 p=PushShortPixel(MSBEndian,p,&short_sans);
434 p=PushLongPixel(MSBEndian,p,&count);
cristy3ed852e2009-09-05 21:47:34 +0000435 switch (id)
436 {
437 case 0x03ed:
438 {
cristy1e4a80b2010-05-14 16:18:26 +0000439 char
440 value[MaxTextExtent];
441
cristy3ed852e2009-09-05 21:47:34 +0000442 unsigned short
443 resolution;
444
445 /*
446 Resolution info.
447 */
cristyf11065e2010-05-14 13:26:59 +0000448 p=PushShortPixel(MSBEndian,p,&resolution);
cristy3ed852e2009-09-05 21:47:34 +0000449 image->x_resolution=(double) resolution;
cristy1e4a80b2010-05-14 16:18:26 +0000450 (void) FormatMagickString(value,MaxTextExtent,"%g",image->x_resolution);
451 (void) SetImageProperty(image,"tiff:XResolution",value);
cristyf11065e2010-05-14 13:26:59 +0000452 p=PushShortPixel(MSBEndian,p,&short_sans);
453 p=PushShortPixel(MSBEndian,p,&short_sans);
454 p=PushShortPixel(MSBEndian,p,&short_sans);
455 p=PushShortPixel(MSBEndian,p,&resolution);
cristy3ed852e2009-09-05 21:47:34 +0000456 image->y_resolution=(double) resolution;
cristy1e4a80b2010-05-14 16:18:26 +0000457 (void) FormatMagickString(value,MaxTextExtent,"%g",image->y_resolution);
458 (void) SetImageProperty(image,"tiff:YResolution",value);
cristyf11065e2010-05-14 13:26:59 +0000459 p=PushShortPixel(MSBEndian,p,&short_sans);
460 p=PushShortPixel(MSBEndian,p,&short_sans);
461 p=PushShortPixel(MSBEndian,p,&short_sans);
cristy3ed852e2009-09-05 21:47:34 +0000462 break;
463 }
464 default:
465 {
466 p+=count;
467 break;
468 }
469 }
470 if ((count & 0x01) != 0)
471 p++;
472 }
473 return(MagickTrue);
474}
475
cristycd081772010-03-22 17:19:12 +0000476static CompositeOperator PSDBlendModeToCompositeOperator(const char *mode)
cristy56ed31c2010-03-22 00:46:21 +0000477{
cristycd081772010-03-22 17:19:12 +0000478 if (mode == (const char *) NULL)
479 return(OverCompositeOp);
480 if (LocaleNCompare(mode,"norm",4) == 0)
481 return(OverCompositeOp);
482 if (LocaleNCompare(mode,"mul ",4) == 0)
483 return(MultiplyCompositeOp);
484 if (LocaleNCompare(mode,"diss",4) == 0)
485 return(DissolveCompositeOp);
486 if (LocaleNCompare(mode,"diff",4) == 0)
487 return(DifferenceCompositeOp);
488 if (LocaleNCompare(mode,"dark",4) == 0)
489 return(DarkenCompositeOp);
490 if (LocaleNCompare(mode,"lite",4) == 0)
491 return(LightenCompositeOp);
492 if (LocaleNCompare(mode,"hue ",4) == 0)
493 return(HueCompositeOp);
494 if (LocaleNCompare(mode,"sat ",4) == 0)
495 return(SaturateCompositeOp);
496 if (LocaleNCompare(mode,"colr",4) == 0)
497 return(ColorizeCompositeOp);
498 if (LocaleNCompare(mode,"lum ",4) == 0)
499 return(LuminizeCompositeOp);
500 if (LocaleNCompare(mode,"scrn",4) == 0)
501 return(ScreenCompositeOp);
502 if (LocaleNCompare(mode,"over",4) == 0)
503 return(OverlayCompositeOp);
504 if (LocaleNCompare(mode,"hLit",4) == 0)
505 return(OverCompositeOp);
506 if (LocaleNCompare(mode,"sLit",4) == 0)
507 return(OverCompositeOp);
508 if (LocaleNCompare(mode,"smud",4) == 0)
509 return(OverCompositeOp);
510 if (LocaleNCompare(mode,"div ",4) == 0)
511 return(OverCompositeOp);
512 if (LocaleNCompare(mode,"idiv",4) == 0)
513 return(OverCompositeOp);
514 return(OverCompositeOp);
cristy56ed31c2010-03-22 00:46:21 +0000515}
516
cristye3038982010-05-17 02:20:52 +0000517static MagickBooleanType ReadPSDLayer(Image *image,const unsigned long channels,
518 const long type,const MagickOffsetType *offsets,ExceptionInfo *exception)
cristy56ed31c2010-03-22 00:46:21 +0000519{
520 long
521 y;
522
523 Quantum
524 pixel;
525
526 register IndexPacket
527 *indexes;
528
529 register long
530 x;
531
532 register PixelPacket
533 *q;
534
535 register const unsigned char
536 *p;
537
538 size_t
539 packet_size;
540
541 ssize_t
542 count;
543
544 unsigned char
545 *compact_pixels,
546 *pixels;
547
548 unsigned short
549 nibble;
550
551 packet_size=1;
552 if (image->storage_class == PseudoClass)
553 {
554 if (image->colors > 256)
555 packet_size++;
556 else
557 if (image->depth > 8)
558 packet_size++;
559 }
560 else
561 if (image->depth > 8)
562 packet_size++;
cristycd081772010-03-22 17:19:12 +0000563 pixels=(unsigned char *) AcquireQuantumMemory(image->columns+256,packet_size*
cristy56ed31c2010-03-22 00:46:21 +0000564 sizeof(*pixels));
565 if (pixels == (unsigned char *) NULL)
566 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
567 image->filename);
cristycd081772010-03-22 17:19:12 +0000568 (void) ResetMagickMemory(pixels,0,image->columns*packet_size*sizeof(*pixels));
569 compact_pixels=(unsigned char *) NULL;
cristy56ed31c2010-03-22 00:46:21 +0000570 if (image->compression == RLECompression)
571 {
cristye195f262010-04-16 18:12:35 +0000572 size_t
573 length;
574
575 length=0;
576 for (y=0; y < (long) image->rows; y++)
577 if ((MagickOffsetType) length < offsets[y])
cristy284c7d82010-04-24 00:19:14 +0000578 length=(size_t) offsets[y];
cristye195f262010-04-16 18:12:35 +0000579 compact_pixels=(unsigned char *) AcquireQuantumMemory(length,
580 sizeof(*pixels));
cristy56ed31c2010-03-22 00:46:21 +0000581 if (compact_pixels == (unsigned char *) NULL)
582 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
583 image->filename);
cristy0132ba82010-04-16 23:34:02 +0000584 (void) ResetMagickMemory(compact_pixels,0,length*sizeof(*compact_pixels));
cristy56ed31c2010-03-22 00:46:21 +0000585 }
586 for (y=0; y < (long) image->rows; y++)
587 {
588 if (image->compression != RLECompression)
589 count=ReadBlob(image,packet_size*image->columns,pixels);
590 else
591 {
592 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
593 if (count != (ssize_t) offsets[y])
594 break;
595 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
cristy284c7d82010-04-24 00:19:14 +0000596 (long) image->depth,packet_size*image->columns,pixels);
cristy56ed31c2010-03-22 00:46:21 +0000597 }
cristycd081772010-03-22 17:19:12 +0000598 if (count < (ssize_t) (packet_size*image->columns))
cristy56ed31c2010-03-22 00:46:21 +0000599 break;
600 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
601 if (q == (PixelPacket *) NULL)
602 break;
603 indexes=GetAuthenticIndexQueue(image);
604 p=pixels;
605 for (x=0; x < (long) image->columns; x++)
606 {
607 if (packet_size == 1)
608 pixel=ScaleCharToQuantum(*p++);
609 else
610 {
611 p=PushShortPixel(MSBEndian,p,&nibble);
612 pixel=ScaleShortToQuantum(nibble);
613 }
614 switch (type)
615 {
616 case -1:
617 {
618 q->opacity=(Quantum) (QuantumRange-pixel);
619 break;
620 }
621 case 0:
622 {
623 q->red=pixel;
624 if (channels == 1)
625 {
626 q->green=q->red;
627 q->blue=q->red;
628 }
629 if (image->storage_class == PseudoClass)
630 {
631 if (packet_size == 1)
632 indexes[x]=(IndexPacket) ScaleQuantumToChar(pixel);
633 else
634 indexes[x]=(IndexPacket) ScaleQuantumToShort(pixel);
635 *q=image->colormap[(long) indexes[x]];
636 q->red=image->colormap[(long) indexes[x]].red;
637 q->green=image->colormap[(long) indexes[x]].green;
638 q->blue=image->colormap[(long) indexes[x]].blue;
639 }
640 break;
641 }
642 case 1:
643 {
644 if (image->storage_class == PseudoClass)
645 q->opacity=(Quantum) (QuantumRange-pixel);
646 else
647 q->green=pixel;
648 break;
649 }
650 case 2:
651 {
652 q->blue=pixel;
653 break;
654 }
655 case 3:
656 {
657 if (image->colorspace == CMYKColorspace)
658 indexes[x]=(IndexPacket) pixel;
659 else
660 q->opacity=(Quantum) (QuantumRange-pixel);
661 break;
662 }
663 case 4:
664 {
665 q->opacity=(Quantum) (QuantumRange-pixel);
666 break;
667 }
668 default:
669 break;
670 }
671 q++;
672 }
673 if (SyncAuthenticPixels(image,exception) == MagickFalse)
674 break;
675 }
676 if (image->compression == RLECompression)
677 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
678 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
679 return(MagickTrue);
680}
681
cristy3ed852e2009-09-05 21:47:34 +0000682static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception)
683{
cristy3ed852e2009-09-05 21:47:34 +0000684 char
cristy56ed31c2010-03-22 00:46:21 +0000685 message[MaxTextExtent],
cristy3ed852e2009-09-05 21:47:34 +0000686 type[4];
687
688 Image
689 *image;
690
691 IndexPacket
692 *indexes;
693
694 LayerInfo
695 *layer_info;
696
697 long
698 j,
699 number_layers,
700 y;
701
cristy56ed31c2010-03-22 00:46:21 +0000702 MagickBooleanType
703 status;
704
705 MagickOffsetType
706 offset,
707 *offsets;
708
709 MagickSizeType
710 combinedlength,
711 length,
712 size;
713
cristy3ed852e2009-09-05 21:47:34 +0000714 PSDInfo
715 psd_info;
716
717 register long
cristy56ed31c2010-03-22 00:46:21 +0000718 i,
cristy3ed852e2009-09-05 21:47:34 +0000719 x;
720
721 register PixelPacket
722 *q;
723
cristy3ed852e2009-09-05 21:47:34 +0000724 ssize_t
725 count;
726
cristy3ed852e2009-09-05 21:47:34 +0000727 unsigned char
728 *data;
729
730 unsigned short
731 compression;
732
733 unsigned long
734 mask_size,
cristy3ed852e2009-09-05 21:47:34 +0000735 skip_first_alpha = 0;
736
737 /*
738 Open image file.
739 */
740 assert(image_info != (const ImageInfo *) NULL);
741 assert(image_info->signature == MagickSignature);
742 if (image_info->debug != MagickFalse)
743 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
744 image_info->filename);
745 assert(exception != (ExceptionInfo *) NULL);
746 assert(exception->signature == MagickSignature);
747 image=AcquireImage(image_info);
748 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
749 if (status == MagickFalse)
750 {
751 image=DestroyImageList(image);
752 return((Image *) NULL);
753 }
754 /*
755 Read image header.
756 */
757 count=ReadBlob(image,4,(unsigned char *) psd_info.signature);
758 psd_info.version=ReadBlobMSBShort(image);
cristy50aea4a2010-03-09 17:37:44 +0000759 if ((count == 0) || (LocaleNCompare(psd_info.signature,"8BPS",4) != 0) ||
cristy2d3d87f2010-03-01 00:23:08 +0000760 ((psd_info.version != 1) && (psd_info.version != 2)))
cristy3ed852e2009-09-05 21:47:34 +0000761 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
762 count=ReadBlob(image,6,psd_info.reserved);
763 psd_info.channels=ReadBlobMSBShort(image);
764 if (psd_info.channels > MaxPSDChannels)
765 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
766 psd_info.rows=ReadBlobMSBLong(image);
767 psd_info.columns=ReadBlobMSBLong(image);
cristy2d3d87f2010-03-01 00:23:08 +0000768 if ((psd_info.version == 1) && ((psd_info.rows > 30000) ||
769 (psd_info.columns > 30000)))
770 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000771 psd_info.depth=ReadBlobMSBShort(image);
cristy2d3d87f2010-03-01 00:23:08 +0000772 if ((psd_info.depth != 1) && (psd_info.depth != 8) && (psd_info.depth != 16))
773 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000774 psd_info.mode=ReadBlobMSBShort(image);
775 if (image->debug != MagickFalse)
776 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
777 " Image is %ld x %ld with channels=%d, depth=%d, mode=%s",
778 psd_info.columns,psd_info.rows,psd_info.channels,psd_info.depth,
779 ModeToString((PSDImageType) psd_info.mode));
780 /*
781 Initialize image.
782 */
783 image->depth=psd_info.depth;
784 image->columns=psd_info.columns;
785 image->rows=psd_info.rows;
cristy95524f92010-02-16 18:44:34 +0000786 if (SetImageBackgroundColor(image) == MagickFalse)
787 {
788 InheritException(exception,&image->exception);
789 image=DestroyImageList(image);
790 return((Image *) NULL);
791 }
cristy3ed852e2009-09-05 21:47:34 +0000792 image->matte=psd_info.channels >= 4 ? MagickTrue : MagickFalse;
793 if (psd_info.mode == LabMode)
794 image->colorspace=LabColorspace;
795 if (psd_info.mode == CMYKMode)
796 {
797 image->colorspace=CMYKColorspace;
798 image->matte=psd_info.channels >= 5 ? MagickTrue : MagickFalse;
799 }
800 if ((psd_info.mode == BitmapMode) || (psd_info.mode == GrayscaleMode) ||
801 (psd_info.mode == DuotoneMode))
802 {
cristy52cf7f12010-02-07 18:07:56 +0000803 if (AcquireImageColormap(image,256) == MagickFalse)
804 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +0000805 image->matte=psd_info.channels >= 2 ? MagickTrue : MagickFalse;
806 if (image->debug != MagickFalse)
807 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
808 " ImageColorMap allocated");
cristy0132ba82010-04-16 23:34:02 +0000809 image->colorspace=GRAYColorspace;
cristy3ed852e2009-09-05 21:47:34 +0000810 }
811 if (image->debug != MagickFalse)
812 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
813 image->matte ? " image has matte" : " image has no matte");
814 /*
815 Read PSD raster colormap only present for indexed and duotone images.
816 */
817 length=ReadBlobMSBLong(image);
818 if (length != 0)
819 {
820 if (image->debug != MagickFalse)
821 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
822 " reading colormap");
823 if (psd_info.mode == DuotoneMode)
824 {
825 /*
826 Duotone image data; the format of this data is undocumented.
827 */
cristy56ed31c2010-03-22 00:46:21 +0000828 data=(unsigned char *) AcquireQuantumMemory((size_t) length,
829 sizeof(*data));
cristy3ed852e2009-09-05 21:47:34 +0000830 if (data == (unsigned char *) NULL)
831 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy56ed31c2010-03-22 00:46:21 +0000832 count=ReadBlob(image,(size_t) length,data);
cristy3ed852e2009-09-05 21:47:34 +0000833 data=(unsigned char *) RelinquishMagickMemory(data);
834 }
835 else
836 {
837 /*
838 Read PSD raster colormap.
839 */
840 if (AcquireImageColormap(image,(unsigned long) (length/3)) == MagickFalse)
841 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
842 for (i=0; i < (long) image->colors; i++)
843 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
844 ReadBlobByte(image));
845 for (i=0; i < (long) image->colors; i++)
846 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
847 ReadBlobByte(image));
848 for (i=0; i < (long) image->colors; i++)
849 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
850 ReadBlobByte(image));
851 image->matte=psd_info.channels >= 2 ? MagickTrue : MagickFalse;
852 }
853 }
854 length=ReadBlobMSBLong(image);
855 if (length != 0)
856 {
857 unsigned char
858 *blocks;
859
860 /*
861 Image resources block.
862 */
863 if (image->debug != MagickFalse)
864 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
865 " reading image resource blocks - %ld bytes",(long) length);
cristy56ed31c2010-03-22 00:46:21 +0000866 blocks=(unsigned char *) AcquireQuantumMemory((size_t) length,
867 sizeof(*blocks));
cristy3ed852e2009-09-05 21:47:34 +0000868 if (blocks == (unsigned char *) NULL)
869 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy56ed31c2010-03-22 00:46:21 +0000870 count=ReadBlob(image,(size_t) length,blocks);
871 if ((count != (ssize_t) length) ||
cristy3ed852e2009-09-05 21:47:34 +0000872 (LocaleNCompare((char *) blocks,"8BIM",4) != 0))
873 {
874 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
875 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
876 }
cristy56ed31c2010-03-22 00:46:21 +0000877 (void) ParseImageResourceBlocks(image,blocks,(size_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000878 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
879 }
880 /*
881 If we are only "pinging" the image, then we're done - so return.
882 */
883 if (image_info->ping != MagickFalse)
884 {
885 (void) CloseBlob(image);
886 return(GetFirstImageInList(image));
887 }
888 /*
889 Layer and mask block.
890 */
891 layer_info=(LayerInfo *) NULL;
cristy4689cf02010-02-17 21:15:45 +0000892 number_layers=1;
cristy2d3d87f2010-03-01 00:23:08 +0000893 length=GetPSDSize(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +0000894 if (length == 8)
895 {
896 length=ReadBlobMSBLong(image);
897 length=ReadBlobMSBLong(image);
898 }
899 if ((image_info->number_scenes == 1) && (image_info->scene == 0))
900 for ( ; length != 0; length--)
901 if (ReadBlobByte(image) == EOF)
902 {
903 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
904 image->filename);
905 break;
906 }
907 if (length == 0)
908 {
909 if (image->debug != MagickFalse)
910 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
911 " image has no layers");
912 }
913 else
914 {
915 offset=TellBlob(image);
cristy2d3d87f2010-03-01 00:23:08 +0000916 size=GetPSDSize(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +0000917 if (size == 0)
918 {
cristy2d3d87f2010-03-01 00:23:08 +0000919 unsigned long
920 quantum;
921
cristy3ed852e2009-09-05 21:47:34 +0000922 /*
923 Skip layers & masks.
924 */
cristy56ed31c2010-03-22 00:46:21 +0000925 quantum=psd_info.version == 1 ? 4UL : 8UL;
cristy2d3d87f2010-03-01 00:23:08 +0000926 for (j=0; j < (long) (length-quantum); j++)
cristy3ed852e2009-09-05 21:47:34 +0000927 (void) ReadBlobByte(image);
928 }
929 else
930 {
931 MagickOffsetType
932 layer_offset;
933
934 layer_offset=offset+length;
935 number_layers=(short) ReadBlobMSBShort(image);
936 if (image->debug != MagickFalse)
937 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
938 " image contains %ld layers", number_layers);
939 if (number_layers < 0)
940 {
941 /*
942 Weird hack in PSD format to ignore first alpha channel.
943 */
944 skip_first_alpha=1;
945 number_layers=MagickAbsoluteValue(number_layers);
946 if (image->debug != MagickFalse)
947 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
948 " negative layer count corrected for");
949 }
950 layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers,
951 sizeof(*layer_info));
952 if (layer_info == (LayerInfo *) NULL)
953 {
954 if (image->debug != MagickFalse)
955 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
956 " allocation of LayerInfo failed");
957 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
958 }
959 (void) ResetMagickMemory(layer_info,0,(size_t) number_layers*
960 sizeof(*layer_info));
961 for (i=0; i < number_layers; i++)
962 {
963 if (image->debug != MagickFalse)
964 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
965 " reading layer #%ld",i+1);
966 layer_info[i].page.y=(long) ReadBlobMSBLong(image);
967 layer_info[i].page.x=(long) ReadBlobMSBLong(image);
cristy56ed31c2010-03-22 00:46:21 +0000968 layer_info[i].page.height=(unsigned long)
969 ReadBlobMSBLong(image)-layer_info[i].page.y;
970 layer_info[i].page.width=(unsigned long)
971 ReadBlobMSBLong(image)-layer_info[i].page.x;
cristy3ed852e2009-09-05 21:47:34 +0000972 layer_info[i].channels=ReadBlobMSBShort(image);
973 if (layer_info[i].channels > MaxPSDChannels)
974 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
975 if (image->debug != MagickFalse)
976 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
977 " offset(%ld,%ld), size(%ld,%ld), channels=%d",
978 layer_info[i].page.x, layer_info[i].page.y,
979 layer_info[i].page.height,layer_info[i].page.width,
980 layer_info[i].channels);
981 for (j=0; j < (long) layer_info[i].channels; j++)
982 {
cristy56ed31c2010-03-22 00:46:21 +0000983 layer_info[i].channel_info[j].type=(short)
984 ReadBlobMSBShort(image);
985 layer_info[i].channel_info[j].size=(unsigned long)
986 GetPSDSize(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +0000987 if (image->debug != MagickFalse)
988 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
989 " channel[%ld]: type=%d, size=%ld",j,
990 layer_info[i].channel_info[j].type,
991 (long) layer_info[i].channel_info[j].size);
992 }
993 count=ReadBlob(image,4,(unsigned char *) type);
994 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
995 {
996 if (image->debug != MagickFalse)
997 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
998 " layer type was %.4s instead of 8BIM", type);
999 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1000 }
1001 count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey);
1002 layer_info[i].opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1003 (unsigned char) ReadBlobByte(image)));
1004 layer_info[i].clipping=(unsigned char) ReadBlobByte(image);
1005 layer_info[i].flags=(unsigned char) ReadBlobByte(image);
1006 layer_info[i].visible=!(layer_info[i].flags & 0x02);
1007 if (image->debug != MagickFalse)
1008 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1009 " blend=%.4s, opacity=%lu, clipping=%s, flags=%d, visible=%s",
1010 layer_info[i].blendkey,(unsigned long) layer_info[i].opacity,
1011 layer_info[i].clipping ? "true" : "false",layer_info[i].flags,
1012 layer_info[i].visible ? "true" : "false");
1013 (void) ReadBlobByte(image); /* filler */
1014 combinedlength=0;
1015 size=ReadBlobMSBLong(image);
1016 if (size != 0)
1017 {
1018 if (image->debug != MagickFalse)
1019 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1020 " layer contains additional info");
1021 length=ReadBlobMSBLong(image);
1022 if (length != 0)
1023 {
1024 /*
1025 Layer mask info.
1026 */
1027 layer_info[i].mask.y=(long) ReadBlobMSBLong(image);
1028 layer_info[i].mask.x=(long) ReadBlobMSBLong(image);
cristy749d2152010-04-04 23:47:33 +00001029 layer_info[i].mask.height=(unsigned long)
cristy3ed852e2009-09-05 21:47:34 +00001030 (ReadBlobMSBLong(image)-layer_info[i].mask.y);
cristy749d2152010-04-04 23:47:33 +00001031 layer_info[i].mask.width=(unsigned long)
cristy3ed852e2009-09-05 21:47:34 +00001032 (ReadBlobMSBLong(image)-layer_info[i].mask.x);
1033 if (image->debug != MagickFalse)
1034 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1035 " layer mask: offset(%ld,%ld), size(%ld,%ld), length=%ld",
1036 layer_info[i].mask.x,layer_info[i].mask.y,
1037 layer_info[i].mask.width, layer_info[i].mask.height,
1038 (long) length-16);
1039 /*
1040 Skip over the rest of the layer mask information.
1041 */
1042 for (j=0; j < (long) (length-16); j++)
1043 (void) ReadBlobByte(image);
1044 }
1045 combinedlength+=length+4; /* +4 for length */
1046 length=ReadBlobMSBLong(image);
1047 if (length != 0)
1048 {
1049 /*
1050 Layer blending ranges info.
1051 */
1052 if (image->debug != MagickFalse)
1053 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1054 " layer blending ranges: length=%ld",(long) length);
1055 /*
1056 We read it, but don't use it...
1057 */
1058 for (j=0; j < (long) (length); j+=8)
1059 {
1060 size_t blend_source=ReadBlobMSBLong(image);
1061 size_t blend_dest=ReadBlobMSBLong(image);
1062 if (image->debug != MagickFalse)
1063 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1064 " source(%x), dest(%x)",(unsigned int)
1065 blend_source,(unsigned int) blend_dest);
1066 }
1067 }
cristy2cc39842009-10-10 20:04:02 +00001068 combinedlength+=length+4;
1069 /*
1070 Layer name.
1071 */
cristy3ed852e2009-09-05 21:47:34 +00001072 length=(size_t) ReadBlobByte(image);
cristy2cc39842009-10-10 20:04:02 +00001073 for (j=0; j < (long) length; j++)
1074 layer_info[i].name[j]=(unsigned char) ReadBlobByte(image);
1075 layer_info[i].name[j]='\0';
1076 if (image->debug != MagickFalse)
1077 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1078 " layer name: %s",layer_info[i].name);
1079 combinedlength+=length+1;
cristy3ed852e2009-09-05 21:47:34 +00001080
1081#if 0 /* still in development */
1082 /*
1083 Adjustment layers and other stuff...
1084 */
1085 {
1086 char alsig[4],
1087 alkey[4];
1088
1089 count=ReadBlob(image,4,alsig);
1090 if ((count == 0) || (LocaleNCompare(alsig,"8BIM",4) != 0)) {
1091 if (debug != MagickFalse)
1092 {
1093 if (image->debug != MagickFalse)
1094 (void) LogMagickEvent(CoderEvent,GetMagickModule()," adjustment layer type was %.4s instead of 8BIM", alsig);
1095 }
1096 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1097 }
1098 count=ReadBlob(image,4,alkey);
1099 length=ReadBlobMSBLong(image);
1100 if (debug != MagickFalse)
1101 {
1102 if (image->debug != MagickFalse)
1103 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1104 " adjustment layer key: %.4s, data length=%ld",
1105 alkey, length);
1106 }
1107
1108 if ( length ) {
1109 for (j=0; j < (long) (length); j++)
1110 (void) ReadBlobByte(image);
1111 }
1112
1113 }
1114 combinedlength += 12 + length; /* sig, key, length + the actual length*/
1115#endif
1116
1117 /*
1118 Skip the rest of the variable data until we support it.
1119 */
1120 if (image->debug != MagickFalse)
1121 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1122 " unsupported data: length=%ld",(long)
cristyeec18db2010-03-03 21:15:45 +00001123 (size-combinedlength));
cristy3ed852e2009-09-05 21:47:34 +00001124 for (j=0; j < (long) (size-combinedlength); j++)
1125 (void) ReadBlobByte(image);
1126 }
1127 /*
1128 Allocate layered image.
1129 */
1130 layer_info[i].image=CloneImage(image,layer_info[i].page.width,
1131 layer_info[i].page.height,MagickFalse,&image->exception);
1132 if (layer_info[i].image == (Image *) NULL)
1133 {
1134 for (j=0; j < i; j++)
1135 layer_info[j].image=DestroyImage(layer_info[j].image);
1136 if (image->debug != MagickFalse)
1137 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1138 " allocation of image for layer %ld failed", i);
1139 ThrowReaderException(ResourceLimitError,
1140 "MemoryAllocationFailed");
1141 }
1142 if (image->debug != MagickFalse)
1143 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1144 " setting up new layer image");
1145 (void) SetImageBackgroundColor(layer_info[i].image);
1146 layer_info[i].image->compose=
1147 PSDBlendModeToCompositeOperator(layer_info[i].blendkey);
1148 if (layer_info[i].visible == MagickFalse)
1149 layer_info[i].image->compose=NoCompositeOp;
1150 if (psd_info.mode == CMYKMode)
cristy0132ba82010-04-16 23:34:02 +00001151 layer_info[i].image->colorspace=CMYKColorspace;
1152 if ((psd_info.mode == BitmapMode) ||
1153 (psd_info.mode == GrayscaleMode) ||
1154 (psd_info.mode == DuotoneMode))
1155 layer_info[i].image->colorspace=GRAYColorspace;
cristy3ed852e2009-09-05 21:47:34 +00001156 for (j=0; j < (long) layer_info[i].channels; j++)
1157 if (layer_info[i].channel_info[j].type == -1)
1158 layer_info[i].image->matte=MagickTrue;
1159 /*
1160 Set up some hidden attributes for folks that need them.
1161 */
cristy56ed31c2010-03-22 00:46:21 +00001162 (void) FormatMagickString(message,MaxTextExtent,"%ld",
1163 layer_info[i].page.x);
1164 (void) SetImageArtifact(layer_info[i].image,"psd:layer.x",message);
1165 (void) FormatMagickString(message,MaxTextExtent,"%ld",
cristy3ed852e2009-09-05 21:47:34 +00001166 layer_info[i].page.y);
cristy56ed31c2010-03-22 00:46:21 +00001167 (void) SetImageArtifact(layer_info[i].image,"psd:layer.y",message);
1168 (void) FormatMagickString(message,MaxTextExtent,"%lu",
1169 (unsigned long) layer_info[i].opacity);
1170 (void) SetImageArtifact(layer_info[i].image,"psd:layer.opacity",
1171 message);
cristy3ed852e2009-09-05 21:47:34 +00001172 (void) SetImageProperty(layer_info[i].image,"label",(char *)
1173 layer_info[i].name);
1174 }
1175 if (image->debug != MagickFalse)
1176 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1177 " reading image data for layers");
1178 /*
1179 Read pixel data for each layer.
1180 */
1181 for (i=0; i < number_layers; i++)
1182 {
1183 if (image->debug != MagickFalse)
1184 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1185 " reading data for layer %ld",i);
1186 for (j=0; j < (long) layer_info[i].channels; j++)
1187 {
1188 if (image->debug != MagickFalse)
1189 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1190 " reading data for channel %ld", j);
1191#if 1
1192 if (layer_info[i].channel_info[j].size <= (2*layer_info[i].image->rows))
1193 {
1194 long
1195 k;
1196
1197 if (image->debug != MagickFalse)
1198 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1199 " layer data is empty");
1200 /*
1201 A layer without data.
1202 */
1203 for (k=0; k < (long) layer_info[i].channel_info[j].size; k++)
1204 (void) ReadBlobByte(layer_info[i].image);
1205 continue;
1206 }
1207#endif
cristy56ed31c2010-03-22 00:46:21 +00001208 offsets=(MagickOffsetType *) NULL;
1209 layer_info[i].image->compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00001210 compression=ReadBlobMSBShort(layer_info[i].image);
cristy2cc39842009-10-10 20:04:02 +00001211 if ((layer_info[i].page.height != 0) &&
1212 (layer_info[i].page.width != 0))
cristy3ed852e2009-09-05 21:47:34 +00001213 {
cristy2cc39842009-10-10 20:04:02 +00001214 if (compression == 1)
1215 {
1216 /*
1217 Read RLE compressed data.
1218 */
cristy56ed31c2010-03-22 00:46:21 +00001219 layer_info[i].image->compression=RLECompression;
cristy2cc39842009-10-10 20:04:02 +00001220 if (image->debug != MagickFalse)
1221 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1222 " layer data is RLE compressed");
cristy56ed31c2010-03-22 00:46:21 +00001223 offsets=(MagickOffsetType *) AcquireQuantumMemory(
1224 layer_info[i].image->rows,sizeof(*offsets));
1225 if (offsets == (MagickOffsetType *) NULL)
1226 ThrowReaderException(ResourceLimitError,
1227 "MemoryAllocationFailed");
cristy2cc39842009-10-10 20:04:02 +00001228 for (y=0; y < (long) layer_info[i].image->rows; y++)
cristy56ed31c2010-03-22 00:46:21 +00001229 offsets[y]=GetPSDOffset(&psd_info,layer_info[i].image);
cristy2cc39842009-10-10 20:04:02 +00001230 }
cristy56ed31c2010-03-22 00:46:21 +00001231 status=ReadPSDLayer(layer_info[i].image,
cristye3038982010-05-17 02:20:52 +00001232 layer_info[i].channels,layer_info[i].channel_info[j].type,
1233 offsets,exception);
cristy56ed31c2010-03-22 00:46:21 +00001234 if (compression == 1)
1235 offsets=(MagickOffsetType *) RelinquishMagickMemory(
1236 offsets);
1237 if (status == MagickFalse)
1238 break;
cristy3ed852e2009-09-05 21:47:34 +00001239 }
cristy56ed31c2010-03-22 00:46:21 +00001240 }
cristy3ed852e2009-09-05 21:47:34 +00001241 if (layer_info[i].opacity != OpaqueOpacity)
1242 {
1243 /*
1244 Correct for opacity level.
1245 */
1246 for (y=0; y < (long) layer_info[i].image->rows; y++)
1247 {
1248 q=GetAuthenticPixels(layer_info[i].image,0,y,
1249 layer_info[i].image->columns,1,exception);
1250 if (q == (PixelPacket *) NULL)
1251 break;
1252 indexes=GetAuthenticIndexQueue(layer_info[i].image);
1253 for (x=0; x < (long) layer_info[i].image->columns; x++)
1254 {
cristy07758662010-01-15 20:25:50 +00001255 q->opacity=(Quantum) (QuantumRange-(Quantum) (QuantumScale*
1256 ((QuantumRange-q->opacity)*(QuantumRange-
1257 layer_info[i].opacity))));
cristy3ed852e2009-09-05 21:47:34 +00001258 q++;
1259 }
1260 if (SyncAuthenticPixels(layer_info[i].image,exception) == MagickFalse)
1261 break;
1262 }
1263 }
1264 if (layer_info[i].image->colorspace == CMYKColorspace)
1265 (void) NegateImage(layer_info[i].image,MagickFalse);
cristy56ed31c2010-03-22 00:46:21 +00001266 status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType)
1267 number_layers);
cristy4689cf02010-02-17 21:15:45 +00001268 if (status == MagickFalse)
1269 break;
cristy3ed852e2009-09-05 21:47:34 +00001270 }
1271 /* added by palf -> invisible group layer make layer of this group
1272 invisible I consider that all layer with width and height null are
1273 layer for group layer */
1274 {
1275 short inside_layer = 0;
1276 short layer_visible = 0;
1277 for (i=number_layers-1; i >=0; i--)
1278 {
1279 if ((layer_info[i].page.width == 0) ||
1280 (layer_info[i].page.height == 0))
1281 {
1282 if (inside_layer == 0)
1283 {
1284 inside_layer=1;
1285 layer_visible=(short int) layer_info[i].visible;
1286 }
1287 else
1288 {
1289 inside_layer = 0;
1290 }
1291 }
1292 else
1293 if ((inside_layer == 1) && (layer_visible == 0))
1294 {
1295 layer_info[i].visible=(unsigned char) layer_visible;
1296 layer_info[i].image->compose=NoCompositeOp;
1297 }
1298 }
1299 }
1300 /* added by palf -> suppression of empty layer */
1301 /* I consider that all layer with width and height null are layer for group layer */
1302 for (i=0; i < number_layers; i++)
1303 {
1304 if ((layer_info[i].page.width == 0) ||
1305 (layer_info[i].page.height == 0))
1306 {
1307 if (layer_info[i].image != (Image *) NULL)
1308 layer_info[i].image=DestroyImage(layer_info[i].image);
1309 for (j=i; j < number_layers - 1; j++)
1310 layer_info[j] = layer_info[j+1];
1311 number_layers--;
1312 i--;
1313 }
cristy56ed31c2010-03-22 00:46:21 +00001314 }
cristy3ed852e2009-09-05 21:47:34 +00001315 mask_size = ReadBlobMSBLong(image); /* global mask size: currently ignored */
cristy56ed31c2010-03-22 00:46:21 +00001316 if (number_layers > 0)
1317 {
1318 if (image->debug != MagickFalse)
1319 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1320 " putting layers into image list");
1321 for (i=0; i < number_layers; i++)
cristy3ed852e2009-09-05 21:47:34 +00001322 {
cristy56ed31c2010-03-22 00:46:21 +00001323 if (i > 0)
1324 layer_info[i].image->previous=layer_info[i-1].image;
1325 if (i < (number_layers-1))
1326 layer_info[i].image->next=layer_info[i+1].image;
1327 layer_info[i].image->page=layer_info[i].page;
1328 }
cristy3ed852e2009-09-05 21:47:34 +00001329 image->next=layer_info[0].image;
1330 layer_info[0].image->previous=image;
1331 layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info);
cristy56ed31c2010-03-22 00:46:21 +00001332 }
1333 layer_offset-=TellBlob(image);
1334 offset=SeekBlob(image,layer_offset,SEEK_CUR);
1335 }
cristy3ed852e2009-09-05 21:47:34 +00001336 }
1337 /*
1338 Read the precombined layer, present for PSD < 4 compatibility
1339 */
1340 if (image->debug != MagickFalse)
1341 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1342 " reading the precombined layer");
cristy56ed31c2010-03-22 00:46:21 +00001343 offsets=(MagickOffsetType *) NULL;
1344 image->compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00001345 compression=ReadBlobMSBShort(image);
1346 if (compression == 1)
1347 {
1348 /*
cristy3f938c62010-03-07 00:53:52 +00001349 Read Packbit encoded pixel data as separate planes.
cristy3ed852e2009-09-05 21:47:34 +00001350 */
cristy56ed31c2010-03-22 00:46:21 +00001351 image->compression=RLECompression;
1352 offsets=(MagickOffsetType *) AcquireQuantumMemory(image->rows,
1353 psd_info.channels*sizeof(*offsets));
1354 if (offsets == (MagickOffsetType *) NULL)
1355 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00001356 for (i=0; i < (long) (image->rows*psd_info.channels); i++)
cristy56ed31c2010-03-22 00:46:21 +00001357 offsets[i]=GetPSDOffset(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +00001358 }
cristy56ed31c2010-03-22 00:46:21 +00001359 for (i=0; i < (long) psd_info.channels; i++)
1360 {
cristye3038982010-05-17 02:20:52 +00001361 status=ReadPSDLayer(image,psd_info.channels,i,offsets+i*image->rows,
1362 exception);
cristy56ed31c2010-03-22 00:46:21 +00001363 if (status == MagickFalse)
1364 break;
1365 status=SetImageProgress(image,LoadImagesTag,i,psd_info.channels);
1366 if (status == MagickFalse)
1367 break;
1368 }
1369 if (compression == 1)
1370 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
cristy3ed852e2009-09-05 21:47:34 +00001371 if (image->colorspace == CMYKColorspace)
1372 (void) NegateImage(image,MagickFalse);
1373 (void) CloseBlob(image);
1374 return(GetFirstImageInList(image));
1375}
1376
1377/*
1378%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1379% %
1380% %
1381% %
1382% R e g i s t e r P S D I m a g e %
1383% %
1384% %
1385% %
1386%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387%
1388% RegisterPSDImage() adds properties for the PSD image format to
1389% the list of supported formats. The properties include the image format
1390% tag, a method to read and/or write the format, whether the format
1391% supports the saving of more than one frame to the same file or blob,
1392% whether the format supports native in-memory I/O, and a brief
1393% description of the format.
1394%
1395% The format of the RegisterPSDImage method is:
1396%
1397% unsigned long RegisterPSDImage(void)
1398%
1399*/
1400ModuleExport unsigned long RegisterPSDImage(void)
1401{
1402 MagickInfo
1403 *entry;
1404
cristyb4233012010-02-28 20:09:14 +00001405 entry=SetMagickInfo("PSB");
1406 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1407 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1408 entry->magick=(IsImageFormatHandler *) IsPSD;
1409 entry->description=ConstantString("Adobe Large Document Format");
1410 entry->module=ConstantString("PSD");
1411 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001412 entry=SetMagickInfo("PSD");
1413 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1414 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1415 entry->magick=(IsImageFormatHandler *) IsPSD;
1416 entry->description=ConstantString("Adobe Photoshop bitmap");
1417 entry->module=ConstantString("PSD");
1418 (void) RegisterMagickInfo(entry);
1419 return(MagickImageCoderSignature);
1420}
1421
1422/*
1423%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1424% %
1425% %
1426% %
1427% U n r e g i s t e r P S D I m a g e %
1428% %
1429% %
1430% %
1431%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1432%
1433% UnregisterPSDImage() removes format registrations made by the
1434% PSD module from the list of supported formats.
1435%
1436% The format of the UnregisterPSDImage method is:
1437%
1438% UnregisterPSDImage(void)
1439%
1440*/
1441ModuleExport void UnregisterPSDImage(void)
1442{
cristyb4233012010-02-28 20:09:14 +00001443 (void) UnregisterMagickInfo("PSB");
cristy3ed852e2009-09-05 21:47:34 +00001444 (void) UnregisterMagickInfo("PSD");
1445}
1446
1447/*
1448%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1449% %
1450% %
1451% %
1452% W r i t e P S D I m a g e %
1453% %
1454% %
1455% %
1456%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1457%
cristyb1459bc2010-03-23 21:41:43 +00001458% WritePSDImage() writes an image in the Adobe Photoshop encoded image format.
cristy3ed852e2009-09-05 21:47:34 +00001459%
1460% The format of the WritePSDImage method is:
1461%
1462% MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image)
1463%
1464% A description of each parameter follows.
1465%
1466% o image_info: the image info.
1467%
1468% o image: The image.
1469%
1470%
1471*/
1472
cristy50aea4a2010-03-09 17:37:44 +00001473static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image,
cristyf0460ee2010-03-06 02:55:11 +00001474 const size_t offset)
1475{
1476 if (psd_info->version == 1)
cristy56ed31c2010-03-22 00:46:21 +00001477 return(WriteBlobMSBShort(image,(unsigned short) offset));
1478 return(WriteBlobMSBLong(image,(unsigned short) offset));
cristyf0460ee2010-03-06 02:55:11 +00001479}
1480
cristy50aea4a2010-03-09 17:37:44 +00001481static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image,
cristyf0460ee2010-03-06 02:55:11 +00001482 const MagickSizeType size)
1483{
1484 if (psd_info->version == 1)
cristy56ed31c2010-03-22 00:46:21 +00001485 return(WriteBlobMSBLong(image,(unsigned int) size));
cristyf0460ee2010-03-06 02:55:11 +00001486 return(WriteBlobMSBLongLong(image,size));
1487}
1488
cristy4aff1572010-02-15 13:34:01 +00001489static size_t PSDPackbitsEncodeImage(Image *image,const size_t length,
cristyb1459bc2010-03-23 21:41:43 +00001490 const unsigned char *pixels,unsigned char *compact_pixels)
cristy4aff1572010-02-15 13:34:01 +00001491{
1492 int
1493 count;
1494
1495 register long
1496 i,
1497 j;
1498
1499 register unsigned char
1500 *q;
1501
1502 unsigned char
1503 *packbits;
1504
1505 /*
1506 Compress pixels with Packbits encoding.
1507 */
1508 assert(image != (Image *) NULL);
1509 assert(image->signature == MagickSignature);
1510 if (image->debug != MagickFalse)
1511 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1512 assert(pixels != (unsigned char *) NULL);
1513 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1514 if (packbits == (unsigned char *) NULL)
1515 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1516 image->filename);
cristyb1459bc2010-03-23 21:41:43 +00001517 q=compact_pixels;
cristy4aff1572010-02-15 13:34:01 +00001518 for (i=(long) length; i != 0; )
1519 {
1520 switch (i)
1521 {
1522 case 1:
1523 {
1524 i--;
1525 *q++=(unsigned char) 0;
1526 *q++=(*pixels);
1527 break;
1528 }
1529 case 2:
1530 {
1531 i-=2;
1532 *q++=(unsigned char) 1;
1533 *q++=(*pixels);
1534 *q++=pixels[1];
1535 break;
1536 }
1537 case 3:
1538 {
1539 i-=3;
1540 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1541 {
1542 *q++=(unsigned char) ((256-3)+1);
1543 *q++=(*pixels);
1544 break;
1545 }
1546 *q++=(unsigned char) 2;
1547 *q++=(*pixels);
1548 *q++=pixels[1];
1549 *q++=pixels[2];
1550 break;
1551 }
1552 default:
1553 {
1554 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1555 {
1556 /*
1557 Packed run.
1558 */
1559 count=3;
1560 while (((long) count < i) && (*pixels == *(pixels+count)))
1561 {
1562 count++;
1563 if (count >= 127)
1564 break;
1565 }
1566 i-=count;
1567 *q++=(unsigned char) ((256-count)+1);
1568 *q++=(*pixels);
1569 pixels+=count;
1570 break;
1571 }
1572 /*
1573 Literal run.
1574 */
1575 count=0;
1576 while ((*(pixels+count) != *(pixels+count+1)) ||
1577 (*(pixels+count+1) != *(pixels+count+2)))
1578 {
1579 packbits[count+1]=pixels[count];
1580 count++;
1581 if (((long) count >= (i-3)) || (count >= 127))
1582 break;
1583 }
1584 i-=count;
1585 *packbits=(unsigned char) (count-1);
1586 for (j=0; j <= (long) count; j++)
1587 *q++=packbits[j];
1588 pixels+=count;
1589 break;
1590 }
1591 }
1592 }
1593 *q++=(unsigned char) 128; /* EOD marker */
1594 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
cristyb1459bc2010-03-23 21:41:43 +00001595 return((size_t) (q-compact_pixels));
cristy4aff1572010-02-15 13:34:01 +00001596}
1597
cristy875e28a2010-03-06 19:46:55 +00001598static void WritePackbitsLength(const PSDInfo *psd_info,
1599 const ImageInfo *image_info,Image *image,Image *tmp_image,
cristyb1459bc2010-03-23 21:41:43 +00001600 unsigned char *pixels,unsigned char *compact_pixels,
cristy4aff1572010-02-15 13:34:01 +00001601 const QuantumType quantum_type)
cristy3ed852e2009-09-05 21:47:34 +00001602{
1603 int
1604 y;
1605
1606 QuantumInfo
1607 *quantum_info;
1608
1609 register const PixelPacket
1610 *p;
1611
1612 size_t
cristy4aff1572010-02-15 13:34:01 +00001613 length,
cristy3ed852e2009-09-05 21:47:34 +00001614 packet_size;
1615
cristy3ed852e2009-09-05 21:47:34 +00001616 if (tmp_image->depth > 8)
1617 tmp_image->depth=16;
cristy3ed852e2009-09-05 21:47:34 +00001618 packet_size=tmp_image->depth > 8UL ? 2UL : 1UL;
cristy4aff1572010-02-15 13:34:01 +00001619 quantum_info=AcquireQuantumInfo(image_info,image);
cristy3ed852e2009-09-05 21:47:34 +00001620 for (y=0; y < (long) tmp_image->rows; y++)
1621 {
1622 p=GetVirtualPixels(tmp_image,0,y,tmp_image->columns,1,&image->exception);
1623 if (p == (const PixelPacket *) NULL)
cristy4aff1572010-02-15 13:34:01 +00001624 break;
1625 length=ExportQuantumPixels(tmp_image,(CacheView *) NULL,quantum_info,
cristy3ed852e2009-09-05 21:47:34 +00001626 quantum_type,pixels,&image->exception);
cristyb1459bc2010-03-23 21:41:43 +00001627 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels);
cristy875e28a2010-03-06 19:46:55 +00001628 (void) SetPSDOffset(psd_info,image,length);
cristy4aff1572010-02-15 13:34:01 +00001629 }
1630 quantum_info=DestroyQuantumInfo(quantum_info);
1631}
1632
cristy875e28a2010-03-06 19:46:55 +00001633static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info,
1634 Image *image,Image *tmp_image,unsigned char *pixels,
cristyb1459bc2010-03-23 21:41:43 +00001635 unsigned char *compact_pixels,const QuantumType quantum_type,
cristy875e28a2010-03-06 19:46:55 +00001636 const MagickBooleanType compression_flag)
cristy4aff1572010-02-15 13:34:01 +00001637{
1638 int
1639 y;
1640
cristy0910f242010-04-01 18:55:09 +00001641 MagickBooleanType
1642 monochrome;
1643
cristy4aff1572010-02-15 13:34:01 +00001644 QuantumInfo
1645 *quantum_info;
1646
1647 register const PixelPacket
1648 *p;
1649
cristy0910f242010-04-01 18:55:09 +00001650 register long
1651 i;
1652
cristy4aff1572010-02-15 13:34:01 +00001653 size_t
1654 length,
1655 packet_size;
1656
cristy50aea4a2010-03-09 17:37:44 +00001657 (void) psd_info;
cristy4aff1572010-02-15 13:34:01 +00001658 if ((compression_flag != MagickFalse) &&
cristyb1459bc2010-03-23 21:41:43 +00001659 (tmp_image->compression != RLECompression))
cristy4aff1572010-02-15 13:34:01 +00001660 (void) WriteBlobMSBShort(image,0);
1661 if (tmp_image->depth > 8)
1662 tmp_image->depth=16;
cristy0910f242010-04-01 18:55:09 +00001663 monochrome=IsMonochromeImage(image,&image->exception);
cristy4aff1572010-02-15 13:34:01 +00001664 packet_size=tmp_image->depth > 8UL ? 2UL : 1UL;
1665 quantum_info=AcquireQuantumInfo(image_info,image);
1666 for (y=0; y < (long) tmp_image->rows; y++)
1667 {
1668 p=GetVirtualPixels(tmp_image,0,y,tmp_image->columns,1,&image->exception);
1669 if (p == (const PixelPacket *) NULL)
1670 break;
1671 length=ExportQuantumPixels(tmp_image,(CacheView *) NULL,quantum_info,
1672 quantum_type,pixels,&image->exception);
cristy0910f242010-04-01 18:55:09 +00001673 if (monochrome != MagickFalse)
cristy735e8942010-04-02 20:32:57 +00001674 for (i=0; i < (long) length; i++)
cristy0910f242010-04-01 18:55:09 +00001675 pixels[i]=(~pixels[i]);
cristyb1459bc2010-03-23 21:41:43 +00001676 if (tmp_image->compression != RLECompression)
cristy4aff1572010-02-15 13:34:01 +00001677 (void) WriteBlob(image,length,pixels);
1678 else
1679 {
cristyb1459bc2010-03-23 21:41:43 +00001680 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels);
1681 (void) WriteBlob(image,length,compact_pixels);
cristy4aff1572010-02-15 13:34:01 +00001682 }
cristy3ed852e2009-09-05 21:47:34 +00001683 }
1684 quantum_info=DestroyQuantumInfo(quantum_info);
1685}
1686
cristy875e28a2010-03-06 19:46:55 +00001687static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info,
1688 const ImageInfo *image_info,Image *image,Image *tmp_image,
1689 const MagickBooleanType separate)
cristy3ed852e2009-09-05 21:47:34 +00001690{
1691 int
1692 i;
1693
1694 size_t
1695 channels,
1696 packet_size;
1697
1698 unsigned char
cristyb1459bc2010-03-23 21:41:43 +00001699 *compact_pixels,
cristy3ed852e2009-09-05 21:47:34 +00001700 *pixels;
1701
1702 /*
cristy875e28a2010-03-06 19:46:55 +00001703 Write uncompressed pixels as separate planes.
cristy3ed852e2009-09-05 21:47:34 +00001704 */
1705 channels=1;
cristy3ed852e2009-09-05 21:47:34 +00001706 packet_size=tmp_image->depth > 8UL ? 2UL : 1UL;
1707 pixels=(unsigned char *) AcquireQuantumMemory(channels*tmp_image->columns,
1708 packet_size*sizeof(*pixels));
cristyb1459bc2010-03-23 21:41:43 +00001709 compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels*
cristy4aff1572010-02-15 13:34:01 +00001710 tmp_image->columns,packet_size*sizeof(*pixels));
1711 if ((pixels == (unsigned char *) NULL) ||
cristyb1459bc2010-03-23 21:41:43 +00001712 (compact_pixels == (unsigned char *) NULL))
cristy4aff1572010-02-15 13:34:01 +00001713 {
1714 if (pixels != (unsigned char *) NULL)
1715 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
cristyb1459bc2010-03-23 21:41:43 +00001716 if (compact_pixels != (unsigned char *) NULL)
1717 compact_pixels=(unsigned char *)
1718 RelinquishMagickMemory(compact_pixels);
cristy4aff1572010-02-15 13:34:01 +00001719 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1720 }
cristy3ed852e2009-09-05 21:47:34 +00001721 i=0;
cristy2045da32010-04-16 00:59:35 +00001722 if (IsGrayImage(tmp_image,&tmp_image->exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001723 {
cristyb1459bc2010-03-23 21:41:43 +00001724 if (tmp_image->compression == RLECompression)
cristy4aff1572010-02-15 13:34:01 +00001725 {
1726 /*
1727 Packbits compression.
1728 */
1729 (void) WriteBlobMSBShort(image,1);
cristy4aff1572010-02-15 13:34:01 +00001730 if (tmp_image->matte != MagickFalse)
cristy875e28a2010-03-06 19:46:55 +00001731 WritePackbitsLength(psd_info,image_info,image,tmp_image,pixels,
cristyb1459bc2010-03-23 21:41:43 +00001732 compact_pixels,AlphaQuantum);
cristy875e28a2010-03-06 19:46:55 +00001733 WritePackbitsLength(psd_info,image_info,image,tmp_image,pixels,
cristy6886a752010-04-23 18:23:20 +00001734 compact_pixels,GrayQuantum);
cristy4aff1572010-02-15 13:34:01 +00001735 }
cristy6886a752010-04-23 18:23:20 +00001736 if (tmp_image->matte != MagickFalse)
1737 WriteOneChannel(psd_info,image_info,image,tmp_image,pixels,
1738 compact_pixels,AlphaQuantum,(i++ == 0) ||
1739 (separate != MagickFalse) ? MagickTrue : MagickFalse);
cristy875e28a2010-03-06 19:46:55 +00001740 WriteOneChannel(psd_info,image_info,image,tmp_image,pixels,
cristy6886a752010-04-23 18:23:20 +00001741 compact_pixels,GrayQuantum,(i++ == 0) ||
1742 (separate != MagickFalse) ? MagickTrue : MagickFalse);
1743 (void) SetImageProgress(image,SaveImagesTag,0,1);
cristy3ed852e2009-09-05 21:47:34 +00001744 }
cristy0910f242010-04-01 18:55:09 +00001745 else
1746 if (tmp_image->storage_class == PseudoClass)
1747 {
1748 if (tmp_image->compression == RLECompression)
1749 {
1750 /*
1751 Packbits compression.
1752 */
1753 (void) WriteBlobMSBShort(image,1);
cristy6886a752010-04-23 18:23:20 +00001754 if (tmp_image->matte != MagickFalse)
cristy0910f242010-04-01 18:55:09 +00001755 WritePackbitsLength(psd_info,image_info,image,tmp_image,pixels,
cristy6886a752010-04-23 18:23:20 +00001756 compact_pixels,AlphaQuantum);
1757 WritePackbitsLength(psd_info,image_info,image,tmp_image,pixels,
1758 compact_pixels,IndexQuantum);
cristy0910f242010-04-01 18:55:09 +00001759 }
cristy6886a752010-04-23 18:23:20 +00001760 if (tmp_image->matte != MagickFalse)
cristy0910f242010-04-01 18:55:09 +00001761 WriteOneChannel(psd_info,image_info,image,tmp_image,pixels,
cristy6886a752010-04-23 18:23:20 +00001762 compact_pixels,AlphaQuantum,(i++ == 0) ||
cristy0910f242010-04-01 18:55:09 +00001763 (separate != MagickFalse) ? MagickTrue : MagickFalse);
cristy6886a752010-04-23 18:23:20 +00001764 WriteOneChannel(psd_info,image_info,image,tmp_image,pixels,
1765 compact_pixels,IndexQuantum,(i++ == 0) ||
1766 (separate != MagickFalse) ? MagickTrue : MagickFalse);
cristy0910f242010-04-01 18:55:09 +00001767 (void) SetImageProgress(image,SaveImagesTag,0,1);
1768 }
1769 else
1770 {
1771 if (tmp_image->colorspace == CMYKColorspace)
cristycdb717b2010-04-08 16:00:57 +00001772 {
1773 tmp_image->compression=NoCompression;
1774 (void) NegateImage(tmp_image,MagickFalse);
1775 }
cristy0910f242010-04-01 18:55:09 +00001776 if (tmp_image->compression == RLECompression)
1777 {
1778 /*
1779 Packbits compression.
1780 */
1781 (void) WriteBlobMSBShort(image,1);
1782 if (tmp_image->matte != MagickFalse)
1783 WritePackbitsLength(psd_info,image_info,image,tmp_image,pixels,
1784 compact_pixels,AlphaQuantum);
1785 WritePackbitsLength(psd_info,image_info,image,tmp_image,pixels,
1786 compact_pixels,RedQuantum);
1787 WritePackbitsLength(psd_info,image_info,image,tmp_image,pixels,
1788 compact_pixels,GreenQuantum);
1789 WritePackbitsLength(psd_info,image_info,image,tmp_image,pixels,
1790 compact_pixels,BlueQuantum);
1791 if (tmp_image->colorspace == CMYKColorspace)
1792 WritePackbitsLength(psd_info,image_info,image,tmp_image,pixels,
1793 compact_pixels,BlackQuantum);
1794 }
1795 (void) SetImageProgress(image,SaveImagesTag,0,6);
1796 if (tmp_image->matte != MagickFalse)
1797 WriteOneChannel(psd_info,image_info,image,tmp_image,pixels,
1798 compact_pixels,AlphaQuantum,(i++ == 0) ||
1799 (separate != MagickFalse) ? MagickTrue : MagickFalse);
1800 (void) SetImageProgress(image,SaveImagesTag,1,6);
1801 WriteOneChannel(psd_info,image_info,image,tmp_image,pixels,
1802 compact_pixels,RedQuantum,(i++ == 0) || (separate != MagickFalse) ?
1803 MagickTrue : MagickFalse);
1804 (void) SetImageProgress(image,SaveImagesTag,2,6);
1805 WriteOneChannel(psd_info,image_info,image,tmp_image,pixels,
1806 compact_pixels,GreenQuantum,(i++ == 0) || (separate != MagickFalse) ?
1807 MagickTrue : MagickFalse);
1808 (void) SetImageProgress(image,SaveImagesTag,3,6);
1809 WriteOneChannel(psd_info,image_info,image,tmp_image,pixels,
1810 compact_pixels,BlueQuantum,(i++ == 0) || (separate != MagickFalse) ?
1811 MagickTrue : MagickFalse);
1812 (void) SetImageProgress(image,SaveImagesTag,4,6);
1813 if (tmp_image->colorspace == CMYKColorspace)
1814 {
1815 WriteOneChannel(psd_info,image_info,image,tmp_image,pixels,
1816 compact_pixels,BlackQuantum,(i++ == 0) ||
1817 (separate != MagickFalse) ? MagickTrue : MagickFalse);
cristycdb717b2010-04-08 16:00:57 +00001818 (void) NegateImage(tmp_image,MagickFalse);
cristy0910f242010-04-01 18:55:09 +00001819 }
1820 (void) SetImageProgress(image,SaveImagesTag,5,6);
1821 }
cristy3ed852e2009-09-05 21:47:34 +00001822 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1823 return(MagickTrue);
1824}
1825
cristy3ed852e2009-09-05 21:47:34 +00001826static void WritePascalString(Image* inImage,const char *inString,int inPad)
1827{
1828 size_t
1829 strLength;
1830 int i;
1831
1832 /* max length is 255 */
1833
1834 strLength = (strlen(inString) > 255UL ) ? 255UL : strlen(inString);
1835
1836 if ( strLength != 0 )
1837 {
1838 (void) WriteBlobByte(inImage,(unsigned char) strLength);
1839 (void) WriteBlob(inImage, strLength, (const unsigned char *) inString);
1840 }
1841 else
1842 (void) WriteBlobByte(inImage, 0);
1843
1844 strLength ++;
1845
1846 if ( (strLength % inPad) == 0 )
1847 return;
1848 for (i=0; i < (long) (inPad-(strLength % inPad)); i++)
1849 (void) WriteBlobByte(inImage,0);
1850}
1851
1852static void WriteResolutionResourceBlock(Image *image)
1853{
cristy56ed31c2010-03-22 00:46:21 +00001854 double
1855 x_resolution,
1856 y_resolution;
cristy3ed852e2009-09-05 21:47:34 +00001857
1858 unsigned short
1859 units;
1860
1861 x_resolution=65536.0*image->x_resolution+0.5;
1862 y_resolution=65536.0*image->y_resolution+0.5;
1863 units=1;
1864 if (image->units == PixelsPerCentimeterResolution)
1865 {
1866 x_resolution=2.54*65536.0*image->x_resolution*0.5;
1867 y_resolution=2.54*65536.0*image->y_resolution+0.5;
1868 units=2;
1869 }
1870 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
1871 (void) WriteBlobMSBShort(image,0x03ED);
1872 (void) WriteBlobMSBShort(image,0);
1873 (void) WriteBlobMSBLong(image,16); /* resource size */
cristy56ed31c2010-03-22 00:46:21 +00001874 (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001875 (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */
1876 (void) WriteBlobMSBShort(image,units); /* width unit */
cristy56ed31c2010-03-22 00:46:21 +00001877 (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001878 (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */
1879 (void) WriteBlobMSBShort(image,units); /* height unit */
1880}
1881
cristyd4d3f742010-04-25 20:36:50 +00001882static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile)
1883{
cristy0b796e62010-04-29 00:38:45 +00001884 register const unsigned char
cristyd4d3f742010-04-25 20:36:50 +00001885 *p;
1886
1887 size_t
1888 length;
1889
1890 unsigned char
1891 *datum;
1892
1893 unsigned long
1894 count,
1895 long_sans;
1896
1897 unsigned short
1898 id,
1899 short_sans;
1900
1901 length=GetStringInfoLength(bim_profile);
1902 if (length < 16)
cristy0b796e62010-04-29 00:38:45 +00001903 return;
cristyd4d3f742010-04-25 20:36:50 +00001904 datum=GetStringInfoDatum(bim_profile);
1905 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
1906 {
cristy4176bb22010-05-01 16:29:09 +00001907 register unsigned char
1908 *q;
1909
1910 q=(unsigned char *) p;
cristyd4d3f742010-04-25 20:36:50 +00001911 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
1912 break;
1913 p=PushLongPixel(MSBEndian,p,&long_sans);
1914 p=PushShortPixel(MSBEndian,p,&id);
1915 p=PushShortPixel(MSBEndian,p,&short_sans);
1916 p=PushLongPixel(MSBEndian,p,&count);
1917 if (id == 0x0000040f)
1918 {
cristy4176bb22010-05-01 16:29:09 +00001919 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
1920 (PSDQuantum(count)+12)-(q-datum));
1921 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
cristyd4d3f742010-04-25 20:36:50 +00001922 break;
1923 }
1924 p+=count;
1925 if ((count & 0x01) != 0)
1926 p++;
1927 }
1928}
1929
cristyf11065e2010-05-14 13:26:59 +00001930static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile)
1931{
1932 register const unsigned char
1933 *p;
1934
1935 size_t
1936 length;
1937
1938 unsigned char
1939 *datum;
1940
1941 unsigned long
1942 count,
1943 long_sans;
1944
1945 unsigned short
1946 id,
1947 short_sans;
1948
1949 length=GetStringInfoLength(bim_profile);
1950 if (length < 16)
1951 return;
1952 datum=GetStringInfoDatum(bim_profile);
1953 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
1954 {
1955 register unsigned char
1956 *q;
1957
1958 q=(unsigned char *) p;
1959 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
1960 break;
1961 p=PushLongPixel(MSBEndian,p,&long_sans);
1962 p=PushShortPixel(MSBEndian,p,&id);
1963 p=PushShortPixel(MSBEndian,p,&short_sans);
1964 p=PushLongPixel(MSBEndian,p,&count);
1965 if (id == 0x000003ed)
1966 {
1967 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
1968 (PSDQuantum(count)+12)-(q-datum));
1969 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
1970 break;
1971 }
1972 p+=count;
1973 if ((count & 0x01) != 0)
1974 p++;
1975 }
1976}
1977
cristy3ed852e2009-09-05 21:47:34 +00001978static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image)
1979{
1980 const char
1981 *theAttr;
1982
1983 const StringInfo
cristy749d2152010-04-04 23:47:33 +00001984 *icc_profile;
cristy3ed852e2009-09-05 21:47:34 +00001985
1986 MagickBooleanType
cristy144f1b62010-05-18 00:52:09 +00001987 invert_layer_count,
cristy3ed852e2009-09-05 21:47:34 +00001988 status;
1989
cristy875e28a2010-03-06 19:46:55 +00001990 PSDInfo
1991 psd_info;
1992
cristy3ed852e2009-09-05 21:47:34 +00001993 register long
1994 i;
1995
1996 size_t
cristy749d2152010-04-04 23:47:33 +00001997 length,
cristy3ed852e2009-09-05 21:47:34 +00001998 num_channels,
1999 packet_size;
2000
cristy0b796e62010-04-29 00:38:45 +00002001 StringInfo
2002 *bim_profile;
2003
cristy3ed852e2009-09-05 21:47:34 +00002004 unsigned char
2005 layer_name[4];
2006
2007 unsigned long
2008 channel_size,
2009 channelLength,
2010 layer_count,
2011 layer_info_size,
cristy749d2152010-04-04 23:47:33 +00002012 rounded_layer_info_size;
cristy3ed852e2009-09-05 21:47:34 +00002013
2014 Image
cristy0b796e62010-04-29 00:38:45 +00002015 *tmp_image = (Image *) NULL,
2016 *base_image = GetNextImageInList(image);
cristy4aff1572010-02-15 13:34:01 +00002017
cristy3ed852e2009-09-05 21:47:34 +00002018 /*
cristy56ed31c2010-03-22 00:46:21 +00002019 Open image file.
cristy3ed852e2009-09-05 21:47:34 +00002020 */
2021 assert(image_info != (const ImageInfo *) NULL);
2022 assert(image_info->signature == MagickSignature);
2023 assert(image != (Image *) NULL);
2024 assert(image->signature == MagickSignature);
2025 if (image->debug != MagickFalse)
2026 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2027 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2028 if (status == MagickFalse)
2029 return(status);
cristy144f1b62010-05-18 00:52:09 +00002030 invert_layer_count=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00002031 packet_size=(size_t) (image->depth > 8 ? 6 : 3);
2032 if (image->matte != MagickFalse)
2033 packet_size+=image->depth > 8 ? 2 : 1;
cristy875e28a2010-03-06 19:46:55 +00002034 psd_info.version=1;
2035 if ((LocaleCompare(image_info->magick,"PSB") == 0) ||
2036 (image->columns > 30000) || (image->rows > 30000))
2037 psd_info.version=2;
cristy50aea4a2010-03-09 17:37:44 +00002038 (void) WriteBlob(image,4,(const unsigned char *) "8BPS");
cristy875e28a2010-03-06 19:46:55 +00002039 (void) WriteBlobMSBShort(image,psd_info.version); /* version */
2040 for (i=1; i <= 6; i++)
2041 (void) WriteBlobByte(image, 0); /* 6 bytes of reserved */
cristy2045da32010-04-16 00:59:35 +00002042 if (IsGrayImage(image,&image->exception) != MagickFalse)
cristy6886a752010-04-23 18:23:20 +00002043 num_channels=(image->matte != MagickFalse ? 2UL : 1UL);
cristy3ed852e2009-09-05 21:47:34 +00002044 else
cristy0910f242010-04-01 18:55:09 +00002045 if (image->storage_class == PseudoClass)
cristy6886a752010-04-23 18:23:20 +00002046 num_channels=(image->matte != MagickFalse ? 2UL : 1UL);
cristy0910f242010-04-01 18:55:09 +00002047 else
2048 {
2049 if (image->colorspace != CMYKColorspace)
cristy6886a752010-04-23 18:23:20 +00002050 num_channels=(image->matte != MagickFalse ? 4UL : 3UL);
cristy0910f242010-04-01 18:55:09 +00002051 else
cristy6886a752010-04-23 18:23:20 +00002052 num_channels=(image->matte != MagickFalse ? 5UL : 4UL);
cristy0910f242010-04-01 18:55:09 +00002053 }
cristy3ed852e2009-09-05 21:47:34 +00002054 (void) WriteBlobMSBShort(image,(unsigned short) num_channels);
cristy56ed31c2010-03-22 00:46:21 +00002055 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
2056 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
cristy2045da32010-04-16 00:59:35 +00002057 if (IsGrayImage(image,&image->exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002058 {
cristy2045da32010-04-16 00:59:35 +00002059 MagickBooleanType
2060 monochrome;
2061
cristy0910f242010-04-01 18:55:09 +00002062 /*
2063 Write depth & mode.
2064 */
cristy2045da32010-04-16 00:59:35 +00002065 monochrome=IsMonochromeImage(image,&image->exception);
cristy284c7d82010-04-24 00:19:14 +00002066 (void) WriteBlobMSBShort(image,(unsigned short)
2067 (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8));
cristy2045da32010-04-16 00:59:35 +00002068 (void) WriteBlobMSBShort(image,monochrome != MagickFalse ?
2069 BitmapMode : GrayscaleMode);
cristy3ed852e2009-09-05 21:47:34 +00002070 }
2071 else
2072 {
cristy0910f242010-04-01 18:55:09 +00002073 (void) WriteBlobMSBShort(image,(unsigned short)
2074 (image->storage_class == PseudoClass ? 8 : image->depth > 8 ? 16 : 8));
2075 if (((image->colorspace != UndefinedColorspace) ||
2076 (image->colorspace != CMYKColorspace)) &&
2077 (image->colorspace != CMYKColorspace))
2078 {
2079 if (image->colorspace != RGBColorspace)
2080 (void) TransformImageColorspace(image,RGBColorspace);
2081 (void) WriteBlobMSBShort(image,(unsigned short)
cristy2045da32010-04-16 00:59:35 +00002082 (image->storage_class == PseudoClass ? IndexedMode : RGBMode));
cristy0910f242010-04-01 18:55:09 +00002083 }
2084 else
2085 {
2086 if (image->colorspace != RGBColorspace)
2087 (void) TransformImageColorspace(image,CMYKColorspace);
cristy2045da32010-04-16 00:59:35 +00002088 (void) WriteBlobMSBShort(image,CMYKMode);
cristy0910f242010-04-01 18:55:09 +00002089 }
cristy3ed852e2009-09-05 21:47:34 +00002090 }
cristyca9ddb02010-04-16 01:01:18 +00002091 if ((IsGrayImage(image,&image->exception) != MagickFalse) ||
2092 (image->storage_class == DirectClass) || (image->colors > 256))
cristy3ed852e2009-09-05 21:47:34 +00002093 (void) WriteBlobMSBLong(image,0);
2094 else
2095 {
2096 /*
2097 Write PSD raster colormap.
2098 */
2099 (void) WriteBlobMSBLong(image,768);
2100 for (i=0; i < (long) image->colors; i++)
2101 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red));
2102 for ( ; i < 256; i++)
2103 (void) WriteBlobByte(image,0);
2104 for (i=0; i < (long) image->colors; i++)
2105 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].green));
2106 for ( ; i < 256; i++)
2107 (void) WriteBlobByte(image,0);
2108 for (i=0; i < (long) image->colors; i++)
2109 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue));
2110 for ( ; i < 256; i++)
2111 (void) WriteBlobByte(image,0);
2112 }
2113 /*
2114 Image resource block.
2115 */
cristy749d2152010-04-04 23:47:33 +00002116 length=28; /* 0x03EB */
cristy0b796e62010-04-29 00:38:45 +00002117 bim_profile=(StringInfo *) GetImageProfile(image,"8bim");
cristy749d2152010-04-04 23:47:33 +00002118 icc_profile=GetImageProfile(image,"icc");
cristyd4d3f742010-04-25 20:36:50 +00002119 if (bim_profile != (StringInfo *) NULL)
2120 {
cristy0b796e62010-04-29 00:38:45 +00002121 bim_profile=CloneStringInfo(bim_profile);
cristyd4d3f742010-04-25 20:36:50 +00002122 if (icc_profile != (StringInfo *) NULL)
2123 RemoveICCProfileFromResourceBlock(bim_profile);
cristyf11065e2010-05-14 13:26:59 +00002124 RemoveResolutionFromResourceBlock(bim_profile);
cristyd4d3f742010-04-25 20:36:50 +00002125 length+=PSDQuantum(GetStringInfoLength(bim_profile));
2126 }
cristy0b796e62010-04-29 00:38:45 +00002127 if (icc_profile != (const StringInfo *) NULL)
cristy749d2152010-04-04 23:47:33 +00002128 length+=PSDQuantum(GetStringInfoLength(icc_profile))+12;
cristy284c7d82010-04-24 00:19:14 +00002129 (void) WriteBlobMSBLong(image,(unsigned int) length);
cristyf11065e2010-05-14 13:26:59 +00002130 WriteResolutionResourceBlock(image);
cristy4176bb22010-05-01 16:29:09 +00002131 if (bim_profile != (StringInfo *) NULL)
2132 {
2133 (void) WriteBlob(image,GetStringInfoLength(bim_profile),
2134 GetStringInfoDatum(bim_profile));
2135 bim_profile=DestroyStringInfo(bim_profile);
2136 }
cristy749d2152010-04-04 23:47:33 +00002137 if (icc_profile != (StringInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002138 {
cristy749d2152010-04-04 23:47:33 +00002139 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
cristy4176bb22010-05-01 16:29:09 +00002140 (void) WriteBlobMSBShort(image,0x0000040F);
cristy749d2152010-04-04 23:47:33 +00002141 (void) WriteBlobMSBShort(image,0);
cristy284c7d82010-04-24 00:19:14 +00002142 (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength(
2143 icc_profile));
cristy749d2152010-04-04 23:47:33 +00002144 (void) WriteBlob(image,GetStringInfoLength(icc_profile),
2145 GetStringInfoDatum(icc_profile));
cristye195f262010-04-16 18:12:35 +00002146 if ((MagickOffsetType) GetStringInfoLength(icc_profile) !=
cristy2045da32010-04-16 00:59:35 +00002147 PSDQuantum(GetStringInfoLength(icc_profile)))
cristy749d2152010-04-04 23:47:33 +00002148 (void) WriteBlobByte(image,0);
cristye6365592010-04-02 17:31:23 +00002149 }
cristy3ed852e2009-09-05 21:47:34 +00002150
2151compute_layer_info:
2152 layer_count = 0;
2153 layer_info_size = 2;
2154 tmp_image = base_image;
2155 while ( tmp_image != NULL ) {
2156 packet_size=tmp_image->depth > 8 ? 2UL : 1UL;
2157
cristy2045da32010-04-16 00:59:35 +00002158 if (IsGrayImage(image,&image->exception) != MagickFalse)
cristy6886a752010-04-23 18:23:20 +00002159 num_channels=tmp_image->matte != MagickFalse ? 2UL : 1UL;
cristy3ed852e2009-09-05 21:47:34 +00002160 else
cristy2045da32010-04-16 00:59:35 +00002161 if (tmp_image->storage_class == PseudoClass)
cristy6886a752010-04-23 18:23:20 +00002162 num_channels=tmp_image->matte != MagickFalse ? 2UL : 1UL;
cristy2045da32010-04-16 00:59:35 +00002163 else
2164 if (tmp_image->colorspace != CMYKColorspace)
cristy6886a752010-04-23 18:23:20 +00002165 num_channels=tmp_image->matte != MagickFalse ? 4UL : 3UL;
cristy2045da32010-04-16 00:59:35 +00002166 else
cristy6886a752010-04-23 18:23:20 +00002167 num_channels=tmp_image->matte != MagickFalse ? 5UL : 4UL;
cristy3ed852e2009-09-05 21:47:34 +00002168
cristy4aff1572010-02-15 13:34:01 +00002169 channelLength=(unsigned long) (tmp_image->columns * tmp_image->rows *
2170 packet_size + 2);
cristy3f938c62010-03-07 00:53:52 +00002171 layer_info_size += (unsigned long) (4*4 + 2 + num_channels * 6 +
cristyde9b8f52010-03-12 01:17:00 +00002172 (psd_info.version == 1 ? 8 : 16) + 4 * 1 + 4 + num_channels *
cristy3f938c62010-03-07 00:53:52 +00002173 channelLength);
cristyde9b8f52010-03-12 01:17:00 +00002174 theAttr=(const char *) GetImageProperty(tmp_image,"label");
cristy56ed31c2010-03-22 00:46:21 +00002175 if (!theAttr)
cristyde9b8f52010-03-12 01:17:00 +00002176 layer_info_size += 16;
2177 else
2178 {
cristydfbe6ca2010-03-12 14:08:17 +00002179 size_t length=strlen(theAttr);
2180 layer_info_size += 8+length+(4-(length % 4))+1;
cristyde9b8f52010-03-12 01:17:00 +00002181 }
cristy4aff1572010-02-15 13:34:01 +00002182 layer_count++;
2183 tmp_image = GetNextImageInList(tmp_image);
cristy3ed852e2009-09-05 21:47:34 +00002184 }
cristy144f1b62010-05-18 00:52:09 +00002185 if ((layer_count == 0) && (image->matte == MagickTrue))
2186 {
2187 /*
2188 Image with matte channel requires layers.
2189 */
2190 invert_layer_count=MagickTrue;
2191 image->matte=MagickFalse;
2192 base_image=image;
2193 goto compute_layer_info;
2194 }
2195 if (layer_count == 0)
cristy875e28a2010-03-06 19:46:55 +00002196 (void) SetPSDSize(&psd_info,image,0);
cristy3ed852e2009-09-05 21:47:34 +00002197 else
2198 {
cristy3f938c62010-03-07 00:53:52 +00002199 (void) SetPSDSize(&psd_info,image,layer_info_size+
2200 (psd_info.version == 1 ? 8 : 16));
cristy3ed852e2009-09-05 21:47:34 +00002201 if ( layer_info_size/2 != (layer_info_size+1)/2 ) /* odd */
2202 rounded_layer_info_size = layer_info_size + 1;
2203 else
2204 rounded_layer_info_size = layer_info_size;
cristy3f938c62010-03-07 00:53:52 +00002205 (void) SetPSDSize(&psd_info,image,rounded_layer_info_size);
cristy144f1b62010-05-18 00:52:09 +00002206 if (invert_layer_count != MagickFalse)
2207 layer_count*=(-1); /* if we have a matte, then use negative count! */
cristy3ed852e2009-09-05 21:47:34 +00002208 (void) WriteBlobMSBShort(image,(unsigned short) layer_count);
cristy144f1b62010-05-18 00:52:09 +00002209 layer_count=1;
cristy3ed852e2009-09-05 21:47:34 +00002210 tmp_image = base_image;
cristy9174bfc2010-03-07 01:44:22 +00002211 tmp_image->compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002212 while ( tmp_image != NULL ) {
2213 (void) WriteBlobMSBLong(image,0);
2214 (void) WriteBlobMSBLong(image,0);
cristy56ed31c2010-03-22 00:46:21 +00002215 (void) WriteBlobMSBLong(image,(unsigned int) tmp_image->rows);
2216 (void) WriteBlobMSBLong(image,(unsigned int) tmp_image->columns);
cristy3ed852e2009-09-05 21:47:34 +00002217
2218 packet_size=tmp_image->depth > 8 ? 2UL : 1UL;
cristy4aff1572010-02-15 13:34:01 +00002219 channel_size=(unsigned int) ((packet_size*tmp_image->rows*
2220 tmp_image->columns)+2);
cristy6886a752010-04-23 18:23:20 +00002221 if ((IsGrayImage(tmp_image,&image->exception) != MagickFalse) ||
2222 (tmp_image->storage_class == PseudoClass)) {
cristy3ed852e2009-09-05 21:47:34 +00002223 (void) WriteBlobMSBShort(image,(unsigned short)
cristy6886a752010-04-23 18:23:20 +00002224 (tmp_image->matte != MagickFalse ? 2 : 1));
2225 if (tmp_image->matte != MagickFalse) {
cristy3ed852e2009-09-05 21:47:34 +00002226 (void) WriteBlobMSBShort(image,(unsigned short) -1);
cristy3f938c62010-03-07 00:53:52 +00002227 (void) SetPSDSize(&psd_info,image,channel_size);
cristy3ed852e2009-09-05 21:47:34 +00002228 }
2229 (void) WriteBlobMSBShort(image, 0);
cristy3f938c62010-03-07 00:53:52 +00002230 (void) SetPSDSize(&psd_info,image,channel_size);
cristy3ed852e2009-09-05 21:47:34 +00002231 } else
2232 if (tmp_image->colorspace != CMYKColorspace)
2233 {
2234 (void) WriteBlobMSBShort(image,(unsigned short)
cristy6886a752010-04-23 18:23:20 +00002235 (tmp_image->matte != MagickFalse ? 4 : 3));
2236 if (tmp_image->matte!= MagickFalse ) {
cristy3ed852e2009-09-05 21:47:34 +00002237 (void) WriteBlobMSBShort(image,(unsigned short) -1);
cristy3f938c62010-03-07 00:53:52 +00002238 (void) SetPSDSize(&psd_info,image,channel_size);
cristy3ed852e2009-09-05 21:47:34 +00002239 }
2240 (void) WriteBlobMSBShort(image, 0);
cristy3f938c62010-03-07 00:53:52 +00002241 (void) SetPSDSize(&psd_info,image,channel_size);
cristy3ed852e2009-09-05 21:47:34 +00002242 (void) WriteBlobMSBShort(image, 1);
cristy3f938c62010-03-07 00:53:52 +00002243 (void) SetPSDSize(&psd_info,image,channel_size);
cristy3ed852e2009-09-05 21:47:34 +00002244 (void) WriteBlobMSBShort(image, 2);
cristy3f938c62010-03-07 00:53:52 +00002245 (void) SetPSDSize(&psd_info,image,channel_size);
cristy3ed852e2009-09-05 21:47:34 +00002246 }
2247 else
2248 {
2249 (void) WriteBlobMSBShort(image,(unsigned short)
2250 (tmp_image->matte ? 5 : 4));
2251 if (tmp_image->matte) {
2252 (void) WriteBlobMSBShort(image,(unsigned short) -1);
cristy3f938c62010-03-07 00:53:52 +00002253 (void) SetPSDSize(&psd_info,image,channel_size);
cristy3ed852e2009-09-05 21:47:34 +00002254 }
2255 (void) WriteBlobMSBShort(image, 0);
cristy3f938c62010-03-07 00:53:52 +00002256 (void) SetPSDSize(&psd_info,image,channel_size);
cristy3ed852e2009-09-05 21:47:34 +00002257 (void) WriteBlobMSBShort(image, 1);
cristy3f938c62010-03-07 00:53:52 +00002258 (void) SetPSDSize(&psd_info,image,channel_size);
cristy3ed852e2009-09-05 21:47:34 +00002259 (void) WriteBlobMSBShort(image, 2);
cristy3f938c62010-03-07 00:53:52 +00002260 (void) SetPSDSize(&psd_info,image,channel_size);
cristy3ed852e2009-09-05 21:47:34 +00002261 (void) WriteBlobMSBShort(image, 3);
cristy3f938c62010-03-07 00:53:52 +00002262 (void) SetPSDSize(&psd_info,image,channel_size);
cristy3ed852e2009-09-05 21:47:34 +00002263 }
2264
cristy4aff1572010-02-15 13:34:01 +00002265 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2266 (void) WriteBlob(image,4,(const unsigned char *)
2267 CompositeOperatorToPSDBlendMode(tmp_image->compose));
2268 (void) WriteBlobByte(image, 255); /* BOGUS: layer opacity */
cristy3ed852e2009-09-05 21:47:34 +00002269 (void) WriteBlobByte(image, 0);
cristy4aff1572010-02-15 13:34:01 +00002270 (void) WriteBlobByte(image, 1); /* BOGUS: layer attributes - visible, etc. */
cristy3ed852e2009-09-05 21:47:34 +00002271 (void) WriteBlobByte(image, 0);
2272
cristy3ed852e2009-09-05 21:47:34 +00002273
2274 theAttr=(const char *) GetImageProperty(tmp_image,"label");
cristyde9b8f52010-03-12 01:17:00 +00002275 if (!theAttr) {
2276 (void) WriteBlobMSBLong(image, 16);
2277 (void) WriteBlobMSBLong(image, 0);
2278 (void) WriteBlobMSBLong(image, 0);
2279 (void) FormatMagickString((char *) layer_name,MaxTextExtent,"L%06ld",
cristy3ed852e2009-09-05 21:47:34 +00002280 layer_count++ );
2281 WritePascalString( image, (char*)layer_name, 4 );
cristyde9b8f52010-03-12 01:17:00 +00002282 } else {
cristydfbe6ca2010-03-12 14:08:17 +00002283 size_t length=strlen(theAttr);
cristy56ed31c2010-03-22 00:46:21 +00002284 (void) WriteBlobMSBLong(image,(unsigned int) (length+(4-(length % 4))+
2285 1+8));
2286 (void) WriteBlobMSBLong(image,0);
2287 (void) WriteBlobMSBLong(image,0);
cristyde9b8f52010-03-12 01:17:00 +00002288 WritePascalString( image, theAttr, 4 );
cristy3ed852e2009-09-05 21:47:34 +00002289 }
2290 tmp_image = GetNextImageInList(tmp_image);
cristy144f1b62010-05-18 00:52:09 +00002291 }
2292 /* now the image data! */
cristy3ed852e2009-09-05 21:47:34 +00002293 tmp_image = base_image;
2294 while ( tmp_image != NULL ) {
cristy875e28a2010-03-06 19:46:55 +00002295 status=WriteImageChannels(&psd_info,image_info,image,tmp_image,MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +00002296 /* add in the pad! */
2297 if ( rounded_layer_info_size != layer_info_size )
2298 (void) WriteBlobByte(image,'\0');
2299
2300 tmp_image = GetNextImageInList(tmp_image);
2301 };
cristy3ed852e2009-09-05 21:47:34 +00002302 /* user mask data */
cristy144f1b62010-05-18 00:52:09 +00002303 (void) WriteBlobMSBLong(image,0);
cristy3ed852e2009-09-05 21:47:34 +00002304 }
cristy144f1b62010-05-18 00:52:09 +00002305 /*
2306 Write composite image.
2307 */
2308 if (invert_layer_count != MagickFalse)
2309 image->matte=MagickTrue;
cristy15e25cb2010-03-13 20:31:29 +00002310 status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00002311 (void) CloseBlob(image);
2312 return(status);
2313}