-
Notifications
You must be signed in to change notification settings - Fork 1
/
bmp_open.cpp
97 lines (77 loc) · 3.29 KB
/
bmp_open.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
86
87
88
89
90
91
92
93
94
95
96
97
#include "iobmp.h"
// Open Object Functions ==========================================================================
void bmpFile::openNew(int x, int y, int dpi, std::string file) {
// this function sets resolution variables for a new image object.
image_x = x;
dibheader.biWidth = image_x;
image_y = y;
dibheader.biHeight = image_y;
dpi_x = dpi * 39.3701;
dibheader.biXPelsPerMeter = dpi_x;
dpi_y = dpi * 39.3701;
dibheader.biYPelsPerMeter = dpi_y;
fileName = file;
return;
}
void bmpFile::openLoad(std::string file) {
// this function will load a image file to a new image object.
std::ifstream input(file, std::ios::in|std::ios::binary);
this->erasePixelArray();
this->eraseDataArray();
if (input.fail()) {
throw std::logic_error("Error: file cannot be opened.");
return;
}
//Reading BMP elements
input.read(reinterpret_cast<char*>(&header.bfType), sizeof(header.bfType));
input.read(reinterpret_cast<char*>(&header.bfSize), sizeof(header.bfSize));
input.read(reinterpret_cast<char*>(&header.bfReserved1), sizeof(header.bfReserved1));
input.read(reinterpret_cast<char*>(&header.bfReserved2), sizeof(header.bfReserved2));
input.read(reinterpret_cast<char*>(&header.bfOffBits), sizeof(header.bfOffBits));
input.read(reinterpret_cast<char*>(&dibheader.biSize), sizeof(dibheader.biSize));
input.read(reinterpret_cast<char*>(&dibheader.biWidth), sizeof(dibheader.biWidth));
input.read(reinterpret_cast<char*>(&dibheader.biHeight), sizeof(dibheader.biHeight));
input.read(reinterpret_cast<char*>(&dibheader.biPlanes), sizeof(dibheader.biPlanes));
input.read(reinterpret_cast<char*>(&dibheader.biBitCount), sizeof(dibheader.biBitCount));
input.read(reinterpret_cast<char*>(&dibheader.biCompression), sizeof(dibheader.biCompression));
input.read(reinterpret_cast<char*>(&dibheader.biSizeImage), sizeof(dibheader.biSizeImage));
input.read(reinterpret_cast<char*>(&dibheader.biXPelsPerMeter), sizeof(dibheader.biXPelsPerMeter));
input.read(reinterpret_cast<char*>(&dibheader.biYPelsPerMeter), sizeof(dibheader.biYPelsPerMeter));
input.read(reinterpret_cast<char*>(&dibheader.biClrUsed), sizeof(dibheader.biClrUsed));
input.read(reinterpret_cast<char*>(&dibheader.biClrImportant), sizeof(dibheader.biClrImportant));
image_x = dibheader.biWidth;
image_y = dibheader.biHeight;
dpi_x = static_cast<double>(dibheader.biXPelsPerMeter) / 39.3701;
dpi_y = static_cast<double>(dibheader.biYPelsPerMeter) / 39.3701;
fileName = file;
// Pixel Array Write
for(int y = 0; y <= image_y; y++) {
for(int x = 0; x < image_x; x++) {
RGBQUAD tempPixel;
input.read(reinterpret_cast<char*>(&tempPixel.rgbBlue), 1);
input.read(reinterpret_cast<char*>(&tempPixel.rgbGreen), 1);
input.read(reinterpret_cast<char*>(&tempPixel.rgbRed), 1);
pixelArray.push_back(tempPixel);
}
for(int n = 0; n < (image_x % 4); n++) { // to ensure multipe of 4 requirement of bmp data
int extra;
input.read(reinterpret_cast<char*>(&extra), 1);
}
}
// Conclusion
input.close();
return;
}
void bmpFile::changeFile(std::string newFile) {
// This function changes the pointed file
fileName = newFile;
return;
}
std::string bmpFile::fileNameIs() {
// This function returns the pointed file
return fileName;
}
void bmpFile::convertColorIndex(int temp, RGBQUAD &tempPixel) {
printf("I don't exist yet!");
return;
}