-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriteBMP.hpp
48 lines (43 loc) · 1.37 KB
/
writeBMP.hpp
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
#ifndef UMPIRE_WRITEBMP_H_
#define UMPIRE_WRITEBMP_H_
#include <assert.h>
#include <stdio.h>
static void WriteBMP(int x, int y, unsigned char *bmp, const char * name)
{
const unsigned char bmphdr[54] = {66, 77, 255, 255, 255, 255, 0, 0, 0, 0, 54, 4, 0, 0, 40, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 1, 0, 8, 0, 0, 0, 0, 0, 255, 255, 255, 255, 196, 14, 0, 0, 196, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned char hdr[1078];
int i, j, c, xcorr, diff;
FILE *f;
xcorr = (x+3) >> 2 << 2; // BMPs have to be a multiple of 4 pixels wide.
diff = xcorr - x;
for (i = 0; i < 54; i++) hdr[i] = bmphdr[i];
*((int*)(&hdr[18])) = xcorr;
*((int*)(&hdr[22])) = y;
*((int*)(&hdr[34])) = xcorr*y;
*((int*)(&hdr[2])) = xcorr*y + 1078;
for (i = 0; i < 256; i++) {
j = i*4 + 54;
hdr[j+0] = i; // blue
hdr[j+1] = i; // green
hdr[j+2] = i; // red
hdr[j+3] = 0; // dummy
}
f = fopen(name, "wb");
assert(f != NULL);
c = fwrite(hdr, 1, 1078, f);
assert(c == 1078);
if (diff == 0) {
c = fwrite(bmp, 1, x*y, f);
assert(c == x*y);
} else {
*((int*)(&hdr[0])) = 0; // need up to three zero bytes
for (j = 0; j < y; j++) {
c = fwrite(&bmp[j * x], 1, x, f);
assert(c == x);
c = fwrite(hdr, 1, diff, f);
assert(c == diff);
}
}
fclose(f);
}
#endif //UMPIRE_WRITEBMP_H_