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 com.android.gallery3d.R;
     20 import com.android.gallery3d.filtershow.editors.BasicEditor;
     21 
     22 public class ImageFilterSharpen extends ImageFilterRS {
     23     private static final String SERIALIZATION_NAME = "SHARPEN";
     24     private static final String LOGTAG = "ImageFilterSharpen";
     25     private ScriptC_convolve3x3 mScript;
     26 
     27     private FilterBasicRepresentation mParameters;
     28 
     29     public ImageFilterSharpen() {
     30         mName = "Sharpen";
     31     }
     32 
     33     public FilterRepresentation getDefaultRepresentation() {
     34         FilterRepresentation representation = new FilterBasicRepresentation("Sharpen", 0, 0, 100);
     35         representation.setSerializationName(SERIALIZATION_NAME);
     36         representation.setShowParameterValue(true);
     37         representation.setFilterClass(ImageFilterSharpen.class);
     38         representation.setTextId(R.string.sharpness);
     39         representation.setOverlayId(R.drawable.filtershow_button_colors_sharpen);
     40         representation.setEditorId(BasicEditor.ID);
     41         representation.setSupportsPartialRendering(true);
     42         return representation;
     43     }
     44 
     45     public void useRepresentation(FilterRepresentation representation) {
     46         FilterBasicRepresentation parameters = (FilterBasicRepresentation) representation;
     47         mParameters = parameters;
     48     }
     49 
     50     @Override
     51     protected void resetAllocations() {
     52         // nothing to do
     53     }
     54 
     55     @Override
     56     public void resetScripts() {
     57         if (mScript != null) {
     58             mScript.destroy();
     59             mScript = null;
     60         }
     61     }
     62 
     63     @Override
     64     protected void createFilter(android.content.res.Resources res, float scaleFactor,
     65             int quality) {
     66         if (mScript == null) {
     67             mScript = new ScriptC_convolve3x3(getRenderScriptContext());
     68         }
     69     }
     70 
     71     private void computeKernel() {
     72         float scaleFactor = getEnvironment().getScaleFactor();
     73         float p1 = mParameters.getValue() * scaleFactor;
     74         float value = p1 / 100.0f;
     75         float f[] = new float[9];
     76         float p = value;
     77         f[0] = -p;
     78         f[1] = -p;
     79         f[2] = -p;
     80         f[3] = -p;
     81         f[4] = 8 * p + 1;
     82         f[5] = -p;
     83         f[6] = -p;
     84         f[7] = -p;
     85         f[8] = -p;
     86         mScript.set_gCoeffs(f);
     87     }
     88 
     89     @Override
     90     protected void bindScriptValues() {
     91         int w = getInPixelsAllocation().getType().getX();
     92         int h = getInPixelsAllocation().getType().getY();
     93         mScript.set_gWidth(w);
     94         mScript.set_gHeight(h);
     95     }
     96 
     97     @Override
     98     protected void runFilter() {
     99         if (mParameters == null) {
    100             return;
    101         }
    102         computeKernel();
    103         mScript.set_gIn(getInPixelsAllocation());
    104         mScript.bind_gPixels(getInPixelsAllocation());
    105         mScript.forEach_root(getInPixelsAllocation(), getOutPixelsAllocation());
    106     }
    107 
    108 }
    109