OSDN Git Service

Merge commit '8539fc1a5406af9edd5414fba3615cc045aa63ec' into jb-mr2-release
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / filters / ImageFilterSharpen.java
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
21 public class ImageFilterSharpen extends ImageFilterRS {
22
23     private static final String LOGTAG = "ImageFilterSharpen";
24     private ScriptC_convolve3x3 mScript;
25     float mScaleFactor;
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.setShowParameterValue(true);
36         representation.setFilterClass(ImageFilterSharpen.class);
37         representation.setTextId(R.string.sharpness);
38         representation.setButtonId(R.id.sharpenButton);
39         representation.setOverlayId(R.drawable.filtershow_button_colors_sharpen);
40         representation.setEditorId(R.id.imageZoom);
41         return representation;
42     }
43
44     public void useRepresentation(FilterRepresentation representation) {
45         FilterBasicRepresentation parameters = (FilterBasicRepresentation) representation;
46         mParameters = parameters;
47     }
48
49     @Override
50     public void createFilter(android.content.res.Resources res, float scaleFactor,
51             int quality) {
52         int w = mInPixelsAllocation.getType().getX();
53         int h = mInPixelsAllocation.getType().getY();
54         mScaleFactor = scaleFactor;
55
56         if (mScript == null) {
57             mScript = new ScriptC_convolve3x3(getRenderScriptContext(), res, R.raw.convolve3x3);
58         }
59         mScript.set_gWidth(w);
60         mScript.set_gHeight(h);
61     }
62
63     private void computeKernel(){
64         float p1 = mParameters.getValue() * mScaleFactor;
65         float value = p1 / 100.0f;
66         float f[] = new float[9];
67         float p = value;
68         f[0] = -p;
69         f[1] = -p;
70         f[2] = -p;
71         f[3] = -p;
72         f[4] = 8 * p + 1;
73         f[5] = -p;
74         f[6] = -p;
75         f[7] = -p;
76         f[8] = -p;
77         mScript.set_gCoeffs(f);
78     }
79
80     @Override
81     public void runFilter() {
82         if (mParameters == null) {
83             return;
84         }
85         computeKernel();
86         mScript.set_gIn(mInPixelsAllocation);
87         mScript.bind_gPixels(mInPixelsAllocation);
88         mScript.forEach_root(mInPixelsAllocation, mOutPixelsAllocation);
89     }
90
91 }