-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.cpp
More file actions
330 lines (302 loc) · 8.95 KB
/
image.cpp
File metadata and controls
330 lines (302 loc) · 8.95 KB
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
#include <cmath>
#include <iostream>
#include <cassert>
#include <utility>
#include "image.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
Image::Image(std::string file_path)
{
unsigned char *img_data = stbi_load(file_path.c_str(), &width, &height, &channels, 0);
if (img_data == nullptr) {
const char *error_msg = stbi_failure_reason();
std::cerr << "Failed to load image: " << file_path.c_str() << "\n";
std::cerr << "Error msg (stb_image): " << error_msg << "\n";
std::exit(1);
}
size = width * height * channels;
data = new float[size];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
for (int c = 0; c < channels; c++) {
int src_idx = y*width*channels + x*channels + c;
int dst_idx = c*height*width + y*width + x;
data[dst_idx] = img_data[src_idx] / 255.;
}
}
}
if (channels == 4)
channels = 3; //ignore alpha channel
stbi_image_free(img_data);
}
Image::Image(int w, int h, int c)
:width {w},
height {h},
channels {c},
size {w*h*c},
data {new float[w*h*c]()}
{
}
Image::Image()
:width {0},
height {0},
channels {0},
size {0},
data {nullptr}
{
}
Image::~Image()
{
delete[] this->data;
}
Image::Image(const Image& other)
:width {other.width},
height {other.height},
channels {other.channels},
size {other.size},
data {new float[other.size]}
{
//std::cout << "copy constructor\n";
for (int i = 0; i < size; i++)
data[i] = other.data[i];
}
Image& Image::operator=(const Image& other)
{
if (this != &other) {
delete[] data;
//std::cout << "copy assignment\n";
width = other.width;
height = other.height;
channels = other.channels;
size = other.size;
data = new float[other.size];
for (int i = 0; i < other.size; i++)
data[i] = other.data[i];
}
return *this;
}
Image::Image(Image&& other)
:width {other.width},
height {other.height},
channels {other.channels},
size {other.size},
data {other.data}
{
//std::cout << "move constructor\n";
other.data = nullptr;
other.size = 0;
}
Image& Image::operator=(Image&& other)
{
//std::cout << "move assignment\n";
delete[] data;
data = other.data;
width = other.width;
height = other.height;
channels = other.channels;
size = other.size;
other.data = nullptr;
other.size = 0;
return *this;
}
//save image as jpg file
bool Image::save(std::string file_path)
{
unsigned char *out_data = new unsigned char[width*height*channels];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
for (int c = 0; c < channels; c++) {
int dst_idx = y*width*channels + x*channels + c;
int src_idx = c*height*width + y*width + x;
out_data[dst_idx] = std::roundf(data[src_idx] * 255.);
}
}
}
bool success = stbi_write_jpg(file_path.c_str(), width, height, channels, out_data, 100);
if (!success)
std::cerr << "Failed to save image: " << file_path << "\n";
delete[] out_data;
return true;
}
void Image::set_pixel(int x, int y, int c, float val)
{
if (x >= width || x < 0 || y >= height || y < 0 || c >= channels || c < 0) {
std::cerr << "set_pixel() error: Index out of bounds.\n";
std::exit(1);
}
data[c*width*height + y*width + x] = val;
}
float Image::get_pixel(int x, int y, int c) const
{
if (x < 0)
x = 0;
if (x >= width)
x = width - 1;
if (y < 0)
y = 0;
if (y >= height)
y = height - 1;
return data[c*width*height + y*width + x];
}
void Image::clamp()
{
int size = width * height * channels;
for (int i = 0; i < size; i++) {
float val = data[i];
val = (val > 1.0) ? 1.0 : val;
val = (val < 0.0) ? 0.0 : val;
data[i] = val;
}
}
//map coordinate from 0-current_max range to 0-new_max range
float map_coordinate(float new_max, float current_max, float coord)
{
float a = new_max / current_max;
float b = -0.5 + a*0.5;
return a*coord + b;
}
Image Image::resize(int new_w, int new_h, Interpolation method) const
{
Image resized(new_w, new_h, this->channels);
float value = 0;
for (int x = 0; x < new_w; x++) {
for (int y = 0; y < new_h; y++) {
for (int c = 0; c < resized.channels; c++) {
float old_x = map_coordinate(this->width, new_w, x);
float old_y = map_coordinate(this->height, new_h, y);
if (method == Interpolation::BILINEAR)
value = bilinear_interpolate(*this, old_x, old_y, c);
else if (method == Interpolation::NEAREST)
value = nn_interpolate(*this, old_x, old_y, c);
resized.set_pixel(x, y, c, value);
}
}
}
return resized;
}
float bilinear_interpolate(const Image& img, float x, float y, int c)
{
float p1, p2, p3, p4, q1, q2;
float x_floor = std::floor(x), y_floor = std::floor(y);
float x_ceil = x_floor + 1, y_ceil = y_floor + 1;
p1 = img.get_pixel(x_floor, y_floor, c);
p2 = img.get_pixel(x_ceil, y_floor, c);
p3 = img.get_pixel(x_floor, y_ceil, c);
p4 = img.get_pixel(x_ceil, y_ceil, c);
q1 = (y_ceil-y)*p1 + (y-y_floor)*p3;
q2 = (y_ceil-y)*p2 + (y-y_floor)*p4;
return (x_ceil-x)*q1 + (x-x_floor)*q2;
}
float nn_interpolate(const Image& img, float x, float y, int c)
{
return img.get_pixel(std::round(x), std::round(y), c);
}
Image rgb_to_grayscale(const Image& img)
{
assert(img.channels == 3);
Image gray(img.width, img.height, 1);
for (int x = 0; x < img.width; x++) {
for (int y = 0; y < img.height; y++) {
float red, green, blue;
red = img.get_pixel(x, y, 0);
green = img.get_pixel(x, y, 1);
blue = img.get_pixel(x, y, 2);
gray.set_pixel(x, y, 0, 0.299*red + 0.587*green + 0.114*blue);
}
}
return gray;
}
Image grayscale_to_rgb(const Image& img)
{
assert(img.channels == 1);
Image rgb(img.width, img.height, 3);
for (int x = 0; x < img.width; x++) {
for (int y = 0; y < img.height; y++) {
float gray_val = img.get_pixel(x, y, 0);
rgb.set_pixel(x, y, 0, gray_val);
rgb.set_pixel(x, y, 1, gray_val);
rgb.set_pixel(x, y, 2, gray_val);
}
}
return rgb;
}
// separable 2D gaussian blur for 1 channel image
Image gaussian_blur(const Image& img, float sigma)
{
assert(img.channels == 1);
int size = std::ceil(6 * sigma);
if (size % 2 == 0)
size++;
int center = size / 2;
Image kernel(size, 1, 1);
float sum = 0;
for (int k = -size/2; k <= size/2; k++) {
float val = std::exp(-(k*k) / (2*sigma*sigma));
kernel.set_pixel(center+k, 0, 0, val);
sum += val;
}
for (int k = 0; k < size; k++)
kernel.data[k] /= sum;
Image tmp(img.width, img.height, 1);
Image filtered(img.width, img.height, 1);
// convolve vertical
for (int x = 0; x < img.width; x++) {
for (int y = 0; y < img.height; y++) {
float sum = 0;
for (int k = 0; k < size; k++) {
int dy = -center + k;
sum += img.get_pixel(x, y+dy, 0) * kernel.data[k];
}
tmp.set_pixel(x, y, 0, sum);
}
}
// convolve horizontal
for (int x = 0; x < img.width; x++) {
for (int y = 0; y < img.height; y++) {
float sum = 0;
for (int k = 0; k < size; k++) {
int dx = -center + k;
sum += tmp.get_pixel(x+dx, y, 0) * kernel.data[k];
}
filtered.set_pixel(x, y, 0, sum);
}
}
return filtered;
}
void draw_point(Image& img, int x, int y, int size)
{
for (int i = x-size/2; i <= x+size/2; i++) {
for (int j = y-size/2; j <= y+size/2; j++) {
if (i < 0 || i >= img.width) continue;
if (j < 0 || j >= img.height) continue;
if (std::abs(i-x) + std::abs(j-y) > size/2) continue;
if (img.channels == 3) {
img.set_pixel(i, j, 0, 1.f);
img.set_pixel(i, j, 1, 0.f);
img.set_pixel(i, j, 2, 0.f);
} else {
img.set_pixel(i, j, 0, 1.f);
}
}
}
}
void draw_line(Image& img, int x1, int y1, int x2, int y2)
{
if (x2 < x1) {
std::swap(x1, x2);
std::swap(y1, y2);
}
int dx = x2 - x1, dy = y2 - y1;
for (int x = x1; x < x2; x++) {
int y = y1 + dy*(x-x1)/dx;
if (img.channels == 3) {
img.set_pixel(x, y, 0, 0.f);
img.set_pixel(x, y, 1, 1.f);
img.set_pixel(x, y, 2, 0.f);
} else {
img.set_pixel(x, y, 0, 1.f);
}
}
}