ImageFilterDownsample.java revision ff6b23e655883f8f42bcf5c806f594512beaa322
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 17package com.android.gallery3d.filtershow.filters; 18 19import android.graphics.Bitmap; 20import android.graphics.Rect; 21 22import com.android.gallery3d.R; 23import com.android.gallery3d.filtershow.cache.ImageLoader; 24 25public class ImageFilterDownsample extends ImageFilter { 26 private ImageLoader mImageLoader; 27 28 public ImageFilterDownsample(ImageLoader loader) { 29 mName = "Downsample"; 30 mMaxParameter = 100; 31 mMinParameter = 1; 32 mPreviewParameter = 3; 33 mDefaultParameter = 50; 34 mParameter = 50; 35 mImageLoader = loader; 36 } 37 38 @Override 39 public int getButtonId() { 40 return R.id.downsampleButton; 41 } 42 43 @Override 44 public int getTextId() { 45 return R.string.downsample; 46 } 47 48 @Override 49 public boolean isNil() { 50 return false; 51 } 52 53 @Override 54 public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) { 55 int w = bitmap.getWidth(); 56 int h = bitmap.getHeight(); 57 int p = mParameter; 58 59 // size of original precached image 60 Rect size = mImageLoader.getOriginalBounds(); 61 int orig_w = size.width(); 62 int orig_h = size.height(); 63 64 if (p > 0 && p < 100) { 65 // scale preview to same size as the resulting bitmap from a "save" 66 int newWidth = orig_w * p / 100; 67 int newHeight = orig_h * p / 100; 68 69 // only scale preview if preview isn't already scaled enough 70 if (newWidth <= 0 || newHeight <= 0 || newWidth >= w || newHeight >= h) { 71 return bitmap; 72 } 73 Bitmap ret = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true); 74 if (ret != bitmap) { 75 bitmap.recycle(); 76 } 77 return ret; 78 } 79 return bitmap; 80 } 81} 82