Files
feature-extraction-opencl/kernel/prefast-gray.cl
T
2021-06-18 14:57:59 +02:00

104 lines
4.7 KiB
Common Lisp

// Copyright (C) 2011 Dmitri Nikulin
// Copyright (C) 2011 Monash University
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// Enable OpenCL 32-bit integer atomic functions.
__kernel void prefast_gray(
__read_only image2d_t image,
__global int2 * corners,
__global int * icorner,
int fast_count
) {
// Prepare a suitable OpenCL image sampler.
sampler_t const sampler = CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
// Use global work item as 2D image coordinates.
int const x = get_global_id(0);
int const y = get_global_id(1);
if(x>16 && x<get_image_width(image)-16 && y<get_image_height(image)-16 && y>16){
int2 const xy = (int2)(x, y);
// Read the candidate pixel.
int const p00 = read_imageui(image, sampler, xy).x;
// Read other pixels in a circle around the candidate pixel.
int const p01 = read_imageui(image, sampler, xy + (int2)( 0, 3)).x;
int const p05 = read_imageui(image, sampler, xy + (int2)( 3, 0)).x;
int const p09 = read_imageui(image, sampler, xy + (int2)( 0, -3)).x;
int const p13 = read_imageui(image, sampler, xy + (int2)(-3, 0)).x;
// Check the absolute difference of each circle pixel.
int const d01 = (abs(p01 - p00) > FAST_THRESH);
int const d05 = (abs(p05 - p00) > FAST_THRESH);
int const d09 = (abs(p09 - p00) > FAST_THRESH);
int const d13 = (abs(p13 - p00) > FAST_THRESH);
// Check if any two adjacent circle pixels have a high absolute difference.
if ((d01 && d05) ||(d05 && d09) ||(d09 && d13) ||(d13 && d01)) {
// Read other pixels in a circle around the candidate pixel.
int const p02 = read_imageui(image, sampler, xy + (int2)( 1, 3)).x;
int const p03 = read_imageui(image, sampler, xy + (int2)( 2, 2)).x;
int const p04 = read_imageui(image, sampler, xy + (int2)( 3, 1)).x;
int const p06 = read_imageui(image, sampler, xy + (int2)( 3, -1)).x;
int const p07 = read_imageui(image, sampler, xy + (int2)( 2, -2)).x;
int const p08 = read_imageui(image, sampler, xy + (int2)( 1, -3)).x;
int const p10 = read_imageui(image, sampler, xy + (int2)(-1, -3)).x;
int const p11 = read_imageui(image, sampler, xy + (int2)(-2, -2)).x;
int const p12 = read_imageui(image, sampler, xy + (int2)(-3, -1)).x;
int const p14 = read_imageui(image, sampler, xy + (int2)(-3, 1)).x;
int const p15 = read_imageui(image, sampler, xy + (int2)(-2, 2)).x;
int const p16 = read_imageui(image, sampler, xy + (int2)(-1, 3)).x;
// Select the maximum score.
int sco = p00;
sco = max(sco, p01);
sco = max(sco, p02);
sco = max(sco, p03);
sco = max(sco, p04);
sco = max(sco, p05);
sco = max(sco, p06);
sco = max(sco, p07);
sco = max(sco, p08);
sco = max(sco, p09);
sco = max(sco, p10);
sco = max(sco, p11);
sco = max(sco, p12);
sco = max(sco, p13);
sco = max(sco, p14);
sco = max(sco, p15);
// Keep this score if it is as good as the maximum.
if (p00 >= sco) {
// Atomically append to corner buffer.
int const icorn = atom_inc(icorner);
if (icorn < fast_count)
corners[icorn] = xy;
}
}
}
}