2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Adobe Photoshop Image Format %
21 % Copyright 1999-2012 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);
475 if ((count & 0x01) != 0)
481 static CompositeOperator PSDBlendModeToCompositeOperator(const char *mode)
483 if (mode == (const char *) NULL)
484 return(OverCompositeOp);
485 if (LocaleNCompare(mode,"norm",4) == 0)
486 return(OverCompositeOp);
487 if (LocaleNCompare(mode,"mul ",4) == 0)
488 return(MultiplyCompositeOp);
489 if (LocaleNCompare(mode,"diss",4) == 0)
490 return(DissolveCompositeOp);
491 if (LocaleNCompare(mode,"diff",4) == 0)
492 return(DifferenceCompositeOp);
493 if (LocaleNCompare(mode,"dark",4) == 0)
494 return(DarkenCompositeOp);
495 if (LocaleNCompare(mode,"lite",4) == 0)
496 return(LightenCompositeOp);
497 if (LocaleNCompare(mode,"hue ",4) == 0)
498 return(HueCompositeOp);
499 if (LocaleNCompare(mode,"sat ",4) == 0)
500 return(SaturateCompositeOp);
501 if (LocaleNCompare(mode,"colr",4) == 0)
502 return(ColorizeCompositeOp);
503 if (LocaleNCompare(mode,"lum ",4) == 0)
504 return(LuminizeCompositeOp);
505 if (LocaleNCompare(mode,"scrn",4) == 0)
506 return(ScreenCompositeOp);
507 if (LocaleNCompare(mode,"over",4) == 0)
508 return(OverlayCompositeOp);
509 if (LocaleNCompare(mode,"hLit",4) == 0)
510 return(OverCompositeOp);
511 if (LocaleNCompare(mode,"sLit",4) == 0)
512 return(OverCompositeOp);
513 if (LocaleNCompare(mode,"smud",4) == 0)
514 return(OverCompositeOp);
515 if (LocaleNCompare(mode,"div ",4) == 0)
516 return(OverCompositeOp);
517 if (LocaleNCompare(mode,"idiv",4) == 0)
518 return(OverCompositeOp);
519 return(OverCompositeOp);
522 static MagickBooleanType ReadPSDLayer(Image *image,const size_t channels,
523 const ssize_t type,const MagickOffsetType *offsets,ExceptionInfo *exception)
531 register const unsigned char
555 if (image->storage_class == PseudoClass)
557 if (image->colors > 256)
560 if (image->depth > 8)
564 if (image->depth > 8)
566 pixels=(unsigned char *) AcquireQuantumMemory(image->columns+256,packet_size*
568 if (pixels == (unsigned char *) NULL)
569 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
571 (void) ResetMagickMemory(pixels,0,image->columns*packet_size*sizeof(*pixels));
572 compact_pixels=(unsigned char *) NULL;
573 if (image->compression == RLECompression)
579 for (y=0; y < (ssize_t) image->rows; y++)
580 if ((MagickOffsetType) length < offsets[y])
581 length=(size_t) offsets[y];
582 compact_pixels=(unsigned char *) AcquireQuantumMemory(length,
584 if (compact_pixels == (unsigned char *) NULL)
585 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
587 (void) ResetMagickMemory(compact_pixels,0,length*sizeof(*compact_pixels));
589 colorspace=image->colorspace;
590 for (y=0; y < (ssize_t) image->rows; y++)
592 if (image->depth == 1)
594 if (image->compression != RLECompression)
595 count=ReadBlob(image,(image->columns+7)/8,pixels);
598 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
599 if (count != (ssize_t) offsets[y])
601 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
602 (ssize_t) 123456,(size_t) ((image->columns+7)/8),pixels);
604 if (count < (ssize_t) ((image->columns+7)/8))
609 if (image->compression != RLECompression)
610 count=ReadBlob(image,packet_size*image->columns,pixels);
613 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
614 if (count != (ssize_t) offsets[y])
616 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
617 (ssize_t) image->depth,packet_size*image->columns,pixels);
619 if (count < (ssize_t) (packet_size*image->columns))
622 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
623 if (q == (Quantum *) NULL)
626 for (x=0; x < (ssize_t) image->columns; x++)
628 if (packet_size == 1)
629 pixel=ScaleCharToQuantum(*p++);
632 p=PushShortPixel(MSBEndian,p,&nibble);
633 pixel=ScaleShortToQuantum(nibble);
639 SetPixelAlpha(image,pixel,q);
644 SetPixelRed(image,pixel,q);
646 SetPixelGray(image,pixel,q);
648 SetPixelRed(image,pixel,q);
649 if (image->storage_class == PseudoClass)
651 if (packet_size == 1)
652 SetPixelIndex(image,ScaleQuantumToChar(pixel),q);
654 SetPixelIndex(image,ScaleQuantumToShort(pixel),q);
655 SetPixelInfoPixel(image,image->colormap+(ssize_t)
656 GetPixelIndex(image,q),q);
657 if (image->depth == 1)
663 number_bits=image->columns-x;
666 for (bit=0; bit < number_bits; bit++)
668 SetPixelIndex(image,(((unsigned char) pixel) &
669 (0x01 << (7-bit))) != 0 ? 0 : 255,q);
670 SetPixelInfoPixel(image,image->colormap+(ssize_t)
671 GetPixelIndex(image,q),q);
672 q+=GetPixelChannels(image);
681 if (image->storage_class == PseudoClass)
682 SetPixelAlpha(image,pixel,q);
684 SetPixelGreen(image,pixel,q);
689 if (image->storage_class == PseudoClass)
690 SetPixelAlpha(image,pixel,q);
692 SetPixelBlue(image,pixel,q);
697 if (image->colorspace == CMYKColorspace)
698 SetPixelBlack(image,pixel,q);
700 if (image->alpha_trait == BlendPixelTrait)
701 SetPixelAlpha(image,pixel,q);
706 if ((IssRGBCompatibleColorspace(image->colorspace) != MagickFalse) &&
709 if (image->alpha_trait == BlendPixelTrait)
710 SetPixelAlpha(image,pixel,q);
716 q+=GetPixelChannels(image);
718 if (SyncAuthenticPixels(image,exception) == MagickFalse)
721 image->colorspace=colorspace;
722 if (image->compression == RLECompression)
723 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
724 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
728 static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception)
731 message[MaxTextExtent],
765 skip_first_alpha = 0;
782 assert(image_info != (const ImageInfo *) NULL);
783 assert(image_info->signature == MagickSignature);
784 if (image_info->debug != MagickFalse)
785 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
786 image_info->filename);
787 assert(exception != (ExceptionInfo *) NULL);
788 assert(exception->signature == MagickSignature);
789 image=AcquireImage(image_info,exception);
790 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
791 if (status == MagickFalse)
793 image=DestroyImageList(image);
794 return((Image *) NULL);
799 count=ReadBlob(image,4,(unsigned char *) psd_info.signature);
800 psd_info.version=ReadBlobMSBShort(image);
801 if ((count == 0) || (LocaleNCompare(psd_info.signature,"8BPS",4) != 0) ||
802 ((psd_info.version != 1) && (psd_info.version != 2)))
803 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
804 count=ReadBlob(image,6,psd_info.reserved);
805 psd_info.channels=ReadBlobMSBShort(image);
806 psd_info.color_channels=psd_info.channels;
807 if (psd_info.channels > MaxPSDChannels)
808 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
809 psd_info.rows=ReadBlobMSBLong(image);
810 psd_info.columns=ReadBlobMSBLong(image);
811 if ((psd_info.version == 1) && ((psd_info.rows > 30000) ||
812 (psd_info.columns > 30000)))
813 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
814 psd_info.depth=ReadBlobMSBShort(image);
815 if ((psd_info.depth != 1) && (psd_info.depth != 8) && (psd_info.depth != 16))
816 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
817 psd_info.mode=ReadBlobMSBShort(image);
818 if (image->debug != MagickFalse)
819 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
820 " Image is %.20g x %.20g with channels=%.20g, depth=%.20g, mode=%s",
821 (double) psd_info.columns,(double) psd_info.rows,(double)
822 psd_info.channels,(double) psd_info.depth,ModeToString((PSDImageType)
827 image->depth=psd_info.depth;
828 image->columns=psd_info.columns;
829 image->rows=psd_info.rows;
830 if (SetImageBackgroundColor(image,exception) == MagickFalse)
832 image=DestroyImageList(image);
833 return((Image *) NULL);
835 image->alpha_trait=psd_info.channels >= 4 ? BlendPixelTrait :
837 if (psd_info.mode == LabMode)
838 SetImageColorspace(image,LabColorspace,exception);
839 psd_info.color_channels=3;
840 if (psd_info.mode == CMYKMode)
842 psd_info.color_channels=4;
843 SetImageColorspace(image,CMYKColorspace,exception);
844 image->alpha_trait=psd_info.channels >= 5 ? BlendPixelTrait :
847 if ((psd_info.mode == BitmapMode) || (psd_info.mode == GrayscaleMode) ||
848 (psd_info.mode == DuotoneMode))
850 psd_info.color_channels=1;
851 if (AcquireImageColormap(image,256,exception) == MagickFalse)
852 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
853 image->alpha_trait=psd_info.channels >= 2 ? BlendPixelTrait :
855 if (image->debug != MagickFalse)
856 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
857 " Image colormap allocated");
858 SetImageColorspace(image,GRAYColorspace,exception);
860 image->alpha_trait=UndefinedPixelTrait;
862 Read PSD raster colormap only present for indexed and duotone images.
864 length=ReadBlobMSBLong(image);
867 if (image->debug != MagickFalse)
868 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
869 " reading colormap");
870 if (psd_info.mode == DuotoneMode)
873 Duotone image data; the format of this data is undocumented.
875 data=(unsigned char *) AcquireQuantumMemory((size_t) length,
877 if (data == (unsigned char *) NULL)
878 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
879 count=ReadBlob(image,(size_t) length,data);
880 data=(unsigned char *) RelinquishMagickMemory(data);
885 Read PSD raster colormap.
887 if (AcquireImageColormap(image,(size_t) (length/3),exception) == MagickFalse)
888 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
889 for (i=0; i < (ssize_t) image->colors; i++)
890 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
891 ReadBlobByte(image));
892 for (i=0; i < (ssize_t) image->colors; i++)
893 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
894 ReadBlobByte(image));
895 for (i=0; i < (ssize_t) image->colors; i++)
896 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
897 ReadBlobByte(image));
898 image->alpha_trait=UndefinedPixelTrait;
901 length=ReadBlobMSBLong(image);
908 Image resources block.
910 if (image->debug != MagickFalse)
911 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
912 " reading image resource blocks - %.20g bytes",(double)
913 ((MagickOffsetType) length));
914 blocks=(unsigned char *) AcquireQuantumMemory((size_t) length,
916 if (blocks == (unsigned char *) NULL)
917 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
918 count=ReadBlob(image,(size_t) length,blocks);
919 if ((count != (ssize_t) length) ||
920 (LocaleNCompare((char *) blocks,"8BIM",4) != 0))
922 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
923 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
925 (void) ParseImageResourceBlocks(image,blocks,(size_t) length,
927 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
930 Layer and mask block.
932 layer_info=(LayerInfo *) NULL;
934 length=GetPSDSize(&psd_info,image);
937 length=ReadBlobMSBLong(image);
938 length=ReadBlobMSBLong(image);
940 check_background=MagickFalse;
941 if ((image_info->number_scenes == 1) && (image_info->scene == 0))
943 if (image->debug != MagickFalse)
944 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
945 " read composite only");
946 check_background=MagickTrue;
950 if (image->debug != MagickFalse)
951 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
952 " image has no layers");
956 offset=TellBlob(image);
957 size=GetPSDSize(&psd_info,image);
969 quantum=psd_info.version == 1 ? 4UL : 8UL;
970 tag=ReadBlobMSBLong(image);
972 count=ReadBlob(image,4,(unsigned char *) type);
973 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
975 if (DiscardBlobBytes(image,length-quantum-8) == MagickFalse)
976 ThrowFileException(exception,CorruptImageError,
977 "UnexpectedEndOfFile",image->filename);
981 count=ReadBlob(image,4,(unsigned char *) type);
982 if ((count != 0) && (LocaleNCompare(type,"Lr16",4) == 0))
983 size=GetPSDSize(&psd_info,image);
985 if (DiscardBlobBytes(image,length-quantum-12) == MagickFalse)
986 ThrowFileException(exception,CorruptImageError,
987 "UnexpectedEndOfFile",image->filename);
995 image->alpha_trait=psd_info.channels > psd_info.color_channels ?
996 BlendPixelTrait : UndefinedPixelTrait;
998 if (image->debug != MagickFalse)
999 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1000 image->alpha_trait ? " image has matte" : " image has no matte");
1002 layer_offset=offset+length;
1003 number_layers=(short) ReadBlobMSBShort(image);
1004 if (image->debug != MagickFalse)
1005 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1006 " image contains %.20g layers",(double) number_layers);
1007 if (number_layers < 0)
1010 Weird hack in PSD format to ignore first alpha channel.
1013 (void) skip_first_alpha;
1014 number_layers=MagickAbsoluteValue(number_layers);
1015 if (image->debug != MagickFalse)
1016 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1017 " negative layer count corrected for");
1019 layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers,
1020 sizeof(*layer_info));
1021 if (layer_info == (LayerInfo *) NULL)
1023 if (image->debug != MagickFalse)
1024 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1025 " allocation of LayerInfo failed");
1026 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1028 (void) ResetMagickMemory(layer_info,0,(size_t) number_layers*
1029 sizeof(*layer_info));
1030 for (i=0; i < number_layers; i++)
1036 if (image->debug != MagickFalse)
1037 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1038 " reading layer #%.20g",(double) i+1);
1039 layer_info[i].page.y=(int) ReadBlobMSBLong(image);
1040 layer_info[i].page.x=(int) ReadBlobMSBLong(image);
1041 y=(int) ReadBlobMSBLong(image);
1042 x=(int) ReadBlobMSBLong(image);
1043 layer_info[i].page.width=(ssize_t) (x-layer_info[i].page.x);
1044 layer_info[i].page.height=(ssize_t) (y-layer_info[i].page.y);
1045 layer_info[i].channels=ReadBlobMSBShort(image);
1046 if (check_background == MagickTrue)
1051 if (layer_info[i].channels == psd_info.color_channels)
1052 image->alpha_trait=UndefinedPixelTrait;
1053 quantum=psd_info.version == 1 ? 4UL : 8UL;
1054 if (DiscardBlobBytes(image,length-20-quantum) == MagickFalse)
1055 ThrowFileException(exception,CorruptImageError,
1056 "UnexpectedEndOfFile",image->filename);
1059 if (layer_info[i].channels > MaxPSDChannels)
1060 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
1061 if (image->debug != MagickFalse)
1062 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1063 " offset(%.20g,%.20g), size(%.20g,%.20g), channels=%.20g",
1064 (double) layer_info[i].page.x,(double) layer_info[i].page.y,
1065 (double) layer_info[i].page.height,(double)
1066 layer_info[i].page.width,(double) layer_info[i].channels);
1067 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1069 layer_info[i].channel_info[j].type=(short)
1070 ReadBlobMSBShort(image);
1071 layer_info[i].channel_info[j].size=(size_t)
1072 GetPSDSize(&psd_info,image);
1073 if (image->debug != MagickFalse)
1074 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1075 " channel[%.20g]: type=%.20g, size=%.20g",(double) j,
1076 (double) layer_info[i].channel_info[j].type,
1077 (double) layer_info[i].channel_info[j].size);
1079 count=ReadBlob(image,4,(unsigned char *) type);
1080 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1082 if (image->debug != MagickFalse)
1083 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1084 " layer type was %.4s instead of 8BIM", type);
1085 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1087 count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey);
1088 layer_info[i].opacity=(Quantum) ScaleCharToQuantum((unsigned char)
1089 ReadBlobByte(image));
1090 layer_info[i].clipping=(unsigned char) ReadBlobByte(image);
1091 layer_info[i].flags=(unsigned char) ReadBlobByte(image);
1092 layer_info[i].visible=!(layer_info[i].flags & 0x02);
1093 if (image->debug != MagickFalse)
1094 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1095 " blend=%.4s, opacity=%.20g, clipping=%s, flags=%d, visible=%s",
1096 layer_info[i].blendkey,(double) layer_info[i].opacity,
1097 layer_info[i].clipping ? "true" : "false",layer_info[i].flags,
1098 layer_info[i].visible ? "true" : "false");
1099 (void) ReadBlobByte(image); /* filler */
1101 size=ReadBlobMSBLong(image);
1104 if (image->debug != MagickFalse)
1105 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1106 " layer contains additional info");
1107 length=ReadBlobMSBLong(image);
1113 layer_info[i].mask.y=(int) ReadBlobMSBLong(image);
1114 layer_info[i].mask.x=(int) ReadBlobMSBLong(image);
1115 layer_info[i].mask.height=(size_t)
1116 (ReadBlobMSBLong(image)-layer_info[i].mask.y);
1117 layer_info[i].mask.width=(size_t)
1118 (ReadBlobMSBLong(image)-layer_info[i].mask.x);
1119 if (image->debug != MagickFalse)
1120 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1121 " layer mask: offset(%.20g,%.20g), size(%.20g,%.20g), length=%.20g",
1122 (double) layer_info[i].mask.x,(double) layer_info[i].mask.y,
1123 (double) layer_info[i].mask.width,(double)
1124 layer_info[i].mask.height,(double)
1125 ((MagickOffsetType) length-16));
1127 Skip over the rest of the layer mask information.
1129 if (DiscardBlobBytes(image,length-16) == MagickFalse)
1130 ThrowFileException(exception,CorruptImageError,
1131 "UnexpectedEndOfFile",image->filename);
1133 combinedlength+=length+4; /* +4 for length */
1134 length=ReadBlobMSBLong(image);
1138 Layer blending ranges info.
1140 if (image->debug != MagickFalse)
1141 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1142 " layer blending ranges: length=%.20g",(double)
1143 ((MagickOffsetType) length));
1145 We read it, but don't use it...
1147 for (j=0; j < (ssize_t) (length); j+=8)
1149 size_t blend_source=ReadBlobMSBLong(image);
1150 size_t blend_dest=ReadBlobMSBLong(image);
1151 if (image->debug != MagickFalse)
1152 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1153 " source(%x), dest(%x)",(unsigned int)
1154 blend_source,(unsigned int) blend_dest);
1157 combinedlength+=length+4;
1161 length=(size_t) ReadBlobByte(image);
1162 for (j=0; j < (ssize_t) length; j++)
1163 layer_info[i].name[j]=(unsigned char) ReadBlobByte(image);
1164 layer_info[i].name[j]='\0';
1165 if (image->debug != MagickFalse)
1166 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1167 " layer name: %s",layer_info[i].name);
1168 combinedlength+=length+1;
1170 #if 0 /* still in development */
1172 Adjustment layers and other stuff...
1175 char alsig[4], alkey[4];
1177 count=ReadBlob(image,4,alsig);
1178 if ((count == 0) || (LocaleNCompare(alsig,"8BIM",4) != 0)) {
1179 if (debug != MagickFalse)
1181 if (image->debug != MagickFalse)
1182 (void) LogMagickEvent(CoderEvent,GetMagickModule()," adjustment layer type was %.4s instead of 8BIM", alsig);
1184 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1186 count=ReadBlob(image,4,alkey);
1187 length=ReadBlobMSBLong(image);
1188 if (debug != MagickFalse)
1190 if (image->debug != MagickFalse)
1191 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1192 " adjustment layer key: %.4s, data length=%.20g",
1193 alkey, (double) length);
1197 for (j=0; j < (ssize_t) (length); j++)
1198 (void) ReadBlobByte(image);
1202 combinedlength += 12 + length; /* sig, key, length + the actual length*/
1206 Skip the rest of the variable data until we support it.
1208 if (image->debug != MagickFalse)
1209 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1210 " unsupported data: length=%.20g",(double)
1211 ((MagickOffsetType) (size-combinedlength)));
1212 if (DiscardBlobBytes(image,size-combinedlength) == MagickFalse)
1213 ThrowFileException(exception,CorruptImageError,
1214 "UnexpectedEndOfFile",image->filename);
1217 Allocate layered image.
1219 layer_info[i].image=CloneImage(image,layer_info[i].page.width,
1220 layer_info[i].page.height == ~0U ? 1 : layer_info[i].page.height,
1221 MagickFalse,exception);
1222 if (layer_info[i].image == (Image *) NULL)
1224 for (j=0; j < i; j++)
1225 layer_info[j].image=DestroyImage(layer_info[j].image);
1226 if (image->debug != MagickFalse)
1227 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1228 " allocation of image for layer %.20g failed",(double) i);
1229 ThrowReaderException(ResourceLimitError,
1230 "MemoryAllocationFailed");
1232 if (image->debug != MagickFalse)
1233 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1234 " setting up new layer image");
1235 if (image_info->ping != MagickFalse)
1236 (void) SetImageBackgroundColor(layer_info[i].image,exception);
1237 layer_info[i].image->compose=
1238 PSDBlendModeToCompositeOperator(layer_info[i].blendkey);
1239 if (layer_info[i].visible == MagickFalse)
1240 layer_info[i].image->compose=NoCompositeOp;
1241 if (psd_info.mode == CMYKMode)
1242 SetImageColorspace(layer_info[i].image,CMYKColorspace,exception);
1243 if ((psd_info.mode == BitmapMode) ||
1244 (psd_info.mode == GrayscaleMode) ||
1245 (psd_info.mode == DuotoneMode))
1246 SetImageColorspace(layer_info[i].image,GRAYColorspace,exception);
1247 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1248 if (layer_info[i].channel_info[j].type == -1)
1249 layer_info[i].image->alpha_trait=BlendPixelTrait;
1251 Set up some hidden attributes for folks that need them.
1253 (void) FormatLocaleString(message,MaxTextExtent,"%.20gld",
1254 (double) layer_info[i].page.x);
1255 (void) SetImageArtifact(layer_info[i].image,"psd:layer.x",message);
1256 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",
1257 (double) layer_info[i].page.y);
1258 (void) SetImageArtifact(layer_info[i].image,"psd:layer.y",message);
1259 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",
1260 (double) layer_info[i].opacity);
1261 (void) SetImageArtifact(layer_info[i].image,"psd:layer.opacity",
1263 (void) SetImageProperty(layer_info[i].image,"label",(char *)
1264 layer_info[i].name,exception);
1266 if (check_background == MagickFalse)
1268 if (image->debug != MagickFalse)
1269 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1270 " reading image data for layers");
1272 Read pixel data for each layer.
1274 for (i=0; i < number_layers; i++)
1276 if (image->debug != MagickFalse)
1277 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1278 " reading data for layer %.20g",(double) i);
1279 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1281 if (image->debug != MagickFalse)
1282 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1283 " reading data for channel %.20g",(double) j);
1285 if (layer_info[i].channel_info[j].size <= (2*layer_info[i].image->rows))
1290 if (image->debug != MagickFalse)
1291 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1292 " layer data is empty");
1294 A layer without data.
1296 for (k=0; k < (ssize_t) layer_info[i].channel_info[j].size; k++)
1297 (void) ReadBlobByte(layer_info[i].image);
1301 offsets=(MagickOffsetType *) NULL;
1302 layer_info[i].image->compression=NoCompression;
1303 compression=ReadBlobMSBShort(layer_info[i].image);
1304 if ((layer_info[i].page.height != 0) &&
1305 (layer_info[i].page.width != 0))
1307 if (compression == 1)
1310 Read RLE compressed data.
1312 layer_info[i].image->compression=RLECompression;
1313 if (image->debug != MagickFalse)
1314 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1315 " layer data is RLE compressed");
1316 offsets=(MagickOffsetType *) AcquireQuantumMemory(
1317 layer_info[i].image->rows,sizeof(*offsets));
1318 if (offsets == (MagickOffsetType *) NULL)
1319 ThrowReaderException(ResourceLimitError,
1320 "MemoryAllocationFailed");
1321 for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
1322 offsets[y]=GetPSDOffset(&psd_info,
1323 layer_info[i].image);
1325 status=ReadPSDLayer(layer_info[i].image,
1326 layer_info[i].channels,
1327 layer_info[i].channel_info[j].type,offsets,exception);
1328 if (compression == 1)
1329 offsets=(MagickOffsetType *) RelinquishMagickMemory(
1331 if (status == MagickFalse)
1335 if (layer_info[i].opacity != OpaqueAlpha)
1338 Correct for opacity level.
1340 for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
1342 q=GetAuthenticPixels(layer_info[i].image,0,y,
1343 layer_info[i].image->columns,1,exception);
1344 if (q == (Quantum *) NULL)
1346 for (x=0; x < (ssize_t) layer_info[i].image->columns; x++)
1348 SetPixelAlpha(layer_info[i].image,(Quantum)
1349 (QuantumScale*(GetPixelAlpha(layer_info[i].image,q))*
1350 layer_info[i].opacity),q);
1351 q+=GetPixelChannels(layer_info[i].image);
1353 if (SyncAuthenticPixels(layer_info[i].image,exception) == MagickFalse)
1357 if (layer_info[i].image->colorspace == CMYKColorspace)
1358 (void) NegateImage(layer_info[i].image,MagickFalse,exception);
1359 status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType)
1361 if (status == MagickFalse)
1364 /* added by palf -> invisible group layer make layer of this group
1365 invisible I consider that all layer with width and height null are
1366 layer for group layer */
1368 short inside_layer = 0;
1369 short layer_visible = 0;
1370 for (i=number_layers-1; i >=0; i--)
1372 if ((layer_info[i].page.width == 0) ||
1373 (layer_info[i].page.height == 0))
1375 if (inside_layer == 0)
1378 layer_visible=(short int) layer_info[i].visible;
1386 if ((inside_layer == 1) && (layer_visible == 0))
1388 layer_info[i].visible=(unsigned char) layer_visible;
1389 layer_info[i].image->compose=NoCompositeOp;
1393 /* added by palf -> suppression of empty layer */
1394 /* I consider that all layer with width and height null are layer for group layer */
1395 for (i=0; i < number_layers; i++)
1397 if ((layer_info[i].page.width == 0) ||
1398 (layer_info[i].page.height == 0))
1400 if (layer_info[i].image != (Image *) NULL)
1401 layer_info[i].image=DestroyImage(layer_info[i].image);
1402 for (j=i; j < number_layers - 1; j++)
1403 layer_info[j] = layer_info[j+1];
1408 mask_size = ReadBlobMSBLong(image); /* global mask size: currently ignored */
1410 if (number_layers > 0)
1412 if (image->debug != MagickFalse)
1413 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1414 " putting layers into image list");
1415 for (i=0; i < number_layers; i++)
1418 layer_info[i].image->previous=layer_info[i-1].image;
1419 if (i < (number_layers-1))
1420 layer_info[i].image->next=layer_info[i+1].image;
1421 layer_info[i].image->page=layer_info[i].page;
1423 image->next=layer_info[0].image;
1424 layer_info[0].image->previous=image;
1425 layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info);
1427 layer_offset-=TellBlob(image);
1428 offset=SeekBlob(image,layer_offset,SEEK_CUR);
1433 Read the precombined layer, present for PSD < 4 compatibility
1435 if (image->debug != MagickFalse)
1436 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1437 " reading the precombined layer");
1438 offsets=(MagickOffsetType *) NULL;
1439 image->compression=NoCompression;
1440 compression=ReadBlobMSBShort(image);
1441 if (compression == 1)
1444 Read Packbit encoded pixel data as separate planes.
1446 image->compression=RLECompression;
1447 offsets=(MagickOffsetType *) AcquireQuantumMemory(image->rows,
1448 psd_info.channels*sizeof(*offsets));
1449 if (offsets == (MagickOffsetType *) NULL)
1450 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1451 for (i=0; i < (ssize_t) (image->rows*psd_info.channels); i++)
1452 offsets[i]=GetPSDOffset(&psd_info,image);
1454 for (i=0; i < (ssize_t) psd_info.channels; i++)
1456 status=ReadPSDLayer(image,psd_info.channels,i,offsets+i*image->rows,
1458 if (status == MagickFalse)
1460 status=SetImageProgress(image,LoadImagesTag,i,psd_info.channels);
1461 if (status == MagickFalse)
1464 if (compression == 1)
1465 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1466 if (image->colorspace == CMYKColorspace)
1467 (void) NegateImage(image,MagickFalse,exception);
1468 (void) CloseBlob(image);
1469 return(GetFirstImageInList(image));
1473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1477 % R e g i s t e r P S D I m a g e %
1481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1483 % RegisterPSDImage() adds properties for the PSD image format to
1484 % the list of supported formats. The properties include the image format
1485 % tag, a method to read and/or write the format, whether the format
1486 % supports the saving of more than one frame to the same file or blob,
1487 % whether the format supports native in-memory I/O, and a brief
1488 % description of the format.
1490 % The format of the RegisterPSDImage method is:
1492 % size_t RegisterPSDImage(void)
1495 ModuleExport size_t RegisterPSDImage(void)
1500 entry=SetMagickInfo("PSB");
1501 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1502 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1503 entry->magick=(IsImageFormatHandler *) IsPSD;
1504 entry->seekable_stream=MagickTrue;
1505 entry->description=ConstantString("Adobe Large Document Format");
1506 entry->module=ConstantString("PSD");
1507 (void) RegisterMagickInfo(entry);
1508 entry=SetMagickInfo("PSD");
1509 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1510 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1511 entry->magick=(IsImageFormatHandler *) IsPSD;
1512 entry->seekable_stream=MagickTrue;
1513 entry->description=ConstantString("Adobe Photoshop bitmap");
1514 entry->module=ConstantString("PSD");
1515 (void) RegisterMagickInfo(entry);
1516 return(MagickImageCoderSignature);
1520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1524 % U n r e g i s t e r P S D I m a g e %
1528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1530 % UnregisterPSDImage() removes format registrations made by the
1531 % PSD module from the list of supported formats.
1533 % The format of the UnregisterPSDImage method is:
1535 % UnregisterPSDImage(void)
1538 ModuleExport void UnregisterPSDImage(void)
1540 (void) UnregisterMagickInfo("PSB");
1541 (void) UnregisterMagickInfo("PSD");
1545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1549 % W r i t e P S D I m a g e %
1553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1555 % WritePSDImage() writes an image in the Adobe Photoshop encoded image format.
1557 % The format of the WritePSDImage method is:
1559 % MagickBooleanType WritePSDImage(const ImageInfo *image_info,
1560 % Image *image,ExceptionInfo *exception)
1562 % A description of each parameter follows.
1564 % o image_info: the image info.
1566 % o image: The image.
1568 % o exception: return any errors or warnings in this structure.
1572 static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image,
1573 const size_t offset)
1575 if (psd_info->version == 1)
1576 return(WriteBlobMSBShort(image,(unsigned short) offset));
1577 return(WriteBlobMSBLong(image,(unsigned short) offset));
1580 static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image,
1581 const MagickSizeType size)
1583 if (psd_info->version == 1)
1584 return(WriteBlobMSBLong(image,(unsigned int) size));
1585 return(WriteBlobMSBLongLong(image,size));
1588 static size_t PSDPackbitsEncodeImage(Image *image,const size_t length,
1589 const unsigned char *pixels,unsigned char *compact_pixels,
1590 ExceptionInfo *exception)
1599 register unsigned char
1606 Compress pixels with Packbits encoding.
1608 assert(image != (Image *) NULL);
1609 assert(image->signature == MagickSignature);
1610 if (image->debug != MagickFalse)
1611 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1612 assert(pixels != (unsigned char *) NULL);
1613 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1614 if (packbits == (unsigned char *) NULL)
1615 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1618 for (i=(ssize_t) length; i != 0; )
1625 *q++=(unsigned char) 0;
1632 *q++=(unsigned char) 1;
1640 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1642 *q++=(unsigned char) ((256-3)+1);
1646 *q++=(unsigned char) 2;
1654 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1660 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
1667 *q++=(unsigned char) ((256-count)+1);
1676 while ((*(pixels+count) != *(pixels+count+1)) ||
1677 (*(pixels+count+1) != *(pixels+count+2)))
1679 packbits[count+1]=pixels[count];
1681 if (((ssize_t) count >= (i-3)) || (count >= 127))
1685 *packbits=(unsigned char) (count-1);
1686 for (j=0; j <= (ssize_t) count; j++)
1693 *q++=(unsigned char) 128; /* EOD marker */
1694 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
1695 return((size_t) (q-compact_pixels));
1698 static void WritePackbitsLength(const PSDInfo *psd_info,
1699 const ImageInfo *image_info,Image *image,Image *next_image,
1700 unsigned char *compact_pixels,const QuantumType quantum_type,
1701 ExceptionInfo *exception)
1706 register const Quantum
1719 if (next_image->depth > 8)
1720 next_image->depth=16;
1721 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1723 quantum_info=AcquireQuantumInfo(image_info,image);
1724 pixels=GetQuantumPixels(quantum_info);
1725 for (y=0; y < (ssize_t) next_image->rows; y++)
1727 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
1728 if (p == (const Quantum *) NULL)
1730 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
1731 quantum_type,pixels,exception);
1732 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
1734 (void) SetPSDOffset(psd_info,image,length);
1736 quantum_info=DestroyQuantumInfo(quantum_info);
1739 static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info,
1740 Image *image,Image *next_image,unsigned char *compact_pixels,
1741 const QuantumType quantum_type,const MagickBooleanType compression_flag,
1742 ExceptionInfo *exception)
1753 register const Quantum
1767 if ((compression_flag != MagickFalse) &&
1768 (next_image->compression != RLECompression))
1769 (void) WriteBlobMSBShort(image,0);
1770 if (next_image->depth > 8)
1771 next_image->depth=16;
1772 monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
1773 MagickTrue : MagickFalse;
1774 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1776 quantum_info=AcquireQuantumInfo(image_info,image);
1777 pixels=GetQuantumPixels(quantum_info);
1778 for (y=0; y < (ssize_t) next_image->rows; y++)
1780 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
1781 if (p == (const Quantum *) NULL)
1783 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
1784 quantum_type,pixels,exception);
1785 if (monochrome != MagickFalse)
1786 for (i=0; i < (ssize_t) length; i++)
1787 pixels[i]=(~pixels[i]);
1788 if (next_image->compression != RLECompression)
1789 (void) WriteBlob(image,length,pixels);
1792 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
1794 (void) WriteBlob(image,length,compact_pixels);
1797 quantum_info=DestroyQuantumInfo(quantum_info);
1800 static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info,
1801 const ImageInfo *image_info,Image *image,Image *next_image,
1802 const MagickBooleanType separate,ExceptionInfo *exception)
1815 Write uncompressed pixels as separate planes.
1818 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1819 compact_pixels=(unsigned char *) NULL;
1820 if (next_image->compression == RLECompression)
1822 compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels*
1823 next_image->columns,packet_size*sizeof(*compact_pixels));
1824 if (compact_pixels == (unsigned char *) NULL)
1825 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1828 if (IsImageGray(next_image,exception) != MagickFalse)
1830 if (next_image->compression == RLECompression)
1833 Packbits compression.
1835 (void) WriteBlobMSBShort(image,1);
1836 WritePackbitsLength(psd_info,image_info,image,next_image,
1837 compact_pixels,GrayQuantum,exception);
1838 if (next_image->alpha_trait == BlendPixelTrait)
1839 WritePackbitsLength(psd_info,image_info,image,next_image,
1840 compact_pixels,AlphaQuantum,exception);
1842 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1843 GrayQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1844 MagickFalse,exception);
1845 if (next_image->alpha_trait == BlendPixelTrait)
1846 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1847 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1848 MagickFalse,exception);
1849 (void) SetImageProgress(image,SaveImagesTag,0,1);
1852 if (next_image->storage_class == PseudoClass)
1854 if (next_image->compression == RLECompression)
1857 Packbits compression.
1859 (void) WriteBlobMSBShort(image,1);
1860 WritePackbitsLength(psd_info,image_info,image,next_image,
1861 compact_pixels,IndexQuantum,exception);
1862 if (next_image->alpha_trait == BlendPixelTrait)
1863 WritePackbitsLength(psd_info,image_info,image,next_image,
1864 compact_pixels,AlphaQuantum,exception);
1866 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1867 IndexQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1868 MagickFalse,exception);
1869 if (next_image->alpha_trait == BlendPixelTrait)
1870 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1871 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1872 MagickFalse,exception);
1873 (void) SetImageProgress(image,SaveImagesTag,0,1);
1877 if (next_image->colorspace == CMYKColorspace)
1878 (void) NegateImage(next_image,MagickFalse,exception);
1879 if (next_image->compression == RLECompression)
1882 Packbits compression.
1884 (void) WriteBlobMSBShort(image,1);
1885 WritePackbitsLength(psd_info,image_info,image,next_image,
1886 compact_pixels,RedQuantum,exception);
1887 WritePackbitsLength(psd_info,image_info,image,next_image,
1888 compact_pixels,GreenQuantum,exception);
1889 WritePackbitsLength(psd_info,image_info,image,next_image,
1890 compact_pixels,BlueQuantum,exception);
1891 if (next_image->colorspace == CMYKColorspace)
1892 WritePackbitsLength(psd_info,image_info,image,next_image,
1893 compact_pixels,BlackQuantum,exception);
1894 if (next_image->alpha_trait == BlendPixelTrait)
1895 WritePackbitsLength(psd_info,image_info,image,next_image,
1896 compact_pixels,AlphaQuantum,exception);
1898 (void) SetImageProgress(image,SaveImagesTag,0,6);
1899 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1900 RedQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1901 MagickFalse,exception);
1902 (void) SetImageProgress(image,SaveImagesTag,1,6);
1903 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1904 GreenQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1905 MagickFalse,exception);
1906 (void) SetImageProgress(image,SaveImagesTag,2,6);
1907 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1908 BlueQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1909 MagickFalse,exception);
1910 (void) SetImageProgress(image,SaveImagesTag,3,6);
1911 if (next_image->colorspace == CMYKColorspace)
1912 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1913 BlackQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1914 MagickFalse,exception);
1915 (void) SetImageProgress(image,SaveImagesTag,4,6);
1916 if (next_image->alpha_trait == BlendPixelTrait)
1917 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1918 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1919 MagickFalse,exception);
1920 (void) SetImageProgress(image,SaveImagesTag,5,6);
1921 if (next_image->colorspace == CMYKColorspace)
1922 (void) NegateImage(next_image,MagickFalse,exception);
1924 if (next_image->compression == RLECompression)
1925 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
1929 static void WritePascalString(Image* inImage,const char *inString,int inPad)
1940 length=(strlen(inString) > 255UL ) ? 255UL : strlen(inString);
1942 (void) WriteBlobByte(inImage,0);
1945 (void) WriteBlobByte(inImage,(unsigned char) length);
1946 (void) WriteBlob(inImage, length, (const unsigned char *) inString);
1949 if ((length % inPad) == 0)
1951 for (i=0; i < (ssize_t) (inPad-(length % inPad)); i++)
1952 (void) WriteBlobByte(inImage,0);
1955 static void WriteResolutionResourceBlock(Image *image)
1964 x_resolution=65536.0*image->resolution.x+0.5;
1965 y_resolution=65536.0*image->resolution.y+0.5;
1967 if (image->units == PixelsPerCentimeterResolution)
1969 x_resolution=2.54*65536.0*image->resolution.x*0.5;
1970 y_resolution=2.54*65536.0*image->resolution.y+0.5;
1973 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
1974 (void) WriteBlobMSBShort(image,0x03ED);
1975 (void) WriteBlobMSBShort(image,0);
1976 (void) WriteBlobMSBLong(image,16); /* resource size */
1977 (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5));
1978 (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */
1979 (void) WriteBlobMSBShort(image,units); /* width unit */
1980 (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5));
1981 (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */
1982 (void) WriteBlobMSBShort(image,units); /* height unit */
1985 static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile)
1987 register const unsigned char
2004 length=GetStringInfoLength(bim_profile);
2007 datum=GetStringInfoDatum(bim_profile);
2008 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2010 register unsigned char
2013 q=(unsigned char *) p;
2014 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2016 p=PushLongPixel(MSBEndian,p,&long_sans);
2017 p=PushShortPixel(MSBEndian,p,&id);
2018 p=PushShortPixel(MSBEndian,p,&short_sans);
2019 p=PushLongPixel(MSBEndian,p,&count);
2020 if (id == 0x0000040f)
2022 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2023 (PSDQuantum(count)+12)-(q-datum));
2024 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2028 if ((count & 0x01) != 0)
2033 static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile)
2035 register const unsigned char
2052 length=GetStringInfoLength(bim_profile);
2055 datum=GetStringInfoDatum(bim_profile);
2056 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2058 register unsigned char
2061 q=(unsigned char *) p;
2062 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2064 p=PushLongPixel(MSBEndian,p,&long_sans);
2065 p=PushShortPixel(MSBEndian,p,&id);
2066 p=PushShortPixel(MSBEndian,p,&short_sans);
2067 p=PushLongPixel(MSBEndian,p,&count);
2068 if ((id == 0x000003ed) && (PSDQuantum(count) < (ssize_t) (length-12)))
2070 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2071 (PSDQuantum(count)+12)-(q-datum));
2072 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2076 if ((count & 0x01) != 0)
2081 static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
2082 ExceptionInfo *exception)
2111 rounded_layer_info_size;
2122 assert(image_info != (const ImageInfo *) NULL);
2123 assert(image_info->signature == MagickSignature);
2124 assert(image != (Image *) NULL);
2125 assert(image->signature == MagickSignature);
2126 if (image->debug != MagickFalse)
2127 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2128 assert(exception != (ExceptionInfo *) NULL);
2129 assert(exception->signature == MagickSignature);
2130 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2131 if (status == MagickFalse)
2133 packet_size=(size_t) (image->depth > 8 ? 6 : 3);
2134 if (image->alpha_trait == BlendPixelTrait)
2135 packet_size+=image->depth > 8 ? 2 : 1;
2137 if ((LocaleCompare(image_info->magick,"PSB") == 0) ||
2138 (image->columns > 30000) || (image->rows > 30000))
2140 (void) WriteBlob(image,4,(const unsigned char *) "8BPS");
2141 (void) WriteBlobMSBShort(image,psd_info.version); /* version */
2142 for (i=1; i <= 6; i++)
2143 (void) WriteBlobByte(image, 0); /* 6 bytes of reserved */
2144 if (IsImageGray(image,exception) != MagickFalse)
2145 num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
2147 if (image->storage_class == PseudoClass)
2148 num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
2151 if (image->colorspace != CMYKColorspace)
2152 num_channels=(image->alpha_trait == BlendPixelTrait ? 4UL : 3UL);
2154 num_channels=(image->alpha_trait == BlendPixelTrait ? 5UL : 4UL);
2156 (void) WriteBlobMSBShort(image,(unsigned short) num_channels);
2157 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
2158 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
2159 if (IsImageGray(image,exception) != MagickFalse)
2167 monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
2168 MagickTrue : MagickFalse;
2169 (void) WriteBlobMSBShort(image,(unsigned short)
2170 (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8));
2171 (void) WriteBlobMSBShort(image,(unsigned short)
2172 (monochrome != MagickFalse ? BitmapMode : GrayscaleMode));
2176 (void) WriteBlobMSBShort(image,(unsigned short) (image->storage_class ==
2177 PseudoClass ? 8 : image->depth > 8 ? 16 : 8));
2178 if (((image_info->colorspace != UndefinedColorspace) ||
2179 (image->colorspace != CMYKColorspace)) &&
2180 (image_info->colorspace != CMYKColorspace))
2182 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2183 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2184 (void) WriteBlobMSBShort(image,(unsigned short)
2185 (image->storage_class == PseudoClass ? IndexedMode : RGBMode));
2189 if (image->colorspace != CMYKColorspace)
2190 (void) TransformImageColorspace(image,CMYKColorspace,exception);
2191 (void) WriteBlobMSBShort(image,CMYKMode);
2194 if ((IsImageGray(image,exception) != MagickFalse) ||
2195 (image->storage_class == DirectClass) || (image->colors > 256))
2196 (void) WriteBlobMSBLong(image,0);
2200 Write PSD raster colormap.
2202 (void) WriteBlobMSBLong(image,768);
2203 for (i=0; i < (ssize_t) image->colors; i++)
2204 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red));
2205 for ( ; i < 256; i++)
2206 (void) WriteBlobByte(image,0);
2207 for (i=0; i < (ssize_t) image->colors; i++)
2208 (void) WriteBlobByte(image,ScaleQuantumToChar(
2209 image->colormap[i].green));
2210 for ( ; i < 256; i++)
2211 (void) WriteBlobByte(image,0);
2212 for (i=0; i < (ssize_t) image->colors; i++)
2213 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue));
2214 for ( ; i < 256; i++)
2215 (void) WriteBlobByte(image,0);
2218 Image resource block.
2220 length=28; /* 0x03EB */
2221 bim_profile=(StringInfo *) GetImageProfile(image,"8bim");
2222 icc_profile=GetImageProfile(image,"icc");
2223 if (bim_profile != (StringInfo *) NULL)
2225 bim_profile=CloneStringInfo(bim_profile);
2226 if (icc_profile != (StringInfo *) NULL)
2227 RemoveICCProfileFromResourceBlock(bim_profile);
2228 RemoveResolutionFromResourceBlock(bim_profile);
2229 length+=PSDQuantum(GetStringInfoLength(bim_profile));
2231 if (icc_profile != (const StringInfo *) NULL)
2232 length+=PSDQuantum(GetStringInfoLength(icc_profile))+12;
2233 (void) WriteBlobMSBLong(image,(unsigned int) length);
2234 WriteResolutionResourceBlock(image);
2235 if (bim_profile != (StringInfo *) NULL)
2237 (void) WriteBlob(image,GetStringInfoLength(bim_profile),
2238 GetStringInfoDatum(bim_profile));
2239 bim_profile=DestroyStringInfo(bim_profile);
2241 if (icc_profile != (StringInfo *) NULL)
2243 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2244 (void) WriteBlobMSBShort(image,0x0000040F);
2245 (void) WriteBlobMSBShort(image,0);
2246 (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength(
2248 (void) WriteBlob(image,GetStringInfoLength(icc_profile),
2249 GetStringInfoDatum(icc_profile));
2250 if ((MagickOffsetType) GetStringInfoLength(icc_profile) !=
2251 PSDQuantum(GetStringInfoLength(icc_profile)))
2252 (void) WriteBlobByte(image,0);
2256 base_image=GetNextImageInList(image);
2257 if ((image->alpha_trait == BlendPixelTrait) && (base_image == (Image *) NULL))
2259 next_image=base_image;
2260 while ( next_image != NULL )
2262 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2263 if (IsImageGray(next_image,exception) != MagickFalse)
2264 num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
2266 if (next_image->storage_class == PseudoClass)
2267 num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
2269 if (next_image->colorspace != CMYKColorspace)
2270 num_channels=next_image->alpha_trait == BlendPixelTrait ? 4UL : 3UL;
2272 num_channels=next_image->alpha_trait == BlendPixelTrait ? 5UL : 4UL;
2273 channelLength=(size_t) (next_image->columns*next_image->rows*packet_size+2);
2274 layer_info_size+=(size_t) (4*4+2+num_channels*6+(psd_info.version == 1 ? 8 :
2275 16)+4*1+4+num_channels*channelLength);
2276 property=(const char *) GetImageProperty(next_image,"label",exception);
2277 if (property == (const char *) NULL)
2278 layer_info_size+=16;
2284 length=strlen(property);
2285 layer_info_size+=8+length+(4-(length % 4));
2288 next_image=GetNextImageInList(next_image);
2290 if (layer_count == 0)
2291 (void) SetPSDSize(&psd_info,image,0);
2297 (void) SetPSDSize(&psd_info,image,layer_info_size+
2298 (psd_info.version == 1 ? 8 : 16));
2299 if ((layer_info_size/2) != ((layer_info_size+1)/2))
2300 rounded_layer_info_size=layer_info_size+1;
2302 rounded_layer_info_size=layer_info_size;
2303 (void) SetPSDSize(&psd_info,image,rounded_layer_info_size);
2304 (void) WriteBlobMSBShort(image,(unsigned short) layer_count);
2306 compression=base_image->compression;
2307 next_image=base_image;
2308 while (next_image != NULL)
2310 next_image->compression=NoCompression;
2311 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y);
2312 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x);
2313 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y+
2315 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x+
2316 next_image->columns);
2317 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2318 channel_size=(unsigned int) ((packet_size*next_image->rows*
2319 next_image->columns)+2);
2320 if ((IsImageGray(next_image,exception) != MagickFalse) ||
2321 (next_image->storage_class == PseudoClass))
2323 (void) WriteBlobMSBShort(image,(unsigned short)
2324 (next_image->alpha_trait == BlendPixelTrait ? 2 : 1));
2325 (void) WriteBlobMSBShort(image,0);
2326 (void) SetPSDSize(&psd_info,image,channel_size);
2327 if (next_image->alpha_trait == BlendPixelTrait)
2329 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2330 (void) SetPSDSize(&psd_info,image,channel_size);
2334 if (next_image->colorspace != CMYKColorspace)
2336 (void) WriteBlobMSBShort(image,(unsigned short)
2337 (next_image->alpha_trait == BlendPixelTrait ? 4 : 3));
2338 (void) WriteBlobMSBShort(image,0);
2339 (void) SetPSDSize(&psd_info,image,channel_size);
2340 (void) WriteBlobMSBShort(image,1);
2341 (void) SetPSDSize(&psd_info,image,channel_size);
2342 (void) WriteBlobMSBShort(image,2);
2343 (void) SetPSDSize(&psd_info,image,channel_size);
2344 if (next_image->alpha_trait == BlendPixelTrait)
2346 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2347 (void) SetPSDSize(&psd_info,image,channel_size);
2352 (void) WriteBlobMSBShort(image,(unsigned short)
2353 (next_image->alpha_trait ? 5 : 4));
2354 (void) WriteBlobMSBShort(image,0);
2355 (void) SetPSDSize(&psd_info,image,channel_size);
2356 (void) WriteBlobMSBShort(image,1);
2357 (void) SetPSDSize(&psd_info,image,channel_size);
2358 (void) WriteBlobMSBShort(image,2);
2359 (void) SetPSDSize(&psd_info,image,channel_size);
2360 (void) WriteBlobMSBShort(image,3);
2361 (void) SetPSDSize(&psd_info,image,channel_size);
2362 if (next_image->alpha_trait)
2364 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2365 (void) SetPSDSize(&psd_info,image,channel_size);
2368 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2369 (void) WriteBlob(image,4,(const unsigned char *)
2370 CompositeOperatorToPSDBlendMode(next_image->compose));
2371 (void) WriteBlobByte(image,255); /* layer opacity */
2372 (void) WriteBlobByte(image,0);
2373 (void) WriteBlobByte(image,1); /* layer propertys - visible, etc. */
2374 (void) WriteBlobByte(image,0);
2375 property=(const char *) GetImageProperty(next_image,"label",exception);
2376 if (property == (const char *) NULL)
2378 (void) WriteBlobMSBLong(image,16);
2379 (void) WriteBlobMSBLong(image,0);
2380 (void) WriteBlobMSBLong(image,0);
2381 (void) FormatLocaleString((char *) layer_name,MaxTextExtent,
2382 "L%06ld",(long) layer_count++);
2383 WritePascalString( image, (char*)layer_name, 4 );
2390 length=strlen(property);
2391 (void) WriteBlobMSBLong(image,(unsigned int) (length+(4-
2393 (void) WriteBlobMSBLong(image,0);
2394 (void) WriteBlobMSBLong(image,0);
2395 WritePascalString(image,property,4);
2397 next_image=GetNextImageInList(next_image);
2402 next_image=base_image;
2403 while (next_image != NULL)
2405 status=WriteImageChannels(&psd_info,image_info,image,next_image,
2406 MagickTrue,exception);
2407 next_image=GetNextImageInList(next_image);
2409 (void) WriteBlobMSBLong(image,0); /* user mask data */
2410 base_image->compression=compression;
2413 Write composite image.
2415 status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse,
2417 (void) CloseBlob(image);