13 static int outcode(int x, int y, int xmin, int ymin, int xmax, int ymax)
30 #define FIXMUL(a, b) (((a) * (b)) >> 8)
31 #define FIXDIV(a, b) (((a) << 8) / (b))
33 #define LERP(a, b, t) ((a) + FIXMUL((b) - (a), (t)))
35 int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax, int ymax)
39 int oc0 = outcode(*x0, *y0, xmin, ymin, xmax, ymax);
40 int oc1 = outcode(*x1, *y1, xmin, ymin, xmax, ymax);
42 long fx0, fy0, fx1, fy1, fxmin, fymin, fxmax, fymax;
44 if(!(oc0 | oc1)) return 1; /* both points are inside */
58 if(oc0 & oc1) return 0; /* both have points with the same outbit, not visible */
59 if(!(oc0 | oc1)) break; /* both points are inside */
61 oc_out = oc0 ? oc0 : oc1;
64 t = FIXDIV(fymin - fy0, fy1 - fy0);
65 x = LERP(fx0, fx1, t);
67 } else if(oc_out & BOTTOM) {
68 t = FIXDIV(fymax - fy0, fy1 - fy0);
69 x = LERP(fx0, fx1, t);
71 } else if(oc_out & LEFT) {
72 t = FIXDIV(fxmin - fx0, fx1 - fx0);
74 y = LERP(fy0, fy1, t);
75 } else if(oc_out & RIGHT) {
76 t = FIXDIV(fxmax - fx0, fx1 - fx0);
78 y = LERP(fy0, fy1, t);
84 oc0 = outcode(fx0 >> 8, fy0 >> 8, xmin, ymin, xmax, ymax);
88 oc1 = outcode(fx1 >> 8, fy1 >> 8, xmin, ymin, xmax, ymax);
99 void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
101 int i, dx, dy, x_inc, y_inc, error;
102 unsigned short *fb = fb_pixels;
104 fb += y0 * fb_width + x0;
124 for(i=0; i<=dx; i++) {
135 for(i=0; i<=dy; i++) {
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]; \
159 /* first part adding sptr[rad] and subtracting sptr[0] */ \
160 for(j=0; j<=rad; j++) { \
161 sum += (int)sptr[pstep * rad] - firstpix; \
163 *dptr = scale * sum / count; \
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]; \
170 *dptr = scale * sum / count; \
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]; \
177 *dptr = scale * sum / count; \
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)
189 unsigned char *dptr = (unsigned char*)dest;
190 unsigned char *sptr = (unsigned char*)src;
192 BLUR(xsz, ysz, 2, 0);
196 void blur_grey_vert(uint16_t *dest, uint16_t *src, int xsz, int ysz, int rad, int scale)
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;
204 BLUR(ysz, xsz, pixel_step, scanline_step);
207 void convimg_rgb24_rgb16(uint16_t *dest, unsigned char *src, int xsz, int ysz)
210 int npixels = xsz * ysz;
212 for(i=0; i<npixels; i++) {
216 *dest++ = PACK_RGB16(r, g, b);
220 void blitfb(uint16_t *dest, uint16_t *src, int width, int height, int pitch_pix)
223 for(i=0; i<height; i++) {
224 memcpy(dest, src, width * 2);
230 void blitfb_key(uint16_t *dest, uint16_t *src, int width, int height, int pitch_pix, uint16_t key)
232 int i, j, dadv = 320 - width;
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;