OSDN Git Service

add filters
authorJohn Hoford <hoford@google.com>
Tue, 9 Oct 2012 23:17:17 +0000 (16:17 -0700)
committerJohn Hoford <hoford@google.com>
Wed, 10 Oct 2012 00:18:26 +0000 (17:18 -0700)
bug:7234321
Change-Id: I4c9695c3ad90fcb7f4d67d40faa0a7da85f99030

15 files changed:
jni/Android.mk
jni/filters/fx.c [new file with mode: 0644]
res/drawable/filtershow_fx_0000_vintage.png [new file with mode: 0644]
res/drawable/filtershow_fx_0001_instant.png [new file with mode: 0644]
res/drawable/filtershow_fx_0002_bleach.png [new file with mode: 0644]
res/drawable/filtershow_fx_0003_blue_crush.png [new file with mode: 0644]
res/drawable/filtershow_fx_0004_bw_contrast.png [new file with mode: 0644]
res/drawable/filtershow_fx_0005_punch.png [new file with mode: 0644]
res/drawable/filtershow_fx_0006_x_process.png [new file with mode: 0644]
res/drawable/filtershow_fx_0007_washout.png [new file with mode: 0644]
res/drawable/filtershow_fx_0008_washout_color.png [new file with mode: 0644]
res/values/strings.xml
src/com/android/gallery3d/filtershow/FilterShowActivity.java
src/com/android/gallery3d/filtershow/filters/ImageFilterFx.java [new file with mode: 0644]
src/com/android/gallery3d/filtershow/presets/ImagePresetFX.java [new file with mode: 0644]

index 92789c1..f2f714d 100644 (file)
@@ -36,6 +36,7 @@ LOCAL_SRC_FILES := filters/bw.c \
                    filters/geometry.c \
                    filters/vignette.c \
                    filters/redEyeMath.c \
+                   filters/fx.c \
                    filters/redeye.c
 
 LOCAL_CFLAGS    += -ffast-math -O3 -funroll-loops
