summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/imageshow/ImageCrop.java
blob: 836ec82f8db03aa9a699c22d49d38fc61902a55c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.filtershow.imageshow;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import com.android.gallery3d.R;

import com.android.gallery3d.filtershow.presets.ImagePreset;

public class ImageCrop extends ImageGeometry {
    private static final boolean LOGV = false;
    private static final int MOVE_LEFT = 1;
    private static final int MOVE_TOP = 2;
    private static final int MOVE_RIGHT = 4;
    private static final int MOVE_BOTTOM = 8;
    private static final int MOVE_BLOCK = 16;

    private static final float MIN_CROP_WIDTH_HEIGHT = 0.1f;
    private static final int TOUCH_TOLERANCE = 30;
    private static final int SHADOW_ALPHA = 160;

    private float mAspectWidth = 4;
    private float mAspectHeight = 3;
    private boolean mFixAspectRatio = false; // not working yet

    private final Paint borderPaint;

    private float mCropOffsetX = 0;
    private float mCropOffsetY = 0;
    private float mPrevOffsetX = 0;
    private float mPrevOffsetY = 0;

    private int movingEdges;
    private final Drawable cropIndicator;
    private final int indicatorSize;

    private static final String LOGTAG = "ImageCrop";

    private static final Paint gPaint = new Paint();

    public ImageCrop(Context context) {
        super(context);
        Resources resources = context.getResources();
        cropIndicator = resources.getDrawable(R.drawable.camera_crop_holo);
        indicatorSize = (int) resources.getDimension(R.dimen.crop_indicator_size);
        int borderColor = resources.getColor(R.color.opaque_cyan);
        borderPaint = new Paint();
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setColor(borderColor);
        borderPaint.setStrokeWidth(2f);
    }

    public ImageCrop(Context context, AttributeSet attrs) {
        super(context, attrs);
        Resources resources = context.getResources();
        cropIndicator = resources.getDrawable(R.drawable.camera_crop_holo);
        indicatorSize = (int) resources.getDimension(R.dimen.crop_indicator_size);
        int borderColor = resources.getColor(R.color.opaque_cyan);
        borderPaint = new Paint();
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setColor(borderColor);
        borderPaint.setStrokeWidth(2f);
    }

    private float getScaledMinWidthHeight() {
        RectF disp = getLocalDisplayBounds();
        float scaled = Math.min(disp.width(), disp.height()) * MIN_CROP_WIDTH_HEIGHT
                / getLocalScale();
        return scaled;
    }

    protected static Matrix getCropRotationMatrix(float rotation, RectF localImage) {
        Matrix m = new Matrix();
        m.setRotate(rotation, localImage.centerX(), localImage.centerY());
        if (!m.rectStaysRect()) {
            return null;
        }
        return m;
    }

    protected RectF getCropBoundsDisplayed() {
        RectF bounds = getLocalCropBounds();
        RectF crop = new RectF(bounds);
        Matrix m = getCropRotationMatrix(getLocalRotation(), getLocalPhotoBounds());

        if (m == null) {
            if (LOGV)
                Log.v(LOGTAG, "FAILED TO MAP CROP BOUNDS TO RECTANGLE");
            m = new Matrix();
        } else {
            m.mapRect(crop);
        }
        m = new Matrix();
        float zoom = getLocalScale();
        m.setScale(zoom, zoom, mCenterX, mCenterY);
        m.preTranslate(mXOffset, mYOffset);
        m.mapRect(crop);
        return crop;
    }

    private RectF getRotatedCropBounds() {
        RectF bounds = getLocalCropBounds();
        RectF crop = new RectF(bounds);
        Matrix m = getCropRotationMatrix(getLocalRotation(), getLocalPhotoBounds());

        if (m == null) {
            if (LOGV)
                Log.v(LOGTAG, "FAILED TO MAP CROP BOUNDS TO RECTANGLE");
            return null;
        } else {
            m.mapRect(crop);
        }
        return crop;
    }

