2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Adobe Photoshop Image Format %
21 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
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. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/attribute.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/enhance.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/image.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/list.h"
59 #include "MagickCore/log.h"
60 #include "MagickCore/magick.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/module.h"
63 #include "MagickCore/monitor-private.h"
64 #include "MagickCore/pixel.h"
65 #include "MagickCore/pixel-accessor.h"
66 #include "MagickCore/profile.h"
67 #include "MagickCore/property.h"
68 #include "MagickCore/quantum-private.h"
69 #include "MagickCore/static.h"
70 #include "MagickCore/string_.h"
75 #define MaxPSDChannels 56
76 #define PSDQuantum(x) (((ssize_t) (x)+1) & -2)
79 Enumerated declaractions.
94 Typedef declaractions.
96 typedef struct _ChannelInfo
105 typedef struct _LayerInfo
115 channel_info[MaxPSDChannels];
139 typedef struct _PSDInfo
162 Forward declarations.
164 static MagickBooleanType
165 WritePSDImage(const ImageInfo *,Image *,ExceptionInfo *);
168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 % IsPSD()() returns MagickTrue if the image format type, identified by the
179 % magick string, is PSD.
181 % The format of the IsPSD method is:
183 % MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
185 % A description of each parameter follows:
187 % o magick: compare image format pattern against these bytes.
189 % o length: Specifies the length of the magick string.
192 static MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
196 if (LocaleNCompare((const char *) magick,"8BPS",4) == 0)
202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206 % R e a d P S D I m a g e %
210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 % ReadPSDImage() reads an Adobe Photoshop image file and returns it. It
213 % allocates the memory necessary for the new Image structure and returns a
214 % pointer to the new image.
216 % The format of the ReadPSDImage method is:
218 % Image *ReadPSDImage(image_info)
220 % A description of each parameter follows:
222 % o image_info: the image info.
224 % o exception: return any errors or warnings in this structure.
228 static const char *CompositeOperatorToPSDBlendMode(CompositeOperator op)
235 case OverCompositeOp: blend_mode = "norm"; break;
236 case MultiplyCompositeOp: blend_mode = "mul "; break;
237 case DissolveCompositeOp: blend_mode = "diss"; break;
238 case DifferenceCompositeOp: blend_mode = "diff"; break;
239 case DarkenCompositeOp: blend_mode = "dark"; break;
240 case LightenCompositeOp: blend_mode = "lite"; break;
241 case HueCompositeOp: blend_mode = "hue "; break;
242 case SaturateCompositeOp: blend_mode = "sat "; break;
243 case ColorizeCompositeOp: blend_mode = "colr"; break;
244 case LuminizeCompositeOp: blend_mode = "lum "; break;
245 case ScreenCompositeOp: blend_mode = "scrn"; break;
246 case OverlayCompositeOp: blend_mode = "over"; break;
253 static ssize_t DecodePSDPixels(const size_t number_compact_pixels,
254 const unsigned char *compact_pixels,const ssize_t depth,
255 const size_t number_pixels,unsigned char *pixels)
270 packets=(ssize_t) number_compact_pixels;
271 for (i=0; (packets > 1) && (i < (ssize_t) number_pixels); )
273 length=(*compact_pixels++);
280 pixel=(*compact_pixels++);
282 for (j=0; j < (ssize_t) length; j++)
288 *pixels++=(pixel >> 7) & 0x01 ? 0U : 255U;
289 *pixels++=(pixel >> 6) & 0x01 ? 0U : 255U;
290 *pixels++=(pixel >> 5) & 0x01 ? 0U : 255U;
291 *pixels++=(pixel >> 4) & 0x01 ? 0U : 255U;
292 *pixels++=(pixel >> 3) & 0x01 ? 0U : 255U;
293 *pixels++=(pixel >> 2) & 0x01 ? 0U : 255U;
294 *pixels++=(pixel >> 1) & 0x01 ? 0U : 255U;
295 *pixels++=(pixel >> 0) & 0x01 ? 0U : 255U;
301 *pixels++=(unsigned char) ((pixel >> 4) & 0xff);
302 *pixels++=(unsigned char) ((pixel & 0x0f) & 0xff);
308 *pixels++=(unsigned char) ((pixel >> 6) & 0x03);
309 *pixels++=(unsigned char) ((pixel >> 4) & 0x03);
310 *pixels++=(unsigned char) ((pixel >> 2) & 0x03);
311 *pixels++=(unsigned char) ((pixel & 0x03) & 0x03);
317 *pixels++=(unsigned char) pixel;
326 for (j=0; j < (ssize_t) length; j++)
332 *pixels++=(*compact_pixels >> 7) & 0x01 ? 0U : 255U;
333 *pixels++=(*compact_pixels >> 6) & 0x01 ? 0U : 255U;
334 *pixels++=(*compact_pixels >> 5) & 0x01 ? 0U : 255U;
335 *pixels++=(*compact_pixels >> 4) & 0x01 ? 0U : 255U;
336 *pixels++=(*compact_pixels >> 3) & 0x01 ? 0U : 255U;
337 *pixels++=(*compact_pixels >> 2) & 0x01 ? 0U : 255U;
338 *pixels++=(*compact_pixels >> 1) & 0x01 ? 0U : 255U;
339 *pixels++=(*compact_pixels >> 0) & 0x01 ? 0U : 255U;
345 *pixels++=(*compact_pixels >> 4) & 0xff;
346 *pixels++=(*compact_pixels & 0x0f) & 0xff;
352 *pixels++=(*compact_pixels >> 6) & 0x03;
353 *pixels++=(*compact_pixels >> 4) & 0x03;
354 *pixels++=(*compact_pixels >> 2) & 0x03;
355 *pixels++=(*compact_pixels & 0x03) & 0x03;
361 *pixels++=(*compact_pixels);
372 static inline MagickOffsetType GetPSDOffset(PSDInfo *psd_info,Image *image)
374 if (psd_info->version == 1)
375 return((MagickOffsetType) ReadBlobMSBShort(image));
376 return((MagickOffsetType) ReadBlobMSBLong(image));
379 static inline MagickSizeType GetPSDSize(PSDInfo *psd_info,Image *image)
381 if (psd_info->version == 1)
382 return((MagickSizeType) ReadBlobMSBLong(image));
383 return((MagickSizeType) ReadBlobMSBLongLong(image));
386 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
393 static const char *ModeToString(PSDImageType type)
397 case BitmapMode: return "Bitmap";
398 case GrayscaleMode: return "Grayscale";
399 case IndexedMode: return "Indexed";
400 case RGBMode: return "RGB";
401 case CMYKMode: return "CMYK";
402 case MultichannelMode: return "Multichannel";
403 case DuotoneMode: return "Duotone";
404 case LabMode: return "L*A*B";
405 default: return "unknown";
409 static MagickBooleanType ParseImageResourceBlocks(Image *image,
410 const unsigned char *blocks,size_t length,ExceptionInfo *exception)
428 profile=BlobToStringInfo((const void *) NULL,length);
429 SetStringInfoDatum(profile,blocks);
430 (void) SetImageProfile(image,"8bim",profile,exception);
431 profile=DestroyStringInfo(profile);
432 for (p=blocks; (p >= blocks) && (p < (blocks+length-16)); )
434 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
436 p=PushLongPixel(MSBEndian,p,&long_sans);
437 p=PushShortPixel(MSBEndian,p,&id);
438 p=PushShortPixel(MSBEndian,p,&short_sans);
439 p=PushLongPixel(MSBEndian,p,&count);
445 value[MaxTextExtent];
453 p=PushShortPixel(MSBEndian,p,&resolution);
454 image->resolution.x=(double) resolution;
455 (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.x);
456 (void) SetImageProperty(image,"tiff:XResolution",value,exception);
457 p=PushShortPixel(MSBEndian,p,&short_sans);
458 p=PushShortPixel(MSBEndian,p,&short_sans);
459 p=PushShortPixel(MSBEndian,p,&short_sans);
460 p=PushShortPixel(MSBEndian,p,&resolution);
461 image->resolution.y=(double) resolution;
462 (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.y);
463 (void) SetImageProperty(image,"tiff:YResolution",value,exception);
464 p=PushShortPixel(MSBEndian,p,&short_sans);
465 p=PushShortPixel(MSBEndian,p,&short_sans);
466 p=PushShortPixel(MSBEndian,p,&short_sans);
467 image->units=PixelsPerInchResolution;
476 if ((count & 0x01) != 0)
482 static CompositeOperator PSDBlendModeToCompositeOperator(const char *mode)
484 if (mode == (const char *) NULL)
485 return(OverCompositeOp);
486 if (LocaleNCompare(mode,"norm",4) == 0)
487 return(OverCompositeOp);
488 if (LocaleNCompare(mode,"mul ",4) == 0)
489 return(MultiplyCompositeOp);
490 if (LocaleNCompare(mode,"diss",4) == 0)
491 return(DissolveCompositeOp);
492 if (LocaleNCompare(mode,"diff",4) == 0)
493 return(DifferenceCompositeOp);
494 if (LocaleNCompare(mode,"dark",4) == 0)
495 return(DarkenCompositeOp);
496 if (LocaleNCompare(mode,"lite",4) == 0)
497 return(LightenCompositeOp);
498 if (LocaleNCompare(mode,"hue ",4) == 0)
499 return(HueCompositeOp);
500 if (LocaleNCompare(mode,"sat ",4) == 0)
501 return(SaturateCompositeOp);
502 if (LocaleNCompare(mode,"colr",4) == 0)
503 return(ColorizeCompositeOp);
504 if (LocaleNCompare(mode,"lum ",4) == 0)
505 return(LuminizeCompositeOp);
506 if (LocaleNCompare(mode,"scrn",4) == 0)
507 return(ScreenCompositeOp);
508 if (LocaleNCompare(mode,"over",4) == 0)
509 return(OverlayCompositeOp);
510 if (LocaleNCompare(mode,"hLit",4) == 0)
511 return(OverCompositeOp);
512 if (LocaleNCompare(mode,"sLit",4) == 0)
513 return(OverCompositeOp);
514 if (LocaleNCompare(mode,"smud",4) == 0)
515 return(OverCompositeOp);
516 if (LocaleNCompare(mode,"div ",4) == 0)
517 return(OverCompositeOp);
518 if (LocaleNCompare(mode,"idiv",4) == 0)
519 return(OverCompositeOp);
520 return(OverCompositeOp);
523 static MagickBooleanType ReadPSDLayer(Image *image,const size_t channels,
524 const ssize_t type,const MagickOffsetType *offsets,ExceptionInfo *exception)
532 register const unsigned char
556 if (image->storage_class == PseudoClass)
558 if (image->colors > 256)
561 if (image->depth > 8)
565 if (image->depth > 8)
567 pixels=(unsigned char *) AcquireQuantumMemory(image->columns+256,packet_size*
569 if (pixels == (unsigned char *) NULL)
570 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
572 (void) ResetMagickMemory(pixels,0,image->columns*packet_size*sizeof(*pixels));
573 compact_pixels=(unsigned char *) NULL;
574 if (image->compression == RLECompression)
580 for (y=0; y < (ssize_t) image->rows; y++)
581 if ((MagickOffsetType) length < offsets[y])
582 length=(size_t) offsets[y];
583 compact_pixels=(unsigned char *) AcquireQuantumMemory(length+256,
585 if (compact_pixels == (unsigned char *) NULL)
586 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
588 (void) ResetMagickMemory(compact_pixels,0,length*sizeof(*compact_pixels));
590 colorspace=image->colorspace;
591 for (y=0; y < (ssize_t) image->rows; y++)
593 if (image->depth == 1)
595 if (image->compression != RLECompression)
596 count=ReadBlob(image,(image->columns+7)/8,pixels);
599 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
600 if (count != (ssize_t) offsets[y])
602 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
603 (ssize_t) 123456,(size_t) ((image->columns+7)/8),pixels);
605 if (count < (ssize_t) ((image->columns+7)/8))
610 if (image->compression != RLECompression)
611 count=ReadBlob(image,packet_size*image->columns,pixels);
614 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
615 if (count != (ssize_t) offsets[y])
617 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
618 (ssize_t) image->depth,packet_size*image->columns,pixels);
620 if (count < (ssize_t) (packet_size*image->columns))
623 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
624 if (q == (Quantum *) NULL)
627 for (x=0; x < (ssize_t) image->columns; x++)
629 if (packet_size == 1)
630 pixel=ScaleCharToQuantum(*p++);
633 p=PushShortPixel(MSBEndian,p,&nibble);
634 pixel=ScaleShortToQuantum(nibble);
640 SetPixelAlpha(image,pixel,q);
645 SetPixelRed(image,pixel,q);
647 SetPixelGray(image,pixel,q);
649 SetPixelRed(image,pixel,q);
650 if (image->storage_class == PseudoClass)
652 if (packet_size == 1)
653 SetPixelIndex(image,ScaleQuantumToChar(pixel),q);
655 SetPixelIndex(image,ScaleQuantumToShort(pixel),q);
656 SetPixelInfoPixel(image,image->colormap+(ssize_t)
657 GetPixelIndex(image,q),q);
658 if (image->depth == 1)
664 number_bits=image->columns-x;
667 for (bit=0; bit < number_bits; bit++)
669 SetPixelIndex(image,(((unsigned char) pixel) &
670 (0x01 << (7-bit))) != 0 ? 0 : 255,q);
671 SetPixelInfoPixel(image,image->colormap+(ssize_t)
672 GetPixelIndex(image,q),q);
673 q+=GetPixelChannels(image);
682 if (image->storage_class == PseudoClass)
683 SetPixelAlpha(image,pixel,q);
685 SetPixelGreen(image,pixel,q);
690 if (image->storage_class == PseudoClass)
691 SetPixelAlpha(image,pixel,q);
693 SetPixelBlue(image,pixel,q);
698 if (image->colorspace == CMYKColorspace)
699 SetPixelBlack(image,pixel,q);
701 if (image->alpha_trait == BlendPixelTrait)
702 SetPixelAlpha(image,pixel,q);
707 if ((IssRGBCompatibleColorspace(image->colorspace) != MagickFalse) &&
710 if (image->alpha_trait == BlendPixelTrait)
711 SetPixelAlpha(image,pixel,q);
717 q+=GetPixelChannels(image);
719 if (SyncAuthenticPixels(image,exception) == MagickFalse)
722 image->colorspace=colorspace;
723 if (image->compression == RLECompression)
724 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
725 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
729 static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception)
732 message[MaxTextExtent],
766 skip_first_alpha = 0;
783 assert(image_info != (const ImageInfo *) NULL);
784 assert(image_info->signature == MagickSignature);
785 if (image_info->debug != MagickFalse)
786 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
787 image_info->filename);
788 assert(exception != (ExceptionInfo *) NULL);
789 assert(exception->signature == MagickSignature);
790 image=AcquireImage(image_info,exception);
791 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
792 if (status == MagickFalse)
794 image=DestroyImageList(image);
795 return((Image *) NULL);
800 count=ReadBlob(image,4,(unsigned char *) psd_info.signature);
801 psd_info.version=ReadBlobMSBShort(image);
802 if ((count == 0) || (LocaleNCompare(psd_info.signature,"8BPS",4) != 0) ||
803 ((psd_info.version != 1) && (psd_info.version != 2)))
804 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
805 count=ReadBlob(image,6,psd_info.reserved);
806 psd_info.channels=ReadBlobMSBShort(image);
807 psd_info.color_channels=psd_info.channels;
808 if (psd_info.channels > MaxPSDChannels)
809 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
810 psd_info.rows=ReadBlobMSBLong(image);
811 psd_info.columns=ReadBlobMSBLong(image);
812 if ((psd_info.version == 1) && ((psd_info.rows > 30000) ||
813 (psd_info.columns > 30000)))
814 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
815 psd_info.depth=ReadBlobMSBShort(image);
816 if ((psd_info.depth != 1) && (psd_info.depth != 8) && (psd_info.depth != 16))
817 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
818 psd_info.mode=ReadBlobMSBShort(image);
819 if (image->debug != MagickFalse)
820 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
821 " Image is %.20g x %.20g with channels=%.20g, depth=%.20g, mode=%s",
822 (double) psd_info.columns,(double) psd_info.rows,(double)
823 psd_info.channels,(double) psd_info.depth,ModeToString((PSDImageType)
828 image->depth=psd_info.depth;
829 image->columns=psd_info.columns;
830 image->rows=psd_info.rows;
831 if (SetImageBackgroundColor(image,exception) == MagickFalse)
833 image=DestroyImageList(image);
834 return((Image *) NULL);
836 image->alpha_trait=psd_info.channels >= 4 ? BlendPixelTrait :
838 if (psd_info.mode == LabMode)
839 SetImageColorspace(image,LabColorspace,exception);
840 psd_info.color_channels=3;
841 if (psd_info.mode == CMYKMode)
843 psd_info.color_channels=4;
844 SetImageColorspace(image,CMYKColorspace,exception);
845 image->alpha_trait=psd_info.channels >= 5 ? BlendPixelTrait :
848 if ((psd_info.mode == BitmapMode) || (psd_info.mode == GrayscaleMode) ||
849 (psd_info.mode == DuotoneMode))
851 psd_info.color_channels=1;
852 if (AcquireImageColormap(image,256,exception) == MagickFalse)
853 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
854 image->alpha_trait=psd_info.channels >= 2 ? BlendPixelTrait :
856 if (image->debug != MagickFalse)
857 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
858 " Image colormap allocated");
859 SetImageColorspace(image,GRAYColorspace,exception);
861 image->alpha_trait=UndefinedPixelTrait;
863 Read PSD raster colormap only present for indexed and duotone images.
865 length=ReadBlobMSBLong(image);
868 if (image->debug != MagickFalse)
869 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
870 " reading colormap");
871 if (psd_info.mode == DuotoneMode)
874 Duotone image data; the format of this data is undocumented.
876 data=(unsigned char *) AcquireQuantumMemory((size_t) length,
878 if (data == (unsigned char *) NULL)
879 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
880 count=ReadBlob(image,(size_t) length,data);
881 data=(unsigned char *) RelinquishMagickMemory(data);
886 Read PSD raster colormap.
888 if (AcquireImageColormap(image,(size_t) (length/3),exception) == MagickFalse)
889 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
890 for (i=0; i < (ssize_t) image->colors; i++)
891 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
892 ReadBlobByte(image));
893 for (i=0; i < (ssize_t) image->colors; i++)
894 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
895 ReadBlobByte(image));
896 for (i=0; i < (ssize_t) image->colors; i++)
897 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
898 ReadBlobByte(image));
899 image->alpha_trait=UndefinedPixelTrait;
902 length=ReadBlobMSBLong(image);
909 Image resources block.
911 if (image->debug != MagickFalse)
912 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
913 " reading image resource blocks - %.20g bytes",(double)
914 ((MagickOffsetType) length));
915 blocks=(unsigned char *) AcquireQuantumMemory((size_t) length,
917 if (blocks == (unsigned char *) NULL)
918 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
919 count=ReadBlob(image,(size_t) length,blocks);
920 if ((count != (ssize_t) length) ||
921 (LocaleNCompare((char *) blocks,"8BIM",4) != 0))
923 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
924 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
926 (void) ParseImageResourceBlocks(image,blocks,(size_t) length,
928 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
931 Layer and mask block.
933 layer_info=(LayerInfo *) NULL;
935 length=GetPSDSize(&psd_info,image);
938 length=ReadBlobMSBLong(image);
939 length=ReadBlobMSBLong(image);
941 check_background=MagickFalse;
942 if ((image_info->number_scenes == 1) && (image_info->scene == 0))
944 if (image->debug != MagickFalse)
945 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
946 " read composite only");
947 check_background=MagickTrue;
951 if (image->debug != MagickFalse)
952 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
953 " image has no layers");
957 offset=TellBlob(image);
958 size=GetPSDSize(&psd_info,image);
970 quantum=psd_info.version == 1 ? 4UL : 8UL;
971 tag=ReadBlobMSBLong(image);
973 count=ReadBlob(image,4,(unsigned char *) type);
974 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
976 if (DiscardBlobBytes(image,length-quantum-8) == MagickFalse)
977 ThrowFileException(exception,CorruptImageError,
978 "UnexpectedEndOfFile",image->filename);
982 count=ReadBlob(image,4,(unsigned char *) type);
983 if ((count != 0) && (LocaleNCompare(type,"Lr16",4) == 0))
984 size=GetPSDSize(&psd_info,image);
986 if (DiscardBlobBytes(image,length-quantum-12) == MagickFalse)
987 ThrowFileException(exception,CorruptImageError,
988 "UnexpectedEndOfFile",image->filename);
996 image->alpha_trait=psd_info.channels > psd_info.color_channels ?
997 BlendPixelTrait : UndefinedPixelTrait;
999 if (image->debug != MagickFalse)
1000 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1001 image->alpha_trait ? " image has matte" : " image has no matte");
1003 layer_offset=offset+length;
1004 number_layers=(short) ReadBlobMSBShort(image);
1005 if (image->debug != MagickFalse)
1006 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1007 " image contains %.20g layers",(double) number_layers);
1008 if (number_layers < 0)
1011 Weird hack in PSD format to ignore first alpha channel.
1014 (void) skip_first_alpha;
1015 number_layers=MagickAbsoluteValue(number_layers);
1016 if (image->debug != MagickFalse)
1017 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1018 " negative layer count corrected for");
1020 layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers,
1021 sizeof(*layer_info));
1022 if (layer_info == (LayerInfo *) NULL)
1024 if (image->debug != MagickFalse)
1025 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1026 " allocation of LayerInfo failed");
1027 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1029 (void) ResetMagickMemory(layer_info,0,(size_t) number_layers*
1030 sizeof(*layer_info));
1031 for (i=0; i < number_layers; i++)
1037 if (image->debug != MagickFalse)
1038 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1039 " reading layer #%.20g",(double) i+1);
1040 layer_info[i].page.y=(int) ReadBlobMSBLong(image);
1041 layer_info[i].page.x=(int) ReadBlobMSBLong(image);
1042 y=(int) ReadBlobMSBLong(image);
1043 x=(int) ReadBlobMSBLong(image);
1044 layer_info[i].page.width=(ssize_t) (x-layer_info[i].page.x);
1045 layer_info[i].page.height=(ssize_t) (y-layer_info[i].page.y);
1046 layer_info[i].channels=ReadBlobMSBShort(image);
1047 if (check_background == MagickTrue)
1052 if (layer_info[i].channels == psd_info.color_channels)
1053 image->alpha_trait=UndefinedPixelTrait;
1054 quantum=psd_info.version == 1 ? 4UL : 8UL;
1055 if (DiscardBlobBytes(image,length-20-quantum) == MagickFalse)
1056 ThrowFileException(exception,CorruptImageError,
1057 "UnexpectedEndOfFile",image->filename);
1060 if (layer_info[i].channels > MaxPSDChannels)
1061 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
1062 if (image->debug != MagickFalse)
1063 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1064 " offset(%.20g,%.20g), size(%.20g,%.20g), channels=%.20g",
1065 (double) layer_info[i].page.x,(double) layer_info[i].page.y,
1066 (double) layer_info[i].page.height,(double)
1067 layer_info[i].page.width,(double) layer_info[i].channels);
1068 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1070 layer_info[i].channel_info[j].type=(short)
1071 ReadBlobMSBShort(image);
1072 layer_info[i].channel_info[j].size=(size_t)
1073 GetPSDSize(&psd_info,image);
1074 if (image->debug != MagickFalse)
1075 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1076 " channel[%.20g]: type=%.20g, size=%.20g",(double) j,
1077 (double) layer_info[i].channel_info[j].type,
1078 (double) layer_info[i].channel_info[j].size);
1080 count=ReadBlob(image,4,(unsigned char *) type);
1081 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1083 if (image->debug != MagickFalse)
1084 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1085 " layer type was %.4s instead of 8BIM", type);
1086 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1088 count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey);
1089 layer_info[i].opacity=(Quantum) ScaleCharToQuantum((unsigned char)
1090 ReadBlobByte(image));
1091 layer_info[i].clipping=(unsigned char) ReadBlobByte(image);
1092 layer_info[i].flags=(unsigned char) ReadBlobByte(image);
1093 layer_info[i].visible=!(layer_info[i].flags & 0x02);
1094 if (image->debug != MagickFalse)
1095 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1096 " blend=%.4s, opacity=%.20g, clipping=%s, flags=%d, visible=%s",
1097 layer_info[i].blendkey,(double) layer_info[i].opacity,
1098 layer_info[i].clipping ? "true" : "false",layer_info[i].flags,
1099 layer_info[i].visible ? "true" : "false");
1100 (void) ReadBlobByte(image); /* filler */
1102 size=ReadBlobMSBLong(image);
1105 if (image->debug != MagickFalse)
1106 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1107 " layer contains additional info");
1108 length=ReadBlobMSBLong(image);
1114 layer_info[i].mask.y=(int) ReadBlobMSBLong(image);
1115 layer_info[i].mask.x=(int) ReadBlobMSBLong(image);
1116 layer_info[i].mask.height=(size_t)
1117 (ReadBlobMSBLong(image)-layer_info[i].mask.y);
1118 layer_info[i].mask.width=(size_t)
1119 (ReadBlobMSBLong(image)-layer_info[i].mask.x);
1120 if (image->debug != MagickFalse)
1121 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1122 " layer mask: offset(%.20g,%.20g), size(%.20g,%.20g), length=%.20g",
1123 (double) layer_info[i].mask.x,(double) layer_info[i].mask.y,
1124 (double) layer_info[i].mask.width,(double)
1125 layer_info[i].mask.height,(double)
1126 ((MagickOffsetType) length-16));
1128 Skip over the rest of the layer mask information.
1130 if (DiscardBlobBytes(image,length-16) == MagickFalse)
1131 ThrowFileException(exception,CorruptImageError,
1132 "UnexpectedEndOfFile",image->filename);
1134 combinedlength+=length+4; /* +4 for length */
1135 length=ReadBlobMSBLong(image);
1139 Layer blending ranges info.
1141 if (image->debug != MagickFalse)
1142 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1143 " layer blending ranges: length=%.20g",(double)
1144 ((MagickOffsetType) length));
1146 We read it, but don't use it...
1148 for (j=0; j < (ssize_t) (length); j+=8)
1150 size_t blend_source=ReadBlobMSBLong(image);
1151 size_t blend_dest=ReadBlobMSBLong(image);
1152 if (image->debug != MagickFalse)
1153 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1154 " source(%x), dest(%x)",(unsigned int)
1155 blend_source,(unsigned int) blend_dest);
1158 combinedlength+=length+4;
1162 length=(size_t) ReadBlobByte(image);
1163 for (j=0; j < (ssize_t) length; j++)
1164 layer_info[i].name[j]=(unsigned char) ReadBlobByte(image);
1165 layer_info[i].name[j]='\0';
1166 if (image->debug != MagickFalse)
1167 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1168 " layer name: %s",layer_info[i].name);
1169 combinedlength+=length+1;
1171 #if 0 /* still in development */
1173 Adjustment layers and other stuff...
1176 char alsig[4], alkey[4];
1178 count=ReadBlob(image,4,alsig);
1179 if ((count == 0) || (LocaleNCompare(alsig,"8BIM",4) != 0)) {
1180 if (debug != MagickFalse)
1182 if (image->debug != MagickFalse)
1183 (void) LogMagickEvent(CoderEvent,GetMagickModule()," adjustment layer type was %.4s instead of 8BIM", alsig);
1185 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1187 count=ReadBlob(image,4,alkey);
1188 length=ReadBlobMSBLong(image);
1189 if (debug != MagickFalse)
1191 if (image->debug != MagickFalse)
1192 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1193 " adjustment layer key: %.4s, data length=%.20g",
1194 alkey, (double) length);
1198 for (j=0; j < (ssize_t) (length); j++)
1199 (void) ReadBlobByte(image);
1203 combinedlength += 12 + length; /* sig, key, length + the actual length*/
1207 Skip the rest of the variable data until we support it.
1209 if (image->debug != MagickFalse)
1210 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1211 " unsupported data: length=%.20g",(double)
1212 ((MagickOffsetType) (size-combinedlength)));
1213 if (DiscardBlobBytes(image,size-combinedlength) == MagickFalse)
1214 ThrowFileException(exception,CorruptImageError,
1215 "UnexpectedEndOfFile",image->filename);
1218 Allocate layered image.
1220 layer_info[i].image=CloneImage(image,layer_info[i].page.width,
1221 layer_info[i].page.height == ~0U ? 1 : layer_info[i].page.height,
1222 MagickFalse,exception);
1223 if (layer_info[i].image == (Image *) NULL)
1225 for (j=0; j < i; j++)
1226 layer_info[j].image=DestroyImage(layer_info[j].image);
1227 if (image->debug != MagickFalse)
1228 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1229 " allocation of image for layer %.20g failed",(double) i);
1230 ThrowReaderException(ResourceLimitError,
1231 "MemoryAllocationFailed");
1233 if (image->debug != MagickFalse)
1234 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1235 " setting up new layer image");
1236 if (image_info->ping != MagickFalse)
1237 (void) SetImageBackgroundColor(layer_info[i].image,exception);
1238 layer_info[i].image->compose=
1239 PSDBlendModeToCompositeOperator(layer_info[i].blendkey);
1240 if (layer_info[i].visible == MagickFalse)
1241 layer_info[i].image->compose=NoCompositeOp;
1242 if (psd_info.mode == CMYKMode)
1243 SetImageColorspace(layer_info[i].image,CMYKColorspace,exception);
1244 if ((psd_info.mode == BitmapMode) ||
1245 (psd_info.mode == GrayscaleMode) ||
1246 (psd_info.mode == DuotoneMode))
1247 SetImageColorspace(layer_info[i].image,GRAYColorspace,exception);
1248 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1249 if (layer_info[i].channel_info[j].type == -1)
1250 layer_info[i].image->alpha_trait=BlendPixelTrait;
1252 Set up some hidden attributes for folks that need them.
1254 (void) FormatLocaleString(message,MaxTextExtent,"%.20gld",
1255 (double) layer_info[i].page.x);
1256 (void) SetImageArtifact(layer_info[i].image,"psd:layer.x",message);
1257 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",
1258 (double) layer_info[i].page.y);
1259 (void) SetImageArtifact(layer_info[i].image,"psd:layer.y",message);
1260 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",
1261 (double) layer_info[i].opacity);
1262 (void) SetImageArtifact(layer_info[i].image,"psd:layer.opacity",
1264 (void) SetImageProperty(layer_info[i].image,"label",(char *)
1265 layer_info[i].name,exception);
1267 if (check_background == MagickFalse)
1269 if (image->debug != MagickFalse)
1270 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1271 " reading image data for layers");
1273 Read pixel data for each layer.
1275 for (i=0; i < number_layers; i++)
1277 if (image->debug != MagickFalse)
1278 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1279 " reading data for layer %.20g",(double) i);
1280 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1282 if (image->debug != MagickFalse)
1283 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1284 " reading data for channel %.20g",(double) j);
1286 if (layer_info[i].channel_info[j].size <= (2*layer_info[i].image->rows))
1291 if (image->debug != MagickFalse)
1292 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1293 " layer data is empty");
1295 A layer without data.
1297 for (k=0; k < (ssize_t) layer_info[i].channel_info[j].size; k++)
1298 (void) ReadBlobByte(layer_info[i].image);
1302 offsets=(MagickOffsetType *) NULL;
1303 layer_info[i].image->compression=NoCompression;
1304 compression=ReadBlobMSBShort(layer_info[i].image);
1305 if ((layer_info[i].page.height != 0) &&
1306 (layer_info[i].page.width != 0))
1308 if (compression == 1)
1311 Read RLE compressed data.
1313 layer_info[i].image->compression=RLECompression;
1314 if (image->debug != MagickFalse)
1315 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1316 " layer data is RLE compressed");
1317 offsets=(MagickOffsetType *) AcquireQuantumMemory(
1318 layer_info[i].image->rows,sizeof(*offsets));
1319 if (offsets == (MagickOffsetType *) NULL)
1320 ThrowReaderException(ResourceLimitError,
1321 "MemoryAllocationFailed");
1322 for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
1323 offsets[y]=GetPSDOffset(&psd_info,
1324 layer_info[i].image);
1326 status=ReadPSDLayer(layer_info[i].image,
1327 layer_info[i].channels,
1328 layer_info[i].channel_info[j].type,offsets,exception);
1329 if (compression == 1)
1330 offsets=(MagickOffsetType *) RelinquishMagickMemory(
1332 if (status == MagickFalse)
1336 if (layer_info[i].opacity != OpaqueAlpha)
1339 Correct for opacity level.
1341 for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
1343 q=GetAuthenticPixels(layer_info[i].image,0,y,
1344 layer_info[i].image->columns,1,exception);
1345 if (q == (Quantum *) NULL)
1347 for (x=0; x < (ssize_t) layer_info[i].image->columns; x++)
1349 SetPixelAlpha(layer_info[i].image,(Quantum)
1350 (QuantumScale*(GetPixelAlpha(layer_info[i].image,q))*
1351 layer_info[i].opacity),q);
1352 q+=GetPixelChannels(layer_info[i].image);
1354 if (SyncAuthenticPixels(layer_info[i].image,exception) == MagickFalse)
1358 if (layer_info[i].image->colorspace == CMYKColorspace)
1359 (void) NegateImage(layer_info[i].image,MagickFalse,exception);
1360 status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType)
1362 if (status == MagickFalse)
1365 /* added by palf -> invisible group layer make layer of this group
1366 invisible I consider that all layer with width and height null are
1367 layer for group layer */
1369 short inside_layer = 0;
1370 short layer_visible = 0;
1371 for (i=number_layers-1; i >=0; i--)
1373 if ((layer_info[i].page.width == 0) ||
1374 (layer_info[i].page.height == 0))
1376 if (inside_layer == 0)
1379 layer_visible=(short int) layer_info[i].visible;
1387 if ((inside_layer == 1) && (layer_visible == 0))
1389 layer_info[i].visible=(unsigned char) layer_visible;
1390 layer_info[i].image->compose=NoCompositeOp;
1394 /* added by palf -> suppression of empty layer */
1395 /* I consider that all layer with width and height null are layer for group layer */
1396 for (i=0; i < number_layers; i++)
1398 if ((layer_info[i].page.width == 0) ||
1399 (layer_info[i].page.height == 0))
1401 if (layer_info[i].image != (Image *) NULL)
1402 layer_info[i].image=DestroyImage(layer_info[i].image);
1403 for (j=i; j < number_layers - 1; j++)
1404 layer_info[j] = layer_info[j+1];
1409 mask_size = ReadBlobMSBLong(image); /* global mask size: currently ignored */
1411 if (number_layers > 0)
1413 if (image->debug != MagickFalse)
1414 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1415 " putting layers into image list");
1416 for (i=0; i < number_layers; i++)
1419 layer_info[i].image->previous=layer_info[i-1].image;
1420 if (i < (number_layers-1))
1421 layer_info[i].image->next=layer_info[i+1].image;
1422 layer_info[i].image->page=layer_info[i].page;
1424 image->next=layer_info[0].image;
1425 layer_info[0].image->previous=image;
1426 layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info);
1428 layer_offset-=TellBlob(image);
1429 offset=SeekBlob(image,layer_offset,SEEK_CUR);
1434 Read the precombined layer, present for PSD < 4 compatibility
1436 if (image->debug != MagickFalse)
1437 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1438 " reading the precombined layer");
1439 offsets=(MagickOffsetType *) NULL;
1440 image->compression=NoCompression;
1441 compression=ReadBlobMSBShort(image);
1442 if (compression == 1)
1445 Read Packbit encoded pixel data as separate planes.
1447 image->compression=RLECompression;
1448 offsets=(MagickOffsetType *) AcquireQuantumMemory(image->rows,
1449 psd_info.channels*sizeof(*offsets));
1450 if (offsets == (MagickOffsetType *) NULL)
1451 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1452 for (i=0; i < (ssize_t) (image->rows*psd_info.channels); i++)
1453 offsets[i]=GetPSDOffset(&psd_info,image);
1455 for (i=0; i < (ssize_t) psd_info.channels; i++)
1457 status=ReadPSDLayer(image,psd_info.channels,i,offsets+i*image->rows,
1459 if (status == MagickFalse)
1461 status=SetImageProgress(image,LoadImagesTag,i,psd_info.channels);
1462 if (status == MagickFalse)
1465 if (compression == 1)
1466 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1467 if (image->colorspace == CMYKColorspace)
1468 (void) NegateImage(image,MagickFalse,exception);
1469 (void) CloseBlob(image);
1470 return(GetFirstImageInList(image));
1474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1478 % R e g i s t e r P S D I m a g e %
1482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1484 % RegisterPSDImage() adds properties for the PSD image format to
1485 % the list of supported formats. The properties include the image format
1486 % tag, a method to read and/or write the format, whether the format
1487 % supports the saving of more than one frame to the same file or blob,
1488 % whether the format supports native in-memory I/O, and a brief
1489 % description of the format.
1491 % The format of the RegisterPSDImage method is:
1493 % size_t RegisterPSDImage(void)
1496 ModuleExport size_t RegisterPSDImage(void)
1501 entry=SetMagickInfo("PSB");
1502 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1503 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1504 entry->magick=(IsImageFormatHandler *) IsPSD;
1505 entry->seekable_stream=MagickTrue;
1506 entry->description=ConstantString("Adobe Large Document Format");
1507 entry->module=ConstantString("PSD");
1508 (void) RegisterMagickInfo(entry);
1509 entry=SetMagickInfo("PSD");
1510 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1511 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1512 entry->magick=(IsImageFormatHandler *) IsPSD;
1513 entry->seekable_stream=MagickTrue;
1514 entry->description=ConstantString("Adobe Photoshop bitmap");
1515 entry->module=ConstantString("PSD");
1516 (void) RegisterMagickInfo(entry);
1517 return(MagickImageCoderSignature);
1521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1525 % U n r e g i s t e r P S D I m a g e %
1529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1531 % UnregisterPSDImage() removes format registrations made by the
1532 % PSD module from the list of supported formats.
1534 % The format of the UnregisterPSDImage method is:
1536 % UnregisterPSDImage(void)
1539 ModuleExport void UnregisterPSDImage(void)
1541 (void) UnregisterMagickInfo("PSB");
1542 (void) UnregisterMagickInfo("PSD");
1546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1550 % W r i t e P S D I m a g e %
1554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1556 % WritePSDImage() writes an image in the Adobe Photoshop encoded image format.
1558 % The format of the WritePSDImage method is:
1560 % MagickBooleanType WritePSDImage(const ImageInfo *image_info,
1561 % Image *image,ExceptionInfo *exception)
1563 % A description of each parameter follows.
1565 % o image_info: the image info.
1567 % o image: The image.
1569 % o exception: return any errors or warnings in this structure.
1573 static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image,
1574 const size_t offset)
1576 if (psd_info->version == 1)
1577 return(WriteBlobMSBShort(image,(unsigned short) offset));
1578 return(WriteBlobMSBLong(image,(unsigned short) offset));
1581 static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image,
1582 const MagickSizeType size)
1584 if (psd_info->version == 1)
1585 return(WriteBlobMSBLong(image,(unsigned int) size));
1586 return(WriteBlobMSBLongLong(image,size));
1589 static size_t PSDPackbitsEncodeImage(Image *image,const size_t length,
1590 const unsigned char *pixels,unsigned char *compact_pixels,
1591 ExceptionInfo *exception)
1600 register unsigned char
1607 Compress pixels with Packbits encoding.
1609 assert(image != (Image *) NULL);
1610 assert(image->signature == MagickSignature);
1611 if (image->debug != MagickFalse)
1612 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1613 assert(pixels != (unsigned char *) NULL);
1614 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1615 if (packbits == (unsigned char *) NULL)
1616 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1619 for (i=(ssize_t) length; i != 0; )
1626 *q++=(unsigned char) 0;
1633 *q++=(unsigned char) 1;
1641 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1643 *q++=(unsigned char) ((256-3)+1);
1647 *q++=(unsigned char) 2;
1655 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1661 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
1668 *q++=(unsigned char) ((256-count)+1);
1677 while ((*(pixels+count) != *(pixels+count+1)) ||
1678 (*(pixels+count+1) != *(pixels+count+2)))
1680 packbits[count+1]=pixels[count];
1682 if (((ssize_t) count >= (i-3)) || (count >= 127))
1686 *packbits=(unsigned char) (count-1);
1687 for (j=0; j <= (ssize_t) count; j++)
1694 *q++=(unsigned char) 128; /* EOD marker */
1695 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
1696 return((size_t) (q-compact_pixels));
1699 static void WritePackbitsLength(const PSDInfo *psd_info,
1700 const ImageInfo *image_info,Image *image,Image *next_image,
1701 unsigned char *compact_pixels,const QuantumType quantum_type,
1702 ExceptionInfo *exception)
1707 register const Quantum
1720 if (next_image->depth > 8)
1721 next_image->depth=16;
1722 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1724 quantum_info=AcquireQuantumInfo(image_info,image);
1725 pixels=GetQuantumPixels(quantum_info);
1726 for (y=0; y < (ssize_t) next_image->rows; y++)
1728 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
1729 if (p == (const Quantum *) NULL)
1731 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
1732 quantum_type,pixels,exception);
1733 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
1735 (void) SetPSDOffset(psd_info,image,length);
1737 quantum_info=DestroyQuantumInfo(quantum_info);
1740 static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info,
1741 Image *image,Image *next_image,unsigned char *compact_pixels,
1742 const QuantumType quantum_type,const MagickBooleanType compression_flag,
1743 ExceptionInfo *exception)
1754 register const Quantum
1768 if ((compression_flag != MagickFalse) &&
1769 (next_image->compression != RLECompression))
1770 (void) WriteBlobMSBShort(image,0);
1771 if (next_image->depth > 8)
1772 next_image->depth=16;
1773 monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
1774 MagickTrue : MagickFalse;
1775 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1777 quantum_info=AcquireQuantumInfo(image_info,image);
1778 pixels=GetQuantumPixels(quantum_info);
1779 for (y=0; y < (ssize_t) next_image->rows; y++)
1781 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
1782 if (p == (const Quantum *) NULL)
1784 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
1785 quantum_type,pixels,exception);
1786 if (monochrome != MagickFalse)
1787 for (i=0; i < (ssize_t) length; i++)
1788 pixels[i]=(~pixels[i]);
1789 if (next_image->compression != RLECompression)
1790 (void) WriteBlob(image,length,pixels);
1793 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
1795 (void) WriteBlob(image,length,compact_pixels);
1798 quantum_info=DestroyQuantumInfo(quantum_info);
1801 static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info,
1802 const ImageInfo *image_info,Image *image,Image *next_image,
1803 const MagickBooleanType separate,ExceptionInfo *exception)
1816 Write uncompressed pixels as separate planes.
1819 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1820 compact_pixels=(unsigned char *) NULL;
1821 if (next_image->compression == RLECompression)
1823 compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels*
1824 next_image->columns,packet_size*sizeof(*compact_pixels));
1825 if (compact_pixels == (unsigned char *) NULL)
1826 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1829 if (IsImageGray(next_image,exception) != MagickFalse)
1831 if (next_image->compression == RLECompression)
1834 Packbits compression.
1836 (void) WriteBlobMSBShort(image,1);
1837 WritePackbitsLength(psd_info,image_info,image,next_image,
1838 compact_pixels,GrayQuantum,exception);
1839 if (next_image->alpha_trait == BlendPixelTrait)
1840 WritePackbitsLength(psd_info,image_info,image,next_image,
1841 compact_pixels,AlphaQuantum,exception);
1843 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1844 GrayQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1845 MagickFalse,exception);
1846 if (next_image->alpha_trait == BlendPixelTrait)
1847 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1848 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1849 MagickFalse,exception);
1850 (void) SetImageProgress(image,SaveImagesTag,0,1);
1853 if (next_image->storage_class == PseudoClass)
1855 if (next_image->compression == RLECompression)
1858 Packbits compression.
1860 (void) WriteBlobMSBShort(image,1);
1861 WritePackbitsLength(psd_info,image_info,image,next_image,
1862 compact_pixels,IndexQuantum,exception);
1863 if (next_image->alpha_trait == BlendPixelTrait)
1864 WritePackbitsLength(psd_info,image_info,image,next_image,
1865 compact_pixels,AlphaQuantum,exception);
1867 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1868 IndexQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1869 MagickFalse,exception);
1870 if (next_image->alpha_trait == BlendPixelTrait)
1871 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1872 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1873 MagickFalse,exception);
1874 (void) SetImageProgress(image,SaveImagesTag,0,1);
1878 if (next_image->colorspace == CMYKColorspace)
1879 (void) NegateImage(next_image,MagickFalse,exception);
1880 if (next_image->compression == RLECompression)
1883 Packbits compression.
1885 (void) WriteBlobMSBShort(image,1);
1886 WritePackbitsLength(psd_info,image_info,image,next_image,
1887 compact_pixels,RedQuantum,exception);
1888 WritePackbitsLength(psd_info,image_info,image,next_image,
1889 compact_pixels,GreenQuantum,exception);
1890 WritePackbitsLength(psd_info,image_info,image,next_image,
1891 compact_pixels,BlueQuantum,exception);
1892 if (next_image->colorspace == CMYKColorspace)
1893 WritePackbitsLength(psd_info,image_info,image,next_image,
1894 compact_pixels,BlackQuantum,exception);
1895 if (next_image->alpha_trait == BlendPixelTrait)
1896 WritePackbitsLength(psd_info,image_info,image,next_image,
1897 compact_pixels,AlphaQuantum,exception);
1899 (void) SetImageProgress(image,SaveImagesTag,0,6);
1900 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1901 RedQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1902 MagickFalse,exception);
1903 (void) SetImageProgress(image,SaveImagesTag,1,6);
1904 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1905 GreenQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1906 MagickFalse,exception);
1907 (void) SetImageProgress(image,SaveImagesTag,2,6);
1908 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1909 BlueQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1910 MagickFalse,exception);
1911 (void) SetImageProgress(image,SaveImagesTag,3,6);
1912 if (next_image->colorspace == CMYKColorspace)
1913 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1914 BlackQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1915 MagickFalse,exception);
1916 (void) SetImageProgress(image,SaveImagesTag,4,6);
1917 if (next_image->alpha_trait == BlendPixelTrait)
1918 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1919 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1920 MagickFalse,exception);
1921 (void) SetImageProgress(image,SaveImagesTag,5,6);
1922 if (next_image->colorspace == CMYKColorspace)
1923 (void) NegateImage(next_image,MagickFalse,exception);
1925 if (next_image->compression == RLECompression)
1926 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
1930 static void WritePascalString(Image* inImage,const char *inString,int inPad)
1941 length=(strlen(inString) > 255UL ) ? 255UL : strlen(inString);
1943 (void) WriteBlobByte(inImage,0);
1946 (void) WriteBlobByte(inImage,(unsigned char) length);
1947 (void) WriteBlob(inImage, length, (const unsigned char *) inString);
1950 if ((length % inPad) == 0)
1952 for (i=0; i < (ssize_t) (inPad-(length % inPad)); i++)
1953 (void) WriteBlobByte(inImage,0);
1956 static void WriteResolutionResourceBlock(Image *image)
1965 x_resolution=65536.0*image->resolution.x+0.5;
1966 y_resolution=65536.0*image->resolution.y+0.5;
1968 if (image->units == PixelsPerCentimeterResolution)
1970 x_resolution=2.54*65536.0*image->resolution.x*0.5;
1971 y_resolution=2.54*65536.0*image->resolution.y+0.5;
1974 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
1975 (void) WriteBlobMSBShort(image,0x03ED);
1976 (void) WriteBlobMSBShort(image,0);
1977 (void) WriteBlobMSBLong(image,16); /* resource size */
1978 (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5));
1979 (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */
1980 (void) WriteBlobMSBShort(image,units); /* width unit */
1981 (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5));
1982 (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */
1983 (void) WriteBlobMSBShort(image,units); /* height unit */
1986 static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile)
1988 register const unsigned char
2005 length=GetStringInfoLength(bim_profile);
2008 datum=GetStringInfoDatum(bim_profile);
2009 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2011 register unsigned char
2014 q=(unsigned char *) p;
2015 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2017 p=PushLongPixel(MSBEndian,p,&long_sans);
2018 p=PushShortPixel(MSBEndian,p,&id);
2019 p=PushShortPixel(MSBEndian,p,&short_sans);
2020 p=PushLongPixel(MSBEndian,p,&count);
2021 if (id == 0x0000040f)
2023 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2024 (PSDQuantum(count)+12)-(q-datum));
2025 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2029 if ((count & 0x01) != 0)
2034 static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile)
2036 register const unsigned char
2053 length=GetStringInfoLength(bim_profile);
2056 datum=GetStringInfoDatum(bim_profile);
2057 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2059 register unsigned char
2062 q=(unsigned char *) p;
2063 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2065 p=PushLongPixel(MSBEndian,p,&long_sans);
2066 p=PushShortPixel(MSBEndian,p,&id);
2067 p=PushShortPixel(MSBEndian,p,&short_sans);
2068 p=PushLongPixel(MSBEndian,p,&count);
2069 if ((id == 0x000003ed) && (PSDQuantum(count) < (ssize_t) (length-12)))
2071 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2072 (PSDQuantum(count)+12)-(q-datum));
2073 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2077 if ((count & 0x01) != 0)
2082 static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
2083 ExceptionInfo *exception)
2112 rounded_layer_info_size;
2123 assert(image_info != (const ImageInfo *) NULL);
2124 assert(image_info->signature == MagickSignature);
2125 assert(image != (Image *) NULL);
2126 assert(image->signature == MagickSignature);
2127 if (image->debug != MagickFalse)
2128 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2129 assert(exception != (ExceptionInfo *) NULL);
2130 assert(exception->signature == MagickSignature);
2131 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2132 if (status == MagickFalse)
2134 packet_size=(size_t) (image->depth > 8 ? 6 : 3);
2135 if (image->alpha_trait == BlendPixelTrait)
2136 packet_size+=image->depth > 8 ? 2 : 1;
2138 if ((LocaleCompare(image_info->magick,"PSB") == 0) ||
2139 (image->columns > 30000) || (image->rows > 30000))
2141 (void) WriteBlob(image,4,(const unsigned char *) "8BPS");
2142 (void) WriteBlobMSBShort(image,psd_info.version); /* version */
2143 for (i=1; i <= 6; i++)
2144 (void) WriteBlobByte(image, 0); /* 6 bytes of reserved */
2145 if (IsImageGray(image,exception) != MagickFalse)
2146 num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
2148 if (image->storage_class == PseudoClass)
2149 num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
2152 if (image->colorspace != CMYKColorspace)
2153 num_channels=(image->alpha_trait == BlendPixelTrait ? 4UL : 3UL);
2155 num_channels=(image->alpha_trait == BlendPixelTrait ? 5UL : 4UL);
2157 (void) WriteBlobMSBShort(image,(unsigned short) num_channels);
2158 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
2159 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
2160 if (IsImageGray(image,exception) != MagickFalse)
2168 monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
2169 MagickTrue : MagickFalse;
2170 (void) WriteBlobMSBShort(image,(unsigned short)
2171 (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8));
2172 (void) WriteBlobMSBShort(image,(unsigned short)
2173 (monochrome != MagickFalse ? BitmapMode : GrayscaleMode));
2177 (void) WriteBlobMSBShort(image,(unsigned short) (image->storage_class ==
2178 PseudoClass ? 8 : image->depth > 8 ? 16 : 8));
2179 if (((image_info->colorspace != UndefinedColorspace) ||
2180 (image->colorspace != CMYKColorspace)) &&
2181 (image_info->colorspace != CMYKColorspace))
2183 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2184 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2185 (void) WriteBlobMSBShort(image,(unsigned short)
2186 (image->storage_class == PseudoClass ? IndexedMode : RGBMode));
2190 if (image->colorspace != CMYKColorspace)
2191 (void) TransformImageColorspace(image,CMYKColorspace,exception);
2192 (void) WriteBlobMSBShort(image,CMYKMode);
2195 if ((IsImageGray(image,exception) != MagickFalse) ||
2196 (image->storage_class == DirectClass) || (image->colors > 256))
2197 (void) WriteBlobMSBLong(image,0);
2201 Write PSD raster colormap.
2203 (void) WriteBlobMSBLong(image,768);
2204 for (i=0; i < (ssize_t) image->colors; i++)
2205 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red));
2206 for ( ; i < 256; i++)
2207 (void) WriteBlobByte(image,0);
2208 for (i=0; i < (ssize_t) image->colors; i++)
2209 (void) WriteBlobByte(image,ScaleQuantumToChar(
2210 image->colormap[i].green));
2211 for ( ; i < 256; i++)
2212 (void) WriteBlobByte(image,0);
2213 for (i=0; i < (ssize_t) image->colors; i++)
2214 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue));
2215 for ( ; i < 256; i++)
2216 (void) WriteBlobByte(image,0);
2219 Image resource block.
2221 length=28; /* 0x03EB */
2222 bim_profile=(StringInfo *) GetImageProfile(image,"8bim");
2223 icc_profile=GetImageProfile(image,"icc");
2224 if (bim_profile != (StringInfo *) NULL)
2226 bim_profile=CloneStringInfo(bim_profile);
2227 if (icc_profile != (StringInfo *) NULL)
2228 RemoveICCProfileFromResourceBlock(bim_profile);
2229 RemoveResolutionFromResourceBlock(bim_profile);
2230 length+=PSDQuantum(GetStringInfoLength(bim_profile));
2232 if (icc_profile != (const StringInfo *) NULL)
2233 length+=PSDQuantum(GetStringInfoLength(icc_profile))+12;
2234 (void) WriteBlobMSBLong(image,(unsigned int) length);
2235 WriteResolutionResourceBlock(image);
2236 if (bim_profile != (StringInfo *) NULL)
2238 (void) WriteBlob(image,GetStringInfoLength(bim_profile),
2239 GetStringInfoDatum(bim_profile));
2240 bim_profile=DestroyStringInfo(bim_profile);
2242 if (icc_profile != (StringInfo *) NULL)
2244 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2245 (void) WriteBlobMSBShort(image,0x0000040F);
2246 (void) WriteBlobMSBShort(image,0);
2247 (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength(
2249 (void) WriteBlob(image,GetStringInfoLength(icc_profile),
2250 GetStringInfoDatum(icc_profile));
2251 if ((MagickOffsetType) GetStringInfoLength(icc_profile) !=
2252 PSDQuantum(GetStringInfoLength(icc_profile)))
2253 (void) WriteBlobByte(image,0);
2257 base_image=GetNextImageInList(image);
2258 if ((image->alpha_trait == BlendPixelTrait) && (base_image == (Image *) NULL))
2260 next_image=base_image;
2261 while ( next_image != NULL )
2263 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2264 if (IsImageGray(next_image,exception) != MagickFalse)
2265 num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
2267 if (next_image->storage_class == PseudoClass)
2268 num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
2270 if (next_image->colorspace != CMYKColorspace)
2271 num_channels=next_image->alpha_trait == BlendPixelTrait ? 4UL : 3UL;
2273 num_channels=next_image->alpha_trait == BlendPixelTrait ? 5UL : 4UL;
2274 channelLength=(size_t) (next_image->columns*next_image->rows*packet_size+2);
2275 layer_info_size+=(size_t) (4*4+2+num_channels*6+(psd_info.version == 1 ? 8 :
2276 16)+4*1+4+num_channels*channelLength);
2277 property=(const char *) GetImageProperty(next_image,"label",exception);
2278 if (property == (const char *) NULL)
2279 layer_info_size+=16;
2285 length=strlen(property);
2286 layer_info_size+=8+length+(4-(length % 4));
2289 next_image=GetNextImageInList(next_image);
2291 if (layer_count == 0)
2292 (void) SetPSDSize(&psd_info,image,0);
2298 (void) SetPSDSize(&psd_info,image,layer_info_size+
2299 (psd_info.version == 1 ? 8 : 16));
2300 if ((layer_info_size/2) != ((layer_info_size+1)/2))
2301 rounded_layer_info_size=layer_info_size+1;
2303 rounded_layer_info_size=layer_info_size;
2304 (void) SetPSDSize(&psd_info,image,rounded_layer_info_size);
2305 (void) WriteBlobMSBShort(image,(unsigned short) layer_count);
2307 compression=base_image->compression;
2308 next_image=base_image;
2309 while (next_image != NULL)
2311 next_image->compression=NoCompression;
2312 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y);
2313 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x);
2314 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y+
2316 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x+
2317 next_image->columns);
2318 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2319 channel_size=(unsigned int) ((packet_size*next_image->rows*
2320 next_image->columns)+2);
2321 if ((IsImageGray(next_image,exception) != MagickFalse) ||
2322 (next_image->storage_class == PseudoClass))
2324 (void) WriteBlobMSBShort(image,(unsigned short)
2325 (next_image->alpha_trait == BlendPixelTrait ? 2 : 1));
2326 (void) WriteBlobMSBShort(image,0);
2327 (void) SetPSDSize(&psd_info,image,channel_size);
2328 if (next_image->alpha_trait == BlendPixelTrait)
2330 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2331 (void) SetPSDSize(&psd_info,image,channel_size);
2335 if (next_image->colorspace != CMYKColorspace)
2337 (void) WriteBlobMSBShort(image,(unsigned short)
2338 (next_image->alpha_trait == BlendPixelTrait ? 4 : 3));
2339 (void) WriteBlobMSBShort(image,0);
2340 (void) SetPSDSize(&psd_info,image,channel_size);
2341 (void) WriteBlobMSBShort(image,1);
2342 (void) SetPSDSize(&psd_info,image,channel_size);
2343 (void) WriteBlobMSBShort(image,2);
2344 (void) SetPSDSize(&psd_info,image,channel_size);
2345 if (next_image->alpha_trait == BlendPixelTrait)
2347 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2348 (void) SetPSDSize(&psd_info,image,channel_size);
2353 (void) WriteBlobMSBShort(image,(unsigned short)
2354 (next_image->alpha_trait ? 5 : 4));
2355 (void) WriteBlobMSBShort(image,0);
2356 (void) SetPSDSize(&psd_info,image,channel_size);
2357 (void) WriteBlobMSBShort(image,1);
2358 (void) SetPSDSize(&psd_info,image,channel_size);
2359 (void) WriteBlobMSBShort(image,2);
2360 (void) SetPSDSize(&psd_info,image,channel_size);
2361 (void) WriteBlobMSBShort(image,3);
2362 (void) SetPSDSize(&psd_info,image,channel_size);
2363 if (next_image->alpha_trait)
2365 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2366 (void) SetPSDSize(&psd_info,image,channel_size);
2369 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2370 (void) WriteBlob(image,4,(const unsigned char *)
2371 CompositeOperatorToPSDBlendMode(next_image->compose));
2372 (void) WriteBlobByte(image,255); /* layer opacity */
2373 (void) WriteBlobByte(image,0);
2374 (void) WriteBlobByte(image,1); /* layer propertys - visible, etc. */
2375 (void) WriteBlobByte(image,0);
2376 property=(const char *) GetImageProperty(next_image,"label",exception);
2377 if (property == (const char *) NULL)
2379 (void) WriteBlobMSBLong(image,16);
2380 (void) WriteBlobMSBLong(image,0);
2381 (void) WriteBlobMSBLong(image,0);
2382 (void) FormatLocaleString((char *) layer_name,MaxTextExtent,
2383 "L%06ld",(long) layer_count++);
2384 WritePascalString( image, (char*)layer_name, 4 );
2391 length=strlen(property);
2392 (void) WriteBlobMSBLong(image,(unsigned int) (length+(4-
2394 (void) WriteBlobMSBLong(image,0);
2395 (void) WriteBlobMSBLong(image,0);
2396 WritePascalString(image,property,4);
2398 next_image=GetNextImageInList(next_image);
2403 next_image=base_image;
2404 while (next_image != NULL)
2406 status=WriteImageChannels(&psd_info,image_info,image,next_image,
2407 MagickTrue,exception);
2408 next_image=GetNextImageInList(next_image);
2410 (void) WriteBlobMSBLong(image,0); /* user mask data */
2411 base_image->compression=compression;
2414 Write composite image.
2416 status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse,
2418 (void) CloseBlob(image);