-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.h
94 lines (85 loc) · 1.93 KB
/
func.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
#pragma once
#pragma pack(2)
#include "color.h"
#include <cmath>
#include <fstream>
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef long LONG;
struct BmpFileHeader
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
};
struct BmpInfoHeader
{
DWORD biSize; //0-3
LONG biWidth; //4-7
LONG biHeight; //8-11
WORD biPlanes; //12-13
WORD biBitCount; //14-15
DWORD biCompression; //16-19
DWORD biSizeImage; //20-23
LONG biXPelsPerMeter; //24-27
LONG biYPelsPerMeter; //28-31
DWORD biClrUsed; //32-35
DWORD biClrImportant; //36-39
};
struct RGBType
{
unsigned char r;
unsigned char g;
unsigned char b;
void set(unsigned char r1, unsigned char g1, unsigned char b1)
{
r = r1;
g = g1;
b = b1;
}
void set(Color color)
{
r = (int)floor(color.red * 255);
g = (int)floor(color.green * 255);
b = (int)floor(color.blue * 255);
}
};
void saveBmp(const char *fileName, int w, int h, int dpi, RGBType *data)
{
int size = 4 * w * h;
//设置bmpfileheader的各种数值
BmpFileHeader fileHeader;
fileHeader.bfType = 0x4D42;
fileHeader.bfReserved1 = 0;
fileHeader.bfReserved2 = 0;
fileHeader.bfSize = 54 + size;
fileHeader.bfOffBits = 54;
//设置infoheader的值
BmpInfoHeader infoHeader = {0};
infoHeader.biSize = 40;
infoHeader.biHeight = -h;
infoHeader.biWidth = w;
infoHeader.biPlanes = 1;
infoHeader.biBitCount = 24;
infoHeader.biSizeImage = 0;
infoHeader.biCompression = 0;
FILE *output = fopen(fileName, "wb");
if (output)
{
fwrite(&fileHeader, 14, 1, output);
fwrite(&infoHeader, 40, 1, output);
for (int i = 0; i < w * h; ++i)
{
RGBType rgb = data[i];
// unsigned char r = (int)(floor(255 * rgb.r));
// unsigned char g = (int)(floor(255 * rgb.g));
// unsigned char b = (int)(floor(255 * rgb.b));
unsigned char color[3] = {rgb.r, rgb.g, rgb.b};
fwrite(color, 3, 1, output);
}
fclose(output);
}
}