-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconvolution.c
575 lines (484 loc) · 19.6 KB
/
convolution.c
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/** @file convolution.c
* Code extracted from : http://www.songho.ca/dsp/convolution/convolution.html
* And modified such as it perform a composition of 1D convolution horiz + vertical
* for separable filter
*/
#include "convolution.h"
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include <stdlib.h>
///////////////////////////////////////////////////////////////////////////////
// unsigned char (8-bit) version
///////////////////////////////////////////////////////////////////////////////
bool convolve2DSeparable8(unsigned char* in, unsigned char* out, int dataSizeX, int dataSizeY,
float* kernelX, int kSizeX, float* kernelY, int kSizeY)
{
int i, j, k, m, n;
float *tmp, *sum; // intermediate data buffer
unsigned char *inPtr, *outPtr; // working pointers
float *tmpPtr, *tmpPtr2; // working pointers
int kCenter, kOffset, endIndex; // kernel indice
// check validity of params
if(!in || !out || !kernelX || !kernelY) return false;
if(dataSizeX <= 0 || kSizeX <= 0) return false;
// allocate temp storage to keep intermediate result
tmp = (float*)malloc(dataSizeX * dataSizeY * sizeof(float));
if (!tmp) return false; // memory allocation error
// store accumulated sum
sum = (float*)malloc(dataSizeX * sizeof(float));
if (!sum) return false; // memory allocation error
// covolve horizontal direction ///////////////////////
// find center position of kernel (half of kernel size)
kCenter = kSizeX >> 1; // center index of kernel array
endIndex = dataSizeX - kCenter; // index for full kernel convolution
// init working pointers
inPtr = in;
tmpPtr = tmp; // store intermediate results from 1D horizontal convolution
// start horizontal convolution (x-direction)
for(i=0; i < dataSizeY; ++i) // number of rows
{
kOffset = 0; // starting index of partial kernel varies for each sample
// COLUMN FROM index=0 TO index=kCenter-1
for(j=0; j < kCenter; ++j)
{
*tmpPtr = 0; // init to 0 before accumulation
for(k = kCenter + kOffset, m = 0; k >= 0; --k, ++m) // convolve with partial of kernel
{
*tmpPtr += *(inPtr + m) * kernelX[k];
}
++tmpPtr; // next output
++kOffset; // increase starting index of kernel
}
// COLUMN FROM index=kCenter TO index=(dataSizeX-kCenter-1)
for(j = kCenter; j < endIndex; ++j)
{
*tmpPtr = 0; // init to 0 before accumulate
for(k = kSizeX-1, m = 0; k >= 0; --k, ++m) // full kernel
{
*tmpPtr += *(inPtr + m) * kernelX[k];
}
++inPtr; // next input
++tmpPtr; // next output
}
kOffset = 1; // ending index of partial kernel varies for each sample
// COLUMN FROM index=(dataSizeX-kCenter) TO index=(dataSizeX-1)
for(j = endIndex; j < dataSizeX; ++j)
{
*tmpPtr = 0; // init to 0 before accumulation
for(k = kSizeX-1, m=0; k >= kOffset; --k, ++m) // convolve with partial of kernel
{
*tmpPtr += *(inPtr + m) * kernelX[k];
}
++inPtr; // next input
++tmpPtr; // next output
++kOffset; // increase ending index of partial kernel
}
inPtr += kCenter; // next row
}
// END OF HORIZONTAL CONVOLUTION //////////////////////
// start vertical direction ///////////////////////////
// find center position of kernel (half of kernel size)
kCenter = kSizeY >> 1; // center index of vertical kernel
endIndex = dataSizeY - kCenter; // index where full kernel convolution should stop
// set working pointers
tmpPtr = tmpPtr2 = tmp;
outPtr = out;
// clear out array before accumulation
for(i = 0; i < dataSizeX; ++i)
sum[i] = 0;
// start to convolve vertical direction (y-direction)
// ROW FROM index=0 TO index=(kCenter-1)
kOffset = 0; // starting index of partial kernel varies for each sample
for(i=0; i < kCenter; ++i)
{
for(k = kCenter + kOffset; k >= 0; --k) // convolve with partial kernel
{
for(j=0; j < dataSizeX; ++j)
{
sum[j] += *tmpPtr * kernelY[k];
++tmpPtr;
}
}
for(n = 0; n < dataSizeX; ++n) // convert and copy from sum to out
{
// covert negative to positive
*outPtr = (unsigned char)((float)fabs(sum[n]) + 0.5f);
sum[n] = 0; // reset to zero for next summing
++outPtr; // next element of output
}
tmpPtr = tmpPtr2; // reset input pointer
++kOffset; // increase starting index of kernel
}
// ROW FROM index=kCenter TO index=(dataSizeY-kCenter-1)
for(i = kCenter; i < endIndex; ++i)
{
for(k = kSizeY -1; k >= 0; --k) // convolve with full kernel
{
for(j = 0; j < dataSizeX; ++j)
{
sum[j] += *tmpPtr * kernelY[k];
++tmpPtr;
}
}
for(n = 0; n < dataSizeX; ++n) // convert and copy from sum to out
{
// covert negative to positive
*outPtr = (unsigned char)((float)fabs(sum[n]) + 0.5f);
sum[n] = 0; // reset for next summing
++outPtr; // next output
}
// move to next row
tmpPtr2 += dataSizeX;
tmpPtr = tmpPtr2;
}
// ROW FROM index=(dataSizeY-kCenter) TO index=(dataSizeY-1)
kOffset = 1; // ending index of partial kernel varies for each sample
for(i=endIndex; i < dataSizeY; ++i)
{
for(k = kSizeY-1; k >= kOffset; --k) // convolve with partial kernel
{
for(j=0; j < dataSizeX; ++j)
{
sum[j] += *tmpPtr * kernelY[k];
++tmpPtr;
}
}
for(n = 0; n < dataSizeX; ++n) // convert and copy from sum to out
{
// covert negative to positive
*outPtr = (unsigned char)((float)fabs(sum[n]) + 0.5f);
sum[n] = 0; // reset for next summing
++outPtr; // next output
}
// move to next row
tmpPtr2 += dataSizeX;
tmpPtr = tmpPtr2; // next input
++kOffset; // increase ending index of kernel
}
// END OF VERTICAL CONVOLUTION ////////////////////////
// deallocate temp buffers
free(tmp);
free(sum);
return true;
}
///////////////////////////////////////////////////////////////////////////////
// float version
///////////////////////////////////////////////////////////////////////////////
bool convolve2DSeparable(float* in, float* out, int dataSizeX, int dataSizeY, const float* kernelX,
int kSizeX, const float* kernelY, int kSizeY)
{
int i, j, k, m, n;
float *tmp, *sum; // intermediate data buffer
float *inPtr, *outPtr; // working pointers
float *tmpPtr, *tmpPtr2; // working pointers
int kCenter, kOffset, endIndex; // kernel indice
// check validity of params
if (!in || !out || !kernelX || !kernelY)
return false;
if (dataSizeX <= 0 || kSizeX <= 0)
return false;
// allocate temp storage to keep intermediate result
//tmp = new float[dataSizeX * dataSizeY];
tmp = (float*)malloc(dataSizeX * dataSizeY * sizeof(float));
if (!tmp)
return false; // memory allocation error
// store accumulated sum
//sum = new float[dataSizeX];
sum = (float*)malloc(dataSizeX * sizeof(float));
if (!sum)
return false; // memory allocation error
// covolve horizontal direction ///////////////////////
// find center position of kernel (half of kernel size)
kCenter = kSizeX >> 1; // center index of kernel array
endIndex = dataSizeX - kCenter; // index for full kernel convolution
// init working pointers
inPtr = in;
tmpPtr = tmp; // store intermediate results from 1D horizontal convolution
// start horizontal convolution (x-direction)
for (i = 0; i < dataSizeY; ++i) // number of rows
{
kOffset = 0; // starting index of partial kernel varies for each sample
// COLUMN FROM index=0 TO index=kCenter-1
for (j = 0; j < kCenter; ++j)
{
*tmpPtr = 0; // init to 0 before accumulation
for (k = kCenter + kOffset, m = 0; k >= 0; --k, ++m) // convolve with partial of kernel
{
*tmpPtr += *(inPtr + m) * kernelX[k];
}
++tmpPtr; // next output
++kOffset; // increase starting index of kernel
}
// COLUMN FROM index=kCenter TO index=(dataSizeX-kCenter-1)
for (j = kCenter; j < endIndex; ++j)
{
*tmpPtr = 0; // init to 0 before accumulate
for (k = kSizeX - 1, m = 0; k >= 0; --k, ++m) // full kernel
{
*tmpPtr += *(inPtr + m) * kernelX[k];
}
++inPtr; // next input
++tmpPtr; // next output
}
kOffset = 1; // ending index of partial kernel varies for each sample
// COLUMN FROM index=(dataSizeX-kCenter) TO index=(dataSizeX-1)
for (j = endIndex; j < dataSizeX; ++j)
{
*tmpPtr = 0; // init to 0 before accumulation
for (k = kSizeX - 1, m = 0; k >= kOffset; --k, ++m) // convolve with partial of kernel
{
*tmpPtr += *(inPtr + m) * kernelX[k];
}
++inPtr; // next input
++tmpPtr; // next output
++kOffset; // increase ending index of partial kernel
}
inPtr += kCenter; // next row
}
// END OF HORIZONTAL CONVOLUTION //////////////////////
// start vertical direction ///////////////////////////
// find center position of kernel (half of kernel size)
kCenter = kSizeY >> 1; // center index of vertical kernel
endIndex = dataSizeY - kCenter; // index where full kernel convolution should stop
// set working pointers
tmpPtr = tmpPtr2 = tmp;
outPtr = out;
// clear out array before accumulation
for (i = 0; i < dataSizeX; ++i)
sum[i] = 0;
// start to convolve vertical direction (y-direction)
// ROW FROM index=0 TO index=(kCenter-1)
kOffset = 0; // starting index of partial kernel varies for each sample
for (i = 0; i < kCenter; ++i)
{
for (k = kCenter + kOffset; k >= 0; --k) // convolve with partial kernel
{
for (j = 0; j < dataSizeX; ++j)
{
sum[j] += *tmpPtr * kernelY[k];
++tmpPtr;
}
}
for (n = 0; n < dataSizeX; ++n) // convert and copy from sum to out
{
// covert negative to positive
*outPtr = (float)((float)fabs(sum[n]) + 0.5f);
sum[n] = 0; // reset to zero for next summing
++outPtr; // next element of output
}
tmpPtr = tmpPtr2; // reset input pointer
++kOffset; // increase starting index of kernel
}
// ROW FROM index=kCenter TO index=(dataSizeY-kCenter-1)
for (i = kCenter; i < endIndex; ++i)
{
for (k = kSizeY - 1; k >= 0; --k) // convolve with full kernel
{
for (j = 0; j < dataSizeX; ++j)
{
sum[j] += *tmpPtr * kernelY[k];
++tmpPtr;
}
}
for (n = 0; n < dataSizeX; ++n) // convert and copy from sum to out
{
// covert negative to positive
*outPtr = (float)((float)fabs(sum[n]) + 0.5f);
sum[n] = 0; // reset for next summing
++outPtr; // next output
}
// move to next row
tmpPtr2 += dataSizeX;
tmpPtr = tmpPtr2;
}
// ROW FROM index=(dataSizeY-kCenter) TO index=(dataSizeY-1)
kOffset = 1; // ending index of partial kernel varies for each sample
for (i = endIndex; i < dataSizeY; ++i)
{
for (k = kSizeY - 1; k >= kOffset; --k) // convolve with partial kernel
{
for (j = 0; j < dataSizeX; ++j)
{
sum[j] += *tmpPtr * kernelY[k];
++tmpPtr;
}
}
for (n = 0; n < dataSizeX; ++n) // convert and copy from sum to out
{
// covert negative to positive
*outPtr = (float)((float)fabs(sum[n]) + 0.5f);
sum[n] = 0; // reset for next summing
++outPtr; // next output
}
// move to next row
tmpPtr2 += dataSizeX;
tmpPtr = tmpPtr2; // next input
++kOffset; // increase ending index of kernel
}
// END OF VERTICAL CONVOLUTION ////////////////////////
// deallocate temp buffers
/*delete[] tmp;
delete[] sum;*/
free(tmp); free(sum);
return true;
}
///////////////////////////////////////////////////////////////////////////////
// 1D convolution vertical
///////////////////////////////////////////////////////////////////////////////
bool convV(float* in, float* out, int dataSizeX, int dataSizeY, const float* kernelY, int kSizeY)
{
// check validity of params
if (!in || !out || !kernelY)
return false;
if (dataSizeX <= 0)
return false;
// Save temporary horizontal convolution for the entire image
int N = dataSizeX * dataSizeY;
//float* tmpx = new float[N];
float *tmpx = (float*)calloc(N, sizeof(float));
if (!tmpx)
return false; // memory allocation error
//for (int i = 0; i < N; ++i)
// tmpx[i] = 0;
// Save temporary vertical convolution for one row
//float* tmpsum = new float[dataSizeX];
float *tmpsum = (float*)calloc(dataSizeX, sizeof(float));
if (!tmpsum)
return false; // memory allocation error
//for (int i = 0; i < dataSizeX; ++i)
// tmpsum[i] = 0;
// find center position of kernel (half of kernel size)
int kCenter = kSizeY >> 1; // center index of kernel array
int endIndex = dataSizeX - kCenter; // index for full kernel convolution
int right_half = kSizeY - kCenter - 1; // size of right half right to index 'kCenter' in the kernel
kCenter = kSizeY >> 1;
endIndex = dataSizeY - kCenter;
right_half = kSizeY - kCenter - 1;
// [0, kCenter - 1]
int offset = 0;
for (int j = 0; j < kCenter; ++j)
{
for (int k = kCenter + offset, row = 0; k >= 0; k--, row++)
{
for (int i = 0; i < dataSizeX; ++i)
{
int idx = row * dataSizeX + i;
tmpsum[i] += tmpx[idx] * kernelY[k]; // tmpsum is 1D row vector
}
}
offset++;
// Copy tmpSum result to final output image. One 1D row vector 'tmpsum'
// is enough and this can save storage.
for (int i = 0; i < dataSizeX; ++i)
{
int idx = j * dataSizeX + i;
out[idx] = (float)tmpsum[i] + 0.5f;
tmpsum[i] = 0;
}
}
// [kCenter, endIndex - 1]
for (int j = kCenter; j < endIndex; ++j)
{
for (int k = kSizeY - 1, m = 0; k >= 0; k--, m++)
{
int row = j - right_half + m;
for (int i = 0; i < dataSizeX; ++i)
{
int idx = row * dataSizeX + i;
tmpsum[i] += tmpx[idx] * kernelY[k];
}
}
for (int i = 0; i < dataSizeX; ++i)
{
int idx = j * dataSizeX + i;
out[idx] = (float)tmpsum[i] + 0.5f;
tmpsum[i] = 0;
}
}
// [endIndex, dataSizeY - 1]
offset = 1;
for (int j = endIndex; j < dataSizeY; ++j)
{
for (int k = kSizeY - 1, m = 0; k >= offset; k--, m++)
{
int row = j - right_half + m;
for (int i = 0; i < dataSizeX; ++i)
{
int idx = row * dataSizeX + i;
tmpsum[i] += tmpx[idx] * kernelY[k]; // tmpsum is 1D row vector
}
}
offset++;
for (int i = 0; i < dataSizeX; ++i)
{
int idx = j * dataSizeX + i;
out[idx] =(float)tmpsum[i] + 0.5f;
tmpsum[i] = 0;
}
}
free(tmpx); free(tmpsum);
return true;
}
bool convH(float* in, float* out, int dataSizeX, int dataSizeY, const float* kernelX, int kSizeX)
{
int i, j, k, m;
float *inPtr; // working pointers
float *tmpPtr; // working pointers
int kCenter, kOffset, endIndex; // kernel indice
// check validity of params
if (!in || !out || !kernelX)
return false;
if (dataSizeX <= 0 || kSizeX <= 0)
return false;
// covolve horizontal direction ///////////////////////
// find center position of kernel (half of kernel size)
kCenter = kSizeX >> 1; // center index of kernel array
endIndex = dataSizeX - kCenter; // index for full kernel convolution
// init working pointers
inPtr = in;
tmpPtr = out; // store intermediate results from 1D horizontal convolution
// start horizontal convolution (x-direction)
for (i = 0; i < dataSizeY; ++i) // number of rows
{
kOffset = 0; // starting index of partial kernel varies for each sample
// COLUMN FROM index=0 TO index=kCenter-1
for (j = 0; j < kCenter; ++j)
{
*tmpPtr = 0; // init to 0 before accumulation
for (k = kCenter + kOffset, m = 0; k >= 0; --k, ++m) // convolve with partial of kernel
{
*tmpPtr += *(inPtr + m) * kernelX[k];
}
++tmpPtr; // next output
++kOffset; // increase starting index of kernel
}
// COLUMN FROM index=kCenter TO index=(dataSizeX-kCenter-1)
for (j = kCenter; j < endIndex; ++j)
{
*tmpPtr = 0; // init to 0 before accumulate
for (k = kSizeX - 1, m = 0; k >= 0; --k, ++m) // full kernel
{
*tmpPtr += *(inPtr + m) * kernelX[k];
}
++inPtr; // next input
++tmpPtr; // next output
}
kOffset = 1; // ending index of partial kernel varies for each sample
// COLUMN FROM index=(dataSizeX-kCenter) TO index=(dataSizeX-1)
for (j = endIndex; j < dataSizeX; ++j)
{
*tmpPtr = 0; // init to 0 before accumulation
for (k = kSizeX - 1, m = 0; k >= kOffset; --k, ++m) // convolve with partial of kernel
{
*tmpPtr += *(inPtr + m) * kernelX[k];
}
++inPtr; // next input
++tmpPtr; // next output
++kOffset; // increase ending index of partial kernel
}
inPtr += kCenter; // next row
}
// END OF HORIZONTAL CONVOLUTION //////////////////////
return true;
}