From e1c67a87c53861ad1c0ff1d728ce1edfe09a4e42 Mon Sep 17 00:00:00 2001 From: cristy Date: Sat, 10 Apr 2010 02:20:57 +0000 Subject: [PATCH] --- magick/profile.c | 701 ++++++++++++++++++++++++----------------------- 1 file changed, 355 insertions(+), 346 deletions(-) diff --git a/magick/profile.c b/magick/profile.c index c45030115..985382581 100644 --- a/magick/profile.c +++ b/magick/profile.c @@ -834,7 +834,8 @@ MagickExport MagickBooleanType ProfileImage(Image *image,const char *name, #define ProfileImageTag "Profile/Image" #define ThrowProfileException(severity,tag,context) \ { \ - (void) cmsCloseProfile(source_profile); \ + if (source_profile != (cmsHPROFILE) NULL) \ + (void) cmsCloseProfile(source_profile); \ if (target_profile != (cmsHPROFILE) NULL) \ (void) cmsCloseProfile(target_profile); \ ThrowBinaryException(severity,tag,context); \ @@ -937,54 +938,12 @@ MagickExport MagickBooleanType ProfileImage(Image *image,const char *name, image->filename); #else { - CacheView - *image_view; - - ColorspaceType - source_colorspace, - target_colorspace; - - cmsColorSpaceSignature - signature; - cmsHPROFILE - source_profile, - target_profile; - - cmsHTRANSFORM - *restrict transform; - - cmsUInt32Number - flags, - source_type, - target_type; - - ExceptionInfo - *exception; - - int - intent; - - long - progress, - y; - - MagickBooleanType - status; - - size_t - length, - source_channels, - target_channels; - - unsigned short - **restrict source_pixels, - **restrict target_pixels; + source_profile; /* Transform pixel colors as defined by the color profiles. */ - exception=(&image->exception); #if defined(LCMS_VERSION) && (LCMS_VERSION >= 2000) cmsSetLogErrorHandler(LCMSErrorHandler); #endif @@ -1000,331 +959,381 @@ MagickExport MagickBooleanType ProfileImage(Image *image,const char *name, if (source_profile == (cmsHPROFILE) NULL) ThrowBinaryException(ResourceLimitError, "ColorspaceColorProfileMismatch",name); - target_profile=(cmsHPROFILE) NULL; if ((cmsGetDeviceClass(source_profile) != cmsSigLinkClass) && - (icc_profile != (StringInfo *) NULL)) - { - target_profile=source_profile; - source_profile=cmsOpenProfileFromMem( - GetStringInfoDatum(icc_profile),(cmsUInt32Number) - GetStringInfoLength(icc_profile)); - if (source_profile == (cmsHPROFILE) NULL) - ThrowBinaryException(ResourceLimitError, - "ColorspaceColorProfileMismatch",name); - } - switch (cmsGetColorSpace(source_profile)) - { - case cmsSigCmykData: - { - source_colorspace=CMYKColorspace; - source_type=(cmsUInt32Number) TYPE_CMYK_16; - source_channels=4; - break; - } - case cmsSigGrayData: - { - source_colorspace=GRAYColorspace; - source_type=(cmsUInt32Number) TYPE_GRAY_16; - source_channels=1; - break; - } - case cmsSigLabData: - { - source_colorspace=LabColorspace; - source_type=(cmsUInt32Number) TYPE_Lab_16; - source_channels=3; - break; - } - case cmsSigLuvData: - { - source_colorspace=YUVColorspace; - source_type=(cmsUInt32Number) TYPE_YUV_16; - source_channels=3; - break; - } - case cmsSigRgbData: - { - source_colorspace=RGBColorspace; - source_type=(cmsUInt32Number) TYPE_RGB_16; - source_channels=3; - break; - } - case cmsSigXYZData: - { - source_colorspace=XYZColorspace; - source_type=(cmsUInt32Number) TYPE_XYZ_16; - source_channels=3; - break; - } - case cmsSigYCbCrData: - { - source_colorspace=YCbCrColorspace; - source_type=(cmsUInt32Number) TYPE_YCbCr_16; - source_channels=3; - break; - } - default: - { - source_colorspace=UndefinedColorspace; - source_type=(cmsUInt32Number) TYPE_RGB_16; - source_channels=3; - break; - } - } - signature=cmsGetPCS(source_profile); - if (target_profile != (cmsHPROFILE) NULL) - signature=cmsGetColorSpace(target_profile); - switch (signature) - { - case cmsSigCmykData: - { - target_colorspace=CMYKColorspace; - target_type=(cmsUInt32Number) TYPE_CMYK_16; - target_channels=4; - break; - } - case cmsSigLabData: - { - target_colorspace=LabColorspace; - target_type=(cmsUInt32Number) TYPE_Lab_16; - target_channels=3; - break; - } - case cmsSigGrayData: - { - target_colorspace=GRAYColorspace; - target_type=(cmsUInt32Number) TYPE_GRAY_16; - target_channels=1; - break; - } - case cmsSigLuvData: - { - target_colorspace=YUVColorspace; - target_type=(cmsUInt32Number) TYPE_YUV_16; - target_channels=3; - break; - } - case cmsSigRgbData: - { - target_colorspace=RGBColorspace; - target_type=(cmsUInt32Number) TYPE_RGB_16; - target_channels=3; - break; - } - case cmsSigXYZData: - { - target_colorspace=XYZColorspace; - target_type=(cmsUInt32Number) TYPE_XYZ_16; - target_channels=3; - break; - } - case cmsSigYCbCrData: - { - target_colorspace=YCbCrColorspace; - target_type=(cmsUInt32Number) TYPE_YCbCr_16; - target_channels=3; - break; - } - default: - { - target_colorspace=UndefinedColorspace; - target_type=(cmsUInt32Number) TYPE_RGB_16; - target_channels=3; - break; - } - } - if ((source_colorspace == UndefinedColorspace) || - (target_colorspace == UndefinedColorspace)) - ThrowProfileException(ImageError,"ColorspaceColorProfileMismatch", - name); - if ((source_colorspace == GRAYColorspace) && - (IsGrayImage(image,exception) == MagickFalse)) - ThrowProfileException(ImageError,"ColorspaceColorProfileMismatch", - name); - if ((source_colorspace == CMYKColorspace) && - (image->colorspace != CMYKColorspace)) - ThrowProfileException(ImageError,"ColorspaceColorProfileMismatch", - name); - if ((source_colorspace == XYZColorspace) && - (image->colorspace != XYZColorspace)) - ThrowProfileException(ImageError,"ColorspaceColorProfileMismatch", - name); - if ((source_colorspace == YCbCrColorspace) && - (image->colorspace != YCbCrColorspace)) - ThrowProfileException(ImageError,"ColorspaceColorProfileMismatch", - name); - if ((source_colorspace != CMYKColorspace) && - (source_colorspace != GRAYColorspace) && - (source_colorspace != LabColorspace) && - (source_colorspace != XYZColorspace) && - (source_colorspace != YCbCrColorspace) && - (image->colorspace != RGBColorspace)) - ThrowProfileException(ImageError,"ColorspaceColorProfileMismatch", - name); - switch (image->rendering_intent) - { - case AbsoluteIntent: intent=INTENT_ABSOLUTE_COLORIMETRIC; break; - case PerceptualIntent: intent=INTENT_PERCEPTUAL; break; - case RelativeIntent: intent=INTENT_RELATIVE_COLORIMETRIC; break; - case SaturationIntent: intent=INTENT_SATURATION; break; - default: intent=INTENT_PERCEPTUAL; break; - } - flags=cmsFLAGS_HIGHRESPRECALC; -#if defined(cmsFLAGS_BLACKPOINTCOMPENSATION) - if (image->black_point_compensation != MagickFalse) - flags|=cmsFLAGS_BLACKPOINTCOMPENSATION; -#endif - transform=AcquireTransformThreadSet(source_profile,source_type, - target_profile,target_type,intent,flags); - if (transform == (cmsHTRANSFORM *) NULL) - ThrowBinaryException(ImageError,"UnableToCreateColorTransform",name); - /* - Transform image as dictated by the source and target image profiles. - */ - length=(size_t) image->columns; - source_pixels=AcquirePixelThreadSet(image->columns,source_channels); - target_pixels=AcquirePixelThreadSet(image->columns,target_channels); - if ((source_pixels == (unsigned short **) NULL) || - (target_pixels == (unsigned short **) NULL)) - { - transform=DestroyTransformThreadSet(transform); - if (target_profile != (cmsHPROFILE) NULL) - (void) cmsCloseProfile(target_profile); - ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", - image->filename); - } - if (SetImageStorageClass(image,DirectClass) == MagickFalse) + (icc_profile == (StringInfo *) NULL)) + status=SetImageProfile(image,name,profile); + else { - target_pixels=DestroyPixelThreadSet(target_pixels); - source_pixels=DestroyPixelThreadSet(source_pixels); - transform=DestroyTransformThreadSet(transform); - if (target_profile != (cmsHPROFILE) NULL) - (void) cmsCloseProfile(target_profile); - return(MagickFalse); - } - if (target_colorspace == CMYKColorspace) - (void) SetImageColorspace(image,target_colorspace); - status=MagickTrue; - progress=0; - image_view=AcquireCacheView(image); -#if defined(MAGICKCORE_OPENMP_SUPPORT) - #pragma omp parallel for schedule(dynamic,4) shared(status) -#endif - for (y=0; y < (long) image->rows; y++) - { - MagickBooleanType - sync; + CacheView + *image_view; - register IndexPacket - *restrict indexes; + ColorspaceType + source_colorspace, + target_colorspace; - register long - id, - x; + cmsColorSpaceSignature + signature; - register PixelPacket - *restrict q; + cmsHPROFILE + target_profile; - register unsigned short - *p; + cmsHTRANSFORM + *restrict transform; - if (status == MagickFalse) - continue; - q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1, - exception); - if (q == (PixelPacket *) NULL) + cmsUInt32Number + flags, + source_type, + target_type; + + ExceptionInfo + *exception; + + int + intent; + + long + progress, + y; + + MagickBooleanType + status; + + size_t + length, + source_channels, + target_channels; + + unsigned short + **restrict source_pixels, + **restrict target_pixels; + + exception=(&image->exception); + target_profile=(cmsHPROFILE) NULL; + if (icc_profile != (StringInfo *) NULL) + { + target_profile=source_profile; + source_profile=cmsOpenProfileFromMem( + GetStringInfoDatum(icc_profile),(cmsUInt32Number) + GetStringInfoLength(icc_profile)); + if (source_profile == (cmsHPROFILE) NULL) + ThrowProfileException(ResourceLimitError, + "ColorspaceColorProfileMismatch",name); + } + switch (cmsGetColorSpace(source_profile)) { - status=MagickFalse; - continue; + case cmsSigCmykData: + { + source_colorspace=CMYKColorspace; + source_type=(cmsUInt32Number) TYPE_CMYK_16; + source_channels=4; + break; + } + case cmsSigGrayData: + { + source_colorspace=GRAYColorspace; + source_type=(cmsUInt32Number) TYPE_GRAY_16; + source_channels=1; + break; + } + case cmsSigLabData: + { + source_colorspace=LabColorspace; + source_type=(cmsUInt32Number) TYPE_Lab_16; + source_channels=3; + break; + } + case cmsSigLuvData: + { + source_colorspace=YUVColorspace; + source_type=(cmsUInt32Number) TYPE_YUV_16; + source_channels=3; + break; + } + case cmsSigRgbData: + { + source_colorspace=RGBColorspace; + source_type=(cmsUInt32Number) TYPE_RGB_16; + source_channels=3; + break; + } + case cmsSigXYZData: + { + source_colorspace=XYZColorspace; + source_type=(cmsUInt32Number) TYPE_XYZ_16; + source_channels=3; + break; + } + case cmsSigYCbCrData: + { + source_colorspace=YCbCrColorspace; + source_type=(cmsUInt32Number) TYPE_YCbCr_16; + source_channels=3; + break; + } + default: + { + source_colorspace=UndefinedColorspace; + source_type=(cmsUInt32Number) TYPE_RGB_16; + source_channels=3; + break; + } } - indexes=GetCacheViewAuthenticIndexQueue(image_view); - id=GetOpenMPThreadId(); - p=source_pixels[id]; - for (x=0; x < (long) image->columns; x++) - { - *p++=ScaleQuantumToShort(q->red); - if (source_channels > 1) + signature=cmsGetPCS(source_profile); + if (target_profile != (cmsHPROFILE) NULL) + signature=cmsGetColorSpace(target_profile); + switch (signature) + { + case cmsSigCmykData: { - *p++=ScaleQuantumToShort(q->green); - *p++=ScaleQuantumToShort(q->blue); + target_colorspace=CMYKColorspace; + target_type=(cmsUInt32Number) TYPE_CMYK_16; + target_channels=4; + break; } - if (source_channels > 3) - *p++=ScaleQuantumToShort(indexes[x]); - q++; - } - cmsDoTransform(transform[id],source_pixels[id],target_pixels[id], - (unsigned int) image->columns); - p=target_pixels[id]; - q-=image->columns; - for (x=0; x < (long) image->columns; x++) - { - q->red=ScaleShortToQuantum(*p); - q->green=q->red; - q->blue=q->red; - p++; - if (target_channels > 1) + case cmsSigLabData: { - q->green=ScaleShortToQuantum(*p); - p++; - q->blue=ScaleShortToQuantum(*p); - p++; + target_colorspace=LabColorspace; + target_type=(cmsUInt32Number) TYPE_Lab_16; + target_channels=3; + break; } - if (target_channels > 3) + case cmsSigGrayData: { - indexes[x]=ScaleShortToQuantum(*p); - p++; + target_colorspace=GRAYColorspace; + target_type=(cmsUInt32Number) TYPE_GRAY_16; + target_channels=1; + break; } - q++; - } - sync=SyncCacheViewAuthenticPixels(image_view,exception); - if (sync == MagickFalse) - status=MagickFalse; - if (image->progress_monitor != (MagickProgressMonitor) NULL) + case cmsSigLuvData: + { + target_colorspace=YUVColorspace; + target_type=(cmsUInt32Number) TYPE_YUV_16; + target_channels=3; + break; + } + case cmsSigRgbData: + { + target_colorspace=RGBColorspace; + target_type=(cmsUInt32Number) TYPE_RGB_16; + target_channels=3; + break; + } + case cmsSigXYZData: + { + target_colorspace=XYZColorspace; + target_type=(cmsUInt32Number) TYPE_XYZ_16; + target_channels=3; + break; + } + case cmsSigYCbCrData: + { + target_colorspace=YCbCrColorspace; + target_type=(cmsUInt32Number) TYPE_YCbCr_16; + target_channels=3; + break; + } + default: + { + target_colorspace=UndefinedColorspace; + target_type=(cmsUInt32Number) TYPE_RGB_16; + target_channels=3; + break; + } + } + if ((source_colorspace == UndefinedColorspace) || + (target_colorspace == UndefinedColorspace)) + ThrowProfileException(ImageError,"ColorspaceColorProfileMismatch", + name); + if ((source_colorspace == GRAYColorspace) && + (IsGrayImage(image,exception) == MagickFalse)) + ThrowProfileException(ImageError,"ColorspaceColorProfileMismatch", + name); + if ((source_colorspace == CMYKColorspace) && + (image->colorspace != CMYKColorspace)) + ThrowProfileException(ImageError,"ColorspaceColorProfileMismatch", + name); + if ((source_colorspace == XYZColorspace) && + (image->colorspace != XYZColorspace)) + ThrowProfileException(ImageError,"ColorspaceColorProfileMismatch", + name); + if ((source_colorspace == YCbCrColorspace) && + (image->colorspace != YCbCrColorspace)) + ThrowProfileException(ImageError,"ColorspaceColorProfileMismatch", + name); + if ((source_colorspace != CMYKColorspace) && + (source_colorspace != GRAYColorspace) && + (source_colorspace != LabColorspace) && + (source_colorspace != XYZColorspace) && + (source_colorspace != YCbCrColorspace) && + (image->colorspace != RGBColorspace)) + ThrowProfileException(ImageError,"ColorspaceColorProfileMismatch", + name); + switch (image->rendering_intent) + { + case AbsoluteIntent: intent=INTENT_ABSOLUTE_COLORIMETRIC; break; + case PerceptualIntent: intent=INTENT_PERCEPTUAL; break; + case RelativeIntent: intent=INTENT_RELATIVE_COLORIMETRIC; break; + case SaturationIntent: intent=INTENT_SATURATION; break; + default: intent=INTENT_PERCEPTUAL; break; + } + flags=cmsFLAGS_HIGHRESPRECALC; +#if defined(cmsFLAGS_BLACKPOINTCOMPENSATION) + if (image->black_point_compensation != MagickFalse) + flags|=cmsFLAGS_BLACKPOINTCOMPENSATION; +#endif + transform=AcquireTransformThreadSet(source_profile,source_type, + target_profile,target_type,intent,flags); + if (transform == (cmsHTRANSFORM *) NULL) + ThrowProfileException(ImageError,"UnableToCreateColorTransform", + name); + /* + Transform image as dictated by the source & target image profiles. + */ + length=(size_t) image->columns; + source_pixels=AcquirePixelThreadSet(image->columns,source_channels); + target_pixels=AcquirePixelThreadSet(image->columns,target_channels); + if ((source_pixels == (unsigned short **) NULL) || + (target_pixels == (unsigned short **) NULL)) + { + transform=DestroyTransformThreadSet(transform); + ThrowProfileException(ResourceLimitError, + "MemoryAllocationFailed",image->filename); + } + if (SetImageStorageClass(image,DirectClass) == MagickFalse) + { + target_pixels=DestroyPixelThreadSet(target_pixels); + source_pixels=DestroyPixelThreadSet(source_pixels); + transform=DestroyTransformThreadSet(transform); + if (source_profile != (cmsHPROFILE) NULL) + (void) cmsCloseProfile(source_profile); + if (target_profile != (cmsHPROFILE) NULL) + (void) cmsCloseProfile(target_profile); + return(MagickFalse); + } + if (target_colorspace == CMYKColorspace) + (void) SetImageColorspace(image,target_colorspace); + status=MagickTrue; + progress=0; + image_view=AcquireCacheView(image); +#if defined(MAGICKCORE_OPENMP_SUPPORT) + #pragma omp parallel for schedule(dynamic,4) shared(status) +#endif + for (y=0; y < (long) image->rows; y++) { MagickBooleanType - proceed; + sync; + + register IndexPacket + *restrict indexes; + + register long + id, + x; + + register PixelPacket + *restrict q; + + register unsigned short + *p; + + if (status == MagickFalse) + continue; + q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1, + exception); + if (q == (PixelPacket *) NULL) + { + status=MagickFalse; + continue; + } + indexes=GetCacheViewAuthenticIndexQueue(image_view); + id=GetOpenMPThreadId(); + p=source_pixels[id]; + for (x=0; x < (long) image->columns; x++) + { + *p++=ScaleQuantumToShort(q->red); + if (source_channels > 1) + { + *p++=ScaleQuantumToShort(q->green); + *p++=ScaleQuantumToShort(q->blue); + } + if (source_channels > 3) + *p++=ScaleQuantumToShort(indexes[x]); + q++; + } + cmsDoTransform(transform[id],source_pixels[id],target_pixels[id], + (unsigned int) image->columns); + p=target_pixels[id]; + q-=image->columns; + for (x=0; x < (long) image->columns; x++) + { + q->red=ScaleShortToQuantum(*p); + q->green=q->red; + q->blue=q->red; + p++; + if (target_channels > 1) + { + q->green=ScaleShortToQuantum(*p); + p++; + q->blue=ScaleShortToQuantum(*p); + p++; + } + if (target_channels > 3) + { + indexes[x]=ScaleShortToQuantum(*p); + p++; + } + q++; + } + sync=SyncCacheViewAuthenticPixels(image_view,exception); + if (sync == MagickFalse) + status=MagickFalse; + if (image->progress_monitor != (MagickProgressMonitor) NULL) + { + MagickBooleanType + proceed; #if defined(MAGICKCORE_OPENMP_SUPPORT) #pragma omp critical (MagickCore_ProfileImage) #endif - proceed=SetImageProgress(image,ProfileImageTag,progress++, - image->rows); - if (proceed == MagickFalse) - status=MagickFalse; + proceed=SetImageProgress(image,ProfileImageTag,progress++, + image->rows); + if (proceed == MagickFalse) + status=MagickFalse; + } } - } - image_view=DestroyCacheView(image_view); - (void) SetImageColorspace(image,target_colorspace); - switch (signature) - { - case cmsSigRgbData: - { - image->type=image->matte == MagickFalse ? TrueColorType : - TrueColorMatteType; - break; - } - case cmsSigCmykData: - { - image->type=image->matte == MagickFalse ? ColorSeparationType : - ColorSeparationMatteType; - break; - } - case cmsSigGrayData: - { - image->type=image->matte == MagickFalse ? GrayscaleType : - GrayscaleMatteType; - break; + image_view=DestroyCacheView(image_view); + (void) SetImageColorspace(image,target_colorspace); + switch (signature) + { + case cmsSigRgbData: + { + image->type=image->matte == MagickFalse ? TrueColorType : + TrueColorMatteType; + break; + } + case cmsSigCmykData: + { + image->type=image->matte == MagickFalse ? ColorSeparationType : + ColorSeparationMatteType; + break; + } + case cmsSigGrayData: + { + image->type=image->matte == MagickFalse ? GrayscaleType : + GrayscaleMatteType; + break; + } + default: + break; + } + target_pixels=DestroyPixelThreadSet(target_pixels); + source_pixels=DestroyPixelThreadSet(source_pixels); + transform=DestroyTransformThreadSet(transform); + if (cmsGetDeviceClass(source_profile) != cmsSigLinkClass) + status=SetImageProfile(image,name,profile); + if (target_profile != (cmsHPROFILE) NULL) + (void) cmsCloseProfile(target_profile); } - default: - break; - } - target_pixels=DestroyPixelThreadSet(target_pixels); - source_pixels=DestroyPixelThreadSet(source_pixels); - transform=DestroyTransformThreadSet(transform); - if (cmsGetDeviceClass(source_profile) != cmsSigLinkClass) - status=SetImageProfile(image,name,profile); - if (target_profile != (cmsHPROFILE) NULL) - (void) cmsCloseProfile(target_profile); (void) cmsCloseProfile(source_profile); } #endif -- 2.50.1