Wind Yuan | be50504 | 2017-05-31 23:46:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * function: kernel_wavelet_haar_decomposition |
| 3 | * wavelet haar decomposition kernel |
| 4 | * input: input image data as read only |
| 5 | * ll/hl/lh/hh: wavelet decomposition image |
| 6 | * layer: wavelet decomposition layer |
| 7 | * decomLevels: wavelet decomposition levels |
| 8 | */ |
| 9 | #ifndef WAVELET_DENOISE_Y |
| 10 | #define WAVELET_DENOISE_Y 1 |
| 11 | #endif |
| 12 | |
| 13 | #ifndef WAVELET_DENOISE_UV |
| 14 | #define WAVELET_DENOISE_UV 0 |
| 15 | #endif |
| 16 | |
| 17 | #ifndef WAVELET_BAYES_SHRINK |
| 18 | #define WAVELET_BAYES_SHRINK 1 |
| 19 | #endif |
| 20 | |
| 21 | __kernel void kernel_wavelet_haar_decomposition ( |
| 22 | __read_only image2d_t input, |
| 23 | __write_only image2d_t ll, __write_only image2d_t hl, |
| 24 | __write_only image2d_t lh, __write_only image2d_t hh, |
| 25 | int layer, int decomLevels, |
| 26 | float hardThresh, float softThresh) |
| 27 | { |
| 28 | int x = get_global_id (0); |
| 29 | int y = get_global_id (1); |
| 30 | sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST; |
| 31 | |
| 32 | float8 line[2]; |
| 33 | line[0].lo = read_imagef(input, sampler, (int2)(2 * x, 2 * y)); |
| 34 | line[0].hi = read_imagef(input, sampler, (int2)(2 * x + 1, 2 * y)); |
| 35 | line[1].lo = read_imagef(input, sampler, (int2)(2 * x, 2 * y + 1)); |
| 36 | line[1].hi = read_imagef(input, sampler, (int2)(2 * x + 1, 2 * y + 1)); |
| 37 | |
| 38 | // row transform |
| 39 | float8 row_l; |
| 40 | float8 row_h; |
| 41 | row_l = (float8)(line[0].lo + line[1].lo, line[0].hi + line[1].hi) / 2.0f; |
| 42 | row_h = (float8)(line[0].lo - line[1].lo, line[0].hi - line[1].hi) / 2.0f; |
| 43 | |
| 44 | float4 line_ll; |
| 45 | float4 line_hl; |
| 46 | float4 line_lh; |
| 47 | float4 line_hh; |
| 48 | |
| 49 | #if WAVELET_DENOISE_Y |
| 50 | // column transform |
| 51 | line_ll = (row_l.odd + row_l.even) / 2.0f; |
| 52 | line_hl = (row_l.odd - row_l.even) / 2.0f; |
| 53 | line_lh = (row_h.odd + row_h.even) / 2.0f; |
| 54 | line_hh = (row_h.odd - row_h.even) / 2.0f; |
| 55 | #endif |
| 56 | |
| 57 | #if WAVELET_DENOISE_UV |
| 58 | // U column transform |
| 59 | line_ll.odd = (row_l.odd.odd + row_l.odd.even) / 2.0f; |
| 60 | line_hl.odd = (row_l.odd.odd - row_l.odd.even) / 2.0f; |
| 61 | line_lh.odd = (row_h.odd.odd + row_h.odd.even) / 2.0f; |
| 62 | line_hh.odd = (row_h.odd.odd - row_h.odd.even) / 2.0f; |
| 63 | |
| 64 | // V column transform |
| 65 | line_ll.even = (row_l.even.odd + row_l.even.even) / 2.0f; |
| 66 | line_hl.even = (row_l.even.odd - row_l.even.even) / 2.0f; |
| 67 | line_lh.even = (row_h.even.odd + row_h.even.even) / 2.0f; |
| 68 | line_hh.even = (row_h.even.odd - row_h.even.even) / 2.0f; |
| 69 | #endif |
| 70 | |
| 71 | write_imagef(ll, (int2)(x, y), line_ll); |
| 72 | write_imagef(hl, (int2)(x, y), line_hl + 0.5f); |
| 73 | write_imagef(lh, (int2)(x, y), line_lh + 0.5f); |
| 74 | write_imagef(hh, (int2)(x, y), line_hh + 0.5f); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * function: kernel_wavelet_haar_reconstruction |
| 79 | * wavelet haar reconstruction kernel |
| 80 | * output: output wavelet reconstruction image |
| 81 | * ll/hl/lh/hh: input wavelet transform data as read only |
| 82 | * layer: wavelet reconstruction layer |
| 83 | * decomLevels: wavelet decomposition levels |
| 84 | * threshold: hard/soft denoise thresholding |
| 85 | */ |
| 86 | |
| 87 | __constant float uv_threshConst[5] = { 0.1659f, 0.06719f, 0.03343f, 0.01713f, 0.01043f }; |
| 88 | __constant float y_threshConst[5] = { 0.06129f, 0.027319f, 0.012643f, 0.006513f, 0.003443f }; |
| 89 | |
| 90 | __kernel void kernel_wavelet_haar_reconstruction ( |
| 91 | __write_only image2d_t output, |
| 92 | __read_only image2d_t ll, __read_only image2d_t hl, |
| 93 | __read_only image2d_t lh, __read_only image2d_t hh, |
| 94 | int layer, int decomLevels, |
| 95 | float hardThresh, float softThresh) |
| 96 | { |
| 97 | int x = get_global_id (0); |
| 98 | int y = get_global_id (1); |
| 99 | sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST; |
| 100 | |
| 101 | float thresh = 0.0f; |
| 102 | |
| 103 | float4 line_ll; |
| 104 | float4 line_hl; |
| 105 | float4 line_lh; |
| 106 | float4 line_hh; |
| 107 | |
| 108 | line_ll = read_imagef(ll, sampler, (int2)(x, y)); |
| 109 | line_hl = read_imagef(hl, sampler, (int2)(x, y)) - 0.5f; |
| 110 | line_lh = read_imagef(lh, sampler, (int2)(x, y)) - 0.5f; |
| 111 | line_hh = read_imagef(hh, sampler, (int2)(x, y)) - 0.5f; |
| 112 | |
| 113 | #if WAVELET_DENOISE_Y |
| 114 | thresh = hardThresh * y_threshConst[layer - 1]; |
| 115 | #endif |
| 116 | |
| 117 | #if WAVELET_DENOISE_UV |
| 118 | thresh = hardThresh * uv_threshConst[layer - 1]; |
| 119 | #endif |
| 120 | |
| 121 | #if !WAVELET_BAYES_SHRINK |
| 122 | // thresholding |
| 123 | line_hl = (line_hl < -thresh) ? line_hl + (thresh - thresh * softThresh) : line_hl; |
| 124 | line_hl = (line_hl > thresh) ? line_hl - (thresh - thresh * softThresh) : line_hl; |
| 125 | line_hl = (line_hl > -thresh && line_hl < thresh) ? line_hl * softThresh : line_hl; |
| 126 | |
| 127 | line_lh = (line_lh < -thresh) ? line_lh + (thresh - thresh * softThresh) : line_lh; |
| 128 | line_lh = (line_lh > thresh) ? line_lh - (thresh - thresh * softThresh) : line_lh; |
| 129 | line_lh = (line_lh > -thresh && line_lh < thresh) ? line_lh * softThresh : line_lh; |
| 130 | |
| 131 | line_hh = (line_hh < -thresh) ? line_hh + (thresh - thresh * softThresh) : line_hh; |
| 132 | line_hh = (line_hh > thresh) ? line_hh - (thresh - thresh * softThresh) : line_hh; |
| 133 | line_hh = (line_hh > -thresh && line_hh < thresh) ? line_hh * softThresh : line_hh; |
| 134 | #endif |
| 135 | |
| 136 | #if WAVELET_DENOISE_Y |
| 137 | // row reconstruction |
| 138 | float8 row_l; |
| 139 | float8 row_h; |
| 140 | row_l = (float8)(line_ll + line_lh, line_hl + line_hh); |
| 141 | row_h = (float8)(line_ll - line_lh, line_hl - line_hh); |
| 142 | |
| 143 | // column reconstruction |
| 144 | float8 line[2]; |
| 145 | line[0].odd = row_l.lo + row_l.hi; |
| 146 | line[0].even = row_l.lo - row_l.hi; |
| 147 | line[1].odd = row_h.lo + row_h.hi; |
| 148 | line[1].even = row_h.lo - row_h.hi; |
| 149 | |
| 150 | write_imagef(output, (int2)(2 * x, 2 * y), line[0].lo); |
| 151 | write_imagef(output, (int2)(2 * x + 1, 2 * y), line[0].hi); |
| 152 | write_imagef(output, (int2)(2 * x, 2 * y + 1), line[1].lo); |
| 153 | write_imagef(output, (int2)(2 * x + 1, 2 * y + 1), line[1].hi); |
| 154 | #endif |
| 155 | |
| 156 | #if WAVELET_DENOISE_UV |
| 157 | // row reconstruction |
| 158 | float8 row_l; |
| 159 | float8 row_h; |
| 160 | row_l = (float8)(line_ll + line_lh, line_hl + line_hh); |
| 161 | row_h = (float8)(line_ll - line_lh, line_hl - line_hh); |
| 162 | |
| 163 | float8 line[2]; |
| 164 | |
| 165 | // U column reconstruction |
| 166 | line[0].odd.odd = row_l.lo.odd + row_l.hi.odd; |
| 167 | line[0].odd.even = row_l.lo.odd - row_l.hi.odd; |
| 168 | line[1].odd.odd = row_h.lo.odd + row_h.hi.odd; |
| 169 | line[1].odd.even = row_h.lo.odd - row_h.hi.odd; |
| 170 | |
| 171 | // V column reconstruction |
| 172 | line[0].even.odd = row_l.lo.even + row_l.hi.even; |
| 173 | line[0].even.even = row_l.lo.even - row_l.hi.even; |
| 174 | line[1].even.odd = row_h.lo.even + row_h.hi.even; |
| 175 | line[1].even.even = row_h.lo.even - row_h.hi.even; |
| 176 | |
| 177 | write_imagef(output, (int2)(2 * x, 2 * y), line[0].lo); |
| 178 | write_imagef(output, (int2)(2 * x + 1, 2 * y), line[0].hi); |
| 179 | write_imagef(output, (int2)(2 * x, 2 * y + 1), line[1].lo); |
| 180 | write_imagef(output, (int2)(2 * x + 1, 2 * y + 1), line[1].hi); |
| 181 | #endif |
| 182 | } |