Классы RGBpixel, RGBimage, BMPimage

  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
// imageLoader.cpp : Defines the entry point for the console application.
//
#include <windows.h>
#include <gl/GL.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
class RGBpixel {
public:
unsigned char r,g,b;
RGBpixel() {
r = g = b = 255;
}
RGBpixel(unsigned char r, unsigned char g, unsigned char b) {
this->r = r;
this->g = g;
this->b = b;
}
friend bool operator==(const RGBpixel &left, const RGBpixel &right) {
return left.r == right.r && left.g == right.g && left.b == right.b;
}
friend const RGBpixel operator+(const RGBpixel &left, const RGBpixel &right) {
int c[3] = { left.r + right.r, left.g + right.g, left.b + right.b };
for (int i = 0; i < 3; i++) {
if (c[i] > 255) c[i] = 255;
}
RGBpixel sum(c[0], c[1], c[2]);
return sum;
}
friend const RGBpixel operator-(const RGBpixel &left, const RGBpixel &right) {
int c[3] = { left.r - right.r, left.g - right.g, left.b - right.b };
for (int i = 0; i < 3; i++) {
if (c[i] < 0) c[i] = 0;
}
RGBpixel sum(c[0], c[1], c[2]);
return sum;
}
};
class RGBimage {
private:
public:
RGBpixel **map;
char *data;
int w;
int h;
RGBimage(int w, int h) {
this->w = w;
this->h = h;
map = new RGBpixel*[h];
for (int i = 0; i < h; i++) {
map[i] = new RGBpixel[w];
}
}
virtual ~RGBimage();
void fill_data();
void draw();
};
RGBimage::~RGBimage() {
for (int i = 0; i < h; i++) {
delete [] map[i];
}
delete [] map;
delete [] data;
}
void RGBimage::fill_data() {
int k = 0;
while ((w + k)%4) k++;
data = new char[h*(w + k)*3];
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int index = (i + j*(w + k))*3;
data[index + 0] = map[j][i].r;
data[index + 1] = map[j][i].g;
data[index + 2] = map[j][i].b;
}
}
}
void RGBimage::draw() {
fill_data();
glDrawPixels(w, h, GL_RGB, GL_UNSIGNED_BYTE, data);
//for (int i = 0; i < k; i++) data[h*(w + k)]
}
typedef unsigned short ushort;
typedef unsigned long ulong;
typedef unsigned short WORD;
typedef unsigned long DWORD;
#pragma pack(push, 1)
typedef struct bmpFileHeader
{
WORD bfType; // смещение 0 байт от начала файла
DWORD bfSize; // смещение 2 байта от начала файла, длина 4 байта
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits; // смещение 10 байт от начала файла, длина 4 байта
} BMPFILEHEADER;
typedef struct bmpInfoHeader
{
DWORD biSize;
int biWidth;
int biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
} BMPINFOHEADER;
#pragma pack(pop)
class BMPimage {
private:
void form_image();
public:
string name;
bmpFileHeader file_header;
bmpInfoHeader info_header;
RGBpixel *map;
char *char_map;
RGBimage *image;
BMPimage() { name = ""; }
virtual ~BMPimage();
BMPimage(string fname) {
loadBmp(fname);
}
void loadBmp(string fname);
};
BMPimage::~BMPimage() {
delete [] map;
delete [] char_map;
delete image;
}
void BMPimage::form_image() {
int w = info_header.biWidth;
int h = info_header.biHeight;
int k = 0;
while ((w + k)%4) k++;
image = new RGBimage(w, h);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int index = (i + j*(w + k))*3;
image->map[j][i].b = char_map[index];
image->map[j][i].g = char_map[index + 1];
image->map[j][i].r = char_map[index + 2];
}
}
}
void BMPimage::loadBmp(string fname) {
FILE *pFile;
pFile = fopen (fname.c_str(), "r");
if (pFile!=NULL)
{
fread((void*)&file_header, sizeof(bmpFileHeader), 1, pFile);
fread((void*)&info_header, sizeof(bmpInfoHeader), 1, pFile);
int w = info_header.biWidth;
int h = info_header.biHeight;
int offset = file_header.bfOffBits - sizeof(bmpFileHeader) - sizeof(bmpInfoHeader);
char dummy;
for (int i = 0; i < offset; i++) fread((void*)&dummy, sizeof(char), 1, pFile);
//printf("ptr = %d\n", ptr);
map = new RGBpixel[w*h];
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
fread((void*)&map[i+j*w], sizeof(RGBpixel), 1, pFile);
char t = map[i+j*w].r;
map[i+j*w].r = map[i+j*w].b, map[i+j*w].b = t;
for (int k = 0; k < (4 - (w*3)%4)%4; k++) fread((void*)&dummy, sizeof(char), 1, pFile);
}
}
fseek ( pFile , file_header.bfOffBits , SEEK_SET );
while (w % 4 != 0) w++;
char_map = new char[w*h*3];
//for (int i = 0; i < w*h*3; i++) {
fread((void*)char_map, sizeof(char), w*h*3+4, pFile);
//}
fclose (pFile);
form_image();
} else {
cout << "cannot open file for reading!" << endl;
}
}