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.Rect;
     22 
     23 import com.android.gallery3d.R;
     24 import com.android.gallery3d.filtershow.cache.ImageLoader;
     25 
     26 public class ImageFilterDownsample extends SimpleImageFilter {
     27     private static final int ICON_DOWNSAMPLE_FRACTION = 8;
     28     private ImageLoader mImageLoader;
     29 
     30     public ImageFilterDownsample(ImageLoader loader) {
     31         mName = "Downsample";
     32         mImageLoader = loader;
     33     }
     34 
     35     public FilterRepresentation getDefaultRepresentation() {
     36         FilterBasicRepresentation representation = (FilterBasicRepresentation) super.getDefaultRepresentation();
     37         representation.setName("Downsample");
     38         representation.setFilterClass(ImageFilterDownsample.class);
     39         representation.setMaximum(100);
     40         representation.setMinimum(1);
     41         representation.setValue(50);
     42         representation.setDefaultValue(50);
     43         representation.setPreviewValue(3);
     44         representation.setTextId(R.string.downsample);
     45         representation.setButtonId(R.id.downsampleButton);
     46         return representation;
     47     }
     48 
     49     @Override
     50     public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
     51         if (getParameters() == null) {
     52             return bitmap;
     53         }
     54         int w = bitmap.getWidth();
     55         int h = bitmap.getHeight();
     56         int p = getParameters().getValue();
     57 
     58         // size of original precached image
     59         Rect size = mImageLoader.getOriginalBounds();
     60         int orig_w = size.width();
     61         int orig_h = size.height();
     62 
     63         if (p > 0 && p < 100) {
     64             // scale preview to same size as the resulting bitmap from a "save"
     65             int newWidth = orig_w * p / 100;
     66             int newHeight = orig_h * p / 100;
     67 
     68             // only scale preview if preview isn't already scaled enough
     69             if (newWidth <= 0 || newHeight <= 0 || newWidth >= w || newHeight >= h) {
     70                 return bitmap;
     71             }
     72             Bitmap ret = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
     73             if (ret != bitmap) {
     74                 bitmap.recycle();
     75             }
     76             return ret;
     77         }
     78         return bitmap;
     79     }
     80 }
     81