forked from victoroliv2/halide-casestudies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsharped_mask.cpp
188 lines (151 loc) · 5.02 KB
/
unsharped_mask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include <stdalign.h>
#include <omp.h>
#include "opencl_help.h"
inline static int clamp(int x, int low, int high)
{
return (x < low) ? low : ((x > high) ? high : x);
}
void
unsharped_mask (float const * __restrict__ _input,
float * __restrict__ _output,
int width,
int height,
float sigma,
float detail_thresh,
float sharpen)
{
const int radius = int(3*sigma + 1.0f);
const int channels = 4;
float gaussian_mask[2 * radius + 1];
for (int r=-radius; r<=radius; r++)
gaussian_mask[r+radius] = expf(-(float(r)/sigma)*(float(r)/sigma)*0.5f);
float * __restrict__ input = (float * __restrict__) __builtin_assume_aligned(_input, 32);
float * __restrict__ output = (float * __restrict__) __builtin_assume_aligned(_output, 32);
float * __restrict__ blurx;
float * __restrict__ blury;
posix_memalign((void **) &blurx, 32, width * height * channels * sizeof(float));
posix_memalign((void **) &blury, 32, width * height * channels * sizeof(float));
#define INPUT(x,y,c) input[c+channels*(x + width * y)]
#define OUTPUT(x,y,c) output[c+channels*(x + width * y)]
#define BLURX(x,y,c) blurx[c+channels*(x + width * y)]
#define BLURY(x,y,c) blury[c+channels*(x + width * y)]
for (int k=0; k < (width * height * channels); k++)
{
input[k] = 0.0f;
blurx[k] = 0.0f;
blury[k] = 0.0f;
}
#pragma omp parallel for
for (int y=0; y < height; y++)
for (int x=0; x < width; x++)
for(int c = 0; c < channels; c++)
{
for (int r=-radius; r<=radius; r++)
BLURX(x,y,c) += gaussian_mask[r+radius] * INPUT(clamp(x+r, 0, width-1), y, c);
}
#pragma omp parallel for
for (int y=0; y < height; y++)
for (int x=0; x < width; x++)
for(int c = 0; c < channels; c++)
{
for (int r=-radius; r<=radius; r++)
BLURY(x,y,c) += gaussian_mask[r+radius] * BLURX(x, clamp(y+r, 0, height-1), c);
}
#pragma omp parallel for
for (int y=0; y < height; y++)
for (int x=0; x < width; x++)
for(int c = 0; c < channels; c++)
{
float detail = BLURY(x,y,c) - INPUT(x,y,c);
float sharpened = (detail <= detail_thresh)?
sharpen * copysign(fmax(fabs(detail) - detail_thresh, 0.0f), detail) :
0.0f;
OUTPUT(x,y,c) = INPUT(x,y,c) + sharpened;
}
}
#include <stdio.h>
#include "kernel/unsharped_mask.cl.h"
static cl_kernel *kernel;
void
unsharped_mask_cl_prepare()
{
const char *kernel_name[] = { "blurx",
"blury",
"unsharped_mask",
NULL };
kernel = cl_compile_and_build(unsharped_mask_cl_source, kernel_name);
}
void
unsharped_mask_cl (cl_mem A,
cl_mem B,
int width,
int height,
float sigma,
float detail_thresh,
float sharpen)
{
cl_int cl_err = 0;
const int radius = int(3*sigma + 1.0f);
float gaussian_mask[2 * radius + 1];
for (int r=-radius; r<=radius; r++)
gaussian_mask[r+radius] = expf(-(r/sigma)*(r/sigma)*0.5f);
cl_mem m_gaussian_mask = clCreateBuffer (cl_state.context,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
(2 * radius + 1) * sizeof(cl_float),
gaussian_mask,
&cl_err);
CL_CHECK;
size_t global_ws[2];
{
global_ws[0] = width;
global_ws[1] = height;
CL_ARG_START(kernel[0])
CL_ARG(cl_mem, A);
CL_ARG(cl_mem, B);
CL_ARG(cl_mem, m_gaussian_mask);
CL_ARG(cl_int, radius);
CL_ARG_END
cl_err = clEnqueueNDRangeKernel(cl_state.command_queue,
kernel[0], 2,
NULL, global_ws, NULL,
0, NULL, NULL);
CL_CHECK;
}
{
global_ws[0] = width;
global_ws[1] = height;
CL_ARG_START(kernel[1])
CL_ARG(cl_mem, B);
CL_ARG(cl_mem, A);
CL_ARG(cl_mem, m_gaussian_mask);
CL_ARG(cl_int, radius);
CL_ARG_END
cl_err = clEnqueueNDRangeKernel(cl_state.command_queue,
kernel[1], 2,
NULL, global_ws, NULL,
0, NULL, NULL);
CL_CHECK;
}
{
global_ws[0] = width;
global_ws[1] = height;
CL_ARG_START(kernel[2])
CL_ARG(cl_mem, A);
CL_ARG(cl_mem, B);
CL_ARG(cl_mem, B);
CL_ARG(cl_float, detail_thresh);
CL_ARG(cl_float, sharpen);
CL_ARG_END
cl_err = clEnqueueNDRangeKernel(cl_state.command_queue,
kernel[2], 2,
NULL, global_ws, NULL,
0, NULL, NULL);
CL_CHECK;
}
cl_err = clFinish(cl_state.command_queue);
CL_CHECK;
CL_RELEASE(m_gaussian_mask);
}