-
Notifications
You must be signed in to change notification settings - Fork 0
/
TS1.C
181 lines (169 loc) · 2.8 KB
/
TS1.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
170
171
172
173
174
175
176
177
178
179
180
#include "stdio.h"
#include "graphics.h"
#include "alloc.h"
#include "stdlib.h"
#include "math.h"
typedef struct tagBITMAPFILEHEADER
{
unsigned int bfType;
unsigned long bfSize;
unsigned int bfReserved1;
unsigned int bfReserved2;
unsigned long bfOffBits;
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER
{
unsigned long biSize;
long biWidth;
long biHeight;
unsigned int biPanes;
unsigned int biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
}BITMAPINFOHEADER;
typedef struct tagRGBQUAD
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
}RGBQUAD;
typedef struct tagBITMAPINFO
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[];
}BITMAPINFO;
int xmax;
int ymax;
void InitGraph()
{
int mod=EGA;
int dr=EGAHI;
initgraph(&mod,&dr,"c://bgi");
xmax=getmaxx();
ymax=getmaxy();
}
void CloseGraph()
{
closegraph();
}
void Exit(char *ErrorCode)
{
printf("%s",ErrorCode);
getch();
exit(0);
}
long WidthBytes(long Width,int BitCount)
{
long WBytes;
WBytes=(Width*BitCount+31)/8;
WBytes=WBytes/4*4;
return WBytes;
}
unsigned char SetPalette(int Colors,unsigned char data)
{
switch(Colors)
{
case 16:
switch(data)
{
case 1:
return 4;
case 4:
return 1;
case 3:
return 6;
case 6:
return 3;
case 9:
return 12;
case 12:
return 9;
case 11:
return 14;
case 14:
return 11;
default:
return data;
}
case 2:
if(data==1)
return 15;
else
return 0;
}
}
void main()
{
long i,j;
long WBytes;
int Colors;
long Height,Width;
FILE *fp;
void *Temp=NULL;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
unsigned char SrcData,data;
InitGraph();
if((fp=fopen("calc.bmp","rb"))==NULL)
{
Exit("Can Not Open The File.\n");
}
fread(&bfh,sizeof(BITMAPFILEHEADER),1,fp);
if(bfh.bfType!='M'*256+'B')
{
Exit("This Is Not A Bmp File.\n");
}
fread(&bih,sizeof(BITMAPINFOHEADER),1,fp);
Height=bih.biHeight;
Width=bih.biWidth;
WBytes=WidthBytes(Width,bih.biBitCount);
Colors=1<<bih.biBitCount;
if(!(Colors==16||Colors==2))
{
Exit("This Programme Only For 16 Colors Bitmap.\n");
}
fread(Temp,sizeof(RGBQUAD),Colors,fp);
printf("%d %d",'\f','\t');
for(i=Height-1;i>=0;i--)
{
fseek(fp,54+Colors*sizeof(RGBQUAD)+i*WBytes,SEEK_SET);
for(j=0;j<Width;j++)
{
switch(Colors)
{
case 16:
if(j%2==0)
{
fread(&SrcData,1,1,fp);
data=SetPalette(Colors,SrcData/16);
putpixel(j,Height-1-i,data);
}
else
{
data=SetPalette(Colors,SrcData%16);
putpixel(j,Height-1-i,data);
}
break;
case 2:
if(j%8==0)
{
fread(&SrcData,1,1,fp);
data=SetPalette(Colors,(SrcData>>7)%2);
putpixel(j,Height-1-i,data);
}
else
{
data=SetPalette(Colors,(SrcData>>(7-j%8))%2);
putpixel(j,Height-1-i,data);
}
}
}
}
getch();
CloseGraph();
}