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
use crate::padding::uniform;
use crate::Filter;
use photon_rs::PhotonImage;
fn convolve(
img_padded: &PhotonImage,
filter: &Filter,
width_conv: u32,
height_conv: u32,
stride: u32,
) -> PhotonImage {
let raw_pixel_padded = img_padded.get_raw_pixels();
let width_padded = img_padded.get_width() as usize;
let height_padded = img_padded.get_height() as usize;
let mut img_conv = vec![];
let filter_width = filter.width();
let filter_height = filter.height();
let mut pixel = 0_usize;
let image_end = (width_padded * height_padded * 4) as usize;
let step = 4 * stride as usize;
while pixel < image_end - 4 {
if pixel != 0 && ((pixel / 4) % width_padded) > (width_padded - filter_width) {
pixel = ((pixel / 4) / width_padded + stride as usize) * width_padded * 4;
if (pixel / 4) / width_padded + filter_height > height_padded {
break;
}
}
let mut img_conv_r: f32 = 0_f32;
let mut img_conv_g: f32 = 0_f32;
let mut img_conv_b: f32 = 0_f32;
for x in 0..filter_width {
for y in 0..filter_height {
let kernel_element_val = filter
.get_element(x, y)
.expect("[ERROR]: Tried to access out-of-bounds value in the filter");
let img_pixel_r = raw_pixel_padded[x * width_padded * 4 + pixel + y * 4];
let img_pixel_g = raw_pixel_padded[x * width_padded * 4 + pixel + y * 4 + 1];
let img_pixel_b = raw_pixel_padded[x * width_padded * 4 + pixel + y * 4 + 2];
img_conv_r += img_pixel_r as f32 * kernel_element_val;
img_conv_g += img_pixel_g as f32 * kernel_element_val;
img_conv_b += img_pixel_b as f32 * kernel_element_val;
}
}
img_conv_r = f32::clamp(img_conv_r, 0.0, 255.0);
img_conv_g = f32::clamp(img_conv_g, 0.0, 255.0);
img_conv_b = f32::clamp(img_conv_b, 0.0, 255.0);
img_conv.push(img_conv_r as u8);
img_conv.push(img_conv_g as u8);
img_conv.push(img_conv_b as u8);
img_conv.push(255_u8);
pixel += step;
}
for _ in (img_conv.len()..(width_conv * height_conv * 4) as usize).step_by(1) {
img_conv.push(255_u8);
img_conv.push(255_u8);
img_conv.push(255_u8);
img_conv.push(255_u8);
}
println!("Convolution done...");
PhotonImage::new(img_conv, width_conv, height_conv)
}
fn adjust_data_params(
img: &PhotonImage,
img_padded: &PhotonImage,
filter: &Filter,
stride: u32,
padding: &str,
pad_amt: u32,
) -> PhotonImage {
let mut img_conv_width: u32;
let mut img_conv_height: u32;
match padding {
"uniform" => {
img_conv_width = img.get_width() - filter.width() as u32 + 2 * pad_amt;
if img_conv_width % stride != 0 {
eprintln!("[WARNING]: stride value not suitable. Convolution may fail.");
}
img_conv_width /= stride;
img_conv_width += 1;
img_conv_height = img.get_height() - filter.height() as u32 + 2 * pad_amt;
if img_conv_height % stride != 0 {
eprintln!("[WARNING]: stride value not suitable. Convolution may fail.");
}
img_conv_height /= stride;
img_conv_height += 1;
}
"none" => {
img_conv_width = img.get_width() - filter.width() as u32;
if img_conv_width % stride != 0 {
eprintln!("[WARNING]: stride value not suitable. Convolution may fail.");
}
img_conv_width /= stride;
img_conv_width += 1;
img_conv_height = img.get_height() - filter.height() as u32;
if img_conv_height % stride != 0 {
eprintln!("[WARNING]: stride value not suitable. Convolution may fail.");
}
img_conv_height /= stride;
img_conv_height += 1;
}
_ => {
eprintln!(
"[ERROR]: Couldn't ascertain the padding type and its amount.\n Using zero padding"
);
img_conv_width = img.get_width() - filter.width() as u32 + 1;
img_conv_height = img.get_height() - filter.height() as u32 + 1;
}
};
convolve(img_padded, filter, img_conv_width, img_conv_height, stride)
}
pub fn convolution(
img: &PhotonImage,
filter: Filter,
stride: u32,
padding: &str,
padding_amt: u32,
) -> PhotonImage {
match stride {
0 => {
eprintln!("[ERROR]: Stride provided = 0");
std::process::exit(1);
}
1 => match padding {
"uniform" => {
let img_padded = uniform(&img, padding_amt);
adjust_data_params(img, &img_padded, &filter, stride, padding, padding_amt)
}
"none" => {
let img_padded = img.clone();
adjust_data_params(img, &img_padded, &filter, stride, padding, 0)
}
_ => {
eprintln!("[ERROR]: Only uniform or no-padding allowed");
std::process::exit(1);
}
},
_ => match padding {
"uniform" => {
let img_padded = uniform(&img, padding_amt);
adjust_data_params(img, &img_padded, &filter, stride, padding, padding_amt)
}
"none" => {
let img_padded = img.clone();
adjust_data_params(img, &img_padded, &filter, stride, padding, 0)
}
_ => {
eprintln!("[ERROR]: Only uniform or no-padding allowed");
std::process::exit(1);
}
},
}
}