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