2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.filtershow.filters;
19 import android.graphics.Bitmap;
20 import android.graphics.Canvas;
21 import android.graphics.Rect;
23 import com.android.gallery3d.R;
24 import com.android.gallery3d.filtershow.cache.ImageLoader;
26 public class ImageFilterDownsample extends SimpleImageFilter {
27 private static final String SERIALIZATION_NAME = "DOWNSAMPLE";
28 private static final int ICON_DOWNSAMPLE_FRACTION = 8;
29 private ImageLoader mImageLoader;
31 public ImageFilterDownsample(ImageLoader loader) {
33 mImageLoader = loader;
36 public FilterRepresentation getDefaultRepresentation() {
37 FilterBasicRepresentation representation = (FilterBasicRepresentation) super.getDefaultRepresentation();
38 representation.setName("Downsample");
39 representation.setSerializationName(SERIALIZATION_NAME);
41 representation.setFilterClass(ImageFilterDownsample.class);
42 representation.setMaximum(100);
43 representation.setMinimum(1);
44 representation.setValue(50);
45 representation.setDefaultValue(50);
46 representation.setPreviewValue(3);
47 representation.setTextId(R.string.downsample);
48 return representation;
52 public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
53 if (getParameters() == null) {
56 int w = bitmap.getWidth();
57 int h = bitmap.getHeight();
58 int p = getParameters().getValue();
60 // size of original precached image
61 Rect size = mImageLoader.getOriginalBounds();
62 int orig_w = size.width();
63 int orig_h = size.height();
65 if (p > 0 && p < 100) {
66 // scale preview to same size as the resulting bitmap from a "save"
67 int newWidth = orig_w * p / 100;
68 int newHeight = orig_h * p / 100;
70 // only scale preview if preview isn't already scaled enough
71 if (newWidth <= 0 || newHeight <= 0 || newWidth >= w || newHeight >= h) {
74 Bitmap ret = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);