OSDN Git Service

am e324a388: Merge "Add drop shadow to preview thumb" into gb-ub-photos-bryce
[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         representation.setButtonId(R.id.downsampleButton);
49         return representation;
50     }
51
52     @Override
53     public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
54         if (getParameters() == null) {
55             return bitmap;
56         }
57         int w = bitmap.getWidth();
58         int h = bitmap.getHeight();
59         int p = getParameters().getValue();
60
61         // size of original precached image
62         Rect size = mImageLoader.getOriginalBounds();
63         int orig_w = size.width();
64         int orig_h = size.height();
65
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;
70
71             // only scale preview if preview isn't already scaled enough
72             if (newWidth <= 0 || newHeight <= 0 || newWidth >= w || newHeight >= h) {
73                 return bitmap;
74             }
75             Bitmap ret = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
76             if (ret != bitmap) {
77                 bitmap.recycle();
78             }
79             return ret;
80         }
81         return bitmap;
82     }
83 }