-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageRGB24.cpp
85 lines (80 loc) · 2.32 KB
/
ImageRGB24.cpp
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
#include "ImageRGB24.h"
#include <iostream>
#include "ImageBW.h"
namespace phoenix {
ImageRGB24::ImageRGB24(const char* filename) {
std::string fn = filename;
std::string ext = fn.substr(fn.find_last_of("."));
FIBITMAP *dib;
if (ext == ".png") {
dib = FreeImage_Load(FIF_PNG, filename, PNG_DEFAULT);
} else {
throw;
}
if (dib) {
//std::cout << "Image<RGB<unsigned char>> construct" << std::endl;
unsigned int width = FreeImage_GetWidth(dib);
unsigned int height = FreeImage_GetHeight(dib);
InitWidth(width);
InitHeight(height);
AllocData();
for (int y = 0; y < this->Height(); y++) {
for (int x = 0; x < this->Width(); x++) {
RGBQUAD color;
FreeImage_GetPixelColor(dib, x, y, &color);
RGB24 c(color.rgbRed, color.rgbGreen, color.rgbBlue);
SetPixel(x, y, c);
}
}
} else {
throw;
}
FreeImage_Unload(dib);
}
void ImageRGB24::SaveAs(const char* filename) const {
std::string fn = filename;
std::string ext = fn.substr(fn.find_last_of("."));
FIBITMAP *dib;
if (ext == ".png") {
dib = FreeImage_Allocate(Width(), Height(), 24);
if (!dib) {
throw;
}
} else {
throw;
}
for (int y = 0; y < Height(); y++) {
for (int x = 0; x < Width(); x++) {
RGBQUAD color;
RGB24* colorXY = (RGB24*)(data + y * Width() + x);
color.rgbRed = colorXY->r;
color.rgbGreen = colorXY->g;
color.rgbBlue = colorXY->b;
FreeImage_SetPixelColor(dib, x, y, &color);
}
}
BOOL isSuccess = FreeImage_Save(FIF_PNG, dib, filename);
if (!isSuccess) {
throw;
}
FreeImage_Unload(dib);
}
ImageBW ImageRGB24::ToImageBW() {
ImageBW ret(Width(), Height());
for(int y = 0; y < Height(); y++) {
for(int x = 0; x < Width(); x++) {
RGB24 srcColor = GetPixel(x, y);
BW bwColor;
if(srcColor.r + srcColor.g + srcColor.b <= 190 * 3) {
bwColor = BW::Black;
} else {
bwColor = BW::White;
}
ret.SetPixel(x, y, bwColor);
}
}
return ret;
}
ImageRGB24::~ImageRGB24() {
}
} // namespace phoenix