-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmap.cpp
More file actions
188 lines (146 loc) · 4.64 KB
/
bitmap.cpp
File metadata and controls
188 lines (146 loc) · 4.64 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
#include <bits/stdc++.h>
#include <GL/glut.h>
#include <stdio.h>
using namespace std;
typedef struct {
unsigned short type;
unsigned int size;
unsigned short reserved1;
unsigned short reserved2;
unsigned int offset;
} BMPHeader;
typedef struct {
unsigned int size;
int width;
int height;
unsigned short planes;
unsigned short bpp;
unsigned int compression;
unsigned int imagesize;
int xresolution;
int yresolution;
unsigned int ncolors;
unsigned int importantcolors;
} BMPInfoHeader;
void loadBMP(const char* filename, int at_window) {
FILE* file = fopen(filename, "rb");
if (!file) {
printf("Error: Unable to open BMP file.\n");
return;
}
BMPHeader header;
fread(&header.type, sizeof(unsigned short), 1, file);
if (header.type != 0x4D42) {
printf("Error: Invalid BMP file.\n");
fclose(file);
return;
}
fread(&header.size, sizeof(unsigned int), 1, file);
fread(&header.reserved1, sizeof(unsigned short), 1, file);
fread(&header.reserved2, sizeof(unsigned short), 1, file);
fread(&header.offset, sizeof(unsigned int), 1, file);
BMPInfoHeader infoHeader;
fread(&infoHeader, sizeof(BMPInfoHeader), 1, file);
unsigned char* data = new unsigned char[infoHeader.imagesize];
fseek(file, header.offset, SEEK_SET);
fread(data, sizeof(unsigned char), infoHeader.imagesize, file);
fclose(file);
for(int i = 0; i < infoHeader.imagesize; i += 3) {
if(at_window == 2) {
data[i] = 255 - data[i];
data[i + 1] = 255 - data[i];
data[i + 2] = 255 - data[i];
}
else if(at_window >= 3) {
double B = data[i];
double G = data[i + 1];
double R = data[i + 2];
// double _R = R, _G = G, _B = B;
double _R = R / 255;
double _G = G / 255;
double _B = B / 255;
double Cmax = max({_R, _G, _B});
double Cmin = min({_R, _G, _B});
double del = Cmax - Cmin;
double H, S, V;
if(at_window == 3) {
if(Cmax == _R) {
H = ((_G - _B) / del) * 60;
H += 120;
}
else {
H = (_R - _G) / del;
H *= 60;
H += 240;
}
if(H < 0) H += 360;
H /= 360;
H *= 240;
data[i] = data[i + 1] = data[i + 2] = H;
}
if(at_window == 4) {
if(Cmax == 0) {
S = 0;
} else {
S = del / Cmax;
}
S *= 240;
data[i] = data[i + 1] = data[i + 2] = S;
}
if(at_window == 5) {
V = Cmax * 240;
data[i] = data[i + 1] = data[i + 2] = V;
}
}
}
glEnable(GL_TEXTURE_2D);
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, infoHeader.width, infoHeader.height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0);
glTexCoord2f(0.0, 1.0); glVertex2f(-1.0, 1.0);
glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0);
glTexCoord2f(1.0, 0.0); glVertex2f(1.0, -1.0);
glEnd();
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
// glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Window 1, Original BMP
glutInitWindowSize(800, 600);
glutCreateWindow("BMP Image");
glutDisplayFunc(display);
loadBMP("Input.bmp", 1);
// glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Window 2, Negative Image
glutInitWindowSize(800, 600);
glutCreateWindow("Negative Image");
glutDisplayFunc(display);
loadBMP("Input.bmp", 2);
// Window 3, S
glutInitWindowSize(800, 600);
glutCreateWindow("H");
glutDisplayFunc(display);
loadBMP("Input.bmp", 3);
// Window 4, S
glutInitWindowSize(800, 600);
glutCreateWindow("S");
glutDisplayFunc(display);
loadBMP("Input.bmp", 4);
// Window 5, V
glutInitWindowSize(800, 600);
glutCreateWindow("V");
glutDisplayFunc(display);
loadBMP("Input.bmp", 5);
glutMainLoop();
return 0;
}