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 | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 17 | % SiuChi Chan % |
| 18 | % Guansong Zhang % |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 19 | % January 2010 % |
| 20 | % % |
| 21 | % % |
cristy | fe676ee | 2013-11-18 13:03:38 +0000 | [diff] [blame] | 22 | % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 23 | % 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 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 37 | */ |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 38 | |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 39 | /* |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 40 | Include declarations. |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
| 43 | #include "MagickCore/accelerate.h" |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 44 | #include "MagickCore/accelerate-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 45 | #include "MagickCore/artifact.h" |
cristy | 35f3349 | 2011-07-07 16:54:49 +0000 | [diff] [blame] | 46 | #include "MagickCore/cache.h" |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 47 | #include "MagickCore/cache-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 48 | #include "MagickCore/cache-view.h" |
| 49 | #include "MagickCore/color-private.h" |
cristy | 7f86684 | 2013-07-11 01:15:58 +0000 | [diff] [blame] | 50 | #include "MagickCore/delegate-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 51 | #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" |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 62 | #include "MagickCore/opencl.h" |
| 63 | #include "MagickCore/opencl-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 64 | #include "MagickCore/option.h" |
dirk | 434ce8a | 2014-08-17 08:33:47 +0000 | [diff] [blame^] | 65 | #include "MagickCore/pixel-accessor.h" |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 66 | #include "MagickCore/pixel-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 67 | #include "MagickCore/prepress.h" |
| 68 | #include "MagickCore/quantize.h" |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 69 | #include "MagickCore/quantum-private.h" |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 70 | #include "MagickCore/random_.h" |
| 71 | #include "MagickCore/random-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 72 | #include "MagickCore/registry.h" |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 73 | #include "MagickCore/resize.h" |
| 74 | #include "MagickCore/resize-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 81 | |
| 82 | #ifdef MAGICKCORE_CLPERFMARKER |
| 83 | #include "CLPerfMarker.h" |
| 84 | #endif |
| 85 | |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 86 | #define MAGICK_MAX(x,y) (((x) >= (y))?(x):(y)) |
| 87 | #define MAGICK_MIN(x,y) (((x) <= (y))?(x):(y)) |
| 88 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 89 | #if defined(MAGICKCORE_OPENCL_SUPPORT) |
| 90 | |
| 91 | #define ALIGNED(pointer,type) ((((long)(pointer)) & (sizeof(type)-1)) == 0) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 92 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 93 | /* pad the global workgroup size to the next multiple of |
| 94 | the local workgroup size */ |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 95 | inline static unsigned int padGlobalWorkgroupSizeToLocalWorkgroupSize( |
| 96 | const unsigned int orgGlobalSize,const unsigned int localGroupSize) |
| 97 | { |
| 98 | return ((orgGlobalSize+(localGroupSize-1))/localGroupSize*localGroupSize); |
| 99 | } |
| 100 | |
| 101 | static 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 | |
| 132 | static 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 | |
dirk | 434ce8a | 2014-08-17 08:33:47 +0000 | [diff] [blame^] | 156 | /* 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 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 170 | return(MagickTrue); |
| 171 | } |
| 172 | |
| 173 | static 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 | |
| 190 | static 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 209 | } |
| 210 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 211 | /* |
| 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 | */ |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 242 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 243 | static 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; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 321 | outputReady = MagickFalse; |
| 322 | |
| 323 | clEnv = GetDefaultOpenCLEnv(); |
| 324 | context = GetOpenCLContext(clEnv); |
| 325 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 326 | image_view=AcquireVirtualCacheView(image,exception); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 327 | inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 328 | if (inputPixels == (const void *) NULL) |
| 329 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 330 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 331 | 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 */ |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 348 | length = image->columns * image->rows; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 349 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 350 | if (clStatus != CL_SUCCESS) |
| 351 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 352 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 353 | goto cleanup; |
| 354 | } |
| 355 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 356 | filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 357 | assert(filteredImage != NULL); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 358 | if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 359 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 360 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 361 | goto cleanup; |
| 362 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 363 | filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 364 | filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 365 | if (filteredPixels == (void *) NULL) |
| 366 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 367 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 368 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 382 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 383 | filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 384 | if (clStatus != CL_SUCCESS) |
| 385 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 386 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 387 | goto cleanup; |
| 388 | } |
| 389 | |
| 390 | kernelSize = kernel->width * kernel->height; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 391 | convolutionKernel = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, kernelSize * sizeof(float), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 392 | if (clStatus != CL_SUCCESS) |
| 393 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 394 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 395 | goto cleanup; |
| 396 | } |
| 397 | |
| 398 | queue = AcquireOpenCLCommandQueue(clEnv); |
| 399 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 400 | kernelBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, convolutionKernel, CL_TRUE, CL_MAP_WRITE, 0, kernelSize * sizeof(float) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 401 | , 0, NULL, NULL, &clStatus); |
| 402 | if (clStatus != CL_SUCCESS) |
| 403 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 404 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 405 | goto cleanup; |
| 406 | } |
| 407 | for (i = 0; i < kernelSize; i++) |
| 408 | { |
| 409 | kernelBufferPtr[i] = (float) kernel->values[i]; |
| 410 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 411 | clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, convolutionKernel, kernelBufferPtr, 0, NULL, NULL); |
| 412 | if (clStatus != CL_SUCCESS) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 413 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 414 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 415 | goto cleanup; |
| 416 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 417 | clEnv->library->clFlush(queue); |
| 418 | |
| 419 | deviceLocalMemorySize = GetOpenCLDeviceLocalMemorySize(clEnv); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 420 | |
| 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 427 | |
| 428 | if (localMemoryRequirement > deviceLocalMemorySize) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 429 | { |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 430 | localGroupSize[0] = 8; |
| 431 | localGroupSize[1] = 8; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 432 | localMemoryRequirement = (localGroupSize[0]+kernel->width-1) * (localGroupSize[1]+kernel->height-1) * sizeof(CLPixelPacket) |
| 433 | + kernel->width*kernel->height*sizeof(float); |
| 434 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 435 | if (localMemoryRequirement <= deviceLocalMemorySize) |
| 436 | { |
| 437 | /* get the OpenCL kernel */ |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 438 | clkernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "ConvolveOptimized"); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 439 | if (clkernel == NULL) |
| 440 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 441 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 442 | goto cleanup; |
| 443 | } |
| 444 | |
| 445 | /* set the kernel arguments */ |
| 446 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 447 | clStatus =clEnv->library->clSetKernelArg(clkernel,i++,sizeof(cl_mem),(void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 448 | clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(cl_mem),(void *)&filteredImageBuffer); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 449 | imageWidth = image->columns; |
| 450 | imageHeight = image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 451 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 454 | filterWidth = kernel->width; |
| 455 | filterHeight = kernel->height; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 456 | clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&filterWidth); |
| 457 | clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&filterHeight); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 458 | matte = (image->alpha_trait==BlendPixelTrait)?1:0; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 459 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 463 | if (clStatus != CL_SUCCESS) |
| 464 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 465 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 466 | goto cleanup; |
| 467 | } |
| 468 | |
| 469 | /* pad the global size to a multiple of the local work size dimension */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 470 | 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]; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 472 | |
| 473 | /* launch the kernel */ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 474 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, clkernel, 2, NULL, global_work_size, localGroupSize, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 475 | if (clStatus != CL_SUCCESS) |
| 476 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 477 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 478 | 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 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 487 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 488 | goto cleanup; |
| 489 | } |
| 490 | |
| 491 | /* set the kernel arguments */ |
| 492 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 493 | clStatus =clEnv->library->clSetKernelArg(clkernel,i++,sizeof(cl_mem),(void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 494 | clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(cl_mem),(void *)&filteredImageBuffer); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 495 | imageWidth = image->columns; |
| 496 | imageHeight = image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 497 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 500 | filterWidth = kernel->width; |
| 501 | filterHeight = kernel->height; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 502 | clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&filterWidth); |
| 503 | clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&filterHeight); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 504 | matte = (image->alpha_trait==BlendPixelTrait)?1:0; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 505 | clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(unsigned int),(void *)&matte); |
| 506 | clStatus|=clEnv->library->clSetKernelArg(clkernel,i++,sizeof(ChannelType),(void *)&channel); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 507 | if (clStatus != CL_SUCCESS) |
| 508 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 509 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 510 | goto cleanup; |
| 511 | } |
| 512 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 513 | localGroupSize[0] = 8; |
| 514 | localGroupSize[1] = 8; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 515 | 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]; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 517 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, clkernel, 2, NULL, global_work_size, localGroupSize, 0, NULL, NULL); |
| 518 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 519 | if (clStatus != CL_SUCCESS) |
| 520 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 521 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 522 | goto cleanup; |
| 523 | } |
| 524 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 525 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 526 | |
| 527 | if (ALIGNED(filteredPixels,CLPixelPacket)) |
| 528 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 529 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 530 | clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 531 | } |
| 532 | else |
| 533 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 534 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 535 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 536 | } |
| 537 | if (clStatus != CL_SUCCESS) |
| 538 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 539 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 540 | goto cleanup; |
| 541 | } |
| 542 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 543 | outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception); |
| 544 | |
| 545 | cleanup: |
| 546 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 547 | |
| 548 | image_view=DestroyCacheView(image_view); |
| 549 | if (filteredImage_view != NULL) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 550 | filteredImage_view=DestroyCacheView(filteredImage_view); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 551 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 552 | if (imageBuffer != NULL) |
| 553 | clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 554 | |
| 555 | if (filteredImageBuffer != NULL) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 556 | clEnv->library->clReleaseMemObject(filteredImageBuffer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 557 | |
| 558 | if (convolutionKernel != NULL) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 559 | clEnv->library->clReleaseMemObject(convolutionKernel); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 560 | |
| 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 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 576 | return(filteredImage); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 577 | } |
| 578 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 579 | MagickExport 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 595 | } |
| 596 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 597 | static 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 657 | pixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 658 | if (pixels == (void *) NULL) |
| 659 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 660 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), CacheWarning, |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 661 | "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; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 677 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)pixels, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 678 | if (clStatus != CL_SUCCESS) |
| 679 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 680 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 681 | goto cleanup; |
| 682 | } |
| 683 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 684 | parametersBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, number_parameters * sizeof(float), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 685 | if (clStatus != CL_SUCCESS) |
| 686 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 687 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 688 | goto cleanup; |
| 689 | } |
| 690 | |
| 691 | queue = AcquireOpenCLCommandQueue(clEnv); |
| 692 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 693 | parametersBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, parametersBuffer, CL_TRUE, CL_MAP_WRITE, 0, number_parameters * sizeof(float) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 694 | , 0, NULL, NULL, &clStatus); |
| 695 | if (clStatus != CL_SUCCESS) |
| 696 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 697 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 698 | goto cleanup; |
| 699 | } |
| 700 | for (i = 0; i < number_parameters; i++) |
| 701 | { |
| 702 | parametersBufferPtr[i] = (float)parameters[i]; |
| 703 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 704 | clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, parametersBuffer, parametersBufferPtr, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 705 | if (clStatus != CL_SUCCESS) |
| 706 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 707 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 708 | goto cleanup; |
| 709 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 710 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 711 | |
| 712 | clkernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "FunctionImage"); |
| 713 | if (clkernel == NULL) |
| 714 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 715 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 716 | goto cleanup; |
| 717 | } |
| 718 | |
| 719 | /* set the kernel arguments */ |
| 720 | i = 0; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 721 | 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 *)¶metersBuffer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 726 | if (clStatus != CL_SUCCESS) |
| 727 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 728 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 729 | goto cleanup; |
| 730 | } |
| 731 | |
| 732 | globalWorkSize[0] = image->columns; |
| 733 | globalWorkSize[1] = image->rows; |
| 734 | /* launch the kernel */ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 735 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, clkernel, 2, NULL, globalWorkSize, NULL, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 736 | if (clStatus != CL_SUCCESS) |
| 737 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 738 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 739 | goto cleanup; |
| 740 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 741 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 742 | |
| 743 | |
| 744 | if (ALIGNED(pixels,CLPixelPacket)) |
| 745 | { |
| 746 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 747 | clEnv->library->clEnqueueMapBuffer(queue, imageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 748 | } |
| 749 | else |
| 750 | { |
| 751 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 752 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), pixels, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 753 | } |
| 754 | if (clStatus != CL_SUCCESS) |
| 755 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 756 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 757 | goto cleanup; |
| 758 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 759 | status=SyncCacheViewAuthenticPixels(image_view,exception); |
| 760 | |
| 761 | cleanup: |
| 762 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 763 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 764 | image_view=DestroyCacheView(image_view); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 765 | |
| 766 | if (clkernel != NULL) RelinquishOpenCLKernel(clEnv, clkernel); |
| 767 | if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 768 | if (imageBuffer != NULL) clEnv->library->clReleaseMemObject(imageBuffer); |
| 769 | if (parametersBuffer != NULL) clEnv->library->clReleaseMemObject(parametersBuffer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 770 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 771 | return(status); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 772 | } |
| 773 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 774 | MagickExport 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 791 | } |
| 792 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 793 | /* |
| 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 | */ |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 830 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 831 | static 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 915 | inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 916 | if (inputPixels == (const void *) NULL) |
| 917 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 918 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 919 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 933 | length = image->columns * image->rows; |
| 934 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 935 | if (clStatus != CL_SUCCESS) |
| 936 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 937 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 938 | goto cleanup; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | /* create output */ |
| 943 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 944 | filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 945 | assert(filteredImage != NULL); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 946 | if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 947 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 948 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 949 | goto cleanup; |
| 950 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 951 | filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 952 | filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 953 | if (filteredPixels == (void *) NULL) |
| 954 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 955 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 956 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 970 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 971 | filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 972 | if (clStatus != CL_SUCCESS) |
| 973 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 974 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 975 | 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 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 985 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "MemoryAllocationFailed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 986 | goto cleanup; |
| 987 | } |
| 988 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 989 | imageKernelBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, kernel->width * sizeof(float), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 990 | if (clStatus != CL_SUCCESS) |
| 991 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 992 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 993 | goto cleanup; |
| 994 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 995 | kernelBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, imageKernelBuffer, CL_TRUE, CL_MAP_WRITE, 0, kernel->width * sizeof(float), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 996 | if (clStatus != CL_SUCCESS) |
| 997 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 998 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 999 | goto cleanup; |
| 1000 | } |
| 1001 | |
| 1002 | for (i = 0; i < kernel->width; i++) |
| 1003 | { |
| 1004 | kernelBufferPtr[i] = (float) kernel->values[i]; |
| 1005 | } |
| 1006 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1007 | clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, imageKernelBuffer, kernelBufferPtr, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1008 | if (clStatus != CL_SUCCESS) |
| 1009 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1010 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1011 | goto cleanup; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | { |
| 1016 | |
| 1017 | /* create temp buffer */ |
| 1018 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1019 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1020 | tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length * 4 * sizeof(float), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1021 | if (clStatus != CL_SUCCESS) |
| 1022 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1023 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1024 | goto cleanup; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | /* get the OpenCL kernels */ |
| 1029 | { |
| 1030 | blurRowKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "BlurRow"); |
| 1031 | if (blurRowKernel == NULL) |
| 1032 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1033 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1034 | goto cleanup; |
| 1035 | }; |
| 1036 | |
| 1037 | blurColumnKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "BlurColumn"); |
| 1038 | if (blurColumnKernel == NULL) |
| 1039 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1040 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1041 | goto cleanup; |
| 1042 | }; |
| 1043 | } |
| 1044 | |
| 1045 | { |
| 1046 | /* need logic to decide this value */ |
| 1047 | int chunkSize = 256; |
| 1048 | |
| 1049 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1050 | imageColumns = image->columns; |
| 1051 | imageRows = image->rows; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1052 | |
| 1053 | /* set the kernel arguments */ |
| 1054 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1055 | clStatus=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1056 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1059 | kernelWidth = kernel->width; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1060 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1064 | if (clStatus != CL_SUCCESS) |
| 1065 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1066 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1067 | goto cleanup; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /* launch the kernel */ |
| 1072 | { |
| 1073 | size_t gsize[2]; |
| 1074 | size_t wsize[2]; |
| 1075 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1076 | gsize[0] = chunkSize*((image->columns+chunkSize-1)/chunkSize); |
| 1077 | gsize[1] = image->rows; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1078 | wsize[0] = chunkSize; |
| 1079 | wsize[1] = 1; |
| 1080 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1081 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, blurRowKernel, 2, NULL, gsize, wsize, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1082 | if (clStatus != CL_SUCCESS) |
| 1083 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1084 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1085 | goto cleanup; |
| 1086 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1087 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | { |
| 1092 | /* need logic to decide this value */ |
| 1093 | int chunkSize = 256; |
| 1094 | |
| 1095 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1096 | imageColumns = image->columns; |
| 1097 | imageRows = image->rows; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1098 | |
| 1099 | /* set the kernel arguments */ |
| 1100 | i = 0; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1101 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1105 | kernelWidth = kernel->width; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1106 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1110 | if (clStatus != CL_SUCCESS) |
| 1111 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1112 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1113 | goto cleanup; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | /* launch the kernel */ |
| 1118 | { |
| 1119 | size_t gsize[2]; |
| 1120 | size_t wsize[2]; |
| 1121 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1122 | gsize[0] = image->columns; |
| 1123 | gsize[1] = chunkSize*((image->rows+chunkSize-1)/chunkSize); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1124 | wsize[0] = 1; |
| 1125 | wsize[1] = chunkSize; |
| 1126 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1127 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, blurColumnKernel, 2, NULL, gsize, wsize, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1128 | if (clStatus != CL_SUCCESS) |
| 1129 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1130 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1131 | goto cleanup; |
| 1132 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1133 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | } |
| 1138 | |
| 1139 | /* get result */ |
| 1140 | if (ALIGNED(filteredPixels,CLPixelPacket)) |
| 1141 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1142 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1143 | clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1144 | } |
| 1145 | else |
| 1146 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1147 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1148 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1149 | } |
| 1150 | if (clStatus != CL_SUCCESS) |
| 1151 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1152 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1153 | goto cleanup; |
| 1154 | } |
| 1155 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 1156 | outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception); |
| 1157 | |
| 1158 | cleanup: |
| 1159 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 1160 | |
| 1161 | image_view=DestroyCacheView(image_view); |
| 1162 | if (filteredImage_view != NULL) |
| 1163 | filteredImage_view=DestroyCacheView(filteredImage_view); |
| 1164 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1165 | if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1166 | if (tempImageBuffer!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer); |
| 1167 | if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer); |
| 1168 | if (imageKernelBuffer!=NULL) clEnv->library->clReleaseMemObject(imageKernelBuffer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1169 | 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1173 | if (outputReady == MagickFalse && filteredImage != NULL) |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 1174 | filteredImage=DestroyImage(filteredImage); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1175 | return(filteredImage); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 1178 | static 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1263 | inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1264 | if (inputPixels == (const void *) NULL) |
| 1265 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1266 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1267 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1281 | length = image->columns * image->rows; |
| 1282 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1283 | if (clStatus != CL_SUCCESS) |
| 1284 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1285 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1286 | goto cleanup; |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | /* create output */ |
| 1291 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1292 | filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1293 | assert(filteredImage != NULL); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1294 | if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1295 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1296 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1297 | goto cleanup; |
| 1298 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 1299 | filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1300 | filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1301 | if (filteredPixels == (void *) NULL) |
| 1302 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1303 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1304 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1318 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1319 | filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1320 | if (clStatus != CL_SUCCESS) |
| 1321 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1322 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1323 | 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 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1333 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "MemoryAllocationFailed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1334 | goto cleanup; |
| 1335 | } |
| 1336 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1337 | imageKernelBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, kernel->width * sizeof(float), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1338 | if (clStatus != CL_SUCCESS) |
| 1339 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1340 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1341 | goto cleanup; |
| 1342 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1343 | kernelBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, imageKernelBuffer, CL_TRUE, CL_MAP_WRITE, 0, kernel->width * sizeof(float), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1344 | if (clStatus != CL_SUCCESS) |
| 1345 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1346 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1347 | goto cleanup; |
| 1348 | } |
| 1349 | |
| 1350 | for (i = 0; i < kernel->width; i++) |
| 1351 | { |
| 1352 | kernelBufferPtr[i] = (float) kernel->values[i]; |
| 1353 | } |
| 1354 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1355 | clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, imageKernelBuffer, kernelBufferPtr, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1356 | if (clStatus != CL_SUCCESS) |
| 1357 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1358 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1359 | goto cleanup; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | { |
| 1364 | unsigned int offsetRows; |
| 1365 | unsigned int sec; |
| 1366 | |
| 1367 | /* create temp buffer */ |
| 1368 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1369 | length = image->columns * (image->rows / 2 + 1 + (kernel->width-1) / 2); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1370 | tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length * 4 * sizeof(float), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1371 | if (clStatus != CL_SUCCESS) |
| 1372 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1373 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1374 | goto cleanup; |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | /* get the OpenCL kernels */ |
| 1379 | { |
| 1380 | blurRowKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "BlurRowSection"); |
| 1381 | if (blurRowKernel == NULL) |
| 1382 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1383 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1384 | goto cleanup; |
| 1385 | }; |
| 1386 | |
| 1387 | blurColumnKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "BlurColumnSection"); |
| 1388 | if (blurColumnKernel == NULL) |
| 1389 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1390 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1391 | 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 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1402 | imageColumns = image->columns; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1403 | if (sec == 0) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1404 | imageRows = image->rows / 2 + (kernel->width-1) / 2; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1405 | else |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1406 | imageRows = (image->rows - image->rows / 2) + (kernel->width-1) / 2; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1407 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1408 | offsetRows = sec * image->rows / 2; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1409 | |
| 1410 | kernelWidth = kernel->width; |
| 1411 | |
| 1412 | /* set the kernel arguments */ |
| 1413 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1414 | clStatus=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1415 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1424 | if (clStatus != CL_SUCCESS) |
| 1425 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1426 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1427 | 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 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1441 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, blurRowKernel, 2, NULL, gsize, wsize, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1442 | if (clStatus != CL_SUCCESS) |
| 1443 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1444 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1445 | goto cleanup; |
| 1446 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1447 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | { |
| 1452 | /* need logic to decide this value */ |
| 1453 | int chunkSize = 256; |
| 1454 | |
| 1455 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1456 | imageColumns = image->columns; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1457 | if (sec == 0) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1458 | imageRows = image->rows / 2; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1459 | else |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1460 | imageRows = (image->rows - image->rows / 2); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1461 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1462 | offsetRows = sec * image->rows / 2; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1463 | |
| 1464 | kernelWidth = kernel->width; |
| 1465 | |
| 1466 | /* set the kernel arguments */ |
| 1467 | i = 0; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1468 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1478 | if (clStatus != CL_SUCCESS) |
| 1479 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1480 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1481 | 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 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1495 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, blurColumnKernel, 2, NULL, gsize, wsize, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1496 | if (clStatus != CL_SUCCESS) |
| 1497 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1498 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1499 | goto cleanup; |
| 1500 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1501 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | } |
| 1507 | |
| 1508 | /* get result */ |
| 1509 | if (ALIGNED(filteredPixels,CLPixelPacket)) |
| 1510 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1511 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1512 | clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1513 | } |
| 1514 | else |
| 1515 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1516 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1517 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1518 | } |
| 1519 | if (clStatus != CL_SUCCESS) |
| 1520 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1521 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1522 | goto cleanup; |
| 1523 | } |
| 1524 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 1525 | outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception); |
| 1526 | |
| 1527 | cleanup: |
| 1528 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 1529 | |
| 1530 | image_view=DestroyCacheView(image_view); |
| 1531 | if (filteredImage_view != NULL) |
| 1532 | filteredImage_view=DestroyCacheView(filteredImage_view); |
| 1533 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1534 | if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1535 | if (tempImageBuffer!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer); |
| 1536 | if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer); |
| 1537 | if (imageKernelBuffer!=NULL) clEnv->library->clReleaseMemObject(imageKernelBuffer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1538 | 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 | |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 1553 | static 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 | |
| 1557 | static 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 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 1564 | MagickExport 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 | |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 1578 | if (radius < 12.1) |
| 1579 | filteredImage=ComputeBlurImageSingle(image, channel, radius, sigma, exception); |
| 1580 | else if (splitImage(image) && (image->rows / 2 > radius)) |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 1581 | filteredImage=ComputeBlurImageSection(image, channel, radius, sigma, exception); |
| 1582 | else |
| 1583 | filteredImage=ComputeBlurImage(image, channel, radius, sigma, exception); |
| 1584 | |
| 1585 | return(filteredImage); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1588 | /* |
| 1589 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1590 | % % |
| 1591 | % % |
| 1592 | % % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1593 | % 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 % |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1594 | % % |
| 1595 | % % |
| 1596 | % % |
| 1597 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1598 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1599 | % RotationalBlurImage() applies a rotational blur to the image. |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1600 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1601 | % Andrew Protano contributed this effect. |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1602 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1603 | % 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) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1609 | % |
| 1610 | % A description of each parameter follows: |
| 1611 | % |
| 1612 | % o image: the image. |
| 1613 | % |
| 1614 | % o channel: the channel type. |
| 1615 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1616 | % o angle: the angle of the rotational blur. |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1617 | % |
| 1618 | % o exception: return any errors or warnings in this structure. |
| 1619 | % |
| 1620 | */ |
| 1621 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 1622 | static 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1712 | inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1713 | if (inputPixels == (const void *) NULL) |
| 1714 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1715 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1716 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1731 | length = image->columns * image->rows; |
| 1732 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1733 | if (clStatus != CL_SUCCESS) |
| 1734 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1735 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1736 | goto cleanup; |
| 1737 | } |
| 1738 | |
| 1739 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1740 | filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1741 | assert(filteredImage != NULL); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1742 | if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1743 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1744 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1745 | goto cleanup; |
| 1746 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 1747 | filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1748 | filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1749 | if (filteredPixels == (void *) NULL) |
| 1750 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1751 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1752 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1766 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1767 | filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1768 | if (clStatus != CL_SUCCESS) |
| 1769 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1770 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1771 | goto cleanup; |
| 1772 | } |
| 1773 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1774 | blurCenter.s[0] = (float) (image->columns-1)/2.0; |
| 1775 | blurCenter.s[1] = (float) (image->rows-1)/2.0; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1776 | 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 */ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1780 | sinThetaBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, cossin_theta_size * sizeof(float), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1781 | if (clStatus != CL_SUCCESS) |
| 1782 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1783 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1784 | goto cleanup; |
| 1785 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1786 | cosThetaBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, cossin_theta_size * sizeof(float), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1787 | if (clStatus != CL_SUCCESS) |
| 1788 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1789 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1790 | goto cleanup; |
| 1791 | } |
| 1792 | |
| 1793 | |
| 1794 | queue = AcquireOpenCLCommandQueue(clEnv); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1795 | sinThetaPtr = (float*) clEnv->library->clEnqueueMapBuffer(queue, sinThetaBuffer, CL_TRUE, CL_MAP_WRITE, 0, cossin_theta_size*sizeof(float), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1796 | if (clStatus != CL_SUCCESS) |
| 1797 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1798 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnqueuemapBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1799 | goto cleanup; |
| 1800 | } |
| 1801 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1802 | cosThetaPtr = (float*) clEnv->library->clEnqueueMapBuffer(queue, cosThetaBuffer, CL_TRUE, CL_MAP_WRITE, 0, cossin_theta_size*sizeof(float), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1803 | if (clStatus != CL_SUCCESS) |
| 1804 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1805 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnqueuemapBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1806 | 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 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1817 | clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, sinThetaBuffer, sinThetaPtr, 0, NULL, NULL); |
| 1818 | clStatus |= clEnv->library->clEnqueueUnmapMemObject(queue, cosThetaBuffer, cosThetaPtr, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1819 | if (clStatus != CL_SUCCESS) |
| 1820 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1821 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1822 | goto cleanup; |
| 1823 | } |
| 1824 | |
| 1825 | /* get the OpenCL kernel */ |
dirk | 6d612cf | 2014-03-13 21:17:23 +0000 | [diff] [blame] | 1826 | rotationalBlurKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "RotationalBlur"); |
| 1827 | if (rotationalBlurKernel == NULL) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1828 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1829 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1830 | goto cleanup; |
| 1831 | } |
| 1832 | |
| 1833 | |
| 1834 | /* set the kernel arguments */ |
| 1835 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1836 | clStatus=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(cl_mem),(void *)&imageBuffer); |
dirk | 6d612cf | 2014-03-13 21:17:23 +0000 | [diff] [blame] | 1837 | clStatus|=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(cl_mem),(void *)&filteredImageBuffer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1838 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1839 | GetPixelInfo(image,&bias); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1840 | biasPixel.s[0] = bias.red; |
| 1841 | biasPixel.s[1] = bias.green; |
| 1842 | biasPixel.s[2] = bias.blue; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1843 | biasPixel.s[3] = bias.alpha; |
dirk | 6d612cf | 2014-03-13 21:17:23 +0000 | [diff] [blame] | 1844 | clStatus|=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(cl_float4), &biasPixel); |
| 1845 | clStatus|=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(ChannelType), &channel); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1846 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1847 | matte = (image->alpha_trait==BlendPixelTrait)?1:0; |
dirk | 6d612cf | 2014-03-13 21:17:23 +0000 | [diff] [blame] | 1848 | clStatus|=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(unsigned int), &matte); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1849 | |
dirk | 6d612cf | 2014-03-13 21:17:23 +0000 | [diff] [blame] | 1850 | clStatus=clEnv->library->clSetKernelArg(rotationalBlurKernel,i++,sizeof(cl_float2), &blurCenter); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1851 | |
dirk | 6d612cf | 2014-03-13 21:17:23 +0000 | [diff] [blame] | 1852 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1855 | if (clStatus != CL_SUCCESS) |
| 1856 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1857 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1858 | goto cleanup; |
| 1859 | } |
| 1860 | |
| 1861 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1862 | global_work_size[0] = image->columns; |
| 1863 | global_work_size[1] = image->rows; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1864 | /* launch the kernel */ |
dirk | 6d612cf | 2014-03-13 21:17:23 +0000 | [diff] [blame] | 1865 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, rotationalBlurKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1866 | if (clStatus != CL_SUCCESS) |
| 1867 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1868 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1869 | goto cleanup; |
| 1870 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1871 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1872 | |
| 1873 | if (ALIGNED(filteredPixels,CLPixelPacket)) |
| 1874 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1875 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1876 | clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1877 | } |
| 1878 | else |
| 1879 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1880 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1881 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1882 | } |
| 1883 | if (clStatus != CL_SUCCESS) |
| 1884 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1885 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1886 | goto cleanup; |
| 1887 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 1888 | outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception); |
| 1889 | |
| 1890 | cleanup: |
| 1891 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 1892 | |
| 1893 | image_view=DestroyCacheView(image_view); |
| 1894 | if (filteredImage_view != NULL) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1895 | filteredImage_view=DestroyCacheView(filteredImage_view); |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 1896 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1897 | if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1898 | if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 1899 | if (sinThetaBuffer!=NULL) clEnv->library->clReleaseMemObject(sinThetaBuffer); |
| 1900 | if (cosThetaBuffer!=NULL) clEnv->library->clReleaseMemObject(cosThetaBuffer); |
dirk | 6d612cf | 2014-03-13 21:17:23 +0000 | [diff] [blame] | 1901 | if (rotationalBlurKernel!=NULL) RelinquishOpenCLKernel(clEnv, rotationalBlurKernel); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1902 | 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 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 1914 | MagickExport 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; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1931 | /* |
| 1932 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1933 | % % |
| 1934 | % % |
| 1935 | % % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1936 | % 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 % |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1937 | % % |
| 1938 | % % |
| 1939 | % % |
| 1940 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1941 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1942 | % 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. |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1946 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1947 | % The format of the UnsharpMaskImage method is: |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1948 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1949 | % Image *UnsharpMaskImage(const Image *image,const double radius, |
| 1950 | % const double sigma,const double amount,const double threshold, |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1951 | % ExceptionInfo *exception) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1952 | % Image *UnsharpMaskImageChannel(const Image *image, |
| 1953 | % const ChannelType channel,const double radius,const double sigma, |
| 1954 | % const double gain,const double threshold,ExceptionInfo *exception) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1955 | % |
| 1956 | % A description of each parameter follows: |
| 1957 | % |
| 1958 | % o image: the image. |
| 1959 | % |
| 1960 | % o channel: the channel type. |
| 1961 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 1962 | % 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. |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 1971 | % |
| 1972 | % o exception: return any errors or warnings in this structure. |
| 1973 | % |
| 1974 | */ |
| 1975 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 1976 | static 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2066 | inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2067 | if (inputPixels == (const void *) NULL) |
| 2068 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2069 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2070 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2085 | length = image->columns * image->rows; |
| 2086 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2087 | if (clStatus != CL_SUCCESS) |
| 2088 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2089 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2090 | goto cleanup; |
| 2091 | } |
| 2092 | } |
| 2093 | |
| 2094 | /* create output */ |
| 2095 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2096 | filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2097 | assert(filteredImage != NULL); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2098 | if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2099 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2100 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2101 | goto cleanup; |
| 2102 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 2103 | filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2104 | filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2105 | if (filteredPixels == (void *) NULL) |
| 2106 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2107 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2108 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2123 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2124 | filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2125 | if (clStatus != CL_SUCCESS) |
| 2126 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2127 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2128 | 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 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2138 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireKernelInfo failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2139 | goto cleanup; |
| 2140 | } |
| 2141 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2142 | imageKernelBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY, kernel->width * sizeof(float), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2143 | if (clStatus != CL_SUCCESS) |
| 2144 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2145 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2146 | goto cleanup; |
| 2147 | } |
| 2148 | |
| 2149 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2150 | kernelBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, imageKernelBuffer, CL_TRUE, CL_MAP_WRITE, 0, kernel->width * sizeof(float), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2151 | if (clStatus != CL_SUCCESS) |
| 2152 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2153 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2154 | goto cleanup; |
| 2155 | } |
| 2156 | for (i = 0; i < kernel->width; i++) |
| 2157 | { |
| 2158 | kernelBufferPtr[i] = (float) kernel->values[i]; |
| 2159 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2160 | clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, imageKernelBuffer, kernelBufferPtr, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2161 | if (clStatus != CL_SUCCESS) |
| 2162 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2163 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2164 | goto cleanup; |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | { |
| 2169 | /* create temp buffer */ |
| 2170 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2171 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2172 | tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length * 4 * sizeof(float), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2173 | if (clStatus != CL_SUCCESS) |
| 2174 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2175 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2176 | goto cleanup; |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | /* get the opencl kernel */ |
| 2181 | { |
| 2182 | blurRowKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "BlurRow"); |
| 2183 | if (blurRowKernel == NULL) |
| 2184 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2185 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2186 | goto cleanup; |
| 2187 | }; |
| 2188 | |
| 2189 | unsharpMaskBlurColumnKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "UnsharpMaskBlurColumn"); |
| 2190 | if (unsharpMaskBlurColumnKernel == NULL) |
| 2191 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2192 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2193 | goto cleanup; |
| 2194 | }; |
| 2195 | } |
| 2196 | |
| 2197 | { |
| 2198 | chunkSize = 256; |
| 2199 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2200 | imageColumns = image->columns; |
| 2201 | imageRows = image->rows; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2202 | |
| 2203 | kernelWidth = kernel->width; |
| 2204 | |
| 2205 | /* set the kernel arguments */ |
| 2206 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2207 | clStatus=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2208 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2215 | if (clStatus != CL_SUCCESS) |
| 2216 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2217 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2218 | goto cleanup; |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | /* launch the kernel */ |
| 2223 | { |
| 2224 | size_t gsize[2]; |
| 2225 | size_t wsize[2]; |
| 2226 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2227 | gsize[0] = chunkSize*((image->columns+chunkSize-1)/chunkSize); |
| 2228 | gsize[1] = image->rows; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2229 | wsize[0] = chunkSize; |
| 2230 | wsize[1] = 1; |
| 2231 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2232 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, blurRowKernel, 2, NULL, gsize, wsize, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2233 | if (clStatus != CL_SUCCESS) |
| 2234 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2235 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2236 | goto cleanup; |
| 2237 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2238 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | |
| 2242 | { |
| 2243 | chunkSize = 256; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2244 | imageColumns = image->columns; |
| 2245 | imageRows = image->rows; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2246 | kernelWidth = kernel->width; |
| 2247 | fGain = (float)gain; |
| 2248 | fThreshold = (float)threshold; |
| 2249 | |
| 2250 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2251 | clStatus=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(cl_mem),(void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2252 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2263 | |
| 2264 | if (clStatus != CL_SUCCESS) |
| 2265 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2266 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2267 | goto cleanup; |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | /* launch the kernel */ |
| 2272 | { |
| 2273 | size_t gsize[2]; |
| 2274 | size_t wsize[2]; |
| 2275 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2276 | gsize[0] = image->columns; |
| 2277 | gsize[1] = chunkSize*((image->rows+chunkSize-1)/chunkSize); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2278 | wsize[0] = 1; |
| 2279 | wsize[1] = chunkSize; |
| 2280 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2281 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, unsharpMaskBlurColumnKernel, 2, NULL, gsize, wsize, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2282 | if (clStatus != CL_SUCCESS) |
| 2283 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2284 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2285 | goto cleanup; |
| 2286 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2287 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | } |
| 2291 | |
| 2292 | /* get result */ |
| 2293 | if (ALIGNED(filteredPixels,CLPixelPacket)) |
| 2294 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2295 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2296 | clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2297 | } |
| 2298 | else |
| 2299 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2300 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2301 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2302 | } |
| 2303 | if (clStatus != CL_SUCCESS) |
| 2304 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2305 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2306 | goto cleanup; |
| 2307 | } |
| 2308 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 2309 | outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception); |
| 2310 | |
| 2311 | cleanup: |
| 2312 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 2313 | |
| 2314 | image_view=DestroyCacheView(image_view); |
| 2315 | if (filteredImage_view != NULL) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2316 | filteredImage_view=DestroyCacheView(filteredImage_view); |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2317 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2318 | if (kernel != NULL) kernel=DestroyKernelInfo(kernel); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2319 | if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2320 | if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer); |
| 2321 | if (tempImageBuffer!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer); |
| 2322 | if (imageKernelBuffer!=NULL) clEnv->library->clReleaseMemObject(imageKernelBuffer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2323 | 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 | } |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2334 | return(filteredImage); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 2337 | static 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2427 | inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2428 | if (inputPixels == (const void *) NULL) |
| 2429 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2430 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2431 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2446 | length = image->columns * image->rows; |
| 2447 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2448 | if (clStatus != CL_SUCCESS) |
| 2449 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2450 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2451 | goto cleanup; |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | /* create output */ |
| 2456 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2457 | filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2458 | assert(filteredImage != NULL); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2459 | if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2460 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2461 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2462 | goto cleanup; |
| 2463 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 2464 | filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2465 | filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2466 | if (filteredPixels == (void *) NULL) |
| 2467 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2468 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2469 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2484 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2485 | filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2486 | if (clStatus != CL_SUCCESS) |
| 2487 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2488 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2489 | 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 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2499 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireKernelInfo failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2500 | goto cleanup; |
| 2501 | } |
| 2502 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2503 | imageKernelBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY, kernel->width * sizeof(float), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2504 | if (clStatus != CL_SUCCESS) |
| 2505 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2506 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2507 | goto cleanup; |
| 2508 | } |
| 2509 | |
| 2510 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2511 | kernelBufferPtr = (float*)clEnv->library->clEnqueueMapBuffer(queue, imageKernelBuffer, CL_TRUE, CL_MAP_WRITE, 0, kernel->width * sizeof(float), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2512 | if (clStatus != CL_SUCCESS) |
| 2513 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2514 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2515 | goto cleanup; |
| 2516 | } |
| 2517 | for (i = 0; i < kernel->width; i++) |
| 2518 | { |
| 2519 | kernelBufferPtr[i] = (float) kernel->values[i]; |
| 2520 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2521 | clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, imageKernelBuffer, kernelBufferPtr, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2522 | if (clStatus != CL_SUCCESS) |
| 2523 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2524 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2525 | goto cleanup; |
| 2526 | } |
| 2527 | } |
| 2528 | |
| 2529 | { |
| 2530 | unsigned int offsetRows; |
| 2531 | unsigned int sec; |
| 2532 | |
| 2533 | /* create temp buffer */ |
| 2534 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2535 | length = image->columns * (image->rows / 2 + 1 + (kernel->width-1) / 2); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2536 | tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length * 4 * sizeof(float), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2537 | if (clStatus != CL_SUCCESS) |
| 2538 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2539 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2540 | goto cleanup; |
| 2541 | } |
| 2542 | } |
| 2543 | |
| 2544 | /* get the opencl kernel */ |
| 2545 | { |
| 2546 | blurRowKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "BlurRowSection"); |
| 2547 | if (blurRowKernel == NULL) |
| 2548 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2549 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2550 | goto cleanup; |
| 2551 | }; |
| 2552 | |
| 2553 | unsharpMaskBlurColumnKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "UnsharpMaskBlurColumnSection"); |
| 2554 | if (unsharpMaskBlurColumnKernel == NULL) |
| 2555 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2556 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2557 | goto cleanup; |
| 2558 | }; |
| 2559 | } |
| 2560 | |
| 2561 | for (sec = 0; sec < 2; sec++) |
| 2562 | { |
| 2563 | { |
| 2564 | chunkSize = 256; |
| 2565 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2566 | imageColumns = image->columns; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2567 | if (sec == 0) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2568 | imageRows = image->rows / 2 + (kernel->width-1) / 2; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2569 | else |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2570 | imageRows = (image->rows - image->rows / 2) + (kernel->width-1) / 2; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2571 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2572 | offsetRows = sec * image->rows / 2; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2573 | |
| 2574 | kernelWidth = kernel->width; |
| 2575 | |
| 2576 | /* set the kernel arguments */ |
| 2577 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2578 | clStatus=clEnv->library->clSetKernelArg(blurRowKernel,i++,sizeof(cl_mem),(void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2579 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2588 | if (clStatus != CL_SUCCESS) |
| 2589 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2590 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2591 | 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 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2604 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, blurRowKernel, 2, NULL, gsize, wsize, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2605 | if (clStatus != CL_SUCCESS) |
| 2606 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2607 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2608 | goto cleanup; |
| 2609 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2610 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | |
| 2614 | { |
| 2615 | chunkSize = 256; |
| 2616 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2617 | imageColumns = image->columns; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2618 | if (sec == 0) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2619 | imageRows = image->rows / 2; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2620 | else |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2621 | imageRows = (image->rows - image->rows / 2); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2622 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2623 | offsetRows = sec * image->rows / 2; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2624 | |
| 2625 | kernelWidth = kernel->width; |
| 2626 | |
| 2627 | fGain = (float)gain; |
| 2628 | fThreshold = (float)threshold; |
| 2629 | |
| 2630 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2631 | clStatus=clEnv->library->clSetKernelArg(unsharpMaskBlurColumnKernel,i++,sizeof(cl_mem),(void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2632 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2645 | |
| 2646 | if (clStatus != CL_SUCCESS) |
| 2647 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2648 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2649 | 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 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2663 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, unsharpMaskBlurColumnKernel, 2, NULL, gsize, wsize, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2664 | if (clStatus != CL_SUCCESS) |
| 2665 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2666 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2667 | goto cleanup; |
| 2668 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2669 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2670 | } |
| 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | /* get result */ |
| 2675 | if (ALIGNED(filteredPixels,CLPixelPacket)) |
| 2676 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2677 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2678 | clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2679 | } |
| 2680 | else |
| 2681 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2682 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2683 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2684 | } |
| 2685 | if (clStatus != CL_SUCCESS) |
| 2686 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2687 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2688 | goto cleanup; |
| 2689 | } |
| 2690 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 2691 | outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception); |
| 2692 | |
| 2693 | cleanup: |
| 2694 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 2695 | |
| 2696 | image_view=DestroyCacheView(image_view); |
| 2697 | if (filteredImage_view != NULL) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2698 | filteredImage_view=DestroyCacheView(filteredImage_view); |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 2699 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2700 | if (kernel != NULL) kernel=DestroyKernelInfo(kernel); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 2701 | if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 2702 | if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer); |
| 2703 | if (tempImageBuffer!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer); |
| 2704 | if (imageKernelBuffer!=NULL) clEnv->library->clReleaseMemObject(imageKernelBuffer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 2705 | 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; |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 2719 | static 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 | |
| 2982 | cleanup: |
| 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 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 3007 | MagickExport 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 | |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 3021 | if (radius < 12.1) |
| 3022 | filteredImage = ComputeUnsharpMaskImageSingle(image,channel,radius,sigma,gain,threshold, 0, exception); |
| 3023 | else if (splitImage(image) && (image->rows / 2 > radius)) |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 3024 | filteredImage = ComputeUnsharpMaskImageSection(image,channel,radius,sigma,gain,threshold,exception); |
| 3025 | else |
| 3026 | filteredImage = ComputeUnsharpMaskImage(image,channel,radius,sigma,gain,threshold,exception); |
| 3027 | return(filteredImage); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3030 | /* |
| 3031 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3032 | % % |
| 3033 | % % |
| 3034 | % % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3035 | % A c c e l e r a t e R e s i z e I m a g e % |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3036 | % % |
| 3037 | % % |
| 3038 | % % |
| 3039 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3040 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3041 | % AccelerateResizeImage() is an OpenCL implementation of ResizeImage() |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3042 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3043 | % AccelerateResizeImage() scales an image to the desired dimensions, using the given |
| 3044 | % filter (see AcquireFilterInfo()). |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3045 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3046 | % 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) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3057 | % |
| 3058 | % A description of each parameter follows: |
| 3059 | % |
| 3060 | % o image: the image. |
| 3061 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3062 | % o columns: the number of columns in the scaled image. |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3063 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3064 | % o rows: the number of rows in the scaled image. |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3065 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3066 | % o filter: Image filter to use. |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3067 | % |
| 3068 | % o exception: return any errors or warnings in this structure. |
| 3069 | % |
| 3070 | */ |
| 3071 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 3072 | static 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; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3122 | status = MagickFalse; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3123 | |
| 3124 | /* |
| 3125 | Apply filter to resize vertically from image to resize image. |
| 3126 | */ |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 3127 | scale=MAGICK_MAX(1.0/xFactor+MagickEpsilon,1.0); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3128 | 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 | |
dirk | e3c5f89 | 2013-12-10 06:04:40 +0000 | [diff] [blame] | 3154 | DisableMSCWarning(4127) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3155 | while(1) |
dirk | e3c5f89 | 2013-12-10 06:04:40 +0000 | [diff] [blame] | 3156 | RestoreMSCWarning |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3157 | { |
| 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 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 3210 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3211 | goto cleanup; |
| 3212 | } |
| 3213 | |
| 3214 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3215 | 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3218 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3221 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3222 | clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&resizedColumns); |
| 3223 | clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&resizedRows); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3224 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3225 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3228 | |
| 3229 | resizeFilterScale = (float) GetResizeFilterScale(resizeFilter); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3230 | clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterScale); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3231 | |
| 3232 | resizeFilterSupport = (float) GetResizeFilterSupport(resizeFilter); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3233 | clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterSupport); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3234 | |
| 3235 | resizeFilterWindowSupport = (float) GetResizeFilterWindowSupport(resizeFilter); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3236 | clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterWindowSupport); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3237 | |
| 3238 | resizeFilterBlur = (float) GetResizeFilterBlur(resizeFilter); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3239 | clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterBlur); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3240 | |
| 3241 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3242 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3246 | |
| 3247 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3248 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3251 | |
| 3252 | if (clStatus != CL_SUCCESS) |
| 3253 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3254 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3255 | 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; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3263 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, horizontalKernel, 2, NULL, global_work_size, local_work_size, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3264 | if (clStatus != CL_SUCCESS) |
| 3265 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3266 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3267 | goto cleanup; |
| 3268 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3269 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3270 | status = MagickTrue; |
| 3271 | |
| 3272 | |
| 3273 | cleanup: |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 3274 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 3275 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3276 | if (horizontalKernel != NULL) RelinquishOpenCLKernel(clEnv, horizontalKernel); |
| 3277 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 3278 | return(status); |
| 3279 | } |
| 3280 | |
| 3281 | static 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; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3331 | status = MagickFalse; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3332 | |
| 3333 | /* |
| 3334 | Apply filter to resize vertically from image to resize image. |
| 3335 | */ |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 3336 | scale=MAGICK_MAX(1.0/yFactor+MagickEpsilon,1.0); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3337 | 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 | |
dirk | e3c5f89 | 2013-12-10 06:04:40 +0000 | [diff] [blame] | 3363 | DisableMSCWarning(4127) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3364 | while(1) |
dirk | e3c5f89 | 2013-12-10 06:04:40 +0000 | [diff] [blame] | 3365 | RestoreMSCWarning |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3366 | { |
| 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 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 3415 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3416 | goto cleanup; |
| 3417 | } |
| 3418 | |
| 3419 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3420 | 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3423 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3426 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3427 | clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&resizedColumns); |
| 3428 | clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(unsigned int), (void*)&resizedRows); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3429 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3430 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3433 | |
| 3434 | resizeFilterScale = (float) GetResizeFilterScale(resizeFilter); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3435 | clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterScale); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3436 | |
| 3437 | resizeFilterSupport = (float) GetResizeFilterSupport(resizeFilter); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3438 | clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterSupport); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3439 | |
| 3440 | resizeFilterWindowSupport = (float) GetResizeFilterWindowSupport(resizeFilter); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3441 | clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterWindowSupport); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3442 | |
| 3443 | resizeFilterBlur = (float) GetResizeFilterBlur(resizeFilter); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3444 | clStatus |= clEnv->library->clSetKernelArg(horizontalKernel, i++, sizeof(float), (void*)&resizeFilterBlur); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3445 | |
| 3446 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3447 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3451 | |
| 3452 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3453 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3456 | |
| 3457 | if (clStatus != CL_SUCCESS) |
| 3458 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3459 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3460 | 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; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3468 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, horizontalKernel, 2, NULL, global_work_size, local_work_size, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3469 | if (clStatus != CL_SUCCESS) |
| 3470 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3471 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3472 | goto cleanup; |
| 3473 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3474 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3475 | status = MagickTrue; |
| 3476 | |
| 3477 | |
| 3478 | cleanup: |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 3479 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 3480 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3481 | if (horizontalKernel != NULL) RelinquishOpenCLKernel(clEnv, horizontalKernel); |
| 3482 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3483 | return(status); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 3486 | static 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3559 | inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3560 | if (inputPixels == (const void *) NULL) |
| 3561 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3562 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3563 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3578 | length = image->columns * image->rows; |
| 3579 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3580 | if (clStatus != CL_SUCCESS) |
| 3581 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3582 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3583 | goto cleanup; |
| 3584 | } |
| 3585 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3586 | cubicCoefficientsBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_ONLY, 7 * sizeof(float), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3587 | if (clStatus != CL_SUCCESS) |
| 3588 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3589 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3590 | goto cleanup; |
| 3591 | } |
| 3592 | queue = AcquireOpenCLCommandQueue(clEnv); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3593 | mappedCoefficientBuffer = (float*)clEnv->library->clEnqueueMapBuffer(queue, cubicCoefficientsBuffer, CL_TRUE, CL_MAP_WRITE, 0, 7 * sizeof(float) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3594 | , 0, NULL, NULL, &clStatus); |
| 3595 | if (clStatus != CL_SUCCESS) |
| 3596 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3597 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueMapBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3598 | goto cleanup; |
| 3599 | } |
| 3600 | resizeFilterCoefficient = GetResizeFilterCoefficient(resizeFilter); |
| 3601 | for (i = 0; i < 7; i++) |
| 3602 | { |
| 3603 | mappedCoefficientBuffer[i] = (float) resizeFilterCoefficient[i]; |
| 3604 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3605 | clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, cubicCoefficientsBuffer, mappedCoefficientBuffer, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3606 | if (clStatus != CL_SUCCESS) |
| 3607 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3608 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3609 | goto cleanup; |
| 3610 | } |
| 3611 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3612 | filteredImage = CloneImage(image,resizedColumns,resizedRows,MagickTrue,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3613 | if (filteredImage == NULL) |
| 3614 | goto cleanup; |
| 3615 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3616 | if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3617 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 3618 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3619 | goto cleanup; |
| 3620 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 3621 | filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3622 | filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3623 | if (filteredPixels == (void *) NULL) |
| 3624 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 3625 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3626 | 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; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3642 | filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3643 | if (clStatus != CL_SUCCESS) |
| 3644 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3645 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3646 | goto cleanup; |
| 3647 | } |
| 3648 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3649 | xFactor=(float) resizedColumns/(float) image->columns; |
| 3650 | yFactor=(float) resizedRows/(float) image->rows; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3651 | if (xFactor > yFactor) |
| 3652 | { |
| 3653 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3654 | length = resizedColumns*image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3655 | tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length*sizeof(CLPixelPacket), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3656 | if (clStatus != CL_SUCCESS) |
| 3657 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3658 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3659 | goto cleanup; |
| 3660 | } |
| 3661 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3662 | status = resizeHorizontalFilter(imageBuffer, image->columns, image->rows, (image->alpha_trait==BlendPixelTrait)?1:0 |
| 3663 | , tempImageBuffer, resizedColumns, image->rows |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3664 | , resizeFilter, cubicCoefficientsBuffer |
| 3665 | , xFactor, clEnv, queue, exception); |
| 3666 | if (status != MagickTrue) |
| 3667 | goto cleanup; |
| 3668 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3669 | status = resizeVerticalFilter(tempImageBuffer, resizedColumns, image->rows, (image->alpha_trait==BlendPixelTrait)?1:0 |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3670 | , filteredImageBuffer, resizedColumns, resizedRows |
| 3671 | , resizeFilter, cubicCoefficientsBuffer |
| 3672 | , yFactor, clEnv, queue, exception); |
| 3673 | if (status != MagickTrue) |
| 3674 | goto cleanup; |
| 3675 | } |
| 3676 | else |
| 3677 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3678 | length = image->columns*resizedRows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3679 | tempImageBuffer = clEnv->library->clCreateBuffer(context, CL_MEM_READ_WRITE, length*sizeof(CLPixelPacket), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3680 | if (clStatus != CL_SUCCESS) |
| 3681 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3682 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3683 | goto cleanup; |
| 3684 | } |
| 3685 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3686 | status = resizeVerticalFilter(imageBuffer, image->columns, image->rows, (image->alpha_trait==BlendPixelTrait)?1:0 |
| 3687 | , tempImageBuffer, image->columns, resizedRows |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3688 | , resizeFilter, cubicCoefficientsBuffer |
| 3689 | , yFactor, clEnv, queue, exception); |
| 3690 | if (status != MagickTrue) |
| 3691 | goto cleanup; |
| 3692 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3693 | status = resizeHorizontalFilter(tempImageBuffer, image->columns, resizedRows, (image->alpha_trait==BlendPixelTrait)?1:0 |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3694 | , 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 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3703 | clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3704 | } |
| 3705 | else |
| 3706 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3707 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3708 | } |
| 3709 | if (clStatus != CL_SUCCESS) |
| 3710 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 3711 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3712 | goto cleanup; |
| 3713 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 3714 | outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception); |
| 3715 | |
| 3716 | cleanup: |
| 3717 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 3718 | |
| 3719 | image_view=DestroyCacheView(image_view); |
| 3720 | if (filteredImage_view != NULL) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3721 | filteredImage_view=DestroyCacheView(filteredImage_view); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3722 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3723 | if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3724 | if (tempImageBuffer!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer); |
| 3725 | if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer); |
| 3726 | if (cubicCoefficientsBuffer!=NULL) clEnv->library->clReleaseMemObject(cubicCoefficientsBuffer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3727 | if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3728 | if (outputReady == MagickFalse && filteredImage != NULL) |
| 3729 | filteredImage=DestroyImage(filteredImage); |
| 3730 | return(filteredImage); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 3733 | const ResizeWeightingFunctionType supportedResizeWeighting[] = |
| 3734 | { |
| 3735 | BoxWeightingFunction, |
| 3736 | TriangleWeightingFunction, |
| 3737 | HanningWeightingFunction, |
| 3738 | HammingWeightingFunction, |
| 3739 | BlackmanWeightingFunction, |
| 3740 | CubicBCWeightingFunction, |
| 3741 | SincWeightingFunction, |
| 3742 | SincFastWeightingFunction, |
| 3743 | LastWeightingFunction |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3744 | }; |
| 3745 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 3746 | static 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 | |
| 3762 | MagickExport 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3782 | } |
| 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 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 3812 | static 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3868 | inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3869 | if (inputPixels == (void *) NULL) |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 3870 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3871 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3872 | goto cleanup; |
cristy | d43a46b | 2010-01-21 02:13:41 +0000 | [diff] [blame] | 3873 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3874 | |
| 3875 | /* If the host pointer is aligned to the size of CLPixelPacket, |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3876 | then use the host buffer directly from the GPU; otherwise, |
| 3877 | create a buffer on the GPU and copy the data over */ |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3878 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3887 | length = image->columns * image->rows; |
| 3888 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3889 | if (clStatus != CL_SUCCESS) |
| 3890 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3891 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3892 | goto cleanup; |
| 3893 | } |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3894 | |
| 3895 | filterKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Contrast"); |
| 3896 | if (filterKernel == NULL) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3897 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 3898 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3899 | goto cleanup; |
| 3900 | } |
| 3901 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3902 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3903 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3907 | if (clStatus != CL_SUCCESS) |
| 3908 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 3909 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3910 | goto cleanup; |
| 3911 | } |
| 3912 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3913 | 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) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3919 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3920 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
| 3921 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3922 | } |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3923 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3924 | |
| 3925 | if (ALIGNED(inputPixels,CLPixelPacket)) |
| 3926 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3927 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3929 | } |
| 3930 | else |
| 3931 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3932 | length = image->columns * image->rows; |
| 3933 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3934 | } |
| 3935 | if (clStatus != CL_SUCCESS) |
| 3936 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 3937 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3938 | goto cleanup; |
| 3939 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 3940 | outputReady=SyncCacheViewAuthenticPixels(image_view,exception); |
| 3941 | |
| 3942 | cleanup: |
| 3943 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 3944 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3945 | image_view=DestroyCacheView(image_view); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3946 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 3947 | if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer); |
| 3948 | if (filterKernel!=NULL) RelinquishOpenCLKernel(clEnv, filterKernel); |
| 3949 | if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue); |
| 3950 | return(outputReady); |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
| 3953 | MagickExport 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); |
cristy | 3f6d148 | 2010-01-20 21:01:21 +0000 | [diff] [blame] | 3968 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 3969 | |
| 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 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 4000 | MagickBooleanType 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4069 | inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4070 | if (inputPixels == (void *) NULL) |
| 4071 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4072 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4073 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4089 | length = image->columns * image->rows; |
| 4090 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4091 | if (clStatus != CL_SUCCESS) |
| 4092 | { |
| 4093 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
| 4094 | goto cleanup; |
| 4095 | } |
| 4096 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4097 | modulateKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Modulate"); |
| 4098 | if (modulateKernel == NULL) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4099 | { |
| 4100 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
| 4101 | goto cleanup; |
| 4102 | } |
| 4103 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4104 | bright=percent_brightness; |
| 4105 | hue=percent_hue; |
| 4106 | saturation=percent_saturation; |
| 4107 | color=colorspace; |
| 4108 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4109 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4110 | 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4115 | 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]; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4124 | global_work_size[0] = image->columns; |
| 4125 | global_work_size[1] = image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4126 | /* launch the kernel */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4127 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, modulateKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4128 | 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 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4138 | 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4140 | } |
| 4141 | else |
| 4142 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4143 | length = image->columns * image->rows; |
| 4144 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4145 | } |
| 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 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 4152 | outputReady=SyncCacheViewAuthenticPixels(image_view,exception); |
| 4153 | |
| 4154 | cleanup: |
| 4155 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 4156 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4157 | image_view=DestroyCacheView(image_view); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4158 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4159 | if (imageBuffer!=NULL) |
| 4160 | clEnv->library->clReleaseMemObject(imageBuffer); |
| 4161 | if (modulateKernel!=NULL) |
| 4162 | RelinquishOpenCLKernel(clEnv, modulateKernel); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4163 | if (queue != NULL) |
| 4164 | RelinquishOpenCLCommandQueue(clEnv, queue); |
| 4165 | |
| 4166 | return outputReady; |
| 4167 | |
| 4168 | } |
| 4169 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 4170 | MagickExport 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4189 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4190 | |
| 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 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 4213 | MagickBooleanType 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4278 | inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4279 | if (inputPixels == (void *) NULL) |
| 4280 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4281 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4282 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4298 | length = image->columns * image->rows; |
| 4299 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4300 | if (clStatus != CL_SUCCESS) |
| 4301 | { |
| 4302 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
| 4303 | goto cleanup; |
| 4304 | } |
| 4305 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4306 | negateKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Negate"); |
| 4307 | if (negateKernel == NULL) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4308 | { |
| 4309 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
| 4310 | goto cleanup; |
| 4311 | } |
| 4312 | |
| 4313 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4314 | clStatus=clEnv->library->clSetKernelArg(negateKernel,i++,sizeof(cl_mem),(void *)&imageBuffer); |
| 4315 | clStatus=clEnv->library->clSetKernelArg(negateKernel,i++,sizeof(ChannelType),(void *)&channel); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4316 | 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]; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4325 | global_work_size[0] = image->columns; |
| 4326 | global_work_size[1] = image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4327 | /* launch the kernel */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4328 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, negateKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4329 | 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 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4339 | 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4341 | } |
| 4342 | else |
| 4343 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4344 | length = image->columns * image->rows; |
| 4345 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4346 | } |
| 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 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 4353 | outputReady=SyncCacheViewAuthenticPixels(image_view,exception); |
| 4354 | |
| 4355 | cleanup: |
| 4356 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 4357 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4358 | image_view=DestroyCacheView(image_view); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4359 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4360 | if (imageBuffer!=NULL) |
| 4361 | clEnv->library->clReleaseMemObject(imageBuffer); |
| 4362 | if (negateKernel!=NULL) |
| 4363 | RelinquishOpenCLKernel(clEnv, negateKernel); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4364 | if (queue != NULL) |
| 4365 | RelinquishOpenCLCommandQueue(clEnv, queue); |
| 4366 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4367 | return(outputReady); |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | MagickExport 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4386 | } |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4387 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4388 | /* |
| 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 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 4414 | MagickBooleanType 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4480 | 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 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4486 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4487 | /* 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 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4507 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4508 | intensityMethod = method; |
| 4509 | colorspace = image->colorspace; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4510 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4511 | 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 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4517 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4518 | 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 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4528 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4529 | { |
| 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 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4542 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4543 | 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 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4558 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 4559 | outputReady=SyncCacheViewAuthenticPixels(image_view,exception); |
| 4560 | |
| 4561 | cleanup: |
| 4562 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 4563 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4564 | 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); |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | MagickExport 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4599 | /* |
| 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 | */ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4625 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 4626 | static 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 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4647 | histogramKernel = NULL; |
| 4648 | |
| 4649 | outputReady = MagickFalse; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4650 | method = image->intensity; |
| 4651 | colorspace = image->colorspace; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4652 | |
| 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; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4663 | clStatus=clEnv->library->clSetKernelArg(histogramKernel,i++,sizeof(cl_mem),(void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4664 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4675 | global_work_size[0] = image->columns; |
| 4676 | global_work_size[1] = image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4677 | |
| 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 | |
| 4689 | cleanup: |
| 4690 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 4691 | |
| 4692 | if (histogramKernel!=NULL) |
| 4693 | RelinquishOpenCLKernel(clEnv, histogramKernel); |
| 4694 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4695 | return(outputReady); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 4698 | MagickExport 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4798 | inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4799 | |
| 4800 | if (inputPixels == (void *) NULL) |
| 4801 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4802 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4803 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4817 | length = image->columns * image->rows; |
| 4818 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4819 | if (clStatus != CL_SUCCESS) |
| 4820 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4821 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4822 | goto cleanup; |
| 4823 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4824 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4825 | /* 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4840 | histogramBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(cl_uint4), hostPtr, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4841 | if (clStatus != CL_SUCCESS) |
| 4842 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4843 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4844 | goto cleanup; |
| 4845 | } |
| 4846 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4847 | status = LaunchHistogramKernel(clEnv, queue, imageBuffer, histogramBuffer, image, channel, exception); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4848 | if (status == MagickFalse) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4849 | goto cleanup; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4850 | |
| 4851 | /* read from the kenel output */ |
| 4852 | if (ALIGNED(histogram,cl_uint4)) |
| 4853 | { |
| 4854 | length = (MaxMap+1); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4855 | clEnv->library->clEnqueueMapBuffer(queue, histogramBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(cl_uint4), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4856 | } |
| 4857 | else |
| 4858 | { |
| 4859 | length = (MaxMap+1); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4860 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, histogramBuffer, CL_TRUE, 0, length * sizeof(cl_uint4), histogram, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4861 | } |
| 4862 | if (clStatus != CL_SUCCESS) |
| 4863 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 4864 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4865 | goto cleanup; |
| 4866 | } |
| 4867 | |
| 4868 | /* unmap, don't block gpu to use this buffer again. */ |
| 4869 | if (ALIGNED(histogram,cl_uint4)) |
| 4870 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4871 | clStatus = clEnv->library->clEnqueueUnmapMemObject(queue, histogramBuffer, histogram, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4872 | if (clStatus != CL_SUCCESS) |
| 4873 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4874 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueUnmapMemObject failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4875 | goto cleanup; |
| 4876 | } |
| 4877 | } |
| 4878 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4879 | /* recreate input buffer later, in case image updated */ |
| 4880 | #ifdef RECREATEBUFFER |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4881 | if (imageBuffer!=NULL) |
| 4882 | clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4883 | #endif |
| 4884 | |
| 4885 | /* CPU stuff */ |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4886 | equalize_map=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL, sizeof(*equalize_map)); |
| 4887 | if (equalize_map == (PixelPacket *) NULL) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4888 | ThrowBinaryException(ResourceLimitWarning,"MemoryAllocationFailed", image->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4889 | |
| 4890 | map=(FloatPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*map)); |
| 4891 | if (map == (FloatPixelPacket *) NULL) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4892 | ThrowBinaryException(ResourceLimitWarning,"MemoryAllocationFailed", image->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4893 | |
| 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) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4901 | { |
| 4902 | intensity.red+=histogram[i].s[2]; |
| 4903 | map[i]=intensity; |
| 4904 | continue; |
| 4905 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4906 | 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) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4913 | intensity.alpha+=histogram[i].s[3]; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4914 | /* |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4915 | if (((channel & IndexChannel) != 0) && |
| 4916 | (image->colorspace == CMYKColorspace)) |
| 4917 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4918 | intensity.index+=histogram[i].index; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4919 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4920 | */ |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4921 | 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) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4929 | { |
| 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 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4935 | if (((channel & RedChannel) != 0) && (white.red != black.red)) |
| 4936 | equalize_map[i].red=ScaleMapToQuantum((MagickRealType) ((MaxMap* |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4937 | (map[i].red-black.red))/(white.red-black.red))); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4938 | if (((channel & GreenChannel) != 0) && (white.green != black.green)) |
| 4939 | equalize_map[i].green=ScaleMapToQuantum((MagickRealType) ((MaxMap* |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4940 | (map[i].green-black.green))/(white.green-black.green))); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4941 | if (((channel & BlueChannel) != 0) && (white.blue != black.blue)) |
| 4942 | equalize_map[i].blue=ScaleMapToQuantum((MagickRealType) ((MaxMap* |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4943 | (map[i].blue-black.blue))/(white.blue-black.blue))); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4944 | 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))); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4947 | /* |
| 4948 | if ((((channel & IndexChannel) != 0) && |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4949 | (image->colorspace == CMYKColorspace)) && |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4950 | (white.index != black.index)) |
| 4951 | equalize_map[i].index=ScaleMapToQuantum((MagickRealType) ((MaxMap* |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4952 | (map[i].index-black.index))/(white.index-black.index))); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4953 | */ |
| 4954 | } |
| 4955 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4956 | if (image->storage_class == PseudoClass) |
| 4957 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4958 | /* |
| 4959 | Equalize colormap. |
| 4960 | */ |
| 4961 | for (i=0; i < (ssize_t) image->colors; i++) |
| 4962 | { |
| 4963 | if ((channel & SyncChannels) != 0) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4964 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4965 | if (white.red != black.red) |
| 4966 | { |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4967 | image->colormap[i].red=equalize_map[ |
| 4968 | ScaleQuantumToMap(image->colormap[i].red)].red; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4969 | image->colormap[i].green=equalize_map[ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4970 | ScaleQuantumToMap(image->colormap[i].green)].red; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4971 | image->colormap[i].blue=equalize_map[ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4972 | ScaleQuantumToMap(image->colormap[i].blue)].red; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4973 | image->colormap[i].alpha=equalize_map[ |
| 4974 | ScaleQuantumToMap(image->colormap[i].alpha)].red; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4975 | } |
| 4976 | continue; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4977 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4978 | 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) && |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 4988 | (white.alpha != black.alpha)) |
| 4989 | image->colormap[i].alpha=equalize_map[ |
| 4990 | ScaleQuantumToMap(image->colormap[i].alpha)].alpha; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 4991 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 4992 | } |
| 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 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5003 | #ifdef RECREATEBUFFER |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5004 | /* 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5016 | length = image->columns * image->rows; |
| 5017 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5018 | if (clStatus != CL_SUCCESS) |
| 5019 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5020 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5021 | goto cleanup; |
| 5022 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5023 | #endif |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5024 | |
| 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5038 | equalizeMapBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(PixelPacket), hostPtr, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5039 | if (clStatus != CL_SUCCESS) |
| 5040 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5041 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5042 | goto cleanup; |
| 5043 | } |
| 5044 | |
| 5045 | /* get the OpenCL kernel */ |
| 5046 | equalizeKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "Equalize"); |
| 5047 | if (equalizeKernel == NULL) |
| 5048 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 5049 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "AcquireOpenCLKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5050 | goto cleanup; |
| 5051 | } |
| 5052 | |
| 5053 | /* set the kernel arguments */ |
| 5054 | i = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5055 | clStatus=clEnv->library->clSetKernelArg(equalizeKernel,i++,sizeof(cl_mem),(void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5056 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5060 | if (clStatus != CL_SUCCESS) |
| 5061 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5062 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5063 | goto cleanup; |
| 5064 | } |
| 5065 | |
| 5066 | /* launch the kernel */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5067 | global_work_size[0] = image->columns; |
| 5068 | global_work_size[1] = image->rows; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5069 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5070 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, equalizeKernel, 2, NULL, global_work_size, NULL, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5071 | |
| 5072 | if (clStatus != CL_SUCCESS) |
| 5073 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5074 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5075 | goto cleanup; |
| 5076 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5077 | clEnv->library->clFlush(queue); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5078 | |
| 5079 | /* read the data back */ |
| 5080 | if (ALIGNED(inputPixels,CLPixelPacket)) |
| 5081 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5082 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5084 | } |
| 5085 | else |
| 5086 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5087 | length = image->columns * image->rows; |
| 5088 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5089 | } |
| 5090 | if (clStatus != CL_SUCCESS) |
| 5091 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 5092 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5093 | goto cleanup; |
| 5094 | } |
| 5095 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 5096 | outputReady=SyncCacheViewAuthenticPixels(image_view,exception); |
| 5097 | |
| 5098 | cleanup: |
| 5099 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 5100 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5101 | image_view=DestroyCacheView(image_view); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5102 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5103 | if (imageBuffer!=NULL) |
| 5104 | clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5105 | |
| 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 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5114 | if (histogramBuffer!=NULL) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5115 | clEnv->library->clReleaseMemObject(histogramBuffer); |
| 5116 | if (histogram!=NULL) |
| 5117 | histogram=(cl_uint4 *) RelinquishMagickMemory(histogram); |
| 5118 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5119 | if (histogramKernel!=NULL) |
| 5120 | RelinquishOpenCLKernel(clEnv, histogramKernel); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5121 | if (equalizeKernel!=NULL) |
| 5122 | RelinquishOpenCLKernel(clEnv, equalizeKernel); |
| 5123 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5124 | if (queue != NULL) |
| 5125 | RelinquishOpenCLCommandQueue(clEnv, queue); |
| 5126 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5127 | return(outputReady); |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 5128 | } |
| 5129 | |
| 5130 | MagickExport 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | /* |
| 5149 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 5150 | % % |
| 5151 | % % |
| 5152 | % % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5153 | % 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 % |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5154 | % % |
| 5155 | % % |
| 5156 | % % |
| 5157 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 5158 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5159 | % 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. |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5165 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5166 | % The format of the ContrastStretchImage method is: |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5167 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5168 | % 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) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5173 | % |
| 5174 | % A description of each parameter follows: |
| 5175 | % |
| 5176 | % o image: the image. |
| 5177 | % |
| 5178 | % o channel: the channel. |
| 5179 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5180 | % 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 | % |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5187 | */ |
| 5188 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 5189 | MagickExport 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5306 | inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5307 | |
| 5308 | if (inputPixels == (void *) NULL) |
| 5309 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5310 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5311 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5325 | length = image->columns * image->rows; |
| 5326 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5327 | 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 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5355 | status = LaunchHistogramKernel(clEnv, queue, imageBuffer, histogramBuffer, image, channel, exception); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5356 | 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 |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5389 | if (imageBuffer!=NULL) |
| 5390 | clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5391 | #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 | } |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5460 | black.alpha=0.0; |
| 5461 | white.alpha=MaxRange(QuantumRange); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5462 | 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 | } |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5471 | black.alpha=(MagickRealType) i; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5472 | 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 | } |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5479 | white.alpha=(MagickRealType) i; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5480 | } |
| 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 | |
cristy | 931c806 | 2014-03-10 13:19:17 +0000 | [diff] [blame] | 5509 | if (stretch_map == (PixelPacket *) NULL) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5510 | 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 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5557 | if (i < (ssize_t) black.alpha) |
| 5558 | stretch_map[i].alpha=0; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5559 | else |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5560 | if (i > (ssize_t) white.alpha) |
| 5561 | stretch_map[i].alpha=QuantumRange; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5562 | else |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5563 | if (black.alpha != white.alpha) |
| 5564 | stretch_map[i].alpha=ScaleMapToQuantum((MagickRealType) (MaxMap* |
| 5565 | (i-black.alpha)/(white.alpha-black.alpha))); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5566 | } |
| 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 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5617 | if (black.alpha != white.alpha) |
| 5618 | image->colormap[i].alpha=stretch_map[ |
| 5619 | ScaleQuantumToMap(image->colormap[i].alpha)].alpha; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5620 | } |
| 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5647 | length = image->columns * image->rows; |
| 5648 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5649 | 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; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5686 | clStatus=clEnv->library->clSetKernelArg(stretchKernel,i++,sizeof(cl_mem),(void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5687 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5698 | global_work_size[0] = image->columns; |
| 5699 | global_work_size[1] = image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5700 | |
| 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 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5713 | 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5715 | } |
| 5716 | else |
| 5717 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5718 | length = image->columns * image->rows; |
| 5719 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5720 | } |
| 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 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 5727 | outputReady=SyncCacheViewAuthenticPixels(image_view,exception); |
| 5728 | |
| 5729 | cleanup: |
| 5730 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 5731 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5732 | image_view=DestroyCacheView(image_view); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5733 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5734 | if (imageBuffer!=NULL) |
| 5735 | clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5736 | |
| 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 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 5757 | return(outputReady); |
| 5758 | } |
| 5759 | |
| 5760 | MagickExport 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5777 | } |
| 5778 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5779 | /* |
| 5780 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 5781 | % % |
| 5782 | % % |
| 5783 | % % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5784 | % D e s p e c k l e I m a g e w i t h O p e n C L % |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5785 | % % |
| 5786 | % % |
| 5787 | % % |
| 5788 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 5789 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5790 | % 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). |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5796 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5797 | % The format of the DespeckleImage method is: |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5798 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5799 | % Image *DespeckleImage(const Image *image,ExceptionInfo *exception) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5800 | % |
| 5801 | % A description of each parameter follows: |
| 5802 | % |
| 5803 | % o image: the image. |
| 5804 | % |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5805 | % o exception: return any errors or warnings in this structure. |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5806 | % |
| 5807 | */ |
| 5808 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 5809 | static 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5889 | inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5890 | if (inputPixels == (void *) NULL) |
| 5891 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5892 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5893 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5905 | length = image->columns * image->rows; |
| 5906 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5907 | if (clStatus != CL_SUCCESS) |
| 5908 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5909 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5910 | goto cleanup; |
| 5911 | } |
| 5912 | |
| 5913 | mem_flags = CL_MEM_READ_WRITE; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5914 | length = image->columns * image->rows; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5915 | for (k = 0; k < 2; k++) |
| 5916 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5917 | tempImageBuffer[k] = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5918 | if (clStatus != CL_SUCCESS) |
| 5919 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5920 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5921 | goto cleanup; |
| 5922 | } |
| 5923 | } |
| 5924 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5925 | filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5926 | assert(filteredImage != NULL); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5927 | if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue) |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5928 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 5929 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5930 | goto cleanup; |
| 5931 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 5932 | filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5933 | filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5934 | if (filteredPixels == (void *) NULL) |
| 5935 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 5936 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5937 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5951 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5952 | filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5953 | if (clStatus != CL_SUCCESS) |
| 5954 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5955 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5956 | goto cleanup; |
| 5957 | } |
| 5958 | |
| 5959 | hullPass1 = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "HullPass1"); |
| 5960 | hullPass2 = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "HullPass2"); |
| 5961 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5962 | clStatus =clEnv->library->clSetKernelArg(hullPass1,0,sizeof(cl_mem),(void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5963 | clStatus |=clEnv->library->clSetKernelArg(hullPass1,1,sizeof(cl_mem),(void *)(tempImageBuffer+1)); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5964 | imageWidth = image->columns; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5965 | clStatus |=clEnv->library->clSetKernelArg(hullPass1,2,sizeof(unsigned int),(void *)&imageWidth); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5966 | imageHeight = image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5967 | clStatus |=clEnv->library->clSetKernelArg(hullPass1,3,sizeof(unsigned int),(void *)&imageHeight); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5968 | matte = (image->alpha_trait==BlendPixelTrait)?0:1; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5969 | clStatus |=clEnv->library->clSetKernelArg(hullPass1,6,sizeof(int),(void *)&matte); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5970 | if (clStatus != CL_SUCCESS) |
| 5971 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5972 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5973 | goto cleanup; |
| 5974 | } |
| 5975 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5976 | clStatus = clEnv->library->clSetKernelArg(hullPass2,0,sizeof(cl_mem),(void *)(tempImageBuffer+1)); |
| 5977 | clStatus |=clEnv->library->clSetKernelArg(hullPass2,1,sizeof(cl_mem),(void *)tempImageBuffer); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5978 | imageWidth = image->columns; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5979 | clStatus |=clEnv->library->clSetKernelArg(hullPass2,2,sizeof(unsigned int),(void *)&imageWidth); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5980 | imageHeight = image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5981 | clStatus |=clEnv->library->clSetKernelArg(hullPass2,3,sizeof(unsigned int),(void *)&imageHeight); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5982 | matte = (image->alpha_trait==BlendPixelTrait)?0:1; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5983 | clStatus |=clEnv->library->clSetKernelArg(hullPass2,6,sizeof(int),(void *)&matte); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5984 | if (clStatus != CL_SUCCESS) |
| 5985 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 5986 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5987 | goto cleanup; |
| 5988 | } |
| 5989 | |
| 5990 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 5991 | global_work_size[0] = image->columns; |
| 5992 | global_work_size[1] = image->rows; |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 5993 | |
| 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; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6004 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6008 | if (clStatus != CL_SUCCESS) |
| 6009 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6010 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6011 | goto cleanup; |
| 6012 | } |
| 6013 | /* launch the kernel */ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6014 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass1, 2, NULL, global_work_size, NULL, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6015 | if (clStatus != CL_SUCCESS) |
| 6016 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6017 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6018 | goto cleanup; |
| 6019 | } |
| 6020 | /* launch the kernel */ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6021 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass2, 2, NULL, global_work_size, NULL, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6022 | if (clStatus != CL_SUCCESS) |
| 6023 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6024 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6025 | goto cleanup; |
| 6026 | } |
| 6027 | |
| 6028 | |
| 6029 | if (k == 0) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6030 | clStatus =clEnv->library->clSetKernelArg(hullPass1,0,sizeof(cl_mem),(void *)(tempImageBuffer)); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6031 | offset.s[0] = -X[k]; |
| 6032 | offset.s[1] = -Y[k]; |
| 6033 | polarity = 1; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6034 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6038 | if (clStatus != CL_SUCCESS) |
| 6039 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6040 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6041 | goto cleanup; |
| 6042 | } |
| 6043 | /* launch the kernel */ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6044 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass1, 2, NULL, global_work_size, NULL, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6045 | if (clStatus != CL_SUCCESS) |
| 6046 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6047 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6048 | goto cleanup; |
| 6049 | } |
| 6050 | /* launch the kernel */ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6051 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass2, 2, NULL, global_work_size, NULL, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6052 | if (clStatus != CL_SUCCESS) |
| 6053 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6054 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6055 | goto cleanup; |
| 6056 | } |
| 6057 | |
| 6058 | offset.s[0] = -X[k]; |
| 6059 | offset.s[1] = -Y[k]; |
| 6060 | polarity = -1; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6061 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6065 | if (clStatus != CL_SUCCESS) |
| 6066 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6067 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6068 | goto cleanup; |
| 6069 | } |
| 6070 | /* launch the kernel */ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6071 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass1, 2, NULL, global_work_size, NULL, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6072 | if (clStatus != CL_SUCCESS) |
| 6073 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6074 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6075 | goto cleanup; |
| 6076 | } |
| 6077 | /* launch the kernel */ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6078 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass2, 2, NULL, global_work_size, NULL, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6079 | if (clStatus != CL_SUCCESS) |
| 6080 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6081 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6082 | goto cleanup; |
| 6083 | } |
| 6084 | |
| 6085 | offset.s[0] = X[k]; |
| 6086 | offset.s[1] = Y[k]; |
| 6087 | polarity = -1; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6088 | 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6092 | |
| 6093 | if (k == 3) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6094 | clStatus |=clEnv->library->clSetKernelArg(hullPass2,1,sizeof(cl_mem),(void *)&filteredImageBuffer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6095 | |
| 6096 | if (clStatus != CL_SUCCESS) |
| 6097 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6098 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6099 | goto cleanup; |
| 6100 | } |
| 6101 | /* launch the kernel */ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6102 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass1, 2, NULL, global_work_size, NULL, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6103 | if (clStatus != CL_SUCCESS) |
| 6104 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6105 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6106 | goto cleanup; |
| 6107 | } |
| 6108 | /* launch the kernel */ |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6109 | clStatus = clEnv->library->clEnqueueNDRangeKernel(queue, hullPass2, 2, NULL, global_work_size, NULL, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6110 | if (clStatus != CL_SUCCESS) |
| 6111 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6112 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clEnqueueNDRangeKernel failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6113 | goto cleanup; |
| 6114 | } |
| 6115 | } |
| 6116 | |
| 6117 | if (ALIGNED(filteredPixels,CLPixelPacket)) |
| 6118 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6119 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6120 | clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6121 | } |
| 6122 | else |
| 6123 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6124 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6125 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6126 | } |
| 6127 | if (clStatus != CL_SUCCESS) |
| 6128 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 6129 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6130 | goto cleanup; |
| 6131 | } |
| 6132 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6133 | outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception); |
| 6134 | |
| 6135 | cleanup: |
| 6136 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 6137 | |
| 6138 | image_view=DestroyCacheView(image_view); |
| 6139 | if (filteredImage_view != NULL) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6140 | filteredImage_view=DestroyCacheView(filteredImage_view); |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 6141 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6142 | if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6143 | if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6144 | for (k = 0; k < 2; k++) |
| 6145 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6146 | if (tempImageBuffer[k]!=NULL) clEnv->library->clReleaseMemObject(tempImageBuffer[k]); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6147 | } |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6148 | if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6149 | if (hullPass1!=NULL) RelinquishOpenCLKernel(clEnv, hullPass1); |
| 6150 | if (hullPass2!=NULL) RelinquishOpenCLKernel(clEnv, hullPass2); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6151 | if (outputReady == MagickFalse && filteredImage != NULL) |
| 6152 | filteredImage=DestroyImage(filteredImage); |
| 6153 | return(filteredImage); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6154 | } |
| 6155 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6156 | MagickExport 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); |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 6171 | } |
| 6172 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6173 | static 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 |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 6188 | inputPixelCount, |
| 6189 | pixelsPerWorkitem, |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6190 | clStatus; |
| 6191 | |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 6192 | cl_uint |
| 6193 | seed0, |
| 6194 | seed1; |
| 6195 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6196 | cl_kernel |
| 6197 | addNoiseKernel; |
| 6198 | |
| 6199 | cl_mem_flags |
| 6200 | mem_flags; |
| 6201 | |
| 6202 | cl_mem |
| 6203 | filteredImageBuffer, |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 6204 | imageBuffer; |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6205 | |
| 6206 | const char |
| 6207 | *option; |
| 6208 | |
| 6209 | const void |
| 6210 | *inputPixels; |
| 6211 | |
| 6212 | float |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 6213 | attenuate; |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6214 | |
| 6215 | MagickBooleanType |
| 6216 | outputReady; |
| 6217 | |
| 6218 | MagickCLEnv |
| 6219 | clEnv; |
| 6220 | |
| 6221 | MagickSizeType |
| 6222 | length; |
| 6223 | |
| 6224 | Image |
| 6225 | *filteredImage; |
| 6226 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6227 | RandomInfo |
| 6228 | **restrict random_info; |
| 6229 | |
| 6230 | size_t |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 6231 | global_work_size[1], |
| 6232 | local_work_size[1]; |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6233 | |
| 6234 | unsigned int |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6235 | k, |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 6236 | numRandomNumberPerPixel; |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6237 | |
| 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; |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6253 | context = NULL; |
| 6254 | imageBuffer = NULL; |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6255 | 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6264 | inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6265 | if (inputPixels == (void *) NULL) |
| 6266 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6267 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6268 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6280 | length = image->columns * image->rows; |
| 6281 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6282 | if (clStatus != CL_SUCCESS) |
| 6283 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6284 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6285 | goto cleanup; |
| 6286 | } |
| 6287 | |
| 6288 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6289 | filteredImage = CloneImage(image,image->columns,image->rows,MagickTrue,exception); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6290 | assert(filteredImage != NULL); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6291 | if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue) |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6292 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 6293 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", "'%s'", "."); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6294 | goto cleanup; |
| 6295 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6296 | filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6297 | filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6298 | if (filteredPixels == (void *) NULL) |
| 6299 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 6300 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, "UnableToReadPixelCache.","`%s'",filteredImage->filename); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6301 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6315 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6316 | filteredImageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), hostPtr, &clStatus); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6317 | if (clStatus != CL_SUCCESS) |
| 6318 | { |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6319 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clCreateBuffer failed.","."); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6320 | 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 | |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6353 | /* set up the random number generators */ |
| 6354 | attenuate=1.0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6355 | option=GetImageArtifact(image,"attenuate"); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6356 | 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 | |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 6363 | 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 | } |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6385 | |
| 6386 | k = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6387 | clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_mem),(void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6388 | clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_mem),(void *)&filteredImageBuffer); |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 6389 | clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_uint),(void *)&inputPixelCount); |
| 6390 | clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_uint),(void *)&pixelsPerWorkitem); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6391 | clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(ChannelType),(void *)&channel); |
| 6392 | clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(NoiseType),(void *)&noise_type); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6393 | attenuate=1.0f; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6394 | option=GetImageArtifact(image,"attenuate"); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6395 | if (option != (char *) NULL) |
| 6396 | attenuate=(float)StringToDouble(option,(char **) NULL); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6397 | clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(float),(void *)&attenuate); |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 6398 | clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_uint),(void *)&seed0); |
| 6399 | clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(cl_uint),(void *)&seed1); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6400 | clEnv->library->clSetKernelArg(addNoiseKernel,k++,sizeof(unsigned int),(void *)&numRandomNumberPerPixel); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6401 | |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 6402 | clEnv->library->clEnqueueNDRangeKernel(queue,addNoiseKernel,1,NULL,global_work_size,NULL,0,NULL,NULL); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6403 | |
| 6404 | if (ALIGNED(filteredPixels,CLPixelPacket)) |
| 6405 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6406 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6407 | clEnv->library->clEnqueueMapBuffer(queue, filteredImageBuffer, CL_TRUE, CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, NULL, &clStatus); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6408 | } |
| 6409 | else |
| 6410 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6411 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6412 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, filteredImageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), filteredPixels, 0, NULL, NULL); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6413 | } |
| 6414 | if (clStatus != CL_SUCCESS) |
| 6415 | { |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 6416 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6417 | goto cleanup; |
| 6418 | } |
| 6419 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6420 | outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception); |
| 6421 | |
| 6422 | cleanup: |
| 6423 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 6424 | |
| 6425 | image_view=DestroyCacheView(image_view); |
| 6426 | if (filteredImage_view != NULL) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6427 | filteredImage_view=DestroyCacheView(filteredImage_view); |
cristy | a22457d | 2013-12-07 14:03:06 +0000 | [diff] [blame] | 6428 | |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6429 | if (queue!=NULL) RelinquishOpenCLCommandQueue(clEnv, queue); |
| 6430 | if (addNoiseKernel!=NULL) RelinquishOpenCLKernel(clEnv, addNoiseKernel); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6431 | if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6432 | if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer); |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6433 | if (outputReady == MagickFalse && filteredImage != NULL) |
| 6434 | filteredImage=DestroyImage(filteredImage); |
| 6435 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6436 | return(filteredImage); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6437 | } |
| 6438 | |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6439 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6440 | MagickExport 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 | |
dirk | 859503c | 2014-08-13 20:39:42 +0000 | [diff] [blame] | 6454 | filteredImage = ComputeAddNoiseImage(image,channel,noise_type,exception); |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6455 | |
| 6456 | return(filteredImage); |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 6457 | } |
| 6458 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6459 | static 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 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6480 | status = MagickFalse; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6481 | randomImageKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, "RandomImage"); |
| 6482 | |
| 6483 | k = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6484 | clEnv->library->clSetKernelArg(randomImageKernel,k++,sizeof(cl_mem),(void*)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6485 | 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 | |
| 6512 | cleanup: |
| 6513 | if (randomImageKernel!=NULL) RelinquishOpenCLKernel(clEnv, randomImageKernel); |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6514 | return(status); |
| 6515 | } |
| 6516 | |
| 6517 | static 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6567 | inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6568 | if (inputPixels == (void *) NULL) |
| 6569 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6570 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning,"UnableToReadPixelCache.","`%s'",image->filename); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6571 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6586 | length = image->columns * image->rows; |
| 6587 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, length * sizeof(CLPixelPacket), (void*)inputPixels, &clStatus); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6588 | 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, |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6606 | imageBuffer, |
| 6607 | image->columns, |
| 6608 | image->rows, |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6609 | randomNumberSeedsBuffer, |
| 6610 | GetNumRandGenerators(clEnv), |
| 6611 | exception); |
| 6612 | if (status==MagickFalse) |
| 6613 | { |
| 6614 | goto cleanup; |
| 6615 | } |
| 6616 | |
| 6617 | if (ALIGNED(inputPixels,CLPixelPacket)) |
| 6618 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6619 | 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6621 | } |
| 6622 | else |
| 6623 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6624 | length = image->columns * image->rows; |
| 6625 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6626 | } |
| 6627 | if (clStatus != CL_SUCCESS) |
| 6628 | { |
| 6629 | (void) OpenCLThrowMagickException(exception, GetMagickModule(), ResourceLimitWarning, "Reading output image from CL buffer failed.", "'%s'", "."); |
| 6630 | goto cleanup; |
| 6631 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6632 | outputReady=SyncCacheViewAuthenticPixels(image_view,exception); |
| 6633 | |
| 6634 | cleanup: |
| 6635 | OpenCLLogException(__FUNCTION__,__LINE__,exception); |
| 6636 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6637 | image_view=DestroyCacheView(image_view); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6638 | |
| 6639 | UnlockRandSeedBuffer(clEnv); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6640 | if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6641 | if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue); |
| 6642 | return outputReady; |
| 6643 | } |
| 6644 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6645 | MagickExport 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6660 | } |
| 6661 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6662 | static 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6748 | inputPixels=GetCacheViewVirtualPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6749 | if (inputPixels == (const void *) NULL) |
| 6750 | { |
| 6751 | (void) ThrowMagickException(exception,GetMagickModule(),CacheError, |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6752 | "UnableToReadPixelCache.","`%s'",image->filename); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6753 | 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 |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6768 | length = image->columns * image->rows; |
| 6769 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6770 | 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 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6779 | filteredImage = CloneImage(image,image->columns,image->rows, |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6780 | MagickTrue,exception); |
| 6781 | assert(filteredImage != NULL); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6782 | if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue) |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6783 | { |
| 6784 | (void) ThrowMagickException(exception, GetMagickModule(), |
| 6785 | ResourceLimitError, "CloneImage failed.", "'%s'", "."); |
| 6786 | goto cleanup; |
| 6787 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6788 | filteredImage_view=AcquireAuthenticCacheView(filteredImage,exception); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6789 | filteredPixels=GetCacheViewAuthenticPixels(filteredImage_view,0,0,filteredImage->columns,filteredImage->rows,exception); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6790 | 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 |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6808 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6809 | 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), |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6897 | (void *)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6898 | clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(cl_mem), |
| 6899 | (void *)&filteredImageBuffer); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6900 | imageWidth = image->columns; |
| 6901 | imageHeight = image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6902 | 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 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6913 | GetPixelInfo(image,&bias); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6914 | biasPixel.s[0] = bias.red; |
| 6915 | biasPixel.s[1] = bias.green; |
| 6916 | biasPixel.s[2] = bias.blue; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6917 | biasPixel.s[3] = bias.alpha; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6918 | clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(cl_float4), &biasPixel); |
| 6919 | |
| 6920 | clStatus|=clEnv->library->clSetKernelArg(motionBlurKernel,i++,sizeof(ChannelType), &channel); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6921 | matte = (image->alpha_trait==BlendPixelTrait)?1:0; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6922 | 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( |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6934 | image->columns,local_work_size[0]); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6935 | global_work_size[1] = (size_t)padGlobalWorkgroupSizeToLocalWorkgroupSize( |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6936 | image->rows,local_work_size[1]); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6937 | 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 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6950 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6951 | 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 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6957 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6958 | 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 | } |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6967 | outputReady=SyncCacheViewAuthenticPixels(filteredImage_view,exception); |
| 6968 | |
| 6969 | cleanup: |
| 6970 | |
| 6971 | image_view=DestroyCacheView(image_view); |
| 6972 | if (filteredImage_view != NULL) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6973 | filteredImage_view=DestroyCacheView(filteredImage_view); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6974 | |
| 6975 | if (filteredImageBuffer!=NULL) clEnv->library->clReleaseMemObject(filteredImageBuffer); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6976 | if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6977 | if (imageKernelBuffer!=NULL) clEnv->library->clReleaseMemObject(imageKernelBuffer); |
| 6978 | if (motionBlurKernel!=NULL) RelinquishOpenCLKernel(clEnv, motionBlurKernel); |
| 6979 | if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue); |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6980 | if (outputReady == MagickFalse && filteredImage != NULL) |
| 6981 | filteredImage=DestroyImage(filteredImage); |
| 6982 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 6983 | return(filteredImage); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 6984 | } |
| 6985 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 6986 | MagickExport 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); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7005 | } |
| 7006 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 7007 | static 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 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7031 | magick_unreferenced(exception); |
| 7032 | |
| 7033 | compositeKernel = AcquireOpenCLKernel(clEnv, MAGICK_OPENCL_ACCELERATE, |
| 7034 | "Composite"); |
| 7035 | |
| 7036 | k = 0; |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 7037 | clStatus=clEnv->library->clSetKernelArg(compositeKernel,k++,sizeof(cl_mem),(void*)&imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7038 | 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 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 7065 | return((clStatus==CL_SUCCESS) ? MagickTrue : MagickFalse); |
| 7066 | } |
| 7067 | |
| 7068 | static 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); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 7124 | inputPixels=GetCacheViewAuthenticPixels(image_view,0,0,image->columns,image->rows,exception); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7125 | if (inputPixels == (void *) NULL) |
| 7126 | { |
| 7127 | (void) OpenCLThrowMagickException(exception,GetMagickModule(),CacheWarning, |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 7128 | "UnableToReadPixelCache.","`%s'",image->filename); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7129 | 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 */ |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 7144 | length = image->columns * image->rows; |
| 7145 | imageBuffer = clEnv->library->clCreateBuffer(context, mem_flags, |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7146 | 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 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 7186 | status = LaunchCompositeKernel(clEnv,queue,imageBuffer, |
| 7187 | (unsigned int) image->columns, |
| 7188 | (unsigned int) image->rows, |
| 7189 | (unsigned int) (image->alpha_trait==BlendPixelTrait) ? 1 : 0, |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7190 | 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 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 7199 | length = image->columns * image->rows; |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7200 | if (ALIGNED(inputPixels,CLPixelPacket)) |
| 7201 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 7202 | clEnv->library->clEnqueueMapBuffer(queue, imageBuffer, CL_TRUE, |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7203 | CL_MAP_READ|CL_MAP_WRITE, 0, length * sizeof(CLPixelPacket), 0, NULL, |
| 7204 | NULL, &clStatus); |
| 7205 | } |
| 7206 | else |
| 7207 | { |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 7208 | clStatus = clEnv->library->clEnqueueReadBuffer(queue, imageBuffer, CL_TRUE, 0, |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7209 | length * sizeof(CLPixelPacket), inputPixels, 0, NULL, NULL); |
| 7210 | } |
| 7211 | if (clStatus==CL_SUCCESS) |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 7212 | outputReady=SyncCacheViewAuthenticPixels(image_view,exception); |
| 7213 | |
| 7214 | cleanup: |
| 7215 | |
| 7216 | image_view=DestroyCacheView(image_view); |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 7217 | if (imageBuffer!=NULL) clEnv->library->clReleaseMemObject(imageBuffer); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7218 | if (compositeImageBuffer!=NULL) clEnv->library->clReleaseMemObject(compositeImageBuffer); |
| 7219 | if (queue != NULL) RelinquishOpenCLCommandQueue(clEnv, queue); |
| 7220 | |
dirk | 832becc | 2014-08-04 19:44:34 +0000 | [diff] [blame] | 7221 | return(outputReady); |
| 7222 | } |
| 7223 | |
| 7224 | MagickExport 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)) |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 7238 | return(MagickFalse); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7239 | |
| 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 | |
dirk | 8a5cf51 | 2014-07-28 20:16:27 +0000 | [diff] [blame] | 7260 | return(status); |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7261 | } |
| 7262 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 7263 | #else /* MAGICKCORE_OPENCL_SUPPORT */ |
| 7264 | |
| 7265 | MagickExport 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 | |
| 7278 | MagickExport 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 | |
| 7295 | MagickExport 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 | |
dirk | 6d612cf | 2014-03-13 21:17:23 +0000 | [diff] [blame] | 7308 | MagickExport Image *AccelerateRotationalBlurImage( |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 7309 | 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 | |
| 7321 | MagickExport 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 | |
cristy | 0b8a2e9 | 2014-03-08 00:52:49 +0000 | [diff] [blame] | 7338 | MagickExport |
| 7339 | MagickBooleanType 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); |
cristy | a219e4b | 2014-03-08 12:36:16 +0000 | [diff] [blame] | 7346 | 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); |
cristy | 0b8a2e9 | 2014-03-08 00:52:49 +0000 | [diff] [blame] | 7354 | |
| 7355 | return MagickFalse; |
| 7356 | } |
| 7357 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 7358 | |
| 7359 | MagickExport 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 | |
cristy | 0b8a2e9 | 2014-03-08 00:52:49 +0000 | [diff] [blame] | 7370 | MagickExport MagickBooleanType AccelerateContrastStretchImageChannel( |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7371 | 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 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 7383 | MagickExport 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 | |
| 7394 | MagickExport 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 | |
| 7403 | MagickExport 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 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 7418 | MagickExport |
| 7419 | MagickBooleanType 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 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7432 | MagickExport |
| 7433 | MagickBooleanType 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 | |
| 7443 | MagickExport |
| 7444 | MagickBooleanType 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 | |
cristy | e85d0f7 | 2013-11-27 02:25:43 +0000 | [diff] [blame] | 7453 | MagickExport 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 | } |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 7462 | |
cristy | 0c832c6 | 2014-03-07 22:21:04 +0000 | [diff] [blame] | 7463 | |
| 7464 | MagickExport MagickBooleanType AccelerateRandomImage(Image* image, ExceptionInfo* exception) |
| 7465 | { |
| 7466 | magick_unreferenced(image); |
| 7467 | magick_unreferenced(exception); |
| 7468 | return MagickFalse; |
| 7469 | } |
| 7470 | |
| 7471 | MagickExport |
| 7472 | Image* 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 | |
cristy | f034abb | 2013-11-24 14:16:14 +0000 | [diff] [blame] | 7486 | #endif /* MAGICKCORE_OPENCL_SUPPORT */ |
| 7487 | |
| 7488 | MagickExport 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 | |