2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Adobe Photoshop Image Format %
21 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "magick/studio.h"
44 #include "magick/artifact.h"
45 #include "magick/blob.h"
46 #include "magick/blob-private.h"
47 #include "magick/cache.h"
48 #include "magick/colormap.h"
49 #include "magick/colorspace.h"
50 #include "magick/constitute.h"
51 #include "magick/enhance.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/image.h"
55 #include "magick/image-private.h"
56 #include "magick/list.h"
57 #include "magick/log.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/module.h"
61 #include "magick/monitor-private.h"
62 #include "magick/profile.h"
63 #include "magick/property.h"
64 #include "magick/quantum-private.h"
65 #include "magick/static.h"
66 #include "magick/string_.h"
71 #define MaxPSDChannels 56
72 #define PSDQuantum(x) (((ssize_t) (x)+1) & -2)
75 Enumerated declaractions.
90 Typedef declaractions.
92 typedef struct _ChannelInfo
101 typedef struct _LayerInfo
111 channel_info[MaxPSDChannels];
135 typedef struct _PSDInfo
157 Forward declarations.
159 static MagickBooleanType
160 WritePSDImage(const ImageInfo *,Image *);
163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 % IsPSD()() returns MagickTrue if the image format type, identified by the
174 % magick string, is PSD.
176 % The format of the IsPSD method is:
178 % MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
180 % A description of each parameter follows:
182 % o magick: compare image format pattern against these bytes.
184 % o length: Specifies the length of the magick string.
187 static MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
191 if (LocaleNCompare((const char *) magick,"8BPS",4) == 0)
197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
201 % R e a d P S D I m a g e %
205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 % ReadPSDImage() reads an Adobe Photoshop image file and returns it. It
208 % allocates the memory necessary for the new Image structure and returns a
209 % pointer to the new image.
211 % The format of the ReadPSDImage method is:
213 % Image *ReadPSDImage(image_info)
215 % A description of each parameter follows:
217 % o image_info: the image info.
219 % o exception: return any errors or warnings in this structure.
223 static const char *CompositeOperatorToPSDBlendMode(CompositeOperator op)
230 case OverCompositeOp: blend_mode = "norm"; break;
231 case MultiplyCompositeOp: blend_mode = "mul "; break;
232 case DissolveCompositeOp: blend_mode = "diss"; break;
233 case DifferenceCompositeOp: blend_mode = "diff"; break;
234 case DarkenCompositeOp: blend_mode = "dark"; break;
235 case LightenCompositeOp: blend_mode = "lite"; break;
236 case HueCompositeOp: blend_mode = "hue "; break;
237 case SaturateCompositeOp: blend_mode = "sat "; break;
238 case ColorizeCompositeOp: blend_mode = "colr"; break;
239 case LuminizeCompositeOp: blend_mode = "lum "; break;
240 case ScreenCompositeOp: blend_mode = "scrn"; break;
241 case OverlayCompositeOp: blend_mode = "over"; break;
248 static ssize_t DecodePSDPixels(const size_t number_compact_pixels,
249 const unsigned char *compact_pixels,const ssize_t depth,
250 const size_t number_pixels,unsigned char *pixels)
265 packets=(ssize_t) number_compact_pixels;
266 for (i=0; (packets > 1) && (i < (ssize_t) number_pixels); )
268 length=(*compact_pixels++);
275 pixel=(*compact_pixels++);
277 for (j=0; j < (ssize_t) length; j++)
283 *pixels++=(pixel >> 7) & 0x01 ? 0U : 255U;
284 *pixels++=(pixel >> 6) & 0x01 ? 0U : 255U;
285 *pixels++=(pixel >> 5) & 0x01 ? 0U : 255U;
286 *pixels++=(pixel >> 4) & 0x01 ? 0U : 255U;
287 *pixels++=(pixel >> 3) & 0x01 ? 0U : 255U;
288 *pixels++=(pixel >> 2) & 0x01 ? 0U : 255U;
289 *pixels++=(pixel >> 1) & 0x01 ? 0U : 255U;
290 *pixels++=(pixel >> 0) & 0x01 ? 0U : 255U;
296 *pixels++=(unsigned char) ((pixel >> 4) & 0xff);
297 *pixels++=(unsigned char) ((pixel & 0x0f) & 0xff);
303 *pixels++=(unsigned char) ((pixel >> 6) & 0x03);
304 *pixels++=(unsigned char) ((pixel >> 4) & 0x03);
305 *pixels++=(unsigned char) ((pixel >> 2) & 0x03);
306 *pixels++=(unsigned char) ((pixel & 0x03) & 0x03);
312 *pixels++=(unsigned char) pixel;
321 for (j=0; j < (ssize_t) length; j++)
327 *pixels++=(*compact_pixels >> 7) & 0x01 ? 0U : 255U;
328 *pixels++=(*compact_pixels >> 6) & 0x01 ? 0U : 255U;
329 *pixels++=(*compact_pixels >> 5) & 0x01 ? 0U : 255U;
330 *pixels++=(*compact_pixels >> 4) & 0x01 ? 0U : 255U;
331 *pixels++=(*compact_pixels >> 3) & 0x01 ? 0U : 255U;
332 *pixels++=(*compact_pixels >> 2) & 0x01 ? 0U : 255U;
333 *pixels++=(*compact_pixels >> 1) & 0x01 ? 0U : 255U;
334 *pixels++=(*compact_pixels >> 0) & 0x01 ? 0U : 255U;
340 *pixels++=(*compact_pixels >> 4) & 0xff;
341 *pixels++=(*compact_pixels & 0x0f) & 0xff;
347 *pixels++=(*compact_pixels >> 6) & 0x03;
348 *pixels++=(*compact_pixels >> 4) & 0x03;
349 *pixels++=(*compact_pixels >> 2) & 0x03;
350 *pixels++=(*compact_pixels & 0x03) & 0x03;
356 *pixels++=(*compact_pixels);
367 static inline MagickOffsetType GetPSDOffset(PSDInfo *psd_info,Image *image)
369 if (psd_info->version == 1)
370 return((MagickOffsetType) ReadBlobMSBShort(image));
371 return((MagickOffsetType) ReadBlobMSBLong(image));
374 static inline MagickSizeType GetPSDSize(PSDInfo *psd_info,Image *image)
376 if (psd_info->version == 1)
377 return((MagickSizeType) ReadBlobMSBLong(image));
378 return((MagickSizeType) ReadBlobMSBLongLong(image));
381 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
388 static const char *ModeToString(PSDImageType type)
392 case BitmapMode: return "Bitmap";
393 case GrayscaleMode: return "Grayscale";
394 case IndexedMode: return "Indexed";
395 case RGBMode: return "RGB";
396 case CMYKMode: return "CMYK";
397 case MultichannelMode: return "Multichannel";
398 case DuotoneMode: return "Duotone";
399 case LabMode: return "L*A*B";
400 default: return "unknown";
404 static MagickBooleanType ParseImageResourceBlocks(Image *image,
405 const unsigned char *blocks,size_t length)
423 profile=AcquireStringInfo(length);
424 SetStringInfoDatum(profile,blocks);
425 (void) SetImageProfile(image,"8bim",profile);
426 profile=DestroyStringInfo(profile);
427 for (p=blocks; (p >= blocks) && (p < (blocks+length-16)); )
429 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
431 p=PushLongPixel(MSBEndian,p,&long_sans);
432 p=PushShortPixel(MSBEndian,p,&id);
433 p=PushShortPixel(MSBEndian,p,&short_sans);
434 p=PushLongPixel(MSBEndian,p,&count);
440 value[MaxTextExtent];
448 p=PushShortPixel(MSBEndian,p,&resolution);
449 image->x_resolution=(double) resolution;
450 (void) FormatMagickString(value,MaxTextExtent,"%g",image->x_resolution);
451 (void) SetImageProperty(image,"tiff:XResolution",value);
452 p=PushShortPixel(MSBEndian,p,&short_sans);
453 p=PushShortPixel(MSBEndian,p,&short_sans);
454 p=PushShortPixel(MSBEndian,p,&short_sans);
455 p=PushShortPixel(MSBEndian,p,&resolution);
456 image->y_resolution=(double) resolution;
457 (void) FormatMagickString(value,MaxTextExtent,"%g",image->y_resolution);
458 (void) SetImageProperty(image,"tiff:YResolution",value);
459 p=PushShortPixel(MSBEndian,p,&short_sans);
460 p=PushShortPixel(MSBEndian,p,&short_sans);
461 p=PushShortPixel(MSBEndian,p,&short_sans);
470 if ((count & 0x01) != 0)
476 static CompositeOperator PSDBlendModeToCompositeOperator(const char *mode)
478 if (mode == (const char *) NULL)
479 return(OverCompositeOp);
480 if (LocaleNCompare(mode,"norm",4) == 0)
481 return(OverCompositeOp);
482 if (LocaleNCompare(mode,"mul ",4) == 0)
483 return(MultiplyCompositeOp);
484 if (LocaleNCompare(mode,"diss",4) == 0)
485 return(DissolveCompositeOp);
486 if (LocaleNCompare(mode,"diff",4) == 0)
487 return(DifferenceCompositeOp);
488 if (LocaleNCompare(mode,"dark",4) == 0)
489 return(DarkenCompositeOp);
490 if (LocaleNCompare(mode,"lite",4) == 0)
491 return(LightenCompositeOp);
492 if (LocaleNCompare(mode,"hue ",4) == 0)
493 return(HueCompositeOp);
494 if (LocaleNCompare(mode,"sat ",4) == 0)
495 return(SaturateCompositeOp);
496 if (LocaleNCompare(mode,"colr",4) == 0)
497 return(ColorizeCompositeOp);
498 if (LocaleNCompare(mode,"lum ",4) == 0)
499 return(LuminizeCompositeOp);
500 if (LocaleNCompare(mode,"scrn",4) == 0)
501 return(ScreenCompositeOp);
502 if (LocaleNCompare(mode,"over",4) == 0)
503 return(OverlayCompositeOp);
504 if (LocaleNCompare(mode,"hLit",4) == 0)
505 return(OverCompositeOp);
506 if (LocaleNCompare(mode,"sLit",4) == 0)
507 return(OverCompositeOp);
508 if (LocaleNCompare(mode,"smud",4) == 0)
509 return(OverCompositeOp);
510 if (LocaleNCompare(mode,"div ",4) == 0)
511 return(OverCompositeOp);
512 if (LocaleNCompare(mode,"idiv",4) == 0)
513 return(OverCompositeOp);
514 return(OverCompositeOp);
517 static MagickBooleanType ReadPSDLayer(Image *image,const size_t channels,
518 const ssize_t type,const MagickOffsetType *offsets,ExceptionInfo *exception)
535 register const unsigned char
553 if (image->storage_class == PseudoClass)
555 if (image->colors > 256)
558 if (image->depth > 8)
562 if (image->depth > 8)
564 pixels=(unsigned char *) AcquireQuantumMemory(image->columns+256,packet_size*
566 if (pixels == (unsigned char *) NULL)
567 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
569 (void) ResetMagickMemory(pixels,0,image->columns*packet_size*sizeof(*pixels));
570 compact_pixels=(unsigned char *) NULL;
571 if (image->compression == RLECompression)
577 for (y=0; y < (ssize_t) image->rows; y++)
578 if ((MagickOffsetType) length < offsets[y])
579 length=(size_t) offsets[y];
580 compact_pixels=(unsigned char *) AcquireQuantumMemory(length,
582 if (compact_pixels == (unsigned char *) NULL)
583 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
585 (void) ResetMagickMemory(compact_pixels,0,length*sizeof(*compact_pixels));
587 colorspace=image->colorspace;
588 for (y=0; y < (ssize_t) image->rows; y++)
590 if (image->depth == 1)
592 count=ReadBlob(image,(image->columns+7)/8,pixels);
593 if (count < (ssize_t) ((image->columns+7)/8))
598 if (image->compression != RLECompression)
599 count=ReadBlob(image,packet_size*image->columns,pixels);
602 count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
603 if (count != (ssize_t) offsets[y])
605 count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
606 (ssize_t) image->depth,packet_size*image->columns,pixels);
608 if (count < (ssize_t) (packet_size*image->columns))
611 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
612 if (q == (PixelPacket *) NULL)
614 indexes=GetAuthenticIndexQueue(image);
616 for (x=0; x < (ssize_t) image->columns; x++)
618 if (packet_size == 1)
619 pixel=ScaleCharToQuantum(*p++);
622 p=PushShortPixel(MSBEndian,p,&nibble);
623 pixel=ScaleShortToQuantum(nibble);
629 q->opacity=(Quantum) (QuantumRange-pixel);
640 if (image->storage_class == PseudoClass)
642 if (packet_size == 1)
643 indexes[x]=(IndexPacket) ScaleQuantumToChar(pixel);
645 indexes[x]=(IndexPacket) ScaleQuantumToShort(pixel);
646 q->red=image->colormap[(ssize_t) indexes[x]].red;
647 q->green=image->colormap[(ssize_t) indexes[x]].green;
648 q->blue=image->colormap[(ssize_t) indexes[x]].blue;
649 if (image->depth == 1)
654 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
656 indexes[x]=((((unsigned char) pixel) & (0x01 << (7-bit)))
658 q->red=image->colormap[(ssize_t) indexes[x]].red;
659 q->green=image->colormap[(ssize_t) indexes[x]].green;
660 q->blue=image->colormap[(ssize_t) indexes[x]].blue;
672 if (image->storage_class == PseudoClass)
673 q->opacity=(Quantum) (QuantumRange-pixel);
680 if (image->storage_class == PseudoClass)
681 q->opacity=(Quantum) (QuantumRange-pixel);
688 if (image->colorspace == CMYKColorspace)
689 indexes[x]=(IndexPacket) pixel;
691 q->opacity=(Quantum) (QuantumRange-pixel);
696 if ((image->colorspace == RGBColorspace) && (channels > 3))
698 q->opacity=(Quantum) (QuantumRange-pixel);
706 if (SyncAuthenticPixels(image,exception) == MagickFalse)
709 image->colorspace=colorspace;
710 if (image->compression == RLECompression)
711 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
712 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
716 static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception)
719 message[MaxTextExtent],
769 skip_first_alpha = 0;
774 assert(image_info != (const ImageInfo *) NULL);
775 assert(image_info->signature == MagickSignature);
776 if (image_info->debug != MagickFalse)
777 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
778 image_info->filename);
779 assert(exception != (ExceptionInfo *) NULL);
780 assert(exception->signature == MagickSignature);
781 image=AcquireImage(image_info);
782 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
783 if (status == MagickFalse)
785 image=DestroyImageList(image);
786 return((Image *) NULL);
791 count=ReadBlob(image,4,(unsigned char *) psd_info.signature);
792 psd_info.version=ReadBlobMSBShort(image);
793 if ((count == 0) || (LocaleNCompare(psd_info.signature,"8BPS",4) != 0) ||
794 ((psd_info.version != 1) && (psd_info.version != 2)))
795 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
796 count=ReadBlob(image,6,psd_info.reserved);
797 psd_info.channels=ReadBlobMSBShort(image);
798 if (psd_info.channels > MaxPSDChannels)
799 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
800 psd_info.rows=ReadBlobMSBLong(image);
801 psd_info.columns=ReadBlobMSBLong(image);
802 if ((psd_info.version == 1) && ((psd_info.rows > 30000) ||
803 (psd_info.columns > 30000)))
804 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
805 psd_info.depth=ReadBlobMSBShort(image);
806 if ((psd_info.depth != 1) && (psd_info.depth != 8) && (psd_info.depth != 16))
807 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
808 psd_info.mode=ReadBlobMSBShort(image);
809 if (image->debug != MagickFalse)
810 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
811 " Image is %.20g x %.20g with channels=%.20g, depth=%.20g, mode=%s",
812 (double) psd_info.columns,(double) psd_info.rows,(double)
813 psd_info.channels,(double) psd_info.depth,ModeToString((PSDImageType)
818 image->depth=psd_info.depth;
819 image->columns=psd_info.columns;
820 image->rows=psd_info.rows;
821 if ((image_info->ping != MagickFalse) &&
822 (SetImageBackgroundColor(image) == MagickFalse))
824 InheritException(exception,&image->exception);
825 image=DestroyImageList(image);
826 return((Image *) NULL);
828 image->matte=psd_info.channels >= 4 ? MagickTrue : MagickFalse;
829 if (psd_info.mode == LabMode)
830 image->colorspace=LabColorspace;
831 if (psd_info.mode == CMYKMode)
833 image->colorspace=CMYKColorspace;
834 image->matte=psd_info.channels >= 5 ? MagickTrue : MagickFalse;
836 if ((psd_info.mode == BitmapMode) || (psd_info.mode == GrayscaleMode) ||
837 (psd_info.mode == DuotoneMode))
839 if (AcquireImageColormap(image,256) == MagickFalse)
840 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
841 image->matte=psd_info.channels >= 2 ? MagickTrue : MagickFalse;
842 if (image->debug != MagickFalse)
843 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
844 " Image colormap allocated");
845 image->colorspace=GRAYColorspace;
847 if (image->debug != MagickFalse)
848 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
849 image->matte ? " image has matte" : " image has no matte");
851 Read PSD raster colormap only present for indexed and duotone images.
853 length=ReadBlobMSBLong(image);
856 if (image->debug != MagickFalse)
857 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
858 " reading colormap");
859 if (psd_info.mode == DuotoneMode)
862 Duotone image data; the format of this data is undocumented.
864 data=(unsigned char *) AcquireQuantumMemory((size_t) length,
866 if (data == (unsigned char *) NULL)
867 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
868 count=ReadBlob(image,(size_t) length,data);
869 data=(unsigned char *) RelinquishMagickMemory(data);
874 Read PSD raster colormap.
876 if (AcquireImageColormap(image,(size_t) (length/3)) == MagickFalse)
877 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
878 for (i=0; i < (ssize_t) image->colors; i++)
879 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
880 ReadBlobByte(image));
881 for (i=0; i < (ssize_t) image->colors; i++)
882 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
883 ReadBlobByte(image));
884 for (i=0; i < (ssize_t) image->colors; i++)
885 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
886 ReadBlobByte(image));
887 image->matte=psd_info.channels >= 2 ? MagickTrue : MagickFalse;
890 length=ReadBlobMSBLong(image);
897 Image resources block.
899 if (image->debug != MagickFalse)
900 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
901 " reading image resource blocks - %.20g bytes",(double) length);
902 blocks=(unsigned char *) AcquireQuantumMemory((size_t) length,
904 if (blocks == (unsigned char *) NULL)
905 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
906 count=ReadBlob(image,(size_t) length,blocks);
907 if ((count != (ssize_t) length) ||
908 (LocaleNCompare((char *) blocks,"8BIM",4) != 0))
910 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
911 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
913 (void) ParseImageResourceBlocks(image,blocks,(size_t) length);
914 blocks=(unsigned char *) RelinquishMagickMemory(blocks);
917 Layer and mask block.
919 layer_info=(LayerInfo *) NULL;
921 length=GetPSDSize(&psd_info,image);
924 length=ReadBlobMSBLong(image);
925 length=ReadBlobMSBLong(image);
927 if ((image_info->number_scenes == 1) && (image_info->scene == 0))
929 if (DiscardBlobBytes(image,length) == MagickFalse)
930 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
936 if (image->debug != MagickFalse)
937 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
938 " image has no layers");
942 offset=TellBlob(image);
943 size=GetPSDSize(&psd_info,image);
952 quantum=psd_info.version == 1 ? 4UL : 8UL;
953 if (DiscardBlobBytes(image,length-quantum) == MagickFalse)
954 ThrowFileException(exception,CorruptImageError,
955 "UnexpectedEndOfFile",image->filename);
962 layer_offset=offset+length;
963 number_layers=(short) ReadBlobMSBShort(image);
964 if (image->debug != MagickFalse)
965 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
966 " image contains %.20g layers",(double) number_layers);
967 if (number_layers < 0)
970 Weird hack in PSD format to ignore first alpha channel.
973 number_layers=MagickAbsoluteValue(number_layers);
974 if (image->debug != MagickFalse)
975 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
976 " negative layer count corrected for");
978 layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers,
979 sizeof(*layer_info));
980 if (layer_info == (LayerInfo *) NULL)
982 if (image->debug != MagickFalse)
983 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
984 " allocation of LayerInfo failed");
985 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
987 (void) ResetMagickMemory(layer_info,0,(size_t) number_layers*
988 sizeof(*layer_info));
989 for (i=0; i < number_layers; i++)
995 if (image->debug != MagickFalse)
996 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
997 " reading layer #%.20g",(double) i+1);
998 layer_info[i].page.y=(int) ReadBlobMSBLong(image);
999 layer_info[i].page.x=(int) ReadBlobMSBLong(image);
1000 y=(int) ReadBlobMSBLong(image);
1001 x=(int) ReadBlobMSBLong(image);
1002 layer_info[i].page.width=(ssize_t) (x-layer_info[i].page.x);
1003 layer_info[i].page.height=(ssize_t) (y-layer_info[i].page.y);
1004 layer_info[i].channels=ReadBlobMSBShort(image);
1005 if (layer_info[i].channels > MaxPSDChannels)
1006 ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
1007 if (image->debug != MagickFalse)
1008 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1009 " offset(%.20g,%.20g), size(%.20g,%.20g), channels=%.20g",
1010 (double) layer_info[i].page.x,(double) layer_info[i].page.y,
1011 (double) layer_info[i].page.height,(double)
1012 layer_info[i].page.width,(double) layer_info[i].channels);
1013 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1015 layer_info[i].channel_info[j].type=(short)
1016 ReadBlobMSBShort(image);
1017 layer_info[i].channel_info[j].size=(size_t)
1018 GetPSDSize(&psd_info,image);
1019 if (image->debug != MagickFalse)
1020 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1021 " channel[%.20g]: type=%.20g, size=%.20g",(double) j,
1022 (double) layer_info[i].channel_info[j].type,
1023 (double) layer_info[i].channel_info[j].size);
1025 count=ReadBlob(image,4,(unsigned char *) type);
1026 if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1028 if (image->debug != MagickFalse)
1029 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1030 " layer type was %.4s instead of 8BIM", type);
1031 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1033 count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey);
1034 layer_info[i].opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1035 (unsigned char) ReadBlobByte(image)));
1036 layer_info[i].clipping=(unsigned char) ReadBlobByte(image);
1037 layer_info[i].flags=(unsigned char) ReadBlobByte(image);
1038 layer_info[i].visible=!(layer_info[i].flags & 0x02);
1039 if (image->debug != MagickFalse)
1040 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1041 " blend=%.4s, opacity=%.20g, clipping=%s, flags=%d, visible=%s",
1042 layer_info[i].blendkey,(double) layer_info[i].opacity,
1043 layer_info[i].clipping ? "true" : "false",layer_info[i].flags,
1044 layer_info[i].visible ? "true" : "false");
1045 (void) ReadBlobByte(image); /* filler */
1047 size=ReadBlobMSBLong(image);
1050 if (image->debug != MagickFalse)
1051 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1052 " layer contains additional info");
1053 length=ReadBlobMSBLong(image);
1059 layer_info[i].mask.y=(int) ReadBlobMSBLong(image);
1060 layer_info[i].mask.x=(int) ReadBlobMSBLong(image);
1061 layer_info[i].mask.height=(size_t)
1062 (ReadBlobMSBLong(image)-layer_info[i].mask.y);
1063 layer_info[i].mask.width=(size_t)
1064 (ReadBlobMSBLong(image)-layer_info[i].mask.x);
1065 if (image->debug != MagickFalse)
1066 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1067 " layer mask: offset(%.20g,%.20g), size(%.20g,%.20g), length=%.20g",
1068 (double) layer_info[i].mask.x,(double) layer_info[i].mask.y,
1069 (double) layer_info[i].mask.width,(double)
1070 layer_info[i].mask.height,(double) length-16);
1072 Skip over the rest of the layer mask information.
1074 if (DiscardBlobBytes(image,length-16) == MagickFalse)
1075 ThrowFileException(exception,CorruptImageError,
1076 "UnexpectedEndOfFile",image->filename);
1078 combinedlength+=length+4; /* +4 for length */
1079 length=ReadBlobMSBLong(image);
1083 Layer blending ranges info.
1085 if (image->debug != MagickFalse)
1086 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1087 " layer blending ranges: length=%.20g",(double)
1090 We read it, but don't use it...
1092 for (j=0; j < (ssize_t) (length); j+=8)
1094 size_t blend_source=ReadBlobMSBLong(image);
1095 size_t blend_dest=ReadBlobMSBLong(image);
1096 if (image->debug != MagickFalse)
1097 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1098 " source(%x), dest(%x)",(unsigned int)
1099 blend_source,(unsigned int) blend_dest);
1102 combinedlength+=length+4;
1106 length=(size_t) ReadBlobByte(image);
1107 for (j=0; j < (ssize_t) length; j++)
1108 layer_info[i].name[j]=(unsigned char) ReadBlobByte(image);
1109 layer_info[i].name[j]='\0';
1110 if (image->debug != MagickFalse)
1111 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1112 " layer name: %s",layer_info[i].name);
1113 combinedlength+=length+1;
1115 #if 0 /* still in development */
1117 Adjustment layers and other stuff...
1123 count=ReadBlob(image,4,alsig);
1124 if ((count == 0) || (LocaleNCompare(alsig,"8BIM",4) != 0)) {
1125 if (debug != MagickFalse)
1127 if (image->debug != MagickFalse)
1128 (void) LogMagickEvent(CoderEvent,GetMagickModule()," adjustment layer type was %.4s instead of 8BIM", alsig);
1130 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1132 count=ReadBlob(image,4,alkey);
1133 length=ReadBlobMSBLong(image);
1134 if (debug != MagickFalse)
1136 if (image->debug != MagickFalse)
1137 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1138 " adjustment layer key: %.4s, data length=%.20g",
1139 alkey, (double) length);
1143 for (j=0; j < (ssize_t) (length); j++)
1144 (void) ReadBlobByte(image);
1148 combinedlength += 12 + length; /* sig, key, length + the actual length*/
1152 Skip the rest of the variable data until we support it.
1154 if (image->debug != MagickFalse)
1155 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1156 " unsupported data: length=%.20g",(double)
1157 (size-combinedlength));
1158 if (DiscardBlobBytes(image,size-combinedlength) == MagickFalse)
1159 ThrowFileException(exception,CorruptImageError,
1160 "UnexpectedEndOfFile",image->filename);
1163 Allocate layered image.
1165 layer_info[i].image=CloneImage(image,layer_info[i].page.width,
1166 layer_info[i].page.height == ~0U ? 1 : layer_info[i].page.height,
1167 MagickFalse,&image->exception);
1168 if (layer_info[i].image == (Image *) NULL)
1170 for (j=0; j < i; j++)
1171 layer_info[j].image=DestroyImage(layer_info[j].image);
1172 if (image->debug != MagickFalse)
1173 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1174 " allocation of image for layer %.20g failed",(double) i);
1175 ThrowReaderException(ResourceLimitError,
1176 "MemoryAllocationFailed");
1178 if (image->debug != MagickFalse)
1179 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1180 " setting up new layer image");
1181 if (image_info->ping != MagickFalse)
1182 (void) SetImageBackgroundColor(layer_info[i].image);
1183 layer_info[i].image->compose=
1184 PSDBlendModeToCompositeOperator(layer_info[i].blendkey);
1185 if (layer_info[i].visible == MagickFalse)
1186 layer_info[i].image->compose=NoCompositeOp;
1187 if (psd_info.mode == CMYKMode)
1188 layer_info[i].image->colorspace=CMYKColorspace;
1189 if ((psd_info.mode == BitmapMode) ||
1190 (psd_info.mode == GrayscaleMode) ||
1191 (psd_info.mode == DuotoneMode))
1192 layer_info[i].image->colorspace=GRAYColorspace;
1193 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1194 if (layer_info[i].channel_info[j].type == -1)
1195 layer_info[i].image->matte=MagickTrue;
1197 Set up some hidden attributes for folks that need them.
1199 (void) FormatMagickString(message,MaxTextExtent,"%.20gld",
1200 (double) layer_info[i].page.x);
1201 (void) SetImageArtifact(layer_info[i].image,"psd:layer.x",message);
1202 (void) FormatMagickString(message,MaxTextExtent,"%.20g",
1203 (double) layer_info[i].page.y);
1204 (void) SetImageArtifact(layer_info[i].image,"psd:layer.y",message);
1205 (void) FormatMagickString(message,MaxTextExtent,"%.20g",
1206 (double) layer_info[i].opacity);
1207 (void) SetImageArtifact(layer_info[i].image,"psd:layer.opacity",
1209 (void) SetImageProperty(layer_info[i].image,"label",(char *)
1210 layer_info[i].name);
1212 if (image->debug != MagickFalse)
1213 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1214 " reading image data for layers");
1216 Read pixel data for each layer.
1218 for (i=0; i < number_layers; i++)
1220 if (image->debug != MagickFalse)
1221 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1222 " reading data for layer %.20g",(double) i);
1223 for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1225 if (image->debug != MagickFalse)
1226 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1227 " reading data for channel %.20g",(double) j);
1229 if (layer_info[i].channel_info[j].size <= (2*layer_info[i].image->rows))
1234 if (image->debug != MagickFalse)
1235 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1236 " layer data is empty");
1238 A layer without data.
1240 for (k=0; k < (ssize_t) layer_info[i].channel_info[j].size; k++)
1241 (void) ReadBlobByte(layer_info[i].image);
1245 offsets=(MagickOffsetType *) NULL;
1246 layer_info[i].image->compression=NoCompression;
1247 compression=ReadBlobMSBShort(layer_info[i].image);
1248 if ((layer_info[i].page.height != 0) &&
1249 (layer_info[i].page.width != 0))
1251 if (compression == 1)
1254 Read RLE compressed data.
1256 layer_info[i].image->compression=RLECompression;
1257 if (image->debug != MagickFalse)
1258 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1259 " layer data is RLE compressed");
1260 offsets=(MagickOffsetType *) AcquireQuantumMemory(
1261 layer_info[i].image->rows,sizeof(*offsets));
1262 if (offsets == (MagickOffsetType *) NULL)
1263 ThrowReaderException(ResourceLimitError,
1264 "MemoryAllocationFailed");
1265 for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
1266 offsets[y]=GetPSDOffset(&psd_info,layer_info[i].image);
1268 status=ReadPSDLayer(layer_info[i].image,
1269 layer_info[i].channels,layer_info[i].channel_info[j].type,
1271 if (compression == 1)
1272 offsets=(MagickOffsetType *) RelinquishMagickMemory(
1274 if (status == MagickFalse)
1278 if (layer_info[i].opacity != OpaqueOpacity)
1281 Correct for opacity level.
1283 for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
1285 q=GetAuthenticPixels(layer_info[i].image,0,y,
1286 layer_info[i].image->columns,1,exception);
1287 if (q == (PixelPacket *) NULL)
1289 indexes=GetAuthenticIndexQueue(layer_info[i].image);
1290 for (x=0; x < (ssize_t) layer_info[i].image->columns; x++)
1292 q->opacity=(Quantum) (QuantumRange-(Quantum) (QuantumScale*
1293 ((QuantumRange-q->opacity)*(QuantumRange-
1294 layer_info[i].opacity))));
1297 if (SyncAuthenticPixels(layer_info[i].image,exception) == MagickFalse)
1301 if (layer_info[i].image->colorspace == CMYKColorspace)
1302 (void) NegateImage(layer_info[i].image,MagickFalse);
1303 status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType)
1305 if (status == MagickFalse)
1308 /* added by palf -> invisible group layer make layer of this group
1309 invisible I consider that all layer with width and height null are
1310 layer for group layer */
1312 short inside_layer = 0;
1313 short layer_visible = 0;
1314 for (i=number_layers-1; i >=0; i--)
1316 if ((layer_info[i].page.width == 0) ||
1317 (layer_info[i].page.height == 0))
1319 if (inside_layer == 0)
1322 layer_visible=(short int) layer_info[i].visible;
1330 if ((inside_layer == 1) && (layer_visible == 0))
1332 layer_info[i].visible=(unsigned char) layer_visible;
1333 layer_info[i].image->compose=NoCompositeOp;
1337 /* added by palf -> suppression of empty layer */
1338 /* I consider that all layer with width and height null are layer for group layer */
1339 for (i=0; i < number_layers; i++)
1341 if ((layer_info[i].page.width == 0) ||
1342 (layer_info[i].page.height == 0))
1344 if (layer_info[i].image != (Image *) NULL)
1345 layer_info[i].image=DestroyImage(layer_info[i].image);
1346 for (j=i; j < number_layers - 1; j++)
1347 layer_info[j] = layer_info[j+1];
1352 mask_size = ReadBlobMSBLong(image); /* global mask size: currently ignored */
1353 if (number_layers > 0)
1355 if (image->debug != MagickFalse)
1356 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1357 " putting layers into image list");
1358 for (i=0; i < number_layers; i++)
1361 layer_info[i].image->previous=layer_info[i-1].image;
1362 if (i < (number_layers-1))
1363 layer_info[i].image->next=layer_info[i+1].image;
1364 layer_info[i].image->page=layer_info[i].page;
1366 image->next=layer_info[0].image;
1367 layer_info[0].image->previous=image;
1368 layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info);
1370 layer_offset-=TellBlob(image);
1371 offset=SeekBlob(image,layer_offset,SEEK_CUR);
1375 Read the precombined layer, present for PSD < 4 compatibility
1377 if (image->debug != MagickFalse)
1378 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1379 " reading the precombined layer");
1380 offsets=(MagickOffsetType *) NULL;
1381 image->compression=NoCompression;
1382 compression=ReadBlobMSBShort(image);
1383 if (compression == 1)
1386 Read Packbit encoded pixel data as separate planes.
1388 image->compression=RLECompression;
1389 offsets=(MagickOffsetType *) AcquireQuantumMemory(image->rows,
1390 psd_info.channels*sizeof(*offsets));
1391 if (offsets == (MagickOffsetType *) NULL)
1392 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1393 for (i=0; i < (ssize_t) (image->rows*psd_info.channels); i++)
1394 offsets[i]=GetPSDOffset(&psd_info,image);
1396 for (i=0; i < (ssize_t) psd_info.channels; i++)
1398 status=ReadPSDLayer(image,psd_info.channels,i,offsets+i*image->rows,
1400 if (status == MagickFalse)
1402 status=SetImageProgress(image,LoadImagesTag,i,psd_info.channels);
1403 if (status == MagickFalse)
1406 if (compression == 1)
1407 offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1408 if (image->colorspace == CMYKColorspace)
1409 (void) NegateImage(image,MagickFalse);
1410 if ((number_layers == 1) && (image->next != (Image *) NULL) &&
1411 (image->matte != MagickFalse))
1412 image->next=DestroyImage(image->next);
1413 (void) CloseBlob(image);
1414 return(GetFirstImageInList(image));
1418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1422 % R e g i s t e r P S D I m a g e %
1426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1428 % RegisterPSDImage() adds properties for the PSD image format to
1429 % the list of supported formats. The properties include the image format
1430 % tag, a method to read and/or write the format, whether the format
1431 % supports the saving of more than one frame to the same file or blob,
1432 % whether the format supports native in-memory I/O, and a brief
1433 % description of the format.
1435 % The format of the RegisterPSDImage method is:
1437 % size_t RegisterPSDImage(void)
1440 ModuleExport size_t RegisterPSDImage(void)
1445 entry=SetMagickInfo("PSB");
1446 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1447 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1448 entry->magick=(IsImageFormatHandler *) IsPSD;
1449 entry->description=ConstantString("Adobe Large Document Format");
1450 entry->module=ConstantString("PSD");
1451 (void) RegisterMagickInfo(entry);
1452 entry=SetMagickInfo("PSD");
1453 entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1454 entry->encoder=(EncodeImageHandler *) WritePSDImage;
1455 entry->magick=(IsImageFormatHandler *) IsPSD;
1456 entry->description=ConstantString("Adobe Photoshop bitmap");
1457 entry->module=ConstantString("PSD");
1458 (void) RegisterMagickInfo(entry);
1459 return(MagickImageCoderSignature);
1463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1467 % U n r e g i s t e r P S D I m a g e %
1471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1473 % UnregisterPSDImage() removes format registrations made by the
1474 % PSD module from the list of supported formats.
1476 % The format of the UnregisterPSDImage method is:
1478 % UnregisterPSDImage(void)
1481 ModuleExport void UnregisterPSDImage(void)
1483 (void) UnregisterMagickInfo("PSB");
1484 (void) UnregisterMagickInfo("PSD");
1488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1492 % W r i t e P S D I m a g e %
1496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1498 % WritePSDImage() writes an image in the Adobe Photoshop encoded image format.
1500 % The format of the WritePSDImage method is:
1502 % MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image)
1504 % A description of each parameter follows.
1506 % o image_info: the image info.
1508 % o image: The image.
1513 static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image,
1514 const size_t offset)
1516 if (psd_info->version == 1)
1517 return(WriteBlobMSBShort(image,(unsigned short) offset));
1518 return(WriteBlobMSBLong(image,(unsigned short) offset));
1521 static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image,
1522 const MagickSizeType size)
1524 if (psd_info->version == 1)
1525 return(WriteBlobMSBLong(image,(unsigned int) size));
1526 return(WriteBlobMSBLongLong(image,size));
1529 static size_t PSDPackbitsEncodeImage(Image *image,const size_t length,
1530 const unsigned char *pixels,unsigned char *compact_pixels)
1539 register unsigned char
1546 Compress pixels with Packbits encoding.
1548 assert(image != (Image *) NULL);
1549 assert(image->signature == MagickSignature);
1550 if (image->debug != MagickFalse)
1551 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1552 assert(pixels != (unsigned char *) NULL);
1553 packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1554 if (packbits == (unsigned char *) NULL)
1555 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1558 for (i=(ssize_t) length; i != 0; )
1565 *q++=(unsigned char) 0;
1572 *q++=(unsigned char) 1;
1580 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1582 *q++=(unsigned char) ((256-3)+1);
1586 *q++=(unsigned char) 2;
1594 if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1600 while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
1607 *q++=(unsigned char) ((256-count)+1);
1616 while ((*(pixels+count) != *(pixels+count+1)) ||
1617 (*(pixels+count+1) != *(pixels+count+2)))
1619 packbits[count+1]=pixels[count];
1621 if (((ssize_t) count >= (i-3)) || (count >= 127))
1625 *packbits=(unsigned char) (count-1);
1626 for (j=0; j <= (ssize_t) count; j++)
1633 *q++=(unsigned char) 128; /* EOD marker */
1634 packbits=(unsigned char *) RelinquishMagickMemory(packbits);
1635 return((size_t) (q-compact_pixels));
1638 static void WritePackbitsLength(const PSDInfo *psd_info,
1639 const ImageInfo *image_info,Image *image,Image *next_image,
1640 unsigned char *compact_pixels,const QuantumType quantum_type)
1645 register const PixelPacket
1658 if (next_image->depth > 8)
1659 next_image->depth=16;
1660 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1661 quantum_info=AcquireQuantumInfo(image_info,image);
1662 pixels=GetQuantumPixels(quantum_info);
1663 for (y=0; y < (ssize_t) next_image->rows; y++)
1665 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,&image->exception);
1666 if (p == (const PixelPacket *) NULL)
1668 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
1669 quantum_type,pixels,&image->exception);
1670 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels);
1671 (void) SetPSDOffset(psd_info,image,length);
1673 quantum_info=DestroyQuantumInfo(quantum_info);
1676 static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info,
1677 Image *image,Image *next_image,unsigned char *compact_pixels,
1678 const QuantumType quantum_type,const MagickBooleanType compression_flag)
1689 register const PixelPacket
1703 if ((compression_flag != MagickFalse) &&
1704 (next_image->compression != RLECompression))
1705 (void) WriteBlobMSBShort(image,0);
1706 if (next_image->depth > 8)
1707 next_image->depth=16;
1708 monochrome=IsMonochromeImage(image,&image->exception) && (image->depth == 1)
1709 ? MagickTrue : MagickFalse;
1710 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1711 quantum_info=AcquireQuantumInfo(image_info,image);
1712 pixels=GetQuantumPixels(quantum_info);
1713 for (y=0; y < (ssize_t) next_image->rows; y++)
1715 p=GetVirtualPixels(next_image,0,y,next_image->columns,1,&image->exception);
1716 if (p == (const PixelPacket *) NULL)
1718 length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
1719 quantum_type,pixels,&image->exception);
1720 if (monochrome != MagickFalse)
1721 for (i=0; i < (ssize_t) length; i++)
1722 pixels[i]=(~pixels[i]);
1723 if (next_image->compression != RLECompression)
1724 (void) WriteBlob(image,length,pixels);
1727 length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels);
1728 (void) WriteBlob(image,length,compact_pixels);
1731 quantum_info=DestroyQuantumInfo(quantum_info);
1734 static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info,
1735 const ImageInfo *image_info,Image *image,Image *next_image,
1736 const MagickBooleanType separate)
1749 Write uncompressed pixels as separate planes.
1752 packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1753 compact_pixels=(unsigned char *) NULL;
1754 if (next_image->compression == RLECompression)
1756 compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels*
1757 next_image->columns,packet_size*sizeof(*compact_pixels));
1758 if (compact_pixels == (unsigned char *) NULL)
1759 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1762 if (IsGrayImage(next_image,&next_image->exception) != MagickFalse)
1764 if (next_image->compression == RLECompression)
1767 Packbits compression.
1769 (void) WriteBlobMSBShort(image,1);
1770 WritePackbitsLength(psd_info,image_info,image,next_image,
1771 compact_pixels,GrayQuantum);
1772 if (next_image->matte != MagickFalse)
1773 WritePackbitsLength(psd_info,image_info,image,next_image,
1774 compact_pixels,AlphaQuantum);
1776 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1777 GrayQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1779 if (next_image->matte != MagickFalse)
1780 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1781 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1783 (void) SetImageProgress(image,SaveImagesTag,0,1);
1786 if (next_image->storage_class == PseudoClass)
1788 if (next_image->compression == RLECompression)
1791 Packbits compression.
1793 (void) WriteBlobMSBShort(image,1);
1794 WritePackbitsLength(psd_info,image_info,image,next_image,
1795 compact_pixels,IndexQuantum);
1796 if (next_image->matte != MagickFalse)
1797 WritePackbitsLength(psd_info,image_info,image,next_image,
1798 compact_pixels,AlphaQuantum);
1800 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1801 IndexQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1803 if (next_image->matte != MagickFalse)
1804 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1805 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1807 (void) SetImageProgress(image,SaveImagesTag,0,1);
1811 if (next_image->colorspace == CMYKColorspace)
1812 (void) NegateImage(next_image,MagickFalse);
1813 if (next_image->compression == RLECompression)
1816 Packbits compression.
1818 (void) WriteBlobMSBShort(image,1);
1819 WritePackbitsLength(psd_info,image_info,image,next_image,
1820 compact_pixels,RedQuantum);
1821 WritePackbitsLength(psd_info,image_info,image,next_image,
1822 compact_pixels,GreenQuantum);
1823 WritePackbitsLength(psd_info,image_info,image,next_image,
1824 compact_pixels,BlueQuantum);
1825 if (next_image->colorspace == CMYKColorspace)
1826 WritePackbitsLength(psd_info,image_info,image,next_image,
1827 compact_pixels,BlackQuantum);
1828 if (next_image->matte != MagickFalse)
1829 WritePackbitsLength(psd_info,image_info,image,next_image,
1830 compact_pixels,AlphaQuantum);
1832 (void) SetImageProgress(image,SaveImagesTag,0,6);
1833 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1834 RedQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1836 (void) SetImageProgress(image,SaveImagesTag,1,6);
1837 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1838 GreenQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1840 (void) SetImageProgress(image,SaveImagesTag,2,6);
1841 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1842 BlueQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1844 (void) SetImageProgress(image,SaveImagesTag,3,6);
1845 if (next_image->colorspace == CMYKColorspace)
1846 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1847 BlackQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1849 (void) SetImageProgress(image,SaveImagesTag,4,6);
1850 if (next_image->matte != MagickFalse)
1851 WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1852 AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1854 (void) SetImageProgress(image,SaveImagesTag,5,6);
1855 if (next_image->colorspace == CMYKColorspace)
1856 (void) NegateImage(next_image,MagickFalse);
1858 if (next_image->compression == RLECompression)
1859 compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
1863 static void WritePascalString(Image* inImage,const char *inString,int inPad)
1874 length=(strlen(inString) > 255UL ) ? 255UL : strlen(inString);
1876 (void) WriteBlobByte(inImage,0);
1879 (void) WriteBlobByte(inImage,(unsigned char) length);
1880 (void) WriteBlob(inImage, length, (const unsigned char *) inString);
1883 if ((length % inPad) == 0)
1885 for (i=0; i < (ssize_t) (inPad-(length % inPad)); i++)
1886 (void) WriteBlobByte(inImage,0);
1889 static void WriteResolutionResourceBlock(Image *image)
1898 x_resolution=65536.0*image->x_resolution+0.5;
1899 y_resolution=65536.0*image->y_resolution+0.5;
1901 if (image->units == PixelsPerCentimeterResolution)
1903 x_resolution=2.54*65536.0*image->x_resolution*0.5;
1904 y_resolution=2.54*65536.0*image->y_resolution+0.5;
1907 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
1908 (void) WriteBlobMSBShort(image,0x03ED);
1909 (void) WriteBlobMSBShort(image,0);
1910 (void) WriteBlobMSBLong(image,16); /* resource size */
1911 (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5));
1912 (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */
1913 (void) WriteBlobMSBShort(image,units); /* width unit */
1914 (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5));
1915 (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */
1916 (void) WriteBlobMSBShort(image,units); /* height unit */
1919 static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile)
1921 register const unsigned char
1938 length=GetStringInfoLength(bim_profile);
1941 datum=GetStringInfoDatum(bim_profile);
1942 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
1944 register unsigned char
1947 q=(unsigned char *) p;
1948 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
1950 p=PushLongPixel(MSBEndian,p,&long_sans);
1951 p=PushShortPixel(MSBEndian,p,&id);
1952 p=PushShortPixel(MSBEndian,p,&short_sans);
1953 p=PushLongPixel(MSBEndian,p,&count);
1954 if (id == 0x0000040f)
1956 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
1957 (PSDQuantum(count)+12)-(q-datum));
1958 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
1962 if ((count & 0x01) != 0)
1967 static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile)
1969 register const unsigned char
1986 length=GetStringInfoLength(bim_profile);
1989 datum=GetStringInfoDatum(bim_profile);
1990 for (p=datum; (p >= datum) && (p < (datum+length-16)); )
1992 register unsigned char
1995 q=(unsigned char *) p;
1996 if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
1998 p=PushLongPixel(MSBEndian,p,&long_sans);
1999 p=PushShortPixel(MSBEndian,p,&id);
2000 p=PushShortPixel(MSBEndian,p,&short_sans);
2001 p=PushLongPixel(MSBEndian,p,&count);
2002 if (id == 0x000003ed)
2004 (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2005 (PSDQuantum(count)+12)-(q-datum));
2006 SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2010 if ((count & 0x01) != 0)
2015 static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image)
2044 rounded_layer_info_size;
2055 assert(image_info != (const ImageInfo *) NULL);
2056 assert(image_info->signature == MagickSignature);
2057 assert(image != (Image *) NULL);
2058 assert(image->signature == MagickSignature);
2059 if (image->debug != MagickFalse)
2060 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2061 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2062 if (status == MagickFalse)
2064 packet_size=(size_t) (image->depth > 8 ? 6 : 3);
2065 if (image->matte != MagickFalse)
2066 packet_size+=image->depth > 8 ? 2 : 1;
2068 if ((LocaleCompare(image_info->magick,"PSB") == 0) ||
2069 (image->columns > 30000) || (image->rows > 30000))
2071 (void) WriteBlob(image,4,(const unsigned char *) "8BPS");
2072 (void) WriteBlobMSBShort(image,psd_info.version); /* version */
2073 for (i=1; i <= 6; i++)
2074 (void) WriteBlobByte(image, 0); /* 6 bytes of reserved */
2075 if (IsGrayImage(image,&image->exception) != MagickFalse)
2076 num_channels=(image->matte != MagickFalse ? 2UL : 1UL);
2078 if (image->storage_class == PseudoClass)
2079 num_channels=(image->matte != MagickFalse ? 2UL : 1UL);
2082 if (image->colorspace != CMYKColorspace)
2083 num_channels=(image->matte != MagickFalse ? 4UL : 3UL);
2085 num_channels=(image->matte != MagickFalse ? 5UL : 4UL);
2087 (void) WriteBlobMSBShort(image,(unsigned short) num_channels);
2088 (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
2089 (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
2090 if (IsGrayImage(image,&image->exception) != MagickFalse)
2098 monochrome=IsMonochromeImage(image,&image->exception) &&
2099 (image->depth == 1) ? MagickTrue : MagickFalse;
2100 (void) WriteBlobMSBShort(image,(unsigned short)
2101 (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8));
2102 (void) WriteBlobMSBShort(image,monochrome != MagickFalse ? BitmapMode :
2107 (void) WriteBlobMSBShort(image,(unsigned short) (image->storage_class ==
2108 PseudoClass ? 8 : image->depth > 8 ? 16 : 8));
2109 if (((image_info->colorspace != UndefinedColorspace) ||
2110 (image->colorspace != CMYKColorspace)) &&
2111 (image_info->colorspace != CMYKColorspace))
2113 if (image->colorspace != RGBColorspace)
2114 (void) TransformImageColorspace(image,RGBColorspace);
2115 (void) WriteBlobMSBShort(image,(unsigned short)
2116 (image->storage_class == PseudoClass ? IndexedMode : RGBMode));
2120 if (image->colorspace != CMYKColorspace)
2121 (void) TransformImageColorspace(image,CMYKColorspace);
2122 (void) WriteBlobMSBShort(image,CMYKMode);
2125 if ((IsGrayImage(image,&image->exception) != MagickFalse) ||
2126 (image->storage_class == DirectClass) || (image->colors > 256))
2127 (void) WriteBlobMSBLong(image,0);
2131 Write PSD raster colormap.
2133 (void) WriteBlobMSBLong(image,768);
2134 for (i=0; i < (ssize_t) image->colors; i++)
2135 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red));
2136 for ( ; i < 256; i++)
2137 (void) WriteBlobByte(image,0);
2138 for (i=0; i < (ssize_t) image->colors; i++)
2139 (void) WriteBlobByte(image,ScaleQuantumToChar(
2140 image->colormap[i].green));
2141 for ( ; i < 256; i++)
2142 (void) WriteBlobByte(image,0);
2143 for (i=0; i < (ssize_t) image->colors; i++)
2144 (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue));
2145 for ( ; i < 256; i++)
2146 (void) WriteBlobByte(image,0);
2149 Image resource block.
2151 length=28; /* 0x03EB */
2152 bim_profile=(StringInfo *) GetImageProfile(image,"8bim");
2153 icc_profile=GetImageProfile(image,"icc");
2154 if (bim_profile != (StringInfo *) NULL)
2156 bim_profile=CloneStringInfo(bim_profile);
2157 if (icc_profile != (StringInfo *) NULL)
2158 RemoveICCProfileFromResourceBlock(bim_profile);
2159 RemoveResolutionFromResourceBlock(bim_profile);
2160 length+=PSDQuantum(GetStringInfoLength(bim_profile));
2162 if (icc_profile != (const StringInfo *) NULL)
2163 length+=PSDQuantum(GetStringInfoLength(icc_profile))+12;
2164 (void) WriteBlobMSBLong(image,(unsigned int) length);
2165 WriteResolutionResourceBlock(image);
2166 if (bim_profile != (StringInfo *) NULL)
2168 (void) WriteBlob(image,GetStringInfoLength(bim_profile),
2169 GetStringInfoDatum(bim_profile));
2170 bim_profile=DestroyStringInfo(bim_profile);
2172 if (icc_profile != (StringInfo *) NULL)
2174 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2175 (void) WriteBlobMSBShort(image,0x0000040F);
2176 (void) WriteBlobMSBShort(image,0);
2177 (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength(
2179 (void) WriteBlob(image,GetStringInfoLength(icc_profile),
2180 GetStringInfoDatum(icc_profile));
2181 if ((MagickOffsetType) GetStringInfoLength(icc_profile) !=
2182 PSDQuantum(GetStringInfoLength(icc_profile)))
2183 (void) WriteBlobByte(image,0);
2187 base_image=GetNextImageInList(image);
2188 if ((image->matte != MagickFalse) && (base_image == (Image *) NULL))
2190 next_image=base_image;
2191 while ( next_image != NULL )
2193 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2194 if (IsGrayImage(next_image,&image->exception) != MagickFalse)
2195 num_channels=next_image->matte != MagickFalse ? 2UL : 1UL;
2197 if (next_image->storage_class == PseudoClass)
2198 num_channels=next_image->matte != MagickFalse ? 2UL : 1UL;
2200 if (next_image->colorspace != CMYKColorspace)
2201 num_channels=next_image->matte != MagickFalse ? 4UL : 3UL;
2203 num_channels=next_image->matte != MagickFalse ? 5UL : 4UL;
2204 channelLength=(size_t) (next_image->columns*next_image->rows*packet_size+2);
2205 layer_info_size+=(size_t) (4*4+2+num_channels*6+(psd_info.version == 1 ? 8 :
2206 16)+4*1+4+num_channels*channelLength);
2207 property=(const char *) GetImageProperty(next_image,"label");
2208 if (property == (const char *) NULL)
2209 layer_info_size+=16;
2215 length=strlen(property);
2216 layer_info_size+=8+length+(4-(length % 4));
2219 next_image=GetNextImageInList(next_image);
2221 if (layer_count == 0)
2222 (void) SetPSDSize(&psd_info,image,0);
2228 (void) SetPSDSize(&psd_info,image,layer_info_size+
2229 (psd_info.version == 1 ? 8 : 16));
2230 if ((layer_info_size/2) != ((layer_info_size+1)/2))
2231 rounded_layer_info_size=layer_info_size+1;
2233 rounded_layer_info_size=layer_info_size;
2234 (void) SetPSDSize(&psd_info,image,rounded_layer_info_size);
2235 (void) WriteBlobMSBShort(image,(unsigned short) layer_count);
2237 compression=base_image->compression;
2238 next_image=base_image;
2239 while (next_image != NULL)
2241 next_image->compression=NoCompression;
2242 (void) WriteBlobMSBLong(image,0);
2243 (void) WriteBlobMSBLong(image,0);
2244 (void) WriteBlobMSBLong(image,(unsigned int) next_image->rows);
2245 (void) WriteBlobMSBLong(image,(unsigned int) next_image->columns);
2246 packet_size=next_image->depth > 8 ? 2UL : 1UL;
2247 channel_size=(unsigned int) ((packet_size*next_image->rows*
2248 next_image->columns)+2);
2249 if ((IsGrayImage(next_image,&image->exception) != MagickFalse) ||
2250 (next_image->storage_class == PseudoClass))
2252 (void) WriteBlobMSBShort(image,(unsigned short)
2253 (next_image->matte != MagickFalse ? 2 : 1));
2254 (void) WriteBlobMSBShort(image,0);
2255 (void) SetPSDSize(&psd_info,image,channel_size);
2256 if (next_image->matte != MagickFalse)
2258 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2259 (void) SetPSDSize(&psd_info,image,channel_size);
2263 if (next_image->colorspace != CMYKColorspace)
2265 (void) WriteBlobMSBShort(image,(unsigned short)
2266 (next_image->matte != MagickFalse ? 4 : 3));
2267 (void) WriteBlobMSBShort(image,0);
2268 (void) SetPSDSize(&psd_info,image,channel_size);
2269 (void) WriteBlobMSBShort(image,1);
2270 (void) SetPSDSize(&psd_info,image,channel_size);
2271 (void) WriteBlobMSBShort(image,2);
2272 (void) SetPSDSize(&psd_info,image,channel_size);
2273 if (next_image->matte!= MagickFalse )
2275 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2276 (void) SetPSDSize(&psd_info,image,channel_size);
2281 (void) WriteBlobMSBShort(image,(unsigned short)
2282 (next_image->matte ? 5 : 4));
2283 (void) WriteBlobMSBShort(image,0);
2284 (void) SetPSDSize(&psd_info,image,channel_size);
2285 (void) WriteBlobMSBShort(image,1);
2286 (void) SetPSDSize(&psd_info,image,channel_size);
2287 (void) WriteBlobMSBShort(image,2);
2288 (void) SetPSDSize(&psd_info,image,channel_size);
2289 (void) WriteBlobMSBShort(image,3);
2290 (void) SetPSDSize(&psd_info,image,channel_size);
2291 if (next_image->matte)
2293 (void) WriteBlobMSBShort(image,(unsigned short) -1);
2294 (void) SetPSDSize(&psd_info,image,channel_size);
2297 (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2298 (void) WriteBlob(image,4,(const unsigned char *)
2299 CompositeOperatorToPSDBlendMode(next_image->compose));
2300 (void) WriteBlobByte(image,255); /* layer opacity */
2301 (void) WriteBlobByte(image,0);
2302 (void) WriteBlobByte(image,1); /* layer propertys - visible, etc. */
2303 (void) WriteBlobByte(image,0);
2304 property=(const char *) GetImageProperty(next_image,"label");
2305 if (property == (const char *) NULL)
2307 (void) WriteBlobMSBLong(image,16);
2308 (void) WriteBlobMSBLong(image,0);
2309 (void) WriteBlobMSBLong(image,0);
2310 (void) FormatMagickString((char *) layer_name,MaxTextExtent,
2311 "L%06ld",(long) layer_count++);
2312 WritePascalString( image, (char*)layer_name, 4 );
2319 length=strlen(property);
2320 (void) WriteBlobMSBLong(image,(unsigned int) (length+(4-
2322 (void) WriteBlobMSBLong(image,0);
2323 (void) WriteBlobMSBLong(image,0);
2324 WritePascalString(image,property,4);
2326 next_image=GetNextImageInList(next_image);
2331 next_image=base_image;
2332 while (next_image != NULL)
2334 status=WriteImageChannels(&psd_info,image_info,image,next_image,
2336 next_image=GetNextImageInList(next_image);
2338 (void) WriteBlobMSBLong(image,0); /* user mask data */
2339 base_image->compression=compression;
2342 Write composite image.
2344 status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse);
2345 (void) CloseBlob(image);