#include "MagickCore/pixel-accessor.h"
#include "MagickCore/profile.h"
#include "MagickCore/property.h"
+#include "MagickCore/registry.h"
#include "MagickCore/quantum-private.h"
#include "MagickCore/static.h"
#include "MagickCore/string_.h"
}
}
-static MagickBooleanType CorrectPSDOpacity(LayerInfo *layer_info,
- ExceptionInfo *exception)
+static MagickBooleanType ApplyPSDLayerOpacity(Image *image,Quantum opacity,
+ MagickBooleanType revert,ExceptionInfo *exception)
{
MagickBooleanType
status;
ssize_t
y;
- if (layer_info->opacity == OpaqueAlpha)
+ if (opacity == OpaqueAlpha)
return(MagickTrue);
- layer_info->image->alpha_trait=BlendPixelTrait;
+ image->alpha_trait=BlendPixelTrait;
status=MagickTrue;
#if defined(MAGICKCORE_OPENMP_SUPPORT)
#pragma omp parallel for schedule(static,4) shared(status) \
- magick_threads(layer_info->image,layer_info->image,layer_info->image->rows,1)
+ magick_threads(image,image,image->rows,1)
#endif
- for (y=0; y < (ssize_t) layer_info->image->rows; y++)
+ for (y=0; y < (ssize_t) image->rows; y++)
{
register Quantum
*magick_restrict q;
if (status == MagickFalse)
continue;
- q=GetAuthenticPixels(layer_info->image,0,y,layer_info->image->columns,1,
- exception);
+ q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
if (q == (Quantum *)NULL)
{
status=MagickFalse;
continue;
}
- for (x=0; x < (ssize_t) layer_info->image->columns; x++)
+ for (x=0; x < (ssize_t) image->columns; x++)
{
- SetPixelAlpha(layer_info->image,(Quantum) (QuantumScale*(GetPixelAlpha(
- layer_info->image,q))*layer_info->opacity),q);
- q+=GetPixelChannels(layer_info->image);
+ if (revert == MagickFalse)
+ SetPixelAlpha(image,(Quantum) (QuantumScale*(GetPixelAlpha(image,q))*
+ opacity),q);
+ else if (opacity > 0)
+ SetPixelAlpha(image,(Quantum) (QuantumRange*(GetPixelAlpha(image,q)/
+ opacity)),q);
+ q+=GetPixelChannels(image);
}
- if (SyncAuthenticPixels(layer_info->image,exception) == MagickFalse)
+ if (SyncAuthenticPixels(image,exception) == MagickFalse)
status=MagickFalse;
}
return(status);
}
+static MagickBooleanType ApplyPSDOpacityMask(Image *image,const Image *mask,
+ Quantum background,MagickBooleanType revert,ExceptionInfo *exception)
+{
+ Image
+ *complete_mask;
+
+ MagickBooleanType
+ status;
+
+ PixelInfo
+ color;
+
+ ssize_t
+ y;
+
+ complete_mask=CloneImage(image,image->columns,image->rows,MagickTrue,
+ exception);
+ complete_mask->alpha_trait=BlendPixelTrait;
+ GetPixelInfo(complete_mask,&color);
+ color.red=background;
+ SetImageColor(complete_mask,&color,exception);
+ status=CompositeImage(complete_mask,mask,OverCompositeOp,MagickTrue,
+ mask->page.x-image->page.x,mask->page.y-image->page.y,exception);
+ if (status == MagickFalse)
+ {
+ complete_mask=DestroyImage(complete_mask);
+ return(status);
+ }
+ image->alpha_trait=BlendPixelTrait;
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+#pragma omp parallel for schedule(static,4) shared(status) \
+ magick_threads(image,image,image->rows,1)
+#endif
+ for (y=0; y < (ssize_t) image->rows; y++)
+ {
+ register Quantum
+ *magick_restrict q;
+
+ register Quantum
+ *p;
+
+ register ssize_t
+ x;
+
+ if (status == MagickFalse)
+ continue;
+ q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
+ p=GetAuthenticPixels(complete_mask,0,y,complete_mask->columns,1,exception);
+ if ((q == (Quantum *) NULL) || (p == (Quantum *) NULL))
+ {
+ status=MagickFalse;
+ continue;
+ }
+ for (x=0; x < (ssize_t) image->columns; x++)
+ {
+ MagickRealType
+ alpha,
+ intensity;
+
+ alpha=GetPixelAlpha(image,q);
+ intensity=GetPixelIntensity(complete_mask,p);
+ if (revert == MagickFalse)
+ SetPixelAlpha(image,ClampToQuantum(intensity*(QuantumScale*alpha)),q);
+ else if (intensity > 0)
+ SetPixelAlpha(image,ClampToQuantum((alpha/intensity)*QuantumRange),q);
+ q+=GetPixelChannels(image);
+ p+=GetPixelChannels(complete_mask);
+ }
+ if (SyncAuthenticPixels(image,exception) == MagickFalse)
+ status=MagickFalse;
+ }
+ complete_mask=DestroyImage(complete_mask);
+
+ return(status);
+}
+
static ssize_t DecodePSDPixels(const size_t number_compact_pixels,
const unsigned char *compact_pixels,const ssize_t depth,
const size_t number_pixels,unsigned char *pixels)
}
#endif
-static MagickBooleanType ReadPSDChannel(Image *image,const PSDInfo *psd_info,
- LayerInfo* layer_info,const size_t channel,
- const PSDCompressionType compression,ExceptionInfo *exception)
+static MagickBooleanType ReadPSDChannel(Image *image,
+ const ImageInfo *image_info,const PSDInfo *psd_info,LayerInfo* layer_info,
+ const size_t channel,const PSDCompressionType compression,
+ ExceptionInfo *exception)
{
Image
*channel_image,
mask=(Image *) NULL;
if (layer_info->channel_info[channel].type < -1)
{
+ const char
+ *option;
+
/*
Ignore mask that is not a user supplied layer mask, if the mask is
disabled or if the flags have unsupported values.
*/
+ option=GetImageOption(image_info,"psd:preserve-opacity-mask");
if ((layer_info->channel_info[channel].type != -2) ||
- (layer_info->mask.flags > 3) || (layer_info->mask.flags & 0x02))
+ (layer_info->mask.flags > 2) || ((layer_info->mask.flags & 0x02) &&
+ (IsStringTrue(option) == MagickFalse)))
{
SeekBlob(image,layer_info->channel_info[channel].size-2,SEEK_CUR);
return(MagickTrue);
if (mask != (Image *) NULL)
{
if (status != MagickFalse)
- {
- PixelInfo
- color;
-
- layer_info->mask.image=CloneImage(image,image->columns,image->rows,
- MagickTrue,exception);
- (void) SetImageGray(layer_info->mask.image,exception);
- layer_info->mask.image->alpha_trait=UndefinedPixelTrait;
- GetPixelInfo(layer_info->mask.image,&color);
- color.red=layer_info->mask.background == 0 ? 0 : QuantumRange;
- SetImageColor(layer_info->mask.image,&color,exception);
- (void) CompositeImage(layer_info->mask.image,mask,OverCompositeOp,
- MagickTrue,layer_info->mask.page.x,layer_info->mask.page.y,
- exception);
- }
- DestroyImage(mask);
+ layer_info->mask.image=mask;
+ else
+ mask=DestroyImage(mask);
}
return(status);
}
-static MagickBooleanType ReadPSDLayer(Image *image,const PSDInfo *psd_info,
- LayerInfo* layer_info,ExceptionInfo *exception)
+static MagickBooleanType ReadPSDLayer(Image *image,const ImageInfo *image_info,
+ const PSDInfo *psd_info,LayerInfo* layer_info,ExceptionInfo *exception)
{
char
message[MagickPathExtent];
if (layer_info->channel_info[j].type == -1)
layer_info->image->alpha_trait=BlendPixelTrait;
- status=ReadPSDChannel(layer_info->image,psd_info,layer_info,j,
+ status=ReadPSDChannel(layer_info->image,image_info,psd_info,layer_info,j,
compression,exception);
if (status == MagickFalse)
}
if (status != MagickFalse)
- status=CorrectPSDOpacity(layer_info,exception);
+ status=ApplyPSDLayerOpacity(layer_info->image,layer_info->opacity,
+ MagickFalse,exception);
if ((status != MagickFalse) &&
(layer_info->image->colorspace == CMYKColorspace))
if ((status != MagickFalse) && (layer_info->mask.image != (Image *) NULL))
{
- status=CompositeImage(layer_info->image,layer_info->mask.image,
- CopyAlphaCompositeOp,MagickTrue,0,0,exception);
+ const char
+ *option;
+
+ layer_info->mask.image->page.x=layer_info->mask.page.x;
+ layer_info->mask.image->page.y=layer_info->mask.page.y;
+ /* Do not composite the mask when it is disabled */
+ if ((layer_info->mask.flags & 0x02) == 0x02)
+ layer_info->mask.image->compose=NoCompositeOp;
+ else
+ status=ApplyPSDOpacityMask(layer_info->image,layer_info->mask.image,
+ layer_info->mask.background == 0 ? 0 : QuantumRange,MagickFalse,
+ exception);
+ option=GetImageOption(image_info,"psd:preserve-opacity-mask");
+ if (IsStringTrue(option) != MagickFalse)
+ {
+ char
+ *key;
+
+ RandomInfo
+ *random_info;
+
+ StringInfo
+ *key_info;
+
+ random_info=AcquireRandomInfo();
+ key_info=GetRandomKey(random_info,2+1);
+ key=(char *) GetStringInfoDatum(key_info);
+ key[8]=layer_info->mask.background;
+ key[9]='\0';
+ layer_info->mask.image->page.x+=layer_info->page.x;
+ layer_info->mask.image->page.y+=layer_info->page.y;
+ (void) SetImageRegistry(ImageRegistryType,(const char *) key,
+ layer_info->mask.image,exception);
+ (void) SetImageArtifact(layer_info->image,"psd:opacity-mask",
+ (const char *) key);
+ key_info=DestroyStringInfo(key_info);
+ random_info=DestroyRandomInfo(random_info);
+ }
layer_info->mask.image=DestroyImage(layer_info->mask.image);
}
(void) LogMagickEvent(CoderEvent,GetMagickModule(),
" reading data for layer %.20g",(double) i);
- status=ReadPSDLayer(image,psd_info,&layer_info[i],exception);
+ status=ReadPSDLayer(image,image_info,psd_info,&layer_info[i],exception);
if (status == MagickFalse)
break;
compact_pixels=(unsigned char *) RelinquishMagickMemory(compact_pixels);
if (next_image->colorspace == CMYKColorspace)
(void) NegateCMYK(next_image,exception);
+ if (separate != MagickFalse)
+ {
+ const char
+ *property;
+ property=GetImageArtifact(next_image,"psd:opacity-mask");
+ if (property != (const char *) NULL)
+ {
+ mask=(Image *) GetImageRegistry(ImageRegistryType,property,
+ exception);
+ if (mask != (Image *) NULL)
+ {
+ if (mask->compression == RLECompression)
+ {
+ compact_pixels=AcquireCompactPixels(mask,exception);
+ if (compact_pixels == (unsigned char *) NULL)
+ return(0);
+ }
+ length=WriteOneChannel(psd_info,image_info,image,mask,
+ RedQuantum,compact_pixels,rows_offset,MagickTrue,exception);
+ (void) WritePSDSize(psd_info,image,length,size_offset);
+ count+=length;
+ compact_pixels=(unsigned char *) RelinquishMagickMemory(
+ compact_pixels);
+ }
+ }
+ }
return(count);
}
layer_index=0;
for (next_image=base_image; next_image != NULL; )
{
+ Image
+ *mask;
+
+ unsigned char
+ default_color;
+
unsigned short
channels,
total_channels;
+ mask=(Image *) NULL;
+ property=GetImageArtifact(next_image,"psd:opacity-mask");
+ default_color=0;
+ if (property != (const char *) NULL)
+ {
+ mask=(Image *) GetImageRegistry(ImageRegistryType,property,exception);
+ default_color=strlen(property) == 9 ? 255 : 0;
+ }
size+=WriteBlobMSBLong(image,(unsigned int) next_image->page.y);
size+=WriteBlobMSBLong(image,(unsigned int) next_image->page.x);
size+=WriteBlobMSBLong(image,(unsigned int) (next_image->page.y+
total_channels=channels;
if (next_image->alpha_trait != UndefinedPixelTrait)
total_channels++;
+ if (mask != (Image *) NULL)
+ total_channels++;
size+=WriteBlobMSBShort(image,total_channels);
layer_size_offsets[layer_index++]=TellBlob(image);
for (i=0; i < (ssize_t) channels; i++)
size+=WriteChannelSize(&psd_info,image,(signed short) i);
if (next_image->alpha_trait != UndefinedPixelTrait)
size+=WriteChannelSize(&psd_info,image,-1);
+ if (mask != (Image *) NULL)
+ size+=WriteChannelSize(&psd_info,image,-2);
size+=WriteBlob(image,4,(const unsigned char *) "8BIM");
size+=WriteBlob(image,4,(const unsigned char *)
CompositeOperatorToPSDBlendMode(next_image->compose));
opacity=(Quantum) StringToInteger(property);
size+=WriteBlobByte(image,ScaleQuantumToChar(opacity));
+ (void) ApplyPSDLayerOpacity(next_image,opacity,MagickTrue,exception);
}
else
size+=WriteBlobByte(image,255);
name_length+=(4-(name_length % 4));
if (info != (const StringInfo *) NULL)
name_length+=GetStringInfoLength(info);
- size+=WriteBlobMSBLong(image,(unsigned int) name_length+8);
- size+=WriteBlobMSBLong(image,0);
+ name_length+=8;
+ if (mask != (Image *) NULL)
+ name_length+=20;
+ size+=WriteBlobMSBLong(image,(unsigned int) name_length);
+ if (mask == (Image *) NULL)
+ size+=WriteBlobMSBLong(image,0);
+ else
+ {
+ if (mask->compose != NoCompositeOp)
+ (void) ApplyPSDOpacityMask(next_image,mask,ScaleCharToQuantum(
+ default_color),MagickTrue,exception);
+ mask->page.y+=image->page.y;
+ mask->page.x+=image->page.x;
+ size+=WriteBlobMSBLong(image,20);
+ size+=WriteBlobMSBSignedLong(image,mask->page.y);
+ size+=WriteBlobMSBSignedLong(image,mask->page.x);
+ size+=WriteBlobMSBLong(image,mask->rows+mask->page.y);
+ size+=WriteBlobMSBLong(image,mask->columns+mask->page.x);
+ size+=WriteBlobByte(image,default_color);
+ size+=WriteBlobByte(image,mask->compose == NoCompositeOp ? 2 : 0);
+ size+=WriteBlobMSBShort(image,0);
+ }
size+=WriteBlobMSBLong(image,0);
size+=WritePascalString(image,property,4);
if (info != (const StringInfo *) NULL)
rounded_size=size;
(void) WritePSDSize(&psd_info,image,rounded_size,size_offset);
layer_size_offsets=RelinquishMagickMemory(layer_size_offsets);
+ /*
+ Remove the opacity mask from the registry
+ */
+ next_image=base_image;
+ while (next_image != NULL)
+ {
+ property=GetImageArtifact(next_image,"psd:opacity-mask");
+ if (property != (const char *) NULL)
+ DeleteImageRegistry(property);
+ next_image=GetNextImageInList(next_image);
+ }
/*
Write composite image.
*/