OSDN Git Service

Move classes to pipeline package
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / filters / ImageFilterDownsample.java
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
17 package com.android.gallery3d.filtershow.filters;
18
19 import android.graphics.Bitmap;
20 import android.graphics.Canvas;
21 import android.graphics.Rect;
22
23 import com.android.gallery3d.R;
24 import com.android.gallery3d.filtershow.cache.ImageLoader;
25
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;
30
31     public ImageFilterDownsample(ImageLoader loader) {
32         mName = "Downsample";
33         mImageLoader = loader;
34     }
35
36     public FilterRepresentation getDefaultRepresentation() {
37         FilterBasicRepresentation representation = (FilterBasicRepresentation) super.getDefaultRepresentation();
38         representation.setName("Downsample");
39         representation.setSerializationName(SERIALIZATION_NAME);
40
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;
49     }
50
51     @Override
52     public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
53         if (getParameters() == null) {
54             return bitmap;
55         }
56         int w = bitmap.getWidth();
57         int h = bitmap.getHeight();
58         int p = getParameters().getValue();
59
60         // size of original precached image
61         Rect size = mImageLoader.getOriginalBounds();
62         int orig_w = size.width();
63         int orig_h = size.height();
64
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;
69
70             // only scale preview if preview isn't already scaled enough
71             if (newWidth <= 0 || newHeight <= 0 || newWidth >= w || newHeight >= h) {
72                 return bitmap;
73             }
74             Bitmap ret = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
75             if (ret != bitmap) {
76                 bitmap.recycle();
77             }
78             return ret;
79         }
80         return bitmap;
81     }
82 }