From 7573654166df690125cddc4c30b36e0a6cb12954 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Sat, 10 Aug 2019 11:52:43 +0200 Subject: [PATCH] Fixed issue where not all pixel data would be read and the merged image would become all black (#1594). --- coders/psd-private.h | 3 +++ coders/psd.c | 36 ++++++++++++++++++------------------ coders/tiff.c | 1 + 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/coders/psd-private.h b/coders/psd-private.h index 84ca9c69e..89d947487 100644 --- a/coders/psd-private.h +++ b/coders/psd-private.h @@ -27,6 +27,9 @@ typedef struct _PSDInfo char signature[4]; + MagickBooleanType + has_merged_image; + size_t rows, columns; diff --git a/coders/psd.c b/coders/psd.c index 18986d4cc..ac3eb6d70 100644 --- a/coders/psd.c +++ b/coders/psd.c @@ -756,9 +756,8 @@ static MagickBooleanType NegateCMYK(Image *image,ExceptionInfo *exception) return(status); } -static StringInfo *ParseImageResourceBlocks(Image *image, - const unsigned char *blocks,size_t length, - MagickBooleanType *has_merged_image,ExceptionInfo *exception) +static StringInfo *ParseImageResourceBlocks(PSDInfo *psd_info,Image *image, + const unsigned char *blocks,size_t length,ExceptionInfo *exception) { const unsigned char *p; @@ -837,7 +836,7 @@ static StringInfo *ParseImageResourceBlocks(Image *image, case 0x0421: { if ((offset > 4) && (*(p+4) == 0)) - *has_merged_image=MagickFalse; + psd_info->has_merged_image=MagickFalse; p+=offset; break; } @@ -1631,9 +1630,11 @@ static void AttachPSDLayers(Image *image,LayerInfo *layer_info, layer_info=(LayerInfo *) RelinquishMagickMemory(layer_info); } -static inline MagickBooleanType PSDSkipImage(const ImageInfo *image_info, - const size_t index) +static inline MagickBooleanType PSDSkipImage(const PSDInfo *psd_info, + const ImageInfo *image_info,const size_t index) { + if (psd_info->has_merged_image == MagickFalse) + return(MagickFalse); if (image_info->number_scenes == 0) return(MagickFalse); if (index < image_info->scene) @@ -1986,7 +1987,7 @@ static MagickBooleanType ReadPSDLayersInternal(Image *image, for (i=0; i < number_layers; i++) { if ((layer_info[i].image == (Image *) NULL) || - (PSDSkipImage(image_info,++index) != MagickFalse)) + (PSDSkipImage(psd_info, image_info,++index) != MagickFalse)) { for (j=0; j < (ssize_t) layer_info[i].channels; j++) { @@ -2118,7 +2119,6 @@ static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception) *image; MagickBooleanType - has_merged_image, skip_layers; MagickOffsetType @@ -2286,7 +2286,7 @@ static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception) } if ((image->depth == 1) && (image->storage_class != PseudoClass)) ThrowReaderException(CorruptImageError, "ImproperImageHeader"); - has_merged_image=MagickTrue; + psd_info.has_merged_image=MagickTrue; profile=(StringInfo *) NULL; length=ReadBlobMSBLong(image); if (length != 0) @@ -2314,8 +2314,8 @@ static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception) blocks=(unsigned char *) RelinquishMagickMemory(blocks); ThrowReaderException(CorruptImageError,"ImproperImageHeader"); } - profile=ParseImageResourceBlocks(image,blocks,(size_t) length, - &has_merged_image,exception); + profile=ParseImageResourceBlocks(&psd_info,image,blocks,(size_t) length, + exception); blocks=(unsigned char *) RelinquishMagickMemory(blocks); } /* @@ -2330,7 +2330,7 @@ static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception) offset=TellBlob(image); skip_layers=MagickFalse; if ((image_info->number_scenes == 1) && (image_info->scene == 0) && - (has_merged_image != MagickFalse)) + (psd_info.has_merged_image != MagickFalse)) { if (image->debug != MagickFalse) (void) LogMagickEvent(CoderEvent,GetMagickModule(), @@ -2383,10 +2383,10 @@ static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception) (void) LogMagickEvent(CoderEvent,GetMagickModule(), " reading the precombined layer"); imageListLength=GetImageListLength(image); - if ((has_merged_image != MagickFalse) || (imageListLength == 1)) - has_merged_image=(MagickBooleanType) ReadPSDMergedImage(image_info,image, - &psd_info,exception); - if ((has_merged_image == MagickFalse) && (imageListLength == 1) && + if ((psd_info.has_merged_image != MagickFalse) || (imageListLength == 1)) + psd_info.has_merged_image=(MagickBooleanType) ReadPSDMergedImage( + image_info,image,&psd_info,exception); + if ((psd_info.has_merged_image == MagickFalse) && (imageListLength == 1) && (length != 0)) { (void) SeekBlob(image,offset,SEEK_SET); @@ -2401,7 +2401,7 @@ static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception) return((Image *) NULL); } } - if (has_merged_image == MagickFalse) + if (psd_info.has_merged_image == MagickFalse) { Image *merged; @@ -2427,7 +2427,7 @@ static Image *ReadPSDImage(const ImageInfo *image_info,ExceptionInfo *exception) next=image; while (next != (Image *) NULL) { - if (PSDSkipImage(image_info,i++) == MagickFalse) + if (PSDSkipImage(&psd_info,image_info,i++) == MagickFalse) (void) SetImageProfile(next,GetStringInfoName(profile),profile, exception); next=next->next; diff --git a/coders/tiff.c b/coders/tiff.c index 9bd0c8697..4a877a12a 100644 --- a/coders/tiff.c +++ b/coders/tiff.c @@ -298,6 +298,7 @@ static void InitPSDInfo(const Image *image,PSDInfo *info) info->mode=10; /* Set the mode to a value that won't change the colorspace */ info->channels=1U; info->min_channels=1U; + info->has_merged_image=MagickFalse; if (image->storage_class == PseudoClass) info->mode=2; /* indexed mode */ else -- 2.40.0