forked from npd/imgutils
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.cpp
More file actions
101 lines (89 loc) · 2.48 KB
/
utils.cpp
File metadata and controls
101 lines (89 loc) · 2.48 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
//
// Created by Nicola Pierazzo on 21/10/15.
//
#include <cstdlib>
#include <cstring>
#include "utils.hpp"
extern "C" {
#include "iio.h"
}
using std::string;
using std::free;
using std::strcmp;
namespace imgutils {
const char *pick_option(int *c, char **v, const char *o, const char *d) {
int id = d ? 1 : 0;
for (int i = 0; i < *c - id; i++) {
if (v[i][0] == '-' && 0 == strcmp(v[i] + 1, o)) {
char *r = v[i + id] + 1 - id;
for (int j = i; j < *c - id; j++)
v[j] = v[j + id + 1];
*c -= id + 1;
return r;
}
}
return d;
}
Image read_image(const string &filename) {
int w, h, c;
float *data = iio_read_image_float_vec(filename.c_str(), &w, &h, &c);
Image im(data, h, w, c);
free(data);
return im;
}
void save_image(const Image &image, const string &filename) {
iio_save_image_float_vec(const_cast<char *>(filename.c_str()),
const_cast<float *>(image.data()),
image.columns(),
image.rows(),
image.channels());
}
inline int SymmetricCoordinate(int pos, int size) {
if (pos < 0) pos = -pos - 1;
if (pos >= 2 * size) pos %= 2 * size;
if (pos >= size) pos = 2 * size - 1 - pos;
return pos;
}
Image pad_symmetric(const Image &src, int padding) {
Image result(src.rows() + 2 * padding,
src.columns() + 2 * padding,
src.channels());
for (int row = 0; row < result.rows(); ++row) {
for (int col = 0; col < result.columns(); ++col) {
for (int chan = 0; chan < result.channels(); ++chan) {
result.val(col, row, chan) =
src.val(SymmetricCoordinate(col - padding, src.columns()),
SymmetricCoordinate(row - padding, src.rows()),
chan);
}
}
}
return result;
}
bool isMonochrome (const Image &u) {
for (int row = 0; row < u.rows(); ++row) {
for (int col = 0; col < u.columns(); ++col) {
float v = u.val(col, row, 0);
for (int ch = 1; ch < u.channels(); ++ch) {
if (u.val(col, row, ch) != v) {
return false;
}
}
}
}
return true;
}
Image makeMonochrome (const Image &u) {
Image result(u.rows(), u.columns());
for (int row = 0; row < u.rows(); ++row) {
for (int col = 0; col < u.columns(); ++col) {
double v = 0;
for (int ch = 0; ch < u.channels(); ++ch) {
v += u.val(col, row, ch);
}
result.val(col,row) = v / u.channels();
}
}
return result;
}
}