-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.cpp
More file actions
337 lines (275 loc) · 7.34 KB
/
Image.cpp
File metadata and controls
337 lines (275 loc) · 7.34 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
331
332
333
334
335
336
337
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image.h"
#include "stb_image_write.h"
#include "Image.h"
#include <fstream>
#include <iostream>
#include <math.h>
using namespace std;
//CONSTRUCTORS
Image::Image(const char* filename)
{
readImageData(filename);
}
Image::Image(int w, int h, int channels)
: w(w), h(h), channels(channels)
{
if (size() > 0)
pixels.resize(size());
}
Image::Image(const Image& img)
: Image(img.w, img.h, img.channels)
{
pixels = img.pixels;
}
Image::Pixel::Pixel(uint8_t R, uint8_t G, uint8_t B) : R(R), G(G), B(B) {};
//MANAGING FILES
void Image::readImageData(const char* filename)
{
uint8_t* data = nullptr;
data = stbi_load(filename, &w, &h, &channels, 0);
pixels = formatToPixels(data);
bool isReadSuccessfull = data != nullptr;
delete[] data;
data = nullptr;
printOperationStatus(isReadSuccessfull, filename, "read");
}
void Image::writeImageData(const char* filename)
{
uint8_t* data = formatToUint8(pixels);
ImageType type = getFileType(filename);
bool isWriteSuccessfull = 0;
switch (type) //calls right 'write' function
{
case PNG:
isWriteSuccessfull = stbi_write_png(filename, w, h, channels, data, w * channels);
break;
case JPG:
isWriteSuccessfull = stbi_write_jpg(filename, w, h, channels, data, 100);
break;
case BMP:
isWriteSuccessfull = stbi_write_bmp(filename, w, h, channels, data);
break;
}
printOperationStatus(isWriteSuccessfull, filename, "write");
stbi_image_free(data);
}
void Image::printOperationStatus(bool isOperationSuccessfull, const char* filename, string operationName)
{
if (isOperationSuccessfull)
{
string FORorTO = operationName == "read" ? "from" : "to";
operationName[0] = toupper(operationName[0]);
cout << operationName << " " << FORorTO << " " << filename << " successfull" << endl;
}
else
{
cout << "Failed to " << operationName<< " " << filename << endl;
exit(0);
}
}
//FILTERS
void Image::grayscaleAvg()
{
if (!containsAtLeastThreeChannels()) return;
for (auto& p : pixels)
{
// (R+G+B)/3 = total/channels = avg
uint8_t grayscaledPixel = (p.R + p.G + p.B) / 3;
p.R = p.G = p.B = grayscaledPixel;
}
}
void Image::grayscaleLum()
{
if (!containsAtLeastThreeChannels()) return;
for (auto& p : pixels)
{
//formula for RGB value from wiki (human eye perception)
int gray = (p.R * 0.2126 + p.G * 0.7152 + p.B * 0.0722) / 3;
p.R = p.G = p.B = gray;
}
}
void Image::colorMask(float R, float G, float B)
{
if (!containsAtLeastThreeChannels()) return;
for (auto& p : pixels)
{
p.R *= R;
p.G *= G;
p.B *= B;
}
}
bool Image::containsAtLeastThreeChannels()
{
if (channels < 3)
{
cout << "To use that function, your picture has to have at least 3 channels" << endl;
return false;
}
else
return true;
}
//CHANGING POSITIONS
void Image::flipX()
{
for (int y = 0; y < h; ++y)
for (int x = 0; x < w/2; ++x)
swap(pixels[x + y * w], pixels[(w - 1 - x) + y * w]);
}
void Image::flipY()
{
for (int y = 0; y < h / 2; ++y)
for (int x = 0; x < w; ++x)
swap(pixels[x + y * w], pixels[x + (h - y - 1) * w]);
}
void Image::flipRight()
{
vector <Image::Pixel> pixelsNew;
pixelsNew.resize(size());
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
{
int index1 = (w - x - 1) + w * y;
int index2 = (h - y - 1) + (w - x - 1) * h;
pixelsNew[index2] = pixels[index1];
}
swap(w, h);
pixels = pixelsNew;
}
void Image::reduce(float sizeW, float sizeH)
{
if (!validateSize(sizeW, sizeH))
return;
deducePixelsOrRatio(sizeW, sizeH);
float horizontalRatio = sizeW / (float)w;
float verticalRatio = sizeH / (float)h;
sizeW = floor(sizeW);
sizeH = floor(sizeH);
vector <Image::Pixel> pixelsS(sizeH * sizeW);
// The program copies a pixel whenever one of these values is at least 1 //(gap to the next pixel)
float toNextHorizontal = 1;
float toNextVertical = 1;
// Iterate (from the top to the bottom)
for (int x = 0; x < w; x++)
{
int currentLine = x * horizontalRatio;
if (isBiggerThanDstSize(currentLine, sizeW))
break;
if (!isThePixelToCopy(toNextHorizontal, horizontalRatio))
continue;
for (int y = 0; y < h; y++)
{
int currentColumn = y * verticalRatio;
if (isBiggerThanDstSize(currentColumn, sizeH))
break;
if (!isThePixelToCopy(toNextVertical, verticalRatio))
continue;
pixelsS[(int)(currentLine) + (int)(currentColumn) * sizeW] = pixels[x + y * w];
toNextVertical += verticalRatio - 1.0f;
}
toNextVertical = 1.0f;
toNextHorizontal += horizontalRatio - 1.0f;
}
h = sizeH;
w = sizeW;
pixels = pixelsS;
}
bool Image::validateSize(float sizeW, float sizeH)
{
if (sizeW <= 0 || sizeH <= 0)
{
cout << "Image can't have negative size" << endl;
return false;
}
if (sizeW > w || sizeH > h)
{
cout << "Can't reduce image when target width or/and height are bigger than source" << endl;
return false;
}
return true;
}
void Image::deducePixelsOrRatio(float& dstW, float& dstH)
{
if (dstW <= 1) dstW *= w;
if (dstH <= 1) dstH *= h;
}
bool Image::isBiggerThanDstSize(int current, int size)
{
return current >= size;
}
bool Image::isThePixelToCopy(float& toNext, float ratio)
{
if (toNext < 1.0f)
{
toNext += ratio;
return false;
}
return true;
}
//ASCII
template <typename Out>
void Image::print(Out &out)
{
//copy to prevent turning orginal into grayscale
Image THIS = *this;
THIS.grayscaleAvg();
const string ascii_density = " .:-=+*#%@";
for (int i = 0; i < pixels.size(); i++)
{
if (i % w == 0) out << endl;
int index = map_value(THIS.pixels[i].R, 0, 255, 0, ascii_density.size() - 1);
char brightness = ascii_density[index];
out << brightness;
}
}
void Image::printAscii()
{
print(cout);
}
void Image::printAscii(string resultTextFilename)
{
ofstream file;
file.open(resultTextFilename);
print(file);
file.close();
}
int Image::map_value (int value, int MinBaseRange, int MaxBaseRange, int MinDstRange, int MaxDstRange) {
return (value - MinBaseRange) * (MaxDstRange - MinDstRange) / (MaxBaseRange - MinBaseRange) + MinDstRange;
};
//HELP FUNCTIONS
Image::ImageType Image::getFileType(const char* filename)
{
string filename_s(filename, strlen(filename));
string ext = filename_s.substr(filename_s.find('.'));
if (filename_s.find('.') == string::npos) return PNG; //default PNG
if (ext == ".png") return PNG;
if (ext == ".jpg") return JPG;
if (ext == ".bmp") return BMP;
}
vector <Image::Pixel> Image::formatToPixels(uint8_t* data)
{
vector <Pixel> pixelsNew;
pixelsNew.reserve(size());
for (int i = 0; i < size() * channels; i += channels)
{
uint8_t R = data[i], G = data[i + 1], B = data[i + 2];
Pixel pixel(R, B, G);
pixelsNew.push_back(pixel);
}
return pixelsNew;
}
uint8_t* Image::formatToUint8(vector <Pixel>& pixels)
{
uint8_t* data = new uint8_t[pixels.size() * channels];
for (int i = 0; i < size() * channels; i += channels)
{
data[i] = pixels[i / channels].R;
data[i + 1] = pixels[i / channels].G;
data[i + 2] = pixels[i / channels].B;
}
return data;
}
size_t Image::size() { return w * h; }
int Image::getW() { return w; }
int Image::getH() { return h; }