1 #include "im2col.h"
2 #include <stdio.h>
im2col_get_pixel(float * im,int height,int width,int channels,int row,int col,int channel,int pad)3 float im2col_get_pixel(float *im, int height, int width, int channels,
4                         int row, int col, int channel, int pad)
5 {
6     row -= pad;
7     col -= pad;
8 
9     if (row < 0 || col < 0 ||
10         row >= height || col >= width) return 0;
11     return im[col + width*(row + height*channel)];
12 }
13 
14 //From Berkeley Vision's Caffe!
15 //https://github.com/BVLC/caffe/blob/master/LICENSE
im2col_cpu(float * data_im,int channels,int height,int width,int ksize,int stride,int pad,float * data_col)16 void im2col_cpu(float* data_im,
17      int channels,  int height,  int width,
18      int ksize,  int stride, int pad, float* data_col)
19 {
20     int c,h,w;
21     int height_col = (height + 2*pad - ksize) / stride + 1;
22     int width_col = (width + 2*pad - ksize) / stride + 1;
23 
24     int channels_col = channels * ksize * ksize;
25     for (c = 0; c < channels_col; ++c) {
26         int w_offset = c % ksize;
27         int h_offset = (c / ksize) % ksize;
28         int c_im = c / ksize / ksize;
29         for (h = 0; h < height_col; ++h) {
30             for (w = 0; w < width_col; ++w) {
31                 int im_row = h_offset + h * stride;
32                 int im_col = w_offset + w * stride;
33                 int col_index = (c * height_col + h) * width_col + w;
34                 data_col[col_index] = im2col_get_pixel(data_im, height, width, channels,
35                         im_row, im_col, c_im, pad);
36             }
37         }
38     }
39 }
40 
41 
42 // Function uses casting from int to unsigned to compare if value of
43 // parameter a is greater or equal to zero and lower than value of
44 // parameter b. The b parameter is of type signed and is always positive,
45 // therefore its value is always lower than 0x800... where casting
46 // negative value of a parameter converts it to value higher than 0x800...
47 // The casting allows to use one condition instead of two.
is_a_ge_zero_and_a_lt_b(int a,int b)48 inline static int is_a_ge_zero_and_a_lt_b(int a, int b) {
49     return (unsigned)(a) < (unsigned)(b);
50 }
51 
52 // https://github.com/BVLC/caffe/blob/master/src/caffe/util/im2col.cpp
im2col_cpu_ext(const float * data_im,const int channels,const int height,const int width,const int kernel_h,const int kernel_w,const int pad_h,const int pad_w,const int stride_h,const int stride_w,const int dilation_h,const int dilation_w,float * data_col)53 void im2col_cpu_ext(const float* data_im, const int channels,
54     const int height, const int width, const int kernel_h, const int kernel_w,
55     const int pad_h, const int pad_w,
56     const int stride_h, const int stride_w,
57     const int dilation_h, const int dilation_w,
58     float* data_col)
59 {
60     const int output_h = (height + 2 * pad_h -
61         (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
62     const int output_w = (width + 2 * pad_w -
63         (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
64     const int channel_size = height * width;
65     int channel, kernel_row, kernel_col, output_rows, output_col;
66     for (channel = channels; channel--; data_im += channel_size) {
67         for (kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
68             for (kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
69                 int input_row = -pad_h + kernel_row * dilation_h;
70                 for (output_rows = output_h; output_rows; output_rows--) {
71                     if (!is_a_ge_zero_and_a_lt_b(input_row, height)) {
72                         for (output_col = output_w; output_col; output_col--) {
73                             *(data_col++) = 0;
74                         }
75                     }
76                     else {
77                         int input_col = -pad_w + kernel_col * dilation_w;
78                         for (output_col = output_w; output_col; output_col--) {
79                             if (is_a_ge_zero_and_a_lt_b(input_col, width)) {
80                                 *(data_col++) = data_im[input_row * width + input_col];
81                             }
82                             else {
83                                 *(data_col++) = 0;
84                             }
85                             input_col += stride_w;
86                         }
87                     }
88                     input_row += stride_h;
89                 }
90             }
91         }
92     }
93 }
94