1 |
2 |
fukuchi |
|
2 |
|
|
#include <stdio.h>
|
3 |
|
|
#include <stdlib.h>
|
4 |
|
|
|
5 |
|
|
#include "bmp.h"
|
6 |
|
|
|
7 |
|
|
void BmpRead(BITMAP *bitMap){
|
8 |
|
|
FILE *fp;
|
9 |
|
|
int line;
|
10 |
|
|
int x;
|
11 |
|
|
int y;
|
12 |
|
|
char dummy;
|
13 |
|
|
#ifdef WIN32
|
14 |
|
|
setmode( fileno(stdin), O_BINARY );
|
15 |
|
|
#endif
|
16 |
|
|
fp = stdin;
|
17 |
|
|
|
18 |
|
|
fread((char *)&(bitMap->bitMapFile.bfType) ,1,2,fp);
|
19 |
|
|
fread((char *)&(bitMap->bitMapFile.bfSize) ,1,4,fp);
|
20 |
|
|
fread((char *)&(bitMap->bitMapFile.bfReserved1) ,1,2,fp);
|
21 |
|
|
fread((char *)&(bitMap->bitMapFile.bfReserved2) ,1,2,fp);
|
22 |
|
|
fread((char *)&(bitMap->bitMapFile.bfOffBits) ,1,4,fp);
|
23 |
|
|
// fprintf(stderr,"\nbitMapFile->\n");
|
24 |
|
|
// fprintf(stderr,"bfType =%d\n",bitMap->bitMapFile.bfType);
|
25 |
|
|
// fprintf(stderr,"bfSize =%ld\n",bitMap->bitMapFile.bfSize);
|
26 |
|
|
// fprintf(stderr,"bfReserved =%x\n",bitMap->bitMapFile.bfReserved1);
|
27 |
|
|
// fprintf(stderr,"bfReserved =%x\n",bitMap->bitMapFile.bfReserved2);
|
28 |
|
|
// fprintf(stderr,"bfIffBits =%lx\n",bitMap->bitMapFile.bfOffBits);
|
29 |
|
|
|
30 |
|
|
fread((char *)&(bitMap->bitMapInfo.biSize) ,1,4,fp);
|
31 |
|
|
fread((char *)&(bitMap->bitMapInfo.biWidth) ,1,4,fp);
|
32 |
|
|
fread((char *)&(bitMap->bitMapInfo.biHeight) ,1,4,fp);
|
33 |
|
|
fread((char *)&(bitMap->bitMapInfo.biPlanes) ,1,2,fp);
|
34 |
|
|
fread((char *)&(bitMap->bitMapInfo.biBitCount) ,1,2,fp);
|
35 |
|
|
fread((char *)&(bitMap->bitMapInfo.biCompression) ,1,4,fp);
|
36 |
|
|
fread((char *)&(bitMap->bitMapInfo.biSizeImage) ,1,4,fp);
|
37 |
|
|
fread((char *)&(bitMap->bitMapInfo.biXPixPerMeter) ,1,4,fp);
|
38 |
|
|
fread((char *)&(bitMap->bitMapInfo.biYPixPerMeter) ,1,4,fp);
|
39 |
|
|
fread((char *)&(bitMap->bitMapInfo.biClrUsed) ,1,4,fp);
|
40 |
|
|
fread((char *)&(bitMap->bitMapInfo.biClrImporant) ,1,4,fp);
|
41 |
|
|
// fprintf(stderr,"\nbitMapInfo->\n");
|
42 |
|
|
// fprintf(stderr,"biSize =%ld\n",bitMap->bitMapInfo.biSize);
|
43 |
|
|
// fprintf(stderr,"biWidth =%ld\n",bitMap->bitMapInfo.biWidth);
|
44 |
|
|
// fprintf(stderr,"biHeight =%ld\n",bitMap->bitMapInfo.biHeight);
|
45 |
|
|
// fprintf(stderr,"biPlanes =%d\n",bitMap->bitMapInfo.biPlanes);
|
46 |
|
|
// fprintf(stderr,"biBitCount =%d\n",bitMap->bitMapInfo.biBitCount);
|
47 |
|
|
// fprintf(stderr,"biCopression =%ld\n",bitMap->bitMapInfo.biCompression);
|
48 |
|
|
// fprintf(stderr,"biSizeImage =%ld\n",bitMap->bitMapInfo.biSizeImage);
|
49 |
|
|
// fprintf(stderr,"biXPixPerMeter =%ld\n",bitMap->bitMapInfo.biXPixPerMeter);
|
50 |
|
|
// fprintf(stderr,"biYPixPerMeter =%ld\n",bitMap->bitMapInfo.biYPixPerMeter);
|
51 |
|
|
// fprintf(stderr,"biClrUsed =%ld\n",bitMap->bitMapInfo.biClrUsed);
|
52 |
|
|
// fprintf(stderr,"biClrImporant =%ld\n",bitMap->bitMapInfo.biClrImporant);
|
53 |
|
|
|
54 |
|
|
for ( y=bitMap->bitMapInfo.biHeight - 1 ; 0<=y ; y--) {
|
55 |
|
|
line = (bitMap->bitMapInfo.biWidth * bitMap->bitMapInfo.biBitCount) / 8;
|
56 |
|
|
if(line % 4) line = (((line / 4) + 1) * 4);
|
57 |
|
|
for ( x=0 ; x < bitMap->bitMapInfo.biWidth ; x++) {
|
58 |
|
|
line--;fread((char *)&(bitMap->bitColor[x][y].b),1,1,fp); //printf("[%d][%d]->r=%d",x,y,bitMap->bitColor[x][y].r);
|
59 |
|
|
line--;fread((char *)&(bitMap->bitColor[x][y].g),1,1,fp); //printf("[%d][%d]->g=%d",x,y,bitMap->bitColor[x][y].g);
|
60 |
|
|
line--;fread((char *)&(bitMap->bitColor[x][y].r),1,1,fp); //printf("[%d][%d]->b=%d\n",x,y,bitMap->bitColor[x][y].b);
|
61 |
|
|
}
|
62 |
|
|
while(line>0) {
|
63 |
|
|
line--;
|
64 |
|
|
fread(&dummy,1,1,fp);
|
65 |
|
|
};
|
66 |
|
|
}
|
67 |
|
|
return;
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
void BmpWrite(BITMAP *bitMap){
|
71 |
|
|
FILE *fp;
|
72 |
|
|
int line;
|
73 |
|
|
int x;
|
74 |
|
|
int y;
|
75 |
|
|
char dummy;
|
76 |
|
|
#ifdef WIN32
|
77 |
|
|
setmode( fileno(stdout), O_BINARY );
|
78 |
|
|
#endif
|
79 |
|
|
fp = stdout;
|
80 |
|
|
dummy = 0;
|
81 |
|
|
|
82 |
|
|
fwrite((char *)&(bitMap->bitMapFile.bfType) ,1,2,fp);
|
83 |
|
|
fwrite((char *)&(bitMap->bitMapFile.bfSize) ,1,4,fp);
|
84 |
|
|
fwrite((char *)&(bitMap->bitMapFile.bfReserved1) ,1,2,fp);
|
85 |
|
|
fwrite((char *)&(bitMap->bitMapFile.bfReserved2) ,1,2,fp);
|
86 |
|
|
fwrite((char *)&(bitMap->bitMapFile.bfOffBits) ,1,4,fp);
|
87 |
|
|
// fprintf(stderr,"\nbitMapFile->\n");
|
88 |
|
|
// fprintf(stderr,"bfType =%d\n",bitMap->bitMapFile.bfType);
|
89 |
|
|
// fprintf(stderr,"bfSize =%ld\n",bitMap->bitMapFile.bfSize);
|
90 |
|
|
// fprintf(stderr,"bfReserved =%x\n",bitMap->bitMapFile.bfReserved1);
|
91 |
|
|
// fprintf(stderr,"bfReserved =%x\n",bitMap->bitMapFile.bfReserved2);
|
92 |
|
|
// fprintf(stderr,"bfIffBits =%lx\n",bitMap->bitMapFile.bfOffBits);
|
93 |
|
|
|
94 |
|
|
fwrite((char *)&(bitMap->bitMapInfo.biSize) ,1,4,fp);
|
95 |
|
|
fwrite((char *)&(bitMap->bitMapInfo.biWidth) ,1,4,fp);
|
96 |
|
|
fwrite((char *)&(bitMap->bitMapInfo.biHeight) ,1,4,fp);
|
97 |
|
|
fwrite((char *)&(bitMap->bitMapInfo.biPlanes) ,1,2,fp);
|
98 |
|
|
fwrite((char *)&(bitMap->bitMapInfo.biBitCount) ,1,2,fp);
|
99 |
|
|
fwrite((char *)&(bitMap->bitMapInfo.biCompression) ,1,4,fp);
|
100 |
|
|
fwrite((char *)&(bitMap->bitMapInfo.biSizeImage) ,1,4,fp);
|
101 |
|
|
fwrite((char *)&(bitMap->bitMapInfo.biXPixPerMeter) ,1,4,fp);
|
102 |
|
|
fwrite((char *)&(bitMap->bitMapInfo.biYPixPerMeter) ,1,4,fp);
|
103 |
|
|
fwrite((char *)&(bitMap->bitMapInfo.biClrUsed) ,1,4,fp);
|
104 |
|
|
fwrite((char *)&(bitMap->bitMapInfo.biClrImporant) ,1,4,fp);
|
105 |
|
|
// fprintf(stderr,"\nbitMapInfo->\n");
|
106 |
|
|
// fprintf(stderr,"biSize =%ld\n",bitMap->bitMapInfo.biSize);
|
107 |
|
|
// fprintf(stderr,"biWidth =%ld\n",bitMap->bitMapInfo.biWidth);
|
108 |
|
|
// fprintf(stderr,"biHeight =%ld\n",bitMap->bitMapInfo.biHeight);
|
109 |
|
|
// fprintf(stderr,"biPlanes =%d\n",bitMap->bitMapInfo.biPlanes);
|
110 |
|
|
// fprintf(stderr,"biBitCount =%d\n",bitMap->bitMapInfo.biBitCount);
|
111 |
|
|
// fprintf(stderr,"biCopression =%ld\n",bitMap->bitMapInfo.biCompression);
|
112 |
|
|
// fprintf(stderr,"biSizeImage =%ld\n",bitMap->bitMapInfo.biSizeImage);
|
113 |
|
|
// fprintf(stderr,"biXPixPerMeter =%ld\n",bitMap->bitMapInfo.biXPixPerMeter);
|
114 |
|
|
// fprintf(stderr,"biYPixPerMeter =%ld\n",bitMap->bitMapInfo.biYPixPerMeter);
|
115 |
|
|
// fprintf(stderr,"biClrUsed =%ld\n",bitMap->bitMapInfo.biClrUsed);
|
116 |
|
|
// fprintf(stderr,"biClrImporant =%ld\n",bitMap->bitMapInfo.biClrImporant);
|
117 |
|
|
|
118 |
|
|
for ( y=bitMap->bitMapInfo.biHeight - 1 ; 0<=y ; y--) {
|
119 |
|
|
line = (bitMap->bitMapInfo.biWidth * bitMap->bitMapInfo.biBitCount) / 8;
|
120 |
|
|
if(line % 4) line = (((line / 4) + 1) * 4);
|
121 |
|
|
for ( x=0 ; x < bitMap->bitMapInfo.biWidth ; x++) {
|
122 |
|
|
line--;fwrite((char *)&(bitMap->bitColor[x][y].b),1,1,fp);
|
123 |
|
|
line--;fwrite((char *)&(bitMap->bitColor[x][y].g),1,1,fp);
|
124 |
|
|
line--;fwrite((char *)&(bitMap->bitColor[x][y].r),1,1,fp);
|
125 |
|
|
}
|
126 |
|
|
while(line>0) {
|
127 |
|
|
line--;
|
128 |
|
|
fwrite(&dummy,1,1,fp);
|
129 |
|
|
};
|
130 |
|
|
}
|
131 |
|
|
return;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
|