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 android.graphics.Bitmap;
20
21import com.android.gallery3d.filtershow.imageshow.Spline;
22
23public class ImageFilterCurves extends ImageFilter {
24
25    private static final String LOGTAG = "ImageFilterCurves";
26    FilterCurvesRepresentation mParameters = new FilterCurvesRepresentation();
27
28    @Override
29    public FilterRepresentation getDefaultRepresentation() {
30        return new FilterCurvesRepresentation();
31    }
32
33    @Override
34    public void useRepresentation(FilterRepresentation representation) {
35        FilterCurvesRepresentation parameters = (FilterCurvesRepresentation) representation;
36        mParameters = parameters;
37    }
38
39    public ImageFilterCurves() {
40        mName = "Curves";
41        reset();
42    }
43
44    public void populateArray(int[] array, int curveIndex) {
45        Spline spline = mParameters.getSpline(curveIndex);
46        if (spline == null) {
47            return;
48        }
49        float[] curve = spline.getAppliedCurve();
50        for (int i = 0; i < 256; i++) {
51            array[i] = (int) (curve[i] * 255);
52        }
53    }
54
55    @Override
56    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
57        if (!mParameters.getSpline(Spline.RGB).isOriginal()) {
58            int[] rgbGradient = new int[256];
59            populateArray(rgbGradient, Spline.RGB);
60            nativeApplyGradientFilter(bitmap, bitmap.getWidth(), bitmap.getHeight(),
61                    rgbGradient, rgbGradient, rgbGradient);
62        }
63
64        int[] redGradient = null;
65        if (!mParameters.getSpline(Spline.RED).isOriginal()) {
66            redGradient = new int[256];
67            populateArray(redGradient, Spline.RED);
68        }
69        int[] greenGradient = null;
70        if (!mParameters.getSpline(Spline.GREEN).isOriginal()) {
71            greenGradient = new int[256];
72            populateArray(greenGradient, Spline.GREEN);
73        }
74        int[] blueGradient = null;
75        if (!mParameters.getSpline(Spline.BLUE).isOriginal()) {
76            blueGradient = new int[256];
77            populateArray(blueGradient, Spline.BLUE);
78        }
79
80        nativeApplyGradientFilter(bitmap, bitmap.getWidth(), bitmap.getHeight(),
81                redGradient, greenGradient, blueGradient);
82        return bitmap;
83    }
84
85    public void setSpline(Spline spline, int splineIndex) {
86        mParameters.setSpline(splineIndex, new Spline(spline));
87    }
88
89    public Spline getSpline(int splineIndex) {
90        return mParameters.getSpline(splineIndex);
91    }
92
93    public void reset() {
94        Spline spline = new Spline();
95
96        spline.addPoint(0.0f, 1.0f);
97        spline.addPoint(1.0f, 0.0f);
98
99        for (int i = 0; i < 4; i++) {
100            mParameters.setSpline(i, new Spline(spline));
101        }
102    }
103
104    public void useFilter(ImageFilter a) {
105        ImageFilterCurves c = (ImageFilterCurves) a;
106        for (int i = 0; i < 4; i++) {
107            if (c.mParameters.getSpline(i) != null) {
108                setSpline(c.mParameters.getSpline(i), i);
109            }
110        }
111    }
112}
113