-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitmap.h
276 lines (214 loc) · 5.74 KB
/
bitmap.h
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
#ifndef BIT_FILE_MAP_H
#define BIT_FILE_MAP_H
#ifdef WINDOWS
#include <windows.h>
#include "file_map.h"
#include "file_writer.h"
#else
#include "u_file_map.h"
#include "u_file_writer.h"
#include "bmpDEF.h"
#endif
#include "rgb_color.h"
//per gli algoritmi di sort
#include <algorithm>
using namespace std;
class wrong_bpp { };
#pragma pack(1)
class bitmap_header: public BITMAPFILEHEADER, public BITMAPINFOHEADER
{
public:
inline DWORD height()
{
return (biHeight);
}
inline DWORD width()
{
return (biWidth);
}
inline int get_bpp()
{
return (biBitCount);
}
inline int get_palette_size()
{
if (biBitCount >= 16)
return 0;
return (biClrUsed);
}
inline int get_struct_size()
{
return (biSize + sizeof(BITMAPFILEHEADER));
}
inline int get_header_size()
{
return (bfOffBits);
}
inline DWORD get_image_size()
{
return (biHeight * biWidth * biBitCount / 8);
}
inline DWORD get_file_size()
{
return (bfSize);
}
inline DWORD get_compresion_flag()
{
return (biCompression);
}
inline void set_compression_flag(DWORD dwFlag)
{
biCompression = dwFlag;
}
};
#pragma pack()
class bitmap8bpp: private file_map<BYTE>
{
private:
/////////////////////////// Dichiarazione classi ausiliarie ////////////////////////////////
struct indexed_rgb
{
rgb_color32 rgbValue;
BYTE iValue;
};
class compare_indexed_rgb
{
public:
int operator() (const indexed_rgb& a, const indexed_rgb& b)
{
return (a.rgbValue.to_dword() < b.rgbValue.to_dword());
}
};
//////////////////////////////// Variabili private ////////////////////////////////////////
bitmap_header* m_pBitmapHeader;
rgb_color32* m_aRgb;
BYTE* m_aColorIndex;
indexed_rgb m_IndexedPalette[256];
int m_IndexToGrey[256];
////////////////////////////////// Metodi privati ////////////////////////////////////////
void translate_simil_grey()
{
//create the rgb_color32 - index array
for (int i = 0; i < m_pBitmapHeader->get_palette_size(); ++i)
{
m_IndexedPalette[i].rgbValue = m_aRgb[i];
m_IndexedPalette[i].iValue = i;
}
//ordina con shell-sort
stable_sort(m_IndexedPalette,
m_IndexedPalette + m_pBitmapHeader->get_palette_size(),
compare_indexed_rgb());
for(int j = 0; j < m_pBitmapHeader->get_palette_size(); ++j)
{
m_IndexToGrey[m_IndexedPalette[j].iValue] = j;
}
}
//////////////////////////////// Disbilitazione copia ////////////////////////////////////
bitmap8bpp(const bitmap8bpp&);
bitmap8bpp& operator=(const bitmap8bpp&);
public:
bitmap8bpp(char* szFileName, DWORD dwFileSize = 0)
{
if (!this->create(szFileName, dwFileSize))
throw new bad_file();
m_pBitmapHeader = (bitmap_header*)m_FileMap;
if (m_pBitmapHeader->get_bpp() != 8)
throw new wrong_bpp();
m_aRgb = (rgb_color32*)(m_FileMap + m_pBitmapHeader->get_struct_size());
m_aColorIndex = (m_FileMap + m_pBitmapHeader->get_header_size());
translate_simil_grey();
}
inline DWORD height()
{
return(m_pBitmapHeader->height());
}
inline DWORD width()
{
return(m_pBitmapHeader->width());
}
inline BYTE get_greyed_pixel(DWORD x, DWORD y)
{
if ( (x > (height() - 1)) || (y > (width() - 1)) )
return 0; //pixel fuori immagine
return (m_IndexToGrey[m_aColorIndex[x * width() + y]]);
}
inline void set_greyed_pixel(DWORD x, DWORD y, BYTE pixel_value)
{
ASSERT((x <= (height() - 1)) && (y <= (width() - 1)));
if ((x <= (height() - 1)) && (y <= (width() - 1)))
m_aColorIndex[x * width() + y] = m_IndexedPalette[pixel_value].iValue;
}
inline rgb_color32 get_pixel(DWORD x, DWORD y)
{
if ( (x > (height() - 1)) || (y > (width() - 1)) )
{
//pixel fuori immagine
rgb_color32 ret;
return (ret);
}
return (m_aRgb[m_aColorIndex[x * width() + y]]);
}
inline rgb_color32* get_palette()
{
return m_aRgb;
}
inline bitmap_header* get_header()
{
return m_pBitmapHeader;
}
inline int get_palette_size()
{
return (m_pBitmapHeader->get_palette_size());
}
};
template<class RGB_TYPE>
class bitmap_true: private file_map<BYTE>
{
private:
bitmap_header* m_pBitmapHeader;
RGB_TYPE* m_aColorIndex;
///////////////////////////// Disabilitazione copia //////////////////////////////
bitmap_true(const bitmap_true<RGB_TYPE>&);
bitmap_true<RGB_TYPE>& operator=(const bitmap_true<RGB_TYPE>&);
public:
bitmap_true(char* szFileName, DWORD dwFileSize = 0)
{
if (!this->create(szFileName, dwFileSize))
throw new bad_file();
m_pBitmapHeader = (bitmap_header*)m_FileMap;
if (m_pBitmapHeader->get_bpp() != (sizeof(RGB_TYPE) * 8))
throw new wrong_bpp();
if (m_pBitmapHeader->bfType != 0x4d42) // 0x42 = "B" 0x4d = "M"
throw new bad_file();
m_aColorIndex = (RGB_TYPE*)(m_FileMap + m_pBitmapHeader->get_header_size());
}
inline DWORD height()
{
return(m_pBitmapHeader->height());
}
inline DWORD width()
{
return(m_pBitmapHeader->width());
}
inline RGB_TYPE get_pixel(DWORD x, DWORD y)
{
if ( (x > (height() - 1)) || (y > (width() - 1)) )
{
//pixel fuori immagine
RGB_TYPE ret;
return (ret);
}
return (m_aColorIndex[x * width() + y]);
}
inline void set_pixel(DWORD x, DWORD y, RGB_TYPE rgbColor)
{
ASSERT ((x <= (height() - 1)) && (y <= (width() - 1)));
if ( (x <= (height() - 1)) && (y <= (width() - 1)) )
m_aColorIndex[x * width() + y] = rgbColor;
}
inline bitmap_header* get_header()
{
return m_pBitmapHeader;
}
};
#endif //BIT_FILE_MAP_H