blob: 908478076d56b7c4a88c47538120c2475bbd4f17 [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
91#define ALIGNED(pointer,type) ((((long)(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);
205 tempSize=image->columns * image->rows * 4 * 4;
206
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
390 kernelSize = 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);
dirk8a5cf512014-07-28 20:16:27 +0000449 imageWidth = image->columns;
450 imageHeight = 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);
cristyf034abb2013-11-24 14:16:14 +0000454 filterWidth = kernel->width;
455 filterHeight = 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);
dirk8a5cf512014-07-28 20:16:27 +0000495 imageWidth = image->columns;
496 imageHeight = 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);
cristyf034abb2013-11-24 14:16:14 +0000500 filterWidth = kernel->width;
501 filterHeight = 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 {
dirk8a5cf512014-07-28 20:16:27 +00001050 imageColumns = image->columns;
1051 imageRows = 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);
cristyf034abb2013-11-24 14:16:14 +00001059 kernelWidth = 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 {
dirk8a5cf512014-07-28 20:16:27 +00001096 imageColumns = image->columns;
1097 imageRows = 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);
cristyf034abb2013-11-24 14:16:14 +00001105 kernelWidth = 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 {
dirk8a5cf512014-07-28 20:16:27 +00001402 imageColumns = image->columns;
cristyf034abb2013-11-24 14:16:14 +00001403 if (sec == 0)
dirk8a5cf512014-07-28 20:16:27 +00001404 imageRows = image->rows / 2 + (kernel->width-1) / 2;
cristyf034abb2013-11-24 14:16:14 +00001405 else
dirk8a5cf512014-07-28 20:16:27 +00001406 imageRows = (image->rows - image->rows / 2) + (kernel->width-1) / 2;
cristyf034abb2013-11-24 14:16:14 +00001407
dirk8a5cf512014-07-28 20:16:27 +00001408 offsetRows = sec * image->rows / 2;
cristyf034abb2013-11-24 14:16:14 +00001409
1410 kernelWidth = kernel->width;
1411
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 {
dirk8a5cf512014-07-28 20:16:27 +00001456 imageColumns = image->columns;
cristyf034abb2013-11-24 14:16:14 +00001457 if (sec == 0)
dirk8a5cf512014-07-28 20:16:27 +00001458 imageRows = image->rows / 2;
cristyf034abb2013-11-24 14:16:14 +00001459 else
dirk8a5cf512014-07-28 20:16:27 +00001460 imageRows = (image->rows - image->rows / 2);
cristyf034abb2013-11-24 14:16:14 +00001461
dirk8a5cf512014-07-28 20:16:27 +00001462 offsetRows = sec * image->rows / 2;
cristyf034abb2013-11-24 14:16:14 +00001463
1464 kernelWidth = kernel->width;
1465
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
dirk8a5cf512014-07-28 20:16:27 +00002200 imageColumns = image->columns;
2201 imageRows = image->rows;
cristyf034abb2013-11-24 14:16:14 +00002202
2203 kernelWidth = kernel->width;
2204
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;
dirk8a5cf512014-07-28 20:16:27 +00002244 imageColumns = image->columns;
2245 imageRows = image->rows;
cristyf034abb2013-11-24 14:16:14 +00002246 kernelWidth = kernel->width;
2247 fGain = (float)gain;
2248 fThreshold = (float)threshold;
2249
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
dirk8a5cf512014-07-28 20:16:27 +00002566 imageColumns = image->columns;
cristyf034abb2013-11-24 14:16:14 +00002567 if (sec == 0)
dirk8a5cf512014-07-28 20:16:27 +00002568 imageRows = image->rows / 2 + (kernel->width-1) / 2;
cristyf034abb2013-11-24 14:16:14 +00002569 else
dirk8a5cf512014-07-28 20:16:27 +00002570 imageRows = (image->rows - image->rows / 2) + (kernel->width-1) / 2;
cristyf034abb2013-11-24 14:16:14 +00002571
dirk8a5cf512014-07-28 20:16:27 +00002572 offsetRows = sec * image->rows / 2;
cristyf034abb2013-11-24 14:16:14 +00002573
2574 kernelWidth = kernel->width;
2575
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
dirk8a5cf512014-07-28 20:16:27 +00002617 imageColumns = image->columns;
cristyf034abb2013-11-24 14:16:14 +00002618 if (sec == 0)
dirk8a5cf512014-07-28 20:16:27 +00002619 imageRows = image->rows / 2;
cristyf034abb2013-11-24 14:16:14 +00002620 else
dirk8a5cf512014-07-28 20:16:27 +00002621 imageRows = (image->rows - image->rows / 2);
cristyf034abb2013-11-24 14:16:14 +00002622
dirk8a5cf512014-07-28 20:16:27 +00002623 offsetRows = sec * image->rows / 2;
cristyf034abb2013-11-24 14:16:14 +00002624
2625 kernelWidth = kernel->width;
2626
2627 fGain = (float)gain;
2628 fThreshold = (float)threshold;
2629
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 {
2917 imageColumns = image->columns;
2918 imageRows = image->rows;
2919 kernelWidth = kernel->width;
2920 fGain = (float)gain;
2921 fThreshold = (float)threshold;
2922 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);
cristyf034abb2013-11-24 14:16:14 +00003264 if (clStatus != CL_SUCCESS)
3265 {
cristy0c832c62014-03-07 22:21:04 +00003266 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003267 goto cleanup;
3268 }
cristy0c832c62014-03-07 22:21:04 +00003269 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00003270 status = MagickTrue;
3271
3272
3273cleanup:
cristya22457d2013-12-07 14:03:06 +00003274 OpenCLLogException(__FUNCTION__,__LINE__,exception);
3275
cristyf034abb2013-11-24 14:16:14 +00003276 if (horizontalKernel != NULL) RelinquishOpenCLKernel(clEnv, horizontalKernel);
3277
dirk832becc2014-08-04 19:44:34 +00003278 return(status);
3279}
3280
3281static MagickBooleanType resizeVerticalFilter(cl_mem image,
3282 const unsigned int imageColumns,const unsigned int imageRows,
3283 const unsigned int matte,cl_mem resizedImage,
3284 const unsigned int resizedColumns,const unsigned int resizedRows,
3285 const ResizeFilter *resizeFilter,cl_mem resizeFilterCubicCoefficients,
3286 const float yFactor,MagickCLEnv clEnv,cl_command_queue queue,
3287 ExceptionInfo *exception)
3288{
3289 cl_kernel
3290 horizontalKernel;
3291
3292 cl_int clStatus;
3293
3294 const unsigned int
3295 workgroupSize = 256;
3296
3297 float
3298 resizeFilterScale,
3299 resizeFilterSupport,
3300 resizeFilterWindowSupport,
3301 resizeFilterBlur,
3302 scale,
3303 support;
3304
3305 int
3306 cacheRangeStart,
3307 cacheRangeEnd,
3308 numCachedPixels,
3309 resizeFilterType,
3310 resizeWindowType;
3311
3312 MagickBooleanType
3313 status = MagickFalse;
3314
3315 size_t
3316 deviceLocalMemorySize,
3317 gammaAccumulatorLocalMemorySize,
3318 global_work_size[2],
3319 imageCacheLocalMemorySize,
3320 pixelAccumulatorLocalMemorySize,
3321 local_work_size[2],
3322 totalLocalMemorySize,
3323 weightAccumulatorLocalMemorySize;
3324
3325 unsigned int
3326 chunkSize,
3327 i,
3328 pixelPerWorkgroup;
3329
3330 horizontalKernel = NULL;
dirk8a5cf512014-07-28 20:16:27 +00003331 status = MagickFalse;
cristyf034abb2013-11-24 14:16:14 +00003332
3333 /*
3334 Apply filter to resize vertically from image to resize image.
3335 */
cristye85d0f72013-11-27 02:25:43 +00003336 scale=MAGICK_MAX(1.0/yFactor+MagickEpsilon,1.0);
cristyf034abb2013-11-24 14:16:14 +00003337 support=scale*GetResizeFilterSupport(resizeFilter);
3338 if (support < 0.5)
3339 {
3340 /*
3341 Support too small even for nearest neighbour: Reduce to point
3342 sampling.
3343 */
3344 support=(MagickRealType) 0.5;
3345 scale=1.0;
3346 }
3347 scale=PerceptibleReciprocal(scale);
3348
3349 if (resizedRows < workgroupSize)
3350 {
3351 chunkSize = 32;
3352 pixelPerWorkgroup = 32;
3353 }
3354 else
3355 {
3356 chunkSize = workgroupSize;
3357 pixelPerWorkgroup = workgroupSize;
3358 }
3359
3360 /* get the local memory size supported by the device */
3361 deviceLocalMemorySize = GetOpenCLDeviceLocalMemorySize(clEnv);
3362
dirke3c5f892013-12-10 06:04:40 +00003363DisableMSCWarning(4127)
cristyf034abb2013-11-24 14:16:14 +00003364 while(1)
dirke3c5f892013-12-10 06:04:40 +00003365RestoreMSCWarning
cristyf034abb2013-11-24 14:16:14 +00003366 {
3367 /* calculate the local memory size needed per workgroup */
3368 cacheRangeStart = (int) (((0 + 0.5)/yFactor+MagickEpsilon)-support+0.5);
3369 cacheRangeEnd = (int) ((((pixelPerWorkgroup-1) + 0.5)/yFactor+MagickEpsilon)+support+0.5);
3370 numCachedPixels = cacheRangeEnd - cacheRangeStart + 1;
3371 imageCacheLocalMemorySize = numCachedPixels * sizeof(CLPixelPacket);
3372 totalLocalMemorySize = imageCacheLocalMemorySize;
3373
3374 /* local size for the pixel accumulator */
3375 pixelAccumulatorLocalMemorySize = chunkSize * sizeof(cl_float4);
3376 totalLocalMemorySize+=pixelAccumulatorLocalMemorySize;
3377
3378 /* local memory size for the weight accumulator */
3379 weightAccumulatorLocalMemorySize = chunkSize * sizeof(float);
3380 totalLocalMemorySize+=weightAccumulatorLocalMemorySize;
3381
3382 /* local memory size for the gamma accumulator */
3383 if (matte == 0)
3384 gammaAccumulatorLocalMemorySize = sizeof(float);
3385 else
3386 gammaAccumulatorLocalMemorySize = chunkSize * sizeof(float);
3387 totalLocalMemorySize+=gammaAccumulatorLocalMemorySize;
3388
3389 if (totalLocalMemorySize <= deviceLocalMemorySize)
3390 break;
3391 else
3392 {
3393 pixelPerWorkgroup = pixelPerWorkgroup/2;
3394 chunkSize = chunkSize/2;
3395 if (pixelPerWorkgroup == 0
3396 || chunkSize == 0)
3397 {
3398 /* quit, fallback to CPU */
3399 goto cleanup;
3400 }
3401 }
3402 }
3403
3404 resizeFilterType = (int)GetResizeFilterWeightingType(resizeFilter);
3405 resizeWindowType = (int)GetResizeFilterWindowWeightingType(resizeFilter);
3406
3407 if (resizeFilterType == SincFastWeightingFunction
3408 && resizeWindowType == SincFastWeightingFunction)
3409 horizontalKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "ResizeVerticalFilterSinc");
3410 else
3411 horizontalKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "ResizeVerticalFilter");
3412
3413 if (horizontalKernel == NULL)
3414 {
cristya22457d2013-12-07 14:03:06 +00003415 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003416 goto cleanup;
3417 }
3418
3419 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00003420 clStatus = clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(cl_mem), (void*)&image);
3421 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&imageColumns);
3422 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&imageRows);
cristy0c832c62014-03-07 22:21:04 +00003423 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&matte);
3424 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&yFactor);
3425 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(cl_mem), (void*)&resizedImage);
cristyf034abb2013-11-24 14:16:14 +00003426
cristy0c832c62014-03-07 22:21:04 +00003427 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&resizedColumns);
3428 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&resizedRows);
cristyf034abb2013-11-24 14:16:14 +00003429
cristy0c832c62014-03-07 22:21:04 +00003430 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(int), (void*)&resizeFilterType);
3431 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(int), (void*)&resizeWindowType);
3432 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(cl_mem), (void*)&resizeFilterCubicCoefficients);
cristyf034abb2013-11-24 14:16:14 +00003433
3434 resizeFilterScale = (float) GetResizeFilterScale(resizeFilter);
cristy0c832c62014-03-07 22:21:04 +00003435 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterScale);
cristyf034abb2013-11-24 14:16:14 +00003436
3437 resizeFilterSupport = (float) GetResizeFilterSupport(resizeFilter);
cristy0c832c62014-03-07 22:21:04 +00003438 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterSupport);
cristyf034abb2013-11-24 14:16:14 +00003439
3440 resizeFilterWindowSupport = (float) GetResizeFilterWindowSupport(resizeFilter);
cristy0c832c62014-03-07 22:21:04 +00003441 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterWindowSupport);
cristyf034abb2013-11-24 14:16:14 +00003442
3443 resizeFilterBlur = (float) GetResizeFilterBlur(resizeFilter);
cristy0c832c62014-03-07 22:21:04 +00003444 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterBlur);
cristyf034abb2013-11-24 14:16:14 +00003445
3446
cristy0c832c62014-03-07 22:21:04 +00003447 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, imageCacheLocalMemorySize, NULL);
3448 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(int), &numCachedPixels);
3449 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), &pixelPerWorkgroup);
3450 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), &chunkSize);
cristyf034abb2013-11-24 14:16:14 +00003451
3452
cristy0c832c62014-03-07 22:21:04 +00003453 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, pixelAccumulatorLocalMemorySize, NULL);
3454 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, weightAccumulatorLocalMemorySize, NULL);
3455 clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, gammaAccumulatorLocalMemorySize, NULL);
cristyf034abb2013-11-24 14:16:14 +00003456
3457 if (clStatus != CL_SUCCESS)
3458 {
cristy0c832c62014-03-07 22:21:04 +00003459 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003460 goto cleanup;
3461 }
3462
3463 global_work_size[0] = resizedColumns;
3464 global_work_size[1] = (resizedRows+pixelPerWorkgroup-1)/pixelPerWorkgroup*workgroupSize;
3465
3466 local_work_size[0] = 1;
3467 local_work_size[1] = workgroupSize;
cristy0c832c62014-03-07 22:21:04 +00003468 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, horizontalKernel, 2, NULL, global_work_size, local_work_size, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00003469 if (clStatus != CL_SUCCESS)
3470 {
cristy0c832c62014-03-07 22:21:04 +00003471 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003472 goto cleanup;
3473 }
cristy0c832c62014-03-07 22:21:04 +00003474 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00003475 status = MagickTrue;
3476
3477
3478cleanup:
cristya22457d2013-12-07 14:03:06 +00003479 OpenCLLogException(__FUNCTION__,__LINE__,exception);
3480
cristyf034abb2013-11-24 14:16:14 +00003481 if (horizontalKernel != NULL) RelinquishOpenCLKernel(clEnv, horizontalKernel);
3482
dirk8a5cf512014-07-28 20:16:27 +00003483 return(status);
cristyf034abb2013-11-24 14:16:14 +00003484}
3485
dirk832becc2014-08-04 19:44:34 +00003486static Image *ComputeResizeImage(const Image* image,
3487 const size_t resizedColumns,const size_t resizedRows,
3488 const ResizeFilter *resizeFilter,ExceptionInfo *exception)
3489{
3490 CacheView
3491 *filteredImage_view,
3492 *image_view;
3493
3494 cl_command_queue
3495 queue;
3496
3497 cl_int
3498 clStatus;
3499
3500 cl_context
3501 context;
3502
3503 cl_mem
3504 cubicCoefficientsBuffer,
3505 filteredImageBuffer,
3506 imageBuffer,
3507 tempImageBuffer;
3508
3509 cl_mem_flags
3510 mem_flags;
3511
3512 const double
3513 *resizeFilterCoefficient;
3514
3515 const void
3516 *inputPixels;
3517
3518 float
3519 *mappedCoefficientBuffer,
3520 xFactor,
3521 yFactor;
3522
3523 MagickBooleanType
3524 outputReady,
3525 status;
3526
3527 MagickCLEnv
3528 clEnv;
3529
3530 MagickSizeType
3531 length;
3532
3533 Image
3534 *filteredImage;
3535
3536 unsigned int
3537 i;
3538
3539 void
3540 *filteredPixels,
3541 *hostPtr;
3542
3543 outputReady = MagickFalse;
3544 filteredImage = NULL;
3545 filteredImage_view = NULL;
3546 clEnv = NULL;
3547 context = NULL;
3548 imageBuffer = NULL;
3549 tempImageBuffer = NULL;
3550 filteredImageBuffer = NULL;
3551 cubicCoefficientsBuffer = NULL;
3552 queue = NULL;
3553
3554 clEnv = GetDefaultOpenCLEnv();
3555 context = GetOpenCLContext(clEnv);
3556
3557 /* Create and initialize OpenCL buffers. */
3558 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00003559 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00003560 if (inputPixels == (const void *) NULL)
3561 {
dirk8a5cf512014-07-28 20:16:27 +00003562 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +00003563 goto cleanup;
3564 }
3565
3566 /* If the host pointer is aligned to the size of CLPixelPacket,
3567 then use the host buffer directly from the GPU; otherwise,
3568 create a buffer on the GPU and copy the data over */
3569 if (ALIGNED(inputPixels,CLPixelPacket))
3570 {
3571 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
3572 }
3573 else
3574 {
3575 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
3576 }
3577 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00003578 length = image->columns * image->rows;
3579 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003580 if (clStatus != CL_SUCCESS)
3581 {
cristy0c832c62014-03-07 22:21:04 +00003582 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003583 goto cleanup;
3584 }
3585
cristy0c832c62014-03-07 22:21:04 +00003586 cubicCoefficientsBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY, 7 * sizeof(float), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003587 if (clStatus != CL_SUCCESS)
3588 {
cristy0c832c62014-03-07 22:21:04 +00003589 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003590 goto cleanup;
3591 }
3592 queue = AcquireOpenCLCommandQueue(clEnv);
cristy0c832c62014-03-07 22:21:04 +00003593 mappedCoefficientBuffer = (float*)clEnv->library->clEnqueueMapBuffer(queue, cubicCoefficientsBuffer, CL_TRUE, CL_MAP_WRITE, 0, 7 * sizeof(float)
cristyf034abb2013-11-24 14:16:14 +00003594 , 0, NULL, NULL, &clStatus);
3595 if (clStatus != CL_SUCCESS)
3596 {
cristy0c832c62014-03-07 22:21:04 +00003597 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003598 goto cleanup;
3599 }
3600 resizeFilterCoefficient = GetResizeFilterCoefficient(resizeFilter);
3601 for (i = 0; i < 7; i++)
3602 {
3603 mappedCoefficientBuffer[i] = (float) resizeFilterCoefficient[i];
3604 }
cristy0c832c62014-03-07 22:21:04 +00003605 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, cubicCoefficientsBuffer, mappedCoefficientBuffer, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00003606 if (clStatus != CL_SUCCESS)
3607 {
cristy0c832c62014-03-07 22:21:04 +00003608 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003609 goto cleanup;
3610 }
3611
dirk8a5cf512014-07-28 20:16:27 +00003612 filteredImage = CloneImage(image,resizedColumns,resizedRows,MagickTrue,exception);
cristyf034abb2013-11-24 14:16:14 +00003613 if (filteredImage == NULL)
3614 goto cleanup;
3615
dirk8a5cf512014-07-28 20:16:27 +00003616 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristyf034abb2013-11-24 14:16:14 +00003617 {
cristya22457d2013-12-07 14:03:06 +00003618 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003619 goto cleanup;
3620 }
dirk832becc2014-08-04 19:44:34 +00003621 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +00003622 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00003623 if (filteredPixels == (void *) NULL)
3624 {
cristya22457d2013-12-07 14:03:06 +00003625 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename);
cristyf034abb2013-11-24 14:16:14 +00003626 goto cleanup;
3627 }
3628
3629 if (ALIGNED(filteredPixels,CLPixelPacket))
3630 {
3631 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
3632 hostPtr = filteredPixels;
3633 }
3634 else
3635 {
3636 mem_flags = CL_MEM_WRITE_ONLY;
3637 hostPtr = NULL;
3638 }
3639
3640 /* create a CL buffer from image pixel buffer */
3641 length = filteredImage->columns * filteredImage->rows;
cristy0c832c62014-03-07 22:21:04 +00003642 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003643 if (clStatus != CL_SUCCESS)
3644 {
cristy0c832c62014-03-07 22:21:04 +00003645 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003646 goto cleanup;
3647 }
3648
dirk8a5cf512014-07-28 20:16:27 +00003649 xFactor=(float) resizedColumns/(float) image->columns;
3650 yFactor=(float) resizedRows/(float) image->rows;
cristyf034abb2013-11-24 14:16:14 +00003651 if (xFactor > yFactor)
3652 {
3653
dirk8a5cf512014-07-28 20:16:27 +00003654 length = resizedColumns*image->rows;
cristy0c832c62014-03-07 22:21:04 +00003655 tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length*sizeof(CLPixelPacket), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003656 if (clStatus != CL_SUCCESS)
3657 {
cristy0c832c62014-03-07 22:21:04 +00003658 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003659 goto cleanup;
3660 }
3661
dirk8a5cf512014-07-28 20:16:27 +00003662 status = resizeHorizontalFilter(imageBuffer, image->columns, image->rows, (image->alpha_trait==BlendPixelTrait)?1:0
3663 , tempImageBuffer, resizedColumns, image->rows
cristyf034abb2013-11-24 14:16:14 +00003664 , resizeFilter, cubicCoefficientsBuffer
3665 , xFactor, clEnv, queue, exception);
3666 if (status != MagickTrue)
3667 goto cleanup;
3668
dirk8a5cf512014-07-28 20:16:27 +00003669 status = resizeVerticalFilter(tempImageBuffer, resizedColumns, image->rows, (image->alpha_trait==BlendPixelTrait)?1:0
cristyf034abb2013-11-24 14:16:14 +00003670 , filteredImageBuffer, resizedColumns, resizedRows
3671 , resizeFilter, cubicCoefficientsBuffer
3672 , yFactor, clEnv, queue, exception);
3673 if (status != MagickTrue)
3674 goto cleanup;
3675 }
3676 else
3677 {
dirk8a5cf512014-07-28 20:16:27 +00003678 length = image->columns*resizedRows;
cristy0c832c62014-03-07 22:21:04 +00003679 tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length*sizeof(CLPixelPacket), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003680 if (clStatus != CL_SUCCESS)
3681 {
cristy0c832c62014-03-07 22:21:04 +00003682 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003683 goto cleanup;
3684 }
3685
dirk8a5cf512014-07-28 20:16:27 +00003686 status = resizeVerticalFilter(imageBuffer, image->columns, image->rows, (image->alpha_trait==BlendPixelTrait)?1:0
3687 , tempImageBuffer, image->columns, resizedRows
cristyf034abb2013-11-24 14:16:14 +00003688 , resizeFilter, cubicCoefficientsBuffer
3689 , yFactor, clEnv, queue, exception);
3690 if (status != MagickTrue)
3691 goto cleanup;
3692
dirk8a5cf512014-07-28 20:16:27 +00003693 status = resizeHorizontalFilter(tempImageBuffer, image->columns, resizedRows, (image->alpha_trait==BlendPixelTrait)?1:0
cristyf034abb2013-11-24 14:16:14 +00003694 , filteredImageBuffer, resizedColumns, resizedRows
3695 , resizeFilter, cubicCoefficientsBuffer
3696 , xFactor, clEnv, queue, exception);
3697 if (status != MagickTrue)
3698 goto cleanup;
3699 }
3700 length = resizedColumns*resizedRows;
3701 if (ALIGNED(filteredPixels,CLPixelPacket))
3702 {
cristy0c832c62014-03-07 22:21:04 +00003703 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 +00003704 }
3705 else
3706 {
cristy0c832c62014-03-07 22:21:04 +00003707 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00003708 }
3709 if (clStatus != CL_SUCCESS)
3710 {
cristya22457d2013-12-07 14:03:06 +00003711 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003712 goto cleanup;
3713 }
dirk832becc2014-08-04 19:44:34 +00003714 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
3715
3716cleanup:
3717 OpenCLLogException(__FUNCTION__,__LINE__,exception);
3718
3719 image_view=DestroyCacheView(image_view);
3720 if (filteredImage_view != NULL)
dirk8a5cf512014-07-28 20:16:27 +00003721 filteredImage_view=DestroyCacheView(filteredImage_view);
cristyf034abb2013-11-24 14:16:14 +00003722
dirk8a5cf512014-07-28 20:16:27 +00003723 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00003724 if (tempImageBuffer!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer);
3725 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
3726 if (cubicCoefficientsBuffer!=NULL) clEnv->library->clReleaseMemObject(cubicCoefficientsBuffer);
cristyf034abb2013-11-24 14:16:14 +00003727 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
dirk8a5cf512014-07-28 20:16:27 +00003728 if (outputReady == MagickFalse && filteredImage != NULL)
3729 filteredImage=DestroyImage(filteredImage);
3730 return(filteredImage);
cristyf034abb2013-11-24 14:16:14 +00003731}
3732
dirk832becc2014-08-04 19:44:34 +00003733const ResizeWeightingFunctionType supportedResizeWeighting[] =
3734{
3735 BoxWeightingFunction,
3736 TriangleWeightingFunction,
3737 HanningWeightingFunction,
3738 HammingWeightingFunction,
3739 BlackmanWeightingFunction,
3740 CubicBCWeightingFunction,
3741 SincWeightingFunction,
3742 SincFastWeightingFunction,
3743 LastWeightingFunction
cristyf034abb2013-11-24 14:16:14 +00003744};
3745
dirk832becc2014-08-04 19:44:34 +00003746static MagickBooleanType gpuSupportedResizeWeighting(
3747 ResizeWeightingFunctionType f)
3748{
3749 unsigned int
3750 i;
3751
3752 for (i = 0; ;i++)
3753 {
3754 if (supportedResizeWeighting[i] == LastWeightingFunction)
3755 break;
3756 if (supportedResizeWeighting[i] == f)
3757 return(MagickTrue);
3758 }
3759 return(MagickFalse);
3760}
3761
3762MagickExport Image *AccelerateResizeImage(const Image *image,
3763 const size_t resizedColumns,const size_t resizedRows,
3764 const ResizeFilter *resizeFilter,ExceptionInfo *exception)
3765{
3766 Image
3767 *filteredImage;
3768
3769 assert(image != NULL);
3770 assert(exception != (ExceptionInfo *) NULL);
3771
3772 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
3773 (checkAccelerateCondition(image, AllChannels) == MagickFalse))
3774 return NULL;
3775
3776 if (gpuSupportedResizeWeighting(GetResizeFilterWeightingType(resizeFilter)) == MagickFalse ||
3777 gpuSupportedResizeWeighting(GetResizeFilterWindowWeightingType(resizeFilter)) == MagickFalse)
3778 return NULL;
3779
3780 filteredImage=ComputeResizeImage(image,resizedColumns,resizedRows,resizeFilter,exception);
3781 return(filteredImage);
cristyf034abb2013-11-24 14:16:14 +00003782}
3783
3784/*
3785%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3786% %
3787% %
3788% %
3789% C o n t r a s t I m a g e w i t h O p e n C L %
3790% %
3791% %
3792% %
3793%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3794%
3795% ContrastImage() enhances the intensity differences between the lighter and
3796% darker elements of the image. Set sharpen to a MagickTrue to increase the
3797% image contrast otherwise the contrast is reduced.
3798%
3799% The format of the ContrastImage method is:
3800%
3801% MagickBooleanType ContrastImage(Image *image,
3802% const MagickBooleanType sharpen)
3803%
3804% A description of each parameter follows:
3805%
3806% o image: the image.
3807%
3808% o sharpen: Increase or decrease image contrast.
3809%
3810*/
3811
dirk832becc2014-08-04 19:44:34 +00003812static MagickBooleanType ComputeContrastImage(Image *image,
3813 const MagickBooleanType sharpen,ExceptionInfo *exception)
3814{
3815 CacheView
3816 *image_view;
3817
3818 cl_command_queue
3819 queue;
3820
3821 cl_context
3822 context;
3823
3824 cl_int
3825 clStatus;
3826
3827 cl_kernel
3828 filterKernel;
3829
3830 cl_mem
3831 imageBuffer;
3832
3833 cl_mem_flags
3834 mem_flags;
3835
3836 MagickBooleanType
3837 outputReady;
3838
3839 MagickCLEnv
3840 clEnv;
3841
3842 MagickSizeType
3843 length;
3844
3845 size_t
3846 global_work_size[2];
3847
3848 unsigned int
3849 i,
3850 uSharpen;
3851
3852 void
3853 *inputPixels;
3854
3855 outputReady = MagickFalse;
3856 clEnv = NULL;
3857 inputPixels = NULL;
3858 context = NULL;
3859 imageBuffer = NULL;
3860 filterKernel = NULL;
3861 queue = NULL;
3862
3863 clEnv = GetDefaultOpenCLEnv();
3864 context = GetOpenCLContext(clEnv);
3865
3866 /* Create and initialize OpenCL buffers. */
3867 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00003868 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00003869 if (inputPixels == (void *) NULL)
cristyd43a46b2010-01-21 02:13:41 +00003870 {
dirk8a5cf512014-07-28 20:16:27 +00003871 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +00003872 goto cleanup;
cristyd43a46b2010-01-21 02:13:41 +00003873 }
cristyf034abb2013-11-24 14:16:14 +00003874
3875 /* If the host pointer is aligned to the size of CLPixelPacket,
dirk8a5cf512014-07-28 20:16:27 +00003876 then use the host buffer directly from the GPU; otherwise,
3877 create a buffer on the GPU and copy the data over */
cristyf034abb2013-11-24 14:16:14 +00003878 if (ALIGNED(inputPixels,CLPixelPacket))
3879 {
3880 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
3881 }
3882 else
3883 {
3884 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
3885 }
3886 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00003887 length = image->columns * image->rows;
3888 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00003889 if (clStatus != CL_SUCCESS)
3890 {
cristy0c832c62014-03-07 22:21:04 +00003891 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00003892 goto cleanup;
3893 }
dirk8a5cf512014-07-28 20:16:27 +00003894
3895 filterKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Contrast");
3896 if (filterKernel == NULL)
cristyf034abb2013-11-24 14:16:14 +00003897 {
cristya22457d2013-12-07 14:03:06 +00003898 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003899 goto cleanup;
3900 }
3901
cristyf034abb2013-11-24 14:16:14 +00003902 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00003903 clStatus=clEnv->library->clSetKernelArg(filterKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
3904
3905 uSharpen = (sharpen == MagickFalse)?0:1;
3906 clStatus|=clEnv->library->clSetKernelArg(filterKernel,i++,sizeof(cl_uint),&uSharpen);
cristyf034abb2013-11-24 14:16:14 +00003907 if (clStatus != CL_SUCCESS)
3908 {
cristy0c832c62014-03-07 22:21:04 +00003909 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003910 goto cleanup;
3911 }
3912
dirk8a5cf512014-07-28 20:16:27 +00003913 global_work_size[0] = image->columns;
3914 global_work_size[1] = image->rows;
3915 /* launch the kernel */
3916 queue = AcquireOpenCLCommandQueue(clEnv);
3917 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, filterKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
3918 if (clStatus != CL_SUCCESS)
cristyf034abb2013-11-24 14:16:14 +00003919 {
dirk8a5cf512014-07-28 20:16:27 +00003920 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
3921 goto cleanup;
cristyf034abb2013-11-24 14:16:14 +00003922 }
dirk8a5cf512014-07-28 20:16:27 +00003923 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00003924
3925 if (ALIGNED(inputPixels,CLPixelPacket))
3926 {
dirk8a5cf512014-07-28 20:16:27 +00003927 length = image->columns * image->rows;
3928 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 +00003929 }
3930 else
3931 {
dirk8a5cf512014-07-28 20:16:27 +00003932 length = image->columns * image->rows;
3933 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00003934 }
3935 if (clStatus != CL_SUCCESS)
3936 {
cristya22457d2013-12-07 14:03:06 +00003937 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00003938 goto cleanup;
3939 }
dirk832becc2014-08-04 19:44:34 +00003940 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
3941
3942cleanup:
3943 OpenCLLogException(__FUNCTION__,__LINE__,exception);
3944
dirk8a5cf512014-07-28 20:16:27 +00003945 image_view=DestroyCacheView(image_view);
cristyf034abb2013-11-24 14:16:14 +00003946
dirk8a5cf512014-07-28 20:16:27 +00003947 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
3948 if (filterKernel!=NULL) RelinquishOpenCLKernel(clEnv, filterKernel);
3949 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
3950 return(outputReady);
dirk832becc2014-08-04 19:44:34 +00003951}
3952
3953MagickExport MagickBooleanType AccelerateContrastImage(Image *image,
3954 const MagickBooleanType sharpen,ExceptionInfo *exception)
3955{
3956 MagickBooleanType
3957 status;
3958
3959 assert(image != NULL);
3960 assert(exception != (ExceptionInfo *) NULL);
3961
3962 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
3963 (checkAccelerateCondition(image, AllChannels) == MagickFalse))
3964 return(MagickFalse);
3965
3966 status = ComputeContrastImage(image,sharpen,exception);
3967 return(status);
cristy3f6d1482010-01-20 21:01:21 +00003968}
cristyf034abb2013-11-24 14:16:14 +00003969
3970/*
3971%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3972% %
3973% %
3974% %
3975% M o d u l a t e I m a g e w i t h O p e n C L %
3976% %
3977% %
3978% %
3979%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3980%
3981% ModulateImage() lets you control the brightness, saturation, and hue
3982% of an image. Modulate represents the brightness, saturation, and hue
3983% as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the
3984% modulation is lightness, saturation, and hue. For HWB, use blackness,
3985% whiteness, and hue. And for HCL, use chrome, luma, and hue.
3986%
3987% The format of the ModulateImage method is:
3988%
3989% MagickBooleanType ModulateImage(Image *image,const char *modulate)
3990%
3991% A description of each parameter follows:
3992%
3993% o image: the image.
3994%
3995% o percent_*: Define the percent change in brightness, saturation, and
3996% hue.
3997%
3998*/
3999
dirk832becc2014-08-04 19:44:34 +00004000MagickBooleanType ComputeModulateImage(Image *image,
4001 double percent_brightness,double percent_hue,double percent_saturation,
4002 ColorspaceType colorspace,ExceptionInfo *exception)
4003{
4004 CacheView
4005 *image_view;
4006
4007 cl_float
4008 bright,
4009 hue,
4010 saturation;
4011
4012 cl_context
4013 context;
4014
4015 cl_command_queue
4016 queue;
4017
4018 cl_int
4019 color,
4020 clStatus;
4021
4022 cl_kernel
4023 modulateKernel;
4024
4025 cl_mem
4026 imageBuffer;
4027
4028 cl_mem_flags
4029 mem_flags;
4030
4031 MagickBooleanType
4032 outputReady;
4033
4034 MagickCLEnv
4035 clEnv;
4036
4037 MagickSizeType
4038 length;
4039
4040 register ssize_t
4041 i;
4042
4043 void
4044 *inputPixels;
4045
4046 inputPixels = NULL;
4047 imageBuffer = NULL;
4048 modulateKernel = NULL;
4049
4050 assert(image != (Image *) NULL);
4051 assert(image->signature == MagickSignature);
4052 if (image->debug != MagickFalse)
4053 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4054
4055 /*
4056 * initialize opencl env
4057 */
4058 clEnv = GetDefaultOpenCLEnv();
4059 context = GetOpenCLContext(clEnv);
4060 queue = AcquireOpenCLCommandQueue(clEnv);
4061
4062 outputReady = MagickFalse;
4063
4064 /* Create and initialize OpenCL buffers.
4065 inputPixels = AcquirePixelCachePixels(image, &length, exception);
4066 assume this will get a writable image
4067 */
4068 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00004069 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristy0c832c62014-03-07 22:21:04 +00004070 if (inputPixels == (void *) NULL)
4071 {
dirk8a5cf512014-07-28 20:16:27 +00004072 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristy0c832c62014-03-07 22:21:04 +00004073 goto cleanup;
4074 }
4075
4076 /* If the host pointer is aligned to the size of CLPixelPacket,
4077 then use the host buffer directly from the GPU; otherwise,
4078 create a buffer on the GPU and copy the data over
4079 */
4080 if (ALIGNED(inputPixels,CLPixelPacket))
4081 {
4082 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
4083 }
4084 else
4085 {
4086 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
4087 }
4088 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00004089 length = image->columns * image->rows;
4090 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristy0c832c62014-03-07 22:21:04 +00004091 if (clStatus != CL_SUCCESS)
4092 {
4093 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
4094 goto cleanup;
4095 }
4096
dirk8a5cf512014-07-28 20:16:27 +00004097 modulateKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Modulate");
4098 if (modulateKernel == NULL)
cristy0c832c62014-03-07 22:21:04 +00004099 {
4100 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
4101 goto cleanup;
4102 }
4103
dirk8a5cf512014-07-28 20:16:27 +00004104 bright=percent_brightness;
4105 hue=percent_hue;
4106 saturation=percent_saturation;
4107 color=colorspace;
4108
cristy0c832c62014-03-07 22:21:04 +00004109 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00004110 clStatus=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
4111 clStatus|=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_float),&bright);
4112 clStatus|=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_float),&hue);
4113 clStatus|=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_float),&saturation);
4114 clStatus|=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_float),&color);
cristy0c832c62014-03-07 22:21:04 +00004115 if (clStatus != CL_SUCCESS)
4116 {
4117 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
4118 printf("no kernel\n");
4119 goto cleanup;
4120 }
4121
4122 {
4123 size_t global_work_size[2];
dirk8a5cf512014-07-28 20:16:27 +00004124 global_work_size[0] = image->columns;
4125 global_work_size[1] = image->rows;
cristy0c832c62014-03-07 22:21:04 +00004126 /* launch the kernel */
dirk8a5cf512014-07-28 20:16:27 +00004127 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, modulateKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristy0c832c62014-03-07 22:21:04 +00004128 if (clStatus != CL_SUCCESS)
4129 {
4130 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
4131 goto cleanup;
4132 }
4133 clEnv->library->clFlush(queue);
4134 }
4135
4136 if (ALIGNED(inputPixels,CLPixelPacket))
4137 {
dirk8a5cf512014-07-28 20:16:27 +00004138 length = image->columns * image->rows;
4139 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 +00004140 }
4141 else
4142 {
dirk8a5cf512014-07-28 20:16:27 +00004143 length = image->columns * image->rows;
4144 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
cristy0c832c62014-03-07 22:21:04 +00004145 }
4146 if (clStatus != CL_SUCCESS)
4147 {
4148 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
4149 goto cleanup;
4150 }
4151
dirk832becc2014-08-04 19:44:34 +00004152 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
4153
4154cleanup:
4155 OpenCLLogException(__FUNCTION__,__LINE__,exception);
4156
dirk8a5cf512014-07-28 20:16:27 +00004157 image_view=DestroyCacheView(image_view);
cristy0c832c62014-03-07 22:21:04 +00004158
dirk8a5cf512014-07-28 20:16:27 +00004159 if (imageBuffer!=NULL)
4160 clEnv->library->clReleaseMemObject(imageBuffer);
4161 if (modulateKernel!=NULL)
4162 RelinquishOpenCLKernel(clEnv, modulateKernel);
cristy0c832c62014-03-07 22:21:04 +00004163 if (queue != NULL)
4164 RelinquishOpenCLCommandQueue(clEnv, queue);
4165
4166 return outputReady;
4167
4168}
4169
dirk832becc2014-08-04 19:44:34 +00004170MagickExport MagickBooleanType AccelerateModulateImage(Image *image,
4171 double percent_brightness,double percent_hue,double percent_saturation,
4172 ColorspaceType colorspace,ExceptionInfo *exception)
4173{
4174 MagickBooleanType
4175 status;
4176
4177 assert(image != NULL);
4178 assert(exception != (ExceptionInfo *) NULL);
4179
4180 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
4181 (checkAccelerateCondition(image, AllChannels) == MagickFalse))
4182 return(MagickFalse);
4183
4184 if ((colorspace != HSLColorspace && colorspace != UndefinedColorspace))
4185 return(MagickFalse);
4186
4187 status = ComputeModulateImage(image,percent_brightness, percent_hue, percent_saturation, colorspace, exception);
4188 return(status);
dirk8a5cf512014-07-28 20:16:27 +00004189}
cristy0c832c62014-03-07 22:21:04 +00004190
4191/*
4192%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4193% %
4194% %
4195% %
4196% N e g a t e I m a g e w i t h O p e n C L %
4197% %
4198% %
4199% %
4200%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4201%
4202%
4203% A description of each parameter follows:
4204%
4205% o image: the image.
4206%
4207% o channel: the channel.
4208%
4209% o grayscale: If MagickTrue, only negate grayscale pixels within the image.
4210%
4211*/
4212
dirk832becc2014-08-04 19:44:34 +00004213MagickBooleanType ComputeNegateImageChannel(Image *image,
4214 const ChannelType channel,const MagickBooleanType magick_unused(grayscale),
4215 ExceptionInfo* exception)
4216{
4217 CacheView
4218 *image_view;
4219
4220 cl_context
4221 context;
4222
4223 cl_command_queue
4224 queue;
4225
4226 cl_int
4227 clStatus;
4228
4229 cl_kernel
4230 negateKernel;
4231
4232 cl_mem
4233 imageBuffer;
4234
4235 cl_mem_flags
4236 mem_flags;
4237
4238 MagickBooleanType
4239 outputReady;
4240
4241 MagickCLEnv
4242 clEnv;
4243
4244 MagickSizeType
4245 length;
4246
4247 register ssize_t
4248 i;
4249
4250 void
4251 *inputPixels;
4252
4253 magick_unreferenced(grayscale);
4254
4255 inputPixels = NULL;
4256 imageBuffer = NULL;
4257 negateKernel = NULL;
4258
4259 assert(image != (Image *) NULL);
4260 assert(image->signature == MagickSignature);
4261 if (image->debug != MagickFalse)
4262 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4263
4264 /*
4265 * initialize opencl env
4266 */
4267 clEnv = GetDefaultOpenCLEnv();
4268 context = GetOpenCLContext(clEnv);
4269 queue = AcquireOpenCLCommandQueue(clEnv);
4270
4271 outputReady = MagickFalse;
4272
4273 /* Create and initialize OpenCL buffers.
4274 inputPixels = AcquirePixelCachePixels(image, &length, exception);
4275 assume this will get a writable image
4276 */
4277 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00004278 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristy0c832c62014-03-07 22:21:04 +00004279 if (inputPixels == (void *) NULL)
4280 {
dirk8a5cf512014-07-28 20:16:27 +00004281 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristy0c832c62014-03-07 22:21:04 +00004282 goto cleanup;
4283 }
4284
4285 /* If the host pointer is aligned to the size of CLPixelPacket,
4286 then use the host buffer directly from the GPU; otherwise,
4287 create a buffer on the GPU and copy the data over
4288 */
4289 if (ALIGNED(inputPixels,CLPixelPacket))
4290 {
4291 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
4292 }
4293 else
4294 {
4295 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
4296 }
4297 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00004298 length = image->columns * image->rows;
4299 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristy0c832c62014-03-07 22:21:04 +00004300 if (clStatus != CL_SUCCESS)
4301 {
4302 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
4303 goto cleanup;
4304 }
4305
dirk8a5cf512014-07-28 20:16:27 +00004306 negateKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Negate");
4307 if (negateKernel == NULL)
cristy0c832c62014-03-07 22:21:04 +00004308 {
4309 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
4310 goto cleanup;
4311 }
4312
4313 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00004314 clStatus=clEnv->library->clSetKernelArg(negateKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
4315 clStatus=clEnv->library->clSetKernelArg(negateKernel,i++,sizeof(ChannelType),(void *)&channel);
cristy0c832c62014-03-07 22:21:04 +00004316 if (clStatus != CL_SUCCESS)
4317 {
4318 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
4319 printf("no kernel\n");
4320 goto cleanup;
4321 }
4322
4323 {
4324 size_t global_work_size[2];
dirk8a5cf512014-07-28 20:16:27 +00004325 global_work_size[0] = image->columns;
4326 global_work_size[1] = image->rows;
cristy0c832c62014-03-07 22:21:04 +00004327 /* launch the kernel */
dirk8a5cf512014-07-28 20:16:27 +00004328 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, negateKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristy0c832c62014-03-07 22:21:04 +00004329 if (clStatus != CL_SUCCESS)
4330 {
4331 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
4332 goto cleanup;
4333 }
4334 clEnv->library->clFlush(queue);
4335 }
4336
4337 if (ALIGNED(inputPixels,CLPixelPacket))
4338 {
dirk8a5cf512014-07-28 20:16:27 +00004339 length = image->columns * image->rows;
4340 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 +00004341 }
4342 else
4343 {
dirk8a5cf512014-07-28 20:16:27 +00004344 length = image->columns * image->rows;
4345 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
cristy0c832c62014-03-07 22:21:04 +00004346 }
4347 if (clStatus != CL_SUCCESS)
4348 {
4349 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
4350 goto cleanup;
4351 }
4352
dirk832becc2014-08-04 19:44:34 +00004353 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
4354
4355cleanup:
4356 OpenCLLogException(__FUNCTION__,__LINE__,exception);
4357
dirk8a5cf512014-07-28 20:16:27 +00004358 image_view=DestroyCacheView(image_view);
cristy0c832c62014-03-07 22:21:04 +00004359
dirk8a5cf512014-07-28 20:16:27 +00004360 if (imageBuffer!=NULL)
4361 clEnv->library->clReleaseMemObject(imageBuffer);
4362 if (negateKernel!=NULL)
4363 RelinquishOpenCLKernel(clEnv, negateKernel);
cristy0c832c62014-03-07 22:21:04 +00004364 if (queue != NULL)
4365 RelinquishOpenCLCommandQueue(clEnv, queue);
4366
dirk8a5cf512014-07-28 20:16:27 +00004367 return(outputReady);
dirk832becc2014-08-04 19:44:34 +00004368}
4369
4370MagickExport MagickBooleanType AccelerateNegateImageChannel(Image *image,
4371 const ChannelType channel,const MagickBooleanType grayscale,
4372 ExceptionInfo* exception)
4373{
4374 MagickBooleanType
4375 status;
4376
4377 assert(image != NULL);
4378 assert(exception != (ExceptionInfo *) NULL);
4379
4380 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
4381 (checkAccelerateCondition(image, channel) == MagickFalse))
4382 return(MagickFalse);
4383
4384 status=ComputeNegateImageChannel(image,channel,grayscale,exception);
4385 return(status);
cristy0c832c62014-03-07 22:21:04 +00004386}
dirk8a5cf512014-07-28 20:16:27 +00004387
cristy0c832c62014-03-07 22:21:04 +00004388/*
4389%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4390% %
4391% %
4392% %
4393% G r a y s c a l e I m a g e w i t h O p e n C L %
4394% %
4395% %
4396% %
4397%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4398%
4399% GrayscaleImage() converts the colors in the reference image to gray.
4400%
4401% The format of the GrayscaleImageChannel method is:
4402%
4403% MagickBooleanType GrayscaleImage(Image *image,
4404% const PixelIntensityMethod method)
4405%
4406% A description of each parameter follows:
4407%
4408% o image: the image.
4409%
4410% o channel: the channel.
4411%
4412*/
4413
dirk832becc2014-08-04 19:44:34 +00004414MagickBooleanType ComputeGrayscaleImage(Image *image,
4415 const PixelIntensityMethod method,ExceptionInfo *exception)
4416{
4417 CacheView
4418 *image_view;
4419
4420 cl_command_queue
4421 queue;
4422
4423 cl_context
4424 context;
4425
4426 cl_int
4427 clStatus,
4428 intensityMethod;
4429
4430 cl_int
4431 colorspace;
4432
4433 cl_kernel
4434 grayscaleKernel;
4435
4436 cl_mem
4437 imageBuffer;
4438
4439 cl_mem_flags
4440 mem_flags;
4441
4442 MagickBooleanType
4443 outputReady;
4444
4445 MagickCLEnv
4446 clEnv;
4447
4448 MagickSizeType
4449 length;
4450
4451 register ssize_t
4452 i;
4453
4454 void
4455 *inputPixels;
4456
4457 inputPixels = NULL;
4458 imageBuffer = NULL;
4459 grayscaleKernel = NULL;
4460
4461 assert(image != (Image *) NULL);
4462 assert(image->signature == MagickSignature);
4463 if (image->debug != MagickFalse)
4464 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4465
4466 /*
4467 * initialize opencl env
4468 */
4469 clEnv = GetDefaultOpenCLEnv();
4470 context = GetOpenCLContext(clEnv);
4471 queue = AcquireOpenCLCommandQueue(clEnv);
4472
4473 outputReady = MagickFalse;
4474
4475 /* Create and initialize OpenCL buffers.
4476 inputPixels = AcquirePixelCachePixels(image, &length, exception);
4477 assume this will get a writable image
4478 */
4479 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00004480 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
4481 if (inputPixels == (void *) NULL)
4482 {
4483 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
4484 goto cleanup;
4485 }
cristy0c832c62014-03-07 22:21:04 +00004486
dirk8a5cf512014-07-28 20:16:27 +00004487 /* If the host pointer is aligned to the size of CLPixelPacket,
4488 then use the host buffer directly from the GPU; otherwise,
4489 create a buffer on the GPU and copy the data over
4490 */
4491 if (ALIGNED(inputPixels,CLPixelPacket))
4492 {
4493 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
4494 }
4495 else
4496 {
4497 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
4498 }
4499 /* create a CL buffer from image pixel buffer */
4500 length = image->columns * image->rows;
4501 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
4502 if (clStatus != CL_SUCCESS)
4503 {
4504 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
4505 goto cleanup;
4506 }
cristy0c832c62014-03-07 22:21:04 +00004507
dirk8a5cf512014-07-28 20:16:27 +00004508 intensityMethod = method;
4509 colorspace = image->colorspace;
cristy0c832c62014-03-07 22:21:04 +00004510
dirk8a5cf512014-07-28 20:16:27 +00004511 grayscaleKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Grayscale");
4512 if (grayscaleKernel == NULL)
4513 {
4514 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
4515 goto cleanup;
4516 }
cristy0c832c62014-03-07 22:21:04 +00004517
dirk8a5cf512014-07-28 20:16:27 +00004518 i = 0;
4519 clStatus=clEnv->library->clSetKernelArg(grayscaleKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
4520 clStatus|=clEnv->library->clSetKernelArg(grayscaleKernel,i++,sizeof(cl_int),&intensityMethod);
4521 clStatus|=clEnv->library->clSetKernelArg(grayscaleKernel,i++,sizeof(cl_int),&colorspace);
4522 if (clStatus != CL_SUCCESS)
4523 {
4524 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
4525 printf("no kernel\n");
4526 goto cleanup;
4527 }
cristy0c832c62014-03-07 22:21:04 +00004528
dirk8a5cf512014-07-28 20:16:27 +00004529 {
4530 size_t global_work_size[2];
4531 global_work_size[0] = image->columns;
4532 global_work_size[1] = image->rows;
4533 /* launch the kernel */
4534 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, grayscaleKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
4535 if (clStatus != CL_SUCCESS)
4536 {
4537 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
4538 goto cleanup;
4539 }
4540 clEnv->library->clFlush(queue);
4541 }
cristy0c832c62014-03-07 22:21:04 +00004542
dirk8a5cf512014-07-28 20:16:27 +00004543 if (ALIGNED(inputPixels,CLPixelPacket))
4544 {
4545 length = image->columns * image->rows;
4546 clEnv->library->clEnqueueMapBuffer(queue, imageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus);
4547 }
4548 else
4549 {
4550 length = image->columns * image->rows;
4551 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
4552 }
4553 if (clStatus != CL_SUCCESS)
4554 {
4555 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
4556 goto cleanup;
4557 }
cristy0c832c62014-03-07 22:21:04 +00004558
dirk832becc2014-08-04 19:44:34 +00004559 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
4560
4561cleanup:
4562 OpenCLLogException(__FUNCTION__,__LINE__,exception);
4563
dirk8a5cf512014-07-28 20:16:27 +00004564 image_view=DestroyCacheView(image_view);
4565
4566 if (imageBuffer!=NULL)
4567 clEnv->library->clReleaseMemObject(imageBuffer);
4568 if (grayscaleKernel!=NULL)
4569 RelinquishOpenCLKernel(clEnv, grayscaleKernel);
4570 if (queue != NULL)
4571 RelinquishOpenCLCommandQueue(clEnv, queue);
4572
4573 return( outputReady);
dirk832becc2014-08-04 19:44:34 +00004574}
4575
4576MagickExport MagickBooleanType AccelerateGrayscaleImage(Image* image,
4577 const PixelIntensityMethod method,ExceptionInfo *exception)
4578{
4579 MagickBooleanType
4580 status;
4581
4582 assert(image != NULL);
4583 assert(exception != (ExceptionInfo *) NULL);
4584
4585 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
4586 (checkAccelerateCondition(image, AllChannels) == MagickFalse))
4587 return(MagickFalse);
4588
4589 if (method == Rec601LuminancePixelIntensityMethod || method == Rec709LuminancePixelIntensityMethod)
4590 return(MagickFalse);
4591
4592 if (image->colorspace != sRGBColorspace)
4593 return(MagickFalse);
4594
4595 status=ComputeGrayscaleImage(image,method,exception);
4596 return(status);
cristy0c832c62014-03-07 22:21:04 +00004597}
4598
dirk8a5cf512014-07-28 20:16:27 +00004599/*
4600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4601% %
4602% %
4603% %
4604% E q u a l i z e I m a g e w i t h O p e n C L %
4605% %
4606% %
4607% %
4608%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4609%
4610% EqualizeImage() applies a histogram equalization to the image.
4611%
4612% The format of the EqualizeImage method is:
4613%
4614% MagickBooleanType EqualizeImage(Image *image)
4615% MagickBooleanType EqualizeImageChannel(Image *image,
4616% const ChannelType channel)
4617%
4618% A description of each parameter follows:
4619%
4620% o image: the image.
4621%
4622% o channel: the channel.
4623%
4624*/
cristy0c832c62014-03-07 22:21:04 +00004625
dirk832becc2014-08-04 19:44:34 +00004626static MagickBooleanType LaunchHistogramKernel(MagickCLEnv clEnv,
4627 cl_command_queue queue,cl_mem imageBuffer,cl_mem histogramBuffer,
4628 Image *image,const ChannelType channel,ExceptionInfo *exception)
4629{
4630 MagickBooleanType
4631 outputReady;
4632
4633 cl_int
4634 clStatus,
4635 colorspace,
4636 method;
4637
4638 cl_kernel
4639 histogramKernel;
4640
4641 register ssize_t
4642 i;
4643
4644 size_t
4645 global_work_size[2];
4646
cristy0c832c62014-03-07 22:21:04 +00004647 histogramKernel = NULL;
4648
4649 outputReady = MagickFalse;
dirk8a5cf512014-07-28 20:16:27 +00004650 method = image->intensity;
4651 colorspace = image->colorspace;
cristy0c832c62014-03-07 22:21:04 +00004652
4653 /* get the OpenCL kernel */
4654 histogramKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Histogram");
4655 if (histogramKernel == NULL)
4656 {
4657 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
4658 goto cleanup;
4659 }
4660
4661 /* set the kernel arguments */
4662 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00004663 clStatus=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00004664 clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(ChannelType),&channel);
4665 clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_int),&method);
4666 clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_int),&colorspace);
4667 clStatus|=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_mem),(void *)&histogramBuffer);
4668 if (clStatus != CL_SUCCESS)
4669 {
4670 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
4671 goto cleanup;
4672 }
4673
4674 /* launch the kernel */
dirk8a5cf512014-07-28 20:16:27 +00004675 global_work_size[0] = image->columns;
4676 global_work_size[1] = image->rows;
cristy0c832c62014-03-07 22:21:04 +00004677
4678 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, histogramKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
4679
4680 if (clStatus != CL_SUCCESS)
4681 {
4682 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
4683 goto cleanup;
4684 }
4685 clEnv->library->clFlush(queue);
4686
4687 outputReady = MagickTrue;
4688
4689cleanup:
4690 OpenCLLogException(__FUNCTION__,__LINE__,exception);
4691
4692 if (histogramKernel!=NULL)
4693 RelinquishOpenCLKernel(clEnv, histogramKernel);
4694
dirk8a5cf512014-07-28 20:16:27 +00004695 return(outputReady);
cristy0c832c62014-03-07 22:21:04 +00004696}
4697
dirk832becc2014-08-04 19:44:34 +00004698MagickExport MagickBooleanType ComputeEqualizeImage(Image *image,
4699 const ChannelType channel,ExceptionInfo *exception)
4700{
4701#define EqualizeImageTag "Equalize/Image"
4702
4703 CacheView
4704 *image_view;
4705
4706 cl_command_queue
4707 queue;
4708
4709 cl_context
4710 context;
4711
4712 cl_int
4713 clStatus;
4714
4715 cl_mem_flags
4716 mem_flags;
4717
4718 cl_mem
4719 equalizeMapBuffer,
4720 histogramBuffer,
4721 imageBuffer;
4722
4723 cl_kernel
4724 equalizeKernel,
4725 histogramKernel;
4726
4727 cl_uint4
4728 *histogram;
4729
4730 FloatPixelPacket
4731 white,
4732 black,
4733 intensity,
4734 *map;
4735
4736 MagickBooleanType
4737 outputReady,
4738 status;
4739
4740 MagickCLEnv
4741 clEnv;
4742
4743 MagickSizeType
4744 length;
4745
4746 PixelPacket
4747 *equalize_map;
4748
4749 register ssize_t
4750 i;
4751
4752 size_t
4753 global_work_size[2];
4754
4755 void
4756 *hostPtr,
4757 *inputPixels;
4758
4759 map=NULL;
4760 histogram=NULL;
4761 equalize_map=NULL;
4762 inputPixels = NULL;
4763 imageBuffer = NULL;
4764 histogramBuffer = NULL;
4765 equalizeMapBuffer = NULL;
4766 histogramKernel = NULL;
4767 equalizeKernel = NULL;
4768 context = NULL;
4769 queue = NULL;
4770 outputReady = MagickFalse;
4771
4772 assert(image != (Image *) NULL);
4773 assert(image->signature == MagickSignature);
4774 if (image->debug != MagickFalse)
4775 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4776
4777 /*
4778 * initialize opencl env
4779 */
4780 clEnv = GetDefaultOpenCLEnv();
4781 context = GetOpenCLContext(clEnv);
4782 queue = AcquireOpenCLCommandQueue(clEnv);
4783
4784 /*
4785 Allocate and initialize histogram arrays.
4786 */
4787 histogram=(cl_uint4 *) AcquireQuantumMemory(MaxMap+1UL, sizeof(*histogram));
4788 if (histogram == (cl_uint4 *) NULL)
4789 ThrowBinaryException(ResourceLimitWarning,"MemoryAllocationFailed", image->filename);
4790
4791 /* reset histogram */
4792 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
4793
4794 /* Create and initialize OpenCL buffers. */
4795 /* inputPixels = AcquirePixelCachePixels(image, &length, exception); */
4796 /* assume this will get a writable image */
4797 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00004798 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00004799
4800 if (inputPixels == (void *) NULL)
4801 {
dirk8a5cf512014-07-28 20:16:27 +00004802 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +00004803 goto cleanup;
4804 }
4805 /* If the host pointer is aligned to the size of CLPixelPacket,
4806 then use the host buffer directly from the GPU; otherwise,
4807 create a buffer on the GPU and copy the data over */
4808 if (ALIGNED(inputPixels,CLPixelPacket))
4809 {
4810 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
4811 }
4812 else
4813 {
4814 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
4815 }
4816 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00004817 length = image->columns * image->rows;
4818 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00004819 if (clStatus != CL_SUCCESS)
4820 {
cristy0c832c62014-03-07 22:21:04 +00004821 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00004822 goto cleanup;
4823 }
cristy0c832c62014-03-07 22:21:04 +00004824
cristyf034abb2013-11-24 14:16:14 +00004825 /* If the host pointer is aligned to the size of cl_uint,
4826 then use the host buffer directly from the GPU; otherwise,
4827 create a buffer on the GPU and copy the data over */
4828 if (ALIGNED(histogram,cl_uint4))
4829 {
4830 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
4831 hostPtr = histogram;
4832 }
4833 else
4834 {
4835 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
4836 hostPtr = histogram;
4837 }
4838 /* create a CL buffer for histogram */
4839 length = (MaxMap+1);
cristy0c832c62014-03-07 22:21:04 +00004840 histogramBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(cl_uint4), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00004841 if (clStatus != CL_SUCCESS)
4842 {
cristy0c832c62014-03-07 22:21:04 +00004843 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00004844 goto cleanup;
4845 }
4846
dirk8a5cf512014-07-28 20:16:27 +00004847 status = LaunchHistogramKernel(clEnv, queue, imageBuffer, histogramBuffer, image, channel, exception);
cristy0c832c62014-03-07 22:21:04 +00004848 if (status == MagickFalse)
cristyf034abb2013-11-24 14:16:14 +00004849 goto cleanup;
cristyf034abb2013-11-24 14:16:14 +00004850
4851 /* read from the kenel output */
4852 if (ALIGNED(histogram,cl_uint4))
4853 {
4854 length = (MaxMap+1);
cristy0c832c62014-03-07 22:21:04 +00004855 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 +00004856 }
4857 else
4858 {
4859 length = (MaxMap+1);
cristy0c832c62014-03-07 22:21:04 +00004860 clStatus = clEnv->library->clEnqueueReadBuffer(queue, histogramBuffer, CL_TRUE, 0, length * sizeof(cl_uint4), histogram, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00004861 }
4862 if (clStatus != CL_SUCCESS)
4863 {
cristya22457d2013-12-07 14:03:06 +00004864 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00004865 goto cleanup;
4866 }
4867
4868 /* unmap, don't block gpu to use this buffer again. */
4869 if (ALIGNED(histogram,cl_uint4))
4870 {
cristy0c832c62014-03-07 22:21:04 +00004871 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, histogramBuffer, histogram, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00004872 if (clStatus != CL_SUCCESS)
4873 {
cristy0c832c62014-03-07 22:21:04 +00004874 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00004875 goto cleanup;
4876 }
4877 }
4878
cristy0c832c62014-03-07 22:21:04 +00004879 /* recreate input buffer later, in case image updated */
4880#ifdef RECREATEBUFFER
dirk8a5cf512014-07-28 20:16:27 +00004881 if (imageBuffer!=NULL)
4882 clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00004883#endif
4884
4885 /* CPU stuff */
cristyf034abb2013-11-24 14:16:14 +00004886 equalize_map=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL, sizeof(*equalize_map));
4887 if (equalize_map == (PixelPacket *) NULL)
cristy0c832c62014-03-07 22:21:04 +00004888 ThrowBinaryException(ResourceLimitWarning,"MemoryAllocationFailed", image->filename);
cristyf034abb2013-11-24 14:16:14 +00004889
4890 map=(FloatPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*map));
4891 if (map == (FloatPixelPacket *) NULL)
cristy0c832c62014-03-07 22:21:04 +00004892 ThrowBinaryException(ResourceLimitWarning,"MemoryAllocationFailed", image->filename);
cristyf034abb2013-11-24 14:16:14 +00004893
4894 /*
4895 Integrate the histogram to get the equalization map.
4896 */
4897 (void) ResetMagickMemory(&intensity,0,sizeof(intensity));
4898 for (i=0; i <= (ssize_t) MaxMap; i++)
4899 {
4900 if ((channel & SyncChannels) != 0)
cristy0c832c62014-03-07 22:21:04 +00004901 {
4902 intensity.red+=histogram[i].s[2];
4903 map[i]=intensity;
4904 continue;
4905 }
cristyf034abb2013-11-24 14:16:14 +00004906 if ((channel & RedChannel) != 0)
4907 intensity.red+=histogram[i].s[2];
4908 if ((channel & GreenChannel) != 0)
4909 intensity.green+=histogram[i].s[1];
4910 if ((channel & BlueChannel) != 0)
4911 intensity.blue+=histogram[i].s[0];
4912 if ((channel & OpacityChannel) != 0)
dirk8a5cf512014-07-28 20:16:27 +00004913 intensity.alpha+=histogram[i].s[3];
cristy0c832c62014-03-07 22:21:04 +00004914 /*
cristyf034abb2013-11-24 14:16:14 +00004915 if (((channel & IndexChannel) != 0) &&
4916 (image->colorspace == CMYKColorspace))
4917 {
cristy0c832c62014-03-07 22:21:04 +00004918 intensity.index+=histogram[i].index;
cristyf034abb2013-11-24 14:16:14 +00004919 }
cristy0c832c62014-03-07 22:21:04 +00004920 */
cristyf034abb2013-11-24 14:16:14 +00004921 map[i]=intensity;
4922 }
4923 black=map[0];
4924 white=map[(int) MaxMap];
4925 (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*sizeof(*equalize_map));
4926 for (i=0; i <= (ssize_t) MaxMap; i++)
4927 {
4928 if ((channel & SyncChannels) != 0)
cristy0c832c62014-03-07 22:21:04 +00004929 {
4930 if (white.red != black.red)
4931 equalize_map[i].red=ScaleMapToQuantum((MagickRealType) ((MaxMap*
4932 (map[i].red-black.red))/(white.red-black.red)));
4933 continue;
4934 }
cristyf034abb2013-11-24 14:16:14 +00004935 if (((channel & RedChannel) != 0) && (white.red != black.red))
4936 equalize_map[i].red=ScaleMapToQuantum((MagickRealType) ((MaxMap*
cristy0c832c62014-03-07 22:21:04 +00004937 (map[i].red-black.red))/(white.red-black.red)));
cristyf034abb2013-11-24 14:16:14 +00004938 if (((channel & GreenChannel) != 0) && (white.green != black.green))
4939 equalize_map[i].green=ScaleMapToQuantum((MagickRealType) ((MaxMap*
cristy0c832c62014-03-07 22:21:04 +00004940 (map[i].green-black.green))/(white.green-black.green)));
cristyf034abb2013-11-24 14:16:14 +00004941 if (((channel & BlueChannel) != 0) && (white.blue != black.blue))
4942 equalize_map[i].blue=ScaleMapToQuantum((MagickRealType) ((MaxMap*
cristy0c832c62014-03-07 22:21:04 +00004943 (map[i].blue-black.blue))/(white.blue-black.blue)));
dirk8a5cf512014-07-28 20:16:27 +00004944 if (((channel & OpacityChannel) != 0) && (white.alpha != black.alpha))
4945 equalize_map[i].alpha=ScaleMapToQuantum((MagickRealType) ((MaxMap*
4946 (map[i].alpha-black.alpha))/(white.alpha-black.alpha)));
cristyf034abb2013-11-24 14:16:14 +00004947 /*
4948 if ((((channel & IndexChannel) != 0) &&
cristy0c832c62014-03-07 22:21:04 +00004949 (image->colorspace == CMYKColorspace)) &&
cristyf034abb2013-11-24 14:16:14 +00004950 (white.index != black.index))
4951 equalize_map[i].index=ScaleMapToQuantum((MagickRealType) ((MaxMap*
cristy0c832c62014-03-07 22:21:04 +00004952 (map[i].index-black.index))/(white.index-black.index)));
cristyf034abb2013-11-24 14:16:14 +00004953 */
4954 }
4955
cristyf034abb2013-11-24 14:16:14 +00004956 if (image->storage_class == PseudoClass)
4957 {
cristy0c832c62014-03-07 22:21:04 +00004958 /*
4959 Equalize colormap.
4960 */
4961 for (i=0; i < (ssize_t) image->colors; i++)
4962 {
4963 if ((channel & SyncChannels) != 0)
cristyf034abb2013-11-24 14:16:14 +00004964 {
cristy0c832c62014-03-07 22:21:04 +00004965 if (white.red != black.red)
4966 {
cristyf034abb2013-11-24 14:16:14 +00004967 image->colormap[i].red=equalize_map[
4968 ScaleQuantumToMap(image->colormap[i].red)].red;
cristyf034abb2013-11-24 14:16:14 +00004969 image->colormap[i].green=equalize_map[
cristy0c832c62014-03-07 22:21:04 +00004970 ScaleQuantumToMap(image->colormap[i].green)].red;
cristyf034abb2013-11-24 14:16:14 +00004971 image->colormap[i].blue=equalize_map[
cristy0c832c62014-03-07 22:21:04 +00004972 ScaleQuantumToMap(image->colormap[i].blue)].red;
dirk8a5cf512014-07-28 20:16:27 +00004973 image->colormap[i].alpha=equalize_map[
4974 ScaleQuantumToMap(image->colormap[i].alpha)].red;
cristy0c832c62014-03-07 22:21:04 +00004975 }
4976 continue;
cristyf034abb2013-11-24 14:16:14 +00004977 }
cristy0c832c62014-03-07 22:21:04 +00004978 if (((channel & RedChannel) != 0) && (white.red != black.red))
4979 image->colormap[i].red=equalize_map[
4980 ScaleQuantumToMap(image->colormap[i].red)].red;
4981 if (((channel & GreenChannel) != 0) && (white.green != black.green))
4982 image->colormap[i].green=equalize_map[
4983 ScaleQuantumToMap(image->colormap[i].green)].green;
4984 if (((channel & BlueChannel) != 0) && (white.blue != black.blue))
4985 image->colormap[i].blue=equalize_map[
4986 ScaleQuantumToMap(image->colormap[i].blue)].blue;
4987 if (((channel & OpacityChannel) != 0) &&
dirk8a5cf512014-07-28 20:16:27 +00004988 (white.alpha != black.alpha))
4989 image->colormap[i].alpha=equalize_map[
4990 ScaleQuantumToMap(image->colormap[i].alpha)].alpha;
cristy0c832c62014-03-07 22:21:04 +00004991 }
cristyf034abb2013-11-24 14:16:14 +00004992 }
4993
4994 /*
4995 Equalize image.
4996 */
4997
4998 /* GPU can work on this again, image and equalize map as input
4999 image: uchar4 (CLPixelPacket)
5000 equalize_map: uchar4 (PixelPacket)
5001 black, white: float4 (FloatPixelPacket) */
5002
cristy0c832c62014-03-07 22:21:04 +00005003#ifdef RECREATEBUFFER
cristyf034abb2013-11-24 14:16:14 +00005004 /* If the host pointer is aligned to the size of CLPixelPacket,
5005 then use the host buffer directly from the GPU; otherwise,
5006 create a buffer on the GPU and copy the data over */
5007 if (ALIGNED(inputPixels,CLPixelPacket))
5008 {
5009 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
5010 }
5011 else
5012 {
5013 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
5014 }
5015 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00005016 length = image->columns * image->rows;
5017 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00005018 if (clStatus != CL_SUCCESS)
5019 {
cristy0c832c62014-03-07 22:21:04 +00005020 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00005021 goto cleanup;
5022 }
cristy0c832c62014-03-07 22:21:04 +00005023#endif
cristyf034abb2013-11-24 14:16:14 +00005024
5025 /* Create and initialize OpenCL buffers. */
5026 if (ALIGNED(equalize_map, PixelPacket))
5027 {
5028 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
5029 hostPtr = equalize_map;
5030 }
5031 else
5032 {
5033 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
5034 hostPtr = equalize_map;
5035 }
5036 /* create a CL buffer for eqaulize_map */
5037 length = (MaxMap+1);
cristy0c832c62014-03-07 22:21:04 +00005038 equalizeMapBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(PixelPacket), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00005039 if (clStatus != CL_SUCCESS)
5040 {
cristy0c832c62014-03-07 22:21:04 +00005041 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00005042 goto cleanup;
5043 }
5044
5045 /* get the OpenCL kernel */
5046 equalizeKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Equalize");
5047 if (equalizeKernel == NULL)
5048 {
cristya22457d2013-12-07 14:03:06 +00005049 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005050 goto cleanup;
5051 }
5052
5053 /* set the kernel arguments */
5054 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00005055 clStatus=clEnv->library->clSetKernelArg(equalizeKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00005056 clStatus|=clEnv->library->clSetKernelArg(equalizeKernel,i++,sizeof(ChannelType),&channel);
5057 clStatus|=clEnv->library->clSetKernelArg(equalizeKernel,i++,sizeof(cl_mem),(void *)&equalizeMapBuffer);
5058 clStatus|=clEnv->library->clSetKernelArg(equalizeKernel,i++,sizeof(FloatPixelPacket),&white);
5059 clStatus|=clEnv->library->clSetKernelArg(equalizeKernel,i++,sizeof(FloatPixelPacket),&black);
cristyf034abb2013-11-24 14:16:14 +00005060 if (clStatus != CL_SUCCESS)
5061 {
cristy0c832c62014-03-07 22:21:04 +00005062 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005063 goto cleanup;
5064 }
5065
5066 /* launch the kernel */
dirk8a5cf512014-07-28 20:16:27 +00005067 global_work_size[0] = image->columns;
5068 global_work_size[1] = image->rows;
cristyf034abb2013-11-24 14:16:14 +00005069
cristy0c832c62014-03-07 22:21:04 +00005070 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, equalizeKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00005071
5072 if (clStatus != CL_SUCCESS)
5073 {
cristy0c832c62014-03-07 22:21:04 +00005074 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005075 goto cleanup;
5076 }
cristy0c832c62014-03-07 22:21:04 +00005077 clEnv->library->clFlush(queue);
cristyf034abb2013-11-24 14:16:14 +00005078
5079 /* read the data back */
5080 if (ALIGNED(inputPixels,CLPixelPacket))
5081 {
dirk8a5cf512014-07-28 20:16:27 +00005082 length = image->columns * image->rows;
5083 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 +00005084 }
5085 else
5086 {
dirk8a5cf512014-07-28 20:16:27 +00005087 length = image->columns * image->rows;
5088 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00005089 }
5090 if (clStatus != CL_SUCCESS)
5091 {
cristya22457d2013-12-07 14:03:06 +00005092 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005093 goto cleanup;
5094 }
5095
dirk832becc2014-08-04 19:44:34 +00005096 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
5097
5098cleanup:
5099 OpenCLLogException(__FUNCTION__,__LINE__,exception);
5100
dirk8a5cf512014-07-28 20:16:27 +00005101 image_view=DestroyCacheView(image_view);
cristyf034abb2013-11-24 14:16:14 +00005102
dirk8a5cf512014-07-28 20:16:27 +00005103 if (imageBuffer!=NULL)
5104 clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00005105
5106 if (map!=NULL)
5107 map=(FloatPixelPacket *) RelinquishMagickMemory(map);
5108
5109 if (equalizeMapBuffer!=NULL)
5110 clEnv->library->clReleaseMemObject(equalizeMapBuffer);
5111 if (equalize_map!=NULL)
5112 equalize_map=(PixelPacket *) RelinquishMagickMemory(equalize_map);
5113
cristyf034abb2013-11-24 14:16:14 +00005114 if (histogramBuffer!=NULL)
cristy0c832c62014-03-07 22:21:04 +00005115 clEnv->library->clReleaseMemObject(histogramBuffer);
5116 if (histogram!=NULL)
5117 histogram=(cl_uint4 *) RelinquishMagickMemory(histogram);
5118
cristyf034abb2013-11-24 14:16:14 +00005119 if (histogramKernel!=NULL)
5120 RelinquishOpenCLKernel(clEnv, histogramKernel);
cristy0c832c62014-03-07 22:21:04 +00005121 if (equalizeKernel!=NULL)
5122 RelinquishOpenCLKernel(clEnv, equalizeKernel);
5123
cristyf034abb2013-11-24 14:16:14 +00005124 if (queue != NULL)
5125 RelinquishOpenCLCommandQueue(clEnv, queue);
5126
dirk8a5cf512014-07-28 20:16:27 +00005127 return(outputReady);
dirk832becc2014-08-04 19:44:34 +00005128}
5129
5130MagickExport MagickBooleanType AccelerateEqualizeImage(Image *image,
5131 const ChannelType channel,ExceptionInfo *exception)
5132{
5133 MagickBooleanType
5134 status;
5135
5136 assert(image != NULL);
5137 assert(exception != (ExceptionInfo *) NULL);
5138
5139 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
5140 (checkAccelerateCondition(image, channel) == MagickFalse) ||
5141 (checkHistogramCondition(image, channel) == MagickFalse))
5142 return(MagickFalse);
5143
5144 status=ComputeEqualizeImage(image,channel,exception);
5145 return(status);
cristyf034abb2013-11-24 14:16:14 +00005146}
5147
5148/*
5149%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5150% %
5151% %
5152% %
dirk8a5cf512014-07-28 20:16:27 +00005153% 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 +00005154% %
5155% %
5156% %
5157%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5158%
dirk8a5cf512014-07-28 20:16:27 +00005159% ContrastStretchImage() is a simple image enhancement technique that attempts
5160% to improve the contrast in an image by `stretching' the range of intensity
5161% values it contains to span a desired range of values. It differs from the
5162% more sophisticated histogram equalization in that it can only apply a
5163% linear scaling function to the image pixel values. As a result the
5164% `enhancement' is less harsh.
cristyf034abb2013-11-24 14:16:14 +00005165%
dirk8a5cf512014-07-28 20:16:27 +00005166% The format of the ContrastStretchImage method is:
cristyf034abb2013-11-24 14:16:14 +00005167%
dirk8a5cf512014-07-28 20:16:27 +00005168% MagickBooleanType ContrastStretchImage(Image *image,
5169% const char *levels)
5170% MagickBooleanType ContrastStretchImageChannel(Image *image,
5171% const size_t channel,const double black_point,
5172% const double white_point)
cristyf034abb2013-11-24 14:16:14 +00005173%
5174% A description of each parameter follows:
5175%
5176% o image: the image.
5177%
5178% o channel: the channel.
5179%
dirk8a5cf512014-07-28 20:16:27 +00005180% o black_point: the black point.
5181%
5182% o white_point: the white point.
5183%
5184% o levels: Specify the levels where the black and white points have the
5185% range of 0 to number-of-pixels (e.g. 1%, 10x90%, etc.).
5186%
cristyf034abb2013-11-24 14:16:14 +00005187*/
5188
dirk832becc2014-08-04 19:44:34 +00005189MagickExport MagickBooleanType ComputeContrastStretchImageChannel(Image *image,
5190 const ChannelType channel,const double black_point,const double white_point,
5191 ExceptionInfo *exception)
5192{
5193#define ContrastStretchImageTag "ContrastStretch/Image"
5194#define MaxRange(color) ((MagickRealType) ScaleQuantumToMap((Quantum) (color)))
5195
5196 CacheView
5197 *image_view;
5198
5199 cl_command_queue
5200 queue;
5201
5202 cl_context
5203 context;
5204
5205 cl_int
5206 clStatus;
5207
5208 cl_mem_flags
5209 mem_flags;
5210
5211 cl_mem
5212 histogramBuffer,
5213 imageBuffer,
5214 stretchMapBuffer;
5215
5216 cl_kernel
5217 histogramKernel,
5218 stretchKernel;
5219
5220 cl_uint4
5221 *histogram;
5222
5223 double
5224 intensity;
5225
5226 FloatPixelPacket
5227 black,
5228 white;
5229
5230 MagickBooleanType
5231 outputReady,
5232 status;
5233
5234 MagickCLEnv
5235 clEnv;
5236
5237 MagickSizeType
5238 length;
5239
5240 PixelPacket
5241 *stretch_map;
5242
5243 register ssize_t
5244 i;
5245
5246 size_t
5247 global_work_size[2];
5248
5249 void
5250 *hostPtr,
5251 *inputPixels;
5252
5253 histogram=NULL;
5254 stretch_map=NULL;
5255 inputPixels = NULL;
5256 imageBuffer = NULL;
5257 histogramBuffer = NULL;
5258 stretchMapBuffer = NULL;
5259 histogramKernel = NULL;
5260 stretchKernel = NULL;
5261 context = NULL;
5262 queue = NULL;
5263 outputReady = MagickFalse;
5264
5265
5266 assert(image != (Image *) NULL);
5267 assert(image->signature == MagickSignature);
5268 if (image->debug != MagickFalse)
5269 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5270
5271 //exception=(&image->exception);
5272
5273 /*
5274 * initialize opencl env
5275 */
5276 clEnv = GetDefaultOpenCLEnv();
5277 context = GetOpenCLContext(clEnv);
5278 queue = AcquireOpenCLCommandQueue(clEnv);
5279
5280 /*
5281 Allocate and initialize histogram arrays.
5282 */
5283 histogram=(cl_uint4 *) AcquireQuantumMemory(MaxMap+1UL, sizeof(*histogram));
5284
5285 if (histogram == (cl_uint4 *) NULL)
5286 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", image->filename);
5287
5288 /* reset histogram */
5289 (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
5290
5291 /*
5292 if (IsGrayImage(image,exception) != MagickFalse)
5293 (void) SetImageColorspace(image,GRAYColorspace);
5294 */
5295
5296 status=MagickTrue;
5297
5298
5299 /*
5300 Form histogram.
5301 */
5302 /* Create and initialize OpenCL buffers. */
5303 /* inputPixels = AcquirePixelCachePixels(image, &length, exception); */
5304 /* assume this will get a writable image */
5305 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00005306 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristy0c832c62014-03-07 22:21:04 +00005307
5308 if (inputPixels == (void *) NULL)
5309 {
dirk8a5cf512014-07-28 20:16:27 +00005310 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristy0c832c62014-03-07 22:21:04 +00005311 goto cleanup;
5312 }
5313 /* If the host pointer is aligned to the size of CLPixelPacket,
5314 then use the host buffer directly from the GPU; otherwise,
5315 create a buffer on the GPU and copy the data over */
5316 if (ALIGNED(inputPixels,CLPixelPacket))
5317 {
5318 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
5319 }
5320 else
5321 {
5322 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
5323 }
5324 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00005325 length = image->columns * image->rows;
5326 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristy0c832c62014-03-07 22:21:04 +00005327 if (clStatus != CL_SUCCESS)
5328 {
5329 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
5330 goto cleanup;
5331 }
5332
5333 /* If the host pointer is aligned to the size of cl_uint,
5334 then use the host buffer directly from the GPU; otherwise,
5335 create a buffer on the GPU and copy the data over */
5336 if (ALIGNED(histogram,cl_uint4))
5337 {
5338 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
5339 hostPtr = histogram;
5340 }
5341 else
5342 {
5343 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
5344 hostPtr = histogram;
5345 }
5346 /* create a CL buffer for histogram */
5347 length = (MaxMap+1);
5348 histogramBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(cl_uint4), hostPtr, &clStatus);
5349 if (clStatus != CL_SUCCESS)
5350 {
5351 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
5352 goto cleanup;
5353 }
5354
dirk8a5cf512014-07-28 20:16:27 +00005355 status = LaunchHistogramKernel(clEnv, queue, imageBuffer, histogramBuffer, image, channel, exception);
cristy0c832c62014-03-07 22:21:04 +00005356 if (status == MagickFalse)
5357 goto cleanup;
5358
5359 /* read from the kenel output */
5360 if (ALIGNED(histogram,cl_uint4))
5361 {
5362 length = (MaxMap+1);
5363 clEnv->library->clEnqueueMapBuffer(queue, histogramBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(cl_uint4), 0, NULL, NULL, &clStatus);
5364 }
5365 else
5366 {
5367 length = (MaxMap+1);
5368 clStatus = clEnv->library->clEnqueueReadBuffer(queue, histogramBuffer, CL_TRUE, 0, length * sizeof(cl_uint4), histogram, 0, NULL, NULL);
5369 }
5370 if (clStatus != CL_SUCCESS)
5371 {
5372 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
5373 goto cleanup;
5374 }
5375
5376 /* unmap, don't block gpu to use this buffer again. */
5377 if (ALIGNED(histogram,cl_uint4))
5378 {
5379 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, histogramBuffer, histogram, 0, NULL, NULL);
5380 if (clStatus != CL_SUCCESS)
5381 {
5382 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
5383 goto cleanup;
5384 }
5385 }
5386
5387 /* recreate input buffer later, in case image updated */
5388#ifdef RECREATEBUFFER
dirk8a5cf512014-07-28 20:16:27 +00005389 if (imageBuffer!=NULL)
5390 clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00005391#endif
5392
5393 /* CPU stuff */
5394 /*
5395 Find the histogram boundaries by locating the black/white levels.
5396 */
5397 black.red=0.0;
5398 white.red=MaxRange(QuantumRange);
5399 if ((channel & RedChannel) != 0)
5400 {
5401 intensity=0.0;
5402 for (i=0; i <= (ssize_t) MaxMap; i++)
5403 {
5404 intensity+=histogram[i].s[2];
5405 if (intensity > black_point)
5406 break;
5407 }
5408 black.red=(MagickRealType) i;
5409 intensity=0.0;
5410 for (i=(ssize_t) MaxMap; i != 0; i--)
5411 {
5412 intensity+=histogram[i].s[2];
5413 if (intensity > ((double) image->columns*image->rows-white_point))
5414 break;
5415 }
5416 white.red=(MagickRealType) i;
5417 }
5418 black.green=0.0;
5419 white.green=MaxRange(QuantumRange);
5420 if ((channel & GreenChannel) != 0)
5421 {
5422 intensity=0.0;
5423 for (i=0; i <= (ssize_t) MaxMap; i++)
5424 {
5425 intensity+=histogram[i].s[2];
5426 if (intensity > black_point)
5427 break;
5428 }
5429 black.green=(MagickRealType) i;
5430 intensity=0.0;
5431 for (i=(ssize_t) MaxMap; i != 0; i--)
5432 {
5433 intensity+=histogram[i].s[2];
5434 if (intensity > ((double) image->columns*image->rows-white_point))
5435 break;
5436 }
5437 white.green=(MagickRealType) i;
5438 }
5439 black.blue=0.0;
5440 white.blue=MaxRange(QuantumRange);
5441 if ((channel & BlueChannel) != 0)
5442 {
5443 intensity=0.0;
5444 for (i=0; i <= (ssize_t) MaxMap; i++)
5445 {
5446 intensity+=histogram[i].s[2];
5447 if (intensity > black_point)
5448 break;
5449 }
5450 black.blue=(MagickRealType) i;
5451 intensity=0.0;
5452 for (i=(ssize_t) MaxMap; i != 0; i--)
5453 {
5454 intensity+=histogram[i].s[2];
5455 if (intensity > ((double) image->columns*image->rows-white_point))
5456 break;
5457 }
5458 white.blue=(MagickRealType) i;
5459 }
dirk8a5cf512014-07-28 20:16:27 +00005460 black.alpha=0.0;
5461 white.alpha=MaxRange(QuantumRange);
cristy0c832c62014-03-07 22:21:04 +00005462 if ((channel & OpacityChannel) != 0)
5463 {
5464 intensity=0.0;
5465 for (i=0; i <= (ssize_t) MaxMap; i++)
5466 {
5467 intensity+=histogram[i].s[2];
5468 if (intensity > black_point)
5469 break;
5470 }
dirk8a5cf512014-07-28 20:16:27 +00005471 black.alpha=(MagickRealType) i;
cristy0c832c62014-03-07 22:21:04 +00005472 intensity=0.0;
5473 for (i=(ssize_t) MaxMap; i != 0; i--)
5474 {
5475 intensity+=histogram[i].s[2];
5476 if (intensity > ((double) image->columns*image->rows-white_point))
5477 break;
5478 }
dirk8a5cf512014-07-28 20:16:27 +00005479 white.alpha=(MagickRealType) i;
cristy0c832c62014-03-07 22:21:04 +00005480 }
5481 /*
5482 black.index=0.0;
5483 white.index=MaxRange(QuantumRange);
5484 if (((channel & IndexChannel) != 0) && (image->colorspace == CMYKColorspace))
5485 {
5486 intensity=0.0;
5487 for (i=0; i <= (ssize_t) MaxMap; i++)
5488 {
5489 intensity+=histogram[i].index;
5490 if (intensity > black_point)
5491 break;
5492 }
5493 black.index=(MagickRealType) i;
5494 intensity=0.0;
5495 for (i=(ssize_t) MaxMap; i != 0; i--)
5496 {
5497 intensity+=histogram[i].index;
5498 if (intensity > ((double) image->columns*image->rows-white_point))
5499 break;
5500 }
5501 white.index=(MagickRealType) i;
5502 }
5503 */
5504
5505
5506 stretch_map=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,
5507 sizeof(*stretch_map));
5508
cristy931c8062014-03-10 13:19:17 +00005509 if (stretch_map == (PixelPacket *) NULL)
cristy0c832c62014-03-07 22:21:04 +00005510 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
5511 image->filename);
5512
5513 /*
5514 Stretch the histogram to create the stretched image mapping.
5515 */
5516 (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*sizeof(*stretch_map));
5517 for (i=0; i <= (ssize_t) MaxMap; i++)
5518 {
5519 if ((channel & RedChannel) != 0)
5520 {
5521 if (i < (ssize_t) black.red)
5522 stretch_map[i].red=(Quantum) 0;
5523 else
5524 if (i > (ssize_t) white.red)
5525 stretch_map[i].red=QuantumRange;
5526 else
5527 if (black.red != white.red)
5528 stretch_map[i].red=ScaleMapToQuantum((MagickRealType) (MaxMap*
5529 (i-black.red)/(white.red-black.red)));
5530 }
5531 if ((channel & GreenChannel) != 0)
5532 {
5533 if (i < (ssize_t) black.green)
5534 stretch_map[i].green=0;
5535 else
5536 if (i > (ssize_t) white.green)
5537 stretch_map[i].green=QuantumRange;
5538 else
5539 if (black.green != white.green)
5540 stretch_map[i].green=ScaleMapToQuantum((MagickRealType) (MaxMap*
5541 (i-black.green)/(white.green-black.green)));
5542 }
5543 if ((channel & BlueChannel) != 0)
5544 {
5545 if (i < (ssize_t) black.blue)
5546 stretch_map[i].blue=0;
5547 else
5548 if (i > (ssize_t) white.blue)
5549 stretch_map[i].blue= QuantumRange;
5550 else
5551 if (black.blue != white.blue)
5552 stretch_map[i].blue=ScaleMapToQuantum((MagickRealType) (MaxMap*
5553 (i-black.blue)/(white.blue-black.blue)));
5554 }
5555 if ((channel & OpacityChannel) != 0)
5556 {
dirk8a5cf512014-07-28 20:16:27 +00005557 if (i < (ssize_t) black.alpha)
5558 stretch_map[i].alpha=0;
cristy0c832c62014-03-07 22:21:04 +00005559 else
dirk8a5cf512014-07-28 20:16:27 +00005560 if (i > (ssize_t) white.alpha)
5561 stretch_map[i].alpha=QuantumRange;
cristy0c832c62014-03-07 22:21:04 +00005562 else
dirk8a5cf512014-07-28 20:16:27 +00005563 if (black.alpha != white.alpha)
5564 stretch_map[i].alpha=ScaleMapToQuantum((MagickRealType) (MaxMap*
5565 (i-black.alpha)/(white.alpha-black.alpha)));
cristy0c832c62014-03-07 22:21:04 +00005566 }
5567 /*
5568 if (((channel & IndexChannel) != 0) &&
5569 (image->colorspace == CMYKColorspace))
5570 {
5571 if (i < (ssize_t) black.index)
5572 stretch_map[i].index=0;
5573 else
5574 if (i > (ssize_t) white.index)
5575 stretch_map[i].index=QuantumRange;
5576 else
5577 if (black.index != white.index)
5578 stretch_map[i].index=ScaleMapToQuantum((MagickRealType) (MaxMap*
5579 (i-black.index)/(white.index-black.index)));
5580 }
5581 */
5582 }
5583
5584 /*
5585 Stretch the image.
5586 */
5587 if (((channel & OpacityChannel) != 0) || (((channel & IndexChannel) != 0) &&
5588 (image->colorspace == CMYKColorspace)))
5589 image->storage_class=DirectClass;
5590 if (image->storage_class == PseudoClass)
5591 {
5592 /*
5593 Stretch colormap.
5594 */
5595 for (i=0; i < (ssize_t) image->colors; i++)
5596 {
5597 if ((channel & RedChannel) != 0)
5598 {
5599 if (black.red != white.red)
5600 image->colormap[i].red=stretch_map[
5601 ScaleQuantumToMap(image->colormap[i].red)].red;
5602 }
5603 if ((channel & GreenChannel) != 0)
5604 {
5605 if (black.green != white.green)
5606 image->colormap[i].green=stretch_map[
5607 ScaleQuantumToMap(image->colormap[i].green)].green;
5608 }
5609 if ((channel & BlueChannel) != 0)
5610 {
5611 if (black.blue != white.blue)
5612 image->colormap[i].blue=stretch_map[
5613 ScaleQuantumToMap(image->colormap[i].blue)].blue;
5614 }
5615 if ((channel & OpacityChannel) != 0)
5616 {
dirk8a5cf512014-07-28 20:16:27 +00005617 if (black.alpha != white.alpha)
5618 image->colormap[i].alpha=stretch_map[
5619 ScaleQuantumToMap(image->colormap[i].alpha)].alpha;
cristy0c832c62014-03-07 22:21:04 +00005620 }
5621 }
5622 }
5623
5624 /*
5625 Stretch image.
5626 */
5627
5628
5629 /* GPU can work on this again, image and equalize map as input
5630 image: uchar4 (CLPixelPacket)
5631 stretch_map: uchar4 (PixelPacket)
5632 black, white: float4 (FloatPixelPacket) */
5633
5634#ifdef RECREATEBUFFER
5635 /* If the host pointer is aligned to the size of CLPixelPacket,
5636 then use the host buffer directly from the GPU; otherwise,
5637 create a buffer on the GPU and copy the data over */
5638 if (ALIGNED(inputPixels,CLPixelPacket))
5639 {
5640 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
5641 }
5642 else
5643 {
5644 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
5645 }
5646 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00005647 length = image->columns * image->rows;
5648 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristy0c832c62014-03-07 22:21:04 +00005649 if (clStatus != CL_SUCCESS)
5650 {
5651 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
5652 goto cleanup;
5653 }
5654#endif
5655
5656 /* Create and initialize OpenCL buffers. */
5657 if (ALIGNED(stretch_map, PixelPacket))
5658 {
5659 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
5660 hostPtr = stretch_map;
5661 }
5662 else
5663 {
5664 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
5665 hostPtr = stretch_map;
5666 }
5667 /* create a CL buffer for stretch_map */
5668 length = (MaxMap+1);
5669 stretchMapBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(PixelPacket), hostPtr, &clStatus);
5670 if (clStatus != CL_SUCCESS)
5671 {
5672 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
5673 goto cleanup;
5674 }
5675
5676 /* get the OpenCL kernel */
5677 stretchKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Stretch");
5678 if (stretchKernel == NULL)
5679 {
5680 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", ".");
5681 goto cleanup;
5682 }
5683
5684 /* set the kernel arguments */
5685 i = 0;
dirk8a5cf512014-07-28 20:16:27 +00005686 clStatus=clEnv->library->clSetKernelArg(stretchKernel,i++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00005687 clStatus|=clEnv->library->clSetKernelArg(stretchKernel,i++,sizeof(ChannelType),&channel);
5688 clStatus|=clEnv->library->clSetKernelArg(stretchKernel,i++,sizeof(cl_mem),(void *)&stretchMapBuffer);
5689 clStatus|=clEnv->library->clSetKernelArg(stretchKernel,i++,sizeof(FloatPixelPacket),&white);
5690 clStatus|=clEnv->library->clSetKernelArg(stretchKernel,i++,sizeof(FloatPixelPacket),&black);
5691 if (clStatus != CL_SUCCESS)
5692 {
5693 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
5694 goto cleanup;
5695 }
5696
5697 /* launch the kernel */
dirk8a5cf512014-07-28 20:16:27 +00005698 global_work_size[0] = image->columns;
5699 global_work_size[1] = image->rows;
cristy0c832c62014-03-07 22:21:04 +00005700
5701 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, stretchKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
5702
5703 if (clStatus != CL_SUCCESS)
5704 {
5705 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
5706 goto cleanup;
5707 }
5708 clEnv->library->clFlush(queue);
5709
5710 /* read the data back */
5711 if (ALIGNED(inputPixels,CLPixelPacket))
5712 {
dirk8a5cf512014-07-28 20:16:27 +00005713 length = image->columns * image->rows;
5714 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 +00005715 }
5716 else
5717 {
dirk8a5cf512014-07-28 20:16:27 +00005718 length = image->columns * image->rows;
5719 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
cristy0c832c62014-03-07 22:21:04 +00005720 }
5721 if (clStatus != CL_SUCCESS)
5722 {
5723 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
5724 goto cleanup;
5725 }
5726
dirk832becc2014-08-04 19:44:34 +00005727 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
5728
5729cleanup:
5730 OpenCLLogException(__FUNCTION__,__LINE__,exception);
5731
dirk8a5cf512014-07-28 20:16:27 +00005732 image_view=DestroyCacheView(image_view);
cristy0c832c62014-03-07 22:21:04 +00005733
dirk8a5cf512014-07-28 20:16:27 +00005734 if (imageBuffer!=NULL)
5735 clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00005736
5737 if (stretchMapBuffer!=NULL)
5738 clEnv->library->clReleaseMemObject(stretchMapBuffer);
5739 if (stretch_map!=NULL)
5740 stretch_map=(PixelPacket *) RelinquishMagickMemory(stretch_map);
5741
5742
5743 if (histogramBuffer!=NULL)
5744 clEnv->library->clReleaseMemObject(histogramBuffer);
5745 if (histogram!=NULL)
5746 histogram=(cl_uint4 *) RelinquishMagickMemory(histogram);
5747
5748
5749 if (histogramKernel!=NULL)
5750 RelinquishOpenCLKernel(clEnv, histogramKernel);
5751 if (stretchKernel!=NULL)
5752 RelinquishOpenCLKernel(clEnv, stretchKernel);
5753
5754 if (queue != NULL)
5755 RelinquishOpenCLCommandQueue(clEnv, queue);
5756
dirk832becc2014-08-04 19:44:34 +00005757 return(outputReady);
5758}
5759
5760MagickExport MagickBooleanType AccelerateContrastStretchImageChannel(
5761 Image *image,const ChannelType channel,const double black_point,
5762 const double white_point,ExceptionInfo *exception)
5763{
5764 MagickBooleanType
5765 status;
5766
5767 assert(image != NULL);
5768 assert(exception != (ExceptionInfo *) NULL);
5769
5770 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
5771 (checkAccelerateCondition(image, channel) == MagickFalse) ||
5772 (checkHistogramCondition(image, channel) == MagickFalse))
5773 return(MagickFalse);
5774
5775 status=ComputeContrastStretchImageChannel(image,channel, black_point, white_point, exception);
5776 return(status);
cristy0c832c62014-03-07 22:21:04 +00005777}
5778
cristy0c832c62014-03-07 22:21:04 +00005779/*
5780%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5781% %
5782% %
5783% %
dirk8a5cf512014-07-28 20:16:27 +00005784% 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 +00005785% %
5786% %
5787% %
5788%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5789%
dirk8a5cf512014-07-28 20:16:27 +00005790% DespeckleImage() reduces the speckle noise in an image while perserving the
5791% edges of the original image. A speckle removing filter uses a complementary
5792% hulling technique (raising pixels that are darker than their surrounding
5793% neighbors, then complementarily lowering pixels that are brighter than their
5794% surrounding neighbors) to reduce the speckle index of that image (reference
5795% Crimmins speckle removal).
cristy0c832c62014-03-07 22:21:04 +00005796%
dirk8a5cf512014-07-28 20:16:27 +00005797% The format of the DespeckleImage method is:
cristy0c832c62014-03-07 22:21:04 +00005798%
dirk8a5cf512014-07-28 20:16:27 +00005799% Image *DespeckleImage(const Image *image,ExceptionInfo *exception)
cristy0c832c62014-03-07 22:21:04 +00005800%
5801% A description of each parameter follows:
5802%
5803% o image: the image.
5804%
dirk8a5cf512014-07-28 20:16:27 +00005805% o exception: return any errors or warnings in this structure.
cristy0c832c62014-03-07 22:21:04 +00005806%
5807*/
5808
dirk832becc2014-08-04 19:44:34 +00005809static Image *ComputeDespeckleImage(const Image *image,
5810 ExceptionInfo*exception)
5811{
5812 static const int
5813 X[4] = {0, 1, 1,-1},
5814 Y[4] = {1, 0, 1, 1};
5815
5816 CacheView
5817 *filteredImage_view,
5818 *image_view;
5819
5820 cl_command_queue
5821 queue;
5822
5823 cl_context
5824 context;
5825
5826 cl_int
5827 clStatus;
5828
5829 cl_kernel
5830 hullPass1,
5831 hullPass2;
5832
5833 cl_mem_flags
5834 mem_flags;
5835
5836 cl_mem
5837 filteredImageBuffer,
5838 imageBuffer,
5839 tempImageBuffer[2];
5840
5841 const void
5842 *inputPixels;
5843
5844 Image
5845 *filteredImage;
5846
5847 int
5848 k,
5849 matte;
5850
5851 MagickBooleanType
5852 outputReady;
5853
5854 MagickCLEnv
5855 clEnv;
5856
5857 MagickSizeType
5858 length;
5859
5860 size_t
5861 global_work_size[2];
5862
5863 unsigned int
5864 imageHeight,
5865 imageWidth;
5866
5867 void
5868 *filteredPixels,
5869 *hostPtr;
5870
5871 outputReady = MagickFalse;
5872 clEnv = NULL;
5873 inputPixels = NULL;
5874 filteredImage = NULL;
5875 filteredImage_view = NULL;
5876 filteredPixels = NULL;
5877 context = NULL;
5878 imageBuffer = NULL;
5879 filteredImageBuffer = NULL;
5880 hullPass1 = NULL;
5881 hullPass2 = NULL;
5882 queue = NULL;
5883 tempImageBuffer[0] = tempImageBuffer[1] = NULL;
5884 clEnv = GetDefaultOpenCLEnv();
5885 context = GetOpenCLContext(clEnv);
5886 queue = AcquireOpenCLCommandQueue(clEnv);
5887
5888 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00005889 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00005890 if (inputPixels == (void *) NULL)
5891 {
dirk8a5cf512014-07-28 20:16:27 +00005892 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristyf034abb2013-11-24 14:16:14 +00005893 goto cleanup;
5894 }
5895
5896 if (ALIGNED(inputPixels,CLPixelPacket))
5897 {
5898 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
5899 }
5900 else
5901 {
5902 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
5903 }
5904 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00005905 length = image->columns * image->rows;
5906 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
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->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00005910 goto cleanup;
5911 }
5912
5913 mem_flags = CL_MEM_READ_WRITE;
dirk8a5cf512014-07-28 20:16:27 +00005914 length = image->columns * image->rows;
cristyf034abb2013-11-24 14:16:14 +00005915 for (k = 0; k < 2; k++)
5916 {
cristy0c832c62014-03-07 22:21:04 +00005917 tempImageBuffer[k] = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), NULL, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00005918 if (clStatus != CL_SUCCESS)
5919 {
cristy0c832c62014-03-07 22:21:04 +00005920 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00005921 goto cleanup;
5922 }
5923 }
5924
dirk8a5cf512014-07-28 20:16:27 +00005925 filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristyf034abb2013-11-24 14:16:14 +00005926 assert(filteredImage != NULL);
dirk8a5cf512014-07-28 20:16:27 +00005927 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristyf034abb2013-11-24 14:16:14 +00005928 {
cristya22457d2013-12-07 14:03:06 +00005929 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005930 goto cleanup;
5931 }
dirk832becc2014-08-04 19:44:34 +00005932 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +00005933 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristyf034abb2013-11-24 14:16:14 +00005934 if (filteredPixels == (void *) NULL)
5935 {
cristya22457d2013-12-07 14:03:06 +00005936 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename);
cristyf034abb2013-11-24 14:16:14 +00005937 goto cleanup;
5938 }
5939
5940 if (ALIGNED(filteredPixels,CLPixelPacket))
5941 {
5942 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
5943 hostPtr = filteredPixels;
5944 }
5945 else
5946 {
5947 mem_flags = CL_MEM_WRITE_ONLY;
5948 hostPtr = NULL;
5949 }
5950 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00005951 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00005952 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus);
cristyf034abb2013-11-24 14:16:14 +00005953 if (clStatus != CL_SUCCESS)
5954 {
cristy0c832c62014-03-07 22:21:04 +00005955 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristyf034abb2013-11-24 14:16:14 +00005956 goto cleanup;
5957 }
5958
5959 hullPass1 = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "HullPass1");
5960 hullPass2 = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "HullPass2");
5961
dirk8a5cf512014-07-28 20:16:27 +00005962 clStatus =clEnv->library->clSetKernelArg(hullPass1,0,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00005963 clStatus |=clEnv->library->clSetKernelArg(hullPass1,1,sizeof(cl_mem),(void *)(tempImageBuffer+1));
dirk8a5cf512014-07-28 20:16:27 +00005964 imageWidth = image->columns;
cristy0c832c62014-03-07 22:21:04 +00005965 clStatus |=clEnv->library->clSetKernelArg(hullPass1,2,sizeof(unsigned int),(void *)&imageWidth);
dirk8a5cf512014-07-28 20:16:27 +00005966 imageHeight = image->rows;
cristy0c832c62014-03-07 22:21:04 +00005967 clStatus |=clEnv->library->clSetKernelArg(hullPass1,3,sizeof(unsigned int),(void *)&imageHeight);
dirk8a5cf512014-07-28 20:16:27 +00005968 matte = (image->alpha_trait==BlendPixelTrait)?0:1;
cristy0c832c62014-03-07 22:21:04 +00005969 clStatus |=clEnv->library->clSetKernelArg(hullPass1,6,sizeof(int),(void *)&matte);
cristyf034abb2013-11-24 14:16:14 +00005970 if (clStatus != CL_SUCCESS)
5971 {
cristy0c832c62014-03-07 22:21:04 +00005972 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005973 goto cleanup;
5974 }
5975
cristy0c832c62014-03-07 22:21:04 +00005976 clStatus = clEnv->library->clSetKernelArg(hullPass2,0,sizeof(cl_mem),(void *)(tempImageBuffer+1));
5977 clStatus |=clEnv->library->clSetKernelArg(hullPass2,1,sizeof(cl_mem),(void *)tempImageBuffer);
dirk8a5cf512014-07-28 20:16:27 +00005978 imageWidth = image->columns;
cristy0c832c62014-03-07 22:21:04 +00005979 clStatus |=clEnv->library->clSetKernelArg(hullPass2,2,sizeof(unsigned int),(void *)&imageWidth);
dirk8a5cf512014-07-28 20:16:27 +00005980 imageHeight = image->rows;
cristy0c832c62014-03-07 22:21:04 +00005981 clStatus |=clEnv->library->clSetKernelArg(hullPass2,3,sizeof(unsigned int),(void *)&imageHeight);
dirk8a5cf512014-07-28 20:16:27 +00005982 matte = (image->alpha_trait==BlendPixelTrait)?0:1;
cristy0c832c62014-03-07 22:21:04 +00005983 clStatus |=clEnv->library->clSetKernelArg(hullPass2,6,sizeof(int),(void *)&matte);
cristyf034abb2013-11-24 14:16:14 +00005984 if (clStatus != CL_SUCCESS)
5985 {
cristy0c832c62014-03-07 22:21:04 +00005986 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00005987 goto cleanup;
5988 }
5989
5990
dirk8a5cf512014-07-28 20:16:27 +00005991 global_work_size[0] = image->columns;
5992 global_work_size[1] = image->rows;
cristyf034abb2013-11-24 14:16:14 +00005993
5994
5995 for (k = 0; k < 4; k++)
5996 {
5997 cl_int2 offset;
5998 int polarity;
5999
6000
6001 offset.s[0] = X[k];
6002 offset.s[1] = Y[k];
6003 polarity = 1;
cristy0c832c62014-03-07 22:21:04 +00006004 clStatus = clEnv->library->clSetKernelArg(hullPass1,4,sizeof(cl_int2),(void *)&offset);
6005 clStatus|= clEnv->library->clSetKernelArg(hullPass1,5,sizeof(int),(void *)&polarity);
6006 clStatus|=clEnv->library->clSetKernelArg(hullPass2,4,sizeof(cl_int2),(void *)&offset);
6007 clStatus|=clEnv->library->clSetKernelArg(hullPass2,5,sizeof(int),(void *)&polarity);
cristyf034abb2013-11-24 14:16:14 +00006008 if (clStatus != CL_SUCCESS)
6009 {
cristy0c832c62014-03-07 22:21:04 +00006010 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00006011 goto cleanup;
6012 }
6013 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00006014 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass1, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00006015 if (clStatus != CL_SUCCESS)
6016 {
cristy0c832c62014-03-07 22:21:04 +00006017 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00006018 goto cleanup;
6019 }
6020 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00006021 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass2, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00006022 if (clStatus != CL_SUCCESS)
6023 {
cristy0c832c62014-03-07 22:21:04 +00006024 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00006025 goto cleanup;
6026 }
6027
6028
6029 if (k == 0)
cristy0c832c62014-03-07 22:21:04 +00006030 clStatus =clEnv->library->clSetKernelArg(hullPass1,0,sizeof(cl_mem),(void *)(tempImageBuffer));
cristyf034abb2013-11-24 14:16:14 +00006031 offset.s[0] = -X[k];
6032 offset.s[1] = -Y[k];
6033 polarity = 1;
cristy0c832c62014-03-07 22:21:04 +00006034 clStatus = clEnv->library->clSetKernelArg(hullPass1,4,sizeof(cl_int2),(void *)&offset);
6035 clStatus|= clEnv->library->clSetKernelArg(hullPass1,5,sizeof(int),(void *)&polarity);
6036 clStatus|=clEnv->library->clSetKernelArg(hullPass2,4,sizeof(cl_int2),(void *)&offset);
6037 clStatus|=clEnv->library->clSetKernelArg(hullPass2,5,sizeof(int),(void *)&polarity);
cristyf034abb2013-11-24 14:16:14 +00006038 if (clStatus != CL_SUCCESS)
6039 {
cristy0c832c62014-03-07 22:21:04 +00006040 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00006041 goto cleanup;
6042 }
6043 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00006044 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass1, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00006045 if (clStatus != CL_SUCCESS)
6046 {
cristy0c832c62014-03-07 22:21:04 +00006047 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00006048 goto cleanup;
6049 }
6050 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00006051 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass2, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00006052 if (clStatus != CL_SUCCESS)
6053 {
cristy0c832c62014-03-07 22:21:04 +00006054 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00006055 goto cleanup;
6056 }
6057
6058 offset.s[0] = -X[k];
6059 offset.s[1] = -Y[k];
6060 polarity = -1;
cristy0c832c62014-03-07 22:21:04 +00006061 clStatus = clEnv->library->clSetKernelArg(hullPass1,4,sizeof(cl_int2),(void *)&offset);
6062 clStatus|= clEnv->library->clSetKernelArg(hullPass1,5,sizeof(int),(void *)&polarity);
6063 clStatus|=clEnv->library->clSetKernelArg(hullPass2,4,sizeof(cl_int2),(void *)&offset);
6064 clStatus|=clEnv->library->clSetKernelArg(hullPass2,5,sizeof(int),(void *)&polarity);
cristyf034abb2013-11-24 14:16:14 +00006065 if (clStatus != CL_SUCCESS)
6066 {
cristy0c832c62014-03-07 22:21:04 +00006067 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00006068 goto cleanup;
6069 }
6070 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00006071 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass1, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00006072 if (clStatus != CL_SUCCESS)
6073 {
cristy0c832c62014-03-07 22:21:04 +00006074 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00006075 goto cleanup;
6076 }
6077 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00006078 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass2, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00006079 if (clStatus != CL_SUCCESS)
6080 {
cristy0c832c62014-03-07 22:21:04 +00006081 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00006082 goto cleanup;
6083 }
6084
6085 offset.s[0] = X[k];
6086 offset.s[1] = Y[k];
6087 polarity = -1;
cristy0c832c62014-03-07 22:21:04 +00006088 clStatus = clEnv->library->clSetKernelArg(hullPass1,4,sizeof(cl_int2),(void *)&offset);
6089 clStatus|= clEnv->library->clSetKernelArg(hullPass1,5,sizeof(int),(void *)&polarity);
6090 clStatus|=clEnv->library->clSetKernelArg(hullPass2,4,sizeof(cl_int2),(void *)&offset);
6091 clStatus|=clEnv->library->clSetKernelArg(hullPass2,5,sizeof(int),(void *)&polarity);
cristyf034abb2013-11-24 14:16:14 +00006092
6093 if (k == 3)
cristy0c832c62014-03-07 22:21:04 +00006094 clStatus |=clEnv->library->clSetKernelArg(hullPass2,1,sizeof(cl_mem),(void *)&filteredImageBuffer);
cristyf034abb2013-11-24 14:16:14 +00006095
6096 if (clStatus != CL_SUCCESS)
6097 {
cristy0c832c62014-03-07 22:21:04 +00006098 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00006099 goto cleanup;
6100 }
6101 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00006102 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass1, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00006103 if (clStatus != CL_SUCCESS)
6104 {
cristy0c832c62014-03-07 22:21:04 +00006105 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00006106 goto cleanup;
6107 }
6108 /* launch the kernel */
cristy0c832c62014-03-07 22:21:04 +00006109 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass2, 2, NULL, global_work_size, NULL, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00006110 if (clStatus != CL_SUCCESS)
6111 {
cristy0c832c62014-03-07 22:21:04 +00006112 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00006113 goto cleanup;
6114 }
6115 }
6116
6117 if (ALIGNED(filteredPixels,CLPixelPacket))
6118 {
dirk8a5cf512014-07-28 20:16:27 +00006119 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006120 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 +00006121 }
6122 else
6123 {
dirk8a5cf512014-07-28 20:16:27 +00006124 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006125 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
cristyf034abb2013-11-24 14:16:14 +00006126 }
6127 if (clStatus != CL_SUCCESS)
6128 {
cristya22457d2013-12-07 14:03:06 +00006129 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristyf034abb2013-11-24 14:16:14 +00006130 goto cleanup;
6131 }
6132
dirk832becc2014-08-04 19:44:34 +00006133 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
6134
6135cleanup:
6136 OpenCLLogException(__FUNCTION__,__LINE__,exception);
6137
6138 image_view=DestroyCacheView(image_view);
6139 if (filteredImage_view != NULL)
dirk8a5cf512014-07-28 20:16:27 +00006140 filteredImage_view=DestroyCacheView(filteredImage_view);
cristya22457d2013-12-07 14:03:06 +00006141
cristyf034abb2013-11-24 14:16:14 +00006142 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
dirk8a5cf512014-07-28 20:16:27 +00006143 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristyf034abb2013-11-24 14:16:14 +00006144 for (k = 0; k < 2; k++)
6145 {
cristy0c832c62014-03-07 22:21:04 +00006146 if (tempImageBuffer[k]!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer[k]);
cristyf034abb2013-11-24 14:16:14 +00006147 }
cristy0c832c62014-03-07 22:21:04 +00006148 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
cristyf034abb2013-11-24 14:16:14 +00006149 if (hullPass1!=NULL) RelinquishOpenCLKernel(clEnv, hullPass1);
6150 if (hullPass2!=NULL) RelinquishOpenCLKernel(clEnv, hullPass2);
dirk8a5cf512014-07-28 20:16:27 +00006151 if (outputReady == MagickFalse && filteredImage != NULL)
6152 filteredImage=DestroyImage(filteredImage);
6153 return(filteredImage);
cristyf034abb2013-11-24 14:16:14 +00006154}
6155
dirk832becc2014-08-04 19:44:34 +00006156MagickExport Image *AccelerateDespeckleImage(const Image* image,
6157 ExceptionInfo* exception)
6158{
6159 Image
6160 *filteredImage;
6161
6162 assert(image != NULL);
6163 assert(exception != (ExceptionInfo *) NULL);
6164
6165 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
6166 (checkAccelerateCondition(image, AllChannels) == MagickFalse))
6167 return NULL;
6168
6169 filteredImage=ComputeDespeckleImage(image,exception);
6170 return(filteredImage);
cristyf034abb2013-11-24 14:16:14 +00006171}
6172
dirk832becc2014-08-04 19:44:34 +00006173static Image *ComputeAddNoiseImage(const Image *image,
6174 const ChannelType channel,const NoiseType noise_type,
6175 ExceptionInfo *exception)
6176{
6177 CacheView
6178 *filteredImage_view,
6179 *image_view;
6180
6181 cl_command_queue
6182 queue;
6183
6184 cl_context
6185 context;
6186
6187 cl_int
dirk859503c2014-08-13 20:39:42 +00006188 inputPixelCount,
6189 pixelsPerWorkitem,
dirk832becc2014-08-04 19:44:34 +00006190 clStatus;
6191
dirk859503c2014-08-13 20:39:42 +00006192 cl_uint
6193 seed0,
6194 seed1;
6195
dirk832becc2014-08-04 19:44:34 +00006196 cl_kernel
6197 addNoiseKernel;
6198
6199 cl_mem_flags
6200 mem_flags;
6201
6202 cl_mem
6203 filteredImageBuffer,
dirk859503c2014-08-13 20:39:42 +00006204 imageBuffer;
dirk832becc2014-08-04 19:44:34 +00006205
6206 const char
6207 *option;
6208
6209 const void
6210 *inputPixels;
6211
6212 float
dirk859503c2014-08-13 20:39:42 +00006213 attenuate;
dirk832becc2014-08-04 19:44:34 +00006214
6215 MagickBooleanType
6216 outputReady;
6217
6218 MagickCLEnv
6219 clEnv;
6220
6221 MagickSizeType
6222 length;
6223
6224 Image
6225 *filteredImage;
6226
dirk832becc2014-08-04 19:44:34 +00006227 RandomInfo
6228 **restrict random_info;
6229
6230 size_t
dirk859503c2014-08-13 20:39:42 +00006231 global_work_size[1],
6232 local_work_size[1];
dirk832becc2014-08-04 19:44:34 +00006233
6234 unsigned int
dirk832becc2014-08-04 19:44:34 +00006235 k,
dirk859503c2014-08-13 20:39:42 +00006236 numRandomNumberPerPixel;
dirk832becc2014-08-04 19:44:34 +00006237
6238#if defined(MAGICKCORE_OPENMP_SUPPORT)
6239 unsigned long
6240 key;
6241#endif
6242
6243 void
6244 *filteredPixels,
6245 *hostPtr;
6246
6247 outputReady = MagickFalse;
6248 clEnv = NULL;
6249 inputPixels = NULL;
6250 filteredImage = NULL;
6251 filteredImage_view = NULL;
6252 filteredPixels = NULL;
dirk832becc2014-08-04 19:44:34 +00006253 context = NULL;
6254 imageBuffer = NULL;
dirk832becc2014-08-04 19:44:34 +00006255 filteredImageBuffer = NULL;
6256 queue = NULL;
6257 addNoiseKernel = NULL;
6258
6259 clEnv = GetDefaultOpenCLEnv();
6260 context = GetOpenCLContext(clEnv);
6261 queue = AcquireOpenCLCommandQueue(clEnv);
6262
6263 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00006264 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristye85d0f72013-11-27 02:25:43 +00006265 if (inputPixels == (void *) NULL)
6266 {
dirk8a5cf512014-07-28 20:16:27 +00006267 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristye85d0f72013-11-27 02:25:43 +00006268 goto cleanup;
6269 }
6270
6271 if (ALIGNED(inputPixels,CLPixelPacket))
6272 {
6273 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
6274 }
6275 else
6276 {
6277 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
6278 }
6279 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00006280 length = image->columns * image->rows;
6281 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristye85d0f72013-11-27 02:25:43 +00006282 if (clStatus != CL_SUCCESS)
6283 {
cristy0c832c62014-03-07 22:21:04 +00006284 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristye85d0f72013-11-27 02:25:43 +00006285 goto cleanup;
6286 }
6287
6288
dirk8a5cf512014-07-28 20:16:27 +00006289 filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristye85d0f72013-11-27 02:25:43 +00006290 assert(filteredImage != NULL);
dirk8a5cf512014-07-28 20:16:27 +00006291 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristye85d0f72013-11-27 02:25:43 +00006292 {
cristya22457d2013-12-07 14:03:06 +00006293 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", ".");
cristye85d0f72013-11-27 02:25:43 +00006294 goto cleanup;
6295 }
dirk832becc2014-08-04 19:44:34 +00006296 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +00006297 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristye85d0f72013-11-27 02:25:43 +00006298 if (filteredPixels == (void *) NULL)
6299 {
cristya22457d2013-12-07 14:03:06 +00006300 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename);
cristye85d0f72013-11-27 02:25:43 +00006301 goto cleanup;
6302 }
6303
6304 if (ALIGNED(filteredPixels,CLPixelPacket))
6305 {
6306 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
6307 hostPtr = filteredPixels;
6308 }
6309 else
6310 {
6311 mem_flags = CL_MEM_WRITE_ONLY;
6312 hostPtr = NULL;
6313 }
6314 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00006315 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006316 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus);
cristye85d0f72013-11-27 02:25:43 +00006317 if (clStatus != CL_SUCCESS)
6318 {
cristy0c832c62014-03-07 22:21:04 +00006319 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
cristye85d0f72013-11-27 02:25:43 +00006320 goto cleanup;
6321 }
6322
6323 /* find out how many random numbers needed by pixel */
6324 numRandomNumberPerPixel = 0;
6325 {
6326 unsigned int numRandPerChannel = 0;
6327 switch (noise_type)
6328 {
6329 case UniformNoise:
6330 case ImpulseNoise:
6331 case LaplacianNoise:
6332 case RandomNoise:
6333 default:
6334 numRandPerChannel = 1;
6335 break;
6336 case GaussianNoise:
6337 case MultiplicativeGaussianNoise:
6338 case PoissonNoise:
6339 numRandPerChannel = 2;
6340 break;
6341 };
6342
6343 if ((channel & RedChannel) != 0)
6344 numRandomNumberPerPixel+=numRandPerChannel;
6345 if ((channel & GreenChannel) != 0)
6346 numRandomNumberPerPixel+=numRandPerChannel;
6347 if ((channel & BlueChannel) != 0)
6348 numRandomNumberPerPixel+=numRandPerChannel;
6349 if ((channel & OpacityChannel) != 0)
6350 numRandomNumberPerPixel+=numRandPerChannel;
6351 }
6352
cristye85d0f72013-11-27 02:25:43 +00006353 /* set up the random number generators */
6354 attenuate=1.0;
dirk8a5cf512014-07-28 20:16:27 +00006355 option=GetImageArtifact(image,"attenuate");
cristye85d0f72013-11-27 02:25:43 +00006356 if (option != (char *) NULL)
6357 attenuate=StringToDouble(option,(char **) NULL);
6358 random_info=AcquireRandomInfoThreadSet();
6359#if defined(MAGICKCORE_OPENMP_SUPPORT)
6360 key=GetRandomSecretKey(random_info[0]);
6361#endif
6362
dirk859503c2014-08-13 20:39:42 +00006363 addNoiseKernel = AcquireOpenCLKernel(clEnv,MAGICK_OPENCL_ACCELERATE,"GenerateNoiseImage");
6364
6365 {
6366 cl_uint computeUnitCount;
6367 cl_uint workItemCount;
6368 clEnv->library->clGetDeviceInfo(clEnv->device, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &computeUnitCount, NULL);
6369 workItemCount = computeUnitCount * 2 * 256; // 256 work items per group, 2 groups per CU
6370 inputPixelCount = image->columns * image->rows;
6371 pixelsPerWorkitem = (inputPixelCount + workItemCount - 1) / workItemCount;
6372 pixelsPerWorkitem = ((pixelsPerWorkitem + 3) / 4) * 4;
6373
6374 local_work_size[0] = 256;
6375 global_work_size[0] = workItemCount;
6376 }
6377 {
6378 RandomInfo* randomInfo = AcquireRandomInfo();
6379 const unsigned long* s = GetRandomInfoSeed(randomInfo);
6380 seed0 = s[0];
6381 GetPseudoRandomValue(randomInfo);
6382 seed1 = s[0];
6383 randomInfo = DestroyRandomInfo(randomInfo);
6384 }
cristye85d0f72013-11-27 02:25:43 +00006385
6386 k = 0;
dirk8a5cf512014-07-28 20:16:27 +00006387 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_mem),(void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00006388 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_mem),(void *)&filteredImageBuffer);
dirk859503c2014-08-13 20:39:42 +00006389 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_uint),(void *)&inputPixelCount);
6390 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_uint),(void *)&pixelsPerWorkitem);
cristy0c832c62014-03-07 22:21:04 +00006391 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(ChannelType),(void *)&channel);
6392 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(NoiseType),(void *)&noise_type);
cristye85d0f72013-11-27 02:25:43 +00006393 attenuate=1.0f;
dirk8a5cf512014-07-28 20:16:27 +00006394 option=GetImageArtifact(image,"attenuate");
cristye85d0f72013-11-27 02:25:43 +00006395 if (option != (char *) NULL)
6396 attenuate=(float)StringToDouble(option,(char **) NULL);
cristy0c832c62014-03-07 22:21:04 +00006397 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(float),(void *)&attenuate);
dirk859503c2014-08-13 20:39:42 +00006398 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_uint),(void *)&seed0);
6399 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_uint),(void *)&seed1);
cristy0c832c62014-03-07 22:21:04 +00006400 clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(unsigned int),(void *)&numRandomNumberPerPixel);
cristye85d0f72013-11-27 02:25:43 +00006401
dirk859503c2014-08-13 20:39:42 +00006402 clEnv->library->clEnqueueNDRangeKernel(queue,addNoiseKernel,1,NULL,global_work_size,NULL,0,NULL,NULL);
cristye85d0f72013-11-27 02:25:43 +00006403
6404 if (ALIGNED(filteredPixels,CLPixelPacket))
6405 {
dirk8a5cf512014-07-28 20:16:27 +00006406 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006407 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 +00006408 }
6409 else
6410 {
dirk8a5cf512014-07-28 20:16:27 +00006411 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006412 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
cristye85d0f72013-11-27 02:25:43 +00006413 }
6414 if (clStatus != CL_SUCCESS)
6415 {
cristya22457d2013-12-07 14:03:06 +00006416 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
cristye85d0f72013-11-27 02:25:43 +00006417 goto cleanup;
6418 }
6419
dirk832becc2014-08-04 19:44:34 +00006420 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
6421
6422cleanup:
6423 OpenCLLogException(__FUNCTION__,__LINE__,exception);
6424
6425 image_view=DestroyCacheView(image_view);
6426 if (filteredImage_view != NULL)
dirk8a5cf512014-07-28 20:16:27 +00006427 filteredImage_view=DestroyCacheView(filteredImage_view);
cristya22457d2013-12-07 14:03:06 +00006428
cristye85d0f72013-11-27 02:25:43 +00006429 if (queue!=NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
6430 if (addNoiseKernel!=NULL) RelinquishOpenCLKernel(clEnv, addNoiseKernel);
dirk8a5cf512014-07-28 20:16:27 +00006431 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00006432 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
dirk832becc2014-08-04 19:44:34 +00006433 if (outputReady == MagickFalse && filteredImage != NULL)
6434 filteredImage=DestroyImage(filteredImage);
6435
dirk8a5cf512014-07-28 20:16:27 +00006436 return(filteredImage);
cristye85d0f72013-11-27 02:25:43 +00006437}
6438
cristye85d0f72013-11-27 02:25:43 +00006439
dirk832becc2014-08-04 19:44:34 +00006440MagickExport Image *AccelerateAddNoiseImage(const Image *image,
6441 const ChannelType channel,const NoiseType noise_type,
6442 ExceptionInfo *exception)
6443{
6444 Image
6445 *filteredImage;
6446
6447 assert(image != NULL);
6448 assert(exception != (ExceptionInfo *) NULL);
6449
6450 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
6451 (checkAccelerateCondition(image, channel) == MagickFalse))
6452 return NULL;
6453
dirk859503c2014-08-13 20:39:42 +00006454 filteredImage = ComputeAddNoiseImage(image,channel,noise_type,exception);
dirk832becc2014-08-04 19:44:34 +00006455
6456 return(filteredImage);
cristye85d0f72013-11-27 02:25:43 +00006457}
6458
dirk832becc2014-08-04 19:44:34 +00006459static MagickBooleanType LaunchRandomImageKernel(MagickCLEnv clEnv,
6460 cl_command_queue queue,cl_mem imageBuffer,const unsigned int imageColumns,
6461 const unsigned int imageRows,cl_mem seedBuffer,
6462 const unsigned int numGenerators,ExceptionInfo *exception)
6463{
6464 int
6465 k;
6466
6467 cl_int
6468 clStatus;
6469
6470 cl_kernel
6471 randomImageKernel;
6472
6473 MagickBooleanType
6474 status;
6475
6476 size_t
6477 global_work_size,
6478 local_work_size;
6479
dirk8a5cf512014-07-28 20:16:27 +00006480 status = MagickFalse;
cristy0c832c62014-03-07 22:21:04 +00006481 randomImageKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "RandomImage");
6482
6483 k = 0;
dirk8a5cf512014-07-28 20:16:27 +00006484 clEnv->library->clSetKernelArg(randomImageKernel,k++,sizeof(cl_mem),(void*)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00006485 clEnv->library->clSetKernelArg(randomImageKernel,k++,sizeof(cl_uint),(void*)&imageColumns);
6486 clEnv->library->clSetKernelArg(randomImageKernel,k++,sizeof(cl_uint),(void*)&imageRows);
6487 clEnv->library->clSetKernelArg(randomImageKernel,k++,sizeof(cl_mem),(void*)&seedBuffer);
6488 {
6489 const float randNormNumerator = 1.0f;
6490 const unsigned int randNormDenominator = (unsigned int)(~0UL);
6491 clEnv->library->clSetKernelArg(randomImageKernel,k++,
6492 sizeof(float),(void*)&randNormNumerator);
6493 clEnv->library->clSetKernelArg(randomImageKernel,k++,
6494 sizeof(cl_uint),(void*)&randNormDenominator);
6495 }
6496
6497
6498 global_work_size = numGenerators;
6499 local_work_size = 64;
6500
6501 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue,randomImageKernel,1,NULL,&global_work_size,
6502 &local_work_size,0,NULL,NULL);
6503
6504 if (clStatus != CL_SUCCESS)
6505 {
6506 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning,
6507 "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
6508 goto cleanup;
6509 }
6510 status = MagickTrue;
6511
6512cleanup:
6513 if (randomImageKernel!=NULL) RelinquishOpenCLKernel(clEnv, randomImageKernel);
dirk832becc2014-08-04 19:44:34 +00006514 return(status);
6515}
6516
6517static MagickBooleanType ComputeRandomImage(Image* image,
6518 ExceptionInfo* exception)
6519{
6520 CacheView
6521 *image_view;
6522
6523 cl_command_queue
6524 queue;
6525
6526 cl_context
6527 context;
6528
6529 cl_int
6530 clStatus;
6531
6532 /* Don't release this buffer in this function !!! */
6533 cl_mem
6534 randomNumberSeedsBuffer;
6535
6536 cl_mem_flags
6537 mem_flags;
6538
6539 cl_mem
6540 imageBuffer;
6541
6542 MagickBooleanType
6543 outputReady,
6544 status;
6545
6546 MagickCLEnv
6547 clEnv;
6548
6549 MagickSizeType
6550 length;
6551
6552 void
6553 *inputPixels;
6554
6555 status = MagickFalse;
6556 outputReady = MagickFalse;
6557 inputPixels = NULL;
6558 context = NULL;
6559 imageBuffer = NULL;
6560 queue = NULL;
6561
6562 clEnv = GetDefaultOpenCLEnv();
6563 context = GetOpenCLContext(clEnv);
6564
6565 /* Create and initialize OpenCL buffers. */
6566 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00006567 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristy0c832c62014-03-07 22:21:04 +00006568 if (inputPixels == (void *) NULL)
6569 {
dirk8a5cf512014-07-28 20:16:27 +00006570 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename);
cristy0c832c62014-03-07 22:21:04 +00006571 goto cleanup;
6572 }
6573
6574 /* If the host pointer is aligned to the size of CLPixelPacket,
6575 then use the host buffer directly from the GPU; otherwise,
6576 create a buffer on the GPU and copy the data over */
6577 if (ALIGNED(inputPixels,CLPixelPacket))
6578 {
6579 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
6580 }
6581 else
6582 {
6583 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
6584 }
6585 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00006586 length = image->columns * image->rows;
6587 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
cristy0c832c62014-03-07 22:21:04 +00006588 if (clStatus != CL_SUCCESS)
6589 {
6590 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
6591 goto cleanup;
6592 }
6593
6594 queue = AcquireOpenCLCommandQueue(clEnv);
6595
6596 randomNumberSeedsBuffer = GetAndLockRandSeedBuffer(clEnv);
6597 if (randomNumberSeedsBuffer==NULL)
6598 {
6599 (void) OpenCLThrowMagickException(exception, GetMagickModule(),
6600 ResourceLimitWarning, "Failed to get GPU random number generators.",
6601 "'%s'", ".");
6602 goto cleanup;
6603 }
6604
6605 status = LaunchRandomImageKernel(clEnv,queue,
dirk8a5cf512014-07-28 20:16:27 +00006606 imageBuffer,
6607 image->columns,
6608 image->rows,
cristy0c832c62014-03-07 22:21:04 +00006609 randomNumberSeedsBuffer,
6610 GetNumRandGenerators(clEnv),
6611 exception);
6612 if (status==MagickFalse)
6613 {
6614 goto cleanup;
6615 }
6616
6617 if (ALIGNED(inputPixels,CLPixelPacket))
6618 {
dirk8a5cf512014-07-28 20:16:27 +00006619 length = image->columns * image->rows;
6620 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 +00006621 }
6622 else
6623 {
dirk8a5cf512014-07-28 20:16:27 +00006624 length = image->columns * image->rows;
6625 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
cristy0c832c62014-03-07 22:21:04 +00006626 }
6627 if (clStatus != CL_SUCCESS)
6628 {
6629 (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", ".");
6630 goto cleanup;
6631 }
dirk832becc2014-08-04 19:44:34 +00006632 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
6633
6634cleanup:
6635 OpenCLLogException(__FUNCTION__,__LINE__,exception);
6636
dirk8a5cf512014-07-28 20:16:27 +00006637 image_view=DestroyCacheView(image_view);
cristy0c832c62014-03-07 22:21:04 +00006638
6639 UnlockRandSeedBuffer(clEnv);
dirk8a5cf512014-07-28 20:16:27 +00006640 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00006641 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
6642 return outputReady;
6643}
6644
dirk832becc2014-08-04 19:44:34 +00006645MagickExport MagickBooleanType AccelerateRandomImage(Image *image,
6646 ExceptionInfo* exception)
6647{
6648 MagickBooleanType
6649 status;
6650
6651 assert(image != NULL);
6652 assert(exception != (ExceptionInfo *) NULL);
6653
6654 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
6655 (checkAccelerateCondition(image, AllChannels) == MagickFalse))
6656 return(MagickFalse);
6657
6658 status=ComputeRandomImage(image,exception);
6659 return(status);
cristy0c832c62014-03-07 22:21:04 +00006660}
6661
dirk832becc2014-08-04 19:44:34 +00006662static Image* ComputeMotionBlurImage(const Image *image,
6663 const ChannelType channel,const double *kernel,const size_t width,
6664 const OffsetInfo *offset,ExceptionInfo *exception)
6665{
6666 CacheView
6667 *filteredImage_view,
6668 *image_view;
6669
6670 cl_command_queue
6671 queue;
6672
6673 cl_context
6674 context;
6675
6676 cl_float4
6677 biasPixel;
6678
6679 cl_int
6680 clStatus;
6681
6682 cl_kernel
6683 motionBlurKernel;
6684
6685 cl_mem
6686 filteredImageBuffer,
6687 imageBuffer,
6688 imageKernelBuffer,
6689 offsetBuffer;
6690
6691 cl_mem_flags
6692 mem_flags;
6693
6694 const void
6695 *inputPixels;
6696
6697 float
6698 *kernelBufferPtr;
6699
6700 Image
6701 *filteredImage;
6702
6703 int
6704 *offsetBufferPtr;
6705
6706 MagickBooleanType
6707 outputReady;
6708
6709 MagickCLEnv
6710 clEnv;
6711
6712 PixelInfo
6713 bias;
6714
6715 MagickSizeType
6716 length;
6717
6718 size_t
6719 global_work_size[2],
6720 local_work_size[2];
6721
6722 unsigned int
6723 i,
6724 imageHeight,
6725 imageWidth,
6726 matte;
6727
6728 void
6729 *filteredPixels,
6730 *hostPtr;
6731
6732 outputReady = MagickFalse;
6733 context = NULL;
6734 filteredImage = NULL;
6735 filteredImage_view = NULL;
6736 imageBuffer = NULL;
6737 filteredImageBuffer = NULL;
6738 imageKernelBuffer = NULL;
6739 motionBlurKernel = NULL;
6740 queue = NULL;
6741
6742 clEnv = GetDefaultOpenCLEnv();
6743 context = GetOpenCLContext(clEnv);
6744
6745 /* Create and initialize OpenCL buffers. */
6746
6747 image_view=AcquireVirtualCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00006748 inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception);
cristy0c832c62014-03-07 22:21:04 +00006749 if (inputPixels == (const void *) NULL)
6750 {
6751 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
dirk8a5cf512014-07-28 20:16:27 +00006752 "UnableToReadPixelCache.","`%s'",image->filename);
cristy0c832c62014-03-07 22:21:04 +00006753 goto cleanup;
6754 }
6755
6756 // If the host pointer is aligned to the size of CLPixelPacket,
6757 // then use the host buffer directly from the GPU; otherwise,
6758 // create a buffer on the GPU and copy the data over
6759 if (ALIGNED(inputPixels,CLPixelPacket))
6760 {
6761 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
6762 }
6763 else
6764 {
6765 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
6766 }
6767 // create a CL buffer from image pixel buffer
dirk8a5cf512014-07-28 20:16:27 +00006768 length = image->columns * image->rows;
6769 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags,
cristy0c832c62014-03-07 22:21:04 +00006770 length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
6771 if (clStatus != CL_SUCCESS)
6772 {
6773 (void) ThrowMagickException(exception, GetMagickModule(),
6774 ResourceLimitError, "clEnv->library->clCreateBuffer failed.",".");
6775 goto cleanup;
6776 }
6777
6778
dirk8a5cf512014-07-28 20:16:27 +00006779 filteredImage = CloneImage(image,image->columns,image->rows,
cristy0c832c62014-03-07 22:21:04 +00006780 MagickTrue,exception);
6781 assert(filteredImage != NULL);
dirk8a5cf512014-07-28 20:16:27 +00006782 if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
cristy0c832c62014-03-07 22:21:04 +00006783 {
6784 (void) ThrowMagickException(exception, GetMagickModule(),
6785 ResourceLimitError, "CloneImage failed.", "'%s'", ".");
6786 goto cleanup;
6787 }
dirk832becc2014-08-04 19:44:34 +00006788 filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception);
dirk8a5cf512014-07-28 20:16:27 +00006789 filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception);
cristy0c832c62014-03-07 22:21:04 +00006790 if (filteredPixels == (void *) NULL)
6791 {
6792 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
6793 "UnableToReadPixelCache.","`%s'",filteredImage->filename);
6794 goto cleanup;
6795 }
6796
6797 if (ALIGNED(filteredPixels,CLPixelPacket))
6798 {
6799 mem_flags = CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR;
6800 hostPtr = filteredPixels;
6801 }
6802 else
6803 {
6804 mem_flags = CL_MEM_WRITE_ONLY;
6805 hostPtr = NULL;
6806 }
6807 // create a CL buffer from image pixel buffer
dirk8a5cf512014-07-28 20:16:27 +00006808 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006809 filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags,
6810 length * sizeof(CLPixelPacket), hostPtr, &clStatus);
6811 if (clStatus != CL_SUCCESS)
6812 {
6813 (void) ThrowMagickException(exception, GetMagickModule(),
6814 ResourceLimitError, "clEnv->library->clCreateBuffer failed.",".");
6815 goto cleanup;
6816 }
6817
6818
6819 imageKernelBuffer = clEnv->library->clCreateBuffer(context,
6820 CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, width * sizeof(float), NULL,
6821 &clStatus);
6822 if (clStatus != CL_SUCCESS)
6823 {
6824 (void) ThrowMagickException(exception, GetMagickModule(),
6825 ResourceLimitError, "clEnv->library->clCreateBuffer failed.",".");
6826 goto cleanup;
6827 }
6828
6829 queue = AcquireOpenCLCommandQueue(clEnv);
6830 kernelBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, imageKernelBuffer,
6831 CL_TRUE, CL_MAP_WRITE, 0, width * sizeof(float), 0, NULL, NULL, &clStatus);
6832 if (clStatus != CL_SUCCESS)
6833 {
6834 (void) ThrowMagickException(exception, GetMagickModule(),
6835 ResourceLimitError, "clEnv->library->clEnqueueMapBuffer failed.",".");
6836 goto cleanup;
6837 }
6838 for (i = 0; i < width; i++)
6839 {
6840 kernelBufferPtr[i] = (float) kernel[i];
6841 }
6842 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, imageKernelBuffer, kernelBufferPtr,
6843 0, NULL, NULL);
6844 if (clStatus != CL_SUCCESS)
6845 {
6846 (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError,
6847 "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
6848 goto cleanup;
6849 }
6850
6851 offsetBuffer = clEnv->library->clCreateBuffer(context,
6852 CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, width * sizeof(cl_int2), NULL,
6853 &clStatus);
6854 if (clStatus != CL_SUCCESS)
6855 {
6856 (void) ThrowMagickException(exception, GetMagickModule(),
6857 ResourceLimitError, "clEnv->library->clCreateBuffer failed.",".");
6858 goto cleanup;
6859 }
6860
6861 offsetBufferPtr = (int*)clEnv->library->clEnqueueMapBuffer(queue, offsetBuffer, CL_TRUE,
6862 CL_MAP_WRITE, 0, width * sizeof(cl_int2), 0, NULL, NULL, &clStatus);
6863 if (clStatus != CL_SUCCESS)
6864 {
6865 (void) ThrowMagickException(exception, GetMagickModule(),
6866 ResourceLimitError, "clEnv->library->clEnqueueMapBuffer failed.",".");
6867 goto cleanup;
6868 }
6869 for (i = 0; i < width; i++)
6870 {
6871 offsetBufferPtr[2*i] = (int)offset[i].x;
6872 offsetBufferPtr[2*i+1] = (int)offset[i].y;
6873 }
6874 clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, offsetBuffer, offsetBufferPtr, 0,
6875 NULL, NULL);
6876 if (clStatus != CL_SUCCESS)
6877 {
6878 (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError,
6879 "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", ".");
6880 goto cleanup;
6881 }
6882
6883
6884 // get the OpenCL kernel
6885 motionBlurKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE,
6886 "MotionBlur");
6887 if (motionBlurKernel == NULL)
6888 {
6889 (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError,
6890 "AcquireOpenCLKernel failed.", "'%s'", ".");
6891 goto cleanup;
6892 }
6893
6894 // set the kernel arguments
6895 i = 0;
6896 clStatus=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(cl_mem),
dirk8a5cf512014-07-28 20:16:27 +00006897 (void *)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00006898 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(cl_mem),
6899 (void *)&filteredImageBuffer);
dirk8a5cf512014-07-28 20:16:27 +00006900 imageWidth = image->columns;
6901 imageHeight = image->rows;
cristy0c832c62014-03-07 22:21:04 +00006902 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(unsigned int),
6903 &imageWidth);
6904 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(unsigned int),
6905 &imageHeight);
6906 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(cl_mem),
6907 (void *)&imageKernelBuffer);
6908 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(unsigned int),
6909 &width);
6910 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(cl_mem),
6911 (void *)&offsetBuffer);
6912
dirk8a5cf512014-07-28 20:16:27 +00006913 GetPixelInfo(image,&bias);
cristy0c832c62014-03-07 22:21:04 +00006914 biasPixel.s[0] = bias.red;
6915 biasPixel.s[1] = bias.green;
6916 biasPixel.s[2] = bias.blue;
dirk8a5cf512014-07-28 20:16:27 +00006917 biasPixel.s[3] = bias.alpha;
cristy0c832c62014-03-07 22:21:04 +00006918 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(cl_float4), &biasPixel);
6919
6920 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(ChannelType), &channel);
dirk8a5cf512014-07-28 20:16:27 +00006921 matte = (image->alpha_trait==BlendPixelTrait)?1:0;
cristy0c832c62014-03-07 22:21:04 +00006922 clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(unsigned int), &matte);
6923 if (clStatus != CL_SUCCESS)
6924 {
6925 (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError,
6926 "clEnv->library->clSetKernelArg failed.", "'%s'", ".");
6927 goto cleanup;
6928 }
6929
6930 // launch the kernel
6931 local_work_size[0] = 16;
6932 local_work_size[1] = 16;
6933 global_work_size[0] = (size_t)padGlobalWorkgroupSizeToLocalWorkgroupSize(
dirk8a5cf512014-07-28 20:16:27 +00006934 image->columns,local_work_size[0]);
cristy0c832c62014-03-07 22:21:04 +00006935 global_work_size[1] = (size_t)padGlobalWorkgroupSizeToLocalWorkgroupSize(
dirk8a5cf512014-07-28 20:16:27 +00006936 image->rows,local_work_size[1]);
cristy0c832c62014-03-07 22:21:04 +00006937 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, motionBlurKernel, 2, NULL,
6938 global_work_size, local_work_size, 0, NULL, NULL);
6939
6940 if (clStatus != CL_SUCCESS)
6941 {
6942 (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError,
6943 "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", ".");
6944 goto cleanup;
6945 }
6946 clEnv->library->clFlush(queue);
6947
6948 if (ALIGNED(filteredPixels,CLPixelPacket))
6949 {
dirk8a5cf512014-07-28 20:16:27 +00006950 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006951 clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE,
6952 CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL,
6953 NULL, &clStatus);
6954 }
6955 else
6956 {
dirk8a5cf512014-07-28 20:16:27 +00006957 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00006958 clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0,
6959 length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL);
6960 }
6961 if (clStatus != CL_SUCCESS)
6962 {
6963 (void) ThrowMagickException(exception, GetMagickModule(), ModuleFatalError,
6964 "Reading output image from CL buffer failed.", "'%s'", ".");
6965 goto cleanup;
6966 }
dirk832becc2014-08-04 19:44:34 +00006967 outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception);
6968
6969cleanup:
6970
6971 image_view=DestroyCacheView(image_view);
6972 if (filteredImage_view != NULL)
dirk8a5cf512014-07-28 20:16:27 +00006973 filteredImage_view=DestroyCacheView(filteredImage_view);
cristy0c832c62014-03-07 22:21:04 +00006974
6975 if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer);
dirk8a5cf512014-07-28 20:16:27 +00006976 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00006977 if (imageKernelBuffer!=NULL) clEnv->library->clReleaseMemObject(imageKernelBuffer);
6978 if (motionBlurKernel!=NULL) RelinquishOpenCLKernel(clEnv, motionBlurKernel);
6979 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
dirk832becc2014-08-04 19:44:34 +00006980 if (outputReady == MagickFalse && filteredImage != NULL)
6981 filteredImage=DestroyImage(filteredImage);
6982
dirk8a5cf512014-07-28 20:16:27 +00006983 return(filteredImage);
cristy0c832c62014-03-07 22:21:04 +00006984}
6985
dirk832becc2014-08-04 19:44:34 +00006986MagickExport Image *AccelerateMotionBlurImage(const Image *image,
6987 const ChannelType channel,const double* kernel,const size_t width,
6988 const OffsetInfo *offset,ExceptionInfo *exception)
6989{
6990 Image
6991 *filteredImage;
6992
6993 assert(image != NULL);
6994 assert(kernel != (double *) NULL);
6995 assert(offset != (OffsetInfo *) NULL);
6996 assert(exception != (ExceptionInfo *) NULL);
6997
6998 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
6999 (checkAccelerateCondition(image, channel) == MagickFalse))
7000 return NULL;
7001
7002 filteredImage=ComputeMotionBlurImage(image, channel, kernel, width,
7003 offset, exception);
7004 return(filteredImage);
cristy0c832c62014-03-07 22:21:04 +00007005}
7006
dirk832becc2014-08-04 19:44:34 +00007007static MagickBooleanType LaunchCompositeKernel(MagickCLEnv clEnv,
7008 cl_command_queue queue,cl_mem imageBuffer,const unsigned int inputWidth,
7009 const unsigned int inputHeight,const unsigned int matte,
7010 const ChannelType channel,const CompositeOperator compose,
7011 const cl_mem compositeImageBuffer,const unsigned int compositeWidth,
7012 const unsigned int compositeHeight,const float destination_dissolve,
7013 const float source_dissolve,ExceptionInfo *magick_unused(exception))
7014{
7015 cl_int
7016 clStatus;
7017
7018 cl_kernel
7019 compositeKernel;
7020
7021 int
7022 k;
7023
7024 size_t
7025 global_work_size[2],
7026 local_work_size[2];
7027
7028 unsigned int
7029 composeOp;
7030
cristy0c832c62014-03-07 22:21:04 +00007031 magick_unreferenced(exception);
7032
7033 compositeKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE,
7034 "Composite");
7035
7036 k = 0;
dirk8a5cf512014-07-28 20:16:27 +00007037 clStatus=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(cl_mem),(void*)&imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00007038 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(unsigned int),(void*)&inputWidth);
7039 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(unsigned int),(void*)&inputHeight);
7040 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(cl_mem),(void*)&compositeImageBuffer);
7041 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(unsigned int),(void*)&compositeWidth);
7042 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(unsigned int),(void*)&compositeHeight);
7043 composeOp = (unsigned int)compose;
7044 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(unsigned int),(void*)&composeOp);
7045 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(ChannelType),(void*)&channel);
7046 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(unsigned int),(void*)&matte);
7047 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(float),(void*)&destination_dissolve);
7048 clStatus|=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(float),(void*)&source_dissolve);
7049
7050 if (clStatus!=CL_SUCCESS)
7051 return MagickFalse;
7052
7053 local_work_size[0] = 64;
7054 local_work_size[1] = 1;
7055
7056 global_work_size[0] = padGlobalWorkgroupSizeToLocalWorkgroupSize(inputWidth,
7057 local_work_size[0]);
7058 global_work_size[1] = inputHeight;
7059 clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, compositeKernel, 2, NULL,
7060 global_work_size, local_work_size, 0, NULL, NULL);
7061
7062
7063 RelinquishOpenCLKernel(clEnv, compositeKernel);
7064
dirk832becc2014-08-04 19:44:34 +00007065 return((clStatus==CL_SUCCESS) ? MagickTrue : MagickFalse);
7066}
7067
7068static MagickBooleanType ComputeCompositeImage(Image *image,
7069 const ChannelType channel,const CompositeOperator compose,
7070 const Image *compositeImage,const ssize_t magick_unused(x_offset),
7071 const ssize_t magick_unused(y_offset),const float destination_dissolve,
7072 const float source_dissolve,ExceptionInfo *exception)
7073{
7074 CacheView
7075 *image_view;
7076
7077 cl_command_queue
7078 queue;
7079
7080 cl_context
7081 context;
7082
7083 cl_int
7084 clStatus;
7085
7086 cl_mem_flags
7087 mem_flags;
7088
7089 cl_mem
7090 compositeImageBuffer,
7091 imageBuffer;
7092
7093 const void
7094 *composePixels;
7095
7096 MagickBooleanType
7097 outputReady,
7098 status;
7099
7100 MagickCLEnv
7101 clEnv;
7102
7103 MagickSizeType
7104 length;
7105
7106 void
7107 *inputPixels;
7108
7109 magick_unreferenced(x_offset);
7110 magick_unreferenced(y_offset);
7111
7112 status = MagickFalse;
7113 outputReady = MagickFalse;
7114 composePixels = NULL;
7115 imageBuffer = NULL;
7116 compositeImageBuffer = NULL;
7117
7118 clEnv = GetDefaultOpenCLEnv();
7119 context = GetOpenCLContext(clEnv);
7120 queue = AcquireOpenCLCommandQueue(clEnv);
7121
7122 /* Create and initialize OpenCL buffers. */
7123 image_view=AcquireAuthenticCacheView(image,exception);
dirk8a5cf512014-07-28 20:16:27 +00007124 inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception);
cristy0c832c62014-03-07 22:21:04 +00007125 if (inputPixels == (void *) NULL)
7126 {
7127 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,
dirk8a5cf512014-07-28 20:16:27 +00007128 "UnableToReadPixelCache.","`%s'",image->filename);
cristy0c832c62014-03-07 22:21:04 +00007129 goto cleanup;
7130 }
7131
7132 /* If the host pointer is aligned to the size of CLPixelPacket,
7133 then use the host buffer directly from the GPU; otherwise,
7134 create a buffer on the GPU and copy the data over */
7135 if (ALIGNED(inputPixels,CLPixelPacket))
7136 {
7137 mem_flags = CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR;
7138 }
7139 else
7140 {
7141 mem_flags = CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR;
7142 }
7143 /* create a CL buffer from image pixel buffer */
dirk8a5cf512014-07-28 20:16:27 +00007144 length = image->columns * image->rows;
7145 imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags,
cristy0c832c62014-03-07 22:21:04 +00007146 length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus);
7147 if (clStatus != CL_SUCCESS)
7148 {
7149 (void) OpenCLThrowMagickException(exception, GetMagickModule(),
7150 ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
7151 goto cleanup;
7152 }
7153
7154
7155 /* Create and initialize OpenCL buffers. */
7156 composePixels = AcquirePixelCachePixels(compositeImage, &length, exception);
7157 if (composePixels == (void *) NULL)
7158 {
7159 (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,
7160 "UnableToReadPixelCache.","`%s'",compositeImage->filename);
7161 goto cleanup;
7162 }
7163
7164 /* If the host pointer is aligned to the size of CLPixelPacket,
7165 then use the host buffer directly from the GPU; otherwise,
7166 create a buffer on the GPU and copy the data over */
7167 if (ALIGNED(composePixels,CLPixelPacket))
7168 {
7169 mem_flags = CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR;
7170 }
7171 else
7172 {
7173 mem_flags = CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR;
7174 }
7175 /* create a CL buffer from image pixel buffer */
7176 length = compositeImage->columns * compositeImage->rows;
7177 compositeImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags,
7178 length * sizeof(CLPixelPacket), (void*)composePixels, &clStatus);
7179 if (clStatus != CL_SUCCESS)
7180 {
7181 (void) OpenCLThrowMagickException(exception, GetMagickModule(),
7182 ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.",".");
7183 goto cleanup;
7184 }
7185
dirk8a5cf512014-07-28 20:16:27 +00007186 status = LaunchCompositeKernel(clEnv,queue,imageBuffer,
7187 (unsigned int) image->columns,
7188 (unsigned int) image->rows,
7189 (unsigned int) (image->alpha_trait==BlendPixelTrait) ? 1 : 0,
cristy0c832c62014-03-07 22:21:04 +00007190 channel, compose, compositeImageBuffer,
7191 (unsigned int) compositeImage->columns,
7192 (unsigned int) compositeImage->rows,
7193 destination_dissolve,source_dissolve,
7194 exception);
7195
7196 if (status==MagickFalse)
7197 goto cleanup;
7198
dirk8a5cf512014-07-28 20:16:27 +00007199 length = image->columns * image->rows;
cristy0c832c62014-03-07 22:21:04 +00007200 if (ALIGNED(inputPixels,CLPixelPacket))
7201 {
dirk8a5cf512014-07-28 20:16:27 +00007202 clEnv->library->clEnqueueMapBuffer(queue, imageBuffer, CL_TRUE,
cristy0c832c62014-03-07 22:21:04 +00007203 CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL,
7204 NULL, &clStatus);
7205 }
7206 else
7207 {
dirk8a5cf512014-07-28 20:16:27 +00007208 clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0,
cristy0c832c62014-03-07 22:21:04 +00007209 length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL);
7210 }
7211 if (clStatus==CL_SUCCESS)
dirk832becc2014-08-04 19:44:34 +00007212 outputReady=SyncCacheViewAuthenticPixels(image_view,exception);
7213
7214cleanup:
7215
7216 image_view=DestroyCacheView(image_view);
dirk8a5cf512014-07-28 20:16:27 +00007217 if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer);
cristy0c832c62014-03-07 22:21:04 +00007218 if (compositeImageBuffer!=NULL) clEnv->library->clReleaseMemObject(compositeImageBuffer);
7219 if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue);
7220
dirk832becc2014-08-04 19:44:34 +00007221 return(outputReady);
7222}
7223
7224MagickExport MagickBooleanType AccelerateCompositeImage(Image *image,
7225 const ChannelType channel,const CompositeOperator compose,
7226 const Image *composite,const ssize_t x_offset,const ssize_t y_offset,
7227 const float destination_dissolve,const float source_dissolve,
7228 ExceptionInfo *exception)
7229{
7230 MagickBooleanType
7231 status;
7232
7233 assert(image != NULL);
7234 assert(exception != (ExceptionInfo *) NULL);
7235
7236 if ((checkOpenCLEnvironment(exception) == MagickFalse) ||
7237 (checkAccelerateCondition(image, channel) == MagickFalse))
dirk8a5cf512014-07-28 20:16:27 +00007238 return(MagickFalse);
cristy0c832c62014-03-07 22:21:04 +00007239
7240 /* only support zero offset and
7241 images with the size for now */
7242 if (x_offset!=0
7243 || y_offset!=0
7244 || image->columns!=composite->columns
7245 || image->rows!=composite->rows)
7246 return MagickFalse;
7247
7248 switch(compose) {
7249 case ColorDodgeCompositeOp:
7250 case BlendCompositeOp:
7251 break;
7252 default:
7253 // unsupported compose operator, quit
7254 return MagickFalse;
7255 };
7256
7257 status = ComputeCompositeImage(image,channel,compose,composite,
7258 x_offset,y_offset,destination_dissolve,source_dissolve,exception);
7259
dirk8a5cf512014-07-28 20:16:27 +00007260 return(status);
cristy0c832c62014-03-07 22:21:04 +00007261}
7262
cristyf034abb2013-11-24 14:16:14 +00007263#else /* MAGICKCORE_OPENCL_SUPPORT */
7264
7265MagickExport Image *AccelerateConvolveImageChannel(
7266 const Image *magick_unused(image),const ChannelType magick_unused(channel),
7267 const KernelInfo *magick_unused(kernel),
7268 ExceptionInfo *magick_unused(exception))
7269{
7270 magick_unreferenced(image);
7271 magick_unreferenced(channel);
7272 magick_unreferenced(kernel);
7273 magick_unreferenced(exception);
7274
7275 return NULL;
7276}
7277
7278MagickExport MagickBooleanType AccelerateFunctionImage(
7279 Image *magick_unused(image),const ChannelType magick_unused(channel),
7280 const MagickFunction magick_unused(function),
7281 const size_t magick_unused(number_parameters),
7282 const double *magick_unused(parameters),
7283 ExceptionInfo *magick_unused(exception))
7284{
7285 magick_unreferenced(image);
7286 magick_unreferenced(channel);
7287 magick_unreferenced(function);
7288 magick_unreferenced(number_parameters);
7289 magick_unreferenced(parameters);
7290 magick_unreferenced(exception);
7291
7292 return MagickFalse;
7293}
7294
7295MagickExport Image *AccelerateBlurImage(const Image *magick_unused(image),
7296 const ChannelType magick_unused(channel),const double magick_unused(radius),
7297 const double magick_unused(sigma),ExceptionInfo *magick_unused(exception))
7298{
7299 magick_unreferenced(image);
7300 magick_unreferenced(channel);
7301 magick_unreferenced(radius);
7302 magick_unreferenced(sigma);
7303 magick_unreferenced(exception);
7304
7305 return NULL;
7306}
7307
dirk6d612cf2014-03-13 21:17:23 +00007308MagickExport Image *AccelerateRotationalBlurImage(
cristyf034abb2013-11-24 14:16:14 +00007309 const Image *magick_unused(image),const ChannelType magick_unused(channel),
7310 const double magick_unused(angle),ExceptionInfo *magick_unused(exception))
7311{
7312 magick_unreferenced(image);
7313 magick_unreferenced(channel);
7314 magick_unreferenced(angle);
7315 magick_unreferenced(exception);
7316
7317 return NULL;
7318}
7319
7320
7321MagickExport Image *AccelerateUnsharpMaskImage(
7322 const Image *magick_unused(image),const ChannelType magick_unused(channel),
7323 const double magick_unused(radius),const double magick_unused(sigma),
7324 const double magick_unused(gain),const double magick_unused(threshold),
7325 ExceptionInfo *magick_unused(exception))
7326{
7327 magick_unreferenced(image);
7328 magick_unreferenced(channel);
7329 magick_unreferenced(radius);
7330 magick_unreferenced(sigma);
7331 magick_unreferenced(gain);
7332 magick_unreferenced(threshold);
7333 magick_unreferenced(exception);
7334
7335 return NULL;
7336}
7337
cristy0b8a2e92014-03-08 00:52:49 +00007338MagickExport
7339MagickBooleanType AccelerateCompositeImage(Image *image,
7340 const ChannelType channel,const CompositeOperator compose,
7341 const Image *composite,const ssize_t x_offset,const ssize_t y_offset,
7342 const float destination_dissolve,const float source_dissolve,
7343 ExceptionInfo *exception)
7344{
7345 magick_unreferenced(image);
cristya219e4b2014-03-08 12:36:16 +00007346 magick_unreferenced(channel);
7347 magick_unreferenced(compose);
7348 magick_unreferenced(composite);
7349 magick_unreferenced(x_offset);
7350 magick_unreferenced(y_offset);
7351 magick_unreferenced(destination_dissolve);
7352 magick_unreferenced(source_dissolve);
7353 magick_unreferenced(exception);
cristy0b8a2e92014-03-08 00:52:49 +00007354
7355 return MagickFalse;
7356}
7357
cristyf034abb2013-11-24 14:16:14 +00007358
7359MagickExport MagickBooleanType AccelerateContrastImage(
7360 Image* magick_unused(image),const MagickBooleanType magick_unused(sharpen),
7361 ExceptionInfo* magick_unused(exception))
7362{
7363 magick_unreferenced(image);
7364 magick_unreferenced(sharpen);
7365 magick_unreferenced(exception);
7366
7367 return MagickFalse;
7368}
7369
cristy0b8a2e92014-03-08 00:52:49 +00007370MagickExport MagickBooleanType AccelerateContrastStretchImageChannel(
cristy0c832c62014-03-07 22:21:04 +00007371 Image * image, const ChannelType channel, const double black_point, const double white_point,
7372 ExceptionInfo* magick_unused(exception))
7373{
7374 magick_unreferenced(image);
7375 magick_unreferenced(channel);
7376 magick_unreferenced(black_point);
7377 magick_unreferenced(white_point);
7378 magick_unreferenced(exception);
7379
7380 return MagickFalse;
7381}
7382
cristyf034abb2013-11-24 14:16:14 +00007383MagickExport MagickBooleanType AccelerateEqualizeImage(
7384 Image* magick_unused(image), const ChannelType magick_unused(channel),
7385 ExceptionInfo* magick_unused(exception))
7386{
7387 magick_unreferenced(image);
7388 magick_unreferenced(channel);
7389 magick_unreferenced(exception);
7390
7391 return MagickFalse;
7392}
7393
7394MagickExport Image *AccelerateDespeckleImage(const Image* magick_unused(image),
7395 ExceptionInfo* magick_unused(exception))
7396{
7397 magick_unreferenced(image);
7398 magick_unreferenced(exception);
7399
7400 return NULL;
7401}
7402
7403MagickExport Image *AccelerateResizeImage(const Image* magick_unused(image),
7404 const size_t magick_unused(resizedColumns),
7405 const size_t magick_unused(resizedRows),
7406 const ResizeFilter* magick_unused(resizeFilter),
7407 ExceptionInfo *magick_unused(exception))
7408{
7409 magick_unreferenced(image);
7410 magick_unreferenced(resizedColumns);
7411 magick_unreferenced(resizedRows);
7412 magick_unreferenced(resizeFilter);
7413 magick_unreferenced(exception);
7414
7415 return NULL;
7416}
7417
cristyf034abb2013-11-24 14:16:14 +00007418MagickExport
7419MagickBooleanType AccelerateModulateImage(
7420 Image* image, double percent_brightness, double percent_hue,
7421 double percent_saturation, ColorspaceType colorspace, ExceptionInfo* exception)
7422{
7423 magick_unreferenced(image);
7424 magick_unreferenced(percent_brightness);
7425 magick_unreferenced(percent_hue);
7426 magick_unreferenced(percent_saturation);
7427 magick_unreferenced(colorspace);
7428 magick_unreferenced(exception);
7429 return(MagickFalse);
7430}
7431
cristy0c832c62014-03-07 22:21:04 +00007432MagickExport
7433MagickBooleanType AccelerateNegateImageChannel(
7434 Image* image, const ChannelType channel, const MagickBooleanType grayscale, ExceptionInfo* exception)
7435{
7436 magick_unreferenced(image);
7437 magick_unreferenced(channel);
7438 magick_unreferenced(grayscale);
7439 magick_unreferenced(exception);
7440 return(MagickFalse);
7441}
7442
7443MagickExport
7444MagickBooleanType AccelerateGrayscaleImage(
7445 Image* image, const PixelIntensityMethod method, ExceptionInfo* exception)
7446{
7447 magick_unreferenced(image);
7448 magick_unreferenced(method);
7449 magick_unreferenced(exception);
7450 return(MagickFalse);
7451}
7452
cristye85d0f72013-11-27 02:25:43 +00007453MagickExport Image *AccelerateAddNoiseImage(const Image *image,
7454 const ChannelType channel, const NoiseType noise_type,ExceptionInfo *exception)
7455{
7456 magick_unreferenced(image);
7457 magick_unreferenced(channel);
7458 magick_unreferenced(noise_type);
7459 magick_unreferenced(exception);
7460 return NULL;
7461}
cristyf034abb2013-11-24 14:16:14 +00007462
cristy0c832c62014-03-07 22:21:04 +00007463
7464MagickExport MagickBooleanType AccelerateRandomImage(Image* image, ExceptionInfo* exception)
7465{
7466 magick_unreferenced(image);
7467 magick_unreferenced(exception);
7468 return MagickFalse;
7469}
7470
7471MagickExport
7472Image* AccelerateMotionBlurImage(const Image *image, const ChannelType channel,
7473 const double* kernel, const size_t width,
7474 const OffsetInfo *offset,
7475 ExceptionInfo *exception)
7476{
7477 magick_unreferenced(image);
7478 magick_unreferenced(channel);
7479 magick_unreferenced(kernel);
7480 magick_unreferenced(width);
7481 magick_unreferenced(offset);
7482 magick_unreferenced(exception);
7483 return NULL;
7484}
7485
cristyf034abb2013-11-24 14:16:14 +00007486#endif /* MAGICKCORE_OPENCL_SUPPORT */
7487
7488MagickExport MagickBooleanType AccelerateConvolveImage(
7489 const Image *magick_unused(image),const KernelInfo *magick_unused(kernel),
7490 Image *magick_unused(convolve_image),ExceptionInfo *magick_unused(exception))
7491{
7492 magick_unreferenced(image);
7493 magick_unreferenced(kernel);
7494 magick_unreferenced(convolve_image);
7495 magick_unreferenced(exception);
7496
7497 /* legacy, do not use */
7498 return(MagickFalse);
7499}
7500