    /**
     * Sets cropped bounds; modifies the bounds if it's smaller than the allowed
     * dimensions.
     */
    public void setCropBounds(RectF bounds) {
        // Avoid cropping smaller than minimum width or height.
        RectF cbounds = new RectF(bounds);
        float minWidthHeight = getScaledMinWidthHeight();

        float newWidth = cbounds.width();
        float newHeight = cbounds.height();
        if (newWidth < minWidthHeight) {
            newWidth = minWidthHeight;
        }
        if (newHeight < minWidthHeight) {
            newHeight = minWidthHeight;
        }

        RectF pbounds = getLocalPhotoBounds();
        if (pbounds.width() < minWidthHeight) {
            newWidth = pbounds.width();
        }
        if (pbounds.height() < minWidthHeight) {
            newHeight = pbounds.height();
        }

        cbounds.set(cbounds.left, cbounds.top, cbounds.left + newWidth, cbounds.top + newHeight);
        RectF snappedCrop = findCropBoundForRotatedImg(cbounds, pbounds, getLocalStraighten(),
                mCenterX - mXOffset, mCenterY - mYOffset);
        if (mFixAspectRatio) {
            // TODO: add aspect ratio stuff
            fixAspectRatio(snappedCrop, mAspectWidth, mAspectHeight);
        }
        setLocalCropBounds(snappedCrop);
        invalidate();
    }

    private void detectMovingEdges(float x, float y) {
        RectF cropped = getCropBoundsDisplayed();
        movingEdges = 0;

        // Check left or right.
        float left = Math.abs(x - cropped.left);
        float right = Math.abs(x - cropped.right);
        if ((left <= TOUCH_TOLERANCE) && (left < right)) {
            movingEdges |= MOVE_LEFT;
        }
        else if (right <= TOUCH_TOLERANCE) {
            movingEdges |= MOVE_RIGHT;
        }

        // Check top or bottom.
        float top = Math.abs(y - cropped.top);
        float bottom = Math.abs(y - cropped.bottom);
        if ((top <= TOUCH_TOLERANCE) & (top < bottom)) {
            movingEdges |= MOVE_TOP;
        }
        else if (bottom <= TOUCH_TOLERANCE) {
            movingEdges |= MOVE_BOTTOM;
        }
        invalidate();
    }

    private void moveEdges(float dX, float dY) {
        RectF cropped = getRotatedCropBounds();
        float minWidthHeight = getScaledMinWidthHeight();
        float scale = getLocalScale();
        float deltaX = dX / scale;
        float deltaY = dY / scale;
        if (movingEdges == MOVE_BLOCK) {
            // TODO
        } else {
            if ((movingEdges & MOVE_LEFT) != 0) {
                cropped.left = Math.min(cropped.left + deltaX, cropped.right - minWidthHeight);
                fixRectAspectW(cropped);
            }
            if ((movingEdges & MOVE_TOP) != 0) {
                cropped.top = Math.min(cropped.top + deltaY, cropped.bottom - minWidthHeight);
                fixRectAspectH(cropped);
            }
            if ((movingEdges & MOVE_RIGHT) != 0) {
                cropped.right = Math.max(cropped.right + deltaX, cropped.left + minWidthHeight);
                fixRectAspectW(cropped);
            }
            if ((movingEdges & MOVE_BOTTOM) != 0) {
                cropped.bottom = Math.max(cropped.bottom + deltaY, cropped.top + minWidthHeight);
                fixRectAspectH(cropped);
            }
        }
        Matrix m = getCropRotationMatrix(getLocalRotation(), getLocalPhotoBounds());
        Matrix m0 = new Matrix();
        if (!m.invert(m0)) {
            if (LOGV)
                Log.v(LOGTAG, "FAILED TO INVERT ROTATION MATRIX");
        }
        if (!m0.mapRect(cropped)) {
            if (LOGV)
                Log.v(LOGTAG, "FAILED TO UNROTATE CROPPING BOUNDS");
        }
        setCropBounds(cropped);
    }

    private void fixRectAspectH(RectF cropped) {
        if (mFixAspectRatio) {
            float half = getNewWidthForHeightAspect(cropped.height(), mAspectWidth, mAspectHeight) / 2;
            float mid = (cropped.right - cropped.left) / 2;
            cropped.left = mid - half;
            cropped.right = mid + half;
        }
    }

