-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBitmap.cpp
162 lines (134 loc) · 3.76 KB
/
Bitmap.cpp
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
//
// bitmap.cpp
//
// handle MS bitmap I/O. For portability, we don't use the data structure
// defined in Windows.h. However, there is some strange thing, the size of our
// structure is different from what it should be though we define it in the
// same way as MS did. So, there is a hack, we use the hardcoded constant, 14,
// instead of the sizeof to calculate the size of the structure. You are not
// supposed to worry about this part. However, I will appreciate if you find
// out the reason and let me know. Thanks.
//
#include "bitmap.h"
BMP_BITMAPFILEHEADER bmfh;
BMP_BITMAPINFOHEADER bmih;
// Bitmap data returned is (R,G,B) tuples in row-major order.
unsigned char* readBMP(char* fname,
int& width,
int& height)
{
FILE* file;
BMP_DWORD pos;
if ( (file=fopen( fname, "rb" )) == NULL )
return NULL;
// I am doing fread( &bmfh, sizeof(BMP_BITMAPFILEHEADER), 1, file ) in a safe way. :}
fread( &(bmfh.bfType), 2, 1, file);
fread( &(bmfh.bfSize), 4, 1, file);
fread( &(bmfh.bfReserved1), 2, 1, file);
fread( &(bmfh.bfReserved2), 2, 1, file);
fread( &(bmfh.bfOffBits), 4, 1, file);
pos = bmfh.bfOffBits;
fread( &bmih, sizeof(BMP_BITMAPINFOHEADER), 1, file );
// error checking
if ( bmfh.bfType!= 0x4d42 ) { // "BM" actually
return NULL;
}
if ( bmih.biBitCount != 24 )
return NULL;
/*
if ( bmih.biCompression != BMP_BI_RGB ) {
return NULL;
}
*/
fseek( file, pos, SEEK_SET );
width = bmih.biWidth;
height = bmih.biHeight;
int padWidth = width * 3;
int pad = 0;
if ( padWidth % 4 != 0 )
{
pad = 4 - (padWidth % 4);
padWidth += pad;
}
int bytes = height*padWidth;
unsigned char *data = new unsigned char [bytes];
int result = fread( data, bytes, 1, file );
if (!result) {
delete [] data;
return NULL;
}
fclose( file );
// shuffle bitmap data such that it is (R,G,B) tuples in row-major order
int i, j;
j = 0;
unsigned char temp;
unsigned char* in;
unsigned char* out;
in = data;
out = data;
for ( j = 0; j < height; ++j )
{
for ( i = 0; i < width; ++i )
{
out[1] = in[1];
temp = in[2];
out[2] = in[0];
out[0] = temp;
in += 3;
out += 3;
}
in += pad;
}
return data;
}
void writeBMP(char* iname,
int width,
int height,
unsigned char* data)
{
int bytes, pad;
bytes = width * 3;
pad = (bytes%4) ? 4-(bytes%4) : 0;
bytes += pad;
bytes *= height;
bmfh.bfType = 0x4d42; // "BM"
bmfh.bfSize = sizeof(BMP_BITMAPFILEHEADER) + sizeof(BMP_BITMAPINFOHEADER) + bytes;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = /*hack sizeof(BMP_BITMAPFILEHEADER)=14, sizeof doesn't work?*/
14 + sizeof(BMP_BITMAPINFOHEADER);
bmih.biSize = sizeof(BMP_BITMAPINFOHEADER);
bmih.biWidth = width;
bmih.biHeight = height;
bmih.biPlanes = 1;
bmih.biBitCount = 24;
bmih.biCompression = BMP_BI_RGB;
bmih.biSizeImage = 0;
bmih.biXPelsPerMeter = (int)(100 / 2.54 * 72);
bmih.biYPelsPerMeter = (int)(100 / 2.54 * 72);
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;
FILE *outFile=fopen(iname, "wb");
// fwrite(&bmfh, sizeof(BMP_BITMAPFILEHEADER), 1, outFile);
fwrite( &(bmfh.bfType), 2, 1, outFile);
fwrite( &(bmfh.bfSize), 4, 1, outFile);
fwrite( &(bmfh.bfReserved1), 2, 1, outFile);
fwrite( &(bmfh.bfReserved2), 2, 1, outFile);
fwrite( &(bmfh.bfOffBits), 4, 1, outFile);
fwrite(&bmih, sizeof(BMP_BITMAPINFOHEADER), 1, outFile);
bytes /= height;
unsigned char* scanline = new unsigned char [bytes];
for ( int j = 0; j < height; ++j )
{
memcpy( scanline, data + j*3*width, bytes );
for ( int i = 0; i < width; ++i )
{
unsigned char temp = scanline[i*3];
scanline[i*3] = scanline[i*3+2];
scanline[i*3+2] = temp;
}
fwrite( scanline, bytes, 1, outFile);
}
delete [] scanline;
fclose(outFile);
}