1/*
2 * Copyright (C) 2013 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 java.util.Vector;
24
25public class ImageFilterRedEye extends ImageFilter {
26    private static final String LOGTAG = "ImageFilterRedEye";
27    FilterRedEyeRepresentation mParameters = new FilterRedEyeRepresentation();
28
29    public ImageFilterRedEye() {
30        mName = "Red Eye";
31    }
32
33    @Override
34    public FilterRepresentation getDefaultRepresentation() {
35        return new FilterRedEyeRepresentation();
36    }
37
38    public boolean isNil() {
39        return mParameters.isNil();
40    }
41
42    public Vector<FilterPoint> getCandidates() {
43        return mParameters.getCandidates();
44    }
45
46    public void clear() {
47        mParameters.clearCandidates();
48    }
49
50    native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, short[] matrix);
51
52    @Override
53    public void useRepresentation(FilterRepresentation representation) {
54        FilterRedEyeRepresentation parameters = (FilterRedEyeRepresentation) representation;
55        mParameters = parameters;
56    }
57
58    @Override
59    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
60        int w = bitmap.getWidth();
61        int h = bitmap.getHeight();
62        short[] rect = new short[4];
63
64        int size = mParameters.getNumberOfCandidates();
65        Matrix originalToScreen = getOriginalToScreenMatrix(w, h);
66        for (int i = 0; i < size; i++) {
67            RectF r = new RectF(((RedEyeCandidate) (mParameters.getCandidate(i))).mRect);
68            originalToScreen.mapRect(r);
69            if (r.intersect(0, 0, w, h)) {
70                rect[0] = (short) r.left;
71                rect[1] = (short) r.top;
72                rect[2] = (short) r.width();
73                rect[3] = (short) r.height();
74                nativeApplyFilter(bitmap, w, h, rect);
75            }
76        }
77        return bitmap;
78    }
79}
80