1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.filtershow.filters;
18
19import android.graphics.Bitmap;
20import android.graphics.Canvas;
21import android.graphics.Matrix;
22import android.graphics.Paint;
23import android.graphics.Rect;
24import android.graphics.RectF;
25
26import com.android.gallery3d.filtershow.imageshow.GeometryMath;
27import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
28
29public class ImageFilterGeometry extends ImageFilter {
30    private final Bitmap.Config mConfig = Bitmap.Config.ARGB_8888;
31    private GeometryMetadata mGeometry = null;
32    private static final String LOGTAG = "ImageFilterGeometry";
33    private static final boolean LOGV = false;
34    private static final int BOTH = 3;
35    private static final int VERTICAL = 2;
36    private static final int HORIZONTAL = 1;
37    private static final int NINETY = 1;
38    private static final int ONE_EIGHTY = 2;
39    private static final int TWO_SEVENTY = 3;
40
41    public ImageFilterGeometry() {
42        mName = "Geometry";
43    }
44
45    @Override
46    public ImageFilter clone() throws CloneNotSupportedException {
47        ImageFilterGeometry filter = (ImageFilterGeometry) super.clone();
48        return filter;
49    }
50
51    public void setGeometryMetadata(GeometryMetadata m) {
52        mGeometry = m;
53    }
54
55    native protected void nativeApplyFilterFlip(Bitmap src, int srcWidth, int srcHeight,
56            Bitmap dst, int dstWidth, int dstHeight, int flip);
57
58    native protected void nativeApplyFilterRotate(Bitmap src, int srcWidth, int srcHeight,
59            Bitmap dst, int dstWidth, int dstHeight, int rotate);
60
61    native protected void nativeApplyFilterCrop(Bitmap src, int srcWidth, int srcHeight,
62            Bitmap dst, int dstWidth, int dstHeight, int offsetWidth, int offsetHeight);
63
64    native protected void nativeApplyFilterStraighten(Bitmap src, int srcWidth, int srcHeight,
65            Bitmap dst, int dstWidth, int dstHeight, float straightenAngle);
66
67    @Override
68    public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
69        // TODO: implement bilinear or bicubic here... for now, just use
70        // canvas to do a simple implementation...
71        // TODO: and be more memory efficient! (do it in native?)
72        Rect cropBounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
73        RectF crop = mGeometry.getCropBounds(bitmap);
74        if (crop.width() > 0 && crop.height() > 0)
75            cropBounds = GeometryMath.roundNearest(crop);
76        Bitmap temp = null;
77        if (mGeometry.hasSwitchedWidthHeight()) {
78            temp = Bitmap.createBitmap(cropBounds.height(), cropBounds.width(), mConfig);
79        } else {
80            temp = Bitmap.createBitmap(cropBounds.width(), cropBounds.height(), mConfig);
81        }
82        float[] displayCenter = {
83                temp.getWidth() / 2f, temp.getHeight() / 2f
84        };
85
86        Matrix m1 = mGeometry.buildTotalXform(bitmap.getWidth(), bitmap.getHeight(), displayCenter);
87
88        Canvas canvas = new Canvas(temp);
89        Paint paint = new Paint();
90        paint.setAntiAlias(true);
91        paint.setFilterBitmap(true);
92        paint.setDither(true);
93        canvas.drawBitmap(bitmap, m1, paint);
94        return temp;
95    }
96
97}
98