OSDN Git Service

33ecc8ab962444671c1d4395e5fff03a68b667bc
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / filters / ImageFilterGeometry.java
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
17 package com.android.gallery3d.filtershow.filters;
18
19 import android.graphics.Bitmap;
20 import android.graphics.Canvas;
21 import android.graphics.Matrix;
22 import android.graphics.Paint;
23 import android.graphics.Rect;
24 import android.graphics.RectF;
25
26 import com.android.gallery3d.filtershow.CropExtras;
27 import com.android.gallery3d.filtershow.imageshow.GeometryMath;
28 import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
29
30 public class ImageFilterGeometry extends ImageFilter {
31     private final Bitmap.Config mConfig = Bitmap.Config.ARGB_8888;
32     private GeometryMetadata mGeometry = null;
33     private static final String LOGTAG = "ImageFilterGeometry";
34     private static final boolean LOGV = false;
35     private static final int BOTH = 3;
36     private static final int VERTICAL = 2;
37     private static final int HORIZONTAL = 1;
38     private static final int NINETY = 1;
39     private static final int ONE_EIGHTY = 2;
40     private static final int TWO_SEVENTY = 3;
41
42     public ImageFilterGeometry() {
43         mName = "Geometry";
44     }
45
46     @Override
47     public ImageFilter clone() throws CloneNotSupportedException {
48         ImageFilterGeometry filter = (ImageFilterGeometry) super.clone();
49         return filter;
50     }
51
52     public void setGeometryMetadata(GeometryMetadata m) {
53         mGeometry = m;
54     }
55
56     native protected void nativeApplyFilterFlip(Bitmap src, int srcWidth, int srcHeight,
57             Bitmap dst, int dstWidth, int dstHeight, int flip);
58
59     native protected void nativeApplyFilterRotate(Bitmap src, int srcWidth, int srcHeight,
60             Bitmap dst, int dstWidth, int dstHeight, int rotate);
61
62     native protected void nativeApplyFilterCrop(Bitmap src, int srcWidth, int srcHeight,
63             Bitmap dst, int dstWidth, int dstHeight, int offsetWidth, int offsetHeight);
64
65     native protected void nativeApplyFilterStraighten(Bitmap src, int srcWidth, int srcHeight,
66             Bitmap dst, int dstWidth, int dstHeight, float straightenAngle);
67
68     @Override
69     public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
70         // TODO: implement bilinear or bicubic here... for now, just use
71         // canvas to do a simple implementation...
72         // TODO: and be more memory efficient! (do it in native?)
73
74         CropExtras extras = mGeometry.getCropExtras();
75         boolean useExtras = mGeometry.getUseCropExtrasFlag();
76         int outputX = 0;
77         int outputY = 0;
78         boolean s = false;
79         if (extras != null && useExtras){
80             outputX = extras.getOutputX();
81             outputY = extras.getOutputY();
82             s = extras.getScaleUp();
83         }
84
85
86         Rect cropBounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
87         RectF crop = mGeometry.getCropBounds(bitmap);
88         if (crop.width() > 0 && crop.height() > 0)
89             cropBounds = GeometryMath.roundNearest(crop);
90
91         int width = cropBounds.width();
92         int height = cropBounds.height();
93
94         if (mGeometry.hasSwitchedWidthHeight()){
95             int temp = width;
96             width = height;
97             height = temp;
98         }
99
100         if(outputX <= 0 || outputY <= 0){
101             outputX = width;
102             outputY = height;
103         }
104
105         float scaleX = 1;
106         float scaleY = 1;
107         if (s){
108                 scaleX = (float) outputX / width;
109                 scaleY = (float) outputY / height;
110         }
111
112         Bitmap temp = null;
113         temp = Bitmap.createBitmap(outputX, outputY, mConfig);
114
115         float[] displayCenter = {
116                 temp.getWidth() / 2f, temp.getHeight() / 2f
117         };
118
119         Matrix m1 = mGeometry.buildTotalXform(bitmap.getWidth(), bitmap.getHeight(), displayCenter);
120
121         m1.postScale(scaleX, scaleY, displayCenter[0], displayCenter[1]);
122
123         Canvas canvas = new Canvas(temp);
124         Paint paint = new Paint();
125         paint.setAntiAlias(true);
126         paint.setFilterBitmap(true);
127         paint.setDither(true);
128         canvas.drawBitmap(bitmap, m1, paint);
129         return temp;
130     }
131
132 }