ImageFilterDownsample.java revision f4486566491c2f6c51817f92ee1000d12db41c1b
1103ccde8fe2f2c8abde914a8ba736b2e9cb8d20bElliott Hughes/*
2103ccde8fe2f2c8abde914a8ba736b2e9cb8d20bElliott Hughes * Copyright (C) 2012 The Android Open Source Project
39abbbdc5346020e33a8fdbe7254dd0fdff9df616Elliott Hughes *
41fa0d849576555577ffd9675677a3c95f21b754eRaghu Gandham * Licensed under the Apache License, Version 2.0 (the "License");
59abbbdc5346020e33a8fdbe7254dd0fdff9df616Elliott Hughes * you may not use this file except in compliance with the License.
61fa0d849576555577ffd9675677a3c95f21b754eRaghu Gandham * You may obtain a copy of the License at
7eae27dc55adca75c2332e4b767ec667acfbbbcb3Elliott Hughes *
8eae27dc55adca75c2332e4b767ec667acfbbbcb3Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
91fa0d849576555577ffd9675677a3c95f21b754eRaghu Gandham *
10eae27dc55adca75c2332e4b767ec667acfbbbcb3Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11eae27dc55adca75c2332e4b767ec667acfbbbcb3Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12eae27dc55adca75c2332e4b767ec667acfbbbcb3Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131fa0d849576555577ffd9675677a3c95f21b754eRaghu Gandham * See the License for the specific language governing permissions and
141fa0d849576555577ffd9675677a3c95f21b754eRaghu Gandham * limitations under the License.
157efad83d430f4d824f2aaa75edea5106f6ff8aaeElliott Hughes */
16eae27dc55adca75c2332e4b767ec667acfbbbcb3Elliott Hughes
171fa0d849576555577ffd9675677a3c95f21b754eRaghu Gandhampackage com.android.gallery3d.filtershow.filters;
181fa0d849576555577ffd9675677a3c95f21b754eRaghu Gandham
199abbbdc5346020e33a8fdbe7254dd0fdff9df616Elliott Hughesimport 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        representation.setTextId(R.string.downsample);
45        representation.setButtonId(R.id.downsampleButton);
46        return representation;
47    }
48
49    @Override
50    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
51        if (getParameters() == null) {
52            return bitmap;
53        }
54        int w = bitmap.getWidth();
55        int h = bitmap.getHeight();
56        int p = getParameters().getValue();
57
58        // size of original precached image
59        Rect size = mImageLoader.getOriginalBounds();
60        int orig_w = size.width();
61        int orig_h = size.height();
62
63        if (p > 0 && p < 100) {
64            // scale preview to same size as the resulting bitmap from a "save"
65            int newWidth = orig_w * p / 100;
66            int newHeight = orig_h * p / 100;
67
68            // only scale preview if preview isn't already scaled enough
69            if (newWidth <= 0 || newHeight <= 0 || newWidth >= w || newHeight >= h) {
70                return bitmap;
71            }
72            Bitmap ret = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
73            if (ret != bitmap) {
74                bitmap.recycle();
75            }
76            return ret;
77        }
78        return bitmap;
79    }
80}
81