fixed bugs, added progress bar, and more
[dosdemo] / src / gfxutil.c
1 #include <string.h>
2 #include "gfxutil.h"
3 #include "demo.h"
4
5 enum {
6         IN              = 0,
7         LEFT    = 1,
8         RIGHT   = 2,
9         TOP             = 4,
10         BOTTOM  = 8
11 };
12
13 static int outcode(int x, int y, int xmin, int ymin, int xmax, int ymax)
14 {
15         int code = 0;
16
17         if(x < xmin) {
18                 code |= LEFT;
19         } else if(x > xmax) {
20                 code |= RIGHT;
21         }
22         if(y < ymin) {
23                 code |= TOP;
24         } else if(y > ymax) {
25                 code |= BOTTOM;
26         }
27         return code;
28 }
29
30 #define FIXMUL(a, b)    (((a) * (b)) >> 8)
31 #define FIXDIV(a, b)    (((a) << 8) / (b))
32
33 #define LERP(a, b, t)   ((a) + FIXMUL((b) - (a), (t)))
34
35 int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax, int ymax)
36 {
37         int oc_out;
38
39         int oc0 = outcode(*x0, *y0, xmin, ymin, xmax, ymax);
40         int oc1 = outcode(*x1, *y1, xmin, ymin, xmax, ymax);
41
42         long fx0, fy0, fx1, fy1, fxmin, fymin, fxmax, fymax;
43
44         if(!(oc0 | oc1)) return 1;      /* both points are inside */
45
46         fx0 = *x0 << 8;
47         fy0 = *y0 << 8;
48         fx1 = *x1 << 8;
49         fy1 = *y1 << 8;
50         fxmin = xmin << 8;
51         fymin = ymin << 8;
52         fxmax = xmax << 8;
53         fymax = ymax << 8;
54
55         for(;;) {
56                 long x, y, t;
57
58                 if(oc0 & oc1) return 0;         /* both have points with the same outbit, not visible */
59                 if(!(oc0 | oc1)) break;         /* both points are inside */
60
61                 oc_out = oc0 ? oc0 : oc1;
62
63                 if(oc_out & TOP) {
64                         t = FIXDIV(fymin - fy0, fy1 - fy0);
65                         x = LERP(fx0, fx1, t);
66                         y = fymin;
67                 } else if(oc_out & BOTTOM) {
68                         t = FIXDIV(fymax - fy0, fy1 - fy0);
69                         x = LERP(fx0, fx1, t);
70                         y = fymax;
71                 } else if(oc_out & LEFT) {
72                         t = FIXDIV(fxmin - fx0, fx1 - fx0);
73                         x = fxmin;
74                         y = LERP(fy0, fy1, t);
75                 } else if(oc_out & RIGHT) {
76                         t = FIXDIV(fxmax - fx0, fx1 - fx0);
77                         x = fxmax;
78                         y = LERP(fy0, fy1, t);
79                 }
80
81                 if(oc_out == oc0) {
82                         fx0 = x;
83                         fy0 = y;
84                         oc0 = outcode(fx0 >> 8, fy0 >> 8, xmin, ymin, xmax, ymax);
85                 } else {
86                         fx1 = x;
87                         fy1 = y;
88                         oc1 = outcode(fx1 >> 8, fy1 >> 8, xmin, ymin, xmax, ymax);
89                 }
90         }
91
92         *x0 = fx0 >> 8;
93         *y0 = fy0 >> 8;
94         *x1 = fx1 >> 8;
95         *y1 = fy1 >> 8;
96         return 1;
97 }
98
99 void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
100 {
101         int i, dx, dy, x_inc, y_inc, error;
102         unsigned short *fb = fb_pixels;
103
104         fb += y0 * fb_width + x0;
105
106         dx = x1 - x0;
107         dy = y1 - y0;
108
109         if(dx >= 0) {
110                 x_inc = 1;
111         } else {
112                 x_inc = -1;
113                 dx = -dx;
114         }
115         if(dy >= 0) {
116                 y_inc = fb_width;
117         } else {
118                 y_inc = -fb_width;
119                 dy = -dy;
120         }
121
122         if(dx > dy) {
123                 error = dy * 2 - dx;
124                 for(i=0; i<=dx; i++) {
125                         *fb = color;
126                         if(error >= 0) {
127                                 error -= dx * 2;
128                                 fb += y_inc;
129                         }
130                         error += dy * 2;
131                         fb += x_inc;
132                 }
133         } else {
134                 error = dx * 2 - dy;
135                 for(i=0; i<=dy; i++) {
136                         *fb = color;
137                         if(error >= 0) {
138                                 error -= dy * 2;
139                                 fb += x_inc;
140                         }
141                         error += dx * 2;
142                         fb += y_inc;
143                 }
144         }
145 }
146
147
148 #define BLUR(w, h, pstep, sstep) \
149         for(i=0; i<h; i++) { \
150                 int sum = sptr[0] * (rad + 1); \
151                 int count = (rad * 2 + 1) << 8; \
152                 int midsize = w - rad * 2; \
153                 int firstpix = sptr[0]; \
154                 int lastpix = sptr[pstep * (w - 1)]; \
155                 /* add up the contributions for the -1 pixel */ \
156                 for(j=0; j<rad; j++) { \
157                         sum += sptr[pstep * j]; \
158                 } \
159                 /* first part adding sptr[rad] and subtracting sptr[0] */ \
160                 for(j=0; j<=rad; j++) { \
161                         sum += (int)sptr[pstep * rad] - firstpix; \
162                         sptr += pstep; \
163                         *dptr = scale * sum / count; \
164                         dptr += pstep; \
165                 } \
166                 /* middle part adding sptr[rad] and subtracting sptr[-(rad+1)] */ \
167                 for(j=1; j<midsize; j++) { \
168                         sum += (int)sptr[pstep * rad] - (int)sptr[-(rad + 1) * pstep]; \
169                         sptr += pstep; \
170                         *dptr = scale * sum / count; \
171                         dptr += pstep; \
172                 } \
173                 /* last part adding lastpix and subtracting sptr[-(rad+1)] */ \
174                 for(j=0; j<rad; j++) { \
175                         sum += lastpix - (int)sptr[-(rad + 1) * pstep]; \
176                         sptr += pstep; \
177                         *dptr = scale * sum / count; \
178                         dptr += pstep; \
179                 } \
180                 sptr += sstep; \
181                 dptr += sstep; \
182         }
183
184 /* TODO bound blur rad to image size to avoid inner loop conditionals */
185 /* TODO make version with pow2 (rad*2+1) to avoid div with count everywhere */
186 void blur_grey_horiz(uint16_t *dest, uint16_t *src, int xsz, int ysz, int rad, int scale)
187 {
188         int i, j;
189         unsigned char *dptr = (unsigned char*)dest;
190         unsigned char *sptr = (unsigned char*)src;
191
192         BLUR(xsz, ysz, 2, 0);
193 }
194
195
196 void blur_grey_vert(uint16_t *dest, uint16_t *src, int xsz, int ysz, int rad, int scale)
197 {
198         int i, j;
199         unsigned char *dptr = (unsigned char*)dest;
200         unsigned char *sptr = (unsigned char*)src;
201         int pixel_step = xsz * 2;
202         int scanline_step = 2 - ysz * pixel_step;
203
204         BLUR(ysz, xsz, pixel_step, scanline_step);
205 }
206
207 void convimg_rgb24_rgb16(uint16_t *dest, unsigned char *src, int xsz, int ysz)
208 {
209         int i;
210         int npixels = xsz * ysz;
211
212         for(i=0; i<npixels; i++) {
213                 int r = *src++;
214                 int g = *src++;
215                 int b = *src++;
216                 *dest++ = PACK_RGB16(r, g, b);
217         }
218 }
219
220 void blitfb(uint16_t *dest, uint16_t *src, int width, int height, int pitch_pix)
221 {
222         int i;
223         for(i=0; i<height; i++) {
224                 memcpy(dest, src, width * 2);
225                 dest += 320;
226                 src += pitch_pix;
227         }
228 }
229
230 void blitfb_key(uint16_t *dest, uint16_t *src, int width, int height, int pitch_pix, uint16_t key)
231 {
232         int i, j, dadv = 320 - width;
233
234         for(i=0; i<height; i++) {
235                 for(j=0; j<width; j++) {
236                         uint16_t scol = *src++;
237                         if(scol != key) *dest = scol;
238                         dest++;
239                 }
240                 dest += dadv;
241         }
242
243 }