blob: 211d429741772ad5ebec3f1e452d6123c336c586 [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
cristybb503372010-05-27 20:51:26 +000072#define PSDQuantum(x) (((ssize_t) (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
cristybb503372010-05-27 20:51:26 +000097 size_t
cristy2d3d87f2010-03-01 00:23:08 +000098 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
cristybb503372010-05-27 20:51:26 +0000124 size_t
cristy2d3d87f2010-03-01 00:23:08 +0000125 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
cristybb503372010-05-27 20:51:26 +0000147 size_t
cristy2d3d87f2010-03-01 00:23:08 +0000148 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,
cristybb503372010-05-27 20:51:26 +0000249 const unsigned char *compact_pixels,const ssize_t depth,
cristycd081772010-03-22 17:19:12 +0000250 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
cristybb503372010-05-27 20:51:26 +0000381static inline ssize_t MagickAbsoluteValue(const ssize_t x)
cristy3ed852e2009-09-05 21:47:34 +0000382{
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
cristy6befb0f2010-05-31 14:33:15 +0000413 unsigned int
cristy3ed852e2009-09-05 21:47:34 +0000414 count,
cristy6befb0f2010-05-31 14:33:15 +0000415 long_sans;
cristy3ed852e2009-09-05 21:47:34 +0000416
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;
cristy6befb0f2010-05-31 14:33:15 +0000431 p=PushLongPixel(MSBEndian,p,&long_sans);
cristyf11065e2010-05-14 13:26:59 +0000432 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
cristybb503372010-05-27 20:51:26 +0000517static MagickBooleanType ReadPSDLayer(Image *image,const size_t channels,
518 const ssize_t type,const MagickOffsetType *offsets,ExceptionInfo *exception)
cristy56ed31c2010-03-22 00:46:21 +0000519{
cristyd05dca12010-07-25 02:32:32 +0000520 ColorspaceType
521 colorspace;
cristy56ed31c2010-03-22 00:46:21 +0000522
523 Quantum
524 pixel;
525
526 register IndexPacket
527 *indexes;
528
cristybb503372010-05-27 20:51:26 +0000529 register ssize_t
cristy56ed31c2010-03-22 00:46:21 +0000530 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
cristyd05dca12010-07-25 02:32:32 +0000542 count,
543 y;
cristy56ed31c2010-03-22 00:46:21 +0000544
545 unsigned char
546 *compact_pixels,
547 *pixels;
548
549 unsigned short
550 nibble;
551
552 packet_size=1;
553 if (image->storage_class == PseudoClass)
554 {
555 if (image->colors > 256)
556 packet_size++;
557 else
558 if (image->depth > 8)
559 packet_size++;
560 }
561 else
562 if (image->depth > 8)
563 packet_size++;
cristycd081772010-03-22 17:19:12 +0000564 pixels=(unsigned char *) AcquireQuantumMemory(image->columns+256,packet_size*
cristy56ed31c2010-03-22 00:46:21 +0000565 sizeof(*pixels));
566 if (pixels == (unsigned char *) NULL)
567 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
568 image->filename);
cristycd081772010-03-22 17:19:12 +0000569 (void) ResetMagickMemory(pixels,0,image->columns*packet_size*sizeof(*pixels));
570 compact_pixels=(unsigned char *) NULL;
cristy56ed31c2010-03-22 00:46:21 +0000571 if (image->compression == RLECompression)
572 {
cristye195f262010-04-16 18:12:35 +0000573 size_t
574 length;
575
576 length=0;
cristybb503372010-05-27 20:51:26 +0000577 for (y=0; y < (ssize_t) image->rows; y++)
cristye195f262010-04-16 18:12:35 +0000578 if ((MagickOffsetType) length < offsets[y])
cristy284c7d82010-04-24 00:19:14 +0000579 length=(size_t) offsets[y];
cristye195f262010-04-16 18:12:35 +0000580 compact_pixels=(unsigned char *) AcquireQuantumMemory(length,
581 sizeof(*pixels));
cristy56ed31c2010-03-22 00:46:21 +0000582 if (compact_pixels == (unsigned char *) NULL)
583 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
584 image->filename);
cristy0132ba82010-04-16 23:34:02 +0000585 (void) ResetMagickMemory(compact_pixels,0,length*sizeof(*compact_pixels));
cristy56ed31c2010-03-22 00:46:21 +0000586 }
cristyd05dca12010-07-25 02:32:32 +0000587 colorspace=image->colorspace;
cristybb503372010-05-27 20:51:26 +0000588 for (y=0; y < (ssize_t) image->rows; y++)
cristy56ed31c2010-03-22 00:46:21 +0000589 {
cristy3b004082010-08-12 19:56:38 +0000590 if (image->depth == 1)
591 {
592 count=ReadBlob(image,(image->columns+7)/8,pixels);
593 if (count < (ssize_t) ((image->columns+7)/8))
594 break;
595 }
cristy56ed31c2010-03-22 00:46:21 +0000596 else
597 {
cristy3b004082010-08-12 19:56:38 +0000598 if (image->compression != RLECompression)
599 count=ReadBlob(image,packet_size*image->columns,pixels);
600 else
601 {
602 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
603 if (count != (ssize_t) offsets[y])
604 break;
605 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
606 (ssize_t) image->depth,packet_size*image->columns,pixels);
607 }
608 if (count < (ssize_t) (packet_size*image->columns))
cristy56ed31c2010-03-22 00:46:21 +0000609 break;
cristy56ed31c2010-03-22 00:46:21 +0000610 }
cristy56ed31c2010-03-22 00:46:21 +0000611 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
612 if (q == (PixelPacket *) NULL)
613 break;
614 indexes=GetAuthenticIndexQueue(image);
615 p=pixels;
cristybb503372010-05-27 20:51:26 +0000616 for (x=0; x < (ssize_t) image->columns; x++)
cristy56ed31c2010-03-22 00:46:21 +0000617 {
618 if (packet_size == 1)
619 pixel=ScaleCharToQuantum(*p++);
620 else
621 {
622 p=PushShortPixel(MSBEndian,p,&nibble);
623 pixel=ScaleShortToQuantum(nibble);
624 }
625 switch (type)
626 {
627 case -1:
628 {
629 q->opacity=(Quantum) (QuantumRange-pixel);
630 break;
631 }
632 case 0:
633 {
634 q->red=pixel;
635 if (channels == 1)
636 {
637 q->green=q->red;
638 q->blue=q->red;
639 }
640 if (image->storage_class == PseudoClass)
641 {
642 if (packet_size == 1)
643 indexes[x]=(IndexPacket) ScaleQuantumToChar(pixel);
644 else
645 indexes[x]=(IndexPacket) ScaleQuantumToShort(pixel);
cristybb503372010-05-27 20:51:26 +0000646 q->red=image->colormap[(ssize_t) indexes[x]].red;
647 q->green=image->colormap[(ssize_t) indexes[x]].green;
648 q->blue=image->colormap[(ssize_t) indexes[x]].blue;
cristy3b004082010-08-12 19:56:38 +0000649 if (image->depth == 1)
650 {
651 ssize_t
652 bit;
653
cristye8b06272010-10-25 13:12:02 +0000654 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
cristy3b004082010-08-12 19:56:38 +0000655 {
cristy85421392010-08-14 02:41:27 +0000656 indexes[x]=((((unsigned char) pixel) & (0x01 << (7-bit)))
657 != 0 ? 0 : 255);
cristy3b004082010-08-12 19:56:38 +0000658 q->red=image->colormap[(ssize_t) indexes[x]].red;
659 q->green=image->colormap[(ssize_t) indexes[x]].green;
660 q->blue=image->colormap[(ssize_t) indexes[x]].blue;
661 q++;
662 x++;
663 }
664 q--;
665 x--;
666 }
cristy56ed31c2010-03-22 00:46:21 +0000667 }
668 break;
669 }
670 case 1:
671 {
672 if (image->storage_class == PseudoClass)
673 q->opacity=(Quantum) (QuantumRange-pixel);
674 else
675 q->green=pixel;
676 break;
677 }
678 case 2:
679 {
cristydede49f2010-08-20 20:26:26 +0000680 if (image->storage_class == PseudoClass)
681 q->opacity=(Quantum) (QuantumRange-pixel);
682 else
683 q->blue=pixel;
cristy56ed31c2010-03-22 00:46:21 +0000684 break;
685 }
686 case 3:
687 {
688 if (image->colorspace == CMYKColorspace)
689 indexes[x]=(IndexPacket) pixel;
690 else
691 q->opacity=(Quantum) (QuantumRange-pixel);
692 break;
693 }
694 case 4:
695 {
cristya60c0202010-07-31 21:36:45 +0000696 if ((image->colorspace == RGBColorspace) && (channels > 3))
697 break;
cristy56ed31c2010-03-22 00:46:21 +0000698 q->opacity=(Quantum) (QuantumRange-pixel);
699 break;
700 }
701 default:
702 break;
703 }
704 q++;
705 }
706 if (SyncAuthenticPixels(image,exception) == MagickFalse)
707 break;
708 }
cristyd05dca12010-07-25 02:32:32 +0000709 image->colorspace=colorspace;
cristy56ed31c2010-03-22 00:46:21 +0000710 if (image->compression == RLECompression)
711 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
712 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
713 return(MagickTrue);
714}
715
cristy3ed852e2009-09-05 21:47:34 +0000716static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception)
717{
cristy3ed852e2009-09-05 21:47:34 +0000718 char
cristy56ed31c2010-03-22 00:46:21 +0000719 message[MaxTextExtent],
cristy3ed852e2009-09-05 21:47:34 +0000720 type[4];
721
722 Image
723 *image;
724
725 IndexPacket
726 *indexes;
727
728 LayerInfo
729 *layer_info;
730
cristybb503372010-05-27 20:51:26 +0000731 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000732 j,
733 number_layers,
734 y;
735
cristy56ed31c2010-03-22 00:46:21 +0000736 MagickBooleanType
737 status;
738
739 MagickOffsetType
740 offset,
741 *offsets;
742
743 MagickSizeType
744 combinedlength,
745 length,
746 size;
747
cristy3ed852e2009-09-05 21:47:34 +0000748 PSDInfo
749 psd_info;
750
cristydede49f2010-08-20 20:26:26 +0000751 register PixelPacket
752 *q;
753
cristybb503372010-05-27 20:51:26 +0000754 register ssize_t
cristy56ed31c2010-03-22 00:46:21 +0000755 i,
cristy3ed852e2009-09-05 21:47:34 +0000756 x;
757
cristy3ed852e2009-09-05 21:47:34 +0000758 ssize_t
759 count;
760
cristy3ed852e2009-09-05 21:47:34 +0000761 unsigned char
762 *data;
763
764 unsigned short
765 compression;
766
cristybb503372010-05-27 20:51:26 +0000767 size_t
cristy3ed852e2009-09-05 21:47:34 +0000768 mask_size,
cristy3ed852e2009-09-05 21:47:34 +0000769 skip_first_alpha = 0;
770
771 /*
772 Open image file.
773 */
774 assert(image_info != (const ImageInfo *) NULL);
775 assert(image_info->signature == MagickSignature);
776 if (image_info->debug != MagickFalse)
777 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
778 image_info->filename);
779 assert(exception != (ExceptionInfo *) NULL);
780 assert(exception->signature == MagickSignature);
781 image=AcquireImage(image_info);
782 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
783 if (status == MagickFalse)
784 {
785 image=DestroyImageList(image);
786 return((Image *) NULL);
787 }
788 /*
789 Read image header.
790 */
791 count=ReadBlob(image,4,(unsigned char *) psd_info.signature);
792 psd_info.version=ReadBlobMSBShort(image);
cristy50aea4a2010-03-09 17:37:44 +0000793 if ((count == 0) || (LocaleNCompare(psd_info.signature,"8BPS",4) != 0) ||
cristy2d3d87f2010-03-01 00:23:08 +0000794 ((psd_info.version != 1) && (psd_info.version != 2)))
cristy3ed852e2009-09-05 21:47:34 +0000795 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
796 count=ReadBlob(image,6,psd_info.reserved);
797 psd_info.channels=ReadBlobMSBShort(image);
798 if (psd_info.channels > MaxPSDChannels)
799 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
800 psd_info.rows=ReadBlobMSBLong(image);
801 psd_info.columns=ReadBlobMSBLong(image);
cristy2d3d87f2010-03-01 00:23:08 +0000802 if ((psd_info.version == 1) && ((psd_info.rows > 30000) ||
803 (psd_info.columns > 30000)))
804 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000805 psd_info.depth=ReadBlobMSBShort(image);
cristy2d3d87f2010-03-01 00:23:08 +0000806 if ((psd_info.depth != 1) && (psd_info.depth != 8) && (psd_info.depth != 16))
807 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000808 psd_info.mode=ReadBlobMSBShort(image);
809 if (image->debug != MagickFalse)
810 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000811 " Image is %.20g x %.20g with channels=%.20g, depth=%.20g, mode=%s",
812 (double) psd_info.columns,(double) psd_info.rows,(double)
813 psd_info.channels,(double) psd_info.depth,ModeToString((PSDImageType)
814 psd_info.mode));
cristy3ed852e2009-09-05 21:47:34 +0000815 /*
816 Initialize image.
817 */
818 image->depth=psd_info.depth;
819 image->columns=psd_info.columns;
820 image->rows=psd_info.rows;
cristyf6febd72010-11-06 15:54:54 +0000821 if (SetImageBackgroundColor(image) == MagickFalse)
cristy95524f92010-02-16 18:44:34 +0000822 {
823 InheritException(exception,&image->exception);
824 image=DestroyImageList(image);
825 return((Image *) NULL);
826 }
cristy3ed852e2009-09-05 21:47:34 +0000827 image->matte=psd_info.channels >= 4 ? MagickTrue : MagickFalse;
828 if (psd_info.mode == LabMode)
829 image->colorspace=LabColorspace;
830 if (psd_info.mode == CMYKMode)
831 {
832 image->colorspace=CMYKColorspace;
833 image->matte=psd_info.channels >= 5 ? MagickTrue : MagickFalse;
834 }
835 if ((psd_info.mode == BitmapMode) || (psd_info.mode == GrayscaleMode) ||
836 (psd_info.mode == DuotoneMode))
837 {
cristy52cf7f12010-02-07 18:07:56 +0000838 if (AcquireImageColormap(image,256) == MagickFalse)
839 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +0000840 image->matte=psd_info.channels >= 2 ? MagickTrue : MagickFalse;
841 if (image->debug != MagickFalse)
842 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristydede49f2010-08-20 20:26:26 +0000843 " Image colormap allocated");
cristy0132ba82010-04-16 23:34:02 +0000844 image->colorspace=GRAYColorspace;
cristy3ed852e2009-09-05 21:47:34 +0000845 }
846 if (image->debug != MagickFalse)
847 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
848 image->matte ? " image has matte" : " image has no matte");
849 /*
850 Read PSD raster colormap only present for indexed and duotone images.
851 */
852 length=ReadBlobMSBLong(image);
853 if (length != 0)
854 {
855 if (image->debug != MagickFalse)
856 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
857 " reading colormap");
858 if (psd_info.mode == DuotoneMode)
859 {
860 /*
861 Duotone image data; the format of this data is undocumented.
862 */
cristy56ed31c2010-03-22 00:46:21 +0000863 data=(unsigned char *) AcquireQuantumMemory((size_t) length,
864 sizeof(*data));
cristy3ed852e2009-09-05 21:47:34 +0000865 if (data == (unsigned char *) NULL)
866 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy56ed31c2010-03-22 00:46:21 +0000867 count=ReadBlob(image,(size_t) length,data);
cristy3ed852e2009-09-05 21:47:34 +0000868 data=(unsigned char *) RelinquishMagickMemory(data);
869 }
870 else
871 {
872 /*
873 Read PSD raster colormap.
874 */
cristybb503372010-05-27 20:51:26 +0000875 if (AcquireImageColormap(image,(size_t) (length/3)) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000876 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000877 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000878 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
879 ReadBlobByte(image));
cristybb503372010-05-27 20:51:26 +0000880 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000881 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
882 ReadBlobByte(image));
cristybb503372010-05-27 20:51:26 +0000883 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000884 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
885 ReadBlobByte(image));
886 image->matte=psd_info.channels >= 2 ? MagickTrue : MagickFalse;
887 }
888 }
889 length=ReadBlobMSBLong(image);
890 if (length != 0)
891 {
892 unsigned char
893 *blocks;
894
895 /*
896 Image resources block.
897 */
898 if (image->debug != MagickFalse)
899 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000900 " reading image resource blocks - %.20g bytes",(double) length);
cristy56ed31c2010-03-22 00:46:21 +0000901 blocks=(unsigned char *) AcquireQuantumMemory((size_t) length,
902 sizeof(*blocks));
cristy3ed852e2009-09-05 21:47:34 +0000903 if (blocks == (unsigned char *) NULL)
904 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristy56ed31c2010-03-22 00:46:21 +0000905 count=ReadBlob(image,(size_t) length,blocks);
906 if ((count != (ssize_t) length) ||
cristy3ed852e2009-09-05 21:47:34 +0000907 (LocaleNCompare((char *) blocks,"8BIM",4) != 0))
908 {
909 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
910 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
911 }
cristy56ed31c2010-03-22 00:46:21 +0000912 (void) ParseImageResourceBlocks(image,blocks,(size_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000913 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
914 }
915 /*
cristy3ed852e2009-09-05 21:47:34 +0000916 Layer and mask block.
917 */
918 layer_info=(LayerInfo *) NULL;
cristy4689cf02010-02-17 21:15:45 +0000919 number_layers=1;
cristy2d3d87f2010-03-01 00:23:08 +0000920 length=GetPSDSize(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +0000921 if (length == 8)
922 {
923 length=ReadBlobMSBLong(image);
924 length=ReadBlobMSBLong(image);
925 }
926 if ((image_info->number_scenes == 1) && (image_info->scene == 0))
cristyd4297022010-09-16 22:59:09 +0000927 {
928 if (DiscardBlobBytes(image,length) == MagickFalse)
929 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
930 image->filename);
931 length=0;
932 }
cristy3ed852e2009-09-05 21:47:34 +0000933 if (length == 0)
934 {
935 if (image->debug != MagickFalse)
936 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
937 " image has no layers");
938 }
939 else
940 {
941 offset=TellBlob(image);
cristy2d3d87f2010-03-01 00:23:08 +0000942 size=GetPSDSize(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +0000943 if (size == 0)
944 {
cristybb503372010-05-27 20:51:26 +0000945 size_t
cristy2d3d87f2010-03-01 00:23:08 +0000946 quantum;
947
cristy3ed852e2009-09-05 21:47:34 +0000948 /*
949 Skip layers & masks.
950 */
cristy56ed31c2010-03-22 00:46:21 +0000951 quantum=psd_info.version == 1 ? 4UL : 8UL;
cristyc1af14f2010-09-16 20:01:21 +0000952 if (DiscardBlobBytes(image,length-quantum) == MagickFalse)
953 ThrowFileException(exception,CorruptImageError,
954 "UnexpectedEndOfFile",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000955 }
956 else
957 {
958 MagickOffsetType
959 layer_offset;
960
961 layer_offset=offset+length;
962 number_layers=(short) ReadBlobMSBShort(image);
963 if (image->debug != MagickFalse)
964 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000965 " image contains %.20g layers",(double) number_layers);
cristy3ed852e2009-09-05 21:47:34 +0000966 if (number_layers < 0)
967 {
968 /*
969 Weird hack in PSD format to ignore first alpha channel.
970 */
971 skip_first_alpha=1;
972 number_layers=MagickAbsoluteValue(number_layers);
973 if (image->debug != MagickFalse)
974 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
975 " negative layer count corrected for");
976 }
977 layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers,
978 sizeof(*layer_info));
979 if (layer_info == (LayerInfo *) NULL)
980 {
981 if (image->debug != MagickFalse)
982 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
983 " allocation of LayerInfo failed");
984 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
985 }
986 (void) ResetMagickMemory(layer_info,0,(size_t) number_layers*
987 sizeof(*layer_info));
988 for (i=0; i < number_layers; i++)
989 {
cristy4484e332010-10-08 23:46:21 +0000990 int
991 x,
992 y;
993
cristy3ed852e2009-09-05 21:47:34 +0000994 if (image->debug != MagickFalse)
995 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000996 " reading layer #%.20g",(double) i+1);
cristy6cff05d2010-09-02 11:22:46 +0000997 layer_info[i].page.y=(int) ReadBlobMSBLong(image);
998 layer_info[i].page.x=(int) ReadBlobMSBLong(image);
cristy4484e332010-10-08 23:46:21 +0000999 y=(int) ReadBlobMSBLong(image);
1000 x=(int) ReadBlobMSBLong(image);
1001 layer_info[i].page.width=(ssize_t) (x-layer_info[i].page.x);
1002 layer_info[i].page.height=(ssize_t) (y-layer_info[i].page.y);
cristy3ed852e2009-09-05 21:47:34 +00001003 layer_info[i].channels=ReadBlobMSBShort(image);
1004 if (layer_info[i].channels > MaxPSDChannels)
1005 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
1006 if (image->debug != MagickFalse)
1007 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001008 " offset(%.20g,%.20g), size(%.20g,%.20g), channels=%.20g",
1009 (double) layer_info[i].page.x,(double) layer_info[i].page.y,
1010 (double) layer_info[i].page.height,(double)
1011 layer_info[i].page.width,(double) layer_info[i].channels);
cristybb503372010-05-27 20:51:26 +00001012 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
cristy3ed852e2009-09-05 21:47:34 +00001013 {
cristy56ed31c2010-03-22 00:46:21 +00001014 layer_info[i].channel_info[j].type=(short)
1015 ReadBlobMSBShort(image);
cristybb503372010-05-27 20:51:26 +00001016 layer_info[i].channel_info[j].size=(size_t)
cristy56ed31c2010-03-22 00:46:21 +00001017 GetPSDSize(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +00001018 if (image->debug != MagickFalse)
1019 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001020 " channel[%.20g]: type=%.20g, size=%.20g",(double) j,
1021 (double) layer_info[i].channel_info[j].type,
1022 (double) layer_info[i].channel_info[j].size);
cristy3ed852e2009-09-05 21:47:34 +00001023 }
1024 count=ReadBlob(image,4,(unsigned char *) type);
1025 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1026 {
1027 if (image->debug != MagickFalse)
1028 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1029 " layer type was %.4s instead of 8BIM", type);
1030 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1031 }
1032 count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey);
1033 layer_info[i].opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1034 (unsigned char) ReadBlobByte(image)));
1035 layer_info[i].clipping=(unsigned char) ReadBlobByte(image);
1036 layer_info[i].flags=(unsigned char) ReadBlobByte(image);
1037 layer_info[i].visible=!(layer_info[i].flags & 0x02);
1038 if (image->debug != MagickFalse)
1039 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001040 " blend=%.4s, opacity=%.20g, clipping=%s, flags=%d, visible=%s",
1041 layer_info[i].blendkey,(double) layer_info[i].opacity,
cristy3ed852e2009-09-05 21:47:34 +00001042 layer_info[i].clipping ? "true" : "false",layer_info[i].flags,
1043 layer_info[i].visible ? "true" : "false");
1044 (void) ReadBlobByte(image); /* filler */
1045 combinedlength=0;
1046 size=ReadBlobMSBLong(image);
1047 if (size != 0)
1048 {
1049 if (image->debug != MagickFalse)
1050 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1051 " layer contains additional info");
1052 length=ReadBlobMSBLong(image);
1053 if (length != 0)
1054 {
1055 /*
1056 Layer mask info.
1057 */
cristy6cff05d2010-09-02 11:22:46 +00001058 layer_info[i].mask.y=(int) ReadBlobMSBLong(image);
1059 layer_info[i].mask.x=(int) ReadBlobMSBLong(image);
cristybb503372010-05-27 20:51:26 +00001060 layer_info[i].mask.height=(size_t)
cristy3ed852e2009-09-05 21:47:34 +00001061 (ReadBlobMSBLong(image)-layer_info[i].mask.y);
cristybb503372010-05-27 20:51:26 +00001062 layer_info[i].mask.width=(size_t)
cristy3ed852e2009-09-05 21:47:34 +00001063 (ReadBlobMSBLong(image)-layer_info[i].mask.x);
1064 if (image->debug != MagickFalse)
1065 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001066 " layer mask: offset(%.20g,%.20g), size(%.20g,%.20g), length=%.20g",
cristyd4297022010-09-16 22:59:09 +00001067 (double) layer_info[i].mask.x,(double) layer_info[i].mask.y,
cristye8c25f92010-06-03 00:53:06 +00001068 (double) layer_info[i].mask.width,(double)
1069 layer_info[i].mask.height,(double) length-16);
cristy3ed852e2009-09-05 21:47:34 +00001070 /*
1071 Skip over the rest of the layer mask information.
1072 */
cristyc1af14f2010-09-16 20:01:21 +00001073 if (DiscardBlobBytes(image,length-16) == MagickFalse)
cristyd4297022010-09-16 22:59:09 +00001074 ThrowFileException(exception,CorruptImageError,
1075 "UnexpectedEndOfFile",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001076 }
1077 combinedlength+=length+4; /* +4 for length */
1078 length=ReadBlobMSBLong(image);
1079 if (length != 0)
1080 {
1081 /*
1082 Layer blending ranges info.
1083 */
1084 if (image->debug != MagickFalse)
1085 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001086 " layer blending ranges: length=%.20g",(double)
cristyf2faecf2010-05-28 19:19:36 +00001087 length);
cristy3ed852e2009-09-05 21:47:34 +00001088 /*
1089 We read it, but don't use it...
1090 */
cristybb503372010-05-27 20:51:26 +00001091 for (j=0; j < (ssize_t) (length); j+=8)
cristy3ed852e2009-09-05 21:47:34 +00001092 {
1093 size_t blend_source=ReadBlobMSBLong(image);
1094 size_t blend_dest=ReadBlobMSBLong(image);
1095 if (image->debug != MagickFalse)
1096 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1097 " source(%x), dest(%x)",(unsigned int)
1098 blend_source,(unsigned int) blend_dest);
1099 }
1100 }
cristy2cc39842009-10-10 20:04:02 +00001101 combinedlength+=length+4;
1102 /*
1103 Layer name.
1104 */
cristy3ed852e2009-09-05 21:47:34 +00001105 length=(size_t) ReadBlobByte(image);
cristybb503372010-05-27 20:51:26 +00001106 for (j=0; j < (ssize_t) length; j++)
cristy2cc39842009-10-10 20:04:02 +00001107 layer_info[i].name[j]=(unsigned char) ReadBlobByte(image);
1108 layer_info[i].name[j]='\0';
1109 if (image->debug != MagickFalse)
1110 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1111 " layer name: %s",layer_info[i].name);
1112 combinedlength+=length+1;
cristy3ed852e2009-09-05 21:47:34 +00001113
1114#if 0 /* still in development */
1115 /*
1116 Adjustment layers and other stuff...
1117 */
1118 {
1119 char alsig[4],
1120 alkey[4];
1121
1122 count=ReadBlob(image,4,alsig);
1123 if ((count == 0) || (LocaleNCompare(alsig,"8BIM",4) != 0)) {
1124 if (debug != MagickFalse)
1125 {
1126 if (image->debug != MagickFalse)
1127 (void) LogMagickEvent(CoderEvent,GetMagickModule()," adjustment layer type was %.4s instead of 8BIM", alsig);
1128 }
1129 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1130 }
1131 count=ReadBlob(image,4,alkey);
1132 length=ReadBlobMSBLong(image);
1133 if (debug != MagickFalse)
1134 {
1135 if (image->debug != MagickFalse)
1136 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001137 " adjustment layer key: %.4s, data length=%.20g",
1138 alkey, (double) length);
cristy3ed852e2009-09-05 21:47:34 +00001139 }
1140
1141 if ( length ) {
cristybb503372010-05-27 20:51:26 +00001142 for (j=0; j < (ssize_t) (length); j++)
cristy3ed852e2009-09-05 21:47:34 +00001143 (void) ReadBlobByte(image);
1144 }
1145
1146 }
1147 combinedlength += 12 + length; /* sig, key, length + the actual length*/
1148#endif
1149
1150 /*
1151 Skip the rest of the variable data until we support it.
1152 */
1153 if (image->debug != MagickFalse)
1154 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001155 " unsupported data: length=%.20g",(double)
cristyeec18db2010-03-03 21:15:45 +00001156 (size-combinedlength));
cristyc1af14f2010-09-16 20:01:21 +00001157 if (DiscardBlobBytes(image,size-combinedlength) == MagickFalse)
cristyd4297022010-09-16 22:59:09 +00001158 ThrowFileException(exception,CorruptImageError,
1159 "UnexpectedEndOfFile",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001160 }
1161 /*
1162 Allocate layered image.
1163 */
1164 layer_info[i].image=CloneImage(image,layer_info[i].page.width,
cristy390c39f2010-10-05 23:41:44 +00001165 layer_info[i].page.height == ~0U ? 1 : layer_info[i].page.height,
1166 MagickFalse,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00001167 if (layer_info[i].image == (Image *) NULL)
1168 {
1169 for (j=0; j < i; j++)
1170 layer_info[j].image=DestroyImage(layer_info[j].image);
1171 if (image->debug != MagickFalse)
1172 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001173 " allocation of image for layer %.20g failed",(double) i);
cristy3ed852e2009-09-05 21:47:34 +00001174 ThrowReaderException(ResourceLimitError,
1175 "MemoryAllocationFailed");
1176 }
1177 if (image->debug != MagickFalse)
1178 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1179 " setting up new layer image");
cristy2e2e46f2010-10-12 18:55:20 +00001180 if (image_info->ping != MagickFalse)
1181 (void) SetImageBackgroundColor(layer_info[i].image);
cristy3ed852e2009-09-05 21:47:34 +00001182 layer_info[i].image->compose=
1183 PSDBlendModeToCompositeOperator(layer_info[i].blendkey);
1184 if (layer_info[i].visible == MagickFalse)
1185 layer_info[i].image->compose=NoCompositeOp;
1186 if (psd_info.mode == CMYKMode)
cristy0132ba82010-04-16 23:34:02 +00001187 layer_info[i].image->colorspace=CMYKColorspace;
1188 if ((psd_info.mode == BitmapMode) ||
1189 (psd_info.mode == GrayscaleMode) ||
1190 (psd_info.mode == DuotoneMode))
1191 layer_info[i].image->colorspace=GRAYColorspace;
cristybb503372010-05-27 20:51:26 +00001192 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
cristy3ed852e2009-09-05 21:47:34 +00001193 if (layer_info[i].channel_info[j].type == -1)
1194 layer_info[i].image->matte=MagickTrue;
1195 /*
1196 Set up some hidden attributes for folks that need them.
1197 */
cristye8c25f92010-06-03 00:53:06 +00001198 (void) FormatMagickString(message,MaxTextExtent,"%.20gld",
1199 (double) layer_info[i].page.x);
cristy56ed31c2010-03-22 00:46:21 +00001200 (void) SetImageArtifact(layer_info[i].image,"psd:layer.x",message);
cristye8c25f92010-06-03 00:53:06 +00001201 (void) FormatMagickString(message,MaxTextExtent,"%.20g",
1202 (double) layer_info[i].page.y);
cristy56ed31c2010-03-22 00:46:21 +00001203 (void) SetImageArtifact(layer_info[i].image,"psd:layer.y",message);
cristye8c25f92010-06-03 00:53:06 +00001204 (void) FormatMagickString(message,MaxTextExtent,"%.20g",
1205 (double) layer_info[i].opacity);
cristy56ed31c2010-03-22 00:46:21 +00001206 (void) SetImageArtifact(layer_info[i].image,"psd:layer.opacity",
1207 message);
cristy3ed852e2009-09-05 21:47:34 +00001208 (void) SetImageProperty(layer_info[i].image,"label",(char *)
1209 layer_info[i].name);
1210 }
1211 if (image->debug != MagickFalse)
1212 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1213 " reading image data for layers");
1214 /*
1215 Read pixel data for each layer.
1216 */
1217 for (i=0; i < number_layers; i++)
1218 {
1219 if (image->debug != MagickFalse)
1220 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001221 " reading data for layer %.20g",(double) i);
cristybb503372010-05-27 20:51:26 +00001222 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
cristy3ed852e2009-09-05 21:47:34 +00001223 {
1224 if (image->debug != MagickFalse)
1225 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001226 " reading data for channel %.20g",(double) j);
cristy3ed852e2009-09-05 21:47:34 +00001227#if 1
1228 if (layer_info[i].channel_info[j].size <= (2*layer_info[i].image->rows))
1229 {
cristybb503372010-05-27 20:51:26 +00001230 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001231 k;
1232
1233 if (image->debug != MagickFalse)
1234 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1235 " layer data is empty");
1236 /*
1237 A layer without data.
1238 */
cristybb503372010-05-27 20:51:26 +00001239 for (k=0; k < (ssize_t) layer_info[i].channel_info[j].size; k++)
cristy3ed852e2009-09-05 21:47:34 +00001240 (void) ReadBlobByte(layer_info[i].image);
1241 continue;
1242 }
1243#endif
cristy56ed31c2010-03-22 00:46:21 +00001244 offsets=(MagickOffsetType *) NULL;
1245 layer_info[i].image->compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00001246 compression=ReadBlobMSBShort(layer_info[i].image);
cristy2cc39842009-10-10 20:04:02 +00001247 if ((layer_info[i].page.height != 0) &&
1248 (layer_info[i].page.width != 0))
cristy3ed852e2009-09-05 21:47:34 +00001249 {
cristy2cc39842009-10-10 20:04:02 +00001250 if (compression == 1)
1251 {
1252 /*
1253 Read RLE compressed data.
1254 */
cristy56ed31c2010-03-22 00:46:21 +00001255 layer_info[i].image->compression=RLECompression;
cristy2cc39842009-10-10 20:04:02 +00001256 if (image->debug != MagickFalse)
1257 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1258 " layer data is RLE compressed");
cristy56ed31c2010-03-22 00:46:21 +00001259 offsets=(MagickOffsetType *) AcquireQuantumMemory(
1260 layer_info[i].image->rows,sizeof(*offsets));
1261 if (offsets == (MagickOffsetType *) NULL)
1262 ThrowReaderException(ResourceLimitError,
1263 "MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00001264 for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
cristy56ed31c2010-03-22 00:46:21 +00001265 offsets[y]=GetPSDOffset(&psd_info,layer_info[i].image);
cristy2cc39842009-10-10 20:04:02 +00001266 }
cristy56ed31c2010-03-22 00:46:21 +00001267 status=ReadPSDLayer(layer_info[i].image,
cristye3038982010-05-17 02:20:52 +00001268 layer_info[i].channels,layer_info[i].channel_info[j].type,
1269 offsets,exception);
cristy56ed31c2010-03-22 00:46:21 +00001270 if (compression == 1)
1271 offsets=(MagickOffsetType *) RelinquishMagickMemory(
1272 offsets);
1273 if (status == MagickFalse)
1274 break;
cristy3ed852e2009-09-05 21:47:34 +00001275 }
cristy56ed31c2010-03-22 00:46:21 +00001276 }
cristy3ed852e2009-09-05 21:47:34 +00001277 if (layer_info[i].opacity != OpaqueOpacity)
1278 {
1279 /*
1280 Correct for opacity level.
1281 */
cristybb503372010-05-27 20:51:26 +00001282 for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001283 {
1284 q=GetAuthenticPixels(layer_info[i].image,0,y,
1285 layer_info[i].image->columns,1,exception);
1286 if (q == (PixelPacket *) NULL)
1287 break;
1288 indexes=GetAuthenticIndexQueue(layer_info[i].image);
cristybb503372010-05-27 20:51:26 +00001289 for (x=0; x < (ssize_t) layer_info[i].image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001290 {
cristy07758662010-01-15 20:25:50 +00001291 q->opacity=(Quantum) (QuantumRange-(Quantum) (QuantumScale*
1292 ((QuantumRange-q->opacity)*(QuantumRange-
1293 layer_info[i].opacity))));
cristy3ed852e2009-09-05 21:47:34 +00001294 q++;
1295 }
1296 if (SyncAuthenticPixels(layer_info[i].image,exception) == MagickFalse)
1297 break;
1298 }
1299 }
1300 if (layer_info[i].image->colorspace == CMYKColorspace)
1301 (void) NegateImage(layer_info[i].image,MagickFalse);
cristy56ed31c2010-03-22 00:46:21 +00001302 status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType)
1303 number_layers);
cristy4689cf02010-02-17 21:15:45 +00001304 if (status == MagickFalse)
1305 break;
cristy3ed852e2009-09-05 21:47:34 +00001306 }
1307 /* added by palf -> invisible group layer make layer of this group
1308 invisible I consider that all layer with width and height null are
1309 layer for group layer */
1310 {
1311 short inside_layer = 0;
1312 short layer_visible = 0;
1313 for (i=number_layers-1; i >=0; i--)
1314 {
1315 if ((layer_info[i].page.width == 0) ||
1316 (layer_info[i].page.height == 0))
1317 {
1318 if (inside_layer == 0)
1319 {
1320 inside_layer=1;
1321 layer_visible=(short int) layer_info[i].visible;
1322 }
1323 else
1324 {
1325 inside_layer = 0;
1326 }
1327 }
1328 else
1329 if ((inside_layer == 1) && (layer_visible == 0))
1330 {
1331 layer_info[i].visible=(unsigned char) layer_visible;
1332 layer_info[i].image->compose=NoCompositeOp;
1333 }
1334 }
1335 }
1336 /* added by palf -> suppression of empty layer */
1337 /* I consider that all layer with width and height null are layer for group layer */
1338 for (i=0; i < number_layers; i++)
1339 {
1340 if ((layer_info[i].page.width == 0) ||
1341 (layer_info[i].page.height == 0))
1342 {
1343 if (layer_info[i].image != (Image *) NULL)
1344 layer_info[i].image=DestroyImage(layer_info[i].image);
1345 for (j=i; j < number_layers - 1; j++)
1346 layer_info[j] = layer_info[j+1];
1347 number_layers--;
1348 i--;
1349 }
cristy56ed31c2010-03-22 00:46:21 +00001350 }
cristy3ed852e2009-09-05 21:47:34 +00001351 mask_size = ReadBlobMSBLong(image); /* global mask size: currently ignored */
cristy56ed31c2010-03-22 00:46:21 +00001352 if (number_layers > 0)
1353 {
1354 if (image->debug != MagickFalse)
1355 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1356 " putting layers into image list");
1357 for (i=0; i < number_layers; i++)
cristy3ed852e2009-09-05 21:47:34 +00001358 {
cristy56ed31c2010-03-22 00:46:21 +00001359 if (i > 0)
1360 layer_info[i].image->previous=layer_info[i-1].image;
1361 if (i < (number_layers-1))
1362 layer_info[i].image->next=layer_info[i+1].image;
1363 layer_info[i].image->page=layer_info[i].page;
1364 }
cristy3ed852e2009-09-05 21:47:34 +00001365 image->next=layer_info[0].image;
1366 layer_info[0].image->previous=image;
1367 layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info);
cristy56ed31c2010-03-22 00:46:21 +00001368 }
1369 layer_offset-=TellBlob(image);
1370 offset=SeekBlob(image,layer_offset,SEEK_CUR);
1371 }
cristy3ed852e2009-09-05 21:47:34 +00001372 }
1373 /*
1374 Read the precombined layer, present for PSD < 4 compatibility
1375 */
1376 if (image->debug != MagickFalse)
1377 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1378 " reading the precombined layer");
cristy56ed31c2010-03-22 00:46:21 +00001379 offsets=(MagickOffsetType *) NULL;
1380 image->compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00001381 compression=ReadBlobMSBShort(image);
1382 if (compression == 1)
1383 {
1384 /*
cristy3f938c62010-03-07 00:53:52 +00001385 Read Packbit encoded pixel data as separate planes.
cristy3ed852e2009-09-05 21:47:34 +00001386 */
cristy56ed31c2010-03-22 00:46:21 +00001387 image->compression=RLECompression;
1388 offsets=(MagickOffsetType *) AcquireQuantumMemory(image->rows,
1389 psd_info.channels*sizeof(*offsets));
1390 if (offsets == (MagickOffsetType *) NULL)
1391 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +00001392 for (i=0; i < (ssize_t) (image->rows*psd_info.channels); i++)
cristy56ed31c2010-03-22 00:46:21 +00001393 offsets[i]=GetPSDOffset(&psd_info,image);
cristy3ed852e2009-09-05 21:47:34 +00001394 }
cristybb503372010-05-27 20:51:26 +00001395 for (i=0; i < (ssize_t) psd_info.channels; i++)
cristy56ed31c2010-03-22 00:46:21 +00001396 {
cristye3038982010-05-17 02:20:52 +00001397 status=ReadPSDLayer(image,psd_info.channels,i,offsets+i*image->rows,
1398 exception);
cristy56ed31c2010-03-22 00:46:21 +00001399 if (status == MagickFalse)
1400 break;
1401 status=SetImageProgress(image,LoadImagesTag,i,psd_info.channels);
1402 if (status == MagickFalse)
1403 break;
1404 }
1405 if (compression == 1)
1406 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
cristy3ed852e2009-09-05 21:47:34 +00001407 if (image->colorspace == CMYKColorspace)
1408 (void) NegateImage(image,MagickFalse);
cristy2837bcc2010-08-07 23:57:39 +00001409 if ((number_layers == 1) && (image->next != (Image *) NULL) &&
1410 (image->matte != MagickFalse))
cristyb43db672010-11-09 15:23:30 +00001411 DeleteImageFromList(&image);
cristy3ed852e2009-09-05 21:47:34 +00001412 (void) CloseBlob(image);
1413 return(GetFirstImageInList(image));
1414}
1415
1416/*
1417%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1418% %
1419% %
1420% %
1421% R e g i s t e r P S D I m a g e %
1422% %
1423% %
1424% %
1425%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1426%
1427% RegisterPSDImage() adds properties for the PSD image format to
1428% the list of supported formats. The properties include the image format
1429% tag, a method to read and/or write the format, whether the format
1430% supports the saving of more than one frame to the same file or blob,
1431% whether the format supports native in-memory I/O, and a brief
1432% description of the format.
1433%
1434% The format of the RegisterPSDImage method is:
1435%
cristybb503372010-05-27 20:51:26 +00001436% size_t RegisterPSDImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001437%
1438*/
cristybb503372010-05-27 20:51:26 +00001439ModuleExport size_t RegisterPSDImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001440{
1441 MagickInfo
1442 *entry;
1443
cristyb4233012010-02-28 20:09:14 +00001444 entry=SetMagickInfo("PSB");
1445 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1446 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1447 entry->magick=(IsImageFormatHandler *) IsPSD;
1448 entry->description=ConstantString("Adobe Large Document Format");
1449 entry->module=ConstantString("PSD");
1450 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001451 entry=SetMagickInfo("PSD");
1452 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1453 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1454 entry->magick=(IsImageFormatHandler *) IsPSD;
1455 entry->description=ConstantString("Adobe Photoshop bitmap");
1456 entry->module=ConstantString("PSD");
1457 (void) RegisterMagickInfo(entry);
1458 return(MagickImageCoderSignature);
1459}
1460
1461/*
1462%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1463% %
1464% %
1465% %
1466% U n r e g i s t e r P S D I m a g e %
1467% %
1468% %
1469% %
1470%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1471%
1472% UnregisterPSDImage() removes format registrations made by the
1473% PSD module from the list of supported formats.
1474%
1475% The format of the UnregisterPSDImage method is:
1476%
1477% UnregisterPSDImage(void)
1478%
1479*/
1480ModuleExport void UnregisterPSDImage(void)
1481{
cristyb4233012010-02-28 20:09:14 +00001482 (void) UnregisterMagickInfo("PSB");
cristy3ed852e2009-09-05 21:47:34 +00001483 (void) UnregisterMagickInfo("PSD");
1484}
1485
1486/*
1487%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1488% %
1489% %
1490% %
1491% W r i t e P S D I m a g e %
1492% %
1493% %
1494% %
1495%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1496%
cristyb1459bc2010-03-23 21:41:43 +00001497% WritePSDImage() writes an image in the Adobe Photoshop encoded image format.
cristy3ed852e2009-09-05 21:47:34 +00001498%
1499% The format of the WritePSDImage method is:
1500%
1501% MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image)
1502%
1503% A description of each parameter follows.
1504%
1505% o image_info: the image info.
1506%
1507% o image: The image.
1508%
1509%
1510*/
1511
cristy50aea4a2010-03-09 17:37:44 +00001512static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image,
cristyf0460ee2010-03-06 02:55:11 +00001513 const size_t offset)
1514{
1515 if (psd_info->version == 1)
cristy56ed31c2010-03-22 00:46:21 +00001516 return(WriteBlobMSBShort(image,(unsigned short) offset));
1517 return(WriteBlobMSBLong(image,(unsigned short) offset));
cristyf0460ee2010-03-06 02:55:11 +00001518}
1519
cristy50aea4a2010-03-09 17:37:44 +00001520static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image,
cristyf0460ee2010-03-06 02:55:11 +00001521 const MagickSizeType size)
1522{
1523 if (psd_info->version == 1)
cristy56ed31c2010-03-22 00:46:21 +00001524 return(WriteBlobMSBLong(image,(unsigned int) size));
cristyf0460ee2010-03-06 02:55:11 +00001525 return(WriteBlobMSBLongLong(image,size));
1526}
1527
cristy4aff1572010-02-15 13:34:01 +00001528static size_t PSDPackbitsEncodeImage(Image *image,const size_t length,
cristyb1459bc2010-03-23 21:41:43 +00001529 const unsigned char *pixels,unsigned char *compact_pixels)
cristy4aff1572010-02-15 13:34:01 +00001530{
1531 int
1532 count;
1533
cristybb503372010-05-27 20:51:26 +00001534 register ssize_t
cristy4aff1572010-02-15 13:34:01 +00001535 i,
1536 j;
1537
1538 register unsigned char
1539 *q;
1540
1541 unsigned char
1542 *packbits;
1543
1544 /*
1545 Compress pixels with Packbits encoding.
1546 */
1547 assert(image != (Image *) NULL);
1548 assert(image->signature == MagickSignature);
1549 if (image->debug != MagickFalse)
1550 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1551 assert(pixels != (unsigned char *) NULL);
1552 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1553 if (packbits == (unsigned char *) NULL)
1554 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1555 image->filename);
cristyb1459bc2010-03-23 21:41:43 +00001556 q=compact_pixels;
cristybb503372010-05-27 20:51:26 +00001557 for (i=(ssize_t) length; i != 0; )
cristy4aff1572010-02-15 13:34:01 +00001558 {
1559 switch (i)
1560 {
1561 case 1:
1562 {
1563 i--;
1564 *q++=(unsigned char) 0;
1565 *q++=(*pixels);
1566 break;
1567 }
1568 case 2:
1569 {
1570 i-=2;
1571 *q++=(unsigned char) 1;
1572 *q++=(*pixels);
1573 *q++=pixels[1];
1574 break;
1575 }
1576 case 3:
1577 {
1578 i-=3;
1579 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1580 {
1581 *q++=(unsigned char) ((256-3)+1);
1582 *q++=(*pixels);
1583 break;
1584 }
1585 *q++=(unsigned char) 2;
1586 *q++=(*pixels);
1587 *q++=pixels[1];
1588 *q++=pixels[2];
1589 break;
1590 }
1591 default:
1592 {
1593 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1594 {
1595 /*
1596 Packed run.
1597 */
1598 count=3;
cristybb503372010-05-27 20:51:26 +00001599 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
cristy4aff1572010-02-15 13:34:01 +00001600 {
1601 count++;
1602 if (count >= 127)
1603 break;
1604 }
1605 i-=count;
1606 *q++=(unsigned char) ((256-count)+1);
1607 *q++=(*pixels);
1608 pixels+=count;
1609 break;
1610 }
1611 /*
1612 Literal run.
1613 */
1614 count=0;
1615 while ((*(pixels+count) != *(pixels+count+1)) ||
1616 (*(pixels+count+1) != *(pixels+count+2)))
1617 {
1618 packbits[count+1]=pixels[count];
1619 count++;
cristybb503372010-05-27 20:51:26 +00001620 if (((ssize_t) count >= (i-3)) || (count >= 127))
cristy4aff1572010-02-15 13:34:01 +00001621 break;
1622 }
1623 i-=count;
1624 *packbits=(unsigned char) (count-1);
cristybb503372010-05-27 20:51:26 +00001625 for (j=0; j <= (ssize_t) count; j++)
cristy4aff1572010-02-15 13:34:01 +00001626 *q++=packbits[j];
1627 pixels+=count;
1628 break;
1629 }
1630 }
1631 }
1632 *q++=(unsigned char) 128; /* EOD marker */
1633 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
cristyb1459bc2010-03-23 21:41:43 +00001634 return((size_t) (q-compact_pixels));
cristy4aff1572010-02-15 13:34:01 +00001635}
1636
cristy875e28a2010-03-06 19:46:55 +00001637static void WritePackbitsLength(const PSDInfo *psd_info,
cristya20214e2010-08-28 16:52:13 +00001638 const ImageInfo *image_info,Image *image,Image *next_image,
1639 unsigned char *compact_pixels,const QuantumType quantum_type)
cristy3ed852e2009-09-05 21:47:34 +00001640{
cristy3ed852e2009-09-05 21:47:34 +00001641 QuantumInfo
1642 *quantum_info;
1643
1644 register const PixelPacket
1645 *p;
1646
1647 size_t
cristy4aff1572010-02-15 13:34:01 +00001648 length,
cristy3ed852e2009-09-05 21:47:34 +00001649 packet_size;
1650
cristy75f85ae2010-09-25 03:01:06 +00001651 ssize_t
1652 y;
1653
cristya20214e2010-08-28 16:52:13 +00001654 unsigned char
1655 *pixels;
1656
1657 if (next_image->depth > 8)
1658 next_image->depth=16;
1659 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
cristy4aff1572010-02-15 13:34:01 +00001660 quantum_info=AcquireQuantumInfo(image_info,image);
cristya20214e2010-08-28 16:52:13 +00001661 pixels=GetQuantumPixels(quantum_info);
1662 for (y=0; y < (ssize_t) next_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001663 {
cristya20214e2010-08-28 16:52:13 +00001664 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00001665 if (p == (const PixelPacket *) NULL)
cristy4aff1572010-02-15 13:34:01 +00001666 break;
cristya20214e2010-08-28 16:52:13 +00001667 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
cristy3ed852e2009-09-05 21:47:34 +00001668 quantum_type,pixels,&image->exception);
cristyb1459bc2010-03-23 21:41:43 +00001669 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels);
cristy875e28a2010-03-06 19:46:55 +00001670 (void) SetPSDOffset(psd_info,image,length);
cristy4aff1572010-02-15 13:34:01 +00001671 }
1672 quantum_info=DestroyQuantumInfo(quantum_info);
1673}
1674
cristy875e28a2010-03-06 19:46:55 +00001675static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info,
cristya20214e2010-08-28 16:52:13 +00001676 Image *image,Image *next_image,unsigned char *compact_pixels,
1677 const QuantumType quantum_type,const MagickBooleanType compression_flag)
cristy4aff1572010-02-15 13:34:01 +00001678{
1679 int
1680 y;
1681
cristy0910f242010-04-01 18:55:09 +00001682 MagickBooleanType
1683 monochrome;
1684
cristy4aff1572010-02-15 13:34:01 +00001685 QuantumInfo
1686 *quantum_info;
1687
1688 register const PixelPacket
1689 *p;
1690
cristybb503372010-05-27 20:51:26 +00001691 register ssize_t
cristy0910f242010-04-01 18:55:09 +00001692 i;
1693
cristy4aff1572010-02-15 13:34:01 +00001694 size_t
1695 length,
1696 packet_size;
1697
cristya20214e2010-08-28 16:52:13 +00001698 unsigned char
1699 *pixels;
1700
cristy50aea4a2010-03-09 17:37:44 +00001701 (void) psd_info;
cristy4aff1572010-02-15 13:34:01 +00001702 if ((compression_flag != MagickFalse) &&
cristya20214e2010-08-28 16:52:13 +00001703 (next_image->compression != RLECompression))
cristy4aff1572010-02-15 13:34:01 +00001704 (void) WriteBlobMSBShort(image,0);
cristya20214e2010-08-28 16:52:13 +00001705 if (next_image->depth > 8)
1706 next_image->depth=16;
cristybf9f4ba2010-08-20 18:13:29 +00001707 monochrome=IsMonochromeImage(image,&image->exception) && (image->depth == 1)
1708 ? MagickTrue : MagickFalse;
cristya20214e2010-08-28 16:52:13 +00001709 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
cristy4aff1572010-02-15 13:34:01 +00001710 quantum_info=AcquireQuantumInfo(image_info,image);
cristya20214e2010-08-28 16:52:13 +00001711 pixels=GetQuantumPixels(quantum_info);
1712 for (y=0; y < (ssize_t) next_image->rows; y++)
cristy4aff1572010-02-15 13:34:01 +00001713 {
cristya20214e2010-08-28 16:52:13 +00001714 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,&image->exception);
cristy4aff1572010-02-15 13:34:01 +00001715 if (p == (const PixelPacket *) NULL)
1716 break;
cristya20214e2010-08-28 16:52:13 +00001717 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
cristy4aff1572010-02-15 13:34:01 +00001718 quantum_type,pixels,&image->exception);
cristy0910f242010-04-01 18:55:09 +00001719 if (monochrome != MagickFalse)
cristybb503372010-05-27 20:51:26 +00001720 for (i=0; i < (ssize_t) length; i++)
cristy0910f242010-04-01 18:55:09 +00001721 pixels[i]=(~pixels[i]);
cristya20214e2010-08-28 16:52:13 +00001722 if (next_image->compression != RLECompression)
cristy4aff1572010-02-15 13:34:01 +00001723 (void) WriteBlob(image,length,pixels);
1724 else
1725 {
cristyb1459bc2010-03-23 21:41:43 +00001726 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels);
1727 (void) WriteBlob(image,length,compact_pixels);
cristy4aff1572010-02-15 13:34:01 +00001728 }
cristy3ed852e2009-09-05 21:47:34 +00001729 }
1730 quantum_info=DestroyQuantumInfo(quantum_info);
1731}
1732
cristy875e28a2010-03-06 19:46:55 +00001733static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info,
cristya20214e2010-08-28 16:52:13 +00001734 const ImageInfo *image_info,Image *image,Image *next_image,
cristy875e28a2010-03-06 19:46:55 +00001735 const MagickBooleanType separate)
cristy3ed852e2009-09-05 21:47:34 +00001736{
1737 int
1738 i;
1739
1740 size_t
1741 channels,
1742 packet_size;
1743
1744 unsigned char
cristya20214e2010-08-28 16:52:13 +00001745 *compact_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001746
1747 /*
cristy875e28a2010-03-06 19:46:55 +00001748 Write uncompressed pixels as separate planes.
cristy3ed852e2009-09-05 21:47:34 +00001749 */
1750 channels=1;
cristya20214e2010-08-28 16:52:13 +00001751 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1752 compact_pixels=(unsigned char *) NULL;
1753 if (next_image->compression == RLECompression)
cristy4aff1572010-02-15 13:34:01 +00001754 {
cristya20214e2010-08-28 16:52:13 +00001755 compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels*
1756 next_image->columns,packet_size*sizeof(*compact_pixels));
1757 if (compact_pixels == (unsigned char *) NULL)
1758 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy4aff1572010-02-15 13:34:01 +00001759 }
cristy3ed852e2009-09-05 21:47:34 +00001760 i=0;
cristya20214e2010-08-28 16:52:13 +00001761 if (IsGrayImage(next_image,&next_image->exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001762 {
cristya20214e2010-08-28 16:52:13 +00001763 if (next_image->compression == RLECompression)
cristy4aff1572010-02-15 13:34:01 +00001764 {
1765 /*
1766 Packbits compression.
1767 */
1768 (void) WriteBlobMSBShort(image,1);
cristya20214e2010-08-28 16:52:13 +00001769 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy95119782010-06-01 17:40:58 +00001770 compact_pixels,GrayQuantum);
cristya20214e2010-08-28 16:52:13 +00001771 if (next_image->matte != MagickFalse)
1772 WritePackbitsLength(psd_info,image_info,image,next_image,
cristyb1459bc2010-03-23 21:41:43 +00001773 compact_pixels,AlphaQuantum);
cristy4aff1572010-02-15 13:34:01 +00001774 }
cristya20214e2010-08-28 16:52:13 +00001775 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1776 GrayQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1777 MagickFalse);
1778 if (next_image->matte != MagickFalse)
1779 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1780 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1781 MagickFalse);
cristy6886a752010-04-23 18:23:20 +00001782 (void) SetImageProgress(image,SaveImagesTag,0,1);
cristy3ed852e2009-09-05 21:47:34 +00001783 }
cristy0910f242010-04-01 18:55:09 +00001784 else
cristya20214e2010-08-28 16:52:13 +00001785 if (next_image->storage_class == PseudoClass)
cristy0910f242010-04-01 18:55:09 +00001786 {
cristya20214e2010-08-28 16:52:13 +00001787 if (next_image->compression == RLECompression)
cristy0910f242010-04-01 18:55:09 +00001788 {
1789 /*
1790 Packbits compression.
1791 */
1792 (void) WriteBlobMSBShort(image,1);
cristya20214e2010-08-28 16:52:13 +00001793 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy95119782010-06-01 17:40:58 +00001794 compact_pixels,IndexQuantum);
cristya20214e2010-08-28 16:52:13 +00001795 if (next_image->matte != MagickFalse)
1796 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy6886a752010-04-23 18:23:20 +00001797 compact_pixels,AlphaQuantum);
cristy0910f242010-04-01 18:55:09 +00001798 }
cristya20214e2010-08-28 16:52:13 +00001799 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1800 IndexQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1801 MagickFalse);
1802 if (next_image->matte != MagickFalse)
1803 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1804 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1805 MagickFalse);
cristy0910f242010-04-01 18:55:09 +00001806 (void) SetImageProgress(image,SaveImagesTag,0,1);
1807 }
1808 else
1809 {
cristya20214e2010-08-28 16:52:13 +00001810 if (next_image->colorspace == CMYKColorspace)
1811 (void) NegateImage(next_image,MagickFalse);
1812 if (next_image->compression == RLECompression)
cristy0910f242010-04-01 18:55:09 +00001813 {
1814 /*
1815 Packbits compression.
1816 */
1817 (void) WriteBlobMSBShort(image,1);
cristya20214e2010-08-28 16:52:13 +00001818 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy0910f242010-04-01 18:55:09 +00001819 compact_pixels,RedQuantum);
cristya20214e2010-08-28 16:52:13 +00001820 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy0910f242010-04-01 18:55:09 +00001821 compact_pixels,GreenQuantum);
cristya20214e2010-08-28 16:52:13 +00001822 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy0910f242010-04-01 18:55:09 +00001823 compact_pixels,BlueQuantum);
cristya20214e2010-08-28 16:52:13 +00001824 if (next_image->colorspace == CMYKColorspace)
1825 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy0910f242010-04-01 18:55:09 +00001826 compact_pixels,BlackQuantum);
cristya20214e2010-08-28 16:52:13 +00001827 if (next_image->matte != MagickFalse)
1828 WritePackbitsLength(psd_info,image_info,image,next_image,
cristy860cc732010-05-19 16:38:37 +00001829 compact_pixels,AlphaQuantum);
cristy0910f242010-04-01 18:55:09 +00001830 }
1831 (void) SetImageProgress(image,SaveImagesTag,0,6);
cristya20214e2010-08-28 16:52:13 +00001832 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1833 RedQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1834 MagickFalse);
cristy860cc732010-05-19 16:38:37 +00001835 (void) SetImageProgress(image,SaveImagesTag,1,6);
cristya20214e2010-08-28 16:52:13 +00001836 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1837 GreenQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1838 MagickFalse);
cristy860cc732010-05-19 16:38:37 +00001839 (void) SetImageProgress(image,SaveImagesTag,2,6);
cristya20214e2010-08-28 16:52:13 +00001840 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1841 BlueQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1842 MagickFalse);
cristy860cc732010-05-19 16:38:37 +00001843 (void) SetImageProgress(image,SaveImagesTag,3,6);
cristya20214e2010-08-28 16:52:13 +00001844 if (next_image->colorspace == CMYKColorspace)
1845 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1846 BlackQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1847 MagickFalse);
cristy860cc732010-05-19 16:38:37 +00001848 (void) SetImageProgress(image,SaveImagesTag,4,6);
cristya20214e2010-08-28 16:52:13 +00001849 if (next_image->matte != MagickFalse)
1850 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1851 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1852 MagickFalse);
cristy0910f242010-04-01 18:55:09 +00001853 (void) SetImageProgress(image,SaveImagesTag,5,6);
cristya20214e2010-08-28 16:52:13 +00001854 if (next_image->colorspace == CMYKColorspace)
1855 (void) NegateImage(next_image,MagickFalse);
cristy0910f242010-04-01 18:55:09 +00001856 }
cristya20214e2010-08-28 16:52:13 +00001857 if (next_image->compression == RLECompression)
1858 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
cristy3ed852e2009-09-05 21:47:34 +00001859 return(MagickTrue);
1860}
1861
cristy3ed852e2009-09-05 21:47:34 +00001862static void WritePascalString(Image* inImage,const char *inString,int inPad)
1863{
1864 size_t
cristya20214e2010-08-28 16:52:13 +00001865 length;
cristy3ed852e2009-09-05 21:47:34 +00001866
cristya20214e2010-08-28 16:52:13 +00001867 register ssize_t
1868 i;
cristy3ed852e2009-09-05 21:47:34 +00001869
cristya20214e2010-08-28 16:52:13 +00001870 /*
1871 Max length is 255.
1872 */
1873 length=(strlen(inString) > 255UL ) ? 255UL : strlen(inString);
1874 if (length == 0)
1875 (void) WriteBlobByte(inImage,0);
cristy3ed852e2009-09-05 21:47:34 +00001876 else
cristya20214e2010-08-28 16:52:13 +00001877 {
1878 (void) WriteBlobByte(inImage,(unsigned char) length);
1879 (void) WriteBlob(inImage, length, (const unsigned char *) inString);
1880 }
1881 length++;
1882 if ((length % inPad) == 0)
cristy3ed852e2009-09-05 21:47:34 +00001883 return;
cristya20214e2010-08-28 16:52:13 +00001884 for (i=0; i < (ssize_t) (inPad-(length % inPad)); i++)
cristy3ed852e2009-09-05 21:47:34 +00001885 (void) WriteBlobByte(inImage,0);
1886}
1887
1888static void WriteResolutionResourceBlock(Image *image)
1889{
cristy56ed31c2010-03-22 00:46:21 +00001890 double
1891 x_resolution,
1892 y_resolution;
cristy3ed852e2009-09-05 21:47:34 +00001893
1894 unsigned short
1895 units;
1896
1897 x_resolution=65536.0*image->x_resolution+0.5;
1898 y_resolution=65536.0*image->y_resolution+0.5;
1899 units=1;
1900 if (image->units == PixelsPerCentimeterResolution)
1901 {
1902 x_resolution=2.54*65536.0*image->x_resolution*0.5;
1903 y_resolution=2.54*65536.0*image->y_resolution+0.5;
1904 units=2;
1905 }
1906 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
1907 (void) WriteBlobMSBShort(image,0x03ED);
1908 (void) WriteBlobMSBShort(image,0);
1909 (void) WriteBlobMSBLong(image,16); /* resource size */
cristy56ed31c2010-03-22 00:46:21 +00001910 (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001911 (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */
1912 (void) WriteBlobMSBShort(image,units); /* width unit */
cristy56ed31c2010-03-22 00:46:21 +00001913 (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001914 (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */
1915 (void) WriteBlobMSBShort(image,units); /* height unit */
1916}
1917
cristyd4d3f742010-04-25 20:36:50 +00001918static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile)
1919{
cristy0b796e62010-04-29 00:38:45 +00001920 register const unsigned char
cristyd4d3f742010-04-25 20:36:50 +00001921 *p;
1922
1923 size_t
1924 length;
1925
1926 unsigned char
1927 *datum;
1928
cristy6befb0f2010-05-31 14:33:15 +00001929 unsigned int
cristyd4d3f742010-04-25 20:36:50 +00001930 count,
cristy6befb0f2010-05-31 14:33:15 +00001931 long_sans;
cristyd4d3f742010-04-25 20:36:50 +00001932
1933 unsigned short
1934 id,
1935 short_sans;
1936
1937 length=GetStringInfoLength(bim_profile);
1938 if (length < 16)
cristy0b796e62010-04-29 00:38:45 +00001939 return;
cristyd4d3f742010-04-25 20:36:50 +00001940 datum=GetStringInfoDatum(bim_profile);
1941 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
1942 {
cristy4176bb22010-05-01 16:29:09 +00001943 register unsigned char
1944 *q;
1945
1946 q=(unsigned char *) p;
cristyd4d3f742010-04-25 20:36:50 +00001947 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
1948 break;
cristy6befb0f2010-05-31 14:33:15 +00001949 p=PushLongPixel(MSBEndian,p,&long_sans);
cristyd4d3f742010-04-25 20:36:50 +00001950 p=PushShortPixel(MSBEndian,p,&id);
1951 p=PushShortPixel(MSBEndian,p,&short_sans);
1952 p=PushLongPixel(MSBEndian,p,&count);
1953 if (id == 0x0000040f)
1954 {
cristy4176bb22010-05-01 16:29:09 +00001955 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
1956 (PSDQuantum(count)+12)-(q-datum));
1957 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
cristyd4d3f742010-04-25 20:36:50 +00001958 break;
1959 }
1960 p+=count;
1961 if ((count & 0x01) != 0)
1962 p++;
1963 }
1964}
1965
cristyf11065e2010-05-14 13:26:59 +00001966static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile)
1967{
1968 register const unsigned char
1969 *p;
1970
1971 size_t
1972 length;
1973
1974 unsigned char
1975 *datum;
1976
cristy6befb0f2010-05-31 14:33:15 +00001977 unsigned int
cristyf11065e2010-05-14 13:26:59 +00001978 count,
cristy6befb0f2010-05-31 14:33:15 +00001979 long_sans;
cristyf11065e2010-05-14 13:26:59 +00001980
1981 unsigned short
1982 id,
1983 short_sans;
1984
1985 length=GetStringInfoLength(bim_profile);
1986 if (length < 16)
1987 return;
1988 datum=GetStringInfoDatum(bim_profile);
1989 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
1990 {
1991 register unsigned char
1992 *q;
1993
1994 q=(unsigned char *) p;
1995 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
1996 break;
cristy6befb0f2010-05-31 14:33:15 +00001997 p=PushLongPixel(MSBEndian,p,&long_sans);
cristyf11065e2010-05-14 13:26:59 +00001998 p=PushShortPixel(MSBEndian,p,&id);
1999 p=PushShortPixel(MSBEndian,p,&short_sans);
2000 p=PushLongPixel(MSBEndian,p,&count);
2001 if (id == 0x000003ed)
2002 {
2003 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2004 (PSDQuantum(count)+12)-(q-datum));
2005 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2006 break;
2007 }
2008 p+=count;
2009 if ((count & 0x01) != 0)
2010 p++;
2011 }
2012}
2013
cristy3ed852e2009-09-05 21:47:34 +00002014static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image)
2015{
2016 const char
cristya20214e2010-08-28 16:52:13 +00002017 *property;
cristy3ed852e2009-09-05 21:47:34 +00002018
2019 const StringInfo
cristy749d2152010-04-04 23:47:33 +00002020 *icc_profile;
cristy3ed852e2009-09-05 21:47:34 +00002021
cristya20214e2010-08-28 16:52:13 +00002022 Image
2023 *base_image,
2024 *next_image;
2025
cristy3ed852e2009-09-05 21:47:34 +00002026 MagickBooleanType
cristy3ed852e2009-09-05 21:47:34 +00002027 status;
2028
cristy875e28a2010-03-06 19:46:55 +00002029 PSDInfo
2030 psd_info;
2031
cristybb503372010-05-27 20:51:26 +00002032 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002033 i;
2034
2035 size_t
cristya20214e2010-08-28 16:52:13 +00002036 channel_size,
2037 channelLength,
2038 layer_count,
2039 layer_info_size,
cristy749d2152010-04-04 23:47:33 +00002040 length,
cristy3ed852e2009-09-05 21:47:34 +00002041 num_channels,
cristya20214e2010-08-28 16:52:13 +00002042 packet_size,
2043 rounded_layer_info_size;
cristy3ed852e2009-09-05 21:47:34 +00002044
cristy0b796e62010-04-29 00:38:45 +00002045 StringInfo
2046 *bim_profile;
2047
cristy3ed852e2009-09-05 21:47:34 +00002048 unsigned char
2049 layer_name[4];
2050
cristy3ed852e2009-09-05 21:47:34 +00002051 /*
cristy56ed31c2010-03-22 00:46:21 +00002052 Open image file.
cristy3ed852e2009-09-05 21:47:34 +00002053 */
2054 assert(image_info != (const ImageInfo *) NULL);
2055 assert(image_info->signature == MagickSignature);
2056 assert(image != (Image *) NULL);
2057 assert(image->signature == MagickSignature);
2058 if (image->debug != MagickFalse)
2059 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2060 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2061 if (status == MagickFalse)
2062 return(status);
2063 packet_size=(size_t) (image->depth > 8 ? 6 : 3);
2064 if (image->matte != MagickFalse)
2065 packet_size+=image->depth > 8 ? 2 : 1;
cristy875e28a2010-03-06 19:46:55 +00002066 psd_info.version=1;
2067 if ((LocaleCompare(image_info->magick,"PSB") == 0) ||
2068 (image->columns > 30000) || (image->rows > 30000))
2069 psd_info.version=2;
cristy50aea4a2010-03-09 17:37:44 +00002070 (void) WriteBlob(image,4,(const unsigned char *) "8BPS");
cristy875e28a2010-03-06 19:46:55 +00002071 (void) WriteBlobMSBShort(image,psd_info.version); /* version */
2072 for (i=1; i <= 6; i++)
2073 (void) WriteBlobByte(image, 0); /* 6 bytes of reserved */
cristy2045da32010-04-16 00:59:35 +00002074 if (IsGrayImage(image,&image->exception) != MagickFalse)
cristy6886a752010-04-23 18:23:20 +00002075 num_channels=(image->matte != MagickFalse ? 2UL : 1UL);
cristy3ed852e2009-09-05 21:47:34 +00002076 else
cristy0910f242010-04-01 18:55:09 +00002077 if (image->storage_class == PseudoClass)
cristy6886a752010-04-23 18:23:20 +00002078 num_channels=(image->matte != MagickFalse ? 2UL : 1UL);
cristy0910f242010-04-01 18:55:09 +00002079 else
2080 {
2081 if (image->colorspace != CMYKColorspace)
cristy6886a752010-04-23 18:23:20 +00002082 num_channels=(image->matte != MagickFalse ? 4UL : 3UL);
cristy0910f242010-04-01 18:55:09 +00002083 else
cristy6886a752010-04-23 18:23:20 +00002084 num_channels=(image->matte != MagickFalse ? 5UL : 4UL);
cristy0910f242010-04-01 18:55:09 +00002085 }
cristy3ed852e2009-09-05 21:47:34 +00002086 (void) WriteBlobMSBShort(image,(unsigned short) num_channels);
cristy56ed31c2010-03-22 00:46:21 +00002087 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
2088 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
cristy2045da32010-04-16 00:59:35 +00002089 if (IsGrayImage(image,&image->exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002090 {
cristy2045da32010-04-16 00:59:35 +00002091 MagickBooleanType
2092 monochrome;
2093
cristy0910f242010-04-01 18:55:09 +00002094 /*
2095 Write depth & mode.
2096 */
cristy0b55dd32010-06-13 19:04:44 +00002097 monochrome=IsMonochromeImage(image,&image->exception) &&
cristybf9f4ba2010-08-20 18:13:29 +00002098 (image->depth == 1) ? MagickTrue : MagickFalse;
cristy284c7d82010-04-24 00:19:14 +00002099 (void) WriteBlobMSBShort(image,(unsigned short)
2100 (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8));
cristya20214e2010-08-28 16:52:13 +00002101 (void) WriteBlobMSBShort(image,monochrome != MagickFalse ? BitmapMode :
2102 GrayscaleMode);
cristy3ed852e2009-09-05 21:47:34 +00002103 }
2104 else
2105 {
cristya20214e2010-08-28 16:52:13 +00002106 (void) WriteBlobMSBShort(image,(unsigned short) (image->storage_class ==
2107 PseudoClass ? 8 : image->depth > 8 ? 16 : 8));
cristy48845392010-06-02 01:19:17 +00002108 if (((image_info->colorspace != UndefinedColorspace) ||
cristy0910f242010-04-01 18:55:09 +00002109 (image->colorspace != CMYKColorspace)) &&
cristy48845392010-06-02 01:19:17 +00002110 (image_info->colorspace != CMYKColorspace))
cristy0910f242010-04-01 18:55:09 +00002111 {
2112 if (image->colorspace != RGBColorspace)
2113 (void) TransformImageColorspace(image,RGBColorspace);
2114 (void) WriteBlobMSBShort(image,(unsigned short)
cristy2045da32010-04-16 00:59:35 +00002115 (image->storage_class == PseudoClass ? IndexedMode : RGBMode));
cristy0910f242010-04-01 18:55:09 +00002116 }
2117 else
2118 {
cristy48845392010-06-02 01:19:17 +00002119 if (image->colorspace != CMYKColorspace)
cristy0910f242010-04-01 18:55:09 +00002120 (void) TransformImageColorspace(image,CMYKColorspace);
cristy2045da32010-04-16 00:59:35 +00002121 (void) WriteBlobMSBShort(image,CMYKMode);
cristy0910f242010-04-01 18:55:09 +00002122 }
cristy3ed852e2009-09-05 21:47:34 +00002123 }
cristyca9ddb02010-04-16 01:01:18 +00002124 if ((IsGrayImage(image,&image->exception) != MagickFalse) ||
2125 (image->storage_class == DirectClass) || (image->colors > 256))
cristy3ed852e2009-09-05 21:47:34 +00002126 (void) WriteBlobMSBLong(image,0);
2127 else
2128 {
2129 /*
2130 Write PSD raster colormap.
2131 */
2132 (void) WriteBlobMSBLong(image,768);
cristybb503372010-05-27 20:51:26 +00002133 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002134 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red));
2135 for ( ; i < 256; i++)
2136 (void) WriteBlobByte(image,0);
cristybb503372010-05-27 20:51:26 +00002137 for (i=0; i < (ssize_t) image->colors; i++)
cristya20214e2010-08-28 16:52:13 +00002138 (void) WriteBlobByte(image,ScaleQuantumToChar(
2139 image->colormap[i].green));
cristy3ed852e2009-09-05 21:47:34 +00002140 for ( ; i < 256; i++)
2141 (void) WriteBlobByte(image,0);
cristybb503372010-05-27 20:51:26 +00002142 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002143 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue));
2144 for ( ; i < 256; i++)
2145 (void) WriteBlobByte(image,0);
2146 }
2147 /*
2148 Image resource block.
2149 */
cristy749d2152010-04-04 23:47:33 +00002150 length=28; /* 0x03EB */
cristy0b796e62010-04-29 00:38:45 +00002151 bim_profile=(StringInfo *) GetImageProfile(image,"8bim");
cristy749d2152010-04-04 23:47:33 +00002152 icc_profile=GetImageProfile(image,"icc");
cristyd4d3f742010-04-25 20:36:50 +00002153 if (bim_profile != (StringInfo *) NULL)
2154 {
cristy0b796e62010-04-29 00:38:45 +00002155 bim_profile=CloneStringInfo(bim_profile);
cristyd4d3f742010-04-25 20:36:50 +00002156 if (icc_profile != (StringInfo *) NULL)
2157 RemoveICCProfileFromResourceBlock(bim_profile);
cristyf11065e2010-05-14 13:26:59 +00002158 RemoveResolutionFromResourceBlock(bim_profile);
cristyd4d3f742010-04-25 20:36:50 +00002159 length+=PSDQuantum(GetStringInfoLength(bim_profile));
2160 }
cristy0b796e62010-04-29 00:38:45 +00002161 if (icc_profile != (const StringInfo *) NULL)
cristy749d2152010-04-04 23:47:33 +00002162 length+=PSDQuantum(GetStringInfoLength(icc_profile))+12;
cristy284c7d82010-04-24 00:19:14 +00002163 (void) WriteBlobMSBLong(image,(unsigned int) length);
cristyf11065e2010-05-14 13:26:59 +00002164 WriteResolutionResourceBlock(image);
cristy4176bb22010-05-01 16:29:09 +00002165 if (bim_profile != (StringInfo *) NULL)
2166 {
2167 (void) WriteBlob(image,GetStringInfoLength(bim_profile),
2168 GetStringInfoDatum(bim_profile));
2169 bim_profile=DestroyStringInfo(bim_profile);
2170 }
cristy749d2152010-04-04 23:47:33 +00002171 if (icc_profile != (StringInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002172 {
cristy749d2152010-04-04 23:47:33 +00002173 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
cristy4176bb22010-05-01 16:29:09 +00002174 (void) WriteBlobMSBShort(image,0x0000040F);
cristy749d2152010-04-04 23:47:33 +00002175 (void) WriteBlobMSBShort(image,0);
cristy284c7d82010-04-24 00:19:14 +00002176 (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength(
2177 icc_profile));
cristy749d2152010-04-04 23:47:33 +00002178 (void) WriteBlob(image,GetStringInfoLength(icc_profile),
2179 GetStringInfoDatum(icc_profile));
cristye195f262010-04-16 18:12:35 +00002180 if ((MagickOffsetType) GetStringInfoLength(icc_profile) !=
cristy2045da32010-04-16 00:59:35 +00002181 PSDQuantum(GetStringInfoLength(icc_profile)))
cristy749d2152010-04-04 23:47:33 +00002182 (void) WriteBlobByte(image,0);
cristye6365592010-04-02 17:31:23 +00002183 }
cristy48845392010-06-02 01:19:17 +00002184 layer_count=0;
2185 layer_info_size=2;
cristy2837bcc2010-08-07 23:57:39 +00002186 base_image=GetNextImageInList(image);
2187 if ((image->matte != MagickFalse) && (base_image == (Image *) NULL))
2188 base_image=image;
cristya20214e2010-08-28 16:52:13 +00002189 next_image=base_image;
2190 while ( next_image != NULL )
2191 {
2192 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2193 if (IsGrayImage(next_image,&image->exception) != MagickFalse)
2194 num_channels=next_image->matte != MagickFalse ? 2UL : 1UL;
cristy3ed852e2009-09-05 21:47:34 +00002195 else
cristya20214e2010-08-28 16:52:13 +00002196 if (next_image->storage_class == PseudoClass)
2197 num_channels=next_image->matte != MagickFalse ? 2UL : 1UL;
cristy2045da32010-04-16 00:59:35 +00002198 else
cristya20214e2010-08-28 16:52:13 +00002199 if (next_image->colorspace != CMYKColorspace)
2200 num_channels=next_image->matte != MagickFalse ? 4UL : 3UL;
cristy2045da32010-04-16 00:59:35 +00002201 else
cristya20214e2010-08-28 16:52:13 +00002202 num_channels=next_image->matte != MagickFalse ? 5UL : 4UL;
2203 channelLength=(size_t) (next_image->columns*next_image->rows*packet_size+2);
cristy48845392010-06-02 01:19:17 +00002204 layer_info_size+=(size_t) (4*4+2+num_channels*6+(psd_info.version == 1 ? 8 :
2205 16)+4*1+4+num_channels*channelLength);
cristya20214e2010-08-28 16:52:13 +00002206 property=(const char *) GetImageProperty(next_image,"label");
2207 if (property == (const char *) NULL)
2208 layer_info_size+=16;
cristyde9b8f52010-03-12 01:17:00 +00002209 else
2210 {
cristya20214e2010-08-28 16:52:13 +00002211 size_t
2212 length;
2213
2214 length=strlen(property);
2215 layer_info_size+=8+length+(4-(length % 4));
cristyde9b8f52010-03-12 01:17:00 +00002216 }
cristy4aff1572010-02-15 13:34:01 +00002217 layer_count++;
cristya20214e2010-08-28 16:52:13 +00002218 next_image=GetNextImageInList(next_image);
cristy3ed852e2009-09-05 21:47:34 +00002219 }
cristy144f1b62010-05-18 00:52:09 +00002220 if (layer_count == 0)
cristy875e28a2010-03-06 19:46:55 +00002221 (void) SetPSDSize(&psd_info,image,0);
cristy3ed852e2009-09-05 21:47:34 +00002222 else
cristya20214e2010-08-28 16:52:13 +00002223 {
cristy8e9bd3e2010-08-29 00:03:56 +00002224 CompressionType
2225 compression;
2226
2227 (void) SetPSDSize(&psd_info,image,layer_info_size+
2228 (psd_info.version == 1 ? 8 : 16));
2229 if ((layer_info_size/2) != ((layer_info_size+1)/2))
2230 rounded_layer_info_size=layer_info_size+1;
cristya20214e2010-08-28 16:52:13 +00002231 else
cristy8e9bd3e2010-08-29 00:03:56 +00002232 rounded_layer_info_size=layer_info_size;
2233 (void) SetPSDSize(&psd_info,image,rounded_layer_info_size);
2234 (void) WriteBlobMSBShort(image,(unsigned short) layer_count);
2235 layer_count=1;
2236 compression=base_image->compression;
2237 next_image=base_image;
2238 while (next_image != NULL)
2239 {
2240 next_image->compression=NoCompression;
2241 (void) WriteBlobMSBLong(image,0);
2242 (void) WriteBlobMSBLong(image,0);
2243 (void) WriteBlobMSBLong(image,(unsigned int) next_image->rows);
2244 (void) WriteBlobMSBLong(image,(unsigned int) next_image->columns);
2245 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2246 channel_size=(unsigned int) ((packet_size*next_image->rows*
2247 next_image->columns)+2);
2248 if ((IsGrayImage(next_image,&image->exception) != MagickFalse) ||
2249 (next_image->storage_class == PseudoClass))
2250 {
2251 (void) WriteBlobMSBShort(image,(unsigned short)
2252 (next_image->matte != MagickFalse ? 2 : 1));
2253 (void) WriteBlobMSBShort(image,0);
2254 (void) SetPSDSize(&psd_info,image,channel_size);
2255 if (next_image->matte != MagickFalse)
2256 {
2257 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2258 (void) SetPSDSize(&psd_info,image,channel_size);
2259 }
2260 }
2261 else
2262 if (next_image->colorspace != CMYKColorspace)
2263 {
2264 (void) WriteBlobMSBShort(image,(unsigned short)
2265 (next_image->matte != MagickFalse ? 4 : 3));
2266 (void) WriteBlobMSBShort(image,0);
2267 (void) SetPSDSize(&psd_info,image,channel_size);
2268 (void) WriteBlobMSBShort(image,1);
2269 (void) SetPSDSize(&psd_info,image,channel_size);
2270 (void) WriteBlobMSBShort(image,2);
2271 (void) SetPSDSize(&psd_info,image,channel_size);
2272 if (next_image->matte!= MagickFalse )
2273 {
2274 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2275 (void) SetPSDSize(&psd_info,image,channel_size);
2276 }
2277 }
2278 else
2279 {
2280 (void) WriteBlobMSBShort(image,(unsigned short)
2281 (next_image->matte ? 5 : 4));
2282 (void) WriteBlobMSBShort(image,0);
2283 (void) SetPSDSize(&psd_info,image,channel_size);
2284 (void) WriteBlobMSBShort(image,1);
2285 (void) SetPSDSize(&psd_info,image,channel_size);
2286 (void) WriteBlobMSBShort(image,2);
2287 (void) SetPSDSize(&psd_info,image,channel_size);
2288 (void) WriteBlobMSBShort(image,3);
2289 (void) SetPSDSize(&psd_info,image,channel_size);
2290 if (next_image->matte)
2291 {
2292 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2293 (void) SetPSDSize(&psd_info,image,channel_size);
2294 }
2295 }
2296 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2297 (void) WriteBlob(image,4,(const unsigned char *)
2298 CompositeOperatorToPSDBlendMode(next_image->compose));
2299 (void) WriteBlobByte(image,255); /* layer opacity */
2300 (void) WriteBlobByte(image,0);
2301 (void) WriteBlobByte(image,1); /* layer propertys - visible, etc. */
2302 (void) WriteBlobByte(image,0);
2303 property=(const char *) GetImageProperty(next_image,"label");
2304 if (property == (const char *) NULL)
2305 {
2306 (void) WriteBlobMSBLong(image,16);
2307 (void) WriteBlobMSBLong(image,0);
2308 (void) WriteBlobMSBLong(image,0);
2309 (void) FormatMagickString((char *) layer_name,MaxTextExtent,
2310 "L%06ld",(long) layer_count++);
2311 WritePascalString( image, (char*)layer_name, 4 );
2312 }
2313 else
2314 {
2315 size_t
2316 length;
cristy3ed852e2009-09-05 21:47:34 +00002317
cristy8e9bd3e2010-08-29 00:03:56 +00002318 length=strlen(property);
2319 (void) WriteBlobMSBLong(image,(unsigned int) (length+(4-
2320 (length % 4))+8));
2321 (void) WriteBlobMSBLong(image,0);
2322 (void) WriteBlobMSBLong(image,0);
2323 WritePascalString(image,property,4);
2324 }
2325 next_image=GetNextImageInList(next_image);
2326 }
2327 /*
2328 Now the image data!
2329 */
2330 next_image=base_image;
2331 while (next_image != NULL)
2332 {
2333 status=WriteImageChannels(&psd_info,image_info,image,next_image,
2334 MagickTrue);
2335 next_image=GetNextImageInList(next_image);
2336 }
2337 (void) WriteBlobMSBLong(image,0); /* user mask data */
2338 base_image->compression=compression;
cristy144f1b62010-05-18 00:52:09 +00002339 }
cristy144f1b62010-05-18 00:52:09 +00002340 /*
2341 Write composite image.
2342 */
cristy15e25cb2010-03-13 20:31:29 +00002343 status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00002344 (void) CloseBlob(image);
2345 return(status);
2346}