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
196097eca72134034fcc6086c110673b5df94913b0Chris Craikimport android.annotation.NonNull;
206097eca72134034fcc6086c110673b5df94913b0Chris Craikimport android.annotation.Nullable;
216097eca72134034fcc6086c110673b5df94913b0Chris Craik
2213656743cc21bac43676568314366497346713eeRomain Guy/**
2313656743cc21bac43676568314366497346713eeRomain Guy * A color filter that transforms colors through a 4x5 color matrix. This filter
2413656743cc21bac43676568314366497346713eeRomain Guy * can be used to change the saturation of pixels, convert from YUV to RGB, etc.
2513656743cc21bac43676568314366497346713eeRomain Guy *
2613656743cc21bac43676568314366497346713eeRomain Guy * @see ColorMatrix
2713656743cc21bac43676568314366497346713eeRomain Guy */
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class ColorMatrixColorFilter extends ColorFilter {
2913656743cc21bac43676568314366497346713eeRomain Guy    private final ColorMatrix mMatrix = new ColorMatrix();
3013656743cc21bac43676568314366497346713eeRomain Guy
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3213656743cc21bac43676568314366497346713eeRomain Guy     * Create a color filter that transforms colors through a 4x5 color matrix.
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param matrix 4x5 matrix used to transform colors. It is copied into
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *               the filter, so changes made to the matrix after the filter
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *               is constructed will not be reflected in the filter.
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
386097eca72134034fcc6086c110673b5df94913b0Chris Craik    public ColorMatrixColorFilter(@NonNull ColorMatrix matrix) {
3913656743cc21bac43676568314366497346713eeRomain Guy        mMatrix.set(matrix);
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4313656743cc21bac43676568314366497346713eeRomain Guy     * Create a color filter that transforms colors through a 4x5 color matrix.
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4513656743cc21bac43676568314366497346713eeRomain Guy     * @param array Array of floats used to transform colors, treated as a 4x5
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              matrix. The first 20 entries of the array are copied into
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              the filter. See ColorMatrix.
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
496097eca72134034fcc6086c110673b5df94913b0Chris Craik    public ColorMatrixColorFilter(@NonNull float[] array) {
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (array.length < 20) {
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new ArrayIndexOutOfBoundsException();
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5313656743cc21bac43676568314366497346713eeRomain Guy        mMatrix.set(array);
5413656743cc21bac43676568314366497346713eeRomain Guy    }
5513656743cc21bac43676568314366497346713eeRomain Guy
5613656743cc21bac43676568314366497346713eeRomain Guy    /**
576097eca72134034fcc6086c110673b5df94913b0Chris Craik     * Copies the ColorMatrix from the filter into the passed ColorMatrix.
58f559326b182e321f51ab9711614d3e37fefa603aChris Craik     *
596097eca72134034fcc6086c110673b5df94913b0Chris Craik     * @param colorMatrix Set to the current value of the filter's ColorMatrix.
6013656743cc21bac43676568314366497346713eeRomain Guy     */
616097eca72134034fcc6086c110673b5df94913b0Chris Craik    public void getColorMatrix(ColorMatrix colorMatrix) {
626097eca72134034fcc6086c110673b5df94913b0Chris Craik        colorMatrix.set(mMatrix);
6313656743cc21bac43676568314366497346713eeRomain Guy    }
6413656743cc21bac43676568314366497346713eeRomain Guy
6513656743cc21bac43676568314366497346713eeRomain Guy    /**
666097eca72134034fcc6086c110673b5df94913b0Chris Craik     * Copies the provided color matrix to be used by this filter.
6713656743cc21bac43676568314366497346713eeRomain Guy     *
686097eca72134034fcc6086c110673b5df94913b0Chris Craik     * If the specified color matrix is null, this filter's color matrix will be reset to the
696097eca72134034fcc6086c110673b5df94913b0Chris Craik     * identity matrix.
7013656743cc21bac43676568314366497346713eeRomain Guy     *
716097eca72134034fcc6086c110673b5df94913b0Chris Craik     * @param matrix A {@link ColorMatrix} or null
72f559326b182e321f51ab9711614d3e37fefa603aChris Craik     *
736097eca72134034fcc6086c110673b5df94913b0Chris Craik     * @see #getColorMatrix(ColorMatrix)
746097eca72134034fcc6086c110673b5df94913b0Chris Craik     * @see #setColorMatrixArray(float[])
756097eca72134034fcc6086c110673b5df94913b0Chris Craik     * @see ColorMatrix#reset()
768a6ad4a89d1b9ed61d3ec6f5bc12d479bc8d9f3bChris Craik     *
778a6ad4a89d1b9ed61d3ec6f5bc12d479bc8d9f3bChris Craik     * @hide
7813656743cc21bac43676568314366497346713eeRomain Guy     */
796097eca72134034fcc6086c110673b5df94913b0Chris Craik    public void setColorMatrix(@Nullable ColorMatrix matrix) {
806097eca72134034fcc6086c110673b5df94913b0Chris Craik        discardNativeInstance();
8113656743cc21bac43676568314366497346713eeRomain Guy        if (matrix == null) {
8213656743cc21bac43676568314366497346713eeRomain Guy            mMatrix.reset();
836097eca72134034fcc6086c110673b5df94913b0Chris Craik        } else {
8413656743cc21bac43676568314366497346713eeRomain Guy            mMatrix.set(matrix);
8513656743cc21bac43676568314366497346713eeRomain Guy        }
8613656743cc21bac43676568314366497346713eeRomain Guy    }
8713656743cc21bac43676568314366497346713eeRomain Guy
8813656743cc21bac43676568314366497346713eeRomain Guy    /**
896097eca72134034fcc6086c110673b5df94913b0Chris Craik     * Copies the provided color matrix to be used by this filter.
906097eca72134034fcc6086c110673b5df94913b0Chris Craik     *
916097eca72134034fcc6086c110673b5df94913b0Chris Craik     * If the specified color matrix is null, this filter's color matrix will be reset to the
926097eca72134034fcc6086c110673b5df94913b0Chris Craik     * identity matrix.
9313656743cc21bac43676568314366497346713eeRomain Guy     *
9413656743cc21bac43676568314366497346713eeRomain Guy     * @param array Array of floats used to transform colors, treated as a 4x5
9513656743cc21bac43676568314366497346713eeRomain Guy     *              matrix. The first 20 entries of the array are copied into
9613656743cc21bac43676568314366497346713eeRomain Guy     *              the filter. See {@link ColorMatrix}.
9713656743cc21bac43676568314366497346713eeRomain Guy     *
986097eca72134034fcc6086c110673b5df94913b0Chris Craik     * @see #getColorMatrix(ColorMatrix)
9913656743cc21bac43676568314366497346713eeRomain Guy     * @see #setColorMatrix(ColorMatrix)
1006097eca72134034fcc6086c110673b5df94913b0Chris Craik     * @see ColorMatrix#reset()
10113656743cc21bac43676568314366497346713eeRomain Guy     *
10213656743cc21bac43676568314366497346713eeRomain Guy     * @throws ArrayIndexOutOfBoundsException if the specified array's
10313656743cc21bac43676568314366497346713eeRomain Guy     *         length is < 20
1048a6ad4a89d1b9ed61d3ec6f5bc12d479bc8d9f3bChris Craik     *
1058a6ad4a89d1b9ed61d3ec6f5bc12d479bc8d9f3bChris Craik     * @hide
10613656743cc21bac43676568314366497346713eeRomain Guy     */
1076097eca72134034fcc6086c110673b5df94913b0Chris Craik    public void setColorMatrixArray(@Nullable float[] array) {
1086097eca72134034fcc6086c110673b5df94913b0Chris Craik        // called '...Array' so that passing null isn't ambiguous
1096097eca72134034fcc6086c110673b5df94913b0Chris Craik        discardNativeInstance();
11013656743cc21bac43676568314366497346713eeRomain Guy        if (array == null) {
11113656743cc21bac43676568314366497346713eeRomain Guy            mMatrix.reset();
11213656743cc21bac43676568314366497346713eeRomain Guy        } else {
11313656743cc21bac43676568314366497346713eeRomain Guy            if (array.length < 20) {
11413656743cc21bac43676568314366497346713eeRomain Guy                throw new ArrayIndexOutOfBoundsException();
11513656743cc21bac43676568314366497346713eeRomain Guy            }
11613656743cc21bac43676568314366497346713eeRomain Guy            mMatrix.set(array);
11713656743cc21bac43676568314366497346713eeRomain Guy        }
11813656743cc21bac43676568314366497346713eeRomain Guy    }
11913656743cc21bac43676568314366497346713eeRomain Guy
1206097eca72134034fcc6086c110673b5df94913b0Chris Craik    @Override
1216097eca72134034fcc6086c110673b5df94913b0Chris Craik    long createNativeInstance() {
1226097eca72134034fcc6086c110673b5df94913b0Chris Craik        return nativeColorMatrixFilter(mMatrix.getArray());
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native long nativeColorMatrixFilter(float[] array);
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
127