18921c28c7333ad2b4d34f013904ad4737044f366John Hoford/*
28921c28c7333ad2b4d34f013904ad4737044f366John Hoford * Copyright (C) 2013 The Android Open Source Project
38921c28c7333ad2b4d34f013904ad4737044f366John Hoford *
48921c28c7333ad2b4d34f013904ad4737044f366John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
58921c28c7333ad2b4d34f013904ad4737044f366John Hoford * you may not use this file except in compliance with the License.
68921c28c7333ad2b4d34f013904ad4737044f366John Hoford * You may obtain a copy of the License at
78921c28c7333ad2b4d34f013904ad4737044f366John Hoford *
88921c28c7333ad2b4d34f013904ad4737044f366John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
98921c28c7333ad2b4d34f013904ad4737044f366John Hoford *
108921c28c7333ad2b4d34f013904ad4737044f366John Hoford * Unless required by applicable law or agreed to in writing, software
118921c28c7333ad2b4d34f013904ad4737044f366John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
128921c28c7333ad2b4d34f013904ad4737044f366John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138921c28c7333ad2b4d34f013904ad4737044f366John Hoford * See the License for the specific language governing permissions and
148921c28c7333ad2b4d34f013904ad4737044f366John Hoford * limitations under the License.
158921c28c7333ad2b4d34f013904ad4737044f366John Hoford */
168921c28c7333ad2b4d34f013904ad4737044f366John Hoford
178921c28c7333ad2b4d34f013904ad4737044f366John Hofordpackage com.android.gallery3d.filtershow.filters;
188921c28c7333ad2b4d34f013904ad4737044f366John Hoford
198921c28c7333ad2b4d34f013904ad4737044f366John Hofordimport android.graphics.Bitmap;
2087835aa01412f9ba20eb57ac0cbbad3a19b48623John Hoford
2187835aa01412f9ba20eb57ac0cbbad3a19b48623John Hofordimport com.android.gallery3d.R;
228921c28c7333ad2b4d34f013904ad4737044f366John Hoford
238921c28c7333ad2b4d34f013904ad4737044f366John Hofordpublic class ImageFilterHighlights extends SimpleImageFilter {
24afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford    private static final String SERIALIZATION_NAME = "HIGHLIGHTS";
258921c28c7333ad2b4d34f013904ad4737044f366John Hoford    private static final String LOGTAG = "ImageFilterVignette";
268921c28c7333ad2b4d34f013904ad4737044f366John Hoford
278921c28c7333ad2b4d34f013904ad4737044f366John Hoford    public ImageFilterHighlights() {
288921c28c7333ad2b4d34f013904ad4737044f366John Hoford        mName = "Highlights";
298921c28c7333ad2b4d34f013904ad4737044f366John Hoford    }
308921c28c7333ad2b4d34f013904ad4737044f366John Hoford
318921c28c7333ad2b4d34f013904ad4737044f366John Hoford    SplineMath mSpline = new SplineMath(5);
328921c28c7333ad2b4d34f013904ad4737044f366John Hoford    double[] mHighlightCurve = { 0.0, 0.32, 0.418, 0.476, 0.642 };
338921c28c7333ad2b4d34f013904ad4737044f366John Hoford
348921c28c7333ad2b4d34f013904ad4737044f366John Hoford    public FilterRepresentation getDefaultRepresentation() {
358921c28c7333ad2b4d34f013904ad4737044f366John Hoford        FilterBasicRepresentation representation =
368921c28c7333ad2b4d34f013904ad4737044f366John Hoford                (FilterBasicRepresentation) super.getDefaultRepresentation();
37afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford        representation.setName("Highlights");
38afa8ed9d46e760d4b0c0331cfcb4bb49ef6ba280John Hoford        representation.setSerializationName(SERIALIZATION_NAME);
398921c28c7333ad2b4d34f013904ad4737044f366John Hoford        representation.setFilterClass(ImageFilterHighlights.class);
408921c28c7333ad2b4d34f013904ad4737044f366John Hoford        representation.setTextId(R.string.highlight_recovery);
418921c28c7333ad2b4d34f013904ad4737044f366John Hoford        representation.setMinimum(-100);
428921c28c7333ad2b4d34f013904ad4737044f366John Hoford        representation.setMaximum(100);
438921c28c7333ad2b4d34f013904ad4737044f366John Hoford        representation.setDefaultValue(0);
44e5498da0262fc836738aad63ea30286d8afa643cnicolasroard        representation.setSupportsPartialRendering(true);
458921c28c7333ad2b4d34f013904ad4737044f366John Hoford        return representation;
468921c28c7333ad2b4d34f013904ad4737044f366John Hoford    }
478921c28c7333ad2b4d34f013904ad4737044f366John Hoford
488921c28c7333ad2b4d34f013904ad4737044f366John Hoford    native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, float[] luminanceMap);
498921c28c7333ad2b4d34f013904ad4737044f366John Hoford
508921c28c7333ad2b4d34f013904ad4737044f366John Hoford    @Override
518921c28c7333ad2b4d34f013904ad4737044f366John Hoford    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
528921c28c7333ad2b4d34f013904ad4737044f366John Hoford        if (getParameters() == null) {
538921c28c7333ad2b4d34f013904ad4737044f366John Hoford            return bitmap;
548921c28c7333ad2b4d34f013904ad4737044f366John Hoford        }
558921c28c7333ad2b4d34f013904ad4737044f366John Hoford        float p = getParameters().getValue();
568921c28c7333ad2b4d34f013904ad4737044f366John Hoford        double t = p/100.;
578921c28c7333ad2b4d34f013904ad4737044f366John Hoford        for (int i = 0; i < 5; i++) {
588921c28c7333ad2b4d34f013904ad4737044f366John Hoford            double x = i / 4.;
598921c28c7333ad2b4d34f013904ad4737044f366John Hoford            double y = mHighlightCurve[i] *t+x*(1-t);
608921c28c7333ad2b4d34f013904ad4737044f366John Hoford            mSpline.setPoint(i, x, y);
618921c28c7333ad2b4d34f013904ad4737044f366John Hoford        }
628921c28c7333ad2b4d34f013904ad4737044f366John Hoford
638921c28c7333ad2b4d34f013904ad4737044f366John Hoford        float[][] curve = mSpline.calculatetCurve(256);
648921c28c7333ad2b4d34f013904ad4737044f366John Hoford        float[] luminanceMap = new float[curve.length];
658921c28c7333ad2b4d34f013904ad4737044f366John Hoford        for (int i = 0; i < luminanceMap.length; i++) {
668921c28c7333ad2b4d34f013904ad4737044f366John Hoford            luminanceMap[i] = curve[i][1];
678921c28c7333ad2b4d34f013904ad4737044f366John Hoford        }
688921c28c7333ad2b4d34f013904ad4737044f366John Hoford        int w = bitmap.getWidth();
698921c28c7333ad2b4d34f013904ad4737044f366John Hoford        int h = bitmap.getHeight();
708921c28c7333ad2b4d34f013904ad4737044f366John Hoford
718921c28c7333ad2b4d34f013904ad4737044f366John Hoford        nativeApplyFilter(bitmap, w, h, luminanceMap);
728921c28c7333ad2b4d34f013904ad4737044f366John Hoford        return bitmap;
738921c28c7333ad2b4d34f013904ad4737044f366John Hoford    }
748921c28c7333ad2b4d34f013904ad4737044f366John Hoford}
75