Home | History | Annotate | Download | only in filters
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.gallery3d.filtershow.filters;
     18 
     19 import android.graphics.Bitmap;
     20 import android.text.format.Time;
     21 
     22 import com.android.gallery3d.R;
     23 
     24 public class ImageFilterKMeans extends SimpleImageFilter {
     25     private int mSeed = 0;
     26 
     27     public ImageFilterKMeans() {
     28         mName = "KMeans";
     29 
     30         // set random seed for session
     31         Time t = new Time();
     32         t.setToNow();
     33         mSeed = (int) t.toMillis(false);
     34     }
     35 
     36     public FilterRepresentation getDefaultRepresentation() {
     37         FilterBasicRepresentation representation = (FilterBasicRepresentation) super.getDefaultRepresentation();
     38         representation.setName("KMeans");
     39         representation.setFilterClass(ImageFilterKMeans.class);
     40         representation.setMaximum(20);
     41         representation.setMinimum(2);
     42         representation.setValue(4);
     43         representation.setDefaultValue(4);
     44         representation.setPreviewValue(4);
     45         representation.setTextId(R.string.kmeans);
     46         representation.setButtonId(R.id.kmeansButton);
     47         representation.setSupportsPartialRendering(true);
     48         return representation;
     49     }
     50 
     51     native protected void nativeApplyFilter(Bitmap bitmap, int width, int height,
     52             Bitmap large_ds_bm, int lwidth, int lheight, Bitmap small_ds_bm,
     53             int swidth, int sheight, int p, int seed);
     54 
     55     @Override
     56     public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
     57         if (getParameters() == null) {
     58             return bitmap;
     59         }
     60         int w = bitmap.getWidth();
     61         int h = bitmap.getHeight();
     62 
     63         Bitmap large_bm_ds = bitmap;
     64         Bitmap small_bm_ds = bitmap;
     65 
     66         // find width/height for larger downsampled bitmap
     67         int lw = w;
     68         int lh = h;
     69         while (lw > 256 && lh > 256) {
     70             lw /= 2;
     71             lh /= 2;
     72         }
     73         if (lw != w) {
     74             large_bm_ds = Bitmap.createScaledBitmap(bitmap, lw, lh, true);
     75         }
     76 
     77         // find width/height for smaller downsampled bitmap
     78         int sw = lw;
     79         int sh = lh;
     80         while (sw > 64 && sh > 64) {
     81             sw /= 2;
     82             sh /= 2;
     83         }
     84         if (sw != lw) {
     85             small_bm_ds = Bitmap.createScaledBitmap(large_bm_ds, sw, sh, true);
     86         }
     87 
     88         if (getParameters() != null) {
     89             int p = Math.max(getParameters().getValue(), getParameters().getMinimum()) % (getParameters().getMaximum() + 1);
     90             nativeApplyFilter(bitmap, w, h, large_bm_ds, lw, lh, small_bm_ds, sw, sh, p, mSeed);
     91         }
     92         return bitmap;
     93     }
     94 }
     95