ImageFilterDownsample.java revision 71f04cbaedbb89e313e0b86b531640db2d3f6016
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.Canvas; 21import android.graphics.Rect; 22 23import com.android.gallery3d.R; 24import com.android.gallery3d.filtershow.cache.ImageLoader; 25 26public 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 return representation; 45 } 46 47 @Override 48 public int getButtonId() { 49 return R.id.downsampleButton; 50 } 51 52 @Override 53 public int getTextId() { 54 return R.string.downsample; 55 } 56 57 @Override 58 public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) { 59 if (getParameters() == null) { 60 return bitmap; 61 } 62 int w = bitmap.getWidth(); 63 int h = bitmap.getHeight(); 64 int p = getParameters().getValue(); 65 66 // size of original precached image 67 Rect size = mImageLoader.getOriginalBounds(); 68 int orig_w = size.width(); 69 int orig_h = size.height(); 70 71 if (p > 0 && p < 100) { 72 // scale preview to same size as the resulting bitmap from a "save" 73 int newWidth = orig_w * p / 100; 74 int newHeight = orig_h * p / 100; 75 76 // only scale preview if preview isn't already scaled enough 77 if (newWidth <= 0 || newHeight <= 0 || newWidth >= w || newHeight >= h) { 78 return bitmap; 79 } 80 Bitmap ret = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true); 81 if (ret != bitmap) { 82 bitmap.recycle(); 83 } 84 return ret; 85 } 86 return bitmap; 87 } 88 89 @Override 90 public Bitmap iconApply(Bitmap bitmap, float scaleFactor, boolean highQuality) { 91 int w = bitmap.getWidth(); 92 int h = bitmap.getHeight(); 93 Bitmap ret = Bitmap.createScaledBitmap(bitmap, w / ICON_DOWNSAMPLE_FRACTION, h 94 / ICON_DOWNSAMPLE_FRACTION, false); 95 Rect dst = new Rect(0, 0, w, h); 96 Rect src = new Rect(0, 0, w / ICON_DOWNSAMPLE_FRACTION, h / ICON_DOWNSAMPLE_FRACTION); 97 Canvas c = new Canvas(bitmap); 98 c.drawBitmap(ret, src, dst, null); 99 return bitmap; 100 } 101} 102