-
Notifications
You must be signed in to change notification settings - Fork 6
/
Bitmap.h
139 lines (106 loc) · 2.49 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
#ifndef BITMAP_H
#define BITMAP_H
#include <iostream>
#include <fstream>
#include <stdint.h>
class Bitmap {
public:
Bitmap() : image( NULL ) {}
Bitmap( int w, int h ) {
image = new char[ w * h ];
header.filesz = sizeof( bmpHeader ) + sizeof( bmpInfo ) + ( w * h ) + 2 + 1024;
header.bmp_offset = sizeof( bmpHeader ) + sizeof( bmpInfo ) + 2 + 1024;
info.header_sz = sizeof( bmpInfo );
info.width = w;
info.height = h;
info.nplanes = 1;
info.bitspp = 8;
info.compress_type = 0;
info.bmp_bytesz = w * h;
info.hres = 2835;
info.vres = 2835;
info.ncolors = 0;
info.nimpcolors = 0;
}
bool Save( const char* filename ) {
if( image == NULL ) {
std::cerr << "Image unitialized" << std::endl;
return false;
}
std::ofstream file( filename, std::ios::out | std::ios::binary );
file.write( "BM", 2 );
file.write( (char*)( &header ), sizeof( bmpHeader ) );
file.write( (char*)( &info ), sizeof( bmpInfo ) );
char rgba[ 4 ];
for( int i = 0; i < 256; ++i ) {
rgba[ 0 ] = i;
rgba[ 1 ] = i;
rgba[ 2 ] = i;
rgba[ 3 ] = 0;
file.write( rgba, 4 );
}
file.write( image, Width() * Height() );
file.close();
return true;
}
bool Load(const char* filename) {
if( image != NULL ) {
delete[] image;
}
std::ifstream file( filename, std::ios::in | std::ios::binary );
if( !file.is_open() ) {
std::cerr << "Cannot open " << filename << std::endl;
return false;
}
char BM[ 2 ];
file.read( (char*)( BM ), 2 );
file.read( (char*)( &header ), sizeof( bmpHeader ) );
file.read( (char*)( &info ), sizeof( bmpInfo ) );
file.seekg( header.bmp_offset, std::ios::beg );
image = new char[ info.width * info.height ];
file.read(image, info.width * info.height);
file.close();
return true;
}
~Bitmap() {
if( image != NULL ) {
delete [] image;
}
}
int Width() {
return info.width;
}
int Height() {
return info.height;
}
char GetPixel( int x, int y ) {
return image[ y * info.width + x ];
}
void SetPixel( int x, int y, char color ) {
image[ y * info.width + x ] = color;
}
char* image;
private:
struct bmpHeader {
uint32_t filesz;
uint16_t creator1;
uint16_t creator2;
uint32_t bmp_offset;
};
struct bmpInfo {
uint32_t header_sz;
int32_t width;
int32_t height;
uint16_t nplanes;
uint16_t bitspp;
uint32_t compress_type;
uint32_t bmp_bytesz;
int32_t hres;
int32_t vres;
uint32_t ncolors;
uint32_t nimpcolors;
};
bmpHeader header;
bmpInfo info;
};
#endif