diff --git a/jni/filters/fx.c b/jni/filters/fx.c
new file mode 100644 (file)
index 0000000..24fa5e0
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "filters.h"
+
+__inline__ int  interp(unsigned char  *src, int p , int *off ,float dr,float dg, float db){
+
+    float fr00 = (src[p+off[0]])*(1-dr)+(src[p+off[1]])*dr;
+    float fr01 = (src[p+off[2]])*(1-dr)+(src[p+off[3]])*dr;
+    float fr10 = (src[p+off[4]])*(1-dr)+(src[p+off[5]])*dr;
+    float fr11 = (src[p+off[6]])*(1-dr)+(src[p+off[7]])*dr;
+    float frb0 = fr00 * (1-db)+fr01*db;
+    float frb1 = fr10 * (1-db)+fr11*db;
+    float frbg = frb0 * (1-dg)+frb1*dg;
+
+    return (int)frbg ;
+}
+
+void JNIFUNCF(ImageFilterFx, nativeApplyFilter, jobject bitmap, jint width, jint height, jobject lutbitmap,jint lutwidth, jint lutheight )
+{
+    char* destination = 0;
+    char* lut = 0;
+    AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
+    AndroidBitmap_lockPixels(env, lutbitmap, (void**) &lut);
+    unsigned char * rgb = (unsigned char * )destination;
+    unsigned char * lutrgb = (unsigned char * )lut;
+    int lutdim_r   = lutheight;
+    int lutdim_g   = lutheight;;
+    int lutdim_b   = lutwidth/lutheight;;
+    int STEP = 4;
+
+    int off[8] =  {
+            0,
+            STEP*1,
+            STEP*lutdim_r,
+            STEP*(lutdim_r + 1),
+            STEP*(lutdim_r*lutdim_b),
+            STEP*(lutdim_r*lutdim_b+1),
+            STEP*(lutdim_r*lutdim_b+lutdim_r),
+            STEP*(lutdim_r*lutdim_b+lutdim_r + 1)
+    };
+
+    float scale_R = (lutdim_r-1.f)/256.f;
+    float scale_G = (lutdim_g-1.f)/256.f;
+    float scale_B = (lutdim_b-1.f)/256.f;
+
+    int i;
+    int len = width * height * STEP;
+
+    for (i = 0; i < len; i+=STEP)
+    {
+        int r = rgb[RED];
+        int g = rgb[GREEN];
+        int b = rgb[BLUE];
+
+        float fb = b*scale_B;
+        float fg = g*scale_G;
+        float fr = r*scale_R;
+        int lut_b = (int)fb;
+        int lut_g = (int)fg;
+        int lut_r = (int)fr;
+        int p = lut_r+lut_b*lutdim_r+lut_g*lutdim_r*lutdim_b;
+        p*=STEP;
+        float dr = fr-lut_r;
+        float dg = fg-lut_g;
+        float db = fb-lut_b;
+        rgb[RED]   = clamp(interp(lutrgb,p  ,off,dr,dg,db));
+        rgb[GREEN] = clamp(interp(lutrgb,p+1,off,dr,dg,db));
+        rgb[BLUE]  = clamp(interp(lutrgb,p+2,off,dr,dg,db));
+
+    }
+
+    AndroidBitmap_unlockPixels(env, bitmap);
+    AndroidBitmap_unlockPixels(env, lutbitmap);
+}
diff --git a/res/drawable/filtershow_fx_0000_vintage.png b/res/drawable/filtershow_fx_0000_vintage.png
new file mode 100644 (file)
index 0000000..6783bb6
Binary files /dev/null and b/res/drawable/filtershow_fx_0000_vintage.png differ
diff --git a/res/drawable/filtershow_fx_0001_instant.png b/res/drawable/filtershow_fx_0001_instant.png
new file mode 100644 (file)
index 0000000..1652a4b
Binary files /dev/null and b/res/drawable/filtershow_fx_0001_instant.png differ
diff --git a/res/drawable/filtershow_fx_0002_bleach.png b/res/drawable/filtershow_fx_0002_bleach.png
new file mode 100644 (file)
index 0000000..afb8a82
Binary files /dev/null and b/res/drawable/filtershow_fx_0002_bleach.png differ
diff --git a/res/drawable/filtershow_fx_0003_blue_crush.png b/res/drawable/filtershow_fx_0003_blue_crush.png
new file mode 100644 (file)
index 0000000..2b238e3
Binary files /dev/null and b/res/drawable/filtershow_fx_0003_blue_crush.png differ
diff --git a/res/drawable/filtershow_fx_0004_bw_contrast.png b/res/drawable/filtershow_fx_0004_bw_contrast.png
new file mode 100644 (file)
index 0000000..40eb397
Binary files /dev/null and b/res/drawable/filtershow_fx_0004_bw_contrast.png differ
diff --git a/res/drawable/filtershow_fx_0005_punch.png b/res/drawable/filtershow_fx_0005_punch.png
new file mode 100644 (file)
index 0000000..e7e0803
Binary files /dev/null and b/res/drawable/filtershow_fx_0005_punch.png differ
diff --git a/res/drawable/filtershow_fx_0006_x_process.png b/res/drawable/filtershow_fx_0006_x_process.png
new file mode 100644 (file)
index 0000000..5de9bb4
Binary files /dev/null and b/res/drawable/filtershow_fx_0006_x_process.png differ
diff --git a/res/drawable/filtershow_fx_0007_washout.png b/res/drawable/filtershow_fx_0007_washout.png
new file mode 100644 (file)
index 0000000..20dfb5e
Binary files /dev/null and b/res/drawable/filtershow_fx_0007_washout.png differ
diff --git a/res/drawable/filtershow_fx_0008_washout_color.png b/res/drawable/filtershow_fx_0008_washout_color.png
new file mode 100644 (file)
index 0000000..bb6602b
Binary files /dev/null and b/res/drawable/filtershow_fx_0008_washout_color.png differ
index 67ba40d..c6ef704 100644 (file)
     <!-- String indicating camera flash is not used. [CHAR LIMIT=14] -->
     <string name="flash_off">No flash</string>
 
