19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.graphics;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1913656743cc21bac43676568314366497346713eeRomain Guy/**
2013656743cc21bac43676568314366497346713eeRomain Guy * A color filter that transforms colors through a 4x5 color matrix. This filter
2113656743cc21bac43676568314366497346713eeRomain Guy * can be used to change the saturation of pixels, convert from YUV to RGB, etc.
2213656743cc21bac43676568314366497346713eeRomain Guy *
2313656743cc21bac43676568314366497346713eeRomain Guy * @see ColorMatrix
2413656743cc21bac43676568314366497346713eeRomain Guy */
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class ColorMatrixColorFilter extends ColorFilter {
2613656743cc21bac43676568314366497346713eeRomain Guy    private final ColorMatrix mMatrix = new ColorMatrix();
2713656743cc21bac43676568314366497346713eeRomain Guy
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2913656743cc21bac43676568314366497346713eeRomain Guy     * Create a color filter that transforms colors through a 4x5 color matrix.
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param matrix 4x5 matrix used to transform colors. It is copied into
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *               the filter, so changes made to the matrix after the filter
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *               is constructed will not be reflected in the filter.
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public ColorMatrixColorFilter(ColorMatrix matrix) {
3613656743cc21bac43676568314366497346713eeRomain Guy        mMatrix.set(matrix);
3713656743cc21bac43676568314366497346713eeRomain Guy        update();
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4113656743cc21bac43676568314366497346713eeRomain Guy     * Create a color filter that transforms colors through a 4x5 color matrix.
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4313656743cc21bac43676568314366497346713eeRomain Guy     * @param array Array of floats used to transform colors, treated as a 4x5
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              matrix. The first 20 entries of the array are copied into
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              the filter. See ColorMatrix.
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public ColorMatrixColorFilter(float[] array) {
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (array.length < 20) {
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new ArrayIndexOutOfBoundsException();
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5113656743cc21bac43676568314366497346713eeRomain Guy        mMatrix.set(array);
5213656743cc21bac43676568314366497346713eeRomain Guy        update();
5313656743cc21bac43676568314366497346713eeRomain Guy    }
5413656743cc21bac43676568314366497346713eeRomain Guy
5513656743cc21bac43676568314366497346713eeRomain Guy    /**
5613656743cc21bac43676568314366497346713eeRomain Guy     * Returns the {@link ColorMatrix} used by this filter. The returned
5713656743cc21bac43676568314366497346713eeRomain Guy     * value is never null. Modifying the returned matrix does not have
5813656743cc21bac43676568314366497346713eeRomain Guy     * any effect until you call {@link #setColorMatrix(ColorMatrix)}.
5913656743cc21bac43676568314366497346713eeRomain Guy     *
6013656743cc21bac43676568314366497346713eeRomain Guy     * @see #setColorMatrix(ColorMatrix)
61f559326b182e321f51ab9711614d3e37fefa603aChris Craik     *
62f559326b182e321f51ab9711614d3e37fefa603aChris Craik     * @hide
6313656743cc21bac43676568314366497346713eeRomain Guy     */
6413656743cc21bac43676568314366497346713eeRomain Guy    public ColorMatrix getColorMatrix() {
6513656743cc21bac43676568314366497346713eeRomain Guy        return mMatrix;
6613656743cc21bac43676568314366497346713eeRomain Guy    }
6713656743cc21bac43676568314366497346713eeRomain Guy
6813656743cc21bac43676568314366497346713eeRomain Guy    /**
6913656743cc21bac43676568314366497346713eeRomain Guy     * Specifies the color matrix used by this filter. If the specified
7013656743cc21bac43676568314366497346713eeRomain Guy     * color matrix is null, this filter's color matrix will be reset to
7113656743cc21bac43676568314366497346713eeRomain Guy     * the identity matrix.
7213656743cc21bac43676568314366497346713eeRomain Guy     *
7313656743cc21bac43676568314366497346713eeRomain Guy     * @param matrix A {@link ColorMatrix} or null
7413656743cc21bac43676568314366497346713eeRomain Guy     *
7513656743cc21bac43676568314366497346713eeRomain Guy     * @see #getColorMatrix()
7613656743cc21bac43676568314366497346713eeRomain Guy     * @see android.graphics.ColorMatrix#reset()
7713656743cc21bac43676568314366497346713eeRomain Guy     * @see #setColorMatrix(float[])
78f559326b182e321f51ab9711614d3e37fefa603aChris Craik     *
79f559326b182e321f51ab9711614d3e37fefa603aChris Craik     * @hide
8013656743cc21bac43676568314366497346713eeRomain Guy     */
8113656743cc21bac43676568314366497346713eeRomain Guy    public void setColorMatrix(ColorMatrix matrix) {
8213656743cc21bac43676568314366497346713eeRomain Guy        if (matrix == null) {
8313656743cc21bac43676568314366497346713eeRomain Guy            mMatrix.reset();
8413656743cc21bac43676568314366497346713eeRomain Guy        } else if (matrix != mMatrix) {
8513656743cc21bac43676568314366497346713eeRomain Guy            mMatrix.set(matrix);
8613656743cc21bac43676568314366497346713eeRomain Guy        }
8713656743cc21bac43676568314366497346713eeRomain Guy        update();
8813656743cc21bac43676568314366497346713eeRomain Guy    }
8913656743cc21bac43676568314366497346713eeRomain Guy
9013656743cc21bac43676568314366497346713eeRomain Guy    /**
9113656743cc21bac43676568314366497346713eeRomain Guy     * Specifies the color matrix used by this filter. If the specified
9213656743cc21bac43676568314366497346713eeRomain Guy     * color matrix is null, this filter's color matrix will be reset to
9313656743cc21bac43676568314366497346713eeRomain Guy     * the identity matrix.
9413656743cc21bac43676568314366497346713eeRomain Guy     *
9513656743cc21bac43676568314366497346713eeRomain Guy     * @param array Array of floats used to transform colors, treated as a 4x5
9613656743cc21bac43676568314366497346713eeRomain Guy     *              matrix. The first 20 entries of the array are copied into
9713656743cc21bac43676568314366497346713eeRomain Guy     *              the filter. See {@link ColorMatrix}.
9813656743cc21bac43676568314366497346713eeRomain Guy     *
9913656743cc21bac43676568314366497346713eeRomain Guy     * @see #getColorMatrix()
10013656743cc21bac43676568314366497346713eeRomain Guy     * @see android.graphics.ColorMatrix#reset()
10113656743cc21bac43676568314366497346713eeRomain Guy     * @see #setColorMatrix(ColorMatrix)
10213656743cc21bac43676568314366497346713eeRomain Guy     *
10313656743cc21bac43676568314366497346713eeRomain Guy     * @throws ArrayIndexOutOfBoundsException if the specified array's
10413656743cc21bac43676568314366497346713eeRomain Guy     *         length is < 20
105f559326b182e321f51ab9711614d3e37fefa603aChris Craik     *
106f559326b182e321f51ab9711614d3e37fefa603aChris Craik     * @hide
10713656743cc21bac43676568314366497346713eeRomain Guy     */
10813656743cc21bac43676568314366497346713eeRomain Guy    public void setColorMatrix(float[] array) {
10913656743cc21bac43676568314366497346713eeRomain Guy        if (array == null) {
11013656743cc21bac43676568314366497346713eeRomain Guy            mMatrix.reset();
11113656743cc21bac43676568314366497346713eeRomain Guy        } else {
11213656743cc21bac43676568314366497346713eeRomain Guy            if (array.length < 20) {
11313656743cc21bac43676568314366497346713eeRomain Guy                throw new ArrayIndexOutOfBoundsException();
11413656743cc21bac43676568314366497346713eeRomain Guy            }
11513656743cc21bac43676568314366497346713eeRomain Guy
11613656743cc21bac43676568314366497346713eeRomain Guy            mMatrix.set(array);
11713656743cc21bac43676568314366497346713eeRomain Guy        }
11813656743cc21bac43676568314366497346713eeRomain Guy        update();
11913656743cc21bac43676568314366497346713eeRomain Guy    }
12013656743cc21bac43676568314366497346713eeRomain Guy
12113656743cc21bac43676568314366497346713eeRomain Guy    private void update() {
12213656743cc21bac43676568314366497346713eeRomain Guy        final float[] colorMatrix = mMatrix.getArray();
12376d3a1b8d035d27bc80b0f2fc480a903bd001514Derek Sollenberger        destroyFilter(native_instance);
12413656743cc21bac43676568314366497346713eeRomain Guy        native_instance = nativeColorMatrixFilter(colorMatrix);
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native long nativeColorMatrixFilter(float[] array);
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
129