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