MagickCore 7.1.2-37
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
accelerate-kernels-private.h
1/*
2 Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
4
5 You may not use this file except in compliance with the License. You may
6 obtain a copy of the License at
7
8 https://imagemagick.org/license/
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 MagickCore private kernels for accelerated functions.
17*/
18
19#ifndef MAGICKCORE_ACCELERATE_KERNELS_PRIVATE_H
20#define MAGICKCORE_ACCELERATE_KERNELS_PRIVATE_H
21
22#if defined(__cplusplus) || defined(c_plusplus)
23extern "C" {
24#endif
25
26#if defined(MAGICKCORE_OPENCL_SUPPORT)
27
28/*
29 Define declarations.
30*/
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"
37
38const char *accelerateKernels =
39
40/*
41 Define declarations.
42*/
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))
54
55/*
56 Typedef declarations.
57*/
58 STRINGIFY(
59 typedef enum
60 {
61 UndefinedColorspace,
62 CMYColorspace, /* negated linear RGB colorspace */
63 CMYKColorspace, /* CMY with Black separation */
64 GRAYColorspace, /* Single Channel greyscale (non-linear) image */
65 HCLColorspace,
66 HCLpColorspace,
67 HSBColorspace,
68 HSIColorspace,
69 HSLColorspace,
70 HSVColorspace, /* alias for HSB */
71 HWBColorspace,
72 LabColorspace,
73 LCHColorspace, /* alias for LCHuv */
74 LCHabColorspace, /* Cylindrical (Polar) Lab */
75 LCHuvColorspace, /* Cylindrical (Polar) Luv */
76 LogColorspace,
77 LMSColorspace,
78 LuvColorspace,
79 OHTAColorspace,
80 Rec601YCbCrColorspace,
81 Rec709YCbCrColorspace,
82 RGBColorspace, /* Linear RGB colorspace */
83 scRGBColorspace, /* ??? */
84 sRGBColorspace, /* Default: non-linear sRGB colorspace */
85 TransparentColorspace,
86 xyYColorspace,
87 XYZColorspace, /* IEEE Color Reference colorspace */
88 YCbCrColorspace,
89 YCCColorspace,
90 YDbDrColorspace,
91 YIQColorspace,
92 YPbPrColorspace,
93 YUVColorspace,
94 LinearGRAYColorspace /* Single Channel greyscale (linear) image */
95 } ColorspaceType;
96 )
97
98 STRINGIFY(
99 typedef enum
100 {
101 UndefinedCompositeOp,
102 AlphaCompositeOp,
103 AtopCompositeOp,
104 BlendCompositeOp,
105 BlurCompositeOp,
106 BumpmapCompositeOp,
107 ChangeMaskCompositeOp,
108 ClearCompositeOp,
109 ColorBurnCompositeOp,
110 ColorDodgeCompositeOp,
111 ColorizeCompositeOp,
112 CopyBlackCompositeOp,
113 CopyBlueCompositeOp,
114 CopyCompositeOp,
115 CopyCyanCompositeOp,
116 CopyGreenCompositeOp,
117 CopyMagentaCompositeOp,
118 CopyAlphaCompositeOp,
119 CopyRedCompositeOp,
120 CopyYellowCompositeOp,
121 DarkenCompositeOp,
122 DarkenIntensityCompositeOp,
123 DifferenceCompositeOp,
124 DisplaceCompositeOp,
125 DissolveCompositeOp,
126 DistortCompositeOp,
127 DivideDstCompositeOp,
128 DivideSrcCompositeOp,
129 DstAtopCompositeOp,
130 DstCompositeOp,
131 DstInCompositeOp,
132 DstOutCompositeOp,
133 DstOverCompositeOp,
134 ExclusionCompositeOp,
135 HardLightCompositeOp,
136 HardMixCompositeOp,
137 HueCompositeOp,
138 InCompositeOp,
139 IntensityCompositeOp,
140 LightenCompositeOp,
141 LightenIntensityCompositeOp,
142 LinearBurnCompositeOp,
143 LinearDodgeCompositeOp,
144 LinearLightCompositeOp,
145 LuminizeCompositeOp,
146 MathematicsCompositeOp,
147 MinusDstCompositeOp,
148 MinusSrcCompositeOp,
149 ModulateCompositeOp,
150 ModulusAddCompositeOp,
151 ModulusSubtractCompositeOp,
152 MultiplyCompositeOp,
153 NoCompositeOp,
154 OutCompositeOp,
155 OverCompositeOp,
156 OverlayCompositeOp,
157 PegtopLightCompositeOp,
158 PinLightCompositeOp,
159 PlusCompositeOp,
160 ReplaceCompositeOp,
161 SaturateCompositeOp,
162 ScreenCompositeOp,
163 SoftLightCompositeOp,
164 SrcAtopCompositeOp,
165 SrcCompositeOp,
166 SrcInCompositeOp,
167 SrcOutCompositeOp,
168 SrcOverCompositeOp,
169 ThresholdCompositeOp,
170 VividLightCompositeOp,
171 XorCompositeOp,
172 StereoCompositeOp
173 } CompositeOperator;
174 )
175
176 STRINGIFY(
177 typedef enum
178 {
179 UndefinedFunction,
180 ArcsinFunction,
181 ArctanFunction,
182 PolynomialFunction,
183 SinusoidFunction
184 } MagickFunction;
185 )
186
187 STRINGIFY(
188 typedef enum
189 {
190 UndefinedNoise,
191 UniformNoise,
192 GaussianNoise,
193 MultiplicativeGaussianNoise,
194 ImpulseNoise,
195 LaplacianNoise,
196 PoissonNoise,
197 RandomNoise
198 } NoiseType;
199 )
200
201 STRINGIFY(
202 typedef enum
203 {
204 UndefinedPixelIntensityMethod = 0,
205 AveragePixelIntensityMethod,
206 BrightnessPixelIntensityMethod,
207 LightnessPixelIntensityMethod,
208 MSPixelIntensityMethod,
209 Rec601LumaPixelIntensityMethod,
210 Rec601LuminancePixelIntensityMethod,
211 Rec709LumaPixelIntensityMethod,
212 Rec709LuminancePixelIntensityMethod,
213 RMSPixelIntensityMethod
214 } PixelIntensityMethod;
215 )
216
217 STRINGIFY(
218 typedef enum
219 {
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;
237 )
238
239 STRINGIFY(
240 typedef enum
241 {
242 UndefinedChannel = 0x0000,
243 RedChannel = 0x0001,
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, /* Color Index Table? */
254 ReadMaskChannel = 0x0040, /* Pixel is Not Readable? */
255 WriteMaskChannel = 0x0080, /* Pixel is Write Protected? */
256 MetaChannel = 0x0100, /* ???? */
257 CompositeChannels = 0x001F,
258 AllChannels = 0x7ffffff, /* 0x7FFFFFFFFFFFFFFF for 64-bit channel masks */
259 /*
260 Special purpose channel types.
261 FUTURE: are these needed any more - they are more like hacks
262 SyncChannels for example is NOT a real channel but a 'flag'
263 It really says -- "User has not defined channels"
264 Though it does have extra meaning in the "-auto-level" operator
265 */
266 TrueAlphaChannel = 0x0100, /* extract actual alpha channel from opacity */
267 RGBChannels = 0x0200, /* set alpha from grayscale mask in RGB */
268 GrayChannels = 0x0400,
269 SyncChannels = 0x20000, /* channels modified as a single unit */
270 DefaultChannels = AllChannels
271 } ChannelType; /* must correspond to PixelChannel */
272 )
273
274/*
275 Helper functions.
276*/
277
278OPENCL_IF((MAGICKCORE_QUANTUM_DEPTH == 8))
279
280 STRINGIFY(
281 static inline CLQuantum ScaleCharToQuantum(const unsigned char value)
282 {
283 return((CLQuantum) value);
284 }
285 )
286
287OPENCL_ELIF((MAGICKCORE_QUANTUM_DEPTH == 16))
288
289 STRINGIFY(
290 static inline CLQuantum ScaleCharToQuantum(const unsigned char value)
291 {
292 return((CLQuantum) (257.0f*value));
293 }
294 )
295
296OPENCL_ELIF((MAGICKCORE_QUANTUM_DEPTH == 32))
297
298 STRINGIFY(
299 static inline CLQuantum ScaleCharToQuantum(const unsigned char value)
300 {
301 return((CLQuantum) (16843009.0*value));
302 }
303 )
304
305OPENCL_ENDIF()
306
307OPENCL_IF((MAGICKCORE_HDRI_SUPPORT == 1))
308
309 STRINGIFY(
310 static inline CLQuantum ClampToQuantum(const float value)
311 {
312 return (CLQuantum) clamp(value, 0.0f, QuantumRange);
313 }
314 )
315
316OPENCL_ELSE()
317
318 STRINGIFY(
319 static inline CLQuantum ClampToQuantum(const float value)
320 {
321 return (CLQuantum) (clamp(value, 0.0f, QuantumRange) + 0.5f);
322 }
323 )
324
325OPENCL_ENDIF()
326
327 STRINGIFY(
328 static inline int ClampToCanvas(const int offset,const int range)
329 {
330 return clamp(offset, (int)0, range-1);
331 }
332 )
333
334 STRINGIFY(
335 static inline uint ScaleQuantumToMap(CLQuantum value)
336 {
337 if (value >= (CLQuantum) MaxMap)
338 return ((uint)MaxMap);
339 else
340 return ((uint)value);
341 }
342 )
343
344 STRINGIFY(
345 static inline float MagickSafeReciprocal(const float x)
346 {
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));
349 }
350 )
351
352 STRINGIFY(
353
354 static inline unsigned int getPixelIndex(const unsigned int number_channels,
355 const unsigned int columns, const unsigned int x, const unsigned int y)
356 {
357 return (x * number_channels) + (y * columns * number_channels);
358 }
359
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); }
364
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; }
369
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; }
374
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; }
379
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; }
384
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; }
389
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)
392 {
393 if ((channel & RedChannel) != 0)
394 *red=getPixelRed(p);
395
396 if (number_channels > 2)
397 {
398 if ((channel & GreenChannel) != 0)
399 *green=getPixelGreen(p);
400
401 if ((channel & BlueChannel) != 0)
402 *blue=getPixelBlue(p);
403 }
404
405 if (((number_channels == 4) || (number_channels == 2)) &&
406 ((channel & AlphaChannel) != 0))
407 *alpha=getPixelAlpha(p,number_channels);
408 }
409
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)
412 {
413 const __global CLQuantum *p = image + getPixelIndex(number_channels, columns, x, y);
414
415 float4 pixel;
416
417 pixel.x=getPixelRed(p);
418
419 if (number_channels > 2)
420 {
421 pixel.y=getPixelGreen(p);
422 pixel.z=getPixelBlue(p);
423 }
424
425 if ((number_channels == 4) || (number_channels == 2))
426 pixel.w=getPixelAlpha(p,number_channels);
427 return(pixel);
428 }
429
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)
432 {
433 const __global CLQuantum *p = image + getPixelIndex(number_channels, columns, x, y);
434
435 float red = 0.0f;
436 float green = 0.0f;
437 float blue = 0.0f;
438 float alpha = 0.0f;
439
440 ReadChannels(p, number_channels, channel, &red, &green, &blue, &alpha);
441 return (float4)(red, green, blue, alpha);
442 }
443
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)
446 {
447 if ((channel & RedChannel) != 0)
448 setPixelRed(p,ClampToQuantum(red));
449
450 if (number_channels > 2)
451 {
452 if ((channel & GreenChannel) != 0)
453 setPixelGreen(p,ClampToQuantum(green));
454
455 if ((channel & BlueChannel) != 0)
456 setPixelBlue(p,ClampToQuantum(blue));
457 }
458
459 if (((number_channels == 4) || (number_channels == 2)) &&
460 ((channel & AlphaChannel) != 0))
461 setPixelAlpha(p,number_channels,ClampToQuantum(alpha));
462 }
463
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)
466 {
467 __global CLQuantum *p = image + getPixelIndex(number_channels, columns, x, y);
468
469 setPixelRed(p,ClampToQuantum(pixel.x));
470
471 if (number_channels > 2)
472 {
473 setPixelGreen(p,ClampToQuantum(pixel.y));
474 setPixelBlue(p,ClampToQuantum(pixel.z));
475 }
476
477 if ((number_channels == 4) || (number_channels == 2))
478 setPixelAlpha(p,number_channels,ClampToQuantum(pixel.w));
479 }
480
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,
483 float4 pixel)
484 {
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);
487 }
488
489 static inline float GetPixelIntensity(const unsigned int colorspace,
490 const unsigned int method,float red,float green,float blue)
491 {
492 float intensity;
493
494 if ((colorspace == GRAYColorspace) || (colorspace == LinearGRAYColorspace))
495 return red;
496
497 switch (method)
498 {
499 case AveragePixelIntensityMethod:
500 {
501 intensity=(red+green+blue)/3.0;
502 break;
503 }
504 case BrightnessPixelIntensityMethod:
505 {
506 intensity=MagickMax(MagickMax(red,green),blue);
507 break;
508 }
509 case LightnessPixelIntensityMethod:
510 {
511 intensity=(MagickMin(MagickMin(red,green),blue)+
512 MagickMax(MagickMax(red,green),blue))/2.0;
513 break;
514 }
515 case MSPixelIntensityMethod:
516 {
517 intensity=(float) (((float) red*red+green*green+blue*blue)/
518 (3.0*QuantumRange));
519 break;
520 }
521 case Rec601LumaPixelIntensityMethod:
522 {
523 /*
524 if (image->colorspace == RGBColorspace)
525 {
526 red=EncodePixelGamma(red);
527 green=EncodePixelGamma(green);
528 blue=EncodePixelGamma(blue);
529 }
530 */
531 if ((red == green) && (green == blue))
532 intensity=red;
533 else
534 intensity=0.298839*red+0.586811*green+0.114350*blue;
535 break;
536 }
537 case Rec601LuminancePixelIntensityMethod:
538 {
539 /*
540 if (image->colorspace == sRGBColorspace)
541 {
542 red=DecodePixelGamma(red);
543 green=DecodePixelGamma(green);
544 blue=DecodePixelGamma(blue);
545 }
546 */
547 if ((red == green) && (green == blue))
548 intensity=red;
549 else
550 intensity=0.298839*red+0.586811*green+0.114350*blue;
551 break;
552 }
553 case Rec709LumaPixelIntensityMethod:
554 default:
555 {
556 /*
557 if (image->colorspace == RGBColorspace)
558 {
559 red=EncodePixelGamma(red);
560 green=EncodePixelGamma(green);
561 blue=EncodePixelGamma(blue);
562 }
563 */
564 if ((red == green) && (green == blue))
565 intensity=red;
566 else
567 intensity=0.212656*red+0.715158*green+0.072186*blue;
568 break;
569 }
570 case Rec709LuminancePixelIntensityMethod:
571 {
572 /*
573 if (image->colorspace == sRGBColorspace)
574 {
575 red=DecodePixelGamma(red);
576 green=DecodePixelGamma(green);
577 blue=DecodePixelGamma(blue);
578 }
579 */
580 if ((red == green) && (green == blue))
581 intensity=red;
582 else
583 intensity=0.212656*red+0.715158*green+0.072186*blue;
584 break;
585 }
586 case RMSPixelIntensityMethod:
587 {
588 intensity=(float) (sqrt((float) red*red+green*green+blue*blue)/
589 sqrt(3.0));
590 break;
591 }
592 }
593
594 return intensity;
595 }
596
597 static inline int mirrorBottom(int value)
598 {
599 return (value < 0) ? - (value) : value;
600 }
601
602 static inline int mirrorTop(int value, int width)
603 {
604 return (value >= width) ? (2 * width - value - 1) : value;
605 }
606 )
607
608/*
609%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
610% %
611% %
612% %
613% A d d N o i s e %
614% %
615% %
616% %
617%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
618*/
619
620 STRINGIFY(
621 /*
622 Part of MWC64X by David Thomas, dt10@imperial.ac.uk
623 This is provided under BSD, full license is with the main package.
624 See http://www.doc.ic.ac.uk/~dt10/research
625 */
626
627 // Pre: a<M, b<M
628 // Post: r=(a+b) mod M
629 ulong MWC_AddMod64(ulong a, ulong b, ulong M)
630 {
631 ulong v=a+b;
632 //if( (v>=M) || (v<a) )
633 if( (v>=M) || (convert_float(v) < convert_float(a)) ) // workaround for what appears to be an optimizer bug.
634 v=v-M;
635 return v;
636 }
637
638 // Pre: a<M,b<M
639 // Post: r=(a*b) mod M
640 // This could be done more efficiently, but it is portable, and should
641 // be easy to understand. It can be replaced with any of the better
642 // modular multiplication algorithms (for example if you know you have
643 // double precision available or something).
644 ulong MWC_MulMod64(ulong a, ulong b, ulong M)
645 {
646 ulong r=0;
647 while(a!=0){
648 if(a&1)
649 r=MWC_AddMod64(r,b,M);
650 b=MWC_AddMod64(b,b,M);
651 a=a>>1;
652 }
653 return r;
654 }
655
656 // Pre: a<M, e>=0
657 // Post: r=(a^b) mod M
658 // This takes at most ~64^2 modular additions, so probably about 2^15 or so instructions on
659 // most architectures
660 ulong MWC_PowMod64(ulong a, ulong e, ulong M)
661 {
662 ulong sqr=a, acc=1;
663 while(e!=0){
664 if(e&1)
665 acc=MWC_MulMod64(acc,sqr,M);
666 sqr=MWC_MulMod64(sqr,sqr,M);
667 e=e>>1;
668 }
669 return acc;
670 }
671
672 uint2 MWC_SkipImpl_Mod64(uint2 curr, ulong A, ulong M, ulong distance)
673 {
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));
678 }
679
680 uint2 MWC_SeedImpl_Mod64(ulong A, ulong M, uint vecSize, uint vecOffset, ulong streamBase, ulong streamGap)
681 {
682 // This is an arbitrary constant for starting LCG jumping from. I didn't
683 // want to start from 1, as then you end up with the two or three first values
684 // being a bit poor in ones - once you've decided that, one constant is as
685 // good as any another. There is no deep mathematical reason for it, I just
686 // generated a random number.
687 enum{ MWC_BASEID = 4077358422479273989UL };
688
689 ulong dist=streamBase + (get_global_id(0)*vecSize+vecOffset)*streamGap;
690 ulong m=MWC_PowMod64(A, dist, M);
691
692 ulong x=MWC_MulMod64(MWC_BASEID, m, M);
693 return (uint2)((uint)(x/A), (uint)(x%A));
694 }
695
697 typedef struct{ uint x; uint c; uint seed0; ulong seed1; } mwc64x_state_t;
698
699 void MWC64X_Step(mwc64x_state_t *s)
700 {
701 uint X=s->x, C=s->c;
702
703 uint Xn=s->seed0*X+C;
704 uint carry=(uint)(Xn<C); // The (Xn<C) will be zero or one for scalar
705 uint Cn=mad_hi(s->seed0,X,carry);
706
707 s->x=Xn;
708 s->c=Cn;
709 }
710
711 void MWC64X_Skip(mwc64x_state_t *s, ulong distance)
712 {
713 uint2 tmp=MWC_SkipImpl_Mod64((uint2)(s->x,s->c), s->seed0, s->seed1, distance);
714 s->x=tmp.x;
715 s->c=tmp.y;
716 }
717
718 void MWC64X_SeedStreams(mwc64x_state_t *s, ulong baseOffset, ulong perStreamOffset)
719 {
720 uint2 tmp=MWC_SeedImpl_Mod64(s->seed0, s->seed1, 1, 0, baseOffset, perStreamOffset);
721 s->x=tmp.x;
722 s->c=tmp.y;
723 }
724
726 uint MWC64X_NextUint(mwc64x_state_t *s)
727 {
728 uint res=s->x ^ s->c;
729 MWC64X_Step(s);
730 return res;
731 }
732
733 //
734 // End of MWC64X excerpt
735 //
736
737 float mwcReadPseudoRandomValue(mwc64x_state_t* rng)
738 {
739 return (1.0f * MWC64X_NextUint(rng)) / (float)(0xffffffff); // normalized to 1.0
740 }
741
742 float mwcGenerateDifferentialNoise(mwc64x_state_t* r, float pixel, NoiseType noise_type, float attenuate)
743 {
744 float
745 alpha,
746 beta,
747 noise,
748 sigma;
749
750 noise = 0.0f;
751 alpha=mwcReadPseudoRandomValue(r);
752 switch(noise_type)
753 {
754 case UniformNoise:
755 default:
756 {
757 noise=(pixel+QuantumRange*SigmaUniform*(alpha-0.5f));
758 break;
759 }
760 case GaussianNoise:
761 {
762 float
763 gamma,
764 tau;
765
766 if (alpha == 0.0f)
767 alpha=1.0f;
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;
773 break;
774 }
775 case ImpulseNoise:
776 {
777 if (alpha < (SigmaImpulse/2.0f))
778 noise=0.0f;
779 else
780 if (alpha >= (1.0f-(SigmaImpulse/2.0f)))
781 noise=QuantumRange;
782 else
783 noise=pixel;
784 break;
785 }
786 case LaplacianNoise:
787 {
788 if (alpha <= 0.5f)
789 {
790 if (alpha <= MagickEpsilon)
791 noise=(pixel-QuantumRange);
792 else
793 noise=(pixel+QuantumRange*SigmaLaplacian*log(2.0f*alpha)+
794 0.5f);
795 break;
796 }
797 beta=1.0f-alpha;
798 if (beta <= (0.5f*MagickEpsilon))
799 noise=(pixel+QuantumRange);
800 else
801 noise=(pixel-QuantumRange*SigmaLaplacian*log(2.0f*beta)+0.5f);
802 break;
803 }
804 case MultiplicativeGaussianNoise:
805 {
806 sigma=1.0f;
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);
812 break;
813 }
814 case PoissonNoise:
815 {
816 float
817 poisson;
818 unsigned int i;
819 poisson=exp(-SigmaPoisson*QuantumScale*pixel);
820 for (i=0; alpha > poisson; i++)
821 {
822 beta=mwcReadPseudoRandomValue(r);
823 alpha*=beta;
824 }
825 noise=(QuantumRange*i*MagickSafeReciprocal(SigmaPoisson));
826 break;
827 }
828 case RandomNoise:
829 {
830 noise=(QuantumRange*SigmaRandom*alpha);
831 break;
832 }
833 }
834 return noise;
835 }
836 )
837
838/*
839%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
840% %
841% %
842% %
843% B l u r %
844% %
845% %
846% %
847%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
848*/
849
850 STRINGIFY(
851 /*
852 Reduce image noise and reduce detail levels by row
853 */
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)
859 {
860 const int x = get_global_id(0);
861 const int y = get_global_id(1);
862
863 const int columns = imageColumns;
864
865 const unsigned int radius = (width-1)/2;
866 const int wsize = get_local_size(0);
867 const unsigned int loadSize = wsize+width;
868
869 //group coordinate
870 const int groupX=get_local_size(0)*get_group_id(0);
871
872 //parallel load and clamp
873 for (int i=get_local_id(0); i < loadSize; i=i+get_local_size(0))
874 {
875 int cx = ClampToCanvas(i + groupX - radius, columns);
876 temp[i] = ReadFloat4(image, number_channels, columns, cx, y, channel);
877 }
878
879 // barrier
880 barrier(CLK_LOCAL_MEM_FENCE);
881
882 // only do the work if this is not a patched item
883 if (get_global_id(0) < columns)
884 {
885 // compute
886 float4 result = (float4) 0;
887
888 int i = 0;
889
890 for ( ; i+7 < width; )
891 {
892 for (int j=0; j < 8; j++)
893 result+=filter[i+j]*temp[i+j+get_local_id(0)];
894 i+=8;
895 }
896
897 for ( ; i < width; i++)
898 result+=filter[i]*temp[i+get_local_id(0)];
899
900 // write back to global
901 tempImage[y*columns+x] = result;
902 }
903 }
904 )
905
906 STRINGIFY(
907 /*
908 Reduce image noise and reduce detail levels by line
909 */
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)
915 {
916 const int x = get_global_id(0);
917 const int y = get_global_id(1);
918
919 const int columns = imageColumns;
920 const int rows = imageRows;
921
922 unsigned int radius = (width-1)/2;
923 const int wsize = get_local_size(1);
924 const unsigned int loadSize = wsize+width;
925
926 //group coordinate
927 const int groupX=get_local_size(0)*get_group_id(0);
928 const int groupY=get_local_size(1)*get_group_id(1);
929 //notice that get_local_size(0) is 1, so
930 //groupX=get_group_id(0);
931
932 //parallel load and clamp
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];
935
936 // barrier
937 barrier(CLK_LOCAL_MEM_FENCE);
938
939 // only do the work if this is not a patched item
940 if (get_global_id(1) < rows)
941 {
942 // compute
943 float4 result = (float4) 0;
944
945 int i = 0;
946
947 for ( ; i+7 < width; )
948 {
949 for (int j=0; j < 8; j++)
950 result+=filter[i+j]*temp[i+j+get_local_id(1)];
951 i+=8;
952 }
953
954 for ( ; i < width; i++)
955 result+=filter[i]*temp[i+get_local_id(1)];
956
957 // write back to global
958 WriteFloat4(filteredImage, number_channels, columns, x, y, channel, result);
959 }
960 }
961 )
962
963/*
964%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
965% %
966% %
967% %
968% C o n t r a s t %
969% %
970% %
971% %
972%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
973*/
974
975 STRINGIFY(
976
977 static inline float4 ConvertRGBToHSB(const float4 pixel)
978 {
979 float4 result=0.0f;
980 result.w=pixel.w;
981 float tmax=MagickMax(MagickMax(pixel.x,pixel.y),pixel.z);
982 if (tmax != 0.0f)
983 {
984 float tmin=MagickMin(MagickMin(pixel.x,pixel.y),pixel.z);
985 float delta=tmax-tmin;
986
987 result.y=delta/tmax;
988 result.z=QuantumScale*tmax;
989 if (delta != 0.0f)
990 {
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;
994 result.x/=6.0f;
995 result.x+=(result.x < 0.0f) ? 0.0f : 1.0f;
996 }
997 }
998 return(result);
999 }
1000
1001 static inline float4 ConvertHSBToRGB(const float4 pixel)
1002 {
1003 float hue=pixel.x;
1004 float saturation=pixel.y;
1005 float brightness=pixel.z;
1006
1007 float4 result=pixel;
1008
1009 if (saturation == 0.0f)
1010 {
1011 result.x=result.y=result.z=ClampToQuantum(QuantumRange*brightness);
1012 }
1013 else
1014 {
1015 float h=6.0f*(hue-floor(hue));
1016 float f=h-floor(h);
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)));
1020 int ih = (int)h;
1021
1022 if (ih == 1)
1023 {
1024 result.x=ClampToQuantum(QuantumRange*q);
1025 result.y=ClampToQuantum(QuantumRange*brightness);
1026 result.z=ClampToQuantum(QuantumRange*p);
1027 }
1028 else if (ih == 2)
1029 {
1030 result.x=ClampToQuantum(QuantumRange*p);
1031 result.y=ClampToQuantum(QuantumRange*brightness);
1032 result.z=ClampToQuantum(QuantumRange*t);
1033 }
1034 else if (ih == 3)
1035 {
1036 result.x=ClampToQuantum(QuantumRange*p);
1037 result.y=ClampToQuantum(QuantumRange*q);
1038 result.z=ClampToQuantum(QuantumRange*brightness);
1039 }
1040 else if (ih == 4)
1041 {
1042 result.x=ClampToQuantum(QuantumRange*t);
1043 result.y=ClampToQuantum(QuantumRange*p);
1044 result.z=ClampToQuantum(QuantumRange*brightness);
1045 }
1046 else if (ih == 5)
1047 {
1048 result.x=ClampToQuantum(QuantumRange*brightness);
1049 result.y=ClampToQuantum(QuantumRange*p);
1050 result.z=ClampToQuantum(QuantumRange*q);
1051 }
1052 else
1053 {
1054 result.x=ClampToQuantum(QuantumRange*brightness);
1055 result.y=ClampToQuantum(QuantumRange*t);
1056 result.z=ClampToQuantum(QuantumRange*p);
1057 }
1058 }
1059 return(result);
1060 }
1061
1062 __kernel void Contrast(__global CLQuantum *image,
1063 const unsigned int number_channels,const int sign)
1064 {
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);
1068
1069 float4 pixel=ReadAllChannels(image,number_channels,columns,x,y);
1070 if (number_channels < 3)
1071 pixel.y=pixel.z=pixel.x;
1072
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);
1077 pixel.z=brightness;
1078 pixel=ConvertHSBToRGB(pixel);
1079
1080 WriteAllChannels(image,number_channels,columns,x,y,pixel);
1081 }
1082 )
1083
1084/*
1085%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1086% %
1087% %
1088% %
1089% C o n t r a s t S t r e t c h %
1090% %
1091% %
1092% %
1093%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1094*/
1095
1096 STRINGIFY(
1097 /*
1098 */
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)
1104 {
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)
1110 {
1111 float red=(float)getRed(im[c]);
1112 float green=(float)getGreen(im[c]);
1113 float blue=(float)getBlue(im[c]);
1114
1115 float intensity = GetPixelIntensity(colorspace, method, red, green, blue);
1116 uint pos = ScaleQuantumToMap(ClampToQuantum(intensity));
1117 atomic_inc((__global uint *)(&(histogram[pos]))+2); //red position
1118 }
1119 else
1120 {
1121 // for equalizing, we always need all channels?
1122 // otherwise something more
1123 }
1124 }
1125 )
1126
1127 STRINGIFY(
1128 /*
1129 */
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)
1134 {
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;
1139
1140 uint ePos;
1141 CLPixelType oValue, eValue;
1142 CLQuantum red, green, blue, alpha;
1143
1144 //read from global
1145 oValue=im[c];
1146
1147 if ((channel & RedChannel) != 0)
1148 {
1149 if (getRedF4(white) != getRedF4(black))
1150 {
1151 ePos = ScaleQuantumToMap(getRed(oValue));
1152 eValue = stretch_map[ePos];
1153 red = getRed(eValue);
1154 }
1155 }
1156
1157 if ((channel & GreenChannel) != 0)
1158 {
1159 if (getGreenF4(white) != getGreenF4(black))
1160 {
1161 ePos = ScaleQuantumToMap(getGreen(oValue));
1162 eValue = stretch_map[ePos];
1163 green = getGreen(eValue);
1164 }
1165 }
1166
1167 if ((channel & BlueChannel) != 0)
1168 {
1169 if (getBlueF4(white) != getBlueF4(black))
1170 {
1171 ePos = ScaleQuantumToMap(getBlue(oValue));
1172 eValue = stretch_map[ePos];
1173 blue = getBlue(eValue);
1174 }
1175 }
1176
1177 if ((channel & AlphaChannel) != 0)
1178 {
1179 if (getAlphaF4(white) != getAlphaF4(black))
1180 {
1181 ePos = ScaleQuantumToMap(getAlpha(oValue));
1182 eValue = stretch_map[ePos];
1183 alpha = getAlpha(eValue);
1184 }
1185 }
1186
1187 //write back
1188 im[c]=(CLPixelType)(blue, green, red, alpha);
1189
1190 }
1191 )
1192/*
1193%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1194% %
1195% %
1196% %
1197% D e s p e c k l e %
1198% %
1199% %
1200% %
1201%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1202*/
1203
1204 STRINGIFY(
1205
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) {
1209
1210 int x = get_global_id(0);
1211 int y = get_global_id(1);
1212
1213 CLPixelType v = inputImage[y*imageWidth+x];
1214
1215 int2 neighbor;
1216 neighbor.y = y + offset.y;
1217 neighbor.x = x + offset.x;
1218
1219 int2 clampedNeighbor;
1220 clampedNeighbor.x = ClampToCanvas(neighbor.x, imageWidth);
1221 clampedNeighbor.y = ClampToCanvas(neighbor.y, imageHeight);
1222
1223 CLPixelType r = (clampedNeighbor.x == neighbor.x
1224 && clampedNeighbor.y == neighbor.y)?inputImage[clampedNeighbor.y*imageWidth+clampedNeighbor.x]
1225 :(CLPixelType)0;
1226
1227 int sv[4];
1228 sv[0] = (int)v.x;
1229 sv[1] = (int)v.y;
1230 sv[2] = (int)v.z;
1231 sv[3] = (int)v.w;
1232
1233 int sr[4];
1234 sr[0] = (int)r.x;
1235 sr[1] = (int)r.y;
1236 sr[2] = (int)r.z;
1237 sr[3] = (int)r.w;
1238
1239 if (polarity > 0) {
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];
1243 }
1244 }
1245 else {
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];
1249 }
1250
1251 }
1252
1253 v.x = (CLQuantum)sv[0];
1254 v.y = (CLQuantum)sv[1];
1255 v.z = (CLQuantum)sv[2];
1256
1257 if (matte!=0)
1258 v.w = (CLQuantum)sv[3];
1259
1260 outputImage[y*imageWidth+x] = v;
1261
1262 }
1263
1264
1265 )
1266
1267 STRINGIFY(
1268
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) {
1272
1273 int x = get_global_id(0);
1274 int y = get_global_id(1);
1275
1276 CLPixelType v = inputImage[y*imageWidth+x];
1277
1278 int2 neighbor, clampedNeighbor;
1279
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);
1284
1285 CLPixelType r = (clampedNeighbor.x == neighbor.x
1286 && clampedNeighbor.y == neighbor.y)?inputImage[clampedNeighbor.y*imageWidth+clampedNeighbor.x]
1287 :(CLPixelType)0;
1288
1289
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);
1294
1295 CLPixelType s = (clampedNeighbor.x == neighbor.x
1296 && clampedNeighbor.y == neighbor.y)?inputImage[clampedNeighbor.y*imageWidth+clampedNeighbor.x]
1297 :(CLPixelType)0;
1298
1299
1300 int sv[4];
1301 sv[0] = (int)v.x;
1302 sv[1] = (int)v.y;
1303 sv[2] = (int)v.z;
1304 sv[3] = (int)v.w;
1305
1306 int sr[4];
1307 sr[0] = (int)r.x;
1308 sr[1] = (int)r.y;
1309 sr[2] = (int)r.z;
1310 sr[3] = (int)r.w;
1311
1312 int ss[4];
1313 ss[0] = (int)s.x;
1314 ss[1] = (int)s.y;
1315 ss[2] = (int)s.z;
1316 ss[3] = (int)s.w;
1317
1318 if (polarity > 0) {
1319 \n #pragma unroll 4\n
1320 for (unsigned int i = 0; i < 4; i++) {
1321 //sv[i] = (ss[i] >= (sv[i]+ScaleCharToQuantum(2)) && sr[i] > sv[i] ) ? (sv[i]+ScaleCharToQuantum(1)):sv[i];
1322 //
1323 //sv[i] =(!( (int)(ss[i] >= (sv[i]+ScaleCharToQuantum(2))) && (int) (sr[i] > sv[i] ) )) ? sv[i]:(sv[i]+ScaleCharToQuantum(1));
1324 //sv[i] =(( (int)( ss[i] < (sv[i]+ScaleCharToQuantum(2))) || (int) ( sr[i] <= sv[i] ) )) ? sv[i]:(sv[i]+ScaleCharToQuantum(1));
1325 sv[i] =(( (int)( ss[i] < (sv[i]+ScaleCharToQuantum(2))) + (int) ( sr[i] <= sv[i] ) ) !=0) ? sv[i]:(sv[i]+ScaleCharToQuantum(1));
1326 }
1327 }
1328 else {
1329 \n #pragma unroll 4\n
1330 for (unsigned int i = 0; i < 4; i++) {
1331 //sv[i] = (ss[i] <= (sv[i]-ScaleCharToQuantum(2)) && sr[i] < sv[i] ) ? (sv[i]-ScaleCharToQuantum(1)):sv[i];
1332 //
1333 //sv[i] = ( (int)(ss[i] <= (sv[i]-ScaleCharToQuantum(2)) ) + (int)( sr[i] < sv[i] ) ==0) ? sv[i]:(sv[i]-ScaleCharToQuantum(1));
1334 sv[i] = (( (int)(ss[i] > (sv[i]-ScaleCharToQuantum(2))) + (int)( sr[i] >= sv[i] )) !=0) ? sv[i]:(sv[i]-ScaleCharToQuantum(1));
1335 }
1336 }
1337
1338 v.x = (CLQuantum)sv[0];
1339 v.y = (CLQuantum)sv[1];
1340 v.z = (CLQuantum)sv[2];
1341
1342 if (matte!=0)
1343 v.w = (CLQuantum)sv[3];
1344
1345 outputImage[y*imageWidth+x] = v;
1346
1347 }
1348 )
1349
1350/*
1351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1352% %
1353% %
1354% %
1355% E q u a l i z e %
1356% %
1357% %
1358% %
1359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1360*/
1361
1362 STRINGIFY(
1363 /*
1364 */
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)
1369 {
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;
1374
1375 uint ePos;
1376 CLPixelType oValue, eValue;
1377 CLQuantum red, green, blue, alpha;
1378
1379 //read from global
1380 oValue=im[c];
1381
1382 if ((channel & SyncChannels) != 0)
1383 {
1384 if (getRedF4(white) != getRedF4(black))
1385 {
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);
1398
1399 //write back
1400 im[c]=(CLPixelType)(blue, green, red, alpha);
1401 }
1402
1403 }
1404
1405 // for equalizing, we always need all channels?
1406 // otherwise something more
1407
1408 }
1409 )
1410
1411/*
1412%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1413% %
1414% %
1415% %
1416% F u n c t i o n %
1417% %
1418% %
1419% %
1420%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1421*/
1422
1423 STRINGIFY(
1424 /*
1425 apply FunctionImageChannel(brightness-contrast)
1426 */
1427 CLQuantum ApplyFunction(float pixel,const MagickFunction function,
1428 const unsigned int number_parameters,__constant float *parameters)
1429 {
1430 float result = 0.0f;
1431
1432 switch (function)
1433 {
1434 case PolynomialFunction:
1435 {
1436 for (unsigned int i=0; i < number_parameters; i++)
1437 result = result*QuantumScale*pixel + parameters[i];
1438 result *= QuantumRange;
1439 break;
1440 }
1441 case SinusoidFunction:
1442 {
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);
1450 break;
1451 }
1452 case ArcsinFunction:
1453 {
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;
1459
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;
1465 break;
1466 }
1467 case ArctanFunction:
1468 {
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);
1476 break;
1477 }
1478 case UndefinedFunction:
1479 break;
1480 }
1481 return(ClampToQuantum(result));
1482 }
1483 )
1484
1485 STRINGIFY(
1486 /*
1487 Improve brightness / contrast of the image
1488 channel : define which channel is improved
1489 function : the function called to enhance the brightness contrast
1490 number_parameters : numbers of parameters
1491 parameters : the parameter
1492 */
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)
1496 {
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);
1501
1502 float red;
1503 float green;
1504 float blue;
1505 float alpha;
1506
1507 ReadChannels(p, number_channels, channel, &red, &green, &blue, &alpha);
1508
1509 if ((channel & RedChannel) != 0)
1510 red=ApplyFunction(red, function, number_parameters, parameters);
1511
1512 if (number_channels > 2)
1513 {
1514 if ((channel & GreenChannel) != 0)
1515 green=ApplyFunction(green, function, number_parameters, parameters);
1516
1517 if ((channel & BlueChannel) != 0)
1518 blue=ApplyFunction(blue, function, number_parameters, parameters);
1519 }
1520
1521 if (((number_channels == 4) || (number_channels == 2)) &&
1522 ((channel & AlphaChannel) != 0))
1523 alpha=ApplyFunction(alpha, function, number_parameters, parameters);
1524
1525 WriteChannels(p, number_channels, channel, red, green, blue, alpha);
1526 }
1527 )
1528
1529/*
1530%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1531% %
1532% %
1533% %
1534% G r a y s c a l e %
1535% %
1536% %
1537% %
1538%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1539*/
1540
1541 STRINGIFY(
1542 __kernel void Grayscale(__global CLQuantum *image,const int number_channels,
1543 const unsigned int colorspace,const unsigned int method)
1544 {
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);
1549
1550 float
1551 blue,
1552 green,
1553 red;
1554
1555 red=getPixelRed(p);
1556 green=getPixelGreen(p);
1557 blue=getPixelBlue(p);
1558
1559 CLQuantum intensity=ClampToQuantum(GetPixelIntensity(colorspace, method, red, green, blue));
1560
1561 setPixelRed(p,intensity);
1562 setPixelGreen(p,intensity);
1563 setPixelBlue(p,intensity);
1564 }
1565 )
1566
1567/*
1568%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1569% %
1570% %
1571% %
1572% L o c a l C o n t r a s t %
1573% %
1574% %
1575% %
1576%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1577*/
1578
1579 STRINGIFY(
1580
1581 __kernel void LocalContrastBlurRow(__global CLPixelType *srcImage, __global CLPixelType *dstImage, __global float *tmpImage,
1582 const int radius,
1583 const int imageWidth,
1584 const int imageHeight)
1585 {
1586 const float4 RGB = ((float4)(0.2126f, 0.7152f, 0.0722f, 0.0f));
1587
1588 int x = get_local_id(0);
1589 int y = get_global_id(1);
1590
1591 if ((x >= imageWidth) || (y >= imageHeight))
1592 return;
1593
1594 global CLPixelType *src = srcImage + y * imageWidth;
1595
1596 for (int i = x; i < imageWidth; i += get_local_size(0)) {
1597 float sum = 0.0f;
1598 float weight = 1.0f;
1599
1600 int j = i - radius;
1601 while ((j + 7) < i) {
1602 for (int k = 0; k < 8; ++k) // Unroll 8x
1603 sum += (weight + k) * dot(RGB, convert_float4(src[mirrorBottom(j+k)]));
1604 weight += 8.0f;
1605 j+=8;
1606 }
1607 while (j < i) {
1608 sum += weight * dot(RGB, convert_float4(src[mirrorBottom(j)]));
1609 weight += 1.0f;
1610 ++j;
1611 }
1612
1613 while ((j + 7) < radius + i) {
1614 for (int k = 0; k < 8; ++k) // Unroll 8x
1615 sum += (weight - k) * dot(RGB, convert_float4(src[mirrorTop(j + k, imageWidth)]));
1616 weight -= 8.0f;
1617 j+=8;
1618 }
1619 while (j < radius + i) {
1620 sum += weight * dot(RGB, convert_float4(src[mirrorTop(j, imageWidth)]));
1621 weight -= 1.0f;
1622 ++j;
1623 }
1624
1625 tmpImage[i + y * imageWidth] = sum / ((radius + 1) * (radius + 1));
1626 }
1627 }
1628 )
1629
1630 STRINGIFY(
1631 __kernel void LocalContrastBlurApplyColumn(__global CLPixelType *srcImage, __global CLPixelType *dstImage, __global float *blurImage,
1632 const int radius,
1633 const float strength,
1634 const int imageWidth,
1635 const int imageHeight)
1636 {
1637 const float4 RGB = (float4)(0.2126f, 0.7152f, 0.0722f, 0.0f);
1638
1639 int x = get_global_id(0);
1640 int y = get_global_id(1);
1641
1642 if ((x >= imageWidth) || (y >= imageHeight))
1643 return;
1644
1645 global float *src = blurImage + x;
1646
1647 float sum = 0.0f;
1648 float weight = 1.0f;
1649
1650 int j = y - radius;
1651 while ((j + 7) < y) {
1652 for (int k = 0; k < 8; ++k) // Unroll 8x
1653 sum += (weight + k) * src[mirrorBottom(j+k) * imageWidth];
1654 weight += 8.0f;
1655 j+=8;
1656 }
1657 while (j < y) {
1658 sum += weight * src[mirrorBottom(j) * imageWidth];
1659 weight += 1.0f;
1660 ++j;
1661 }
1662
1663 while ((j + 7) < radius + y) {
1664 for (int k = 0; k < 8; ++k) // Unroll 8x
1665 sum += (weight - k) * src[mirrorTop(j + k, imageHeight) * imageWidth];
1666 weight -= 8.0f;
1667 j+=8;
1668 }
1669 while (j < radius + y) {
1670 sum += weight * src[mirrorTop(j, imageHeight) * imageWidth];
1671 weight -= 1.0f;
1672 ++j;
1673 }
1674
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;
1679
1680 pixel.x = ClampToQuantum(pixel.x * mult);
1681 pixel.y = ClampToQuantum(pixel.y * mult);
1682 pixel.z = ClampToQuantum(pixel.z * mult);
1683
1684 dstImage[x + y * imageWidth] = pixel;
1685 }
1686 )
1687
1688/*
1689%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1690% %
1691% %
1692% %
1693% M o d u l a t e %
1694% %
1695% %
1696% %
1697%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1698*/
1699
1700 STRINGIFY(
1701
1702 static inline void ConvertRGBToHSL(const CLQuantum red,const CLQuantum green, const CLQuantum blue,
1703 float *hue, float *saturation, float *lightness)
1704 {
1705 float
1706 c,
1707 tmax,
1708 tmin;
1709
1710 /*
1711 Convert RGB to HSL colorspace.
1712 */
1713 tmax=MagickMax(QuantumScale*red,MagickMax(QuantumScale*green, QuantumScale*blue));
1714 tmin=MagickMin(QuantumScale*red,MagickMin(QuantumScale*green, QuantumScale*blue));
1715
1716 c=tmax-tmin;
1717
1718 *lightness=(tmax+tmin)/2.0;
1719 if (c <= 0.0)
1720 {
1721 *hue=0.0;
1722 *saturation=0.0;
1723 return;
1724 }
1725
1726 if (tmax == (QuantumScale*red))
1727 {
1728 *hue=(QuantumScale*green-QuantumScale*blue)/c;
1729 if ((QuantumScale*green) < (QuantumScale*blue))
1730 *hue+=6.0;
1731 }
1732 else
1733 if (tmax == (QuantumScale*green))
1734 *hue=2.0+(QuantumScale*blue-QuantumScale*red)/c;
1735 else
1736 *hue=4.0+(QuantumScale*red-QuantumScale*green)/c;
1737
1738 *hue*=60.0/360.0;
1739 if (*lightness <= 0.5)
1740 *saturation=c/(2.0*(*lightness));
1741 else
1742 *saturation=c/(2.0-2.0*(*lightness));
1743 }
1744
1745 static inline void ConvertHSLToRGB(const float hue,const float saturation, const float lightness,
1746 CLQuantum *red,CLQuantum *green,CLQuantum *blue)
1747 {
1748 float
1749 b,
1750 c,
1751 g,
1752 h,
1753 tmin,
1754 r,
1755 x;
1756
1757 /*
1758 Convert HSL to RGB colorspace.
1759 */
1760 h=hue*360.0;
1761 if (lightness <= 0.5)
1762 c=2.0*lightness*saturation;
1763 else
1764 c=(2.0-2.0*lightness)*saturation;
1765 tmin=lightness-0.5*c;
1766 h-=360.0*floor(h/360.0);
1767 h/=60.0;
1768 x=c*(1.0-fabs(h-2.0*floor(h/2.0)-1.0));
1769 switch ((int) floor(h) % 6)
1770 {
1771 case 0:
1772 default:
1773 {
1774 r=tmin+c;
1775 g=tmin+x;
1776 b=tmin;
1777 break;
1778 }
1779 case 1:
1780 {
1781 r=tmin+x;
1782 g=tmin+c;
1783 b=tmin;
1784 break;
1785 }
1786 case 2:
1787 {
1788 r=tmin;
1789 g=tmin+c;
1790 b=tmin+x;
1791 break;
1792 }
1793 case 3:
1794 {
1795 r=tmin;
1796 g=tmin+x;
1797 b=tmin+c;
1798 break;
1799 }
1800 case 4:
1801 {
1802 r=tmin+x;
1803 g=tmin;
1804 b=tmin+c;
1805 break;
1806 }
1807 case 5:
1808 {
1809 r=tmin+c;
1810 g=tmin;
1811 b=tmin+x;
1812 break;
1813 }
1814 }
1815 *red=ClampToQuantum(QuantumRange*r);
1816 *green=ClampToQuantum(QuantumRange*g);
1817 *blue=ClampToQuantum(QuantumRange*b);
1818 }
1819
1820 static inline void ModulateHSL(const float percent_hue, const float percent_saturation,const float percent_lightness,
1821 CLQuantum *red,CLQuantum *green,CLQuantum *blue)
1822 {
1823 float
1824 hue,
1825 lightness,
1826 saturation;
1827
1828 /*
1829 Increase or decrease color lightness, saturation, or hue.
1830 */
1831 ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
1832 hue+=0.5*(0.01*percent_hue-1.0);
1833 while (hue < 0.0)
1834 hue+=1.0;
1835 while (hue >= 1.0)
1836 hue-=1.0;
1837 saturation*=0.01*percent_saturation;
1838 lightness*=0.01*percent_lightness;
1839 ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
1840 }
1841
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)
1847 {
1848
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;
1853
1854 CLPixelType pixel = im[c];
1855
1856 CLQuantum
1857 blue,
1858 green,
1859 red;
1860
1861 red=getRed(pixel);
1862 green=getGreen(pixel);
1863 blue=getBlue(pixel);
1864
1865 switch (colorspace)
1866 {
1867 case HSLColorspace:
1868 default:
1869 {
1870 ModulateHSL(percent_hue, percent_saturation, percent_brightness,
1871 &red, &green, &blue);
1872 }
1873
1874 }
1875
1876 CLPixelType filteredPixel;
1877
1878 setRed(&filteredPixel, red);
1879 setGreen(&filteredPixel, green);
1880 setBlue(&filteredPixel, blue);
1881 filteredPixel.w = pixel.w;
1882
1883 im[c] = filteredPixel;
1884 }
1885 )
1886
1887/*
1888%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1889% %
1890% %
1891% %
1892% M o t i o n B l u r %
1893% %
1894% %
1895% %
1896%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1897*/
1898
1899 STRINGIFY(
1900 __kernel
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,
1904 const float4 bias,
1905 const ChannelType channel, const unsigned int matte) {
1906
1907 int2 currentPixel;
1908 currentPixel.x = get_global_id(0);
1909 currentPixel.y = get_global_id(1);
1910
1911 if (currentPixel.x >= imageWidth
1912 || currentPixel.y >= imageHeight)
1913 return;
1914
1915 float4 pixel;
1916 pixel.x = (float)bias.x;
1917 pixel.y = (float)bias.y;
1918 pixel.z = (float)bias.z;
1919 pixel.w = (float)bias.w;
1920
1921 if (((channel & AlphaChannel) == 0) || (matte == 0)) {
1922
1923 for (int i = 0; i < width; i++) {
1924 // only support EdgeVirtualPixelMethod through ClampToCanvas
1925 // TODO: implement other virtual pixel method
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];
1930
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);
1935 }
1936
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;
1943 }
1944 else {
1945
1946 float gamma = 0.0f;
1947 for (int i = 0; i < width; i++) {
1948 // only support EdgeVirtualPixelMethod through ClampToCanvas
1949 // TODO: implement other virtual pixel method
1950 int2 samplePixel = currentPixel + offset[i];
1951 samplePixel.x = ClampToCanvas(samplePixel.x, imageWidth);
1952 samplePixel.y = ClampToCanvas(samplePixel.y, imageHeight);
1953
1954 CLPixelType samplePixelValue = input[ samplePixel.y * imageWidth + samplePixel.x];
1955
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;
1961
1962 pixel.w += k * alpha * samplePixelValue.w;
1963
1964 gamma+=k*alpha;
1965 }
1966 gamma = MagickSafeReciprocal(gamma);
1967 pixel.xyz = gamma*pixel.xyz;
1968
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;
1975 }
1976 }
1977 )
1978
1979/*
1980%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1981% %
1982% %
1983% %
1984% R e s i z e %
1985% %
1986% %
1987% %
1988%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1989*/
1990
1991 STRINGIFY(
1992 // Based on Box from resize.c
1993 float BoxResizeFilter(const float x)
1994 {
1995 return 1.0f;
1996 }
1997 )
1998
1999 STRINGIFY(
2000 // Based on CubicBC from resize.c
2001 float CubicBC(const float x,const __global float* resizeFilterCoefficients)
2002 {
2003 /*
2004 Cubic Filters using B,C determined values:
2005 Mitchell-Netravali B = 1/3 C = 1/3 "Balanced" cubic spline filter
2006 Catmull-Rom B = 0 C = 1/2 Interpolatory and exact on linears
2007 Spline B = 1 C = 0 B-Spline Gaussian approximation
2008 Hermite B = 0 C = 0 B-Spline interpolator
2009
2010 See paper by Mitchell and Netravali, Reconstruction Filters in Computer
2011 Graphics Computer Graphics, Volume 22, Number 4, August 1988
2012 http://www.cs.utexas.edu/users/fussell/courses/cs384g/lectures/mitchell/
2013 Mitchell.pdf.
2014
2015 Coefficients are determined from B,C values:
2016 P0 = ( 6 - 2*B )/6 = coeff[0]
2017 P1 = 0
2018 P2 = (-18 +12*B + 6*C )/6 = coeff[1]
2019 P3 = ( 12 - 9*B - 6*C )/6 = coeff[2]
2020 Q0 = ( 8*B +24*C )/6 = coeff[3]
2021 Q1 = ( -12*B -48*C )/6 = coeff[4]
2022 Q2 = ( 6*B +30*C )/6 = coeff[5]
2023 Q3 = ( - 1*B - 6*C )/6 = coeff[6]
2024
2025 which are used to define the filter:
2026
2027 P0 + P1*x + P2*x^2 + P3*x^3 0 <= x < 1
2028 Q0 + Q1*x + Q2*x^2 + Q3*x^3 1 <= x < 2
2029
2030 which ensures function is continuous in value and derivative (slope).
2031 */
2032 if (x < 1.0)
2033 return(resizeFilterCoefficients[0]+x*(x*
2034 (resizeFilterCoefficients[1]+x*resizeFilterCoefficients[2])));
2035 if (x < 2.0)
2036 return(resizeFilterCoefficients[3]+x*(resizeFilterCoefficients[4]+x*
2037 (resizeFilterCoefficients[5]+x*resizeFilterCoefficients[6])));
2038 return(0.0);
2039 }
2040 )
2041
2042 STRINGIFY(
2043 float Sinc(const float x)
2044 {
2045 if (x != 0.0f)
2046 {
2047 const float alpha=(float) (MagickPI*x);
2048 return sinpi(x)/alpha;
2049 }
2050 return(1.0f);
2051 }
2052 )
2053
2054 STRINGIFY(
2055 float Triangle(const float x)
2056 {
2057 /*
2058 1st order (linear) B-Spline, bilinear interpolation, Tent 1D filter, or
2059 a Bartlett 2D Cone filter. Also used as a Bartlett Windowing function
2060 for Sinc().
2061 */
2062 return ((x<1.0f)?(1.0f-x):0.0f);
2063 }
2064 )
2065
2066
2067 STRINGIFY(
2068 float Hann(const float x)
2069 {
2070 /*
2071 Cosine window function:
2072 0.5+0.5*cos(pi*x).
2073 */
2074 const float cosine=cos((MagickPI*x));
2075 return(0.5f+0.5f*cosine);
2076 }
2077 )
2078
2079 STRINGIFY(
2080 float Hamming(const float x)
2081 {
2082 /*
2083 Offset cosine window function:
2084 .54 + .46 cos(pi x).
2085 */
2086 const float cosine=cos((MagickPI*x));
2087 return(0.54f+0.46f*cosine);
2088 }
2089 )
2090
2091 STRINGIFY(
2092 float Blackman(const float x)
2093 {
2094 /*
2095 Blackman: 2nd order cosine windowing function:
2096 0.42 + 0.5 cos(pi x) + 0.08 cos(2pi x)
2097
2098 Refactored by Chantal Racette and Nicolas Robidoux to one trig call and
2099 five flops.
2100 */
2101 const float cosine=cos((MagickPI*x));
2102 return(0.34f+cosine*(0.5f+cosine*0.16f));
2103 }
2104 )
2105
2106 STRINGIFY(
2107 static inline float applyResizeFilter(const float x, const ResizeWeightingFunctionType filterType, const __global float* filterCoefficients)
2108 {
2109 switch (filterType)
2110 {
2111 /* Call Sinc even for SincFast to get better precision on GPU
2112 and to avoid thread divergence. Sinc is pretty fast on GPU anyway...*/
2113 case SincWeightingFunction:
2114 case SincFastWeightingFunction:
2115 return Sinc(x);
2116 case CubicBCWeightingFunction:
2117 return CubicBC(x,filterCoefficients);
2118 case BoxWeightingFunction:
2119 return BoxResizeFilter(x);
2120 case TriangleWeightingFunction:
2121 return Triangle(x);
2122 case HannWeightingFunction:
2123 return Hann(x);
2124 case HammingWeightingFunction:
2125 return Hamming(x);
2126 case BlackmanWeightingFunction:
2127 return Blackman(x);
2128
2129 default:
2130 return 0.0f;
2131 }
2132 }
2133 )
2134
2135
2136 STRINGIFY(
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)
2140 {
2141 float scale;
2142 float xBlur = fabs(x/resizeFilterBlur);
2143 if (resizeWindowSupport < MagickEpsilon
2144 || resizeWindowType == BoxWeightingFunction)
2145 {
2146 scale = 1.0f;
2147 }
2148 else
2149 {
2150 scale = resizeFilterScale;
2151 scale = applyResizeFilter(xBlur*scale, resizeWindowType, resizeFilterCubicCoefficients);
2152 }
2153 float weight = scale * applyResizeFilter(xBlur, resizeFilterType, resizeFilterCubicCoefficients);
2154 return weight;
2155 }
2156
2157 )
2158
2159 ;
2160 const char *accelerateKernels2 =
2161
2162 STRINGIFY(
2163
2164 static inline unsigned int getNumWorkItemsPerPixel(const unsigned int pixelPerWorkgroup, const unsigned int numWorkItems) {
2165 return (numWorkItems/pixelPerWorkgroup);
2166 }
2167
2168 // returns the index of the pixel for the current workitem to compute.
2169 // returns -1 if this workitem doesn't need to participate in any computation
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;
2174 return pixelIndex;
2175 }
2176
2177 )
2178
2179 STRINGIFY(
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)
2189 {
2190 // calculate the range of resized image pixels computed by this workgroup
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;
2194
2195 // calculate the range of input image pixels to cache
2196 float scale = MagickMax(1.0f/xFactor+MagickEpsilon ,1.0f);
2197 const float support = MagickMax(scale*resizeFilterSupport,0.5f);
2198 scale = MagickSafeReciprocal(scale);
2199
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);
2202
2203 // cache the input pixels into local memory
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);
2209
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++)
2213 {
2214 const unsigned int chunkStartX = startX + chunk*pixelChunkSize;
2215 const unsigned int chunkStopX = MagickMin(chunkStartX + pixelChunkSize, stopX);
2216 const unsigned int actualNumPixelInThisChunk = chunkStopX - chunkStartX;
2217
2218 // determine which resized pixel computed by this workitem
2219 const unsigned int itemID = get_local_id(0);
2220 const unsigned int numItems = getNumWorkItemsPerPixel(actualNumPixelInThisChunk, get_local_size(0));
2221
2222 const int pixelIndex = pixelToCompute(itemID, actualNumPixelInThisChunk, get_local_size(0));
2223
2224 float4 filteredPixel = (float4)0.0f;
2225 float density = 0.0f;
2226 float gamma = 0.0f;
2227 // -1 means this workitem doesn't participate in the computation
2228 if (pixelIndex != -1)
2229 {
2230 // x coordinated of the resized pixel computed by this workitem
2231 const int x = chunkStartX + pixelIndex;
2232
2233 // calculate how many steps required for this pixel
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;
2238
2239 // calculate how many steps this workitem will contribute
2240 unsigned int numStepsPerWorkItem = n / numItems;
2241 numStepsPerWorkItem += ((numItems*numStepsPerWorkItem)==n?0:1);
2242
2243 const unsigned int startStep = (itemID%numItems)*numStepsPerWorkItem;
2244 if (startStep < n)
2245 {
2246 const unsigned int stopStep = MagickMin(startStep+numStepsPerWorkItem, n);
2247
2248 unsigned int cacheIndex = start+startStep-cacheRangeStartX;
2249 for (unsigned int i = startStep; i < stopStep; i++, cacheIndex++)
2250 {
2251 float weight = getResizeFilterWeight(resizeFilterCubicCoefficients,
2252 (ResizeWeightingFunctionType) resizeFilterType,
2253 (ResizeWeightingFunctionType) resizeWindowType,
2254 resizeFilterScale, resizeFilterWindowSupport,
2255 resizeFilterBlur, scale*(start + i - bisect + 0.5));
2256
2257 float4 cp = (float4)0.0f;
2258
2259 __local CLQuantum *p = inputImageCache + (cacheIndex*number_channels);
2260 cp.x = (float) *(p);
2261 if (number_channels > 2)
2262 {
2263 cp.y = (float) *(p + 1);
2264 cp.z = (float) *(p + 2);
2265 }
2266
2267 if (alpha_index != 0)
2268 {
2269 cp.w = (float) *(p + alpha_index);
2270
2271 float alpha = weight * QuantumScale * cp.w;
2272
2273 filteredPixel.x += alpha * cp.x;
2274 filteredPixel.y += alpha * cp.y;
2275 filteredPixel.z += alpha * cp.z;
2276 filteredPixel.w += weight * cp.w;
2277 gamma += alpha;
2278 }
2279 else
2280 filteredPixel += ((float4) weight)*cp;
2281
2282 density += weight;
2283 }
2284 }
2285 }
2286
2287 // initialize the accumulators to zero
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;
2293 }
2294 barrier(CLK_LOCAL_MEM_FENCE);
2295
2296 // accumulate the filtered pixel value and the density
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;
2304 }
2305 }
2306 barrier(CLK_LOCAL_MEM_FENCE);
2307 }
2308
2309 if (itemID < actualNumPixelInThisChunk)
2310 {
2311 float4 filteredPixel = outputPixelCache[itemID];
2312
2313 float gamma = 0.0f;
2314 if (alpha_index != 0)
2315 gamma = gammaCache[itemID];
2316
2317 float density = densityCache[itemID];
2318 if ((density != 0.0f) && (density != 1.0f))
2319 {
2320 density = MagickSafeReciprocal(density);
2321 filteredPixel *= (float4) density;
2322 if (alpha_index != 0)
2323 gamma *= density;
2324 }
2325
2326 if (alpha_index != 0)
2327 {
2328 gamma = MagickSafeReciprocal(gamma);
2329 filteredPixel.x *= gamma;
2330 filteredPixel.y *= gamma;
2331 filteredPixel.z *= gamma;
2332 }
2333
2334 WriteAllChannels(filteredImage, number_channels, filteredColumns, chunkStartX + itemID, y, filteredPixel);
2335 }
2336 }
2337 }
2338 )
2339
2340
2341 STRINGIFY(
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)
2351 {
2352 // calculate the range of resized image pixels computed by this workgroup
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;
2356
2357 // calculate the range of input image pixels to cache
2358 float scale = MagickMax(1.0f/yFactor+MagickEpsilon ,1.0f);
2359 const float support = MagickMax(scale*resizeFilterSupport,0.5f);
2360 scale = MagickSafeReciprocal(scale);
2361
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);
2364
2365 // cache the input pixels into local memory
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++)
2371 {
2372 event_t e = async_work_group_strided_copy(inputImageCache + (rangeLength*i), inputImage+pos+i, rangeLength, stride, 0);
2373 wait_group_events(1,&e);
2374 }
2375
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++)
2379 {
2380 const unsigned int chunkStartY = startY + chunk*pixelChunkSize;
2381 const unsigned int chunkStopY = MagickMin(chunkStartY + pixelChunkSize, stopY);
2382 const unsigned int actualNumPixelInThisChunk = chunkStopY - chunkStartY;
2383
2384 // determine which resized pixel computed by this workitem
2385 const unsigned int itemID = get_local_id(1);
2386 const unsigned int numItems = getNumWorkItemsPerPixel(actualNumPixelInThisChunk, get_local_size(1));
2387
2388 const int pixelIndex = pixelToCompute(itemID, actualNumPixelInThisChunk, get_local_size(1));
2389
2390 float4 filteredPixel = (float4)0.0f;
2391 float density = 0.0f;
2392 float gamma = 0.0f;
2393 // -1 means this workitem doesn't participate in the computation
2394 if (pixelIndex != -1)
2395 {
2396 // x coordinated of the resized pixel computed by this workitem
2397 const int y = chunkStartY + pixelIndex;
2398
2399 // calculate how many steps required for this pixel
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;
2404
2405 // calculate how many steps this workitem will contribute
2406 unsigned int numStepsPerWorkItem = n / numItems;
2407 numStepsPerWorkItem += ((numItems*numStepsPerWorkItem)==n?0:1);
2408
2409 const unsigned int startStep = (itemID%numItems)*numStepsPerWorkItem;
2410 if (startStep < n)
2411 {
2412 const unsigned int stopStep = MagickMin(startStep+numStepsPerWorkItem, n);
2413
2414 unsigned int cacheIndex = start+startStep-cacheRangeStartY;
2415 for (unsigned int i = startStep; i < stopStep; i++, cacheIndex++)
2416 {
2417 float weight = getResizeFilterWeight(resizeFilterCubicCoefficients,
2418 (ResizeWeightingFunctionType) resizeFilterType,
2419 (ResizeWeightingFunctionType) resizeWindowType,
2420 resizeFilterScale, resizeFilterWindowSupport,
2421 resizeFilterBlur, scale*(start + i - bisect + 0.5));
2422
2423 float4 cp = (float4)0.0f;
2424
2425 __local CLQuantum *p = inputImageCache + cacheIndex;
2426 cp.x = (float) *(p);
2427 if (number_channels > 2)
2428 {
2429 cp.y = (float) *(p + rangeLength);
2430 cp.z = (float) *(p + (rangeLength * 2));
2431 }
2432
2433 if (alpha_index != 0)
2434 {
2435 cp.w = (float) *(p + (rangeLength * alpha_index));
2436
2437 float alpha = weight * QuantumScale * cp.w;
2438
2439 filteredPixel.x += alpha * cp.x;
2440 filteredPixel.y += alpha * cp.y;
2441 filteredPixel.z += alpha * cp.z;
2442 filteredPixel.w += weight * cp.w;
2443 gamma += alpha;
2444 }
2445 else
2446 filteredPixel += ((float4) weight)*cp;
2447
2448 density += weight;
2449 }
2450 }
2451 }
2452
2453 // initialize the accumulators to zero
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;
2459 }
2460 barrier(CLK_LOCAL_MEM_FENCE);
2461
2462 // accumulate the filtered pixel value and the density
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;
2470 }
2471 }
2472 barrier(CLK_LOCAL_MEM_FENCE);
2473 }
2474
2475 if (itemID < actualNumPixelInThisChunk)
2476 {
2477 float4 filteredPixel = outputPixelCache[itemID];
2478
2479 float gamma = 0.0f;
2480 if (alpha_index != 0)
2481 gamma = gammaCache[itemID];
2482
2483 float density = densityCache[itemID];
2484 if ((density != 0.0f) && (density != 1.0f))
2485 {
2486 density = MagickSafeReciprocal(density);
2487 filteredPixel *= (float4) density;
2488 if (alpha_index != 0)
2489 gamma *= density;
2490 }
2491
2492 if (alpha_index != 0)
2493 {
2494 gamma = MagickSafeReciprocal(gamma);
2495 filteredPixel.x *= gamma;
2496 filteredPixel.y *= gamma;
2497 filteredPixel.z *= gamma;
2498 }
2499
2500 WriteAllChannels(filteredImage, number_channels, filteredColumns, x, chunkStartY + itemID, filteredPixel);
2501 }
2502 }
2503 }
2504 )
2505
2506/*
2507%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2508% %
2509% %
2510% %
2511% R o t a t i o n a l B l u r %
2512% %
2513% %
2514% %
2515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2516*/
2517
2518 STRINGIFY(
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)
2524 {
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);
2533
2534 float blur_radius = hypot(blurCenter.x, blurCenter.y);
2535
2536 if (radius > MagickEpsilon)
2537 {
2538 step = (unsigned int) (blur_radius / radius);
2539 if (step == 0)
2540 step = 1;
2541 if (step >= cossin_theta_size)
2542 step = cossin_theta_size-1;
2543 }
2544
2545 float4 result = 0.0f;
2546 float normalize = 0.0f;
2547 float gamma = 0.0f;
2548
2549 for (unsigned int i=0; i<cossin_theta_size; i+=step)
2550 {
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);
2553
2554 float4 pixel = ReadAllChannels(image, number_channels, columns, cx, cy);
2555
2556 if ((number_channels == 4) || (number_channels == 2))
2557 {
2558 float alpha = (float)(QuantumScale*pixel.w);
2559
2560 gamma += alpha;
2561
2562 result.x += alpha * pixel.x;
2563 result.y += alpha * pixel.y;
2564 result.z += alpha * pixel.z;
2565 result.w += pixel.w;
2566 }
2567 else
2568 result += pixel;
2569
2570 normalize += 1.0f;
2571 }
2572
2573 normalize = MagickSafeReciprocal(normalize);
2574
2575 if ((number_channels == 4) || (number_channels == 2))
2576 {
2577 gamma = MagickSafeReciprocal(gamma);
2578 result.x *= gamma;
2579 result.y *= gamma;
2580 result.z *= gamma;
2581 result.w *= normalize;
2582 }
2583 else
2584 result *= normalize;
2585
2586 WriteFloat4(filteredImage, number_channels, columns, x, y, channel, result);
2587 }
2588 )
2589
2590/*
2591%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2592% %
2593% %
2594% %
2595% U n s h a r p M a s k %
2596% %
2597% %
2598% %
2599%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2600*/
2601
2602 STRINGIFY(
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)
2610 {
2611 const unsigned int radius = (width-1)/2;
2612
2613 // cache the pixel shared by the workgroup
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;
2617
2618 if ((groupStartY >= 0) && (groupStopY < rows))
2619 {
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);
2623 }
2624 else
2625 {
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];
2628
2629 barrier(CLK_LOCAL_MEM_FENCE);
2630 }
2631 // cache the filter as well
2632 event_t e = async_work_group_copy(cachedFilter,filter,width,0);
2633 wait_group_events(1,&e);
2634
2635 // only do the work if this is not a patched item
2636 const int cy = get_global_id(1);
2637
2638 if (cy < rows)
2639 {
2640 float4 blurredPixel = (float4) 0.0f;
2641
2642 int i = 0;
2643
2644 for ( ; i+7 < width; )
2645 {
2646 for (int j=0; j < 8; j++, i++)
2647 blurredPixel+=cachedFilter[i+j]*cachedData[i+j+get_local_id(1)];
2648 }
2649
2650 for ( ; i < width; i++)
2651 blurredPixel+=cachedFilter[i]*cachedData[i+get_local_id(1)];
2652
2653 float4 inputImagePixel = ReadFloat4(image,number_channels,columns,groupX,cy,channel);
2654 float4 outputPixel = inputImagePixel - blurredPixel;
2655
2656 float quantumThreshold = QuantumRange*threshold;
2657
2658 int4 mask = isless(fabs(2.0f*outputPixel), (float4)quantumThreshold);
2659 outputPixel = select(inputImagePixel + outputPixel * gain, inputImagePixel, mask);
2660
2661 //write back
2662 WriteFloat4(filteredImage,number_channels,columns,groupX,cy,channel,outputPixel);
2663 }
2664 }
2665 )
2666
2667 STRINGIFY(
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)
2672 {
2673 const unsigned int x = get_global_id(0);
2674 const unsigned int y = get_global_id(1);
2675
2676 const unsigned int radius = (width - 1) / 2;
2677
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;
2681
2682 while (row < endRow) {
2683 int srcy = (row < 0) ? -row : row; // mirror pad
2684 srcy = (srcy >= rows) ? (2 * rows - srcy - 1) : srcy;
2685
2686 float4 value = 0.0f;
2687
2688 int ix = x - radius;
2689 int i = 0;
2690
2691 while (i + 7 < width) {
2692 for (int j = 0; j < 8; ++j) { // unrolled
2693 int srcx = ix + 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);
2697 }
2698 ix += 8;
2699 i += 8;
2700 }
2701
2702 while (i < width) {
2703 int srcx = (ix < 0) ? -ix : ix; // mirror pad
2704 srcx = (srcx >= columns) ? (2 * columns - srcx - 1) : srcx;
2705 value += filter[i] * ReadFloat4(image, number_channels, columns, srcx, srcy, channel);
2706 ++i;
2707 ++ix;
2708 }
2709 pixels[(row - baseRow) * get_local_size(0) + get_local_id(0)] = value;
2710 row += get_local_size(1);
2711 }
2712
2713 barrier(CLK_LOCAL_MEM_FENCE);
2714
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);
2719
2720 int i = 0;
2721 while (i + 7 < width) {
2722 for (int j = 0; j < 8; ++j) // unrolled
2723 value += (float4)(filter[i]) * pixels[px + (py + i + j) * prp];
2724 i += 8;
2725 }
2726 while (i < width) {
2727 value += (float4)(filter[i]) * pixels[px + (py + i) * prp];
2728 ++i;
2729 }
2730
2731 if ((x < columns) && (y < rows)) {
2732 float4 srcPixel = ReadFloat4(image, number_channels, columns, x, y, channel);
2733 float4 diff = srcPixel - value;
2734
2735 float quantumThreshold = QuantumRange*threshold;
2736
2737 int4 mask = isless(fabs(2.0f * diff), (float4)quantumThreshold);
2738 value = select(srcPixel + diff * gain, srcPixel, mask);
2739
2740 WriteFloat4(filteredImage, number_channels, columns, x, y, channel, value);
2741 }
2742 }
2743 )
2744
2745/*
2746%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2747% %
2748% %
2749% %
2750% W a v e l e t D e n o i s e %
2751% %
2752% %
2753% %
2754%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2755*/
2756
2757 STRINGIFY(
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)
2763 {
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 };
2768
2769 CLQuantum stage[48]; // 16 * 3 (we only need 3 channels)
2770
2771 local float buffer[64 * 64];
2772
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;
2775
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;
2779
2780 for (int channel = 0; channel < max_channels; ++channel)
2781 stage[(i / 4) + (16 * channel)] = srcImage[pos + channel];
2782 }
2783
2784 for (int channel = 0; channel < max_channels; ++channel) {
2785 // Load LDS
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)]);
2788
2789 // Process
2790
2791 float tmp[16];
2792 float accum[16];
2793 float pixel;
2794
2795 for (int i = 0; i < 16; i++)
2796 accum[i]=0.0f;
2797
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];
2802
2803 // Apply horizontal hat
2804 for (int i = get_local_id(1); i < tileSize; i += get_local_size(1)) {
2805 const int offset = i * tileRowPixels;
2806 if (pass == 0)
2807 tmp[i / 4] = buffer[x + offset]; // snapshot input on first pass
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;
2811 }
2812 barrier(CLK_LOCAL_MEM_FENCE);
2813
2814 // Apply vertical hat
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;
2818 tmp[i / 4] = pixel; // hold output in tmp until all workitems are done
2819 if (delta < -thresh)
2820 delta += thresh;
2821 else if (delta > thresh)
2822 delta -= thresh;
2823 else
2824 delta = 0;
2825 accum[i / 4] += delta;
2826 }
2827 barrier(CLK_LOCAL_MEM_FENCE);
2828
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]; // store lowpass for next pass
2832 else // last pass
2833 for (int i = get_local_id(1); i < tileSize; i += get_local_size(1))
2834 accum[i / 4] += tmp[i / 4]; // add the lowpass signal back to output
2835 barrier(CLK_LOCAL_MEM_FENCE);
2836 }
2837
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]);
2840
2841 barrier(CLK_LOCAL_MEM_FENCE);
2842 }
2843
2844 // Write from stage to output
2845
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)];
2852 }
2853 }
2854 }
2855 }
2856 }
2857 )
2858
2859 ;
2860
2861#endif // MAGICKCORE_OPENCL_SUPPORT
2862
2863#if defined(__cplusplus) || defined(c_plusplus)
2864}
2865#endif
2866
2867#endif // MAGICKCORE_ACCELERATE_KERNELS_PRIVATE_H