2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.filtershow.filters;
19 import android.graphics.Bitmap;
20 import android.graphics.Matrix;
21 import android.graphics.RectF;
23 import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
25 import java.util.Vector;
27 public class ImageFilterRedEye extends ImageFilter {
28 private static final String LOGTAG = "ImageFilterRedEye";
29 private Vector<RedEyeCandidate> mCandidates = null;
31 public ImageFilterRedEye() {
36 public ImageFilter clone() throws CloneNotSupportedException {
37 ImageFilterRedEye filter = (ImageFilterRedEye) super.clone();
38 if (mCandidates != null) {
39 int size = mCandidates.size();
40 filter.mCandidates = new Vector<RedEyeCandidate>();
41 for (int i = 0; i < size; i++) {
42 filter.mCandidates.add(new RedEyeCandidate(mCandidates.elementAt(i)));
49 public boolean isNil() {
50 if (mCandidates != null && mCandidates.size() > 0) {
57 public boolean same(ImageFilter filter) {
58 boolean isRedEyeFilter = super.same(filter);
59 if (!isRedEyeFilter) {
62 ImageFilterRedEye redEyeFilter = (ImageFilterRedEye) filter;
63 if (redEyeFilter.mCandidates == null && mCandidates == null) {
66 if (redEyeFilter.mCandidates == null || mCandidates == null) {
69 if (redEyeFilter.mCandidates.size() != mCandidates.size()) {
72 int size = mCandidates.size();
73 for (int i = 0; i < size; i++) {
74 RedEyeCandidate c1 = mCandidates.elementAt(i);
75 RedEyeCandidate c2 = redEyeFilter.mCandidates.elementAt(i);
83 public Vector<RedEyeCandidate> getCandidates() {
84 if (mCandidates == null) {
85 mCandidates = new Vector<RedEyeCandidate>();
90 public void addRect(RectF rect, RectF bounds) {
91 if (mCandidates == null) {
92 mCandidates = new Vector<RedEyeCandidate>();
94 Vector<RedEyeCandidate> intersects = new Vector<RedEyeCandidate>();
95 for (int i = 0; i < mCandidates.size(); i++) {
96 RedEyeCandidate r = mCandidates.elementAt(i);
97 if (r.intersect(rect)) {
101 for (int i = 0; i < intersects.size(); i++) {
102 RedEyeCandidate r = intersects.elementAt(i);
104 bounds.union(r.mBounds);
105 mCandidates.remove(r);
107 mCandidates.add(new RedEyeCandidate(rect, bounds));
110 public void clear() {
111 if (mCandidates == null) {
112 mCandidates = new Vector<RedEyeCandidate>();
117 native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, short[] matrix);
120 public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
121 int w = bitmap.getWidth();
122 int h = bitmap.getHeight();
123 short[] rect = new short[4];
125 if (mCandidates != null && mCandidates.size() > 0) {
126 for (int i = 0; i < mCandidates.size(); i++) {
127 RectF r = new RectF(mCandidates.elementAt(i).mRect);
128 GeometryMetadata geo = getImagePreset().mGeoData;
129 Matrix originalToScreen = geo.getOriginalToScreen(true,
130 getImagePreset().getImageLoader().getOriginalBounds().width(),
131 getImagePreset().getImageLoader().getOriginalBounds().height(),
133 originalToScreen.mapRect(r);
158 rect[0] = (short) r.left;
159 rect[1] = (short) r.top;
160 rect[2] = (short) r.width();
161 rect[3] = (short) r.height();
162 nativeApplyFilter(bitmap, w, h, rect);