1/*
2 * Copyright (C) 2013 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.R;
22
23public class ImageFilterHighlights extends SimpleImageFilter {
24    private static final String SERIALIZATION_NAME = "HIGHLIGHTS";
25    private static final String LOGTAG = "ImageFilterVignette";
26
27    public ImageFilterHighlights() {
28        mName = "Highlights";
29    }
30
31    SplineMath mSpline = new SplineMath(5);
32    double[] mHighlightCurve = { 0.0, 0.32, 0.418, 0.476, 0.642 };
33
34    public FilterRepresentation getDefaultRepresentation() {
35        FilterBasicRepresentation representation =
36                (FilterBasicRepresentation) super.getDefaultRepresentation();
37        representation.setName("Highlights");
38        representation.setSerializationName(SERIALIZATION_NAME);
39        representation.setFilterClass(ImageFilterHighlights.class);
40        representation.setTextId(R.string.highlight_recovery);
41        representation.setMinimum(-100);
42        representation.setMaximum(100);
43        representation.setDefaultValue(0);
44        representation.setSupportsPartialRendering(true);
45        return representation;
46    }
47
48    native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, float[] luminanceMap);
49
50    @Override
51    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
52        if (getParameters() == null) {
53            return bitmap;
54        }
55        float p = getParameters().getValue();
56        double t = p/100.;
57        for (int i = 0; i < 5; i++) {
58            double x = i / 4.;
59            double y = mHighlightCurve[i] *t+x*(1-t);
60            mSpline.setPoint(i, x, y);
61        }
62
63        float[][] curve = mSpline.calculatetCurve(256);
64        float[] luminanceMap = new float[curve.length];
65        for (int i = 0; i < luminanceMap.length; i++) {
66            luminanceMap[i] = curve[i][1];
67        }
68        int w = bitmap.getWidth();
69        int h = bitmap.getHeight();
70
71        nativeApplyFilter(bitmap, w, h, luminanceMap);
72        return bitmap;
73    }
74}
75