blob: 0ab5f102a97fcff21d17013de2c69fb518e4ad18 [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 %
cristy0d127ab2010-05-14 23:29:46 +000016% John Cristy %
cristy3f6d1482010-01-20 21:01:21 +000017% January 2010 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3f6d1482010-01-20 21:01:21 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
cristy0d127ab2010-05-14 23:29:46 +000036% Morphology is the the application of various kernals, of any size and even
cristy3f6d1482010-01-20 21:01:21 +000037% shape, to a image in various ways (typically binary, but not always).
38%
39% Convolution (weighted sum or average) is just one specific type of
40% accelerate. Just one that is very common for image bluring and sharpening
41% effects. Not only 2D Gaussian blurring, but also 2-pass 1D Blurring.
42%
43% This module provides not only a general accelerate function, and the ability
44% to apply more advanced or iterative morphologies, but also functions for the
45% generation of many different types of kernel arrays from user supplied
46% arguments. Prehaps even the generation of a kernel from a small image.
47*/
48
49/*
50 Include declarations.
51*/
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/studio.h"
53#include "MagickCore/accelerate.h"
54#include "MagickCore/artifact.h"
cristy35f33492011-07-07 16:54:49 +000055#include "MagickCore/cache.h"
cristyd1dd6e42011-09-04 01:46:08 +000056#include "MagickCore/cache-private.h"
cristy4c08aed2011-07-01 19:47:50 +000057#include "MagickCore/cache-view.h"
58#include "MagickCore/color-private.h"
59#include "MagickCore/enhance.h"
60#include "MagickCore/exception.h"
61#include "MagickCore/exception-private.h"
62#include "MagickCore/gem.h"
63#include "MagickCore/hashmap.h"
64#include "MagickCore/image.h"
65#include "MagickCore/image-private.h"
66#include "MagickCore/list.h"
67#include "MagickCore/memory_.h"
68#include "MagickCore/monitor-private.h"
69#include "MagickCore/accelerate.h"
70#include "MagickCore/option.h"
71#include "MagickCore/pixel-accessor.h"
72#include "MagickCore/prepress.h"
73#include "MagickCore/quantize.h"
74#include "MagickCore/registry.h"
75#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"
cristy3f6d1482010-01-20 21:01:21 +000081
82/*
83%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84% %
85% %
86% %
87% A c c e l e r a t e C o n v o l v e I m a g e %
88% %
89% %
90% %
91%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92%
93% AccelerateConvolveImage() applies a custom convolution kernel to the image.
94% It is accelerated by taking advantage of speed-ups offered by executing in
95% concert across heterogeneous platforms consisting of CPUs, GPUs, and other
96% processors.
97%
98% The format of the AccelerateConvolveImage method is:
99%
100% Image *AccelerateConvolveImage(const Image *image,
cristy2be15382010-01-21 02:38:03 +0000101% const KernelInfo *kernel,Image *convolve_image,
cristy3f6d1482010-01-20 21:01:21 +0000102% ExceptionInfo *exception)
103%
104% A description of each parameter follows:
105%
106% o image: the image.
107%
108% o kernel: the convolution kernel.
109%
110% o convole_image: the convoleed image.
111%
112% o exception: return any errors or warnings in this structure.
113%
114*/
cristyd43a46b2010-01-21 02:13:41 +0000115
116#if defined(MAGICKCORE_OPENCL_SUPPORT)
117
118#if defined(MAGICKCORE_HDRI_SUPPORT)
119#define CLOptions "-DMAGICKCORE_HDRI_SUPPORT=1 -DCLQuantum=float " \
120 "-DCLPixelType=float4 -DQuantumRange=%g -DMagickEpsilon=%g"
cristy101ab702011-10-13 13:06:32 +0000121#define CLPixelInfo cl_float4
cristyd43a46b2010-01-21 02:13:41 +0000122#else
123#if (MAGICKCORE_QUANTUM_DEPTH == 8)
124#define CLOptions "-DCLQuantum=uchar -DCLPixelType=uchar4 " \
125 "-DQuantumRange=%g -DMagickEpsilon=%g"
cristy101ab702011-10-13 13:06:32 +0000126#define CLPixelInfo cl_uchar4
cristyd43a46b2010-01-21 02:13:41 +0000127#elif (MAGICKCORE_QUANTUM_DEPTH == 16)
128#define CLOptions "-DCLQuantum=ushort -DCLPixelType=ushort4 " \
129 "-DQuantumRange=%g -DMagickEpsilon=%g"
cristy101ab702011-10-13 13:06:32 +0000130#define CLPixelInfo cl_ushort4
cristyd43a46b2010-01-21 02:13:41 +0000131#elif (MAGICKCORE_QUANTUM_DEPTH == 32)
132#define CLOptions "-DCLQuantum=uint -DCLPixelType=uint4 " \
133 "-DQuantumRange=%g -DMagickEpsilon=%g"
cristy101ab702011-10-13 13:06:32 +0000134#define CLPixelInfo cl_uint4
cristy4434d7b2011-09-01 18:19:57 +0000135#elif (MAGICKCORE_QUANTUM_DEPTH == 64)
cristybb503372010-05-27 20:51:26 +0000136#define CLOptions "-DCLQuantum=ussize_t -DCLPixelType=ussize_t4 " \
cristyd43a46b2010-01-21 02:13:41 +0000137 "-DQuantumRange=%g -DMagickEpsilon=%g"
cristy101ab702011-10-13 13:06:32 +0000138#define CLPixelInfo cl_ulong4
cristyd43a46b2010-01-21 02:13:41 +0000139#endif
140#endif
141
142typedef struct _ConvolveInfo
143{
144 cl_context
145 context;
146
147 cl_device_id
148 *devices;
149
150 cl_command_queue
151 command_queue;
152
153 cl_kernel
154 kernel;
155
156 cl_program
157 program;
158
159 cl_mem
160 pixels,
161 convolve_pixels;
162
cristy5f959472010-05-27 22:19:46 +0000163 cl_ulong
cristyd43a46b2010-01-21 02:13:41 +0000164 width,
165 height;
166
cristy966032e2011-09-12 19:12:00 +0000167 cl_uint
cristyd43a46b2010-01-21 02:13:41 +0000168 matte;
169
170 cl_mem
171 filter;
172} ConvolveInfo;
173
cristy1daf0e82011-09-26 18:10:05 +0000174static const char
cristyd43a46b2010-01-21 02:13:41 +0000175 *ConvolveKernel =
cristy5f959472010-05-27 22:19:46 +0000176 "static inline long ClampToCanvas(const long offset,const unsigned long range)\n"
cristyd43a46b2010-01-21 02:13:41 +0000177 "{\n"
178 " if (offset < 0L)\n"
179 " return(0L);\n"
180 " if (offset >= range)\n"
cristy5f959472010-05-27 22:19:46 +0000181 " return((long) (range-1L));\n"
cristyd43a46b2010-01-21 02:13:41 +0000182 " return(offset);\n"
183 "}\n"
184 "\n"
cristy1a2e2762011-09-13 00:31:55 +0000185 "static inline CLQuantum ClampToQuantum(const float value)\n"
cristyd43a46b2010-01-21 02:13:41 +0000186 "{\n"
187 "#if defined(MAGICKCORE_HDRI_SUPPORT)\n"
cristyc9ab8902012-01-08 13:44:42 +0000188 " return((CLQuantum) value);\n"
cristyd43a46b2010-01-21 02:13:41 +0000189 "#else\n"
190 " if (value < 0.0)\n"
191 " return((CLQuantum) 0);\n"
cristy1a2e2762011-09-13 00:31:55 +0000192 " if (value >= (float) QuantumRange)\n"
cristyd43a46b2010-01-21 02:13:41 +0000193 " return((CLQuantum) QuantumRange);\n"
194 " return((CLQuantum) (value+0.5));\n"
195 "#endif\n"
196 "}\n"
197 "\n"
cristy2d5be002012-08-05 12:11:45 +0000198 "static inline float MagickEpsilonReciprocal(const float x)\n"
199 "{\n"
200 " float sign = x < (float) 0.0 ? (float) -1.0 : (float) 1.0;\n"
201 " return((sign*x) >= MagickEpsilon ? (float) 1.0/x : sign*((float) 1.0/\n"
202 " MagickEpsilon));\n"
203 "}\n"
204 "\n"
cristyd43a46b2010-01-21 02:13:41 +0000205 "__kernel void Convolve(const __global CLPixelType *input,\n"
cristy1a2e2762011-09-13 00:31:55 +0000206 " __constant float *filter,const unsigned long width,const unsigned long height,\n"
cristy966032e2011-09-12 19:12:00 +0000207 " const unsigned int matte,__global CLPixelType *output)\n"
cristyd43a46b2010-01-21 02:13:41 +0000208 "{\n"
cristy5f959472010-05-27 22:19:46 +0000209 " const unsigned long columns = get_global_size(0);\n"
210 " const unsigned long rows = get_global_size(1);\n"
cristyd43a46b2010-01-21 02:13:41 +0000211 "\n"
cristy5f959472010-05-27 22:19:46 +0000212 " const long x = get_global_id(0);\n"
213 " const long y = get_global_id(1);\n"
cristyd43a46b2010-01-21 02:13:41 +0000214 "\n"
cristy1a2e2762011-09-13 00:31:55 +0000215 " const float scale = (1.0/QuantumRange);\n"
cristy5f959472010-05-27 22:19:46 +0000216 " const long mid_width = (width-1)/2;\n"
217 " const long mid_height = (height-1)/2;\n"
cristy1a2e2762011-09-13 00:31:55 +0000218 " float4 sum = { 0.0, 0.0, 0.0, 0.0 };\n"
219 " float gamma = 0.0;\n"
cristy5f959472010-05-27 22:19:46 +0000220 " register unsigned long i = 0;\n"
cristyd43a46b2010-01-21 02:13:41 +0000221 "\n"
222 " int method = 0;\n"
223 " if (matte != false)\n"
224 " method=1;\n"
225 " if ((x >= width) && (x < (columns-width-1)) &&\n"
226 " (y >= height) && (y < (rows-height-1)))\n"
227 " {\n"
228 " method=2;\n"
229 " if (matte != false)\n"
230 " method=3;\n"
231 " }\n"
232 " switch (method)\n"
233 " {\n"
234 " case 0:\n"
235 " {\n"
cristy5f959472010-05-27 22:19:46 +0000236 " for (long v=(-mid_height); v <= mid_height; v++)\n"
cristyd43a46b2010-01-21 02:13:41 +0000237 " {\n"
cristy5f959472010-05-27 22:19:46 +0000238 " for (long u=(-mid_width); u <= mid_width; u++)\n"
cristyd43a46b2010-01-21 02:13:41 +0000239 " {\n"
cristy5f959472010-05-27 22:19:46 +0000240 " const long index=ClampToCanvas(y+v,rows)*columns+\n"
cristyd43a46b2010-01-21 02:13:41 +0000241 " ClampToCanvas(x+u,columns);\n"
242 " sum.x+=filter[i]*input[index].x;\n"
243 " sum.y+=filter[i]*input[index].y;\n"
244 " sum.z+=filter[i]*input[index].z;\n"
245 " gamma+=filter[i];\n"
246 " i++;\n"
247 " }\n"
248 " }\n"
249 " break;\n"
250 " }\n"
251 " case 1:\n"
252 " {\n"
cristy5f959472010-05-27 22:19:46 +0000253 " for (long v=(-mid_height); v <= mid_height; v++)\n"
cristyd43a46b2010-01-21 02:13:41 +0000254 " {\n"
cristy5f959472010-05-27 22:19:46 +0000255 " for (long u=(-mid_width); u <= mid_width; u++)\n"
cristyd43a46b2010-01-21 02:13:41 +0000256 " {\n"
cristy5f959472010-05-27 22:19:46 +0000257 " const unsigned long index=ClampToCanvas(y+v,rows)*columns+\n"
cristyd43a46b2010-01-21 02:13:41 +0000258 " ClampToCanvas(x+u,columns);\n"
cristy1a2e2762011-09-13 00:31:55 +0000259 " const float alpha=scale*input[index].w;\n"
cristyd43a46b2010-01-21 02:13:41 +0000260 " sum.x+=alpha*filter[i]*input[index].x;\n"
261 " sum.y+=alpha*filter[i]*input[index].y;\n"
262 " sum.z+=alpha*filter[i]*input[index].z;\n"
263 " sum.w+=filter[i]*input[index].w;\n"
264 " gamma+=alpha*filter[i];\n"
265 " i++;\n"
266 " }\n"
267 " }\n"
268 " break;\n"
269 " }\n"
270 " case 2:\n"
271 " {\n"
cristy5f959472010-05-27 22:19:46 +0000272 " for (long v=(-mid_height); v <= mid_height; v++)\n"
cristyd43a46b2010-01-21 02:13:41 +0000273 " {\n"
cristy5f959472010-05-27 22:19:46 +0000274 " for (long u=(-mid_width); u <= mid_width; u++)\n"
cristyd43a46b2010-01-21 02:13:41 +0000275 " {\n"
cristy5f959472010-05-27 22:19:46 +0000276 " const unsigned long index=(y+v)*columns+(x+u);\n"
cristyd43a46b2010-01-21 02:13:41 +0000277 " sum.x+=filter[i]*input[index].x;\n"
278 " sum.y+=filter[i]*input[index].y;\n"
279 " sum.z+=filter[i]*input[index].z;\n"
280 " gamma+=filter[i];\n"
281 " i++;\n"
282 " }\n"
283 " }\n"
284 " break;\n"
285 " }\n"
286 " case 3:\n"
287 " {\n"
cristy5f959472010-05-27 22:19:46 +0000288 " for (long v=(-mid_height); v <= mid_height; v++)\n"
cristyd43a46b2010-01-21 02:13:41 +0000289 " {\n"
cristy5f959472010-05-27 22:19:46 +0000290 " for (long u=(-mid_width); u <= mid_width; u++)\n"
cristyd43a46b2010-01-21 02:13:41 +0000291 " {\n"
cristy5f959472010-05-27 22:19:46 +0000292 " const unsigned long index=(y+v)*columns+(x+u);\n"
cristy1a2e2762011-09-13 00:31:55 +0000293 " const float alpha=scale*input[index].w;\n"
cristyd43a46b2010-01-21 02:13:41 +0000294 " sum.x+=alpha*filter[i]*input[index].x;\n"
295 " sum.y+=alpha*filter[i]*input[index].y;\n"
296 " sum.z+=alpha*filter[i]*input[index].z;\n"
297 " sum.w+=filter[i]*input[index].w;\n"
298 " gamma+=alpha*filter[i];\n"
299 " i++;\n"
300 " }\n"
301 " }\n"
302 " break;\n"
303 " }\n"
304 " }\n"
cristy2d5be002012-08-05 12:11:45 +0000305 " gamma=MagickEpsilonReciprocal(gamma);\n"
cristy5f959472010-05-27 22:19:46 +0000306 " const unsigned long index = y*columns+x;\n"
cristyd43a46b2010-01-21 02:13:41 +0000307 " output[index].x=ClampToQuantum(gamma*sum.x);\n"
308 " output[index].y=ClampToQuantum(gamma*sum.y);\n"
309 " output[index].z=ClampToQuantum(gamma*sum.z);\n"
310 " if (matte == false)\n"
311 " output[index].w=input[index].w;\n"
312 " else\n"
313 " output[index].w=ClampToQuantum(sum.w);\n"
314 "}\n";
315
316static void ConvolveNotify(const char *message,const void *data,size_t length,
317 void *user_context)
318{
319 ExceptionInfo
320 *exception;
321
322 (void) data;
323 (void) length;
324 exception=(ExceptionInfo *) user_context;
cristy32cca402010-01-23 04:02:23 +0000325 (void) ThrowMagickException(exception,GetMagickModule(),DelegateWarning,
anthonye5b39652012-04-21 05:37:29 +0000326 "DelegateFailed","'%s'",message);
cristyd43a46b2010-01-21 02:13:41 +0000327}
328
329static MagickBooleanType BindConvolveParameters(ConvolveInfo *convolve_info,
cristy1a2e2762011-09-13 00:31:55 +0000330 const Image *image,const void *pixels,float *filter,const size_t width,
cristyc8523c12011-09-13 00:02:53 +0000331 const size_t height,void *convolve_pixels)
cristyd43a46b2010-01-21 02:13:41 +0000332{
333 cl_int
334 status;
335
cristy5f959472010-05-27 22:19:46 +0000336 register cl_uint
cristyd43a46b2010-01-21 02:13:41 +0000337 i;
338
339 size_t
340 length;
341
342 /*
343 Allocate OpenCL buffers.
344 */
345 length=image->columns*image->rows;
cristy5f959472010-05-27 22:19:46 +0000346 convolve_info->pixels=clCreateBuffer(convolve_info->context,(cl_mem_flags)
cristy101ab702011-10-13 13:06:32 +0000347 (CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR),length*sizeof(CLPixelInfo),
cristy5f959472010-05-27 22:19:46 +0000348 (void *) pixels,&status);
cristyd43a46b2010-01-21 02:13:41 +0000349 if ((convolve_info->pixels == (cl_mem) NULL) || (status != CL_SUCCESS))
350 return(MagickFalse);
351 length=width*height;
cristy5f959472010-05-27 22:19:46 +0000352 convolve_info->filter=clCreateBuffer(convolve_info->context,(cl_mem_flags)
cristy1a2e2762011-09-13 00:31:55 +0000353 (CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR),length*sizeof(cl_float),filter,
cristy5f959472010-05-27 22:19:46 +0000354 &status);
cristyd43a46b2010-01-21 02:13:41 +0000355 if ((convolve_info->filter == (cl_mem) NULL) || (status != CL_SUCCESS))
356 return(MagickFalse);
357 length=image->columns*image->rows;
358 convolve_info->convolve_pixels=clCreateBuffer(convolve_info->context,
cristy5f959472010-05-27 22:19:46 +0000359 (cl_mem_flags) (CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR),length*
cristy101ab702011-10-13 13:06:32 +0000360 sizeof(CLPixelInfo),convolve_pixels,&status);
cristyd43a46b2010-01-21 02:13:41 +0000361 if ((convolve_info->convolve_pixels == (cl_mem) NULL) ||
362 (status != CL_SUCCESS))
363 return(MagickFalse);
364 /*
365 Bind OpenCL buffers.
366 */
367 i=0;
368 status=clSetKernelArg(convolve_info->kernel,i++,sizeof(cl_mem),(void *)
369 &convolve_info->pixels);
370 if (status != CL_SUCCESS)
371 return(MagickFalse);
372 status=clSetKernelArg(convolve_info->kernel,i++,sizeof(cl_mem),(void *)
373 &convolve_info->filter);
374 if (status != CL_SUCCESS)
375 return(MagickFalse);
cristy5f959472010-05-27 22:19:46 +0000376 convolve_info->width=(cl_ulong) width;
377 status=clSetKernelArg(convolve_info->kernel,i++,sizeof(cl_ulong),(void *)
cristyd43a46b2010-01-21 02:13:41 +0000378 &convolve_info->width);
379 if (status != CL_SUCCESS)
380 return(MagickFalse);
cristy5f959472010-05-27 22:19:46 +0000381 convolve_info->height=(cl_ulong) height;
382 status=clSetKernelArg(convolve_info->kernel,i++,sizeof(cl_ulong),(void *)
cristyd43a46b2010-01-21 02:13:41 +0000383 &convolve_info->height);
384 if (status != CL_SUCCESS)
385 return(MagickFalse);
cristy966032e2011-09-12 19:12:00 +0000386 convolve_info->matte=(cl_uint) image->matte;
387 status=clSetKernelArg(convolve_info->kernel,i++,sizeof(cl_uint),(void *)
cristyd43a46b2010-01-21 02:13:41 +0000388 &convolve_info->matte);
389 if (status != CL_SUCCESS)
390 return(MagickFalse);
391 status=clSetKernelArg(convolve_info->kernel,i++,sizeof(cl_mem),(void *)
392 &convolve_info->convolve_pixels);
393 if (status != CL_SUCCESS)
394 return(MagickFalse);
395 status=clFinish(convolve_info->command_queue);
396 if (status != CL_SUCCESS)
397 return(MagickFalse);
398 return(MagickTrue);
399}
400
401static void DestroyConvolveBuffers(ConvolveInfo *convolve_info)
402{
403 cl_int
404 status;
405
cristy9f027d12011-09-21 01:17:17 +0000406 status=0;
cristyd43a46b2010-01-21 02:13:41 +0000407 if (convolve_info->convolve_pixels != (cl_mem) NULL)
408 status=clReleaseMemObject(convolve_info->convolve_pixels);
409 if (convolve_info->pixels != (cl_mem) NULL)
410 status=clReleaseMemObject(convolve_info->pixels);
411 if (convolve_info->filter != (cl_mem) NULL)
412 status=clReleaseMemObject(convolve_info->filter);
cristyaa83c2c2011-09-21 13:36:25 +0000413 (void) status;
cristyd43a46b2010-01-21 02:13:41 +0000414}
415
416static ConvolveInfo *DestroyConvolveInfo(ConvolveInfo *convolve_info)
417{
418 cl_int
419 status;
420
cristy9f027d12011-09-21 01:17:17 +0000421 status=0;
cristyd43a46b2010-01-21 02:13:41 +0000422 if (convolve_info->kernel != (cl_kernel) NULL)
423 status=clReleaseKernel(convolve_info->kernel);
424 if (convolve_info->program != (cl_program) NULL)
425 status=clReleaseProgram(convolve_info->program);
426 if (convolve_info->command_queue != (cl_command_queue) NULL)
427 status=clReleaseCommandQueue(convolve_info->command_queue);
428 if (convolve_info->context != (cl_context) NULL)
429 status=clReleaseContext(convolve_info->context);
cristyaa83c2c2011-09-21 13:36:25 +0000430 (void) status;
cristyd43a46b2010-01-21 02:13:41 +0000431 convolve_info=(ConvolveInfo *) RelinquishMagickMemory(convolve_info);
432 return(convolve_info);
433}
434
435static MagickBooleanType EnqueueConvolveKernel(ConvolveInfo *convolve_info,
cristy1a2e2762011-09-13 00:31:55 +0000436 const Image *image,const void *pixels,float *filter,const size_t width,
cristyc8523c12011-09-13 00:02:53 +0000437 const size_t height,void *convolve_pixels)
cristyd43a46b2010-01-21 02:13:41 +0000438{
439 cl_int
440 status;
441
442 size_t
443 global_work_size[2],
444 length;
445
446 length=image->columns*image->rows;
447 status=clEnqueueWriteBuffer(convolve_info->command_queue,
cristy101ab702011-10-13 13:06:32 +0000448 convolve_info->pixels,CL_TRUE,0,length*sizeof(CLPixelInfo),pixels,0,NULL,
cristyd43a46b2010-01-21 02:13:41 +0000449 NULL);
450 length=width*height;
451 status=clEnqueueWriteBuffer(convolve_info->command_queue,
cristy1a2e2762011-09-13 00:31:55 +0000452 convolve_info->filter,CL_TRUE,0,length*sizeof(cl_float),filter,0,NULL,
cristyd43a46b2010-01-21 02:13:41 +0000453 NULL);
454 if (status != CL_SUCCESS)
455 return(MagickFalse);
456 global_work_size[0]=image->columns;
457 global_work_size[1]=image->rows;
458 status=clEnqueueNDRangeKernel(convolve_info->command_queue,
459 convolve_info->kernel,2,NULL,global_work_size,NULL,0,NULL,NULL);
460 if (status != CL_SUCCESS)
461 return(MagickFalse);
462 length=image->columns*image->rows;
463 status=clEnqueueReadBuffer(convolve_info->command_queue,
cristy101ab702011-10-13 13:06:32 +0000464 convolve_info->convolve_pixels,CL_TRUE,0,length*sizeof(CLPixelInfo),
cristyd43a46b2010-01-21 02:13:41 +0000465 convolve_pixels,0,NULL,NULL);
466 if (status != CL_SUCCESS)
467 return(MagickFalse);
468 status=clFinish(convolve_info->command_queue);
469 if (status != CL_SUCCESS)
470 return(MagickFalse);
471 return(MagickTrue);
472}
473
474static ConvolveInfo *GetConvolveInfo(const Image *image,const char *name,
475 const char *source,ExceptionInfo *exception)
476{
477 char
478 options[MaxTextExtent];
479
cristy966032e2011-09-12 19:12:00 +0000480 cl_context_properties
481 context_properties[3];
482
cristyd43a46b2010-01-21 02:13:41 +0000483 cl_int
484 status;
485
cristy966032e2011-09-12 19:12:00 +0000486 cl_platform_id
487 platforms[1];
488
489 cl_uint
490 number_platforms;
491
cristyd43a46b2010-01-21 02:13:41 +0000492 ConvolveInfo
493 *convolve_info;
494
495 size_t
496 length,
497 lengths[] = { strlen(source) };
498
499 /*
500 Create OpenCL info.
501 */
cristy73bd4a52010-10-05 11:24:23 +0000502 convolve_info=(ConvolveInfo *) AcquireMagickMemory(sizeof(*convolve_info));
cristyd43a46b2010-01-21 02:13:41 +0000503 if (convolve_info == (ConvolveInfo *) NULL)
504 {
505 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000506 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
cristyd43a46b2010-01-21 02:13:41 +0000507 return((ConvolveInfo *) NULL);
508 }
509 (void) ResetMagickMemory(convolve_info,0,sizeof(*convolve_info));
510 /*
511 Create OpenCL context.
512 */
cristy61b76e62011-09-13 12:04:12 +0000513 status=clGetPlatformIDs(0,(cl_platform_id *) NULL,&number_platforms);
514 if ((status == CL_SUCCESS) && (number_platforms > 0))
cristy966032e2011-09-12 19:12:00 +0000515 status=clGetPlatformIDs(1,platforms,NULL);
516 if (status != CL_SUCCESS)
517 {
518 (void) ThrowMagickException(exception,GetMagickModule(),DelegateWarning,
anthonye5b39652012-04-21 05:37:29 +0000519 "failed to create OpenCL context","'%s' (%d)",image->filename,status);
cristy966032e2011-09-12 19:12:00 +0000520 convolve_info=DestroyConvolveInfo(convolve_info);
521 return((ConvolveInfo *) NULL);
522 }
523 context_properties[0]=CL_CONTEXT_PLATFORM;
524 context_properties[1]=(cl_context_properties) platforms[0];
525 context_properties[2]=0;
526 convolve_info->context=clCreateContextFromType(context_properties,
527 (cl_device_type) CL_DEVICE_TYPE_GPU,ConvolveNotify,exception,&status);
cristyd43a46b2010-01-21 02:13:41 +0000528 if ((convolve_info->context == (cl_context) NULL) || (status != CL_SUCCESS))
cristy966032e2011-09-12 19:12:00 +0000529 convolve_info->context=clCreateContextFromType(context_properties,
530 (cl_device_type) CL_DEVICE_TYPE_CPU,ConvolveNotify,exception,&status);
cristyd43a46b2010-01-21 02:13:41 +0000531 if ((convolve_info->context == (cl_context) NULL) || (status != CL_SUCCESS))
cristy966032e2011-09-12 19:12:00 +0000532 convolve_info->context=clCreateContextFromType(context_properties,
533 (cl_device_type) CL_DEVICE_TYPE_DEFAULT,ConvolveNotify,exception,&status);
cristyd43a46b2010-01-21 02:13:41 +0000534 if ((convolve_info->context == (cl_context) NULL) || (status != CL_SUCCESS))
535 {
cristy32cca402010-01-23 04:02:23 +0000536 (void) ThrowMagickException(exception,GetMagickModule(),DelegateWarning,
anthonye5b39652012-04-21 05:37:29 +0000537 "failed to create OpenCL context","'%s' (%d)",image->filename,status);
cristy5f959472010-05-27 22:19:46 +0000538 convolve_info=DestroyConvolveInfo(convolve_info);
cristyd43a46b2010-01-21 02:13:41 +0000539 return((ConvolveInfo *) NULL);
540 }
541 /*
542 Detect OpenCL devices.
543 */
544 status=clGetContextInfo(convolve_info->context,CL_CONTEXT_DEVICES,0,NULL,
545 &length);
546 if ((status != CL_SUCCESS) || (length == 0))
547 {
cristy5f959472010-05-27 22:19:46 +0000548 convolve_info=DestroyConvolveInfo(convolve_info);
cristyd43a46b2010-01-21 02:13:41 +0000549 return((ConvolveInfo *) NULL);
550 }
551 convolve_info->devices=(cl_device_id *) AcquireMagickMemory(length);
552 if (convolve_info->devices == (cl_device_id *) NULL)
553 {
554 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000555 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
cristy5f959472010-05-27 22:19:46 +0000556 convolve_info=DestroyConvolveInfo(convolve_info);
cristyd43a46b2010-01-21 02:13:41 +0000557 return((ConvolveInfo *) NULL);
558 }
559 status=clGetContextInfo(convolve_info->context,CL_CONTEXT_DEVICES,length,
560 convolve_info->devices,NULL);
561 if (status != CL_SUCCESS)
562 {
cristy5f959472010-05-27 22:19:46 +0000563 convolve_info=DestroyConvolveInfo(convolve_info);
cristyd43a46b2010-01-21 02:13:41 +0000564 return((ConvolveInfo *) NULL);
565 }
cristydc9c80d2011-10-26 23:47:02 +0000566 if (image->debug != MagickFalse)
567 {
568 char
569 attribute[MaxTextExtent];
570
571 size_t
572 length;
573
574 clGetDeviceInfo(convolve_info->devices[0],CL_DEVICE_NAME,
575 sizeof(attribute),attribute,&length);
576 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),"Name: %s",
577 attribute);
578 clGetDeviceInfo(convolve_info->devices[0],CL_DEVICE_VENDOR,
579 sizeof(attribute),attribute,&length);
580 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),"Vendor: %s",
581 attribute);
582 clGetDeviceInfo(convolve_info->devices[0],CL_DEVICE_VERSION,
583 sizeof(attribute),attribute,&length);
584 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),
585 "Driver Version: %s",attribute);
586 clGetDeviceInfo(convolve_info->devices[0],CL_DEVICE_PROFILE,
587 sizeof(attribute),attribute,&length);
588 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),"Profile: %s",
589 attribute);
590 clGetDeviceInfo(convolve_info->devices[0],CL_DRIVER_VERSION,
591 sizeof(attribute),attribute,&length);
592 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),"Driver: %s",
593 attribute);
594 clGetDeviceInfo(convolve_info->devices[0],CL_DEVICE_EXTENSIONS,
595 sizeof(attribute),attribute,&length);
596 (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),"Extensions: %s",
597 attribute);
598 }
cristyd43a46b2010-01-21 02:13:41 +0000599 /*
600 Create OpenCL command queue.
601 */
602 convolve_info->command_queue=clCreateCommandQueue(convolve_info->context,
603 convolve_info->devices[0],0,&status);
604 if ((convolve_info->command_queue == (cl_command_queue) NULL) ||
605 (status != CL_SUCCESS))
606 {
cristy5f959472010-05-27 22:19:46 +0000607 convolve_info=DestroyConvolveInfo(convolve_info);
cristyd43a46b2010-01-21 02:13:41 +0000608 return((ConvolveInfo *) NULL);
609 }
610 /*
611 Build OpenCL program.
612 */
613 convolve_info->program=clCreateProgramWithSource(convolve_info->context,1,
614 &source,lengths,&status);
615 if ((convolve_info->program == (cl_program) NULL) || (status != CL_SUCCESS))
616 {
cristy5f959472010-05-27 22:19:46 +0000617 convolve_info=DestroyConvolveInfo(convolve_info);
cristyd43a46b2010-01-21 02:13:41 +0000618 return((ConvolveInfo *) NULL);
619 }
cristy1a2e2762011-09-13 00:31:55 +0000620 (void) FormatLocaleString(options,MaxTextExtent,CLOptions,(float)
cristyd43a46b2010-01-21 02:13:41 +0000621 QuantumRange,MagickEpsilon);
cristy00243d12010-01-21 02:45:27 +0000622 status=clBuildProgram(convolve_info->program,1,convolve_info->devices,options,
623 NULL,NULL);
cristyd43a46b2010-01-21 02:13:41 +0000624 if ((convolve_info->program == (cl_program) NULL) || (status != CL_SUCCESS))
625 {
626 char
627 *log;
628
629 status=clGetProgramBuildInfo(convolve_info->program,
630 convolve_info->devices[0],CL_PROGRAM_BUILD_LOG,0,NULL,&length);
631 log=(char *) AcquireMagickMemory(length);
632 if (log == (char *) NULL)
633 {
cristy5f959472010-05-27 22:19:46 +0000634 convolve_info=DestroyConvolveInfo(convolve_info);
cristyd43a46b2010-01-21 02:13:41 +0000635 return((ConvolveInfo *) NULL);
636 }
637 status=clGetProgramBuildInfo(convolve_info->program,
638 convolve_info->devices[0],CL_PROGRAM_BUILD_LOG,length,log,&length);
cristy32cca402010-01-23 04:02:23 +0000639 (void) ThrowMagickException(exception,GetMagickModule(),DelegateWarning,
anthonye5b39652012-04-21 05:37:29 +0000640 "failed to build OpenCL program","'%s' (%s)",image->filename,log);
cristyd43a46b2010-01-21 02:13:41 +0000641 log=DestroyString(log);
cristy5f959472010-05-27 22:19:46 +0000642 convolve_info=DestroyConvolveInfo(convolve_info);
cristyd43a46b2010-01-21 02:13:41 +0000643 return((ConvolveInfo *) NULL);
644 }
645 /*
646 Get a kernel object.
647 */
648 convolve_info->kernel=clCreateKernel(convolve_info->program,name,&status);
649 if ((convolve_info->kernel == (cl_kernel) NULL) || (status != CL_SUCCESS))
650 {
cristy5f959472010-05-27 22:19:46 +0000651 convolve_info=DestroyConvolveInfo(convolve_info);
cristyd43a46b2010-01-21 02:13:41 +0000652 return((ConvolveInfo *) NULL);
653 }
654 return(convolve_info);
655}
656
657#endif
658
cristy3f6d1482010-01-20 21:01:21 +0000659MagickExport MagickBooleanType AccelerateConvolveImage(const Image *image,
cristy2be15382010-01-21 02:38:03 +0000660 const KernelInfo *kernel,Image *convolve_image,ExceptionInfo *exception)
cristy3f6d1482010-01-20 21:01:21 +0000661{
662 assert(image != (Image *) NULL);
663 assert(image->signature == MagickSignature);
664 if (image->debug != MagickFalse)
665 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy2be15382010-01-21 02:38:03 +0000666 assert(kernel != (KernelInfo *) NULL);
cristyd43a46b2010-01-21 02:13:41 +0000667 assert(kernel->signature == MagickSignature);
cristy3f6d1482010-01-20 21:01:21 +0000668 assert(convolve_image != (Image *) NULL);
669 assert(convolve_image->signature == MagickSignature);
670 assert(exception != (ExceptionInfo *) NULL);
671 assert(exception->signature == MagickSignature);
cristy394651a2010-01-23 21:05:55 +0000672 if ((image->storage_class != DirectClass) ||
673 (image->colorspace == CMYKColorspace))
cristybfa02fa2011-09-13 00:40:44 +0000674 return(MagickFalse);
cristyd43a46b2010-01-21 02:13:41 +0000675 if ((GetImageVirtualPixelMethod(image) != UndefinedVirtualPixelMethod) &&
676 (GetImageVirtualPixelMethod(image) != EdgeVirtualPixelMethod))
677 return(MagickFalse);
cristyb572edf2011-09-13 12:25:22 +0000678 if (GetPixelChannels(image) != 4)
679 return(MagickFalse);
cristyd43a46b2010-01-21 02:13:41 +0000680#if !defined(MAGICKCORE_OPENCL_SUPPORT)
cristy3f6d1482010-01-20 21:01:21 +0000681 return(MagickFalse);
cristyd43a46b2010-01-21 02:13:41 +0000682#else
683 {
684 const void
685 *pixels;
686
cristy1a2e2762011-09-13 00:31:55 +0000687 float
688 *filter;
689
cristyd43a46b2010-01-21 02:13:41 +0000690 ConvolveInfo
691 *convolve_info;
692
693 MagickBooleanType
694 status;
695
696 MagickSizeType
697 length;
698
cristy1a2e2762011-09-13 00:31:55 +0000699 register ssize_t
700 i;
701
cristyd43a46b2010-01-21 02:13:41 +0000702 void
703 *convolve_pixels;
704
cristyd43a46b2010-01-21 02:13:41 +0000705 convolve_info=GetConvolveInfo(image,"Convolve",ConvolveKernel,exception);
706 if (convolve_info == (ConvolveInfo *) NULL)
707 return(MagickFalse);
708 pixels=AcquirePixelCachePixels(image,&length,exception);
709 if (pixels == (const void *) NULL)
710 {
cristy1a2e2762011-09-13 00:31:55 +0000711 convolve_info=DestroyConvolveInfo(convolve_info);
cristyd43a46b2010-01-21 02:13:41 +0000712 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
anthonye5b39652012-04-21 05:37:29 +0000713 "UnableToReadPixelCache","'%s'",image->filename);
cristyd43a46b2010-01-21 02:13:41 +0000714 return(MagickFalse);
715 }
716 convolve_pixels=GetPixelCachePixels(convolve_image,&length,exception);
717 if (convolve_pixels == (void *) NULL)
718 {
cristy1a2e2762011-09-13 00:31:55 +0000719 convolve_info=DestroyConvolveInfo(convolve_info);
cristyd43a46b2010-01-21 02:13:41 +0000720 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
anthonye5b39652012-04-21 05:37:29 +0000721 "UnableToReadPixelCache","'%s'",image->filename);
cristyd43a46b2010-01-21 02:13:41 +0000722 return(MagickFalse);
723 }
cristy1a2e2762011-09-13 00:31:55 +0000724 filter=(float *) AcquireQuantumMemory(kernel->width,kernel->height*
725 sizeof(*filter));
726 if (filter == (float *) NULL)
727 {
728 DestroyConvolveBuffers(convolve_info);
729 convolve_info=DestroyConvolveInfo(convolve_info);
730 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000731 ResourceLimitError,"MemoryAllocationFailed","'%s'",image->filename);
cristy1a2e2762011-09-13 00:31:55 +0000732 return(MagickFalse);
733 }
734 for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++)
735 filter[i]=(float) kernel->values[i];
736 status=BindConvolveParameters(convolve_info,image,pixels,filter,
cristyd43a46b2010-01-21 02:13:41 +0000737 kernel->width,kernel->height,convolve_pixels);
738 if (status == MagickFalse)
739 {
cristy1a2e2762011-09-13 00:31:55 +0000740 filter=(float *) RelinquishMagickMemory(filter);
cristyd43a46b2010-01-21 02:13:41 +0000741 DestroyConvolveBuffers(convolve_info);
742 convolve_info=DestroyConvolveInfo(convolve_info);
743 return(MagickFalse);
744 }
cristy1a2e2762011-09-13 00:31:55 +0000745 status=EnqueueConvolveKernel(convolve_info,image,pixels,filter,
cristyd43a46b2010-01-21 02:13:41 +0000746 kernel->width,kernel->height,convolve_pixels);
cristy1a2e2762011-09-13 00:31:55 +0000747 filter=(float *) RelinquishMagickMemory(filter);
cristyd43a46b2010-01-21 02:13:41 +0000748 if (status == MagickFalse)
749 {
750 DestroyConvolveBuffers(convolve_info);
751 convolve_info=DestroyConvolveInfo(convolve_info);
752 return(MagickFalse);
753 }
754 DestroyConvolveBuffers(convolve_info);
755 convolve_info=DestroyConvolveInfo(convolve_info);
756 return(MagickTrue);
757 }
758#endif
cristy3f6d1482010-01-20 21:01:21 +0000759}