Home | History | Annotate | Download | only in filters
      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.Paint;
     22 import android.graphics.Rect;
     23 
     24 public class ImageFilterStraighten extends ImageFilter {
     25     private final Bitmap.Config mConfig = Bitmap.Config.ARGB_8888;
     26     private float mRotation;
     27     private float mZoomFactor;
     28 
     29     public ImageFilterStraighten() {
     30         mName = "Straighten";
     31     }
     32 
     33     @Override
     34     public ImageFilter clone() throws CloneNotSupportedException {
     35         // FIXME: clone() should not be needed. Remove when we fix geometry.
     36         ImageFilterStraighten filter = (ImageFilterStraighten) super.clone();
     37         filter.mRotation = mRotation;
     38         filter.mZoomFactor = mZoomFactor;
     39         return filter;
     40     }
     41 
     42     public ImageFilterStraighten(float rotation, float zoomFactor) {
     43         mRotation = rotation;
     44         mZoomFactor = zoomFactor;
     45     }
     46 
     47     public void setRotation(float rotation) {
     48         mRotation = rotation;
     49     }
     50 
     51     public void setRotationZoomFactor(float zoomFactor) {
     52         mZoomFactor = zoomFactor;
     53     }
     54 
     55     @Override
     56     public void useRepresentation(FilterRepresentation representation) {
     57 
     58     }
     59 
     60     @Override
     61     public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
     62         // TODO: implement bilinear or bicubic here... for now, just use
     63         // canvas to do a simple implementation...
     64         // TODO: and be more memory efficient! (do it in native?)
     65 
     66         Bitmap temp = bitmap.copy(mConfig, true);
     67         Canvas canvas = new Canvas(temp);
     68         canvas.save();
     69         Rect bounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
     70         float w = temp.getWidth();
     71         float h = temp.getHeight();
     72         float mw = temp.getWidth() / 2.0f;
     73         float mh = temp.getHeight() / 2.0f;
     74 
     75         canvas.scale(mZoomFactor, mZoomFactor, mw, mh);
     76         canvas.rotate(mRotation, mw, mh);
     77         canvas.drawBitmap(bitmap, bounds, bounds, new Paint());
     78         canvas.restore();
     79 
     80         int[] pixels = new int[(int) (w * h)];
     81         temp.getPixels(pixels, 0, (int) w, 0, 0, (int) w, (int) h);
     82         bitmap.setPixels(pixels, 0, (int) w, 0, 0, (int) w, (int) h);
     83         temp.recycle();
     84         temp = null;
     85         pixels = null;
     86         return bitmap;
     87     }
     88 
     89 }
     90