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