112017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford/*
212017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford * Copyright (C) 2013 The Android Open Source Project
312017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford *
412017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
512017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford * you may not use this file except in compliance with the License.
612017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford * You may obtain a copy of the License at
712017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford *
812017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
912017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford *
1012017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford * Unless required by applicable law or agreed to in writing, software
1112017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
1212017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1312017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford * See the License for the specific language governing permissions and
1412017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford * limitations under the License.
1512017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford */
16cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
17cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardpackage com.android.gallery3d.filtershow.imageshow;
18cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
19cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport android.content.Context;
20cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport android.graphics.Canvas;
21cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport android.graphics.Color;
22cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport android.graphics.Matrix;
23cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport android.graphics.Paint;
24cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport android.graphics.Paint.Style;
25cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport android.graphics.RectF;
26cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport android.view.MotionEvent;
27cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
2812017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hofordimport com.android.gallery3d.filtershow.filters.FilterPoint;
29cc93226fc364a50de3a1479c0912e9af1854b666nicolasroardimport com.android.gallery3d.filtershow.filters.RedEyeCandidate;
30cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
3112017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hofordpublic class ImageRedEye extends ImagePoint {
32cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    private static final String LOGTAG = "ImageRedEyes";
33cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    private RectF mCurrentRect = null;
34cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
3512c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford    public ImageRedEye(Context context) {
36cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        super(context);
37cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    }
38cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
39cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    @Override
40cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    public void resetParameter() {
4112017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        super.resetParameter();
42cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        invalidate();
43cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    }
44cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
45cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    @Override
46cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
47cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    public boolean onTouchEvent(MotionEvent event) {
48cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        super.onTouchEvent(event);
49df0b0c4eaafcc9e4652fe6ec0791e4abe1c3b750nicolasroard
50df0b0c4eaafcc9e4652fe6ec0791e4abe1c3b750nicolasroard        if (event.getPointerCount() > 1) {
51df0b0c4eaafcc9e4652fe6ec0791e4abe1c3b750nicolasroard            return true;
52df0b0c4eaafcc9e4652fe6ec0791e4abe1c3b750nicolasroard        }
53df0b0c4eaafcc9e4652fe6ec0791e4abe1c3b750nicolasroard
54df0b0c4eaafcc9e4652fe6ec0791e4abe1c3b750nicolasroard        if (didFinishScalingOperation()) {
55df0b0c4eaafcc9e4652fe6ec0791e4abe1c3b750nicolasroard            return true;
56df0b0c4eaafcc9e4652fe6ec0791e4abe1c3b750nicolasroard        }
57df0b0c4eaafcc9e4652fe6ec0791e4abe1c3b750nicolasroard
58cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        float ex = event.getX();
59cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        float ey = event.getY();
60cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
61cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        // let's transform (ex, ey) to displayed image coordinates
62cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        if (event.getAction() == MotionEvent.ACTION_DOWN) {
63cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            mCurrentRect = new RectF();
64cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            mCurrentRect.left = ex - mTouchPadding;
65cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            mCurrentRect.top = ey - mTouchPadding;
66cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        }
67cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        if (event.getAction() == MotionEvent.ACTION_MOVE) {
68cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            mCurrentRect.right = ex + mTouchPadding;
69cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            mCurrentRect.bottom = ey + mTouchPadding;
70cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        }
71cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        if (event.getAction() == MotionEvent.ACTION_UP) {
72cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            if (mCurrentRect != null) {
73cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard                // transform to original coordinates
74a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford                Matrix originalNoRotateToScreen = getImageToScreenMatrix(false);
75a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford                Matrix originalToScreen = getImageToScreenMatrix(true);
76cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard                Matrix invert = new Matrix();
77cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard                originalToScreen.invert(invert);
78cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard                RectF r = new RectF(mCurrentRect);
79cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard                invert.mapRect(r);
80cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard                RectF r2 = new RectF(mCurrentRect);
81cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard                invert.reset();
82cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard                originalNoRotateToScreen.invert(invert);
83cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard                invert.mapRect(r2);
8412c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford                mRedEyeRep.addRect(r, r2);
85cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard                this.resetImageCaches(this);
86cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            }
87cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            mCurrentRect = null;
88cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        }
8912c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford        mEditorRedEye.commitLocalRepresentation();
90cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        invalidate();
91cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        return true;
92cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    }
93cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard
94cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    @Override
95cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    public void onDraw(Canvas canvas) {
96cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        super.onDraw(canvas);
97cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        Paint paint = new Paint();
98cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        paint.setStyle(Style.STROKE);
99cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        paint.setColor(Color.RED);
100cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        paint.setStrokeWidth(2);
101cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        if (mCurrentRect != null) {
102cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            paint.setColor(Color.RED);
103cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            RectF drawRect = new RectF(mCurrentRect);
104cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard            canvas.drawRect(drawRect, paint);
105cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard        }
106cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard    }
10712c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford
10812017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford    @Override
10912017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford    protected void drawPoint(FilterPoint point, Canvas canvas, Matrix originalToScreen,
11012017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford            Matrix originalRotateToScreen, Paint paint) {
11112017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        RedEyeCandidate candidate = (RedEyeCandidate) point;
11212017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        RectF rect = candidate.getRect();
11312017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        RectF drawRect = new RectF();
11412017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        originalToScreen.mapRect(drawRect, rect);
11512017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        RectF fullRect = new RectF();
11612017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        originalRotateToScreen.mapRect(fullRect, rect);
11712017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        paint.setColor(Color.BLUE);
11812017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        canvas.drawRect(fullRect, paint);
11912017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        canvas.drawLine(fullRect.centerX(), fullRect.top,
12012017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford                fullRect.centerX(), fullRect.bottom, paint);
12112017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        canvas.drawLine(fullRect.left, fullRect.centerY(),
12212017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford                fullRect.right, fullRect.centerY(), paint);
12312017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        paint.setColor(Color.GREEN);
12412017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        float dw = drawRect.width();
12512017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        float dh = drawRect.height();
12612017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        float dx = fullRect.centerX() - dw / 2;
12712017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        float dy = fullRect.centerY() - dh / 2;
12812017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        drawRect.set(dx, dy, dx + dw, dy + dh);
12912017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        canvas.drawRect(drawRect, paint);
13012017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        canvas.drawLine(drawRect.centerX(), drawRect.top,
13112017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford                drawRect.centerX(), drawRect.bottom, paint);
13212017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        canvas.drawLine(drawRect.left, drawRect.centerY(),
13312017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford                drawRect.right, drawRect.centerY(), paint);
13412017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford        canvas.drawCircle(drawRect.centerX(), drawRect.centerY(),
13512017c1b3bc314b5fee96c3b2a033d7877ea84e4John Hoford                mTouchPadding, paint);
13612c7c8ed14ca9dff9de64e121282f1970cbe7ae4John Hoford    }
137cc93226fc364a50de3a1479c0912e9af1854b666nicolasroard}
138