cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 1 | /* |
| 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 % |
cristy | 0d127ab | 2010-05-14 23:29:46 +0000 | [diff] [blame] | 16 | % John Cristy % |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 17 | % January 2010 % |
| 18 | % % |
| 19 | % % |
cristy | fe676ee | 2013-11-18 13:03:38 +0000 | [diff] [blame] | 20 | % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 21 | % 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 | % |
cristy | 0d127ab | 2010-05-14 23:29:46 +0000 | [diff] [blame] | 36 | % Morphology is the the application of various kernals, of any size and even |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 37 | % 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 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 52 | #include "MagickCore/studio.h" |
| 53 | #include "MagickCore/accelerate.h" |
| 54 | #include "MagickCore/artifact.h" |
cristy | 35f3349 | 2011-07-07 16:54:49 +0000 | [diff] [blame] | 55 | #include "MagickCore/cache.h" |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 56 | #include "MagickCore/cache-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 57 | #include "MagickCore/cache-view.h" |
| 58 | #include "MagickCore/color-private.h" |
cristy | 7f86684 | 2013-07-11 01:15:58 +0000 | [diff] [blame] | 59 | #include "MagickCore/delegate-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 60 | #include "MagickCore/enhance.h" |
| 61 | #include "MagickCore/exception.h" |
| 62 | #include "MagickCore/exception-private.h" |
| 63 | #include "MagickCore/gem.h" |
| 64 | #include "MagickCore/hashmap.h" |
| 65 | #include "MagickCore/image.h" |
| 66 | #include "MagickCore/image-private.h" |
| 67 | #include "MagickCore/list.h" |
| 68 | #include "MagickCore/memory_.h" |
| 69 | #include "MagickCore/monitor-private.h" |
| 70 | #include "MagickCore/accelerate.h" |
| 71 | #include "MagickCore/option.h" |
| 72 | #include "MagickCore/pixel-accessor.h" |
| 73 | #include "MagickCore/prepress.h" |
| 74 | #include "MagickCore/quantize.h" |
| 75 | #include "MagickCore/registry.h" |
| 76 | #include "MagickCore/semaphore.h" |
| 77 | #include "MagickCore/splay-tree.h" |
| 78 | #include "MagickCore/statistic.h" |
| 79 | #include "MagickCore/string_.h" |
| 80 | #include "MagickCore/string-private.h" |
| 81 | #include "MagickCore/token.h" |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 85 | % % |
| 86 | % % |
| 87 | % % |
| 88 | % A c c e l e r a t e C o n v o l v e I m a g e % |
| 89 | % % |
| 90 | % % |
| 91 | % % |
| 92 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 93 | % |
| 94 | % AccelerateConvolveImage() applies a custom convolution kernel to the image. |
| 95 | % It is accelerated by taking advantage of speed-ups offered by executing in |
| 96 | % concert across heterogeneous platforms consisting of CPUs, GPUs, and other |
| 97 | % processors. |
| 98 | % |
| 99 | % The format of the AccelerateConvolveImage method is: |
| 100 | % |
| 101 | % Image *AccelerateConvolveImage(const Image *image, |
cristy | 2be1538 | 2010-01-21 02:38:03 +0000 | [diff] [blame] | 102 | % const KernelInfo *kernel,Image *convolve_image, |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 103 | % ExceptionInfo *exception) |
| 104 | % |
| 105 | % A description of each parameter follows: |
| 106 | % |
| 107 | % o image: the image. |
| 108 | % |
| 109 | % o kernel: the convolution kernel. |
| 110 | % |
| 111 | % o convole_image: the convoleed image. |
| 112 | % |
| 113 | % o exception: return any errors or warnings in this structure. |
| 114 | % |
| 115 | */ |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 116 | |
| 117 | #if defined(MAGICKCORE_OPENCL_SUPPORT) |
| 118 | |
| 119 | #if defined(MAGICKCORE_HDRI_SUPPORT) |
| 120 | #define CLOptions "-DMAGICKCORE_HDRI_SUPPORT=1 -DCLQuantum=float " \ |
| 121 | "-DCLPixelType=float4 -DQuantumRange=%g -DMagickEpsilon=%g" |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 122 | #define CLPixelInfo cl_float4 |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 123 | #else |
| 124 | #if (MAGICKCORE_QUANTUM_DEPTH == 8) |
| 125 | #define CLOptions "-DCLQuantum=uchar -DCLPixelType=uchar4 " \ |
| 126 | "-DQuantumRange=%g -DMagickEpsilon=%g" |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 127 | #define CLPixelInfo cl_uchar4 |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 128 | #elif (MAGICKCORE_QUANTUM_DEPTH == 16) |
| 129 | #define CLOptions "-DCLQuantum=ushort -DCLPixelType=ushort4 " \ |
| 130 | "-DQuantumRange=%g -DMagickEpsilon=%g" |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 131 | #define CLPixelInfo cl_ushort4 |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 132 | #elif (MAGICKCORE_QUANTUM_DEPTH == 32) |
| 133 | #define CLOptions "-DCLQuantum=uint -DCLPixelType=uint4 " \ |
| 134 | "-DQuantumRange=%g -DMagickEpsilon=%g" |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 135 | #define CLPixelInfo cl_uint4 |
cristy | 4434d7b | 2011-09-01 18:19:57 +0000 | [diff] [blame] | 136 | #elif (MAGICKCORE_QUANTUM_DEPTH == 64) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 137 | #define CLOptions "-DCLQuantum=ussize_t -DCLPixelType=ussize_t4 " \ |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 138 | "-DQuantumRange=%g -DMagickEpsilon=%g" |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 139 | #define CLPixelInfo cl_ulong4 |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 140 | #endif |
| 141 | #endif |
| 142 | |
| 143 | typedef struct _ConvolveInfo |
| 144 | { |
| 145 | cl_context |
| 146 | context; |
| 147 | |
| 148 | cl_device_id |
| 149 | *devices; |
| 150 | |
| 151 | cl_command_queue |
| 152 | command_queue; |
| 153 | |
| 154 | cl_kernel |
| 155 | kernel; |
| 156 | |
| 157 | cl_program |
| 158 | program; |
| 159 | |
| 160 | cl_mem |
| 161 | pixels, |
| 162 | convolve_pixels; |
| 163 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 164 | cl_ulong |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 165 | width, |
| 166 | height; |
| 167 | |
cristy | 966032e | 2011-09-12 19:12:00 +0000 | [diff] [blame] | 168 | cl_uint |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 169 | matte; |
| 170 | |
| 171 | cl_mem |
| 172 | filter; |
| 173 | } ConvolveInfo; |
| 174 | |
cristy | 1daf0e8 | 2011-09-26 18:10:05 +0000 | [diff] [blame] | 175 | static const char |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 176 | *ConvolveKernel = |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 177 | "static inline long ClampToCanvas(const long offset,const unsigned long range)\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 178 | "{\n" |
| 179 | " if (offset < 0L)\n" |
| 180 | " return(0L);\n" |
| 181 | " if (offset >= range)\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 182 | " return((long) (range-1L));\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 183 | " return(offset);\n" |
| 184 | "}\n" |
| 185 | "\n" |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 186 | "static inline CLQuantum ClampToQuantum(const float value)\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 187 | "{\n" |
| 188 | "#if defined(MAGICKCORE_HDRI_SUPPORT)\n" |
cristy | c9ab890 | 2012-01-08 13:44:42 +0000 | [diff] [blame] | 189 | " return((CLQuantum) value);\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 190 | "#else\n" |
| 191 | " if (value < 0.0)\n" |
| 192 | " return((CLQuantum) 0);\n" |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 193 | " if (value >= (float) QuantumRange)\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 194 | " return((CLQuantum) QuantumRange);\n" |
| 195 | " return((CLQuantum) (value+0.5));\n" |
| 196 | "#endif\n" |
| 197 | "}\n" |
| 198 | "\n" |
cristy | 3e3ec3a | 2012-11-03 23:11:06 +0000 | [diff] [blame] | 199 | "static inline float PerceptibleReciprocal(const float x)\n" |
cristy | 2d5be00 | 2012-08-05 12:11:45 +0000 | [diff] [blame] | 200 | "{\n" |
| 201 | " float sign = x < (float) 0.0 ? (float) -1.0 : (float) 1.0;\n" |
| 202 | " return((sign*x) >= MagickEpsilon ? (float) 1.0/x : sign*((float) 1.0/\n" |
| 203 | " MagickEpsilon));\n" |
| 204 | "}\n" |
| 205 | "\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 206 | "__kernel void Convolve(const __global CLPixelType *input,\n" |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 207 | " __constant float *filter,const unsigned long width,const unsigned long height,\n" |
cristy | 966032e | 2011-09-12 19:12:00 +0000 | [diff] [blame] | 208 | " const unsigned int matte,__global CLPixelType *output)\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 209 | "{\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 210 | " const unsigned long columns = get_global_size(0);\n" |
| 211 | " const unsigned long rows = get_global_size(1);\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 212 | "\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 213 | " const long x = get_global_id(0);\n" |
| 214 | " const long y = get_global_id(1);\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 215 | "\n" |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 216 | " const float scale = (1.0/QuantumRange);\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 217 | " const long mid_width = (width-1)/2;\n" |
| 218 | " const long mid_height = (height-1)/2;\n" |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 219 | " float4 sum = { 0.0, 0.0, 0.0, 0.0 };\n" |
| 220 | " float gamma = 0.0;\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 221 | " register unsigned long i = 0;\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 222 | "\n" |
| 223 | " int method = 0;\n" |
| 224 | " if (matte != false)\n" |
| 225 | " method=1;\n" |
| 226 | " if ((x >= width) && (x < (columns-width-1)) &&\n" |
| 227 | " (y >= height) && (y < (rows-height-1)))\n" |
| 228 | " {\n" |
| 229 | " method=2;\n" |
| 230 | " if (matte != false)\n" |
| 231 | " method=3;\n" |
| 232 | " }\n" |
| 233 | " switch (method)\n" |
| 234 | " {\n" |
| 235 | " case 0:\n" |
| 236 | " {\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 237 | " for (long v=(-mid_height); v <= mid_height; v++)\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 238 | " {\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 239 | " for (long u=(-mid_width); u <= mid_width; u++)\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 240 | " {\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 241 | " const long index=ClampToCanvas(y+v,rows)*columns+\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 242 | " ClampToCanvas(x+u,columns);\n" |
| 243 | " sum.x+=filter[i]*input[index].x;\n" |
| 244 | " sum.y+=filter[i]*input[index].y;\n" |
| 245 | " sum.z+=filter[i]*input[index].z;\n" |
| 246 | " gamma+=filter[i];\n" |
| 247 | " i++;\n" |
| 248 | " }\n" |
| 249 | " }\n" |
| 250 | " break;\n" |
| 251 | " }\n" |
| 252 | " case 1:\n" |
| 253 | " {\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 254 | " for (long v=(-mid_height); v <= mid_height; v++)\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 255 | " {\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 256 | " for (long u=(-mid_width); u <= mid_width; u++)\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 257 | " {\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 258 | " const unsigned long index=ClampToCanvas(y+v,rows)*columns+\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 259 | " ClampToCanvas(x+u,columns);\n" |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 260 | " const float alpha=scale*input[index].w;\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 261 | " sum.x+=alpha*filter[i]*input[index].x;\n" |
| 262 | " sum.y+=alpha*filter[i]*input[index].y;\n" |
| 263 | " sum.z+=alpha*filter[i]*input[index].z;\n" |
| 264 | " sum.w+=filter[i]*input[index].w;\n" |
| 265 | " gamma+=alpha*filter[i];\n" |
| 266 | " i++;\n" |
| 267 | " }\n" |
| 268 | " }\n" |
| 269 | " break;\n" |
| 270 | " }\n" |
| 271 | " case 2:\n" |
| 272 | " {\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 273 | " for (long v=(-mid_height); v <= mid_height; v++)\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 274 | " {\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 275 | " for (long u=(-mid_width); u <= mid_width; u++)\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 276 | " {\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 277 | " const unsigned long index=(y+v)*columns+(x+u);\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 278 | " sum.x+=filter[i]*input[index].x;\n" |
| 279 | " sum.y+=filter[i]*input[index].y;\n" |
| 280 | " sum.z+=filter[i]*input[index].z;\n" |
| 281 | " gamma+=filter[i];\n" |
| 282 | " i++;\n" |
| 283 | " }\n" |
| 284 | " }\n" |
| 285 | " break;\n" |
| 286 | " }\n" |
| 287 | " case 3:\n" |
| 288 | " {\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 289 | " for (long v=(-mid_height); v <= mid_height; v++)\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 290 | " {\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 291 | " for (long u=(-mid_width); u <= mid_width; u++)\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 292 | " {\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 293 | " const unsigned long index=(y+v)*columns+(x+u);\n" |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 294 | " const float alpha=scale*input[index].w;\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 295 | " sum.x+=alpha*filter[i]*input[index].x;\n" |
| 296 | " sum.y+=alpha*filter[i]*input[index].y;\n" |
| 297 | " sum.z+=alpha*filter[i]*input[index].z;\n" |
| 298 | " sum.w+=filter[i]*input[index].w;\n" |
| 299 | " gamma+=alpha*filter[i];\n" |
| 300 | " i++;\n" |
| 301 | " }\n" |
| 302 | " }\n" |
| 303 | " break;\n" |
| 304 | " }\n" |
| 305 | " }\n" |
cristy | 3e3ec3a | 2012-11-03 23:11:06 +0000 | [diff] [blame] | 306 | " gamma=PerceptibleReciprocal(gamma);\n" |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 307 | " const unsigned long index = y*columns+x;\n" |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 308 | " output[index].x=ClampToQuantum(gamma*sum.x);\n" |
| 309 | " output[index].y=ClampToQuantum(gamma*sum.y);\n" |
| 310 | " output[index].z=ClampToQuantum(gamma*sum.z);\n" |
| 311 | " if (matte == false)\n" |
| 312 | " output[index].w=input[index].w;\n" |
| 313 | " else\n" |
| 314 | " output[index].w=ClampToQuantum(sum.w);\n" |
| 315 | "}\n"; |
| 316 | |
cristy | 7f86684 | 2013-07-11 01:15:58 +0000 | [diff] [blame] | 317 | static MagickDLLCall void ConvolveNotify(const char *message,const void *data, |
| 318 | size_t length,void *user_context) |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 319 | { |
| 320 | ExceptionInfo |
| 321 | *exception; |
| 322 | |
| 323 | (void) data; |
| 324 | (void) length; |
| 325 | exception=(ExceptionInfo *) user_context; |
cristy | 32cca40 | 2010-01-23 04:02:23 +0000 | [diff] [blame] | 326 | (void) ThrowMagickException(exception,GetMagickModule(),DelegateWarning, |
cristy | efe601c | 2013-01-05 17:51:12 +0000 | [diff] [blame] | 327 | "DelegateFailed","`%s'",message); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static MagickBooleanType BindConvolveParameters(ConvolveInfo *convolve_info, |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 331 | const Image *image,const void *pixels,float *filter,const size_t width, |
cristy | c8523c1 | 2011-09-13 00:02:53 +0000 | [diff] [blame] | 332 | const size_t height,void *convolve_pixels) |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 333 | { |
| 334 | cl_int |
| 335 | status; |
| 336 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 337 | register cl_uint |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 338 | i; |
| 339 | |
| 340 | size_t |
| 341 | length; |
| 342 | |
| 343 | /* |
| 344 | Allocate OpenCL buffers. |
| 345 | */ |
| 346 | length=image->columns*image->rows; |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 347 | convolve_info->pixels=clCreateBuffer(convolve_info->context,(cl_mem_flags) |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 348 | (CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR),length*sizeof(CLPixelInfo), |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 349 | (void *) pixels,&status); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 350 | if ((convolve_info->pixels == (cl_mem) NULL) || (status != CL_SUCCESS)) |
| 351 | return(MagickFalse); |
| 352 | length=width*height; |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 353 | convolve_info->filter=clCreateBuffer(convolve_info->context,(cl_mem_flags) |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 354 | (CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR),length*sizeof(cl_float),filter, |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 355 | &status); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 356 | if ((convolve_info->filter == (cl_mem) NULL) || (status != CL_SUCCESS)) |
| 357 | return(MagickFalse); |
| 358 | length=image->columns*image->rows; |
| 359 | convolve_info->convolve_pixels=clCreateBuffer(convolve_info->context, |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 360 | (cl_mem_flags) (CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR),length* |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 361 | sizeof(CLPixelInfo),convolve_pixels,&status); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 362 | if ((convolve_info->convolve_pixels == (cl_mem) NULL) || |
| 363 | (status != CL_SUCCESS)) |
| 364 | return(MagickFalse); |
| 365 | /* |
| 366 | Bind OpenCL buffers. |
| 367 | */ |
| 368 | i=0; |
| 369 | status=clSetKernelArg(convolve_info->kernel,i++,sizeof(cl_mem),(void *) |
| 370 | &convolve_info->pixels); |
| 371 | if (status != CL_SUCCESS) |
| 372 | return(MagickFalse); |
| 373 | status=clSetKernelArg(convolve_info->kernel,i++,sizeof(cl_mem),(void *) |
| 374 | &convolve_info->filter); |
| 375 | if (status != CL_SUCCESS) |
| 376 | return(MagickFalse); |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 377 | convolve_info->width=(cl_ulong) width; |
| 378 | status=clSetKernelArg(convolve_info->kernel,i++,sizeof(cl_ulong),(void *) |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 379 | &convolve_info->width); |
| 380 | if (status != CL_SUCCESS) |
| 381 | return(MagickFalse); |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 382 | convolve_info->height=(cl_ulong) height; |
| 383 | status=clSetKernelArg(convolve_info->kernel,i++,sizeof(cl_ulong),(void *) |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 384 | &convolve_info->height); |
| 385 | if (status != CL_SUCCESS) |
| 386 | return(MagickFalse); |
cristy | 644d5d0 | 2012-08-29 11:20:44 +0000 | [diff] [blame] | 387 | convolve_info->matte=(cl_uint) image->alpha_trait == BlendPixelTrait ? |
| 388 | MagickTrue : MagickFalse; |
cristy | 966032e | 2011-09-12 19:12:00 +0000 | [diff] [blame] | 389 | status=clSetKernelArg(convolve_info->kernel,i++,sizeof(cl_uint),(void *) |
cristy | 644d5d0 | 2012-08-29 11:20:44 +0000 | [diff] [blame] | 390 | &convolve_info->matte); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 391 | if (status != CL_SUCCESS) |
| 392 | return(MagickFalse); |
| 393 | status=clSetKernelArg(convolve_info->kernel,i++,sizeof(cl_mem),(void *) |
| 394 | &convolve_info->convolve_pixels); |
| 395 | if (status != CL_SUCCESS) |
| 396 | return(MagickFalse); |
| 397 | status=clFinish(convolve_info->command_queue); |
| 398 | if (status != CL_SUCCESS) |
| 399 | return(MagickFalse); |
| 400 | return(MagickTrue); |
| 401 | } |
| 402 | |
| 403 | static void DestroyConvolveBuffers(ConvolveInfo *convolve_info) |
| 404 | { |
| 405 | cl_int |
| 406 | status; |
| 407 | |
cristy | 9f027d1 | 2011-09-21 01:17:17 +0000 | [diff] [blame] | 408 | status=0; |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 409 | if (convolve_info->convolve_pixels != (cl_mem) NULL) |
| 410 | status=clReleaseMemObject(convolve_info->convolve_pixels); |
| 411 | if (convolve_info->pixels != (cl_mem) NULL) |
| 412 | status=clReleaseMemObject(convolve_info->pixels); |
| 413 | if (convolve_info->filter != (cl_mem) NULL) |
| 414 | status=clReleaseMemObject(convolve_info->filter); |
cristy | aa83c2c | 2011-09-21 13:36:25 +0000 | [diff] [blame] | 415 | (void) status; |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static ConvolveInfo *DestroyConvolveInfo(ConvolveInfo *convolve_info) |
| 419 | { |
| 420 | cl_int |
| 421 | status; |
| 422 | |
cristy | 9f027d1 | 2011-09-21 01:17:17 +0000 | [diff] [blame] | 423 | status=0; |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 424 | if (convolve_info->kernel != (cl_kernel) NULL) |
| 425 | status=clReleaseKernel(convolve_info->kernel); |
| 426 | if (convolve_info->program != (cl_program) NULL) |
| 427 | status=clReleaseProgram(convolve_info->program); |
| 428 | if (convolve_info->command_queue != (cl_command_queue) NULL) |
| 429 | status=clReleaseCommandQueue(convolve_info->command_queue); |
| 430 | if (convolve_info->context != (cl_context) NULL) |
| 431 | status=clReleaseContext(convolve_info->context); |
cristy | aa83c2c | 2011-09-21 13:36:25 +0000 | [diff] [blame] | 432 | (void) status; |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 433 | convolve_info=(ConvolveInfo *) RelinquishMagickMemory(convolve_info); |
| 434 | return(convolve_info); |
| 435 | } |
| 436 | |
| 437 | static MagickBooleanType EnqueueConvolveKernel(ConvolveInfo *convolve_info, |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 438 | const Image *image,const void *pixels,float *filter,const size_t width, |
cristy | c8523c1 | 2011-09-13 00:02:53 +0000 | [diff] [blame] | 439 | const size_t height,void *convolve_pixels) |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 440 | { |
| 441 | cl_int |
| 442 | status; |
| 443 | |
| 444 | size_t |
| 445 | global_work_size[2], |
| 446 | length; |
| 447 | |
| 448 | length=image->columns*image->rows; |
| 449 | status=clEnqueueWriteBuffer(convolve_info->command_queue, |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 450 | convolve_info->pixels,CL_TRUE,0,length*sizeof(CLPixelInfo),pixels,0,NULL, |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 451 | NULL); |
| 452 | length=width*height; |
| 453 | status=clEnqueueWriteBuffer(convolve_info->command_queue, |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 454 | convolve_info->filter,CL_TRUE,0,length*sizeof(cl_float),filter,0,NULL, |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 455 | NULL); |
| 456 | if (status != CL_SUCCESS) |
| 457 | return(MagickFalse); |
| 458 | global_work_size[0]=image->columns; |
| 459 | global_work_size[1]=image->rows; |
| 460 | status=clEnqueueNDRangeKernel(convolve_info->command_queue, |
| 461 | convolve_info->kernel,2,NULL,global_work_size,NULL,0,NULL,NULL); |
| 462 | if (status != CL_SUCCESS) |
| 463 | return(MagickFalse); |
| 464 | length=image->columns*image->rows; |
| 465 | status=clEnqueueReadBuffer(convolve_info->command_queue, |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 466 | convolve_info->convolve_pixels,CL_TRUE,0,length*sizeof(CLPixelInfo), |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 467 | convolve_pixels,0,NULL,NULL); |
| 468 | if (status != CL_SUCCESS) |
| 469 | return(MagickFalse); |
| 470 | status=clFinish(convolve_info->command_queue); |
| 471 | if (status != CL_SUCCESS) |
| 472 | return(MagickFalse); |
| 473 | return(MagickTrue); |
| 474 | } |
| 475 | |
| 476 | static ConvolveInfo *GetConvolveInfo(const Image *image,const char *name, |
| 477 | const char *source,ExceptionInfo *exception) |
| 478 | { |
| 479 | char |
| 480 | options[MaxTextExtent]; |
| 481 | |
cristy | 966032e | 2011-09-12 19:12:00 +0000 | [diff] [blame] | 482 | cl_context_properties |
| 483 | context_properties[3]; |
| 484 | |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 485 | cl_int |
| 486 | status; |
| 487 | |
cristy | 966032e | 2011-09-12 19:12:00 +0000 | [diff] [blame] | 488 | cl_platform_id |
| 489 | platforms[1]; |
| 490 | |
| 491 | cl_uint |
| 492 | number_platforms; |
| 493 | |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 494 | ConvolveInfo |
| 495 | *convolve_info; |
| 496 | |
| 497 | size_t |
| 498 | length, |
| 499 | lengths[] = { strlen(source) }; |
| 500 | |
| 501 | /* |
| 502 | Create OpenCL info. |
| 503 | */ |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 504 | convolve_info=(ConvolveInfo *) AcquireMagickMemory(sizeof(*convolve_info)); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 505 | if (convolve_info == (ConvolveInfo *) NULL) |
| 506 | { |
| 507 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | efe601c | 2013-01-05 17:51:12 +0000 | [diff] [blame] | 508 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 509 | return((ConvolveInfo *) NULL); |
| 510 | } |
| 511 | (void) ResetMagickMemory(convolve_info,0,sizeof(*convolve_info)); |
| 512 | /* |
| 513 | Create OpenCL context. |
| 514 | */ |
cristy | 61b76e6 | 2011-09-13 12:04:12 +0000 | [diff] [blame] | 515 | status=clGetPlatformIDs(0,(cl_platform_id *) NULL,&number_platforms); |
| 516 | if ((status == CL_SUCCESS) && (number_platforms > 0)) |
cristy | 966032e | 2011-09-12 19:12:00 +0000 | [diff] [blame] | 517 | status=clGetPlatformIDs(1,platforms,NULL); |
| 518 | if (status != CL_SUCCESS) |
| 519 | { |
| 520 | (void) ThrowMagickException(exception,GetMagickModule(),DelegateWarning, |
anthony | e5b3965 | 2012-04-21 05:37:29 +0000 | [diff] [blame] | 521 | "failed to create OpenCL context","'%s' (%d)",image->filename,status); |
cristy | 966032e | 2011-09-12 19:12:00 +0000 | [diff] [blame] | 522 | convolve_info=DestroyConvolveInfo(convolve_info); |
| 523 | return((ConvolveInfo *) NULL); |
| 524 | } |
| 525 | context_properties[0]=CL_CONTEXT_PLATFORM; |
| 526 | context_properties[1]=(cl_context_properties) platforms[0]; |
| 527 | context_properties[2]=0; |
| 528 | convolve_info->context=clCreateContextFromType(context_properties, |
| 529 | (cl_device_type) CL_DEVICE_TYPE_GPU,ConvolveNotify,exception,&status); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 530 | if ((convolve_info->context == (cl_context) NULL) || (status != CL_SUCCESS)) |
cristy | 966032e | 2011-09-12 19:12:00 +0000 | [diff] [blame] | 531 | convolve_info->context=clCreateContextFromType(context_properties, |
| 532 | (cl_device_type) CL_DEVICE_TYPE_CPU,ConvolveNotify,exception,&status); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 533 | if ((convolve_info->context == (cl_context) NULL) || (status != CL_SUCCESS)) |
cristy | 966032e | 2011-09-12 19:12:00 +0000 | [diff] [blame] | 534 | convolve_info->context=clCreateContextFromType(context_properties, |
| 535 | (cl_device_type) CL_DEVICE_TYPE_DEFAULT,ConvolveNotify,exception,&status); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 536 | if ((convolve_info->context == (cl_context) NULL) || (status != CL_SUCCESS)) |
| 537 | { |
cristy | 32cca40 | 2010-01-23 04:02:23 +0000 | [diff] [blame] | 538 | (void) ThrowMagickException(exception,GetMagickModule(),DelegateWarning, |
anthony | e5b3965 | 2012-04-21 05:37:29 +0000 | [diff] [blame] | 539 | "failed to create OpenCL context","'%s' (%d)",image->filename,status); |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 540 | convolve_info=DestroyConvolveInfo(convolve_info); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 541 | return((ConvolveInfo *) NULL); |
| 542 | } |
| 543 | /* |
| 544 | Detect OpenCL devices. |
| 545 | */ |
| 546 | status=clGetContextInfo(convolve_info->context,CL_CONTEXT_DEVICES,0,NULL, |
| 547 | &length); |
| 548 | if ((status != CL_SUCCESS) || (length == 0)) |
| 549 | { |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 550 | convolve_info=DestroyConvolveInfo(convolve_info); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 551 | return((ConvolveInfo *) NULL); |
| 552 | } |
| 553 | convolve_info->devices=(cl_device_id *) AcquireMagickMemory(length); |
| 554 | if (convolve_info->devices == (cl_device_id *) NULL) |
| 555 | { |
| 556 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | efe601c | 2013-01-05 17:51:12 +0000 | [diff] [blame] | 557 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 558 | convolve_info=DestroyConvolveInfo(convolve_info); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 559 | return((ConvolveInfo *) NULL); |
| 560 | } |
| 561 | status=clGetContextInfo(convolve_info->context,CL_CONTEXT_DEVICES,length, |
| 562 | convolve_info->devices,NULL); |
| 563 | if (status != CL_SUCCESS) |
| 564 | { |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 565 | convolve_info=DestroyConvolveInfo(convolve_info); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 566 | return((ConvolveInfo *) NULL); |
| 567 | } |
cristy | dc9c80d | 2011-10-26 23:47:02 +0000 | [diff] [blame] | 568 | if (image->debug != MagickFalse) |
| 569 | { |
| 570 | char |
| 571 | attribute[MaxTextExtent]; |
| 572 | |
| 573 | size_t |
| 574 | length; |
| 575 | |
| 576 | clGetDeviceInfo(convolve_info->devices[0],CL_DEVICE_NAME, |
| 577 | sizeof(attribute),attribute,&length); |
| 578 | (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),"Name: %s", |
| 579 | attribute); |
| 580 | clGetDeviceInfo(convolve_info->devices[0],CL_DEVICE_VENDOR, |
| 581 | sizeof(attribute),attribute,&length); |
| 582 | (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),"Vendor: %s", |
| 583 | attribute); |
| 584 | clGetDeviceInfo(convolve_info->devices[0],CL_DEVICE_VERSION, |
| 585 | sizeof(attribute),attribute,&length); |
| 586 | (void) LogMagickEvent(AccelerateEvent,GetMagickModule(), |
| 587 | "Driver Version: %s",attribute); |
| 588 | clGetDeviceInfo(convolve_info->devices[0],CL_DEVICE_PROFILE, |
| 589 | sizeof(attribute),attribute,&length); |
| 590 | (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),"Profile: %s", |
| 591 | attribute); |
| 592 | clGetDeviceInfo(convolve_info->devices[0],CL_DRIVER_VERSION, |
| 593 | sizeof(attribute),attribute,&length); |
| 594 | (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),"Driver: %s", |
| 595 | attribute); |
| 596 | clGetDeviceInfo(convolve_info->devices[0],CL_DEVICE_EXTENSIONS, |
| 597 | sizeof(attribute),attribute,&length); |
| 598 | (void) LogMagickEvent(AccelerateEvent,GetMagickModule(),"Extensions: %s", |
| 599 | attribute); |
| 600 | } |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 601 | /* |
| 602 | Create OpenCL command queue. |
| 603 | */ |
| 604 | convolve_info->command_queue=clCreateCommandQueue(convolve_info->context, |
| 605 | convolve_info->devices[0],0,&status); |
| 606 | if ((convolve_info->command_queue == (cl_command_queue) NULL) || |
| 607 | (status != CL_SUCCESS)) |
| 608 | { |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 609 | convolve_info=DestroyConvolveInfo(convolve_info); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 610 | return((ConvolveInfo *) NULL); |
| 611 | } |
| 612 | /* |
| 613 | Build OpenCL program. |
| 614 | */ |
| 615 | convolve_info->program=clCreateProgramWithSource(convolve_info->context,1, |
| 616 | &source,lengths,&status); |
| 617 | if ((convolve_info->program == (cl_program) NULL) || (status != CL_SUCCESS)) |
| 618 | { |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 619 | convolve_info=DestroyConvolveInfo(convolve_info); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 620 | return((ConvolveInfo *) NULL); |
| 621 | } |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 622 | (void) FormatLocaleString(options,MaxTextExtent,CLOptions,(float) |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 623 | QuantumRange,MagickEpsilon); |
cristy | 00243d1 | 2010-01-21 02:45:27 +0000 | [diff] [blame] | 624 | status=clBuildProgram(convolve_info->program,1,convolve_info->devices,options, |
| 625 | NULL,NULL); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 626 | if ((convolve_info->program == (cl_program) NULL) || (status != CL_SUCCESS)) |
| 627 | { |
| 628 | char |
| 629 | *log; |
| 630 | |
| 631 | status=clGetProgramBuildInfo(convolve_info->program, |
| 632 | convolve_info->devices[0],CL_PROGRAM_BUILD_LOG,0,NULL,&length); |
| 633 | log=(char *) AcquireMagickMemory(length); |
| 634 | if (log == (char *) NULL) |
| 635 | { |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 636 | convolve_info=DestroyConvolveInfo(convolve_info); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 637 | return((ConvolveInfo *) NULL); |
| 638 | } |
| 639 | status=clGetProgramBuildInfo(convolve_info->program, |
| 640 | convolve_info->devices[0],CL_PROGRAM_BUILD_LOG,length,log,&length); |
cristy | 32cca40 | 2010-01-23 04:02:23 +0000 | [diff] [blame] | 641 | (void) ThrowMagickException(exception,GetMagickModule(),DelegateWarning, |
anthony | e5b3965 | 2012-04-21 05:37:29 +0000 | [diff] [blame] | 642 | "failed to build OpenCL program","'%s' (%s)",image->filename,log); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 643 | log=DestroyString(log); |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 644 | convolve_info=DestroyConvolveInfo(convolve_info); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 645 | return((ConvolveInfo *) NULL); |
| 646 | } |
| 647 | /* |
| 648 | Get a kernel object. |
| 649 | */ |
| 650 | convolve_info->kernel=clCreateKernel(convolve_info->program,name,&status); |
| 651 | if ((convolve_info->kernel == (cl_kernel) NULL) || (status != CL_SUCCESS)) |
| 652 | { |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 653 | convolve_info=DestroyConvolveInfo(convolve_info); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 654 | return((ConvolveInfo *) NULL); |
| 655 | } |
| 656 | return(convolve_info); |
| 657 | } |
| 658 | |
| 659 | #endif |
| 660 | |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 661 | MagickExport MagickBooleanType AccelerateConvolveImage(const Image *image, |
cristy | 2be1538 | 2010-01-21 02:38:03 +0000 | [diff] [blame] | 662 | const KernelInfo *kernel,Image *convolve_image,ExceptionInfo *exception) |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 663 | { |
| 664 | assert(image != (Image *) NULL); |
| 665 | assert(image->signature == MagickSignature); |
| 666 | if (image->debug != MagickFalse) |
| 667 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 2be1538 | 2010-01-21 02:38:03 +0000 | [diff] [blame] | 668 | assert(kernel != (KernelInfo *) NULL); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 669 | assert(kernel->signature == MagickSignature); |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 670 | assert(convolve_image != (Image *) NULL); |
| 671 | assert(convolve_image->signature == MagickSignature); |
| 672 | assert(exception != (ExceptionInfo *) NULL); |
| 673 | assert(exception->signature == MagickSignature); |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 674 | if ((image->storage_class != DirectClass) || |
cristy | 394651a | 2010-01-23 21:05:55 +0000 | [diff] [blame] | 675 | (image->colorspace == CMYKColorspace)) |
cristy | bfa02fa | 2011-09-13 00:40:44 +0000 | [diff] [blame] | 676 | return(MagickFalse); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 677 | if ((GetImageVirtualPixelMethod(image) != UndefinedVirtualPixelMethod) && |
| 678 | (GetImageVirtualPixelMethod(image) != EdgeVirtualPixelMethod)) |
| 679 | return(MagickFalse); |
cristy | b572edf | 2011-09-13 12:25:22 +0000 | [diff] [blame] | 680 | if (GetPixelChannels(image) != 4) |
| 681 | return(MagickFalse); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 682 | #if !defined(MAGICKCORE_OPENCL_SUPPORT) |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 683 | return(MagickFalse); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 684 | #else |
| 685 | { |
| 686 | const void |
| 687 | *pixels; |
| 688 | |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 689 | float |
| 690 | *filter; |
| 691 | |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 692 | ConvolveInfo |
| 693 | *convolve_info; |
| 694 | |
| 695 | MagickBooleanType |
| 696 | status; |
| 697 | |
| 698 | MagickSizeType |
| 699 | length; |
| 700 | |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 701 | register ssize_t |
| 702 | i; |
| 703 | |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 704 | void |
| 705 | *convolve_pixels; |
| 706 | |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 707 | convolve_info=GetConvolveInfo(image,"Convolve",ConvolveKernel,exception); |
| 708 | if (convolve_info == (ConvolveInfo *) NULL) |
| 709 | return(MagickFalse); |
| 710 | pixels=AcquirePixelCachePixels(image,&length,exception); |
| 711 | if (pixels == (const void *) NULL) |
| 712 | { |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 713 | convolve_info=DestroyConvolveInfo(convolve_info); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 714 | (void) ThrowMagickException(exception,GetMagickModule(),CacheError, |
cristy | efe601c | 2013-01-05 17:51:12 +0000 | [diff] [blame] | 715 | "UnableToReadPixelCache","`%s'",image->filename); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 716 | return(MagickFalse); |
| 717 | } |
| 718 | convolve_pixels=GetPixelCachePixels(convolve_image,&length,exception); |
| 719 | if (convolve_pixels == (void *) NULL) |
| 720 | { |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 721 | convolve_info=DestroyConvolveInfo(convolve_info); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 722 | (void) ThrowMagickException(exception,GetMagickModule(),CacheError, |
cristy | efe601c | 2013-01-05 17:51:12 +0000 | [diff] [blame] | 723 | "UnableToReadPixelCache","`%s'",image->filename); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 724 | return(MagickFalse); |
| 725 | } |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 726 | filter=(float *) AcquireQuantumMemory(kernel->width,kernel->height* |
| 727 | sizeof(*filter)); |
| 728 | if (filter == (float *) NULL) |
| 729 | { |
| 730 | DestroyConvolveBuffers(convolve_info); |
| 731 | convolve_info=DestroyConvolveInfo(convolve_info); |
| 732 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | efe601c | 2013-01-05 17:51:12 +0000 | [diff] [blame] | 733 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 734 | return(MagickFalse); |
| 735 | } |
| 736 | for (i=0; i < (ssize_t) (kernel->width*kernel->height); i++) |
| 737 | filter[i]=(float) kernel->values[i]; |
| 738 | status=BindConvolveParameters(convolve_info,image,pixels,filter, |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 739 | kernel->width,kernel->height,convolve_pixels); |
| 740 | if (status == MagickFalse) |
| 741 | { |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 742 | filter=(float *) RelinquishMagickMemory(filter); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 743 | DestroyConvolveBuffers(convolve_info); |
| 744 | convolve_info=DestroyConvolveInfo(convolve_info); |
| 745 | return(MagickFalse); |
| 746 | } |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 747 | status=EnqueueConvolveKernel(convolve_info,image,pixels,filter, |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 748 | kernel->width,kernel->height,convolve_pixels); |
cristy | 1a2e276 | 2011-09-13 00:31:55 +0000 | [diff] [blame] | 749 | filter=(float *) RelinquishMagickMemory(filter); |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 750 | if (status == MagickFalse) |
| 751 | { |
| 752 | DestroyConvolveBuffers(convolve_info); |
| 753 | convolve_info=DestroyConvolveInfo(convolve_info); |
| 754 | return(MagickFalse); |
| 755 | } |
| 756 | DestroyConvolveBuffers(convolve_info); |
| 757 | convolve_info=DestroyConvolveInfo(convolve_info); |
| 758 | return(MagickTrue); |
| 759 | } |
| 760 | #endif |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 761 | } |