bmp2int32.c
1.19 KB
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
/*
convert 8bits bmp to biniary file
File format:
color index (#of color * 4)
Then pixel value
*/
#include <stdio.h>
#include <stdlib.h>
#define PIXEL_START (54 + 1024)
int main(int argc, char **argv)
{
FILE *src, *dst;
int i, r, g, b, a, rgba, w, h, color_num, bpp;
unsigned char header[54];
if (argc != 3 ) {
printf("Usage: bmp2int 8/4bits_bmp fake_intensity_file\n");
return;
}
src = fopen(argv[1], "rb");
dst = fopen(argv[2], "wb");
fread(header, 1, sizeof(header), src);
w = (header[21] << 24) | (header[20] << 16) | (header[19] << 8) | header[18];
h = (header[25] << 24) | (header[24] << 16) | (header[23] << 8) | header[22];
color_num = 1 << header[0x1c];
bpp = w * header[0x1c] / 8;
for (i=0; i<color_num; i++) {
b = fgetc(src);
g = fgetc(src);
r = fgetc(src);
a = fgetc(src);
fputc( r, dst);
fputc( g, dst);
fputc( b, dst);
fputc( 0xff, dst);
}
for (i=0; i<h; i++) {
fseek(src, 54+color_num*4+(h-i-1)*bpp, SEEK_SET);
for (r=0; r<bpp; r++) fputc(fgetc(src), dst);
}
fclose(src);
fclose(dst);
return 0;
}