From 4c54808f10fa647c4dc8f95551eb369172535d50 Mon Sep 17 00:00:00 2001 From: John Hoford Date: Sat, 16 Feb 2013 09:20:59 -0800 Subject: add movable vignette Change-Id: I54f2fccd0d748ca4c908d5b0f3c3ef7349cd686d --- jni/filters/vignette.c | 44 ++++++++++++-------------------------------- 1 file changed, 12 insertions(+), 32 deletions(-) (limited to 'jni') diff --git a/jni/filters/vignette.c b/jni/filters/vignette.c index 2799ff001..b9ee3ff01 100644 --- a/jni/filters/vignette.c +++ b/jni/filters/vignette.c @@ -15,52 +15,32 @@ */ #include "filters.h" +#include static int* gVignetteMap = 0; static int gVignetteWidth = 0; static int gVignetteHeight = 0; -__inline__ void createVignetteMap(int w, int h) -{ - if (gVignetteMap && (gVignetteWidth != w || gVignetteHeight != h)) - { - free(gVignetteMap); - gVignetteMap = 0; - } - if (gVignetteMap == 0) - { - gVignetteWidth = w; - gVignetteHeight = h; - - int cx = w / 2; - int cy = h / 2; - int i, j; - - gVignetteMap = malloc(w * h * sizeof(int)); - float maxDistance = cx * cx * 2.0f; - for (i = 0; i < w; i++) - { - for (j = 0; j < h; j++) - { - float distance = (cx - i) * (cx - i) + (cy - j) * (cy - j); - gVignetteMap[j * w + i] = (int) (distance / maxDistance * 255); - } - } - } -} - -void JNIFUNCF(ImageFilterVignette, nativeApplyFilter, jobject bitmap, jint width, jint height, jfloat strength) +void JNIFUNCF(ImageFilterVignette, nativeApplyFilter, jobject bitmap, jint width, jint height, jint centerx, jint centery, jfloat radiusx, jfloat radiusy, jfloat strength) { char* destination = 0; AndroidBitmap_lockPixels(env, bitmap, (void**) &destination); - createVignetteMap(width, height); int i; int len = width * height * 4; int vignette = 0; + float d = centerx; + if (radiusx == 0) radiusx = 10; + if (radiusy == 0) radiusy = 10; + float scalex = 1/radiusx; + float scaley = 1/radiusy; for (i = 0; i < len; i += 4) { - vignette = (int) (strength * gVignetteMap[i / 4]); + int p = i/4; + float x = ((p%width)-centerx)*scalex; + float y = ((p/width)-centery)*scaley; + float dist = sqrt(x*x+y*y)-1; + vignette = (int) (strength*256*MAX(dist,0)); destination[RED] = CLAMP(destination[RED] - vignette); destination[GREEN] = CLAMP(destination[GREEN] - vignette); destination[BLUE] = CLAMP(destination[BLUE] - vignette); -- cgit v1.2.3