ImageFilterCurves.java revision 856acb652fad51d650723a890cac08c536363fcd
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 boolean hasDefaultRepresentation() { 32 return true; 33 } 34 35 @Override 36 public FilterRepresentation getDefaultRepresentation() { 37 FilterCurvesRepresentation representation = new FilterCurvesRepresentation(); 38 representation.setName("Draw"); 39 representation.setFilterClass(ImageFilterCurves.class); 40 return representation; 41 } 42 43 @Override 44 public void useRepresentation(FilterRepresentation representation) { 45 FilterCurvesRepresentation parameters = (FilterCurvesRepresentation) representation; 46 mParameters = parameters; 47 } 48 49 public ImageFilterCurves() { 50 mName = "Curves"; 51 reset(); 52 } 53 54 @Override 55 public int getButtonId() { 56 return R.id.curvesButtonRGB; 57 } 58 59 @Override 60 public int getTextId() { 61 return R.string.curvesRGB; 62 } 63 64 @Override 65 public int getOverlayBitmaps() { 66 return R.drawable.filtershow_button_colors_curve; 67 } 68 69 @Override 70 public int getEditingViewId() { 71 return R.id.imageCurves; 72 } 73 74 @Override 75 public boolean showParameterValue() { 76 return false; 77 } 78 79 public void populateArray(int[] array, int curveIndex) { 80 Spline spline = mParameters.getSpline(curveIndex); 81 if (spline == null) { 82 return; 83 } 84 float[] curve = spline.getAppliedCurve(); 85 for (int i = 0; i < 256; i++) { 86 array[i] = (int) (curve[i] * 255); 87 } 88 } 89 90 @Override 91 public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) { 92 if (!mParameters.getSpline(Spline.RGB).isOriginal()) { 93 int[] rgbGradient = new int[256]; 94 populateArray(rgbGradient, Spline.RGB); 95 nativeApplyGradientFilter(bitmap, bitmap.getWidth(), bitmap.getHeight(), 96 rgbGradient, rgbGradient, rgbGradient); 97 } 98 99 int[] redGradient = null; 100 if (!mParameters.getSpline(Spline.RED).isOriginal()) { 101 redGradient = new int[256]; 102 populateArray(redGradient, Spline.RED); 103 } 104 int[] greenGradient = null; 105 if (!mParameters.getSpline(Spline.GREEN).isOriginal()) { 106 greenGradient = new int[256]; 107 populateArray(greenGradient, Spline.GREEN); 108 } 109 int[] blueGradient = null; 110 if (!mParameters.getSpline(Spline.BLUE).isOriginal()) { 111 blueGradient = new int[256]; 112 populateArray(blueGradient, Spline.BLUE); 113 } 114 115 nativeApplyGradientFilter(bitmap, bitmap.getWidth(), bitmap.getHeight(), 116 redGradient, greenGradient, blueGradient); 117 return bitmap; 118 } 119 120 public void setSpline(Spline spline, int splineIndex) { 121 mParameters.setSpline(splineIndex, new Spline(spline)); 122 } 123 124 public Spline getSpline(int splineIndex) { 125 return mParameters.getSpline(splineIndex); 126 } 127 128 public void reset() { 129 Spline spline = new Spline(); 130 131 spline.addPoint(0.0f, 1.0f); 132 spline.addPoint(1.0f, 0.0f); 133 134 for (int i = 0; i < 4; i++) { 135 mParameters.setSpline(i, new Spline(spline)); 136 } 137 } 138 139 public void useFilter(ImageFilter a) { 140 ImageFilterCurves c = (ImageFilterCurves) a; 141 for (int i = 0; i < 4; i++) { 142 if (c.mParameters.getSpline(i) != null) { 143 setSpline(c.mParameters.getSpline(i), i); 144 } 145 } 146 } 147} 148