forked from kif/sift_pyocl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.cl
365 lines (340 loc) · 12.1 KB
/
preprocess.cl
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Project: SIFT: An algorithm for image alignement
* Kernel for image pre-processing: Normalization, ...
*
*
* Copyright (C) 2013 European Synchrotron Radiation Facility
* Grenoble, France
* All rights reserved.
*
* Principal authors: J. Kieffer ([email protected])
* Last revision: 30/05/2013
*
* 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.
*
**/
#ifndef WORKGROUP_SIZE
#define WORKGROUP_SIZE 1024
#endif
#define MAX_CONST_SIZE 16384
/**
* \brief Cast values of an array of uint8 into a float output array.
*
* @param array_int: Pointer to global memory with the input data as unsigned8 array
* @param array_float: Pointer to global memory with the output data as float array
* @param IMAGE_W: Width of the image
* @param IMAGE_H: Height of the image
*/
__kernel void
u8_to_float( __global unsigned char *array_int,
__global float *array_float,
const int IMAGE_W,
const int IMAGE_H
)
{
//Global memory guard for padding
if ((get_global_id(0)<IMAGE_W) && (get_global_id(1) < IMAGE_H)){
int i = get_global_id(0) + IMAGE_W * get_global_id(1);
array_float[i]=(float)array_int[i];
} //end test in image
}//end kernel
/**
* \brief cast values of an array of uint16 into a float output array.
*
* @param array_int: Pointer to global memory with the input data as unsigned16 array
* @param array_float: Pointer to global memory with the output data as float array
* @param IMAGE_W: Width of the image
* @param IMAGE_H: Height of the image
*/
__kernel void
u16_to_float(__global unsigned short *array_int,
__global float *array_float,
const int IMAGE_W,
const int IMAGE_H
)
{
//Global memory guard for padding
if ((get_global_id(0)<IMAGE_W) && (get_global_id(1) < IMAGE_H)){
int i = get_global_id(0) + IMAGE_W * get_global_id(1);
array_float[i]=(float)array_int[i];
}
}//end kernel
/**
* \brief cast values of an array of uint32 into a float output array.
*
* @param array_int: Pointer to global memory with the input data as unsigned16 array
* @param array_float: Pointer to global memory with the output data as float array
* @param IMAGE_W: Width of the image
* @param IMAGE_H: Height of the image
*/
__kernel void
u32_to_float(__global unsigned int *array_int,
__global float *array_float,
const int IMAGE_W,
const int IMAGE_H
)
{
//Global memory guard for padding
if ((get_global_id(0)<IMAGE_W) && (get_global_id(1) < IMAGE_H)){
int i = get_global_id(0) + IMAGE_W * get_global_id(1);
array_float[i]=(float)array_int[i];
}
}//end kernel
/**
* \brief convert values of an array of int32 into a float output array.
*
* @param array_int: Pointer to global memory with the data in int
* @param array_float: Pointer to global memory with the data in float
* @param IMAGE_W: Width of the image
* @param IMAGE_H: Height of the image
*/
__kernel void
s32_to_float( __global int *array_int,
__global float *array_float,
const int IMAGE_W,
const int IMAGE_H
)
{
//Global memory guard for padding
if ((get_global_id(0)<IMAGE_W) && (get_global_id(1) < IMAGE_H)){
int i = get_global_id(0) + IMAGE_W * get_global_id(1);
array_float[i] = (float)(array_int[i]);
}//end test in image
}//end kernel
/**
* \brief convert values of an array of int64 into a float output array.
*
* @param array_int: Pointer to global memory with the data in int
* @param array_float: Pointer to global memory with the data in float
* @param IMAGE_W: Width of the image
* @param IMAGE_H: Height of the image
*/
__kernel void
s64_to_float( __global long *array_int,
__global float *array_float,
const int IMAGE_W,
const int IMAGE_H
)
{
//Global memory guard for padding
if ((get_global_id(0)<IMAGE_W) && (get_global_id(1) < IMAGE_H)){
int i = get_global_id(0) + IMAGE_W * get_global_id(1);
array_float[i] = (float)(array_int[i]);
}//end test in image
}//end kernel
/**
* \brief convert values of an array of float64 into a float output array.
*
* @param array_int: Pointer to global memory with the data in double
* @param array_float: Pointer to global memory with the data in float
* @param IMAGE_W: Width of the image
* @param IMAGE_H: Height of the image
*
* COMMENTED OUT AS THIS RUNS ONLY ON GPU WITH FP64
*/
//__kernel void
//double_to_float(__global double *array_int,
// __global float *array_float,
// const int IMAGE_W,
// const int IMAGE_H
//)
//{
// int i = get_global_id(0) * IMAGE_W + get_global_id(1);
// //Global memory guard for padding
// if(i < IMAGE_W*IMAGE_H)
// array_float[i] = (float)(array_int[i]);
//}//end kernel
/**
* \brief convert RGB of an array of 3xuint8 into a float output array.
*
* @param array_int: Pointer to global memory with the data in int
* @param array_float: Pointer to global memory with the data in float
* @param IMAGE_W: Width of the image
* @param IMAGE_H: Height of the image
*
* WARNING: still untested (formula is the same as PIL)
*/
__kernel void
rgb_to_float( __global unsigned char *array_int,
__global float *array_float,
const int IMAGE_W,
const int IMAGE_H
)
{
//Global memory guard for padding
if ((get_global_id(0)<IMAGE_W) && (get_global_id(1) < IMAGE_H)){
int i = get_global_id(0) + IMAGE_W * get_global_id(1);
array_float[i] = 0.299f*array_int[3*i] + 0.587f*array_int[3*i+1] + 0.114f*array_int[3*i+2];
} //end test in image
}//end kernel
/**
* \brief Performs normalization of image between 0 and max_out (255) in place.
*
*
* @param image Float pointer to global memory storing the image.
* @param min_in: Minimum value in the input array
* @param max_in: Maximum value in the input array
* @param max_out: Maximum value in the output array (255 adviced)
* @param IMAGE_W: Width of the image
* @param IMAGE_H: Height of the image
*
**/
__kernel void
normalizes( __global float *image,
__constant float * min_in __attribute__((max_constant_size(MAX_CONST_SIZE))),
__constant float * max_in __attribute__((max_constant_size(MAX_CONST_SIZE))),
__constant float * max_out __attribute__((max_constant_size(MAX_CONST_SIZE))),
const int IMAGE_W,
const int IMAGE_H
)
{
//Global memory guard for padding
if((get_global_id(0) < IMAGE_W) && (get_global_id(1)<IMAGE_H)){
int i = get_global_id(0) + IMAGE_W * get_global_id(1);
image[i] = max_out[0]*(image[i]-min_in[0])/(max_in[0]-min_in[0]);
};//end if in IMAGE
};//end kernel
/**
* \brief shrink: Subsampling of the image_in into a smaller image_out.
*
*
* @param image_in Float pointer to global memory storing the big image.
* @param image_ou Float pointer to global memory storing the small image.
* @param scale_w: Minimum value in the input array
* @param scale_h: Maximum value in the input array
* @param IMAGE_W: Width of the output image
* @param IMAGE_H: Height of the output image
*
**/
__kernel void
shrink(const __global float *image_in,
__global float *image_out,
const int scale_w,
const int scale_h,
const int LARGE_W,
const int LARGE_H,
const int SMALL_W,
const int SMALL_H
)
{
int gid0=get_global_id(0), gid1=get_global_id(1);
int j,i = gid0 + SMALL_W * gid1;
//Global memory guard for padding
if ((gid0 < SMALL_W) && (gid1 <SMALL_H))
{
j = gid0 * scale_w + gid1 * scale_h * LARGE_W;
image_out[i] = image_in[j];
};//end if in IMAGE
};//end kernel
/**
* \brief bin: resampling of the image_in into a smaller image_out with higher dynamics.
*
*
* @param image_in Float pointer to global memory storing the big image.
* @param image_ou Float pointer to global memory storing the small image.
* @param scale_width: Binning factor in horizontal
* @param scale_heigth: Binning factor in vertical
* @param orig_width: Original image size in horizontal
* @param orig_heigth: Original image size in vertical
* @param binned_width: Width of the output binned image
* @param binned_heigth: Height of the output binned image
*
* Nota: this is a 2D kernel. This is non working and non TESTED !!!
**/
__kernel void
bin( const __global float *image_in,
__global float *image_out,
const int scale_width,
const int scale_heigth,
const int orig_width,
const int orig_heigth,
const int binned_width,
const int binned_heigth
)
{
int gid0=get_global_id(0), gid1=get_global_id(1);
//Global memory guard for padding
if((gid0 < binned_width) && (gid1 < binned_heigth) ){
int j,i = gid0 + binned_width * gid1;
float data=0.0f;
int w, h, big_h, big_w;
for (h=gid1 * scale_heigth; h<(gid1+1) * scale_heigth; h++){
if (h>=orig_heigth){
big_h = 2*orig_heigth - h - 1;
}else{
big_h = h;
}
for (w=gid0*scale_width; w<(gid0+1)*scale_width; w++){
if (w>=orig_width){
big_w = 2*orig_width - w - 1;
}else{
big_w = w;
}
j = big_h * orig_width + big_w;
data += image_in[j];
};//end for horiz
};//end for vertical
image_out[i] = data/((float)(scale_width*scale_heigth));
};//end if in IMAGE
};//end kernel
/**
* \brief gaussian: Initialize a vector with a gaussian function.
*
*
* @param data: Float pointer to global memory storing the vector.
* @param sigma: width of the gaussian
* @param size: size of the function
*
**/
__kernel void
gaussian( __global float *data,
const float sigma,
const int SIZE
)
{
int gid=get_global_id(0);
if(gid < SIZE){
float x = ((float)gid - ((float)SIZE - 1.0f)/2.0f) / sigma;
float y = exp(-x * x / 2.0f);
data[gid] = y / sigma / sqrt(2.0f * M_PI_F);
}
}
/**
* \brief divide_cst: divide a vector by a constant.
*
*
* @param data: Float pointer to global memory storing the vector.
* @param value: calc data/value
* @param size: size of the vector
*
**/
__kernel void
divide_cst( __global float *data,
__global float *value,
const int SIZE
)
{
int gid=get_global_id(0);
if(gid < SIZE){
data[gid] = data[gid] / value[0];
}
}