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
|
void createBitmap(const std::string& filename, unsigned int width, unsigned int height, DWORD* pixeldata)
{
std::fstream fileStream(filename.c_str(), std::ios::binary | std::ios::out);
if (!fileStream) {
return;
}
const int headeroffset = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
const int scanlineWidth = ((width * 3) + 3) & ~3;
const int padding = scanlineWidth - width * 3;
const BITMAPFILEHEADER fileHeader =
{
('B' | ('M' << 8)),
headeroffset + height * scanlineWidth,
0,
0,
headeroffset
};
const BITMAPINFOHEADER fileInfoHeader =
{
sizeof(BITMAPINFOHEADER),
width,
height * -1,
1,
24,
BI_RGB,
0, 0, 0, 0, 0
};
/* BitmapFileHeader */
fileStream.write(reinterpret_cast<const char*>(&fileHeader), sizeof(fileHeader));
/* BitmapInfoHeader */
fileStream.write(reinterpret_cast<const char*>(&fileInfoHeader), sizeof(fileInfoHeader));
for(unsigned int y = 0; y < height; ++y)
{
for(unsigned int x = 0; x < width; ++x)
{
const DWORD pixel = *pixeldata++;
const char bgr[] = {(char)pixel & 0xff,
(char)(pixel >> 8) & 0xff,
(char)(pixel >> 16) & 0xff};
fileStream.write(reinterpret_cast<const char*>(bgr), sizeof(bgr));
}
const char filler[4] = {};
fileStream.write(filler, padding);
}
fileStream.close();
}
int main()
{
srand(time(0));
int x = rand() % 512;
int y = rand() % 512;
DWORD *data = new DWORD[x * y];
const float step = 255.0f / y;
float color = 0;
DWORD *scan = data;
for (int i = 0; i < y; ++i, scan += x, color += step )
memset(scan, (int)color, sizeof(DWORD) * x);
createBitmap("mybmp.bmp", x, y, data);
delete[] data;
return 0;
}
|