From: dirk Date: Sun, 27 Mar 2016 09:29:30 +0000 (+0200) Subject: Fixes for getting the pixel intensity. X-Git-Tag: 7.0.1-0~116 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=15a8904f9bf5b48551ef4cc3e41c6c63b7ef0204;p=imagemagick Fixes for getting the pixel intensity. --- diff --git a/MagickCore/accelerate-private.h b/MagickCore/accelerate-private.h index 424275051..0a271c8b3 100644 --- a/MagickCore/accelerate-private.h +++ b/MagickCore/accelerate-private.h @@ -385,12 +385,9 @@ OPENCL_ENDIF() inline float getAlphaF4(float4 p) { return p.w; } inline void setAlphaF4(float4* p, float value) { (*p).w = value; } - inline float GetPixelIntensity(const int method, const int colorspace, CLPixelType p) + inline float GetPixelIntensity(const unsigned int colorspace, + const unsigned int method,float red,float green,float blue) { - float red = getRed(p); - float green = getGreen(p); - float blue = getBlue(p); - float intensity; if (colorspace == GRAYColorspace) @@ -481,8 +478,7 @@ OPENCL_ENDIF() } } - return intensity; - + return intensity; } ) @@ -1443,8 +1439,8 @@ OPENCL_ENDIF() */ __kernel void Histogram(__global CLPixelType * restrict im, const ChannelType channel, - const int method, - const int colorspace, + const unsigned int colorspace, + const unsigned int method, __global uint4 * restrict histogram) { const int x = get_global_id(0); @@ -1453,7 +1449,11 @@ OPENCL_ENDIF() const int c = x + y * columns; if ((channel & SyncChannels) != 0) { - float intensity = GetPixelIntensity(method, colorspace,im[c]); + float red=(float)getRed(im[c]); + float green=(float)getGreen(im[c]); + float blue=(float)getBlue(im[c]); + + float intensity = GetPixelIntensity(colorspace, method, red, green, blue); uint pos = ScaleQuantumToMap(ClampToQuantum(intensity)); atomic_inc((__global uint *)(&(histogram[pos]))+2); //red position } @@ -2107,7 +2107,7 @@ OPENCL_ENDIF() STRINGIFY( __kernel void Grayscale(__global CLQuantum *im,const int number_channels, - const int method,const int colorspace) + const unsigned int colorspace,const unsigned int method) { const int x = get_global_id(0); const int y = get_global_id(1); @@ -2124,94 +2124,7 @@ OPENCL_ENDIF() green=(float)im[c+1]; blue=(float)im[c+2]; - intensity=0.0; - - switch (method) - { - case AveragePixelIntensityMethod: - { - intensity=(red+green+blue)/3.0; - break; - } - case BrightnessPixelIntensityMethod: - { - intensity=MagickMax(MagickMax(red,green),blue); - break; - } - case LightnessPixelIntensityMethod: - { - intensity=(MagickMin(MagickMin(red,green),blue)+ - MagickMax(MagickMax(red,green),blue))/2.0; - break; - } - case MSPixelIntensityMethod: - { - intensity=(float) (((float) red*red+green*green+ - blue*blue)/(3.0*QuantumRange)); - break; - } - case Rec601LumaPixelIntensityMethod: - { - /* - if (colorspace == RGBColorspace) - { - red=EncodePixelGamma(red); - green=EncodePixelGamma(green); - blue=EncodePixelGamma(blue); - } - */ - intensity=0.298839*red+0.586811*green+0.114350*blue; - break; - } - case Rec601LuminancePixelIntensityMethod: - { - /* - if (image->colorspace == sRGBColorspace) - { - red=DecodePixelGamma(red); - green=DecodePixelGamma(green); - blue=DecodePixelGamma(blue); - } - */ - intensity=0.298839*red+0.586811*green+0.114350*blue; - break; - } - case Rec709LumaPixelIntensityMethod: - default: - { - /* - if (image->colorspace == RGBColorspace) - { - red=EncodePixelGamma(red); - green=EncodePixelGamma(green); - blue=EncodePixelGamma(blue); - } - */ - intensity=0.212656*red+0.715158*green+0.072186*blue; - break; - } - case Rec709LuminancePixelIntensityMethod: - { - /* - if (image->colorspace == sRGBColorspace) - { - red=DecodePixelGamma(red); - green=DecodePixelGamma(green); - blue=DecodePixelGamma(blue); - } - */ - intensity=0.212656*red+0.715158*green+0.072186*blue; - break; - } - case RMSPixelIntensityMethod: - { - intensity=(float) (sqrt((float) red*red+green*green+ - blue*blue)/sqrt(3.0)); - break; - } - - } - + intensity = GetPixelIntensity(colorspace, method, red, green, blue); im[c] = im[c+1] = im[c+2] = ClampToQuantum(intensity); } ) diff --git a/MagickCore/accelerate.c b/MagickCore/accelerate.c index 6e30bed8f..bc5a3122c 100644 --- a/MagickCore/accelerate.c +++ b/MagickCore/accelerate.c @@ -187,20 +187,35 @@ static MagickBooleanType checkAccelerateConditionRGBA(const Image* image) return(MagickTrue); } -static MagickBooleanType checkHistogramCondition(Image *image) +static MagickBooleanType checkPixelIntensity(const Image *image, + const PixelIntensityMethod method) { - /* ensure this is the only pass get in for now. */ - if ((image->channel_mask & SyncChannels) == 0) - return MagickFalse; + /* EncodePixelGamma and DecodePixelGamma are not supported */ + if ((method == Rec601LumaPixelIntensityMethod) || + (method == Rec709LumaPixelIntensityMethod)) + { + if (image->colorspace == RGBColorspace) + return(MagickFalse); + } - if (image->intensity == Rec601LuminancePixelIntensityMethod || - image->intensity == Rec709LuminancePixelIntensityMethod) - return MagickFalse; + if ((method == Rec601LuminancePixelIntensityMethod) || + (method == Rec709LuminancePixelIntensityMethod)) + { + if (image->colorspace == sRGBColorspace) + return(MagickFalse); + } + + return(MagickTrue); +} - if (image->colorspace != sRGBColorspace) +static MagickBooleanType checkHistogramCondition(const Image *image, + const PixelIntensityMethod method) +{ + /* ensure this is the only pass get in for now. */ + if ((image->channel_mask & SyncChannels) == 0) return MagickFalse; - return MagickTrue; + return(checkPixelIntensity(image,method)); } static MagickBooleanType checkOpenCLEnvironment(ExceptionInfo* exception) @@ -1798,9 +1813,7 @@ static MagickBooleanType LaunchHistogramKernel(MagickCLEnv clEnv, outputReady; cl_int - clStatus, - colorspace, - method; + clStatus; cl_kernel histogramKernel; @@ -1808,6 +1821,10 @@ static MagickBooleanType LaunchHistogramKernel(MagickCLEnv clEnv, cl_event event; + cl_uint + colorspace, + method; + register ssize_t i; @@ -1817,8 +1834,8 @@ static MagickBooleanType LaunchHistogramKernel(MagickCLEnv clEnv, histogramKernel = NULL; outputReady = MagickFalse; - method = image->intensity; colorspace = image->colorspace; + method = image->intensity; /* get the OpenCL kernel */ histogramKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Histogram"); @@ -1832,8 +1849,8 @@ static MagickBooleanType LaunchHistogramKernel(MagickCLEnv clEnv, i = 0; clStatus=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_mem),(void *)&imageBuffer); clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(ChannelType),&channel); - clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_int),&method); - clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_int),&colorspace); + clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_uint),&colorspace); + clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_uint),&method); clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_mem),(void *)&histogramBuffer); if (clStatus != CL_SUCCESS) { @@ -2454,7 +2471,7 @@ MagickExport MagickBooleanType AccelerateContrastStretchImage( assert(exception != (ExceptionInfo *) NULL); if ((checkAccelerateConditionRGBA(image) == MagickFalse) || - (checkHistogramCondition(image) == MagickFalse) || + (checkHistogramCondition(image,image->intensity) == MagickFalse) || (checkOpenCLEnvironment(exception) == MagickFalse)) return(MagickFalse); @@ -3698,7 +3715,7 @@ MagickExport MagickBooleanType AccelerateEqualizeImage(Image *image, assert(exception != (ExceptionInfo *) NULL); if ((checkAccelerateConditionRGBA(image) == MagickFalse) || - (checkHistogramCondition(image) == MagickFalse) || + (checkHistogramCondition(image,image->intensity) == MagickFalse) || (checkOpenCLEnvironment(exception) == MagickFalse)) return(MagickFalse); @@ -3942,10 +3959,7 @@ static MagickBooleanType ComputeGrayscaleImage(Image *image, context; cl_int - clStatus, - number_channels, - colorspace, - intensityMethod; + clStatus; cl_kernel grayscaleKernel; @@ -3959,6 +3973,11 @@ static MagickBooleanType ComputeGrayscaleImage(Image *image, cl_mem_flags mem_flags; + cl_uint + number_channels, + colorspace, + intensityMethod; + MagickBooleanType outputReady; @@ -4032,15 +4051,15 @@ static MagickBooleanType ComputeGrayscaleImage(Image *image, goto cleanup; } - number_channels = (cl_int) image->number_channels; - intensityMethod = (cl_int) method; - colorspace = (cl_int) image->colorspace; + number_channels = (cl_uint) image->number_channels; + intensityMethod = (cl_uint) method; + colorspace = (cl_uint) image->colorspace; i = 0; clStatus=clEnv->library->clSetKernelArg(grayscaleKernel,i++,sizeof(cl_mem),(void *)&imageBuffer); - clStatus|=clEnv->library->clSetKernelArg(grayscaleKernel,i++,sizeof(cl_int),&number_channels); - clStatus|=clEnv->library->clSetKernelArg(grayscaleKernel,i++,sizeof(cl_int),&intensityMethod); - clStatus|=clEnv->library->clSetKernelArg(grayscaleKernel,i++,sizeof(cl_int),&colorspace); + clStatus|=clEnv->library->clSetKernelArg(grayscaleKernel,i++,sizeof(cl_uint),&number_channels); + clStatus|=clEnv->library->clSetKernelArg(grayscaleKernel,i++,sizeof(cl_uint),&colorspace); + clStatus|=clEnv->library->clSetKernelArg(grayscaleKernel,i++,sizeof(cl_uint),&intensityMethod); if (clStatus != CL_SUCCESS) { (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); @@ -4105,16 +4124,10 @@ MagickExport MagickBooleanType AccelerateGrayscaleImage(Image* image, assert(exception != (ExceptionInfo *) NULL); if ((checkAccelerateCondition(image) == MagickFalse) || + (checkPixelIntensity(image,method) == MagickFalse) || (checkOpenCLEnvironment(exception) == MagickFalse)) return(MagickFalse); - if ((method == Rec601LuminancePixelIntensityMethod) || - (method == Rec709LuminancePixelIntensityMethod)) - return(MagickFalse); - - if (image->colorspace != sRGBColorspace) - return(MagickFalse); - if (image->number_channels < 3) return(MagickFalse);