Wangfei | 8e5e3e4 | 2016-02-18 19:41:54 +0800 | [diff] [blame] | 1 | /* |
| 2 | * function: kernel_retinex |
| 3 | * input: image2d_t as read only |
| 4 | * output: image2d_t as write only |
| 5 | */ |
| 6 | typedef struct { |
| 7 | float gain; |
| 8 | float threshold; |
| 9 | float log_min; |
| 10 | float log_max; |
| 11 | } CLRetinexConfig; |
| 12 | |
| 13 | __kernel void kernel_retinex (__read_only image2d_t input, __write_only image2d_t output, uint vertical_offset_in, uint vertical_offset_out, CLRetinexConfig re_config, __global float *table) |
| 14 | { |
| 15 | int x = get_global_id (0); |
| 16 | int y = get_global_id (1); |
| 17 | sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST; |
| 18 | |
| 19 | float4 y_out, uv_in; |
| 20 | float4 y_in[25]; |
| 21 | float y_ga, y_lg; |
| 22 | int i; |
| 23 | // cpy UV |
| 24 | if(y % 2 == 0) { |
| 25 | uv_in = read_imagef(input, sampler, (int2)(x, y / 2 + vertical_offset_in)); |
| 26 | write_imagef(output, (int2)(x, y / 2 + vertical_offset_out), uv_in); |
| 27 | } |
| 28 | |
| 29 | for(i = 0; i < 25; i++) |
| 30 | y_in[i] = read_imagef(input, sampler, (int2)(x - 2 + i % 5, y - 2 + i / 5)) * 255.0; |
| 31 | |
| 32 | for(i = 0; i < 25; i++) |
| 33 | y_ga += y_in[i].x * table[i]; |
| 34 | y_lg = log(y_in[12].x) - log(y_ga); |
| 35 | |
| 36 | if(y_lg < re_config.log_min) |
| 37 | y_out.x = 0.0f; |
| 38 | else if(y_lg > re_config.log_max) |
| 39 | y_out.x = 1.0; |
| 40 | else |
| 41 | y_out.x = re_config.gain * (y_lg - re_config.log_min) / 255.0; |
| 42 | write_imagef(output, (int2)(x, y), y_out); |
| 43 | } |
| 44 | |