blob: 3b342751f757194c71c1d17b2ff54f34cd34ec10 [file] [log] [blame]
cristy3f6d1482010-01-20 21:01:21 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% AAA CCCC CCCC EEEEE L EEEEE RRRR AAA TTTTT EEEEE %
7% A A C C E L E R R A A T E %
8% AAAAA C C EEE L EEE RRRR AAAAA T EEE %
9% A A C C E L E R R A A T E %
10% A A CCCC CCCC EEEEE LLLLL EEEEE R R A A T EEEEE %
11% %
12% %
13% MagickCore Acceleration Methods %
14% %
15% Software Design %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristyf034abb2013-11-24 14:16:14 +000017% SiuChi Chan %
18% Guansong Zhang %
cristy3f6d1482010-01-20 21:01:21 +000019% January 2010 %
20% %
21% %
cristyfe676ee2013-11-18 13:03:38 +000022% Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
cristy3f6d1482010-01-20 21:01:21 +000023% dedicated to making software imaging solutions freely available. %
24% %
25% You may not use this file except in compliance with the License. You may %
26% obtain a copy of the License at %
27% %
28% http://www.imagemagick.org/script/license.php %
29% %
30% Unless required by applicable law or agreed to in writing, software %
31% distributed under the License is distributed on an "AS IS" BASIS, %
32% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
33% See the License for the specific language governing permissions and %
34% limitations under the License. %
35% %
36%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cristy3f6d1482010-01-20 21:01:21 +000037*/
cristyf034abb2013-11-24 14:16:14 +000038
cristy3f6d1482010-01-20 21:01:21 +000039/*
cristyf034abb2013-11-24 14:16:14 +000040Include declarations.
cristy3f6d1482010-01-20 21:01:21 +000041*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/accelerate.h"
cristyf034abb2013-11-24 14:16:14 +000044#include "MagickCore/accelerate-private.h"
cristy4c08aed2011-07-01 19:47:50 +000045#include "MagickCore/artifact.h"
cristy35f33492011-07-07 16:54:49 +000046#include "MagickCore/cache.h"
cristyd1dd6e42011-09-04 01:46:08 +000047#include "MagickCore/cache-private.h"
cristy4c08aed2011-07-01 19:47:50 +000048#include "MagickCore/cache-view.h"
49#include "MagickCore/color-private.h"
cristy7f866842013-07-11 01:15:58 +000050#include "MagickCore/delegate-private.h"
cristy4c08aed2011-07-01 19:47:50 +000051#include "MagickCore/enhance.h"
52#include "MagickCore/exception.h"
53#include "MagickCore/exception-private.h"
54#include "MagickCore/gem.h"
55#include "MagickCore/hashmap.h"
56#include "MagickCore/image.h"
57#include "MagickCore/image-private.h"
58#include "MagickCore/list.h"
59#include "MagickCore/memory_.h"
60#include "MagickCore/monitor-private.h"
61#include "MagickCore/accelerate.h"
cristyf034abb2013-11-24 14:16:14 +000062#include "MagickCore/opencl.h"
63#include "MagickCore/opencl-private.h"
cristy4c08aed2011-07-01 19:47:50 +000064#include "MagickCore/option.h"
dirk434ce8a2014-08-17 08:33:47 +000065#include "MagickCore/pixel-accessor.h"
cristyf034abb2013-11-24 14:16:14 +000066#include "MagickCore/pixel-private.h"
cristy4c08aed2011-07-01 19:47:50 +000067#include "MagickCore/prepress.h"
68#include "MagickCore/quantize.h"
dirk8a5cf512014-07-28 20:16:27 +000069#include "MagickCore/quantum-private.h"
cristye85d0f72013-11-27 02:25:43 +000070#include "MagickCore/random_.h"
71#include "MagickCore/random-private.h"
cristy4c08aed2011-07-01 19:47:50 +000072#include "MagickCore/registry.h"
cristyf034abb2013-11-24 14:16:14 +000073#include "MagickCore/resize.h"
74#include "MagickCore/resize-private.h"
cristy4c08aed2011-07-01 19:47:50 +000075#include "MagickCore/semaphore.h"
76#include "MagickCore/splay-tree.h"
77#include "MagickCore/statistic.h"
78#include "MagickCore/string_.h"
79#include "MagickCore/string-private.h"
80#include "MagickCore/token.h"
cristyf034abb2013-11-24 14:16:14 +000081
82#ifdef MAGICKCORE_CLPERFMARKER
83#include "CLPerfMarker.h"
84#endif
85
cristye85d0f72013-11-27 02:25:43 +000086#define MAGICK_MAX(x,y) (((x) >= (y))?(x):(y))
87#define MAGICK_MIN(x,y) (((x) <= (y))?(x):(y))
88
cristyf034abb2013-11-24 14:16:14 +000089#if defined(MAGICKCORE_OPENCL_SUPPORT)
90
dirkb0d783f2014-08-31 10:48:05 +000091#define ALIGNED(pointer,type) ((((size_t)(pointer)) & (sizeof(type)-1)) == 0)
cristyf034abb2013-11-24 14:16:14 +000092
cristy0c832c62014-03-07 22:21:04 +000093/* pad the global workgroup size to the next multiple of
94 the local workgroup size */
dirk832becc2014-08-04 19:44:34 +000095inline static unsigned int padGlobalWorkgroupSizeToLocalWorkgroupSize(
96 const unsigned int orgGlobalSize,const unsigned int localGroupSize)
97{
98 return ((orgGlobalSize+(localGroupSize-1))/localGroupSize*localGroupSize);
99}
100
101static MagickBooleanType checkOpenCLEnvironment(ExceptionInfo* exception)
102{
103 MagickBooleanType
104 flag;
105
106 MagickCLEnv
107 clEnv;
108
109 clEnv=GetDefaultOpenCLEnv();
110
111 GetMagickOpenCLEnvParam(clEnv,MAGICK_OPENCL_ENV_PARAM_OPENCL_DISABLED,
112 sizeof(MagickBooleanType),&flag,exception);
113 if (flag != MagickFalse)
114 return(MagickFalse);
115
116 GetMagickOpenCLEnvParam(clEnv,MAGICK_OPENCL_ENV_PARAM_OPENCL_INITIALIZED,
117 sizeof(MagickBooleanType),&flag,exception);
118 if (flag == MagickFalse)
119 {
120 if (InitOpenCLEnv(clEnv,exception) == MagickFalse)
121 return(MagickFalse);
122
123 GetMagickOpenCLEnvParam(clEnv,MAGICK_OPENCL_ENV_PARAM_OPENCL_DISABLED,
124 sizeof(MagickBooleanType),&flag,exception);
125 if (flag != MagickFalse)
126 return(MagickFalse);
127 }
128
129 return(MagickTrue);
130}
131
132static MagickBooleanType checkAccelerateCondition(const Image* image,
133 const ChannelType channel)
134{
135 /* check if the image's colorspace is supported */
136 if (image->colorspace != RGBColorspace &&
137 image->colorspace != sRGBColorspace &&
138 image->colorspace != GRAYColorspace)
139 return(MagickFalse);
140
141 /* check if the channel is supported */
142 if (((channel & RedChannel) == 0) ||
143 ((channel & GreenChannel) == 0) ||
144 ((channel & BlueChannel) == 0))
145 return(MagickFalse);
146
147 /* check if the virtual pixel method is compatible with the OpenCL implementation */
148 if ((GetImageVirtualPixelMethod(image) != UndefinedVirtualPixelMethod) &&
149 (GetImageVirtualPixelMethod(image) != EdgeVirtualPixelMethod))
150 return(MagickFalse);
151
152 /* check if the image has read / write mask */
153 if (image->read_mask != MagickFalse || image->write_mask != MagickFalse)
154 return(MagickFalse);
155
dirk434ce8a2014-08-17 08:33:47 +0000156 /* check if pixel order is RGBA */
157 if (GetPixelChannelOffset(image,RedPixelChannel) != 0 ||
158 GetPixelChannelOffset(image,GreenPixelChannel) != 1 ||
159 GetPixelChannelOffset(image,BluePixelChannel) != 2 ||
160 GetPixelChannelOffset(image,AlphaPixelChannel) != 3)
161 return(MagickFalse);
162
163 /* check if all channels are available */
164 if (((GetPixelRedTraits(image) & UpdatePixelTrait) == 0) ||
165 ((GetPixelGreenTraits(image) & UpdatePixelTrait) == 0) ||
166 ((GetPixelBlueTraits(image) & UpdatePixelTrait) == 0) ||
167 ((GetPixelAlphaTraits(image) & UpdatePixelTrait) == 0))
168 return(MagickFalse);
169
dirk832becc2014-08-04 19:44:34 +0000170 return(MagickTrue);
171}
172
173static MagickBooleanType checkHistogramCondition(Image *image,
174 const ChannelType channel)
175{
176 /* ensure this is the only pass get in for now. */
177 if ((channel & SyncChannels) == 0)
178 return MagickFalse;
179
180 if (image->intensity == Rec601LuminancePixelIntensityMethod ||
181 image->intensity == Rec709LuminancePixelIntensityMethod)
182 return MagickFalse;
183
184 if (image->colorspace != sRGBColorspace)
185 return MagickFalse;
186
187 return MagickTrue;
188}
189
190static MagickBooleanType splitImage(const Image* image)
191{
192 MagickBooleanType
193 split;
194
195 MagickCLEnv
196 clEnv;
197
198 unsigned long
199 allocSize,
200 tempSize;
201
202 clEnv=GetDefaultOpenCLEnv();
203
204 allocSize=GetOpenCLDeviceMaxMemAllocSize(clEnv);
dirkb0d783f2014-08-31 10:48:05 +0000205 tempSize=(unsigned long) (image->columns * image->rows * 4 * 4);
dirk832becc2014-08-04 19:44:34 +0000206
207 split = ((tempSize > allocSize) ? MagickTrue : MagickFalse);
208 return(split);
cristy0c832c62014-03-07 22:21:04 +0000209}
210
dirk8a5cf512014-07-28 20:16:27 +0000211/*
212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213% %
214% %
215% %
216% C o n v o l v e I m a g e w i t h O p e n C L %
217% %
218% %
219% %
220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221%
222% ConvolveImage() applies a custom convolution kernel to the image.
223%
224% The format of the ConvolveImage method is:
225%
226% Image *ConvolveImage(const Image *image,const size_t order,
227% const double *kernel,ExceptionInfo *exception)
228% Image *ConvolveImageChannel(const Image *image,const ChannelType channel,
229% const size_t order,const double *kernel,ExceptionInfo *exception)
230%
231% A description of each parameter follows:
232%
233% o image: the image.
234%
235% o channel: the channel type.
236%
237% o kernel: kernel info.
238%
239% o exception: return any errors or warnings in this structure.
240%
241*/
cristyf034abb2013-11-24 14:16:14 +0000242
dirk832becc2014-08-04 19:44:34 +0000243static Image *ComputeConvolveImage(const Image* image,
244 const ChannelType channel,const KernelInfo *kernel,ExceptionInfo *exception)
245{
246 CacheView
247 *filteredImage_view,
248 *image_view;
249
250 cl_command_queue
251 queue;
252
253 cl_context
254 context;
255
256 cl_kernel
257 clkernel;
258
259 cl_int
260 clStatus;
261
262 cl_mem
263 convolutionKernel,
264 filteredImageBuffer,
265 imageBuffer;
266
267 cl_mem_flags
268 mem_flags;
269
270 cl_ulong
271 deviceLocalMemorySize;
272
273 const void
274 *inputPixels;
275
276 float
277 *kernelBufferPtr;
278
279 Image
280 *filteredImage;
281
282 MagickBooleanType
283 outputReady;
284
285 MagickCLEnv
286 clEnv;
287
288 MagickSizeType
289 length;
290
291 size_t
292 global_work_size[3],
293 localGroupSize[3],
294 localMemoryRequirement;
295
296 unsigned
297 kernelSize;
298
299 unsigned int
300 filterHeight,
301 filterWidth,
302 i,
303 imageHeight,
304 imageWidth,
305 matte;
306
307 void
308 *filteredPixels,
309 *hostPtr;
310
311 /* intialize all CL objects to NULL */
312 context = NULL;
313 imageBuffer = NULL;
314 filteredImageBuffer = NULL;
315 convolutionKernel = NULL;
316 clkernel = NULL;
317 queue = NULL;
318
319 filteredImage = NULL;
320 filteredImage_view = NULL;
cristyf034abb2013-11-24 14:16:14 +0000321 outputReady = MagickFalse;
322
323 clEnv = GetDefaultOpenCLEnv();
324 context = GetOpenCLContext(clEnv);
325
dirk832becc2014-08-04 19:44:34 +0000326 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +0000327 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +0000328 if (inputPixels == (const void *) NULL)
329 {
dirk8a5cf512014-07-28 20:16:27 +0000330 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +0000331 goto cleanup;
332 }
333
334 /* Create and initialize OpenCL buffers. */
335
336 /* If the host pointer is aligned to the size of CLPixelPacket,
337 then use the host buffer directly from the GPU; otherwise,
338 create a buffer on the GPU and copy the data over */
339 if (ALIGNED(inputPixels,CLPixelPacket))
340 {
341 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
342 }
343 else
344 {
345 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
346 }
347 /* create a CL buffer from image pixel buffer */
dirk832becc2014-08-04 19:44:34 +0000348 length = image->columns * image->rows;
dirk8a5cf512014-07-28 20:16:27 +0000349 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +0000350 if (clStatus != CL_SUCCESS)
351 {
cristy0c832c62014-03-07 22:21:04 +0000352 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +0000353 goto cleanup;
354 }
355
dirk8a5cf512014-07-28 20:16:27 +0000356 filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristyf034abb2013-11-24 14:16:14 +0000357 assert(filteredImage != NULL);
dirk8a5cf512014-07-28 20:16:27 +0000358 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristyf034abb2013-11-24 14:16:14 +0000359 {
cristya22457d2013-12-07 14:03:06 +0000360 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000361 goto cleanup;
362 }
dirk832becc2014-08-04 19:44:34 +0000363 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +0000364 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristyf034abb2013-11-24 14:16:14 +0000365 if (filteredPixels == (void *) NULL)
366 {
cristya22457d2013-12-07 14:03:06 +0000367 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename);
cristyf034abb2013-11-24 14:16:14 +0000368 goto cleanup;
369 }
370
371 if (ALIGNED(filteredPixels,CLPixelPacket))
372 {
373 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
374 hostPtr = filteredPixels;
375 }
376 else
377 {
378 mem_flags = CL_MEM_WRITE_ONLY;
379 hostPtr = NULL;
380 }
381 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +0000382 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +0000383 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +0000384 if (clStatus != CL_SUCCESS)
385 {
cristy0c832c62014-03-07 22:21:04 +0000386 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +0000387 goto cleanup;
388 }
389
dirkb0d783f2014-08-31 10:48:05 +0000390 kernelSize = (unsigned int) (kernel->width * kernel->height);
cristy0c832c62014-03-07 22:21:04 +0000391 convolutionKernel = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, kernelSize * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +0000392 if (clStatus != CL_SUCCESS)
393 {
cristy0c832c62014-03-07 22:21:04 +0000394 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +0000395 goto cleanup;
396 }
397
398 queue = AcquireOpenCLCommandQueue(clEnv);
399
cristy0c832c62014-03-07 22:21:04 +0000400 kernelBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, convolutionKernel, CL_TRUE, CL_MAP_WRITE, 0, kernelSize * sizeof(float)
cristyf034abb2013-11-24 14:16:14 +0000401 , 0, NULL, NULL, &clStatus);
402 if (clStatus != CL_SUCCESS)
403 {
cristy0c832c62014-03-07 22:21:04 +0000404 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +0000405 goto cleanup;
406 }
407 for (i = 0; i < kernelSize; i++)
408 {
409 kernelBufferPtr[i] = (float) kernel->values[i];
410 }
cristy0c832c62014-03-07 22:21:04 +0000411 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, convolutionKernel, kernelBufferPtr, 0, NULL, NULL);
412 if (clStatus != CL_SUCCESS)
cristyf034abb2013-11-24 14:16:14 +0000413 {
cristy0c832c62014-03-07 22:21:04 +0000414 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000415 goto cleanup;
416 }
cristy0c832c62014-03-07 22:21:04 +0000417 clEnv->library->clFlush(queue);
418
419 deviceLocalMemorySize = GetOpenCLDeviceLocalMemorySize(clEnv);
cristyf034abb2013-11-24 14:16:14 +0000420
421 /* Compute the local memory requirement for a 16x16 workgroup.
422 If it's larger than 16k, reduce the workgroup size to 8x8 */
423 localGroupSize[0] = 16;
424 localGroupSize[1] = 16;
425 localMemoryRequirement = (localGroupSize[0]+kernel->width-1) * (localGroupSize[1]+kernel->height-1) * sizeof(CLPixelPacket)
426 + kernel->width*kernel->height*sizeof(float);
cristy0c832c62014-03-07 22:21:04 +0000427
428 if (localMemoryRequirement > deviceLocalMemorySize)
cristyf034abb2013-11-24 14:16:14 +0000429 {
cristyf034abb2013-11-24 14:16:14 +0000430 localGroupSize[0] = 8;
431 localGroupSize[1] = 8;
cristyf034abb2013-11-24 14:16:14 +0000432 localMemoryRequirement = (localGroupSize[0]+kernel->width-1) * (localGroupSize[1]+kernel->height-1) * sizeof(CLPixelPacket)
433 + kernel->width*kernel->height*sizeof(float);
434 }
cristyf034abb2013-11-24 14:16:14 +0000435 if (localMemoryRequirement <= deviceLocalMemorySize)
436 {
437 /* get the OpenCL kernel */
cristya22457d2013-12-07 14:03:06 +0000438 clkernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "ConvolveOptimized");
cristyf034abb2013-11-24 14:16:14 +0000439 if (clkernel == NULL)
440 {
cristya22457d2013-12-07 14:03:06 +0000441 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000442 goto cleanup;
443 }
444
445 /* set the kernel arguments */
446 i = 0;
dirk8a5cf512014-07-28 20:16:27 +0000447 clStatus =clEnv->library->clSetKernelArg(clkernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +0000448 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(cl_mem),(void *)&filteredImageBuffer);
dirkb0d783f2014-08-31 10:48:05 +0000449 imageWidth = (unsigned int) image->columns;
450 imageHeight = (unsigned int) image->rows;
cristy0c832c62014-03-07 22:21:04 +0000451 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&imageWidth);
452 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&imageHeight);
453 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(cl_mem),(void *)&convolutionKernel);
dirkb0d783f2014-08-31 10:48:05 +0000454 filterWidth = (unsigned int) kernel->width;
455 filterHeight = (unsigned int) kernel->height;
cristy0c832c62014-03-07 22:21:04 +0000456 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&filterWidth);
457 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&filterHeight);
dirk8a5cf512014-07-28 20:16:27 +0000458 matte = (image->alpha_trait==BlendPixelTrait)?1:0;
cristy0c832c62014-03-07 22:21:04 +0000459 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&matte);
460 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(ChannelType),(void *)&channel);
461 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++, (localGroupSize[0] + kernel->width-1)*(localGroupSize[1] + kernel->height-1)*sizeof(CLPixelPacket),NULL);
462 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++, kernel->width*kernel->height*sizeof(float),NULL);
cristyf034abb2013-11-24 14:16:14 +0000463 if (clStatus != CL_SUCCESS)
464 {
cristy0c832c62014-03-07 22:21:04 +0000465 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000466 goto cleanup;
467 }
468
469 /* pad the global size to a multiple of the local work size dimension */
dirk8a5cf512014-07-28 20:16:27 +0000470 global_work_size[0] = ((image->columns + localGroupSize[0] - 1)/localGroupSize[0] ) * localGroupSize[0] ;
471 global_work_size[1] = ((image->rows + localGroupSize[1] - 1)/localGroupSize[1]) * localGroupSize[1];
cristyf034abb2013-11-24 14:16:14 +0000472
473 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +0000474 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, clkernel, 2, NULL, global_work_size, localGroupSize, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +0000475 if (clStatus != CL_SUCCESS)
476 {
cristy0c832c62014-03-07 22:21:04 +0000477 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000478 goto cleanup;
479 }
480 }
481 else
482 {
483 /* get the OpenCL kernel */
484 clkernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Convolve");
485 if (clkernel == NULL)
486 {
cristya22457d2013-12-07 14:03:06 +0000487 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000488 goto cleanup;
489 }
490
491 /* set the kernel arguments */
492 i = 0;
dirk8a5cf512014-07-28 20:16:27 +0000493 clStatus =clEnv->library->clSetKernelArg(clkernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +0000494 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(cl_mem),(void *)&filteredImageBuffer);
dirkb0d783f2014-08-31 10:48:05 +0000495 imageWidth = (unsigned int) image->columns;
496 imageHeight = (unsigned int) image->rows;
cristy0c832c62014-03-07 22:21:04 +0000497 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&imageWidth);
498 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&imageHeight);
499 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(cl_mem),(void *)&convolutionKernel);
dirkb0d783f2014-08-31 10:48:05 +0000500 filterWidth = (unsigned int) kernel->width;
501 filterHeight = (unsigned int) kernel->height;
cristy0c832c62014-03-07 22:21:04 +0000502 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&filterWidth);
503 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&filterHeight);
dirk8a5cf512014-07-28 20:16:27 +0000504 matte = (image->alpha_trait==BlendPixelTrait)?1:0;
cristy0c832c62014-03-07 22:21:04 +0000505 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&matte);
506 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(ChannelType),(void *)&channel);
cristyf034abb2013-11-24 14:16:14 +0000507 if (clStatus != CL_SUCCESS)
508 {
cristy0c832c62014-03-07 22:21:04 +0000509 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000510 goto cleanup;
511 }
512
cristy0c832c62014-03-07 22:21:04 +0000513 localGroupSize[0] = 8;
514 localGroupSize[1] = 8;
dirk8a5cf512014-07-28 20:16:27 +0000515 global_work_size[0] = (image->columns + (localGroupSize[0]-1))/localGroupSize[0] * localGroupSize[0];
516 global_work_size[1] = (image->rows + (localGroupSize[1]-1))/localGroupSize[1] * localGroupSize[1];
cristy0c832c62014-03-07 22:21:04 +0000517 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, clkernel, 2, NULL, global_work_size, localGroupSize, 0, NULL, NULL);
518
cristyf034abb2013-11-24 14:16:14 +0000519 if (clStatus != CL_SUCCESS)
520 {
cristy0c832c62014-03-07 22:21:04 +0000521 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000522 goto cleanup;
523 }
524 }
cristy0c832c62014-03-07 22:21:04 +0000525 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +0000526
527 if (ALIGNED(filteredPixels,CLPixelPacket))
528 {
dirk8a5cf512014-07-28 20:16:27 +0000529 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +0000530 clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +0000531 }
532 else
533 {
dirk8a5cf512014-07-28 20:16:27 +0000534 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +0000535 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +0000536 }
537 if (clStatus != CL_SUCCESS)
538 {
cristya22457d2013-12-07 14:03:06 +0000539 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000540 goto cleanup;
541 }
542
dirk832becc2014-08-04 19:44:34 +0000543 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
544
545cleanup:
546 OpenCLLogException(__FUNCTION__,__LINE__,exception);
547
548 image_view=DestroyCacheView(image_view);
549 if (filteredImage_view != NULL)
dirk8a5cf512014-07-28 20:16:27 +0000550 filteredImage_view=DestroyCacheView(filteredImage_view);
cristyf034abb2013-11-24 14:16:14 +0000551
dirk8a5cf512014-07-28 20:16:27 +0000552 if (imageBuffer != NULL)
553 clEnv->library->clReleaseMemObject(imageBuffer);
cristyf034abb2013-11-24 14:16:14 +0000554
555 if (filteredImageBuffer != NULL)
cristy0c832c62014-03-07 22:21:04 +0000556 clEnv->library->clReleaseMemObject(filteredImageBuffer);
cristyf034abb2013-11-24 14:16:14 +0000557
558 if (convolutionKernel != NULL)
cristy0c832c62014-03-07 22:21:04 +0000559 clEnv->library->clReleaseMemObject(convolutionKernel);
cristyf034abb2013-11-24 14:16:14 +0000560
561 if (clkernel != NULL)
562 RelinquishOpenCLKernel(clEnv, clkernel);
563
564 if (queue != NULL)
565 RelinquishOpenCLCommandQueue(clEnv, queue);
566
567 if (outputReady == MagickFalse)
568 {
569 if (filteredImage != NULL)
570 {
571 DestroyImage(filteredImage);
572 filteredImage = NULL;
573 }
574 }
575
dirk8a5cf512014-07-28 20:16:27 +0000576 return(filteredImage);
cristyf034abb2013-11-24 14:16:14 +0000577}
578
dirk832becc2014-08-04 19:44:34 +0000579MagickExport Image *AccelerateConvolveImageChannel(const Image *image,
580 const ChannelType channel,const KernelInfo *kernel,ExceptionInfo *exception)
581{
582 Image
583 *filteredImage;
584
585 assert(image != NULL);
586 assert(kernel != (KernelInfo *) NULL);
587 assert(exception != (ExceptionInfo *) NULL);
588
589 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
590 (checkAccelerateCondition(image, channel) == MagickFalse))
591 return NULL;
592
593 filteredImage=ComputeConvolveImage(image, channel, kernel, exception);
594 return(filteredImage);
cristyf034abb2013-11-24 14:16:14 +0000595}
596
dirk832becc2014-08-04 19:44:34 +0000597static MagickBooleanType ComputeFunctionImage(Image *image,
598 const ChannelType channel,const MagickFunction function,
599 const size_t number_parameters,const double *parameters,
600 ExceptionInfo *exception)
601{
602 CacheView
603 *image_view;
604
605 cl_command_queue
606 queue;
607
608 cl_context
609 context;
610
611 cl_int
612 clStatus;
613
614 cl_kernel
615 clkernel;
616
617 cl_mem
618 imageBuffer,
619 parametersBuffer;
620
621 cl_mem_flags
622 mem_flags;
623
624 float
625 *parametersBufferPtr;
626
627 MagickBooleanType
628 status;
629
630 MagickCLEnv
631 clEnv;
632
633 MagickSizeType
634 length;
635
636 size_t
637 globalWorkSize[2];
638
639 unsigned int
640 i;
641
642 void
643 *pixels;
644
645 status = MagickFalse;
646
647 context = NULL;
648 clkernel = NULL;
649 queue = NULL;
650 imageBuffer = NULL;
651 parametersBuffer = NULL;
652
653 clEnv = GetDefaultOpenCLEnv();
654 context = GetOpenCLContext(clEnv);
655
656 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +0000657 pixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +0000658 if (pixels == (void *) NULL)
659 {
cristya22457d2013-12-07 14:03:06 +0000660 (void) OpenCLThrowMagickException(exception, GetMagickModule(), CacheWarning,
cristyf034abb2013-11-24 14:16:14 +0000661 "GetPixelCachePixels failed.",
662 "'%s'", image->filename);
663 goto cleanup;
664 }
665
666
667 if (ALIGNED(pixels,CLPixelPacket))
668 {
669 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
670 }
671 else
672 {
673 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
674 }
675 /* create a CL buffer from image pixel buffer */
676 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +0000677 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)pixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +0000678 if (clStatus != CL_SUCCESS)
679 {
cristy0c832c62014-03-07 22:21:04 +0000680 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +0000681 goto cleanup;
682 }
683
cristy0c832c62014-03-07 22:21:04 +0000684 parametersBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, number_parameters * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +0000685 if (clStatus != CL_SUCCESS)
686 {
cristy0c832c62014-03-07 22:21:04 +0000687 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +0000688 goto cleanup;
689 }
690
691 queue = AcquireOpenCLCommandQueue(clEnv);
692
cristy0c832c62014-03-07 22:21:04 +0000693 parametersBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, parametersBuffer, CL_TRUE, CL_MAP_WRITE, 0, number_parameters * sizeof(float)
cristyf034abb2013-11-24 14:16:14 +0000694 , 0, NULL, NULL, &clStatus);
695 if (clStatus != CL_SUCCESS)
696 {
cristy0c832c62014-03-07 22:21:04 +0000697 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +0000698 goto cleanup;
699 }
700 for (i = 0; i < number_parameters; i++)
701 {
702 parametersBufferPtr[i] = (float)parameters[i];
703 }
cristy0c832c62014-03-07 22:21:04 +0000704 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, parametersBuffer, parametersBufferPtr, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +0000705 if (clStatus != CL_SUCCESS)
706 {
cristy0c832c62014-03-07 22:21:04 +0000707 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000708 goto cleanup;
709 }
cristy0c832c62014-03-07 22:21:04 +0000710 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +0000711
712 clkernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "FunctionImage");
713 if (clkernel == NULL)
714 {
cristya22457d2013-12-07 14:03:06 +0000715 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000716 goto cleanup;
717 }
718
719 /* set the kernel arguments */
720 i = 0;
cristy0c832c62014-03-07 22:21:04 +0000721 clStatus =clEnv->library->clSetKernelArg(clkernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
722 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(ChannelType),(void *)&channel);
723 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(MagickFunction),(void *)&function);
724 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&number_parameters);
725 clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(cl_mem),(void *)&parametersBuffer);
cristyf034abb2013-11-24 14:16:14 +0000726 if (clStatus != CL_SUCCESS)
727 {
cristy0c832c62014-03-07 22:21:04 +0000728 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000729 goto cleanup;
730 }
731
732 globalWorkSize[0] = image->columns;
733 globalWorkSize[1] = image->rows;
734 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +0000735 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, clkernel, 2, NULL, globalWorkSize, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +0000736 if (clStatus != CL_SUCCESS)
737 {
cristy0c832c62014-03-07 22:21:04 +0000738 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000739 goto cleanup;
740 }
cristy0c832c62014-03-07 22:21:04 +0000741 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +0000742
743
744 if (ALIGNED(pixels,CLPixelPacket))
745 {
746 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +0000747 clEnv->library->clEnqueueMapBuffer(queue, imageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +0000748 }
749 else
750 {
751 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +0000752 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), pixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +0000753 }
754 if (clStatus != CL_SUCCESS)
755 {
cristya22457d2013-12-07 14:03:06 +0000756 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000757 goto cleanup;
758 }
dirk832becc2014-08-04 19:44:34 +0000759 status=SyncCacheViewAuthenticPixels(image_view,exception);
760
761cleanup:
762 OpenCLLogException(__FUNCTION__,__LINE__,exception);
763
dirk8a5cf512014-07-28 20:16:27 +0000764 image_view=DestroyCacheView(image_view);
cristyf034abb2013-11-24 14:16:14 +0000765
766 if (clkernel != NULL) RelinquishOpenCLKernel(clEnv, clkernel);
767 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
cristy0c832c62014-03-07 22:21:04 +0000768 if (imageBuffer != NULL) clEnv->library->clReleaseMemObject(imageBuffer);
769 if (parametersBuffer != NULL) clEnv->library->clReleaseMemObject(parametersBuffer);
cristyf034abb2013-11-24 14:16:14 +0000770
dirk8a5cf512014-07-28 20:16:27 +0000771 return(status);
cristyf034abb2013-11-24 14:16:14 +0000772}
773
dirk832becc2014-08-04 19:44:34 +0000774MagickExport MagickBooleanType AccelerateFunctionImage(Image *image,
775 const ChannelType channel,const MagickFunction function,
776 const size_t number_parameters,const double *parameters,
777 ExceptionInfo *exception)
778{
779 MagickBooleanType
780 status;
781
782 assert(image != NULL);
783 assert(exception != (ExceptionInfo *) NULL);
784
785 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
786 (checkAccelerateCondition(image, channel) == MagickFalse))
787 return(MagickFalse);
788
789 status=ComputeFunctionImage(image, channel, function, number_parameters, parameters, exception);
790 return(status);
cristyf034abb2013-11-24 14:16:14 +0000791}
792
dirk8a5cf512014-07-28 20:16:27 +0000793/*
794%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
795% %
796% %
797% %
798% B l u r I m a g e w i t h O p e n C L %
799% %
800% %
801% %
802%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
803%
804% BlurImage() blurs an image. We convolve the image with a Gaussian operator
805% of the given radius and standard deviation (sigma). For reasonable results,
806% the radius should be larger than sigma. Use a radius of 0 and BlurImage()
807% selects a suitable radius for you.
808%
809% The format of the BlurImage method is:
810%
811% Image *BlurImage(const Image *image,const double radius,
812% const double sigma,ExceptionInfo *exception)
813% Image *BlurImageChannel(const Image *image,const ChannelType channel,
814% const double radius,const double sigma,ExceptionInfo *exception)
815%
816% A description of each parameter follows:
817%
818% o image: the image.
819%
820% o channel: the channel type.
821%
822% o radius: the radius of the Gaussian, in pixels, not counting the center
823% pixel.
824%
825% o sigma: the standard deviation of the Gaussian, in pixels.
826%
827% o exception: return any errors or warnings in this structure.
828%
829*/
cristyf034abb2013-11-24 14:16:14 +0000830
dirk832becc2014-08-04 19:44:34 +0000831static Image *ComputeBlurImage(const Image* image,const ChannelType channel,
832 const double radius,const double sigma,ExceptionInfo *exception)
833{
834 CacheView
835 *filteredImage_view,
836 *image_view;
837
838 char
839 geometry[MaxTextExtent];
840
841 cl_command_queue
842 queue;
843
844 cl_context
845 context;
846
847 cl_int
848 clStatus;
849
850 cl_kernel
851 blurColumnKernel,
852 blurRowKernel;
853
854 cl_mem
855 filteredImageBuffer,
856 imageBuffer,
857 imageKernelBuffer,
858 tempImageBuffer;
859
860 cl_mem_flags
861 mem_flags;
862
863 const void
864 *inputPixels;
865
866 float
867 *kernelBufferPtr;
868
869 Image
870 *filteredImage;
871
872 MagickBooleanType
873 outputReady;
874
875 MagickCLEnv
876 clEnv;
877
878 MagickSizeType
879 length;
880
881 KernelInfo
882 *kernel;
883
884 unsigned int
885 i,
886 imageColumns,
887 imageRows,
888 kernelWidth;
889
890 void
891 *filteredPixels,
892 *hostPtr;
893
894 context = NULL;
895 filteredImage = NULL;
896 filteredImage_view = NULL;
897 imageBuffer = NULL;
898 tempImageBuffer = NULL;
899 filteredImageBuffer = NULL;
900 imageKernelBuffer = NULL;
901 blurRowKernel = NULL;
902 blurColumnKernel = NULL;
903 queue = NULL;
904 kernel = NULL;
905
906 outputReady = MagickFalse;
907
908 clEnv = GetDefaultOpenCLEnv();
909 context = GetOpenCLContext(clEnv);
910 queue = AcquireOpenCLCommandQueue(clEnv);
911
912 /* Create and initialize OpenCL buffers. */
913 {
914 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +0000915 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +0000916 if (inputPixels == (const void *) NULL)
917 {
dirk8a5cf512014-07-28 20:16:27 +0000918 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +0000919 goto cleanup;
920 }
921 /* If the host pointer is aligned to the size of CLPixelPacket,
922 then use the host buffer directly from the GPU; otherwise,
923 create a buffer on the GPU and copy the data over */
924 if (ALIGNED(inputPixels,CLPixelPacket))
925 {
926 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
927 }
928 else
929 {
930 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
931 }
932 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +0000933 length = image->columns * image->rows;
934 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +0000935 if (clStatus != CL_SUCCESS)
936 {
cristy0c832c62014-03-07 22:21:04 +0000937 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +0000938 goto cleanup;
939 }
940 }
941
942 /* create output */
943 {
dirk8a5cf512014-07-28 20:16:27 +0000944 filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristyf034abb2013-11-24 14:16:14 +0000945 assert(filteredImage != NULL);
dirk8a5cf512014-07-28 20:16:27 +0000946 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristyf034abb2013-11-24 14:16:14 +0000947 {
cristya22457d2013-12-07 14:03:06 +0000948 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +0000949 goto cleanup;
950 }
dirk832becc2014-08-04 19:44:34 +0000951 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +0000952 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristyf034abb2013-11-24 14:16:14 +0000953 if (filteredPixels == (void *) NULL)
954 {
cristya22457d2013-12-07 14:03:06 +0000955 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename);
cristyf034abb2013-11-24 14:16:14 +0000956 goto cleanup;
957 }
958
959 if (ALIGNED(filteredPixels,CLPixelPacket))
960 {
961 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
962 hostPtr = filteredPixels;
963 }
964 else
965 {
966 mem_flags = CL_MEM_WRITE_ONLY;
967 hostPtr = NULL;
968 }
969 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +0000970 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +0000971 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +0000972 if (clStatus != CL_SUCCESS)
973 {
cristy0c832c62014-03-07 22:21:04 +0000974 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +0000975 goto cleanup;
976 }
977 }
978
979 /* create processing kernel */
980 {
981 (void) FormatLocaleString(geometry,MaxTextExtent,"blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
982 kernel=AcquireKernelInfo(geometry);
983 if (kernel == (KernelInfo *) NULL)
984 {
cristya22457d2013-12-07 14:03:06 +0000985 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "MemoryAllocationFailed.",".");
cristyf034abb2013-11-24 14:16:14 +0000986 goto cleanup;
987 }
988
cristy0c832c62014-03-07 22:21:04 +0000989 imageKernelBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, kernel->width * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +0000990 if (clStatus != CL_SUCCESS)
991 {
cristy0c832c62014-03-07 22:21:04 +0000992 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +0000993 goto cleanup;
994 }
cristy0c832c62014-03-07 22:21:04 +0000995 kernelBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, imageKernelBuffer, CL_TRUE, CL_MAP_WRITE, 0, kernel->width * sizeof(float), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +0000996 if (clStatus != CL_SUCCESS)
997 {
cristy0c832c62014-03-07 22:21:04 +0000998 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +0000999 goto cleanup;
1000 }
1001
1002 for (i = 0; i < kernel->width; i++)
1003 {
1004 kernelBufferPtr[i] = (float) kernel->values[i];
1005 }
1006
cristy0c832c62014-03-07 22:21:04 +00001007 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, imageKernelBuffer, kernelBufferPtr, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00001008 if (clStatus != CL_SUCCESS)
1009 {
cristy0c832c62014-03-07 22:21:04 +00001010 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001011 goto cleanup;
1012 }
1013 }
1014
1015 {
1016
1017 /* create temp buffer */
1018 {
dirk8a5cf512014-07-28 20:16:27 +00001019 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00001020 tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length * 4 * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001021 if (clStatus != CL_SUCCESS)
1022 {
cristy0c832c62014-03-07 22:21:04 +00001023 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00001024 goto cleanup;
1025 }
1026 }
1027
1028 /* get the OpenCL kernels */
1029 {
1030 blurRowKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "BlurRow");
1031 if (blurRowKernel == NULL)
1032 {
cristya22457d2013-12-07 14:03:06 +00001033 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001034 goto cleanup;
1035 };
1036
1037 blurColumnKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "BlurColumn");
1038 if (blurColumnKernel == NULL)
1039 {
cristya22457d2013-12-07 14:03:06 +00001040 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001041 goto cleanup;
1042 };
1043 }
1044
1045 {
1046 /* need logic to decide this value */
1047 int chunkSize = 256;
1048
1049 {
dirkb0d783f2014-08-31 10:48:05 +00001050 imageColumns = (unsigned int) image->columns;
1051 imageRows = (unsigned int) image->rows;
cristyf034abb2013-11-24 14:16:14 +00001052
1053 /* set the kernel arguments */
1054 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00001055 clStatus=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00001056 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&tempImageBuffer);
1057 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(ChannelType),&channel);
1058 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&imageKernelBuffer);
dirkb0d783f2014-08-31 10:48:05 +00001059 kernelWidth = (unsigned int) kernel->width;
cristy0c832c62014-03-07 22:21:04 +00001060 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&kernelWidth);
1061 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&imageColumns);
1062 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&imageRows);
1063 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(CLPixelPacket)*(chunkSize+kernel->width),(void *)NULL);
cristyf034abb2013-11-24 14:16:14 +00001064 if (clStatus != CL_SUCCESS)
1065 {
cristy0c832c62014-03-07 22:21:04 +00001066 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001067 goto cleanup;
1068 }
1069 }
1070
1071 /* launch the kernel */
1072 {
1073 size_t gsize[2];
1074 size_t wsize[2];
1075
dirk8a5cf512014-07-28 20:16:27 +00001076 gsize[0] = chunkSize*((image->columns+chunkSize-1)/chunkSize);
1077 gsize[1] = image->rows;
cristyf034abb2013-11-24 14:16:14 +00001078 wsize[0] = chunkSize;
1079 wsize[1] = 1;
1080
cristy0c832c62014-03-07 22:21:04 +00001081 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, blurRowKernel, 2, NULL, gsize, wsize, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00001082 if (clStatus != CL_SUCCESS)
1083 {
cristy0c832c62014-03-07 22:21:04 +00001084 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001085 goto cleanup;
1086 }
cristy0c832c62014-03-07 22:21:04 +00001087 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00001088 }
1089 }
1090
1091 {
1092 /* need logic to decide this value */
1093 int chunkSize = 256;
1094
1095 {
dirkb0d783f2014-08-31 10:48:05 +00001096 imageColumns = (unsigned int) image->columns;
1097 imageRows = (unsigned int) image->rows;
cristyf034abb2013-11-24 14:16:14 +00001098
1099 /* set the kernel arguments */
1100 i = 0;
cristy0c832c62014-03-07 22:21:04 +00001101 clStatus=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(cl_mem),(void *)&tempImageBuffer);
1102 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(cl_mem),(void *)&filteredImageBuffer);
1103 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(ChannelType),&channel);
1104 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(cl_mem),(void *)&imageKernelBuffer);
dirkb0d783f2014-08-31 10:48:05 +00001105 kernelWidth = (unsigned int) kernel->width;
cristy0c832c62014-03-07 22:21:04 +00001106 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(unsigned int),(void *)&kernelWidth);
1107 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(unsigned int),(void *)&imageColumns);
1108 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(unsigned int),(void *)&imageRows);
1109 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(cl_float4)*(chunkSize+kernel->width),(void *)NULL);
cristyf034abb2013-11-24 14:16:14 +00001110 if (clStatus != CL_SUCCESS)
1111 {
cristy0c832c62014-03-07 22:21:04 +00001112 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001113 goto cleanup;
1114 }
1115 }
1116
1117 /* launch the kernel */
1118 {
1119 size_t gsize[2];
1120 size_t wsize[2];
1121
dirk8a5cf512014-07-28 20:16:27 +00001122 gsize[0] = image->columns;
1123 gsize[1] = chunkSize*((image->rows+chunkSize-1)/chunkSize);
cristyf034abb2013-11-24 14:16:14 +00001124 wsize[0] = 1;
1125 wsize[1] = chunkSize;
1126
cristy0c832c62014-03-07 22:21:04 +00001127 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, blurColumnKernel, 2, NULL, gsize, wsize, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00001128 if (clStatus != CL_SUCCESS)
1129 {
cristy0c832c62014-03-07 22:21:04 +00001130 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001131 goto cleanup;
1132 }
cristy0c832c62014-03-07 22:21:04 +00001133 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00001134 }
1135 }
1136
1137 }
1138
1139 /* get result */
1140 if (ALIGNED(filteredPixels,CLPixelPacket))
1141 {
dirk8a5cf512014-07-28 20:16:27 +00001142 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00001143 clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001144 }
1145 else
1146 {
dirk8a5cf512014-07-28 20:16:27 +00001147 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00001148 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00001149 }
1150 if (clStatus != CL_SUCCESS)
1151 {
cristya22457d2013-12-07 14:03:06 +00001152 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001153 goto cleanup;
1154 }
1155
dirk832becc2014-08-04 19:44:34 +00001156 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
1157
1158cleanup:
1159 OpenCLLogException(__FUNCTION__,__LINE__,exception);
1160
1161 image_view=DestroyCacheView(image_view);
1162 if (filteredImage_view != NULL)
1163 filteredImage_view=DestroyCacheView(filteredImage_view);
1164
dirk8a5cf512014-07-28 20:16:27 +00001165 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00001166 if (tempImageBuffer!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer);
1167 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
1168 if (imageKernelBuffer!=NULL) clEnv->library->clReleaseMemObject(imageKernelBuffer);
cristyf034abb2013-11-24 14:16:14 +00001169 if (blurRowKernel!=NULL) RelinquishOpenCLKernel(clEnv, blurRowKernel);
1170 if (blurColumnKernel!=NULL) RelinquishOpenCLKernel(clEnv, blurColumnKernel);
1171 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
1172 if (kernel!=NULL) DestroyKernelInfo(kernel);
dirk8a5cf512014-07-28 20:16:27 +00001173 if (outputReady == MagickFalse && filteredImage != NULL)
dirk832becc2014-08-04 19:44:34 +00001174 filteredImage=DestroyImage(filteredImage);
dirk8a5cf512014-07-28 20:16:27 +00001175 return(filteredImage);
cristyf034abb2013-11-24 14:16:14 +00001176}
1177
dirk832becc2014-08-04 19:44:34 +00001178static Image* ComputeBlurImageSection(const Image* image,
1179 const ChannelType channel,const double radius,const double sigma,
1180 ExceptionInfo *exception)
1181{
1182 CacheView
1183 *filteredImage_view,
1184 *image_view;
1185
1186 char
1187 geometry[MaxTextExtent];
1188
1189 cl_command_queue
1190 queue;
1191
1192 cl_int
1193 clStatus;
1194
1195 cl_kernel
1196 blurColumnKernel,
1197 blurRowKernel;
1198
1199 cl_mem
1200 imageBuffer,
1201 tempImageBuffer,
1202 filteredImageBuffer,
1203 imageKernelBuffer;
1204
1205 cl_mem_flags
1206 mem_flags;
1207
1208 cl_context
1209 context;
1210
1211 const void
1212 *inputPixels;
1213
1214 float
1215 *kernelBufferPtr;
1216
1217 Image
1218 *filteredImage;
1219
1220 KernelInfo
1221 *kernel;
1222
1223 MagickBooleanType
1224 outputReady;
1225
1226 MagickCLEnv
1227 clEnv;
1228
1229 MagickSizeType
1230 length;
1231
1232 unsigned int
1233 i,
1234 imageColumns,
1235 imageRows,
1236 kernelWidth;
1237
1238 void
1239 *filteredPixels,
1240 *hostPtr;
1241
1242 context = NULL;
1243 filteredImage = NULL;
1244 filteredImage_view = NULL;
1245 imageBuffer = NULL;
1246 tempImageBuffer = NULL;
1247 filteredImageBuffer = NULL;
1248 imageKernelBuffer = NULL;
1249 blurRowKernel = NULL;
1250 blurColumnKernel = NULL;
1251 queue = NULL;
1252 kernel = NULL;
1253
1254 outputReady = MagickFalse;
1255
1256 clEnv = GetDefaultOpenCLEnv();
1257 context = GetOpenCLContext(clEnv);
1258 queue = AcquireOpenCLCommandQueue(clEnv);
1259
1260 /* Create and initialize OpenCL buffers. */
1261 {
1262 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00001263 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00001264 if (inputPixels == (const void *) NULL)
1265 {
dirk8a5cf512014-07-28 20:16:27 +00001266 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +00001267 goto cleanup;
1268 }
1269 /* If the host pointer is aligned to the size of CLPixelPacket,
1270 then use the host buffer directly from the GPU; otherwise,
1271 create a buffer on the GPU and copy the data over */
1272 if (ALIGNED(inputPixels,CLPixelPacket))
1273 {
1274 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
1275 }
1276 else
1277 {
1278 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
1279 }
1280 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00001281 length = image->columns * image->rows;
1282 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001283 if (clStatus != CL_SUCCESS)
1284 {
cristy0c832c62014-03-07 22:21:04 +00001285 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00001286 goto cleanup;
1287 }
1288 }
1289
1290 /* create output */
1291 {
dirk8a5cf512014-07-28 20:16:27 +00001292 filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristyf034abb2013-11-24 14:16:14 +00001293 assert(filteredImage != NULL);
dirk8a5cf512014-07-28 20:16:27 +00001294 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristyf034abb2013-11-24 14:16:14 +00001295 {
cristya22457d2013-12-07 14:03:06 +00001296 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001297 goto cleanup;
1298 }
dirk832becc2014-08-04 19:44:34 +00001299 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +00001300 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00001301 if (filteredPixels == (void *) NULL)
1302 {
cristya22457d2013-12-07 14:03:06 +00001303 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename);
cristyf034abb2013-11-24 14:16:14 +00001304 goto cleanup;
1305 }
1306
1307 if (ALIGNED(filteredPixels,CLPixelPacket))
1308 {
1309 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
1310 hostPtr = filteredPixels;
1311 }
1312 else
1313 {
1314 mem_flags = CL_MEM_WRITE_ONLY;
1315 hostPtr = NULL;
1316 }
1317 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00001318 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00001319 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001320 if (clStatus != CL_SUCCESS)
1321 {
cristy0c832c62014-03-07 22:21:04 +00001322 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00001323 goto cleanup;
1324 }
1325 }
1326
1327 /* create processing kernel */
1328 {
1329 (void) FormatLocaleString(geometry,MaxTextExtent,"blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
1330 kernel=AcquireKernelInfo(geometry);
1331 if (kernel == (KernelInfo *) NULL)
1332 {
cristya22457d2013-12-07 14:03:06 +00001333 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "MemoryAllocationFailed.",".");
cristyf034abb2013-11-24 14:16:14 +00001334 goto cleanup;
1335 }
1336
cristy0c832c62014-03-07 22:21:04 +00001337 imageKernelBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, kernel->width * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001338 if (clStatus != CL_SUCCESS)
1339 {
cristy0c832c62014-03-07 22:21:04 +00001340 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00001341 goto cleanup;
1342 }
cristy0c832c62014-03-07 22:21:04 +00001343 kernelBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, imageKernelBuffer, CL_TRUE, CL_MAP_WRITE, 0, kernel->width * sizeof(float), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001344 if (clStatus != CL_SUCCESS)
1345 {
cristy0c832c62014-03-07 22:21:04 +00001346 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00001347 goto cleanup;
1348 }
1349
1350 for (i = 0; i < kernel->width; i++)
1351 {
1352 kernelBufferPtr[i] = (float) kernel->values[i];
1353 }
1354
cristy0c832c62014-03-07 22:21:04 +00001355 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, imageKernelBuffer, kernelBufferPtr, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00001356 if (clStatus != CL_SUCCESS)
1357 {
cristy0c832c62014-03-07 22:21:04 +00001358 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001359 goto cleanup;
1360 }
1361 }
1362
1363 {
1364 unsigned int offsetRows;
1365 unsigned int sec;
1366
1367 /* create temp buffer */
1368 {
dirk8a5cf512014-07-28 20:16:27 +00001369 length = image->columns * (image->rows / 2 + 1 + (kernel->width-1) / 2);
cristy0c832c62014-03-07 22:21:04 +00001370 tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length * 4 * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001371 if (clStatus != CL_SUCCESS)
1372 {
cristy0c832c62014-03-07 22:21:04 +00001373 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00001374 goto cleanup;
1375 }
1376 }
1377
1378 /* get the OpenCL kernels */
1379 {
1380 blurRowKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "BlurRowSection");
1381 if (blurRowKernel == NULL)
1382 {
cristya22457d2013-12-07 14:03:06 +00001383 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001384 goto cleanup;
1385 };
1386
1387 blurColumnKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "BlurColumnSection");
1388 if (blurColumnKernel == NULL)
1389 {
cristya22457d2013-12-07 14:03:06 +00001390 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001391 goto cleanup;
1392 };
1393 }
1394
1395 for (sec = 0; sec < 2; sec++)
1396 {
1397 {
1398 /* need logic to decide this value */
1399 int chunkSize = 256;
1400
1401 {
dirkb0d783f2014-08-31 10:48:05 +00001402 imageColumns = (unsigned int) image->columns;
cristyf034abb2013-11-24 14:16:14 +00001403 if (sec == 0)
dirkb0d783f2014-08-31 10:48:05 +00001404 imageRows = (unsigned int) (image->rows / 2 + (kernel->width-1) / 2);
cristyf034abb2013-11-24 14:16:14 +00001405 else
dirkb0d783f2014-08-31 10:48:05 +00001406 imageRows = (unsigned int) ((image->rows - image->rows / 2) + (kernel->width-1) / 2);
cristyf034abb2013-11-24 14:16:14 +00001407
dirkb0d783f2014-08-31 10:48:05 +00001408 offsetRows = (unsigned int) (sec * image->rows / 2);
cristyf034abb2013-11-24 14:16:14 +00001409
dirkb0d783f2014-08-31 10:48:05 +00001410 kernelWidth = (unsigned int) kernel->width;
cristyf034abb2013-11-24 14:16:14 +00001411
1412 /* set the kernel arguments */
1413 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00001414 clStatus=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00001415 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&tempImageBuffer);
1416 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(ChannelType),&channel);
1417 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&imageKernelBuffer);
1418 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&kernelWidth);
1419 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&imageColumns);
1420 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&imageRows);
1421 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(CLPixelPacket)*(chunkSize+kernel->width),(void *)NULL);
1422 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&offsetRows);
1423 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&sec);
cristyf034abb2013-11-24 14:16:14 +00001424 if (clStatus != CL_SUCCESS)
1425 {
cristy0c832c62014-03-07 22:21:04 +00001426 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001427 goto cleanup;
1428 }
1429 }
1430
1431 /* launch the kernel */
1432 {
1433 size_t gsize[2];
1434 size_t wsize[2];
1435
1436 gsize[0] = chunkSize*((imageColumns+chunkSize-1)/chunkSize);
1437 gsize[1] = imageRows;
1438 wsize[0] = chunkSize;
1439 wsize[1] = 1;
1440
cristy0c832c62014-03-07 22:21:04 +00001441 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, blurRowKernel, 2, NULL, gsize, wsize, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00001442 if (clStatus != CL_SUCCESS)
1443 {
cristy0c832c62014-03-07 22:21:04 +00001444 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001445 goto cleanup;
1446 }
cristy0c832c62014-03-07 22:21:04 +00001447 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00001448 }
1449 }
1450
1451 {
1452 /* need logic to decide this value */
1453 int chunkSize = 256;
1454
1455 {
dirkb0d783f2014-08-31 10:48:05 +00001456 imageColumns = (unsigned int) image->columns;
cristyf034abb2013-11-24 14:16:14 +00001457 if (sec == 0)
dirkb0d783f2014-08-31 10:48:05 +00001458 imageRows = (unsigned int) (image->rows / 2);
cristyf034abb2013-11-24 14:16:14 +00001459 else
dirkb0d783f2014-08-31 10:48:05 +00001460 imageRows = (unsigned int) ((image->rows - image->rows / 2));
cristyf034abb2013-11-24 14:16:14 +00001461
dirkb0d783f2014-08-31 10:48:05 +00001462 offsetRows = (unsigned int) (sec * image->rows / 2);
cristyf034abb2013-11-24 14:16:14 +00001463
dirkb0d783f2014-08-31 10:48:05 +00001464 kernelWidth = (unsigned int) kernel->width;
cristyf034abb2013-11-24 14:16:14 +00001465
1466 /* set the kernel arguments */
1467 i = 0;
cristy0c832c62014-03-07 22:21:04 +00001468 clStatus=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(cl_mem),(void *)&tempImageBuffer);
1469 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(cl_mem),(void *)&filteredImageBuffer);
1470 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(ChannelType),&channel);
1471 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(cl_mem),(void *)&imageKernelBuffer);
1472 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(unsigned int),(void *)&kernelWidth);
1473 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(unsigned int),(void *)&imageColumns);
1474 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(unsigned int),(void *)&imageRows);
1475 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(cl_float4)*(chunkSize+kernel->width),(void *)NULL);
1476 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(unsigned int),(void *)&offsetRows);
1477 clStatus|=clEnv->library->clSetKernelArg(blurColumnKernel,i++,sizeof(unsigned int),(void *)&sec);
cristyf034abb2013-11-24 14:16:14 +00001478 if (clStatus != CL_SUCCESS)
1479 {
cristy0c832c62014-03-07 22:21:04 +00001480 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001481 goto cleanup;
1482 }
1483 }
1484
1485 /* launch the kernel */
1486 {
1487 size_t gsize[2];
1488 size_t wsize[2];
1489
1490 gsize[0] = imageColumns;
1491 gsize[1] = chunkSize*((imageRows+chunkSize-1)/chunkSize);
1492 wsize[0] = 1;
1493 wsize[1] = chunkSize;
1494
cristy0c832c62014-03-07 22:21:04 +00001495 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, blurColumnKernel, 2, NULL, gsize, wsize, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00001496 if (clStatus != CL_SUCCESS)
1497 {
cristy0c832c62014-03-07 22:21:04 +00001498 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001499 goto cleanup;
1500 }
cristy0c832c62014-03-07 22:21:04 +00001501 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00001502 }
1503 }
1504 }
1505
1506 }
1507
1508 /* get result */
1509 if (ALIGNED(filteredPixels,CLPixelPacket))
1510 {
dirk8a5cf512014-07-28 20:16:27 +00001511 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00001512 clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001513 }
1514 else
1515 {
dirk8a5cf512014-07-28 20:16:27 +00001516 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00001517 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00001518 }
1519 if (clStatus != CL_SUCCESS)
1520 {
cristya22457d2013-12-07 14:03:06 +00001521 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001522 goto cleanup;
1523 }
1524
dirk832becc2014-08-04 19:44:34 +00001525 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
1526
1527cleanup:
1528 OpenCLLogException(__FUNCTION__,__LINE__,exception);
1529
1530 image_view=DestroyCacheView(image_view);
1531 if (filteredImage_view != NULL)
1532 filteredImage_view=DestroyCacheView(filteredImage_view);
1533
dirk8a5cf512014-07-28 20:16:27 +00001534 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00001535 if (tempImageBuffer!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer);
1536 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
1537 if (imageKernelBuffer!=NULL) clEnv->library->clReleaseMemObject(imageKernelBuffer);
cristyf034abb2013-11-24 14:16:14 +00001538 if (blurRowKernel!=NULL) RelinquishOpenCLKernel(clEnv, blurRowKernel);
1539 if (blurColumnKernel!=NULL) RelinquishOpenCLKernel(clEnv, blurColumnKernel);
1540 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
1541 if (kernel!=NULL) DestroyKernelInfo(kernel);
1542 if (outputReady == MagickFalse)
1543 {
1544 if (filteredImage != NULL)
1545 {
1546 DestroyImage(filteredImage);
1547 filteredImage = NULL;
1548 }
1549 }
1550 return filteredImage;
1551}
1552
dirk859503c2014-08-13 20:39:42 +00001553static Image *ComputeUnsharpMaskImageSingle(const Image *image,
1554 const ChannelType channel,const double radius,const double sigma,
1555 const double gain,const double threshold,int blurOnly, ExceptionInfo *exception);
1556
1557static Image* ComputeBlurImageSingle(const Image* image,
1558 const ChannelType channel,const double radius,const double sigma,
1559 ExceptionInfo *exception)
1560{
1561 return ComputeUnsharpMaskImageSingle(image, channel, radius, sigma, 0.0, 0.0, 1, exception);
1562}
1563
dirk832becc2014-08-04 19:44:34 +00001564MagickExport Image* AccelerateBlurImage(const Image *image,
1565 const ChannelType channel,const double radius,const double sigma,
1566 ExceptionInfo *exception)
1567{
1568 Image
1569 *filteredImage;
1570
1571 assert(image != NULL);
1572 assert(exception != (ExceptionInfo *) NULL);
1573
1574 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
1575 (checkAccelerateCondition(image, channel) == MagickFalse))
1576 return NULL;
1577
dirk859503c2014-08-13 20:39:42 +00001578 if (radius < 12.1)
1579 filteredImage=ComputeBlurImageSingle(image, channel, radius, sigma, exception);
1580 else if (splitImage(image) && (image->rows / 2 > radius))
dirk832becc2014-08-04 19:44:34 +00001581 filteredImage=ComputeBlurImageSection(image, channel, radius, sigma, exception);
1582 else
1583 filteredImage=ComputeBlurImage(image, channel, radius, sigma, exception);
1584
1585 return(filteredImage);
dirk8a5cf512014-07-28 20:16:27 +00001586}
1587
cristyf034abb2013-11-24 14:16:14 +00001588/*
1589%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1590% %
1591% %
1592% %
dirk8a5cf512014-07-28 20:16:27 +00001593% R o t a t i o n a l B l u r I m a g e w i t h O p e n C L %
cristyf034abb2013-11-24 14:16:14 +00001594% %
1595% %
1596% %
1597%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1598%
dirk8a5cf512014-07-28 20:16:27 +00001599% RotationalBlurImage() applies a rotational blur to the image.
cristyf034abb2013-11-24 14:16:14 +00001600%
dirk8a5cf512014-07-28 20:16:27 +00001601% Andrew Protano contributed this effect.
cristyf034abb2013-11-24 14:16:14 +00001602%
dirk8a5cf512014-07-28 20:16:27 +00001603% The format of the RotationalBlurImage method is:
1604%
1605% Image *RotationalBlurImage(const Image *image,const double angle,
1606% ExceptionInfo *exception)
1607% Image *RotationalBlurImageChannel(const Image *image,const ChannelType channel,
1608% const double angle,ExceptionInfo *exception)
cristyf034abb2013-11-24 14:16:14 +00001609%
1610% A description of each parameter follows:
1611%
1612% o image: the image.
1613%
1614% o channel: the channel type.
1615%
dirk8a5cf512014-07-28 20:16:27 +00001616% o angle: the angle of the rotational blur.
cristyf034abb2013-11-24 14:16:14 +00001617%
1618% o exception: return any errors or warnings in this structure.
1619%
1620*/
1621
dirk832becc2014-08-04 19:44:34 +00001622static Image* ComputeRotationalBlurImage(const Image *image,
1623 const ChannelType channel,const double angle,ExceptionInfo *exception)
1624{
1625 CacheView
1626 *image_view,
1627 *filteredImage_view;
1628
1629 cl_command_queue
1630 queue;
1631
1632 cl_context
1633 context;
1634
1635 cl_float2
1636 blurCenter;
1637
1638 cl_float4
1639 biasPixel;
1640
1641 cl_int
1642 clStatus;
1643
1644 cl_mem
1645 cosThetaBuffer,
1646 filteredImageBuffer,
1647 imageBuffer,
1648 sinThetaBuffer;
1649
1650 cl_mem_flags
1651 mem_flags;
1652
1653 cl_kernel
1654 rotationalBlurKernel;
1655
1656 const void
1657 *inputPixels;
1658
1659 float
1660 blurRadius,
1661 *cosThetaPtr,
1662 offset,
1663 *sinThetaPtr,
1664 theta;
1665
1666 Image
1667 *filteredImage;
1668
1669 MagickBooleanType
1670 outputReady;
1671
1672 MagickCLEnv
1673 clEnv;
1674
1675 PixelInfo
1676 bias;
1677
1678 MagickSizeType
1679 length;
1680
1681 size_t
1682 global_work_size[2];
1683
1684 unsigned int
1685 cossin_theta_size,
1686 i,
1687 matte;
1688
1689 void
1690 *filteredPixels,
1691 *hostPtr;
1692
1693 outputReady = MagickFalse;
1694 context = NULL;
1695 filteredImage = NULL;
1696 filteredImage_view = NULL;
1697 imageBuffer = NULL;
1698 filteredImageBuffer = NULL;
1699 sinThetaBuffer = NULL;
1700 cosThetaBuffer = NULL;
1701 queue = NULL;
1702 rotationalBlurKernel = NULL;
1703
1704
1705 clEnv = GetDefaultOpenCLEnv();
1706 context = GetOpenCLContext(clEnv);
1707
1708
1709 /* Create and initialize OpenCL buffers. */
1710
1711 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00001712 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00001713 if (inputPixels == (const void *) NULL)
1714 {
dirk8a5cf512014-07-28 20:16:27 +00001715 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +00001716 goto cleanup;
1717 }
1718
1719 /* If the host pointer is aligned to the size of CLPixelPacket,
1720 then use the host buffer directly from the GPU; otherwise,
1721 create a buffer on the GPU and copy the data over */
1722 if (ALIGNED(inputPixels,CLPixelPacket))
1723 {
1724 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
1725 }
1726 else
1727 {
1728 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
1729 }
1730 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00001731 length = image->columns * image->rows;
1732 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001733 if (clStatus != CL_SUCCESS)
1734 {
cristy0c832c62014-03-07 22:21:04 +00001735 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00001736 goto cleanup;
1737 }
1738
1739
dirk8a5cf512014-07-28 20:16:27 +00001740 filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristyf034abb2013-11-24 14:16:14 +00001741 assert(filteredImage != NULL);
dirk8a5cf512014-07-28 20:16:27 +00001742 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristyf034abb2013-11-24 14:16:14 +00001743 {
cristya22457d2013-12-07 14:03:06 +00001744 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001745 goto cleanup;
1746 }
dirk832becc2014-08-04 19:44:34 +00001747 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +00001748 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00001749 if (filteredPixels == (void *) NULL)
1750 {
cristya22457d2013-12-07 14:03:06 +00001751 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename);
cristyf034abb2013-11-24 14:16:14 +00001752 goto cleanup;
1753 }
1754
1755 if (ALIGNED(filteredPixels,CLPixelPacket))
1756 {
1757 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
1758 hostPtr = filteredPixels;
1759 }
1760 else
1761 {
1762 mem_flags = CL_MEM_WRITE_ONLY;
1763 hostPtr = NULL;
1764 }
1765 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00001766 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00001767 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001768 if (clStatus != CL_SUCCESS)
1769 {
cristy0c832c62014-03-07 22:21:04 +00001770 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00001771 goto cleanup;
1772 }
1773
dirk8a5cf512014-07-28 20:16:27 +00001774 blurCenter.s[0] = (float) (image->columns-1)/2.0;
1775 blurCenter.s[1] = (float) (image->rows-1)/2.0;
cristyf034abb2013-11-24 14:16:14 +00001776 blurRadius=hypot(blurCenter.s[0],blurCenter.s[1]);
1777 cossin_theta_size=(unsigned int) fabs(4.0*DegreesToRadians(angle)*sqrt((double)blurRadius)+2UL);
1778
1779 /* create a buffer for sin_theta and cos_theta */
cristy0c832c62014-03-07 22:21:04 +00001780 sinThetaBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, cossin_theta_size * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001781 if (clStatus != CL_SUCCESS)
1782 {
cristy0c832c62014-03-07 22:21:04 +00001783 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00001784 goto cleanup;
1785 }
cristy0c832c62014-03-07 22:21:04 +00001786 cosThetaBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, cossin_theta_size * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001787 if (clStatus != CL_SUCCESS)
1788 {
cristy0c832c62014-03-07 22:21:04 +00001789 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00001790 goto cleanup;
1791 }
1792
1793
1794 queue = AcquireOpenCLCommandQueue(clEnv);
cristy0c832c62014-03-07 22:21:04 +00001795 sinThetaPtr = (float*) clEnv->library->clEnqueueMapBuffer(queue, sinThetaBuffer, CL_TRUE, CL_MAP_WRITE, 0, cossin_theta_size*sizeof(float), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001796 if (clStatus != CL_SUCCESS)
1797 {
cristya22457d2013-12-07 14:03:06 +00001798 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnqueuemapBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00001799 goto cleanup;
1800 }
1801
cristy0c832c62014-03-07 22:21:04 +00001802 cosThetaPtr = (float*) clEnv->library->clEnqueueMapBuffer(queue, cosThetaBuffer, CL_TRUE, CL_MAP_WRITE, 0, cossin_theta_size*sizeof(float), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001803 if (clStatus != CL_SUCCESS)
1804 {
cristya22457d2013-12-07 14:03:06 +00001805 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnqueuemapBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00001806 goto cleanup;
1807 }
1808
1809 theta=DegreesToRadians(angle)/(MagickRealType) (cossin_theta_size-1);
1810 offset=theta*(MagickRealType) (cossin_theta_size-1)/2.0;
1811 for (i=0; i < (ssize_t) cossin_theta_size; i++)
1812 {
1813 cosThetaPtr[i]=(float)cos((double) (theta*i-offset));
1814 sinThetaPtr[i]=(float)sin((double) (theta*i-offset));
1815 }
1816
cristy0c832c62014-03-07 22:21:04 +00001817 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, sinThetaBuffer, sinThetaPtr, 0, NULL, NULL);
1818 clStatus |= clEnv->library->clEnqueueUnmapMemObject(queue, cosThetaBuffer, cosThetaPtr, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00001819 if (clStatus != CL_SUCCESS)
1820 {
cristy0c832c62014-03-07 22:21:04 +00001821 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001822 goto cleanup;
1823 }
1824
1825 /* get the OpenCL kernel */
dirk6d612cf2014-03-13 21:17:23 +00001826 rotationalBlurKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "RotationalBlur");
1827 if (rotationalBlurKernel == NULL)
cristyf034abb2013-11-24 14:16:14 +00001828 {
cristya22457d2013-12-07 14:03:06 +00001829 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001830 goto cleanup;
1831 }
1832
1833
1834 /* set the kernel arguments */
1835 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00001836 clStatus=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
dirk6d612cf2014-03-13 21:17:23 +00001837 clStatus|=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(cl_mem),(void *)&filteredImageBuffer);
cristyf034abb2013-11-24 14:16:14 +00001838
dirk8a5cf512014-07-28 20:16:27 +00001839 GetPixelInfo(image,&bias);
cristyf034abb2013-11-24 14:16:14 +00001840 biasPixel.s[0] = bias.red;
1841 biasPixel.s[1] = bias.green;
1842 biasPixel.s[2] = bias.blue;
dirk8a5cf512014-07-28 20:16:27 +00001843 biasPixel.s[3] = bias.alpha;
dirk6d612cf2014-03-13 21:17:23 +00001844 clStatus|=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(cl_float4), &biasPixel);
1845 clStatus|=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(ChannelType), &channel);
cristyf034abb2013-11-24 14:16:14 +00001846
dirk8a5cf512014-07-28 20:16:27 +00001847 matte = (image->alpha_trait==BlendPixelTrait)?1:0;
dirk6d612cf2014-03-13 21:17:23 +00001848 clStatus|=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(unsigned int), &matte);
cristyf034abb2013-11-24 14:16:14 +00001849
dirk6d612cf2014-03-13 21:17:23 +00001850 clStatus=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(cl_float2), &blurCenter);
cristyf034abb2013-11-24 14:16:14 +00001851
dirk6d612cf2014-03-13 21:17:23 +00001852 clStatus|=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(cl_mem),(void *)&cosThetaBuffer);
1853 clStatus|=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(cl_mem),(void *)&sinThetaBuffer);
1854 clStatus|=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(unsigned int), &cossin_theta_size);
cristyf034abb2013-11-24 14:16:14 +00001855 if (clStatus != CL_SUCCESS)
1856 {
cristy0c832c62014-03-07 22:21:04 +00001857 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001858 goto cleanup;
1859 }
1860
1861
dirk8a5cf512014-07-28 20:16:27 +00001862 global_work_size[0] = image->columns;
1863 global_work_size[1] = image->rows;
cristyf034abb2013-11-24 14:16:14 +00001864 /* launch the kernel */
dirk6d612cf2014-03-13 21:17:23 +00001865 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, rotationalBlurKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00001866 if (clStatus != CL_SUCCESS)
1867 {
cristy0c832c62014-03-07 22:21:04 +00001868 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001869 goto cleanup;
1870 }
cristy0c832c62014-03-07 22:21:04 +00001871 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00001872
1873 if (ALIGNED(filteredPixels,CLPixelPacket))
1874 {
dirk8a5cf512014-07-28 20:16:27 +00001875 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00001876 clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00001877 }
1878 else
1879 {
dirk8a5cf512014-07-28 20:16:27 +00001880 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00001881 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00001882 }
1883 if (clStatus != CL_SUCCESS)
1884 {
cristya22457d2013-12-07 14:03:06 +00001885 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00001886 goto cleanup;
1887 }
dirk832becc2014-08-04 19:44:34 +00001888 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
1889
1890cleanup:
1891 OpenCLLogException(__FUNCTION__,__LINE__,exception);
1892
1893 image_view=DestroyCacheView(image_view);
1894 if (filteredImage_view != NULL)
dirk8a5cf512014-07-28 20:16:27 +00001895 filteredImage_view=DestroyCacheView(filteredImage_view);
cristya22457d2013-12-07 14:03:06 +00001896
cristy0c832c62014-03-07 22:21:04 +00001897 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
dirk8a5cf512014-07-28 20:16:27 +00001898 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00001899 if (sinThetaBuffer!=NULL) clEnv->library->clReleaseMemObject(sinThetaBuffer);
1900 if (cosThetaBuffer!=NULL) clEnv->library->clReleaseMemObject(cosThetaBuffer);
dirk6d612cf2014-03-13 21:17:23 +00001901 if (rotationalBlurKernel!=NULL) RelinquishOpenCLKernel(clEnv, rotationalBlurKernel);
cristyf034abb2013-11-24 14:16:14 +00001902 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
1903 if (outputReady == MagickFalse)
1904 {
1905 if (filteredImage != NULL)
1906 {
1907 DestroyImage(filteredImage);
1908 filteredImage = NULL;
1909 }
1910 }
1911 return filteredImage;
1912}
1913
dirk832becc2014-08-04 19:44:34 +00001914MagickExport Image* AccelerateRotationalBlurImage(const Image *image,
1915 const ChannelType channel,const double angle,ExceptionInfo *exception)
1916{
1917 Image
1918 *filteredImage;
1919
1920 assert(image != NULL);
1921 assert(exception != (ExceptionInfo *) NULL);
1922
1923 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
1924 (checkAccelerateCondition(image, channel) == MagickFalse))
1925 return NULL;
1926
1927 filteredImage=ComputeRotationalBlurImage(image, channel, angle, exception);
1928 return filteredImage;
dirk8a5cf512014-07-28 20:16:27 +00001929}
1930
cristyf034abb2013-11-24 14:16:14 +00001931/*
1932%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1933% %
1934% %
1935% %
dirk8a5cf512014-07-28 20:16:27 +00001936% U n s h a r p M a s k I m a g e w i t h O p e n C L %
cristyf034abb2013-11-24 14:16:14 +00001937% %
1938% %
1939% %
1940%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1941%
dirk8a5cf512014-07-28 20:16:27 +00001942% UnsharpMaskImage() sharpens one or more image channels. We convolve the
1943% image with a Gaussian operator of the given radius and standard deviation
1944% (sigma). For reasonable results, radius should be larger than sigma. Use a
1945% radius of 0 and UnsharpMaskImage() selects a suitable radius for you.
cristyf034abb2013-11-24 14:16:14 +00001946%
dirk8a5cf512014-07-28 20:16:27 +00001947% The format of the UnsharpMaskImage method is:
cristyf034abb2013-11-24 14:16:14 +00001948%
dirk8a5cf512014-07-28 20:16:27 +00001949% Image *UnsharpMaskImage(const Image *image,const double radius,
1950% const double sigma,const double amount,const double threshold,
cristyf034abb2013-11-24 14:16:14 +00001951% ExceptionInfo *exception)
dirk8a5cf512014-07-28 20:16:27 +00001952% Image *UnsharpMaskImageChannel(const Image *image,
1953% const ChannelType channel,const double radius,const double sigma,
1954% const double gain,const double threshold,ExceptionInfo *exception)
cristyf034abb2013-11-24 14:16:14 +00001955%
1956% A description of each parameter follows:
1957%
1958% o image: the image.
1959%
1960% o channel: the channel type.
1961%
dirk8a5cf512014-07-28 20:16:27 +00001962% o radius: the radius of the Gaussian, in pixels, not counting the center
1963% pixel.
1964%
1965% o sigma: the standard deviation of the Gaussian, in pixels.
1966%
1967% o gain: the percentage of the difference between the original and the
1968% blur image that is added back into the original.
1969%
1970% o threshold: the threshold in pixels needed to apply the diffence gain.
cristyf034abb2013-11-24 14:16:14 +00001971%
1972% o exception: return any errors or warnings in this structure.
1973%
1974*/
1975
dirk832becc2014-08-04 19:44:34 +00001976static Image *ComputeUnsharpMaskImage(const Image *image,
1977 const ChannelType channel,const double radius,const double sigma,
1978 const double gain,const double threshold,ExceptionInfo *exception)
1979{
1980 CacheView
1981 *filteredImage_view,
1982 *image_view;
1983
1984 char
1985 geometry[MaxTextExtent];
1986
1987 cl_command_queue
1988 queue;
1989
1990 cl_context
1991 context;
1992
1993 cl_int
1994 clStatus;
1995
1996 cl_kernel
1997 blurRowKernel,
1998 unsharpMaskBlurColumnKernel;
1999
2000 cl_mem
2001 filteredImageBuffer,
2002 imageBuffer,
2003 imageKernelBuffer,
2004 tempImageBuffer;
2005
2006 cl_mem_flags
2007 mem_flags;
2008
2009 const void
2010 *inputPixels;
2011
2012 float
2013 fGain,
2014 fThreshold,
2015 *kernelBufferPtr;
2016
2017 Image
2018 *filteredImage;
2019
2020 int
2021 chunkSize;
2022
2023 KernelInfo
2024 *kernel;
2025
2026 MagickBooleanType
2027 outputReady;
2028
2029 MagickCLEnv
2030 clEnv;
2031
2032 MagickSizeType
2033 length;
2034
2035 void
2036 *filteredPixels,
2037 *hostPtr;
2038
2039 unsigned int
2040 i,
2041 imageColumns,
2042 imageRows,
2043 kernelWidth;
2044
2045 clEnv = NULL;
2046 filteredImage = NULL;
2047 filteredImage_view = NULL;
2048 kernel = NULL;
2049 context = NULL;
2050 imageBuffer = NULL;
2051 filteredImageBuffer = NULL;
2052 tempImageBuffer = NULL;
2053 imageKernelBuffer = NULL;
2054 blurRowKernel = NULL;
2055 unsharpMaskBlurColumnKernel = NULL;
2056 queue = NULL;
2057 outputReady = MagickFalse;
2058
2059 clEnv = GetDefaultOpenCLEnv();
2060 context = GetOpenCLContext(clEnv);
2061 queue = AcquireOpenCLCommandQueue(clEnv);
2062
2063 /* Create and initialize OpenCL buffers. */
2064 {
2065 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00002066 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00002067 if (inputPixels == (const void *) NULL)
2068 {
dirk8a5cf512014-07-28 20:16:27 +00002069 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +00002070 goto cleanup;
2071 }
2072
2073 /* If the host pointer is aligned to the size of CLPixelPacket,
2074 then use the host buffer directly from the GPU; otherwise,
2075 create a buffer on the GPU and copy the data over */
2076 if (ALIGNED(inputPixels,CLPixelPacket))
2077 {
2078 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
2079 }
2080 else
2081 {
2082 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
2083 }
2084 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00002085 length = image->columns * image->rows;
2086 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00002087 if (clStatus != CL_SUCCESS)
2088 {
cristy0c832c62014-03-07 22:21:04 +00002089 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00002090 goto cleanup;
2091 }
2092 }
2093
2094 /* create output */
2095 {
dirk8a5cf512014-07-28 20:16:27 +00002096 filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristyf034abb2013-11-24 14:16:14 +00002097 assert(filteredImage != NULL);
dirk8a5cf512014-07-28 20:16:27 +00002098 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristyf034abb2013-11-24 14:16:14 +00002099 {
cristya22457d2013-12-07 14:03:06 +00002100 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002101 goto cleanup;
2102 }
dirk832becc2014-08-04 19:44:34 +00002103 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +00002104 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00002105 if (filteredPixels == (void *) NULL)
2106 {
cristya22457d2013-12-07 14:03:06 +00002107 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename);
cristyf034abb2013-11-24 14:16:14 +00002108 goto cleanup;
2109 }
2110
2111 if (ALIGNED(filteredPixels,CLPixelPacket))
2112 {
2113 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
2114 hostPtr = filteredPixels;
2115 }
2116 else
2117 {
2118 mem_flags = CL_MEM_WRITE_ONLY;
2119 hostPtr = NULL;
2120 }
2121
2122 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00002123 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00002124 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00002125 if (clStatus != CL_SUCCESS)
2126 {
cristy0c832c62014-03-07 22:21:04 +00002127 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00002128 goto cleanup;
2129 }
2130 }
2131
2132 /* create the blur kernel */
2133 {
2134 (void) FormatLocaleString(geometry,MaxTextExtent,"blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
2135 kernel=AcquireKernelInfo(geometry);
2136 if (kernel == (KernelInfo *) NULL)
2137 {
cristya22457d2013-12-07 14:03:06 +00002138 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireKernelInfo failed.",".");
cristyf034abb2013-11-24 14:16:14 +00002139 goto cleanup;
2140 }
2141
cristy0c832c62014-03-07 22:21:04 +00002142 imageKernelBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY, kernel->width * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00002143 if (clStatus != CL_SUCCESS)
2144 {
cristy0c832c62014-03-07 22:21:04 +00002145 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00002146 goto cleanup;
2147 }
2148
2149
cristy0c832c62014-03-07 22:21:04 +00002150 kernelBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, imageKernelBuffer, CL_TRUE, CL_MAP_WRITE, 0, kernel->width * sizeof(float), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00002151 if (clStatus != CL_SUCCESS)
2152 {
cristy0c832c62014-03-07 22:21:04 +00002153 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00002154 goto cleanup;
2155 }
2156 for (i = 0; i < kernel->width; i++)
2157 {
2158 kernelBufferPtr[i] = (float) kernel->values[i];
2159 }
cristy0c832c62014-03-07 22:21:04 +00002160 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, imageKernelBuffer, kernelBufferPtr, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00002161 if (clStatus != CL_SUCCESS)
2162 {
cristy0c832c62014-03-07 22:21:04 +00002163 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002164 goto cleanup;
2165 }
2166 }
2167
2168 {
2169 /* create temp buffer */
2170 {
dirk8a5cf512014-07-28 20:16:27 +00002171 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00002172 tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length * 4 * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00002173 if (clStatus != CL_SUCCESS)
2174 {
cristy0c832c62014-03-07 22:21:04 +00002175 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00002176 goto cleanup;
2177 }
2178 }
2179
2180 /* get the opencl kernel */
2181 {
2182 blurRowKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "BlurRow");
2183 if (blurRowKernel == NULL)
2184 {
cristya22457d2013-12-07 14:03:06 +00002185 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002186 goto cleanup;
2187 };
2188
2189 unsharpMaskBlurColumnKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "UnsharpMaskBlurColumn");
2190 if (unsharpMaskBlurColumnKernel == NULL)
2191 {
cristya22457d2013-12-07 14:03:06 +00002192 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002193 goto cleanup;
2194 };
2195 }
2196
2197 {
2198 chunkSize = 256;
2199
dirkb0d783f2014-08-31 10:48:05 +00002200 imageColumns = (unsigned int) image->columns;
2201 imageRows = (unsigned int) image->rows;
cristyf034abb2013-11-24 14:16:14 +00002202
dirkb0d783f2014-08-31 10:48:05 +00002203 kernelWidth = (unsigned int) kernel->width;
cristyf034abb2013-11-24 14:16:14 +00002204
2205 /* set the kernel arguments */
2206 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00002207 clStatus=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00002208 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&tempImageBuffer);
2209 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(ChannelType),&channel);
2210 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&imageKernelBuffer);
2211 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&kernelWidth);
2212 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&imageColumns);
2213 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&imageRows);
2214 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(CLPixelPacket)*(chunkSize+kernel->width),(void *)NULL);
cristyf034abb2013-11-24 14:16:14 +00002215 if (clStatus != CL_SUCCESS)
2216 {
cristy0c832c62014-03-07 22:21:04 +00002217 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002218 goto cleanup;
2219 }
2220 }
2221
2222 /* launch the kernel */
2223 {
2224 size_t gsize[2];
2225 size_t wsize[2];
2226
dirk8a5cf512014-07-28 20:16:27 +00002227 gsize[0] = chunkSize*((image->columns+chunkSize-1)/chunkSize);
2228 gsize[1] = image->rows;
cristyf034abb2013-11-24 14:16:14 +00002229 wsize[0] = chunkSize;
2230 wsize[1] = 1;
2231
cristy0c832c62014-03-07 22:21:04 +00002232 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, blurRowKernel, 2, NULL, gsize, wsize, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00002233 if (clStatus != CL_SUCCESS)
2234 {
cristy0c832c62014-03-07 22:21:04 +00002235 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002236 goto cleanup;
2237 }
cristy0c832c62014-03-07 22:21:04 +00002238 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00002239 }
2240
2241
2242 {
2243 chunkSize = 256;
dirkb0d783f2014-08-31 10:48:05 +00002244 imageColumns = (unsigned int) image->columns;
2245 imageRows = (unsigned int) image->rows;
2246 kernelWidth = (unsigned int) kernel->width;
2247 fGain = (float) gain;
2248 fThreshold = (float) threshold;
cristyf034abb2013-11-24 14:16:14 +00002249
2250 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00002251 clStatus=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00002252 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(cl_mem),(void *)&tempImageBuffer);
2253 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(cl_mem),(void *)&filteredImageBuffer);
2254 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(unsigned int),(void *)&imageColumns);
2255 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(unsigned int),(void *)&imageRows);
2256 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++, (chunkSize+kernelWidth-1)*sizeof(cl_float4),NULL);
2257 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++, kernelWidth*sizeof(float),NULL);
2258 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(ChannelType),&channel);
2259 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(cl_mem),(void *)&imageKernelBuffer);
2260 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(unsigned int),(void *)&kernelWidth);
2261 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(float),(void *)&fGain);
2262 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(float),(void *)&fThreshold);
cristyf034abb2013-11-24 14:16:14 +00002263
2264 if (clStatus != CL_SUCCESS)
2265 {
cristy0c832c62014-03-07 22:21:04 +00002266 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002267 goto cleanup;
2268 }
2269 }
2270
2271 /* launch the kernel */
2272 {
2273 size_t gsize[2];
2274 size_t wsize[2];
2275
dirk8a5cf512014-07-28 20:16:27 +00002276 gsize[0] = image->columns;
2277 gsize[1] = chunkSize*((image->rows+chunkSize-1)/chunkSize);
cristyf034abb2013-11-24 14:16:14 +00002278 wsize[0] = 1;
2279 wsize[1] = chunkSize;
2280
cristy0c832c62014-03-07 22:21:04 +00002281 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, unsharpMaskBlurColumnKernel, 2, NULL, gsize, wsize, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00002282 if (clStatus != CL_SUCCESS)
2283 {
cristy0c832c62014-03-07 22:21:04 +00002284 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002285 goto cleanup;
2286 }
cristy0c832c62014-03-07 22:21:04 +00002287 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00002288 }
2289
2290 }
2291
2292 /* get result */
2293 if (ALIGNED(filteredPixels,CLPixelPacket))
2294 {
dirk8a5cf512014-07-28 20:16:27 +00002295 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00002296 clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00002297 }
2298 else
2299 {
dirk8a5cf512014-07-28 20:16:27 +00002300 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00002301 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00002302 }
2303 if (clStatus != CL_SUCCESS)
2304 {
cristya22457d2013-12-07 14:03:06 +00002305 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002306 goto cleanup;
2307 }
2308
dirk832becc2014-08-04 19:44:34 +00002309 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
2310
2311cleanup:
2312 OpenCLLogException(__FUNCTION__,__LINE__,exception);
2313
2314 image_view=DestroyCacheView(image_view);
2315 if (filteredImage_view != NULL)
dirk8a5cf512014-07-28 20:16:27 +00002316 filteredImage_view=DestroyCacheView(filteredImage_view);
cristya22457d2013-12-07 14:03:06 +00002317
cristyf034abb2013-11-24 14:16:14 +00002318 if (kernel != NULL) kernel=DestroyKernelInfo(kernel);
dirk8a5cf512014-07-28 20:16:27 +00002319 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00002320 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
2321 if (tempImageBuffer!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer);
2322 if (imageKernelBuffer!=NULL) clEnv->library->clReleaseMemObject(imageKernelBuffer);
cristyf034abb2013-11-24 14:16:14 +00002323 if (blurRowKernel!=NULL) RelinquishOpenCLKernel(clEnv, blurRowKernel);
2324 if (unsharpMaskBlurColumnKernel!=NULL) RelinquishOpenCLKernel(clEnv, unsharpMaskBlurColumnKernel);
2325 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
2326 if (outputReady == MagickFalse)
2327 {
2328 if (filteredImage != NULL)
2329 {
2330 DestroyImage(filteredImage);
2331 filteredImage = NULL;
2332 }
2333 }
dirk8a5cf512014-07-28 20:16:27 +00002334 return(filteredImage);
cristyf034abb2013-11-24 14:16:14 +00002335}
2336
dirk832becc2014-08-04 19:44:34 +00002337static Image *ComputeUnsharpMaskImageSection(const Image *image,
2338 const ChannelType channel,const double radius,const double sigma,
2339 const double gain,const double threshold,ExceptionInfo *exception)
2340{
2341 CacheView
2342 *filteredImage_view,
2343 *image_view;
2344
2345 char
2346 geometry[MaxTextExtent];
2347
2348 cl_command_queue
2349 queue;
2350
2351 cl_context
2352 context;
2353
2354 cl_int
2355 clStatus;
2356
2357 cl_kernel
2358 blurRowKernel,
2359 unsharpMaskBlurColumnKernel;
2360
2361 cl_mem
2362 filteredImageBuffer,
2363 imageBuffer,
2364 imageKernelBuffer,
2365 tempImageBuffer;
2366
2367 cl_mem_flags
2368 mem_flags;
2369
2370 const void
2371 *inputPixels;
2372
2373 float
2374 fGain,
2375 fThreshold,
2376 *kernelBufferPtr;
2377
2378 Image
2379 *filteredImage;
2380
2381 int
2382 chunkSize;
2383
2384 KernelInfo
2385 *kernel;
2386
2387 MagickBooleanType
2388 outputReady;
2389
2390 MagickCLEnv
2391 clEnv;
2392
2393 MagickSizeType
2394 length;
2395
2396 void
2397 *filteredPixels,
2398 *hostPtr;
2399
2400 unsigned int
2401 i,
2402 imageColumns,
2403 imageRows,
2404 kernelWidth;
2405
2406 clEnv = NULL;
2407 filteredImage = NULL;
2408 filteredImage_view = NULL;
2409 kernel = NULL;
2410 context = NULL;
2411 imageBuffer = NULL;
2412 filteredImageBuffer = NULL;
2413 tempImageBuffer = NULL;
2414 imageKernelBuffer = NULL;
2415 blurRowKernel = NULL;
2416 unsharpMaskBlurColumnKernel = NULL;
2417 queue = NULL;
2418 outputReady = MagickFalse;
2419
2420 clEnv = GetDefaultOpenCLEnv();
2421 context = GetOpenCLContext(clEnv);
2422 queue = AcquireOpenCLCommandQueue(clEnv);
2423
2424 /* Create and initialize OpenCL buffers. */
2425 {
2426 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00002427 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00002428 if (inputPixels == (const void *) NULL)
2429 {
dirk8a5cf512014-07-28 20:16:27 +00002430 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +00002431 goto cleanup;
2432 }
2433
2434 /* If the host pointer is aligned to the size of CLPixelPacket,
2435 then use the host buffer directly from the GPU; otherwise,
2436 create a buffer on the GPU and copy the data over */
2437 if (ALIGNED(inputPixels,CLPixelPacket))
2438 {
2439 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
2440 }
2441 else
2442 {
2443 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
2444 }
2445 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00002446 length = image->columns * image->rows;
2447 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00002448 if (clStatus != CL_SUCCESS)
2449 {
cristy0c832c62014-03-07 22:21:04 +00002450 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00002451 goto cleanup;
2452 }
2453 }
2454
2455 /* create output */
2456 {
dirk8a5cf512014-07-28 20:16:27 +00002457 filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristyf034abb2013-11-24 14:16:14 +00002458 assert(filteredImage != NULL);
dirk8a5cf512014-07-28 20:16:27 +00002459 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristyf034abb2013-11-24 14:16:14 +00002460 {
cristya22457d2013-12-07 14:03:06 +00002461 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002462 goto cleanup;
2463 }
dirk832becc2014-08-04 19:44:34 +00002464 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +00002465 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00002466 if (filteredPixels == (void *) NULL)
2467 {
cristya22457d2013-12-07 14:03:06 +00002468 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename);
cristyf034abb2013-11-24 14:16:14 +00002469 goto cleanup;
2470 }
2471
2472 if (ALIGNED(filteredPixels,CLPixelPacket))
2473 {
2474 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
2475 hostPtr = filteredPixels;
2476 }
2477 else
2478 {
2479 mem_flags = CL_MEM_WRITE_ONLY;
2480 hostPtr = NULL;
2481 }
2482
2483 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00002484 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00002485 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00002486 if (clStatus != CL_SUCCESS)
2487 {
cristy0c832c62014-03-07 22:21:04 +00002488 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00002489 goto cleanup;
2490 }
2491 }
2492
2493 /* create the blur kernel */
2494 {
2495 (void) FormatLocaleString(geometry,MaxTextExtent,"blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
2496 kernel=AcquireKernelInfo(geometry);
2497 if (kernel == (KernelInfo *) NULL)
2498 {
cristya22457d2013-12-07 14:03:06 +00002499 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireKernelInfo failed.",".");
cristyf034abb2013-11-24 14:16:14 +00002500 goto cleanup;
2501 }
2502
cristy0c832c62014-03-07 22:21:04 +00002503 imageKernelBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY, kernel->width * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00002504 if (clStatus != CL_SUCCESS)
2505 {
cristy0c832c62014-03-07 22:21:04 +00002506 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00002507 goto cleanup;
2508 }
2509
2510
cristy0c832c62014-03-07 22:21:04 +00002511 kernelBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, imageKernelBuffer, CL_TRUE, CL_MAP_WRITE, 0, kernel->width * sizeof(float), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00002512 if (clStatus != CL_SUCCESS)
2513 {
cristy0c832c62014-03-07 22:21:04 +00002514 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00002515 goto cleanup;
2516 }
2517 for (i = 0; i < kernel->width; i++)
2518 {
2519 kernelBufferPtr[i] = (float) kernel->values[i];
2520 }
cristy0c832c62014-03-07 22:21:04 +00002521 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, imageKernelBuffer, kernelBufferPtr, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00002522 if (clStatus != CL_SUCCESS)
2523 {
cristy0c832c62014-03-07 22:21:04 +00002524 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002525 goto cleanup;
2526 }
2527 }
2528
2529 {
2530 unsigned int offsetRows;
2531 unsigned int sec;
2532
2533 /* create temp buffer */
2534 {
dirk8a5cf512014-07-28 20:16:27 +00002535 length = image->columns * (image->rows / 2 + 1 + (kernel->width-1) / 2);
cristy0c832c62014-03-07 22:21:04 +00002536 tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length * 4 * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00002537 if (clStatus != CL_SUCCESS)
2538 {
cristy0c832c62014-03-07 22:21:04 +00002539 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00002540 goto cleanup;
2541 }
2542 }
2543
2544 /* get the opencl kernel */
2545 {
2546 blurRowKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "BlurRowSection");
2547 if (blurRowKernel == NULL)
2548 {
cristya22457d2013-12-07 14:03:06 +00002549 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002550 goto cleanup;
2551 };
2552
2553 unsharpMaskBlurColumnKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "UnsharpMaskBlurColumnSection");
2554 if (unsharpMaskBlurColumnKernel == NULL)
2555 {
cristya22457d2013-12-07 14:03:06 +00002556 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002557 goto cleanup;
2558 };
2559 }
2560
2561 for (sec = 0; sec < 2; sec++)
2562 {
2563 {
2564 chunkSize = 256;
2565
dirkb0d783f2014-08-31 10:48:05 +00002566 imageColumns = (unsigned int) image->columns;
cristyf034abb2013-11-24 14:16:14 +00002567 if (sec == 0)
dirkb0d783f2014-08-31 10:48:05 +00002568 imageRows = (unsigned int) (image->rows / 2 + (kernel->width-1) / 2);
cristyf034abb2013-11-24 14:16:14 +00002569 else
dirkb0d783f2014-08-31 10:48:05 +00002570 imageRows = (unsigned int) ((image->rows - image->rows / 2) + (kernel->width-1) / 2);
cristyf034abb2013-11-24 14:16:14 +00002571
dirkb0d783f2014-08-31 10:48:05 +00002572 offsetRows = (unsigned int) (sec * image->rows / 2);
cristyf034abb2013-11-24 14:16:14 +00002573
dirkb0d783f2014-08-31 10:48:05 +00002574 kernelWidth = (unsigned int) kernel->width;
cristyf034abb2013-11-24 14:16:14 +00002575
2576 /* set the kernel arguments */
2577 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00002578 clStatus=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00002579 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&tempImageBuffer);
2580 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(ChannelType),&channel);
2581 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&imageKernelBuffer);
2582 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&kernelWidth);
2583 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&imageColumns);
2584 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&imageRows);
2585 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(CLPixelPacket)*(chunkSize+kernel->width),(void *)NULL);
2586 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&offsetRows);
2587 clStatus|=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(unsigned int),(void *)&sec);
cristyf034abb2013-11-24 14:16:14 +00002588 if (clStatus != CL_SUCCESS)
2589 {
cristy0c832c62014-03-07 22:21:04 +00002590 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002591 goto cleanup;
2592 }
2593 }
2594 /* launch the kernel */
2595 {
2596 size_t gsize[2];
2597 size_t wsize[2];
2598
2599 gsize[0] = chunkSize*((imageColumns+chunkSize-1)/chunkSize);
2600 gsize[1] = imageRows;
2601 wsize[0] = chunkSize;
2602 wsize[1] = 1;
2603
cristy0c832c62014-03-07 22:21:04 +00002604 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, blurRowKernel, 2, NULL, gsize, wsize, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00002605 if (clStatus != CL_SUCCESS)
2606 {
cristy0c832c62014-03-07 22:21:04 +00002607 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002608 goto cleanup;
2609 }
cristy0c832c62014-03-07 22:21:04 +00002610 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00002611 }
2612
2613
2614 {
2615 chunkSize = 256;
2616
dirkb0d783f2014-08-31 10:48:05 +00002617 imageColumns = (unsigned int) image->columns;
cristyf034abb2013-11-24 14:16:14 +00002618 if (sec == 0)
dirkb0d783f2014-08-31 10:48:05 +00002619 imageRows = (unsigned int) (image->rows / 2);
cristyf034abb2013-11-24 14:16:14 +00002620 else
dirkb0d783f2014-08-31 10:48:05 +00002621 imageRows = (unsigned int) (image->rows - image->rows / 2);
cristyf034abb2013-11-24 14:16:14 +00002622
dirkb0d783f2014-08-31 10:48:05 +00002623 offsetRows = (unsigned int) (sec * image->rows / 2);
cristyf034abb2013-11-24 14:16:14 +00002624
dirkb0d783f2014-08-31 10:48:05 +00002625 kernelWidth = (unsigned int) kernel->width;
cristyf034abb2013-11-24 14:16:14 +00002626
dirkb0d783f2014-08-31 10:48:05 +00002627 fGain = (float) gain;
2628 fThreshold = (float) threshold;
cristyf034abb2013-11-24 14:16:14 +00002629
2630 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00002631 clStatus=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00002632 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(cl_mem),(void *)&tempImageBuffer);
2633 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(cl_mem),(void *)&filteredImageBuffer);
2634 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(unsigned int),(void *)&imageColumns);
2635 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(unsigned int),(void *)&imageRows);
2636 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++, (chunkSize+kernelWidth-1)*sizeof(cl_float4),NULL);
2637 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++, kernelWidth*sizeof(float),NULL);
2638 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(ChannelType),&channel);
2639 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(cl_mem),(void *)&imageKernelBuffer);
2640 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(unsigned int),(void *)&kernelWidth);
2641 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(float),(void *)&fGain);
2642 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(float),(void *)&fThreshold);
2643 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(unsigned int),(void *)&offsetRows);
2644 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(unsigned int),(void *)&sec);
cristyf034abb2013-11-24 14:16:14 +00002645
2646 if (clStatus != CL_SUCCESS)
2647 {
cristy0c832c62014-03-07 22:21:04 +00002648 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002649 goto cleanup;
2650 }
2651 }
2652
2653 /* launch the kernel */
2654 {
2655 size_t gsize[2];
2656 size_t wsize[2];
2657
2658 gsize[0] = imageColumns;
2659 gsize[1] = chunkSize*((imageRows+chunkSize-1)/chunkSize);
2660 wsize[0] = 1;
2661 wsize[1] = chunkSize;
2662
cristy0c832c62014-03-07 22:21:04 +00002663 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, unsharpMaskBlurColumnKernel, 2, NULL, gsize, wsize, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00002664 if (clStatus != CL_SUCCESS)
2665 {
cristy0c832c62014-03-07 22:21:04 +00002666 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002667 goto cleanup;
2668 }
cristy0c832c62014-03-07 22:21:04 +00002669 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00002670 }
2671 }
2672 }
2673
2674 /* get result */
2675 if (ALIGNED(filteredPixels,CLPixelPacket))
2676 {
dirk8a5cf512014-07-28 20:16:27 +00002677 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00002678 clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00002679 }
2680 else
2681 {
dirk8a5cf512014-07-28 20:16:27 +00002682 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00002683 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00002684 }
2685 if (clStatus != CL_SUCCESS)
2686 {
cristya22457d2013-12-07 14:03:06 +00002687 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00002688 goto cleanup;
2689 }
2690
dirk832becc2014-08-04 19:44:34 +00002691 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
2692
2693cleanup:
2694 OpenCLLogException(__FUNCTION__,__LINE__,exception);
2695
2696 image_view=DestroyCacheView(image_view);
2697 if (filteredImage_view != NULL)
dirk8a5cf512014-07-28 20:16:27 +00002698 filteredImage_view=DestroyCacheView(filteredImage_view);
cristya22457d2013-12-07 14:03:06 +00002699
cristyf034abb2013-11-24 14:16:14 +00002700 if (kernel != NULL) kernel=DestroyKernelInfo(kernel);
dirk8a5cf512014-07-28 20:16:27 +00002701 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00002702 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
2703 if (tempImageBuffer!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer);
2704 if (imageKernelBuffer!=NULL) clEnv->library->clReleaseMemObject(imageKernelBuffer);
cristyf034abb2013-11-24 14:16:14 +00002705 if (blurRowKernel!=NULL) RelinquishOpenCLKernel(clEnv, blurRowKernel);
2706 if (unsharpMaskBlurColumnKernel!=NULL) RelinquishOpenCLKernel(clEnv, unsharpMaskBlurColumnKernel);
2707 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
2708 if (outputReady == MagickFalse)
2709 {
2710 if (filteredImage != NULL)
2711 {
2712 DestroyImage(filteredImage);
2713 filteredImage = NULL;
2714 }
2715 }
2716 return filteredImage;
dirk832becc2014-08-04 19:44:34 +00002717}
2718
dirk859503c2014-08-13 20:39:42 +00002719static Image *ComputeUnsharpMaskImageSingle(const Image *image,
2720 const ChannelType channel,const double radius,const double sigma,
2721 const double gain,const double threshold,int blurOnly, ExceptionInfo *exception)
2722{
2723 CacheView
2724 *filteredImage_view,
2725 *image_view;
2726
2727 char
2728 geometry[MaxTextExtent];
2729
2730 cl_command_queue
2731 queue;
2732
2733 cl_context
2734 context;
2735
2736 cl_int
2737 justBlur,
2738 clStatus;
2739
2740 cl_kernel
2741 unsharpMaskKernel;
2742
2743 cl_mem
2744 filteredImageBuffer,
2745 imageBuffer,
2746 imageKernelBuffer;
2747
2748 cl_mem_flags
2749 mem_flags;
2750
2751 const void
2752 *inputPixels;
2753
2754 float
2755 fGain,
2756 fThreshold,
2757 *kernelBufferPtr;
2758
2759 Image
2760 *filteredImage;
2761
2762 KernelInfo
2763 *kernel;
2764
2765 MagickBooleanType
2766 outputReady;
2767
2768 MagickCLEnv
2769 clEnv;
2770
2771 MagickSizeType
2772 length;
2773
2774 void
2775 *filteredPixels,
2776 *hostPtr;
2777
2778 unsigned int
2779 i,
2780 imageColumns,
2781 imageRows,
2782 kernelWidth;
2783
2784 clEnv = NULL;
2785 filteredImage = NULL;
2786 filteredImage_view = NULL;
2787 kernel = NULL;
2788 context = NULL;
2789 imageBuffer = NULL;
2790 filteredImageBuffer = NULL;
2791 imageKernelBuffer = NULL;
2792 unsharpMaskKernel = NULL;
2793 queue = NULL;
2794 outputReady = MagickFalse;
2795
2796 clEnv = GetDefaultOpenCLEnv();
2797 context = GetOpenCLContext(clEnv);
2798 queue = AcquireOpenCLCommandQueue(clEnv);
2799
2800 /* Create and initialize OpenCL buffers. */
2801 {
2802 image_view=AcquireVirtualCacheView(image,exception);
2803 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
2804 if (inputPixels == (const void *) NULL)
2805 {
2806 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
2807 goto cleanup;
2808 }
2809
2810 /* If the host pointer is aligned to the size of CLPixelPacket,
2811 then use the host buffer directly from the GPU; otherwise,
2812 create a buffer on the GPU and copy the data over */
2813 if (ALIGNED(inputPixels,CLPixelPacket))
2814 {
2815 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
2816 }
2817 else
2818 {
2819 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
2820 }
2821 /* create a CL buffer from image pixel buffer */
2822 length = image->columns * image->rows;
2823 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
2824 if (clStatus != CL_SUCCESS)
2825 {
2826 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
2827 goto cleanup;
2828 }
2829 }
2830
2831 /* create output */
2832 {
2833 filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2834 assert(filteredImage != NULL);
2835 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
2836 {
2837 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", ".");
2838 goto cleanup;
2839 }
2840 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
2841 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
2842 if (filteredPixels == (void *) NULL)
2843 {
2844 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename);
2845 goto cleanup;
2846 }
2847
2848 if (ALIGNED(filteredPixels,CLPixelPacket))
2849 {
2850 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
2851 hostPtr = filteredPixels;
2852 }
2853 else
2854 {
2855 mem_flags = CL_MEM_WRITE_ONLY;
2856 hostPtr = NULL;
2857 }
2858
2859 /* create a CL buffer from image pixel buffer */
2860 length = image->columns * image->rows;
2861 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus);
2862 if (clStatus != CL_SUCCESS)
2863 {
2864 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
2865 goto cleanup;
2866 }
2867 }
2868
2869 /* create the blur kernel */
2870 {
2871 (void) FormatLocaleString(geometry,MaxTextExtent,"blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
2872 kernel=AcquireKernelInfo(geometry);
2873 if (kernel == (KernelInfo *) NULL)
2874 {
2875 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireKernelInfo failed.",".");
2876 goto cleanup;
2877 }
2878
2879 imageKernelBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY, kernel->width * sizeof(float), NULL, &clStatus);
2880 if (clStatus != CL_SUCCESS)
2881 {
2882 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
2883 goto cleanup;
2884 }
2885
2886
2887 kernelBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, imageKernelBuffer, CL_TRUE, CL_MAP_WRITE, 0, kernel->width * sizeof(float), 0, NULL, NULL, &clStatus);
2888 if (clStatus != CL_SUCCESS)
2889 {
2890 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.",".");
2891 goto cleanup;
2892 }
2893 for (i = 0; i < kernel->width; i++)
2894 {
2895 kernelBufferPtr[i] = (float) kernel->values[i];
2896 }
2897 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, imageKernelBuffer, kernelBufferPtr, 0, NULL, NULL);
2898 if (clStatus != CL_SUCCESS)
2899 {
2900 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
2901 goto cleanup;
2902 }
2903 }
2904
2905 {
2906 /* get the opencl kernel */
2907 {
2908 unsharpMaskKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "UnsharpMask");
2909 if (unsharpMaskKernel == NULL)
2910 {
2911 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
2912 goto cleanup;
2913 };
2914 }
2915
2916 {
dirkb0d783f2014-08-31 10:48:05 +00002917 imageColumns = (unsigned int) image->columns;
2918 imageRows = (unsigned int) image->rows;
2919 kernelWidth = (unsigned int) kernel->width;
2920 fGain = (float) gain;
2921 fThreshold = (float) threshold;
dirk859503c2014-08-13 20:39:42 +00002922 justBlur = blurOnly;
2923
2924 /* set the kernel arguments */
2925 i = 0;
2926 clStatus=clEnv->library->clSetKernelArg(unsharpMaskKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
2927 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskKernel,i++,sizeof(cl_mem),(void *)&filteredImageBuffer);
2928 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskKernel,i++,sizeof(cl_mem),(void *)&imageKernelBuffer);
2929 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskKernel,i++,sizeof(unsigned int),(void *)&kernelWidth);
2930 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskKernel,i++,sizeof(unsigned int),(void *)&imageColumns);
2931 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskKernel,i++,sizeof(unsigned int),(void *)&imageRows);
2932 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskKernel,i++,sizeof(cl_float4)*(8 * (32 + kernel->width)),(void *)NULL);
2933 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskKernel,i++,sizeof(float),(void *)&fGain);
2934 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskKernel,i++,sizeof(float),(void *)&fThreshold);
2935 clStatus|=clEnv->library->clSetKernelArg(unsharpMaskKernel,i++,sizeof(cl_uint),(void *)&justBlur);
2936 if (clStatus != CL_SUCCESS)
2937 {
2938 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
2939 goto cleanup;
2940 }
2941 }
2942
2943 /* launch the kernel */
2944 {
2945 size_t gsize[2];
2946 size_t wsize[2];
2947
2948 gsize[0] = ((image->columns + 7) / 8) * 8;
2949 gsize[1] = ((image->rows + 31) / 32) * 32;
2950 wsize[0] = 8;
2951 wsize[1] = 32;
2952
2953 clStatus = clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, unsharpMaskKernel, 2, NULL, gsize, wsize, 0, NULL, NULL);
2954 if (clStatus != CL_SUCCESS)
2955 {
2956 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
2957 goto cleanup;
2958 }
2959 clEnv->library->clFlush(queue);
2960 }
2961 }
2962
2963 /* get result */
2964 if (ALIGNED(filteredPixels,CLPixelPacket))
2965 {
2966 length = image->columns * image->rows;
2967 clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
2968 }
2969 else
2970 {
2971 length = image->columns * image->rows;
2972 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
2973 }
2974 if (clStatus != CL_SUCCESS)
2975 {
2976 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
2977 goto cleanup;
2978 }
2979
2980 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
2981
2982cleanup:
2983 OpenCLLogException(__FUNCTION__,__LINE__,exception);
2984
2985 image_view=DestroyCacheView(image_view);
2986 if (filteredImage_view != NULL)
2987 filteredImage_view=DestroyCacheView(filteredImage_view);
2988
2989 if (kernel != NULL) kernel=DestroyKernelInfo(kernel);
2990 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
2991 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
2992 if (imageKernelBuffer!=NULL) clEnv->library->clReleaseMemObject(imageKernelBuffer);
2993 if (unsharpMaskKernel!=NULL) RelinquishOpenCLKernel(clEnv, unsharpMaskKernel);
2994 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
2995 if (outputReady == MagickFalse)
2996 {
2997 if (filteredImage != NULL)
2998 {
2999 DestroyImage(filteredImage);
3000 filteredImage = NULL;
3001 }
3002 }
3003 return(filteredImage);
3004}
3005
3006
dirk832becc2014-08-04 19:44:34 +00003007MagickExport Image *AccelerateUnsharpMaskImage(const Image *image,
3008 const ChannelType channel,const double radius,const double sigma,
3009 const double gain,const double threshold,ExceptionInfo *exception)
3010{
3011 Image
3012 *filteredImage;
3013
3014 assert(image != NULL);
3015 assert(exception != (ExceptionInfo *) NULL);
3016
3017 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
3018 (checkAccelerateCondition(image, channel) == MagickFalse))
3019 return NULL;
3020
dirk859503c2014-08-13 20:39:42 +00003021 if (radius < 12.1)
3022 filteredImage = ComputeUnsharpMaskImageSingle(image,channel,radius,sigma,gain,threshold, 0, exception);
3023 else if (splitImage(image) && (image->rows / 2 > radius))
dirk832becc2014-08-04 19:44:34 +00003024 filteredImage = ComputeUnsharpMaskImageSection(image,channel,radius,sigma,gain,threshold,exception);
3025 else
3026 filteredImage = ComputeUnsharpMaskImage(image,channel,radius,sigma,gain,threshold,exception);
3027 return(filteredImage);
cristyf034abb2013-11-24 14:16:14 +00003028}
3029
cristyf034abb2013-11-24 14:16:14 +00003030/*
3031%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3032% %
3033% %
3034% %
dirk8a5cf512014-07-28 20:16:27 +00003035% A c c e l e r a t e R e s i z e I m a g e %
cristyf034abb2013-11-24 14:16:14 +00003036% %
3037% %
3038% %
3039%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3040%
dirk8a5cf512014-07-28 20:16:27 +00003041% AccelerateResizeImage() is an OpenCL implementation of ResizeImage()
cristyf034abb2013-11-24 14:16:14 +00003042%
dirk8a5cf512014-07-28 20:16:27 +00003043% AccelerateResizeImage() scales an image to the desired dimensions, using the given
3044% filter (see AcquireFilterInfo()).
cristyf034abb2013-11-24 14:16:14 +00003045%
dirk8a5cf512014-07-28 20:16:27 +00003046% If an undefined filter is given the filter defaults to Mitchell for a
3047% colormapped image, a image with a matte channel, or if the image is
3048% enlarged. Otherwise the filter defaults to a Lanczos.
3049%
3050% AccelerateResizeImage() was inspired by Paul Heckbert's "zoom" program.
3051%
3052% The format of the AccelerateResizeImage method is:
3053%
3054% Image *ResizeImage(Image *image,const size_t columns,
3055% const size_t rows, const ResizeFilter* filter,
3056% ExceptionInfo *exception)
cristyf034abb2013-11-24 14:16:14 +00003057%
3058% A description of each parameter follows:
3059%
3060% o image: the image.
3061%
dirk8a5cf512014-07-28 20:16:27 +00003062% o columns: the number of columns in the scaled image.
cristyf034abb2013-11-24 14:16:14 +00003063%
dirk8a5cf512014-07-28 20:16:27 +00003064% o rows: the number of rows in the scaled image.
cristyf034abb2013-11-24 14:16:14 +00003065%
dirk8a5cf512014-07-28 20:16:27 +00003066% o filter: Image filter to use.
cristyf034abb2013-11-24 14:16:14 +00003067%
3068% o exception: return any errors or warnings in this structure.
3069%
3070*/
3071
dirk832becc2014-08-04 19:44:34 +00003072static MagickBooleanType resizeHorizontalFilter(cl_mem image,
3073 const unsigned int imageColumns,const unsigned int imageRows,
3074 const unsigned int matte,cl_mem resizedImage,
3075 const unsigned int resizedColumns,const unsigned int resizedRows,
3076 const ResizeFilter *resizeFilter,cl_mem resizeFilterCubicCoefficients,
3077 const float xFactor,MagickCLEnv clEnv,cl_command_queue queue,
3078 ExceptionInfo *exception)
3079{
3080 cl_kernel
3081 horizontalKernel;
3082
3083 cl_int clStatus;
3084
3085 const unsigned int
3086 workgroupSize = 256;
3087
3088 float
3089 resizeFilterScale,
3090 resizeFilterSupport,
3091 resizeFilterWindowSupport,
3092 resizeFilterBlur,
3093 scale,
3094 support;
3095
3096 int
3097 cacheRangeStart,
3098 cacheRangeEnd,
3099 numCachedPixels,
3100 resizeFilterType,
3101 resizeWindowType;
3102
3103 MagickBooleanType
3104 status = MagickFalse;
3105
3106 size_t
3107 deviceLocalMemorySize,
3108 gammaAccumulatorLocalMemorySize,
3109 global_work_size[2],
3110 imageCacheLocalMemorySize,
3111 pixelAccumulatorLocalMemorySize,
3112 local_work_size[2],
3113 totalLocalMemorySize,
3114 weightAccumulatorLocalMemorySize;
3115
3116 unsigned int
3117 chunkSize,
3118 i,
3119 pixelPerWorkgroup;
3120
3121 horizontalKernel = NULL;
dirk8a5cf512014-07-28 20:16:27 +00003122 status = MagickFalse;
cristyf034abb2013-11-24 14:16:14 +00003123
3124 /*
3125 Apply filter to resize vertically from image to resize image.
3126 */
cristye85d0f72013-11-27 02:25:43 +00003127 scale=MAGICK_MAX(1.0/xFactor+MagickEpsilon,1.0);
cristyf034abb2013-11-24 14:16:14 +00003128 support=scale*GetResizeFilterSupport(resizeFilter);
3129 if (support < 0.5)
3130 {
3131 /*
3132 Support too small even for nearest neighbour: Reduce to point
3133 sampling.
3134 */
3135 support=(MagickRealType) 0.5;
3136 scale=1.0;
3137 }
3138 scale=PerceptibleReciprocal(scale);
3139
3140 if (resizedColumns < workgroupSize)
3141 {
3142 chunkSize = 32;
3143 pixelPerWorkgroup = 32;
3144 }
3145 else
3146 {
3147 chunkSize = workgroupSize;
3148 pixelPerWorkgroup = workgroupSize;
3149 }
3150
3151 /* get the local memory size supported by the device */
3152 deviceLocalMemorySize = GetOpenCLDeviceLocalMemorySize(clEnv);
3153
dirke3c5f892013-12-10 06:04:40 +00003154DisableMSCWarning(4127)
cristyf034abb2013-11-24 14:16:14 +00003155 while(1)
dirke3c5f892013-12-10 06:04:40 +00003156RestoreMSCWarning
cristyf034abb2013-11-24 14:16:14 +00003157 {
3158 /* calculate the local memory size needed per workgroup */
3159 cacheRangeStart = (int) (((0 + 0.5)/xFactor+MagickEpsilon)-support+0.5);
3160 cacheRangeEnd = (int) ((((pixelPerWorkgroup-1) + 0.5)/xFactor+MagickEpsilon)+support+0.5);
3161 numCachedPixels = cacheRangeEnd - cacheRangeStart + 1;
3162 imageCacheLocalMemorySize = numCachedPixels * sizeof(CLPixelPacket);
3163 totalLocalMemorySize = imageCacheLocalMemorySize;
3164
3165 /* local size for the pixel accumulator */
3166 pixelAccumulatorLocalMemorySize = chunkSize * sizeof(cl_float4);
3167 totalLocalMemorySize+=pixelAccumulatorLocalMemorySize;
3168
3169 /* local memory size for the weight accumulator */
3170 weightAccumulatorLocalMemorySize = chunkSize * sizeof(float);
3171 totalLocalMemorySize+=weightAccumulatorLocalMemorySize;
3172
3173 /* local memory size for the gamma accumulator */
3174 if (matte == 0)
3175 gammaAccumulatorLocalMemorySize = sizeof(float);
3176 else
3177 gammaAccumulatorLocalMemorySize = chunkSize * sizeof(float);
3178 totalLocalMemorySize+=gammaAccumulatorLocalMemorySize;
3179
3180 if (totalLocalMemorySize <= deviceLocalMemorySize)
3181 break;
3182 else
3183 {
3184 pixelPerWorkgroup = pixelPerWorkgroup/2;
3185 chunkSize = chunkSize/2;
3186 if (pixelPerWorkgroup == 0
3187 || chunkSize == 0)
3188 {
3189 /* quit, fallback to CPU */
3190 goto cleanup;
3191 }
3192 }
3193 }
3194
3195 resizeFilterType = (int)GetResizeFilterWeightingType(resizeFilter);
3196 resizeWindowType = (int)GetResizeFilterWindowWeightingType(resizeFilter);
3197
3198
3199 if (resizeFilterType == SincFastWeightingFunction
3200 && resizeWindowType == SincFastWeightingFunction)
3201 {
3202 horizontalKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "ResizeHorizontalFilterSinc");
3203 }
3204 else
3205 {
3206 horizontalKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "ResizeHorizontalFilter");
3207 }
3208 if (horizontalKernel == NULL)
3209 {
cristya22457d2013-12-07 14:03:06 +00003210 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003211 goto cleanup;
3212 }
3213
3214 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00003215 clStatus = clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(cl_mem), (void*)&image);
3216 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&imageColumns);
3217 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&imageRows);
cristy0c832c62014-03-07 22:21:04 +00003218 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&matte);
3219 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&xFactor);
3220 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(cl_mem), (void*)&resizedImage);
cristyf034abb2013-11-24 14:16:14 +00003221
cristy0c832c62014-03-07 22:21:04 +00003222 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&resizedColumns);
3223 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&resizedRows);
cristyf034abb2013-11-24 14:16:14 +00003224
cristy0c832c62014-03-07 22:21:04 +00003225 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(int), (void*)&resizeFilterType);
3226 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(int), (void*)&resizeWindowType);
3227 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(cl_mem), (void*)&resizeFilterCubicCoefficients);
cristyf034abb2013-11-24 14:16:14 +00003228
3229 resizeFilterScale = (float) GetResizeFilterScale(resizeFilter);
cristy0c832c62014-03-07 22:21:04 +00003230 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterScale);
cristyf034abb2013-11-24 14:16:14 +00003231
3232 resizeFilterSupport = (float) GetResizeFilterSupport(resizeFilter);
cristy0c832c62014-03-07 22:21:04 +00003233 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterSupport);
cristyf034abb2013-11-24 14:16:14 +00003234
3235 resizeFilterWindowSupport = (float) GetResizeFilterWindowSupport(resizeFilter);
cristy0c832c62014-03-07 22:21:04 +00003236 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterWindowSupport);
cristyf034abb2013-11-24 14:16:14 +00003237
3238 resizeFilterBlur = (float) GetResizeFilterBlur(resizeFilter);
cristy0c832c62014-03-07 22:21:04 +00003239 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterBlur);
cristyf034abb2013-11-24 14:16:14 +00003240
3241
cristy0c832c62014-03-07 22:21:04 +00003242 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, imageCacheLocalMemorySize, NULL);
3243 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(int), &numCachedPixels);
3244 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), &pixelPerWorkgroup);
3245 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), &chunkSize);
cristyf034abb2013-11-24 14:16:14 +00003246
3247
cristy0c832c62014-03-07 22:21:04 +00003248 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, pixelAccumulatorLocalMemorySize, NULL);
3249 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, weightAccumulatorLocalMemorySize, NULL);
3250 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, gammaAccumulatorLocalMemorySize, NULL);
cristyf034abb2013-11-24 14:16:14 +00003251
3252 if (clStatus != CL_SUCCESS)
3253 {
cristy0c832c62014-03-07 22:21:04 +00003254 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003255 goto cleanup;
3256 }
3257
3258 global_work_size[0] = (resizedColumns+pixelPerWorkgroup-1)/pixelPerWorkgroup*workgroupSize;
3259 global_work_size[1] = resizedRows;
3260
3261 local_work_size[0] = workgroupSize;
3262 local_work_size[1] = 1;
cristy0c832c62014-03-07 22:21:04 +00003263 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, horizontalKernel, 2, NULL, global_work_size, local_work_size, 0, NULL, NULL);
cristy9ff50022014-08-26 17:38:59 +00003264 (void) local_work_size;
cristyf034abb2013-11-24 14:16:14 +00003265 if (clStatus != CL_SUCCESS)
3266 {
cristy0c832c62014-03-07 22:21:04 +00003267 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003268 goto cleanup;
3269 }
cristy0c832c62014-03-07 22:21:04 +00003270 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00003271 status = MagickTrue;
3272
3273
3274cleanup:
cristya22457d2013-12-07 14:03:06 +00003275 OpenCLLogException(__FUNCTION__,__LINE__,exception);
3276
cristyf034abb2013-11-24 14:16:14 +00003277 if (horizontalKernel != NULL) RelinquishOpenCLKernel(clEnv, horizontalKernel);
3278
dirk832becc2014-08-04 19:44:34 +00003279 return(status);
3280}
3281
3282static MagickBooleanType resizeVerticalFilter(cl_mem image,
3283 const unsigned int imageColumns,const unsigned int imageRows,
3284 const unsigned int matte,cl_mem resizedImage,
3285 const unsigned int resizedColumns,const unsigned int resizedRows,
3286 const ResizeFilter *resizeFilter,cl_mem resizeFilterCubicCoefficients,
3287 const float yFactor,MagickCLEnv clEnv,cl_command_queue queue,
3288 ExceptionInfo *exception)
3289{
3290 cl_kernel
3291 horizontalKernel;
3292
3293 cl_int clStatus;
3294
3295 const unsigned int
3296 workgroupSize = 256;
3297
3298 float
3299 resizeFilterScale,
3300 resizeFilterSupport,
3301 resizeFilterWindowSupport,
3302 resizeFilterBlur,
3303 scale,
3304 support;
3305
3306 int
3307 cacheRangeStart,
3308 cacheRangeEnd,
3309 numCachedPixels,
3310 resizeFilterType,
3311 resizeWindowType;
3312
3313 MagickBooleanType
3314 status = MagickFalse;
3315
3316 size_t
3317 deviceLocalMemorySize,
3318 gammaAccumulatorLocalMemorySize,
3319 global_work_size[2],
3320 imageCacheLocalMemorySize,
3321 pixelAccumulatorLocalMemorySize,
3322 local_work_size[2],
3323 totalLocalMemorySize,
3324 weightAccumulatorLocalMemorySize;
3325
3326 unsigned int
3327 chunkSize,
3328 i,
3329 pixelPerWorkgroup;
3330
3331 horizontalKernel = NULL;
dirk8a5cf512014-07-28 20:16:27 +00003332 status = MagickFalse;
cristyf034abb2013-11-24 14:16:14 +00003333
3334 /*
3335 Apply filter to resize vertically from image to resize image.
3336 */
cristye85d0f72013-11-27 02:25:43 +00003337 scale=MAGICK_MAX(1.0/yFactor+MagickEpsilon,1.0);
cristyf034abb2013-11-24 14:16:14 +00003338 support=scale*GetResizeFilterSupport(resizeFilter);
3339 if (support < 0.5)
3340 {
3341 /*
3342 Support too small even for nearest neighbour: Reduce to point
3343 sampling.
3344 */
3345 support=(MagickRealType) 0.5;
3346 scale=1.0;
3347 }
3348 scale=PerceptibleReciprocal(scale);
3349
3350 if (resizedRows < workgroupSize)
3351 {
3352 chunkSize = 32;
3353 pixelPerWorkgroup = 32;
3354 }
3355 else
3356 {
3357 chunkSize = workgroupSize;
3358 pixelPerWorkgroup = workgroupSize;
3359 }
3360
3361 /* get the local memory size supported by the device */
3362 deviceLocalMemorySize = GetOpenCLDeviceLocalMemorySize(clEnv);
3363
dirke3c5f892013-12-10 06:04:40 +00003364DisableMSCWarning(4127)
cristyf034abb2013-11-24 14:16:14 +00003365 while(1)
dirke3c5f892013-12-10 06:04:40 +00003366RestoreMSCWarning
cristyf034abb2013-11-24 14:16:14 +00003367 {
3368 /* calculate the local memory size needed per workgroup */
3369 cacheRangeStart = (int) (((0 + 0.5)/yFactor+MagickEpsilon)-support+0.5);
3370 cacheRangeEnd = (int) ((((pixelPerWorkgroup-1) + 0.5)/yFactor+MagickEpsilon)+support+0.5);
3371 numCachedPixels = cacheRangeEnd - cacheRangeStart + 1;
3372 imageCacheLocalMemorySize = numCachedPixels * sizeof(CLPixelPacket);
3373 totalLocalMemorySize = imageCacheLocalMemorySize;
3374
3375 /* local size for the pixel accumulator */
3376 pixelAccumulatorLocalMemorySize = chunkSize * sizeof(cl_float4);
3377 totalLocalMemorySize+=pixelAccumulatorLocalMemorySize;
3378
3379 /* local memory size for the weight accumulator */
3380 weightAccumulatorLocalMemorySize = chunkSize * sizeof(float);
3381 totalLocalMemorySize+=weightAccumulatorLocalMemorySize;
3382
3383 /* local memory size for the gamma accumulator */
3384 if (matte == 0)
3385 gammaAccumulatorLocalMemorySize = sizeof(float);
3386 else
3387 gammaAccumulatorLocalMemorySize = chunkSize * sizeof(float);
3388 totalLocalMemorySize+=gammaAccumulatorLocalMemorySize;
3389
3390 if (totalLocalMemorySize <= deviceLocalMemorySize)
3391 break;
3392 else
3393 {
3394 pixelPerWorkgroup = pixelPerWorkgroup/2;
3395 chunkSize = chunkSize/2;
3396 if (pixelPerWorkgroup == 0
3397 || chunkSize == 0)
3398 {
3399 /* quit, fallback to CPU */
3400 goto cleanup;
3401 }
3402 }
3403 }
3404
3405 resizeFilterType = (int)GetResizeFilterWeightingType(resizeFilter);
3406 resizeWindowType = (int)GetResizeFilterWindowWeightingType(resizeFilter);
3407
3408 if (resizeFilterType == SincFastWeightingFunction
3409 && resizeWindowType == SincFastWeightingFunction)
3410 horizontalKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "ResizeVerticalFilterSinc");
3411 else
3412 horizontalKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "ResizeVerticalFilter");
3413
3414 if (horizontalKernel == NULL)
3415 {
cristya22457d2013-12-07 14:03:06 +00003416 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003417 goto cleanup;
3418 }
3419
3420 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00003421 clStatus = clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(cl_mem), (void*)&image);
3422 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&imageColumns);
3423 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&imageRows);
cristy0c832c62014-03-07 22:21:04 +00003424 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&matte);
3425 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&yFactor);
3426 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(cl_mem), (void*)&resizedImage);
cristyf034abb2013-11-24 14:16:14 +00003427
cristy0c832c62014-03-07 22:21:04 +00003428 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&resizedColumns);
3429 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&resizedRows);
cristyf034abb2013-11-24 14:16:14 +00003430
cristy0c832c62014-03-07 22:21:04 +00003431 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(int), (void*)&resizeFilterType);
3432 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(int), (void*)&resizeWindowType);
3433 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(cl_mem), (void*)&resizeFilterCubicCoefficients);
cristyf034abb2013-11-24 14:16:14 +00003434
3435 resizeFilterScale = (float) GetResizeFilterScale(resizeFilter);
cristy0c832c62014-03-07 22:21:04 +00003436 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterScale);
cristyf034abb2013-11-24 14:16:14 +00003437
3438 resizeFilterSupport = (float) GetResizeFilterSupport(resizeFilter);
cristy0c832c62014-03-07 22:21:04 +00003439 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterSupport);
cristyf034abb2013-11-24 14:16:14 +00003440
3441 resizeFilterWindowSupport = (float) GetResizeFilterWindowSupport(resizeFilter);
cristy0c832c62014-03-07 22:21:04 +00003442 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterWindowSupport);
cristyf034abb2013-11-24 14:16:14 +00003443
3444 resizeFilterBlur = (float) GetResizeFilterBlur(resizeFilter);
cristy0c832c62014-03-07 22:21:04 +00003445 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterBlur);
cristyf034abb2013-11-24 14:16:14 +00003446
3447
cristy0c832c62014-03-07 22:21:04 +00003448 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, imageCacheLocalMemorySize, NULL);
3449 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(int), &numCachedPixels);
3450 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), &pixelPerWorkgroup);
3451 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), &chunkSize);
cristyf034abb2013-11-24 14:16:14 +00003452
3453
cristy0c832c62014-03-07 22:21:04 +00003454 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, pixelAccumulatorLocalMemorySize, NULL);
3455 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, weightAccumulatorLocalMemorySize, NULL);
3456 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, gammaAccumulatorLocalMemorySize, NULL);
cristyf034abb2013-11-24 14:16:14 +00003457
3458 if (clStatus != CL_SUCCESS)
3459 {
cristy0c832c62014-03-07 22:21:04 +00003460 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003461 goto cleanup;
3462 }
3463
3464 global_work_size[0] = resizedColumns;
3465 global_work_size[1] = (resizedRows+pixelPerWorkgroup-1)/pixelPerWorkgroup*workgroupSize;
3466
3467 local_work_size[0] = 1;
3468 local_work_size[1] = workgroupSize;
cristy0c832c62014-03-07 22:21:04 +00003469 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, horizontalKernel, 2, NULL, global_work_size, local_work_size, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00003470 if (clStatus != CL_SUCCESS)
3471 {
cristy0c832c62014-03-07 22:21:04 +00003472 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003473 goto cleanup;
3474 }
cristy0c832c62014-03-07 22:21:04 +00003475 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00003476 status = MagickTrue;
3477
3478
3479cleanup:
cristya22457d2013-12-07 14:03:06 +00003480 OpenCLLogException(__FUNCTION__,__LINE__,exception);
3481
cristyf034abb2013-11-24 14:16:14 +00003482 if (horizontalKernel != NULL) RelinquishOpenCLKernel(clEnv, horizontalKernel);
3483
dirk8a5cf512014-07-28 20:16:27 +00003484 return(status);
cristyf034abb2013-11-24 14:16:14 +00003485}
3486
dirk832becc2014-08-04 19:44:34 +00003487static Image *ComputeResizeImage(const Image* image,
3488 const size_t resizedColumns,const size_t resizedRows,
3489 const ResizeFilter *resizeFilter,ExceptionInfo *exception)
3490{
3491 CacheView
3492 *filteredImage_view,
3493 *image_view;
3494
3495 cl_command_queue
3496 queue;
3497
3498 cl_int
3499 clStatus;
3500
3501 cl_context
3502 context;
3503
3504 cl_mem
3505 cubicCoefficientsBuffer,
3506 filteredImageBuffer,
3507 imageBuffer,
3508 tempImageBuffer;
3509
3510 cl_mem_flags
3511 mem_flags;
3512
3513 const double
3514 *resizeFilterCoefficient;
3515
3516 const void
3517 *inputPixels;
3518
3519 float
3520 *mappedCoefficientBuffer,
3521 xFactor,
3522 yFactor;
3523
3524 MagickBooleanType
3525 outputReady,
3526 status;
3527
3528 MagickCLEnv
3529 clEnv;
3530
3531 MagickSizeType
3532 length;
3533
3534 Image
3535 *filteredImage;
3536
3537 unsigned int
3538 i;
3539
3540 void
3541 *filteredPixels,
3542 *hostPtr;
3543
3544 outputReady = MagickFalse;
3545 filteredImage = NULL;
3546 filteredImage_view = NULL;
3547 clEnv = NULL;
3548 context = NULL;
3549 imageBuffer = NULL;
3550 tempImageBuffer = NULL;
3551 filteredImageBuffer = NULL;
3552 cubicCoefficientsBuffer = NULL;
3553 queue = NULL;
3554
3555 clEnv = GetDefaultOpenCLEnv();
3556 context = GetOpenCLContext(clEnv);
3557
3558 /* Create and initialize OpenCL buffers. */
3559 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00003560 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00003561 if (inputPixels == (const void *) NULL)
3562 {
dirk8a5cf512014-07-28 20:16:27 +00003563 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +00003564 goto cleanup;
3565 }
3566
3567 /* If the host pointer is aligned to the size of CLPixelPacket,
3568 then use the host buffer directly from the GPU; otherwise,
3569 create a buffer on the GPU and copy the data over */
3570 if (ALIGNED(inputPixels,CLPixelPacket))
3571 {
3572 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
3573 }
3574 else
3575 {
3576 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
3577 }
3578 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00003579 length = image->columns * image->rows;
3580 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003581 if (clStatus != CL_SUCCESS)
3582 {
cristy0c832c62014-03-07 22:21:04 +00003583 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003584 goto cleanup;
3585 }
3586
cristy0c832c62014-03-07 22:21:04 +00003587 cubicCoefficientsBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY, 7 * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003588 if (clStatus != CL_SUCCESS)
3589 {
cristy0c832c62014-03-07 22:21:04 +00003590 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003591 goto cleanup;
3592 }
3593 queue = AcquireOpenCLCommandQueue(clEnv);
cristy0c832c62014-03-07 22:21:04 +00003594 mappedCoefficientBuffer = (float*)clEnv->library->clEnqueueMapBuffer(queue, cubicCoefficientsBuffer, CL_TRUE, CL_MAP_WRITE, 0, 7 * sizeof(float)
cristyf034abb2013-11-24 14:16:14 +00003595 , 0, NULL, NULL, &clStatus);
3596 if (clStatus != CL_SUCCESS)
3597 {
cristy0c832c62014-03-07 22:21:04 +00003598 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003599 goto cleanup;
3600 }
3601 resizeFilterCoefficient = GetResizeFilterCoefficient(resizeFilter);
3602 for (i = 0; i < 7; i++)
3603 {
3604 mappedCoefficientBuffer[i] = (float) resizeFilterCoefficient[i];
3605 }
cristy0c832c62014-03-07 22:21:04 +00003606 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, cubicCoefficientsBuffer, mappedCoefficientBuffer, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00003607 if (clStatus != CL_SUCCESS)
3608 {
cristy0c832c62014-03-07 22:21:04 +00003609 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003610 goto cleanup;
3611 }
3612
dirk8a5cf512014-07-28 20:16:27 +00003613 filteredImage = CloneImage(image,resizedColumns,resizedRows,MagickTrue,exception);
cristyf034abb2013-11-24 14:16:14 +00003614 if (filteredImage == NULL)
3615 goto cleanup;
3616
dirk8a5cf512014-07-28 20:16:27 +00003617 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristyf034abb2013-11-24 14:16:14 +00003618 {
cristya22457d2013-12-07 14:03:06 +00003619 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003620 goto cleanup;
3621 }
dirk832becc2014-08-04 19:44:34 +00003622 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +00003623 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00003624 if (filteredPixels == (void *) NULL)
3625 {
cristya22457d2013-12-07 14:03:06 +00003626 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename);
cristyf034abb2013-11-24 14:16:14 +00003627 goto cleanup;
3628 }
3629
3630 if (ALIGNED(filteredPixels,CLPixelPacket))
3631 {
3632 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
3633 hostPtr = filteredPixels;
3634 }
3635 else
3636 {
3637 mem_flags = CL_MEM_WRITE_ONLY;
3638 hostPtr = NULL;
3639 }
3640
3641 /* create a CL buffer from image pixel buffer */
3642 length = filteredImage->columns * filteredImage->rows;
cristy0c832c62014-03-07 22:21:04 +00003643 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003644 if (clStatus != CL_SUCCESS)
3645 {
cristy0c832c62014-03-07 22:21:04 +00003646 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003647 goto cleanup;
3648 }
3649
dirk8a5cf512014-07-28 20:16:27 +00003650 xFactor=(float) resizedColumns/(float) image->columns;
3651 yFactor=(float) resizedRows/(float) image->rows;
cristyf034abb2013-11-24 14:16:14 +00003652 if (xFactor > yFactor)
3653 {
3654
dirk8a5cf512014-07-28 20:16:27 +00003655 length = resizedColumns*image->rows;
cristy0c832c62014-03-07 22:21:04 +00003656 tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length*sizeof(CLPixelPacket), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003657 if (clStatus != CL_SUCCESS)
3658 {
cristy0c832c62014-03-07 22:21:04 +00003659 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003660 goto cleanup;
3661 }
3662
dirkb0d783f2014-08-31 10:48:05 +00003663 status = resizeHorizontalFilter(imageBuffer, (unsigned int) image->columns, (unsigned int) image->rows, (image->alpha_trait==BlendPixelTrait)?1:0
3664 , tempImageBuffer, (unsigned int) resizedColumns, (unsigned int) image->rows
cristyf034abb2013-11-24 14:16:14 +00003665 , resizeFilter, cubicCoefficientsBuffer
3666 , xFactor, clEnv, queue, exception);
3667 if (status != MagickTrue)
3668 goto cleanup;
3669
dirkb0d783f2014-08-31 10:48:05 +00003670 status = resizeVerticalFilter(tempImageBuffer, (unsigned int) resizedColumns, (unsigned int) image->rows, (image->alpha_trait==BlendPixelTrait)?1:0
3671 , filteredImageBuffer, (unsigned int) resizedColumns, (unsigned int) resizedRows
cristyf034abb2013-11-24 14:16:14 +00003672 , resizeFilter, cubicCoefficientsBuffer
3673 , yFactor, clEnv, queue, exception);
3674 if (status != MagickTrue)
3675 goto cleanup;
3676 }
3677 else
3678 {
dirk8a5cf512014-07-28 20:16:27 +00003679 length = image->columns*resizedRows;
cristy0c832c62014-03-07 22:21:04 +00003680 tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length*sizeof(CLPixelPacket), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003681 if (clStatus != CL_SUCCESS)
3682 {
cristy0c832c62014-03-07 22:21:04 +00003683 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003684 goto cleanup;
3685 }
3686
dirkb0d783f2014-08-31 10:48:05 +00003687 status = resizeVerticalFilter(imageBuffer, (unsigned int) image->columns, (unsigned int) image->rows, (image->alpha_trait==BlendPixelTrait)?1:0
3688 , tempImageBuffer, (unsigned int) image->columns, (unsigned int) resizedRows
cristyf034abb2013-11-24 14:16:14 +00003689 , resizeFilter, cubicCoefficientsBuffer
3690 , yFactor, clEnv, queue, exception);
3691 if (status != MagickTrue)
3692 goto cleanup;
3693
dirkb0d783f2014-08-31 10:48:05 +00003694 status = resizeHorizontalFilter(tempImageBuffer, (unsigned int) image->columns, (unsigned int) resizedRows, (image->alpha_trait==BlendPixelTrait)?1:0
3695 , filteredImageBuffer, (unsigned int) resizedColumns, (unsigned int) resizedRows
cristyf034abb2013-11-24 14:16:14 +00003696 , resizeFilter, cubicCoefficientsBuffer
3697 , xFactor, clEnv, queue, exception);
3698 if (status != MagickTrue)
3699 goto cleanup;
3700 }
3701 length = resizedColumns*resizedRows;
3702 if (ALIGNED(filteredPixels,CLPixelPacket))
3703 {
cristy0c832c62014-03-07 22:21:04 +00003704 clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003705 }
3706 else
3707 {
cristy0c832c62014-03-07 22:21:04 +00003708 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00003709 }
3710 if (clStatus != CL_SUCCESS)
3711 {
cristya22457d2013-12-07 14:03:06 +00003712 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003713 goto cleanup;
3714 }
dirk832becc2014-08-04 19:44:34 +00003715 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
3716
3717cleanup:
3718 OpenCLLogException(__FUNCTION__,__LINE__,exception);
3719
3720 image_view=DestroyCacheView(image_view);
3721 if (filteredImage_view != NULL)
dirk8a5cf512014-07-28 20:16:27 +00003722 filteredImage_view=DestroyCacheView(filteredImage_view);
cristyf034abb2013-11-24 14:16:14 +00003723
dirk8a5cf512014-07-28 20:16:27 +00003724 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00003725 if (tempImageBuffer!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer);
3726 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
3727 if (cubicCoefficientsBuffer!=NULL) clEnv->library->clReleaseMemObject(cubicCoefficientsBuffer);
cristyf034abb2013-11-24 14:16:14 +00003728 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
dirk8a5cf512014-07-28 20:16:27 +00003729 if (outputReady == MagickFalse && filteredImage != NULL)
3730 filteredImage=DestroyImage(filteredImage);
3731 return(filteredImage);
cristyf034abb2013-11-24 14:16:14 +00003732}
3733
dirk832becc2014-08-04 19:44:34 +00003734const ResizeWeightingFunctionType supportedResizeWeighting[] =
3735{
3736 BoxWeightingFunction,
3737 TriangleWeightingFunction,
3738 HanningWeightingFunction,
3739 HammingWeightingFunction,
3740 BlackmanWeightingFunction,
3741 CubicBCWeightingFunction,
3742 SincWeightingFunction,
3743 SincFastWeightingFunction,
3744 LastWeightingFunction
cristyf034abb2013-11-24 14:16:14 +00003745};
3746
dirk832becc2014-08-04 19:44:34 +00003747static MagickBooleanType gpuSupportedResizeWeighting(
3748 ResizeWeightingFunctionType f)
3749{
3750 unsigned int
3751 i;
3752
3753 for (i = 0; ;i++)
3754 {
3755 if (supportedResizeWeighting[i] == LastWeightingFunction)
3756 break;
3757 if (supportedResizeWeighting[i] == f)
3758 return(MagickTrue);
3759 }
3760 return(MagickFalse);
3761}
3762
3763MagickExport Image *AccelerateResizeImage(const Image *image,
3764 const size_t resizedColumns,const size_t resizedRows,
3765 const ResizeFilter *resizeFilter,ExceptionInfo *exception)
3766{
3767 Image
3768 *filteredImage;
3769
3770 assert(image != NULL);
3771 assert(exception != (ExceptionInfo *) NULL);
3772
3773 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
3774 (checkAccelerateCondition(image, AllChannels) == MagickFalse))
3775 return NULL;
3776
3777 if (gpuSupportedResizeWeighting(GetResizeFilterWeightingType(resizeFilter)) == MagickFalse ||
3778 gpuSupportedResizeWeighting(GetResizeFilterWindowWeightingType(resizeFilter)) == MagickFalse)
3779 return NULL;
3780
3781 filteredImage=ComputeResizeImage(image,resizedColumns,resizedRows,resizeFilter,exception);
3782 return(filteredImage);
cristyf034abb2013-11-24 14:16:14 +00003783}
3784
3785/*
3786%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3787% %
3788% %
3789% %
3790% C o n t r a s t I m a g e w i t h O p e n C L %
3791% %
3792% %
3793% %
3794%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3795%
3796% ContrastImage() enhances the intensity differences between the lighter and
3797% darker elements of the image. Set sharpen to a MagickTrue to increase the
3798% image contrast otherwise the contrast is reduced.
3799%
3800% The format of the ContrastImage method is:
3801%
3802% MagickBooleanType ContrastImage(Image *image,
3803% const MagickBooleanType sharpen)
3804%
3805% A description of each parameter follows:
3806%
3807% o image: the image.
3808%
3809% o sharpen: Increase or decrease image contrast.
3810%
3811*/
3812
dirk832becc2014-08-04 19:44:34 +00003813static MagickBooleanType ComputeContrastImage(Image *image,
3814 const MagickBooleanType sharpen,ExceptionInfo *exception)
3815{
3816 CacheView
3817 *image_view;
3818
3819 cl_command_queue
3820 queue;
3821
3822 cl_context
3823 context;
3824
3825 cl_int
3826 clStatus;
3827
3828 cl_kernel
3829 filterKernel;
3830
3831 cl_mem
3832 imageBuffer;
3833
3834 cl_mem_flags
3835 mem_flags;
3836
3837 MagickBooleanType
3838 outputReady;
3839
3840 MagickCLEnv
3841 clEnv;
3842
3843 MagickSizeType
3844 length;
3845
3846 size_t
3847 global_work_size[2];
3848
3849 unsigned int
3850 i,
3851 uSharpen;
3852
3853 void
3854 *inputPixels;
3855
3856 outputReady = MagickFalse;
3857 clEnv = NULL;
3858 inputPixels = NULL;
3859 context = NULL;
3860 imageBuffer = NULL;
3861 filterKernel = NULL;
3862 queue = NULL;
3863
3864 clEnv = GetDefaultOpenCLEnv();
3865 context = GetOpenCLContext(clEnv);
3866
3867 /* Create and initialize OpenCL buffers. */
3868 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00003869 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00003870 if (inputPixels == (void *) NULL)
cristyd43a46b2010-01-21 02:13:41 +00003871 {
dirk8a5cf512014-07-28 20:16:27 +00003872 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +00003873 goto cleanup;
cristyd43a46b2010-01-21 02:13:41 +00003874 }
cristyf034abb2013-11-24 14:16:14 +00003875
3876 /* If the host pointer is aligned to the size of CLPixelPacket,
dirk8a5cf512014-07-28 20:16:27 +00003877 then use the host buffer directly from the GPU; otherwise,
3878 create a buffer on the GPU and copy the data over */
cristyf034abb2013-11-24 14:16:14 +00003879 if (ALIGNED(inputPixels,CLPixelPacket))
3880 {
3881 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
3882 }
3883 else
3884 {
3885 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
3886 }
3887 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00003888 length = image->columns * image->rows;
3889 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003890 if (clStatus != CL_SUCCESS)
3891 {
cristy0c832c62014-03-07 22:21:04 +00003892 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003893 goto cleanup;
3894 }
dirk8a5cf512014-07-28 20:16:27 +00003895
3896 filterKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Contrast");
3897 if (filterKernel == NULL)
cristyf034abb2013-11-24 14:16:14 +00003898 {
cristya22457d2013-12-07 14:03:06 +00003899 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003900 goto cleanup;
3901 }
3902
cristyf034abb2013-11-24 14:16:14 +00003903 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00003904 clStatus=clEnv->library->clSetKernelArg(filterKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
3905
3906 uSharpen = (sharpen == MagickFalse)?0:1;
3907 clStatus|=clEnv->library->clSetKernelArg(filterKernel,i++,sizeof(cl_uint),&uSharpen);
cristyf034abb2013-11-24 14:16:14 +00003908 if (clStatus != CL_SUCCESS)
3909 {
cristy0c832c62014-03-07 22:21:04 +00003910 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003911 goto cleanup;
3912 }
3913
dirk8a5cf512014-07-28 20:16:27 +00003914 global_work_size[0] = image->columns;
3915 global_work_size[1] = image->rows;
3916 /* launch the kernel */
3917 queue = AcquireOpenCLCommandQueue(clEnv);
3918 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, filterKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
3919 if (clStatus != CL_SUCCESS)
cristyf034abb2013-11-24 14:16:14 +00003920 {
dirk8a5cf512014-07-28 20:16:27 +00003921 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
3922 goto cleanup;
cristyf034abb2013-11-24 14:16:14 +00003923 }
dirk8a5cf512014-07-28 20:16:27 +00003924 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00003925
3926 if (ALIGNED(inputPixels,CLPixelPacket))
3927 {
dirk8a5cf512014-07-28 20:16:27 +00003928 length = image->columns * image->rows;
3929 clEnv->library->clEnqueueMapBuffer(queue, imageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003930 }
3931 else
3932 {
dirk8a5cf512014-07-28 20:16:27 +00003933 length = image->columns * image->rows;
3934 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00003935 }
3936 if (clStatus != CL_SUCCESS)
3937 {
cristya22457d2013-12-07 14:03:06 +00003938 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003939 goto cleanup;
3940 }
dirk832becc2014-08-04 19:44:34 +00003941 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
3942
3943cleanup:
3944 OpenCLLogException(__FUNCTION__,__LINE__,exception);
3945
dirk8a5cf512014-07-28 20:16:27 +00003946 image_view=DestroyCacheView(image_view);
cristyf034abb2013-11-24 14:16:14 +00003947
dirk8a5cf512014-07-28 20:16:27 +00003948 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
3949 if (filterKernel!=NULL) RelinquishOpenCLKernel(clEnv, filterKernel);
3950 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
3951 return(outputReady);
dirk832becc2014-08-04 19:44:34 +00003952}
3953
3954MagickExport MagickBooleanType AccelerateContrastImage(Image *image,
3955 const MagickBooleanType sharpen,ExceptionInfo *exception)
3956{
3957 MagickBooleanType
3958 status;
3959
3960 assert(image != NULL);
3961 assert(exception != (ExceptionInfo *) NULL);
3962
3963 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
3964 (checkAccelerateCondition(image, AllChannels) == MagickFalse))
3965 return(MagickFalse);
3966
3967 status = ComputeContrastImage(image,sharpen,exception);
3968 return(status);
cristy3f6d1482010-01-20 21:01:21 +00003969}
cristyf034abb2013-11-24 14:16:14 +00003970
3971/*
3972%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3973% %
3974% %
3975% %
3976% M o d u l a t e I m a g e w i t h O p e n C L %
3977% %
3978% %
3979% %
3980%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3981%
3982% ModulateImage() lets you control the brightness, saturation, and hue
3983% of an image. Modulate represents the brightness, saturation, and hue
3984% as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
3985% modulation is lightness, saturation, and hue. For HWB, use blackness,
3986% whiteness, and hue. And for HCL, use chrome, luma, and hue.
3987%
3988% The format of the ModulateImage method is:
3989%
3990% MagickBooleanType ModulateImage(Image *image,const char *modulate)
3991%
3992% A description of each parameter follows:
3993%
3994% o image: the image.
3995%
3996% o percent_*: Define the percent change in brightness, saturation, and
3997% hue.
3998%
3999*/
4000
dirk832becc2014-08-04 19:44:34 +00004001MagickBooleanType ComputeModulateImage(Image *image,
4002 double percent_brightness,double percent_hue,double percent_saturation,
4003 ColorspaceType colorspace,ExceptionInfo *exception)
4004{
4005 CacheView
4006 *image_view;
4007
4008 cl_float
4009 bright,
4010 hue,
4011 saturation;
4012
4013 cl_context
4014 context;
4015
4016 cl_command_queue
4017 queue;
4018
4019 cl_int
4020 color,
4021 clStatus;
4022
4023 cl_kernel
4024 modulateKernel;
4025
4026 cl_mem
4027 imageBuffer;
4028
4029 cl_mem_flags
4030 mem_flags;
4031
4032 MagickBooleanType
4033 outputReady;
4034
4035 MagickCLEnv
4036 clEnv;
4037
4038 MagickSizeType
4039 length;
4040
4041 register ssize_t
4042 i;
4043
4044 void
4045 *inputPixels;
4046
4047 inputPixels = NULL;
4048 imageBuffer = NULL;
4049 modulateKernel = NULL;
4050
4051 assert(image != (Image *) NULL);
4052 assert(image->signature == MagickSignature);
4053 if (image->debug != MagickFalse)
4054 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4055
4056 /*
4057 * initialize opencl env
4058 */
4059 clEnv = GetDefaultOpenCLEnv();
4060 context = GetOpenCLContext(clEnv);
4061 queue = AcquireOpenCLCommandQueue(clEnv);
4062
4063 outputReady = MagickFalse;
4064
4065 /* Create and initialize OpenCL buffers.
4066 inputPixels = AcquirePixelCachePixels(image, &length, exception);
4067 assume this will get a writable image
4068 */
4069 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00004070 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristy0c832c62014-03-07 22:21:04 +00004071 if (inputPixels == (void *) NULL)
4072 {
dirk8a5cf512014-07-28 20:16:27 +00004073 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristy0c832c62014-03-07 22:21:04 +00004074 goto cleanup;
4075 }
4076
4077 /* If the host pointer is aligned to the size of CLPixelPacket,
4078 then use the host buffer directly from the GPU; otherwise,
4079 create a buffer on the GPU and copy the data over
4080 */
4081 if (ALIGNED(inputPixels,CLPixelPacket))
4082 {
4083 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
4084 }
4085 else
4086 {
4087 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
4088 }
4089 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00004090 length = image->columns * image->rows;
4091 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristy0c832c62014-03-07 22:21:04 +00004092 if (clStatus != CL_SUCCESS)
4093 {
4094 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
4095 goto cleanup;
4096 }
4097
dirk8a5cf512014-07-28 20:16:27 +00004098 modulateKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Modulate");
4099 if (modulateKernel == NULL)
cristy0c832c62014-03-07 22:21:04 +00004100 {
4101 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
4102 goto cleanup;
4103 }
4104
dirk8a5cf512014-07-28 20:16:27 +00004105 bright=percent_brightness;
4106 hue=percent_hue;
4107 saturation=percent_saturation;
4108 color=colorspace;
4109
cristy0c832c62014-03-07 22:21:04 +00004110 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00004111 clStatus=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
4112 clStatus|=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_float),&bright);
4113 clStatus|=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_float),&hue);
4114 clStatus|=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_float),&saturation);
4115 clStatus|=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_float),&color);
cristy0c832c62014-03-07 22:21:04 +00004116 if (clStatus != CL_SUCCESS)
4117 {
4118 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
4119 printf("no kernel\n");
4120 goto cleanup;
4121 }
4122
4123 {
4124 size_t global_work_size[2];
dirk8a5cf512014-07-28 20:16:27 +00004125 global_work_size[0] = image->columns;
4126 global_work_size[1] = image->rows;
cristy0c832c62014-03-07 22:21:04 +00004127 /* launch the kernel */
dirk8a5cf512014-07-28 20:16:27 +00004128 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, modulateKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristy0c832c62014-03-07 22:21:04 +00004129 if (clStatus != CL_SUCCESS)
4130 {
4131 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
4132 goto cleanup;
4133 }
4134 clEnv->library->clFlush(queue);
4135 }
4136
4137 if (ALIGNED(inputPixels,CLPixelPacket))
4138 {
dirk8a5cf512014-07-28 20:16:27 +00004139 length = image->columns * image->rows;
4140 clEnv->library->clEnqueueMapBuffer(queue, imageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristy0c832c62014-03-07 22:21:04 +00004141 }
4142 else
4143 {
dirk8a5cf512014-07-28 20:16:27 +00004144 length = image->columns * image->rows;
4145 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
cristy0c832c62014-03-07 22:21:04 +00004146 }
4147 if (clStatus != CL_SUCCESS)
4148 {
4149 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
4150 goto cleanup;
4151 }
4152
dirk832becc2014-08-04 19:44:34 +00004153 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
4154
4155cleanup:
4156 OpenCLLogException(__FUNCTION__,__LINE__,exception);
4157
dirk8a5cf512014-07-28 20:16:27 +00004158 image_view=DestroyCacheView(image_view);
cristy0c832c62014-03-07 22:21:04 +00004159
dirk8a5cf512014-07-28 20:16:27 +00004160 if (imageBuffer!=NULL)
4161 clEnv->library->clReleaseMemObject(imageBuffer);
4162 if (modulateKernel!=NULL)
4163 RelinquishOpenCLKernel(clEnv, modulateKernel);
cristy0c832c62014-03-07 22:21:04 +00004164 if (queue != NULL)
4165 RelinquishOpenCLCommandQueue(clEnv, queue);
4166
4167 return outputReady;
4168
4169}
4170
dirk832becc2014-08-04 19:44:34 +00004171MagickExport MagickBooleanType AccelerateModulateImage(Image *image,
4172 double percent_brightness,double percent_hue,double percent_saturation,
4173 ColorspaceType colorspace,ExceptionInfo *exception)
4174{
4175 MagickBooleanType
4176 status;
4177
4178 assert(image != NULL);
4179 assert(exception != (ExceptionInfo *) NULL);
4180
4181 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
4182 (checkAccelerateCondition(image, AllChannels) == MagickFalse))
4183 return(MagickFalse);
4184
4185 if ((colorspace != HSLColorspace && colorspace != UndefinedColorspace))
4186 return(MagickFalse);
4187
4188 status = ComputeModulateImage(image,percent_brightness, percent_hue, percent_saturation, colorspace, exception);
4189 return(status);
dirk8a5cf512014-07-28 20:16:27 +00004190}
cristy0c832c62014-03-07 22:21:04 +00004191
4192/*
4193%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4194% %
4195% %
4196% %
cristy0c832c62014-03-07 22:21:04 +00004197% G r a y s c a l e I m a g e w i t h O p e n C L %
4198% %
4199% %
4200% %
4201%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4202%
4203% GrayscaleImage() converts the colors in the reference image to gray.
4204%
4205% The format of the GrayscaleImageChannel method is:
4206%
4207% MagickBooleanType GrayscaleImage(Image *image,
4208% const PixelIntensityMethod method)
4209%
4210% A description of each parameter follows:
4211%
4212% o image: the image.
4213%
4214% o channel: the channel.
4215%
4216*/
4217
dirk832becc2014-08-04 19:44:34 +00004218MagickBooleanType ComputeGrayscaleImage(Image *image,
4219 const PixelIntensityMethod method,ExceptionInfo *exception)
4220{
4221 CacheView
4222 *image_view;
4223
4224 cl_command_queue
4225 queue;
4226
4227 cl_context
4228 context;
4229
4230 cl_int
4231 clStatus,
4232 intensityMethod;
4233
4234 cl_int
4235 colorspace;
4236
4237 cl_kernel
4238 grayscaleKernel;
4239
4240 cl_mem
4241 imageBuffer;
4242
4243 cl_mem_flags
4244 mem_flags;
4245
4246 MagickBooleanType
4247 outputReady;
4248
4249 MagickCLEnv
4250 clEnv;
4251
4252 MagickSizeType
4253 length;
4254
4255 register ssize_t
4256 i;
4257
4258 void
4259 *inputPixels;
4260
4261 inputPixels = NULL;
4262 imageBuffer = NULL;
4263 grayscaleKernel = NULL;
4264
4265 assert(image != (Image *) NULL);
4266 assert(image->signature == MagickSignature);
4267 if (image->debug != MagickFalse)
4268 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4269
4270 /*
4271 * initialize opencl env
4272 */
4273 clEnv = GetDefaultOpenCLEnv();
4274 context = GetOpenCLContext(clEnv);
4275 queue = AcquireOpenCLCommandQueue(clEnv);
4276
4277 outputReady = MagickFalse;
4278
4279 /* Create and initialize OpenCL buffers.
4280 inputPixels = AcquirePixelCachePixels(image, &length, exception);
4281 assume this will get a writable image
4282 */
4283 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00004284 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
4285 if (inputPixels == (void *) NULL)
4286 {
4287 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
4288 goto cleanup;
4289 }
cristy0c832c62014-03-07 22:21:04 +00004290
dirk8a5cf512014-07-28 20:16:27 +00004291 /* If the host pointer is aligned to the size of CLPixelPacket,
4292 then use the host buffer directly from the GPU; otherwise,
4293 create a buffer on the GPU and copy the data over
4294 */
4295 if (ALIGNED(inputPixels,CLPixelPacket))
4296 {
4297 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
4298 }
4299 else
4300 {
4301 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
4302 }
4303 /* create a CL buffer from image pixel buffer */
4304 length = image->columns * image->rows;
4305 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
4306 if (clStatus != CL_SUCCESS)
4307 {
4308 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
4309 goto cleanup;
4310 }
cristy0c832c62014-03-07 22:21:04 +00004311
dirk8a5cf512014-07-28 20:16:27 +00004312 intensityMethod = method;
4313 colorspace = image->colorspace;
cristy0c832c62014-03-07 22:21:04 +00004314
dirk8a5cf512014-07-28 20:16:27 +00004315 grayscaleKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Grayscale");
4316 if (grayscaleKernel == NULL)
4317 {
4318 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
4319 goto cleanup;
4320 }
cristy0c832c62014-03-07 22:21:04 +00004321
dirk8a5cf512014-07-28 20:16:27 +00004322 i = 0;
4323 clStatus=clEnv->library->clSetKernelArg(grayscaleKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
4324 clStatus|=clEnv->library->clSetKernelArg(grayscaleKernel,i++,sizeof(cl_int),&intensityMethod);
4325 clStatus|=clEnv->library->clSetKernelArg(grayscaleKernel,i++,sizeof(cl_int),&colorspace);
4326 if (clStatus != CL_SUCCESS)
4327 {
4328 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
4329 printf("no kernel\n");
4330 goto cleanup;
4331 }
cristy0c832c62014-03-07 22:21:04 +00004332
dirk8a5cf512014-07-28 20:16:27 +00004333 {
4334 size_t global_work_size[2];
4335 global_work_size[0] = image->columns;
4336 global_work_size[1] = image->rows;
4337 /* launch the kernel */
4338 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, grayscaleKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
4339 if (clStatus != CL_SUCCESS)
4340 {
4341 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
4342 goto cleanup;
4343 }
4344 clEnv->library->clFlush(queue);
4345 }
cristy0c832c62014-03-07 22:21:04 +00004346
dirk8a5cf512014-07-28 20:16:27 +00004347 if (ALIGNED(inputPixels,CLPixelPacket))
4348 {
4349 length = image->columns * image->rows;
4350 clEnv->library->clEnqueueMapBuffer(queue, imageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
4351 }
4352 else
4353 {
4354 length = image->columns * image->rows;
4355 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
4356 }
4357 if (clStatus != CL_SUCCESS)
4358 {
4359 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
4360 goto cleanup;
4361 }
cristy0c832c62014-03-07 22:21:04 +00004362
dirk832becc2014-08-04 19:44:34 +00004363 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
4364
4365cleanup:
4366 OpenCLLogException(__FUNCTION__,__LINE__,exception);
4367
dirk8a5cf512014-07-28 20:16:27 +00004368 image_view=DestroyCacheView(image_view);
4369
4370 if (imageBuffer!=NULL)
4371 clEnv->library->clReleaseMemObject(imageBuffer);
4372 if (grayscaleKernel!=NULL)
4373 RelinquishOpenCLKernel(clEnv, grayscaleKernel);
4374 if (queue != NULL)
4375 RelinquishOpenCLCommandQueue(clEnv, queue);
4376
4377 return( outputReady);
dirk832becc2014-08-04 19:44:34 +00004378}
4379
4380MagickExport MagickBooleanType AccelerateGrayscaleImage(Image* image,
4381 const PixelIntensityMethod method,ExceptionInfo *exception)
4382{
4383 MagickBooleanType
4384 status;
4385
4386 assert(image != NULL);
4387 assert(exception != (ExceptionInfo *) NULL);
4388
4389 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
4390 (checkAccelerateCondition(image, AllChannels) == MagickFalse))
4391 return(MagickFalse);
4392
4393 if (method == Rec601LuminancePixelIntensityMethod || method == Rec709LuminancePixelIntensityMethod)
4394 return(MagickFalse);
4395
4396 if (image->colorspace != sRGBColorspace)
4397 return(MagickFalse);
4398
4399 status=ComputeGrayscaleImage(image,method,exception);
4400 return(status);
cristy0c832c62014-03-07 22:21:04 +00004401}
4402
dirk8a5cf512014-07-28 20:16:27 +00004403/*
4404%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4405% %
4406% %
4407% %
4408% E q u a l i z e I m a g e w i t h O p e n C L %
4409% %
4410% %
4411% %
4412%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4413%
4414% EqualizeImage() applies a histogram equalization to the image.
4415%
4416% The format of the EqualizeImage method is:
4417%
4418% MagickBooleanType EqualizeImage(Image *image)
4419% MagickBooleanType EqualizeImageChannel(Image *image,
4420% const ChannelType channel)
4421%
4422% A description of each parameter follows:
4423%
4424% o image: the image.
4425%
4426% o channel: the channel.
4427%
4428*/
cristy0c832c62014-03-07 22:21:04 +00004429
dirk832becc2014-08-04 19:44:34 +00004430static MagickBooleanType LaunchHistogramKernel(MagickCLEnv clEnv,
4431 cl_command_queue queue,cl_mem imageBuffer,cl_mem histogramBuffer,
4432 Image *image,const ChannelType channel,ExceptionInfo *exception)
4433{
4434 MagickBooleanType
4435 outputReady;
4436
4437 cl_int
4438 clStatus,
4439 colorspace,
4440 method;
4441
4442 cl_kernel
4443 histogramKernel;
4444
4445 register ssize_t
4446 i;
4447
4448 size_t
4449 global_work_size[2];
4450
cristy0c832c62014-03-07 22:21:04 +00004451 histogramKernel = NULL;
4452
4453 outputReady = MagickFalse;
dirk8a5cf512014-07-28 20:16:27 +00004454 method = image->intensity;
4455 colorspace = image->colorspace;
cristy0c832c62014-03-07 22:21:04 +00004456
4457 /* get the OpenCL kernel */
4458 histogramKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Histogram");
4459 if (histogramKernel == NULL)
4460 {
4461 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
4462 goto cleanup;
4463 }
4464
4465 /* set the kernel arguments */
4466 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00004467 clStatus=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00004468 clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(ChannelType),&channel);
4469 clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_int),&method);
4470 clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_int),&colorspace);
4471 clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_mem),(void *)&histogramBuffer);
4472 if (clStatus != CL_SUCCESS)
4473 {
4474 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
4475 goto cleanup;
4476 }
4477
4478 /* launch the kernel */
dirk8a5cf512014-07-28 20:16:27 +00004479 global_work_size[0] = image->columns;
4480 global_work_size[1] = image->rows;
cristy0c832c62014-03-07 22:21:04 +00004481
4482 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, histogramKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
4483
4484 if (clStatus != CL_SUCCESS)
4485 {
4486 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
4487 goto cleanup;
4488 }
4489 clEnv->library->clFlush(queue);
4490
4491 outputReady = MagickTrue;
4492
4493cleanup:
4494 OpenCLLogException(__FUNCTION__,__LINE__,exception);
4495
4496 if (histogramKernel!=NULL)
4497 RelinquishOpenCLKernel(clEnv, histogramKernel);
4498
dirk8a5cf512014-07-28 20:16:27 +00004499 return(outputReady);
cristy0c832c62014-03-07 22:21:04 +00004500}
4501
dirk832becc2014-08-04 19:44:34 +00004502MagickExport MagickBooleanType ComputeEqualizeImage(Image *image,
4503 const ChannelType channel,ExceptionInfo *exception)
4504{
4505#define EqualizeImageTag "Equalize/Image"
4506
4507 CacheView
4508 *image_view;
4509
4510 cl_command_queue
4511 queue;
4512
4513 cl_context
4514 context;
4515
4516 cl_int
4517 clStatus;
4518
4519 cl_mem_flags
4520 mem_flags;
4521
4522 cl_mem
4523 equalizeMapBuffer,
4524 histogramBuffer,
4525 imageBuffer;
4526
4527 cl_kernel
4528 equalizeKernel,
4529 histogramKernel;
4530
4531 cl_uint4
4532 *histogram;
4533
4534 FloatPixelPacket
4535 white,
4536 black,
4537 intensity,
4538 *map;
4539
4540 MagickBooleanType
4541 outputReady,
4542 status;
4543
4544 MagickCLEnv
4545 clEnv;
4546
4547 MagickSizeType
4548 length;
4549
4550 PixelPacket
4551 *equalize_map;
4552
4553 register ssize_t
4554 i;
4555
4556 size_t
4557 global_work_size[2];
4558
4559 void
4560 *hostPtr,
4561 *inputPixels;
4562
4563 map=NULL;
4564 histogram=NULL;
4565 equalize_map=NULL;
4566 inputPixels = NULL;
4567 imageBuffer = NULL;
4568 histogramBuffer = NULL;
4569 equalizeMapBuffer = NULL;
4570 histogramKernel = NULL;
4571 equalizeKernel = NULL;
4572 context = NULL;
4573 queue = NULL;
4574 outputReady = MagickFalse;
4575
4576 assert(image != (Image *) NULL);
4577 assert(image->signature == MagickSignature);
4578 if (image->debug != MagickFalse)
4579 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4580
4581 /*
4582 * initialize opencl env
4583 */
4584 clEnv = GetDefaultOpenCLEnv();
4585 context = GetOpenCLContext(clEnv);
4586 queue = AcquireOpenCLCommandQueue(clEnv);
4587
4588 /*
4589 Allocate and initialize histogram arrays.
4590 */
4591 histogram=(cl_uint4 *) AcquireQuantumMemory(MaxMap+1UL, sizeof(*histogram));
4592 if (histogram == (cl_uint4 *) NULL)
4593 ThrowBinaryException(ResourceLimitWarning,"MemoryAllocationFailed", image->filename);
4594
4595 /* reset histogram */
4596 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
4597
4598 /* Create and initialize OpenCL buffers. */
4599 /* inputPixels = AcquirePixelCachePixels(image, &length, exception); */
4600 /* assume this will get a writable image */
4601 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00004602 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00004603
4604 if (inputPixels == (void *) NULL)
4605 {
dirk8a5cf512014-07-28 20:16:27 +00004606 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +00004607 goto cleanup;
4608 }
4609 /* If the host pointer is aligned to the size of CLPixelPacket,
4610 then use the host buffer directly from the GPU; otherwise,
4611 create a buffer on the GPU and copy the data over */
4612 if (ALIGNED(inputPixels,CLPixelPacket))
4613 {
4614 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
4615 }
4616 else
4617 {
4618 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
4619 }
4620 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00004621 length = image->columns * image->rows;
4622 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00004623 if (clStatus != CL_SUCCESS)
4624 {
cristy0c832c62014-03-07 22:21:04 +00004625 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00004626 goto cleanup;
4627 }
cristy0c832c62014-03-07 22:21:04 +00004628
cristyf034abb2013-11-24 14:16:14 +00004629 /* If the host pointer is aligned to the size of cl_uint,
4630 then use the host buffer directly from the GPU; otherwise,
4631 create a buffer on the GPU and copy the data over */
4632 if (ALIGNED(histogram,cl_uint4))
4633 {
4634 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
4635 hostPtr = histogram;
4636 }
4637 else
4638 {
4639 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
4640 hostPtr = histogram;
4641 }
4642 /* create a CL buffer for histogram */
4643 length = (MaxMap+1);
cristy0c832c62014-03-07 22:21:04 +00004644 histogramBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(cl_uint4), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00004645 if (clStatus != CL_SUCCESS)
4646 {
cristy0c832c62014-03-07 22:21:04 +00004647 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00004648 goto cleanup;
4649 }
4650
dirk8a5cf512014-07-28 20:16:27 +00004651 status = LaunchHistogramKernel(clEnv, queue, imageBuffer, histogramBuffer, image, channel, exception);
cristy0c832c62014-03-07 22:21:04 +00004652 if (status == MagickFalse)
cristyf034abb2013-11-24 14:16:14 +00004653 goto cleanup;
cristyf034abb2013-11-24 14:16:14 +00004654
4655 /* read from the kenel output */
4656 if (ALIGNED(histogram,cl_uint4))
4657 {
4658 length = (MaxMap+1);
cristy0c832c62014-03-07 22:21:04 +00004659 clEnv->library->clEnqueueMapBuffer(queue, histogramBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(cl_uint4), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00004660 }
4661 else
4662 {
4663 length = (MaxMap+1);
cristy0c832c62014-03-07 22:21:04 +00004664 clStatus = clEnv->library->clEnqueueReadBuffer(queue, histogramBuffer, CL_TRUE, 0, length * sizeof(cl_uint4), histogram, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00004665 }
4666 if (clStatus != CL_SUCCESS)
4667 {
cristya22457d2013-12-07 14:03:06 +00004668 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00004669 goto cleanup;
4670 }
4671
4672 /* unmap, don't block gpu to use this buffer again. */
4673 if (ALIGNED(histogram,cl_uint4))
4674 {
cristy0c832c62014-03-07 22:21:04 +00004675 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, histogramBuffer, histogram, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00004676 if (clStatus != CL_SUCCESS)
4677 {
cristy0c832c62014-03-07 22:21:04 +00004678 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00004679 goto cleanup;
4680 }
4681 }
4682
cristy0c832c62014-03-07 22:21:04 +00004683 /* recreate input buffer later, in case image updated */
4684#ifdef RECREATEBUFFER
dirk8a5cf512014-07-28 20:16:27 +00004685 if (imageBuffer!=NULL)
4686 clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00004687#endif
4688
4689 /* CPU stuff */
cristyf034abb2013-11-24 14:16:14 +00004690 equalize_map=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL, sizeof(*equalize_map));
4691 if (equalize_map == (PixelPacket *) NULL)
cristy0c832c62014-03-07 22:21:04 +00004692 ThrowBinaryException(ResourceLimitWarning,"MemoryAllocationFailed", image->filename);
cristyf034abb2013-11-24 14:16:14 +00004693
4694 map=(FloatPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*map));
4695 if (map == (FloatPixelPacket *) NULL)
cristy0c832c62014-03-07 22:21:04 +00004696 ThrowBinaryException(ResourceLimitWarning,"MemoryAllocationFailed", image->filename);
cristyf034abb2013-11-24 14:16:14 +00004697
4698 /*
4699 Integrate the histogram to get the equalization map.
4700 */
4701 (void) ResetMagickMemory(&intensity,0,sizeof(intensity));
4702 for (i=0; i <= (ssize_t) MaxMap; i++)
4703 {
4704 if ((channel & SyncChannels) != 0)
cristy0c832c62014-03-07 22:21:04 +00004705 {
4706 intensity.red+=histogram[i].s[2];
4707 map[i]=intensity;
4708 continue;
4709 }
cristyf034abb2013-11-24 14:16:14 +00004710 if ((channel & RedChannel) != 0)
4711 intensity.red+=histogram[i].s[2];
4712 if ((channel & GreenChannel) != 0)
4713 intensity.green+=histogram[i].s[1];
4714 if ((channel & BlueChannel) != 0)
4715 intensity.blue+=histogram[i].s[0];
4716 if ((channel & OpacityChannel) != 0)
dirk8a5cf512014-07-28 20:16:27 +00004717 intensity.alpha+=histogram[i].s[3];
cristy0c832c62014-03-07 22:21:04 +00004718 /*
cristyf034abb2013-11-24 14:16:14 +00004719 if (((channel & IndexChannel) != 0) &&
4720 (image->colorspace == CMYKColorspace))
4721 {
cristy0c832c62014-03-07 22:21:04 +00004722 intensity.index+=histogram[i].index;
cristyf034abb2013-11-24 14:16:14 +00004723 }
cristy0c832c62014-03-07 22:21:04 +00004724 */
cristyf034abb2013-11-24 14:16:14 +00004725 map[i]=intensity;
4726 }
4727 black=map[0];
4728 white=map[(int) MaxMap];
4729 (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*sizeof(*equalize_map));
4730 for (i=0; i <= (ssize_t) MaxMap; i++)
4731 {
4732 if ((channel & SyncChannels) != 0)
cristy0c832c62014-03-07 22:21:04 +00004733 {
4734 if (white.red != black.red)
4735 equalize_map[i].red=ScaleMapToQuantum((MagickRealType) ((MaxMap*
4736 (map[i].red-black.red))/(white.red-black.red)));
4737 continue;
4738 }
cristyf034abb2013-11-24 14:16:14 +00004739 if (((channel & RedChannel) != 0) && (white.red != black.red))
4740 equalize_map[i].red=ScaleMapToQuantum((MagickRealType) ((MaxMap*
cristy0c832c62014-03-07 22:21:04 +00004741 (map[i].red-black.red))/(white.red-black.red)));
cristyf034abb2013-11-24 14:16:14 +00004742 if (((channel & GreenChannel) != 0) && (white.green != black.green))
4743 equalize_map[i].green=ScaleMapToQuantum((MagickRealType) ((MaxMap*
cristy0c832c62014-03-07 22:21:04 +00004744 (map[i].green-black.green))/(white.green-black.green)));
cristyf034abb2013-11-24 14:16:14 +00004745 if (((channel & BlueChannel) != 0) && (white.blue != black.blue))
4746 equalize_map[i].blue=ScaleMapToQuantum((MagickRealType) ((MaxMap*
cristy0c832c62014-03-07 22:21:04 +00004747 (map[i].blue-black.blue))/(white.blue-black.blue)));
dirk8a5cf512014-07-28 20:16:27 +00004748 if (((channel & OpacityChannel) != 0) && (white.alpha != black.alpha))
4749 equalize_map[i].alpha=ScaleMapToQuantum((MagickRealType) ((MaxMap*
4750 (map[i].alpha-black.alpha))/(white.alpha-black.alpha)));
cristyf034abb2013-11-24 14:16:14 +00004751 /*
4752 if ((((channel & IndexChannel) != 0) &&
cristy0c832c62014-03-07 22:21:04 +00004753 (image->colorspace == CMYKColorspace)) &&
cristyf034abb2013-11-24 14:16:14 +00004754 (white.index != black.index))
4755 equalize_map[i].index=ScaleMapToQuantum((MagickRealType) ((MaxMap*
cristy0c832c62014-03-07 22:21:04 +00004756 (map[i].index-black.index))/(white.index-black.index)));
cristyf034abb2013-11-24 14:16:14 +00004757 */
4758 }
4759
cristyf034abb2013-11-24 14:16:14 +00004760 if (image->storage_class == PseudoClass)
4761 {
cristy0c832c62014-03-07 22:21:04 +00004762 /*
4763 Equalize colormap.
4764 */
4765 for (i=0; i < (ssize_t) image->colors; i++)
4766 {
4767 if ((channel & SyncChannels) != 0)
cristyf034abb2013-11-24 14:16:14 +00004768 {
cristy0c832c62014-03-07 22:21:04 +00004769 if (white.red != black.red)
4770 {
cristyf034abb2013-11-24 14:16:14 +00004771 image->colormap[i].red=equalize_map[
4772 ScaleQuantumToMap(image->colormap[i].red)].red;
cristyf034abb2013-11-24 14:16:14 +00004773 image->colormap[i].green=equalize_map[
cristy0c832c62014-03-07 22:21:04 +00004774 ScaleQuantumToMap(image->colormap[i].green)].red;
cristyf034abb2013-11-24 14:16:14 +00004775 image->colormap[i].blue=equalize_map[
cristy0c832c62014-03-07 22:21:04 +00004776 ScaleQuantumToMap(image->colormap[i].blue)].red;
dirk8a5cf512014-07-28 20:16:27 +00004777 image->colormap[i].alpha=equalize_map[
4778 ScaleQuantumToMap(image->colormap[i].alpha)].red;
cristy0c832c62014-03-07 22:21:04 +00004779 }
4780 continue;
cristyf034abb2013-11-24 14:16:14 +00004781 }
cristy0c832c62014-03-07 22:21:04 +00004782 if (((channel & RedChannel) != 0) && (white.red != black.red))
4783 image->colormap[i].red=equalize_map[
4784 ScaleQuantumToMap(image->colormap[i].red)].red;
4785 if (((channel & GreenChannel) != 0) && (white.green != black.green))
4786 image->colormap[i].green=equalize_map[
4787 ScaleQuantumToMap(image->colormap[i].green)].green;
4788 if (((channel & BlueChannel) != 0) && (white.blue != black.blue))
4789 image->colormap[i].blue=equalize_map[
4790 ScaleQuantumToMap(image->colormap[i].blue)].blue;
4791 if (((channel & OpacityChannel) != 0) &&
dirk8a5cf512014-07-28 20:16:27 +00004792 (white.alpha != black.alpha))
4793 image->colormap[i].alpha=equalize_map[
4794 ScaleQuantumToMap(image->colormap[i].alpha)].alpha;
cristy0c832c62014-03-07 22:21:04 +00004795 }
cristyf034abb2013-11-24 14:16:14 +00004796 }
4797
4798 /*
4799 Equalize image.
4800 */
4801
4802 /* GPU can work on this again, image and equalize map as input
4803 image: uchar4 (CLPixelPacket)
4804 equalize_map: uchar4 (PixelPacket)
4805 black, white: float4 (FloatPixelPacket) */
4806
cristy0c832c62014-03-07 22:21:04 +00004807#ifdef RECREATEBUFFER
cristyf034abb2013-11-24 14:16:14 +00004808 /* If the host pointer is aligned to the size of CLPixelPacket,
4809 then use the host buffer directly from the GPU; otherwise,
4810 create a buffer on the GPU and copy the data over */
4811 if (ALIGNED(inputPixels,CLPixelPacket))
4812 {
4813 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
4814 }
4815 else
4816 {
4817 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
4818 }
4819 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00004820 length = image->columns * image->rows;
4821 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00004822 if (clStatus != CL_SUCCESS)
4823 {
cristy0c832c62014-03-07 22:21:04 +00004824 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00004825 goto cleanup;
4826 }
cristy0c832c62014-03-07 22:21:04 +00004827#endif
cristyf034abb2013-11-24 14:16:14 +00004828
4829 /* Create and initialize OpenCL buffers. */
4830 if (ALIGNED(equalize_map, PixelPacket))
4831 {
4832 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
4833 hostPtr = equalize_map;
4834 }
4835 else
4836 {
4837 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
4838 hostPtr = equalize_map;
4839 }
4840 /* create a CL buffer for eqaulize_map */
4841 length = (MaxMap+1);
cristy0c832c62014-03-07 22:21:04 +00004842 equalizeMapBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(PixelPacket), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00004843 if (clStatus != CL_SUCCESS)
4844 {
cristy0c832c62014-03-07 22:21:04 +00004845 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00004846 goto cleanup;
4847 }
4848
4849 /* get the OpenCL kernel */
4850 equalizeKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Equalize");
4851 if (equalizeKernel == NULL)
4852 {
cristya22457d2013-12-07 14:03:06 +00004853 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00004854 goto cleanup;
4855 }
4856
4857 /* set the kernel arguments */
4858 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00004859 clStatus=clEnv->library->clSetKernelArg(equalizeKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00004860 clStatus|=clEnv->library->clSetKernelArg(equalizeKernel,i++,sizeof(ChannelType),&channel);
4861 clStatus|=clEnv->library->clSetKernelArg(equalizeKernel,i++,sizeof(cl_mem),(void *)&equalizeMapBuffer);
4862 clStatus|=clEnv->library->clSetKernelArg(equalizeKernel,i++,sizeof(FloatPixelPacket),&white);
4863 clStatus|=clEnv->library->clSetKernelArg(equalizeKernel,i++,sizeof(FloatPixelPacket),&black);
cristyf034abb2013-11-24 14:16:14 +00004864 if (clStatus != CL_SUCCESS)
4865 {
cristy0c832c62014-03-07 22:21:04 +00004866 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00004867 goto cleanup;
4868 }
4869
4870 /* launch the kernel */
dirk8a5cf512014-07-28 20:16:27 +00004871 global_work_size[0] = image->columns;
4872 global_work_size[1] = image->rows;
cristyf034abb2013-11-24 14:16:14 +00004873
cristy0c832c62014-03-07 22:21:04 +00004874 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, equalizeKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00004875
4876 if (clStatus != CL_SUCCESS)
4877 {
cristy0c832c62014-03-07 22:21:04 +00004878 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00004879 goto cleanup;
4880 }
cristy0c832c62014-03-07 22:21:04 +00004881 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00004882
4883 /* read the data back */
4884 if (ALIGNED(inputPixels,CLPixelPacket))
4885 {
dirk8a5cf512014-07-28 20:16:27 +00004886 length = image->columns * image->rows;
4887 clEnv->library->clEnqueueMapBuffer(queue, imageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00004888 }
4889 else
4890 {
dirk8a5cf512014-07-28 20:16:27 +00004891 length = image->columns * image->rows;
4892 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00004893 }
4894 if (clStatus != CL_SUCCESS)
4895 {
cristya22457d2013-12-07 14:03:06 +00004896 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00004897 goto cleanup;
4898 }
4899
dirk832becc2014-08-04 19:44:34 +00004900 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
4901
4902cleanup:
4903 OpenCLLogException(__FUNCTION__,__LINE__,exception);
4904
dirk8a5cf512014-07-28 20:16:27 +00004905 image_view=DestroyCacheView(image_view);
cristyf034abb2013-11-24 14:16:14 +00004906
dirk8a5cf512014-07-28 20:16:27 +00004907 if (imageBuffer!=NULL)
4908 clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00004909
4910 if (map!=NULL)
4911 map=(FloatPixelPacket *) RelinquishMagickMemory(map);
4912
4913 if (equalizeMapBuffer!=NULL)
4914 clEnv->library->clReleaseMemObject(equalizeMapBuffer);
4915 if (equalize_map!=NULL)
4916 equalize_map=(PixelPacket *) RelinquishMagickMemory(equalize_map);
4917
cristyf034abb2013-11-24 14:16:14 +00004918 if (histogramBuffer!=NULL)
cristy0c832c62014-03-07 22:21:04 +00004919 clEnv->library->clReleaseMemObject(histogramBuffer);
4920 if (histogram!=NULL)
4921 histogram=(cl_uint4 *) RelinquishMagickMemory(histogram);
4922
cristyf034abb2013-11-24 14:16:14 +00004923 if (histogramKernel!=NULL)
4924 RelinquishOpenCLKernel(clEnv, histogramKernel);
cristy0c832c62014-03-07 22:21:04 +00004925 if (equalizeKernel!=NULL)
4926 RelinquishOpenCLKernel(clEnv, equalizeKernel);
4927
cristyf034abb2013-11-24 14:16:14 +00004928 if (queue != NULL)
4929 RelinquishOpenCLCommandQueue(clEnv, queue);
4930
dirk8a5cf512014-07-28 20:16:27 +00004931 return(outputReady);
dirk832becc2014-08-04 19:44:34 +00004932}
4933
4934MagickExport MagickBooleanType AccelerateEqualizeImage(Image *image,
4935 const ChannelType channel,ExceptionInfo *exception)
4936{
4937 MagickBooleanType
4938 status;
4939
4940 assert(image != NULL);
4941 assert(exception != (ExceptionInfo *) NULL);
4942
4943 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
4944 (checkAccelerateCondition(image, channel) == MagickFalse) ||
4945 (checkHistogramCondition(image, channel) == MagickFalse))
4946 return(MagickFalse);
4947
4948 status=ComputeEqualizeImage(image,channel,exception);
4949 return(status);
cristyf034abb2013-11-24 14:16:14 +00004950}
4951
4952/*
4953%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4954% %
4955% %
4956% %
dirk8a5cf512014-07-28 20:16:27 +00004957% C o n t r a s t S t r e t c h I m a g e w i t h O p e n C L %
cristyf034abb2013-11-24 14:16:14 +00004958% %
4959% %
4960% %
4961%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4962%
dirk8a5cf512014-07-28 20:16:27 +00004963% ContrastStretchImage() is a simple image enhancement technique that attempts
4964% to improve the contrast in an image by `stretching' the range of intensity
4965% values it contains to span a desired range of values. It differs from the
4966% more sophisticated histogram equalization in that it can only apply a
4967% linear scaling function to the image pixel values. As a result the
4968% `enhancement' is less harsh.
cristyf034abb2013-11-24 14:16:14 +00004969%
dirk8a5cf512014-07-28 20:16:27 +00004970% The format of the ContrastStretchImage method is:
cristyf034abb2013-11-24 14:16:14 +00004971%
dirk8a5cf512014-07-28 20:16:27 +00004972% MagickBooleanType ContrastStretchImage(Image *image,
4973% const char *levels)
4974% MagickBooleanType ContrastStretchImageChannel(Image *image,
4975% const size_t channel,const double black_point,
4976% const double white_point)
cristyf034abb2013-11-24 14:16:14 +00004977%
4978% A description of each parameter follows:
4979%
4980% o image: the image.
4981%
4982% o channel: the channel.
4983%
dirk8a5cf512014-07-28 20:16:27 +00004984% o black_point: the black point.
4985%
4986% o white_point: the white point.
4987%
4988% o levels: Specify the levels where the black and white points have the
4989% range of 0 to number-of-pixels (e.g. 1%, 10x90%, etc.).
4990%
cristyf034abb2013-11-24 14:16:14 +00004991*/
4992
dirk832becc2014-08-04 19:44:34 +00004993MagickExport MagickBooleanType ComputeContrastStretchImageChannel(Image *image,
4994 const ChannelType channel,const double black_point,const double white_point,
4995 ExceptionInfo *exception)
4996{
4997#define ContrastStretchImageTag "ContrastStretch/Image"
4998#define MaxRange(color) ((MagickRealType) ScaleQuantumToMap((Quantum) (color)))
4999
5000 CacheView
5001 *image_view;
5002
5003 cl_command_queue
5004 queue;
5005
5006 cl_context
5007 context;
5008
5009 cl_int
5010 clStatus;
5011
5012 cl_mem_flags
5013 mem_flags;
5014
5015 cl_mem
5016 histogramBuffer,
5017 imageBuffer,
5018 stretchMapBuffer;
5019
5020 cl_kernel
5021 histogramKernel,
5022 stretchKernel;
5023
5024 cl_uint4
5025 *histogram;
5026
5027 double
5028 intensity;
5029
5030 FloatPixelPacket
5031 black,
5032 white;
5033
5034 MagickBooleanType
5035 outputReady,
5036 status;
5037
5038 MagickCLEnv
5039 clEnv;
5040
5041 MagickSizeType
5042 length;
5043
5044 PixelPacket
5045 *stretch_map;
5046
5047 register ssize_t
5048 i;
5049
5050 size_t
5051 global_work_size[2];
5052
5053 void
5054 *hostPtr,
5055 *inputPixels;
5056
5057 histogram=NULL;
5058 stretch_map=NULL;
5059 inputPixels = NULL;
5060 imageBuffer = NULL;
5061 histogramBuffer = NULL;
5062 stretchMapBuffer = NULL;
5063 histogramKernel = NULL;
5064 stretchKernel = NULL;
5065 context = NULL;
5066 queue = NULL;
5067 outputReady = MagickFalse;
5068
5069
5070 assert(image != (Image *) NULL);
5071 assert(image->signature == MagickSignature);
5072 if (image->debug != MagickFalse)
5073 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5074
5075 //exception=(&image->exception);
5076
5077 /*
5078 * initialize opencl env
5079 */
5080 clEnv = GetDefaultOpenCLEnv();
5081 context = GetOpenCLContext(clEnv);
5082 queue = AcquireOpenCLCommandQueue(clEnv);
5083
5084 /*
5085 Allocate and initialize histogram arrays.
5086 */
5087 histogram=(cl_uint4 *) AcquireQuantumMemory(MaxMap+1UL, sizeof(*histogram));
5088
5089 if (histogram == (cl_uint4 *) NULL)
5090 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", image->filename);
5091
5092 /* reset histogram */
5093 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
5094
5095 /*
5096 if (IsGrayImage(image,exception) != MagickFalse)
5097 (void) SetImageColorspace(image,GRAYColorspace);
5098 */
5099
5100 status=MagickTrue;
5101
5102
5103 /*
5104 Form histogram.
5105 */
5106 /* Create and initialize OpenCL buffers. */
5107 /* inputPixels = AcquirePixelCachePixels(image, &length, exception); */
5108 /* assume this will get a writable image */
5109 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00005110 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristy0c832c62014-03-07 22:21:04 +00005111
5112 if (inputPixels == (void *) NULL)
5113 {
dirk8a5cf512014-07-28 20:16:27 +00005114 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristy0c832c62014-03-07 22:21:04 +00005115 goto cleanup;
5116 }
5117 /* If the host pointer is aligned to the size of CLPixelPacket,
5118 then use the host buffer directly from the GPU; otherwise,
5119 create a buffer on the GPU and copy the data over */
5120 if (ALIGNED(inputPixels,CLPixelPacket))
5121 {
5122 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
5123 }
5124 else
5125 {
5126 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
5127 }
5128 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00005129 length = image->columns * image->rows;
5130 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristy0c832c62014-03-07 22:21:04 +00005131 if (clStatus != CL_SUCCESS)
5132 {
5133 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
5134 goto cleanup;
5135 }
5136
5137 /* If the host pointer is aligned to the size of cl_uint,
5138 then use the host buffer directly from the GPU; otherwise,
5139 create a buffer on the GPU and copy the data over */
5140 if (ALIGNED(histogram,cl_uint4))
5141 {
5142 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
5143 hostPtr = histogram;
5144 }
5145 else
5146 {
5147 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
5148 hostPtr = histogram;
5149 }
5150 /* create a CL buffer for histogram */
5151 length = (MaxMap+1);
5152 histogramBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(cl_uint4), hostPtr, &clStatus);
5153 if (clStatus != CL_SUCCESS)
5154 {
5155 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
5156 goto cleanup;
5157 }
5158
dirk8a5cf512014-07-28 20:16:27 +00005159 status = LaunchHistogramKernel(clEnv, queue, imageBuffer, histogramBuffer, image, channel, exception);
cristy0c832c62014-03-07 22:21:04 +00005160 if (status == MagickFalse)
5161 goto cleanup;
5162
5163 /* read from the kenel output */
5164 if (ALIGNED(histogram,cl_uint4))
5165 {
5166 length = (MaxMap+1);
5167 clEnv->library->clEnqueueMapBuffer(queue, histogramBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(cl_uint4), 0, NULL, NULL, &clStatus);
5168 }
5169 else
5170 {
5171 length = (MaxMap+1);
5172 clStatus = clEnv->library->clEnqueueReadBuffer(queue, histogramBuffer, CL_TRUE, 0, length * sizeof(cl_uint4), histogram, 0, NULL, NULL);
5173 }
5174 if (clStatus != CL_SUCCESS)
5175 {
5176 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
5177 goto cleanup;
5178 }
5179
5180 /* unmap, don't block gpu to use this buffer again. */
5181 if (ALIGNED(histogram,cl_uint4))
5182 {
5183 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, histogramBuffer, histogram, 0, NULL, NULL);
5184 if (clStatus != CL_SUCCESS)
5185 {
5186 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
5187 goto cleanup;
5188 }
5189 }
5190
5191 /* recreate input buffer later, in case image updated */
5192#ifdef RECREATEBUFFER
dirk8a5cf512014-07-28 20:16:27 +00005193 if (imageBuffer!=NULL)
5194 clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00005195#endif
5196
5197 /* CPU stuff */
5198 /*
5199 Find the histogram boundaries by locating the black/white levels.
5200 */
5201 black.red=0.0;
5202 white.red=MaxRange(QuantumRange);
5203 if ((channel & RedChannel) != 0)
5204 {
5205 intensity=0.0;
5206 for (i=0; i <= (ssize_t) MaxMap; i++)
5207 {
5208 intensity+=histogram[i].s[2];
5209 if (intensity > black_point)
5210 break;
5211 }
5212 black.red=(MagickRealType) i;
5213 intensity=0.0;
5214 for (i=(ssize_t) MaxMap; i != 0; i--)
5215 {
5216 intensity+=histogram[i].s[2];
5217 if (intensity > ((double) image->columns*image->rows-white_point))
5218 break;
5219 }
5220 white.red=(MagickRealType) i;
5221 }
5222 black.green=0.0;
5223 white.green=MaxRange(QuantumRange);
5224 if ((channel & GreenChannel) != 0)
5225 {
5226 intensity=0.0;
5227 for (i=0; i <= (ssize_t) MaxMap; i++)
5228 {
5229 intensity+=histogram[i].s[2];
5230 if (intensity > black_point)
5231 break;
5232 }
5233 black.green=(MagickRealType) i;
5234 intensity=0.0;
5235 for (i=(ssize_t) MaxMap; i != 0; i--)
5236 {
5237 intensity+=histogram[i].s[2];
5238 if (intensity > ((double) image->columns*image->rows-white_point))
5239 break;
5240 }
5241 white.green=(MagickRealType) i;
5242 }
5243 black.blue=0.0;
5244 white.blue=MaxRange(QuantumRange);
5245 if ((channel & BlueChannel) != 0)
5246 {
5247 intensity=0.0;
5248 for (i=0; i <= (ssize_t) MaxMap; i++)
5249 {
5250 intensity+=histogram[i].s[2];
5251 if (intensity > black_point)
5252 break;
5253 }
5254 black.blue=(MagickRealType) i;
5255 intensity=0.0;
5256 for (i=(ssize_t) MaxMap; i != 0; i--)
5257 {
5258 intensity+=histogram[i].s[2];
5259 if (intensity > ((double) image->columns*image->rows-white_point))
5260 break;
5261 }
5262 white.blue=(MagickRealType) i;
5263 }
dirk8a5cf512014-07-28 20:16:27 +00005264 black.alpha=0.0;
5265 white.alpha=MaxRange(QuantumRange);
cristy0c832c62014-03-07 22:21:04 +00005266 if ((channel & OpacityChannel) != 0)
5267 {
5268 intensity=0.0;
5269 for (i=0; i <= (ssize_t) MaxMap; i++)
5270 {
5271 intensity+=histogram[i].s[2];
5272 if (intensity > black_point)
5273 break;
5274 }
dirk8a5cf512014-07-28 20:16:27 +00005275 black.alpha=(MagickRealType) i;
cristy0c832c62014-03-07 22:21:04 +00005276 intensity=0.0;
5277 for (i=(ssize_t) MaxMap; i != 0; i--)
5278 {
5279 intensity+=histogram[i].s[2];
5280 if (intensity > ((double) image->columns*image->rows-white_point))
5281 break;
5282 }
dirk8a5cf512014-07-28 20:16:27 +00005283 white.alpha=(MagickRealType) i;
cristy0c832c62014-03-07 22:21:04 +00005284 }
5285 /*
5286 black.index=0.0;
5287 white.index=MaxRange(QuantumRange);
5288 if (((channel & IndexChannel) != 0) && (image->colorspace == CMYKColorspace))
5289 {
5290 intensity=0.0;
5291 for (i=0; i <= (ssize_t) MaxMap; i++)
5292 {
5293 intensity+=histogram[i].index;
5294 if (intensity > black_point)
5295 break;
5296 }
5297 black.index=(MagickRealType) i;
5298 intensity=0.0;
5299 for (i=(ssize_t) MaxMap; i != 0; i--)
5300 {
5301 intensity+=histogram[i].index;
5302 if (intensity > ((double) image->columns*image->rows-white_point))
5303 break;
5304 }
5305 white.index=(MagickRealType) i;
5306 }
5307 */
5308
5309
5310 stretch_map=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,
5311 sizeof(*stretch_map));
5312
cristy931c8062014-03-10 13:19:17 +00005313 if (stretch_map == (PixelPacket *) NULL)
cristy0c832c62014-03-07 22:21:04 +00005314 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
5315 image->filename);
5316
5317 /*
5318 Stretch the histogram to create the stretched image mapping.
5319 */
5320 (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*sizeof(*stretch_map));
5321 for (i=0; i <= (ssize_t) MaxMap; i++)
5322 {
5323 if ((channel & RedChannel) != 0)
5324 {
5325 if (i < (ssize_t) black.red)
5326 stretch_map[i].red=(Quantum) 0;
5327 else
5328 if (i > (ssize_t) white.red)
5329 stretch_map[i].red=QuantumRange;
5330 else
5331 if (black.red != white.red)
5332 stretch_map[i].red=ScaleMapToQuantum((MagickRealType) (MaxMap*
5333 (i-black.red)/(white.red-black.red)));
5334 }
5335 if ((channel & GreenChannel) != 0)
5336 {
5337 if (i < (ssize_t) black.green)
5338 stretch_map[i].green=0;
5339 else
5340 if (i > (ssize_t) white.green)
5341 stretch_map[i].green=QuantumRange;
5342 else
5343 if (black.green != white.green)
5344 stretch_map[i].green=ScaleMapToQuantum((MagickRealType) (MaxMap*
5345 (i-black.green)/(white.green-black.green)));
5346 }
5347 if ((channel & BlueChannel) != 0)
5348 {
5349 if (i < (ssize_t) black.blue)
5350 stretch_map[i].blue=0;
5351 else
5352 if (i > (ssize_t) white.blue)
5353 stretch_map[i].blue= QuantumRange;
5354 else
5355 if (black.blue != white.blue)
5356 stretch_map[i].blue=ScaleMapToQuantum((MagickRealType) (MaxMap*
5357 (i-black.blue)/(white.blue-black.blue)));
5358 }
5359 if ((channel & OpacityChannel) != 0)
5360 {
dirk8a5cf512014-07-28 20:16:27 +00005361 if (i < (ssize_t) black.alpha)
5362 stretch_map[i].alpha=0;
cristy0c832c62014-03-07 22:21:04 +00005363 else
dirk8a5cf512014-07-28 20:16:27 +00005364 if (i > (ssize_t) white.alpha)
5365 stretch_map[i].alpha=QuantumRange;
cristy0c832c62014-03-07 22:21:04 +00005366 else
dirk8a5cf512014-07-28 20:16:27 +00005367 if (black.alpha != white.alpha)
5368 stretch_map[i].alpha=ScaleMapToQuantum((MagickRealType) (MaxMap*
5369 (i-black.alpha)/(white.alpha-black.alpha)));
cristy0c832c62014-03-07 22:21:04 +00005370 }
5371 /*
5372 if (((channel & IndexChannel) != 0) &&
5373 (image->colorspace == CMYKColorspace))
5374 {
5375 if (i < (ssize_t) black.index)
5376 stretch_map[i].index=0;
5377 else
5378 if (i > (ssize_t) white.index)
5379 stretch_map[i].index=QuantumRange;
5380 else
5381 if (black.index != white.index)
5382 stretch_map[i].index=ScaleMapToQuantum((MagickRealType) (MaxMap*
5383 (i-black.index)/(white.index-black.index)));
5384 }
5385 */
5386 }
5387
5388 /*
5389 Stretch the image.
5390 */
5391 if (((channel & OpacityChannel) != 0) || (((channel & IndexChannel) != 0) &&
5392 (image->colorspace == CMYKColorspace)))
5393 image->storage_class=DirectClass;
5394 if (image->storage_class == PseudoClass)
5395 {
5396 /*
5397 Stretch colormap.
5398 */
5399 for (i=0; i < (ssize_t) image->colors; i++)
5400 {
5401 if ((channel & RedChannel) != 0)
5402 {
5403 if (black.red != white.red)
5404 image->colormap[i].red=stretch_map[
5405 ScaleQuantumToMap(image->colormap[i].red)].red;
5406 }
5407 if ((channel & GreenChannel) != 0)
5408 {
5409 if (black.green != white.green)
5410 image->colormap[i].green=stretch_map[
5411 ScaleQuantumToMap(image->colormap[i].green)].green;
5412 }
5413 if ((channel & BlueChannel) != 0)
5414 {
5415 if (black.blue != white.blue)
5416 image->colormap[i].blue=stretch_map[
5417 ScaleQuantumToMap(image->colormap[i].blue)].blue;
5418 }
5419 if ((channel & OpacityChannel) != 0)
5420 {
dirk8a5cf512014-07-28 20:16:27 +00005421 if (black.alpha != white.alpha)
5422 image->colormap[i].alpha=stretch_map[
5423 ScaleQuantumToMap(image->colormap[i].alpha)].alpha;
cristy0c832c62014-03-07 22:21:04 +00005424 }
5425 }
5426 }
5427
5428 /*
5429 Stretch image.
5430 */
5431
5432
5433 /* GPU can work on this again, image and equalize map as input
5434 image: uchar4 (CLPixelPacket)
5435 stretch_map: uchar4 (PixelPacket)
5436 black, white: float4 (FloatPixelPacket) */
5437
5438#ifdef RECREATEBUFFER
5439 /* If the host pointer is aligned to the size of CLPixelPacket,
5440 then use the host buffer directly from the GPU; otherwise,
5441 create a buffer on the GPU and copy the data over */
5442 if (ALIGNED(inputPixels,CLPixelPacket))
5443 {
5444 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
5445 }
5446 else
5447 {
5448 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
5449 }
5450 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00005451 length = image->columns * image->rows;
5452 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristy0c832c62014-03-07 22:21:04 +00005453 if (clStatus != CL_SUCCESS)
5454 {
5455 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
5456 goto cleanup;
5457 }
5458#endif
5459
5460 /* Create and initialize OpenCL buffers. */
5461 if (ALIGNED(stretch_map, PixelPacket))
5462 {
5463 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
5464 hostPtr = stretch_map;
5465 }
5466 else
5467 {
5468 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
5469 hostPtr = stretch_map;
5470 }
5471 /* create a CL buffer for stretch_map */
5472 length = (MaxMap+1);
5473 stretchMapBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(PixelPacket), hostPtr, &clStatus);
5474 if (clStatus != CL_SUCCESS)
5475 {
5476 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
5477 goto cleanup;
5478 }
5479
5480 /* get the OpenCL kernel */
5481 stretchKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Stretch");
5482 if (stretchKernel == NULL)
5483 {
5484 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
5485 goto cleanup;
5486 }
5487
5488 /* set the kernel arguments */
5489 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00005490 clStatus=clEnv->library->clSetKernelArg(stretchKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00005491 clStatus|=clEnv->library->clSetKernelArg(stretchKernel,i++,sizeof(ChannelType),&channel);
5492 clStatus|=clEnv->library->clSetKernelArg(stretchKernel,i++,sizeof(cl_mem),(void *)&stretchMapBuffer);
5493 clStatus|=clEnv->library->clSetKernelArg(stretchKernel,i++,sizeof(FloatPixelPacket),&white);
5494 clStatus|=clEnv->library->clSetKernelArg(stretchKernel,i++,sizeof(FloatPixelPacket),&black);
5495 if (clStatus != CL_SUCCESS)
5496 {
5497 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
5498 goto cleanup;
5499 }
5500
5501 /* launch the kernel */
dirk8a5cf512014-07-28 20:16:27 +00005502 global_work_size[0] = image->columns;
5503 global_work_size[1] = image->rows;
cristy0c832c62014-03-07 22:21:04 +00005504
5505 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, stretchKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
5506
5507 if (clStatus != CL_SUCCESS)
5508 {
5509 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
5510 goto cleanup;
5511 }
5512 clEnv->library->clFlush(queue);
5513
5514 /* read the data back */
5515 if (ALIGNED(inputPixels,CLPixelPacket))
5516 {
dirk8a5cf512014-07-28 20:16:27 +00005517 length = image->columns * image->rows;
5518 clEnv->library->clEnqueueMapBuffer(queue, imageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristy0c832c62014-03-07 22:21:04 +00005519 }
5520 else
5521 {
dirk8a5cf512014-07-28 20:16:27 +00005522 length = image->columns * image->rows;
5523 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
cristy0c832c62014-03-07 22:21:04 +00005524 }
5525 if (clStatus != CL_SUCCESS)
5526 {
5527 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
5528 goto cleanup;
5529 }
5530
dirk832becc2014-08-04 19:44:34 +00005531 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
5532
5533cleanup:
5534 OpenCLLogException(__FUNCTION__,__LINE__,exception);
5535
dirk8a5cf512014-07-28 20:16:27 +00005536 image_view=DestroyCacheView(image_view);
cristy0c832c62014-03-07 22:21:04 +00005537
dirk8a5cf512014-07-28 20:16:27 +00005538 if (imageBuffer!=NULL)
5539 clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00005540
5541 if (stretchMapBuffer!=NULL)
5542 clEnv->library->clReleaseMemObject(stretchMapBuffer);
5543 if (stretch_map!=NULL)
5544 stretch_map=(PixelPacket *) RelinquishMagickMemory(stretch_map);
5545
5546
5547 if (histogramBuffer!=NULL)
5548 clEnv->library->clReleaseMemObject(histogramBuffer);
5549 if (histogram!=NULL)
5550 histogram=(cl_uint4 *) RelinquishMagickMemory(histogram);
5551
5552
5553 if (histogramKernel!=NULL)
5554 RelinquishOpenCLKernel(clEnv, histogramKernel);
5555 if (stretchKernel!=NULL)
5556 RelinquishOpenCLKernel(clEnv, stretchKernel);
5557
5558 if (queue != NULL)
5559 RelinquishOpenCLCommandQueue(clEnv, queue);
5560
dirk832becc2014-08-04 19:44:34 +00005561 return(outputReady);
5562}
5563
5564MagickExport MagickBooleanType AccelerateContrastStretchImageChannel(
5565 Image *image,const ChannelType channel,const double black_point,
5566 const double white_point,ExceptionInfo *exception)
5567{
5568 MagickBooleanType
5569 status;
5570
5571 assert(image != NULL);
5572 assert(exception != (ExceptionInfo *) NULL);
5573
5574 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
5575 (checkAccelerateCondition(image, channel) == MagickFalse) ||
5576 (checkHistogramCondition(image, channel) == MagickFalse))
5577 return(MagickFalse);
5578
5579 status=ComputeContrastStretchImageChannel(image,channel, black_point, white_point, exception);
5580 return(status);
cristy0c832c62014-03-07 22:21:04 +00005581}
5582
cristy0c832c62014-03-07 22:21:04 +00005583/*
5584%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5585% %
5586% %
5587% %
dirk8a5cf512014-07-28 20:16:27 +00005588% D e s p e c k l e I m a g e w i t h O p e n C L %
cristy0c832c62014-03-07 22:21:04 +00005589% %
5590% %
5591% %
5592%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5593%
dirk8a5cf512014-07-28 20:16:27 +00005594% DespeckleImage() reduces the speckle noise in an image while perserving the
5595% edges of the original image. A speckle removing filter uses a complementary
5596% hulling technique (raising pixels that are darker than their surrounding
5597% neighbors, then complementarily lowering pixels that are brighter than their
5598% surrounding neighbors) to reduce the speckle index of that image (reference
5599% Crimmins speckle removal).
cristy0c832c62014-03-07 22:21:04 +00005600%
dirk8a5cf512014-07-28 20:16:27 +00005601% The format of the DespeckleImage method is:
cristy0c832c62014-03-07 22:21:04 +00005602%
dirk8a5cf512014-07-28 20:16:27 +00005603% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
cristy0c832c62014-03-07 22:21:04 +00005604%
5605% A description of each parameter follows:
5606%
5607% o image: the image.
5608%
dirk8a5cf512014-07-28 20:16:27 +00005609% o exception: return any errors or warnings in this structure.
cristy0c832c62014-03-07 22:21:04 +00005610%
5611*/
5612
dirk832becc2014-08-04 19:44:34 +00005613static Image *ComputeDespeckleImage(const Image *image,
5614 ExceptionInfo*exception)
5615{
5616 static const int
5617 X[4] = {0, 1, 1,-1},
5618 Y[4] = {1, 0, 1, 1};
5619
5620 CacheView
5621 *filteredImage_view,
5622 *image_view;
5623
5624 cl_command_queue
5625 queue;
5626
5627 cl_context
5628 context;
5629
5630 cl_int
5631 clStatus;
5632
5633 cl_kernel
5634 hullPass1,
5635 hullPass2;
5636
5637 cl_mem_flags
5638 mem_flags;
5639
5640 cl_mem
5641 filteredImageBuffer,
5642 imageBuffer,
5643 tempImageBuffer[2];
5644
5645 const void
5646 *inputPixels;
5647
5648 Image
5649 *filteredImage;
5650
5651 int
5652 k,
5653 matte;
5654
5655 MagickBooleanType
5656 outputReady;
5657
5658 MagickCLEnv
5659 clEnv;
5660
5661 MagickSizeType
5662 length;
5663
5664 size_t
5665 global_work_size[2];
5666
5667 unsigned int
5668 imageHeight,
5669 imageWidth;
5670
5671 void
5672 *filteredPixels,
5673 *hostPtr;
5674
5675 outputReady = MagickFalse;
5676 clEnv = NULL;
5677 inputPixels = NULL;
5678 filteredImage = NULL;
5679 filteredImage_view = NULL;
5680 filteredPixels = NULL;
5681 context = NULL;
5682 imageBuffer = NULL;
5683 filteredImageBuffer = NULL;
5684 hullPass1 = NULL;
5685 hullPass2 = NULL;
5686 queue = NULL;
5687 tempImageBuffer[0] = tempImageBuffer[1] = NULL;
5688 clEnv = GetDefaultOpenCLEnv();
5689 context = GetOpenCLContext(clEnv);
5690 queue = AcquireOpenCLCommandQueue(clEnv);
5691
5692 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00005693 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00005694 if (inputPixels == (void *) NULL)
5695 {
dirk8a5cf512014-07-28 20:16:27 +00005696 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +00005697 goto cleanup;
5698 }
5699
5700 if (ALIGNED(inputPixels,CLPixelPacket))
5701 {
5702 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
5703 }
5704 else
5705 {
5706 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
5707 }
5708 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00005709 length = image->columns * image->rows;
5710 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00005711 if (clStatus != CL_SUCCESS)
5712 {
cristy0c832c62014-03-07 22:21:04 +00005713 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00005714 goto cleanup;
5715 }
5716
5717 mem_flags = CL_MEM_READ_WRITE;
dirk8a5cf512014-07-28 20:16:27 +00005718 length = image->columns * image->rows;
cristyf034abb2013-11-24 14:16:14 +00005719 for (k = 0; k < 2; k++)
5720 {
cristy0c832c62014-03-07 22:21:04 +00005721 tempImageBuffer[k] = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00005722 if (clStatus != CL_SUCCESS)
5723 {
cristy0c832c62014-03-07 22:21:04 +00005724 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00005725 goto cleanup;
5726 }
5727 }
5728
dirk8a5cf512014-07-28 20:16:27 +00005729 filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristyf034abb2013-11-24 14:16:14 +00005730 assert(filteredImage != NULL);
dirk8a5cf512014-07-28 20:16:27 +00005731 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristyf034abb2013-11-24 14:16:14 +00005732 {
cristya22457d2013-12-07 14:03:06 +00005733 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005734 goto cleanup;
5735 }
dirk832becc2014-08-04 19:44:34 +00005736 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +00005737 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00005738 if (filteredPixels == (void *) NULL)
5739 {
cristya22457d2013-12-07 14:03:06 +00005740 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename);
cristyf034abb2013-11-24 14:16:14 +00005741 goto cleanup;
5742 }
5743
5744 if (ALIGNED(filteredPixels,CLPixelPacket))
5745 {
5746 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
5747 hostPtr = filteredPixels;
5748 }
5749 else
5750 {
5751 mem_flags = CL_MEM_WRITE_ONLY;
5752 hostPtr = NULL;
5753 }
5754 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00005755 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00005756 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00005757 if (clStatus != CL_SUCCESS)
5758 {
cristy0c832c62014-03-07 22:21:04 +00005759 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00005760 goto cleanup;
5761 }
5762
5763 hullPass1 = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "HullPass1");
5764 hullPass2 = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "HullPass2");
5765
dirk8a5cf512014-07-28 20:16:27 +00005766 clStatus =clEnv->library->clSetKernelArg(hullPass1,0,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00005767 clStatus |=clEnv->library->clSetKernelArg(hullPass1,1,sizeof(cl_mem),(void *)(tempImageBuffer+1));
dirkb0d783f2014-08-31 10:48:05 +00005768 imageWidth = (unsigned int) image->columns;
cristy0c832c62014-03-07 22:21:04 +00005769 clStatus |=clEnv->library->clSetKernelArg(hullPass1,2,sizeof(unsigned int),(void *)&imageWidth);
dirkb0d783f2014-08-31 10:48:05 +00005770 imageHeight = (unsigned int) image->rows;
cristy0c832c62014-03-07 22:21:04 +00005771 clStatus |=clEnv->library->clSetKernelArg(hullPass1,3,sizeof(unsigned int),(void *)&imageHeight);
dirk8a5cf512014-07-28 20:16:27 +00005772 matte = (image->alpha_trait==BlendPixelTrait)?0:1;
cristy0c832c62014-03-07 22:21:04 +00005773 clStatus |=clEnv->library->clSetKernelArg(hullPass1,6,sizeof(int),(void *)&matte);
cristyf034abb2013-11-24 14:16:14 +00005774 if (clStatus != CL_SUCCESS)
5775 {
cristy0c832c62014-03-07 22:21:04 +00005776 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005777 goto cleanup;
5778 }
5779
cristy0c832c62014-03-07 22:21:04 +00005780 clStatus = clEnv->library->clSetKernelArg(hullPass2,0,sizeof(cl_mem),(void *)(tempImageBuffer+1));
5781 clStatus |=clEnv->library->clSetKernelArg(hullPass2,1,sizeof(cl_mem),(void *)tempImageBuffer);
dirkb0d783f2014-08-31 10:48:05 +00005782 imageWidth = (unsigned int) image->columns;
cristy0c832c62014-03-07 22:21:04 +00005783 clStatus |=clEnv->library->clSetKernelArg(hullPass2,2,sizeof(unsigned int),(void *)&imageWidth);
dirkb0d783f2014-08-31 10:48:05 +00005784 imageHeight = (unsigned int) image->rows;
cristy0c832c62014-03-07 22:21:04 +00005785 clStatus |=clEnv->library->clSetKernelArg(hullPass2,3,sizeof(unsigned int),(void *)&imageHeight);
dirk8a5cf512014-07-28 20:16:27 +00005786 matte = (image->alpha_trait==BlendPixelTrait)?0:1;
cristy0c832c62014-03-07 22:21:04 +00005787 clStatus |=clEnv->library->clSetKernelArg(hullPass2,6,sizeof(int),(void *)&matte);
cristyf034abb2013-11-24 14:16:14 +00005788 if (clStatus != CL_SUCCESS)
5789 {
cristy0c832c62014-03-07 22:21:04 +00005790 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005791 goto cleanup;
5792 }
5793
5794
dirk8a5cf512014-07-28 20:16:27 +00005795 global_work_size[0] = image->columns;
5796 global_work_size[1] = image->rows;
cristyf034abb2013-11-24 14:16:14 +00005797
5798
5799 for (k = 0; k < 4; k++)
5800 {
5801 cl_int2 offset;
5802 int polarity;
5803
5804
5805 offset.s[0] = X[k];
5806 offset.s[1] = Y[k];
5807 polarity = 1;
cristy0c832c62014-03-07 22:21:04 +00005808 clStatus = clEnv->library->clSetKernelArg(hullPass1,4,sizeof(cl_int2),(void *)&offset);
5809 clStatus|= clEnv->library->clSetKernelArg(hullPass1,5,sizeof(int),(void *)&polarity);
5810 clStatus|=clEnv->library->clSetKernelArg(hullPass2,4,sizeof(cl_int2),(void *)&offset);
5811 clStatus|=clEnv->library->clSetKernelArg(hullPass2,5,sizeof(int),(void *)&polarity);
cristyf034abb2013-11-24 14:16:14 +00005812 if (clStatus != CL_SUCCESS)
5813 {
cristy0c832c62014-03-07 22:21:04 +00005814 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005815 goto cleanup;
5816 }
5817 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00005818 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass1, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00005819 if (clStatus != CL_SUCCESS)
5820 {
cristy0c832c62014-03-07 22:21:04 +00005821 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005822 goto cleanup;
5823 }
5824 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00005825 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass2, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00005826 if (clStatus != CL_SUCCESS)
5827 {
cristy0c832c62014-03-07 22:21:04 +00005828 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005829 goto cleanup;
5830 }
5831
5832
5833 if (k == 0)
cristy0c832c62014-03-07 22:21:04 +00005834 clStatus =clEnv->library->clSetKernelArg(hullPass1,0,sizeof(cl_mem),(void *)(tempImageBuffer));
cristyf034abb2013-11-24 14:16:14 +00005835 offset.s[0] = -X[k];
5836 offset.s[1] = -Y[k];
5837 polarity = 1;
cristy0c832c62014-03-07 22:21:04 +00005838 clStatus = clEnv->library->clSetKernelArg(hullPass1,4,sizeof(cl_int2),(void *)&offset);
5839 clStatus|= clEnv->library->clSetKernelArg(hullPass1,5,sizeof(int),(void *)&polarity);
5840 clStatus|=clEnv->library->clSetKernelArg(hullPass2,4,sizeof(cl_int2),(void *)&offset);
5841 clStatus|=clEnv->library->clSetKernelArg(hullPass2,5,sizeof(int),(void *)&polarity);
cristyf034abb2013-11-24 14:16:14 +00005842 if (clStatus != CL_SUCCESS)
5843 {
cristy0c832c62014-03-07 22:21:04 +00005844 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005845 goto cleanup;
5846 }
5847 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00005848 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass1, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00005849 if (clStatus != CL_SUCCESS)
5850 {
cristy0c832c62014-03-07 22:21:04 +00005851 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005852 goto cleanup;
5853 }
5854 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00005855 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass2, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00005856 if (clStatus != CL_SUCCESS)
5857 {
cristy0c832c62014-03-07 22:21:04 +00005858 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005859 goto cleanup;
5860 }
5861
5862 offset.s[0] = -X[k];
5863 offset.s[1] = -Y[k];
5864 polarity = -1;
cristy0c832c62014-03-07 22:21:04 +00005865 clStatus = clEnv->library->clSetKernelArg(hullPass1,4,sizeof(cl_int2),(void *)&offset);
5866 clStatus|= clEnv->library->clSetKernelArg(hullPass1,5,sizeof(int),(void *)&polarity);
5867 clStatus|=clEnv->library->clSetKernelArg(hullPass2,4,sizeof(cl_int2),(void *)&offset);
5868 clStatus|=clEnv->library->clSetKernelArg(hullPass2,5,sizeof(int),(void *)&polarity);
cristyf034abb2013-11-24 14:16:14 +00005869 if (clStatus != CL_SUCCESS)
5870 {
cristy0c832c62014-03-07 22:21:04 +00005871 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005872 goto cleanup;
5873 }
5874 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00005875 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass1, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00005876 if (clStatus != CL_SUCCESS)
5877 {
cristy0c832c62014-03-07 22:21:04 +00005878 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005879 goto cleanup;
5880 }
5881 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00005882 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass2, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00005883 if (clStatus != CL_SUCCESS)
5884 {
cristy0c832c62014-03-07 22:21:04 +00005885 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005886 goto cleanup;
5887 }
5888
5889 offset.s[0] = X[k];
5890 offset.s[1] = Y[k];
5891 polarity = -1;
cristy0c832c62014-03-07 22:21:04 +00005892 clStatus = clEnv->library->clSetKernelArg(hullPass1,4,sizeof(cl_int2),(void *)&offset);
5893 clStatus|= clEnv->library->clSetKernelArg(hullPass1,5,sizeof(int),(void *)&polarity);
5894 clStatus|=clEnv->library->clSetKernelArg(hullPass2,4,sizeof(cl_int2),(void *)&offset);
5895 clStatus|=clEnv->library->clSetKernelArg(hullPass2,5,sizeof(int),(void *)&polarity);
cristyf034abb2013-11-24 14:16:14 +00005896
5897 if (k == 3)
cristy0c832c62014-03-07 22:21:04 +00005898 clStatus |=clEnv->library->clSetKernelArg(hullPass2,1,sizeof(cl_mem),(void *)&filteredImageBuffer);
cristyf034abb2013-11-24 14:16:14 +00005899
5900 if (clStatus != CL_SUCCESS)
5901 {
cristy0c832c62014-03-07 22:21:04 +00005902 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005903 goto cleanup;
5904 }
5905 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00005906 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass1, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00005907 if (clStatus != CL_SUCCESS)
5908 {
cristy0c832c62014-03-07 22:21:04 +00005909 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005910 goto cleanup;
5911 }
5912 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00005913 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass2, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00005914 if (clStatus != CL_SUCCESS)
5915 {
cristy0c832c62014-03-07 22:21:04 +00005916 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005917 goto cleanup;
5918 }
5919 }
5920
5921 if (ALIGNED(filteredPixels,CLPixelPacket))
5922 {
dirk8a5cf512014-07-28 20:16:27 +00005923 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00005924 clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00005925 }
5926 else
5927 {
dirk8a5cf512014-07-28 20:16:27 +00005928 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00005929 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00005930 }
5931 if (clStatus != CL_SUCCESS)
5932 {
cristya22457d2013-12-07 14:03:06 +00005933 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005934 goto cleanup;
5935 }
5936
dirk832becc2014-08-04 19:44:34 +00005937 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
5938
5939cleanup:
5940 OpenCLLogException(__FUNCTION__,__LINE__,exception);
5941
5942 image_view=DestroyCacheView(image_view);
5943 if (filteredImage_view != NULL)
dirk8a5cf512014-07-28 20:16:27 +00005944 filteredImage_view=DestroyCacheView(filteredImage_view);
cristya22457d2013-12-07 14:03:06 +00005945
cristyf034abb2013-11-24 14:16:14 +00005946 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
dirk8a5cf512014-07-28 20:16:27 +00005947 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristyf034abb2013-11-24 14:16:14 +00005948 for (k = 0; k < 2; k++)
5949 {
cristy0c832c62014-03-07 22:21:04 +00005950 if (tempImageBuffer[k]!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer[k]);
cristyf034abb2013-11-24 14:16:14 +00005951 }
cristy0c832c62014-03-07 22:21:04 +00005952 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
cristyf034abb2013-11-24 14:16:14 +00005953 if (hullPass1!=NULL) RelinquishOpenCLKernel(clEnv, hullPass1);
5954 if (hullPass2!=NULL) RelinquishOpenCLKernel(clEnv, hullPass2);
dirk8a5cf512014-07-28 20:16:27 +00005955 if (outputReady == MagickFalse && filteredImage != NULL)
5956 filteredImage=DestroyImage(filteredImage);
5957 return(filteredImage);
cristyf034abb2013-11-24 14:16:14 +00005958}
5959
dirk832becc2014-08-04 19:44:34 +00005960MagickExport Image *AccelerateDespeckleImage(const Image* image,
5961 ExceptionInfo* exception)
5962{
5963 Image
5964 *filteredImage;
5965
5966 assert(image != NULL);
5967 assert(exception != (ExceptionInfo *) NULL);
5968
5969 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
5970 (checkAccelerateCondition(image, AllChannels) == MagickFalse))
5971 return NULL;
5972
5973 filteredImage=ComputeDespeckleImage(image,exception);
5974 return(filteredImage);
cristyf034abb2013-11-24 14:16:14 +00005975}
5976
dirk832becc2014-08-04 19:44:34 +00005977static Image *ComputeAddNoiseImage(const Image *image,
5978 const ChannelType channel,const NoiseType noise_type,
5979 ExceptionInfo *exception)
5980{
5981 CacheView
5982 *filteredImage_view,
5983 *image_view;
5984
5985 cl_command_queue
5986 queue;
5987
5988 cl_context
5989 context;
5990
5991 cl_int
dirk859503c2014-08-13 20:39:42 +00005992 inputPixelCount,
5993 pixelsPerWorkitem,
dirk832becc2014-08-04 19:44:34 +00005994 clStatus;
5995
dirk859503c2014-08-13 20:39:42 +00005996 cl_uint
5997 seed0,
5998 seed1;
5999
dirk832becc2014-08-04 19:44:34 +00006000 cl_kernel
6001 addNoiseKernel;
6002
6003 cl_mem_flags
6004 mem_flags;
6005
6006 cl_mem
6007 filteredImageBuffer,
dirk859503c2014-08-13 20:39:42 +00006008 imageBuffer;
dirk832becc2014-08-04 19:44:34 +00006009
6010 const char
6011 *option;
6012
6013 const void
6014 *inputPixels;
6015
6016 float
dirk859503c2014-08-13 20:39:42 +00006017 attenuate;
dirk832becc2014-08-04 19:44:34 +00006018
6019 MagickBooleanType
6020 outputReady;
6021
6022 MagickCLEnv
6023 clEnv;
6024
6025 MagickSizeType
6026 length;
6027
6028 Image
6029 *filteredImage;
6030
dirk832becc2014-08-04 19:44:34 +00006031 RandomInfo
6032 **restrict random_info;
6033
6034 size_t
dirk859503c2014-08-13 20:39:42 +00006035 global_work_size[1],
6036 local_work_size[1];
dirk832becc2014-08-04 19:44:34 +00006037
6038 unsigned int
dirk832becc2014-08-04 19:44:34 +00006039 k,
dirk859503c2014-08-13 20:39:42 +00006040 numRandomNumberPerPixel;
dirk832becc2014-08-04 19:44:34 +00006041
6042#if defined(MAGICKCORE_OPENMP_SUPPORT)
6043 unsigned long
6044 key;
6045#endif
6046
6047 void
6048 *filteredPixels,
6049 *hostPtr;
6050
6051 outputReady = MagickFalse;
6052 clEnv = NULL;
6053 inputPixels = NULL;
6054 filteredImage = NULL;
6055 filteredImage_view = NULL;
6056 filteredPixels = NULL;
dirk832becc2014-08-04 19:44:34 +00006057 context = NULL;
6058 imageBuffer = NULL;
dirk832becc2014-08-04 19:44:34 +00006059 filteredImageBuffer = NULL;
6060 queue = NULL;
6061 addNoiseKernel = NULL;
6062
6063 clEnv = GetDefaultOpenCLEnv();
6064 context = GetOpenCLContext(clEnv);
6065 queue = AcquireOpenCLCommandQueue(clEnv);
6066
6067 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00006068 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristye85d0f72013-11-27 02:25:43 +00006069 if (inputPixels == (void *) NULL)
6070 {
dirk8a5cf512014-07-28 20:16:27 +00006071 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristye85d0f72013-11-27 02:25:43 +00006072 goto cleanup;
6073 }
6074
6075 if (ALIGNED(inputPixels,CLPixelPacket))
6076 {
6077 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
6078 }
6079 else
6080 {
6081 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
6082 }
6083 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00006084 length = image->columns * image->rows;
6085 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristye85d0f72013-11-27 02:25:43 +00006086 if (clStatus != CL_SUCCESS)
6087 {
cristy0c832c62014-03-07 22:21:04 +00006088 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristye85d0f72013-11-27 02:25:43 +00006089 goto cleanup;
6090 }
6091
6092
dirk8a5cf512014-07-28 20:16:27 +00006093 filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristye85d0f72013-11-27 02:25:43 +00006094 assert(filteredImage != NULL);
dirk8a5cf512014-07-28 20:16:27 +00006095 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristye85d0f72013-11-27 02:25:43 +00006096 {
cristya22457d2013-12-07 14:03:06 +00006097 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", ".");
cristye85d0f72013-11-27 02:25:43 +00006098 goto cleanup;
6099 }
dirk832becc2014-08-04 19:44:34 +00006100 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +00006101 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristye85d0f72013-11-27 02:25:43 +00006102 if (filteredPixels == (void *) NULL)
6103 {
cristya22457d2013-12-07 14:03:06 +00006104 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename);
cristye85d0f72013-11-27 02:25:43 +00006105 goto cleanup;
6106 }
6107
6108 if (ALIGNED(filteredPixels,CLPixelPacket))
6109 {
6110 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
6111 hostPtr = filteredPixels;
6112 }
6113 else
6114 {
6115 mem_flags = CL_MEM_WRITE_ONLY;
6116 hostPtr = NULL;
6117 }
6118 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00006119 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006120 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus);
cristye85d0f72013-11-27 02:25:43 +00006121 if (clStatus != CL_SUCCESS)
6122 {
cristy0c832c62014-03-07 22:21:04 +00006123 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristye85d0f72013-11-27 02:25:43 +00006124 goto cleanup;
6125 }
6126
6127 /* find out how many random numbers needed by pixel */
6128 numRandomNumberPerPixel = 0;
6129 {
6130 unsigned int numRandPerChannel = 0;
6131 switch (noise_type)
6132 {
6133 case UniformNoise:
6134 case ImpulseNoise:
6135 case LaplacianNoise:
6136 case RandomNoise:
6137 default:
6138 numRandPerChannel = 1;
6139 break;
6140 case GaussianNoise:
6141 case MultiplicativeGaussianNoise:
6142 case PoissonNoise:
6143 numRandPerChannel = 2;
6144 break;
6145 };
6146
6147 if ((channel & RedChannel) != 0)
6148 numRandomNumberPerPixel+=numRandPerChannel;
6149 if ((channel & GreenChannel) != 0)
6150 numRandomNumberPerPixel+=numRandPerChannel;
6151 if ((channel & BlueChannel) != 0)
6152 numRandomNumberPerPixel+=numRandPerChannel;
6153 if ((channel & OpacityChannel) != 0)
6154 numRandomNumberPerPixel+=numRandPerChannel;
6155 }
6156
cristye85d0f72013-11-27 02:25:43 +00006157 /* set up the random number generators */
6158 attenuate=1.0;
dirk8a5cf512014-07-28 20:16:27 +00006159 option=GetImageArtifact(image,"attenuate");
cristye85d0f72013-11-27 02:25:43 +00006160 if (option != (char *) NULL)
6161 attenuate=StringToDouble(option,(char **) NULL);
6162 random_info=AcquireRandomInfoThreadSet();
6163#if defined(MAGICKCORE_OPENMP_SUPPORT)
6164 key=GetRandomSecretKey(random_info[0]);
cristy9ff50022014-08-26 17:38:59 +00006165 (void) key;
cristye85d0f72013-11-27 02:25:43 +00006166#endif
6167
dirk859503c2014-08-13 20:39:42 +00006168 addNoiseKernel = AcquireOpenCLKernel(clEnv,MAGICK_OPENCL_ACCELERATE,"GenerateNoiseImage");
6169
6170 {
6171 cl_uint computeUnitCount;
6172 cl_uint workItemCount;
6173 clEnv->library->clGetDeviceInfo(clEnv->device, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &computeUnitCount, NULL);
6174 workItemCount = computeUnitCount * 2 * 256; // 256 work items per group, 2 groups per CU
dirkb0d783f2014-08-31 10:48:05 +00006175 inputPixelCount = (cl_int) (image->columns * image->rows);
dirk859503c2014-08-13 20:39:42 +00006176 pixelsPerWorkitem = (inputPixelCount + workItemCount - 1) / workItemCount;
6177 pixelsPerWorkitem = ((pixelsPerWorkitem + 3) / 4) * 4;
6178
6179 local_work_size[0] = 256;
6180 global_work_size[0] = workItemCount;
6181 }
6182 {
6183 RandomInfo* randomInfo = AcquireRandomInfo();
6184 const unsigned long* s = GetRandomInfoSeed(randomInfo);
6185 seed0 = s[0];
6186 GetPseudoRandomValue(randomInfo);
6187 seed1 = s[0];
6188 randomInfo = DestroyRandomInfo(randomInfo);
6189 }
cristye85d0f72013-11-27 02:25:43 +00006190
6191 k = 0;
dirk8a5cf512014-07-28 20:16:27 +00006192 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00006193 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_mem),(void *)&filteredImageBuffer);
dirk859503c2014-08-13 20:39:42 +00006194 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_uint),(void *)&inputPixelCount);
6195 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_uint),(void *)&pixelsPerWorkitem);
cristy0c832c62014-03-07 22:21:04 +00006196 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(ChannelType),(void *)&channel);
6197 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(NoiseType),(void *)&noise_type);
cristye85d0f72013-11-27 02:25:43 +00006198 attenuate=1.0f;
dirk8a5cf512014-07-28 20:16:27 +00006199 option=GetImageArtifact(image,"attenuate");
cristye85d0f72013-11-27 02:25:43 +00006200 if (option != (char *) NULL)
6201 attenuate=(float)StringToDouble(option,(char **) NULL);
cristy0c832c62014-03-07 22:21:04 +00006202 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(float),(void *)&attenuate);
dirk859503c2014-08-13 20:39:42 +00006203 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_uint),(void *)&seed0);
6204 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_uint),(void *)&seed1);
cristy0c832c62014-03-07 22:21:04 +00006205 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(unsigned int),(void *)&numRandomNumberPerPixel);
cristye85d0f72013-11-27 02:25:43 +00006206
dirk859503c2014-08-13 20:39:42 +00006207 clEnv->library->clEnqueueNDRangeKernel(queue,addNoiseKernel,1,NULL,global_work_size,NULL,0,NULL,NULL);
cristye85d0f72013-11-27 02:25:43 +00006208
6209 if (ALIGNED(filteredPixels,CLPixelPacket))
6210 {
dirk8a5cf512014-07-28 20:16:27 +00006211 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006212 clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristye85d0f72013-11-27 02:25:43 +00006213 }
6214 else
6215 {
dirk8a5cf512014-07-28 20:16:27 +00006216 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006217 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
cristye85d0f72013-11-27 02:25:43 +00006218 }
6219 if (clStatus != CL_SUCCESS)
6220 {
cristya22457d2013-12-07 14:03:06 +00006221 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristye85d0f72013-11-27 02:25:43 +00006222 goto cleanup;
6223 }
6224
dirk832becc2014-08-04 19:44:34 +00006225 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
6226
6227cleanup:
6228 OpenCLLogException(__FUNCTION__,__LINE__,exception);
6229
6230 image_view=DestroyCacheView(image_view);
6231 if (filteredImage_view != NULL)
dirk8a5cf512014-07-28 20:16:27 +00006232 filteredImage_view=DestroyCacheView(filteredImage_view);
cristya22457d2013-12-07 14:03:06 +00006233
cristye85d0f72013-11-27 02:25:43 +00006234 if (queue!=NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
6235 if (addNoiseKernel!=NULL) RelinquishOpenCLKernel(clEnv, addNoiseKernel);
dirk8a5cf512014-07-28 20:16:27 +00006236 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00006237 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
dirk832becc2014-08-04 19:44:34 +00006238 if (outputReady == MagickFalse && filteredImage != NULL)
6239 filteredImage=DestroyImage(filteredImage);
6240
dirk8a5cf512014-07-28 20:16:27 +00006241 return(filteredImage);
cristye85d0f72013-11-27 02:25:43 +00006242}
6243
cristye85d0f72013-11-27 02:25:43 +00006244
dirk832becc2014-08-04 19:44:34 +00006245MagickExport Image *AccelerateAddNoiseImage(const Image *image,
6246 const ChannelType channel,const NoiseType noise_type,
6247 ExceptionInfo *exception)
6248{
6249 Image
6250 *filteredImage;
6251
6252 assert(image != NULL);
6253 assert(exception != (ExceptionInfo *) NULL);
6254
6255 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
6256 (checkAccelerateCondition(image, channel) == MagickFalse))
6257 return NULL;
6258
dirk859503c2014-08-13 20:39:42 +00006259 filteredImage = ComputeAddNoiseImage(image,channel,noise_type,exception);
dirk832becc2014-08-04 19:44:34 +00006260
6261 return(filteredImage);
cristye85d0f72013-11-27 02:25:43 +00006262}
6263
dirk832becc2014-08-04 19:44:34 +00006264static MagickBooleanType LaunchRandomImageKernel(MagickCLEnv clEnv,
6265 cl_command_queue queue,cl_mem imageBuffer,const unsigned int imageColumns,
6266 const unsigned int imageRows,cl_mem seedBuffer,
6267 const unsigned int numGenerators,ExceptionInfo *exception)
6268{
6269 int
6270 k;
6271
6272 cl_int
6273 clStatus;
6274
6275 cl_kernel
6276 randomImageKernel;
6277
6278 MagickBooleanType
6279 status;
6280
6281 size_t
6282 global_work_size,
6283 local_work_size;
6284
dirk8a5cf512014-07-28 20:16:27 +00006285 status = MagickFalse;
cristy0c832c62014-03-07 22:21:04 +00006286 randomImageKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "RandomImage");
6287
6288 k = 0;
dirk8a5cf512014-07-28 20:16:27 +00006289 clEnv->library->clSetKernelArg(randomImageKernel,k++,sizeof(cl_mem),(void*)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00006290 clEnv->library->clSetKernelArg(randomImageKernel,k++,sizeof(cl_uint),(void*)&imageColumns);
6291 clEnv->library->clSetKernelArg(randomImageKernel,k++,sizeof(cl_uint),(void*)&imageRows);
6292 clEnv->library->clSetKernelArg(randomImageKernel,k++,sizeof(cl_mem),(void*)&seedBuffer);
6293 {
6294 const float randNormNumerator = 1.0f;
6295 const unsigned int randNormDenominator = (unsigned int)(~0UL);
6296 clEnv->library->clSetKernelArg(randomImageKernel,k++,
6297 sizeof(float),(void*)&randNormNumerator);
6298 clEnv->library->clSetKernelArg(randomImageKernel,k++,
6299 sizeof(cl_uint),(void*)&randNormDenominator);
6300 }
6301
6302
6303 global_work_size = numGenerators;
6304 local_work_size = 64;
6305
6306 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue,randomImageKernel,1,NULL,&global_work_size,
6307 &local_work_size,0,NULL,NULL);
6308
6309 if (clStatus != CL_SUCCESS)
6310 {
6311 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning,
6312 "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
6313 goto cleanup;
6314 }
6315 status = MagickTrue;
6316
6317cleanup:
6318 if (randomImageKernel!=NULL) RelinquishOpenCLKernel(clEnv, randomImageKernel);
dirk832becc2014-08-04 19:44:34 +00006319 return(status);
6320}
6321
6322static MagickBooleanType ComputeRandomImage(Image* image,
6323 ExceptionInfo* exception)
6324{
6325 CacheView
6326 *image_view;
6327
6328 cl_command_queue
6329 queue;
6330
6331 cl_context
6332 context;
6333
6334 cl_int
6335 clStatus;
6336
6337 /* Don't release this buffer in this function !!! */
6338 cl_mem
6339 randomNumberSeedsBuffer;
6340
6341 cl_mem_flags
6342 mem_flags;
6343
6344 cl_mem
6345 imageBuffer;
6346
6347 MagickBooleanType
6348 outputReady,
6349 status;
6350
6351 MagickCLEnv
6352 clEnv;
6353
6354 MagickSizeType
6355 length;
6356
6357 void
6358 *inputPixels;
6359
6360 status = MagickFalse;
6361 outputReady = MagickFalse;
6362 inputPixels = NULL;
6363 context = NULL;
6364 imageBuffer = NULL;
6365 queue = NULL;
6366
6367 clEnv = GetDefaultOpenCLEnv();
6368 context = GetOpenCLContext(clEnv);
6369
6370 /* Create and initialize OpenCL buffers. */
6371 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00006372 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristy0c832c62014-03-07 22:21:04 +00006373 if (inputPixels == (void *) NULL)
6374 {
dirk8a5cf512014-07-28 20:16:27 +00006375 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristy0c832c62014-03-07 22:21:04 +00006376 goto cleanup;
6377 }
6378
6379 /* If the host pointer is aligned to the size of CLPixelPacket,
6380 then use the host buffer directly from the GPU; otherwise,
6381 create a buffer on the GPU and copy the data over */
6382 if (ALIGNED(inputPixels,CLPixelPacket))
6383 {
6384 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
6385 }
6386 else
6387 {
6388 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
6389 }
6390 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00006391 length = image->columns * image->rows;
6392 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristy0c832c62014-03-07 22:21:04 +00006393 if (clStatus != CL_SUCCESS)
6394 {
6395 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
6396 goto cleanup;
6397 }
6398
6399 queue = AcquireOpenCLCommandQueue(clEnv);
6400
6401 randomNumberSeedsBuffer = GetAndLockRandSeedBuffer(clEnv);
6402 if (randomNumberSeedsBuffer==NULL)
6403 {
6404 (void) OpenCLThrowMagickException(exception, GetMagickModule(),
6405 ResourceLimitWarning, "Failed to get GPU random number generators.",
6406 "'%s'", ".");
6407 goto cleanup;
6408 }
6409
6410 status = LaunchRandomImageKernel(clEnv,queue,
dirk8a5cf512014-07-28 20:16:27 +00006411 imageBuffer,
dirkb0d783f2014-08-31 10:48:05 +00006412 (unsigned int) image->columns,
6413 (unsigned int) image->rows,
cristy0c832c62014-03-07 22:21:04 +00006414 randomNumberSeedsBuffer,
6415 GetNumRandGenerators(clEnv),
6416 exception);
6417 if (status==MagickFalse)
6418 {
6419 goto cleanup;
6420 }
6421
6422 if (ALIGNED(inputPixels,CLPixelPacket))
6423 {
dirk8a5cf512014-07-28 20:16:27 +00006424 length = image->columns * image->rows;
6425 clEnv->library->clEnqueueMapBuffer(queue, imageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
cristy0c832c62014-03-07 22:21:04 +00006426 }
6427 else
6428 {
dirk8a5cf512014-07-28 20:16:27 +00006429 length = image->columns * image->rows;
6430 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
cristy0c832c62014-03-07 22:21:04 +00006431 }
6432 if (clStatus != CL_SUCCESS)
6433 {
6434 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
6435 goto cleanup;
6436 }
dirk832becc2014-08-04 19:44:34 +00006437 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
6438
6439cleanup:
6440 OpenCLLogException(__FUNCTION__,__LINE__,exception);
6441
dirk8a5cf512014-07-28 20:16:27 +00006442 image_view=DestroyCacheView(image_view);
cristy0c832c62014-03-07 22:21:04 +00006443
6444 UnlockRandSeedBuffer(clEnv);
dirk8a5cf512014-07-28 20:16:27 +00006445 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00006446 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
6447 return outputReady;
6448}
6449
dirk832becc2014-08-04 19:44:34 +00006450MagickExport MagickBooleanType AccelerateRandomImage(Image *image,
6451 ExceptionInfo* exception)
6452{
6453 MagickBooleanType
6454 status;
6455
6456 assert(image != NULL);
6457 assert(exception != (ExceptionInfo *) NULL);
6458
6459 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
6460 (checkAccelerateCondition(image, AllChannels) == MagickFalse))
6461 return(MagickFalse);
6462
6463 status=ComputeRandomImage(image,exception);
6464 return(status);
cristy0c832c62014-03-07 22:21:04 +00006465}
6466
dirk832becc2014-08-04 19:44:34 +00006467static Image* ComputeMotionBlurImage(const Image *image,
6468 const ChannelType channel,const double *kernel,const size_t width,
6469 const OffsetInfo *offset,ExceptionInfo *exception)
6470{
6471 CacheView
6472 *filteredImage_view,
6473 *image_view;
6474
6475 cl_command_queue
6476 queue;
6477
6478 cl_context
6479 context;
6480
6481 cl_float4
6482 biasPixel;
6483
6484 cl_int
6485 clStatus;
6486
6487 cl_kernel
6488 motionBlurKernel;
6489
6490 cl_mem
6491 filteredImageBuffer,
6492 imageBuffer,
6493 imageKernelBuffer,
6494 offsetBuffer;
6495
6496 cl_mem_flags
6497 mem_flags;
6498
6499 const void
6500 *inputPixels;
6501
6502 float
6503 *kernelBufferPtr;
6504
6505 Image
6506 *filteredImage;
6507
6508 int
6509 *offsetBufferPtr;
6510
6511 MagickBooleanType
6512 outputReady;
6513
6514 MagickCLEnv
6515 clEnv;
6516
6517 PixelInfo
6518 bias;
6519
6520 MagickSizeType
6521 length;
6522
6523 size_t
6524 global_work_size[2],
6525 local_work_size[2];
6526
6527 unsigned int
6528 i,
6529 imageHeight,
6530 imageWidth,
6531 matte;
6532
6533 void
6534 *filteredPixels,
6535 *hostPtr;
6536
6537 outputReady = MagickFalse;
6538 context = NULL;
6539 filteredImage = NULL;
6540 filteredImage_view = NULL;
6541 imageBuffer = NULL;
6542 filteredImageBuffer = NULL;
6543 imageKernelBuffer = NULL;
6544 motionBlurKernel = NULL;
6545 queue = NULL;
6546
6547 clEnv = GetDefaultOpenCLEnv();
6548 context = GetOpenCLContext(clEnv);
6549
6550 /* Create and initialize OpenCL buffers. */
6551
6552 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00006553 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristy0c832c62014-03-07 22:21:04 +00006554 if (inputPixels == (const void *) NULL)
6555 {
6556 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
dirk8a5cf512014-07-28 20:16:27 +00006557 "UnableToReadPixelCache.","`%s'",image->filename);
cristy0c832c62014-03-07 22:21:04 +00006558 goto cleanup;
6559 }
6560
6561 // If the host pointer is aligned to the size of CLPixelPacket,
6562 // then use the host buffer directly from the GPU; otherwise,
6563 // create a buffer on the GPU and copy the data over
6564 if (ALIGNED(inputPixels,CLPixelPacket))
6565 {
6566 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
6567 }
6568 else
6569 {
6570 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
6571 }
6572 // create a CL buffer from image pixel buffer
dirk8a5cf512014-07-28 20:16:27 +00006573 length = image->columns * image->rows;
6574 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags,
cristy0c832c62014-03-07 22:21:04 +00006575 length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
6576 if (clStatus != CL_SUCCESS)
6577 {
6578 (void) ThrowMagickException(exception, GetMagickModule(),
6579 ResourceLimitError, "clEnv->library->clCreateBuffer failed.",".");
6580 goto cleanup;
6581 }
6582
6583
dirk8a5cf512014-07-28 20:16:27 +00006584 filteredImage = CloneImage(image,image->columns,image->rows,
cristy0c832c62014-03-07 22:21:04 +00006585 MagickTrue,exception);
6586 assert(filteredImage != NULL);
dirk8a5cf512014-07-28 20:16:27 +00006587 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristy0c832c62014-03-07 22:21:04 +00006588 {
6589 (void) ThrowMagickException(exception, GetMagickModule(),
6590 ResourceLimitError, "CloneImage failed.", "'%s'", ".");
6591 goto cleanup;
6592 }
dirk832becc2014-08-04 19:44:34 +00006593 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +00006594 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristy0c832c62014-03-07 22:21:04 +00006595 if (filteredPixels == (void *) NULL)
6596 {
6597 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
6598 "UnableToReadPixelCache.","`%s'",filteredImage->filename);
6599 goto cleanup;
6600 }
6601
6602 if (ALIGNED(filteredPixels,CLPixelPacket))
6603 {
6604 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
6605 hostPtr = filteredPixels;
6606 }
6607 else
6608 {
6609 mem_flags = CL_MEM_WRITE_ONLY;
6610 hostPtr = NULL;
6611 }
6612 // create a CL buffer from image pixel buffer
dirk8a5cf512014-07-28 20:16:27 +00006613 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006614 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags,
6615 length * sizeof(CLPixelPacket), hostPtr, &clStatus);
6616 if (clStatus != CL_SUCCESS)
6617 {
6618 (void) ThrowMagickException(exception, GetMagickModule(),
6619 ResourceLimitError, "clEnv->library->clCreateBuffer failed.",".");
6620 goto cleanup;
6621 }
6622
6623
6624 imageKernelBuffer = clEnv->library->clCreateBuffer(context,
6625 CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, width * sizeof(float), NULL,
6626 &clStatus);
6627 if (clStatus != CL_SUCCESS)
6628 {
6629 (void) ThrowMagickException(exception, GetMagickModule(),
6630 ResourceLimitError, "clEnv->library->clCreateBuffer failed.",".");
6631 goto cleanup;
6632 }
6633
6634 queue = AcquireOpenCLCommandQueue(clEnv);
6635 kernelBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, imageKernelBuffer,
6636 CL_TRUE, CL_MAP_WRITE, 0, width * sizeof(float), 0, NULL, NULL, &clStatus);
6637 if (clStatus != CL_SUCCESS)
6638 {
6639 (void) ThrowMagickException(exception, GetMagickModule(),
6640 ResourceLimitError, "clEnv->library->clEnqueueMapBuffer failed.",".");
6641 goto cleanup;
6642 }
6643 for (i = 0; i < width; i++)
6644 {
6645 kernelBufferPtr[i] = (float) kernel[i];
6646 }
6647 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, imageKernelBuffer, kernelBufferPtr,
6648 0, NULL, NULL);
6649 if (clStatus != CL_SUCCESS)
6650 {
6651 (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError,
6652 "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
6653 goto cleanup;
6654 }
6655
6656 offsetBuffer = clEnv->library->clCreateBuffer(context,
6657 CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, width * sizeof(cl_int2), NULL,
6658 &clStatus);
6659 if (clStatus != CL_SUCCESS)
6660 {
6661 (void) ThrowMagickException(exception, GetMagickModule(),
6662 ResourceLimitError, "clEnv->library->clCreateBuffer failed.",".");
6663 goto cleanup;
6664 }
6665
6666 offsetBufferPtr = (int*)clEnv->library->clEnqueueMapBuffer(queue, offsetBuffer, CL_TRUE,
6667 CL_MAP_WRITE, 0, width * sizeof(cl_int2), 0, NULL, NULL, &clStatus);
6668 if (clStatus != CL_SUCCESS)
6669 {
6670 (void) ThrowMagickException(exception, GetMagickModule(),
6671 ResourceLimitError, "clEnv->library->clEnqueueMapBuffer failed.",".");
6672 goto cleanup;
6673 }
6674 for (i = 0; i < width; i++)
6675 {
6676 offsetBufferPtr[2*i] = (int)offset[i].x;
6677 offsetBufferPtr[2*i+1] = (int)offset[i].y;
6678 }
6679 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, offsetBuffer, offsetBufferPtr, 0,
6680 NULL, NULL);
6681 if (clStatus != CL_SUCCESS)
6682 {
6683 (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError,
6684 "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
6685 goto cleanup;
6686 }
6687
6688
6689 // get the OpenCL kernel
6690 motionBlurKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE,
6691 "MotionBlur");
6692 if (motionBlurKernel == NULL)
6693 {
6694 (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError,
6695 "AcquireOpenCLKernel failed.", "'%s'", ".");
6696 goto cleanup;
6697 }
6698
6699 // set the kernel arguments
6700 i = 0;
6701 clStatus=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(cl_mem),
dirk8a5cf512014-07-28 20:16:27 +00006702 (void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00006703 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(cl_mem),
6704 (void *)&filteredImageBuffer);
dirkb0d783f2014-08-31 10:48:05 +00006705 imageWidth = (unsigned int) image->columns;
6706 imageHeight = (unsigned int) image->rows;
cristy0c832c62014-03-07 22:21:04 +00006707 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(unsigned int),
6708 &imageWidth);
6709 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(unsigned int),
6710 &imageHeight);
6711 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(cl_mem),
6712 (void *)&imageKernelBuffer);
6713 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(unsigned int),
6714 &width);
6715 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(cl_mem),
6716 (void *)&offsetBuffer);
6717
dirk8a5cf512014-07-28 20:16:27 +00006718 GetPixelInfo(image,&bias);
cristy0c832c62014-03-07 22:21:04 +00006719 biasPixel.s[0] = bias.red;
6720 biasPixel.s[1] = bias.green;
6721 biasPixel.s[2] = bias.blue;
dirk8a5cf512014-07-28 20:16:27 +00006722 biasPixel.s[3] = bias.alpha;
cristy0c832c62014-03-07 22:21:04 +00006723 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(cl_float4), &biasPixel);
6724
6725 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(ChannelType), &channel);
dirk8a5cf512014-07-28 20:16:27 +00006726 matte = (image->alpha_trait==BlendPixelTrait)?1:0;
cristy0c832c62014-03-07 22:21:04 +00006727 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(unsigned int), &matte);
6728 if (clStatus != CL_SUCCESS)
6729 {
6730 (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError,
6731 "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
6732 goto cleanup;
6733 }
6734
6735 // launch the kernel
6736 local_work_size[0] = 16;
6737 local_work_size[1] = 16;
6738 global_work_size[0] = (size_t)padGlobalWorkgroupSizeToLocalWorkgroupSize(
dirkb0d783f2014-08-31 10:48:05 +00006739 (unsigned int) image->columns,(unsigned int) local_work_size[0]);
cristy0c832c62014-03-07 22:21:04 +00006740 global_work_size[1] = (size_t)padGlobalWorkgroupSizeToLocalWorkgroupSize(
dirkb0d783f2014-08-31 10:48:05 +00006741 (unsigned int) image->rows,(unsigned int) local_work_size[1]);
cristy0c832c62014-03-07 22:21:04 +00006742 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, motionBlurKernel, 2, NULL,
6743 global_work_size, local_work_size, 0, NULL, NULL);
6744
6745 if (clStatus != CL_SUCCESS)
6746 {
6747 (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError,
6748 "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
6749 goto cleanup;
6750 }
6751 clEnv->library->clFlush(queue);
6752
6753 if (ALIGNED(filteredPixels,CLPixelPacket))
6754 {
dirk8a5cf512014-07-28 20:16:27 +00006755 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006756 clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE,
6757 CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL,
6758 NULL, &clStatus);
6759 }
6760 else
6761 {
dirk8a5cf512014-07-28 20:16:27 +00006762 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006763 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0,
6764 length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
6765 }
6766 if (clStatus != CL_SUCCESS)
6767 {
6768 (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError,
6769 "Reading output image from CL buffer failed.", "'%s'", ".");
6770 goto cleanup;
6771 }
dirk832becc2014-08-04 19:44:34 +00006772 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
6773
6774cleanup:
6775
6776 image_view=DestroyCacheView(image_view);
6777 if (filteredImage_view != NULL)
dirk8a5cf512014-07-28 20:16:27 +00006778 filteredImage_view=DestroyCacheView(filteredImage_view);
cristy0c832c62014-03-07 22:21:04 +00006779
6780 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
dirk8a5cf512014-07-28 20:16:27 +00006781 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00006782 if (imageKernelBuffer!=NULL) clEnv->library->clReleaseMemObject(imageKernelBuffer);
6783 if (motionBlurKernel!=NULL) RelinquishOpenCLKernel(clEnv, motionBlurKernel);
6784 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
dirk832becc2014-08-04 19:44:34 +00006785 if (outputReady == MagickFalse && filteredImage != NULL)
6786 filteredImage=DestroyImage(filteredImage);
6787
dirk8a5cf512014-07-28 20:16:27 +00006788 return(filteredImage);
cristy0c832c62014-03-07 22:21:04 +00006789}
6790
dirk832becc2014-08-04 19:44:34 +00006791MagickExport Image *AccelerateMotionBlurImage(const Image *image,
6792 const ChannelType channel,const double* kernel,const size_t width,
6793 const OffsetInfo *offset,ExceptionInfo *exception)
6794{
6795 Image
6796 *filteredImage;
6797
6798 assert(image != NULL);
6799 assert(kernel != (double *) NULL);
6800 assert(offset != (OffsetInfo *) NULL);
6801 assert(exception != (ExceptionInfo *) NULL);
6802
6803 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
6804 (checkAccelerateCondition(image, channel) == MagickFalse))
6805 return NULL;
6806
6807 filteredImage=ComputeMotionBlurImage(image, channel, kernel, width,
6808 offset, exception);
6809 return(filteredImage);
cristy0c832c62014-03-07 22:21:04 +00006810}
6811
dirk832becc2014-08-04 19:44:34 +00006812static MagickBooleanType LaunchCompositeKernel(MagickCLEnv clEnv,
6813 cl_command_queue queue,cl_mem imageBuffer,const unsigned int inputWidth,
6814 const unsigned int inputHeight,const unsigned int matte,
6815 const ChannelType channel,const CompositeOperator compose,
6816 const cl_mem compositeImageBuffer,const unsigned int compositeWidth,
6817 const unsigned int compositeHeight,const float destination_dissolve,
6818 const float source_dissolve,ExceptionInfo *magick_unused(exception))
6819{
6820 cl_int
6821 clStatus;
6822
6823 cl_kernel
6824 compositeKernel;
6825
6826 int
6827 k;
6828
6829 size_t
6830 global_work_size[2],
6831 local_work_size[2];
6832
6833 unsigned int
6834 composeOp;
6835
cristy0c832c62014-03-07 22:21:04 +00006836 magick_unreferenced(exception);
6837
6838 compositeKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE,
6839 "Composite");
6840
6841 k = 0;
dirk8a5cf512014-07-28 20:16:27 +00006842 clStatus=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(cl_mem),(void*)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00006843 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(unsigned int),(void*)&inputWidth);
6844 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(unsigned int),(void*)&inputHeight);
6845 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(cl_mem),(void*)&compositeImageBuffer);
6846 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(unsigned int),(void*)&compositeWidth);
6847 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(unsigned int),(void*)&compositeHeight);
6848 composeOp = (unsigned int)compose;
6849 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(unsigned int),(void*)&composeOp);
6850 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(ChannelType),(void*)&channel);
6851 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(unsigned int),(void*)&matte);
6852 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(float),(void*)&destination_dissolve);
6853 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(float),(void*)&source_dissolve);
6854
6855 if (clStatus!=CL_SUCCESS)
6856 return MagickFalse;
6857
6858 local_work_size[0] = 64;
6859 local_work_size[1] = 1;
6860
6861 global_work_size[0] = padGlobalWorkgroupSizeToLocalWorkgroupSize(inputWidth,
dirkb0d783f2014-08-31 10:48:05 +00006862 (unsigned int) local_work_size[0]);
cristy0c832c62014-03-07 22:21:04 +00006863 global_work_size[1] = inputHeight;
6864 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, compositeKernel, 2, NULL,
6865 global_work_size, local_work_size, 0, NULL, NULL);
6866
6867
6868 RelinquishOpenCLKernel(clEnv, compositeKernel);
6869
dirk832becc2014-08-04 19:44:34 +00006870 return((clStatus==CL_SUCCESS) ? MagickTrue : MagickFalse);
6871}
6872
6873static MagickBooleanType ComputeCompositeImage(Image *image,
6874 const ChannelType channel,const CompositeOperator compose,
6875 const Image *compositeImage,const ssize_t magick_unused(x_offset),
6876 const ssize_t magick_unused(y_offset),const float destination_dissolve,
6877 const float source_dissolve,ExceptionInfo *exception)
6878{
6879 CacheView
6880 *image_view;
6881
6882 cl_command_queue
6883 queue;
6884
6885 cl_context
6886 context;
6887
6888 cl_int
6889 clStatus;
6890
6891 cl_mem_flags
6892 mem_flags;
6893
6894 cl_mem
6895 compositeImageBuffer,
6896 imageBuffer;
6897
6898 const void
6899 *composePixels;
6900
6901 MagickBooleanType
6902 outputReady,
6903 status;
6904
6905 MagickCLEnv
6906 clEnv;
6907
6908 MagickSizeType
6909 length;
6910
6911 void
6912 *inputPixels;
6913
6914 magick_unreferenced(x_offset);
6915 magick_unreferenced(y_offset);
6916
6917 status = MagickFalse;
6918 outputReady = MagickFalse;
6919 composePixels = NULL;
6920 imageBuffer = NULL;
6921 compositeImageBuffer = NULL;
6922
6923 clEnv = GetDefaultOpenCLEnv();
6924 context = GetOpenCLContext(clEnv);
6925 queue = AcquireOpenCLCommandQueue(clEnv);
6926
6927 /* Create and initialize OpenCL buffers. */
6928 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00006929 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristy0c832c62014-03-07 22:21:04 +00006930 if (inputPixels == (void *) NULL)
6931 {
6932 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,
dirk8a5cf512014-07-28 20:16:27 +00006933 "UnableToReadPixelCache.","`%s'",image->filename);
cristy0c832c62014-03-07 22:21:04 +00006934 goto cleanup;
6935 }
6936
6937 /* If the host pointer is aligned to the size of CLPixelPacket,
6938 then use the host buffer directly from the GPU; otherwise,
6939 create a buffer on the GPU and copy the data over */
6940 if (ALIGNED(inputPixels,CLPixelPacket))
6941 {
6942 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
6943 }
6944 else
6945 {
6946 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
6947 }
6948 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00006949 length = image->columns * image->rows;
6950 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags,
cristy0c832c62014-03-07 22:21:04 +00006951 length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
6952 if (clStatus != CL_SUCCESS)
6953 {
6954 (void) OpenCLThrowMagickException(exception, GetMagickModule(),
6955 ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
6956 goto cleanup;
6957 }
6958
6959
6960 /* Create and initialize OpenCL buffers. */
6961 composePixels = AcquirePixelCachePixels(compositeImage, &length, exception);
6962 if (composePixels == (void *) NULL)
6963 {
6964 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,
6965 "UnableToReadPixelCache.","`%s'",compositeImage->filename);
6966 goto cleanup;
6967 }
6968
6969 /* If the host pointer is aligned to the size of CLPixelPacket,
6970 then use the host buffer directly from the GPU; otherwise,
6971 create a buffer on the GPU and copy the data over */
6972 if (ALIGNED(composePixels,CLPixelPacket))
6973 {
6974 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
6975 }
6976 else
6977 {
6978 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
6979 }
6980 /* create a CL buffer from image pixel buffer */
6981 length = compositeImage->columns * compositeImage->rows;
6982 compositeImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags,
6983 length * sizeof(CLPixelPacket), (void*)composePixels, &clStatus);
6984 if (clStatus != CL_SUCCESS)
6985 {
6986 (void) OpenCLThrowMagickException(exception, GetMagickModule(),
6987 ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
6988 goto cleanup;
6989 }
6990
dirk8a5cf512014-07-28 20:16:27 +00006991 status = LaunchCompositeKernel(clEnv,queue,imageBuffer,
6992 (unsigned int) image->columns,
6993 (unsigned int) image->rows,
6994 (unsigned int) (image->alpha_trait==BlendPixelTrait) ? 1 : 0,
cristy0c832c62014-03-07 22:21:04 +00006995 channel, compose, compositeImageBuffer,
6996 (unsigned int) compositeImage->columns,
6997 (unsigned int) compositeImage->rows,
6998 destination_dissolve,source_dissolve,
6999 exception);
7000
7001 if (status==MagickFalse)
7002 goto cleanup;
7003
dirk8a5cf512014-07-28 20:16:27 +00007004 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00007005 if (ALIGNED(inputPixels,CLPixelPacket))
7006 {
dirk8a5cf512014-07-28 20:16:27 +00007007 clEnv->library->clEnqueueMapBuffer(queue, imageBuffer, CL_TRUE,
cristy0c832c62014-03-07 22:21:04 +00007008 CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL,
7009 NULL, &clStatus);
7010 }
7011 else
7012 {
dirk8a5cf512014-07-28 20:16:27 +00007013 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0,
cristy0c832c62014-03-07 22:21:04 +00007014 length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
7015 }
7016 if (clStatus==CL_SUCCESS)
dirk832becc2014-08-04 19:44:34 +00007017 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
7018
7019cleanup:
7020
7021 image_view=DestroyCacheView(image_view);
dirk8a5cf512014-07-28 20:16:27 +00007022 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00007023 if (compositeImageBuffer!=NULL) clEnv->library->clReleaseMemObject(compositeImageBuffer);
7024 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
7025
dirk832becc2014-08-04 19:44:34 +00007026 return(outputReady);
7027}
7028
7029MagickExport MagickBooleanType AccelerateCompositeImage(Image *image,
7030 const ChannelType channel,const CompositeOperator compose,
7031 const Image *composite,const ssize_t x_offset,const ssize_t y_offset,
7032 const float destination_dissolve,const float source_dissolve,
7033 ExceptionInfo *exception)
7034{
7035 MagickBooleanType
7036 status;
7037
7038 assert(image != NULL);
7039 assert(exception != (ExceptionInfo *) NULL);
7040
7041 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
7042 (checkAccelerateCondition(image, channel) == MagickFalse))
dirk8a5cf512014-07-28 20:16:27 +00007043 return(MagickFalse);
cristy0c832c62014-03-07 22:21:04 +00007044
7045 /* only support zero offset and
7046 images with the size for now */
7047 if (x_offset!=0
7048 || y_offset!=0
7049 || image->columns!=composite->columns
7050 || image->rows!=composite->rows)
7051 return MagickFalse;
7052
7053 switch(compose) {
7054 case ColorDodgeCompositeOp:
7055 case BlendCompositeOp:
7056 break;
7057 default:
7058 // unsupported compose operator, quit
7059 return MagickFalse;
7060 };
7061
7062 status = ComputeCompositeImage(image,channel,compose,composite,
7063 x_offset,y_offset,destination_dissolve,source_dissolve,exception);
7064
dirk8a5cf512014-07-28 20:16:27 +00007065 return(status);
cristy0c832c62014-03-07 22:21:04 +00007066}
7067
cristyf034abb2013-11-24 14:16:14 +00007068#else /* MAGICKCORE_OPENCL_SUPPORT */
7069
7070MagickExport Image *AccelerateConvolveImageChannel(
7071 const Image *magick_unused(image),const ChannelType magick_unused(channel),
7072 const KernelInfo *magick_unused(kernel),
7073 ExceptionInfo *magick_unused(exception))
7074{
7075 magick_unreferenced(image);
7076 magick_unreferenced(channel);
7077 magick_unreferenced(kernel);
7078 magick_unreferenced(exception);
7079
7080 return NULL;
7081}
7082
7083MagickExport MagickBooleanType AccelerateFunctionImage(
7084 Image *magick_unused(image),const ChannelType magick_unused(channel),
7085 const MagickFunction magick_unused(function),
7086 const size_t magick_unused(number_parameters),
7087 const double *magick_unused(parameters),
7088 ExceptionInfo *magick_unused(exception))
7089{
7090 magick_unreferenced(image);
7091 magick_unreferenced(channel);
7092 magick_unreferenced(function);
7093 magick_unreferenced(number_parameters);
7094 magick_unreferenced(parameters);
7095 magick_unreferenced(exception);
7096
7097 return MagickFalse;
7098}
7099
7100MagickExport Image *AccelerateBlurImage(const Image *magick_unused(image),
7101 const ChannelType magick_unused(channel),const double magick_unused(radius),
7102 const double magick_unused(sigma),ExceptionInfo *magick_unused(exception))
7103{
7104 magick_unreferenced(image);
7105 magick_unreferenced(channel);
7106 magick_unreferenced(radius);
7107 magick_unreferenced(sigma);
7108 magick_unreferenced(exception);
7109
7110 return NULL;
7111}
7112
dirk6d612cf2014-03-13 21:17:23 +00007113MagickExport Image *AccelerateRotationalBlurImage(
cristyf034abb2013-11-24 14:16:14 +00007114 const Image *magick_unused(image),const ChannelType magick_unused(channel),
7115 const double magick_unused(angle),ExceptionInfo *magick_unused(exception))
7116{
7117 magick_unreferenced(image);
7118 magick_unreferenced(channel);
7119 magick_unreferenced(angle);
7120 magick_unreferenced(exception);
7121
7122 return NULL;
7123}
7124
7125
7126MagickExport Image *AccelerateUnsharpMaskImage(
7127 const Image *magick_unused(image),const ChannelType magick_unused(channel),
7128 const double magick_unused(radius),const double magick_unused(sigma),
7129 const double magick_unused(gain),const double magick_unused(threshold),
7130 ExceptionInfo *magick_unused(exception))
7131{
7132 magick_unreferenced(image);
7133 magick_unreferenced(channel);
7134 magick_unreferenced(radius);
7135 magick_unreferenced(sigma);
7136 magick_unreferenced(gain);
7137 magick_unreferenced(threshold);
7138 magick_unreferenced(exception);
7139
7140 return NULL;
7141}
7142
cristy0b8a2e92014-03-08 00:52:49 +00007143MagickExport
7144MagickBooleanType AccelerateCompositeImage(Image *image,
7145 const ChannelType channel,const CompositeOperator compose,
7146 const Image *composite,const ssize_t x_offset,const ssize_t y_offset,
7147 const float destination_dissolve,const float source_dissolve,
7148 ExceptionInfo *exception)
7149{
7150 magick_unreferenced(image);
cristya219e4b2014-03-08 12:36:16 +00007151 magick_unreferenced(channel);
7152 magick_unreferenced(compose);
7153 magick_unreferenced(composite);
7154 magick_unreferenced(x_offset);
7155 magick_unreferenced(y_offset);
7156 magick_unreferenced(destination_dissolve);
7157 magick_unreferenced(source_dissolve);
7158 magick_unreferenced(exception);
cristy0b8a2e92014-03-08 00:52:49 +00007159
7160 return MagickFalse;
7161}
7162
cristyf034abb2013-11-24 14:16:14 +00007163
7164MagickExport MagickBooleanType AccelerateContrastImage(
7165 Image* magick_unused(image),const MagickBooleanType magick_unused(sharpen),
7166 ExceptionInfo* magick_unused(exception))
7167{
7168 magick_unreferenced(image);
7169 magick_unreferenced(sharpen);
7170 magick_unreferenced(exception);
7171
7172 return MagickFalse;
7173}
7174
cristy0b8a2e92014-03-08 00:52:49 +00007175MagickExport MagickBooleanType AccelerateContrastStretchImageChannel(
cristy0c832c62014-03-07 22:21:04 +00007176 Image * image, const ChannelType channel, const double black_point, const double white_point,
7177 ExceptionInfo* magick_unused(exception))
7178{
7179 magick_unreferenced(image);
7180 magick_unreferenced(channel);
7181 magick_unreferenced(black_point);
7182 magick_unreferenced(white_point);
7183 magick_unreferenced(exception);
7184
7185 return MagickFalse;
7186}
7187
cristyf034abb2013-11-24 14:16:14 +00007188MagickExport MagickBooleanType AccelerateEqualizeImage(
7189 Image* magick_unused(image), const ChannelType magick_unused(channel),
7190 ExceptionInfo* magick_unused(exception))
7191{
7192 magick_unreferenced(image);
7193 magick_unreferenced(channel);
7194 magick_unreferenced(exception);
7195
7196 return MagickFalse;
7197}
7198
7199MagickExport Image *AccelerateDespeckleImage(const Image* magick_unused(image),
7200 ExceptionInfo* magick_unused(exception))
7201{
7202 magick_unreferenced(image);
7203 magick_unreferenced(exception);
7204
7205 return NULL;
7206}
7207
7208MagickExport Image *AccelerateResizeImage(const Image* magick_unused(image),
7209 const size_t magick_unused(resizedColumns),
7210 const size_t magick_unused(resizedRows),
7211 const ResizeFilter* magick_unused(resizeFilter),
7212 ExceptionInfo *magick_unused(exception))
7213{
7214 magick_unreferenced(image);
7215 magick_unreferenced(resizedColumns);
7216 magick_unreferenced(resizedRows);
7217 magick_unreferenced(resizeFilter);
7218 magick_unreferenced(exception);
7219
7220 return NULL;
7221}
7222
cristyf034abb2013-11-24 14:16:14 +00007223MagickExport
7224MagickBooleanType AccelerateModulateImage(
7225 Image* image, double percent_brightness, double percent_hue,
7226 double percent_saturation, ColorspaceType colorspace, ExceptionInfo* exception)
7227{
7228 magick_unreferenced(image);
7229 magick_unreferenced(percent_brightness);
7230 magick_unreferenced(percent_hue);
7231 magick_unreferenced(percent_saturation);
7232 magick_unreferenced(colorspace);
7233 magick_unreferenced(exception);
7234 return(MagickFalse);
7235}
7236
cristy0c832c62014-03-07 22:21:04 +00007237MagickExport
cristy0c832c62014-03-07 22:21:04 +00007238MagickBooleanType AccelerateGrayscaleImage(
7239 Image* image, const PixelIntensityMethod method, ExceptionInfo* exception)
7240{
7241 magick_unreferenced(image);
7242 magick_unreferenced(method);
7243 magick_unreferenced(exception);
7244 return(MagickFalse);
7245}
7246
cristye85d0f72013-11-27 02:25:43 +00007247MagickExport Image *AccelerateAddNoiseImage(const Image *image,
7248 const ChannelType channel, const NoiseType noise_type,ExceptionInfo *exception)
7249{
7250 magick_unreferenced(image);
7251 magick_unreferenced(channel);
7252 magick_unreferenced(noise_type);
7253 magick_unreferenced(exception);
7254 return NULL;
7255}
cristyf034abb2013-11-24 14:16:14 +00007256
cristy0c832c62014-03-07 22:21:04 +00007257
7258MagickExport MagickBooleanType AccelerateRandomImage(Image* image, ExceptionInfo* exception)
7259{
7260 magick_unreferenced(image);
7261 magick_unreferenced(exception);
7262 return MagickFalse;
7263}
7264
7265MagickExport
7266Image* AccelerateMotionBlurImage(const Image *image, const ChannelType channel,
7267 const double* kernel, const size_t width,
7268 const OffsetInfo *offset,
7269 ExceptionInfo *exception)
7270{
7271 magick_unreferenced(image);
7272 magick_unreferenced(channel);
7273 magick_unreferenced(kernel);
7274 magick_unreferenced(width);
7275 magick_unreferenced(offset);
7276 magick_unreferenced(exception);
7277 return NULL;
7278}
7279
dirkbae82632014-08-28 20:13:59 +00007280#endif /* MAGICKCORE_OPENCL_SUPPORT */