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