+    <!-- String for filter filtershow_fx_0000_vintage [CHAR LIMIT=14] -->
+    <string name="ffx_vintage">Vintage</string>
+    <!-- String for filter filtershow_fx_0001_instant [CHAR LIMIT=14] -->
+    <string name="ffx_instant">Instant</string>
+    <!-- String for filter filtershow_fx_0002_bleach [CHAR LIMIT=14] -->
+    <string name="ffx_bleach">Bleach</string>
+    <!-- String for filter filtershow_fx_0003_blue_crush [CHAR LIMIT=14] -->
+    <string name="ffx_blue_crush">Blue Crush</string>
+    <!-- String for filter filtershow_fx_0004_bw_contrast [CHAR LIMIT=14] -->
+    <string name="ffx_bw_contrast">BW Contrast</string>
+    <!-- String for filter filtershow_fx_0005_punch [CHAR LIMIT=14] -->
+    <string name="ffx_punch">Punch</string>
+    <!-- String for filter filtershow_fx_0006_x_process [CHAR LIMIT=14] -->
+    <string name="ffx_x_process">X Process</string>
+    <!-- String for filter filtershow_fx_0007_washout [CHAR LIMIT=14] -->
+    <string name="ffx_washout">Washout</string>
+    <!-- String for filter filtershow_fx_0008_washout_color [CHAR LIMIT=14] -->
+    <string name="ffx_washout_color">Washout Color</string>
 
     <!-- Toast message shown after we make some album(s) available offline [CHAR LIMIT=50] -->
     <plurals name="make_albums_available_offline">
index f2fb3da..1cd5fde 100644 (file)
@@ -6,9 +6,13 @@ import android.app.ActionBar;
 import android.app.Activity;
 import android.app.ProgressDialog;
 import android.content.ContentValues;
+import android.content.Context;
 import android.content.Intent;
 import android.content.res.Resources;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 import android.graphics.Color;
+import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
 import android.os.Bundle;
@@ -33,6 +37,7 @@ import com.android.gallery3d.R;
 import com.android.gallery3d.filtershow.cache.ImageLoader;
 import com.android.gallery3d.filtershow.filters.ImageFilter;
 import com.android.gallery3d.filtershow.filters.ImageFilterBorder;
+import com.android.gallery3d.filtershow.filters.ImageFilterFx;
 import com.android.gallery3d.filtershow.filters.ImageFilterParametricBorder;
 import com.android.gallery3d.filtershow.filters.ImageFilterRS;
 import com.android.gallery3d.filtershow.imageshow.ImageBorder;
@@ -48,6 +53,7 @@ import com.android.gallery3d.filtershow.presets.ImagePresetBW;
 import com.android.gallery3d.filtershow.presets.ImagePresetBWBlue;
 import com.android.gallery3d.filtershow.presets.ImagePresetBWGreen;
 import com.android.gallery3d.filtershow.presets.ImagePresetBWRed;
+import com.android.gallery3d.filtershow.presets.ImagePresetFX;
 import com.android.gallery3d.filtershow.presets.ImagePresetOld;
 import com.android.gallery3d.filtershow.presets.ImagePresetSaturated;
 import com.android.gallery3d.filtershow.presets.ImagePresetXProcessing;
