minor performance improvements, optional mouse, mouse cursor now drawn
[dosdemo] / src / gfxutil.c
index 0257f79..259b56d 100644 (file)
@@ -1,3 +1,4 @@
+#include <string.h>
 #include "gfxutil.h"
 #include "demo.h"
 
@@ -100,7 +101,7 @@ void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
        int i, dx, dy, x_inc, y_inc, error;
        unsigned short *fb = fb_pixels;
 
-       fb += y0 * fb_width + x0;
+       fb += y0 * FB_WIDTH + x0;
 
        dx = x1 - x0;
        dy = y1 - y0;
@@ -112,9 +113,9 @@ void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
                dx = -dx;
        }
        if(dy >= 0) {
-               y_inc = fb_width;
+               y_inc = FB_WIDTH;
        } else {
-               y_inc = -fb_width;
+               y_inc = -FB_WIDTH;
                dy = -dy;
        }
 
@@ -142,3 +143,101 @@ void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
                }
        }
 }
+
+
+#define BLUR(w, h, pstep, sstep) \
+       for(i=0; i<h; i++) { \
+               int sum = sptr[0] * (rad + 1); \
+               int count = (rad * 2 + 1) << 8; \
+               int midsize = w - rad * 2; \
+               int firstpix = sptr[0]; \
+               int lastpix = sptr[pstep * (w - 1)]; \
+               /* add up the contributions for the -1 pixel */ \
+               for(j=0; j<rad; j++) { \
+                       sum += sptr[pstep * j]; \
+               } \
+               /* first part adding sptr[rad] and subtracting sptr[0] */ \
+               for(j=0; j<=rad; j++) { \
+                       sum += (int)sptr[pstep * rad] - firstpix; \
+                       sptr += pstep; \
+                       *dptr = scale * sum / count; \
+                       dptr += pstep; \
+               } \
+               /* middle part adding sptr[rad] and subtracting sptr[-(rad+1)] */ \
+               for(j=1; j<midsize; j++) { \
+                       sum += (int)sptr[pstep * rad] - (int)sptr[-(rad + 1) * pstep]; \
+                       sptr += pstep; \
+                       *dptr = scale * sum / count; \
+                       dptr += pstep; \
+               } \
+               /* last part adding lastpix and subtracting sptr[-(rad+1)] */ \
+               for(j=0; j<rad; j++) { \
+                       sum += lastpix - (int)sptr[-(rad + 1) * pstep]; \
+                       sptr += pstep; \
+                       *dptr = scale * sum / count; \
+                       dptr += pstep; \
+               } \
+               sptr += sstep; \
+               dptr += sstep; \
+       }
+
+/* TODO bound blur rad to image size to avoid inner loop conditionals */
+/* TODO make version with pow2 (rad*2+1) to avoid div with count everywhere */
+void blur_grey_horiz(uint16_t *dest, uint16_t *src, int xsz, int ysz, int rad, int scale)
+{
+       int i, j;
+       unsigned char *dptr = (unsigned char*)dest;
+       unsigned char *sptr = (unsigned char*)src;
+
+       BLUR(xsz, ysz, 2, 0);
+}
+
+
+void blur_grey_vert(uint16_t *dest, uint16_t *src, int xsz, int ysz, int rad, int scale)
+{
+       int i, j;
+       unsigned char *dptr = (unsigned char*)dest;
+       unsigned char *sptr = (unsigned char*)src;
+       int pixel_step = xsz * 2;
+       int scanline_step = 2 - ysz * pixel_step;
+
+       BLUR(ysz, xsz, pixel_step, scanline_step);
+}
+
+void convimg_rgb24_rgb16(uint16_t *dest, unsigned char *src, int xsz, int ysz)
+{
+       int i;
+       int npixels = xsz * ysz;
+
+       for(i=0; i<npixels; i++) {
+               int r = *src++;
+               int g = *src++;
+               int b = *src++;
+               *dest++ = PACK_RGB16(r, g, b);
+       }
+}
+
+void blitfb(uint16_t *dest, uint16_t *src, int width, int height, int pitch_pix)
+{
+       int i;
+       for(i=0; i<height; i++) {
+               memcpy(dest, src, width * 2);
+               dest += 320;
+               src += pitch_pix;
+       }
+}
+
+void blitfb_key(uint16_t *dest, uint16_t *src, int width, int height, int pitch_pix, uint16_t key)
+{
+       int i, j, dadv = 320 - width;
+
+       for(i=0; i<height; i++) {
+               for(j=0; j<width; j++) {
+                       uint16_t scol = *src++;
+                       if(scol != key) *dest = scol;
+                       dest++;
+               }
+               dest += dadv;
+       }
+
+}