1a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard/*
212017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford * Copyright (C) 2013 The Android Open Source Project
3a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard *
4a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * Licensed under the Apache License, Version 2.0 (the "License");
5a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * you may not use this file except in compliance with the License.
6a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * You may obtain a copy of the License at
7a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard *
8a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard *      http://www.apache.org/licenses/LICENSE-2.0
9a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard *
10a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * Unless required by applicable law or agreed to in writing, software
11a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * distributed under the License is distributed on an "AS IS" BASIS,
12a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * See the License for the specific language governing permissions and
14a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * limitations under the License.
15a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard */
1690b1d251973bfa748d435896fc277cb4024451adJohn Hoford
1790b1d251973bfa748d435896fc277cb4024451adJohn Hofordpackage com.android.gallery3d.filtershow.filters;
1890b1d251973bfa748d435896fc277cb4024451adJohn Hoford
1990b1d251973bfa748d435896fc277cb4024451adJohn Hofordimport android.graphics.Bitmap;
20cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport android.graphics.Matrix;
21cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport android.graphics.RectF;
2290b1d251973bfa748d435896fc277cb4024451adJohn Hoford
23cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport java.util.Vector;
2490b1d251973bfa748d435896fc277cb4024451adJohn Hoford
25cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardpublic class ImageFilterRedEye extends ImageFilter {
26cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    private static final String LOGTAG = "ImageFilterRedEye";
2712c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford    FilterRedEyeRepresentation mParameters = new FilterRedEyeRepresentation();
2890b1d251973bfa748d435896fc277cb4024451adJohn Hoford
29cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    public ImageFilterRedEye() {
30cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        mName = "Red Eye";
3190b1d251973bfa748d435896fc277cb4024451adJohn Hoford    }
3290b1d251973bfa748d435896fc277cb4024451adJohn Hoford
33c649360ce22f0138bfcb745eed585a32eb8570e7nicolasroard    @Override
3412c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford    public FilterRepresentation getDefaultRepresentation() {
35c8aa8ac19118f1c1be1bda63d4d72589aed49da4nicolasroard        return new FilterRedEyeRepresentation();
3690b1d251973bfa748d435896fc277cb4024451adJohn Hoford    }
3790b1d251973bfa748d435896fc277cb4024451adJohn Hoford
38cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    public boolean isNil() {
3912017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        return mParameters.isNil();
40cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    }
41cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
4212017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford    public Vector<FilterPoint> getCandidates() {
4312c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford        return mParameters.getCandidates();
44cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    }
45cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
46cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    public void clear() {
4712c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford        mParameters.clearCandidates();
48cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    }
49cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
50cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, short[] matrix);
5190b1d251973bfa748d435896fc277cb4024451adJohn Hoford
52a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard    @Override
534e6c07b71b269ee3fe7f6fa455bc540238df9dedJohn Hoford    public void useRepresentation(FilterRepresentation representation) {
544e6c07b71b269ee3fe7f6fa455bc540238df9dedJohn Hoford        FilterRedEyeRepresentation parameters = (FilterRedEyeRepresentation) representation;
554e6c07b71b269ee3fe7f6fa455bc540238df9dedJohn Hoford        mParameters = parameters;
564e6c07b71b269ee3fe7f6fa455bc540238df9dedJohn Hoford    }
574e6c07b71b269ee3fe7f6fa455bc540238df9dedJohn Hoford
584e6c07b71b269ee3fe7f6fa455bc540238df9dedJohn Hoford    @Override
5999baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
6090b1d251973bfa748d435896fc277cb4024451adJohn Hoford        int w = bitmap.getWidth();
6190b1d251973bfa748d435896fc277cb4024451adJohn Hoford        int h = bitmap.getHeight();
62cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        short[] rect = new short[4];
6312017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford
6412c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford        int size = mParameters.getNumberOfCandidates();
6512017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        Matrix originalToScreen = getOriginalToScreenMatrix(w, h);
6612c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford        for (int i = 0; i < size; i++) {
6712017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford            RectF r = new RectF(((RedEyeCandidate) (mParameters.getCandidate(i))).mRect);
6812c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            originalToScreen.mapRect(r);
6912017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford            if (r.intersect(0, 0, w, h)) {
7012017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford                rect[0] = (short) r.left;
7112017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford                rect[1] = (short) r.top;
7212017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford                rect[2] = (short) r.width();
7312017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford                rect[3] = (short) r.height();
7412017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford                nativeApplyFilter(bitmap, w, h, rect);
75cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            }
76cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        }
771b72a2f1124610b8050dbbdff9f1bb548199fd2eJohn Hoford        return bitmap;
7890b1d251973bfa748d435896fc277cb4024451adJohn Hoford    }
7990b1d251973bfa748d435896fc277cb4024451adJohn Hoford}
80