@@ -390,8 +396,38 @@ public class FilterShowActivity extends Activity implements OnItemClickListener,
     private void fillListImages(LinearLayout listFilters) {
         // TODO: use listview
         // TODO: load the filters straight from the filesystem
-        ImagePreset[] preset = new ImagePreset[9];
+        ImagePreset[] preset = new ImagePreset[18];
         int p = 0;
+
+        int[]drawid = {
+                R.drawable.filtershow_fx_0000_vintage,
+                R.drawable.filtershow_fx_0001_instant,
+                R.drawable.filtershow_fx_0002_bleach,
+                R.drawable.filtershow_fx_0003_blue_crush,
+                R.drawable.filtershow_fx_0004_bw_contrast,
+                R.drawable.filtershow_fx_0005_punch,
+                R.drawable.filtershow_fx_0006_x_process,
+                R.drawable.filtershow_fx_0007_washout,
+                R.drawable.filtershow_fx_0008_washout_color
+        };
+
+        int[]fxNameid = {
+                R.string.ffx_vintage,
+                R.string.ffx_instant,
+                R.string.ffx_bleach,
+                R.string.ffx_blue_crush,
+                R.string.ffx_bw_contrast,
+                R.string.ffx_punch,
+                R.string.ffx_x_process,
+                R.string.ffx_washout,
+                R.string.ffx_washout_color,
+        };
+
+        for (int i = 0; i < drawid.length; i++) {
+            Bitmap b =  BitmapFactory.decodeResource(getResources(),drawid[i]);
+            preset[p++] = new ImagePresetFX(b, getString(fxNameid[i]));
+        }
+
         preset[p++] = new ImagePreset();
         preset[p++] = new ImagePresetSaturated();
         preset[p++] = new ImagePresetOld();
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterFx.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterFx.java
new file mode 100644 (file)
index 0000000..00fd20c
--- /dev/null
@@ -0,0 +1,41 @@
+
+package com.android.gallery3d.filtershow.filters;
+
+import android.graphics.Bitmap;
+import android.graphics.drawable.Drawable;
+import android.util.Log;
+
+import com.android.gallery3d.R;
+
+import java.util.Arrays;
+
+public class ImageFilterFx extends ImageFilter {
+    private static final String TAG = "ImageFilterFx";
+    Bitmap fxBitmap;
+
+    public ImageFilterFx(Bitmap fxBitmap) {
+        mName = "fx";
+        this.fxBitmap = fxBitmap;
+    }
+
+    @Override
+    public ImageFilter clone() throws CloneNotSupportedException {
+        ImageFilterFx filter = (ImageFilterFx) super.clone();
+        filter.fxBitmap = this.fxBitmap;
+        return filter;
+    }
+
+    native protected void nativeApplyFilter(Bitmap bitmap, int w, int h,Bitmap  fxBitmap, int fxw, int fxh);
+
+    public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
+
+        int w = bitmap.getWidth();
+        int h = bitmap.getHeight();
+
+        int fxw = fxBitmap.getWidth();
+        int fxh = fxBitmap.getHeight();
+
+        nativeApplyFilter(bitmap, w, h,   fxBitmap,  fxw,  fxh);
+        return bitmap;
+    }
+}
diff --git a/src/com/android/gallery3d/filtershow/presets/ImagePresetFX.java b/src/com/android/gallery3d/filtershow/presets/ImagePresetFX.java
new file mode 100644 (file)
index 0000000..326471e
--- /dev/null
@@ -0,0 +1,29 @@
+
+package com.android.gallery3d.filtershow.presets;
+
+import android.graphics.Bitmap;
+
+import com.android.gallery3d.filtershow.filters.ImageFilterBWRed;
+import com.android.gallery3d.filtershow.filters.ImageFilterFx;
+
+public class ImagePresetFX extends ImagePreset {
+    String name;
+    Bitmap fxBitmap;
+
+    public String name() {
+        return name;
+    }
+
+    public ImagePresetFX(Bitmap bitmap, String name) {
+        fxBitmap = bitmap;
+        this.name = name;
+        setup();
+    }
+
+    public void setup() {
+        if (fxBitmap != null) {
+            mFilters.add(new ImageFilterFx(fxBitmap));
+        }
+    }
+
+}