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;
25 import android.util.Log;
27 import com.android.gallery3d.filtershow.crop.CropExtras;
28 import com.android.gallery3d.filtershow.imageshow.GeometryMath;
29 import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
31 public class ImageFilterGeometry extends ImageFilter {
32 private final Bitmap.Config mConfig = Bitmap.Config.ARGB_8888;
33 private GeometryMetadata mGeometry = null;
34 private static final String LOGTAG = "ImageFilterGeometry";
35 private static final boolean LOGV = false;
36 private static final int BOTH = 3;
37 private static final int VERTICAL = 2;
38 private static final int HORIZONTAL = 1;
39 private static final int NINETY = 1;
40 private static final int ONE_EIGHTY = 2;
41 private static final int TWO_SEVENTY = 3;
43 public ImageFilterGeometry() {
48 public ImageFilter clone() throws CloneNotSupportedException {
49 // FIXME: clone() should not be needed. Remove when we fix geometry.
50 ImageFilterGeometry filter = (ImageFilterGeometry) super.clone();
54 native protected void nativeApplyFilterFlip(Bitmap src, int srcWidth, int srcHeight,
55 Bitmap dst, int dstWidth, int dstHeight, int flip);
57 native protected void nativeApplyFilterRotate(Bitmap src, int srcWidth, int srcHeight,
58 Bitmap dst, int dstWidth, int dstHeight, int rotate);
60 native protected void nativeApplyFilterCrop(Bitmap src, int srcWidth, int srcHeight,
61 Bitmap dst, int dstWidth, int dstHeight, int offsetWidth, int offsetHeight);
63 native protected void nativeApplyFilterStraighten(Bitmap src, int srcWidth, int srcHeight,
64 Bitmap dst, int dstWidth, int dstHeight, float straightenAngle);
67 public void useRepresentation(FilterRepresentation representation) {
68 mGeometry = (GeometryMetadata) representation;
72 public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
73 // TODO: implement bilinear or bicubic here... for now, just use
74 // canvas to do a simple implementation...
75 // TODO: and be more memory efficient! (do it in native?)
76 RectF cb = mGeometry.getPreviewCropBounds();
77 RectF pb = mGeometry.getPhotoBounds();
78 if (cb.width() == 0 || cb.height() == 0 || pb.width() == 0 || pb.height() == 0) {
79 Log.w(LOGTAG, "Cannot apply geometry: geometry metadata has not been initialized");
82 CropExtras extras = mGeometry.getCropExtras();
83 boolean useExtras = mGeometry.getUseCropExtrasFlag();
87 if (extras != null && useExtras){
88 outputX = extras.getOutputX();
89 outputY = extras.getOutputY();
90 s = extras.getScaleUp();
94 Rect cropBounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
95 RectF crop = mGeometry.getCropBounds(bitmap);
96 if (crop.width() > 0 && crop.height() > 0)
97 cropBounds = GeometryMath.roundNearest(crop);
99 int width = cropBounds.width();
100 int height = cropBounds.height();
102 if (mGeometry.hasSwitchedWidthHeight()){
108 if(outputX <= 0 || outputY <= 0){
116 scaleX = (float) outputX / width;
117 scaleY = (float) outputY / height;
121 temp = Bitmap.createBitmap(outputX, outputY, mConfig);
123 float[] displayCenter = {
124 temp.getWidth() / 2f, temp.getHeight() / 2f
127 Matrix m1 = mGeometry.buildTotalXform(bitmap.getWidth(), bitmap.getHeight(), displayCenter);
129 m1.postScale(scaleX, scaleY, displayCenter[0], displayCenter[1]);
131 Canvas canvas = new Canvas(temp);
132 Paint paint = new Paint();
133 paint.setAntiAlias(true);
134 paint.setFilterBitmap(true);
135 paint.setDither(true);
136 canvas.drawBitmap(bitmap, m1, paint);