2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.filtershow.filters;
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;
26 import com.android.gallery3d.filtershow.CropExtras;
27 import com.android.gallery3d.filtershow.imageshow.GeometryMath;
28 import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
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;
42 public ImageFilterGeometry() {
47 public ImageFilter clone() throws CloneNotSupportedException {
48 ImageFilterGeometry filter = (ImageFilterGeometry) super.clone();
52 public void setGeometryMetadata(GeometryMetadata m) {
56 native protected void nativeApplyFilterFlip(Bitmap src, int srcWidth, int srcHeight,
57 Bitmap dst, int dstWidth, int dstHeight, int flip);
59 native protected void nativeApplyFilterRotate(Bitmap src, int srcWidth, int srcHeight,
60 Bitmap dst, int dstWidth, int dstHeight, int rotate);
62 native protected void nativeApplyFilterCrop(Bitmap src, int srcWidth, int srcHeight,
63 Bitmap dst, int dstWidth, int dstHeight, int offsetWidth, int offsetHeight);
65 native protected void nativeApplyFilterStraighten(Bitmap src, int srcWidth, int srcHeight,
66 Bitmap dst, int dstWidth, int dstHeight, float straightenAngle);
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?)
74 CropExtras extras = mGeometry.getCropExtras();
75 boolean useExtras = mGeometry.getUseCropExtrasFlag();
79 if (extras != null && useExtras){
80 outputX = extras.getOutputX();
81 outputY = extras.getOutputY();
82 s = extras.getScaleUp();
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);
91 int width = cropBounds.width();
92 int height = cropBounds.height();
94 if (mGeometry.hasSwitchedWidthHeight()){
100 if(outputX <= 0 || outputY <= 0){
108 scaleX = (float) outputX / width;
109 scaleY = (float) outputY / height;
113 temp = Bitmap.createBitmap(outputX, outputY, mConfig);
115 float[] displayCenter = {
116 temp.getWidth() / 2f, temp.getHeight() / 2f
119 Matrix m1 = mGeometry.buildTotalXform(bitmap.getWidth(), bitmap.getHeight(), displayCenter);
121 m1.postScale(scaleX, scaleY, displayCenter[0], displayCenter[1]);
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);