ImageFilterDownsample.java revision b5abda28145e68e68a74a5aa2004361cf62edcc2
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;
20
21import com.android.gallery3d.R;
22
23public class ImageFilterDownsample extends ImageFilter {
24
25    public ImageFilterDownsample() {
26        mName = "Downsample";
27        mMaxParameter = 100;
28        mMinParameter = 5;
29        mPreviewParameter = 10;
30        mDefaultParameter = 50;
31        mParameter = 50;
32    }
33
34    @Override
35    public int getButtonId() {
36        return R.id.downsampleButton;
37    }
38
39    @Override
40    public int getTextId() {
41        return R.string.downsample;
42    }
43
44    @Override
45    public boolean isNil() {
46        return false;
47    }
48
49    @Override
50    public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
51        int w = bitmap.getWidth();
52        int h = bitmap.getHeight();
53        int p = mParameter;
54        if (p > 0 && p < 100) {
55            int newWidth =  w * p / 100;
56            int newHeight = h * p / 100;
57            if (newWidth <= 0 || newHeight <= 0) {
58                return bitmap;
59            }
60            Bitmap ret = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
61            if (ret != bitmap) {
62                bitmap.recycle();
63            }
64            return ret;
65        }
66        return bitmap;
67    }
68}
69