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;
20import android.graphics.Canvas;
21import android.graphics.Color;
22import android.graphics.Paint;
23import android.graphics.Path;
24import android.graphics.RectF;
25
26public class ImageFilterColorBorder extends ImageFilter {
27    private static final String LOGTAG = "ImageFilterColorBorder";
28    private FilterColorBorderRepresentation mParameters = null;
29    Paint mPaint = new Paint();
30    RectF mBounds = new RectF();
31    RectF mInsideBounds = new RectF();
32    Path mBorderPath = new Path();
33
34    public ImageFilterColorBorder() {
35        mName = "Border";
36    }
37
38    public FilterRepresentation getDefaultRepresentation() {
39        return new FilterColorBorderRepresentation(Color.WHITE, 3, 2);
40    }
41
42    public void useRepresentation(FilterRepresentation representation) {
43        FilterColorBorderRepresentation parameters =
44                (FilterColorBorderRepresentation) representation;
45        mParameters = parameters;
46    }
47
48    public FilterColorBorderRepresentation getParameters() {
49        return mParameters;
50    }
51
52    private void applyHelper(Canvas canvas, int w, int h) {
53        if (getParameters() == null) {
54            return;
55        }
56        float size = getParameters().getBorderSize();
57        float radius = getParameters().getBorderRadius();
58
59        mPaint.reset();
60        mPaint.setColor(getParameters().getColor());
61        mPaint.setAntiAlias(true);
62        mBounds.set(0, 0, w, h);
63        mBorderPath.reset();
64        mBorderPath.moveTo(0, 0);
65
66        float bs = size / 100.f * mBounds.width();
67        float r = radius / 100.f * mBounds.width();
68
69        mInsideBounds.set(mBounds.left + bs,
70                mBounds.top + bs, mBounds.right - bs,
71                mBounds.bottom - bs);
72
73        mBorderPath.moveTo(mBounds.left, mBounds.top);
74        mBorderPath.lineTo(mBounds.right, mBounds.top);
75        mBorderPath.lineTo(mBounds.right, mBounds.bottom);
76        mBorderPath.lineTo(mBounds.left, mBounds.bottom);
77        mBorderPath.addRoundRect(mInsideBounds,
78                r, r, Path.Direction.CCW);
79
80        canvas.drawPath(mBorderPath, mPaint);
81    }
82
83    @Override
84    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
85        Canvas canvas = new Canvas(bitmap);
86        applyHelper(canvas, bitmap.getWidth(), bitmap.getHeight());
87        return bitmap;
88    }
89
90}
91