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
|
// .h
namespace bmp
{
typedef unsigned short word;
typedef unsigned long dword;
#pragma pack( 1 )
struct BMPFileHeader
{
word bfType;
dword bfSize;
word bfReserved1;
word bfReserved2;
dword bfOffBits;
};
struct BMPInfoHeader
{
dword biSize;
long biWidth;
long biHeight;
word biPlanes;
word biBitCount;
dword biCompression;
dword biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
dword biClrUsed;
dword biClrImportant;
};
#pragma pack()
bool saveBMP( const char* fileName, dword* pBmp, int width, int height );
}
// .cpp
bool bmp::saveBMP( const char* fileName, dword* pBmp, int width, int height )
{
if( pBmp == 0 )
return false;
FILE* file = fopen( fileName, "wb" );
if( file == 0 )
return false;
BMPFileHeader header;
BMPInfoHeader info;
int padding = ( width * 3 ) % 4;
if( padding )
padding = 4 - padding;
header.bfType = 19778;
header.bfSize = sizeof( BMPFileHeader ) + sizeof( BMPInfoHeader ) + (3 * width + padding) * height;
header.bfReserved1 = 0;
header.bfReserved2 = 0;
header.bfOffBits = sizeof( BMPFileHeader ) + sizeof( BMPInfoHeader );
info.biSize = sizeof( BMPInfoHeader );
info.biWidth = width;
info.biHeight = height;
info.biPlanes = 1;
info.biBitCount = 24;
info.biCompression = 0;
info.biSizeImage = 0;
info.biXPelsPerMeter = 0;
info.biYPelsPerMeter = 0;
info.biClrUsed = 0;
info.biClrImportant = 0;
fwrite( &header, sizeof( BMPFileHeader ), 1, file );
fwrite( &info, sizeof( BMPInfoHeader ), 1, file );
for( int y = height - 1; y >= 0; --y )
{
dword* scanline = pBmp + y * width;
for( int x = 0; x < width; ++x )
{
fwrite( scanline++, 3, 1, file );
}
char pad = 0;
fwrite( &pad, 1, padding, file );
}
fclose( file );
return true;
}
|