-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.c
340 lines (286 loc) · 8.93 KB
/
draw.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "main.h"
extern int fb;
extern int * ptr;
int clearCanvas(Canvas * canvas, int color){
int x, y;
for(x=0; x<canvas->width; x++){
for(y=0; y<canvas->height; y++){
drawPoint(canvas, x, y, color);
}
}
}
int drawPoint(Canvas * canvas, int x, int y, int color){
if(x < 0 | x >= canvas->width | y < 0 | y >= canvas->height){
// printf("DrawPoint: Out of canvas \n");
return -1;
}
*(canvas->p + canvas->width * y + x) = color;
}
int drawRect(Canvas * canvas, int sx, int sy, int width, int height, int color){
if(sx < 0) sx = 0;
if(sy < 0) sy = 0;
int tx, ty;
tx = sx + width;
ty = sy + height;
if(tx > canvas->width) tx = canvas->width;
if(ty > canvas->height) ty = canvas->height;
int x,y;
for(x=sx; x<tx; x++){
for(y=sy; y<ty; y++){
drawPoint(canvas, x, y, color);
}
}
return 1;
}
int drawCircle(Canvas * canvas, int sx, int sy, int r, int color){
int x, y;
for(x=(sx-r>0?sx-r:0); x < (sx+r<canvas->width ? sx+r: canvas->width); x++){
for(y=(sy-r>0?sy-r:0); y<(sy+r<canvas->height ? sy+r: canvas->height); y++){
if(pointsDistance(sx, sy, x, y) < r){
drawPoint(canvas, x, y, color);
}
}
}
}
int drawTaiji(Canvas * canvas, int sx, int sy, int r){
// 创建一块缓冲区
Canvas buf;
buf.height = 2*r;
buf.width = 2*r;
buf.p = malloc(4 * r * r * sizeof(int));
clearCanvas(&buf, -1);
int x, y;
for(x=0; x<2*r; x++){
for(y=0; y<2*r; y++){
if(pointsDistance(r, r, x, y) < r){
if(x<r){
drawPoint(&buf, x, y, 0x00000000);
}
else{
drawPoint(&buf, x, y, 0x00ffffff);
}
}
}
}
drawCircle(&buf, r, r/2, r/2, 0x00000000);
drawCircle(&buf, r, r/2, r/6, 0x00ffffff);
drawCircle(&buf, r, r+(r/2), r/2, 0x00ffffff);
drawCircle(&buf, r, r+(r/2), r/6, 0x00000000);
buflash(canvas, &buf, sx-r, sy-r);
free(buf.p);
return 0;
}
int buflash(Canvas * canvas, Canvas * buf, int sx, int sy){
int x, y;
int color;
for(y=0; y<buf->height; y++){
for(x=0; x<buf->width; x++){
color = *(buf->p + buf->width * y + x);
if(color == -1) continue;
drawPoint(canvas, sx+x, sy+y, color);
}
}
return 1;
}
Canvas * getBmpImg(char * path){
BmpImg * img = openBmpImg(path);
Canvas * image = malloc(sizeof(Canvas));
image->width = img->width;
image->height = img->height;
image->p = malloc(image->width * image->height * sizeof(int));
int x, y;
int bufSize = 0;
// 将图片保存到Canvas
for(y=0; y<image->height; y++){
for(x=0; x<image->width; x++){
char * cp = (img->p + (img->depth/8) * (y * img->width + x));
int color = 0;
color = *cp | *(cp+1) << 8 | *(cp+2) << 16;
*(image->p + (image->width * image->height) - (image->width * y + (image->width) - x)) = color;
bufSize ++;
}
}
free(img);
return image;
}
int showBmpImg(Canvas * canvas, BmpImg * img, int sx, int sy){
Canvas image;
image.width = img->width;
image.height = img->height;
image.p = malloc(image.width * image.height * sizeof(int));
int x, y;
int bufSize = 0;
// 将图片保存到Canvas
for(y=0; y<image.height; y++){
for(x=0; x<image.width; x++){
char * cp = (img->p + (img->depth/8) * (y * img->width + x));
int color = 0;
color = *cp | *(cp+1) << 8 | *(cp+2) << 16;
*(image.p + (image.width * image.height) - (image.width * y + (image.width) - x)) = color;
bufSize ++;
}
}
buflash(canvas, &image, sx, sy);
}
int drawAnimeMove(Canvas * canvas, Canvas buf, Anime anime){
//计算x,y方向的速度
int xd = anime.tx - anime.sx;
int yd = anime.ty - anime.sy;
int step = 100/anime.speed;
// 计算xy方向的速度
int xs = xd/step;
int ys = yd/step;
int x = anime.sx;
int y = anime.sy;
int i;
for(i=0; i<step; i++){
buflash(canvas, &buf, x, y);
usleep(40000);
drawRect(canvas, x, y, buf.width, buf.height, 0x00ffffff);
x = x+xs;
y = y+ys;
}
}
int drawAnimeGrad(Canvas * canvas, Canvas buf, Anime anime){
int step = anime.speed;
int i;
for(i=1; i<100; i+=step){
printf("%d \n", i);
Canvas newBuf;
newBuf.width = buf.width;
newBuf.height = buf.height;
newBuf.p = malloc(newBuf.height*newBuf.width*4);
int x, y;
for(y=0; y < newBuf.height; y++){
for(x=0; x<newBuf.width; x++){
int color = buf.p[buf.width*y + x];
int newColor = 0x00000000;
char * c = (char*)&color;
unsigned char r = *(c+2);
unsigned char g = *(c+1);
unsigned char b = *(c);
r = 0xff + (float)(r - 0xff) * (100.00/i);
g = 0xff + (float)(g - 0xff) * (100.00/i);
b = 0xff + (float)(b - 0xff) * (100.00/i);
newColor = r << 16 | g << 8 | b;
newBuf.p[buf.width*y + x] = newColor;
}
}
buflash(canvas, &newBuf, anime.sx, anime.sy);
free(newBuf.p);
}
}
int drawChar(Canvas * canvas, int sx, int sy, char c, int width, int height, int color){
// 获取字符的字形
CharFont font = getCharFont(c);
// 创建一个字符Canvas
Canvas charCanvas;
charCanvas.width = width;
charCanvas.height = height;
charCanvas.p = malloc(width*height*sizeof(int));
// 初始化内容
clearCanvas(&charCanvas, -1);
// 绘制字符
// 计算字符每个像素的像素宽度
int pixSize;
int wpixSize = charCanvas.width / font.width; // 宽度能取得的最大像素
int hpixSize = charCanvas.height / font.height; // 高度能取得的最大像素
pixSize = wpixSize > hpixSize ? hpixSize : wpixSize;
// 计算外边距
int xmargin = (charCanvas.width - font.width * pixSize) / 2;
int ymargin = (charCanvas.height - font.height * pixSize) / 2;
// 绘制字符
int x, y;
for(y=0; y<font.height; y++){
for(x=0; x<font.width; x++){
if(*(font.p + y*font.width + x) != 0){
drawRect(&charCanvas, xmargin + pixSize * x, ymargin + pixSize * y, pixSize, pixSize, color);
}
}
}
buflash(canvas, &charCanvas, sx, sy);
free(charCanvas.p);
free(font.p);
return 1;
}
int drawString(Canvas * canvas, int x, int y, char * str, int size, int color){
// 计算字符串的长度
int length = strlen(str);
// 设置一个字符间距
int space = 0.5 * size;
//绘制每一个字符
int i;
for(i=0; i<length; i++){
drawChar(canvas, x + space*i, y, *str, space, size, color);
str++;
}
return 1;
}
int *saveAs(Canvas *canvas){
// 计算填充字节大小
int filler;
if((canvas->width * 3)%4 == 0){
filler = 0;
}
else{
filler = 4 - ( (canvas->width * 3) % 4 );
}
// 初始化数据头
BMIHeader *bmiHeader = malloc(sizeof(BMIHeader));
bmiHeader->biSize = 40;
bmiHeader->biWidth = canvas->width;
bmiHeader->biHeight = canvas->height;
bmiHeader->biPlanes = 1;
bmiHeader->biBitCount = 24;
bmiHeader->biCompression = 0;
bmiHeader->biSize = (canvas->width*3 + filler) * canvas->height;
bmiHeader->biXPelsPerMeter = 0;
bmiHeader->biYPelsPerMeter = 0;
bmiHeader->biClrUsed = 0;
bmiHeader->biClrImportant = 0;
// 写入图片数据
char * biData = malloc(bmiHeader->biSize);
int p = 0;
int x,y;
for(y=canvas->height; y>0; y--){
for(x=0; x<canvas->width; x++){
int color = *(canvas->p + x + canvas->width * y);
unsigned char red = color>>16;
unsigned char green = color>>8;
unsigned char blue = color;
biData[p++] = blue;
biData[p++] = green;
biData[p++] = red;
}
int j;
for(j=0; j<filler; j++){
biData[p++] = 0x00;
}
}
// 初始化文件头
BMFHeader bmfHeader;
bmfHeader.bfType = 0x4d42;
bmfHeader.bfSize = 40 + 14 + bmiHeader->biSize;
bmfHeader.bfReserved1 = 0;
bmfHeader.bfReserved2 = 0;
bmfHeader.bfOffBits = 14;
printf("saveAs: BMFHeader %d, BMIHeader %d,", sizeof(BMFHeader), sizeof(BMIHeader));
// 保存为图片文件
FILE *fb = fopen("ScreenShot.bmp", "wb");
// 写入文件头
fwrite(&bmfHeader, 2, 1, fb);
fwrite(&bmfHeader.bfSize, 4, 1, fb);
fwrite(&bmfHeader.bfReserved1, 2, 1, fb);
fwrite(&bmfHeader.bfReserved2, 2, 1, fb);
fwrite(&bmfHeader.bfOffBits, 4, 1, fb);
fwrite(bmiHeader, 40, 1, fb);
fwrite(biData, bmiHeader->biSize, 1, fb);
fseek(fb, 14, 0);
fputc(40, fb);
fputc(0, fb);
fputc(0, fb);
fputc(0, fb);
fclose(fb);
free(biData);
printf("saveAs: Image Saved \n");
}