summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/filters/ImageFilterGradient.java
blob: a0b733abde16fad12e3d2cff12709d745e08957a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.filtershow.filters;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Shader.TileMode;

public class ImageFilterGradient extends ImageFilter {

    private Bitmap mGradientBitmap = null;
    private int[] mColors = null;
    private float[] mPositions = null;

    public ImageFilterGradient() {
        mName = "Gradient";
    }

    @Override
    public ImageFilter clone() throws CloneNotSupportedException {
        // FIXME: clone() should not be needed. Remove?.
        ImageFilterGradient filter = (ImageFilterGradient) super.clone();
        System.arraycopy(mColors, 0, filter.mColors, 0, mColors.length);
        System.arraycopy(mPositions, 0, filter.mPositions, 0, mPositions.length);
        return filter;
    }

    public void addColor(int color, float position) {
        int length = 0;
        if (mColors != null) {
            length = mColors.length;
        }
        int[] colors = new int[length + 1];
        float[] positions = new float[length + 1];

        for (int i = 0; i < length; i++) {
            colors[i] = mColors[i];
            positions[i] = mPositions[i];
        }

        colors[length] = color;
        positions[length] = position;

        mColors = colors;
        mPositions = positions;
    }

    @Override
    public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
        createGradient();
        int[] gradient = new int[256];
        int[] redGradient = new int[256];
        int[] greenGradient = new int[256];
        int[] blueGradient = new int[256];
        mGradientBitmap.getPixels(gradient, 0, 256, 0, 0, 256, 1);

        for (int i = 0; i < 256; i++) {
            redGradient[i] = Color.red(gradient[i]);
            greenGradient[i] = Color.green(gradient[i]);
            blueGradient[i] = Color.blue(gradient[i]);
        }
        nativeApplyGradientFilter(bitmap, bitmap.getWidth(), bitmap.getHeight(),
                redGradient, greenGradient, blueGradient);
        return bitmap;
    }

    public void createGradient() {
        if (mGradientBitmap != null) {
            return;
        }

        /* Create a 200 x 200 bitmap and fill it with black. */
        Bitmap b = Bitmap.createBitmap(256, 1, Config.ARGB_8888);
        Canvas c = new Canvas(b);
        c.drawColor(Color.BLACK);

        /* Create your gradient. */

        /*
         * int[] colors = new int[2]; colors[0] = Color.argb(255, 20, 20, 10);
         * colors[0] = Color.BLACK; colors[1] = Color.argb(255, 228, 231, 193);
         * float[] positions = new float[2]; positions[0] = 0; positions[1] = 1;
         */

        LinearGradient grad = new LinearGradient(0, 0, 255, 1, mColors,
                mPositions, TileMode.CLAMP);

        /* Draw your gradient to the top of your bitmap. */
        Paint p = new Paint();
        p.setStyle(Style.FILL);
        p.setShader(grad);
        c.drawRect(0, 0, 256, 1, p);
        mGradientBitmap = b;
    }

}