ImageFilterRedEye.java revision c8aa8ac19118f1c1be1bda63d4d72589aed49da4
1a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard/*
2a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * Copyright (C) 2012 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
234b1e6fbdf437b7a25fb3eb18a2ed43c9ab4eccc8John Hofordimport com.android.gallery3d.R;
24cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
2590b1d251973bfa748d435896fc277cb4024451adJohn Hoford
26cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport java.util.Vector;
2790b1d251973bfa748d435896fc277cb4024451adJohn Hoford
28cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardpublic class ImageFilterRedEye extends ImageFilter {
29cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    private static final String LOGTAG = "ImageFilterRedEye";
3012c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford    FilterRedEyeRepresentation mParameters = new FilterRedEyeRepresentation();
3190b1d251973bfa748d435896fc277cb4024451adJohn Hoford
32cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    public ImageFilterRedEye() {
33cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        mName = "Red Eye";
3490b1d251973bfa748d435896fc277cb4024451adJohn Hoford    }
3590b1d251973bfa748d435896fc277cb4024451adJohn Hoford
36c649360ce22f0138bfcb745eed585a32eb8570e7nicolasroard    @Override
3712c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford    public FilterRepresentation getDefaultRepresentation() {
38c8aa8ac19118f1c1be1bda63d4d72589aed49da4nicolasroard        return new FilterRedEyeRepresentation();
3990b1d251973bfa748d435896fc277cb4024451adJohn Hoford    }
4090b1d251973bfa748d435896fc277cb4024451adJohn Hoford
41cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    public boolean isNil() {
4212c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford        if (mParameters.getCandidates() != null && mParameters.getCandidates().size() > 0) {
43cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            return false;
44cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        }
45cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        return true;
46cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    }
47cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
48cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    public Vector<RedEyeCandidate> getCandidates() {
4912c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford        if (!mParameters.hasCandidates()) {
5012c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            mParameters.setCandidates(new Vector<RedEyeCandidate>());
51cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        }
5212c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford        return mParameters.getCandidates();
53cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    }
54cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
55cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    public void clear() {
5612c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford        if (!mParameters.hasCandidates()) {
5712c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            mParameters.setCandidates(new Vector<RedEyeCandidate>());
58cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        }
5912c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford        mParameters.clearCandidates();
60cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    }
61cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
62cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, short[] matrix);
6390b1d251973bfa748d435896fc277cb4024451adJohn Hoford
64a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard    @Override
6599baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
6690b1d251973bfa748d435896fc277cb4024451adJohn Hoford        int w = bitmap.getWidth();
6790b1d251973bfa748d435896fc277cb4024451adJohn Hoford        int h = bitmap.getHeight();
68cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        short[] rect = new short[4];
6912c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford        int size = mParameters.getNumberOfCandidates();
7012c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford
7112c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford        for (int i = 0; i < size; i++) {
7212c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            RectF r = new RectF(mParameters.getCandidate(i).mRect);
7312c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            GeometryMetadata geo = getImagePreset().mGeoData;
7412c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            Matrix originalToScreen = geo.getOriginalToScreen(true,
7512c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford                    getImagePreset().getImageLoader().getOriginalBounds().width(),
7612c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford                    getImagePreset().getImageLoader().getOriginalBounds().height(),
7712c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford                    w, h);
7812c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            originalToScreen.mapRect(r);
7912c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            if (r.left < 0) {
8012c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford                r.left = 0;
8112c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            }
8212c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            if (r.left > w) {
8312c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford                r.left = w;
8412c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            }
8512c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            if (r.top < 0) {
8612c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford                r.top = 0;
87cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            }
8812c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            if (r.top > h) {
8912c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford                r.top = h;
9012c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            }
9112c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            if (r.right < 0) {
9212c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford                r.right = 0;
9312c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            }
9412c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            if (r.right > w) {
9512c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford                r.right = w;
9612c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            }
9712c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            if (r.bottom < 0) {
9812c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford                r.bottom = 0;
9912c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            }
10012c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            if (r.bottom > h) {
10112c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford                r.bottom = h;
10212c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            }
10312c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            rect[0] = (short) r.left;
10412c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            rect[1] = (short) r.top;
10512c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            rect[2] = (short) r.width();
10612c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            rect[3] = (short) r.height();
10712c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford            nativeApplyFilter(bitmap, w, h, rect);
108cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        }
10912c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford
1101b72a2f1124610b8050dbbdff9f1bb548199fd2eJohn Hoford        return bitmap;
11190b1d251973bfa748d435896fc277cb4024451adJohn Hoford    }
11290b1d251973bfa748d435896fc277cb4024451adJohn Hoford}
113