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