ImageVignette.java revision 37f8e432f4eb94d0ab483bfdc2a1fa024508e85f
1a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford/*
2e1d875757bbe6634d7224c4f759d904688de4013nicolasroard * Copyright (C) 2013 The Android Open Source Project
3a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford *
4a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
5a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford * you may not use this file except in compliance with the License.
6a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford * You may obtain a copy of the License at
7a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford *
8a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
9a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford *
10a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford * Unless required by applicable law or agreed to in writing, software
11a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
12a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford * See the License for the specific language governing permissions and
14a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford * limitations under the License.
15a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford */
16a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
17a93d5ee9c409e2328dcbe2326591436f8ac23146John Hofordpackage com.android.gallery3d.filtershow.imageshow;
18a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
19a93d5ee9c409e2328dcbe2326591436f8ac23146John Hofordimport android.content.Context;
20a93d5ee9c409e2328dcbe2326591436f8ac23146John Hofordimport android.graphics.Canvas;
21a93d5ee9c409e2328dcbe2326591436f8ac23146John Hofordimport android.graphics.Matrix;
22a93d5ee9c409e2328dcbe2326591436f8ac23146John Hofordimport android.util.AttributeSet;
232fc7efdcd75279649091471dda3ee600db25a995John Hofordimport android.util.Log;
24a93d5ee9c409e2328dcbe2326591436f8ac23146John Hofordimport android.view.MotionEvent;
25a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
26a93d5ee9c409e2328dcbe2326591436f8ac23146John Hofordimport com.android.gallery3d.filtershow.editors.EditorVignette;
27a93d5ee9c409e2328dcbe2326591436f8ac23146John Hofordimport com.android.gallery3d.filtershow.filters.FilterVignetteRepresentation;
28a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
29a93d5ee9c409e2328dcbe2326591436f8ac23146John Hofordpublic class ImageVignette extends ImageShow {
30a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    private static final String LOGTAG = "ImageVignette";
31a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
32a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    private FilterVignetteRepresentation mVignetteRep;
33a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    private EditorVignette mEditorVignette;
34a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
35a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    private int mActiveHandle = -1;
36a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
37a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    EclipseControl mElipse;
38a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
39a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    public ImageVignette(Context context) {
40a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        super(context);
41a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        mElipse = new EclipseControl(context);
42a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    }
43a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
44a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    public ImageVignette(Context context, AttributeSet attrs) {
45a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        super(context, attrs);
46a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        mElipse = new EclipseControl(context);
47a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    }
48a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
49a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    @Override
50a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    public boolean onTouchEvent(MotionEvent event) {
5137f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford        int w = MasterImage.getImage().getOriginalBounds().width();
5237f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford        int h = MasterImage.getImage().getOriginalBounds().height();
53a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        int mask = event.getActionMasked();
542fc7efdcd75279649091471dda3ee600db25a995John Hoford        if (mActiveHandle == -1) {
552fc7efdcd75279649091471dda3ee600db25a995John Hoford            if (MotionEvent.ACTION_DOWN != mask) {
562fc7efdcd75279649091471dda3ee600db25a995John Hoford                return super.onTouchEvent(event);
572fc7efdcd75279649091471dda3ee600db25a995John Hoford            }
582fc7efdcd75279649091471dda3ee600db25a995John Hoford            if (event.getPointerCount() == 1) {
592fc7efdcd75279649091471dda3ee600db25a995John Hoford                mActiveHandle = mElipse.getCloseHandle(event.getX(), event.getY());
602fc7efdcd75279649091471dda3ee600db25a995John Hoford            }
612fc7efdcd75279649091471dda3ee600db25a995John Hoford            if (mActiveHandle == -1) {
622fc7efdcd75279649091471dda3ee600db25a995John Hoford                return super.onTouchEvent(event);
632fc7efdcd75279649091471dda3ee600db25a995John Hoford            }
642fc7efdcd75279649091471dda3ee600db25a995John Hoford        } else {
652fc7efdcd75279649091471dda3ee600db25a995John Hoford            switch (mask) {
662fc7efdcd75279649091471dda3ee600db25a995John Hoford                case MotionEvent.ACTION_UP:
672fc7efdcd75279649091471dda3ee600db25a995John Hoford                    mActiveHandle = -1;
682fc7efdcd75279649091471dda3ee600db25a995John Hoford                    break;
692fc7efdcd75279649091471dda3ee600db25a995John Hoford                case MotionEvent.ACTION_DOWN:
702fc7efdcd75279649091471dda3ee600db25a995John Hoford                    break;
712fc7efdcd75279649091471dda3ee600db25a995John Hoford            }
72a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        }
73a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        float x = event.getX();
74a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        float y = event.getY();
75a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
763e3e9e219a76e8ef4fb56b8ef4c6e0bac06c6cd1John Hoford        mElipse.setScrImageInfo(getScreenToImageMatrix(true),
773e3e9e219a76e8ef4fb56b8ef4c6e0bac06c6cd1John Hoford                MasterImage.getImage().getOriginalBounds());
78a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
797097d5173dbbddae315d13251572feb910954a70nicolasroard        boolean didComputeEllipses = false;
80a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        switch (mask) {
81a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford            case (MotionEvent.ACTION_DOWN):
8237f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford                mElipse.actionDown2(x, y, w, h, mVignetteRep);
83a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford                break;
84a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford            case (MotionEvent.ACTION_UP):
85a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford            case (MotionEvent.ACTION_MOVE):
8637f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford                mElipse.actionMove2(mActiveHandle, x, y, w, h, mVignetteRep);
87a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford                setRepresentation(mVignetteRep);
887097d5173dbbddae315d13251572feb910954a70nicolasroard                didComputeEllipses = true;
89a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford                break;
90a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        }
917097d5173dbbddae315d13251572feb910954a70nicolasroard        if (!didComputeEllipses) {
927097d5173dbbddae315d13251572feb910954a70nicolasroard            computeEllipses();
937097d5173dbbddae315d13251572feb910954a70nicolasroard        }
94a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        invalidate();
95a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        return true;
96a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    }
97a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
98a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    public void setRepresentation(FilterVignetteRepresentation vignetteRep) {
99a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        mVignetteRep = vignetteRep;
100e1d875757bbe6634d7224c4f759d904688de4013nicolasroard        computeEllipses();
101e1d875757bbe6634d7224c4f759d904688de4013nicolasroard    }
102e1d875757bbe6634d7224c4f759d904688de4013nicolasroard
103e1d875757bbe6634d7224c4f759d904688de4013nicolasroard    public void computeEllipses() {
104e1d875757bbe6634d7224c4f759d904688de4013nicolasroard        if (mVignetteRep == null) {
105e1d875757bbe6634d7224c4f759d904688de4013nicolasroard            return;
106e1d875757bbe6634d7224c4f759d904688de4013nicolasroard        }
10737f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford        float w = MasterImage.getImage().getOriginalBounds().width();
10837f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford        float h = MasterImage.getImage().getOriginalBounds().height();
109a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        Matrix toImg = getScreenToImageMatrix(false);
110a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        Matrix toScr = new Matrix();
111a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        toImg.invert(toScr);
112a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
113a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        float[] c = new float[] {
11437f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford                mVignetteRep.getCenterX() * w, mVignetteRep.getCenterY() * h};
115a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        if (Float.isNaN(c[0])) {
11637f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford            float cx = w / 2;
11737f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford            float cy = h / 2;
1181863a3119a802517b1c2a53763f904f73fdc940bJohn Hoford            float rx = Math.min(cx, cy) * .8f;
1191863a3119a802517b1c2a53763f904f73fdc940bJohn Hoford            float ry = rx;
12037f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford            mVignetteRep.setCenter(cx / w, cy / h);
12137f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford            mVignetteRep.setRadius(rx / w, ry / h);
122a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
123a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford            c[0] = cx;
124a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford            c[1] = cy;
125a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford            toScr.mapPoints(c);
1261863a3119a802517b1c2a53763f904f73fdc940bJohn Hoford            if (getWidth() != 0) {
1271863a3119a802517b1c2a53763f904f73fdc940bJohn Hoford                mElipse.setCenter(c[0], c[1]);
1281863a3119a802517b1c2a53763f904f73fdc940bJohn Hoford                mElipse.setRadius(c[0] * 0.8f, c[1] * 0.8f);
1291863a3119a802517b1c2a53763f904f73fdc940bJohn Hoford            }
130a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        } else {
131a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
132a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford            toScr.mapPoints(c);
133a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
134a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford            mElipse.setCenter(c[0], c[1]);
13537f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford            mElipse.setRadius(toScr.mapRadius(mVignetteRep.getRadiusX() * w),
13637f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford                    toScr.mapRadius(mVignetteRep.getRadiusY() * h));
137a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        }
138e1d875757bbe6634d7224c4f759d904688de4013nicolasroard        mEditorVignette.commitLocalRepresentation();
139a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    }
140a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
141a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    public void setEditor(EditorVignette editorVignette) {
142a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        mEditorVignette = editorVignette;
143a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    }
144a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
145a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    @Override
146e1d875757bbe6634d7224c4f759d904688de4013nicolasroard    public void onSizeChanged(int w, int h, int oldw, int oldh) {
147e1d875757bbe6634d7224c4f759d904688de4013nicolasroard        super.onSizeChanged(w,  h, oldw, oldh);
148e1d875757bbe6634d7224c4f759d904688de4013nicolasroard        computeEllipses();
149e1d875757bbe6634d7224c4f759d904688de4013nicolasroard    }
150e1d875757bbe6634d7224c4f759d904688de4013nicolasroard
151e1d875757bbe6634d7224c4f759d904688de4013nicolasroard    @Override
152a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    public void onDraw(Canvas canvas) {
153a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        super.onDraw(canvas);
154e1d875757bbe6634d7224c4f759d904688de4013nicolasroard        if (mVignetteRep == null) {
155e1d875757bbe6634d7224c4f759d904688de4013nicolasroard            return;
156e1d875757bbe6634d7224c4f759d904688de4013nicolasroard        }
15737f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford        float w = MasterImage.getImage().getOriginalBounds().width();
15837f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford        float h = MasterImage.getImage().getOriginalBounds().height();
159fc7d46515db7a3a50f7416c2462030609391cf95John Hoford        Matrix toImg = getScreenToImageMatrix(false);
160fc7d46515db7a3a50f7416c2462030609391cf95John Hoford        Matrix toScr = new Matrix();
161fc7d46515db7a3a50f7416c2462030609391cf95John Hoford        toImg.invert(toScr);
162fc7d46515db7a3a50f7416c2462030609391cf95John Hoford        float[] c = new float[] {
16337f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford                mVignetteRep.getCenterX() * w, mVignetteRep.getCenterY() * h };
164fc7d46515db7a3a50f7416c2462030609391cf95John Hoford        toScr.mapPoints(c);
165fc7d46515db7a3a50f7416c2462030609391cf95John Hoford        mElipse.setCenter(c[0], c[1]);
16637f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford        mElipse.setRadius(toScr.mapRadius(mVignetteRep.getRadiusX() * w),
16737f8e432f4eb94d0ab483bfdc2a1fa024508e85fJohn Hoford                toScr.mapRadius(mVignetteRep.getRadiusY() * h));
168fc7d46515db7a3a50f7416c2462030609391cf95John Hoford
169a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford        mElipse.draw(canvas);
170a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford    }
171a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford
172a93d5ee9c409e2328dcbe2326591436f8ac23146John Hoford}
173