+
+
+#define BLUR(w, h, pstep, sstep) \
+ for(i=0; i<h; i++) { \
+ int r, g, b; \
+ int rsum = UNPACK_R16(sptr[0]) * (rad + 1); \
+ int gsum = UNPACK_G16(sptr[0]) * (rad + 1); \
+ int bsum = UNPACK_B16(sptr[0]) * (rad + 1); \
+ int count = (rad * 2 + 1) << 8; \
+ int midsize = w - rad * 2; \
+ int rfirstpix = UNPACK_R16(sptr[0]); \
+ int rlastpix = UNPACK_R16(sptr[pstep * (w - 1)]); \
+ int gfirstpix = UNPACK_G16(sptr[0]); \
+ int glastpix = UNPACK_G16(sptr[pstep * (w - 1)]); \
+ int bfirstpix = UNPACK_B16(sptr[0]); \
+ int blastpix = UNPACK_B16(sptr[pstep * (w - 1)]); \
+ /* add up the contributions for the -1 pixel */ \
+ for(j=0; j<rad; j++) { \
+ rsum += UNPACK_R16(sptr[pstep * j]); \
+ gsum += UNPACK_G16(sptr[pstep * j]); \
+ bsum += UNPACK_B16(sptr[pstep * j]); \
+ } \
+ /* first part adding sptr[rad] and subtracting sptr[0] */ \
+ for(j=0; j<=rad; j++) { \
+ rsum += UNPACK_R16((int)sptr[pstep * rad]) - rfirstpix; \
+ gsum += UNPACK_G16((int)sptr[pstep * rad]) - gfirstpix; \
+ bsum += UNPACK_B16((int)sptr[pstep * rad]) - bfirstpix; \
+ sptr += pstep; \
+ r = scale * rsum / count; \
+ g = scale * gsum / count; \
+ b = scale * bsum / count; \
+ *dptr = PACK_RGB16(r, g, b); \
+ dptr += pstep; \
+ } \
+ /* middle part adding sptr[rad] and subtracting sptr[-(rad+1)] */ \
+ for(j=1; j<midsize; j++) { \
+ rsum += UNPACK_R16((int)sptr[pstep * rad]) - UNPACK_R16((int)sptr[-(rad + 1) * pstep]); \
+ gsum += UNPACK_G16((int)sptr[pstep * rad]) - UNPACK_G16((int)sptr[-(rad + 1) * pstep]); \
+ bsum += UNPACK_B16((int)sptr[pstep * rad]) - UNPACK_B16((int)sptr[-(rad + 1) * pstep]); \
+ sptr += pstep; \
+ r = scale * rsum / count; \
+ g = scale * gsum / count; \
+ b = scale * bsum / count; \
+ *dptr = PACK_RGB16(r, g, b); \
+ dptr += pstep; \
+ } \
+ /* last part adding lastpix and subtracting sptr[-(rad+1)] */ \
+ for(j=0; j<rad; j++) { \
+ rsum += rlastpix - UNPACK_R16((int)sptr[-(rad + 1) * pstep]); \
+ gsum += glastpix - UNPACK_G16((int)sptr[-(rad + 1) * pstep]); \
+ bsum += blastpix - UNPACK_B16((int)sptr[-(rad + 1) * pstep]); \
+ sptr += pstep; \
+ r = scale * rsum / count; \
+ g = scale * gsum / count; \
+ b = scale * bsum / count; \
+ *dptr = PACK_RGB16(r, g, b); \
+ 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_horiz(uint16_t *dest, uint16_t *src, int xsz, int ysz, int rad, int scale)
+{
+ int i, j;
+ uint16_t *dptr = dest;
+ uint16_t *sptr = src;
+
+ BLUR(xsz, ysz, 1, 0);
+}
+
+
+void blur_vert(uint16_t *dest, uint16_t *src, int xsz, int ysz, int rad, int scale)
+{
+ int i, j;
+ uint16_t *dptr = dest;
+ uint16_t *sptr = src;
+ int pixel_step = xsz;
+ int scanline_step = 1 - 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 << 1);
+ dest += 320;
+ src += pitch_pix;
+ }
+}
+
+void blit(uint16_t *dest, int destwidth, uint16_t *src, int width, int height, int pitch_pix)
+{
+ int i, spansz = width << 1;
+ for(i=0; i<height; i++) {
+ memcpy(dest, src, spansz);
+ dest += destwidth;
+ src += pitch_pix;
+ }
+}
+
+void blit_key(uint16_t *dest, int destwidth, uint16_t *src, int width, int height, int pitch_pix, uint16_t key)
+{
+ int i, j;
+ int dadv = destwidth - width;
+ int sadv = pitch_pix - 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;
+ src += sadv;
+ }
+
+}