2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Adobe Photoshop Image Format %
23 % Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization %
24 % dedicated to making software imaging solutions freely available. %
26 % You may not use this file except in compliance with the License. You may %
27 % obtain a copy of the License at %
29 % http://www.imagemagick.org/script/license.php %
31 % Unless required by applicable law or agreed to in writing, software %
32 % distributed under the License is distributed on an "AS IS" BASIS, %
33 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
34 % See the License for the specific language governing permissions and %
35 % limitations under the License. %
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 #include "MagickCore/studio.h"
46 #include "MagickCore/artifact.h"
47 #include "MagickCore/attribute.h"
48 #include "MagickCore/blob.h"
49 #include "MagickCore/blob-private.h"
50 #include "MagickCore/cache.h"
51 #include "MagickCore/channel.h"
52 #include "MagickCore/colormap.h"
53 #include "MagickCore/colormap-private.h"
54 #include "MagickCore/colorspace.h"
55 #include "MagickCore/colorspace-private.h"
56 #include "MagickCore/constitute.h"
57 #include "MagickCore/enhance.h"
58 #include "MagickCore/exception.h"
59 #include "MagickCore/exception-private.h"
60 #include "MagickCore/image.h"
61 #include "MagickCore/image-private.h"
62 #include "MagickCore/list.h"
63 #include "MagickCore/log.h"
64 #include "MagickCore/magick.h"
65 #include "MagickCore/memory_.h"
66 #include "MagickCore/module.h"
67 #include "MagickCore/monitor-private.h"
68 #include "MagickCore/pixel.h"
69 #include "MagickCore/pixel-accessor.h"
70 #include "MagickCore/profile.h"
71 #include "MagickCore/property.h"
72 #include "MagickCore/quantum-private.h"
73 #include "MagickCore/static.h"
74 #include "MagickCore/string_.h"
75 #ifdef MAGICKCORE_ZLIB_DELEGATE
78 #include "psd-private.h"
83 #define MaxPSDChannels 56
84 #define PSDQuantum(x) (((ssize_t) (x)+1) & -2)
87 Enumerated declaractions.
93 ZipWithoutPrediction = 2,
104 MultichannelMode = 7,
110 Typedef declaractions.
112 typedef struct _ChannelInfo
121 typedef struct _MaskInfo
134 typedef struct _LayerInfo
137 channel_info[MaxPSDChannels];
169 Forward declarations.
171 static MagickBooleanType
172 WritePSDImage(const ImageInfo *,Image *,ExceptionInfo *);
175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 % IsPSD()() returns MagickTrue if the image format type, identified by the
186 % magick string, is PSD.
188 % The format of the IsPSD method is:
190 % MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
192 % A description of each parameter follows:
194 % o magick: compare image format pattern against these bytes.
196 % o length: Specifies the length of the magick string.
199 static MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
203 if (LocaleNCompare((const char *) magick,"8BPS",4) == 0)
209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213 % R e a d P S D I m a g e %
217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 % ReadPSDImage() reads an Adobe Photoshop image file and returns it. It
220 % allocates the memory necessary for the new Image structure and returns a
221 % pointer to the new image.
223 % The format of the ReadPSDImage method is:
225 % Image *ReadPSDImage(image_info,ExceptionInfo *exception)
227 % A description of each parameter follows:
229 % o image_info: the image info.
231 % o exception: return any errors or warnings in this structure.
235 static const char *CompositeOperatorToPSDBlendMode(CompositeOperator op)
242 case ColorBurnCompositeOp: blend_mode = "idiv"; break;
243 case ColorDodgeCompositeOp: blend_mode = "div "; break;
244 case ColorizeCompositeOp: blend_mode = "colr"; break;
245 case DarkenCompositeOp: blend_mode = "dark"; break;
246 case DifferenceCompositeOp: blend_mode = "diff"; break;
247 case DissolveCompositeOp: blend_mode = "diss"; break;
248 case ExclusionCompositeOp: blend_mode = "smud"; break;
249 case HardLightCompositeOp: blend_mode = "hLit"; break;
250 case HardMixCompositeOp: blend_mode = "hMix"; break;
251 case HueCompositeOp: blend_mode = "hue "; break;
252 case LightenCompositeOp: blend_mode = "lite"; break;
253 case LinearBurnCompositeOp: blend_mode = "lbrn"; break;
254 case LinearDodgeCompositeOp:blend_mode = "lddg"; break;
255 case LinearLightCompositeOp:blend_mode = "lLit"; break;
256 case LuminizeCompositeOp: blend_mode = "lum "; break;
257 case MultiplyCompositeOp: blend_mode = "mul "; break;
258 case OverCompositeOp: blend_mode = "norm"; break;
259 case OverlayCompositeOp: blend_mode = "over"; break;
260 case PinLightCompositeOp: blend_mode = "pLit"; break;
261 case SaturateCompositeOp: blend_mode = "sat "; break;
262 case ScreenCompositeOp: blend_mode = "scrn"; break;
263 case SoftLightCompositeOp: blend_mode = "sLit"; break;
264 case VividLightCompositeOp: blend_mode = "vLit"; break;
265 default: blend_mode = "norm";
270 static inline CompressionType ConvertPSDCompression(
271 PSDCompressionType compression)
276 return RLECompression;
277 case ZipWithPrediction:
278 case ZipWithoutPrediction:
279 return ZipCompression;
281 return NoCompression;
285 static MagickStatusType CorrectPSDOpacity(LayerInfo* layer_info,
286 ExceptionInfo *exception)
297 if (layer_info->opacity == OpaqueAlpha)
300 layer_info->image->alpha_trait=BlendPixelTrait;
301 for (y=0; y < (ssize_t) layer_info->image->rows; y++)
303 q=GetAuthenticPixels(layer_info->image,0,y,layer_info->image->columns,1,
305 if (q == (Quantum *) NULL)
307 for (x=0; x < (ssize_t) layer_info->image->columns; x++)
309 SetPixelAlpha(layer_info->image,(Quantum) (QuantumScale*(GetPixelAlpha(
310 layer_info->image,q))*layer_info->opacity),q);
311 q+=GetPixelChannels(layer_info->image);
313 if (SyncAuthenticPixels(layer_info->image,exception) == MagickFalse)
320 static ssize_t DecodePSDPixels(const size_t number_compact_pixels,
321 const unsigned char *compact_pixels,const ssize_t depth,
322 const size_t number_pixels,unsigned char *pixels)
324 #define CheckNumberCompactPixels \
329 #define CheckNumberPixels(count) \
330 if (((ssize_t) i + count) > (ssize_t) number_pixels) \
347 packets=(ssize_t) number_compact_pixels;
348 for (i=0; (packets > 1) && (i < (ssize_t) number_pixels); )
351 length=(size_t) (*compact_pixels++);
357 CheckNumberCompactPixels;
358 pixel=(*compact_pixels++);
359 for (j=0; j < (ssize_t) length; j++)
365 CheckNumberPixels(8);
366 *pixels++=(pixel >> 7) & 0x01 ? 0U : 255U;
367 *pixels++=(pixel >> 6) & 0x01 ? 0U : 255U;
368 *pixels++=(pixel >> 5) & 0x01 ? 0U : 255U;
369 *pixels++=(pixel >> 4) & 0x01 ? 0U : 255U;
370 *pixels++=(pixel >> 3) & 0x01 ? 0U : 255U;
371 *pixels++=(pixel >> 2) & 0x01 ? 0U : 255U;
372 *pixels++=(pixel >> 1) & 0x01 ? 0U : 255U;
373 *pixels++=(pixel >> 0) & 0x01 ? 0U : 255U;
378 CheckNumberPixels(4);
379 *pixels++=(unsigned char) ((pixel >> 6) & 0x03);
380 *pixels++=(unsigned char) ((pixel >> 4) & 0x03);
381 *pixels++=(unsigned char) ((pixel >> 2) & 0x03);
382 *pixels++=(unsigned char) ((pixel & 0x03) & 0x03);
387 CheckNumberPixels(2);
388 *pixels++=(unsigned char) ((pixel >> 4) & 0xff);
389 *pixels++=(unsigned char) ((pixel & 0x0f) & 0xff);
394 CheckNumberPixels(1);
395 *pixels++=(unsigned char) pixel;
403 for (j=0; j < (ssize_t) length; j++)
409 CheckNumberPixels(8);
410 *pixels++=(*compact_pixels >> 7) & 0x01 ? 0U : 255U;
411 *pixels++=(*compact_pixels >> 6) & 0x01 ? 0U : 255U;
412 *pixels++=(*compact_pixels >> 5) & 0x01 ? 0U : 255U;
413 *pixels++=(*compact_pixels >> 4) & 0x01 ? 0U : 255U;
414 *pixels++=(*compact_pixels >> 3) & 0x01 ? 0U : 255U;
415 *pixels++=(*compact_pixels >> 2) & 0x01 ? 0U : 255U;
416 *pixels++=(*compact_pixels >> 1) & 0x01 ? 0U : 255U;
417 *pixels++=(*compact_pixels >> 0) & 0x01 ? 0U : 255U;
422 CheckNumberPixels(4);
423 *pixels++=(*compact_pixels >> 6) & 0x03;
424 *pixels++=(*compact_pixels >> 4) & 0x03;
425 *pixels++=(*compact_pixels >> 2) & 0x03;
426 *pixels++=(*compact_pixels & 0x03) & 0x03;
431 CheckNumberPixels(2);
432 *pixels++=(*compact_pixels >> 4) & 0xff;
433 *pixels++=(*compact_pixels & 0x0f) & 0xff;
438 CheckNumberPixels(1);
439 *pixels++=(*compact_pixels);
443 CheckNumberCompactPixels;
450 static inline LayerInfo *DestroyLayerInfo(LayerInfo *layer_info,
451 const ssize_t number_layers)
456 for (i=0; i<number_layers; i++)
458 if (layer_info[i].image != (Image *) NULL)
459 layer_info[i].image=DestroyImage(layer_info[i].image);
460 if (layer_info[i].mask.image != (Image *) NULL)
461 layer_info[i].mask.image=DestroyImage(layer_info[i].mask.image);
464 return (LayerInfo *) RelinquishMagickMemory(layer_info);
467 static inline size_t GetPSDPacketSize(Image *image)
469 if (image->storage_class == PseudoClass)
471 if (image->colors > 256)
473 else if (image->depth > 8)
477 if (image->depth > 8)
483 static inline MagickSizeType GetPSDSize(const PSDInfo *psd_info,Image *image)
485 if (psd_info->version == 1)
486 return((MagickSizeType) ReadBlobLong(image));
487 return((MagickSizeType) ReadBlobLongLong(image));
490 static inline size_t GetPSDRowSize(Image *image)
492 if (image->depth == 1)
493 return((image->columns+7)/8);
495 return(image->columns*GetPSDPacketSize(image));
498 static const char *ModeToString(PSDImageType type)
502 case BitmapMode: return "Bitmap";
503 case GrayscaleMode: return "Grayscale";
504 case IndexedMode: return "Indexed";
505 case RGBMode: return "RGB";
506 case CMYKMode: return "CMYK";
507 case MultichannelMode: return "Multichannel";
508 case DuotoneMode: return "Duotone";
509 case LabMode: return "L*A*B";
510 default: return "unknown";
514 static void NegateCMYK(Image *image,ExceptionInfo *exception)
519 channel_mask=SetImageChannelMask(image,(ChannelType)(AllChannels &~
521 NegateImage(image,MagickFalse,exception);
522 (void) SetImageChannelMask(image,channel_mask);
525 static void ParseImageResourceBlocks(Image *image,
526 const unsigned char *blocks,size_t length,
527 MagickBooleanType *has_merged_image,ExceptionInfo *exception)
545 profile=BlobToStringInfo((const unsigned char *) NULL,length);
546 SetStringInfoDatum(profile,blocks);
547 (void) SetImageProfile(image,"8bim",profile,exception);
548 profile=DestroyStringInfo(profile);
549 for (p=blocks; (p >= blocks) && (p < (blocks+length-16)); )
551 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
553 p=PushLongPixel(MSBEndian,p,&long_sans);
554 p=PushShortPixel(MSBEndian,p,&id);
555 p=PushShortPixel(MSBEndian,p,&short_sans);
556 p=PushLongPixel(MSBEndian,p,&count);
557 if (p+count > blocks+length)
564 value[MagickPathExtent];
572 p=PushShortPixel(MSBEndian,p,&resolution);
573 image->resolution.x=(double) resolution;
574 (void) FormatLocaleString(value,MagickPathExtent,"%g",image->resolution.x);
575 (void) SetImageProperty(image,"tiff:XResolution",value,exception);
576 p=PushShortPixel(MSBEndian,p,&short_sans);
577 p=PushShortPixel(MSBEndian,p,&short_sans);
578 p=PushShortPixel(MSBEndian,p,&short_sans);
579 p=PushShortPixel(MSBEndian,p,&resolution);
580 image->resolution.y=(double) resolution;
581 (void) FormatLocaleString(value,MagickPathExtent,"%g",image->resolution.y);
582 (void) SetImageProperty(image,"tiff:YResolution",value,exception);
583 p=PushShortPixel(MSBEndian,p,&short_sans);
584 p=PushShortPixel(MSBEndian,p,&short_sans);
585 p=PushShortPixel(MSBEndian,p,&short_sans);
586 image->units=PixelsPerInchResolution;
592 *has_merged_image=MagickFalse;
602 if ((count & 0x01) != 0)
608 static CompositeOperator PSDBlendModeToCompositeOperator(const char *mode)
610 if (mode == (const char *) NULL)
611 return(OverCompositeOp);
612 if (LocaleNCompare(mode,"norm",4) == 0)
613 return(OverCompositeOp);
614 if (LocaleNCompare(mode,"mul ",4) == 0)
615 return(MultiplyCompositeOp);
616 if (LocaleNCompare(mode,"diss",4) == 0)
617 return(DissolveCompositeOp);
618 if (LocaleNCompare(mode,"diff",4) == 0)
619 return(DifferenceCompositeOp);
620 if (LocaleNCompare(mode,"dark",4) == 0)
621 return(DarkenCompositeOp);
622 if (LocaleNCompare(mode,"lite",4) == 0)
623 return(LightenCompositeOp);
624 if (LocaleNCompare(mode,"hue ",4) == 0)
625 return(HueCompositeOp);
626 if (LocaleNCompare(mode,"sat ",4) == 0)
627 return(SaturateCompositeOp);
628 if (LocaleNCompare(mode,"colr",4) == 0)
629 return(ColorizeCompositeOp);
630 if (LocaleNCompare(mode,"lum ",4) == 0)
631 return(LuminizeCompositeOp);
632 if (LocaleNCompare(mode,"scrn",4) == 0)
633 return(ScreenCompositeOp);
634 if (LocaleNCompare(mode,"over",4) == 0)
635 return(OverlayCompositeOp);
636 if (LocaleNCompare(mode,"hLit",4) == 0)
637 return(HardLightCompositeOp);
638 if (LocaleNCompare(mode,"sLit",4) == 0)
639 return(SoftLightCompositeOp);
640 if (LocaleNCompare(mode,"smud",4) == 0)
641 return(ExclusionCompositeOp);
642 if (LocaleNCompare(mode,"div ",4) == 0)
643 return(ColorDodgeCompositeOp);
644 if (LocaleNCompare(mode,"idiv",4) == 0)
645 return(ColorBurnCompositeOp);
646 if (LocaleNCompare(mode,"lbrn",4) == 0)
647 return(LinearBurnCompositeOp);
648 if (LocaleNCompare(mode,"lddg",4) == 0)
649 return(LinearDodgeCompositeOp);
650 if (LocaleNCompare(mode,"lLit",4) == 0)
651 return(LinearLightCompositeOp);
652 if (LocaleNCompare(mode,"vLit",4) == 0)
653 return(VividLightCompositeOp);
654 if (LocaleNCompare(mode,"pLit",4) == 0)
655 return(PinLightCompositeOp);
656 if (LocaleNCompare(mode,"hMix",4) == 0)
657 return(HardMixCompositeOp);
658 return(OverCompositeOp);
661 static inline void ReversePSDString(Image *image,char *p,size_t length)
666 if (image->endian == MSBEndian)
670 for(--q; p < q; ++p, --q)
678 static MagickStatusType ReadPSDChannelPixels(Image *image,
679 const size_t channels,const size_t row,const ssize_t type,
680 const unsigned char *pixels,ExceptionInfo *exception)
685 register const unsigned char
701 q=GetAuthenticPixels(image,0,row,image->columns,1,exception);
702 if (q == (Quantum *) NULL)
704 packet_size=GetPSDPacketSize(image);
705 for (x=0; x < (ssize_t) image->columns; x++)
707 if (packet_size == 1)
708 pixel=ScaleCharToQuantum(*p++);
711 p=PushShortPixel(MSBEndian,p,&nibble);
712 pixel=ScaleShortToQuantum(nibble);
718 SetPixelAlpha(image,pixel,q);
724 SetPixelRed(image,pixel,q);
725 if (channels == 1 || type == -2)
726 SetPixelGray(image,pixel,q);
727 if (image->storage_class == PseudoClass)
729 if (packet_size == 1)
730 SetPixelIndex(image,ScaleQuantumToChar(pixel),q);
732 SetPixelIndex(image,ScaleQuantumToShort(pixel),q);
733 SetPixelViaPixelInfo(image,image->colormap+(ssize_t)
734 ConstrainColormapIndex(image,GetPixelIndex(image,q),exception),q);
735 if (image->depth == 1)
741 number_bits=image->columns-x;
744 for (bit=0; bit < number_bits; bit++)
746 SetPixelIndex(image,(((unsigned char) pixel) &
747 (0x01 << (7-bit))) != 0 ? 0 : 255,q);
748 SetPixelViaPixelInfo(image,image->colormap+(ssize_t)
749 GetPixelIndex(image,q),q);
750 q+=GetPixelChannels(image);
761 if (image->storage_class == PseudoClass)
762 SetPixelAlpha(image,pixel,q);
764 SetPixelGreen(image,pixel,q);
769 if (image->storage_class == PseudoClass)
770 SetPixelAlpha(image,pixel,q);
772 SetPixelBlue(image,pixel,q);
777 if (image->colorspace == CMYKColorspace)
778 SetPixelBlack(image,pixel,q);
780 if (image->alpha_trait != UndefinedPixelTrait)
781 SetPixelAlpha(image,pixel,q);
786 if ((IssRGBCompatibleColorspace(image->colorspace) != MagickFalse) &&
789 if (image->alpha_trait != UndefinedPixelTrait)
790 SetPixelAlpha(image,pixel,q);
796 q+=GetPixelChannels(image);
798 return(SyncAuthenticPixels(image,exception));
801 static MagickStatusType ReadPSDChannelRaw(Image *image,const size_t channels,
802 const ssize_t type,ExceptionInfo *exception)
817 if (image->debug != MagickFalse)
818 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
819 " layer data is RAW");
821 row_size=GetPSDRowSize(image);
822 pixels=(unsigned char *) AcquireQuantumMemory(row_size,sizeof(*pixels));
823 if (pixels == (unsigned char *) NULL)
824 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
828 for (y=0; y < (ssize_t) image->rows; y++)
832 count=ReadBlob(image,row_size,pixels);
833 if (count != row_size)
836 status=ReadPSDChannelPixels(image,channels,y,type,pixels,exception);
837 if (status == MagickFalse)
841 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
845 static inline MagickOffsetType *ReadPSDRLEOffsets(Image *image,
846 const PSDInfo *psd_info,const size_t size)
854 offsets=(MagickOffsetType *) AcquireQuantumMemory(size,sizeof(*offsets));
855 if(offsets != (MagickOffsetType *) NULL)
857 for (y=0; y < (ssize_t) size; y++)
859 if (psd_info->version == 1)
860 offsets[y]=(MagickOffsetType) ReadBlobShort(image);
862 offsets[y]=(MagickOffsetType) ReadBlobLong(image);
868 static MagickStatusType ReadPSDChannelRLE(Image *image,const PSDInfo *psd_info,
869 const ssize_t type,MagickOffsetType *offsets,ExceptionInfo *exception)
886 if (image->debug != MagickFalse)
887 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
888 " layer data is RLE compressed");
890 row_size=GetPSDRowSize(image);
891 pixels=(unsigned char *) AcquireQuantumMemory(row_size,sizeof(*pixels));
892 if (pixels == (unsigned char *) NULL)
893 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
897 for (y=0; y < (ssize_t) image->rows; y++)
898 if ((MagickOffsetType) length < offsets[y])
899 length=(size_t) offsets[y];
901 if (length > row_size + 256) // arbitrary number
903 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
904 ThrowBinaryException(ResourceLimitError,"InvalidLength",
908 compact_pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
909 if (compact_pixels == (unsigned char *) NULL)
911 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
912 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
916 (void) ResetMagickMemory(compact_pixels,0,length*sizeof(*compact_pixels));
919 for (y=0; y < (ssize_t) image->rows; y++)
923 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
924 if (count != (ssize_t) offsets[y])
927 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
928 (ssize_t) (image->depth == 1 ? 123456 : image->depth),row_size,pixels);
929 if (count != (ssize_t) row_size)
932 status=ReadPSDChannelPixels(image,psd_info->channels,y,type,pixels,
934 if (status == MagickFalse)
938 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
939 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
943 #ifdef MAGICKCORE_ZLIB_DELEGATE
944 static MagickStatusType ReadPSDChannelZip(Image *image,const size_t channels,
945 const ssize_t type,const PSDCompressionType compression,
946 const size_t compact_size,ExceptionInfo *exception)
951 register unsigned char
970 if (image->debug != MagickFalse)
971 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
972 " layer data is ZIP compressed");
974 compact_pixels=(unsigned char *) AcquireQuantumMemory(compact_size,
975 sizeof(*compact_pixels));
976 if (compact_pixels == (unsigned char *) NULL)
977 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
980 packet_size=GetPSDPacketSize(image);
981 row_size=image->columns*packet_size;
982 count=image->rows*row_size;
984 pixels=(unsigned char *) AcquireQuantumMemory(count,sizeof(*pixels));
985 if (pixels == (unsigned char *) NULL)
987 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
988 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
992 ResetMagickMemory(&stream, 0, sizeof(z_stream));
993 stream.data_type=Z_BINARY;
994 (void) ReadBlob(image,compact_size,compact_pixels);
996 stream.next_in=(Bytef *)compact_pixels;
997 stream.avail_in=(unsigned int) compact_size;
998 stream.next_out=(Bytef *)pixels;
999 stream.avail_out=(unsigned int) count;
1001 if(inflateInit(&stream) == Z_OK)
1006 while (stream.avail_out > 0)
1008 ret=inflate(&stream, Z_SYNC_FLUSH);
1009 if (ret != Z_OK && ret != Z_STREAM_END)
1011 compact_pixels=(unsigned char *) RelinquishMagickMemory(
1013 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1014 return(MagickFalse);
1019 if (compression == ZipWithPrediction)
1024 length=image->columns;
1027 if (packet_size == 2)
1029 p[2]+=p[0]+((p[1]+p[3]) >> 8);
1043 for (y=0; y < (ssize_t) image->rows; y++)
1045 status=ReadPSDChannelPixels(image,channels,y,type,p,exception);
1046 if (status == MagickFalse)
1052 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
1053 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1058 static MagickStatusType ReadPSDChannel(Image *image,const PSDInfo *psd_info,
1059 LayerInfo* layer_info,const size_t channel,
1060 const PSDCompressionType compression,ExceptionInfo *exception)
1072 channel_image=image;
1073 mask=(Image *) NULL;
1074 if (layer_info->channel_info[channel].type < -1)
1077 Ignore mask that is not a user supplied layer mask, if the mask is
1078 disabled or if the flags have unsupported values.
1080 if (layer_info->channel_info[channel].type != -2 ||
1081 (layer_info->mask.flags > 3) || (layer_info->mask.flags & 0x02))
1083 SeekBlob(image,layer_info->channel_info[channel].size-2,SEEK_CUR);
1086 mask=CloneImage(image,layer_info->mask.page.width,
1087 layer_info->mask.page.height,MagickFalse,exception);
1088 SetImageType(mask,GrayscaleType,exception);
1092 offset=TellBlob(channel_image);
1097 return(ReadPSDChannelRaw(channel_image,psd_info->channels,
1098 layer_info->channel_info[channel].type,exception));
1104 offsets=ReadPSDRLEOffsets(channel_image,psd_info,channel_image->rows);
1105 if (offsets == (MagickOffsetType *) NULL)
1106 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1108 status=ReadPSDChannelRLE(channel_image,psd_info,
1109 layer_info->channel_info[channel].type,offsets,exception);
1110 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1113 case ZipWithPrediction:
1114 case ZipWithoutPrediction:
1115 #ifdef MAGICKCORE_ZLIB_DELEGATE
1116 status=ReadPSDChannelZip(channel_image,layer_info->channels,
1117 layer_info->channel_info[channel].type,compression,
1118 layer_info->channel_info[channel].size-2,exception);
1120 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1121 (void) ThrowMagickException(exception,GetMagickModule(),
1122 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn",
1123 "'%s' (ZLIB)",image->filename);
1127 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1128 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
1129 "CompressionNotSupported","'%.20g'",(double) compression);
1133 if (status == MagickFalse)
1134 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1135 if (mask != (Image *) NULL)
1137 if (status != MagickFalse)
1142 layer_info->mask.image=CloneImage(image,image->columns,image->rows,
1143 MagickTrue,exception);
1144 layer_info->mask.image->alpha_trait=UndefinedPixelTrait;
1145 GetPixelInfo(layer_info->mask.image,&color);
1146 color.red=layer_info->mask.background == 0 ? 0 : QuantumRange;
1147 SetImageColor(layer_info->mask.image,&color,exception);
1148 (void) CompositeImage(layer_info->mask.image,mask,OverCompositeOp,
1149 MagickTrue,layer_info->mask.page.x,layer_info->mask.page.y,
1158 static MagickStatusType ReadPSDLayer(Image *image,const PSDInfo *psd_info,
1159 LayerInfo* layer_info,ExceptionInfo *exception)
1162 message[MagickPathExtent];
1173 if (image->debug != MagickFalse)
1174 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1175 " setting up new layer image");
1176 (void) SetImageBackgroundColor(layer_info->image,exception);
1177 layer_info->image->compose=PSDBlendModeToCompositeOperator(
1178 layer_info->blendkey);
1179 if (layer_info->visible == MagickFalse)
1180 layer_info->image->compose=NoCompositeOp;
1181 if (psd_info->mode == CMYKMode)
1182 SetImageColorspace(layer_info->image,CMYKColorspace,exception);
1183 if ((psd_info->mode == BitmapMode) || (psd_info->mode == GrayscaleMode) ||
1184 (psd_info->mode == DuotoneMode))
1185 SetImageColorspace(layer_info->image,GRAYColorspace,exception);
1187 Set up some hidden attributes for folks that need them.
1189 (void) FormatLocaleString(message,MagickPathExtent,"%.20g",
1190 (double) layer_info->page.x);
1191 (void) SetImageArtifact(layer_info->image,"psd:layer.x",message);
1192 (void) FormatLocaleString(message,MagickPathExtent,"%.20g",
1193 (double) layer_info->page.y);
1194 (void) SetImageArtifact(layer_info->image,"psd:layer.y",message);
1195 (void) FormatLocaleString(message,MagickPathExtent,"%.20g",(double)
1196 layer_info->opacity);
1197 (void) SetImageArtifact(layer_info->image,"psd:layer.opacity",message);
1198 (void) SetImageProperty(layer_info->image,"label",(char *) layer_info->name,
1202 for (j=0; j < (ssize_t) layer_info->channels; j++)
1204 if (image->debug != MagickFalse)
1205 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1206 " reading data for channel %.20g",(double) j);
1208 compression=(PSDCompressionType) ReadBlobShort(layer_info->image);
1209 layer_info->image->compression=ConvertPSDCompression(compression);
1210 if (layer_info->channel_info[j].type == -1)
1211 layer_info->image->alpha_trait=BlendPixelTrait;
1213 status=ReadPSDChannel(layer_info->image,psd_info,layer_info,j,
1214 compression,exception);
1216 if (status == MagickFalse)
1220 if (status != MagickFalse)
1221 status=CorrectPSDOpacity(layer_info,exception);
1223 if (status != MagickFalse)
1225 if (layer_info->image->colorspace == CMYKColorspace)
1226 NegateCMYK(layer_info->image,exception);
1227 if (layer_info->mask.image != (Image *) NULL)
1229 CompositeImage(layer_info->image,layer_info->mask.image,
1230 CopyAlphaCompositeOp,MagickTrue,0,0,exception);
1231 layer_info->mask.image=DestroyImage(layer_info->mask.image);
1238 ModuleExport MagickStatusType ReadPSDLayers(Image *image,
1239 const ImageInfo *image_info,const PSDInfo *psd_info,
1240 const MagickBooleanType skip_layers,ExceptionInfo *exception)
1262 size=GetPSDSize(psd_info,image);
1266 Skip layers & masks.
1268 (void) ReadBlobLong(image);
1269 count=ReadBlob(image,4,(unsigned char *) type);
1270 ReversePSDString(image,type,4);
1272 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1276 count=ReadBlob(image,4,(unsigned char *) type);
1277 ReversePSDString(image,type,4);
1278 if ((count != 0) && (LocaleNCompare(type,"Lr16",4) == 0))
1279 size=GetPSDSize(psd_info,image);
1287 layer_info=(LayerInfo *) NULL;
1288 number_layers=(short) ReadBlobShort(image);
1290 if (number_layers < 0)
1293 The first alpha channel in the merged result contains the
1294 transparency data for the merged result.
1296 number_layers=MagickAbsoluteValue(number_layers);
1297 if (image->debug != MagickFalse)
1298 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1299 " negative layer count corrected for");
1300 image->alpha_trait=BlendPixelTrait;
1304 We only need to know if the image has an alpha channel
1306 if (skip_layers != MagickFalse)
1309 if (image->debug != MagickFalse)
1310 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1311 " image contains %.20g layers",(double) number_layers);
1313 if (number_layers == 0)
1314 ThrowBinaryException(CorruptImageError,"InvalidNumberOfLayers",
1317 layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers,
1318 sizeof(*layer_info));
1319 if (layer_info == (LayerInfo *) NULL)
1321 if (image->debug != MagickFalse)
1322 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1323 " allocation of LayerInfo failed");
1324 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1327 (void) ResetMagickMemory(layer_info,0,(size_t) number_layers*
1328 sizeof(*layer_info));
1330 for (i=0; i < number_layers; i++)
1336 if (image->debug != MagickFalse)
1337 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1338 " reading layer #%.20g",(double) i+1);
1339 layer_info[i].page.y=(int) ReadBlobLong(image);
1340 layer_info[i].page.x=(int) ReadBlobLong(image);
1341 y=(int) ReadBlobLong(image);
1342 x=(int) ReadBlobLong(image);
1343 layer_info[i].page.width=(size_t) (x-layer_info[i].page.x);
1344 layer_info[i].page.height=(size_t) (y-layer_info[i].page.y);
1345 layer_info[i].channels=ReadBlobShort(image);
1346 if (layer_info[i].channels > MaxPSDChannels)
1348 layer_info=DestroyLayerInfo(layer_info,number_layers);
1349 ThrowBinaryException(CorruptImageError,"MaximumChannelsExceeded",
1352 if (image->debug != MagickFalse)
1353 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1354 " offset(%.20g,%.20g), size(%.20g,%.20g), channels=%.20g",
1355 (double) layer_info[i].page.x,(double) layer_info[i].page.y,
1356 (double) layer_info[i].page.height,(double)
1357 layer_info[i].page.width,(double) layer_info[i].channels);
1358 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1360 layer_info[i].channel_info[j].type=(short) ReadBlobShort(image);
1361 layer_info[i].channel_info[j].size=(size_t) GetPSDSize(psd_info,
1363 if (image->debug != MagickFalse)
1364 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1365 " channel[%.20g]: type=%.20g, size=%.20g",(double) j,
1366 (double) layer_info[i].channel_info[j].type,
1367 (double) layer_info[i].channel_info[j].size);
1369 count=ReadBlob(image,4,(unsigned char *) type);
1370 ReversePSDString(image,type,4);
1371 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1373 if (image->debug != MagickFalse)
1374 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1375 " layer type was %.4s instead of 8BIM", type);
1376 layer_info=DestroyLayerInfo(layer_info,number_layers);
1377 ThrowBinaryException(CorruptImageError,"ImproperImageHeader",
1380 count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey);
1381 ReversePSDString(image,layer_info[i].blendkey,4);
1382 layer_info[i].opacity=(Quantum) ScaleCharToQuantum((unsigned char)
1383 ReadBlobByte(image));
1384 layer_info[i].clipping=(unsigned char) ReadBlobByte(image);
1385 layer_info[i].flags=(unsigned char) ReadBlobByte(image);
1386 layer_info[i].visible=!(layer_info[i].flags & 0x02);
1387 if (image->debug != MagickFalse)
1388 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1389 " blend=%.4s, opacity=%.20g, clipping=%s, flags=%d, visible=%s",
1390 layer_info[i].blendkey,(double) layer_info[i].opacity,
1391 layer_info[i].clipping ? "true" : "false",layer_info[i].flags,
1392 layer_info[i].visible ? "true" : "false");
1393 (void) ReadBlobByte(image); /* filler */
1395 size=ReadBlobLong(image);
1402 if (image->debug != MagickFalse)
1403 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1404 " layer contains additional info");
1405 length=ReadBlobLong(image);
1406 combined_length=length+4;
1412 layer_info[i].mask.page.y=(int) ReadBlobLong(image);
1413 layer_info[i].mask.page.x=(int) ReadBlobLong(image);
1414 layer_info[i].mask.page.height=(size_t) (ReadBlobLong(image)-
1415 layer_info[i].mask.page.y);
1416 layer_info[i].mask.page.width=(size_t) (ReadBlobLong(image)-
1417 layer_info[i].mask.page.x);
1418 layer_info[i].mask.background=(unsigned char) ReadBlobByte(
1420 layer_info[i].mask.flags=(unsigned char) ReadBlobByte(image);
1421 if (!(layer_info[i].mask.flags & 0x01))
1423 layer_info[i].mask.page.y=layer_info[i].mask.page.y-
1424 layer_info[i].page.y;
1425 layer_info[i].mask.page.x=layer_info[i].mask.page.x-
1426 layer_info[i].page.x;
1428 if (image->debug != MagickFalse)
1429 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1430 " layer mask: offset(%.20g,%.20g), size(%.20g,%.20g), length=%.20g",
1431 (double) layer_info[i].mask.page.x,(double)
1432 layer_info[i].mask.page.y,(double) layer_info[i].mask.page.width,
1433 (double) layer_info[i].mask.page.height,(double)
1434 ((MagickOffsetType) length)-18);
1436 Skip over the rest of the layer mask information.
1438 if (DiscardBlobBytes(image,(MagickSizeType) (length-18)) == MagickFalse)
1440 layer_info=DestroyLayerInfo(layer_info,number_layers);
1441 ThrowBinaryException(CorruptImageError,"UnexpectedEndOfFile",
1445 length=ReadBlobLong(image);
1446 combined_length+=length+4;
1450 Layer blending ranges info.
1452 if (image->debug != MagickFalse)
1453 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1454 " layer blending ranges: length=%.20g",(double)
1455 ((MagickOffsetType) length));
1457 We read it, but don't use it...
1459 for (j=0; j < (ssize_t) (length); j+=8)
1461 size_t blend_source=ReadBlobLong(image);
1462 size_t blend_dest=ReadBlobLong(image);
1463 if (image->debug != MagickFalse)
1464 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1465 " source(%x), dest(%x)",(unsigned int)
1466 blend_source,(unsigned int) blend_dest);
1472 length=(size_t) ReadBlobByte(image);
1473 combined_length+=length+1;
1475 (void) ReadBlob(image,(size_t) length++,layer_info[i].name);
1476 layer_info[i].name[length]='\0';
1477 if (image->debug != MagickFalse)
1478 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1479 " layer name: %s",layer_info[i].name);
1481 Skip the rest of the variable data until we support it.
1483 if (image->debug != MagickFalse)
1484 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1485 " unsupported data: length=%.20g",(double)
1486 ((MagickOffsetType) (size-combined_length)));
1487 if (DiscardBlobBytes(image,(MagickSizeType) (size-combined_length)) == MagickFalse)
1489 layer_info=DestroyLayerInfo(layer_info,number_layers);
1490 ThrowBinaryException(CorruptImageError,
1491 "UnexpectedEndOfFile",image->filename);
1496 for (i=0; i < number_layers; i++)
1498 if ((layer_info[i].page.width == 0) ||
1499 (layer_info[i].page.height == 0))
1501 if (image->debug != MagickFalse)
1502 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1503 " layer data is empty");
1508 Allocate layered image.
1510 layer_info[i].image=CloneImage(image,layer_info[i].page.width,
1511 layer_info[i].page.height,MagickFalse,exception);
1512 if (layer_info[i].image == (Image *) NULL)
1514 layer_info=DestroyLayerInfo(layer_info,number_layers);
1515 if (image->debug != MagickFalse)
1516 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1517 " allocation of image for layer %.20g failed",(double) i);
1518 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1523 if (image_info->ping == MagickFalse)
1525 for (i=0; i < number_layers; i++)
1527 if (layer_info[i].image == (Image *) NULL)
1529 for (j=0; j < layer_info[i].channels; j++)
1531 if (DiscardBlobBytes(image,(MagickSizeType)
1532 layer_info[i].channel_info[j].size) == MagickFalse)
1534 layer_info=DestroyLayerInfo(layer_info,number_layers);
1535 ThrowBinaryException(CorruptImageError,
1536 "UnexpectedEndOfFile",image->filename);
1542 if (image->debug != MagickFalse)
1543 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1544 " reading data for layer %.20g",(double) i);
1546 status=ReadPSDLayer(image,psd_info,&layer_info[i],exception);
1547 if (status == MagickFalse)
1550 status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType)
1552 if (status == MagickFalse)
1557 if (status != MagickFalse)
1559 for (i=0; i < number_layers; i++)
1561 if (layer_info[i].image == (Image *) NULL)
1563 for (j=i; j < number_layers - 1; j++)
1564 layer_info[j] = layer_info[j+1];
1570 if (number_layers > 0)
1572 for (i=0; i < number_layers; i++)
1575 layer_info[i].image->previous=layer_info[i-1].image;
1576 if (i < (number_layers-1))
1577 layer_info[i].image->next=layer_info[i+1].image;
1578 layer_info[i].image->page=layer_info[i].page;
1580 image->next=layer_info[0].image;
1581 layer_info[0].image->previous=image;
1584 layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info);
1590 static MagickStatusType ReadPSDMergedImage(Image* image,
1591 const PSDInfo* psd_info,ExceptionInfo *exception)
1605 compression=(PSDCompressionType) ReadBlobMSBShort(image);
1606 image->compression=ConvertPSDCompression(compression);
1608 if (compression != Raw && compression != RLE)
1610 (void) ThrowMagickException(exception,GetMagickModule(),
1611 TypeWarning,"CompressionNotSupported","'%.20g'",(double) compression);
1612 return(MagickFalse);
1615 offsets=(MagickOffsetType *) NULL;
1616 if (compression == RLE)
1618 offsets=ReadPSDRLEOffsets(image,psd_info,image->rows*psd_info->channels);
1619 if (offsets == (MagickOffsetType *) NULL)
1620 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1625 for (i=0; i < (ssize_t) psd_info->channels; i++)
1627 if (compression == RLE)
1628 status=ReadPSDChannelRLE(image,psd_info,i,offsets+(i*image->rows),
1631 status=ReadPSDChannelRaw(image,psd_info->channels,i,exception);
1633 if (status == MagickFalse)
1635 status=SetImageProgress(image,LoadImagesTag,i,psd_info->channels);
1636 if (status == MagickFalse)
1640 if (image->colorspace == CMYKColorspace)
1641 (void) NegateCMYK(image,exception);
1643 if (offsets != (MagickOffsetType *) NULL)
1644 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1649 static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception)
1682 assert(image_info != (const ImageInfo *) NULL);
1683 assert(image_info->signature == MagickCoreSignature);
1684 if (image_info->debug != MagickFalse)
1685 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1686 image_info->filename);
1687 assert(exception != (ExceptionInfo *) NULL);
1688 assert(exception->signature == MagickCoreSignature);
1690 image=AcquireImage(image_info,exception);
1691 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1692 if (status == MagickFalse)
1694 image=DestroyImageList(image);
1695 return((Image *) NULL);
1700 image->endian=MSBEndian;
1701 count=ReadBlob(image,4,(unsigned char *) psd_info.signature);
1702 psd_info.version=ReadBlobMSBShort(image);
1703 if ((count == 0) || (LocaleNCompare(psd_info.signature,"8BPS",4) != 0) ||
1704 ((psd_info.version != 1) && (psd_info.version != 2)))
1705 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1706 (void) ReadBlob(image,6,psd_info.reserved);
1707 psd_info.channels=ReadBlobMSBShort(image);
1708 if (psd_info.channels > MaxPSDChannels)
1709 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
1710 psd_info.rows=ReadBlobMSBLong(image);
1711 psd_info.columns=ReadBlobMSBLong(image);
1712 if ((psd_info.version == 1) && ((psd_info.rows > 30000) ||
1713 (psd_info.columns > 30000)))
1714 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1715 psd_info.depth=ReadBlobMSBShort(image);
1716 if ((psd_info.depth != 1) && (psd_info.depth != 8) && (psd_info.depth != 16))
1717 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1718 psd_info.mode=ReadBlobMSBShort(image);
1719 if (image->debug != MagickFalse)
1720 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1721 " Image is %.20g x %.20g with channels=%.20g, depth=%.20g, mode=%s",
1722 (double) psd_info.columns,(double) psd_info.rows,(double)
1723 psd_info.channels,(double) psd_info.depth,ModeToString((PSDImageType)
1728 image->depth=psd_info.depth;
1729 image->columns=psd_info.columns;
1730 image->rows=psd_info.rows;
1731 status=SetImageExtent(image,image->columns,image->rows,exception);
1732 if (status == MagickFalse)
1733 return(DestroyImageList(image));
1734 if (SetImageBackgroundColor(image,exception) == MagickFalse)
1736 image=DestroyImageList(image);
1737 return((Image *) NULL);
1739 if (psd_info.mode == LabMode)
1740 SetImageColorspace(image,LabColorspace,exception);
1741 if (psd_info.mode == CMYKMode)
1743 SetImageColorspace(image,CMYKColorspace,exception);
1744 image->alpha_trait=psd_info.channels > 4 ? BlendPixelTrait :
1745 UndefinedPixelTrait;
1747 else if ((psd_info.mode == BitmapMode) || (psd_info.mode == GrayscaleMode) ||
1748 (psd_info.mode == DuotoneMode))
1750 status=AcquireImageColormap(image,psd_info.depth != 16 ? 256 : 65536,
1752 if (status == MagickFalse)
1753 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1754 if (image->debug != MagickFalse)
1755 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1756 " Image colormap allocated");
1757 SetImageColorspace(image,GRAYColorspace,exception);
1758 image->alpha_trait=psd_info.channels > 1 ? BlendPixelTrait :
1759 UndefinedPixelTrait;
1762 image->alpha_trait=psd_info.channels > 3 ? BlendPixelTrait :
1763 UndefinedPixelTrait;
1765 Read PSD raster colormap only present for indexed and duotone images.
1767 length=ReadBlobMSBLong(image);
1770 if (image->debug != MagickFalse)
1771 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1772 " reading colormap");
1773 if (psd_info.mode == DuotoneMode)
1776 Duotone image data; the format of this data is undocumented.
1778 data=(unsigned char *) AcquireQuantumMemory((size_t) length,
1780 if (data == (unsigned char *) NULL)
1781 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1782 (void) ReadBlob(image,(size_t) length,data);
1783 data=(unsigned char *) RelinquishMagickMemory(data);
1791 Read PSD raster colormap.
1793 number_colors=length/3;
1794 if (number_colors > 65536)
1795 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1796 if (AcquireImageColormap(image,number_colors,exception) == MagickFalse)
1797 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1798 for (i=0; i < (ssize_t) image->colors; i++)
1799 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
1800 ReadBlobByte(image));
1801 for (i=0; i < (ssize_t) image->colors; i++)
1802 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
1803 ReadBlobByte(image));
1804 for (i=0; i < (ssize_t) image->colors; i++)
1805 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
1806 ReadBlobByte(image));
1807 image->alpha_trait=UndefinedPixelTrait;
1810 has_merged_image=MagickTrue;
1811 length=ReadBlobMSBLong(image);
1818 Image resources block.
1820 if (image->debug != MagickFalse)
1821 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1822 " reading image resource blocks - %.20g bytes",(double)
1823 ((MagickOffsetType) length));
1824 blocks=(unsigned char *) AcquireQuantumMemory((size_t) length,
1826 if (blocks == (unsigned char *) NULL)
1827 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1828 count=ReadBlob(image,(size_t) length,blocks);
1829 if ((count != (ssize_t) length) ||
1830 (LocaleNCompare((char *) blocks,"8BIM",4) != 0))
1832 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
1833 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1835 ParseImageResourceBlocks(image,blocks,(size_t) length,&has_merged_image,
1837 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
1840 Layer and mask block.
1842 length=GetPSDSize(&psd_info,image);
1845 length=ReadBlobMSBLong(image);
1846 length=ReadBlobMSBLong(image);
1848 offset=TellBlob(image);
1849 skip_layers=MagickFalse;
1850 if ((image_info->number_scenes == 1) && (image_info->scene == 0) &&
1851 (has_merged_image != MagickFalse))
1853 if (image->debug != MagickFalse)
1854 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1855 " read composite only");
1856 skip_layers=MagickTrue;
1860 if (image->debug != MagickFalse)
1861 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1862 " image has no layers");
1866 if (ReadPSDLayers(image,image_info,&psd_info,skip_layers,exception) !=
1869 (void) CloseBlob(image);
1870 image=DestroyImageList(image);
1871 return((Image *) NULL);
1875 Skip the rest of the layer and mask information.
1877 SeekBlob(image,offset+length,SEEK_SET);
1880 If we are only "pinging" the image, then we're done - so return.
1882 if (image_info->ping != MagickFalse)
1884 (void) CloseBlob(image);
1885 return(GetFirstImageInList(image));
1888 Read the precombined layer, present for PSD < 4 compatibility.
1890 if (image->debug != MagickFalse)
1891 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1892 " reading the precombined layer");
1893 if ((has_merged_image != MagickFalse) || (GetImageListLength(image) == 1))
1894 has_merged_image=(MagickBooleanType) ReadPSDMergedImage(image,&psd_info,
1896 if ((has_merged_image == MagickFalse) && (GetImageListLength(image) == 1) &&
1899 SeekBlob(image,offset,SEEK_SET);
1900 status=ReadPSDLayers(image,image_info,&psd_info,MagickFalse,exception);
1901 if (status != MagickTrue)
1903 (void) CloseBlob(image);
1904 return((Image *) NULL);
1907 if ((has_merged_image == MagickFalse) && (GetImageListLength(image) > 1))
1912 SetImageAlphaChannel(image,TransparentAlphaChannel,exception);
1913 image->background_color.alpha=TransparentAlpha;
1914 image->background_color.alpha_trait=BlendPixelTrait;
1915 merged=MergeImageLayers(image,FlattenLayer,exception);
1916 ReplaceImageInList(&image,merged);
1918 (void) CloseBlob(image);
1919 return(GetFirstImageInList(image));
1923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1927 % R e g i s t e r P S D I m a g e %
1931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1933 % RegisterPSDImage() adds properties for the PSD image format to
1934 % the list of supported formats. The properties include the image format
1935 % tag, a method to read and/or write the format, whether the format
1936 % supports the saving of more than one frame to the same file or blob,
1937 % whether the format supports native in-memory I/O, and a brief
1938 % description of the format.
1940 % The format of the RegisterPSDImage method is:
1942 % size_t RegisterPSDImage(void)
1945 ModuleExport size_t RegisterPSDImage(void)
1950 entry=AcquireMagickInfo("PSD","PSB","Adobe Large Document Format");
1951 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1952 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1953 entry->magick=(IsImageFormatHandler *) IsPSD;
1954 entry->flags|=CoderSeekableStreamFlag;
1955 (void) RegisterMagickInfo(entry);
1956 entry=AcquireMagickInfo("PSD","PSD","Adobe Photoshop bitmap");
1957 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1958 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1959 entry->magick=(IsImageFormatHandler *) IsPSD;
1960 entry->flags|=CoderSeekableStreamFlag;
1961 (void) RegisterMagickInfo(entry);
1962 return(MagickImageCoderSignature);
1966 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1970 % U n r e g i s t e r P S D I m a g e %
1974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1976 % UnregisterPSDImage() removes format registrations made by the
1977 % PSD module from the list of supported formats.
1979 % The format of the UnregisterPSDImage method is:
1981 % UnregisterPSDImage(void)
1984 ModuleExport void UnregisterPSDImage(void)
1986 (void) UnregisterMagickInfo("PSB");
1987 (void) UnregisterMagickInfo("PSD");
1991 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1995 % W r i t e P S D I m a g e %
1999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2001 % WritePSDImage() writes an image in the Adobe Photoshop encoded image format.
2003 % The format of the WritePSDImage method is:
2005 % MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
2006 % ExceptionInfo *exception)
2008 % A description of each parameter follows.
2010 % o image_info: the image info.
2012 % o image: The image.
2014 % o exception: return any errors or warnings in this structure.
2018 static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image,
2019 const size_t offset)
2021 if (psd_info->version == 1)
2022 return(WriteBlobMSBShort(image,(unsigned short) offset));
2023 return(WriteBlobMSBLong(image,(unsigned short) offset));
2026 static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image,
2027 const MagickSizeType size)
2029 if (psd_info->version == 1)
2030 return(WriteBlobMSBLong(image,(unsigned int) size));
2031 return(WriteBlobMSBLongLong(image,size));
2034 static size_t PSDPackbitsEncodeImage(Image *image,const size_t length,
2035 const unsigned char *pixels,unsigned char *compact_pixels,
2036 ExceptionInfo *exception)
2045 register unsigned char
2052 Compress pixels with Packbits encoding.
2054 assert(image != (Image *) NULL);
2055 assert(image->signature == MagickCoreSignature);
2056 if (image->debug != MagickFalse)
2057 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2058 assert(pixels != (unsigned char *) NULL);
2059 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
2060 if (packbits == (unsigned char *) NULL)
2061 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2064 for (i=(ssize_t) length; i != 0; )
2071 *q++=(unsigned char) 0;
2078 *q++=(unsigned char) 1;
2086 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
2088 *q++=(unsigned char) ((256-3)+1);
2092 *q++=(unsigned char) 2;
2100 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
2106 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
2113 *q++=(unsigned char) ((256-count)+1);
2122 while ((*(pixels+count) != *(pixels+count+1)) ||
2123 (*(pixels+count+1) != *(pixels+count+2)))
2125 packbits[count+1]=pixels[count];
2127 if (((ssize_t) count >= (i-3)) || (count >= 127))
2131 *packbits=(unsigned char) (count-1);
2132 for (j=0; j <= (ssize_t) count; j++)
2139 *q++=(unsigned char) 128; /* EOD marker */
2140 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
2141 return((size_t) (q-compact_pixels));
2144 static void WritePackbitsLength(const PSDInfo *psd_info,
2145 const ImageInfo *image_info,Image *image,Image *next_image,
2146 unsigned char *compact_pixels,const QuantumType quantum_type,
2147 ExceptionInfo *exception)
2152 register const Quantum
2165 if (next_image->depth > 8)
2166 next_image->depth=16;
2167 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
2169 quantum_info=AcquireQuantumInfo(image_info,image);
2170 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
2171 for (y=0; y < (ssize_t) next_image->rows; y++)
2173 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
2174 if (p == (const Quantum *) NULL)
2176 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
2177 quantum_type,pixels,exception);
2178 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
2180 (void) SetPSDOffset(psd_info,image,length);
2182 quantum_info=DestroyQuantumInfo(quantum_info);
2185 static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info,
2186 Image *image,Image *next_image,unsigned char *compact_pixels,
2187 const QuantumType quantum_type,const MagickBooleanType compression_flag,
2188 ExceptionInfo *exception)
2199 register const Quantum
2213 if ((compression_flag != MagickFalse) &&
2214 (next_image->compression != RLECompression))
2215 (void) WriteBlobMSBShort(image,0);
2216 if (next_image->depth > 8)
2217 next_image->depth=16;
2218 monochrome=IsImageMonochrome(image) && (image->depth == 1) ?
2219 MagickTrue : MagickFalse;
2220 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
2222 quantum_info=AcquireQuantumInfo(image_info,image);
2223 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
2224 for (y=0; y < (ssize_t) next_image->rows; y++)
2226 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
2227 if (p == (const Quantum *) NULL)
2229 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
2230 quantum_type,pixels,exception);
2231 if (monochrome != MagickFalse)
2232 for (i=0; i < (ssize_t) length; i++)
2233 pixels[i]=(~pixels[i]);
2234 if (next_image->compression != RLECompression)
2235 (void) WriteBlob(image,length,pixels);
2238 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
2240 (void) WriteBlob(image,length,compact_pixels);
2243 quantum_info=DestroyQuantumInfo(quantum_info);
2246 static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info,
2247 const ImageInfo *image_info,Image *image,Image *next_image,
2248 const MagickBooleanType separate,ExceptionInfo *exception)
2258 Write uncompressed pixels as separate planes.
2261 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
2262 compact_pixels=(unsigned char *) NULL;
2263 if (next_image->compression == RLECompression)
2265 compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels*
2266 next_image->columns,packet_size*sizeof(*compact_pixels));
2267 if (compact_pixels == (unsigned char *) NULL)
2268 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2270 if (IsImageGray(next_image) != MagickFalse)
2272 if (next_image->compression == RLECompression)
2275 Packbits compression.
2277 (void) WriteBlobMSBShort(image,1);
2278 WritePackbitsLength(psd_info,image_info,image,next_image,
2279 compact_pixels,GrayQuantum,exception);
2280 if (next_image->alpha_trait != UndefinedPixelTrait)
2281 WritePackbitsLength(psd_info,image_info,image,next_image,
2282 compact_pixels,AlphaQuantum,exception);
2284 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2285 GrayQuantum,MagickTrue,exception);
2286 if (next_image->alpha_trait != UndefinedPixelTrait)
2287 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2288 AlphaQuantum,separate,exception);
2289 (void) SetImageProgress(image,SaveImagesTag,0,1);
2292 if (next_image->storage_class == PseudoClass)
2294 if (next_image->compression == RLECompression)
2297 Packbits compression.
2299 (void) WriteBlobMSBShort(image,1);
2300 WritePackbitsLength(psd_info,image_info,image,next_image,
2301 compact_pixels,IndexQuantum,exception);
2302 if (next_image->alpha_trait != UndefinedPixelTrait)
2303 WritePackbitsLength(psd_info,image_info,image,next_image,
2304 compact_pixels,AlphaQuantum,exception);
2306 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2307 IndexQuantum,MagickTrue,exception);
2308 if (next_image->alpha_trait != UndefinedPixelTrait)
2309 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2310 AlphaQuantum,separate,exception);
2311 (void) SetImageProgress(image,SaveImagesTag,0,1);
2315 if (next_image->colorspace == CMYKColorspace)
2316 (void) NegateCMYK(next_image,exception);
2317 if (next_image->compression == RLECompression)
2320 Packbits compression.
2322 (void) WriteBlobMSBShort(image,1);
2323 WritePackbitsLength(psd_info,image_info,image,next_image,
2324 compact_pixels,RedQuantum,exception);
2325 WritePackbitsLength(psd_info,image_info,image,next_image,
2326 compact_pixels,GreenQuantum,exception);
2327 WritePackbitsLength(psd_info,image_info,image,next_image,
2328 compact_pixels,BlueQuantum,exception);
2329 if (next_image->colorspace == CMYKColorspace)
2330 WritePackbitsLength(psd_info,image_info,image,next_image,
2331 compact_pixels,BlackQuantum,exception);
2332 if (next_image->alpha_trait != UndefinedPixelTrait)
2333 WritePackbitsLength(psd_info,image_info,image,next_image,
2334 compact_pixels,AlphaQuantum,exception);
2336 (void) SetImageProgress(image,SaveImagesTag,0,6);
2337 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2338 RedQuantum,MagickTrue,exception);
2339 (void) SetImageProgress(image,SaveImagesTag,1,6);
2340 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2341 GreenQuantum,separate,exception);
2342 (void) SetImageProgress(image,SaveImagesTag,2,6);
2343 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2344 BlueQuantum,separate,exception);
2345 (void) SetImageProgress(image,SaveImagesTag,3,6);
2346 if (next_image->colorspace == CMYKColorspace)
2347 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2348 BlackQuantum,separate,exception);
2349 (void) SetImageProgress(image,SaveImagesTag,4,6);
2350 if (next_image->alpha_trait != UndefinedPixelTrait)
2351 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2352 AlphaQuantum,separate,exception);
2353 (void) SetImageProgress(image,SaveImagesTag,5,6);
2354 if (next_image->colorspace == CMYKColorspace)
2355 (void) NegateCMYK(next_image,exception);
2357 if (next_image->compression == RLECompression)
2358 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
2362 static void WritePascalString(Image* inImage,const char *inString,int inPad)
2373 length=(strlen(inString) > 255UL ) ? 255UL : strlen(inString);
2375 (void) WriteBlobByte(inImage,0);
2378 (void) WriteBlobByte(inImage,(unsigned char) length);
2379 (void) WriteBlob(inImage, length, (const unsigned char *) inString);
2382 if ((length % inPad) == 0)
2384 for (i=0; i < (ssize_t) (inPad-(length % inPad)); i++)
2385 (void) WriteBlobByte(inImage,0);
2388 static void WriteResolutionResourceBlock(Image *image)
2397 if (image->units == PixelsPerCentimeterResolution)
2399 x_resolution=2.54*65536.0*image->resolution.x+0.5;
2400 y_resolution=2.54*65536.0*image->resolution.y+0.5;
2405 x_resolution=65536.0*image->resolution.x+0.5;
2406 y_resolution=65536.0*image->resolution.y+0.5;
2409 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2410 (void) WriteBlobMSBShort(image,0x03ED);
2411 (void) WriteBlobMSBShort(image,0);
2412 (void) WriteBlobMSBLong(image,16); /* resource size */
2413 (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5));
2414 (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */
2415 (void) WriteBlobMSBShort(image,units); /* width unit */
2416 (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5));
2417 (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */
2418 (void) WriteBlobMSBShort(image,units); /* height unit */
2421 static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile)
2423 register const unsigned char
2440 length=GetStringInfoLength(bim_profile);
2443 datum=GetStringInfoDatum(bim_profile);
2444 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2446 register unsigned char
2449 q=(unsigned char *) p;
2450 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2452 p=PushLongPixel(MSBEndian,p,&long_sans);
2453 p=PushShortPixel(MSBEndian,p,&id);
2454 p=PushShortPixel(MSBEndian,p,&short_sans);
2455 p=PushLongPixel(MSBEndian,p,&count);
2456 if (id == 0x0000040f)
2458 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2459 (PSDQuantum(count)+12)-(q-datum));
2460 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2464 if ((count & 0x01) != 0)
2469 static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile)
2471 register const unsigned char
2488 length=GetStringInfoLength(bim_profile);
2491 datum=GetStringInfoDatum(bim_profile);
2492 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2494 register unsigned char
2497 q=(unsigned char *) p;
2498 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2500 p=PushLongPixel(MSBEndian,p,&long_sans);
2501 p=PushShortPixel(MSBEndian,p,&id);
2502 p=PushShortPixel(MSBEndian,p,&short_sans);
2503 p=PushLongPixel(MSBEndian,p,&count);
2504 if ((id == 0x000003ed) && (PSDQuantum(count) < (ssize_t) (length-12)))
2506 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2507 (PSDQuantum(count)+12)-(q-datum));
2508 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2512 if ((count & 0x01) != 0)
2517 static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
2518 ExceptionInfo *exception)
2547 rounded_layer_info_size;
2555 assert(image_info != (const ImageInfo *) NULL);
2556 assert(image_info->signature == MagickCoreSignature);
2557 assert(image != (Image *) NULL);
2558 assert(image->signature == MagickCoreSignature);
2559 if (image->debug != MagickFalse)
2560 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2561 assert(exception != (ExceptionInfo *) NULL);
2562 assert(exception->signature == MagickCoreSignature);
2563 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2564 if (status == MagickFalse)
2566 packet_size=(size_t) (image->depth > 8 ? 6 : 3);
2567 if (image->alpha_trait != UndefinedPixelTrait)
2568 packet_size+=image->depth > 8 ? 2 : 1;
2570 if ((LocaleCompare(image_info->magick,"PSB") == 0) ||
2571 (image->columns > 30000) || (image->rows > 30000))
2573 (void) WriteBlob(image,4,(const unsigned char *) "8BPS");
2574 (void) WriteBlobMSBShort(image,psd_info.version); /* version */
2575 for (i=1; i <= 6; i++)
2576 (void) WriteBlobByte(image, 0); /* 6 bytes of reserved */
2577 if (SetImageGray(image,exception) != MagickFalse)
2578 num_channels=(image->alpha_trait != UndefinedPixelTrait ? 2UL : 1UL);
2580 if ((image_info->type != TrueColorType) && (image_info->type !=
2581 TrueColorAlphaType) && (image->storage_class == PseudoClass))
2582 num_channels=(image->alpha_trait != UndefinedPixelTrait ? 2UL : 1UL);
2585 if (image->storage_class == PseudoClass)
2586 (void) SetImageStorageClass(image,DirectClass,exception);
2587 if (image->colorspace != CMYKColorspace)
2588 num_channels=(image->alpha_trait != UndefinedPixelTrait ? 4UL : 3UL);
2590 num_channels=(image->alpha_trait != UndefinedPixelTrait ? 5UL : 4UL);
2592 (void) WriteBlobMSBShort(image,(unsigned short) num_channels);
2593 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
2594 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
2595 if (IsImageGray(image) != MagickFalse)
2603 monochrome=IsImageMonochrome(image) && (image->depth == 1) ?
2604 MagickTrue : MagickFalse;
2605 (void) WriteBlobMSBShort(image,(unsigned short)
2606 (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8));
2607 (void) WriteBlobMSBShort(image,(unsigned short)
2608 (monochrome != MagickFalse ? BitmapMode : GrayscaleMode));
2612 (void) WriteBlobMSBShort(image,(unsigned short) (image->storage_class ==
2613 PseudoClass ? 8 : image->depth > 8 ? 16 : 8));
2615 if (((image_info->colorspace != UndefinedColorspace) ||
2616 (image->colorspace != CMYKColorspace)) &&
2617 (image_info->colorspace != CMYKColorspace))
2619 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2620 (void) WriteBlobMSBShort(image,(unsigned short)
2621 (image->storage_class == PseudoClass ? IndexedMode : RGBMode));
2625 if (image->colorspace != CMYKColorspace)
2626 (void) TransformImageColorspace(image,CMYKColorspace,exception);
2627 (void) WriteBlobMSBShort(image,CMYKMode);
2630 if ((IsImageGray(image) != MagickFalse) ||
2631 (image->storage_class == DirectClass) || (image->colors > 256))
2632 (void) WriteBlobMSBLong(image,0);
2636 Write PSD raster colormap.
2638 (void) WriteBlobMSBLong(image,768);
2639 for (i=0; i < (ssize_t) image->colors; i++)
2640 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red));
2641 for ( ; i < 256; i++)
2642 (void) WriteBlobByte(image,0);
2643 for (i=0; i < (ssize_t) image->colors; i++)
2644 (void) WriteBlobByte(image,ScaleQuantumToChar(
2645 image->colormap[i].green));
2646 for ( ; i < 256; i++)
2647 (void) WriteBlobByte(image,0);
2648 for (i=0; i < (ssize_t) image->colors; i++)
2649 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue));
2650 for ( ; i < 256; i++)
2651 (void) WriteBlobByte(image,0);
2654 Image resource block.
2656 length=28; /* 0x03EB */
2657 bim_profile=(StringInfo *) GetImageProfile(image,"8bim");
2658 icc_profile=GetImageProfile(image,"icc");
2659 if (bim_profile != (StringInfo *) NULL)
2661 bim_profile=CloneStringInfo(bim_profile);
2662 if (icc_profile != (StringInfo *) NULL)
2663 RemoveICCProfileFromResourceBlock(bim_profile);
2664 RemoveResolutionFromResourceBlock(bim_profile);
2665 length+=PSDQuantum(GetStringInfoLength(bim_profile));
2667 if (icc_profile != (const StringInfo *) NULL)
2668 length+=PSDQuantum(GetStringInfoLength(icc_profile))+12;
2669 (void) WriteBlobMSBLong(image,(unsigned int) length);
2670 WriteResolutionResourceBlock(image);
2671 if (bim_profile != (StringInfo *) NULL)
2673 (void) WriteBlob(image,GetStringInfoLength(bim_profile),
2674 GetStringInfoDatum(bim_profile));
2675 bim_profile=DestroyStringInfo(bim_profile);
2677 if (icc_profile != (StringInfo *) NULL)
2679 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2680 (void) WriteBlobMSBShort(image,0x0000040F);
2681 (void) WriteBlobMSBShort(image,0);
2682 (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength(
2684 (void) WriteBlob(image,GetStringInfoLength(icc_profile),
2685 GetStringInfoDatum(icc_profile));
2686 if ((MagickOffsetType) GetStringInfoLength(icc_profile) !=
2687 PSDQuantum(GetStringInfoLength(icc_profile)))
2688 (void) WriteBlobByte(image,0);
2692 base_image=GetNextImageInList(image);
2693 if ((image->alpha_trait != UndefinedPixelTrait) && (base_image == (Image *) NULL))
2695 next_image=base_image;
2696 while ( next_image != NULL )
2698 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2699 if (IsImageGray(next_image) != MagickFalse)
2700 num_channels=next_image->alpha_trait != UndefinedPixelTrait ? 2UL : 1UL;
2702 if (next_image->storage_class == PseudoClass)
2703 num_channels=next_image->alpha_trait != UndefinedPixelTrait ? 2UL : 1UL;
2705 if (next_image->colorspace != CMYKColorspace)
2706 num_channels=next_image->alpha_trait != UndefinedPixelTrait ? 4UL : 3UL;
2708 num_channels=next_image->alpha_trait != UndefinedPixelTrait ? 5UL : 4UL;
2709 channelLength=(size_t) (next_image->columns*next_image->rows*packet_size+2);
2710 layer_info_size+=(size_t) (4*4+2+num_channels*6+(psd_info.version == 1 ? 8 :
2711 16)+4*1+4+num_channels*channelLength);
2712 property=(const char *) GetImageProperty(next_image,"label",exception);
2713 if (property == (const char *) NULL)
2714 layer_info_size+=16;
2720 layer_length=strlen(property);
2721 layer_info_size+=8+layer_length+(4-(layer_length % 4));
2724 next_image=GetNextImageInList(next_image);
2726 if (layer_count == 0)
2727 (void) SetPSDSize(&psd_info,image,0);
2733 (void) SetPSDSize(&psd_info,image,layer_info_size+
2734 (psd_info.version == 1 ? 8 : 16));
2735 if ((layer_info_size/2) != ((layer_info_size+1)/2))
2736 rounded_layer_info_size=layer_info_size+1;
2738 rounded_layer_info_size=layer_info_size;
2739 (void) SetPSDSize(&psd_info,image,rounded_layer_info_size);
2740 if (image->alpha_trait != UndefinedPixelTrait)
2741 (void) WriteBlobMSBShort(image,-(unsigned short) layer_count);
2743 (void) WriteBlobMSBShort(image,(unsigned short) layer_count);
2745 compression=base_image->compression;
2746 for (next_image=base_image; next_image != NULL; )
2748 next_image->compression=NoCompression;
2749 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y);
2750 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x);
2751 (void) WriteBlobMSBLong(image,(unsigned int) (next_image->page.y+
2753 (void) WriteBlobMSBLong(image,(unsigned int) (next_image->page.x+
2754 next_image->columns));
2755 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2756 channel_size=(unsigned int) ((packet_size*next_image->rows*
2757 next_image->columns)+2);
2758 if ((IsImageGray(next_image) != MagickFalse) ||
2759 (next_image->storage_class == PseudoClass))
2761 (void) WriteBlobMSBShort(image,(unsigned short)
2762 (next_image->alpha_trait != UndefinedPixelTrait ? 2 : 1));
2763 (void) WriteBlobMSBShort(image,0);
2764 (void) SetPSDSize(&psd_info,image,channel_size);
2765 if (next_image->alpha_trait != UndefinedPixelTrait)
2767 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2768 (void) SetPSDSize(&psd_info,image,channel_size);
2772 if (next_image->colorspace != CMYKColorspace)
2774 (void) WriteBlobMSBShort(image,(unsigned short)
2775 (next_image->alpha_trait != UndefinedPixelTrait ? 4 : 3));
2776 (void) WriteBlobMSBShort(image,0);
2777 (void) SetPSDSize(&psd_info,image,channel_size);
2778 (void) WriteBlobMSBShort(image,1);
2779 (void) SetPSDSize(&psd_info,image,channel_size);
2780 (void) WriteBlobMSBShort(image,2);
2781 (void) SetPSDSize(&psd_info,image,channel_size);
2782 if (next_image->alpha_trait != UndefinedPixelTrait)
2784 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2785 (void) SetPSDSize(&psd_info,image,channel_size);
2790 (void) WriteBlobMSBShort(image,(unsigned short)
2791 (next_image->alpha_trait ? 5 : 4));
2792 (void) WriteBlobMSBShort(image,0);
2793 (void) SetPSDSize(&psd_info,image,channel_size);
2794 (void) WriteBlobMSBShort(image,1);
2795 (void) SetPSDSize(&psd_info,image,channel_size);
2796 (void) WriteBlobMSBShort(image,2);
2797 (void) SetPSDSize(&psd_info,image,channel_size);
2798 (void) WriteBlobMSBShort(image,3);
2799 (void) SetPSDSize(&psd_info,image,channel_size);
2800 if (next_image->alpha_trait)
2802 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2803 (void) SetPSDSize(&psd_info,image,channel_size);
2806 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2807 (void) WriteBlob(image,4,(const unsigned char *)
2808 CompositeOperatorToPSDBlendMode(next_image->compose));
2809 (void) WriteBlobByte(image,255); /* layer opacity */
2810 (void) WriteBlobByte(image,0);
2811 (void) WriteBlobByte(image,next_image->compose==NoCompositeOp ?
2812 1 << 0x02 : 1); /* layer properties - visible, etc. */
2813 (void) WriteBlobByte(image,0);
2814 property=(const char *) GetImageProperty(next_image,"label",exception);
2815 if (property == (const char *) NULL)
2818 layer_name[MagickPathExtent];
2820 (void) WriteBlobMSBLong(image,16);
2821 (void) WriteBlobMSBLong(image,0);
2822 (void) WriteBlobMSBLong(image,0);
2823 (void) FormatLocaleString(layer_name,MagickPathExtent,"L%04ld",(long)
2825 WritePascalString(image,layer_name,4);
2832 label_length=strlen(property);
2833 (void) WriteBlobMSBLong(image,(unsigned int) (label_length+(4-
2834 (label_length % 4))+8));
2835 (void) WriteBlobMSBLong(image,0);
2836 (void) WriteBlobMSBLong(image,0);
2837 WritePascalString(image,property,4);
2839 next_image=GetNextImageInList(next_image);
2844 next_image=base_image;
2845 while (next_image != NULL)
2847 status=WriteImageChannels(&psd_info,image_info,image,next_image,
2848 MagickTrue,exception);
2849 next_image=GetNextImageInList(next_image);
2851 (void) WriteBlobMSBLong(image,0); /* user mask data */
2852 base_image->compression=compression;
2855 Write composite image.
2857 if (status != MagickFalse)
2858 status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse,
2860 (void) CloseBlob(image);