ImageFilterRedEye.java revision 99baf61387ab1ef15bb9db5fa3b2b55591e87059
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.graphics.Matrix;
21import android.graphics.RectF;
22
23import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
24
25import java.util.Vector;
26
27public class ImageFilterRedEye extends ImageFilter {
28    private static final String LOGTAG = "ImageFilterRedEye";
29    FilterRedEyeRepresentation mParameters = new FilterRedEyeRepresentation();
30
31    public ImageFilterRedEye() {
32        mName = "Red Eye";
33    }
34
35    @Override
36    public FilterRepresentation getDefaultRepresentation() {
37        FilterRedEyeRepresentation representation = new FilterRedEyeRepresentation();
38
39        return representation;
40    }
41
42    public boolean isNil() {
43        if (mParameters.getCandidates() != null && mParameters.getCandidates().size() > 0) {
44            return false;
45        }
46        return true;
47    }
48
49    public Vector<RedEyeCandidate> getCandidates() {
50        if (!mParameters.hasCandidates()) {
51            mParameters.setCandidates(new Vector<RedEyeCandidate>());
52        }
53        return mParameters.getCandidates();
54    }
55
56    public void clear() {
57        if (!mParameters.hasCandidates()) {
58            mParameters.setCandidates(new Vector<RedEyeCandidate>());
59        }
60        mParameters.clearCandidates();
61    }
62
63    native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, short[] matrix);
64
65    @Override
66    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
67        int w = bitmap.getWidth();
68        int h = bitmap.getHeight();
69        short[] rect = new short[4];
70        int size = mParameters.getNumberOfCandidates();
71
72        for (int i = 0; i < size; i++) {
73            RectF r = new RectF(mParameters.getCandidate(i).mRect);
74            GeometryMetadata geo = getImagePreset().mGeoData;
75            Matrix originalToScreen = geo.getOriginalToScreen(true,
76                    getImagePreset().getImageLoader().getOriginalBounds().width(),
77                    getImagePreset().getImageLoader().getOriginalBounds().height(),
78                    w, h);
79            originalToScreen.mapRect(r);
80            if (r.left < 0) {
81                r.left = 0;
82            }
83            if (r.left > w) {
84                r.left = w;
85            }
86            if (r.top < 0) {
87                r.top = 0;
88            }
89            if (r.top > h) {
90                r.top = h;
91            }
92            if (r.right < 0) {
93                r.right = 0;
94            }
95            if (r.right > w) {
96                r.right = w;
97            }
98            if (r.bottom < 0) {
99                r.bottom = 0;
100            }
101            if (r.bottom > h) {
102                r.bottom = h;
103            }
104            rect[0] = (short) r.left;
105            rect[1] = (short) r.top;
106            rect[2] = (short) r.width();
107            rect[3] = (short) r.height();
108            nativeApplyFilter(bitmap, w, h, rect);
109        }
110
111        return bitmap;
112    }
113}
114