2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Adobe Photoshop Image Format %
23 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
24 % dedicated to making software imaging solutions freely available. %
26 % You may not use this file except in compliance with the License. You may %
27 % obtain a copy of the License at %
29 % http://www.imagemagick.org/script/license.php %
31 % Unless required by applicable law or agreed to in writing, software %
32 % distributed under the License is distributed on an "AS IS" BASIS, %
33 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
34 % See the License for the specific language governing permissions and %
35 % limitations under the License. %
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 #include "MagickCore/studio.h"
46 #include "MagickCore/artifact.h"
47 #include "MagickCore/attribute.h"
48 #include "MagickCore/blob.h"
49 #include "MagickCore/blob-private.h"
50 #include "MagickCore/cache.h"
51 #include "MagickCore/colormap.h"
52 #include "MagickCore/colorspace.h"
53 #include "MagickCore/colorspace-private.h"
54 #include "MagickCore/constitute.h"
55 #include "MagickCore/enhance.h"
56 #include "MagickCore/exception.h"
57 #include "MagickCore/exception-private.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/log.h"
62 #include "MagickCore/magick.h"
63 #include "MagickCore/memory_.h"
64 #include "MagickCore/module.h"
65 #include "MagickCore/monitor-private.h"
66 #include "MagickCore/pixel.h"
67 #include "MagickCore/pixel-accessor.h"
68 #include "MagickCore/profile.h"
69 #include "MagickCore/property.h"
70 #include "MagickCore/quantum-private.h"
71 #include "MagickCore/static.h"
72 #include "MagickCore/string_.h"
73 #ifdef MAGICKCORE_ZLIB_DELEGATE
80 #define MaxPSDChannels 56
81 #define PSDQuantum(x) (((ssize_t) (x)+1) & -2)
84 Enumerated declaractions.
90 ZipWithoutPrediction = 2,
101 MultichannelMode = 7,
107 Typedef declaractions.
109 typedef struct _ChannelInfo
118 typedef struct _LayerInfo
128 channel_info[MaxPSDChannels];
152 typedef struct _PSDInfo
174 Forward declarations.
176 static MagickBooleanType
177 WritePSDImage(const ImageInfo *,Image *,ExceptionInfo *);
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 % IsPSD()() returns MagickTrue if the image format type, identified by the
191 % magick string, is PSD.
193 % The format of the IsPSD method is:
195 % MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
197 % A description of each parameter follows:
199 % o magick: compare image format pattern against these bytes.
201 % o length: Specifies the length of the magick string.
204 static MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
208 if (LocaleNCompare((const char *) magick,"8BPS",4) == 0)
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 % R e a d P S D I m a g e %
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 % ReadPSDImage() reads an Adobe Photoshop image file and returns it. It
225 % allocates the memory necessary for the new Image structure and returns a
226 % pointer to the new image.
228 % The format of the ReadPSDImage method is:
230 % Image *ReadPSDImage(image_info)
232 % A description of each parameter follows:
234 % o image_info: the image info.
236 % o exception: return any errors or warnings in this structure.
240 static const char *CompositeOperatorToPSDBlendMode(CompositeOperator op)
247 case OverCompositeOp: blend_mode = "norm"; break;
248 case MultiplyCompositeOp: blend_mode = "mul "; break;
249 case DissolveCompositeOp: blend_mode = "diss"; break;
250 case DifferenceCompositeOp: blend_mode = "diff"; break;
251 case DarkenCompositeOp: blend_mode = "dark"; break;
252 case LightenCompositeOp: blend_mode = "lite"; break;
253 case HueCompositeOp: blend_mode = "hue "; break;
254 case SaturateCompositeOp: blend_mode = "sat "; break;
255 case ColorizeCompositeOp: blend_mode = "colr"; break;
256 case LuminizeCompositeOp: blend_mode = "lum "; break;
257 case ScreenCompositeOp: blend_mode = "scrn"; break;
258 case OverlayCompositeOp: blend_mode = "over"; break;
265 static inline CompressionType ConvertPSDCompression(
266 PSDCompressionType compression)
271 return RLECompression;
272 case ZipWithPrediction:
273 case ZipWithoutPrediction:
274 return ZipCompression;
276 return NoCompression;
280 static MagickStatusType CorrectPSDOpacity(LayerInfo* layer_info,
281 ExceptionInfo *exception)
292 if (layer_info->opacity == OpaqueAlpha)
295 layer_info->image->alpha_trait=BlendPixelTrait;
296 for (y=0; y < (ssize_t) layer_info->image->rows; y++)
298 q=GetAuthenticPixels(layer_info->image,0,y,layer_info->image->columns,1,
300 if (q == (Quantum *) NULL)
302 for (x=0; x < (ssize_t) layer_info->image->columns; x++)
304 SetPixelAlpha(layer_info->image,(Quantum) (QuantumScale*(GetPixelAlpha(
305 layer_info->image,q))*layer_info->opacity),q);
306 q+=GetPixelChannels(layer_info->image);
308 if (SyncAuthenticPixels(layer_info->image,exception) == MagickFalse)
315 static ssize_t DecodePSDPixels(const size_t number_compact_pixels,
316 const unsigned char *compact_pixels,const ssize_t depth,
317 const size_t number_pixels,unsigned char *pixels)
332 packets=(ssize_t) number_compact_pixels;
333 for (i=0; (packets > 1) && (i < (ssize_t) number_pixels); )
335 length=(size_t) (*compact_pixels++);
342 if (((ssize_t) length+i) > (ssize_t) number_pixels)
343 length=number_pixels-(size_t) i;
344 pixel=(*compact_pixels++);
346 for (j=0; j < (ssize_t) length; j++)
352 *pixels++=(pixel >> 7) & 0x01 ? 0U : 255U;
353 *pixels++=(pixel >> 6) & 0x01 ? 0U : 255U;
354 *pixels++=(pixel >> 5) & 0x01 ? 0U : 255U;
355 *pixels++=(pixel >> 4) & 0x01 ? 0U : 255U;
356 *pixels++=(pixel >> 3) & 0x01 ? 0U : 255U;
357 *pixels++=(pixel >> 2) & 0x01 ? 0U : 255U;
358 *pixels++=(pixel >> 1) & 0x01 ? 0U : 255U;
359 *pixels++=(pixel >> 0) & 0x01 ? 0U : 255U;
365 *pixels++=(unsigned char) ((pixel >> 4) & 0xff);
366 *pixels++=(unsigned char) ((pixel & 0x0f) & 0xff);
372 *pixels++=(unsigned char) ((pixel >> 6) & 0x03);
373 *pixels++=(unsigned char) ((pixel >> 4) & 0x03);
374 *pixels++=(unsigned char) ((pixel >> 2) & 0x03);
375 *pixels++=(unsigned char) ((pixel & 0x03) & 0x03);
381 *pixels++=(unsigned char) pixel;
390 if (((ssize_t) length+i) > (ssize_t) number_pixels)
391 length=number_pixels-(size_t) i;
392 for (j=0; j < (ssize_t) length; j++)
398 *pixels++=(*compact_pixels >> 7) & 0x01 ? 0U : 255U;
399 *pixels++=(*compact_pixels >> 6) & 0x01 ? 0U : 255U;
400 *pixels++=(*compact_pixels >> 5) & 0x01 ? 0U : 255U;
401 *pixels++=(*compact_pixels >> 4) & 0x01 ? 0U : 255U;
402 *pixels++=(*compact_pixels >> 3) & 0x01 ? 0U : 255U;
403 *pixels++=(*compact_pixels >> 2) & 0x01 ? 0U : 255U;
404 *pixels++=(*compact_pixels >> 1) & 0x01 ? 0U : 255U;
405 *pixels++=(*compact_pixels >> 0) & 0x01 ? 0U : 255U;
411 *pixels++=(*compact_pixels >> 4) & 0xff;
412 *pixels++=(*compact_pixels & 0x0f) & 0xff;
418 *pixels++=(*compact_pixels >> 6) & 0x03;
419 *pixels++=(*compact_pixels >> 4) & 0x03;
420 *pixels++=(*compact_pixels >> 2) & 0x03;
421 *pixels++=(*compact_pixels & 0x03) & 0x03;
427 *pixels++=(*compact_pixels);
438 static inline LayerInfo *DestroyLayerInfo(LayerInfo *layer_info,
439 const ssize_t number_layers)
444 for (i=0; i<number_layers; i++)
446 if (layer_info[i].image != (Image *) NULL)
447 layer_info[i].image=DestroyImage(layer_info[i].image);
450 return (LayerInfo *) RelinquishMagickMemory(layer_info);
453 static inline size_t GetPSDPacketSize(Image *image)
455 if (image->storage_class == PseudoClass)
457 if (image->colors > 256)
459 else if (image->depth > 8)
463 if (image->depth > 8)
469 static inline MagickSizeType GetPSDSize(PSDInfo *psd_info,Image *image)
471 if (psd_info->version == 1)
472 return((MagickSizeType) ReadBlobMSBLong(image));
473 return((MagickSizeType) ReadBlobMSBLongLong(image));
476 static inline size_t GetPSDRowSize(Image *image)
478 if (image->depth == 1)
479 return((image->columns+7)/8);
481 return(image->columns*GetPSDPacketSize(image));
484 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
491 static const char *ModeToString(PSDImageType type)
495 case BitmapMode: return "Bitmap";
496 case GrayscaleMode: return "Grayscale";
497 case IndexedMode: return "Indexed";
498 case RGBMode: return "RGB";
499 case CMYKMode: return "CMYK";
500 case MultichannelMode: return "Multichannel";
501 case DuotoneMode: return "Duotone";
502 case LabMode: return "L*A*B";
503 default: return "unknown";
507 static MagickBooleanType ParseImageResourceBlocks(Image *image,
508 const unsigned char *blocks,size_t length,ExceptionInfo *exception)
526 profile=BlobToStringInfo((const void *) NULL,length);
527 SetStringInfoDatum(profile,blocks);
528 (void) SetImageProfile(image,"8bim",profile,exception);
529 profile=DestroyStringInfo(profile);
530 for (p=blocks; (p >= blocks) && (p < (blocks+length-16)); )
532 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
534 p=PushLongPixel(MSBEndian,p,&long_sans);
535 p=PushShortPixel(MSBEndian,p,&id);
536 p=PushShortPixel(MSBEndian,p,&short_sans);
537 p=PushLongPixel(MSBEndian,p,&count);
543 value[MaxTextExtent];
551 p=PushShortPixel(MSBEndian,p,&resolution);
552 image->resolution.x=(double) resolution;
553 (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.x);
554 (void) SetImageProperty(image,"tiff:XResolution",value,exception);
555 p=PushShortPixel(MSBEndian,p,&short_sans);
556 p=PushShortPixel(MSBEndian,p,&short_sans);
557 p=PushShortPixel(MSBEndian,p,&short_sans);
558 p=PushShortPixel(MSBEndian,p,&resolution);
559 image->resolution.y=(double) resolution;
560 (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.y);
561 (void) SetImageProperty(image,"tiff:YResolution",value,exception);
562 p=PushShortPixel(MSBEndian,p,&short_sans);
563 p=PushShortPixel(MSBEndian,p,&short_sans);
564 p=PushShortPixel(MSBEndian,p,&short_sans);
565 image->units=PixelsPerInchResolution;
574 if ((count & 0x01) != 0)
580 static CompositeOperator PSDBlendModeToCompositeOperator(const char *mode)
582 if (mode == (const char *) NULL)
583 return(OverCompositeOp);
584 if (LocaleNCompare(mode,"norm",4) == 0)
585 return(OverCompositeOp);
586 if (LocaleNCompare(mode,"mul ",4) == 0)
587 return(MultiplyCompositeOp);
588 if (LocaleNCompare(mode,"diss",4) == 0)
589 return(DissolveCompositeOp);
590 if (LocaleNCompare(mode,"diff",4) == 0)
591 return(DifferenceCompositeOp);
592 if (LocaleNCompare(mode,"dark",4) == 0)
593 return(DarkenCompositeOp);
594 if (LocaleNCompare(mode,"lite",4) == 0)
595 return(LightenCompositeOp);
596 if (LocaleNCompare(mode,"hue ",4) == 0)
597 return(HueCompositeOp);
598 if (LocaleNCompare(mode,"sat ",4) == 0)
599 return(SaturateCompositeOp);
600 if (LocaleNCompare(mode,"colr",4) == 0)
601 return(ColorizeCompositeOp);
602 if (LocaleNCompare(mode,"lum ",4) == 0)
603 return(LuminizeCompositeOp);
604 if (LocaleNCompare(mode,"scrn",4) == 0)
605 return(ScreenCompositeOp);
606 if (LocaleNCompare(mode,"over",4) == 0)
607 return(OverlayCompositeOp);
608 if (LocaleNCompare(mode,"hLit",4) == 0)
609 return(OverCompositeOp);
610 if (LocaleNCompare(mode,"sLit",4) == 0)
611 return(OverCompositeOp);
612 if (LocaleNCompare(mode,"smud",4) == 0)
613 return(OverCompositeOp);
614 if (LocaleNCompare(mode,"div ",4) == 0)
615 return(OverCompositeOp);
616 if (LocaleNCompare(mode,"idiv",4) == 0)
617 return(OverCompositeOp);
618 return(OverCompositeOp);
621 static MagickStatusType ReadPSDChannelPixels(Image *image,
622 const size_t channels,const size_t row,const ssize_t type,
623 const unsigned char *pixels,ExceptionInfo *exception)
628 register const unsigned char
644 q=GetAuthenticPixels(image,0,row,image->columns,1,exception);
645 if (q == (Quantum *) NULL)
647 packet_size=GetPSDPacketSize(image);
648 for (x=0; x < (ssize_t) image->columns; x++)
650 if (packet_size == 1)
651 pixel=ScaleCharToQuantum(*p++);
654 p=PushShortPixel(MSBEndian,p,&nibble);
655 pixel=ScaleShortToQuantum(nibble);
661 SetPixelAlpha(image,pixel,q);
666 SetPixelRed(image,pixel,q);
668 SetPixelGray(image,pixel,q);
670 SetPixelRed(image,pixel,q);
671 if (image->storage_class == PseudoClass)
673 if (packet_size == 1)
674 SetPixelIndex(image,ScaleQuantumToChar(pixel),q);
676 SetPixelIndex(image,ScaleQuantumToShort(pixel),q);
677 SetPixelInfoPixel(image,image->colormap+(ssize_t)
678 GetPixelIndex(image,q),q);
679 if (image->depth == 1)
685 number_bits=image->columns-x;
688 for (bit=0; bit < number_bits; bit++)
690 SetPixelIndex(image,(((unsigned char) pixel) &
691 (0x01 << (7-bit))) != 0 ? 0 : 255,q);
692 SetPixelInfoPixel(image,image->colormap+(ssize_t)
693 GetPixelIndex(image,q),q);
694 q+=GetPixelChannels(image);
703 if (image->storage_class == PseudoClass)
704 SetPixelAlpha(image,pixel,q);
706 SetPixelGreen(image,pixel,q);
711 if (image->storage_class == PseudoClass)
712 SetPixelAlpha(image,pixel,q);
714 SetPixelBlue(image,pixel,q);
719 if (image->colorspace == CMYKColorspace)
720 SetPixelBlack(image,pixel,q);
722 if (image->alpha_trait == BlendPixelTrait)
723 SetPixelAlpha(image,pixel,q);
728 if ((IssRGBCompatibleColorspace(image->colorspace) != MagickFalse) &&
731 if (image->alpha_trait == BlendPixelTrait)
732 SetPixelAlpha(image,pixel,q);
738 q+=GetPixelChannels(image);
740 return(SyncAuthenticPixels(image,exception));
743 static MagickStatusType ReadPSDChannelRaw(Image *image,const size_t channels,
744 const ssize_t type,ExceptionInfo *exception)
759 if (image->debug != MagickFalse)
760 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
761 " layer data is RAW");
763 row_size=GetPSDRowSize(image);
764 pixels=(unsigned char *) AcquireQuantumMemory(row_size,sizeof(*pixels));
765 if (pixels == (unsigned char *) NULL)
766 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
770 for (y=0; y < (ssize_t) image->rows; y++)
774 count=ReadBlob(image,row_size,pixels);
775 if (count != row_size)
778 status=ReadPSDChannelPixels(image,channels,y,type,pixels,exception);
779 if (status == MagickFalse)
783 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
787 static inline MagickOffsetType *ReadPSDRLEOffsets(Image *image,
788 PSDInfo *psd_info,const size_t size)
796 offsets=(MagickOffsetType *) AcquireQuantumMemory(size,sizeof(*offsets));
797 if(offsets != (MagickOffsetType *) NULL)
799 for (y=0; y < (ssize_t) size; y++)
801 if (psd_info->version == 1)
802 offsets[y]=(MagickOffsetType) ReadBlobMSBShort(image);
804 offsets[y]=(MagickOffsetType) ReadBlobMSBLong(image);
810 static MagickStatusType ReadPSDChannelRLE(Image *image,PSDInfo *psd_info,
811 const ssize_t type,MagickOffsetType *offsets,ExceptionInfo *exception)
828 if (image->debug != MagickFalse)
829 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
830 " layer data is RLE compressed");
832 row_size=GetPSDRowSize(image);
833 pixels=(unsigned char *) AcquireQuantumMemory(row_size,sizeof(*pixels));
834 if (pixels == (unsigned char *) NULL)
835 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
839 for (y=0; y < (ssize_t) image->rows; y++)
840 if ((MagickOffsetType) length < offsets[y])
841 length=(size_t) offsets[y];
843 if (length > row_size + 256) // arbitrary number
845 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
846 ThrowBinaryException(ResourceLimitError,"InvalidLength",
850 compact_pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
851 if (compact_pixels == (unsigned char *) NULL)
853 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
854 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
858 (void) ResetMagickMemory(compact_pixels,0,length*sizeof(*compact_pixels));
861 for (y=0; y < (ssize_t) image->rows; y++)
865 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
866 if (count != (ssize_t) offsets[y])
869 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
870 (ssize_t) (image->depth == 1 ? 123456 : image->depth),row_size,pixels);
871 if (count != (ssize_t) row_size)
874 status=ReadPSDChannelPixels(image,psd_info->channels,y,type,pixels,
876 if (status == MagickFalse)
880 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
881 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
885 static MagickStatusType ReadPSDChannelZip(Image *image,
886 const size_t channels,const ssize_t type,
887 const PSDCompressionType compression,const size_t compact_size,
888 ExceptionInfo *exception)
890 #ifdef MAGICKCORE_ZLIB_DELEGATE
894 register unsigned char
913 if (image->debug != MagickFalse)
914 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
915 " layer data is ZIP compressed");
917 compact_pixels=(unsigned char *) AcquireQuantumMemory(compact_size,
918 sizeof(*compact_pixels));
919 if (compact_pixels == (unsigned char *) NULL)
920 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
923 packet_size=GetPSDPacketSize(image);
924 row_size=image->columns*packet_size;
925 count=image->rows*row_size;
927 pixels=(unsigned char *) AcquireQuantumMemory(count,sizeof(*pixels));
928 if (pixels == (unsigned char *) NULL)
930 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
931 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
935 ResetMagickMemory(&stream, 0, sizeof(z_stream));
936 stream.data_type=Z_BINARY;
937 (void) ReadBlob(image,compact_size,compact_pixels);
939 stream.next_in=(Bytef *)compact_pixels;
940 stream.avail_in=compact_size;
941 stream.next_out=(Bytef *)pixels;
942 stream.avail_out=count;
944 if(inflateInit(&stream) == Z_OK)
949 while (stream.avail_out > 0)
951 ret=inflate(&stream, Z_SYNC_FLUSH);
952 if (ret != Z_OK && ret != Z_STREAM_END)
954 compact_pixels=(unsigned char *) RelinquishMagickMemory(
956 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
962 if (compression == ZipWithPrediction)
967 length=image->columns;
970 if (packet_size == 2)
972 p[2]+=p[0]+((p[1]+p[3]) >> 8);
986 for (y=0; y < (ssize_t) image->rows; y++)
988 status=ReadPSDChannelPixels(image,channels,y,type,p,exception);
989 if (status == MagickFalse)
995 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
996 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
999 magick_unreferenced(image);
1000 magick_unreferenced(channels);
1001 magick_unreferenced(type);
1002 magick_unreferenced(compression);
1003 magick_unreferenced(compact_size);
1004 magick_unreferenced(exception);
1005 return(MagickFalse);
1009 static MagickStatusType ReadPSDChannel(Image *image,PSDInfo *psd_info,
1010 const LayerInfo* layer_info,const size_t channel,
1011 const PSDCompressionType compression,ExceptionInfo *exception)
1019 if (layer_info->channel_info[channel].type < -1)
1021 /* ignore user supplied layer mask */
1022 SeekBlob(image,layer_info->channel_info[channel].size-2,SEEK_CUR);
1026 offset=TellBlob(image);
1031 return(ReadPSDChannelRaw(image,psd_info->channels,
1032 layer_info->channel_info[channel].type,exception));
1038 offsets=ReadPSDRLEOffsets(image,psd_info,image->rows);
1039 if (offsets == (MagickOffsetType *) NULL)
1040 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1042 status=ReadPSDChannelRLE(image,psd_info,
1043 layer_info->channel_info[channel].type,offsets,exception);
1044 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1047 case ZipWithPrediction:
1048 case ZipWithoutPrediction:
1049 #ifdef MAGICKCORE_ZLIB_DELEGATE
1050 status=ReadPSDChannelZip(image,layer_info->channels,
1051 layer_info->channel_info[channel].type,compression,
1052 layer_info->channel_info[channel].size-2,exception);
1054 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1055 (void) ThrowMagickException(exception,GetMagickModule(),
1056 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn",
1057 "'%s' (ZLIB)",image->filename);
1061 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1062 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
1063 "CompressionNotSupported","'%.20g'",(double) compression);
1067 if (status == MagickFalse)
1068 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1073 static MagickStatusType ReadPSDLayer(Image *image,PSDInfo *psd_info,
1074 LayerInfo* layer_info,ExceptionInfo *exception)
1077 message[MaxTextExtent];
1088 if (image->debug != MagickFalse)
1089 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1090 " setting up new layer image");
1091 (void) SetImageBackgroundColor(layer_info->image,exception);
1092 layer_info->image->compose=PSDBlendModeToCompositeOperator(
1093 layer_info->blendkey);
1094 if (layer_info->visible == MagickFalse)
1095 layer_info->image->compose=NoCompositeOp;
1096 if (psd_info->mode == CMYKMode)
1097 SetImageColorspace(layer_info->image,CMYKColorspace,exception);
1098 if ((psd_info->mode == BitmapMode) || (psd_info->mode == GrayscaleMode) ||
1099 (psd_info->mode == DuotoneMode))
1100 SetImageColorspace(layer_info->image,GRAYColorspace,exception);
1102 Set up some hidden attributes for folks that need them.
1104 (void) FormatLocaleString(message,MaxTextExtent,"%.20gld",
1105 (double) layer_info->page.x);
1106 (void) SetImageArtifact(layer_info->image,"psd:layer.x",message);
1107 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",
1108 (double) layer_info->page.y);
1109 (void) SetImageArtifact(layer_info->image,"psd:layer.y",message);
1110 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",(double)
1111 layer_info->opacity);
1112 (void) SetImageArtifact(layer_info->image,"psd:layer.opacity",message);
1113 (void) SetImageProperty(layer_info->image,"label",(char *) layer_info->name,
1117 for (j=0; j < (ssize_t) layer_info->channels; j++)
1119 if (image->debug != MagickFalse)
1120 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1121 " reading data for channel %.20g",(double) j);
1123 compression=(PSDCompressionType) ReadBlobMSBShort(layer_info->image);
1124 layer_info->image->compression=ConvertPSDCompression(compression);
1125 if (layer_info->channel_info[j].type == -1)
1126 layer_info->image->alpha_trait=BlendPixelTrait;
1127 status=ReadPSDChannel(layer_info->image,psd_info,layer_info,j,
1128 compression,exception);
1130 if (status == MagickFalse)
1134 if (status != MagickFalse)
1135 status=CorrectPSDOpacity(layer_info,exception);
1137 if (status != MagickFalse && layer_info->image->colorspace == CMYKColorspace)
1138 (void) NegateImage(layer_info->image,MagickFalse,exception);
1143 static MagickStatusType ReadPSDLayers(Image *image,PSDInfo *psd_info,
1144 MagickBooleanType skip_layers,ExceptionInfo *exception)
1166 size=GetPSDSize(psd_info,image);
1173 Skip layers & masks.
1175 quantum=psd_info->version == 1 ? 4UL : 8UL;
1176 (void) ReadBlobMSBLong(image);
1177 count=ReadBlob(image,4,(unsigned char *) type);
1178 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1180 if (DiscardBlobBytes(image,size-quantum-8) == MagickFalse)
1181 ThrowFileException(exception,CorruptImageError,
1182 "UnexpectedEndOfFile",image->filename);
1186 count=ReadBlob(image,4,(unsigned char *) type);
1187 if ((count != 0) && (LocaleNCompare(type,"Lr16",4) == 0))
1188 size=GetPSDSize(psd_info,image);
1190 if (DiscardBlobBytes(image,size-quantum-12) == MagickFalse)
1191 ThrowFileException(exception,CorruptImageError,
1192 "UnexpectedEndOfFile",image->filename);
1199 layer_info=(LayerInfo *) NULL;
1200 number_layers=(short) ReadBlobMSBShort(image);
1202 if (number_layers < 0)
1205 The first alpha channel in the merged result contains the
1206 transparency data for the merged result.
1208 number_layers=MagickAbsoluteValue(number_layers);
1209 if (image->debug != MagickFalse)
1210 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1211 " negative layer count corrected for");
1212 image->alpha_trait=BlendPixelTrait;
1215 if (skip_layers != MagickFalse)
1218 if (image->debug != MagickFalse)
1219 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1220 " image contains %.20g layers",(double) number_layers);
1222 if (number_layers == 0)
1223 return(MagickFalse);
1225 layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers,
1226 sizeof(*layer_info));
1227 if (layer_info == (LayerInfo *) NULL)
1229 if (image->debug != MagickFalse)
1230 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1231 " allocation of LayerInfo failed");
1232 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1235 (void) ResetMagickMemory(layer_info,0,(size_t) number_layers*
1236 sizeof(*layer_info));
1238 for (i=0; i < number_layers; i++)
1244 if (image->debug != MagickFalse)
1245 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1246 " reading layer #%.20g",(double) i+1);
1247 layer_info[i].page.y=(int) ReadBlobMSBLong(image);
1248 layer_info[i].page.x=(int) ReadBlobMSBLong(image);
1249 y=(int) ReadBlobMSBLong(image);
1250 x=(int) ReadBlobMSBLong(image);
1251 layer_info[i].page.width=(ssize_t) (x-layer_info[i].page.x);
1252 layer_info[i].page.height=(ssize_t) (y-layer_info[i].page.y);
1253 layer_info[i].channels=ReadBlobMSBShort(image);
1254 if (layer_info[i].channels > MaxPSDChannels)
1256 layer_info=DestroyLayerInfo(layer_info,number_layers);
1257 ThrowBinaryException(CorruptImageError,"MaximumChannelsExceeded",
1260 if (image->debug != MagickFalse)
1261 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1262 " offset(%.20g,%.20g), size(%.20g,%.20g), channels=%.20g",
1263 (double) layer_info[i].page.x,(double) layer_info[i].page.y,
1264 (double) layer_info[i].page.height,(double)
1265 layer_info[i].page.width,(double) layer_info[i].channels);
1266 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1268 layer_info[i].channel_info[j].type=(short) ReadBlobMSBShort(image);
1269 layer_info[i].channel_info[j].size=(size_t) GetPSDSize(psd_info,
1271 if (image->debug != MagickFalse)
1272 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1273 " channel[%.20g]: type=%.20g, size=%.20g",(double) j,
1274 (double) layer_info[i].channel_info[j].type,
1275 (double) layer_info[i].channel_info[j].size);
1277 count=ReadBlob(image,4,(unsigned char *) type);
1278 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1280 if (image->debug != MagickFalse)
1281 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1282 " layer type was %.4s instead of 8BIM", type);
1283 layer_info=DestroyLayerInfo(layer_info,number_layers);
1284 ThrowBinaryException(CorruptImageError,"ImproperImageHeader",
1287 count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey);
1288 layer_info[i].opacity=(Quantum) ScaleCharToQuantum((unsigned char)
1289 ReadBlobByte(image));
1290 layer_info[i].clipping=(unsigned char) ReadBlobByte(image);
1291 layer_info[i].flags=(unsigned char) ReadBlobByte(image);
1292 layer_info[i].visible=!(layer_info[i].flags & 0x02);
1293 if (image->debug != MagickFalse)
1294 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1295 " blend=%.4s, opacity=%.20g, clipping=%s, flags=%d, visible=%s",
1296 layer_info[i].blendkey,(double) layer_info[i].opacity,
1297 layer_info[i].clipping ? "true" : "false",layer_info[i].flags,
1298 layer_info[i].visible ? "true" : "false");
1299 (void) ReadBlobByte(image); /* filler */
1301 size=ReadBlobMSBLong(image);
1308 if (image->debug != MagickFalse)
1309 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1310 " layer contains additional info");
1311 length=ReadBlobMSBLong(image);
1312 combined_length=length+4;
1318 layer_info[i].mask.y=(int) ReadBlobMSBLong(image);
1319 layer_info[i].mask.x=(int) ReadBlobMSBLong(image);
1320 layer_info[i].mask.height=(size_t)
1321 (ReadBlobMSBLong(image)-layer_info[i].mask.y);
1322 layer_info[i].mask.width=(size_t)
1323 (ReadBlobMSBLong(image)-layer_info[i].mask.x);
1324 if (image->debug != MagickFalse)
1325 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1326 " layer mask: offset(%.20g,%.20g), size(%.20g,%.20g), length=%.20g",
1327 (double) layer_info[i].mask.x,(double)
1328 layer_info[i].mask.y,(double) layer_info[i].mask.width,
1329 (double) layer_info[i].mask.height,(double)
1330 ((MagickOffsetType) length)-16);
1332 Skip over the rest of the layer mask information.
1334 if (DiscardBlobBytes(image,length-16) == MagickFalse)
1336 layer_info=DestroyLayerInfo(layer_info,number_layers);
1337 ThrowFileException(exception,CorruptImageError,
1338 "UnexpectedEndOfFile",image->filename);
1341 length=ReadBlobMSBLong(image);
1342 combined_length+=length+4;
1346 Layer blending ranges info.
1348 if (image->debug != MagickFalse)
1349 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1350 " layer blending ranges: length=%.20g",(double)
1351 ((MagickOffsetType) length));
1353 We read it, but don't use it...
1355 for (j=0; j < (ssize_t) (length); j+=8)
1357 size_t blend_source=ReadBlobMSBLong(image);
1358 size_t blend_dest=ReadBlobMSBLong(image);
1359 if (image->debug != MagickFalse)
1360 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1361 " source(%x), dest(%x)",(unsigned int)
1362 blend_source,(unsigned int) blend_dest);
1368 length=(size_t) ReadBlobByte(image);
1369 combined_length+=length+1;
1370 for (j=0; j < (ssize_t) length; j++)
1371 layer_info[i].name[j]=(unsigned char) ReadBlobByte(image);
1372 layer_info[i].name[j]='\0';
1373 if (image->debug != MagickFalse)
1374 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1375 " layer name: %s",layer_info[i].name);
1377 Skip the rest of the variable data until we support it.
1379 if (image->debug != MagickFalse)
1380 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1381 " unsupported data: length=%.20g",(double)
1382 ((MagickOffsetType) (size-combined_length)));
1383 if (DiscardBlobBytes(image,size-combined_length) == MagickFalse)
1385 layer_info=DestroyLayerInfo(layer_info,number_layers);
1386 ThrowBinaryException(CorruptImageError,
1387 "UnexpectedEndOfFile",image->filename);
1392 for (i=0; i < number_layers; i++)
1394 if ((layer_info[i].page.width == 0) ||
1395 (layer_info[i].page.height == 0))
1397 if (image->debug != MagickFalse)
1398 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1399 " layer data is empty");
1404 Allocate layered image.
1406 layer_info[i].image=CloneImage(image,layer_info[i].page.width,
1407 layer_info[i].page.height,MagickFalse,exception);
1408 if (layer_info[i].image == (Image *) NULL)
1410 layer_info=DestroyLayerInfo(layer_info,number_layers);
1411 if (image->debug != MagickFalse)
1412 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1413 " allocation of image for layer %.20g failed",(double) i);
1414 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1419 for (i=0; i < number_layers; i++)
1421 if (layer_info[i].image == (Image *) NULL)
1423 for (j=0; j < layer_info[i].channels; j++)
1425 if (DiscardBlobBytes(image,layer_info[i].channel_info[j].size) ==
1428 layer_info=DestroyLayerInfo(layer_info,number_layers);
1429 ThrowBinaryException(CorruptImageError,
1430 "UnexpectedEndOfFile",image->filename);
1436 if (image->debug != MagickFalse)
1437 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1438 " reading data for layer %.20g",(double) i);
1440 status=ReadPSDLayer(image,psd_info,&layer_info[i],exception);
1441 if (status == MagickFalse)
1444 status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType)
1446 if (status == MagickFalse)
1450 if (status != MagickFalse)
1452 for (i=0; i < number_layers; i++)
1454 if (layer_info[i].image == (Image *) NULL)
1456 for (j=i; j < number_layers - 1; j++)
1457 layer_info[j] = layer_info[j+1];
1463 if (number_layers > 0)
1465 for (i=0; i < number_layers; i++)
1468 layer_info[i].image->previous=layer_info[i-1].image;
1469 if (i < (number_layers-1))
1470 layer_info[i].image->next=layer_info[i+1].image;
1471 layer_info[i].image->page=layer_info[i].page;
1473 image->next=layer_info[0].image;
1474 layer_info[0].image->previous=image;
1477 layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info);
1483 static MagickStatusType ReadPSDMergedImage(Image* image,PSDInfo* psd_info,
1484 ExceptionInfo *exception)
1498 compression=(PSDCompressionType) ReadBlobMSBShort(image);
1499 image->compression=ConvertPSDCompression(compression);
1501 if (compression != Raw && compression != RLE)
1503 (void) ThrowMagickException(exception,GetMagickModule(),
1504 TypeWarning,"CompressionNotSupported","'%.20g'",(double) compression);
1505 return(MagickFalse);
1508 offsets=(MagickOffsetType *) NULL;
1509 if (compression == RLE)
1511 offsets=ReadPSDRLEOffsets(image,psd_info,image->rows*psd_info->channels);
1512 if (offsets == (MagickOffsetType *) NULL)
1513 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1518 for (i=0; i < (ssize_t) psd_info->channels; i++)
1520 if (compression == RLE)
1521 status=ReadPSDChannelRLE(image,psd_info,i,offsets+(i*image->rows),
1524 status=ReadPSDChannelRaw(image,psd_info->channels,i,exception);
1526 if (status == MagickFalse)
1528 status=SetImageProgress(image,LoadImagesTag,i,psd_info->channels);
1529 if (status == MagickFalse)
1533 if (image->colorspace == CMYKColorspace)
1534 (void) NegateImage(image,MagickFalse,exception);
1536 if (offsets != (MagickOffsetType *) NULL)
1537 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1542 static Image *ReadPSDImage(const ImageInfo *image_info,
1543 ExceptionInfo *exception)
1573 assert(image_info != (const ImageInfo *) NULL);
1574 assert(image_info->signature == MagickSignature);
1575 if (image_info->debug != MagickFalse)
1576 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1577 image_info->filename);
1578 assert(exception != (ExceptionInfo *) NULL);
1579 assert(exception->signature == MagickSignature);
1581 image=AcquireImage(image_info,exception);
1582 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1583 if (status == MagickFalse)
1585 image=DestroyImageList(image);
1586 return((Image *) NULL);
1591 count=ReadBlob(image,4,(unsigned char *) psd_info.signature);
1592 psd_info.version=ReadBlobMSBShort(image);
1593 if ((count == 0) || (LocaleNCompare(psd_info.signature,"8BPS",4) != 0) ||
1594 ((psd_info.version != 1) && (psd_info.version != 2)))
1595 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1596 count=ReadBlob(image,6,psd_info.reserved);
1597 psd_info.channels=ReadBlobMSBShort(image);
1598 if (psd_info.channels > MaxPSDChannels)
1599 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
1600 psd_info.rows=ReadBlobMSBLong(image);
1601 psd_info.columns=ReadBlobMSBLong(image);
1602 if ((psd_info.version == 1) && ((psd_info.rows > 30000) ||
1603 (psd_info.columns > 30000)))
1604 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1605 psd_info.depth=ReadBlobMSBShort(image);
1606 if ((psd_info.depth != 1) && (psd_info.depth != 8) && (psd_info.depth != 16))
1607 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1608 psd_info.mode=ReadBlobMSBShort(image);
1609 if (image->debug != MagickFalse)
1610 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1611 " Image is %.20g x %.20g with channels=%.20g, depth=%.20g, mode=%s",
1612 (double) psd_info.columns,(double) psd_info.rows,(double)
1613 psd_info.channels,(double) psd_info.depth,ModeToString((PSDImageType)
1618 image->depth=psd_info.depth;
1619 image->columns=psd_info.columns;
1620 image->rows=psd_info.rows;
1621 if (SetImageBackgroundColor(image,exception) == MagickFalse)
1623 image=DestroyImageList(image);
1624 return((Image *) NULL);
1626 if (psd_info.mode == LabMode)
1627 SetImageColorspace(image,LabColorspace,exception);
1628 if (psd_info.mode == CMYKMode)
1629 SetImageColorspace(image,CMYKColorspace,exception);
1630 if ((psd_info.mode == BitmapMode) || (psd_info.mode == GrayscaleMode) ||
1631 (psd_info.mode == DuotoneMode))
1633 status=AcquireImageColormap(image,psd_info.depth != 16 ? 256 : 65536,
1635 if (status == MagickFalse)
1636 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1637 if (image->debug != MagickFalse)
1638 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1639 " Image colormap allocated");
1640 SetImageColorspace(image,GRAYColorspace,exception);
1642 image->alpha_trait=UndefinedPixelTrait;
1644 Read PSD raster colormap only present for indexed and duotone images.
1646 length=ReadBlobMSBLong(image);
1649 if (image->debug != MagickFalse)
1650 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1651 " reading colormap");
1652 if (psd_info.mode == DuotoneMode)
1655 Duotone image data; the format of this data is undocumented.
1657 data=(unsigned char *) AcquireQuantumMemory((size_t) length,
1659 if (data == (unsigned char *) NULL)
1660 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1661 count=ReadBlob(image,(size_t) length,data);
1662 data=(unsigned char *) RelinquishMagickMemory(data);
1667 Read PSD raster colormap.
1669 if (AcquireImageColormap(image,(size_t) (length/3),exception) == MagickFalse)
1670 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1671 for (i=0; i < (ssize_t) image->colors; i++)
1672 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
1673 ReadBlobByte(image));
1674 for (i=0; i < (ssize_t) image->colors; i++)
1675 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
1676 ReadBlobByte(image));
1677 for (i=0; i < (ssize_t) image->colors; i++)
1678 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
1679 ReadBlobByte(image));
1680 image->alpha_trait=UndefinedPixelTrait;
1683 length=ReadBlobMSBLong(image);
1690 Image resources block.
1692 if (image->debug != MagickFalse)
1693 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1694 " reading image resource blocks - %.20g bytes",(double)
1695 ((MagickOffsetType) length));
1696 blocks=(unsigned char *) AcquireQuantumMemory((size_t) length,
1698 if (blocks == (unsigned char *) NULL)
1699 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1700 count=ReadBlob(image,(size_t) length,blocks);
1701 if ((count != (ssize_t) length) ||
1702 (LocaleNCompare((char *) blocks,"8BIM",4) != 0))
1704 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
1705 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1707 (void) ParseImageResourceBlocks(image,blocks,(size_t) length,exception);
1708 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
1711 If we are only "pinging" the image, then we're done - so return.
1713 if (image_info->ping != MagickFalse)
1715 (void) CloseBlob(image);
1716 return(GetFirstImageInList(image));
1719 Layer and mask block.
1721 length=GetPSDSize(&psd_info,image);
1724 length=ReadBlobMSBLong(image);
1725 length=ReadBlobMSBLong(image);
1727 offset=TellBlob(image);
1728 skip_layers=MagickFalse;
1729 if ((image_info->number_scenes == 1) && (image_info->scene == 0))
1731 if (image->debug != MagickFalse)
1732 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1733 " read composite only");
1734 skip_layers=MagickTrue;
1738 if (image->debug != MagickFalse)
1739 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1740 " image has no layers");
1744 if (ReadPSDLayers(image,&psd_info,skip_layers,exception) != MagickTrue)
1746 (void) CloseBlob(image);
1747 return((Image *) NULL);
1751 Skip the rest of the layer and mask information.
1753 SeekBlob(image,offset+length,SEEK_SET);
1757 Read the precombined layer, present for PSD < 4 compatibility.
1759 if (image->debug != MagickFalse)
1760 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1761 " reading the precombined layer");
1762 (void) ReadPSDMergedImage(image,&psd_info,exception);
1763 (void) CloseBlob(image);
1764 return(GetFirstImageInList(image));
1768 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1772 % R e g i s t e r P S D I m a g e %
1776 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1778 % RegisterPSDImage() adds properties for the PSD image format to
1779 % the list of supported formats. The properties include the image format
1780 % tag, a method to read and/or write the format, whether the format
1781 % supports the saving of more than one frame to the same file or blob,
1782 % whether the format supports native in-memory I/O, and a brief
1783 % description of the format.
1785 % The format of the RegisterPSDImage method is:
1787 % size_t RegisterPSDImage(void)
1790 ModuleExport size_t RegisterPSDImage(void)
1795 entry=SetMagickInfo("PSB");
1796 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1797 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1798 entry->magick=(IsImageFormatHandler *) IsPSD;
1799 entry->seekable_stream=MagickTrue;
1800 entry->description=ConstantString("Adobe Large Document Format");
1801 entry->module=ConstantString("PSD");
1802 (void) RegisterMagickInfo(entry);
1803 entry=SetMagickInfo("PSD");
1804 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1805 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1806 entry->magick=(IsImageFormatHandler *) IsPSD;
1807 entry->seekable_stream=MagickTrue;
1808 entry->description=ConstantString("Adobe Photoshop bitmap");
1809 entry->module=ConstantString("PSD");
1810 (void) RegisterMagickInfo(entry);
1811 return(MagickImageCoderSignature);
1815 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1819 % U n r e g i s t e r P S D I m a g e %
1823 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1825 % UnregisterPSDImage() removes format registrations made by the
1826 % PSD module from the list of supported formats.
1828 % The format of the UnregisterPSDImage method is:
1830 % UnregisterPSDImage(void)
1833 ModuleExport void UnregisterPSDImage(void)
1835 (void) UnregisterMagickInfo("PSB");
1836 (void) UnregisterMagickInfo("PSD");
1840 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1844 % W r i t e P S D I m a g e %
1848 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1850 % WritePSDImage() writes an image in the Adobe Photoshop encoded image format.
1852 % The format of the WritePSDImage method is:
1854 % MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
1855 % ExceptionInfo *exception)
1857 % A description of each parameter follows.
1859 % o image_info: the image info.
1861 % o image: The image.
1863 % o exception: return any errors or warnings in this structure.
1867 static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image,
1868 const size_t offset)
1870 if (psd_info->version == 1)
1871 return(WriteBlobMSBShort(image,(unsigned short) offset));
1872 return(WriteBlobMSBLong(image,(unsigned short) offset));
1875 static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image,
1876 const MagickSizeType size)
1878 if (psd_info->version == 1)
1879 return(WriteBlobMSBLong(image,(unsigned int) size));
1880 return(WriteBlobMSBLongLong(image,size));
1883 static size_t PSDPackbitsEncodeImage(Image *image,const size_t length,
1884 const unsigned char *pixels,unsigned char *compact_pixels,
1885 ExceptionInfo *exception)
1894 register unsigned char
1901 Compress pixels with Packbits encoding.
1903 assert(image != (Image *) NULL);
1904 assert(image->signature == MagickSignature);
1905 if (image->debug != MagickFalse)
1906 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1907 assert(pixels != (unsigned char *) NULL);
1908 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1909 if (packbits == (unsigned char *) NULL)
1910 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1913 for (i=(ssize_t) length; i != 0; )
1920 *q++=(unsigned char) 0;
1927 *q++=(unsigned char) 1;
1935 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1937 *q++=(unsigned char) ((256-3)+1);
1941 *q++=(unsigned char) 2;
1949 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1955 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
1962 *q++=(unsigned char) ((256-count)+1);
1971 while ((*(pixels+count) != *(pixels+count+1)) ||
1972 (*(pixels+count+1) != *(pixels+count+2)))
1974 packbits[count+1]=pixels[count];
1976 if (((ssize_t) count >= (i-3)) || (count >= 127))
1980 *packbits=(unsigned char) (count-1);
1981 for (j=0; j <= (ssize_t) count; j++)
1988 *q++=(unsigned char) 128; /* EOD marker */
1989 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
1990 return((size_t) (q-compact_pixels));
1993 static void WritePackbitsLength(const PSDInfo *psd_info,
1994 const ImageInfo *image_info,Image *image,Image *next_image,
1995 unsigned char *compact_pixels,const QuantumType quantum_type,
1996 ExceptionInfo *exception)
2001 register const Quantum
2014 if (next_image->depth > 8)
2015 next_image->depth=16;
2016 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
2018 quantum_info=AcquireQuantumInfo(image_info,image);
2019 pixels=GetQuantumPixels(quantum_info);
2020 for (y=0; y < (ssize_t) next_image->rows; y++)
2022 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
2023 if (p == (const Quantum *) NULL)
2025 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
2026 quantum_type,pixels,exception);
2027 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
2029 (void) SetPSDOffset(psd_info,image,length);
2031 quantum_info=DestroyQuantumInfo(quantum_info);
2034 static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info,
2035 Image *image,Image *next_image,unsigned char *compact_pixels,
2036 const QuantumType quantum_type,const MagickBooleanType compression_flag,
2037 ExceptionInfo *exception)
2048 register const Quantum
2062 if ((compression_flag != MagickFalse) &&
2063 (next_image->compression != RLECompression))
2064 (void) WriteBlobMSBShort(image,0);
2065 if (next_image->depth > 8)
2066 next_image->depth=16;
2067 monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
2068 MagickTrue : MagickFalse;
2069 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
2071 quantum_info=AcquireQuantumInfo(image_info,image);
2072 pixels=GetQuantumPixels(quantum_info);
2073 for (y=0; y < (ssize_t) next_image->rows; y++)
2075 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
2076 if (p == (const Quantum *) NULL)
2078 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
2079 quantum_type,pixels,exception);
2080 if (monochrome != MagickFalse)
2081 for (i=0; i < (ssize_t) length; i++)
2082 pixels[i]=(~pixels[i]);
2083 if (next_image->compression != RLECompression)
2084 (void) WriteBlob(image,length,pixels);
2087 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
2089 (void) WriteBlob(image,length,compact_pixels);
2092 quantum_info=DestroyQuantumInfo(quantum_info);
2095 static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info,
2096 const ImageInfo *image_info,Image *image,Image *next_image,
2097 const MagickBooleanType separate,ExceptionInfo *exception)
2110 Write uncompressed pixels as separate planes.
2113 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
2114 compact_pixels=(unsigned char *) NULL;
2115 if (next_image->compression == RLECompression)
2117 compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels*
2118 next_image->columns,packet_size*sizeof(*compact_pixels));
2119 if (compact_pixels == (unsigned char *) NULL)
2120 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2123 if (IsImageGray(next_image,exception) != MagickFalse)
2125 if (next_image->compression == RLECompression)
2128 Packbits compression.
2130 (void) WriteBlobMSBShort(image,1);
2131 WritePackbitsLength(psd_info,image_info,image,next_image,
2132 compact_pixels,GrayQuantum,exception);
2133 if (next_image->alpha_trait == BlendPixelTrait)
2134 WritePackbitsLength(psd_info,image_info,image,next_image,
2135 compact_pixels,AlphaQuantum,exception);
2137 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2138 GrayQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
2139 MagickFalse,exception);
2140 if (next_image->alpha_trait == BlendPixelTrait)
2141 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2142 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
2143 MagickFalse,exception);
2144 (void) SetImageProgress(image,SaveImagesTag,0,1);
2147 if (next_image->storage_class == PseudoClass)
2149 if (next_image->compression == RLECompression)
2152 Packbits compression.
2154 (void) WriteBlobMSBShort(image,1);
2155 WritePackbitsLength(psd_info,image_info,image,next_image,
2156 compact_pixels,IndexQuantum,exception);
2157 if (next_image->alpha_trait == BlendPixelTrait)
2158 WritePackbitsLength(psd_info,image_info,image,next_image,
2159 compact_pixels,AlphaQuantum,exception);
2161 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2162 IndexQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
2163 MagickFalse,exception);
2164 if (next_image->alpha_trait == BlendPixelTrait)
2165 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2166 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
2167 MagickFalse,exception);
2168 (void) SetImageProgress(image,SaveImagesTag,0,1);
2172 if (next_image->colorspace == CMYKColorspace)
2173 (void) NegateImage(next_image,MagickFalse,exception);
2174 if (next_image->compression == RLECompression)
2177 Packbits compression.
2179 (void) WriteBlobMSBShort(image,1);
2180 WritePackbitsLength(psd_info,image_info,image,next_image,
2181 compact_pixels,RedQuantum,exception);
2182 WritePackbitsLength(psd_info,image_info,image,next_image,
2183 compact_pixels,GreenQuantum,exception);
2184 WritePackbitsLength(psd_info,image_info,image,next_image,
2185 compact_pixels,BlueQuantum,exception);
2186 if (next_image->colorspace == CMYKColorspace)
2187 WritePackbitsLength(psd_info,image_info,image,next_image,
2188 compact_pixels,BlackQuantum,exception);
2189 if (next_image->alpha_trait == BlendPixelTrait)
2190 WritePackbitsLength(psd_info,image_info,image,next_image,
2191 compact_pixels,AlphaQuantum,exception);
2193 (void) SetImageProgress(image,SaveImagesTag,0,6);
2194 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2195 RedQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
2196 MagickFalse,exception);
2197 (void) SetImageProgress(image,SaveImagesTag,1,6);
2198 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2199 GreenQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
2200 MagickFalse,exception);
2201 (void) SetImageProgress(image,SaveImagesTag,2,6);
2202 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2203 BlueQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
2204 MagickFalse,exception);
2205 (void) SetImageProgress(image,SaveImagesTag,3,6);
2206 if (next_image->colorspace == CMYKColorspace)
2207 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2208 BlackQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
2209 MagickFalse,exception);
2210 (void) SetImageProgress(image,SaveImagesTag,4,6);
2211 if (next_image->alpha_trait == BlendPixelTrait)
2212 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2213 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
2214 MagickFalse,exception);
2215 (void) SetImageProgress(image,SaveImagesTag,5,6);
2216 if (next_image->colorspace == CMYKColorspace)
2217 (void) NegateImage(next_image,MagickFalse,exception);
2219 if (next_image->compression == RLECompression)
2220 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
2224 static void WritePascalString(Image* inImage,const char *inString,int inPad)
2235 length=(strlen(inString) > 255UL ) ? 255UL : strlen(inString);
2237 (void) WriteBlobByte(inImage,0);
2240 (void) WriteBlobByte(inImage,(unsigned char) length);
2241 (void) WriteBlob(inImage, length, (const unsigned char *) inString);
2244 if ((length % inPad) == 0)
2246 for (i=0; i < (ssize_t) (inPad-(length % inPad)); i++)
2247 (void) WriteBlobByte(inImage,0);
2250 static void WriteResolutionResourceBlock(Image *image)
2259 x_resolution=65536.0*image->resolution.x+0.5;
2260 y_resolution=65536.0*image->resolution.y+0.5;
2262 if (image->units == PixelsPerCentimeterResolution)
2264 x_resolution=2.54*65536.0*image->resolution.x*0.5;
2265 y_resolution=2.54*65536.0*image->resolution.y+0.5;
2268 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2269 (void) WriteBlobMSBShort(image,0x03ED);
2270 (void) WriteBlobMSBShort(image,0);
2271 (void) WriteBlobMSBLong(image,16); /* resource size */
2272 (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5));
2273 (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */
2274 (void) WriteBlobMSBShort(image,units); /* width unit */
2275 (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5));
2276 (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */
2277 (void) WriteBlobMSBShort(image,units); /* height unit */
2280 static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile)
2282 register const unsigned char
2299 length=GetStringInfoLength(bim_profile);
2302 datum=GetStringInfoDatum(bim_profile);
2303 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2305 register unsigned char
2308 q=(unsigned char *) p;
2309 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2311 p=PushLongPixel(MSBEndian,p,&long_sans);
2312 p=PushShortPixel(MSBEndian,p,&id);
2313 p=PushShortPixel(MSBEndian,p,&short_sans);
2314 p=PushLongPixel(MSBEndian,p,&count);
2315 if (id == 0x0000040f)
2317 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2318 (PSDQuantum(count)+12)-(q-datum));
2319 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2323 if ((count & 0x01) != 0)
2328 static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile)
2330 register const unsigned char
2347 length=GetStringInfoLength(bim_profile);
2350 datum=GetStringInfoDatum(bim_profile);
2351 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2353 register unsigned char
2356 q=(unsigned char *) p;
2357 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2359 p=PushLongPixel(MSBEndian,p,&long_sans);
2360 p=PushShortPixel(MSBEndian,p,&id);
2361 p=PushShortPixel(MSBEndian,p,&short_sans);
2362 p=PushLongPixel(MSBEndian,p,&count);
2363 if ((id == 0x000003ed) && (PSDQuantum(count) < (ssize_t) (length-12)))
2365 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2366 (PSDQuantum(count)+12)-(q-datum));
2367 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2371 if ((count & 0x01) != 0)
2376 static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
2377 ExceptionInfo *exception)
2406 rounded_layer_info_size;
2414 assert(image_info != (const ImageInfo *) NULL);
2415 assert(image_info->signature == MagickSignature);
2416 assert(image != (Image *) NULL);
2417 assert(image->signature == MagickSignature);
2418 if (image->debug != MagickFalse)
2419 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2420 assert(exception != (ExceptionInfo *) NULL);
2421 assert(exception->signature == MagickSignature);
2422 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2423 if (status == MagickFalse)
2425 packet_size=(size_t) (image->depth > 8 ? 6 : 3);
2426 if (image->alpha_trait == BlendPixelTrait)
2427 packet_size+=image->depth > 8 ? 2 : 1;
2429 if ((LocaleCompare(image_info->magick,"PSB") == 0) ||
2430 (image->columns > 30000) || (image->rows > 30000))
2432 (void) WriteBlob(image,4,(const unsigned char *) "8BPS");
2433 (void) WriteBlobMSBShort(image,psd_info.version); /* version */
2434 for (i=1; i <= 6; i++)
2435 (void) WriteBlobByte(image, 0); /* 6 bytes of reserved */
2436 if (IsImageGray(image,exception) != MagickFalse)
2437 num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
2439 if (image->storage_class == PseudoClass)
2440 num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
2443 if (image->colorspace != CMYKColorspace)
2444 num_channels=(image->alpha_trait == BlendPixelTrait ? 4UL : 3UL);
2446 num_channels=(image->alpha_trait == BlendPixelTrait ? 5UL : 4UL);
2448 (void) WriteBlobMSBShort(image,(unsigned short) num_channels);
2449 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
2450 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
2451 if (IsImageGray(image,exception) != MagickFalse)
2459 monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
2460 MagickTrue : MagickFalse;
2461 (void) WriteBlobMSBShort(image,(unsigned short)
2462 (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8));
2463 (void) WriteBlobMSBShort(image,(unsigned short)
2464 (monochrome != MagickFalse ? BitmapMode : GrayscaleMode));
2468 (void) WriteBlobMSBShort(image,(unsigned short) (image->storage_class ==
2469 PseudoClass ? 8 : image->depth > 8 ? 16 : 8));
2470 if (((image_info->colorspace != UndefinedColorspace) ||
2471 (image->colorspace != CMYKColorspace)) &&
2472 (image_info->colorspace != CMYKColorspace))
2474 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2475 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2476 (void) WriteBlobMSBShort(image,(unsigned short)
2477 (image->storage_class == PseudoClass ? IndexedMode : RGBMode));
2481 if (image->colorspace != CMYKColorspace)
2482 (void) TransformImageColorspace(image,CMYKColorspace,exception);
2483 (void) WriteBlobMSBShort(image,CMYKMode);
2486 if ((IsImageGray(image,exception) != MagickFalse) ||
2487 (image->storage_class == DirectClass) || (image->colors > 256))
2488 (void) WriteBlobMSBLong(image,0);
2492 Write PSD raster colormap.
2494 (void) WriteBlobMSBLong(image,768);
2495 for (i=0; i < (ssize_t) image->colors; i++)
2496 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red));
2497 for ( ; i < 256; i++)
2498 (void) WriteBlobByte(image,0);
2499 for (i=0; i < (ssize_t) image->colors; i++)
2500 (void) WriteBlobByte(image,ScaleQuantumToChar(
2501 image->colormap[i].green));
2502 for ( ; i < 256; i++)
2503 (void) WriteBlobByte(image,0);
2504 for (i=0; i < (ssize_t) image->colors; i++)
2505 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue));
2506 for ( ; i < 256; i++)
2507 (void) WriteBlobByte(image,0);
2510 Image resource block.
2512 length=28; /* 0x03EB */
2513 bim_profile=(StringInfo *) GetImageProfile(image,"8bim");
2514 icc_profile=GetImageProfile(image,"icc");
2515 if (bim_profile != (StringInfo *) NULL)
2517 bim_profile=CloneStringInfo(bim_profile);
2518 if (icc_profile != (StringInfo *) NULL)
2519 RemoveICCProfileFromResourceBlock(bim_profile);
2520 RemoveResolutionFromResourceBlock(bim_profile);
2521 length+=PSDQuantum(GetStringInfoLength(bim_profile));
2523 if (icc_profile != (const StringInfo *) NULL)
2524 length+=PSDQuantum(GetStringInfoLength(icc_profile))+12;
2525 (void) WriteBlobMSBLong(image,(unsigned int) length);
2526 WriteResolutionResourceBlock(image);
2527 if (bim_profile != (StringInfo *) NULL)
2529 (void) WriteBlob(image,GetStringInfoLength(bim_profile),
2530 GetStringInfoDatum(bim_profile));
2531 bim_profile=DestroyStringInfo(bim_profile);
2533 if (icc_profile != (StringInfo *) NULL)
2535 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2536 (void) WriteBlobMSBShort(image,0x0000040F);
2537 (void) WriteBlobMSBShort(image,0);
2538 (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength(
2540 (void) WriteBlob(image,GetStringInfoLength(icc_profile),
2541 GetStringInfoDatum(icc_profile));
2542 if ((MagickOffsetType) GetStringInfoLength(icc_profile) !=
2543 PSDQuantum(GetStringInfoLength(icc_profile)))
2544 (void) WriteBlobByte(image,0);
2548 base_image=GetNextImageInList(image);
2549 if ((image->alpha_trait == BlendPixelTrait) && (base_image == (Image *) NULL))
2551 next_image=base_image;
2552 while ( next_image != NULL )
2554 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2555 if (IsImageGray(next_image,exception) != MagickFalse)
2556 num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
2558 if (next_image->storage_class == PseudoClass)
2559 num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
2561 if (next_image->colorspace != CMYKColorspace)
2562 num_channels=next_image->alpha_trait == BlendPixelTrait ? 4UL : 3UL;
2564 num_channels=next_image->alpha_trait == BlendPixelTrait ? 5UL : 4UL;
2565 channelLength=(size_t) (next_image->columns*next_image->rows*packet_size+2);
2566 layer_info_size+=(size_t) (4*4+2+num_channels*6+(psd_info.version == 1 ? 8 :
2567 16)+4*1+4+num_channels*channelLength);
2568 property=(const char *) GetImageProperty(next_image,"label",exception);
2569 if (property == (const char *) NULL)
2570 layer_info_size+=16;
2576 length=strlen(property);
2577 layer_info_size+=8+length+(4-(length % 4));
2580 next_image=GetNextImageInList(next_image);
2582 if (layer_count == 0)
2583 (void) SetPSDSize(&psd_info,image,0);
2589 (void) SetPSDSize(&psd_info,image,layer_info_size+
2590 (psd_info.version == 1 ? 8 : 16));
2591 if ((layer_info_size/2) != ((layer_info_size+1)/2))
2592 rounded_layer_info_size=layer_info_size+1;
2594 rounded_layer_info_size=layer_info_size;
2595 (void) SetPSDSize(&psd_info,image,rounded_layer_info_size);
2596 if (next_image->alpha_trait == BlendPixelTrait)
2597 (void) WriteBlobMSBShort(image,-(unsigned short) layer_count);
2599 (void) WriteBlobMSBShort(image,(unsigned short) layer_count);
2601 compression=base_image->compression;
2602 next_image=base_image;
2603 while (next_image != NULL)
2605 next_image->compression=NoCompression;
2606 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y);
2607 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x);
2608 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y+
2610 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x+
2611 next_image->columns);
2612 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2613 channel_size=(unsigned int) ((packet_size*next_image->rows*
2614 next_image->columns)+2);
2615 if ((IsImageGray(next_image,exception) != MagickFalse) ||
2616 (next_image->storage_class == PseudoClass))
2618 (void) WriteBlobMSBShort(image,(unsigned short)
2619 (next_image->alpha_trait == BlendPixelTrait ? 2 : 1));
2620 (void) WriteBlobMSBShort(image,0);
2621 (void) SetPSDSize(&psd_info,image,channel_size);
2622 if (next_image->alpha_trait == BlendPixelTrait)
2624 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2625 (void) SetPSDSize(&psd_info,image,channel_size);
2629 if (next_image->colorspace != CMYKColorspace)
2631 (void) WriteBlobMSBShort(image,(unsigned short)
2632 (next_image->alpha_trait == BlendPixelTrait ? 4 : 3));
2633 (void) WriteBlobMSBShort(image,0);
2634 (void) SetPSDSize(&psd_info,image,channel_size);
2635 (void) WriteBlobMSBShort(image,1);
2636 (void) SetPSDSize(&psd_info,image,channel_size);
2637 (void) WriteBlobMSBShort(image,2);
2638 (void) SetPSDSize(&psd_info,image,channel_size);
2639 if (next_image->alpha_trait == BlendPixelTrait)
2641 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2642 (void) SetPSDSize(&psd_info,image,channel_size);
2647 (void) WriteBlobMSBShort(image,(unsigned short)
2648 (next_image->alpha_trait ? 5 : 4));
2649 (void) WriteBlobMSBShort(image,0);
2650 (void) SetPSDSize(&psd_info,image,channel_size);
2651 (void) WriteBlobMSBShort(image,1);
2652 (void) SetPSDSize(&psd_info,image,channel_size);
2653 (void) WriteBlobMSBShort(image,2);
2654 (void) SetPSDSize(&psd_info,image,channel_size);
2655 (void) WriteBlobMSBShort(image,3);
2656 (void) SetPSDSize(&psd_info,image,channel_size);
2657 if (next_image->alpha_trait)
2659 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2660 (void) SetPSDSize(&psd_info,image,channel_size);
2663 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2664 (void) WriteBlob(image,4,(const unsigned char *)
2665 CompositeOperatorToPSDBlendMode(next_image->compose));
2666 (void) WriteBlobByte(image,255); /* layer opacity */
2667 (void) WriteBlobByte(image,0);
2668 (void) WriteBlobByte(image,1); /* layer propertys - visible, etc. */
2669 (void) WriteBlobByte(image,0);
2670 property=(const char *) GetImageProperty(next_image,"label",exception);
2671 if (property == (const char *) NULL)
2674 layer_name[MaxTextExtent];
2676 (void) WriteBlobMSBLong(image,16);
2677 (void) WriteBlobMSBLong(image,0);
2678 (void) WriteBlobMSBLong(image,0);
2679 (void) FormatLocaleString(layer_name,MaxTextExtent,"L%06ld",(long)
2681 WritePascalString(image,layer_name,4);
2688 length=strlen(property);
2689 (void) WriteBlobMSBLong(image,(unsigned int) (length+(4-
2691 (void) WriteBlobMSBLong(image,0);
2692 (void) WriteBlobMSBLong(image,0);
2693 WritePascalString(image,property,4);
2695 next_image=GetNextImageInList(next_image);
2700 next_image=base_image;
2701 while (next_image != NULL)
2703 status=WriteImageChannels(&psd_info,image_info,image,next_image,
2704 MagickTrue,exception);
2705 next_image=GetNextImageInList(next_image);
2707 (void) WriteBlobMSBLong(image,0); /* user mask data */
2708 base_image->compression=compression;
2711 Write composite image.
2713 status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse,
2715 (void) CloseBlob(image);