Merge "Improve ColorMatrix docs" into lmp-mr1-dev

This commit is contained in:
Chris Craik
2014-11-20 21:39:45 +00:00
committed by Android (Google) Code Review

View File

@@ -21,23 +21,43 @@ import android.util.FloatMath;
import java.util.Arrays;
/**
* 4x5 matrix for transforming the color+alpha components of a Bitmap.
* The matrix is stored in a single array, and its treated as follows:
* 4x5 matrix for transforming the color and alpha components of a Bitmap.
* The matrix can be passed as single array, and is treated as follows:
*
* <pre>
* [ a, b, c, d, e,
* f, g, h, i, j,
* k, l, m, n, o,
* p, q, r, s, t ]
* </pre>
* p, q, r, s, t ]</pre>
*
* <p>
* When applied to a color <code>[R, G, B, A]</code>, the resulting color
* is computed as:
* </p>
*
* When applied to a color <code>[r, g, b, a]</code>, the resulting color
* is computed as (after clamping):
* <pre>
* R' = a*R + b*G + c*B + d*A + e;
* G' = f*R + g*G + h*B + i*A + j;
* B' = k*R + l*G + m*B + n*A + o;
* A' = p*R + q*G + r*B + s*A + t;
* </pre>
* R&rsquo; = a*R + b*G + c*B + d*A + e;
* G&rsquo; = f*R + g*G + h*B + i*A + j;
* B&rsquo; = k*R + l*G + m*B + n*A + o;
* A&rsquo; = p*R + q*G + r*B + s*A + t;</pre>
*
* <p>
* That resulting color <code>[R&rsquo;, G&rsquo;, B&rsquo;, A&rsquo;]</code>
* then has each channel clamped to the <code>0</code> to <code>255</code>
* range.
* </p>
*
* <p>
* The sample ColorMatrix below inverts incoming colors by scaling each
* channel by <code>-1</code>, and then shifting the result up by
* <code>255</code> to remain in the standard color space.
* </p>
*
* <pre>
* [ -1, 0, 0, 0, 255,
* 0, -1, 0, 0, 255,
* 0, 0, -1, 0, 255,
* 0, 0, 0, 1, 0 ]</pre>
*/
@SuppressWarnings({ "MismatchedReadAndWriteOfArray", "PointlessArithmeticExpression" })
public class ColorMatrix {
@@ -52,24 +72,24 @@ public class ColorMatrix {
}
/**
* Create a new colormatrix initialized with the specified array of values.
* Create a new colormatrix initialized with the specified array of values.
*/
public ColorMatrix(float[] src) {
System.arraycopy(src, 0, mArray, 0, 20);
}
/**
* Create a new colormatrix initialized with the specified colormatrix.
*/
public ColorMatrix(ColorMatrix src) {
System.arraycopy(src.mArray, 0, mArray, 0, 20);
}
/**
* Return the array of floats representing this colormatrix.
*/
public final float[] getArray() { return mArray; }
/**
* Set this colormatrix to identity:
* <pre>
@@ -84,7 +104,7 @@ public class ColorMatrix {
Arrays.fill(a, 0);
a[0] = a[6] = a[12] = a[18] = 1;
}
/**
* Assign the src colormatrix into this matrix, copying all of its values.
*/
@@ -98,7 +118,7 @@ public class ColorMatrix {
public void set(float[] src) {
System.arraycopy(src, 0, mArray, 0, 20);
}
/**
* Set this colormatrix to scale by the specified values.
*/
@@ -114,12 +134,14 @@ public class ColorMatrix {
a[12] = bScale;
a[18] = aScale;
}
/**
* Set the rotation on a color axis by the specified values.
* <p>
* <code>axis=0</code> correspond to a rotation around the RED color
* <code>axis=1</code> correspond to a rotation around the GREEN color
* <code>axis=2</code> correspond to a rotation around the BLUE color
* </p>
*/
public void setRotate(int axis, float degrees) {
reset();
@@ -153,8 +175,10 @@ public class ColorMatrix {
/**
* Set this colormatrix to the concatenation of the two specified
* colormatrices, such that the resulting colormatrix has the same effect
* as applying matB and then applying matA. It is legal for either matA or
* matB to be the same colormatrix as this.
* as applying matB and then applying matA.
* <p>
* It is legal for either matA or matB to be the same colormatrix as this.
* </p>
*/
public void setConcat(ColorMatrix matA, ColorMatrix matB) {
float[] tmp;
@@ -163,7 +187,7 @@ public class ColorMatrix {
} else {
tmp = mArray;
}
final float[] a = matA.mArray;
final float[] b = matB.mArray;
int index = 0;
@@ -176,38 +200,43 @@ public class ColorMatrix {
a[j + 2] * b[14] + a[j + 3] * b[19] +
a[j + 4];
}
if (tmp != mArray) {
System.arraycopy(tmp, 0, mArray, 0, 20);
}
}
/**
* Concat this colormatrix with the specified prematrix. This is logically
* the same as calling setConcat(this, prematrix);
* Concat this colormatrix with the specified prematrix.
* <p>
* This is logically the same as calling setConcat(this, prematrix);
* </p>
*/
public void preConcat(ColorMatrix prematrix) {
setConcat(this, prematrix);
}
/**
* Concat this colormatrix with the specified postmatrix. This is logically
* the same as calling setConcat(postmatrix, this);
* Concat this colormatrix with the specified postmatrix.
* <p>
* This is logically the same as calling setConcat(postmatrix, this);
* </p>
*/
public void postConcat(ColorMatrix postmatrix) {
setConcat(postmatrix, this);
}
///////////////////////////////////////////////////////////////////////////
/**
* Set the matrix to affect the saturation of colors. A value of 0 maps the
* color to gray-scale. 1 is identity.
* Set the matrix to affect the saturation of colors.
*
* @param sat A value of 0 maps the color to gray-scale. 1 is identity.
*/
public void setSaturation(float sat) {
reset();
float[] m = mArray;
final float invSat = 1 - sat;
final float R = 0.213f * invSat;
final float G = 0.715f * invSat;
@@ -217,7 +246,7 @@ public class ColorMatrix {
m[5] = R; m[6] = G + sat; m[7] = B;
m[10] = R; m[11] = G; m[12] = B + sat;
}
/**
* Set the matrix to convert RGB to YUV
*/
@@ -229,7 +258,7 @@ public class ColorMatrix {
m[5] = -0.16874f; m[6] = -0.33126f; m[7] = 0.5f;
m[10] = 0.5f; m[11] = -0.41869f; m[12] = -0.08131f;
}
/**
* Set the matrix to convert from YUV to RGB
*/
@@ -242,4 +271,3 @@ public class ColorMatrix {
m[10] = 1; m[11] = 1.772f; m[12] = 0;
}
}