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 status=AcquireImageColormap(image,psd_info.depth != 16 ? 256 : 65536,
854 if (status == MagickFalse)
855 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
856 image->alpha_trait=psd_info.channels >= 2 ? BlendPixelTrait :
858 if (image->debug != MagickFalse)
859 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
860 " Image colormap allocated");
861 SetImageColorspace(image,GRAYColorspace,exception);
863 image->alpha_trait=UndefinedPixelTrait;
865 Read PSD raster colormap only present for indexed and duotone images.
867 length=ReadBlobMSBLong(image);
870 if (image->debug != MagickFalse)
871 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
872 " reading colormap");
873 if (psd_info.mode == DuotoneMode)
876 Duotone image data; the format of this data is undocumented.
878 data=(unsigned char *) AcquireQuantumMemory((size_t) length,
880 if (data == (unsigned char *) NULL)
881 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
882 count=ReadBlob(image,(size_t) length,data);
883 data=(unsigned char *) RelinquishMagickMemory(data);
888 Read PSD raster colormap.
890 if (AcquireImageColormap(image,(size_t) (length/3),exception) == MagickFalse)
891 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
892 for (i=0; i < (ssize_t) image->colors; i++)
893 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
894 ReadBlobByte(image));
895 for (i=0; i < (ssize_t) image->colors; i++)
896 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
897 ReadBlobByte(image));
898 for (i=0; i < (ssize_t) image->colors; i++)
899 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
900 ReadBlobByte(image));
901 image->alpha_trait=UndefinedPixelTrait;
904 length=ReadBlobMSBLong(image);
911 Image resources block.
913 if (image->debug != MagickFalse)
914 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
915 " reading image resource blocks - %.20g bytes",(double)
916 ((MagickOffsetType) length));
917 blocks=(unsigned char *) AcquireQuantumMemory((size_t) length,
919 if (blocks == (unsigned char *) NULL)
920 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
921 count=ReadBlob(image,(size_t) length,blocks);
922 if ((count != (ssize_t) length) ||
923 (LocaleNCompare((char *) blocks,"8BIM",4) != 0))
925 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
926 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
928 (void) ParseImageResourceBlocks(image,blocks,(size_t) length,
930 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
933 If we are only "pinging" the image, then we're done - so return.
935 if (image_info->ping != MagickFalse)
937 (void) CloseBlob(image);
938 return(GetFirstImageInList(image));
941 Layer and mask block.
943 layer_info=(LayerInfo *) NULL;
945 length=GetPSDSize(&psd_info,image);
948 length=ReadBlobMSBLong(image);
949 length=ReadBlobMSBLong(image);
951 check_background=MagickFalse;
952 if ((image_info->number_scenes == 1) && (image_info->scene == 0))
954 if (image->debug != MagickFalse)
955 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
956 " read composite only");
957 check_background=MagickTrue;
961 if (image->debug != MagickFalse)
962 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
963 " image has no layers");
967 offset=TellBlob(image);
968 size=GetPSDSize(&psd_info,image);
980 quantum=psd_info.version == 1 ? 4UL : 8UL;
981 tag=ReadBlobMSBLong(image);
983 count=ReadBlob(image,4,(unsigned char *) type);
984 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
986 if (DiscardBlobBytes(image,length-quantum-8) == MagickFalse)
987 ThrowFileException(exception,CorruptImageError,
988 "UnexpectedEndOfFile",image->filename);
992 count=ReadBlob(image,4,(unsigned char *) type);
993 if ((count != 0) && (LocaleNCompare(type,"Lr16",4) == 0))
994 size=GetPSDSize(&psd_info,image);
996 if (DiscardBlobBytes(image,length-quantum-12) == MagickFalse)
997 ThrowFileException(exception,CorruptImageError,
998 "UnexpectedEndOfFile",image->filename);
1006 image->alpha_trait=psd_info.channels > psd_info.color_channels ?
1007 BlendPixelTrait : UndefinedPixelTrait;
1009 if (image->debug != MagickFalse)
1010 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1011 image->alpha_trait ? " image has matte" : " image has no matte");
1013 layer_offset=offset+length;
1014 number_layers=(short) ReadBlobMSBShort(image);
1015 if (image->debug != MagickFalse)
1016 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1017 " image contains %.20g layers",(double) number_layers);
1018 if (number_layers < 0)
1021 Weird hack in PSD format to ignore first alpha channel.
1024 (void) skip_first_alpha;
1025 number_layers=MagickAbsoluteValue(number_layers);
1026 if (image->debug != MagickFalse)
1027 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1028 " negative layer count corrected for");
1030 layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers,
1031 sizeof(*layer_info));
1032 if (layer_info == (LayerInfo *) NULL)
1034 if (image->debug != MagickFalse)
1035 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1036 " allocation of LayerInfo failed");
1037 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1039 (void) ResetMagickMemory(layer_info,0,(size_t) number_layers*
1040 sizeof(*layer_info));
1041 for (i=0; i < number_layers; i++)
1047 if (image->debug != MagickFalse)
1048 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1049 " reading layer #%.20g",(double) i+1);
1050 layer_info[i].page.y=(int) ReadBlobMSBLong(image);
1051 layer_info[i].page.x=(int) ReadBlobMSBLong(image);
1052 y=(int) ReadBlobMSBLong(image);
1053 x=(int) ReadBlobMSBLong(image);
1054 layer_info[i].page.width=(ssize_t) (x-layer_info[i].page.x);
1055 layer_info[i].page.height=(ssize_t) (y-layer_info[i].page.y);
1056 layer_info[i].channels=ReadBlobMSBShort(image);
1057 if (check_background == MagickTrue)
1062 if (layer_info[i].channels == psd_info.color_channels)
1063 image->alpha_trait=UndefinedPixelTrait;
1064 quantum=psd_info.version == 1 ? 4UL : 8UL;
1065 if (DiscardBlobBytes(image,length-20-quantum) == MagickFalse)
1066 ThrowFileException(exception,CorruptImageError,
1067 "UnexpectedEndOfFile",image->filename);
1070 if (layer_info[i].channels > MaxPSDChannels)
1071 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
1072 if (image->debug != MagickFalse)
1073 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1074 " offset(%.20g,%.20g), size(%.20g,%.20g), channels=%.20g",
1075 (double) layer_info[i].page.x,(double) layer_info[i].page.y,
1076 (double) layer_info[i].page.height,(double)
1077 layer_info[i].page.width,(double) layer_info[i].channels);
1078 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1080 layer_info[i].channel_info[j].type=(short)
1081 ReadBlobMSBShort(image);
1082 layer_info[i].channel_info[j].size=(size_t)
1083 GetPSDSize(&psd_info,image);
1084 if (image->debug != MagickFalse)
1085 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1086 " channel[%.20g]: type=%.20g, size=%.20g",(double) j,
1087 (double) layer_info[i].channel_info[j].type,
1088 (double) layer_info[i].channel_info[j].size);
1090 count=ReadBlob(image,4,(unsigned char *) type);
1091 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1093 if (image->debug != MagickFalse)
1094 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1095 " layer type was %.4s instead of 8BIM", type);
1096 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1098 count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey);
1099 layer_info[i].opacity=(Quantum) ScaleCharToQuantum((unsigned char)
1100 ReadBlobByte(image));
1101 layer_info[i].clipping=(unsigned char) ReadBlobByte(image);
1102 layer_info[i].flags=(unsigned char) ReadBlobByte(image);
1103 layer_info[i].visible=!(layer_info[i].flags & 0x02);
1104 if (image->debug != MagickFalse)
1105 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1106 " blend=%.4s, opacity=%.20g, clipping=%s, flags=%d, visible=%s",
1107 layer_info[i].blendkey,(double) layer_info[i].opacity,
1108 layer_info[i].clipping ? "true" : "false",layer_info[i].flags,
1109 layer_info[i].visible ? "true" : "false");
1110 (void) ReadBlobByte(image); /* filler */
1112 size=ReadBlobMSBLong(image);
1115 if (image->debug != MagickFalse)
1116 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1117 " layer contains additional info");
1118 length=ReadBlobMSBLong(image);
1124 layer_info[i].mask.y=(int) ReadBlobMSBLong(image);
1125 layer_info[i].mask.x=(int) ReadBlobMSBLong(image);
1126 layer_info[i].mask.height=(size_t)
1127 (ReadBlobMSBLong(image)-layer_info[i].mask.y);
1128 layer_info[i].mask.width=(size_t)
1129 (ReadBlobMSBLong(image)-layer_info[i].mask.x);
1130 if (image->debug != MagickFalse)
1131 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1132 " layer mask: offset(%.20g,%.20g), size(%.20g,%.20g), length=%.20g",
1133 (double) layer_info[i].mask.x,(double) layer_info[i].mask.y,
1134 (double) layer_info[i].mask.width,(double)
1135 layer_info[i].mask.height,(double)
1136 ((MagickOffsetType) length-16));
1138 Skip over the rest of the layer mask information.
1140 if (DiscardBlobBytes(image,length-16) == MagickFalse)
1141 ThrowFileException(exception,CorruptImageError,
1142 "UnexpectedEndOfFile",image->filename);
1144 combinedlength+=length+4; /* +4 for length */
1145 length=ReadBlobMSBLong(image);
1149 Layer blending ranges info.
1151 if (image->debug != MagickFalse)
1152 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1153 " layer blending ranges: length=%.20g",(double)
1154 ((MagickOffsetType) length));
1156 We read it, but don't use it...
1158 for (j=0; j < (ssize_t) (length); j+=8)
1160 size_t blend_source=ReadBlobMSBLong(image);
1161 size_t blend_dest=ReadBlobMSBLong(image);
1162 if (image->debug != MagickFalse)
1163 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1164 " source(%x), dest(%x)",(unsigned int)
1165 blend_source,(unsigned int) blend_dest);
1168 combinedlength+=length+4;
1172 length=(size_t) ReadBlobByte(image);
1173 for (j=0; j < (ssize_t) length; j++)
1174 layer_info[i].name[j]=(unsigned char) ReadBlobByte(image);
1175 layer_info[i].name[j]='\0';
1176 if (image->debug != MagickFalse)
1177 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1178 " layer name: %s",layer_info[i].name);
1179 combinedlength+=length+1;
1181 #if 0 /* still in development */
1183 Adjustment layers and other stuff...
1186 char alsig[4], alkey[4];
1188 count=ReadBlob(image,4,alsig);
1189 if ((count == 0) || (LocaleNCompare(alsig,"8BIM",4) != 0)) {
1190 if (debug != MagickFalse)
1192 if (image->debug != MagickFalse)
1193 (void) LogMagickEvent(CoderEvent,GetMagickModule()," adjustment layer type was %.4s instead of 8BIM", alsig);
1195 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1197 count=ReadBlob(image,4,alkey);
1198 length=ReadBlobMSBLong(image);
1199 if (debug != MagickFalse)
1201 if (image->debug != MagickFalse)
1202 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1203 " adjustment layer key: %.4s, data length=%.20g",
1204 alkey, (double) length);
1208 for (j=0; j < (ssize_t) (length); j++)
1209 (void) ReadBlobByte(image);
1213 combinedlength += 12 + length; /* sig, key, length + the actual length*/
1217 Skip the rest of the variable data until we support it.
1219 if (image->debug != MagickFalse)
1220 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1221 " unsupported data: length=%.20g",(double)
1222 ((MagickOffsetType) (size-combinedlength)));
1223 if (DiscardBlobBytes(image,size-combinedlength) == MagickFalse)
1224 ThrowFileException(exception,CorruptImageError,
1225 "UnexpectedEndOfFile",image->filename);
1228 Allocate layered image.
1230 layer_info[i].image=CloneImage(image,layer_info[i].page.width,
1231 layer_info[i].page.height == ~0U ? 1 : layer_info[i].page.height,
1232 MagickFalse,exception);
1233 if (layer_info[i].image == (Image *) NULL)
1235 for (j=0; j < i; j++)
1236 layer_info[j].image=DestroyImage(layer_info[j].image);
1237 if (image->debug != MagickFalse)
1238 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1239 " allocation of image for layer %.20g failed",(double) i);
1240 ThrowReaderException(ResourceLimitError,
1241 "MemoryAllocationFailed");
1243 if (image->debug != MagickFalse)
1244 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1245 " setting up new layer image");
1246 if (image_info->ping != MagickFalse)
1247 (void) SetImageBackgroundColor(layer_info[i].image,exception);
1248 layer_info[i].image->compose=
1249 PSDBlendModeToCompositeOperator(layer_info[i].blendkey);
1250 if (layer_info[i].visible == MagickFalse)
1251 layer_info[i].image->compose=NoCompositeOp;
1252 if (psd_info.mode == CMYKMode)
1253 SetImageColorspace(layer_info[i].image,CMYKColorspace,exception);
1254 if ((psd_info.mode == BitmapMode) ||
1255 (psd_info.mode == GrayscaleMode) ||
1256 (psd_info.mode == DuotoneMode))
1257 SetImageColorspace(layer_info[i].image,GRAYColorspace,exception);
1258 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1259 if (layer_info[i].channel_info[j].type == -1)
1260 layer_info[i].image->alpha_trait=BlendPixelTrait;
1262 Set up some hidden attributes for folks that need them.
1264 (void) FormatLocaleString(message,MaxTextExtent,"%.20gld",
1265 (double) layer_info[i].page.x);
1266 (void) SetImageArtifact(layer_info[i].image,"psd:layer.x",message);
1267 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",
1268 (double) layer_info[i].page.y);
1269 (void) SetImageArtifact(layer_info[i].image,"psd:layer.y",message);
1270 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",
1271 (double) layer_info[i].opacity);
1272 (void) SetImageArtifact(layer_info[i].image,"psd:layer.opacity",
1274 (void) SetImageProperty(layer_info[i].image,"label",(char *)
1275 layer_info[i].name,exception);
1277 if (check_background == MagickFalse)
1279 if (image->debug != MagickFalse)
1280 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1281 " reading image data for layers");
1283 Read pixel data for each layer.
1285 for (i=0; i < number_layers; i++)
1287 if (image->debug != MagickFalse)
1288 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1289 " reading data for layer %.20g",(double) i);
1290 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1292 if (image->debug != MagickFalse)
1293 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1294 " reading data for channel %.20g",(double) j);
1296 if (layer_info[i].channel_info[j].size <= (2*layer_info[i].image->rows))
1301 if (image->debug != MagickFalse)
1302 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1303 " layer data is empty");
1305 A layer without data.
1307 for (k=0; k < (ssize_t) layer_info[i].channel_info[j].size; k++)
1308 (void) ReadBlobByte(layer_info[i].image);
1312 offsets=(MagickOffsetType *) NULL;
1313 layer_info[i].image->compression=NoCompression;
1314 compression=ReadBlobMSBShort(layer_info[i].image);
1315 if ((layer_info[i].page.height != 0) &&
1316 (layer_info[i].page.width != 0))
1318 if (compression == 1)
1321 Read RLE compressed data.
1323 layer_info[i].image->compression=RLECompression;
1324 if (image->debug != MagickFalse)
1325 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1326 " layer data is RLE compressed");
1327 offsets=(MagickOffsetType *) AcquireQuantumMemory(
1328 layer_info[i].image->rows,sizeof(*offsets));
1329 if (offsets == (MagickOffsetType *) NULL)
1330 ThrowReaderException(ResourceLimitError,
1331 "MemoryAllocationFailed");
1332 for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
1333 offsets[y]=GetPSDOffset(&psd_info,
1334 layer_info[i].image);
1336 status=ReadPSDLayer(layer_info[i].image,
1337 layer_info[i].channels,
1338 layer_info[i].channel_info[j].type,offsets,exception);
1339 if (compression == 1)
1340 offsets=(MagickOffsetType *) RelinquishMagickMemory(
1342 if (status == MagickFalse)
1346 if (layer_info[i].opacity != OpaqueAlpha)
1349 Correct for opacity level.
1351 for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
1353 q=GetAuthenticPixels(layer_info[i].image,0,y,
1354 layer_info[i].image->columns,1,exception);
1355 if (q == (Quantum *) NULL)
1357 for (x=0; x < (ssize_t) layer_info[i].image->columns; x++)
1359 SetPixelAlpha(layer_info[i].image,(Quantum)
1360 (QuantumScale*(GetPixelAlpha(layer_info[i].image,q))*
1361 layer_info[i].opacity),q);
1362 q+=GetPixelChannels(layer_info[i].image);
1364 if (SyncAuthenticPixels(layer_info[i].image,exception) == MagickFalse)
1368 if (layer_info[i].image->colorspace == CMYKColorspace)
1369 (void) NegateImage(layer_info[i].image,MagickFalse,exception);
1370 status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType)
1372 if (status == MagickFalse)
1375 /* added by palf -> invisible group layer make layer of this group
1376 invisible I consider that all layer with width and height null are
1377 layer for group layer */
1379 short inside_layer = 0;
1380 short layer_visible = 0;
1381 for (i=number_layers-1; i >=0; i--)
1383 if ((layer_info[i].page.width == 0) ||
1384 (layer_info[i].page.height == 0))
1386 if (inside_layer == 0)
1389 layer_visible=(short int) layer_info[i].visible;
1397 if ((inside_layer == 1) && (layer_visible == 0))
1399 layer_info[i].visible=(unsigned char) layer_visible;
1400 layer_info[i].image->compose=NoCompositeOp;
1404 /* added by palf -> suppression of empty layer */
1405 /* I consider that all layer with width and height null are layer for group layer */
1406 for (i=0; i < number_layers; i++)
1408 if ((layer_info[i].page.width == 0) ||
1409 (layer_info[i].page.height == 0))
1411 if (layer_info[i].image != (Image *) NULL)
1412 layer_info[i].image=DestroyImage(layer_info[i].image);
1413 for (j=i; j < number_layers - 1; j++)
1414 layer_info[j] = layer_info[j+1];
1419 mask_size = ReadBlobMSBLong(image); /* global mask size: currently ignored */
1421 if (number_layers > 0)
1423 if (image->debug != MagickFalse)
1424 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1425 " putting layers into image list");
1426 for (i=0; i < number_layers; i++)
1429 layer_info[i].image->previous=layer_info[i-1].image;
1430 if (i < (number_layers-1))
1431 layer_info[i].image->next=layer_info[i+1].image;
1432 layer_info[i].image->page=layer_info[i].page;
1434 image->next=layer_info[0].image;
1435 layer_info[0].image->previous=image;
1436 layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info);
1438 layer_offset-=TellBlob(image);
1439 offset=SeekBlob(image,layer_offset,SEEK_CUR);
1444 Read the precombined layer, present for PSD < 4 compatibility
1446 if (image->debug != MagickFalse)
1447 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1448 " reading the precombined layer");
1449 offsets=(MagickOffsetType *) NULL;
1450 image->compression=NoCompression;
1451 compression=ReadBlobMSBShort(image);
1452 if (compression == 1)
1455 Read Packbit encoded pixel data as separate planes.
1457 image->compression=RLECompression;
1458 offsets=(MagickOffsetType *) AcquireQuantumMemory(image->rows,
1459 psd_info.channels*sizeof(*offsets));
1460 if (offsets == (MagickOffsetType *) NULL)
1461 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1462 for (i=0; i < (ssize_t) (image->rows*psd_info.channels); i++)
1463 offsets[i]=GetPSDOffset(&psd_info,image);
1465 for (i=0; i < (ssize_t) psd_info.channels; i++)
1467 status=ReadPSDLayer(image,psd_info.channels,i,offsets+i*image->rows,
1469 if (status == MagickFalse)
1471 status=SetImageProgress(image,LoadImagesTag,i,psd_info.channels);
1472 if (status == MagickFalse)
1475 if (compression == 1)
1476 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1477 if (image->colorspace == CMYKColorspace)
1478 (void) NegateImage(image,MagickFalse,exception);
1479 (void) CloseBlob(image);
1480 return(GetFirstImageInList(image));
1484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1488 % R e g i s t e r P S D I m a g e %
1492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1494 % RegisterPSDImage() adds properties for the PSD image format to
1495 % the list of supported formats. The properties include the image format
1496 % tag, a method to read and/or write the format, whether the format
1497 % supports the saving of more than one frame to the same file or blob,
1498 % whether the format supports native in-memory I/O, and a brief
1499 % description of the format.
1501 % The format of the RegisterPSDImage method is:
1503 % size_t RegisterPSDImage(void)
1506 ModuleExport size_t RegisterPSDImage(void)
1511 entry=SetMagickInfo("PSB");
1512 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1513 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1514 entry->magick=(IsImageFormatHandler *) IsPSD;
1515 entry->seekable_stream=MagickTrue;
1516 entry->description=ConstantString("Adobe Large Document Format");
1517 entry->module=ConstantString("PSD");
1518 (void) RegisterMagickInfo(entry);
1519 entry=SetMagickInfo("PSD");
1520 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1521 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1522 entry->magick=(IsImageFormatHandler *) IsPSD;
1523 entry->seekable_stream=MagickTrue;
1524 entry->description=ConstantString("Adobe Photoshop bitmap");
1525 entry->module=ConstantString("PSD");
1526 (void) RegisterMagickInfo(entry);
1527 return(MagickImageCoderSignature);
1531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1535 % U n r e g i s t e r P S D I m a g e %
1539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1541 % UnregisterPSDImage() removes format registrations made by the
1542 % PSD module from the list of supported formats.
1544 % The format of the UnregisterPSDImage method is:
1546 % UnregisterPSDImage(void)
1549 ModuleExport void UnregisterPSDImage(void)
1551 (void) UnregisterMagickInfo("PSB");
1552 (void) UnregisterMagickInfo("PSD");
1556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1560 % W r i t e P S D I m a g e %
1564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1566 % WritePSDImage() writes an image in the Adobe Photoshop encoded image format.
1568 % The format of the WritePSDImage method is:
1570 % MagickBooleanType WritePSDImage(const ImageInfo *image_info,
1571 % Image *image,ExceptionInfo *exception)
1573 % A description of each parameter follows.
1575 % o image_info: the image info.
1577 % o image: The image.
1579 % o exception: return any errors or warnings in this structure.
1583 static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image,
1584 const size_t offset)
1586 if (psd_info->version == 1)
1587 return(WriteBlobMSBShort(image,(unsigned short) offset));
1588 return(WriteBlobMSBLong(image,(unsigned short) offset));
1591 static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image,
1592 const MagickSizeType size)
1594 if (psd_info->version == 1)
1595 return(WriteBlobMSBLong(image,(unsigned int) size));
1596 return(WriteBlobMSBLongLong(image,size));
1599 static size_t PSDPackbitsEncodeImage(Image *image,const size_t length,
1600 const unsigned char *pixels,unsigned char *compact_pixels,
1601 ExceptionInfo *exception)
1610 register unsigned char
1617 Compress pixels with Packbits encoding.
1619 assert(image != (Image *) NULL);
1620 assert(image->signature == MagickSignature);
1621 if (image->debug != MagickFalse)
1622 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1623 assert(pixels != (unsigned char *) NULL);
1624 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1625 if (packbits == (unsigned char *) NULL)
1626 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1629 for (i=(ssize_t) length; i != 0; )
1636 *q++=(unsigned char) 0;
1643 *q++=(unsigned char) 1;
1651 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1653 *q++=(unsigned char) ((256-3)+1);
1657 *q++=(unsigned char) 2;
1665 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1671 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
1678 *q++=(unsigned char) ((256-count)+1);
1687 while ((*(pixels+count) != *(pixels+count+1)) ||
1688 (*(pixels+count+1) != *(pixels+count+2)))
1690 packbits[count+1]=pixels[count];
1692 if (((ssize_t) count >= (i-3)) || (count >= 127))
1696 *packbits=(unsigned char) (count-1);
1697 for (j=0; j <= (ssize_t) count; j++)
1704 *q++=(unsigned char) 128; /* EOD marker */
1705 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
1706 return((size_t) (q-compact_pixels));
1709 static void WritePackbitsLength(const PSDInfo *psd_info,
1710 const ImageInfo *image_info,Image *image,Image *next_image,
1711 unsigned char *compact_pixels,const QuantumType quantum_type,
1712 ExceptionInfo *exception)
1717 register const Quantum
1730 if (next_image->depth > 8)
1731 next_image->depth=16;
1732 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1734 quantum_info=AcquireQuantumInfo(image_info,image);
1735 pixels=GetQuantumPixels(quantum_info);
1736 for (y=0; y < (ssize_t) next_image->rows; y++)
1738 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
1739 if (p == (const Quantum *) NULL)
1741 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
1742 quantum_type,pixels,exception);
1743 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
1745 (void) SetPSDOffset(psd_info,image,length);
1747 quantum_info=DestroyQuantumInfo(quantum_info);
1750 static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info,
1751 Image *image,Image *next_image,unsigned char *compact_pixels,
1752 const QuantumType quantum_type,const MagickBooleanType compression_flag,
1753 ExceptionInfo *exception)
1764 register const Quantum
1778 if ((compression_flag != MagickFalse) &&
1779 (next_image->compression != RLECompression))
1780 (void) WriteBlobMSBShort(image,0);
1781 if (next_image->depth > 8)
1782 next_image->depth=16;
1783 monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
1784 MagickTrue : MagickFalse;
1785 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1787 quantum_info=AcquireQuantumInfo(image_info,image);
1788 pixels=GetQuantumPixels(quantum_info);
1789 for (y=0; y < (ssize_t) next_image->rows; y++)
1791 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
1792 if (p == (const Quantum *) NULL)
1794 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
1795 quantum_type,pixels,exception);
1796 if (monochrome != MagickFalse)
1797 for (i=0; i < (ssize_t) length; i++)
1798 pixels[i]=(~pixels[i]);
1799 if (next_image->compression != RLECompression)
1800 (void) WriteBlob(image,length,pixels);
1803 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
1805 (void) WriteBlob(image,length,compact_pixels);
1808 quantum_info=DestroyQuantumInfo(quantum_info);
1811 static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info,
1812 const ImageInfo *image_info,Image *image,Image *next_image,
1813 const MagickBooleanType separate,ExceptionInfo *exception)
1826 Write uncompressed pixels as separate planes.
1829 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1830 compact_pixels=(unsigned char *) NULL;
1831 if (next_image->compression == RLECompression)
1833 compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels*
1834 next_image->columns,packet_size*sizeof(*compact_pixels));
1835 if (compact_pixels == (unsigned char *) NULL)
1836 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1839 if (IsImageGray(next_image,exception) != MagickFalse)
1841 if (next_image->compression == RLECompression)
1844 Packbits compression.
1846 (void) WriteBlobMSBShort(image,1);
1847 WritePackbitsLength(psd_info,image_info,image,next_image,
1848 compact_pixels,GrayQuantum,exception);
1849 if (next_image->alpha_trait == BlendPixelTrait)
1850 WritePackbitsLength(psd_info,image_info,image,next_image,
1851 compact_pixels,AlphaQuantum,exception);
1853 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1854 GrayQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1855 MagickFalse,exception);
1856 if (next_image->alpha_trait == BlendPixelTrait)
1857 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1858 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1859 MagickFalse,exception);
1860 (void) SetImageProgress(image,SaveImagesTag,0,1);
1863 if (next_image->storage_class == PseudoClass)
1865 if (next_image->compression == RLECompression)
1868 Packbits compression.
1870 (void) WriteBlobMSBShort(image,1);
1871 WritePackbitsLength(psd_info,image_info,image,next_image,
1872 compact_pixels,IndexQuantum,exception);
1873 if (next_image->alpha_trait == BlendPixelTrait)
1874 WritePackbitsLength(psd_info,image_info,image,next_image,
1875 compact_pixels,AlphaQuantum,exception);
1877 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1878 IndexQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1879 MagickFalse,exception);
1880 if (next_image->alpha_trait == BlendPixelTrait)
1881 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1882 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1883 MagickFalse,exception);
1884 (void) SetImageProgress(image,SaveImagesTag,0,1);
1888 if (next_image->colorspace == CMYKColorspace)
1889 (void) NegateImage(next_image,MagickFalse,exception);
1890 if (next_image->compression == RLECompression)
1893 Packbits compression.
1895 (void) WriteBlobMSBShort(image,1);
1896 WritePackbitsLength(psd_info,image_info,image,next_image,
1897 compact_pixels,RedQuantum,exception);
1898 WritePackbitsLength(psd_info,image_info,image,next_image,
1899 compact_pixels,GreenQuantum,exception);
1900 WritePackbitsLength(psd_info,image_info,image,next_image,
1901 compact_pixels,BlueQuantum,exception);
1902 if (next_image->colorspace == CMYKColorspace)
1903 WritePackbitsLength(psd_info,image_info,image,next_image,
1904 compact_pixels,BlackQuantum,exception);
1905 if (next_image->alpha_trait == BlendPixelTrait)
1906 WritePackbitsLength(psd_info,image_info,image,next_image,
1907 compact_pixels,AlphaQuantum,exception);
1909 (void) SetImageProgress(image,SaveImagesTag,0,6);
1910 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1911 RedQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1912 MagickFalse,exception);
1913 (void) SetImageProgress(image,SaveImagesTag,1,6);
1914 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1915 GreenQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1916 MagickFalse,exception);
1917 (void) SetImageProgress(image,SaveImagesTag,2,6);
1918 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1919 BlueQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1920 MagickFalse,exception);
1921 (void) SetImageProgress(image,SaveImagesTag,3,6);
1922 if (next_image->colorspace == CMYKColorspace)
1923 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1924 BlackQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1925 MagickFalse,exception);
1926 (void) SetImageProgress(image,SaveImagesTag,4,6);
1927 if (next_image->alpha_trait == BlendPixelTrait)
1928 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1929 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1930 MagickFalse,exception);
1931 (void) SetImageProgress(image,SaveImagesTag,5,6);
1932 if (next_image->colorspace == CMYKColorspace)
1933 (void) NegateImage(next_image,MagickFalse,exception);
1935 if (next_image->compression == RLECompression)
1936 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
1940 static void WritePascalString(Image* inImage,const char *inString,int inPad)
1951 length=(strlen(inString) > 255UL ) ? 255UL : strlen(inString);
1953 (void) WriteBlobByte(inImage,0);
1956 (void) WriteBlobByte(inImage,(unsigned char) length);
1957 (void) WriteBlob(inImage, length, (const unsigned char *) inString);
1960 if ((length % inPad) == 0)
1962 for (i=0; i < (ssize_t) (inPad-(length % inPad)); i++)
1963 (void) WriteBlobByte(inImage,0);
1966 static void WriteResolutionResourceBlock(Image *image)
1975 x_resolution=65536.0*image->resolution.x+0.5;
1976 y_resolution=65536.0*image->resolution.y+0.5;
1978 if (image->units == PixelsPerCentimeterResolution)
1980 x_resolution=2.54*65536.0*image->resolution.x*0.5;
1981 y_resolution=2.54*65536.0*image->resolution.y+0.5;
1984 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
1985 (void) WriteBlobMSBShort(image,0x03ED);
1986 (void) WriteBlobMSBShort(image,0);
1987 (void) WriteBlobMSBLong(image,16); /* resource size */
1988 (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5));
1989 (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */
1990 (void) WriteBlobMSBShort(image,units); /* width unit */
1991 (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5));
1992 (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */
1993 (void) WriteBlobMSBShort(image,units); /* height unit */
1996 static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile)
1998 register const unsigned char
2015 length=GetStringInfoLength(bim_profile);
2018 datum=GetStringInfoDatum(bim_profile);
2019 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2021 register unsigned char
2024 q=(unsigned char *) p;
2025 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2027 p=PushLongPixel(MSBEndian,p,&long_sans);
2028 p=PushShortPixel(MSBEndian,p,&id);
2029 p=PushShortPixel(MSBEndian,p,&short_sans);
2030 p=PushLongPixel(MSBEndian,p,&count);
2031 if (id == 0x0000040f)
2033 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2034 (PSDQuantum(count)+12)-(q-datum));
2035 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2039 if ((count & 0x01) != 0)
2044 static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile)
2046 register const unsigned char
2063 length=GetStringInfoLength(bim_profile);
2066 datum=GetStringInfoDatum(bim_profile);
2067 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2069 register unsigned char
2072 q=(unsigned char *) p;
2073 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2075 p=PushLongPixel(MSBEndian,p,&long_sans);
2076 p=PushShortPixel(MSBEndian,p,&id);
2077 p=PushShortPixel(MSBEndian,p,&short_sans);
2078 p=PushLongPixel(MSBEndian,p,&count);
2079 if ((id == 0x000003ed) && (PSDQuantum(count) < (ssize_t) (length-12)))
2081 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2082 (PSDQuantum(count)+12)-(q-datum));
2083 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2087 if ((count & 0x01) != 0)
2092 static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
2093 ExceptionInfo *exception)
2122 rounded_layer_info_size;
2133 assert(image_info != (const ImageInfo *) NULL);
2134 assert(image_info->signature == MagickSignature);
2135 assert(image != (Image *) NULL);
2136 assert(image->signature == MagickSignature);
2137 if (image->debug != MagickFalse)
2138 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2139 assert(exception != (ExceptionInfo *) NULL);
2140 assert(exception->signature == MagickSignature);
2141 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2142 if (status == MagickFalse)
2144 packet_size=(size_t) (image->depth > 8 ? 6 : 3);
2145 if (image->alpha_trait == BlendPixelTrait)
2146 packet_size+=image->depth > 8 ? 2 : 1;
2148 if ((LocaleCompare(image_info->magick,"PSB") == 0) ||
2149 (image->columns > 30000) || (image->rows > 30000))
2151 (void) WriteBlob(image,4,(const unsigned char *) "8BPS");
2152 (void) WriteBlobMSBShort(image,psd_info.version); /* version */
2153 for (i=1; i <= 6; i++)
2154 (void) WriteBlobByte(image, 0); /* 6 bytes of reserved */
2155 if (IsImageGray(image,exception) != MagickFalse)
2156 num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
2158 if (image->storage_class == PseudoClass)
2159 num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
2162 if (image->colorspace != CMYKColorspace)
2163 num_channels=(image->alpha_trait == BlendPixelTrait ? 4UL : 3UL);
2165 num_channels=(image->alpha_trait == BlendPixelTrait ? 5UL : 4UL);
2167 (void) WriteBlobMSBShort(image,(unsigned short) num_channels);
2168 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
2169 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
2170 if (IsImageGray(image,exception) != MagickFalse)
2178 monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
2179 MagickTrue : MagickFalse;
2180 (void) WriteBlobMSBShort(image,(unsigned short)
2181 (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8));
2182 (void) WriteBlobMSBShort(image,(unsigned short)
2183 (monochrome != MagickFalse ? BitmapMode : GrayscaleMode));
2187 (void) WriteBlobMSBShort(image,(unsigned short) (image->storage_class ==
2188 PseudoClass ? 8 : image->depth > 8 ? 16 : 8));
2189 if (((image_info->colorspace != UndefinedColorspace) ||
2190 (image->colorspace != CMYKColorspace)) &&
2191 (image_info->colorspace != CMYKColorspace))
2193 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2194 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2195 (void) WriteBlobMSBShort(image,(unsigned short)
2196 (image->storage_class == PseudoClass ? IndexedMode : RGBMode));
2200 if (image->colorspace != CMYKColorspace)
2201 (void) TransformImageColorspace(image,CMYKColorspace,exception);
2202 (void) WriteBlobMSBShort(image,CMYKMode);
2205 if ((IsImageGray(image,exception) != MagickFalse) ||
2206 (image->storage_class == DirectClass) || (image->colors > 256))
2207 (void) WriteBlobMSBLong(image,0);
2211 Write PSD raster colormap.
2213 (void) WriteBlobMSBLong(image,768);
2214 for (i=0; i < (ssize_t) image->colors; i++)
2215 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red));
2216 for ( ; i < 256; i++)
2217 (void) WriteBlobByte(image,0);
2218 for (i=0; i < (ssize_t) image->colors; i++)
2219 (void) WriteBlobByte(image,ScaleQuantumToChar(
2220 image->colormap[i].green));
2221 for ( ; i < 256; i++)
2222 (void) WriteBlobByte(image,0);
2223 for (i=0; i < (ssize_t) image->colors; i++)
2224 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue));
2225 for ( ; i < 256; i++)
2226 (void) WriteBlobByte(image,0);
2229 Image resource block.
2231 length=28; /* 0x03EB */
2232 bim_profile=(StringInfo *) GetImageProfile(image,"8bim");
2233 icc_profile=GetImageProfile(image,"icc");
2234 if (bim_profile != (StringInfo *) NULL)
2236 bim_profile=CloneStringInfo(bim_profile);
2237 if (icc_profile != (StringInfo *) NULL)
2238 RemoveICCProfileFromResourceBlock(bim_profile);
2239 RemoveResolutionFromResourceBlock(bim_profile);
2240 length+=PSDQuantum(GetStringInfoLength(bim_profile));
2242 if (icc_profile != (const StringInfo *) NULL)
2243 length+=PSDQuantum(GetStringInfoLength(icc_profile))+12;
2244 (void) WriteBlobMSBLong(image,(unsigned int) length);
2245 WriteResolutionResourceBlock(image);
2246 if (bim_profile != (StringInfo *) NULL)
2248 (void) WriteBlob(image,GetStringInfoLength(bim_profile),
2249 GetStringInfoDatum(bim_profile));
2250 bim_profile=DestroyStringInfo(bim_profile);
2252 if (icc_profile != (StringInfo *) NULL)
2254 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2255 (void) WriteBlobMSBShort(image,0x0000040F);
2256 (void) WriteBlobMSBShort(image,0);
2257 (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength(
2259 (void) WriteBlob(image,GetStringInfoLength(icc_profile),
2260 GetStringInfoDatum(icc_profile));
2261 if ((MagickOffsetType) GetStringInfoLength(icc_profile) !=
2262 PSDQuantum(GetStringInfoLength(icc_profile)))
2263 (void) WriteBlobByte(image,0);
2267 base_image=GetNextImageInList(image);
2268 if ((image->alpha_trait == BlendPixelTrait) && (base_image == (Image *) NULL))
2270 next_image=base_image;
2271 while ( next_image != NULL )
2273 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2274 if (IsImageGray(next_image,exception) != MagickFalse)
2275 num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
2277 if (next_image->storage_class == PseudoClass)
2278 num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
2280 if (next_image->colorspace != CMYKColorspace)
2281 num_channels=next_image->alpha_trait == BlendPixelTrait ? 4UL : 3UL;
2283 num_channels=next_image->alpha_trait == BlendPixelTrait ? 5UL : 4UL;
2284 channelLength=(size_t) (next_image->columns*next_image->rows*packet_size+2);
2285 layer_info_size+=(size_t) (4*4+2+num_channels*6+(psd_info.version == 1 ? 8 :
2286 16)+4*1+4+num_channels*channelLength);
2287 property=(const char *) GetImageProperty(next_image,"label",exception);
2288 if (property == (const char *) NULL)
2289 layer_info_size+=16;
2295 length=strlen(property);
2296 layer_info_size+=8+length+(4-(length % 4));
2299 next_image=GetNextImageInList(next_image);
2301 if (layer_count == 0)
2302 (void) SetPSDSize(&psd_info,image,0);
2308 (void) SetPSDSize(&psd_info,image,layer_info_size+
2309 (psd_info.version == 1 ? 8 : 16));
2310 if ((layer_info_size/2) != ((layer_info_size+1)/2))
2311 rounded_layer_info_size=layer_info_size+1;
2313 rounded_layer_info_size=layer_info_size;
2314 (void) SetPSDSize(&psd_info,image,rounded_layer_info_size);
2315 (void) WriteBlobMSBShort(image,(unsigned short) layer_count);
2317 compression=base_image->compression;
2318 next_image=base_image;
2319 while (next_image != NULL)
2321 next_image->compression=NoCompression;
2322 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y);
2323 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x);
2324 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y+
2326 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x+
2327 next_image->columns);
2328 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2329 channel_size=(unsigned int) ((packet_size*next_image->rows*
2330 next_image->columns)+2);
2331 if ((IsImageGray(next_image,exception) != MagickFalse) ||
2332 (next_image->storage_class == PseudoClass))
2334 (void) WriteBlobMSBShort(image,(unsigned short)
2335 (next_image->alpha_trait == BlendPixelTrait ? 2 : 1));
2336 (void) WriteBlobMSBShort(image,0);
2337 (void) SetPSDSize(&psd_info,image,channel_size);
2338 if (next_image->alpha_trait == BlendPixelTrait)
2340 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2341 (void) SetPSDSize(&psd_info,image,channel_size);
2345 if (next_image->colorspace != CMYKColorspace)
2347 (void) WriteBlobMSBShort(image,(unsigned short)
2348 (next_image->alpha_trait == BlendPixelTrait ? 4 : 3));
2349 (void) WriteBlobMSBShort(image,0);
2350 (void) SetPSDSize(&psd_info,image,channel_size);
2351 (void) WriteBlobMSBShort(image,1);
2352 (void) SetPSDSize(&psd_info,image,channel_size);
2353 (void) WriteBlobMSBShort(image,2);
2354 (void) SetPSDSize(&psd_info,image,channel_size);
2355 if (next_image->alpha_trait == BlendPixelTrait)
2357 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2358 (void) SetPSDSize(&psd_info,image,channel_size);
2363 (void) WriteBlobMSBShort(image,(unsigned short)
2364 (next_image->alpha_trait ? 5 : 4));
2365 (void) WriteBlobMSBShort(image,0);
2366 (void) SetPSDSize(&psd_info,image,channel_size);
2367 (void) WriteBlobMSBShort(image,1);
2368 (void) SetPSDSize(&psd_info,image,channel_size);
2369 (void) WriteBlobMSBShort(image,2);
2370 (void) SetPSDSize(&psd_info,image,channel_size);
2371 (void) WriteBlobMSBShort(image,3);
2372 (void) SetPSDSize(&psd_info,image,channel_size);
2373 if (next_image->alpha_trait)
2375 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2376 (void) SetPSDSize(&psd_info,image,channel_size);
2379 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2380 (void) WriteBlob(image,4,(const unsigned char *)
2381 CompositeOperatorToPSDBlendMode(next_image->compose));
2382 (void) WriteBlobByte(image,255); /* layer opacity */
2383 (void) WriteBlobByte(image,0);
2384 (void) WriteBlobByte(image,1); /* layer propertys - visible, etc. */
2385 (void) WriteBlobByte(image,0);
2386 property=(const char *) GetImageProperty(next_image,"label",exception);
2387 if (property == (const char *) NULL)
2389 (void) WriteBlobMSBLong(image,16);
2390 (void) WriteBlobMSBLong(image,0);
2391 (void) WriteBlobMSBLong(image,0);
2392 (void) FormatLocaleString((char *) layer_name,MaxTextExtent,
2393 "L%06ld",(long) layer_count++);
2394 WritePascalString( image, (char*)layer_name, 4 );
2401 length=strlen(property);
2402 (void) WriteBlobMSBLong(image,(unsigned int) (length+(4-
2404 (void) WriteBlobMSBLong(image,0);
2405 (void) WriteBlobMSBLong(image,0);
2406 WritePascalString(image,property,4);
2408 next_image=GetNextImageInList(next_image);
2413 next_image=base_image;
2414 while (next_image != NULL)
2416 status=WriteImageChannels(&psd_info,image_info,image,next_image,
2417 MagickTrue,exception);
2418 next_image=GetNextImageInList(next_image);
2420 (void) WriteBlobMSBLong(image,0); /* user mask data */
2421 base_image->compression=compression;
2424 Write composite image.
2426 status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse,
2428 (void) CloseBlob(image);