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 representation.setButtonId(R.id.downsampleButton);
49 return representation;
53 public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
54 if (getParameters() == null) {
57 int w = bitmap.getWidth();
58 int h = bitmap.getHeight();
59 int p = getParameters().getValue();
61 // size of original precached image
62 Rect size = mImageLoader.getOriginalBounds();
63 int orig_w = size.width();
64 int orig_h = size.height();
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;
71 // only scale preview if preview isn't already scaled enough
72 if (newWidth <= 0 || newHeight <= 0 || newWidth >= w || newHeight >= h) {
75 Bitmap ret = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);