    private void fixRectAspectW(RectF cropped) {
        if (mFixAspectRatio) {
            float half = getNewHeightForWidthAspect(cropped.width(), mAspectWidth, mAspectHeight) / 2;
            float mid = (cropped.bottom - cropped.top) / 2;
            cropped.top = mid - half;
            cropped.bottom = mid + half;
        }
    }

    private void drawShadow(Canvas canvas, float left, float top, float right, float bottom) {
        canvas.save();
        canvas.clipRect(left, top, right, bottom);
        canvas.drawARGB(SHADOW_ALPHA, 0, 0, 0);
        canvas.restore();
    }

    private void drawIndicator(Canvas canvas, Drawable indicator, float centerX, float centerY) {
        int left = (int) centerX - indicatorSize / 2;
        int top = (int) centerY - indicatorSize / 2;
        indicator.setBounds(left, top, left + indicatorSize, top + indicatorSize);
        indicator.draw(canvas);
    }

    @Override
    protected void setActionDown(float x, float y) {
        super.setActionDown(x, y);
        detectMovingEdges(x, y);
        if (movingEdges == 0) {
            mPrevOffsetX = mCropOffsetX;
            mPrevOffsetY = mCropOffsetY;
        }
    }

    @Override
    protected void setActionMove(float x, float y) {
        if (movingEdges != 0) {
            moveEdges(x - mCurrentX, y - mCurrentY);
        } else {
            float dx = x - mTouchCenterX;
            float dy = y - mTouchCenterY;
            mCropOffsetX = dx + mPrevOffsetX;
            mCropOffsetY = dy + mPrevOffsetY;
        }
        super.setActionMove(x, y);
    }

    @Override
    protected void gainedVisibility() {
        setCropBounds(getLocalCropBounds());
    }

    @Override
    protected void lostVisibility() {
    }

    @Override
    protected void drawShape(Canvas canvas, Bitmap image) {
        gPaint.setAntiAlias(true);
        gPaint.setFilterBitmap(true);
        gPaint.setDither(true);
        gPaint.setARGB(255, 255, 255, 255);

        drawRegularFlippedBitmap(canvas, image, gPaint);

        RectF displayRect = getLocalDisplayBounds();
        float dWidth = displayRect.width();
        float dHeight = displayRect.height();
        RectF boundsRect = getCropBoundsDisplayed();
        gPaint.setARGB(128, 0, 0, 0);
        gPaint.setStyle(Paint.Style.FILL);
        // TODO: move this to style when refactoring
        canvas.drawRect(0, 0, dWidth, boundsRect.top, gPaint);
        canvas.drawRect(0, boundsRect.bottom, dWidth, dHeight, gPaint);
        canvas.drawRect(0, boundsRect.top, boundsRect.left, boundsRect.bottom,
                gPaint);
        canvas.drawRect(boundsRect.right, boundsRect.top, dWidth,
                boundsRect.bottom, gPaint);

        Path path = new Path();
        path.addRect(boundsRect, Path.Direction.CCW);
        gPaint.setARGB(255, 255, 255, 255);
        gPaint.setStrokeWidth(3);
        gPaint.setStyle(Paint.Style.STROKE);

        canvas.drawPath(path, gPaint);

        boolean notMoving = movingEdges == 0;
        if (((movingEdges & MOVE_TOP) != 0) || notMoving) {
            drawIndicator(canvas, cropIndicator, boundsRect.centerX(), boundsRect.top);
        }
        if (((movingEdges & MOVE_BOTTOM) != 0) || notMoving) {
            drawIndicator(canvas, cropIndicator, boundsRect.centerX(), boundsRect.bottom);
        }
        if (((movingEdges & MOVE_LEFT) != 0) || notMoving) {
            drawIndicator(canvas, cropIndicator, boundsRect.left, boundsRect.centerY());
        }
        if (((movingEdges & MOVE_RIGHT) != 0) || notMoving) {
            drawIndicator(canvas, cropIndicator, boundsRect.right, boundsRect.centerY());
        }
    }

}