-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkbmp.c
169 lines (134 loc) · 3.82 KB
/
mkbmp.c
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
163
164
165
166
167
168
169
#include "mkbmp.h"
int mk_bmp(const int width, const int height, const int pxsize, const unsigned char* rdat, const unsigned char* gdat, const unsigned char* bdat, const char* fname)
{
FILE* pf = fopen(fname, "wb");
if (!pf) return 1;
int bmhsz, dibhsz, datasz, fsz;
char buffer[1024];
int temp;
bmhsz = 0x0e;
dibhsz = 0x28;
datasz = width * pxsize * height * pxsize * 3;
fsz = bmhsz + dibhsz + datasz;
// BMP header
fwrite("BM", 2, 1, pf); // magic
fwrite(&fsz, 1, 4, pf); // file size
fwrite("\x00\x00\x00\x00", 4, 1, pf); // app-dependent info
temp = bmhsz+dibhsz;
fwrite(&temp, 1, 4, pf); // data block offset
// DIB header
fwrite(&dibhsz, 1, 4, pf); // header size
temp = width*pxsize;
fwrite(&temp, 1, 4, pf); // image width
temp = height*pxsize;
fwrite(&temp, 1, 4, pf); // image height
fwrite("\x01\x00", 2, 1, pf); // color planes - always 1
fwrite("\x18\x00", 2, 1, pf); // bits per pixel
fwrite("\x00\x00\x00\x00", 4, 1, pf); // compression method; 0=none
fwrite(&datasz, 1, 4, pf); // bitmap data size
temp = 10;
fwrite(&temp, 1, 4, pf); // horizontal pixels per meter
fwrite(&temp, 1, 4, pf); // vertical pixels per meter
fwrite("\x00\x00\x00\x00", 4, 1, pf); // colors in palette
fwrite("\x00\x00\x00\x00", 4, 1, pf); // important colors; ignored
int x, y, i, j, aidx, rowsz;
for (y=height-1; y>=0; y--)
{
for (i=0; i<pxsize; i++)
{
rowsz = 0;
for (x=0; x<width; x++)
{
aidx = x+width*y;
for (j=0; j<pxsize; j++)
{
// BGR order
fwrite(&bdat[aidx], 1, 1, pf);
fwrite(&gdat[aidx], 1, 1, pf);
fwrite(&rdat[aidx], 1, 1, pf);
rowsz += 3;
}
}
rowsz &= 0x03;
if (rowsz)
{
rowsz = 4-rowsz;
fwrite("\x00\x00\x00\x00", rowsz, 1, pf); // pad each row to multiple of 4 bytes
}
}
}
fclose(pf);
return 0;
}
int read_bmp( int* width,
int* height,
unsigned char* rdat,
unsigned char* gdat,
unsigned char* bdat,
const char* fname
)
{
FILE* pf = fopen(fname, "rb");
if (!pf) return 1;
char buffer[1024];
int wid, hei, offset;
// BMP header
fread(buffer, 10, 1, pf);
fread(&offset, 4, 1, pf);
// DIB header
//fread(buffer, 40, 1, pf);
fread(buffer, 4, 1, pf);
fread(&wid, 4, 1, pf);
fread(&hei, 4, 1, pf);
fread(buffer, 28, 1, pf);
if (offset > 54) fread(buffer, offset-54, 1, pf);
// printf("%d x %d\n", wid, hei);
if (width) *width = wid;
if (height) *height = hei;
int l, ll, rowsize, x, y, perln;
/*
ll = wid * hei;
for (l=0; l<ll; l++)
{ fread(buffer, 1, 3, pf);
bdat[l] = buffer[0];
gdat[l] = buffer[1];
rdat[l] = buffer[2];
rowsize += 3;
if (rowsize >= wid*3)
{ rowsize &= 3;
rowsize = 3-rowsize;
fread(buffer, 1, rowsize, pf);
rowsize = 0;
}
}*/
perln = 3*wid;
if (perln % 4) perln += (4 - (perln % 4));
for (y = hei-1; y >= 0; y--) // bottom to top
{ rowsize = 0;
l = y * wid;
for (x=0; x<wid; x++)
{ ll = l + x;
fread(buffer, 1, 3, pf);
bdat[ll] = buffer[0];
gdat[ll] = buffer[1];
rdat[ll] = buffer[2];
rowsize += 3;
/*
if (rowsize >= wid*3)
{ rowsize &= 3;
rowsize = 3-rowsize;
fread(buffer, 1, rowsize, pf);
rowsize = 0;
}*/
}
if (rowsize % 4)
{
rowsize &= 3;
rowsize = 3-rowsize;
fread(buffer, 1, rowsize, pf);
}
rowsize = 0;
}
fclose(pf);
return 0;
}