19#ifndef MAGICKCORE_ACCELERATE_KERNELS_PRIVATE_H
20#define MAGICKCORE_ACCELERATE_KERNELS_PRIVATE_H
22#if defined(__cplusplus) || defined(c_plusplus)
26#if defined(MAGICKCORE_OPENCL_SUPPORT)
31#define OPENCL_DEFINE(VAR,...) "\n #""define " #VAR " " #__VA_ARGS__ " \n"
32#define OPENCL_ELIF(...) "\n #""elif " #__VA_ARGS__ " \n"
33#define OPENCL_ELSE() "\n #""else " " \n"
34#define OPENCL_ENDIF() "\n #""endif " " \n"
35#define OPENCL_IF(...) "\n #""if " #__VA_ARGS__ " \n"
36#define STRINGIFY(...) #__VA_ARGS__ "\n"
38const char *accelerateKernels =
43 OPENCL_DEFINE(SigmaUniform, (attenuate*0.015625f))
44 OPENCL_DEFINE(SigmaGaussian, (attenuate*0.015625f))
45 OPENCL_DEFINE(SigmaImpulse, (attenuate*0.1f))
46 OPENCL_DEFINE(SigmaLaplacian, (attenuate*0.0390625f))
47 OPENCL_DEFINE(SigmaMultiplicativeGaussian, (attenuate*0.5f))
48 OPENCL_DEFINE(SigmaPoisson, (attenuate*12.5f))
49 OPENCL_DEFINE(SigmaRandom, (attenuate))
50 OPENCL_DEFINE(TauGaussian, (attenuate*0.078125f))
51 OPENCL_DEFINE(MagickMax(x,y), (((x) > (y)) ? (x) : (y)))
52 OPENCL_DEFINE(MagickMin(x,y), (((x) < (y)) ? (x) : (y)))
53 OPENCL_DEFINE(QuantumScale, (1.0/QuantumRange))
80 Rec601YCbCrColorspace,
81 Rec709YCbCrColorspace,
85 TransparentColorspace,
101 UndefinedCompositeOp,
107 ChangeMaskCompositeOp,
109 ColorBurnCompositeOp,
110 ColorDodgeCompositeOp,
112 CopyBlackCompositeOp,
116 CopyGreenCompositeOp,
117 CopyMagentaCompositeOp,
118 CopyAlphaCompositeOp,
120 CopyYellowCompositeOp,
122 DarkenIntensityCompositeOp,
123 DifferenceCompositeOp,
127 DivideDstCompositeOp,
128 DivideSrcCompositeOp,
134 ExclusionCompositeOp,
135 HardLightCompositeOp,
139 IntensityCompositeOp,
141 LightenIntensityCompositeOp,
142 LinearBurnCompositeOp,
143 LinearDodgeCompositeOp,
144 LinearLightCompositeOp,
146 MathematicsCompositeOp,
150 ModulusAddCompositeOp,
151 ModulusSubtractCompositeOp,
157 PegtopLightCompositeOp,
163 SoftLightCompositeOp,
169 ThresholdCompositeOp,
170 VividLightCompositeOp,
193 MultiplicativeGaussianNoise,
204 UndefinedPixelIntensityMethod = 0,
205 AveragePixelIntensityMethod,
206 BrightnessPixelIntensityMethod,
207 LightnessPixelIntensityMethod,
208 MSPixelIntensityMethod,
209 Rec601LumaPixelIntensityMethod,
210 Rec601LuminancePixelIntensityMethod,
211 Rec709LumaPixelIntensityMethod,
212 Rec709LuminancePixelIntensityMethod,
213 RMSPixelIntensityMethod
214 } PixelIntensityMethod;
220 BoxWeightingFunction = 0,
221 TriangleWeightingFunction,
222 CubicBCWeightingFunction,
223 HannWeightingFunction,
224 HammingWeightingFunction,
225 BlackmanWeightingFunction,
226 GaussianWeightingFunction,
227 QuadraticWeightingFunction,
228 JincWeightingFunction,
229 SincWeightingFunction,
230 SincFastWeightingFunction,
231 KaiserWeightingFunction,
232 WelchWeightingFunction,
233 BohmanWeightingFunction,
234 LagrangeWeightingFunction,
235 CosineWeightingFunction
236 } ResizeWeightingFunctionType;
242 UndefinedChannel = 0x0000,
244 GrayChannel = 0x0001,
245 CyanChannel = 0x0001,
246 GreenChannel = 0x0002,
247 MagentaChannel = 0x0002,
248 BlueChannel = 0x0004,
249 YellowChannel = 0x0004,
250 BlackChannel = 0x0008,
251 AlphaChannel = 0x0010,
252 OpacityChannel = 0x0010,
253 IndexChannel = 0x0020,
254 ReadMaskChannel = 0x0040,
255 WriteMaskChannel = 0x0080,
256 MetaChannel = 0x0100,
257 CompositeChannels = 0x001F,
258 AllChannels = 0x7ffffff,
266 TrueAlphaChannel = 0x0100,
267 RGBChannels = 0x0200,
268 GrayChannels = 0x0400,
269 SyncChannels = 0x20000,
270 DefaultChannels = AllChannels
278OPENCL_IF((MAGICKCORE_QUANTUM_DEPTH == 8))
281 static inline CLQuantum ScaleCharToQuantum(const
unsigned char value)
283 return((CLQuantum) value);
287OPENCL_ELIF((MAGICKCORE_QUANTUM_DEPTH == 16))
290 static inline CLQuantum ScaleCharToQuantum(
const unsigned char value)
292 return((CLQuantum) (257.0f*value));
296OPENCL_ELIF((MAGICKCORE_QUANTUM_DEPTH == 32))
299 static inline CLQuantum ScaleCharToQuantum(
const unsigned char value)
301 return((CLQuantum) (16843009.0*value));
307OPENCL_IF((MAGICKCORE_HDRI_SUPPORT == 1))
310 static inline CLQuantum ClampToQuantum(const
float value)
312 return (CLQuantum) clamp(value, 0.0f, QuantumRange);
319 static inline CLQuantum ClampToQuantum(
const float value)
321 return (CLQuantum) (clamp(value, 0.0f, QuantumRange) + 0.5f);
328 static inline
int ClampToCanvas(const
int offset,const
int range)
330 return clamp(offset, (
int)0, range-1);
335 static inline uint ScaleQuantumToMap(CLQuantum value)
337 if (value >= (CLQuantum) MaxMap)
338 return ((uint)MaxMap);
340 return ((uint)value);
345 static inline float MagickSafeReciprocal(
const float x)
347 float sign = x < (float) 0.0 ? (
float) -1.0 : (float) 1.0;
348 return((sign*x) >= MagickEpsilon ? (
float) 1.0/x : sign*((
float) 1.0/MagickEpsilon));
354 static inline unsigned int getPixelIndex(
const unsigned int number_channels,
355 const unsigned int columns,
const unsigned int x,
const unsigned int y)
357 return (x * number_channels) + (y * columns * number_channels);
360 static inline float getPixelRed(
const __global CLQuantum *p) {
return (
float)*p; }
361 static inline float getPixelGreen(
const __global CLQuantum *p) {
return (
float)*(p+1); }
362 static inline float getPixelBlue(
const __global CLQuantum *p) {
return (
float)*(p+2); }
363 static inline float getPixelAlpha(
const __global CLQuantum *p,
const unsigned int number_channels) {
return (
float)*(p+number_channels-1); }
365 static inline void setPixelRed(__global CLQuantum *p,
const CLQuantum value) { *p=value; }
366 static inline void setPixelGreen(__global CLQuantum *p,
const CLQuantum value) { *(p+1)=value; }
367 static inline void setPixelBlue(__global CLQuantum *p,
const CLQuantum value) { *(p+2)=value; }
368 static inline void setPixelAlpha(__global CLQuantum *p,
const unsigned int number_channels,
const CLQuantum value) { *(p+number_channels-1)=value; }
370 static inline CLQuantum getBlue(CLPixelType p) {
return p.x; }
371 static inline void setBlue(CLPixelType* p, CLQuantum value) { (*p).x = value; }
372 static inline float getBlueF4(float4 p) {
return p.x; }
373 static inline void setBlueF4(float4* p,
float value) { (*p).x = value; }
375 static inline CLQuantum getGreen(CLPixelType p) {
return p.y; }
376 static inline void setGreen(CLPixelType* p, CLQuantum value) { (*p).y = value; }
377 static inline float getGreenF4(float4 p) {
return p.y; }
378 static inline void setGreenF4(float4* p,
float value) { (*p).y = value; }
380 static inline CLQuantum getRed(CLPixelType p) {
return p.z; }
381 static inline void setRed(CLPixelType* p, CLQuantum value) { (*p).z = value; }
382 static inline float getRedF4(float4 p) {
return p.z; }
383 static inline void setRedF4(float4* p,
float value) { (*p).z = value; }
385 static inline CLQuantum getAlpha(CLPixelType p) {
return p.w; }
386 static inline void setAlpha(CLPixelType* p, CLQuantum value) { (*p).w = value; }
387 static inline float getAlphaF4(float4 p) {
return p.w; }
388 static inline void setAlphaF4(float4* p,
float value) { (*p).w = value; }
390 static inline void ReadChannels(
const __global CLQuantum *p,
const unsigned int number_channels,
391 const ChannelType channel,
float *red,
float *green,
float *blue,
float *alpha)
393 if ((channel & RedChannel) != 0)
396 if (number_channels > 2)
398 if ((channel & GreenChannel) != 0)
399 *green=getPixelGreen(p);
401 if ((channel & BlueChannel) != 0)
402 *blue=getPixelBlue(p);
405 if (((number_channels == 4) || (number_channels == 2)) &&
406 ((channel & AlphaChannel) != 0))
407 *alpha=getPixelAlpha(p,number_channels);
410 static inline float4 ReadAllChannels(
const __global CLQuantum *image,
const unsigned int number_channels,
411 const unsigned int columns,
const unsigned int x,
const unsigned int y)
413 const __global CLQuantum *p = image + getPixelIndex(number_channels, columns, x, y);
417 pixel.x=getPixelRed(p);
419 if (number_channels > 2)
421 pixel.y=getPixelGreen(p);
422 pixel.z=getPixelBlue(p);
425 if ((number_channels == 4) || (number_channels == 2))
426 pixel.w=getPixelAlpha(p,number_channels);
430 static inline float4 ReadFloat4(
const __global CLQuantum *image,
const unsigned int number_channels,
431 const unsigned int columns,
const unsigned int x,
const unsigned int y,
const ChannelType channel)
433 const __global CLQuantum *p = image + getPixelIndex(number_channels, columns, x, y);
440 ReadChannels(p, number_channels, channel, &red, &green, &blue, &alpha);
441 return (float4)(red, green, blue, alpha);
444 static inline void WriteChannels(__global CLQuantum *p,
const unsigned int number_channels,
445 const ChannelType channel,
float red,
float green,
float blue,
float alpha)
447 if ((channel & RedChannel) != 0)
448 setPixelRed(p,ClampToQuantum(red));
450 if (number_channels > 2)
452 if ((channel & GreenChannel) != 0)
453 setPixelGreen(p,ClampToQuantum(green));
455 if ((channel & BlueChannel) != 0)
456 setPixelBlue(p,ClampToQuantum(blue));
459 if (((number_channels == 4) || (number_channels == 2)) &&
460 ((channel & AlphaChannel) != 0))
461 setPixelAlpha(p,number_channels,ClampToQuantum(alpha));
464 static inline void WriteAllChannels(__global CLQuantum *image,
const unsigned int number_channels,
465 const unsigned int columns,
const unsigned int x,
const unsigned int y, float4 pixel)
467 __global CLQuantum *p = image + getPixelIndex(number_channels, columns, x, y);
469 setPixelRed(p,ClampToQuantum(pixel.x));
471 if (number_channels > 2)
473 setPixelGreen(p,ClampToQuantum(pixel.y));
474 setPixelBlue(p,ClampToQuantum(pixel.z));
477 if ((number_channels == 4) || (number_channels == 2))
478 setPixelAlpha(p,number_channels,ClampToQuantum(pixel.w));
481 static inline void WriteFloat4(__global CLQuantum *image,
const unsigned int number_channels,
482 const unsigned int columns,
const unsigned int x,
const unsigned int y,
const ChannelType channel,
485 __global CLQuantum *p = image + getPixelIndex(number_channels, columns, x, y);
486 WriteChannels(p, number_channels, channel, pixel.x, pixel.y, pixel.z, pixel.w);
489 static inline float GetPixelIntensity(
const unsigned int colorspace,
490 const unsigned int method,
float red,
float green,
float blue)
494 if ((colorspace == GRAYColorspace) || (colorspace == LinearGRAYColorspace))
499 case AveragePixelIntensityMethod:
501 intensity=(red+green+blue)/3.0;
504 case BrightnessPixelIntensityMethod:
506 intensity=MagickMax(MagickMax(red,green),blue);
509 case LightnessPixelIntensityMethod:
511 intensity=(MagickMin(MagickMin(red,green),blue)+
512 MagickMax(MagickMax(red,green),blue))/2.0;
515 case MSPixelIntensityMethod:
517 intensity=(float) (((
float) red*red+green*green+blue*blue)/
521 case Rec601LumaPixelIntensityMethod:
531 if ((red == green) && (green == blue))
534 intensity=0.298839*red+0.586811*green+0.114350*blue;
537 case Rec601LuminancePixelIntensityMethod:
547 if ((red == green) && (green == blue))
550 intensity=0.298839*red+0.586811*green+0.114350*blue;
553 case Rec709LumaPixelIntensityMethod:
564 if ((red == green) && (green == blue))
567 intensity=0.212656*red+0.715158*green+0.072186*blue;
570 case Rec709LuminancePixelIntensityMethod:
580 if ((red == green) && (green == blue))
583 intensity=0.212656*red+0.715158*green+0.072186*blue;
586 case RMSPixelIntensityMethod:
588 intensity=(float) (sqrt((
float) red*red+green*green+blue*blue)/
597 static inline int mirrorBottom(
int value)
599 return (value < 0) ? - (value) : value;
602 static inline int mirrorTop(
int value,
int width)
604 return (value >= width) ? (2 * width - value - 1) : value;
629 ulong MWC_AddMod64(ulong a, ulong b, ulong M)
633 if( (v>=M) || (convert_float(v) < convert_float(a)) )
644 ulong MWC_MulMod64(ulong a, ulong b, ulong M)
649 r=MWC_AddMod64(r,b,M);
650 b=MWC_AddMod64(b,b,M);
660 ulong MWC_PowMod64(ulong a, ulong e, ulong M)
665 acc=MWC_MulMod64(acc,sqr,M);
666 sqr=MWC_MulMod64(sqr,sqr,M);
672 uint2 MWC_SkipImpl_Mod64(uint2 curr, ulong A, ulong M, ulong distance)
674 ulong m=MWC_PowMod64(A, distance, M);
675 ulong x=curr.x*(ulong)A+curr.y;
676 x=MWC_MulMod64(x, m, M);
677 return (uint2)((uint)(x/A), (uint)(x%A));
680 uint2 MWC_SeedImpl_Mod64(ulong A, ulong M, uint vecSize, uint vecOffset, ulong streamBase, ulong streamGap)
687 enum{ MWC_BASEID = 4077358422479273989UL };
689 ulong dist=streamBase + (get_global_id(0)*vecSize+vecOffset)*streamGap;
690 ulong m=MWC_PowMod64(A, dist, M);
692 ulong x=MWC_MulMod64(MWC_BASEID, m, M);
693 return (uint2)((uint)(x/A), (uint)(x%A));
697 typedef struct{ uint x; uint c; uint seed0; ulong seed1; } mwc64x_state_t;
699 void MWC64X_Step(mwc64x_state_t *s)
703 uint Xn=s->seed0*X+C;
704 uint carry=(uint)(Xn<C);
705 uint Cn=mad_hi(s->seed0,X,carry);
711 void MWC64X_Skip(mwc64x_state_t *s, ulong distance)
713 uint2 tmp=MWC_SkipImpl_Mod64((uint2)(s->x,s->c), s->seed0, s->seed1, distance);
718 void MWC64X_SeedStreams(mwc64x_state_t *s, ulong baseOffset, ulong perStreamOffset)
720 uint2 tmp=MWC_SeedImpl_Mod64(s->seed0, s->seed1, 1, 0, baseOffset, perStreamOffset);
726 uint MWC64X_NextUint(mwc64x_state_t *s)
728 uint res=s->x ^ s->c;
737 float mwcReadPseudoRandomValue(mwc64x_state_t* rng)
739 return (1.0f * MWC64X_NextUint(rng)) / (float)(0xffffffff);
742 float mwcGenerateDifferentialNoise(mwc64x_state_t* r,
float pixel, NoiseType noise_type,
float attenuate)
751 alpha=mwcReadPseudoRandomValue(r);
757 noise=(pixel+QuantumRange*SigmaUniform*(alpha-0.5f));
768 beta=mwcReadPseudoRandomValue(r);
769 gamma=sqrt(-2.0f*log(alpha));
770 sigma=gamma*cospi((2.0f*beta));
771 tau=gamma*sinpi((2.0f*beta));
772 noise=pixel+sqrt(pixel)*SigmaGaussian*sigma+QuantumRange*TauGaussian*tau;
777 if (alpha < (SigmaImpulse/2.0f))
780 if (alpha >= (1.0f-(SigmaImpulse/2.0f)))
790 if (alpha <= MagickEpsilon)
791 noise=(pixel-QuantumRange);
793 noise=(pixel+QuantumRange*SigmaLaplacian*log(2.0f*alpha)+
798 if (beta <= (0.5f*MagickEpsilon))
799 noise=(pixel+QuantumRange);
801 noise=(pixel-QuantumRange*SigmaLaplacian*log(2.0f*beta)+0.5f);
804 case MultiplicativeGaussianNoise:
807 if (alpha > MagickEpsilon)
808 sigma=sqrt(-2.0f*log(alpha));
809 beta=mwcReadPseudoRandomValue(r);
810 noise=(pixel+pixel*SigmaMultiplicativeGaussian*sigma*
811 cospi((2.0f*beta))/2.0f);
819 poisson=exp(-SigmaPoisson*QuantumScale*pixel);
820 for (i=0; alpha > poisson; i++)
822 beta=mwcReadPseudoRandomValue(r);
825 noise=(QuantumRange*i*MagickSafeReciprocal(SigmaPoisson));
830 noise=(QuantumRange*SigmaRandom*alpha);
854 __kernel
void BlurRow(
const __global CLQuantum *image,
855 const unsigned int number_channels,
const ChannelType channel,
856 __constant
float *filter,
const unsigned int width,
857 const unsigned int imageColumns,
const unsigned int imageRows,
858 __local float4 *temp,__global float4 *tempImage)
860 const int x = get_global_id(0);
861 const int y = get_global_id(1);
863 const int columns = imageColumns;
865 const unsigned int radius = (width-1)/2;
866 const int wsize = get_local_size(0);
867 const unsigned int loadSize = wsize+width;
870 const int groupX=get_local_size(0)*get_group_id(0);
873 for (
int i=get_local_id(0); i < loadSize; i=i+get_local_size(0))
875 int cx = ClampToCanvas(i + groupX - radius, columns);
876 temp[i] = ReadFloat4(image, number_channels, columns, cx, y, channel);
880 barrier(CLK_LOCAL_MEM_FENCE);
883 if (get_global_id(0) < columns)
886 float4 result = (float4) 0;
890 for ( ; i+7 < width; )
892 for (
int j=0; j < 8; j++)
893 result+=filter[i+j]*temp[i+j+get_local_id(0)];
897 for ( ; i < width; i++)
898 result+=filter[i]*temp[i+get_local_id(0)];
901 tempImage[y*columns+x] = result;
910 __kernel
void BlurColumn(
const __global float4 *blurRowData,
911 const unsigned int number_channels,
const ChannelType channel,
912 __constant
float *filter,
const unsigned int width,
913 const unsigned int imageColumns,
const unsigned int imageRows,
914 __local float4 *temp,__global CLQuantum *filteredImage)
916 const int x = get_global_id(0);
917 const int y = get_global_id(1);
919 const int columns = imageColumns;
920 const int rows = imageRows;
922 unsigned int radius = (width-1)/2;
923 const int wsize = get_local_size(1);
924 const unsigned int loadSize = wsize+width;
927 const int groupX=get_local_size(0)*get_group_id(0);
928 const int groupY=get_local_size(1)*get_group_id(1);
933 for (
int i = get_local_id(1); i < loadSize; i=i+get_local_size(1))
934 temp[i] = blurRowData[ClampToCanvas(i+groupY-radius, rows) * columns + groupX];
937 barrier(CLK_LOCAL_MEM_FENCE);
940 if (get_global_id(1) < rows)
943 float4 result = (float4) 0;
947 for ( ; i+7 < width; )
949 for (
int j=0; j < 8; j++)
950 result+=filter[i+j]*temp[i+j+get_local_id(1)];
954 for ( ; i < width; i++)
955 result+=filter[i]*temp[i+get_local_id(1)];
958 WriteFloat4(filteredImage, number_channels, columns, x, y, channel, result);
977 static inline float4 ConvertRGBToHSB(
const float4 pixel)
981 float tmax=MagickMax(MagickMax(pixel.x,pixel.y),pixel.z);
984 float tmin=MagickMin(MagickMin(pixel.x,pixel.y),pixel.z);
985 float delta=tmax-tmin;
988 result.z=QuantumScale*tmax;
991 result.x =((pixel.x == tmax) ? 0.0f : ((pixel.y == tmax) ? 2.0f : 4.0f));
992 result.x+=((pixel.x == tmax) ? (pixel.y-pixel.z) : ((pixel.y == tmax) ?
993 (pixel.z-pixel.x) : (pixel.x-pixel.y)))/delta;
995 result.x+=(result.x < 0.0f) ? 0.0f : 1.0f;
1001 static inline float4 ConvertHSBToRGB(
const float4 pixel)
1004 float saturation=pixel.y;
1005 float brightness=pixel.z;
1007 float4 result=pixel;
1009 if (saturation == 0.0f)
1011 result.x=result.y=result.z=ClampToQuantum(QuantumRange*brightness);
1015 float h=6.0f*(hue-floor(hue));
1017 float p=brightness*(1.0f-saturation);
1018 float q=brightness*(1.0f-saturation*f);
1019 float t=brightness*(1.0f-(saturation*(1.0f-f)));
1024 result.x=ClampToQuantum(QuantumRange*q);
1025 result.y=ClampToQuantum(QuantumRange*brightness);
1026 result.z=ClampToQuantum(QuantumRange*p);
1030 result.x=ClampToQuantum(QuantumRange*p);
1031 result.y=ClampToQuantum(QuantumRange*brightness);
1032 result.z=ClampToQuantum(QuantumRange*t);
1036 result.x=ClampToQuantum(QuantumRange*p);
1037 result.y=ClampToQuantum(QuantumRange*q);
1038 result.z=ClampToQuantum(QuantumRange*brightness);
1042 result.x=ClampToQuantum(QuantumRange*t);
1043 result.y=ClampToQuantum(QuantumRange*p);
1044 result.z=ClampToQuantum(QuantumRange*brightness);
1048 result.x=ClampToQuantum(QuantumRange*brightness);
1049 result.y=ClampToQuantum(QuantumRange*p);
1050 result.z=ClampToQuantum(QuantumRange*q);
1054 result.x=ClampToQuantum(QuantumRange*brightness);
1055 result.y=ClampToQuantum(QuantumRange*t);
1056 result.z=ClampToQuantum(QuantumRange*p);
1062 __kernel
void Contrast(__global CLQuantum *image,
1063 const unsigned int number_channels,
const int sign)
1065 const int x=get_global_id(0);
1066 const int y=get_global_id(1);
1067 const unsigned int columns=get_global_size(0);
1069 float4 pixel=ReadAllChannels(image,number_channels,columns,x,y);
1070 if (number_channels < 3)
1071 pixel.y=pixel.z=pixel.x;
1073 pixel=ConvertRGBToHSB(pixel);
1074 float brightness=pixel.z;
1075 brightness+=0.5f*sign*(0.5f*(sinpi(brightness-0.5f)+1.0f)-brightness);
1076 brightness=clamp(brightness,0.0f,1.0f);
1078 pixel=ConvertHSBToRGB(pixel);
1080 WriteAllChannels(image,number_channels,columns,x,y,pixel);
1099 __kernel
void Histogram(__global CLPixelType * restrict im,
1100 const ChannelType channel,
1101 const unsigned int colorspace,
1102 const unsigned int method,
1103 __global uint4 * restrict histogram)
1105 const int x = get_global_id(0);
1106 const int y = get_global_id(1);
1107 const int columns = get_global_size(0);
1108 const int c = x + y * columns;
1109 if ((channel & SyncChannels) != 0)
1111 float red=(float)getRed(im[c]);
1112 float green=(float)getGreen(im[c]);
1113 float blue=(float)getBlue(im[c]);
1115 float intensity = GetPixelIntensity(colorspace, method, red, green, blue);
1116 uint pos = ScaleQuantumToMap(ClampToQuantum(intensity));
1117 atomic_inc((__global uint *)(&(histogram[pos]))+2);
1130 __kernel
void ContrastStretch(__global CLPixelType * restrict im,
1131 const ChannelType channel,
1132 __global CLPixelType * restrict stretch_map,
1133 const float4 white,
const float4 black)
1135 const int x = get_global_id(0);
1136 const int y = get_global_id(1);
1137 const int columns = get_global_size(0);
1138 const int c = x + y * columns;
1141 CLPixelType oValue, eValue;
1142 CLQuantum red, green, blue, alpha;
1147 if ((channel & RedChannel) != 0)
1149 if (getRedF4(white) != getRedF4(black))
1151 ePos = ScaleQuantumToMap(getRed(oValue));
1152 eValue = stretch_map[ePos];
1153 red = getRed(eValue);
1157 if ((channel & GreenChannel) != 0)
1159 if (getGreenF4(white) != getGreenF4(black))
1161 ePos = ScaleQuantumToMap(getGreen(oValue));
1162 eValue = stretch_map[ePos];
1163 green = getGreen(eValue);
1167 if ((channel & BlueChannel) != 0)
1169 if (getBlueF4(white) != getBlueF4(black))
1171 ePos = ScaleQuantumToMap(getBlue(oValue));
1172 eValue = stretch_map[ePos];
1173 blue = getBlue(eValue);
1177 if ((channel & AlphaChannel) != 0)
1179 if (getAlphaF4(white) != getAlphaF4(black))
1181 ePos = ScaleQuantumToMap(getAlpha(oValue));
1182 eValue = stretch_map[ePos];
1183 alpha = getAlpha(eValue);
1188 im[c]=(CLPixelType)(blue, green, red, alpha);
1206 __kernel
void HullPass1(
const __global CLPixelType *inputImage, __global CLPixelType *outputImage
1207 ,
const unsigned int imageWidth,
const unsigned int imageHeight
1208 ,
const int2 offset,
const int polarity,
const int matte) {
1210 int x = get_global_id(0);
1211 int y = get_global_id(1);
1213 CLPixelType v = inputImage[y*imageWidth+x];
1216 neighbor.y = y + offset.y;
1217 neighbor.x = x + offset.x;
1219 int2 clampedNeighbor;
1220 clampedNeighbor.x = ClampToCanvas(neighbor.x, imageWidth);
1221 clampedNeighbor.y = ClampToCanvas(neighbor.y, imageHeight);
1223 CLPixelType r = (clampedNeighbor.x == neighbor.x
1224 && clampedNeighbor.y == neighbor.y)?inputImage[clampedNeighbor.y*imageWidth+clampedNeighbor.x]
1240 \n #pragma unroll 4\n
1241 for (
unsigned int i = 0; i < 4; i++) {
1242 sv[i] = (sr[i] >= (sv[i]+ScaleCharToQuantum(2)))?(sv[i]+ScaleCharToQuantum(1)):sv[i];
1246 \n #pragma unroll 4\n
1247 for (
unsigned int i = 0; i < 4; i++) {
1248 sv[i] = (sr[i] <= (sv[i]-ScaleCharToQuantum(2)))?(sv[i]-ScaleCharToQuantum(1)):sv[i];
1253 v.x = (CLQuantum)sv[0];
1254 v.y = (CLQuantum)sv[1];
1255 v.z = (CLQuantum)sv[2];
1258 v.w = (CLQuantum)sv[3];
1260 outputImage[y*imageWidth+x] = v;
1269 __kernel
void HullPass2(
const __global CLPixelType *inputImage, __global CLPixelType *outputImage
1270 ,
const unsigned int imageWidth,
const unsigned int imageHeight
1271 ,
const int2 offset,
const int polarity,
const int matte) {
1273 int x = get_global_id(0);
1274 int y = get_global_id(1);
1276 CLPixelType v = inputImage[y*imageWidth+x];
1278 int2 neighbor, clampedNeighbor;
1280 neighbor.y = y + offset.y;
1281 neighbor.x = x + offset.x;
1282 clampedNeighbor.x = ClampToCanvas(neighbor.x, imageWidth);
1283 clampedNeighbor.y = ClampToCanvas(neighbor.y, imageHeight);
1285 CLPixelType r = (clampedNeighbor.x == neighbor.x
1286 && clampedNeighbor.y == neighbor.y)?inputImage[clampedNeighbor.y*imageWidth+clampedNeighbor.x]
1290 neighbor.y = y - offset.y;
1291 neighbor.x = x - offset.x;
1292 clampedNeighbor.x = ClampToCanvas(neighbor.x, imageWidth);
1293 clampedNeighbor.y = ClampToCanvas(neighbor.y, imageHeight);
1295 CLPixelType s = (clampedNeighbor.x == neighbor.x
1296 && clampedNeighbor.y == neighbor.y)?inputImage[clampedNeighbor.y*imageWidth+clampedNeighbor.x]
1319 \n #pragma unroll 4\n
1320 for (
unsigned int i = 0; i < 4; i++) {
1325 sv[i] =(( (int)( ss[i] < (sv[i]+ScaleCharToQuantum(2))) + (int) ( sr[i] <= sv[i] ) ) !=0) ? sv[i]:(sv[i]+ScaleCharToQuantum(1));
1329 \n #pragma unroll 4\n
1330 for (
unsigned int i = 0; i < 4; i++) {
1334 sv[i] = (( (int)(ss[i] > (sv[i]-ScaleCharToQuantum(2))) + (int)( sr[i] >= sv[i] )) !=0) ? sv[i]:(sv[i]-ScaleCharToQuantum(1));
1338 v.x = (CLQuantum)sv[0];
1339 v.y = (CLQuantum)sv[1];
1340 v.z = (CLQuantum)sv[2];
1343 v.w = (CLQuantum)sv[3];
1345 outputImage[y*imageWidth+x] = v;
1365 __kernel
void Equalize(__global CLPixelType * restrict im,
1366 const ChannelType channel,
1367 __global CLPixelType * restrict equalize_map,
1368 const float4 white,
const float4 black)
1370 const int x = get_global_id(0);
1371 const int y = get_global_id(1);
1372 const int columns = get_global_size(0);
1373 const int c = x + y * columns;
1376 CLPixelType oValue, eValue;
1377 CLQuantum red, green, blue, alpha;
1382 if ((channel & SyncChannels) != 0)
1384 if (getRedF4(white) != getRedF4(black))
1386 ePos = ScaleQuantumToMap(getRed(oValue));
1387 eValue = equalize_map[ePos];
1388 red = getRed(eValue);
1389 ePos = ScaleQuantumToMap(getGreen(oValue));
1390 eValue = equalize_map[ePos];
1391 green = getRed(eValue);
1392 ePos = ScaleQuantumToMap(getBlue(oValue));
1393 eValue = equalize_map[ePos];
1394 blue = getRed(eValue);
1395 ePos = ScaleQuantumToMap(getAlpha(oValue));
1396 eValue = equalize_map[ePos];
1397 alpha = getRed(eValue);
1400 im[c]=(CLPixelType)(blue, green, red, alpha);
1427 CLQuantum ApplyFunction(
float pixel,
const MagickFunction function,
1428 const unsigned int number_parameters,__constant
float *parameters)
1430 float result = 0.0f;
1434 case PolynomialFunction:
1436 for (
unsigned int i=0; i < number_parameters; i++)
1437 result = result*QuantumScale*pixel + parameters[i];
1438 result *= QuantumRange;
1441 case SinusoidFunction:
1443 float freq,phase,ampl,bias;
1444 freq = ( number_parameters >= 1 ) ? parameters[0] : 1.0f;
1445 phase = ( number_parameters >= 2 ) ? parameters[1] : 0.0f;
1446 ampl = ( number_parameters >= 3 ) ? parameters[2] : 0.5f;
1447 bias = ( number_parameters >= 4 ) ? parameters[3] : 0.5f;
1448 result = QuantumRange*(ampl*sin(2.0f*MagickPI*
1449 (freq*QuantumScale*pixel + phase/360.0f)) + bias);
1452 case ArcsinFunction:
1454 float width,range,center,bias;
1455 width = ( number_parameters >= 1 ) ? parameters[0] : 1.0f;
1456 center = ( number_parameters >= 2 ) ? parameters[1] : 0.5f;
1457 range = ( number_parameters >= 3 ) ? parameters[2] : 1.0f;
1458 bias = ( number_parameters >= 4 ) ? parameters[3] : 0.5f;
1460 result = 2.0f/width*(QuantumScale*pixel - center);
1461 result = range/MagickPI*asin(result)+bias;
1462 result = ( result <= -1.0f ) ? bias - range/2.0f : result;
1463 result = ( result >= 1.0f ) ? bias + range/2.0f : result;
1464 result *= QuantumRange;
1467 case ArctanFunction:
1469 float slope,range,center,bias;
1470 slope = ( number_parameters >= 1 ) ? parameters[0] : 1.0f;
1471 center = ( number_parameters >= 2 ) ? parameters[1] : 0.5f;
1472 range = ( number_parameters >= 3 ) ? parameters[2] : 1.0f;
1473 bias = ( number_parameters >= 4 ) ? parameters[3] : 0.5f;
1474 result = MagickPI*slope*(QuantumScale*pixel-center);
1475 result = QuantumRange*(range/MagickPI*atan(result) + bias);
1478 case UndefinedFunction:
1481 return(ClampToQuantum(result));
1493 __kernel
void ComputeFunction(__global CLQuantum *image,
const unsigned int number_channels,
1494 const ChannelType channel,
const MagickFunction function,
const unsigned int number_parameters,
1495 __constant
float *parameters)
1497 const unsigned int x = get_global_id(0);
1498 const unsigned int y = get_global_id(1);
1499 const unsigned int columns = get_global_size(0);
1500 __global CLQuantum *p = image + getPixelIndex(number_channels, columns, x, y);
1507 ReadChannels(p, number_channels, channel, &red, &green, &blue, &alpha);
1509 if ((channel & RedChannel) != 0)
1510 red=ApplyFunction(red, function, number_parameters, parameters);
1512 if (number_channels > 2)
1514 if ((channel & GreenChannel) != 0)
1515 green=ApplyFunction(green, function, number_parameters, parameters);
1517 if ((channel & BlueChannel) != 0)
1518 blue=ApplyFunction(blue, function, number_parameters, parameters);
1521 if (((number_channels == 4) || (number_channels == 2)) &&
1522 ((channel & AlphaChannel) != 0))
1523 alpha=ApplyFunction(alpha, function, number_parameters, parameters);
1525 WriteChannels(p, number_channels, channel, red, green, blue, alpha);
1542 __kernel
void Grayscale(__global CLQuantum *image,
const int number_channels,
1543 const unsigned int colorspace,
const unsigned int method)
1545 const unsigned int x = get_global_id(0);
1546 const unsigned int y = get_global_id(1);
1547 const unsigned int columns = get_global_size(0);
1548 __global CLQuantum *p = image + getPixelIndex(number_channels, columns, x, y);
1556 green=getPixelGreen(p);
1557 blue=getPixelBlue(p);
1559 CLQuantum intensity=ClampToQuantum(GetPixelIntensity(colorspace, method, red, green, blue));
1561 setPixelRed(p,intensity);
1562 setPixelGreen(p,intensity);
1563 setPixelBlue(p,intensity);
1581 __kernel
void LocalContrastBlurRow(__global CLPixelType *srcImage, __global CLPixelType *dstImage, __global
float *tmpImage,
1583 const int imageWidth,
1584 const int imageHeight)
1586 const float4 RGB = ((float4)(0.2126f, 0.7152f, 0.0722f, 0.0f));
1588 int x = get_local_id(0);
1589 int y = get_global_id(1);
1591 if ((x >= imageWidth) || (y >= imageHeight))
1594 global CLPixelType *src = srcImage + y * imageWidth;
1596 for (
int i = x; i < imageWidth; i += get_local_size(0)) {
1598 float weight = 1.0f;
1601 while ((j + 7) < i) {
1602 for (
int k = 0; k < 8; ++k)
1603 sum += (weight + k) * dot(RGB, convert_float4(src[mirrorBottom(j+k)]));
1608 sum += weight * dot(RGB, convert_float4(src[mirrorBottom(j)]));
1613 while ((j + 7) < radius + i) {
1614 for (
int k = 0; k < 8; ++k)
1615 sum += (weight - k) * dot(RGB, convert_float4(src[mirrorTop(j + k, imageWidth)]));
1619 while (j < radius + i) {
1620 sum += weight * dot(RGB, convert_float4(src[mirrorTop(j, imageWidth)]));
1625 tmpImage[i + y * imageWidth] = sum / ((radius + 1) * (radius + 1));
1631 __kernel
void LocalContrastBlurApplyColumn(__global CLPixelType *srcImage, __global CLPixelType *dstImage, __global
float *blurImage,
1633 const float strength,
1634 const int imageWidth,
1635 const int imageHeight)
1637 const float4 RGB = (float4)(0.2126f, 0.7152f, 0.0722f, 0.0f);
1639 int x = get_global_id(0);
1640 int y = get_global_id(1);
1642 if ((x >= imageWidth) || (y >= imageHeight))
1645 global
float *src = blurImage + x;
1648 float weight = 1.0f;
1651 while ((j + 7) < y) {
1652 for (
int k = 0; k < 8; ++k)
1653 sum += (weight + k) * src[mirrorBottom(j+k) * imageWidth];
1658 sum += weight * src[mirrorBottom(j) * imageWidth];
1663 while ((j + 7) < radius + y) {
1664 for (
int k = 0; k < 8; ++k)
1665 sum += (weight - k) * src[mirrorTop(j + k, imageHeight) * imageWidth];
1669 while (j < radius + y) {
1670 sum += weight * src[mirrorTop(j, imageHeight) * imageWidth];
1675 CLPixelType pixel = srcImage[x + y * imageWidth];
1676 float srcVal = dot(RGB, convert_float4(pixel));
1677 float mult = (srcVal - (sum / ((radius + 1) * (radius + 1)))) * (strength / 100.0f);
1678 mult = (srcVal + mult) / srcVal;
1680 pixel.x = ClampToQuantum(pixel.x * mult);
1681 pixel.y = ClampToQuantum(pixel.y * mult);
1682 pixel.z = ClampToQuantum(pixel.z * mult);
1684 dstImage[x + y * imageWidth] = pixel;
1702 static inline void ConvertRGBToHSL(
const CLQuantum red,
const CLQuantum green,
const CLQuantum blue,
1703 float *hue,
float *saturation,
float *lightness)
1713 tmax=MagickMax(QuantumScale*red,MagickMax(QuantumScale*green, QuantumScale*blue));
1714 tmin=MagickMin(QuantumScale*red,MagickMin(QuantumScale*green, QuantumScale*blue));
1718 *lightness=(tmax+tmin)/2.0;
1726 if (tmax == (QuantumScale*red))
1728 *hue=(QuantumScale*green-QuantumScale*blue)/c;
1729 if ((QuantumScale*green) < (QuantumScale*blue))
1733 if (tmax == (QuantumScale*green))
1734 *hue=2.0+(QuantumScale*blue-QuantumScale*red)/c;
1736 *hue=4.0+(QuantumScale*red-QuantumScale*green)/c;
1739 if (*lightness <= 0.5)
1740 *saturation=c/(2.0*(*lightness));
1742 *saturation=c/(2.0-2.0*(*lightness));
1745 static inline void ConvertHSLToRGB(
const float hue,
const float saturation,
const float lightness,
1746 CLQuantum *red,CLQuantum *green,CLQuantum *blue)
1761 if (lightness <= 0.5)
1762 c=2.0*lightness*saturation;
1764 c=(2.0-2.0*lightness)*saturation;
1765 tmin=lightness-0.5*c;
1766 h-=360.0*floor(h/360.0);
1768 x=c*(1.0-fabs(h-2.0*floor(h/2.0)-1.0));
1769 switch ((
int) floor(h) % 6)
1815 *red=ClampToQuantum(QuantumRange*r);
1816 *green=ClampToQuantum(QuantumRange*g);
1817 *blue=ClampToQuantum(QuantumRange*b);
1820 static inline void ModulateHSL(
const float percent_hue,
const float percent_saturation,
const float percent_lightness,
1821 CLQuantum *red,CLQuantum *green,CLQuantum *blue)
1831 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
1832 hue+=0.5*(0.01*percent_hue-1.0);
1837 saturation*=0.01*percent_saturation;
1838 lightness*=0.01*percent_lightness;
1839 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
1842 __kernel
void Modulate(__global CLPixelType *im,
1843 const float percent_brightness,
1844 const float percent_hue,
1845 const float percent_saturation,
1846 const int colorspace)
1849 const int x = get_global_id(0);
1850 const int y = get_global_id(1);
1851 const int columns = get_global_size(0);
1852 const int c = x + y * columns;
1854 CLPixelType pixel = im[c];
1862 green=getGreen(pixel);
1863 blue=getBlue(pixel);
1870 ModulateHSL(percent_hue, percent_saturation, percent_brightness,
1871 &red, &green, &blue);
1876 CLPixelType filteredPixel;
1878 setRed(&filteredPixel, red);
1879 setGreen(&filteredPixel, green);
1880 setBlue(&filteredPixel, blue);
1881 filteredPixel.w = pixel.w;
1883 im[c] = filteredPixel;
1901 void MotionBlur(
const __global CLPixelType *input, __global CLPixelType *output,
1902 const unsigned int imageWidth,
const unsigned int imageHeight,
1903 const __global
float *filter,
const unsigned int width,
const __global int2* offset,
1905 const ChannelType channel,
const unsigned int matte) {
1908 currentPixel.x = get_global_id(0);
1909 currentPixel.y = get_global_id(1);
1911 if (currentPixel.x >= imageWidth
1912 || currentPixel.y >= imageHeight)
1916 pixel.x = (float)bias.x;
1917 pixel.y = (float)bias.y;
1918 pixel.z = (float)bias.z;
1919 pixel.w = (float)bias.w;
1921 if (((channel & AlphaChannel) == 0) || (matte == 0)) {
1923 for (
int i = 0; i < width; i++) {
1926 int2 samplePixel = currentPixel + offset[i];
1927 samplePixel.x = ClampToCanvas(samplePixel.x, imageWidth);
1928 samplePixel.y = ClampToCanvas(samplePixel.y, imageHeight);
1929 CLPixelType samplePixelValue = input[ samplePixel.y * imageWidth + samplePixel.x];
1931 pixel.x += (filter[i] * (float)samplePixelValue.x);
1932 pixel.y += (filter[i] * (float)samplePixelValue.y);
1933 pixel.z += (filter[i] * (float)samplePixelValue.z);
1934 pixel.w += (filter[i] * (float)samplePixelValue.w);
1937 CLPixelType outputPixel;
1938 outputPixel.x = ClampToQuantum(pixel.x);
1939 outputPixel.y = ClampToQuantum(pixel.y);
1940 outputPixel.z = ClampToQuantum(pixel.z);
1941 outputPixel.w = ClampToQuantum(pixel.w);
1942 output[currentPixel.y * imageWidth + currentPixel.x] = outputPixel;
1947 for (
int i = 0; i < width; i++) {
1950 int2 samplePixel = currentPixel + offset[i];
1951 samplePixel.x = ClampToCanvas(samplePixel.x, imageWidth);
1952 samplePixel.y = ClampToCanvas(samplePixel.y, imageHeight);
1954 CLPixelType samplePixelValue = input[ samplePixel.y * imageWidth + samplePixel.x];
1956 float alpha = QuantumScale*samplePixelValue.w;
1957 float k = filter[i];
1958 pixel.x = pixel.x + k * alpha * samplePixelValue.x;
1959 pixel.y = pixel.y + k * alpha * samplePixelValue.y;
1960 pixel.z = pixel.z + k * alpha * samplePixelValue.z;
1962 pixel.w += k * alpha * samplePixelValue.w;
1966 gamma = MagickSafeReciprocal(gamma);
1967 pixel.xyz = gamma*pixel.xyz;
1969 CLPixelType outputPixel;
1970 outputPixel.x = ClampToQuantum(pixel.x);
1971 outputPixel.y = ClampToQuantum(pixel.y);
1972 outputPixel.z = ClampToQuantum(pixel.z);
1973 outputPixel.w = ClampToQuantum(pixel.w);
1974 output[currentPixel.y * imageWidth + currentPixel.x] = outputPixel;
1993 float BoxResizeFilter(
const float x)
2001 float CubicBC(
const float x,
const __global
float* resizeFilterCoefficients)
2033 return(resizeFilterCoefficients[0]+x*(x*
2034 (resizeFilterCoefficients[1]+x*resizeFilterCoefficients[2])));
2036 return(resizeFilterCoefficients[3]+x*(resizeFilterCoefficients[4]+x*
2037 (resizeFilterCoefficients[5]+x*resizeFilterCoefficients[6])));
2043 float Sinc(
const float x)
2047 const float alpha=(float) (MagickPI*x);
2048 return sinpi(x)/alpha;
2055 float Triangle(
const float x)
2062 return ((x<1.0f)?(1.0f-x):0.0f);
2068 float Hann(
const float x)
2074 const float cosine=cos((MagickPI*x));
2075 return(0.5f+0.5f*cosine);
2080 float Hamming(
const float x)
2086 const float cosine=cos((MagickPI*x));
2087 return(0.54f+0.46f*cosine);
2092 float Blackman(
const float x)
2101 const float cosine=cos((MagickPI*x));
2102 return(0.34f+cosine*(0.5f+cosine*0.16f));
2107 static inline float applyResizeFilter(
const float x,
const ResizeWeightingFunctionType filterType,
const __global
float* filterCoefficients)
2113 case SincWeightingFunction:
2114 case SincFastWeightingFunction:
2116 case CubicBCWeightingFunction:
2117 return CubicBC(x,filterCoefficients);
2118 case BoxWeightingFunction:
2119 return BoxResizeFilter(x);
2120 case TriangleWeightingFunction:
2122 case HannWeightingFunction:
2124 case HammingWeightingFunction:
2126 case BlackmanWeightingFunction:
2137 static inline float getResizeFilterWeight(
const __global
float* resizeFilterCubicCoefficients,
const ResizeWeightingFunctionType resizeFilterType
2138 ,
const ResizeWeightingFunctionType resizeWindowType
2139 ,
const float resizeFilterScale,
const float resizeWindowSupport,
const float resizeFilterBlur,
const float x)
2142 float xBlur = fabs(x/resizeFilterBlur);
2143 if (resizeWindowSupport < MagickEpsilon
2144 || resizeWindowType == BoxWeightingFunction)
2150 scale = resizeFilterScale;
2151 scale = applyResizeFilter(xBlur*scale, resizeWindowType, resizeFilterCubicCoefficients);
2153 float weight = scale * applyResizeFilter(xBlur, resizeFilterType, resizeFilterCubicCoefficients);
2160 const char *accelerateKernels2 =
2164 static inline unsigned int getNumWorkItemsPerPixel(
const unsigned int pixelPerWorkgroup,
const unsigned int numWorkItems) {
2165 return (numWorkItems/pixelPerWorkgroup);
2170 static inline int pixelToCompute(
const unsigned itemID,
const unsigned int pixelPerWorkgroup,
const unsigned int numWorkItems) {
2171 const unsigned int numWorkItemsPerPixel = getNumWorkItemsPerPixel(pixelPerWorkgroup, numWorkItems);
2172 int pixelIndex = itemID/numWorkItemsPerPixel;
2173 pixelIndex = (pixelIndex<pixelPerWorkgroup)?pixelIndex:-1;
2180 __kernel __attribute__((reqd_work_group_size(256, 1, 1)))
2181 void ResizeHorizontalFilter(
const __global CLQuantum *inputImage,
const unsigned int number_channels,
2182 const unsigned int inputColumns,
const unsigned int inputRows, __global CLQuantum *filteredImage,
2183 const unsigned int filteredColumns,
const unsigned int filteredRows,
const float xFactor,
2184 const int resizeFilterType,
const int resizeWindowType,
const __global
float *resizeFilterCubicCoefficients,
2185 const float resizeFilterScale,
const float resizeFilterSupport,
const float resizeFilterWindowSupport,
2186 const float resizeFilterBlur, __local CLQuantum *inputImageCache,
const int numCachedPixels,
2187 const unsigned int pixelPerWorkgroup,
const unsigned int pixelChunkSize,
2188 __local float4 *outputPixelCache, __local
float *densityCache, __local
float *gammaCache)
2191 const unsigned int startX = get_group_id(0)*pixelPerWorkgroup;
2192 const unsigned int stopX = MagickMin(startX + pixelPerWorkgroup,filteredColumns);
2193 const unsigned int actualNumPixelToCompute = stopX - startX;
2196 float scale = MagickMax(1.0f/xFactor+MagickEpsilon ,1.0f);
2197 const float support = MagickMax(scale*resizeFilterSupport,0.5f);
2198 scale = MagickSafeReciprocal(scale);
2200 const int cacheRangeStartX = MagickMax((
int)((startX+0.5f)/xFactor+MagickEpsilon-support+0.5f),(
int)(0));
2201 const int cacheRangeEndX = MagickMin((
int)(cacheRangeStartX + numCachedPixels), (
int)inputColumns);
2204 const unsigned int y = get_global_id(1);
2205 const unsigned int pos = getPixelIndex(number_channels, inputColumns, cacheRangeStartX, y);
2206 const unsigned int num_elements = (cacheRangeEndX - cacheRangeStartX) * number_channels;
2207 event_t e = async_work_group_copy(inputImageCache, inputImage + pos, num_elements, 0);
2208 wait_group_events(1, &e);
2210 unsigned int alpha_index = (number_channels == 4) || (number_channels == 2) ? number_channels - 1 : 0;
2211 unsigned int totalNumChunks = (actualNumPixelToCompute+pixelChunkSize-1)/pixelChunkSize;
2212 for (
unsigned int chunk = 0; chunk < totalNumChunks; chunk++)
2214 const unsigned int chunkStartX = startX + chunk*pixelChunkSize;
2215 const unsigned int chunkStopX = MagickMin(chunkStartX + pixelChunkSize, stopX);
2216 const unsigned int actualNumPixelInThisChunk = chunkStopX - chunkStartX;
2219 const unsigned int itemID = get_local_id(0);
2220 const unsigned int numItems = getNumWorkItemsPerPixel(actualNumPixelInThisChunk, get_local_size(0));
2222 const int pixelIndex = pixelToCompute(itemID, actualNumPixelInThisChunk, get_local_size(0));
2224 float4 filteredPixel = (float4)0.0f;
2225 float density = 0.0f;
2228 if (pixelIndex != -1)
2231 const int x = chunkStartX + pixelIndex;
2234 const float bisect = (x+0.5)/xFactor+MagickEpsilon;
2235 const unsigned int start = (
unsigned int)MagickMax(bisect-support+0.5f,0.0f);
2236 const unsigned int stop = (
unsigned int)MagickMin(bisect+support+0.5f,(
float)inputColumns);
2237 const unsigned int n = stop - start;
2240 unsigned int numStepsPerWorkItem = n / numItems;
2241 numStepsPerWorkItem += ((numItems*numStepsPerWorkItem)==n?0:1);
2243 const unsigned int startStep = (itemID%numItems)*numStepsPerWorkItem;
2246 const unsigned int stopStep = MagickMin(startStep+numStepsPerWorkItem, n);
2248 unsigned int cacheIndex = start+startStep-cacheRangeStartX;
2249 for (
unsigned int i = startStep; i < stopStep; i++, cacheIndex++)
2251 float weight = getResizeFilterWeight(resizeFilterCubicCoefficients,
2252 (ResizeWeightingFunctionType) resizeFilterType,
2253 (ResizeWeightingFunctionType) resizeWindowType,
2254 resizeFilterScale, resizeFilterWindowSupport,
2255 resizeFilterBlur, scale*(start + i - bisect + 0.5));
2257 float4 cp = (float4)0.0f;
2259 __local CLQuantum *p = inputImageCache + (cacheIndex*number_channels);
2260 cp.x = (float) *(p);
2261 if (number_channels > 2)
2263 cp.y = (float) *(p + 1);
2264 cp.z = (float) *(p + 2);
2267 if (alpha_index != 0)
2269 cp.w = (float) *(p + alpha_index);
2271 float alpha = weight * QuantumScale * cp.w;
2273 filteredPixel.x += alpha * cp.x;
2274 filteredPixel.y += alpha * cp.y;
2275 filteredPixel.z += alpha * cp.z;
2276 filteredPixel.w += weight * cp.w;
2280 filteredPixel += ((float4) weight)*cp;
2288 if (itemID < actualNumPixelInThisChunk) {
2289 outputPixelCache[itemID] = (float4)0.0f;
2290 densityCache[itemID] = 0.0f;
2291 if (alpha_index != 0)
2292 gammaCache[itemID] = 0.0f;
2294 barrier(CLK_LOCAL_MEM_FENCE);
2297 for (
unsigned int i = 0; i < numItems; i++) {
2298 if (pixelIndex != -1) {
2299 if (itemID%numItems == i) {
2300 outputPixelCache[pixelIndex]+=filteredPixel;
2301 densityCache[pixelIndex]+=density;
2302 if (alpha_index != 0)
2303 gammaCache[pixelIndex]+=gamma;
2306 barrier(CLK_LOCAL_MEM_FENCE);
2309 if (itemID < actualNumPixelInThisChunk)
2311 float4 filteredPixel = outputPixelCache[itemID];
2314 if (alpha_index != 0)
2315 gamma = gammaCache[itemID];
2317 float density = densityCache[itemID];
2318 if ((density != 0.0f) && (density != 1.0f))
2320 density = MagickSafeReciprocal(density);
2321 filteredPixel *= (float4) density;
2322 if (alpha_index != 0)
2326 if (alpha_index != 0)
2328 gamma = MagickSafeReciprocal(gamma);
2329 filteredPixel.x *= gamma;
2330 filteredPixel.y *= gamma;
2331 filteredPixel.z *= gamma;
2334 WriteAllChannels(filteredImage, number_channels, filteredColumns, chunkStartX + itemID, y, filteredPixel);
2342 __kernel __attribute__((reqd_work_group_size(1, 256, 1)))
2343 void ResizeVerticalFilter(
const __global CLQuantum *inputImage,
const unsigned int number_channels,
2344 const unsigned int inputColumns,
const unsigned int inputRows, __global CLQuantum *filteredImage,
2345 const unsigned int filteredColumns,
const unsigned int filteredRows,
const float yFactor,
2346 const int resizeFilterType,
const int resizeWindowType,
const __global
float *resizeFilterCubicCoefficients,
2347 const float resizeFilterScale,
const float resizeFilterSupport,
const float resizeFilterWindowSupport,
2348 const float resizeFilterBlur, __local CLQuantum *inputImageCache,
const int numCachedPixels,
2349 const unsigned int pixelPerWorkgroup,
const unsigned int pixelChunkSize,
2350 __local float4 *outputPixelCache, __local
float *densityCache, __local
float *gammaCache)
2353 const unsigned int startY = get_group_id(1)*pixelPerWorkgroup;
2354 const unsigned int stopY = MagickMin(startY + pixelPerWorkgroup,filteredRows);
2355 const unsigned int actualNumPixelToCompute = stopY - startY;
2358 float scale = MagickMax(1.0f/yFactor+MagickEpsilon ,1.0f);
2359 const float support = MagickMax(scale*resizeFilterSupport,0.5f);
2360 scale = MagickSafeReciprocal(scale);
2362 const int cacheRangeStartY = MagickMax((
int)((startY+0.5f)/yFactor+MagickEpsilon-support+0.5f),(
int)(0));
2363 const int cacheRangeEndY = MagickMin((
int)(cacheRangeStartY + numCachedPixels), (
int)inputRows);
2366 const unsigned int x = get_global_id(0);
2367 unsigned int pos = getPixelIndex(number_channels, inputColumns, x, cacheRangeStartY);
2368 unsigned int rangeLength = cacheRangeEndY-cacheRangeStartY;
2369 unsigned int stride = inputColumns * number_channels;
2370 for (
unsigned int i = 0; i < number_channels; i++)
2372 event_t e = async_work_group_strided_copy(inputImageCache + (rangeLength*i), inputImage+pos+i, rangeLength, stride, 0);
2373 wait_group_events(1,&e);
2376 unsigned int alpha_index = (number_channels == 4) || (number_channels == 2) ? number_channels - 1 : 0;
2377 unsigned int totalNumChunks = (actualNumPixelToCompute+pixelChunkSize-1)/pixelChunkSize;
2378 for (
unsigned int chunk = 0; chunk < totalNumChunks; chunk++)
2380 const unsigned int chunkStartY = startY + chunk*pixelChunkSize;
2381 const unsigned int chunkStopY = MagickMin(chunkStartY + pixelChunkSize, stopY);
2382 const unsigned int actualNumPixelInThisChunk = chunkStopY - chunkStartY;
2385 const unsigned int itemID = get_local_id(1);
2386 const unsigned int numItems = getNumWorkItemsPerPixel(actualNumPixelInThisChunk, get_local_size(1));
2388 const int pixelIndex = pixelToCompute(itemID, actualNumPixelInThisChunk, get_local_size(1));
2390 float4 filteredPixel = (float4)0.0f;
2391 float density = 0.0f;
2394 if (pixelIndex != -1)
2397 const int y = chunkStartY + pixelIndex;
2400 const float bisect = (y+0.5)/yFactor+MagickEpsilon;
2401 const unsigned int start = (
unsigned int)MagickMax(bisect-support+0.5f,0.0f);
2402 const unsigned int stop = (
unsigned int)MagickMin(bisect+support+0.5f,(
float)inputRows);
2403 const unsigned int n = stop - start;
2406 unsigned int numStepsPerWorkItem = n / numItems;
2407 numStepsPerWorkItem += ((numItems*numStepsPerWorkItem)==n?0:1);
2409 const unsigned int startStep = (itemID%numItems)*numStepsPerWorkItem;
2412 const unsigned int stopStep = MagickMin(startStep+numStepsPerWorkItem, n);
2414 unsigned int cacheIndex = start+startStep-cacheRangeStartY;
2415 for (
unsigned int i = startStep; i < stopStep; i++, cacheIndex++)
2417 float weight = getResizeFilterWeight(resizeFilterCubicCoefficients,
2418 (ResizeWeightingFunctionType) resizeFilterType,
2419 (ResizeWeightingFunctionType) resizeWindowType,
2420 resizeFilterScale, resizeFilterWindowSupport,
2421 resizeFilterBlur, scale*(start + i - bisect + 0.5));
2423 float4 cp = (float4)0.0f;
2425 __local CLQuantum *p = inputImageCache + cacheIndex;
2426 cp.x = (float) *(p);
2427 if (number_channels > 2)
2429 cp.y = (float) *(p + rangeLength);
2430 cp.z = (float) *(p + (rangeLength * 2));
2433 if (alpha_index != 0)
2435 cp.w = (float) *(p + (rangeLength * alpha_index));
2437 float alpha = weight * QuantumScale * cp.w;
2439 filteredPixel.x += alpha * cp.x;
2440 filteredPixel.y += alpha * cp.y;
2441 filteredPixel.z += alpha * cp.z;
2442 filteredPixel.w += weight * cp.w;
2446 filteredPixel += ((float4) weight)*cp;
2454 if (itemID < actualNumPixelInThisChunk) {
2455 outputPixelCache[itemID] = (float4)0.0f;
2456 densityCache[itemID] = 0.0f;
2457 if (alpha_index != 0)
2458 gammaCache[itemID] = 0.0f;
2460 barrier(CLK_LOCAL_MEM_FENCE);
2463 for (
unsigned int i = 0; i < numItems; i++) {
2464 if (pixelIndex != -1) {
2465 if (itemID%numItems == i) {
2466 outputPixelCache[pixelIndex]+=filteredPixel;
2467 densityCache[pixelIndex]+=density;
2468 if (alpha_index != 0)
2469 gammaCache[pixelIndex]+=gamma;
2472 barrier(CLK_LOCAL_MEM_FENCE);
2475 if (itemID < actualNumPixelInThisChunk)
2477 float4 filteredPixel = outputPixelCache[itemID];
2480 if (alpha_index != 0)
2481 gamma = gammaCache[itemID];
2483 float density = densityCache[itemID];
2484 if ((density != 0.0f) && (density != 1.0f))
2486 density = MagickSafeReciprocal(density);
2487 filteredPixel *= (float4) density;
2488 if (alpha_index != 0)
2492 if (alpha_index != 0)
2494 gamma = MagickSafeReciprocal(gamma);
2495 filteredPixel.x *= gamma;
2496 filteredPixel.y *= gamma;
2497 filteredPixel.z *= gamma;
2500 WriteAllChannels(filteredImage, number_channels, filteredColumns, x, chunkStartY + itemID, filteredPixel);
2519 __kernel
void RotationalBlur(
const __global CLQuantum *image,
2520 const unsigned int number_channels,
const unsigned int channel,
2521 const float2 blurCenter,__constant
float *cos_theta,
2522 __constant
float *sin_theta,
const unsigned int cossin_theta_size,
2523 __global CLQuantum *filteredImage)
2525 const int x = get_global_id(0);
2526 const int y = get_global_id(1);
2527 const int columns = get_global_size(0);
2528 const int rows = get_global_size(1);
2529 unsigned int step = 1;
2530 float center_x = (float) x - blurCenter.x;
2531 float center_y = (float) y - blurCenter.y;
2532 float radius = hypot(center_x, center_y);
2534 float blur_radius = hypot(blurCenter.x, blurCenter.y);
2536 if (radius > MagickEpsilon)
2538 step = (
unsigned int) (blur_radius / radius);
2541 if (step >= cossin_theta_size)
2542 step = cossin_theta_size-1;
2545 float4 result = 0.0f;
2546 float normalize = 0.0f;
2549 for (
unsigned int i=0; i<cossin_theta_size; i+=step)
2551 int cx = ClampToCanvas(blurCenter.x+center_x*cos_theta[i]-center_y*sin_theta[i]+0.5f,columns);
2552 int cy = ClampToCanvas(blurCenter.y+center_x*sin_theta[i]+center_y*cos_theta[i]+0.5f,rows);
2554 float4 pixel = ReadAllChannels(image, number_channels, columns, cx, cy);
2556 if ((number_channels == 4) || (number_channels == 2))
2558 float alpha = (float)(QuantumScale*pixel.w);
2562 result.x += alpha * pixel.x;
2563 result.y += alpha * pixel.y;
2564 result.z += alpha * pixel.z;
2565 result.w += pixel.w;
2573 normalize = MagickSafeReciprocal(normalize);
2575 if ((number_channels == 4) || (number_channels == 2))
2577 gamma = MagickSafeReciprocal(gamma);
2581 result.w *= normalize;
2584 result *= normalize;
2586 WriteFloat4(filteredImage, number_channels, columns, x, y, channel, result);
2603 __kernel
void UnsharpMaskBlurColumn(
const __global CLQuantum* image,
2604 const __global float4 *blurRowData,
const unsigned int number_channels,
2605 const ChannelType channel,
const unsigned int columns,
2606 const unsigned int rows,__local float4* cachedData,
2607 __local
float* cachedFilter,
const __global
float *filter,
2608 const unsigned int width,
const float gain,
const float threshold,
2609 __global CLQuantum *filteredImage)
2611 const unsigned int radius = (width-1)/2;
2614 const int groupX = get_group_id(0);
2615 const int groupStartY = get_group_id(1)*get_local_size(1) - radius;
2616 const int groupStopY = (get_group_id(1)+1)*get_local_size(1) + radius;
2618 if ((groupStartY >= 0) && (groupStopY < rows))
2620 event_t e = async_work_group_strided_copy(cachedData,
2621 blurRowData+groupStartY*columns+groupX,groupStopY-groupStartY,columns,0);
2622 wait_group_events(1,&e);
2626 for (
int i = get_local_id(1); i < (groupStopY - groupStartY); i+=get_local_size(1))
2627 cachedData[i] = blurRowData[ClampToCanvas(groupStartY+i,rows)*columns + groupX];
2629 barrier(CLK_LOCAL_MEM_FENCE);
2632 event_t e = async_work_group_copy(cachedFilter,filter,width,0);
2633 wait_group_events(1,&e);
2636 const int cy = get_global_id(1);
2640 float4 blurredPixel = (float4) 0.0f;
2644 for ( ; i+7 < width; )
2646 for (
int j=0; j < 8; j++, i++)
2647 blurredPixel+=cachedFilter[i+j]*cachedData[i+j+get_local_id(1)];
2650 for ( ; i < width; i++)
2651 blurredPixel+=cachedFilter[i]*cachedData[i+get_local_id(1)];
2653 float4 inputImagePixel = ReadFloat4(image,number_channels,columns,groupX,cy,channel);
2654 float4 outputPixel = inputImagePixel - blurredPixel;
2656 float quantumThreshold = QuantumRange*threshold;
2658 int4 mask = isless(fabs(2.0f*outputPixel), (float4)quantumThreshold);
2659 outputPixel = select(inputImagePixel + outputPixel * gain, inputImagePixel, mask);
2662 WriteFloat4(filteredImage,number_channels,columns,groupX,cy,channel,outputPixel);
2668 __kernel
void UnsharpMask(
const __global CLQuantum *image,
const unsigned int number_channels,
2669 const ChannelType channel,__constant
float *filter,
const unsigned int width,
2670 const unsigned int columns,
const unsigned int rows,__local float4 *pixels,
2671 const float gain,
const float threshold,__global CLQuantum *filteredImage)
2673 const unsigned int x = get_global_id(0);
2674 const unsigned int y = get_global_id(1);
2676 const unsigned int radius = (width - 1) / 2;
2678 int row = y - radius;
2679 int baseRow = get_group_id(1) * get_local_size(1) - radius;
2680 int endRow = (get_group_id(1) + 1) * get_local_size(1) + radius;
2682 while (row < endRow) {
2683 int srcy = (row < 0) ? -row : row;
2684 srcy = (srcy >= rows) ? (2 * rows - srcy - 1) : srcy;
2686 float4 value = 0.0f;
2688 int ix = x - radius;
2691 while (i + 7 < width) {
2692 for (
int j = 0; j < 8; ++j) {
2694 srcx = (srcx < 0) ? -srcx : srcx;
2695 srcx = (srcx >= columns) ? (2 * columns - srcx - 1) : srcx;
2696 value += filter[i + j] * ReadFloat4(image, number_channels, columns, srcx, srcy, channel);
2703 int srcx = (ix < 0) ? -ix : ix;
2704 srcx = (srcx >= columns) ? (2 * columns - srcx - 1) : srcx;
2705 value += filter[i] * ReadFloat4(image, number_channels, columns, srcx, srcy, channel);
2709 pixels[(row - baseRow) * get_local_size(0) + get_local_id(0)] = value;
2710 row += get_local_size(1);
2713 barrier(CLK_LOCAL_MEM_FENCE);
2715 const int px = get_local_id(0);
2716 const int py = get_local_id(1);
2717 const int prp = get_local_size(0);
2718 float4 value = (float4)(0.0f);
2721 while (i + 7 < width) {
2722 for (
int j = 0; j < 8; ++j)
2723 value += (float4)(filter[i]) * pixels[px + (py + i + j) * prp];
2727 value += (float4)(filter[i]) * pixels[px + (py + i) * prp];
2731 if ((x < columns) && (y < rows)) {
2732 float4 srcPixel = ReadFloat4(image, number_channels, columns, x, y, channel);
2733 float4 diff = srcPixel - value;
2735 float quantumThreshold = QuantumRange*threshold;
2737 int4 mask = isless(fabs(2.0f * diff), (float4)quantumThreshold);
2738 value = select(srcPixel + diff * gain, srcPixel, mask);
2740 WriteFloat4(filteredImage, number_channels, columns, x, y, channel, value);
2758 __kernel __attribute__((reqd_work_group_size(64, 4, 1)))
2759 void WaveletDenoise(__global CLQuantum *srcImage,__global CLQuantum *dstImage,
2760 const unsigned int number_channels,
const unsigned int max_channels,
2761 const float threshold,
const int passes,
const unsigned int imageWidth,
2762 const unsigned int imageHeight)
2764 const int pad = (1 << (passes - 1));
2765 const int tileSize = 64;
2766 const int tileRowPixels = 64;
2767 const float noise[] = { 0.8002, 0.2735, 0.1202, 0.0585, 0.0291, 0.0152, 0.0080, 0.0044 };
2769 CLQuantum stage[48];
2771 local
float buffer[64 * 64];
2773 int srcx = (get_group_id(0) + get_global_offset(0) / tileSize) * (tileSize - 2 * pad) - pad + get_local_id(0);
2774 int srcy = (get_group_id(1) + get_global_offset(1) / 4) * (tileSize - 2 * pad) - pad;
2776 for (
int i = get_local_id(1); i < tileSize; i += get_local_size(1)) {
2777 int pos = (mirrorTop(mirrorBottom(srcx), imageWidth) * number_channels) +
2778 (mirrorTop(mirrorBottom(srcy + i), imageHeight)) * imageWidth * number_channels;
2780 for (
int channel = 0; channel < max_channels; ++channel)
2781 stage[(i / 4) + (16 * channel)] = srcImage[pos + channel];
2784 for (
int channel = 0; channel < max_channels; ++channel) {
2786 for (
int i = get_local_id(1); i < tileSize; i += get_local_size(1))
2787 buffer[get_local_id(0) + i * tileRowPixels] = convert_float(stage[(i / 4) + (16 * channel)]);
2795 for (
int i = 0; i < 16; i++)
2798 for (
int pass = 0; pass < passes; ++pass) {
2799 const int radius = 1 << pass;
2800 const int x = get_local_id(0);
2801 const float thresh = threshold * noise[pass];
2804 for (
int i = get_local_id(1); i < tileSize; i += get_local_size(1)) {
2805 const int offset = i * tileRowPixels;
2807 tmp[i / 4] = buffer[x + offset];
2808 pixel = 0.5f * tmp[i / 4] + 0.25 * (buffer[mirrorBottom(x - radius) + offset] + buffer[mirrorTop(x + radius, tileSize) + offset]);
2809 barrier(CLK_LOCAL_MEM_FENCE);
2810 buffer[x + offset] = pixel;
2812 barrier(CLK_LOCAL_MEM_FENCE);
2815 for (
int i = get_local_id(1); i < tileSize; i += get_local_size(1)) {
2816 pixel = 0.5f * buffer[x + i * tileRowPixels] + 0.25 * (buffer[x + mirrorBottom(i - radius) * tileRowPixels] + buffer[x + mirrorTop(i + radius, tileRowPixels) * tileRowPixels]);
2817 float delta = tmp[i / 4] - pixel;
2819 if (delta < -thresh)
2821 else if (delta > thresh)
2825 accum[i / 4] += delta;
2827 barrier(CLK_LOCAL_MEM_FENCE);
2829 if (pass < passes - 1)
2830 for (
int i = get_local_id(1); i < tileSize; i += get_local_size(1))
2831 buffer[x + i * tileRowPixels] = tmp[i / 4];
2833 for (
int i = get_local_id(1); i < tileSize; i += get_local_size(1))
2834 accum[i / 4] += tmp[i / 4];
2835 barrier(CLK_LOCAL_MEM_FENCE);
2838 for (
int i = get_local_id(1); i < tileSize; i += get_local_size(1))
2839 stage[(i / 4) + (16 * channel)] = ClampToQuantum(accum[i / 4]);
2841 barrier(CLK_LOCAL_MEM_FENCE);
2846 if ((get_local_id(0) >= pad) && (get_local_id(0) < tileSize - pad) && (srcx >= 0) && (srcx < imageWidth)) {
2847 for (
int i = get_local_id(1); i < tileSize; i += get_local_size(1)) {
2848 if ((i >= pad) && (i < tileSize - pad) && (srcy + i >= 0) && (srcy + i < imageHeight)) {
2849 int pos = (srcx * number_channels) + ((srcy + i) * (imageWidth * number_channels));
2850 for (
int channel = 0; channel < max_channels; ++channel) {
2851 dstImage[pos + channel] = stage[(i / 4) + (16 * channel)];
2863#if defined(__cplusplus) || defined(c_plusplus)