12 static int outcode(int x, int y, int xmin, int ymin, int xmax, int ymax)
29 #define FIXMUL(a, b) (((a) * (b)) >> 8)
30 #define FIXDIV(a, b) (((a) << 8) / (b))
32 #define LERP(a, b, t) ((a) + FIXMUL((b) - (a), (t)))
34 int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax, int ymax)
38 int oc0 = outcode(*x0, *y0, xmin, ymin, xmax, ymax);
39 int oc1 = outcode(*x1, *y1, xmin, ymin, xmax, ymax);
41 long fx0, fy0, fx1, fy1, fxmin, fymin, fxmax, fymax;
43 if(!(oc0 | oc1)) return 1; /* both points are inside */
57 if(oc0 & oc1) return 0; /* both have points with the same outbit, not visible */
58 if(!(oc0 | oc1)) break; /* both points are inside */
60 oc_out = oc0 ? oc0 : oc1;
63 t = FIXDIV(fymin - fy0, fy1 - fy0);
64 x = LERP(fx0, fx1, t);
66 } else if(oc_out & BOTTOM) {
67 t = FIXDIV(fymax - fy0, fy1 - fy0);
68 x = LERP(fx0, fx1, t);
70 } else if(oc_out & LEFT) {
71 t = FIXDIV(fxmin - fx0, fx1 - fx0);
73 y = LERP(fy0, fy1, t);
74 } else if(oc_out & RIGHT) {
75 t = FIXDIV(fxmax - fx0, fx1 - fx0);
77 y = LERP(fy0, fy1, t);
83 oc0 = outcode(fx0 >> 8, fy0 >> 8, xmin, ymin, xmax, ymax);
87 oc1 = outcode(fx1 >> 8, fy1 >> 8, xmin, ymin, xmax, ymax);
98 void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
100 int i, dx, dy, x_inc, y_inc, error;
101 unsigned short *fb = fb_pixels;
103 fb += y0 * fb_width + x0;
123 for(i=0; i<=dx; i++) {
134 for(i=0; i<=dy; i++) {
147 #define BLUR(w, h, pstep, sstep) \
148 for(i=0; i<h; i++) { \
149 int sum = sptr[0] * (rad + 1); \
150 int count = (rad * 2 + 1) << 8; \
151 int midsize = w - rad * 2; \
152 int firstpix = sptr[0]; \
153 int lastpix = sptr[pstep * (w - 1)]; \
154 /* add up the contributions for the -1 pixel */ \
155 for(j=0; j<rad; j++) { \
156 sum += sptr[pstep * j]; \
158 /* first part adding sptr[rad] and subtracting sptr[0] */ \
159 for(j=0; j<=rad; j++) { \
160 sum += (int)sptr[pstep * rad] - firstpix; \
162 *dptr = scale * sum / count; \
165 /* middle part adding sptr[rad] and subtracting sptr[-(rad+1)] */ \
166 for(j=1; j<midsize; j++) { \
167 sum += (int)sptr[pstep * rad] - (int)sptr[-(rad + 1) * pstep]; \
169 *dptr = scale * sum / count; \
172 /* last part adding lastpix and subtracting sptr[-(rad+1)] */ \
173 for(j=0; j<rad; j++) { \
174 sum += lastpix - (int)sptr[-(rad + 1) * pstep]; \
176 *dptr = scale * sum / count; \
183 /* TODO bound blur rad to image size to avoid inner loop conditionals */
184 /* TODO make version with pow2 (rad*2+1) to avoid div with count everywhere */
185 void blur_grey_horiz(uint16_t *dest, uint16_t *src, int xsz, int ysz, int rad, int scale)
188 unsigned char *dptr = (unsigned char*)dest;
189 unsigned char *sptr = (unsigned char*)src;
191 BLUR(xsz, ysz, 2, 0);
195 void blur_grey_vert(uint16_t *dest, uint16_t *src, int xsz, int ysz, int rad, int scale)
198 unsigned char *dptr = (unsigned char*)dest;
199 unsigned char *sptr = (unsigned char*)src;
200 int pixel_step = xsz * 2;
201 int scanline_step = 2 - ysz * pixel_step;
203 BLUR(ysz, xsz, pixel_step, scanline_step);
206 void convimg_rgb24_rgb16(uint16_t *dest, unsigned char *src, int xsz, int ysz)
209 int npixels = xsz * ysz;
211 for(i=0; i<npixels; i++) {
215 *dest++ = PACK_RGB16(r, g, b);