adding fast blur function
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Fri, 9 Feb 2018 01:03:47 +0000 (03:03 +0200)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Fri, 9 Feb 2018 01:03:47 +0000 (03:03 +0200)
src/fract.c
src/gfxutil.c
src/gfxutil.h
src/greets.c

index a17550b..e082620 100644 (file)
@@ -43,9 +43,6 @@ static void destroy(void)
 {
 }
 
 {
 }
 
-#define PACK_RGB16(r, g, b) \
-       (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
-
 static void draw(void)
 {
        int i, j;
 static void draw(void)
 {
        int i, j;
index 0257f79..811915e 100644 (file)
@@ -142,3 +142,47 @@ void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
                }
        }
 }
                }
        }
 }
+
+
+/* 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 i, j;
+
+       for(i=0; i<ysz; i++) {
+               int sum = src[0] * (rad + 1);
+               int count = rad * 2 + 1;
+               uint16_t lastpix = src[xsz - 1];
+
+               /* add up the contributions for the -1 pixel */
+               for(j=0; j<rad; j++) {
+                       sum += src[j];
+               }
+
+               /* first part adding src[rad] and subtracting src[0] */
+               for(j=0; j<rad; j++) {
+                       sum += src[rad] - src[0];
+                       ++src;
+                       *dest++ = sum / count;
+               }
+
+               /* middle part adding src[rad] and subtracting src[-(rad+1)] */
+               for(j=0; j<xsz - rad; j++) {
+                       sum += src[rad] - src[-(rad + 1)];
+                       ++src;
+                       *dest++ = sum / count;
+               }
+
+               /* last part adding lastpix and subtracting src[-(rad+1)] */
+               for(j=0; j<rad; j++) {
+                       sum += lastpix - src[-(rad + 1)];
+                       ++src;
+                       *dest++ = sum / count;
+               }
+       }
+}
+
+void blur_grey_vert(uint16_t *dest, uint16_t *src, int xsz, int ysz, int radius)
+{
+}
index 603c11d..cf64487 100644 (file)
@@ -1,8 +1,12 @@
 #ifndef GFXUTIL_H_
 #define GFXUTIL_H_
 
 #ifndef GFXUTIL_H_
 #define GFXUTIL_H_
 
+#include "inttypes.h"
+
 #define PACK_RGB16(r, g, b) \
 #define PACK_RGB16(r, g, b) \
-       (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
+       (((((uint16_t)(r) >> 3) & 0x1f) << 11) | \
+        ((((uint16_t)(g) >> 2) & 0x3f) << 5) | \
+        (((uint16_t)(b) >> 3) & 0x1f))
 
 #define UNPACK_R16(c)  (((c) >> 8) & 0xf8)
 #define UNPACK_G16(c)  (((c) >> 3) & 0xfc)
 
 #define UNPACK_R16(c)  (((c) >> 8) & 0xf8)
 #define UNPACK_G16(c)  (((c) >> 3) & 0xfc)
@@ -14,4 +18,7 @@
 int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax, int ymax);
 void draw_line(int x0, int y0, int x1, int y1, unsigned short color);
 
 int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax, int ymax);
 void draw_line(int x0, int y0, int x1, int y1, unsigned short color);
 
+void blur_grey_horiz(uint16_t *dest, uint16_t *src, int xsz, int ysz, int radius);
+void blur_grey_vert(uint16_t *dest, uint16_t *src, int xsz, int ysz, int radius);
+
 #endif /* GFXUTIL_H_ */
 #endif /* GFXUTIL_H_ */
index 0876b60..9fa14df 100644 (file)
@@ -30,7 +30,7 @@ struct vec3 {
 struct particle {
        float x, y, z;
        float vx, vy, vz;       /* velocity */
 struct particle {
        float x, y, z;
        float vx, vy, vz;       /* velocity */
-       float r, g, b;
+       int r, g, b;
        float life;
 };
 
        float life;
 };
 
@@ -203,7 +203,9 @@ int init_emitter(struct emitter *em, int num, unsigned char *map, int xsz, int y
                p->x = (float)x / (float)xsz - 0.5;
                p->y = -(float)y / (float)xsz + 0.5 / aspect;
                p->z = ((float)i / (float)num * 2.0 - 1.0) * 0.005;
                p->x = (float)x / (float)xsz - 0.5;
                p->y = -(float)y / (float)xsz + 0.5 / aspect;
                p->z = ((float)i / (float)num * 2.0 - 1.0) * 0.005;
-               p->r = p->g = p->b = 0.9;
+               p->r = 0;
+               p->g = 0x1f;
+               p->b = 255;
                p->vx = p->vy = p->vz = 0.0f;
                p->life = MAX_LIFE;
                ++p;
                p->vx = p->vy = p->vz = 0.0f;
                p->life = MAX_LIFE;
                ++p;
@@ -233,7 +235,9 @@ void update_particles(struct emitter *em, float dt)
                v->y = p->y;
                v->z = p->z;
                v->w = 1.0f;
                v->y = p->y;
                v->z = p->z;
                v->w = 1.0f;
-               v->r = v->g = v->b = 200;
+               v->r = p->r;
+               v->g = p->g;
+               v->b = p->b;
                v->a = cround64(p->life * 255.0 / MAX_LIFE);
                ++v;
 
                v->a = cround64(p->life * 255.0 / MAX_LIFE);
                ++v;