-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImgProc.hpp
More file actions
679 lines (553 loc) · 21 KB
/
ImgProc.hpp
File metadata and controls
679 lines (553 loc) · 21 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#ifndef IMGPROC_HPP
#define IMGPROC_HPP
#include <vector>
#include <fstream>
#include <cstdint>
#include <string>
#include <initializer_list>
#include <array>
#include <algorithm>
#include <cstddef>
constexpr std::size_t COLOR_TABLE_SIZE = 256;
// Follow the naming convention of wingdi.h
#pragma pack(push, 1)
struct RGBTRIPLE {
uint8_t rgbtBlue;
uint8_t rgbtGreen;
uint8_t rgbtRed;
};
struct BITMAPFILEHEADER {
uint16_t bfType;
uint32_t bfSize;
uint16_t bfReserved1;
uint16_t bfReserved2;
uint32_t bfOffBits;
};
struct BITMAPINFOHEADER {
uint32_t biSize;
int32_t biWidth;
int32_t biHeight;
uint16_t biPlanes;
uint16_t biBitCount;
uint32_t biCompression;
uint32_t biSizeRGBImage;
int32_t biXPelsPerMeter;
int32_t biYPelsPerMeter;
uint32_t biClrUsed;
uint32_t biClrImportant;
};
#pragma pack(pop)
enum Compression {
BI_RGB,
BI_RLE8,
BI_RLE4,
BI_BITFIELDS,
BI_JPEG,
BI_PNG,
};
enum class ColorSpace {
RGB,
HSI,
YCC,
};
enum class Channel {
BLUE,
GREEN,
RED,
};
struct Matrix : std::vector<std::vector<double>>
{
int width, height;
Matrix(int height, int width) noexcept;
Matrix(int height, int width, double value) noexcept;
Matrix(std::initializer_list<std::initializer_list<double>> list);
Matrix operator-() const noexcept; // Component-wise negation
Matrix operator+(const Matrix& other) const;
Matrix operator-(const Matrix& other) const;
Matrix operator*(const Matrix& other) const;
Matrix operator+(double scalar) const noexcept;
Matrix operator-(double scalar) const noexcept;
Matrix operator*(double scalar) const noexcept;
friend Matrix operator+(double scalar, const Matrix& matrix) noexcept;
friend Matrix operator-(double scalar, const Matrix& matrix) noexcept;
friend Matrix operator*(double scalar, const Matrix& matrix) noexcept;
Matrix& operator+= (const Matrix& other);
Matrix& operator-= (const Matrix& other);
Matrix& operator*= (const Matrix& other);
Matrix& operator+= (double scalar) noexcept;
Matrix& operator-= (double scalar) noexcept;
Matrix& operator*= (double scalar) noexcept;
bool operator==(const Matrix& other) const noexcept;
bool operator!=(const Matrix& other) const noexcept;
Matrix transpose() const noexcept;
Matrix T() const noexcept;
double dot(const Matrix& other) const;
Matrix submatrix(int y, int x, int h, int w) const;
};
struct GrayImage : std::vector<std::vector<uint8_t>>
{
int width, height;
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
GrayImage() noexcept : width(0), height(0) {}
GrayImage(int height, int width) noexcept;
GrayImage& toFile(const std::string& filename);
Matrix toMatrix() const noexcept;
static GrayImage fromMatrix(const Matrix& matrix) noexcept;
};
struct RGBImage : std::vector<std::vector<RGBTRIPLE>>
{
int width, height;
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
RGBImage() noexcept : width(0), height(0) {}
RGBImage(int height, int width) noexcept;
GrayImage getChannel(const Channel& channel) const;
static RGBImage fromFile(const std::string& filename);
RGBImage& toFile(const std::string& filename);
GrayImage toGray(const ColorSpace& method);
static RGBImage fromGrays(const GrayImage& bChannel, const GrayImage& gChannel, const GrayImage& rChannel) noexcept;
};
///////////////////////////////////////
// Matrix implementation //
///////////////////////////////////////
Matrix::Matrix(int height, int width) noexcept : std::vector<std::vector<double>>(height, std::vector<double>(width)), height(height), width(width) {}
Matrix::Matrix(std::initializer_list<std::initializer_list<double>> list) : std::vector<std::vector<double>>(list.size(), std::vector<double>(list.begin()->size())), width(list.begin()->size()), height(list.size()) {
std::copy(list.begin(), list.end(), begin());
}
Matrix::Matrix(int height, int width, double value) noexcept : std::vector<std::vector<double>>(height, std::vector<double>(width, value)), height(height), width(width) {}
Matrix Matrix::operator*(const Matrix& other) const {
if (width != other.height)
throw std::runtime_error("Cannot multiply matrices with incompatible shapes! Got " + std::to_string(width) + "x" + std::to_string(height) + " and " + std::to_string(other.width) + "x" + std::to_string(other.height));
Matrix result(height, other.width);
for (int y = 0; y < height; y++) {
for (int x = 0; x < other.width; x++) {
double sum = 0;
for (int i = 0; i < width; i++) {
sum += (*this)[y][i] * other[i][x];
}
result[y][x] = sum;
}
}
return result;
}
Matrix Matrix::operator*(double scalar) const noexcept {
Matrix result(height, width);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
result[y][x] = (*this)[y][x] * scalar;
}
}
return result;
}
Matrix operator*(double scalar, const Matrix& matrix) noexcept {
return matrix * scalar;
}
Matrix Matrix::operator+(const Matrix& other) const {
if (width != other.width || height != other.height)
throw std::runtime_error("Cannot add matrices with different shapes! Got " + std::to_string(width) + "x" + std::to_string(height) + " and " + std::to_string(other.width) + "x" + std::to_string(other.height));
Matrix result(height, width);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
result[y][x] = (*this)[y][x] + other[y][x];
}
}
return result;
}
Matrix Matrix::operator+(double scalar) const noexcept {
Matrix result(height, width);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
result[y][x] = (*this)[y][x] + scalar;
}
}
return result;
}
Matrix operator+(double scalar, const Matrix& matrix) noexcept {
return matrix + scalar;
}
Matrix Matrix::operator-(const Matrix& other) const {
if (width != other.width || height != other.height)
throw std::runtime_error("Cannot subtract matrices with different shapes! Got " + std::to_string(width) + "x" + std::to_string(height) + " and " + std::to_string(other.width) + "x" + std::to_string(other.height));
Matrix result(height, width);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
result[y][x] = (*this)[y][x] - other[y][x];
}
}
return result;
}
Matrix Matrix::operator-() const noexcept {
Matrix result(height, width);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
result[y][x] = -(*this)[y][x];
}
}
return result;
}
Matrix Matrix::operator-(double scalar) const noexcept {
return *this + (-scalar);
}
Matrix operator-(double scalar, const Matrix& matrix) noexcept {
return scalar + (-matrix);
}
Matrix& Matrix::operator*=(const Matrix& other) {
return *this = *this * other;
}
Matrix& Matrix::operator*=(double scalar) noexcept {
return *this = *this * scalar;
}
Matrix& Matrix::operator+=(const Matrix& other) {
return *this = *this + other;
}
Matrix& Matrix::operator+=(double scalar) noexcept {
return *this = *this + scalar;
}
Matrix& Matrix::operator-=(const Matrix& other) {
return *this = *this - other;
}
Matrix& Matrix::operator-=(double scalar) noexcept {
return *this = *this - scalar;
}
bool Matrix::operator==(const Matrix& other) const noexcept {
if (width != other.width || height != other.height) return false;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if ((*this)[y][x] != other[y][x]) return false;
}
}
return true;
}
bool Matrix::operator!=(const Matrix& other) const noexcept {
return !(*this == other);
}
Matrix Matrix::transpose() const noexcept {
Matrix result(width, height);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
result[x][y] = (*this)[y][x];
}
}
return result;
}
Matrix Matrix::T() const noexcept {
return transpose();
}
// Frobenius inner product
double Matrix::dot(const Matrix& other) const {
if (width != other.width || height != other.height)
throw std::runtime_error("Cannot calculate dot product of matrices with different shapes! Got " + std::to_string(width) + "x" + std::to_string(height) + " and " + std::to_string(other.width) + "x" + std::to_string(other.height));
double sum = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
sum += (*this)[y][x] * other[y][x];
}
}
return sum;
}
Matrix Matrix::submatrix(int y, int x, int h, int w) const {
if (y + h > height || x + w > width)
throw std::runtime_error("Submatrix out of bounds! Matrix size is " + std::to_string(width) + "x" + std::to_string(height) + ", requested submatrix size is " + std::to_string(w) + "x" + std::to_string(h) + " at position " + std::to_string(x) + "," + std::to_string(y));
if (y < 0 || x < 0 || h < 0 || w < 0)
throw std::runtime_error("Submatrix size and position must be positive! Got " + std::to_string(w) + "x" + std::to_string(h) + " at position " + std::to_string(x) + "," + std::to_string(y));
Matrix result(h, w);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
result[i][j] = (*this)[y + i][x + j];
}
}
return result;
}
////////////////////////////////////////
// GrayImage implementation //
////////////////////////////////////////
GrayImage::GrayImage(int height, int width) noexcept : std::vector<std::vector<uint8_t>>(height, std::vector<uint8_t>(width)), height(height), width(width)
{
// Initializer list
fileHeader = {
0x4D42,
static_cast<uint32_t>(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + COLOR_TABLE_SIZE * 4 + height * ((width + 3) & ~3)),
0, 0,
static_cast<uint32_t>(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + COLOR_TABLE_SIZE * 4),
};
infoHeader = {
sizeof(BITMAPINFOHEADER),
width,
height,
1,
8,
BI_RGB,
0, 0, 0,
COLOR_TABLE_SIZE,
0,
};
}
GrayImage& GrayImage::toFile(const std::string& filename) {
std::ofstream file(filename, std::ios::binary);
if (!file) throw std::runtime_error("Failed to open the file: " + filename);
file.write(reinterpret_cast<char*>(&fileHeader), sizeof(BITMAPFILEHEADER));
file.write(reinterpret_cast<char*>(&infoHeader), sizeof(BITMAPINFOHEADER));
// Color table(Mandatory for color depths ≤ 8 bits)
std::array<uint8_t, COLOR_TABLE_SIZE * 4> colorTable;
for (uint16_t i = 0; i < COLOR_TABLE_SIZE; i++) {
uint8_t* color = colorTable.data() + i * 4;
color[0] = color[1] = color[2] = i; // BGR channels
color[3] = 0; // Alpha channel
}
file.write(reinterpret_cast<char*>(colorTable.data()), colorTable.size() * sizeof(uint8_t));
int padding = (4 - (width % 4)) % 4;
// BMP files are stored bottom-up
for (int y = height - 1; y >= 0; --y) {
file.write(reinterpret_cast<char*>(this->at(y).data()), width);
file.write("\0\0\0", padding);
}
file.close();
return *this;
}
Matrix GrayImage::toMatrix() const noexcept {
Matrix matrix(height, width);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
matrix[y][x] = (*this)[y][x];
}
}
return matrix;
}
GrayImage GrayImage::fromMatrix(const Matrix& matrix) noexcept {
GrayImage image(matrix.height, matrix.width);
for (int y = 0; y < matrix.height; y++) {
for (int x = 0; x < matrix.width; x++) {
image[y][x] = static_cast<uint8_t>(matrix[y][x]);
}
}
return image;
}
RGBImage::RGBImage(int height, int width) noexcept : std::vector<std::vector<RGBTRIPLE>>(height, std::vector<RGBTRIPLE>(width)), height(height), width(width)
{
int paddingSize = (4 - (width * 3 % 4)) % 4;
fileHeader = {
0x4D42,
static_cast<uint32_t>(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + height * width * 3 + height * paddingSize),
0, 0,
static_cast<uint32_t>(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)),
};
infoHeader = {
sizeof(BITMAPINFOHEADER),
width,
height,
1,
24,
BI_RGB,
0, 0, 0, 0,
};
}
GrayImage RGBImage::getChannel(const Channel& channel) const {
GrayImage image(height, width);
switch (channel) {
case Channel::BLUE:
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
image[y][x] = (*this)[y][x].rgbtBlue;
}
}
break;
case Channel::GREEN:
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
image[y][x] = (*this)[y][x].rgbtGreen;
}
}
break;
case Channel::RED:
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
image[y][x] = (*this)[y][x].rgbtRed;
}
}
break;
default:
throw std::runtime_error("Unknown channel!");
}
return image;
}
RGBImage RGBImage::fromFile(const std::string& filename) {
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
std::ifstream file(filename, std::ios::binary);
if (!file) throw std::runtime_error("Failed to open the file: " + filename);
file.read(reinterpret_cast<char*>(&fileHeader), sizeof(BITMAPFILEHEADER));
file.read(reinterpret_cast<char*>(&infoHeader), sizeof(BITMAPINFOHEADER));
if(infoHeader.biCompression != BI_RGB) throw std::runtime_error("Only uncompressed BMP files are supported!");
int width = infoHeader.biWidth;
int height = infoHeader.biHeight;
int paddingSize = (4 - (width * 3 % 4)) % 4;
RGBImage image(height, width);
file.seekg(fileHeader.bfOffBits, std::ios::beg);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
RGBTRIPLE pixel;
file.read(reinterpret_cast<char*>(&pixel), sizeof(RGBTRIPLE));
// BMP files are stored bottom-up
image[height - y - 1][x] = pixel;
}
file.seekg(paddingSize, std::ios::cur);
}
file.close();
return image;
}
RGBImage& RGBImage::toFile(const std::string& filename) {
int paddingSize = (4 - (width * 3 % 4)) % 4;
std::ofstream file(filename, std::ios::binary);
if (!file) throw std::runtime_error("Failed to open the file: " + filename);
file.write(reinterpret_cast<char*>(&fileHeader), sizeof(BITMAPFILEHEADER));
file.write(reinterpret_cast<char*>(&infoHeader), sizeof(BITMAPINFOHEADER));
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// BMP files are stored bottom-up
RGBTRIPLE pixel = (*this)[height - y - 1][x];
file.write(reinterpret_cast<char*>(&pixel), sizeof(RGBTRIPLE));
}
file.seekp(paddingSize, std::ios::cur);
}
file.close();
return *this;
}
GrayImage RGBImage::toGray(const ColorSpace& method = ColorSpace::HSI) {
GrayImage grayImage(height, width);
switch (method) {
case ColorSpace::HSI:
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
RGBTRIPLE pixel = (*this)[y][x];
uint8_t gray = (pixel.rgbtRed + pixel.rgbtGreen + pixel.rgbtBlue) / 3;
grayImage[y][x] = gray;
}
}
break;
case ColorSpace::YCC:
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
RGBTRIPLE pixel = (*this)[y][x];
uint8_t gray = static_cast<uint8_t>(0.299 * pixel.rgbtRed + 0.587 * pixel.rgbtGreen + 0.114 * pixel.rgbtBlue);
grayImage[y][x] = gray;
}
}
break;
default:
throw std::runtime_error("Unknown method! Supported methods are HSI and YCC.");
}
return grayImage;
}
RGBImage RGBImage::fromGrays(const GrayImage& bChannel, const GrayImage& gChannel, const GrayImage& rChannel) noexcept {
RGBImage image(bChannel.height, bChannel.width);
for (int y = 0; y < bChannel.height; y++) {
for (int x = 0; x < bChannel.width; x++) {
image[y][x] = { bChannel[y][x], gChannel[y][x], rChannel[y][x] };
}
}
return image;
}
////////////////////////////////////////
// OutlineRenderer implementation //
////////////////////////////////////////
enum class ShapeType {
NONE,
CIRCLE,
RECTANGLE,
};
class OutlineRenderer {
private:
int y, x;
int radius;
int height, width;
RGBTRIPLE color;
int thickness;
RGBImage image;
ShapeType shape;
// Render the hollow rectangle
// x, y is the top-left corner
RGBImage renderRectangle() {
if (height <= 0 || width <= 0) throw std::runtime_error("Rectangle dimensions must greater than 0!");
if (thickness <= 0) throw std::runtime_error("Thickness must be greater than 0!");
if (thickness >= height || thickness >= width) throw std::runtime_error("Thickness must be less than the height and width!");
if (this->y + height > image.height || this->x + width > image.width) throw std::runtime_error("Rectangle out of bounds!");
for (int y = this->y; y < this->y + height; y++) {
for (int x = this->x; x < this->x + width; x++) {
if (y - this->y < thickness || x - this->x < thickness || (this->y + height - 1 - y) < thickness || (this->x + width - 1 - x) < thickness) {
image[y][x] = color;
}
}
}
return image;
}
// Render the hollow circle
// x, y is the center
RGBImage renderCircle() {
for (int y = this->y - radius; y <= this->y + radius; y++) {
for (int x = this->x - radius; x <= this->x + radius; x++) {
if (y < 0 || y >= image.height || x < 0 || x >= image.width) continue;
if ((y - this->y) * (y - this->y) + (x - this->x) * (x - this->x) <= radius * radius) {
image[y][x] = color;
}
}
}
return image;
}
public:
OutlineRenderer() noexcept : y(0), x(0), color({ 0, 0, 0 }), thickness(1), shape(ShapeType::NONE) {}
OutlineRenderer(const RGBImage& image) noexcept : y(0), x(0), color({ 0, 0, 0 }), thickness(1), image(image) {}
OutlineRenderer(const GrayImage& image) noexcept : y(0), x(0), color({ 0, 0, 0 }), thickness(1) {
this->setImage(image);
}
OutlineRenderer& setPos(int y, int x) noexcept {
this->y = y;
this->x = x;
return *this;
}
OutlineRenderer& setColor(const RGBTRIPLE& color) noexcept {
this->color = color;
return *this;
}
OutlineRenderer& setThickness(int thickness) noexcept {
this->thickness = thickness;
return *this;
}
OutlineRenderer& setImage(const RGBImage& image) noexcept {
this->image = image;
return *this;
}
OutlineRenderer& setImage(const GrayImage& image) noexcept {
// for(int i = 0; i < image.height; i++) {
// for (int j = 0; j < image.width; j++) {
// this->image[i][j] = { image[i][j], image[i][j], image[i][j] };
// }
// }
// return *this;
return setImage(RGBImage::fromGrays(image, image, image));
}
OutlineRenderer& setShape(const ShapeType& shape) noexcept {
this->shape = shape;
return *this;
}
OutlineRenderer& setDimensions(int height, int width) noexcept {
this->height = height;
this->width = width;
return *this;
}
OutlineRenderer& setRadius(int radius) noexcept {
this->radius = radius;
return *this;
}
RGBImage render() {
switch (shape) {
case ShapeType::RECTANGLE:
return renderRectangle();
case ShapeType::CIRCLE:
return renderCircle();
default:
throw std::runtime_error("Unknown shape type!");
}
}
};
#endif