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
82 #define MaxPSDChannels 56
83 #define PSDQuantum(x) (((ssize_t) (x)+1) & -2)
86 Enumerated declaractions.
92 ZipWithoutPrediction = 2,
103 MultichannelMode = 7,
109 Typedef declaractions.
111 typedef struct _ChannelInfo
120 typedef struct _LayerInfo
130 channel_info[MaxPSDChannels];
154 typedef struct _PSDInfo
176 Forward declarations.
178 static MagickBooleanType
179 WritePSDImage(const ImageInfo *,Image *,ExceptionInfo *);
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 % IsPSD()() returns MagickTrue if the image format type, identified by the
193 % magick string, is PSD.
195 % The format of the IsPSD method is:
197 % MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
199 % A description of each parameter follows:
201 % o magick: compare image format pattern against these bytes.
203 % o length: Specifies the length of the magick string.
206 static MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
210 if (LocaleNCompare((const char *) magick,"8BPS",4) == 0)
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 % R e a d P S D I m a g e %
224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 % ReadPSDImage() reads an Adobe Photoshop image file and returns it. It
227 % allocates the memory necessary for the new Image structure and returns a
228 % pointer to the new image.
230 % The format of the ReadPSDImage method is:
232 % Image *ReadPSDImage(image_info)
234 % A description of each parameter follows:
236 % o image_info: the image info.
238 % o exception: return any errors or warnings in this structure.
242 static const char *CompositeOperatorToPSDBlendMode(CompositeOperator op)
249 case ColorBurnCompositeOp: blend_mode = "idiv"; break;
250 case ColorDodgeCompositeOp: blend_mode = "div "; break;
251 case ColorizeCompositeOp: blend_mode = "colr"; break;
252 case DarkenCompositeOp: blend_mode = "dark"; break;
253 case DifferenceCompositeOp: blend_mode = "diff"; break;
254 case DissolveCompositeOp: blend_mode = "diss"; break;
255 case ExclusionCompositeOp: blend_mode = "smud"; break;
256 case HardLightCompositeOp: blend_mode = "hLit"; break;
257 case HardMixCompositeOp: blend_mode = "hMix"; break;
258 case HueCompositeOp: blend_mode = "hue "; break;
259 case LightenCompositeOp: blend_mode = "lite"; break;
260 case LinearBurnCompositeOp: blend_mode = "lbrn"; break;
261 case LinearDodgeCompositeOp:blend_mode = "lddg"; break;
262 case LinearLightCompositeOp:blend_mode = "lLit"; break;
263 case LuminizeCompositeOp: blend_mode = "lum "; break;
264 case MultiplyCompositeOp: blend_mode = "mul "; break;
265 case OverCompositeOp: blend_mode = "norm"; break;
266 case OverlayCompositeOp: blend_mode = "over"; break;
267 case PinLightCompositeOp: blend_mode = "pLit"; break;
268 case SaturateCompositeOp: blend_mode = "sat "; break;
269 case ScreenCompositeOp: blend_mode = "scrn"; break;
270 case SoftLightCompositeOp: blend_mode = "sLit"; break;
271 case VividLightCompositeOp: blend_mode = "vLit"; break;
272 default: blend_mode = "norm";
277 static inline CompressionType ConvertPSDCompression(
278 PSDCompressionType compression)
283 return RLECompression;
284 case ZipWithPrediction:
285 case ZipWithoutPrediction:
286 return ZipCompression;
288 return NoCompression;
292 static MagickStatusType CorrectPSDOpacity(LayerInfo* layer_info,
293 ExceptionInfo *exception)
304 if (layer_info->opacity == OpaqueAlpha)
307 layer_info->image->alpha_trait=BlendPixelTrait;
308 for (y=0; y < (ssize_t) layer_info->image->rows; y++)
310 q=GetAuthenticPixels(layer_info->image,0,y,layer_info->image->columns,1,
312 if (q == (Quantum *) NULL)
314 for (x=0; x < (ssize_t) layer_info->image->columns; x++)
316 SetPixelAlpha(layer_info->image,(Quantum) (QuantumScale*(GetPixelAlpha(
317 layer_info->image,q))*layer_info->opacity),q);
318 q+=GetPixelChannels(layer_info->image);
320 if (SyncAuthenticPixels(layer_info->image,exception) == MagickFalse)
327 static ssize_t DecodePSDPixels(const size_t number_compact_pixels,
328 const unsigned char *compact_pixels,const ssize_t depth,
329 const size_t number_pixels,unsigned char *pixels)
331 #define CheckNumberCompactPixels \
336 #define CheckNumberPixels(count) \
337 if (((ssize_t) i + count) > (ssize_t) number_pixels) \
354 packets=(ssize_t) number_compact_pixels;
355 for (i=0; (packets > 1) && (i < (ssize_t) number_pixels); )
358 length=(size_t) (*compact_pixels++);
364 CheckNumberCompactPixels;
365 pixel=(*compact_pixels++);
366 for (j=0; j < (ssize_t) length; j++)
372 CheckNumberPixels(8);
373 *pixels++=(pixel >> 7) & 0x01 ? 0U : 255U;
374 *pixels++=(pixel >> 6) & 0x01 ? 0U : 255U;
375 *pixels++=(pixel >> 5) & 0x01 ? 0U : 255U;
376 *pixels++=(pixel >> 4) & 0x01 ? 0U : 255U;
377 *pixels++=(pixel >> 3) & 0x01 ? 0U : 255U;
378 *pixels++=(pixel >> 2) & 0x01 ? 0U : 255U;
379 *pixels++=(pixel >> 1) & 0x01 ? 0U : 255U;
380 *pixels++=(pixel >> 0) & 0x01 ? 0U : 255U;
385 CheckNumberPixels(4);
386 *pixels++=(unsigned char) ((pixel >> 6) & 0x03);
387 *pixels++=(unsigned char) ((pixel >> 4) & 0x03);
388 *pixels++=(unsigned char) ((pixel >> 2) & 0x03);
389 *pixels++=(unsigned char) ((pixel & 0x03) & 0x03);
394 CheckNumberPixels(2);
395 *pixels++=(unsigned char) ((pixel >> 4) & 0xff);
396 *pixels++=(unsigned char) ((pixel & 0x0f) & 0xff);
401 CheckNumberPixels(1);
402 *pixels++=(unsigned char) pixel;
410 for (j=0; j < (ssize_t) length; j++)
416 CheckNumberPixels(8);
417 *pixels++=(*compact_pixels >> 7) & 0x01 ? 0U : 255U;
418 *pixels++=(*compact_pixels >> 6) & 0x01 ? 0U : 255U;
419 *pixels++=(*compact_pixels >> 5) & 0x01 ? 0U : 255U;
420 *pixels++=(*compact_pixels >> 4) & 0x01 ? 0U : 255U;
421 *pixels++=(*compact_pixels >> 3) & 0x01 ? 0U : 255U;
422 *pixels++=(*compact_pixels >> 2) & 0x01 ? 0U : 255U;
423 *pixels++=(*compact_pixels >> 1) & 0x01 ? 0U : 255U;
424 *pixels++=(*compact_pixels >> 0) & 0x01 ? 0U : 255U;
429 CheckNumberPixels(4);
430 *pixels++=(*compact_pixels >> 6) & 0x03;
431 *pixels++=(*compact_pixels >> 4) & 0x03;
432 *pixels++=(*compact_pixels >> 2) & 0x03;
433 *pixels++=(*compact_pixels & 0x03) & 0x03;
438 CheckNumberPixels(2);
439 *pixels++=(*compact_pixels >> 4) & 0xff;
440 *pixels++=(*compact_pixels & 0x0f) & 0xff;
445 CheckNumberPixels(1);
446 *pixels++=(*compact_pixels);
450 CheckNumberCompactPixels;
457 static inline LayerInfo *DestroyLayerInfo(LayerInfo *layer_info,
458 const ssize_t number_layers)
463 for (i=0; i<number_layers; i++)
465 if (layer_info[i].image != (Image *) NULL)
466 layer_info[i].image=DestroyImage(layer_info[i].image);
469 return (LayerInfo *) RelinquishMagickMemory(layer_info);
472 static inline size_t GetPSDPacketSize(Image *image)
474 if (image->storage_class == PseudoClass)
476 if (image->colors > 256)
478 else if (image->depth > 8)
482 if (image->depth > 8)
488 static inline MagickSizeType GetPSDSize(const PSDInfo *psd_info,Image *image)
490 if (psd_info->version == 1)
491 return((MagickSizeType) ReadBlobMSBLong(image));
492 return((MagickSizeType) ReadBlobMSBLongLong(image));
495 static inline size_t GetPSDRowSize(Image *image)
497 if (image->depth == 1)
498 return((image->columns+7)/8);
500 return(image->columns*GetPSDPacketSize(image));
503 static const char *ModeToString(PSDImageType type)
507 case BitmapMode: return "Bitmap";
508 case GrayscaleMode: return "Grayscale";
509 case IndexedMode: return "Indexed";
510 case RGBMode: return "RGB";
511 case CMYKMode: return "CMYK";
512 case MultichannelMode: return "Multichannel";
513 case DuotoneMode: return "Duotone";
514 case LabMode: return "L*A*B";
515 default: return "unknown";
519 static void NegateCMYK(Image *image,ExceptionInfo *exception)
524 channel_mask=SetImageChannelMask(image,(ChannelType)(AllChannels &~
526 NegateImage(image,MagickFalse,exception);
527 (void) SetImageChannelMask(image,channel_mask);
530 static void ParseImageResourceBlocks(Image *image,
531 const unsigned char *blocks,size_t length,
532 MagickBooleanType *has_merged_image,ExceptionInfo *exception)
550 profile=BlobToStringInfo((const void *) NULL,length);
551 SetStringInfoDatum(profile,blocks);
552 (void) SetImageProfile(image,"8bim",profile,exception);
553 profile=DestroyStringInfo(profile);
554 for (p=blocks; (p >= blocks) && (p < (blocks+length-16)); )
556 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
558 p=PushLongPixel(MSBEndian,p,&long_sans);
559 p=PushShortPixel(MSBEndian,p,&id);
560 p=PushShortPixel(MSBEndian,p,&short_sans);
561 p=PushLongPixel(MSBEndian,p,&count);
562 if (p+count > blocks+length)
569 value[MaxTextExtent];
577 p=PushShortPixel(MSBEndian,p,&resolution);
578 image->resolution.x=(double) resolution;
579 (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.x);
580 (void) SetImageProperty(image,"tiff:XResolution",value,exception);
581 p=PushShortPixel(MSBEndian,p,&short_sans);
582 p=PushShortPixel(MSBEndian,p,&short_sans);
583 p=PushShortPixel(MSBEndian,p,&short_sans);
584 p=PushShortPixel(MSBEndian,p,&resolution);
585 image->resolution.y=(double) resolution;
586 (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.y);
587 (void) SetImageProperty(image,"tiff:YResolution",value,exception);
588 p=PushShortPixel(MSBEndian,p,&short_sans);
589 p=PushShortPixel(MSBEndian,p,&short_sans);
590 p=PushShortPixel(MSBEndian,p,&short_sans);
591 image->units=PixelsPerInchResolution;
597 *has_merged_image=MagickFalse;
607 if ((count & 0x01) != 0)
613 static CompositeOperator PSDBlendModeToCompositeOperator(const char *mode)
615 if (mode == (const char *) NULL)
616 return(OverCompositeOp);
617 if (LocaleNCompare(mode,"norm",4) == 0)
618 return(OverCompositeOp);
619 if (LocaleNCompare(mode,"mul ",4) == 0)
620 return(MultiplyCompositeOp);
621 if (LocaleNCompare(mode,"diss",4) == 0)
622 return(DissolveCompositeOp);
623 if (LocaleNCompare(mode,"diff",4) == 0)
624 return(DifferenceCompositeOp);
625 if (LocaleNCompare(mode,"dark",4) == 0)
626 return(DarkenCompositeOp);
627 if (LocaleNCompare(mode,"lite",4) == 0)
628 return(LightenCompositeOp);
629 if (LocaleNCompare(mode,"hue ",4) == 0)
630 return(HueCompositeOp);
631 if (LocaleNCompare(mode,"sat ",4) == 0)
632 return(SaturateCompositeOp);
633 if (LocaleNCompare(mode,"colr",4) == 0)
634 return(ColorizeCompositeOp);
635 if (LocaleNCompare(mode,"lum ",4) == 0)
636 return(LuminizeCompositeOp);
637 if (LocaleNCompare(mode,"scrn",4) == 0)
638 return(ScreenCompositeOp);
639 if (LocaleNCompare(mode,"over",4) == 0)
640 return(OverlayCompositeOp);
641 if (LocaleNCompare(mode,"hLit",4) == 0)
642 return(HardLightCompositeOp);
643 if (LocaleNCompare(mode,"sLit",4) == 0)
644 return(SoftLightCompositeOp);
645 if (LocaleNCompare(mode,"smud",4) == 0)
646 return(ExclusionCompositeOp);
647 if (LocaleNCompare(mode,"div ",4) == 0)
648 return(ColorDodgeCompositeOp);
649 if (LocaleNCompare(mode,"idiv",4) == 0)
650 return(ColorBurnCompositeOp);
651 if (LocaleNCompare(mode,"lbrn",4) == 0)
652 return(LinearBurnCompositeOp);
653 if (LocaleNCompare(mode,"lddg",4) == 0)
654 return(LinearDodgeCompositeOp);
655 if (LocaleNCompare(mode,"lLit",4) == 0)
656 return(LinearLightCompositeOp);
657 if (LocaleNCompare(mode,"vLit",4) == 0)
658 return(VividLightCompositeOp);
659 if (LocaleNCompare(mode,"pLit",4) == 0)
660 return(PinLightCompositeOp);
661 if (LocaleNCompare(mode,"hMix",4) == 0)
662 return(HardMixCompositeOp);
663 return(OverCompositeOp);
666 static MagickStatusType ReadPSDChannelPixels(Image *image,
667 const size_t channels,const size_t row,const ssize_t type,
668 const unsigned char *pixels,ExceptionInfo *exception)
673 register const unsigned char
689 q=GetAuthenticPixels(image,0,row,image->columns,1,exception);
690 if (q == (Quantum *) NULL)
692 packet_size=GetPSDPacketSize(image);
693 for (x=0; x < (ssize_t) image->columns; x++)
695 if (packet_size == 1)
696 pixel=ScaleCharToQuantum(*p++);
699 p=PushShortPixel(MSBEndian,p,&nibble);
700 pixel=ScaleShortToQuantum(nibble);
706 SetPixelAlpha(image,pixel,q);
711 SetPixelRed(image,pixel,q);
713 SetPixelGray(image,pixel,q);
714 if (image->storage_class == PseudoClass)
716 if (packet_size == 1)
717 SetPixelIndex(image,ScaleQuantumToChar(pixel),q);
719 SetPixelIndex(image,ScaleQuantumToShort(pixel),q);
720 SetPixelViaPixelInfo(image,image->colormap+(ssize_t)
721 ConstrainColormapIndex(image,GetPixelIndex(image,q),exception),q);
722 if (image->depth == 1)
728 number_bits=image->columns-x;
731 for (bit=0; bit < number_bits; bit++)
733 SetPixelIndex(image,(((unsigned char) pixel) &
734 (0x01 << (7-bit))) != 0 ? 0 : 255,q);
735 SetPixelViaPixelInfo(image,image->colormap+(ssize_t)
736 GetPixelIndex(image,q),q);
737 q+=GetPixelChannels(image);
748 if (image->storage_class == PseudoClass)
749 SetPixelAlpha(image,pixel,q);
751 SetPixelGreen(image,pixel,q);
756 if (image->storage_class == PseudoClass)
757 SetPixelAlpha(image,pixel,q);
759 SetPixelBlue(image,pixel,q);
764 if (image->colorspace == CMYKColorspace)
765 SetPixelBlack(image,pixel,q);
767 if (image->alpha_trait != UndefinedPixelTrait)
768 SetPixelAlpha(image,pixel,q);
773 if ((IssRGBCompatibleColorspace(image->colorspace) != MagickFalse) &&
776 if (image->alpha_trait != UndefinedPixelTrait)
777 SetPixelAlpha(image,pixel,q);
783 q+=GetPixelChannels(image);
785 return(SyncAuthenticPixels(image,exception));
788 static MagickStatusType ReadPSDChannelRaw(Image *image,const size_t channels,
789 const ssize_t type,ExceptionInfo *exception)
804 if (image->debug != MagickFalse)
805 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
806 " layer data is RAW");
808 row_size=GetPSDRowSize(image);
809 pixels=(unsigned char *) AcquireQuantumMemory(row_size,sizeof(*pixels));
810 if (pixels == (unsigned char *) NULL)
811 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
815 for (y=0; y < (ssize_t) image->rows; y++)
819 count=ReadBlob(image,row_size,pixels);
820 if (count != row_size)
823 status=ReadPSDChannelPixels(image,channels,y,type,pixels,exception);
824 if (status == MagickFalse)
828 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
832 static inline MagickOffsetType *ReadPSDRLEOffsets(Image *image,
833 const PSDInfo *psd_info,const size_t size)
841 offsets=(MagickOffsetType *) AcquireQuantumMemory(size,sizeof(*offsets));
842 if(offsets != (MagickOffsetType *) NULL)
844 for (y=0; y < (ssize_t) size; y++)
846 if (psd_info->version == 1)
847 offsets[y]=(MagickOffsetType) ReadBlobMSBShort(image);
849 offsets[y]=(MagickOffsetType) ReadBlobMSBLong(image);
855 static MagickStatusType ReadPSDChannelRLE(Image *image,const PSDInfo *psd_info,
856 const ssize_t type,MagickOffsetType *offsets,ExceptionInfo *exception)
873 if (image->debug != MagickFalse)
874 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
875 " layer data is RLE compressed");
877 row_size=GetPSDRowSize(image);
878 pixels=(unsigned char *) AcquireQuantumMemory(row_size,sizeof(*pixels));
879 if (pixels == (unsigned char *) NULL)
880 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
884 for (y=0; y < (ssize_t) image->rows; y++)
885 if ((MagickOffsetType) length < offsets[y])
886 length=(size_t) offsets[y];
888 if (length > row_size + 256) // arbitrary number
890 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
891 ThrowBinaryException(ResourceLimitError,"InvalidLength",
895 compact_pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
896 if (compact_pixels == (unsigned char *) NULL)
898 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
899 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
903 (void) ResetMagickMemory(compact_pixels,0,length*sizeof(*compact_pixels));
906 for (y=0; y < (ssize_t) image->rows; y++)
910 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
911 if (count != (ssize_t) offsets[y])
914 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
915 (ssize_t) (image->depth == 1 ? 123456 : image->depth),row_size,pixels);
916 if (count != (ssize_t) row_size)
919 status=ReadPSDChannelPixels(image,psd_info->channels,y,type,pixels,
921 if (status == MagickFalse)
925 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
926 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
930 #ifdef MAGICKCORE_ZLIB_DELEGATE
931 static MagickStatusType ReadPSDChannelZip(Image *image,const size_t channels,
932 const ssize_t type,const PSDCompressionType compression,
933 const size_t compact_size,ExceptionInfo *exception)
938 register unsigned char
957 if (image->debug != MagickFalse)
958 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
959 " layer data is ZIP compressed");
961 compact_pixels=(unsigned char *) AcquireQuantumMemory(compact_size,
962 sizeof(*compact_pixels));
963 if (compact_pixels == (unsigned char *) NULL)
964 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
967 packet_size=GetPSDPacketSize(image);
968 row_size=image->columns*packet_size;
969 count=image->rows*row_size;
971 pixels=(unsigned char *) AcquireQuantumMemory(count,sizeof(*pixels));
972 if (pixels == (unsigned char *) NULL)
974 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
975 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
979 ResetMagickMemory(&stream, 0, sizeof(z_stream));
980 stream.data_type=Z_BINARY;
981 (void) ReadBlob(image,compact_size,compact_pixels);
983 stream.next_in=(Bytef *)compact_pixels;
984 stream.avail_in=(unsigned int) compact_size;
985 stream.next_out=(Bytef *)pixels;
986 stream.avail_out=(unsigned int) count;
988 if(inflateInit(&stream) == Z_OK)
993 while (stream.avail_out > 0)
995 ret=inflate(&stream, Z_SYNC_FLUSH);
996 if (ret != Z_OK && ret != Z_STREAM_END)
998 compact_pixels=(unsigned char *) RelinquishMagickMemory(
1000 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1001 return(MagickFalse);
1006 if (compression == ZipWithPrediction)
1011 length=image->columns;
1014 if (packet_size == 2)
1016 p[2]+=p[0]+((p[1]+p[3]) >> 8);
1030 for (y=0; y < (ssize_t) image->rows; y++)
1032 status=ReadPSDChannelPixels(image,channels,y,type,p,exception);
1033 if (status == MagickFalse)
1039 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
1040 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1045 static MagickStatusType ReadPSDChannel(Image *image,const PSDInfo *psd_info,
1046 const LayerInfo* layer_info,const size_t channel,
1047 const PSDCompressionType compression,ExceptionInfo *exception)
1055 if (layer_info->channel_info[channel].type < -1)
1057 /* ignore user supplied layer mask */
1058 SeekBlob(image,layer_info->channel_info[channel].size-2,SEEK_CUR);
1062 offset=TellBlob(image);
1067 return(ReadPSDChannelRaw(image,psd_info->channels,
1068 layer_info->channel_info[channel].type,exception));
1074 offsets=ReadPSDRLEOffsets(image,psd_info,image->rows);
1075 if (offsets == (MagickOffsetType *) NULL)
1076 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1078 status=ReadPSDChannelRLE(image,psd_info,
1079 layer_info->channel_info[channel].type,offsets,exception);
1080 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1083 case ZipWithPrediction:
1084 case ZipWithoutPrediction:
1085 #ifdef MAGICKCORE_ZLIB_DELEGATE
1086 status=ReadPSDChannelZip(image,layer_info->channels,
1087 layer_info->channel_info[channel].type,compression,
1088 layer_info->channel_info[channel].size-2,exception);
1090 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1091 (void) ThrowMagickException(exception,GetMagickModule(),
1092 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn",
1093 "'%s' (ZLIB)",image->filename);
1097 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1098 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
1099 "CompressionNotSupported","'%.20g'",(double) compression);
1103 if (status == MagickFalse)
1104 SeekBlob(image,offset+layer_info->channel_info[channel].size-2,SEEK_SET);
1109 static MagickStatusType ReadPSDLayer(Image *image,const PSDInfo *psd_info,
1110 LayerInfo* layer_info,ExceptionInfo *exception)
1113 message[MaxTextExtent];
1124 if (image->debug != MagickFalse)
1125 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1126 " setting up new layer image");
1127 (void) SetImageBackgroundColor(layer_info->image,exception);
1128 layer_info->image->compose=PSDBlendModeToCompositeOperator(
1129 layer_info->blendkey);
1130 if (layer_info->visible == MagickFalse)
1131 layer_info->image->compose=NoCompositeOp;
1132 if (psd_info->mode == CMYKMode)
1133 SetImageColorspace(layer_info->image,CMYKColorspace,exception);
1134 if ((psd_info->mode == BitmapMode) || (psd_info->mode == GrayscaleMode) ||
1135 (psd_info->mode == DuotoneMode))
1136 SetImageColorspace(layer_info->image,GRAYColorspace,exception);
1138 Set up some hidden attributes for folks that need them.
1140 (void) FormatLocaleString(message,MaxTextExtent,"%.20gld",
1141 (double) layer_info->page.x);
1142 (void) SetImageArtifact(layer_info->image,"psd:layer.x",message);
1143 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",
1144 (double) layer_info->page.y);
1145 (void) SetImageArtifact(layer_info->image,"psd:layer.y",message);
1146 (void) FormatLocaleString(message,MaxTextExtent,"%.20g",(double)
1147 layer_info->opacity);
1148 (void) SetImageArtifact(layer_info->image,"psd:layer.opacity",message);
1149 (void) SetImageProperty(layer_info->image,"label",(char *) layer_info->name,
1153 for (j=0; j < (ssize_t) layer_info->channels; j++)
1155 if (image->debug != MagickFalse)
1156 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1157 " reading data for channel %.20g",(double) j);
1159 compression=(PSDCompressionType) ReadBlobMSBShort(layer_info->image);
1160 layer_info->image->compression=ConvertPSDCompression(compression);
1161 if (layer_info->channel_info[j].type == -1)
1162 layer_info->image->alpha_trait=BlendPixelTrait;
1164 status=ReadPSDChannel(layer_info->image,psd_info,layer_info,j,
1165 compression,exception);
1167 if (status == MagickFalse)
1171 if (status != MagickFalse)
1172 status=CorrectPSDOpacity(layer_info,exception);
1174 if (status != MagickFalse && layer_info->image->colorspace == CMYKColorspace)
1175 (void) NegateCMYK(layer_info->image,exception);
1180 static MagickStatusType ReadPSDLayers(Image *image,const ImageInfo *image_info,
1181 const PSDInfo *psd_info,const MagickBooleanType skip_layers,
1182 ExceptionInfo *exception)
1204 size=GetPSDSize(psd_info,image);
1211 Skip layers & masks.
1213 quantum=psd_info->version == 1 ? 4UL : 8UL;
1214 (void) ReadBlobMSBLong(image);
1215 count=ReadBlob(image,4,(unsigned char *) type);
1216 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1218 if (DiscardBlobBytes(image,(MagickSizeType) (size-quantum-8)) ==
1220 ThrowBinaryException(CorruptImageError,"UnexpectedEndOfFile",
1225 count=ReadBlob(image,4,(unsigned char *) type);
1226 if ((count != 0) && (LocaleNCompare(type,"Lr16",4) == 0))
1227 size=GetPSDSize(psd_info,image);
1229 if (DiscardBlobBytes(image,(MagickSizeType) (size-quantum-12UL)) ==
1231 ThrowBinaryException(CorruptImageError,"UnexpectedEndOfFile",
1238 layer_info=(LayerInfo *) NULL;
1239 number_layers=(short) ReadBlobMSBShort(image);
1241 if (number_layers < 0)
1244 The first alpha channel in the merged result contains the
1245 transparency data for the merged result.
1247 number_layers=MagickAbsoluteValue(number_layers);
1248 if (image->debug != MagickFalse)
1249 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1250 " negative layer count corrected for");
1251 image->alpha_trait=BlendPixelTrait;
1254 if (skip_layers != MagickFalse)
1257 if (image->debug != MagickFalse)
1258 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1259 " image contains %.20g layers",(double) number_layers);
1261 if (number_layers == 0)
1262 ThrowBinaryException(CorruptImageError,"InvalidNumberOfLayers",
1265 layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers,
1266 sizeof(*layer_info));
1267 if (layer_info == (LayerInfo *) NULL)
1269 if (image->debug != MagickFalse)
1270 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1271 " allocation of LayerInfo failed");
1272 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1275 (void) ResetMagickMemory(layer_info,0,(size_t) number_layers*
1276 sizeof(*layer_info));
1278 for (i=0; i < number_layers; i++)
1284 if (image->debug != MagickFalse)
1285 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1286 " reading layer #%.20g",(double) i+1);
1287 layer_info[i].page.y=(int) ReadBlobMSBLong(image);
1288 layer_info[i].page.x=(int) ReadBlobMSBLong(image);
1289 y=(int) ReadBlobMSBLong(image);
1290 x=(int) ReadBlobMSBLong(image);
1291 layer_info[i].page.width=(ssize_t) (x-layer_info[i].page.x);
1292 layer_info[i].page.height=(ssize_t) (y-layer_info[i].page.y);
1293 layer_info[i].channels=ReadBlobMSBShort(image);
1294 if (layer_info[i].channels > MaxPSDChannels)
1296 layer_info=DestroyLayerInfo(layer_info,number_layers);
1297 ThrowBinaryException(CorruptImageError,"MaximumChannelsExceeded",
1300 if (image->debug != MagickFalse)
1301 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1302 " offset(%.20g,%.20g), size(%.20g,%.20g), channels=%.20g",
1303 (double) layer_info[i].page.x,(double) layer_info[i].page.y,
1304 (double) layer_info[i].page.height,(double)
1305 layer_info[i].page.width,(double) layer_info[i].channels);
1306 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1308 layer_info[i].channel_info[j].type=(short) ReadBlobMSBShort(image);
1309 layer_info[i].channel_info[j].size=(size_t) GetPSDSize(psd_info,
1311 if (image->debug != MagickFalse)
1312 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1313 " channel[%.20g]: type=%.20g, size=%.20g",(double) j,
1314 (double) layer_info[i].channel_info[j].type,
1315 (double) layer_info[i].channel_info[j].size);
1317 count=ReadBlob(image,4,(unsigned char *) type);
1318 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1320 if (image->debug != MagickFalse)
1321 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1322 " layer type was %.4s instead of 8BIM", type);
1323 layer_info=DestroyLayerInfo(layer_info,number_layers);
1324 ThrowBinaryException(CorruptImageError,"ImproperImageHeader",
1327 count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey);
1328 layer_info[i].opacity=(Quantum) ScaleCharToQuantum((unsigned char)
1329 ReadBlobByte(image));
1330 layer_info[i].clipping=(unsigned char) ReadBlobByte(image);
1331 layer_info[i].flags=(unsigned char) ReadBlobByte(image);
1332 layer_info[i].visible=!(layer_info[i].flags & 0x02);
1333 if (image->debug != MagickFalse)
1334 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1335 " blend=%.4s, opacity=%.20g, clipping=%s, flags=%d, visible=%s",
1336 layer_info[i].blendkey,(double) layer_info[i].opacity,
1337 layer_info[i].clipping ? "true" : "false",layer_info[i].flags,
1338 layer_info[i].visible ? "true" : "false");
1339 (void) ReadBlobByte(image); /* filler */
1341 size=ReadBlobMSBLong(image);
1348 if (image->debug != MagickFalse)
1349 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1350 " layer contains additional info");
1351 length=ReadBlobMSBLong(image);
1352 combined_length=length+4;
1358 layer_info[i].mask.y=(int) ReadBlobMSBLong(image);
1359 layer_info[i].mask.x=(int) ReadBlobMSBLong(image);
1360 layer_info[i].mask.height=(size_t)
1361 (ReadBlobMSBLong(image)-layer_info[i].mask.y);
1362 layer_info[i].mask.width=(size_t)
1363 (ReadBlobMSBLong(image)-layer_info[i].mask.x);
1364 if (image->debug != MagickFalse)
1365 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1366 " layer mask: offset(%.20g,%.20g), size(%.20g,%.20g), length=%.20g",
1367 (double) layer_info[i].mask.x,(double)
1368 layer_info[i].mask.y,(double) layer_info[i].mask.width,
1369 (double) layer_info[i].mask.height,(double)
1370 ((MagickOffsetType) length)-16);
1372 Skip over the rest of the layer mask information.
1374 if (DiscardBlobBytes(image,(MagickSizeType) (length-16)) == MagickFalse)
1376 layer_info=DestroyLayerInfo(layer_info,number_layers);
1377 ThrowBinaryException(CorruptImageError,"UnexpectedEndOfFile",
1381 length=ReadBlobMSBLong(image);
1382 combined_length+=length+4;
1386 Layer blending ranges info.
1388 if (image->debug != MagickFalse)
1389 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1390 " layer blending ranges: length=%.20g",(double)
1391 ((MagickOffsetType) length));
1393 We read it, but don't use it...
1395 for (j=0; j < (ssize_t) (length); j+=8)
1397 size_t blend_source=ReadBlobMSBLong(image);
1398 size_t blend_dest=ReadBlobMSBLong(image);
1399 if (image->debug != MagickFalse)
1400 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1401 " source(%x), dest(%x)",(unsigned int)
1402 blend_source,(unsigned int) blend_dest);
1408 length=(size_t) ReadBlobByte(image);
1409 combined_length+=length+1;
1410 for (j=0; j < (ssize_t) length; j++)
1411 layer_info[i].name[j]=(unsigned char) ReadBlobByte(image);
1412 layer_info[i].name[j]='\0';
1413 if (image->debug != MagickFalse)
1414 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1415 " layer name: %s",layer_info[i].name);
1417 Skip the rest of the variable data until we support it.
1419 if (image->debug != MagickFalse)
1420 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1421 " unsupported data: length=%.20g",(double)
1422 ((MagickOffsetType) (size-combined_length)));
1423 if (DiscardBlobBytes(image,(MagickSizeType) (size-combined_length)) == MagickFalse)
1425 layer_info=DestroyLayerInfo(layer_info,number_layers);
1426 ThrowBinaryException(CorruptImageError,
1427 "UnexpectedEndOfFile",image->filename);
1432 for (i=0; i < number_layers; i++)
1434 if ((layer_info[i].page.width == 0) ||
1435 (layer_info[i].page.height == 0))
1437 if (image->debug != MagickFalse)
1438 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1439 " layer data is empty");
1444 Allocate layered image.
1446 layer_info[i].image=CloneImage(image,layer_info[i].page.width,
1447 layer_info[i].page.height,MagickFalse,exception);
1448 if (layer_info[i].image == (Image *) NULL)
1450 layer_info=DestroyLayerInfo(layer_info,number_layers);
1451 if (image->debug != MagickFalse)
1452 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1453 " allocation of image for layer %.20g failed",(double) i);
1454 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1459 if (image_info->ping == MagickFalse)
1461 for (i=0; i < number_layers; i++)
1463 if (layer_info[i].image == (Image *) NULL)
1465 for (j=0; j < layer_info[i].channels; j++)
1467 if (DiscardBlobBytes(image,(MagickSizeType)
1468 layer_info[i].channel_info[j].size) == MagickFalse)
1470 layer_info=DestroyLayerInfo(layer_info,number_layers);
1471 ThrowBinaryException(CorruptImageError,
1472 "UnexpectedEndOfFile",image->filename);
1478 if (image->debug != MagickFalse)
1479 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1480 " reading data for layer %.20g",(double) i);
1482 status=ReadPSDLayer(image,psd_info,&layer_info[i],exception);
1483 if (status == MagickFalse)
1486 status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType)
1488 if (status == MagickFalse)
1493 if (status != MagickFalse)
1495 for (i=0; i < number_layers; i++)
1497 if (layer_info[i].image == (Image *) NULL)
1499 for (j=i; j < number_layers - 1; j++)
1500 layer_info[j] = layer_info[j+1];
1506 if (number_layers > 0)
1508 for (i=0; i < number_layers; i++)
1511 layer_info[i].image->previous=layer_info[i-1].image;
1512 if (i < (number_layers-1))
1513 layer_info[i].image->next=layer_info[i+1].image;
1514 layer_info[i].image->page=layer_info[i].page;
1516 image->next=layer_info[0].image;
1517 layer_info[0].image->previous=image;
1520 layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info);
1526 static MagickStatusType ReadPSDMergedImage(Image* image,
1527 const PSDInfo* psd_info,ExceptionInfo *exception)
1541 compression=(PSDCompressionType) ReadBlobMSBShort(image);
1542 image->compression=ConvertPSDCompression(compression);
1544 if (compression != Raw && compression != RLE)
1546 (void) ThrowMagickException(exception,GetMagickModule(),
1547 TypeWarning,"CompressionNotSupported","'%.20g'",(double) compression);
1548 return(MagickFalse);
1551 offsets=(MagickOffsetType *) NULL;
1552 if (compression == RLE)
1554 offsets=ReadPSDRLEOffsets(image,psd_info,image->rows*psd_info->channels);
1555 if (offsets == (MagickOffsetType *) NULL)
1556 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1561 for (i=0; i < (ssize_t) psd_info->channels; i++)
1563 if (compression == RLE)
1564 status=ReadPSDChannelRLE(image,psd_info,i,offsets+(i*image->rows),
1567 status=ReadPSDChannelRaw(image,psd_info->channels,i,exception);
1569 if (status == MagickFalse)
1571 status=SetImageProgress(image,LoadImagesTag,i,psd_info->channels);
1572 if (status == MagickFalse)
1576 if (image->colorspace == CMYKColorspace)
1577 (void) NegateCMYK(image,exception);
1579 if (offsets != (MagickOffsetType *) NULL)
1580 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1585 static Image *ReadPSDImage(const ImageInfo *image_info,
1586 ExceptionInfo *exception)
1617 assert(image_info != (const ImageInfo *) NULL);
1618 assert(image_info->signature == MagickSignature);
1619 if (image_info->debug != MagickFalse)
1620 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1621 image_info->filename);
1622 assert(exception != (ExceptionInfo *) NULL);
1623 assert(exception->signature == MagickSignature);
1625 image=AcquireImage(image_info,exception);
1626 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1627 if (status == MagickFalse)
1629 image=DestroyImageList(image);
1630 return((Image *) NULL);
1635 count=ReadBlob(image,4,(unsigned char *) psd_info.signature);
1636 psd_info.version=ReadBlobMSBShort(image);
1637 if ((count == 0) || (LocaleNCompare(psd_info.signature,"8BPS",4) != 0) ||
1638 ((psd_info.version != 1) && (psd_info.version != 2)))
1639 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1640 count=ReadBlob(image,6,psd_info.reserved);
1641 psd_info.channels=ReadBlobMSBShort(image);
1642 if (psd_info.channels > MaxPSDChannels)
1643 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
1644 psd_info.rows=ReadBlobMSBLong(image);
1645 psd_info.columns=ReadBlobMSBLong(image);
1646 if ((psd_info.version == 1) && ((psd_info.rows > 30000) ||
1647 (psd_info.columns > 30000)))
1648 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1649 psd_info.depth=ReadBlobMSBShort(image);
1650 if ((psd_info.depth != 1) && (psd_info.depth != 8) && (psd_info.depth != 16))
1651 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1652 psd_info.mode=ReadBlobMSBShort(image);
1653 if (image->debug != MagickFalse)
1654 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1655 " Image is %.20g x %.20g with channels=%.20g, depth=%.20g, mode=%s",
1656 (double) psd_info.columns,(double) psd_info.rows,(double)
1657 psd_info.channels,(double) psd_info.depth,ModeToString((PSDImageType)
1662 image->depth=psd_info.depth;
1663 image->columns=psd_info.columns;
1664 image->rows=psd_info.rows;
1665 status=SetImageExtent(image,image->columns,image->rows,exception);
1666 if (status == MagickFalse)
1667 return(DestroyImageList(image));
1668 if (SetImageBackgroundColor(image,exception) == MagickFalse)
1670 image=DestroyImageList(image);
1671 return((Image *) NULL);
1673 if (psd_info.mode == LabMode)
1674 SetImageColorspace(image,LabColorspace,exception);
1675 if (psd_info.mode == CMYKMode)
1677 SetImageColorspace(image,CMYKColorspace,exception);
1678 image->alpha_trait=psd_info.channels > 4 ? BlendPixelTrait :
1679 UndefinedPixelTrait;
1681 else if ((psd_info.mode == BitmapMode) || (psd_info.mode == GrayscaleMode) ||
1682 (psd_info.mode == DuotoneMode))
1684 status=AcquireImageColormap(image,psd_info.depth != 16 ? 256 : 65536,
1686 if (status == MagickFalse)
1687 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1688 if (image->debug != MagickFalse)
1689 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1690 " Image colormap allocated");
1691 SetImageColorspace(image,GRAYColorspace,exception);
1692 image->alpha_trait=psd_info.channels > 1 ? BlendPixelTrait :
1693 UndefinedPixelTrait;
1696 image->alpha_trait=psd_info.channels > 3 ? BlendPixelTrait :
1697 UndefinedPixelTrait;
1699 Read PSD raster colormap only present for indexed and duotone images.
1701 length=ReadBlobMSBLong(image);
1704 if (image->debug != MagickFalse)
1705 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1706 " reading colormap");
1707 if (psd_info.mode == DuotoneMode)
1710 Duotone image data; the format of this data is undocumented.
1712 data=(unsigned char *) AcquireQuantumMemory((size_t) length,
1714 if (data == (unsigned char *) NULL)
1715 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1716 count=ReadBlob(image,(size_t) length,data);
1717 data=(unsigned char *) RelinquishMagickMemory(data);
1725 Read PSD raster colormap.
1727 number_colors=length/3;
1728 if (number_colors > 65536)
1729 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1730 if (AcquireImageColormap(image,number_colors,exception) == MagickFalse)
1731 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1732 for (i=0; i < (ssize_t) image->colors; i++)
1733 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
1734 ReadBlobByte(image));
1735 for (i=0; i < (ssize_t) image->colors; i++)
1736 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
1737 ReadBlobByte(image));
1738 for (i=0; i < (ssize_t) image->colors; i++)
1739 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
1740 ReadBlobByte(image));
1741 image->alpha_trait=UndefinedPixelTrait;
1744 has_merged_image=MagickTrue;
1745 length=ReadBlobMSBLong(image);
1752 Image resources block.
1754 if (image->debug != MagickFalse)
1755 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1756 " reading image resource blocks - %.20g bytes",(double)
1757 ((MagickOffsetType) length));
1758 blocks=(unsigned char *) AcquireQuantumMemory((size_t) length,
1760 if (blocks == (unsigned char *) NULL)
1761 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1762 count=ReadBlob(image,(size_t) length,blocks);
1763 if ((count != (ssize_t) length) ||
1764 (LocaleNCompare((char *) blocks,"8BIM",4) != 0))
1766 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
1767 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1769 ParseImageResourceBlocks(image,blocks,(size_t) length,&has_merged_image,
1771 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
1774 Layer and mask block.
1776 length=GetPSDSize(&psd_info,image);
1779 length=ReadBlobMSBLong(image);
1780 length=ReadBlobMSBLong(image);
1782 offset=TellBlob(image);
1783 skip_layers=MagickFalse;
1784 if ((image_info->number_scenes == 1) && (image_info->scene == 0) &&
1785 (has_merged_image != MagickFalse))
1787 if (image->debug != MagickFalse)
1788 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1789 " read composite only");
1790 skip_layers=MagickTrue;
1794 if (image->debug != MagickFalse)
1795 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1796 " image has no layers");
1800 if (ReadPSDLayers(image,image_info,&psd_info,skip_layers,exception) !=
1803 (void) CloseBlob(image);
1804 image=DestroyImageList(image);
1805 return((Image *) NULL);
1809 Skip the rest of the layer and mask information.
1811 SeekBlob(image,offset+length,SEEK_SET);
1814 If we are only "pinging" the image, then we're done - so return.
1816 if (image_info->ping != MagickFalse)
1818 (void) CloseBlob(image);
1819 return(GetFirstImageInList(image));
1822 Read the precombined layer, present for PSD < 4 compatibility.
1824 if (image->debug != MagickFalse)
1825 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1826 " reading the precombined layer");
1827 if (has_merged_image != MagickFalse || GetImageListLength(image) == 1)
1828 has_merged_image=(MagickBooleanType) ReadPSDMergedImage(image,&psd_info,
1830 if (has_merged_image == MagickFalse && GetImageListLength(image) == 1 &&
1833 SeekBlob(image,offset,SEEK_SET);
1834 if (ReadPSDLayers(image,image_info,&psd_info,MagickFalse,exception) !=
1837 (void) CloseBlob(image);
1838 return((Image *) NULL);
1841 if (has_merged_image == MagickFalse && GetImageListLength(image) > 1)
1846 SetImageAlphaChannel(image,TransparentAlphaChannel,exception);
1847 image->background_color.alpha=TransparentAlpha;
1848 merged=MergeImageLayers(image,FlattenLayer,exception);
1849 ReplaceImageInList(&image,merged);
1851 (void) CloseBlob(image);
1852 return(GetFirstImageInList(image));
1856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1860 % R e g i s t e r P S D I m a g e %
1864 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1866 % RegisterPSDImage() adds properties for the PSD image format to
1867 % the list of supported formats. The properties include the image format
1868 % tag, a method to read and/or write the format, whether the format
1869 % supports the saving of more than one frame to the same file or blob,
1870 % whether the format supports native in-memory I/O, and a brief
1871 % description of the format.
1873 % The format of the RegisterPSDImage method is:
1875 % size_t RegisterPSDImage(void)
1878 ModuleExport size_t RegisterPSDImage(void)
1883 entry=SetMagickInfo("PSB");
1884 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1885 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1886 entry->magick=(IsImageFormatHandler *) IsPSD;
1887 entry->flags|=CoderSeekableStreamFlag;
1888 entry->description=ConstantString("Adobe Large Document Format");
1889 entry->module=ConstantString("PSD");
1890 (void) RegisterMagickInfo(entry);
1891 entry=SetMagickInfo("PSD");
1892 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1893 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1894 entry->magick=(IsImageFormatHandler *) IsPSD;
1895 entry->flags|=CoderSeekableStreamFlag;
1896 entry->description=ConstantString("Adobe Photoshop bitmap");
1897 entry->module=ConstantString("PSD");
1898 (void) RegisterMagickInfo(entry);
1899 return(MagickImageCoderSignature);
1903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1907 % U n r e g i s t e r P S D I m a g e %
1911 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1913 % UnregisterPSDImage() removes format registrations made by the
1914 % PSD module from the list of supported formats.
1916 % The format of the UnregisterPSDImage method is:
1918 % UnregisterPSDImage(void)
1921 ModuleExport void UnregisterPSDImage(void)
1923 (void) UnregisterMagickInfo("PSB");
1924 (void) UnregisterMagickInfo("PSD");
1928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1932 % W r i t e P S D I m a g e %
1936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1938 % WritePSDImage() writes an image in the Adobe Photoshop encoded image format.
1940 % The format of the WritePSDImage method is:
1942 % MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
1943 % ExceptionInfo *exception)
1945 % A description of each parameter follows.
1947 % o image_info: the image info.
1949 % o image: The image.
1951 % o exception: return any errors or warnings in this structure.
1955 static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image,
1956 const size_t offset)
1958 if (psd_info->version == 1)
1959 return(WriteBlobMSBShort(image,(unsigned short) offset));
1960 return(WriteBlobMSBLong(image,(unsigned short) offset));
1963 static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image,
1964 const MagickSizeType size)
1966 if (psd_info->version == 1)
1967 return(WriteBlobMSBLong(image,(unsigned int) size));
1968 return(WriteBlobMSBLongLong(image,size));
1971 static size_t PSDPackbitsEncodeImage(Image *image,const size_t length,
1972 const unsigned char *pixels,unsigned char *compact_pixels,
1973 ExceptionInfo *exception)
1982 register unsigned char
1989 Compress pixels with Packbits encoding.
1991 assert(image != (Image *) NULL);
1992 assert(image->signature == MagickSignature);
1993 if (image->debug != MagickFalse)
1994 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1995 assert(pixels != (unsigned char *) NULL);
1996 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1997 if (packbits == (unsigned char *) NULL)
1998 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2001 for (i=(ssize_t) length; i != 0; )
2008 *q++=(unsigned char) 0;
2015 *q++=(unsigned char) 1;
2023 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
2025 *q++=(unsigned char) ((256-3)+1);
2029 *q++=(unsigned char) 2;
2037 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
2043 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
2050 *q++=(unsigned char) ((256-count)+1);
2059 while ((*(pixels+count) != *(pixels+count+1)) ||
2060 (*(pixels+count+1) != *(pixels+count+2)))
2062 packbits[count+1]=pixels[count];
2064 if (((ssize_t) count >= (i-3)) || (count >= 127))
2068 *packbits=(unsigned char) (count-1);
2069 for (j=0; j <= (ssize_t) count; j++)
2076 *q++=(unsigned char) 128; /* EOD marker */
2077 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
2078 return((size_t) (q-compact_pixels));
2081 static void WritePackbitsLength(const PSDInfo *psd_info,
2082 const ImageInfo *image_info,Image *image,Image *next_image,
2083 unsigned char *compact_pixels,const QuantumType quantum_type,
2084 ExceptionInfo *exception)
2089 register const Quantum
2102 if (next_image->depth > 8)
2103 next_image->depth=16;
2104 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
2106 quantum_info=AcquireQuantumInfo(image_info,image);
2107 pixels=GetQuantumPixels(quantum_info);
2108 for (y=0; y < (ssize_t) next_image->rows; y++)
2110 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
2111 if (p == (const Quantum *) NULL)
2113 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
2114 quantum_type,pixels,exception);
2115 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
2117 (void) SetPSDOffset(psd_info,image,length);
2119 quantum_info=DestroyQuantumInfo(quantum_info);
2122 static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info,
2123 Image *image,Image *next_image,unsigned char *compact_pixels,
2124 const QuantumType quantum_type,const MagickBooleanType compression_flag,
2125 ExceptionInfo *exception)
2136 register const Quantum
2150 if ((compression_flag != MagickFalse) &&
2151 (next_image->compression != RLECompression))
2152 (void) WriteBlobMSBShort(image,0);
2153 if (next_image->depth > 8)
2154 next_image->depth=16;
2155 monochrome=IsImageMonochrome(image) && (image->depth == 1) ?
2156 MagickTrue : MagickFalse;
2157 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
2159 quantum_info=AcquireQuantumInfo(image_info,image);
2160 pixels=GetQuantumPixels(quantum_info);
2161 for (y=0; y < (ssize_t) next_image->rows; y++)
2163 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
2164 if (p == (const Quantum *) NULL)
2166 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
2167 quantum_type,pixels,exception);
2168 if (monochrome != MagickFalse)
2169 for (i=0; i < (ssize_t) length; i++)
2170 pixels[i]=(~pixels[i]);
2171 if (next_image->compression != RLECompression)
2172 (void) WriteBlob(image,length,pixels);
2175 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
2177 (void) WriteBlob(image,length,compact_pixels);
2180 quantum_info=DestroyQuantumInfo(quantum_info);
2183 static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info,
2184 const ImageInfo *image_info,Image *image,Image *next_image,
2185 const MagickBooleanType separate,ExceptionInfo *exception)
2195 Write uncompressed pixels as separate planes.
2198 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
2199 compact_pixels=(unsigned char *) NULL;
2200 if (next_image->compression == RLECompression)
2202 compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels*
2203 next_image->columns,packet_size*sizeof(*compact_pixels));
2204 if (compact_pixels == (unsigned char *) NULL)
2205 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2207 if (IsImageGray(next_image) != MagickFalse)
2209 if (next_image->compression == RLECompression)
2212 Packbits compression.
2214 (void) WriteBlobMSBShort(image,1);
2215 WritePackbitsLength(psd_info,image_info,image,next_image,
2216 compact_pixels,GrayQuantum,exception);
2217 if (next_image->alpha_trait != UndefinedPixelTrait)
2218 WritePackbitsLength(psd_info,image_info,image,next_image,
2219 compact_pixels,AlphaQuantum,exception);
2221 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2222 GrayQuantum,MagickTrue,exception);
2223 if (next_image->alpha_trait != UndefinedPixelTrait)
2224 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2225 AlphaQuantum,separate,exception);
2226 (void) SetImageProgress(image,SaveImagesTag,0,1);
2229 if (next_image->storage_class == PseudoClass)
2231 if (next_image->compression == RLECompression)
2234 Packbits compression.
2236 (void) WriteBlobMSBShort(image,1);
2237 WritePackbitsLength(psd_info,image_info,image,next_image,
2238 compact_pixels,IndexQuantum,exception);
2239 if (next_image->alpha_trait != UndefinedPixelTrait)
2240 WritePackbitsLength(psd_info,image_info,image,next_image,
2241 compact_pixels,AlphaQuantum,exception);
2243 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2244 IndexQuantum,MagickTrue,exception);
2245 if (next_image->alpha_trait != UndefinedPixelTrait)
2246 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2247 AlphaQuantum,separate,exception);
2248 (void) SetImageProgress(image,SaveImagesTag,0,1);
2252 if (next_image->colorspace == CMYKColorspace)
2253 (void) NegateCMYK(next_image,exception);
2254 if (next_image->compression == RLECompression)
2257 Packbits compression.
2259 (void) WriteBlobMSBShort(image,1);
2260 WritePackbitsLength(psd_info,image_info,image,next_image,
2261 compact_pixels,RedQuantum,exception);
2262 WritePackbitsLength(psd_info,image_info,image,next_image,
2263 compact_pixels,GreenQuantum,exception);
2264 WritePackbitsLength(psd_info,image_info,image,next_image,
2265 compact_pixels,BlueQuantum,exception);
2266 if (next_image->colorspace == CMYKColorspace)
2267 WritePackbitsLength(psd_info,image_info,image,next_image,
2268 compact_pixels,BlackQuantum,exception);
2269 if (next_image->alpha_trait != UndefinedPixelTrait)
2270 WritePackbitsLength(psd_info,image_info,image,next_image,
2271 compact_pixels,AlphaQuantum,exception);
2273 (void) SetImageProgress(image,SaveImagesTag,0,6);
2274 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2275 RedQuantum,MagickTrue,exception);
2276 (void) SetImageProgress(image,SaveImagesTag,1,6);
2277 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2278 GreenQuantum,separate,exception);
2279 (void) SetImageProgress(image,SaveImagesTag,2,6);
2280 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2281 BlueQuantum,separate,exception);
2282 (void) SetImageProgress(image,SaveImagesTag,3,6);
2283 if (next_image->colorspace == CMYKColorspace)
2284 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2285 BlackQuantum,separate,exception);
2286 (void) SetImageProgress(image,SaveImagesTag,4,6);
2287 if (next_image->alpha_trait != UndefinedPixelTrait)
2288 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
2289 AlphaQuantum,separate,exception);
2290 (void) SetImageProgress(image,SaveImagesTag,5,6);
2291 if (next_image->colorspace == CMYKColorspace)
2292 (void) NegateCMYK(next_image,exception);
2294 if (next_image->compression == RLECompression)
2295 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
2299 static void WritePascalString(Image* inImage,const char *inString,int inPad)
2310 length=(strlen(inString) > 255UL ) ? 255UL : strlen(inString);
2312 (void) WriteBlobByte(inImage,0);
2315 (void) WriteBlobByte(inImage,(unsigned char) length);
2316 (void) WriteBlob(inImage, length, (const unsigned char *) inString);
2319 if ((length % inPad) == 0)
2321 for (i=0; i < (ssize_t) (inPad-(length % inPad)); i++)
2322 (void) WriteBlobByte(inImage,0);
2325 static void WriteResolutionResourceBlock(Image *image)
2334 if (image->units == PixelsPerCentimeterResolution)
2336 x_resolution=2.54*65536.0*image->resolution.x+0.5;
2337 y_resolution=2.54*65536.0*image->resolution.y+0.5;
2342 x_resolution=65536.0*image->resolution.x+0.5;
2343 y_resolution=65536.0*image->resolution.y+0.5;
2346 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2347 (void) WriteBlobMSBShort(image,0x03ED);
2348 (void) WriteBlobMSBShort(image,0);
2349 (void) WriteBlobMSBLong(image,16); /* resource size */
2350 (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5));
2351 (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */
2352 (void) WriteBlobMSBShort(image,units); /* width unit */
2353 (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5));
2354 (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */
2355 (void) WriteBlobMSBShort(image,units); /* height unit */
2358 static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile)
2360 register const unsigned char
2377 length=GetStringInfoLength(bim_profile);
2380 datum=GetStringInfoDatum(bim_profile);
2381 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2383 register unsigned char
2386 q=(unsigned char *) p;
2387 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2389 p=PushLongPixel(MSBEndian,p,&long_sans);
2390 p=PushShortPixel(MSBEndian,p,&id);
2391 p=PushShortPixel(MSBEndian,p,&short_sans);
2392 p=PushLongPixel(MSBEndian,p,&count);
2393 if (id == 0x0000040f)
2395 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2396 (PSDQuantum(count)+12)-(q-datum));
2397 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2401 if ((count & 0x01) != 0)
2406 static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile)
2408 register const unsigned char
2425 length=GetStringInfoLength(bim_profile);
2428 datum=GetStringInfoDatum(bim_profile);
2429 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2431 register unsigned char
2434 q=(unsigned char *) p;
2435 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2437 p=PushLongPixel(MSBEndian,p,&long_sans);
2438 p=PushShortPixel(MSBEndian,p,&id);
2439 p=PushShortPixel(MSBEndian,p,&short_sans);
2440 p=PushLongPixel(MSBEndian,p,&count);
2441 if ((id == 0x000003ed) && (PSDQuantum(count) < (ssize_t) (length-12)))
2443 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2444 (PSDQuantum(count)+12)-(q-datum));
2445 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2449 if ((count & 0x01) != 0)
2454 static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
2455 ExceptionInfo *exception)
2484 rounded_layer_info_size;
2492 assert(image_info != (const ImageInfo *) NULL);
2493 assert(image_info->signature == MagickSignature);
2494 assert(image != (Image *) NULL);
2495 assert(image->signature == MagickSignature);
2496 if (image->debug != MagickFalse)
2497 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2498 assert(exception != (ExceptionInfo *) NULL);
2499 assert(exception->signature == MagickSignature);
2500 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2501 if (status == MagickFalse)
2503 packet_size=(size_t) (image->depth > 8 ? 6 : 3);
2504 if (image->alpha_trait != UndefinedPixelTrait)
2505 packet_size+=image->depth > 8 ? 2 : 1;
2507 if ((LocaleCompare(image_info->magick,"PSB") == 0) ||
2508 (image->columns > 30000) || (image->rows > 30000))
2510 (void) WriteBlob(image,4,(const unsigned char *) "8BPS");
2511 (void) WriteBlobMSBShort(image,psd_info.version); /* version */
2512 for (i=1; i <= 6; i++)
2513 (void) WriteBlobByte(image, 0); /* 6 bytes of reserved */
2514 if (IsImageGray(image) != MagickFalse)
2515 num_channels=(image->alpha_trait != UndefinedPixelTrait ? 2UL : 1UL);
2517 if (image->storage_class == PseudoClass)
2518 num_channels=(image->alpha_trait != UndefinedPixelTrait ? 2UL : 1UL);
2521 if (image->colorspace != CMYKColorspace)
2522 num_channels=(image->alpha_trait != UndefinedPixelTrait ? 4UL : 3UL);
2524 num_channels=(image->alpha_trait != UndefinedPixelTrait ? 5UL : 4UL);
2526 (void) WriteBlobMSBShort(image,(unsigned short) num_channels);
2527 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
2528 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
2529 if (IsImageGray(image) != MagickFalse)
2537 monochrome=IsImageMonochrome(image) && (image->depth == 1) ?
2538 MagickTrue : MagickFalse;
2539 (void) WriteBlobMSBShort(image,(unsigned short)
2540 (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8));
2541 (void) WriteBlobMSBShort(image,(unsigned short)
2542 (monochrome != MagickFalse ? BitmapMode : GrayscaleMode));
2546 (void) WriteBlobMSBShort(image,(unsigned short) (image->storage_class ==
2547 PseudoClass ? 8 : image->depth > 8 ? 16 : 8));
2549 if (((image_info->colorspace != UndefinedColorspace) ||
2550 (image->colorspace != CMYKColorspace)) &&
2551 (image_info->colorspace != CMYKColorspace))
2553 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2554 (void) WriteBlobMSBShort(image,(unsigned short)
2555 (image->storage_class == PseudoClass ? IndexedMode : RGBMode));
2559 if (image->colorspace != CMYKColorspace)
2560 (void) TransformImageColorspace(image,CMYKColorspace,exception);
2561 (void) WriteBlobMSBShort(image,CMYKMode);
2564 if ((IsImageGray(image) != MagickFalse) ||
2565 (image->storage_class == DirectClass) || (image->colors > 256))
2566 (void) WriteBlobMSBLong(image,0);
2570 Write PSD raster colormap.
2572 (void) WriteBlobMSBLong(image,768);
2573 for (i=0; i < (ssize_t) image->colors; i++)
2574 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red));
2575 for ( ; i < 256; i++)
2576 (void) WriteBlobByte(image,0);
2577 for (i=0; i < (ssize_t) image->colors; i++)
2578 (void) WriteBlobByte(image,ScaleQuantumToChar(
2579 image->colormap[i].green));
2580 for ( ; i < 256; i++)
2581 (void) WriteBlobByte(image,0);
2582 for (i=0; i < (ssize_t) image->colors; i++)
2583 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue));
2584 for ( ; i < 256; i++)
2585 (void) WriteBlobByte(image,0);
2588 Image resource block.
2590 length=28; /* 0x03EB */
2591 bim_profile=(StringInfo *) GetImageProfile(image,"8bim");
2592 icc_profile=GetImageProfile(image,"icc");
2593 if (bim_profile != (StringInfo *) NULL)
2595 bim_profile=CloneStringInfo(bim_profile);
2596 if (icc_profile != (StringInfo *) NULL)
2597 RemoveICCProfileFromResourceBlock(bim_profile);
2598 RemoveResolutionFromResourceBlock(bim_profile);
2599 length+=PSDQuantum(GetStringInfoLength(bim_profile));
2601 if (icc_profile != (const StringInfo *) NULL)
2602 length+=PSDQuantum(GetStringInfoLength(icc_profile))+12;
2603 (void) WriteBlobMSBLong(image,(unsigned int) length);
2604 WriteResolutionResourceBlock(image);
2605 if (bim_profile != (StringInfo *) NULL)
2607 (void) WriteBlob(image,GetStringInfoLength(bim_profile),
2608 GetStringInfoDatum(bim_profile));
2609 bim_profile=DestroyStringInfo(bim_profile);
2611 if (icc_profile != (StringInfo *) NULL)
2613 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2614 (void) WriteBlobMSBShort(image,0x0000040F);
2615 (void) WriteBlobMSBShort(image,0);
2616 (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength(
2618 (void) WriteBlob(image,GetStringInfoLength(icc_profile),
2619 GetStringInfoDatum(icc_profile));
2620 if ((MagickOffsetType) GetStringInfoLength(icc_profile) !=
2621 PSDQuantum(GetStringInfoLength(icc_profile)))
2622 (void) WriteBlobByte(image,0);
2626 base_image=GetNextImageInList(image);
2627 if ((image->alpha_trait != UndefinedPixelTrait) && (base_image == (Image *) NULL))
2629 next_image=base_image;
2630 while ( next_image != NULL )
2632 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2633 if (IsImageGray(next_image) != MagickFalse)
2634 num_channels=next_image->alpha_trait != UndefinedPixelTrait ? 2UL : 1UL;
2636 if (next_image->storage_class == PseudoClass)
2637 num_channels=next_image->alpha_trait != UndefinedPixelTrait ? 2UL : 1UL;
2639 if (next_image->colorspace != CMYKColorspace)
2640 num_channels=next_image->alpha_trait != UndefinedPixelTrait ? 4UL : 3UL;
2642 num_channels=next_image->alpha_trait != UndefinedPixelTrait ? 5UL : 4UL;
2643 channelLength=(size_t) (next_image->columns*next_image->rows*packet_size+2);
2644 layer_info_size+=(size_t) (4*4+2+num_channels*6+(psd_info.version == 1 ? 8 :
2645 16)+4*1+4+num_channels*channelLength);
2646 property=(const char *) GetImageProperty(next_image,"label",exception);
2647 if (property == (const char *) NULL)
2648 layer_info_size+=16;
2654 length=strlen(property);
2655 layer_info_size+=8+length+(4-(length % 4));
2658 next_image=GetNextImageInList(next_image);
2660 if (layer_count == 0)
2661 (void) SetPSDSize(&psd_info,image,0);
2667 (void) SetPSDSize(&psd_info,image,layer_info_size+
2668 (psd_info.version == 1 ? 8 : 16));
2669 if ((layer_info_size/2) != ((layer_info_size+1)/2))
2670 rounded_layer_info_size=layer_info_size+1;
2672 rounded_layer_info_size=layer_info_size;
2673 (void) SetPSDSize(&psd_info,image,rounded_layer_info_size);
2674 if (image->alpha_trait != UndefinedPixelTrait)
2675 (void) WriteBlobMSBShort(image,-(unsigned short) layer_count);
2677 (void) WriteBlobMSBShort(image,(unsigned short) layer_count);
2679 compression=base_image->compression;
2680 for (next_image=base_image; next_image != NULL; )
2682 next_image->compression=NoCompression;
2683 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y);
2684 (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x);
2685 (void) WriteBlobMSBLong(image,(unsigned int) (next_image->page.y+
2687 (void) WriteBlobMSBLong(image,(unsigned int) (next_image->page.x+
2688 next_image->columns));
2689 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2690 channel_size=(unsigned int) ((packet_size*next_image->rows*
2691 next_image->columns)+2);
2692 if ((IsImageGray(next_image) != MagickFalse) ||
2693 (next_image->storage_class == PseudoClass))
2695 (void) WriteBlobMSBShort(image,(unsigned short)
2696 (next_image->alpha_trait != UndefinedPixelTrait ? 2 : 1));
2697 (void) WriteBlobMSBShort(image,0);
2698 (void) SetPSDSize(&psd_info,image,channel_size);
2699 if (next_image->alpha_trait != UndefinedPixelTrait)
2701 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2702 (void) SetPSDSize(&psd_info,image,channel_size);
2706 if (next_image->colorspace != CMYKColorspace)
2708 (void) WriteBlobMSBShort(image,(unsigned short)
2709 (next_image->alpha_trait != UndefinedPixelTrait ? 4 : 3));
2710 (void) WriteBlobMSBShort(image,0);
2711 (void) SetPSDSize(&psd_info,image,channel_size);
2712 (void) WriteBlobMSBShort(image,1);
2713 (void) SetPSDSize(&psd_info,image,channel_size);
2714 (void) WriteBlobMSBShort(image,2);
2715 (void) SetPSDSize(&psd_info,image,channel_size);
2716 if (next_image->alpha_trait != UndefinedPixelTrait)
2718 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2719 (void) SetPSDSize(&psd_info,image,channel_size);
2724 (void) WriteBlobMSBShort(image,(unsigned short)
2725 (next_image->alpha_trait ? 5 : 4));
2726 (void) WriteBlobMSBShort(image,0);
2727 (void) SetPSDSize(&psd_info,image,channel_size);
2728 (void) WriteBlobMSBShort(image,1);
2729 (void) SetPSDSize(&psd_info,image,channel_size);
2730 (void) WriteBlobMSBShort(image,2);
2731 (void) SetPSDSize(&psd_info,image,channel_size);
2732 (void) WriteBlobMSBShort(image,3);
2733 (void) SetPSDSize(&psd_info,image,channel_size);
2734 if (next_image->alpha_trait)
2736 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2737 (void) SetPSDSize(&psd_info,image,channel_size);
2740 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2741 (void) WriteBlob(image,4,(const unsigned char *)
2742 CompositeOperatorToPSDBlendMode(next_image->compose));
2743 (void) WriteBlobByte(image,255); /* layer opacity */
2744 (void) WriteBlobByte(image,0);
2745 (void) WriteBlobByte(image,next_image->compose==NoCompositeOp ?
2746 1 << 0x02 : 1); /* layer properties - visible, etc. */
2747 (void) WriteBlobByte(image,0);
2748 property=(const char *) GetImageProperty(next_image,"label",exception);
2749 if (property == (const char *) NULL)
2752 layer_name[MaxTextExtent];
2754 (void) WriteBlobMSBLong(image,16);
2755 (void) WriteBlobMSBLong(image,0);
2756 (void) WriteBlobMSBLong(image,0);
2757 (void) FormatLocaleString(layer_name,MaxTextExtent,"L%04ld",(long)
2759 WritePascalString(image,layer_name,4);
2766 length=strlen(property);
2767 (void) WriteBlobMSBLong(image,(unsigned int) (length+(4-
2769 (void) WriteBlobMSBLong(image,0);
2770 (void) WriteBlobMSBLong(image,0);
2771 WritePascalString(image,property,4);
2773 next_image=GetNextImageInList(next_image);
2778 next_image=base_image;
2779 while (next_image != NULL)
2781 status=WriteImageChannels(&psd_info,image_info,image,next_image,
2782 MagickTrue,exception);
2783 next_image=GetNextImageInList(next_image);
2785 (void) WriteBlobMSBLong(image,0); /* user mask data */
2786 base_image->compression=compression;
2789 Write composite image.
2791 status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse,
2793 (void) CloseBlob(image);