HOG2
utils
BitMap.cpp
Go to the documentation of this file.
1
/*
2
* BitMap.cpp
3
* hog2
4
*
5
* Created by Nathan Sturtevant on 3/4/11.
6
* Copyright 2011 NS Software. All rights reserved.
7
*
8
*/
9
10
#include "
BitMap.h
"
11
#include <stdio.h>
12
#include <iostream>
13
14
BitMapPic::BitMapPic
(
int
w,
int
h):
width
(w),
height
(h)
15
{
16
image
=
new
uint8_t[
width
*
height
*4];
17
}
18
19
BitMapPic::BitMapPic
(
const
char
* file)
20
{
21
bool
loadFailed =
false
;
22
FILE *f = fopen(file,
"rb"
);
23
BitMapHeader
header
;
24
25
if
(f == 0)
26
{
27
std::cout <<
"Unable to find file\n"
;
28
loadFailed =
true
;
29
}
30
else
31
{
32
uint16_t bfType;
33
fread(&bfType,
sizeof
(bfType), 1, f);
34
if
(bfType != 19778)
35
{
36
std::cout <<
"Unable to load file properly\n"
;
37
fclose(f);
38
loadFailed =
true
;
39
}
40
else
41
{
42
fread(&
header
,
sizeof
(
BitMapHeader
), 1, f);
43
if
(
header
.biBitCount < 24)
44
{
45
std::cout <<
"We only support 24 & 32-bit files\n"
;
46
fclose(f);
47
loadFailed =
true
;
48
}
49
else
50
{
51
std::cout <<
":"
<<
header
.biBitCount <<
"bit image\n"
;
52
53
if
(
header
.biCompression != 0)
54
{
55
std::cout <<
"We don't support compressed files\n"
;
56
fclose(f);
57
loadFailed =
true
;
58
}
59
else
60
{
61
height
=
header
.biHeight;
62
bool
reverseHeight =
false
;
63
if
((int32_t)
header
.biHeight < 0)
64
{
65
std::cout <<
":height reversed\n"
;
66
height
= -
height
;
67
reverseHeight =
true
;
68
}
69
70
width
=
header
.biWidth;
71
image
=
new
uint8_t[
height
*
width
*4];
72
73
fseek(f,
header
.bfOffBits, SEEK_SET);
74
75
if
(
header
.biBitCount == 32)
76
{
77
for
(
size_t
x = 0; x <
height
; x++)
78
{
79
if
(reverseHeight)
80
fread(&
image
[(
height
-x-1)*
width
*4],
sizeof
(
char
),
width
*4, f);
81
else
82
fread(&
image
[x*
width
*4],
sizeof
(
char
),
width
*4, f);
83
}
84
}
85
else
if
(
header
.biBitCount == 24)
86
{
87
bool
padding =
false
;
88
for
(
int
x = 0; x <
height
; x++)
89
{
90
int
bytesRead = 0;
91
for
(
int
y = 0; y <
width
; y++)
92
{
93
if
(reverseHeight)
94
bytesRead += fread(&
image
[(
height
-x-1)*
width
*4+y*4],
sizeof
(
char
), 3, f);
95
else
96
bytesRead += fread(&
image
[x*
width
*4+y*4],
sizeof
(
char
), 3, f);
97
image
[x*
width
*4+y*4+3] = 0;
98
}
99
while
(0 != bytesRead%4)
100
{
101
char
zero[4] = {0, 0, 0, 0};
102
bytesRead += fread(zero,
sizeof
(
char
), 1, f);
103
padding =
true
;
104
}
105
}
106
if
(padding)
107
std::cout <<
":padding necessary\n"
;
108
}
109
fclose(f);
110
}
111
}
112
}
113
}
114
115
if
(loadFailed)
116
{
117
width
= 10;
118
height
= 10;
119
image
=
new
uint8_t[
width
*
height
*4];
120
for
(
int
i=0; i <
width
*
height
*4; i++)
121
{
122
image
[i] = 255;
123
}
124
}
125
126
127
}
128
129
BitMapPic::BitMapPic
(
const
BitMapPic
& b):
width
(b.
width
),
height
(b.
height
)
130
{
131
image
=
new
uint8_t[
width
*
height
*4];
132
133
for
(
int
i=0; i <
width
*
height
*4; i++)
134
{
135
image
[i] = b.
image
[i];
136
}
137
}
138
139
BitMapPic::~BitMapPic
()
140
{
141
width
= 0;
142
height
= 0;
143
if
(
image
)
144
{
145
delete
[]
image
;
146
image
= NULL;
147
}
148
}
149
150
BitMapPic
&
BitMapPic::operator=
(
const
BitMapPic
& b)
151
{
152
if
(
this
== &b)
153
return
*
this
;
154
155
width
= b.
width
;
156
height
= b.
height
;
157
158
delete
[]
image
;
159
image
=
new
uint8_t[
width
*
height
*4];
160
161
for
(
int
i=0; i <
width
*
height
*4; i++)
162
{
163
image
[i] = b.
image
[i];
164
}
165
166
return
*
this
;
167
}
168
169
void
BitMapPic::Save
(
const
char
*file)
170
{
171
FILE *f = fopen(file,
"w+"
);
172
173
if
(f == 0)
174
return
;
175
176
BitMapHeader
header
;
177
header
.biWidth =
width
;
178
header
.biHeight =
height
;
179
180
header
.bfSize =
sizeof
(
BitMapHeader
)+2+(
width
)*
height
*4;
181
header
.biSizeImage = (
width
)*
height
*4;
182
uint16_t bfType = 19778;
// 0x4D42
183
fwrite(&bfType,
sizeof
(bfType), 1, f);
184
fwrite(&
header
,
sizeof
(
header
), 1, f);
185
for
(
int
x = 0; x <
height
; x++)
186
{
187
fwrite(&
image
[x*
width
*4],
sizeof
(
char
),
width
*4, f);
188
}
189
fclose(f);
190
}
191
192
193
void
BitMapPic::SetPixel
(
int
x,
int
y, uint8_t redByte, uint8_t greenByte, uint8_t blueByte, uint8_t alphaByte )
194
{
195
// BGRA
196
image
[y*
width
*4+x*4+0] = blueByte;
197
image
[y*
width
*4+x*4+1] = greenByte;
198
image
[y*
width
*4+x*4+2] = redByte;
199
image
[y*
width
*4+x*4+3] = alphaByte;
200
}
201
202
void
BitMapPic::GetPixel
(
int
x,
int
y, uint8_t &redByte, uint8_t &greenByte, uint8_t &blueByte, uint8_t &alphaByte)
const
203
{
204
blueByte =
image
[y*
width
*4+x*4+0];
205
greenByte =
image
[y*
width
*4+x*4+1];
206
redByte =
image
[y*
width
*4+x*4+2];
207
alphaByte =
image
[y*
width
*4+x*4+3];
208
}
209
210
211
//
212
//BitMapPic::BitMapPic(const char *file)
213
//{
214
// Load(file);
215
//}
216
//
217
//BitMapPic::BitMapPic(int w, int h)
218
//:width(w), height(h)
219
//{
220
// image.resize(w*h*4);
221
// bytesReversed = false;
222
//}
223
//
224
//BitMapPic::BitMapPic(int w, int h, uint8_t *data)
225
//:width(w), height(h)
226
//{
227
// bytesReversed = false;
228
// image.resize(w*h*4);
229
// // slow copy, but correct TODO: memcpy
230
// for (int x = 0; x < w*h*4; x++)
231
// image[x] = data[x];
232
//}
233
//
234
//void BitMapPic::Save(const char *file)
235
//{
236
// FILE *f = fopen(file, "w+");
237
//
238
// if (f == 0) return;
239
//
241
// char zero[4] = {0, 0, 0, 0};
242
//
243
// bmp_header h;
244
// h.biWidth = width;
245
// if (bytesReversed)
246
// h.biHeight = -height;
247
// else
248
// h.biHeight = height;
249
//
250
// int buffer = (4-width%4)%4;
251
// h.bfSize = sizeof(bmp_header)+2+(width+buffer)*height*4;
252
// h.biSizeImage = (width+buffer)*height*4;
253
// uint16_t bfType = 19778;
254
// fwrite(&bfType, sizeof(bfType), 1, f);
255
// fwrite(&h, sizeof(bmp_header), 1, f);
256
// for (int x = 0; x < height; x++)
257
// {
258
// fwrite(&image[x*width*4], sizeof(char), width*4, f);
259
// if (0 != width%4)
260
// fwrite(&zero, sizeof(char), buffer, f);
261
// }
262
// fclose(f);
263
//}
264
//
265
//void BitMapPic::Load(const char *file)
266
//{
267
// FILE *f = fopen(file, "rb");
268
//
269
// if (f == 0)
270
// {
271
// printf("Unable to find file\n");
272
// width = 0;
273
// height = 0;
274
// image.resize(0);
275
// return;
276
// }
277
//
280
// char zero[4] = {0, 0, 0, 0};
281
// int buffer = (4-width%4)%4;
282
//
283
// bmp_header h;
284
// uint16_t bfType;
285
// fread(&bfType, sizeof(bfType), 1, f);
286
// if (bfType != 19778)
287
// {
288
// printf("Unable to load file properly\n");
289
// width = 0;
290
// height = 0;
291
// image.resize(0);
292
// fclose(f);
293
// return;
294
// }
295
// fread(&h, sizeof(bmp_header), 1, f);
296
// h.dump();
297
// height = h.biHeight;
298
// if ((int32_t)h.biHeight < 0)
299
// {
300
// height = -h.biHeight;
301
// bytesReversed = true;
302
// }
303
// width = h.biWidth;
304
// image.resize(height*width*4);
306
// fseek(f, h.bfOffBits, SEEK_SET);
307
// for (int x = 0; x < height; x++)
308
// {
310
// //printf("Read: %d\n", );
311
// fread(&image[x*width*4], sizeof(char), width*4, f);
312
// if (0 != width%4)
313
// {
314
// fread(&zero, sizeof(char), buffer, f);
315
// }
316
// }
317
// fclose(f);
320
//}
321
//
BitMapPic
Definition:
BitMap.h:46
BitMapPic::image
uint8_t * image
Definition:
BitMap.h:63
BitMapPic::width
uint32_t width
Definition:
BitMap.h:62
width
int width
Definition:
SFML_HOG.cpp:54
BitMapPic::height
uint32_t height
Definition:
BitMap.h:62
BitMapPic::BitMapPic
BitMapPic(int w, int h)
Definition:
BitMap.cpp:14
header
Definition:
Map.cpp:489
BitMapHeader
Definition:
BitMap.h:22
BitMapPic::SetPixel
void SetPixel(int x, int y, uint8_t redByte, uint8_t greenByte, uint8_t blueByte, uint8_t alphaByte=0)
Definition:
BitMap.cpp:193
BitMapPic::~BitMapPic
~BitMapPic()
Definition:
BitMap.cpp:139
height
int height
Definition:
SFML_HOG.cpp:54
BitMap.h
BitMapPic::Save
void Save(const char *file)
Definition:
BitMap.cpp:169
BitMapPic::GetPixel
void GetPixel(int x, int y, uint8_t &redByte, uint8_t &greenByte, uint8_t &blueByte, uint8_t &alphaByte) const
Definition:
BitMap.cpp:202
BitMapPic::operator=
BitMapPic & operator=(const BitMapPic &b)
Definition:
BitMap.cpp:150
Generated by
1.8.17