1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <stdint.h>
5#include <time.h>
6#include <math.h>
7#include "wgif.h"
8
9#define WIDTH80 80
10#define HEIGHT40 40
11
12
13int fwr(uint8_t *ptr, size_t bytes, void *wp) {
14 return fwrite(ptr, 1, bytes, wp);
15}
16
17int gbham_sgn(int x) {
18 if (x>0) {
19 return 1;
20 }
21 if (x<0) {
22 return -1;
23 }
24 return 0;
25}
26
27void gbham(uint8_t *bitmap, uint8_t colorid, int x0, int y0, int x1, int y1) {
28 int i;
29 int x, y;
30 int dx, dy;
31 int incx, incy;
32 int pdx, pdy, ddx, ddy;
33 int dfast, dslow;
34 int err;
35
36 dx=x1-x0;
37 dy=y1-y0;
38 incx=gbham_sgn(dx);
39 incy=gbham_sgn(dy);
40 if (dx<0) {
41 dx=-dx;
42 }
43 if (dy<0) {
44 dy=-dy;
45 }
46 if (dx>dy) {
47 pdx=incx;
48 pdy=0;
49 ddx=incx;
50 ddy=incy;
51 dslow=dy;
52 dfast=dx;
53
54 } else {
55 pdx=0;
56 pdy=incy;
57 ddx=incx;
58 ddy=incy;
59 dslow=dx;
60 dfast=dy;
61 }
62 x=x0;
63 y=y0;
64 err=dfast/2;
65 bitmap[x+y*WIDTH80]=colorid;
66 for (i=0;i<dfast;i++) {
67 err=err-dslow;
68 if (err<0) {
69 err=err+dfast;
70 x=x+ddx;
71 y=y+ddy;
72 } else {
73 x=x+pdx;
74 y=y+pdy;
75 }
76 bitmap[x+y*WIDTH80]=colorid;
77 }
78}
79
80void line(struct gif_file *gf, uint8_t *bitmap, int tx, int ty) {
81 uint32_t ct[2];
82
83 ct[0]=(rand()&0xfff)|((rand()&0xfff)<<12);
84 ct[1]=0;
85
86 memset(bitmap, 1, WIDTH80*HEIGHT40);
87 gif_graphicControlExtension(gf, 10, 1);
88 gbham(bitmap, 0, WIDTH80>>1, HEIGHT40>>1, tx, ty);
89 gif_image(gf, 0, 0, WIDTH80, HEIGHT40, ct, 2, bitmap);
90}
91
92int main() {
93 struct gif_file gf;
94 uint8_t bitmap[WIDTH80*HEIGHT40];
95 int i;
96
97 gf.wr_fnct=fwr;
98 gf.wparm=fopen("bresenham.gif", "wb");
99 srand(time(NULL((void *)0)));
100
101 gif_init(&gf, WIDTH80, HEIGHT40, NULL((void *)0), 0, 0);
102 gif_ext_loop(&gf, 0);
103 memset(bitmap, 0, WIDTH80*HEIGHT40);
104
105
106 for (i=0;i<WIDTH80;i++) {
107 line(&gf, bitmap, i, 0);
108 }
109
110 for (i=0;i<HEIGHT40;i++) {
111 line(&gf, bitmap, WIDTH80-1, i);
112 }
113
114 for (i=WIDTH80-1;i>=0;i--) {
115 line(&gf, bitmap, i, HEIGHT40-1);
116 }
117
118 for (i=HEIGHT40-1;i>=0;i--) {
119 line(&gf, bitmap, 0, i);
120 }
121 gif_end(&gf);
122 fclose(gf.wparm);
123
124 return 0;
125}