]> granicus.if.org Git - imagemagick/blob - coders/psd.c
(no commit message)
[imagemagick] / coders / psd.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            PPPP   SSSSS  DDDD                               %
7 %                            P   P  SS     D   D                              %
8 %                            PPPP    SSS   D   D                              %
9 %                            P         SS  D   D                              %
10 %                            P      SSSSS  DDDD                               %
11 %                                                                             %
12 %                                                                             %
13 %                   Read/Write Adobe Photoshop Image Format                   %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                              Leonard Rosenthol                              %
18 %                                 July 1992                                   %
19 %                                                                             %
20 %                                                                             %
21 %  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
22 %  dedicated to making software imaging solutions freely available.           %
23 %                                                                             %
24 %  You may not use this file except in compliance with the License.  You may  %
25 %  obtain a copy of the License at                                            %
26 %                                                                             %
27 %    http://www.imagemagick.org/script/license.php                            %
28 %                                                                             %
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.                                             %
34 %                                                                             %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %
37 %
38 */
39 \f
40 /*
41   Include declarations.
42 */
43 #include "MagickCore/studio.h"
44 #include "MagickCore/artifact.h"
45 #include "MagickCore/attribute.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/enhance.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/image.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/list.h"
59 #include "MagickCore/log.h"
60 #include "MagickCore/magick.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/module.h"
63 #include "MagickCore/monitor-private.h"
64 #include "MagickCore/pixel.h"
65 #include "MagickCore/pixel-accessor.h"
66 #include "MagickCore/profile.h"
67 #include "MagickCore/property.h"
68 #include "MagickCore/quantum-private.h"
69 #include "MagickCore/static.h"
70 #include "MagickCore/string_.h"
71 \f
72 /*
73   Define declaractions.
74 */
75 #define MaxPSDChannels  56
76 #define PSDQuantum(x) (((ssize_t) (x)+1) & -2)
77 \f
78 /*
79   Enumerated declaractions.
80 */
81 typedef enum
82 {
83   BitmapMode = 0,
84   GrayscaleMode = 1,
85   IndexedMode = 2,
86   RGBMode = 3,
87   CMYKMode = 4,
88   MultichannelMode = 7,
89   DuotoneMode = 8,
90   LabMode = 9
91 } PSDImageType;
92 \f
93 /*
94   Typedef declaractions.
95 */
96 typedef struct _ChannelInfo
97 {
98   short int
99     type;
100
101   size_t
102     size;
103 } ChannelInfo;
104
105 typedef struct _LayerInfo
106 {
107   RectangleInfo
108     page,
109     mask;
110
111   unsigned short
112     channels;
113
114   ChannelInfo
115     channel_info[MaxPSDChannels];
116
117   char
118     blendkey[4];
119
120   Quantum
121     opacity;
122
123   unsigned char
124     clipping,
125     visible,
126     flags;
127
128   size_t
129     offset_x,
130     offset_y;
131
132   unsigned char
133     name[256];
134
135   Image
136     *image;
137 } LayerInfo;
138
139 typedef struct _PSDInfo
140 {
141   char
142     signature[4];
143
144   unsigned short
145     channels,
146     color_channels,
147     version;
148
149   unsigned char
150     reserved[6];
151
152   size_t
153     rows,
154     columns;
155
156   unsigned short
157     depth,
158     mode;
159 } PSDInfo;
160 \f
161 /*
162   Forward declarations.
163 */
164 static MagickBooleanType
165   WritePSDImage(const ImageInfo *,Image *,ExceptionInfo *);
166 \f
167 /*
168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 %                                                                             %
170 %                                                                             %
171 %                                                                             %
172 %   I s P S D                                                                 %
173 %                                                                             %
174 %                                                                             %
175 %                                                                             %
176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177 %
178 %  IsPSD()() returns MagickTrue if the image format type, identified by the
179 %  magick string, is PSD.
180 %
181 %  The format of the IsPSD method is:
182 %
183 %      MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
184 %
185 %  A description of each parameter follows:
186 %
187 %    o magick: compare image format pattern against these bytes.
188 %
189 %    o length: Specifies the length of the magick string.
190 %
191 */
192 static MagickBooleanType IsPSD(const unsigned char *magick,const size_t length)
193 {
194   if (length < 4)
195     return(MagickFalse);
196   if (LocaleNCompare((const char *) magick,"8BPS",4) == 0)
197     return(MagickTrue);
198   return(MagickFalse);
199 }
200 \f
201 /*
202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 %                                                                             %
204 %                                                                             %
205 %                                                                             %
206 %   R e a d P S D I m a g e                                                   %
207 %                                                                             %
208 %                                                                             %
209 %                                                                             %
210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211 %
212 %  ReadPSDImage() reads an Adobe Photoshop image file and returns it.  It
213 %  allocates the memory necessary for the new Image structure and returns a
214 %  pointer to the new image.
215 %
216 %  The format of the ReadPSDImage method is:
217 %
218 %      Image *ReadPSDImage(image_info)
219 %
220 %  A description of each parameter follows:
221 %
222 %    o image_info: the image info.
223 %
224 %    o exception: return any errors or warnings in this structure.
225 %
226 */
227
228 static const char *CompositeOperatorToPSDBlendMode(CompositeOperator op)
229 {
230   const char
231     *blend_mode;
232
233   switch (op)
234   {
235     case OverCompositeOp:    blend_mode = "norm";  break;
236     case MultiplyCompositeOp:  blend_mode = "mul ";  break;
237     case DissolveCompositeOp:  blend_mode = "diss";  break;
238     case DifferenceCompositeOp:  blend_mode = "diff";  break;
239     case DarkenCompositeOp:    blend_mode = "dark";  break;
240     case LightenCompositeOp:  blend_mode = "lite";  break;
241     case HueCompositeOp:    blend_mode = "hue ";  break;
242     case SaturateCompositeOp:  blend_mode = "sat ";  break;
243     case ColorizeCompositeOp:  blend_mode = "colr";  break;
244     case LuminizeCompositeOp:  blend_mode = "lum ";  break;
245     case ScreenCompositeOp:    blend_mode = "scrn";  break;
246     case OverlayCompositeOp:  blend_mode = "over";  break;
247     default:
248       blend_mode = "norm";
249   }
250   return(blend_mode);
251 }
252
253 static ssize_t DecodePSDPixels(const size_t number_compact_pixels,
254   const unsigned char *compact_pixels,const ssize_t depth,
255   const size_t number_pixels,unsigned char *pixels)
256 {
257   int
258     pixel;
259
260   register ssize_t
261     i,
262     j;
263
264   size_t
265     length;
266
267   ssize_t
268     packets;
269
270   packets=(ssize_t) number_compact_pixels;
271   for (i=0; (packets > 1) && (i < (ssize_t) number_pixels); )
272   {
273     length=(*compact_pixels++);
274     packets--;
275     if (length == 128)
276       continue;
277     if (length > 128)
278       {
279         length=256-length+1;
280         pixel=(*compact_pixels++);
281         packets--;
282         for (j=0; j < (ssize_t) length; j++)
283         {
284           switch (depth)
285           {
286             case 1:
287             {
288               *pixels++=(pixel >> 7) & 0x01 ? 0U : 255U;
289               *pixels++=(pixel >> 6) & 0x01 ? 0U : 255U;
290               *pixels++=(pixel >> 5) & 0x01 ? 0U : 255U;
291               *pixels++=(pixel >> 4) & 0x01 ? 0U : 255U;
292               *pixels++=(pixel >> 3) & 0x01 ? 0U : 255U;
293               *pixels++=(pixel >> 2) & 0x01 ? 0U : 255U;
294               *pixels++=(pixel >> 1) & 0x01 ? 0U : 255U;
295               *pixels++=(pixel >> 0) & 0x01 ? 0U : 255U;
296               i+=8;
297               break;
298             }
299             case 4:
300             {
301               *pixels++=(unsigned char) ((pixel >> 4) & 0xff);
302               *pixels++=(unsigned char) ((pixel & 0x0f) & 0xff);
303               i+=2;
304               break;
305             }
306             case 2:
307             {
308               *pixels++=(unsigned char) ((pixel >> 6) & 0x03);
309               *pixels++=(unsigned char) ((pixel >> 4) & 0x03);
310               *pixels++=(unsigned char) ((pixel >> 2) & 0x03);
311               *pixels++=(unsigned char) ((pixel & 0x03) & 0x03);
312               i+=4;
313               break;
314             }
315             default:
316             {
317               *pixels++=(unsigned char) pixel;
318               i++;
319               break;
320             }
321           }
322         }
323         continue;
324       }
325     length++;
326     for (j=0; j < (ssize_t) length; j++)
327     {
328       switch (depth)
329       {
330         case 1:
331         {
332           *pixels++=(*compact_pixels >> 7) & 0x01 ? 0U : 255U;
333           *pixels++=(*compact_pixels >> 6) & 0x01 ? 0U : 255U;
334           *pixels++=(*compact_pixels >> 5) & 0x01 ? 0U : 255U;
335           *pixels++=(*compact_pixels >> 4) & 0x01 ? 0U : 255U;
336           *pixels++=(*compact_pixels >> 3) & 0x01 ? 0U : 255U;
337           *pixels++=(*compact_pixels >> 2) & 0x01 ? 0U : 255U;
338           *pixels++=(*compact_pixels >> 1) & 0x01 ? 0U : 255U;
339           *pixels++=(*compact_pixels >> 0) & 0x01 ? 0U : 255U;
340           i+=8;
341           break;
342         }
343         case 4:
344         {
345           *pixels++=(*compact_pixels >> 4) & 0xff;
346           *pixels++=(*compact_pixels & 0x0f) & 0xff;
347           i+=2;
348           break;
349         }
350         case 2:
351         {
352           *pixels++=(*compact_pixels >> 6) & 0x03;
353           *pixels++=(*compact_pixels >> 4) & 0x03;
354           *pixels++=(*compact_pixels >> 2) & 0x03;
355           *pixels++=(*compact_pixels & 0x03) & 0x03;
356           i+=4;
357           break;
358         }
359         default:
360         {
361           *pixels++=(*compact_pixels);
362           i++;
363           break;
364         }
365       }
366       compact_pixels++;
367     }
368   }
369   return(i);
370 }
371
372 static inline MagickOffsetType GetPSDOffset(PSDInfo *psd_info,Image *image)
373 {
374   if (psd_info->version == 1)
375     return((MagickOffsetType) ReadBlobMSBShort(image));
376   return((MagickOffsetType) ReadBlobMSBLong(image));
377 }
378
379 static inline MagickSizeType GetPSDSize(PSDInfo *psd_info,Image *image)
380 {
381   if (psd_info->version == 1)
382     return((MagickSizeType) ReadBlobMSBLong(image));
383   return((MagickSizeType) ReadBlobMSBLongLong(image));
384 }
385
386 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
387 {
388   if (x < 0)
389     return(-x);
390   return(x);
391 }
392
393 static const char *ModeToString(PSDImageType type)
394 {
395   switch (type)
396   {
397     case BitmapMode: return "Bitmap";
398     case GrayscaleMode: return "Grayscale";
399     case IndexedMode: return "Indexed";
400     case RGBMode: return "RGB";
401     case CMYKMode:  return "CMYK";
402     case MultichannelMode: return "Multichannel";
403     case DuotoneMode: return "Duotone";
404     case LabMode: return "L*A*B";
405     default: return "unknown";
406   }
407 }
408
409 static MagickBooleanType ParseImageResourceBlocks(Image *image,
410   const unsigned char *blocks,size_t length,ExceptionInfo *exception)
411 {
412   const unsigned char
413     *p;
414
415   StringInfo
416     *profile;
417
418   unsigned int
419     count,
420     long_sans;
421
422   unsigned short
423     id,
424     short_sans;
425
426   if (length < 16)
427     return(MagickFalse);
428   profile=BlobToStringInfo((const void *) NULL,length);
429   SetStringInfoDatum(profile,blocks);
430   (void) SetImageProfile(image,"8bim",profile,exception);
431   profile=DestroyStringInfo(profile);
432   for (p=blocks; (p >= blocks) && (p < (blocks+length-16)); )
433   {
434     if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
435       break;
436     p=PushLongPixel(MSBEndian,p,&long_sans);
437     p=PushShortPixel(MSBEndian,p,&id);
438     p=PushShortPixel(MSBEndian,p,&short_sans);
439     p=PushLongPixel(MSBEndian,p,&count);
440     switch (id)
441     {
442       case 0x03ed:
443       {
444         char
445           value[MaxTextExtent];
446
447         unsigned short
448           resolution;
449
450         /*
451           Resolution info.
452         */
453         p=PushShortPixel(MSBEndian,p,&resolution);
454         image->resolution.x=(double) resolution;
455         (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.x);
456         (void) SetImageProperty(image,"tiff:XResolution",value,exception);
457         p=PushShortPixel(MSBEndian,p,&short_sans);
458         p=PushShortPixel(MSBEndian,p,&short_sans);
459         p=PushShortPixel(MSBEndian,p,&short_sans);
460         p=PushShortPixel(MSBEndian,p,&resolution);
461         image->resolution.y=(double) resolution;
462         (void) FormatLocaleString(value,MaxTextExtent,"%g",image->resolution.y);
463         (void) SetImageProperty(image,"tiff:YResolution",value,exception);
464         p=PushShortPixel(MSBEndian,p,&short_sans);
465         p=PushShortPixel(MSBEndian,p,&short_sans);
466         p=PushShortPixel(MSBEndian,p,&short_sans);
467         image->units=PixelsPerInchResolution;
468         break;
469       }
470       default:
471       {
472         p+=count;
473         break;
474       }
475     }
476     if ((count & 0x01) != 0)
477       p++;
478   }
479   return(MagickTrue);
480 }
481
482 static CompositeOperator PSDBlendModeToCompositeOperator(const char *mode)
483 {
484   if (mode == (const char *) NULL)
485     return(OverCompositeOp);
486   if (LocaleNCompare(mode,"norm",4) == 0)
487     return(OverCompositeOp);
488   if (LocaleNCompare(mode,"mul ",4) == 0)
489     return(MultiplyCompositeOp);
490   if (LocaleNCompare(mode,"diss",4) == 0)
491     return(DissolveCompositeOp);
492   if (LocaleNCompare(mode,"diff",4) == 0)
493     return(DifferenceCompositeOp);
494   if (LocaleNCompare(mode,"dark",4) == 0)
495     return(DarkenCompositeOp);
496   if (LocaleNCompare(mode,"lite",4) == 0)
497     return(LightenCompositeOp);
498   if (LocaleNCompare(mode,"hue ",4) == 0)
499     return(HueCompositeOp);
500   if (LocaleNCompare(mode,"sat ",4) == 0)
501     return(SaturateCompositeOp);
502   if (LocaleNCompare(mode,"colr",4) == 0)
503     return(ColorizeCompositeOp);
504   if (LocaleNCompare(mode,"lum ",4) == 0)
505     return(LuminizeCompositeOp);
506   if (LocaleNCompare(mode,"scrn",4) == 0)
507     return(ScreenCompositeOp);
508   if (LocaleNCompare(mode,"over",4) == 0)
509     return(OverlayCompositeOp);
510   if (LocaleNCompare(mode,"hLit",4) == 0)
511     return(OverCompositeOp);
512   if (LocaleNCompare(mode,"sLit",4) == 0)
513     return(OverCompositeOp);
514   if (LocaleNCompare(mode,"smud",4) == 0)
515     return(OverCompositeOp);
516   if (LocaleNCompare(mode,"div ",4) == 0)
517     return(OverCompositeOp);
518   if (LocaleNCompare(mode,"idiv",4) == 0)
519     return(OverCompositeOp);
520   return(OverCompositeOp);
521 }
522
523 static MagickBooleanType ReadPSDLayer(Image *image,const size_t channels,
524   const ssize_t type,const MagickOffsetType *offsets,ExceptionInfo *exception)
525 {
526   ColorspaceType
527     colorspace;
528
529   Quantum
530     pixel;
531
532   register const unsigned char
533     *p;
534
535   register Quantum
536     *q;
537
538   register ssize_t
539     x;
540
541   size_t
542     packet_size;
543
544   ssize_t
545     count,
546     y;
547
548   unsigned char
549     *compact_pixels,
550     *pixels;
551
552   unsigned short
553     nibble;
554
555   packet_size=1;
556   if (image->storage_class == PseudoClass)
557     {
558       if (image->colors > 256)
559         packet_size++;
560       else
561         if (image->depth > 8)
562           packet_size++;
563     }
564   else
565     if (image->depth > 8)
566       packet_size++;
567   pixels=(unsigned char *) AcquireQuantumMemory(image->columns+256,packet_size*
568     sizeof(*pixels));
569   if (pixels == (unsigned char *) NULL)
570     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
571       image->filename);
572   (void) ResetMagickMemory(pixels,0,image->columns*packet_size*sizeof(*pixels));
573   compact_pixels=(unsigned char *) NULL;
574   if (image->compression == RLECompression)
575     {
576       size_t
577         length;
578
579       length=0;
580       for (y=0; y < (ssize_t) image->rows; y++)
581         if ((MagickOffsetType) length < offsets[y])
582           length=(size_t) offsets[y];
583       compact_pixels=(unsigned char *) AcquireQuantumMemory(length+256,
584         sizeof(*pixels));
585       if (compact_pixels == (unsigned char *) NULL)
586         ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
587           image->filename);
588       (void) ResetMagickMemory(compact_pixels,0,length*sizeof(*compact_pixels));
589     }
590   colorspace=image->colorspace;
591   for (y=0; y < (ssize_t) image->rows; y++)
592   {
593     if (image->depth == 1)
594       {
595         if (image->compression != RLECompression)
596           count=ReadBlob(image,(image->columns+7)/8,pixels);
597         else
598           {
599             count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
600             if (count != (ssize_t) offsets[y])
601               break;
602             count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
603               (ssize_t) 123456,(size_t) ((image->columns+7)/8),pixels);
604           }
605         if (count < (ssize_t) ((image->columns+7)/8))
606           break;
607       }
608     else
609       {
610         if (image->compression != RLECompression)
611           count=ReadBlob(image,packet_size*image->columns,pixels);
612         else
613           {
614             count=ReadBlob(image,(size_t) offsets[y],compact_pixels);
615             if (count != (ssize_t) offsets[y])
616               break;
617             count=DecodePSDPixels((size_t) offsets[y],compact_pixels,
618               (ssize_t) image->depth,packet_size*image->columns,pixels);
619           }
620         if (count < (ssize_t) (packet_size*image->columns))
621           break;
622       }
623     q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
624     if (q == (Quantum *) NULL)
625       break;
626     p=pixels;
627     for (x=0; x < (ssize_t) image->columns; x++)
628     {
629       if (packet_size == 1)
630         pixel=ScaleCharToQuantum(*p++);
631       else
632         {
633           p=PushShortPixel(MSBEndian,p,&nibble);
634           pixel=ScaleShortToQuantum(nibble);
635         }
636       switch (type)
637       {
638         case -1:
639         {
640           SetPixelAlpha(image,pixel,q);
641           break;
642         }
643         case 0:
644         {
645           SetPixelRed(image,pixel,q);
646           if (channels == 1)
647             SetPixelGray(image,pixel,q);
648           else
649             SetPixelRed(image,pixel,q);
650           if (image->storage_class == PseudoClass)
651             {
652               if (packet_size == 1)
653                 SetPixelIndex(image,ScaleQuantumToChar(pixel),q);
654               else
655                 SetPixelIndex(image,ScaleQuantumToShort(pixel),q);
656               SetPixelInfoPixel(image,image->colormap+(ssize_t)
657                 GetPixelIndex(image,q),q);
658               if (image->depth == 1)
659                 {
660                   ssize_t
661                     bit,
662                     number_bits;
663
664                   number_bits=image->columns-x;
665                   if (number_bits > 8)
666                     number_bits=8;
667                   for (bit=0; bit < number_bits; bit++)
668                   {
669                     SetPixelIndex(image,(((unsigned char) pixel) &
670                       (0x01 << (7-bit))) != 0 ? 0 : 255,q);
671                     SetPixelInfoPixel(image,image->colormap+(ssize_t)
672                       GetPixelIndex(image,q),q);
673                     q+=GetPixelChannels(image);
674                     x++;
675                   }
676                 }
677             }
678           break;
679         }
680         case 1:
681         {
682           if (image->storage_class == PseudoClass)
683             SetPixelAlpha(image,pixel,q);
684           else
685             SetPixelGreen(image,pixel,q);
686           break;
687         }
688         case 2:
689         {
690           if (image->storage_class == PseudoClass)
691             SetPixelAlpha(image,pixel,q);
692           else
693             SetPixelBlue(image,pixel,q);
694           break;
695         }
696         case 3:
697         {
698           if (image->colorspace == CMYKColorspace)
699             SetPixelBlack(image,pixel,q);
700           else
701             if (image->alpha_trait == BlendPixelTrait)
702               SetPixelAlpha(image,pixel,q);
703           break;
704         }
705         case 4:
706         {
707           if ((IssRGBCompatibleColorspace(image->colorspace) != MagickFalse) &&
708               (channels > 3))
709             break;
710           if (image->alpha_trait == BlendPixelTrait)
711             SetPixelAlpha(image,pixel,q);
712           break;
713         }
714         default:
715           break;
716       }
717       q+=GetPixelChannels(image);
718     }
719     if (SyncAuthenticPixels(image,exception) == MagickFalse)
720       break;
721   }
722   image->colorspace=colorspace;
723   if (image->compression == RLECompression)
724     compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
725   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
726   return(MagickTrue);
727 }
728
729 static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception)
730 {
731   char
732     message[MaxTextExtent],
733     type[4];
734
735   Image
736     *image;
737
738   LayerInfo
739     *layer_info;
740
741   MagickBooleanType
742     check_background,
743     status;
744
745   MagickOffsetType
746     offset,
747     *offsets;
748
749   MagickSizeType
750     combinedlength,
751     length,
752     size;
753
754   PSDInfo
755     psd_info;
756
757   register Quantum
758     *q;
759
760   register ssize_t
761     i,
762     x;
763
764   size_t
765     mask_size,
766     skip_first_alpha = 0;
767
768   ssize_t
769     count,
770     j,
771     number_layers,
772     y;
773
774   unsigned char
775     *data;
776
777   unsigned short
778     compression;
779
780   /*
781     Open image file.
782   */
783   assert(image_info != (const ImageInfo *) NULL);
784   assert(image_info->signature == MagickSignature);
785   if (image_info->debug != MagickFalse)
786     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
787       image_info->filename);
788   assert(exception != (ExceptionInfo *) NULL);
789   assert(exception->signature == MagickSignature);
790   image=AcquireImage(image_info,exception);
791   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
792   if (status == MagickFalse)
793     {
794       image=DestroyImageList(image);
795       return((Image *) NULL);
796     }
797   /*
798     Read image header.
799   */
800   count=ReadBlob(image,4,(unsigned char *) psd_info.signature);
801   psd_info.version=ReadBlobMSBShort(image);
802   if ((count == 0) || (LocaleNCompare(psd_info.signature,"8BPS",4) != 0) ||
803       ((psd_info.version != 1) && (psd_info.version != 2)))
804     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
805   count=ReadBlob(image,6,psd_info.reserved);
806   psd_info.channels=ReadBlobMSBShort(image);
807   psd_info.color_channels=psd_info.channels;
808   if (psd_info.channels > MaxPSDChannels)
809     ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
810   psd_info.rows=ReadBlobMSBLong(image);
811   psd_info.columns=ReadBlobMSBLong(image);
812   if ((psd_info.version == 1) && ((psd_info.rows > 30000) ||
813       (psd_info.columns > 30000)))
814     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
815   psd_info.depth=ReadBlobMSBShort(image);
816   if ((psd_info.depth != 1) && (psd_info.depth != 8) && (psd_info.depth != 16))
817     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
818   psd_info.mode=ReadBlobMSBShort(image);
819   if (image->debug != MagickFalse)
820     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
821       "  Image is %.20g x %.20g with channels=%.20g, depth=%.20g, mode=%s",
822       (double) psd_info.columns,(double) psd_info.rows,(double)
823       psd_info.channels,(double) psd_info.depth,ModeToString((PSDImageType)
824       psd_info.mode));
825   /*
826     Initialize image.
827   */
828   image->depth=psd_info.depth;
829   image->columns=psd_info.columns;
830   image->rows=psd_info.rows;
831   if (SetImageBackgroundColor(image,exception) == MagickFalse)
832     {
833       image=DestroyImageList(image);
834       return((Image *) NULL);
835     }
836   image->alpha_trait=psd_info.channels >= 4 ? BlendPixelTrait :
837     UndefinedPixelTrait;
838   if (psd_info.mode == LabMode)
839     SetImageColorspace(image,LabColorspace,exception);
840   psd_info.color_channels=3;
841   if (psd_info.mode == CMYKMode)
842     {
843       psd_info.color_channels=4;
844       SetImageColorspace(image,CMYKColorspace,exception);
845       image->alpha_trait=psd_info.channels >= 5 ? BlendPixelTrait : 
846         UndefinedPixelTrait;
847     }
848   if ((psd_info.mode == BitmapMode) || (psd_info.mode == GrayscaleMode) ||
849       (psd_info.mode == DuotoneMode))
850     {
851       psd_info.color_channels=1;
852       if (AcquireImageColormap(image,256,exception) == MagickFalse)
853         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
854       image->alpha_trait=psd_info.channels >= 2 ? BlendPixelTrait : 
855         UndefinedPixelTrait;
856       if (image->debug != MagickFalse)
857         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
858           "  Image colormap allocated");
859       SetImageColorspace(image,GRAYColorspace,exception);
860     }
861   image->alpha_trait=UndefinedPixelTrait;
862   /*
863     Read PSD raster colormap only present for indexed and duotone images.
864   */
865   length=ReadBlobMSBLong(image);
866   if (length != 0)
867     {
868       if (image->debug != MagickFalse)
869         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
870           "  reading colormap");
871       if (psd_info.mode == DuotoneMode)
872         {
873           /*
874             Duotone image data;  the format of this data is undocumented.
875           */
876           data=(unsigned char *) AcquireQuantumMemory((size_t) length,
877             sizeof(*data));
878           if (data == (unsigned char *) NULL)
879             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
880           count=ReadBlob(image,(size_t) length,data);
881           data=(unsigned char *) RelinquishMagickMemory(data);
882         }
883       else
884         {
885           /*
886             Read PSD raster colormap.
887           */
888           if (AcquireImageColormap(image,(size_t) (length/3),exception) == MagickFalse)
889             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
890           for (i=0; i < (ssize_t) image->colors; i++)
891             image->colormap[i].red=ScaleCharToQuantum((unsigned char)
892               ReadBlobByte(image));
893           for (i=0; i < (ssize_t) image->colors; i++)
894             image->colormap[i].green=ScaleCharToQuantum((unsigned char)
895               ReadBlobByte(image));
896           for (i=0; i < (ssize_t) image->colors; i++)
897             image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
898               ReadBlobByte(image));
899           image->alpha_trait=UndefinedPixelTrait;
900         }
901     }
902   length=ReadBlobMSBLong(image);
903   if (length != 0)
904     {
905       unsigned char
906         *blocks;
907
908       /*
909         Image resources block.
910       */
911       if (image->debug != MagickFalse)
912         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
913           "  reading image resource blocks - %.20g bytes",(double)
914           ((MagickOffsetType) length));
915       blocks=(unsigned char *) AcquireQuantumMemory((size_t) length,
916         sizeof(*blocks));
917       if (blocks == (unsigned char *) NULL)
918         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
919       count=ReadBlob(image,(size_t) length,blocks);
920       if ((count != (ssize_t) length) ||
921           (LocaleNCompare((char *) blocks,"8BIM",4) != 0))
922         {
923           blocks=(unsigned char *) RelinquishMagickMemory(blocks);
924           ThrowReaderException(CorruptImageError,"ImproperImageHeader");
925         }
926       (void) ParseImageResourceBlocks(image,blocks,(size_t) length,
927         exception);
928       blocks=(unsigned char *) RelinquishMagickMemory(blocks);
929     }
930   /*
931     Layer and mask block.
932   */
933   layer_info=(LayerInfo *) NULL;
934   number_layers=1;
935   length=GetPSDSize(&psd_info,image);
936   if (length == 8)
937     {
938       length=ReadBlobMSBLong(image);
939       length=ReadBlobMSBLong(image);
940     }
941   check_background=MagickFalse;
942   if ((image_info->number_scenes == 1) && (image_info->scene == 0))
943     {
944       if (image->debug != MagickFalse)
945         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
946           "  read composite only");
947       check_background=MagickTrue;
948     }
949   if (length == 0)
950     {
951       if (image->debug != MagickFalse)
952         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
953           "  image has no layers");
954     }
955   else
956     {
957       offset=TellBlob(image);
958       size=GetPSDSize(&psd_info,image);
959       if (size == 0)
960         {
961           size_t
962             quantum;
963
964           unsigned long
965             tag;
966
967           /*
968             Skip layers & masks.
969           */
970           quantum=psd_info.version == 1 ? 4UL : 8UL;
971           tag=ReadBlobMSBLong(image);
972           (void) tag;
973           count=ReadBlob(image,4,(unsigned char *) type);
974           if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
975             {
976               if (DiscardBlobBytes(image,length-quantum-8) == MagickFalse)
977                 ThrowFileException(exception,CorruptImageError,
978                   "UnexpectedEndOfFile",image->filename);
979             }
980           else
981             {
982               count=ReadBlob(image,4,(unsigned char *) type);
983               if ((count != 0) && (LocaleNCompare(type,"Lr16",4) == 0))
984                 size=GetPSDSize(&psd_info,image);
985               else
986                 if (DiscardBlobBytes(image,length-quantum-12) == MagickFalse)
987                   ThrowFileException(exception,CorruptImageError,
988                     "UnexpectedEndOfFile",image->filename);
989             }
990         }
991       if (size != 0)
992         {
993           MagickOffsetType
994             layer_offset;
995
996           image->alpha_trait=psd_info.channels > psd_info.color_channels ?
997             BlendPixelTrait : UndefinedPixelTrait;
998  
999           if (image->debug != MagickFalse)
1000             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1001               image->alpha_trait ? "  image has matte" : "  image has no matte");
1002
1003           layer_offset=offset+length;
1004           number_layers=(short) ReadBlobMSBShort(image);
1005           if (image->debug != MagickFalse)
1006             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1007               "  image contains %.20g layers",(double) number_layers);
1008           if (number_layers < 0)
1009             {
1010               /*
1011                 Weird hack in PSD format to ignore first alpha channel.
1012               */
1013               skip_first_alpha=1;
1014               (void) skip_first_alpha;
1015               number_layers=MagickAbsoluteValue(number_layers);
1016               if (image->debug != MagickFalse)
1017                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1018                   "  negative layer count corrected for");
1019             }
1020           layer_info=(LayerInfo *) AcquireQuantumMemory((size_t) number_layers,
1021             sizeof(*layer_info));
1022           if (layer_info == (LayerInfo *) NULL)
1023             {
1024               if (image->debug != MagickFalse)
1025                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1026                   "  allocation of LayerInfo failed");
1027               ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1028             }
1029           (void) ResetMagickMemory(layer_info,0,(size_t) number_layers*
1030             sizeof(*layer_info));
1031           for (i=0; i < number_layers; i++)
1032           {
1033             int
1034               x,
1035               y;
1036
1037             if (image->debug != MagickFalse)
1038               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1039                 "  reading layer #%.20g",(double) i+1);
1040             layer_info[i].page.y=(int) ReadBlobMSBLong(image);
1041             layer_info[i].page.x=(int) ReadBlobMSBLong(image);
1042             y=(int) ReadBlobMSBLong(image);
1043             x=(int) ReadBlobMSBLong(image);
1044             layer_info[i].page.width=(ssize_t) (x-layer_info[i].page.x);
1045             layer_info[i].page.height=(ssize_t) (y-layer_info[i].page.y);
1046             layer_info[i].channels=ReadBlobMSBShort(image);
1047             if (check_background == MagickTrue)
1048               {
1049                 size_t
1050                   quantum;
1051
1052                 if (layer_info[i].channels == psd_info.color_channels)
1053                   image->alpha_trait=UndefinedPixelTrait;
1054                 quantum=psd_info.version == 1 ? 4UL : 8UL;
1055                 if (DiscardBlobBytes(image,length-20-quantum) == MagickFalse)
1056                   ThrowFileException(exception,CorruptImageError,
1057                     "UnexpectedEndOfFile",image->filename);
1058                 break;
1059               }
1060             if (layer_info[i].channels > MaxPSDChannels)
1061               ThrowReaderException(CorruptImageError,"MaximumChannelsExceeded");
1062             if (image->debug != MagickFalse)
1063               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1064                 "    offset(%.20g,%.20g), size(%.20g,%.20g), channels=%.20g",
1065                 (double) layer_info[i].page.x,(double) layer_info[i].page.y,
1066                 (double) layer_info[i].page.height,(double)
1067                 layer_info[i].page.width,(double) layer_info[i].channels);
1068             for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1069             {
1070               layer_info[i].channel_info[j].type=(short)
1071                 ReadBlobMSBShort(image);
1072               layer_info[i].channel_info[j].size=(size_t)
1073                 GetPSDSize(&psd_info,image);
1074               if (image->debug != MagickFalse)
1075                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1076                   "    channel[%.20g]: type=%.20g, size=%.20g",(double) j,
1077                   (double) layer_info[i].channel_info[j].type,
1078                   (double) layer_info[i].channel_info[j].size);
1079             }
1080             count=ReadBlob(image,4,(unsigned char *) type);
1081             if ((count == 0) || (LocaleNCompare(type,"8BIM",4) != 0))
1082               {
1083                 if (image->debug != MagickFalse)
1084                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1085                     "  layer type was %.4s instead of 8BIM", type);
1086                  ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1087               }
1088             count=ReadBlob(image,4,(unsigned char *) layer_info[i].blendkey);
1089             layer_info[i].opacity=(Quantum) ScaleCharToQuantum((unsigned char)
1090               ReadBlobByte(image));
1091             layer_info[i].clipping=(unsigned char) ReadBlobByte(image);
1092             layer_info[i].flags=(unsigned char) ReadBlobByte(image);
1093             layer_info[i].visible=!(layer_info[i].flags & 0x02);
1094             if (image->debug != MagickFalse)
1095               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1096                 "   blend=%.4s, opacity=%.20g, clipping=%s, flags=%d, visible=%s",
1097                 layer_info[i].blendkey,(double) layer_info[i].opacity,
1098                 layer_info[i].clipping ? "true" : "false",layer_info[i].flags,
1099                 layer_info[i].visible ? "true" : "false");
1100             (void) ReadBlobByte(image);  /* filler */
1101             combinedlength=0;
1102             size=ReadBlobMSBLong(image);
1103             if (size != 0)
1104               {
1105                 if (image->debug != MagickFalse)
1106                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1107                     "    layer contains additional info");
1108                 length=ReadBlobMSBLong(image);
1109                 if (length != 0)
1110                   {
1111                     /*
1112                       Layer mask info.
1113                     */
1114                     layer_info[i].mask.y=(int) ReadBlobMSBLong(image);
1115                     layer_info[i].mask.x=(int) ReadBlobMSBLong(image);
1116                     layer_info[i].mask.height=(size_t)
1117                       (ReadBlobMSBLong(image)-layer_info[i].mask.y);
1118                     layer_info[i].mask.width=(size_t)
1119                       (ReadBlobMSBLong(image)-layer_info[i].mask.x);
1120                     if (image->debug != MagickFalse)
1121                       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1122                         "      layer mask: offset(%.20g,%.20g), size(%.20g,%.20g), length=%.20g",
1123                         (double) layer_info[i].mask.x,(double) layer_info[i].mask.y,
1124                         (double) layer_info[i].mask.width,(double)
1125                         layer_info[i].mask.height,(double)
1126                         ((MagickOffsetType) length-16));
1127                     /*
1128                       Skip over the rest of the layer mask information.
1129                     */
1130                     if (DiscardBlobBytes(image,length-16) == MagickFalse)
1131                       ThrowFileException(exception,CorruptImageError,
1132                         "UnexpectedEndOfFile",image->filename);
1133                   }
1134                 combinedlength+=length+4;  /* +4 for length */
1135                 length=ReadBlobMSBLong(image);
1136                 if (length != 0)
1137                   {
1138                     /*
1139                       Layer blending ranges info.
1140                     */
1141                     if (image->debug != MagickFalse)
1142                       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1143                         "      layer blending ranges: length=%.20g",(double)
1144                         ((MagickOffsetType) length));
1145                     /*
1146                       We read it, but don't use it...
1147                     */
1148                     for (j=0; j < (ssize_t) (length); j+=8)
1149                     {
1150                       size_t blend_source=ReadBlobMSBLong(image);
1151                       size_t blend_dest=ReadBlobMSBLong(image);
1152                       if (image->debug != MagickFalse)
1153                         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1154                           "        source(%x), dest(%x)",(unsigned int)
1155                           blend_source,(unsigned int) blend_dest);
1156                     }
1157                   }
1158                 combinedlength+=length+4;
1159                 /*
1160                   Layer name.
1161                 */
1162                 length=(size_t) ReadBlobByte(image);
1163                 for (j=0; j < (ssize_t) length; j++)
1164                   layer_info[i].name[j]=(unsigned char) ReadBlobByte(image);
1165                 layer_info[i].name[j]='\0';
1166                 if (image->debug != MagickFalse)
1167                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1168                     "      layer name: %s",layer_info[i].name);
1169                 combinedlength+=length+1;
1170
1171 #if     0  /* still in development */
1172           /*
1173             Adjustment layers and other stuff...
1174           */
1175           {
1176             char  alsig[4], alkey[4];
1177
1178             count=ReadBlob(image,4,alsig);
1179             if ((count == 0) || (LocaleNCompare(alsig,"8BIM",4) != 0)) {
1180               if (debug != MagickFalse)
1181               {
1182                 if (image->debug != MagickFalse)
1183                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),"  adjustment layer type was %.4s instead of 8BIM", alsig);
1184               }
1185               ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1186             }
1187             count=ReadBlob(image,4,alkey);
1188             length=ReadBlobMSBLong(image);
1189               if (debug != MagickFalse)
1190               {
1191                 if (image->debug != MagickFalse)
1192                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1193                             "      adjustment layer key: %.4s, data length=%.20g",
1194                             alkey, (double) length);
1195               }
1196
1197               if ( length ) {
1198               for (j=0; j < (ssize_t) (length); j++)
1199                 (void) ReadBlobByte(image);
1200               }
1201
1202           }
1203           combinedlength += 12 + length;  /* sig, key, length + the actual length*/
1204 #endif
1205
1206                /*
1207                   Skip the rest of the variable data until we support it.
1208                 */
1209                 if (image->debug != MagickFalse)
1210                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1211                     "      unsupported data: length=%.20g",(double)
1212                     ((MagickOffsetType) (size-combinedlength)));
1213                 if (DiscardBlobBytes(image,size-combinedlength) == MagickFalse)
1214                   ThrowFileException(exception,CorruptImageError,
1215                     "UnexpectedEndOfFile",image->filename);
1216               }
1217             /*
1218               Allocate layered image.
1219             */
1220             layer_info[i].image=CloneImage(image,layer_info[i].page.width,
1221               layer_info[i].page.height == ~0U ? 1 : layer_info[i].page.height,
1222               MagickFalse,exception);
1223             if (layer_info[i].image == (Image *) NULL)
1224               {
1225                 for (j=0; j < i; j++)
1226                   layer_info[j].image=DestroyImage(layer_info[j].image);
1227                 if (image->debug != MagickFalse)
1228                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1229                     "  allocation of image for layer %.20g failed",(double) i);
1230                 ThrowReaderException(ResourceLimitError,
1231                   "MemoryAllocationFailed");
1232               }
1233             if (image->debug != MagickFalse)
1234               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1235                 "    setting up new layer image");
1236             if (image_info->ping != MagickFalse)
1237               (void) SetImageBackgroundColor(layer_info[i].image,exception);
1238             layer_info[i].image->compose=
1239               PSDBlendModeToCompositeOperator(layer_info[i].blendkey);
1240             if (layer_info[i].visible == MagickFalse)
1241               layer_info[i].image->compose=NoCompositeOp;
1242             if (psd_info.mode == CMYKMode)
1243               SetImageColorspace(layer_info[i].image,CMYKColorspace,exception);
1244             if ((psd_info.mode == BitmapMode) ||
1245                 (psd_info.mode == GrayscaleMode) ||
1246                 (psd_info.mode == DuotoneMode))
1247               SetImageColorspace(layer_info[i].image,GRAYColorspace,exception);
1248             for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1249               if (layer_info[i].channel_info[j].type == -1)
1250                 layer_info[i].image->alpha_trait=BlendPixelTrait;
1251             /*
1252               Set up some hidden attributes for folks that need them.
1253             */
1254             (void) FormatLocaleString(message,MaxTextExtent,"%.20gld",
1255               (double) layer_info[i].page.x);
1256             (void) SetImageArtifact(layer_info[i].image,"psd:layer.x",message);
1257             (void) FormatLocaleString(message,MaxTextExtent,"%.20g",
1258               (double) layer_info[i].page.y);
1259             (void) SetImageArtifact(layer_info[i].image,"psd:layer.y",message);
1260             (void) FormatLocaleString(message,MaxTextExtent,"%.20g",
1261               (double) layer_info[i].opacity);
1262             (void) SetImageArtifact(layer_info[i].image,"psd:layer.opacity",
1263               message);
1264             (void) SetImageProperty(layer_info[i].image,"label",(char *)
1265               layer_info[i].name,exception);
1266           }
1267         if (check_background == MagickFalse)
1268           {
1269            if (image->debug != MagickFalse)
1270              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1271                "  reading image data for layers");
1272            /*
1273              Read pixel data for each layer.
1274            */
1275            for (i=0; i < number_layers; i++)
1276            {
1277              if (image->debug != MagickFalse)
1278                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1279                  "  reading data for layer %.20g",(double) i);
1280                for (j=0; j < (ssize_t) layer_info[i].channels; j++)
1281                {
1282                  if (image->debug != MagickFalse)
1283                    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1284                      "    reading data for channel %.20g",(double) j);
1285 #if 1
1286                  if (layer_info[i].channel_info[j].size <= (2*layer_info[i].image->rows))
1287                    {
1288                      ssize_t
1289                        k;
1290
1291                      if (image->debug != MagickFalse)
1292                        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1293                          "      layer data is empty");
1294                      /*
1295                        A layer without data.
1296                      */
1297                      for (k=0; k < (ssize_t) layer_info[i].channel_info[j].size; k++)
1298                        (void) ReadBlobByte(layer_info[i].image);
1299                      continue;
1300                    }
1301 #endif
1302                  offsets=(MagickOffsetType *) NULL;
1303                  layer_info[i].image->compression=NoCompression;
1304                  compression=ReadBlobMSBShort(layer_info[i].image);
1305                  if ((layer_info[i].page.height != 0) &&
1306                      (layer_info[i].page.width != 0))
1307                    {
1308                      if (compression == 1)
1309                        {
1310                          /*
1311                            Read RLE compressed data.
1312                          */
1313                          layer_info[i].image->compression=RLECompression;
1314                          if (image->debug != MagickFalse)
1315                            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1316                              "      layer data is RLE compressed");
1317                          offsets=(MagickOffsetType *) AcquireQuantumMemory(
1318                            layer_info[i].image->rows,sizeof(*offsets));
1319                          if (offsets == (MagickOffsetType *) NULL)
1320                            ThrowReaderException(ResourceLimitError,
1321                              "MemoryAllocationFailed");
1322                          for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
1323                            offsets[y]=GetPSDOffset(&psd_info,
1324                              layer_info[i].image);
1325                        }
1326                      status=ReadPSDLayer(layer_info[i].image,
1327                        layer_info[i].channels,
1328                        layer_info[i].channel_info[j].type,offsets,exception);
1329                      if (compression == 1)
1330                        offsets=(MagickOffsetType *) RelinquishMagickMemory(
1331                          offsets);
1332                      if (status == MagickFalse)
1333                        break;
1334                    }
1335                  }
1336                if (layer_info[i].opacity != OpaqueAlpha)
1337                  {
1338                    /*
1339                      Correct for opacity level.
1340                    */
1341                    for (y=0; y < (ssize_t) layer_info[i].image->rows; y++)
1342                    {
1343                      q=GetAuthenticPixels(layer_info[i].image,0,y,
1344                        layer_info[i].image->columns,1,exception);
1345                      if (q == (Quantum *) NULL)
1346                        break;
1347                      for (x=0; x < (ssize_t) layer_info[i].image->columns; x++)
1348                      {
1349                        SetPixelAlpha(layer_info[i].image,(Quantum)
1350                          (QuantumScale*(GetPixelAlpha(layer_info[i].image,q))*
1351                          layer_info[i].opacity),q);
1352                        q+=GetPixelChannels(layer_info[i].image);
1353                      }
1354                      if (SyncAuthenticPixels(layer_info[i].image,exception) == MagickFalse)
1355                        break;
1356                    }
1357                  }
1358                if (layer_info[i].image->colorspace == CMYKColorspace)
1359                  (void) NegateImage(layer_info[i].image,MagickFalse,exception);
1360                status=SetImageProgress(image,LoadImagesTag,i,(MagickSizeType)
1361                  number_layers);
1362                if (status == MagickFalse)
1363                  break;
1364              }
1365            /* added by palf -> invisible group layer make layer of this group
1366               invisible I consider that all layer with width and height null are
1367               layer for group layer */
1368           {
1369             short inside_layer = 0;
1370             short layer_visible = 0;
1371             for (i=number_layers-1; i >=0; i--)
1372             {
1373               if ((layer_info[i].page.width == 0) ||
1374                   (layer_info[i].page.height == 0))
1375                 {
1376                   if (inside_layer == 0)
1377                     {
1378                       inside_layer=1;
1379                       layer_visible=(short int) layer_info[i].visible;
1380                     }
1381                   else
1382                     {
1383                       inside_layer = 0;
1384                     }
1385                 }
1386               else
1387                 if ((inside_layer == 1) && (layer_visible == 0))
1388                   {
1389                     layer_info[i].visible=(unsigned char) layer_visible;
1390                     layer_info[i].image->compose=NoCompositeOp;
1391                   }
1392             }
1393           }
1394           /* added by palf -> suppression of empty layer */
1395           /* I consider that all layer with width and height null are layer for group layer */
1396           for (i=0; i < number_layers; i++)
1397           {
1398             if ((layer_info[i].page.width == 0) ||
1399                 (layer_info[i].page.height == 0))
1400               {
1401                 if (layer_info[i].image != (Image *) NULL)
1402                   layer_info[i].image=DestroyImage(layer_info[i].image);
1403                 for (j=i; j < number_layers - 1; j++)
1404                   layer_info[j] = layer_info[j+1];
1405                 number_layers--;
1406                 i--;
1407               }
1408            }
1409            mask_size = ReadBlobMSBLong(image);  /* global mask size: currently ignored */
1410            (void) mask_size;
1411            if (number_layers > 0)
1412              {
1413                if (image->debug != MagickFalse)
1414                  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1415                    "  putting layers into image list");
1416                for (i=0; i < number_layers; i++)
1417                {
1418                  if (i > 0)
1419                    layer_info[i].image->previous=layer_info[i-1].image;
1420                  if (i < (number_layers-1))
1421                    layer_info[i].image->next=layer_info[i+1].image;
1422                  layer_info[i].image->page=layer_info[i].page;
1423                }
1424                image->next=layer_info[0].image;
1425                layer_info[0].image->previous=image;
1426                layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info);
1427              }
1428            layer_offset-=TellBlob(image);
1429            offset=SeekBlob(image,layer_offset,SEEK_CUR);
1430          }
1431       }
1432     }
1433   /*
1434     Read the precombined layer, present for PSD < 4 compatibility
1435   */
1436   if (image->debug != MagickFalse)
1437     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1438       "  reading the precombined layer");
1439   offsets=(MagickOffsetType *) NULL;
1440   image->compression=NoCompression;
1441   compression=ReadBlobMSBShort(image);
1442   if (compression == 1)
1443     {
1444       /*
1445         Read Packbit encoded pixel data as separate planes.
1446       */
1447       image->compression=RLECompression;
1448       offsets=(MagickOffsetType *) AcquireQuantumMemory(image->rows,
1449         psd_info.channels*sizeof(*offsets));
1450       if (offsets == (MagickOffsetType *) NULL)
1451         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1452       for (i=0; i < (ssize_t) (image->rows*psd_info.channels); i++)
1453         offsets[i]=GetPSDOffset(&psd_info,image);
1454     }
1455   for (i=0; i < (ssize_t) psd_info.channels; i++)
1456   {
1457     status=ReadPSDLayer(image,psd_info.channels,i,offsets+i*image->rows,
1458       exception);
1459     if (status == MagickFalse)
1460       break;
1461     status=SetImageProgress(image,LoadImagesTag,i,psd_info.channels);
1462     if (status == MagickFalse)
1463       break;
1464   }
1465   if (compression == 1)
1466     offsets=(MagickOffsetType *) RelinquishMagickMemory(offsets);
1467   if (image->colorspace == CMYKColorspace)
1468     (void) NegateImage(image,MagickFalse,exception);
1469   (void) CloseBlob(image);
1470   return(GetFirstImageInList(image));
1471 }
1472 \f
1473 /*
1474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1475 %                                                                             %
1476 %                                                                             %
1477 %                                                                             %
1478 %   R e g i s t e r P S D I m a g e                                           %
1479 %                                                                             %
1480 %                                                                             %
1481 %                                                                             %
1482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1483 %
1484 %  RegisterPSDImage() adds properties for the PSD image format to
1485 %  the list of supported formats.  The properties include the image format
1486 %  tag, a method to read and/or write the format, whether the format
1487 %  supports the saving of more than one frame to the same file or blob,
1488 %  whether the format supports native in-memory I/O, and a brief
1489 %  description of the format.
1490 %
1491 %  The format of the RegisterPSDImage method is:
1492 %
1493 %      size_t RegisterPSDImage(void)
1494 %
1495 */
1496 ModuleExport size_t RegisterPSDImage(void)
1497 {
1498   MagickInfo
1499     *entry;
1500
1501   entry=SetMagickInfo("PSB");
1502   entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1503   entry->encoder=(EncodeImageHandler *) WritePSDImage;
1504   entry->magick=(IsImageFormatHandler *) IsPSD;
1505   entry->seekable_stream=MagickTrue;
1506   entry->description=ConstantString("Adobe Large Document Format");
1507   entry->module=ConstantString("PSD");
1508   (void) RegisterMagickInfo(entry);
1509   entry=SetMagickInfo("PSD");
1510   entry->decoder=(DecodeImageHandler *) ReadPSDImage;
1511   entry->encoder=(EncodeImageHandler *) WritePSDImage;
1512   entry->magick=(IsImageFormatHandler *) IsPSD;
1513   entry->seekable_stream=MagickTrue;
1514   entry->description=ConstantString("Adobe Photoshop bitmap");
1515   entry->module=ConstantString("PSD");
1516   (void) RegisterMagickInfo(entry);
1517   return(MagickImageCoderSignature);
1518 }
1519 \f
1520 /*
1521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1522 %                                                                             %
1523 %                                                                             %
1524 %                                                                             %
1525 %   U n r e g i s t e r P S D I m a g e                                       %
1526 %                                                                             %
1527 %                                                                             %
1528 %                                                                             %
1529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1530 %
1531 %  UnregisterPSDImage() removes format registrations made by the
1532 %  PSD module from the list of supported formats.
1533 %
1534 %  The format of the UnregisterPSDImage method is:
1535 %
1536 %      UnregisterPSDImage(void)
1537 %
1538 */
1539 ModuleExport void UnregisterPSDImage(void)
1540 {
1541   (void) UnregisterMagickInfo("PSB");
1542   (void) UnregisterMagickInfo("PSD");
1543 }
1544 \f
1545 /*
1546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1547 %                                                                             %
1548 %                                                                             %
1549 %                                                                             %
1550 %   W r i t e P S D I m a g e                                                 %
1551 %                                                                             %
1552 %                                                                             %
1553 %                                                                             %
1554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1555 %
1556 %  WritePSDImage() writes an image in the Adobe Photoshop encoded image format.
1557 %
1558 %  The format of the WritePSDImage method is:
1559 %
1560 %      MagickBooleanType WritePSDImage(const ImageInfo *image_info,
1561 %        Image *image,ExceptionInfo *exception)
1562 %
1563 %  A description of each parameter follows.
1564 %
1565 %    o image_info: the image info.
1566 %
1567 %    o image:  The image.
1568 %
1569 %    o exception: return any errors or warnings in this structure.
1570 %
1571 */
1572
1573 static inline ssize_t SetPSDOffset(const PSDInfo *psd_info,Image *image,
1574   const size_t offset)
1575 {
1576   if (psd_info->version == 1)
1577     return(WriteBlobMSBShort(image,(unsigned short) offset));
1578   return(WriteBlobMSBLong(image,(unsigned short) offset));
1579 }
1580
1581 static inline ssize_t SetPSDSize(const PSDInfo *psd_info,Image *image,
1582   const MagickSizeType size)
1583 {
1584   if (psd_info->version == 1)
1585     return(WriteBlobMSBLong(image,(unsigned int) size));
1586   return(WriteBlobMSBLongLong(image,size));
1587 }
1588
1589 static size_t PSDPackbitsEncodeImage(Image *image,const size_t length,
1590   const unsigned char *pixels,unsigned char *compact_pixels,
1591   ExceptionInfo *exception)
1592 {
1593   int
1594     count;
1595
1596   register ssize_t
1597     i,
1598     j;
1599
1600   register unsigned char
1601     *q;
1602
1603   unsigned char
1604     *packbits;
1605
1606   /*
1607     Compress pixels with Packbits encoding.
1608   */
1609   assert(image != (Image *) NULL);
1610   assert(image->signature == MagickSignature);
1611   if (image->debug != MagickFalse)
1612     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1613   assert(pixels != (unsigned char *) NULL);
1614   packbits=(unsigned char *) AcquireQuantumMemory(128UL,sizeof(*packbits));
1615   if (packbits == (unsigned char *) NULL)
1616     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1617       image->filename);
1618   q=compact_pixels;
1619   for (i=(ssize_t) length; i != 0; )
1620   {
1621     switch (i)
1622     {
1623       case 1:
1624       {
1625         i--;
1626         *q++=(unsigned char) 0;
1627         *q++=(*pixels);
1628         break;
1629       }
1630       case 2:
1631       {
1632         i-=2;
1633         *q++=(unsigned char) 1;
1634         *q++=(*pixels);
1635         *q++=pixels[1];
1636         break;
1637       }
1638       case 3:
1639       {
1640         i-=3;
1641         if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1642           {
1643             *q++=(unsigned char) ((256-3)+1);
1644             *q++=(*pixels);
1645             break;
1646           }
1647         *q++=(unsigned char) 2;
1648         *q++=(*pixels);
1649         *q++=pixels[1];
1650         *q++=pixels[2];
1651         break;
1652       }
1653       default:
1654       {
1655         if ((*pixels == *(pixels+1)) && (*(pixels+1) == *(pixels+2)))
1656           {
1657             /*
1658               Packed run.
1659             */
1660             count=3;
1661             while (((ssize_t) count < i) && (*pixels == *(pixels+count)))
1662             {
1663               count++;
1664               if (count >= 127)
1665                 break;
1666             }
1667             i-=count;
1668             *q++=(unsigned char) ((256-count)+1);
1669             *q++=(*pixels);
1670             pixels+=count;
1671             break;
1672           }
1673         /*
1674           Literal run.
1675         */
1676         count=0;
1677         while ((*(pixels+count) != *(pixels+count+1)) ||
1678                (*(pixels+count+1) != *(pixels+count+2)))
1679         {
1680           packbits[count+1]=pixels[count];
1681           count++;
1682           if (((ssize_t) count >= (i-3)) || (count >= 127))
1683             break;
1684         }
1685         i-=count;
1686         *packbits=(unsigned char) (count-1);
1687         for (j=0; j <= (ssize_t) count; j++)
1688           *q++=packbits[j];
1689         pixels+=count;
1690         break;
1691       }
1692     }
1693   }
1694   *q++=(unsigned char) 128;  /* EOD marker */
1695   packbits=(unsigned char *) RelinquishMagickMemory(packbits);
1696   return((size_t) (q-compact_pixels));
1697 }
1698
1699 static void WritePackbitsLength(const PSDInfo *psd_info,
1700   const ImageInfo *image_info,Image *image,Image *next_image,
1701   unsigned char *compact_pixels,const QuantumType quantum_type,
1702   ExceptionInfo *exception)
1703 {
1704   QuantumInfo
1705     *quantum_info;
1706
1707   register const Quantum
1708     *p;
1709
1710   size_t
1711     length,
1712     packet_size;
1713
1714   ssize_t
1715     y;
1716
1717   unsigned char
1718     *pixels;
1719
1720   if (next_image->depth > 8)
1721     next_image->depth=16;
1722   packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1723   (void) packet_size;
1724   quantum_info=AcquireQuantumInfo(image_info,image);
1725   pixels=GetQuantumPixels(quantum_info);
1726   for (y=0; y < (ssize_t) next_image->rows; y++)
1727   {
1728     p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
1729     if (p == (const Quantum *) NULL)
1730       break;
1731     length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
1732       quantum_type,pixels,exception);
1733     length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
1734       exception);
1735     (void) SetPSDOffset(psd_info,image,length);
1736   }
1737   quantum_info=DestroyQuantumInfo(quantum_info);
1738 }
1739
1740 static void WriteOneChannel(const PSDInfo *psd_info,const ImageInfo *image_info,
1741   Image *image,Image *next_image,unsigned char *compact_pixels,
1742   const QuantumType quantum_type,const MagickBooleanType compression_flag,
1743   ExceptionInfo *exception)
1744 {
1745   int
1746     y;
1747
1748   MagickBooleanType
1749     monochrome;
1750
1751   QuantumInfo
1752     *quantum_info;
1753
1754   register const Quantum
1755     *p;
1756
1757   register ssize_t
1758     i;
1759
1760   size_t
1761     length,
1762     packet_size;
1763
1764   unsigned char
1765     *pixels;
1766
1767   (void) psd_info;
1768   if ((compression_flag != MagickFalse) &&
1769       (next_image->compression != RLECompression))
1770     (void) WriteBlobMSBShort(image,0);
1771   if (next_image->depth > 8)
1772     next_image->depth=16;
1773   monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
1774     MagickTrue : MagickFalse;
1775   packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1776   (void) packet_size;
1777   quantum_info=AcquireQuantumInfo(image_info,image);
1778   pixels=GetQuantumPixels(quantum_info);
1779   for (y=0; y < (ssize_t) next_image->rows; y++)
1780   {
1781     p=GetVirtualPixels(next_image,0,y,next_image->columns,1,exception);
1782     if (p == (const Quantum *) NULL)
1783       break;
1784     length=ExportQuantumPixels(next_image,(CacheView *) NULL,quantum_info,
1785       quantum_type,pixels,exception);
1786     if (monochrome != MagickFalse)
1787       for (i=0; i < (ssize_t) length; i++)
1788         pixels[i]=(~pixels[i]);
1789     if (next_image->compression != RLECompression)
1790       (void) WriteBlob(image,length,pixels);
1791     else
1792       {
1793         length=PSDPackbitsEncodeImage(image,length,pixels,compact_pixels,
1794           exception);
1795         (void) WriteBlob(image,length,compact_pixels);
1796       }
1797   }
1798   quantum_info=DestroyQuantumInfo(quantum_info);
1799 }
1800
1801 static MagickBooleanType WriteImageChannels(const PSDInfo *psd_info,
1802   const ImageInfo *image_info,Image *image,Image *next_image,
1803   const MagickBooleanType separate,ExceptionInfo *exception)
1804 {
1805   int
1806     i;
1807
1808   size_t
1809     channels,
1810     packet_size;
1811
1812   unsigned char
1813     *compact_pixels;
1814
1815   /*
1816     Write uncompressed pixels as separate planes.
1817   */
1818   channels=1;
1819   packet_size=next_image->depth > 8UL ? 2UL : 1UL;
1820   compact_pixels=(unsigned char *) NULL;
1821   if (next_image->compression == RLECompression)
1822     {
1823       compact_pixels=(unsigned char *) AcquireQuantumMemory(2*channels*
1824         next_image->columns,packet_size*sizeof(*compact_pixels));
1825       if (compact_pixels == (unsigned char *) NULL)
1826         ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1827     }
1828   i=0;
1829   if (IsImageGray(next_image,exception) != MagickFalse)
1830     {
1831       if (next_image->compression == RLECompression)
1832         {
1833           /*
1834             Packbits compression.
1835           */
1836           (void) WriteBlobMSBShort(image,1);
1837           WritePackbitsLength(psd_info,image_info,image,next_image,
1838             compact_pixels,GrayQuantum,exception);
1839           if (next_image->alpha_trait == BlendPixelTrait)
1840             WritePackbitsLength(psd_info,image_info,image,next_image,
1841               compact_pixels,AlphaQuantum,exception);
1842         }
1843       WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1844         GrayQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1845         MagickFalse,exception);
1846       if (next_image->alpha_trait == BlendPixelTrait)
1847         WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1848           AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1849           MagickFalse,exception);
1850       (void) SetImageProgress(image,SaveImagesTag,0,1);
1851     }
1852   else
1853     if (next_image->storage_class == PseudoClass)
1854       {
1855         if (next_image->compression == RLECompression)
1856           {
1857             /*
1858               Packbits compression.
1859             */
1860             (void) WriteBlobMSBShort(image,1);
1861             WritePackbitsLength(psd_info,image_info,image,next_image,
1862               compact_pixels,IndexQuantum,exception);
1863             if (next_image->alpha_trait == BlendPixelTrait)
1864               WritePackbitsLength(psd_info,image_info,image,next_image,
1865                 compact_pixels,AlphaQuantum,exception);
1866           }
1867         WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1868           IndexQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1869           MagickFalse,exception);
1870         if (next_image->alpha_trait == BlendPixelTrait)
1871           WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1872             AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1873             MagickFalse,exception);
1874         (void) SetImageProgress(image,SaveImagesTag,0,1);
1875       }
1876     else
1877       {
1878         if (next_image->colorspace == CMYKColorspace)
1879           (void) NegateImage(next_image,MagickFalse,exception);
1880         if (next_image->compression == RLECompression)
1881           {
1882             /*
1883               Packbits compression.
1884             */
1885             (void) WriteBlobMSBShort(image,1);
1886             WritePackbitsLength(psd_info,image_info,image,next_image,
1887               compact_pixels,RedQuantum,exception);
1888             WritePackbitsLength(psd_info,image_info,image,next_image,
1889               compact_pixels,GreenQuantum,exception);
1890             WritePackbitsLength(psd_info,image_info,image,next_image,
1891               compact_pixels,BlueQuantum,exception);
1892             if (next_image->colorspace == CMYKColorspace)
1893               WritePackbitsLength(psd_info,image_info,image,next_image,
1894                 compact_pixels,BlackQuantum,exception);
1895             if (next_image->alpha_trait == BlendPixelTrait)
1896               WritePackbitsLength(psd_info,image_info,image,next_image,
1897                 compact_pixels,AlphaQuantum,exception);
1898           }
1899         (void) SetImageProgress(image,SaveImagesTag,0,6);
1900         WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1901           RedQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1902           MagickFalse,exception);
1903         (void) SetImageProgress(image,SaveImagesTag,1,6);
1904         WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1905           GreenQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1906           MagickFalse,exception);
1907         (void) SetImageProgress(image,SaveImagesTag,2,6);
1908         WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1909           BlueQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1910           MagickFalse,exception);
1911         (void) SetImageProgress(image,SaveImagesTag,3,6);
1912         if (next_image->colorspace == CMYKColorspace)
1913           WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1914             BlackQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1915             MagickFalse,exception);
1916         (void) SetImageProgress(image,SaveImagesTag,4,6);
1917         if (next_image->alpha_trait == BlendPixelTrait)
1918           WriteOneChannel(psd_info,image_info,image,next_image,compact_pixels,
1919             AlphaQuantum,(i++ == 0) || (separate != MagickFalse) ? MagickTrue :
1920             MagickFalse,exception);
1921         (void) SetImageProgress(image,SaveImagesTag,5,6);
1922         if (next_image->colorspace == CMYKColorspace)
1923           (void) NegateImage(next_image,MagickFalse,exception);
1924       }
1925   if (next_image->compression == RLECompression)
1926     compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
1927   return(MagickTrue);
1928 }
1929
1930 static void WritePascalString(Image* inImage,const char *inString,int inPad)
1931 {
1932   size_t
1933     length;
1934
1935   register ssize_t
1936     i;
1937
1938   /*
1939     Max length is 255.
1940   */
1941   length=(strlen(inString) > 255UL ) ? 255UL : strlen(inString);
1942   if (length ==  0)
1943     (void) WriteBlobByte(inImage,0);
1944   else
1945     {
1946       (void) WriteBlobByte(inImage,(unsigned char) length);
1947       (void) WriteBlob(inImage, length, (const unsigned char *) inString);
1948     }
1949   length++;
1950   if ((length % inPad) == 0)
1951     return;
1952   for (i=0; i < (ssize_t) (inPad-(length % inPad)); i++)
1953     (void) WriteBlobByte(inImage,0);
1954 }
1955
1956 static void WriteResolutionResourceBlock(Image *image)
1957 {
1958   double
1959     x_resolution,
1960     y_resolution;
1961
1962   unsigned short
1963     units;
1964
1965   x_resolution=65536.0*image->resolution.x+0.5;
1966   y_resolution=65536.0*image->resolution.y+0.5;
1967   units=1;
1968   if (image->units == PixelsPerCentimeterResolution)
1969     {
1970       x_resolution=2.54*65536.0*image->resolution.x*0.5;
1971       y_resolution=2.54*65536.0*image->resolution.y+0.5;
1972       units=2;
1973     }
1974   (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
1975   (void) WriteBlobMSBShort(image,0x03ED);
1976   (void) WriteBlobMSBShort(image,0);
1977   (void) WriteBlobMSBLong(image,16); /* resource size */
1978   (void) WriteBlobMSBLong(image,(unsigned int) (x_resolution+0.5));
1979   (void) WriteBlobMSBShort(image,units); /* horizontal resolution unit */
1980   (void) WriteBlobMSBShort(image,units); /* width unit */
1981   (void) WriteBlobMSBLong(image,(unsigned int) (y_resolution+0.5));
1982   (void) WriteBlobMSBShort(image,units); /* vertical resolution unit */
1983   (void) WriteBlobMSBShort(image,units); /* height unit */
1984 }
1985
1986 static void RemoveICCProfileFromResourceBlock(StringInfo *bim_profile)
1987 {
1988   register const unsigned char
1989     *p;
1990
1991   size_t
1992     length;
1993
1994   unsigned char
1995     *datum;
1996
1997   unsigned int
1998     count,
1999     long_sans;
2000
2001   unsigned short
2002     id,
2003     short_sans;
2004
2005   length=GetStringInfoLength(bim_profile);
2006   if (length < 16)
2007     return;
2008   datum=GetStringInfoDatum(bim_profile);
2009   for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2010   {
2011     register unsigned char
2012       *q;
2013
2014     q=(unsigned char *) p;
2015     if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2016       break;
2017     p=PushLongPixel(MSBEndian,p,&long_sans);
2018     p=PushShortPixel(MSBEndian,p,&id);
2019     p=PushShortPixel(MSBEndian,p,&short_sans);
2020     p=PushLongPixel(MSBEndian,p,&count);
2021     if (id == 0x0000040f)
2022       {
2023         (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2024           (PSDQuantum(count)+12)-(q-datum));
2025         SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2026         break;
2027       }
2028     p+=count;
2029     if ((count & 0x01) != 0)
2030       p++;
2031   }
2032 }
2033
2034 static void RemoveResolutionFromResourceBlock(StringInfo *bim_profile)
2035 {
2036   register const unsigned char
2037     *p;
2038
2039   size_t
2040     length;
2041
2042   unsigned char
2043     *datum;
2044
2045   unsigned int
2046     count,
2047     long_sans;
2048
2049   unsigned short
2050     id,
2051     short_sans;
2052
2053   length=GetStringInfoLength(bim_profile);
2054   if (length < 16)
2055     return;
2056   datum=GetStringInfoDatum(bim_profile);
2057   for (p=datum; (p >= datum) && (p < (datum+length-16)); )
2058   {
2059     register unsigned char
2060       *q;
2061
2062     q=(unsigned char *) p;
2063     if (LocaleNCompare((const char *) p,"8BIM",4) != 0)
2064       break;
2065     p=PushLongPixel(MSBEndian,p,&long_sans);
2066     p=PushShortPixel(MSBEndian,p,&id);
2067     p=PushShortPixel(MSBEndian,p,&short_sans);
2068     p=PushLongPixel(MSBEndian,p,&count);
2069     if ((id == 0x000003ed) && (PSDQuantum(count) < (ssize_t) (length-12)))
2070       {
2071         (void) CopyMagickMemory(q,q+PSDQuantum(count)+12,length-
2072           (PSDQuantum(count)+12)-(q-datum));
2073         SetStringInfoLength(bim_profile,length-(PSDQuantum(count)+12));
2074         break;
2075       }
2076     p+=count;
2077     if ((count & 0x01) != 0)
2078       p++;
2079   }
2080 }
2081
2082 static MagickBooleanType WritePSDImage(const ImageInfo *image_info,Image *image,
2083   ExceptionInfo *exception)
2084 {
2085   const char
2086     *property;
2087
2088   const StringInfo
2089     *icc_profile;
2090
2091   Image
2092     *base_image,
2093     *next_image;
2094
2095   MagickBooleanType
2096     status;
2097
2098   PSDInfo
2099     psd_info;
2100
2101   register ssize_t
2102     i;
2103
2104   size_t
2105     channel_size,
2106     channelLength,
2107     layer_count,
2108     layer_info_size,
2109     length,
2110     num_channels,
2111     packet_size,
2112     rounded_layer_info_size;
2113
2114   StringInfo
2115     *bim_profile;
2116
2117   unsigned char
2118     layer_name[4];
2119
2120   /*
2121     Open image file.
2122   */
2123   assert(image_info != (const ImageInfo *) NULL);
2124   assert(image_info->signature == MagickSignature);
2125   assert(image != (Image *) NULL);
2126   assert(image->signature == MagickSignature);
2127   if (image->debug != MagickFalse)
2128     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2129   assert(exception != (ExceptionInfo *) NULL);
2130   assert(exception->signature == MagickSignature);
2131   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2132   if (status == MagickFalse)
2133     return(status);
2134   packet_size=(size_t) (image->depth > 8 ? 6 : 3);
2135   if (image->alpha_trait == BlendPixelTrait)
2136     packet_size+=image->depth > 8 ? 2 : 1;
2137   psd_info.version=1;
2138   if ((LocaleCompare(image_info->magick,"PSB") == 0) ||
2139       (image->columns > 30000) || (image->rows > 30000))
2140     psd_info.version=2;
2141   (void) WriteBlob(image,4,(const unsigned char *) "8BPS");
2142   (void) WriteBlobMSBShort(image,psd_info.version);  /* version */
2143   for (i=1; i <= 6; i++)
2144     (void) WriteBlobByte(image, 0);  /* 6 bytes of reserved */
2145   if (IsImageGray(image,exception) != MagickFalse)
2146     num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
2147   else
2148     if (image->storage_class == PseudoClass)
2149       num_channels=(image->alpha_trait == BlendPixelTrait ? 2UL : 1UL);
2150     else
2151       {
2152         if (image->colorspace != CMYKColorspace)
2153           num_channels=(image->alpha_trait == BlendPixelTrait ? 4UL : 3UL);
2154         else
2155           num_channels=(image->alpha_trait == BlendPixelTrait ? 5UL : 4UL);
2156       }
2157   (void) WriteBlobMSBShort(image,(unsigned short) num_channels);
2158   (void) WriteBlobMSBLong(image,(unsigned int) image->rows);
2159   (void) WriteBlobMSBLong(image,(unsigned int) image->columns);
2160   if (IsImageGray(image,exception) != MagickFalse)
2161     {
2162       MagickBooleanType
2163         monochrome;
2164
2165       /*
2166         Write depth & mode.
2167       */
2168       monochrome=IsImageMonochrome(image,exception) && (image->depth == 1) ?
2169         MagickTrue : MagickFalse;
2170       (void) WriteBlobMSBShort(image,(unsigned short)
2171         (monochrome != MagickFalse ? 1 : image->depth > 8 ? 16 : 8));
2172       (void) WriteBlobMSBShort(image,(unsigned short)
2173         (monochrome != MagickFalse ? BitmapMode : GrayscaleMode));
2174     }
2175   else
2176     {
2177       (void) WriteBlobMSBShort(image,(unsigned short) (image->storage_class ==
2178         PseudoClass ? 8 : image->depth > 8 ? 16 : 8));
2179       if (((image_info->colorspace != UndefinedColorspace) ||
2180            (image->colorspace != CMYKColorspace)) &&
2181           (image_info->colorspace != CMYKColorspace))
2182         {
2183           if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
2184             (void) TransformImageColorspace(image,sRGBColorspace,exception);
2185           (void) WriteBlobMSBShort(image,(unsigned short)
2186             (image->storage_class == PseudoClass ? IndexedMode : RGBMode));
2187         }
2188       else
2189         {
2190           if (image->colorspace != CMYKColorspace)
2191             (void) TransformImageColorspace(image,CMYKColorspace,exception);
2192           (void) WriteBlobMSBShort(image,CMYKMode);
2193         }
2194     }
2195   if ((IsImageGray(image,exception) != MagickFalse) ||
2196       (image->storage_class == DirectClass) || (image->colors > 256))
2197     (void) WriteBlobMSBLong(image,0);
2198   else
2199     {
2200       /*
2201         Write PSD raster colormap.
2202       */
2203       (void) WriteBlobMSBLong(image,768);
2204       for (i=0; i < (ssize_t) image->colors; i++)
2205         (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].red));
2206       for ( ; i < 256; i++)
2207         (void) WriteBlobByte(image,0);
2208       for (i=0; i < (ssize_t) image->colors; i++)
2209         (void) WriteBlobByte(image,ScaleQuantumToChar(
2210           image->colormap[i].green));
2211       for ( ; i < 256; i++)
2212         (void) WriteBlobByte(image,0);
2213       for (i=0; i < (ssize_t) image->colors; i++)
2214         (void) WriteBlobByte(image,ScaleQuantumToChar(image->colormap[i].blue));
2215       for ( ; i < 256; i++)
2216         (void) WriteBlobByte(image,0);
2217     }
2218   /*
2219     Image resource block.
2220   */
2221   length=28; /* 0x03EB */
2222   bim_profile=(StringInfo *) GetImageProfile(image,"8bim");
2223   icc_profile=GetImageProfile(image,"icc");
2224   if (bim_profile != (StringInfo *) NULL)
2225     {
2226       bim_profile=CloneStringInfo(bim_profile);
2227       if (icc_profile != (StringInfo *) NULL)
2228         RemoveICCProfileFromResourceBlock(bim_profile);
2229       RemoveResolutionFromResourceBlock(bim_profile);
2230       length+=PSDQuantum(GetStringInfoLength(bim_profile));
2231     }
2232   if (icc_profile != (const StringInfo *) NULL)
2233     length+=PSDQuantum(GetStringInfoLength(icc_profile))+12;
2234   (void) WriteBlobMSBLong(image,(unsigned int) length);
2235   WriteResolutionResourceBlock(image);
2236   if (bim_profile != (StringInfo *) NULL)
2237     {
2238       (void) WriteBlob(image,GetStringInfoLength(bim_profile),
2239         GetStringInfoDatum(bim_profile));
2240       bim_profile=DestroyStringInfo(bim_profile);
2241     }
2242   if (icc_profile != (StringInfo *) NULL)
2243     {
2244       (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2245       (void) WriteBlobMSBShort(image,0x0000040F);
2246       (void) WriteBlobMSBShort(image,0);
2247       (void) WriteBlobMSBLong(image,(unsigned int) GetStringInfoLength(
2248         icc_profile));
2249       (void) WriteBlob(image,GetStringInfoLength(icc_profile),
2250         GetStringInfoDatum(icc_profile));
2251       if ((MagickOffsetType) GetStringInfoLength(icc_profile) !=
2252           PSDQuantum(GetStringInfoLength(icc_profile)))
2253         (void) WriteBlobByte(image,0);
2254     }
2255   layer_count=0;
2256   layer_info_size=2;
2257   base_image=GetNextImageInList(image);
2258   if ((image->alpha_trait == BlendPixelTrait) && (base_image == (Image *) NULL))
2259     base_image=image;
2260   next_image=base_image;
2261   while ( next_image != NULL )
2262   {
2263     packet_size=next_image->depth > 8 ? 2UL : 1UL;
2264     if (IsImageGray(next_image,exception) != MagickFalse)
2265       num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
2266     else
2267       if (next_image->storage_class == PseudoClass)
2268         num_channels=next_image->alpha_trait == BlendPixelTrait ? 2UL : 1UL;
2269       else
2270         if (next_image->colorspace != CMYKColorspace)
2271           num_channels=next_image->alpha_trait == BlendPixelTrait ? 4UL : 3UL;
2272         else
2273           num_channels=next_image->alpha_trait == BlendPixelTrait ? 5UL : 4UL;
2274     channelLength=(size_t) (next_image->columns*next_image->rows*packet_size+2);
2275     layer_info_size+=(size_t) (4*4+2+num_channels*6+(psd_info.version == 1 ? 8 :
2276       16)+4*1+4+num_channels*channelLength);
2277     property=(const char *) GetImageProperty(next_image,"label",exception);
2278     if (property == (const char *) NULL)
2279       layer_info_size+=16;
2280     else
2281       {
2282         size_t
2283           length;
2284
2285         length=strlen(property);
2286         layer_info_size+=8+length+(4-(length % 4));
2287       }
2288     layer_count++;
2289     next_image=GetNextImageInList(next_image);
2290   }
2291   if (layer_count == 0)
2292     (void) SetPSDSize(&psd_info,image,0);
2293   else
2294     {
2295       CompressionType
2296         compression;
2297
2298       (void) SetPSDSize(&psd_info,image,layer_info_size+
2299         (psd_info.version == 1 ? 8 : 16));
2300       if ((layer_info_size/2) != ((layer_info_size+1)/2))
2301         rounded_layer_info_size=layer_info_size+1;
2302       else
2303         rounded_layer_info_size=layer_info_size;
2304       (void) SetPSDSize(&psd_info,image,rounded_layer_info_size);
2305       (void) WriteBlobMSBShort(image,(unsigned short) layer_count);
2306       layer_count=1;
2307       compression=base_image->compression;
2308       next_image=base_image;
2309       while (next_image != NULL)
2310       {
2311         next_image->compression=NoCompression;
2312         (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y);
2313         (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x);
2314         (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.y+
2315           next_image->rows);
2316         (void) WriteBlobMSBLong(image,(unsigned int) next_image->page.x+
2317           next_image->columns);
2318         packet_size=next_image->depth > 8 ? 2UL : 1UL;
2319         channel_size=(unsigned int) ((packet_size*next_image->rows*
2320           next_image->columns)+2);
2321         if ((IsImageGray(next_image,exception) != MagickFalse) ||
2322             (next_image->storage_class == PseudoClass))
2323           {
2324              (void) WriteBlobMSBShort(image,(unsigned short)
2325                (next_image->alpha_trait == BlendPixelTrait ? 2 : 1));
2326              (void) WriteBlobMSBShort(image,0);
2327              (void) SetPSDSize(&psd_info,image,channel_size);
2328              if (next_image->alpha_trait == BlendPixelTrait)
2329                {
2330                  (void) WriteBlobMSBShort(image,(unsigned short) -1);
2331                  (void) SetPSDSize(&psd_info,image,channel_size);
2332                }
2333            }
2334           else
2335             if (next_image->colorspace != CMYKColorspace)
2336               {
2337                 (void) WriteBlobMSBShort(image,(unsigned short)
2338                   (next_image->alpha_trait == BlendPixelTrait ? 4 : 3));
2339                (void) WriteBlobMSBShort(image,0);
2340                (void) SetPSDSize(&psd_info,image,channel_size);
2341                (void) WriteBlobMSBShort(image,1);
2342                (void) SetPSDSize(&psd_info,image,channel_size);
2343                (void) WriteBlobMSBShort(image,2);
2344                (void) SetPSDSize(&psd_info,image,channel_size);
2345                if (next_image->alpha_trait == BlendPixelTrait)
2346                  {
2347                    (void) WriteBlobMSBShort(image,(unsigned short) -1);
2348                    (void) SetPSDSize(&psd_info,image,channel_size);
2349                  }
2350              }
2351            else
2352              {
2353                (void) WriteBlobMSBShort(image,(unsigned short)
2354                  (next_image->alpha_trait ? 5 : 4));
2355                (void) WriteBlobMSBShort(image,0);
2356                (void) SetPSDSize(&psd_info,image,channel_size);
2357                (void) WriteBlobMSBShort(image,1);
2358                (void) SetPSDSize(&psd_info,image,channel_size);
2359                (void) WriteBlobMSBShort(image,2);
2360                (void) SetPSDSize(&psd_info,image,channel_size);
2361                (void) WriteBlobMSBShort(image,3);
2362                (void) SetPSDSize(&psd_info,image,channel_size);
2363                if (next_image->alpha_trait)
2364                  {
2365                    (void) WriteBlobMSBShort(image,(unsigned short) -1);
2366                    (void) SetPSDSize(&psd_info,image,channel_size);
2367                  }
2368              }
2369         (void) WriteBlob(image,4,(const unsigned char *) "8BIM");
2370         (void) WriteBlob(image,4,(const unsigned char *)
2371           CompositeOperatorToPSDBlendMode(next_image->compose));
2372         (void) WriteBlobByte(image,255); /* layer opacity */
2373         (void) WriteBlobByte(image,0);
2374         (void) WriteBlobByte(image,1); /* layer propertys - visible, etc. */
2375         (void) WriteBlobByte(image,0);
2376         property=(const char *) GetImageProperty(next_image,"label",exception);
2377         if (property == (const char *) NULL)
2378           {
2379             (void) WriteBlobMSBLong(image,16);
2380             (void) WriteBlobMSBLong(image,0);
2381             (void) WriteBlobMSBLong(image,0);
2382             (void) FormatLocaleString((char *) layer_name,MaxTextExtent,
2383               "L%06ld",(long) layer_count++);
2384             WritePascalString( image, (char*)layer_name, 4 );
2385           }
2386         else
2387           {
2388             size_t
2389               length;
2390
2391             length=strlen(property);
2392             (void) WriteBlobMSBLong(image,(unsigned int) (length+(4-
2393               (length % 4))+8));
2394             (void) WriteBlobMSBLong(image,0);
2395             (void) WriteBlobMSBLong(image,0);
2396             WritePascalString(image,property,4);
2397           }
2398         next_image=GetNextImageInList(next_image);
2399       }
2400       /*
2401         Now the image data!
2402       */
2403       next_image=base_image;
2404       while (next_image != NULL)
2405       {
2406         status=WriteImageChannels(&psd_info,image_info,image,next_image,
2407           MagickTrue,exception);
2408         next_image=GetNextImageInList(next_image);
2409       }
2410       (void) WriteBlobMSBLong(image,0);  /* user mask data */
2411       base_image->compression=compression;
2412     }
2413   /*
2414     Write composite image.
2415   */
2416   status=WriteImageChannels(&psd_info,image_info,image,image,MagickFalse,
2417     exception);
2418   (void) CloseBlob(image);
2419   return(status);
2420 }