1 /* 2 * Copyright (C) 2008 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 package android.graphics.cts; 17 18 import android.graphics.ColorMatrix; 19 import android.test.AndroidTestCase; 20 21 public class ColorMatrixTest extends AndroidTestCase { 22 private ColorMatrix mColorMatrix; 23 24 private final float[] mSrc = new float[]{ 25 0, 1, 2, 3, 4, 26 5, 6, 7, 8, 9, 27 10, 11, 12, 13, 14, 28 15, 16, 17, 18, 19 29 }; 30 31 private static final float TOLERANCE = 0.0000001f; 32 33 @Override setUp()34 protected void setUp() throws Exception { 35 super.setUp(); 36 37 mColorMatrix = new ColorMatrix(mSrc); 38 } 39 testColorMatrix()40 public void testColorMatrix(){ 41 new ColorMatrix(); 42 43 ColorMatrix cM1 = new ColorMatrix(mSrc); 44 float[] fA1 = cM1.getArray(); 45 assertTrue(mSrc.length == fA1.length); 46 int len = mSrc.length; 47 48 for(int i = 0; i < len; i++){ 49 assertEquals(mSrc[i], fA1[i]); 50 } 51 52 ColorMatrix cM2 = new ColorMatrix(cM1); 53 float[] fA2 = cM2.getArray(); 54 assertTrue(fA1.length == fA2.length); 55 len = fA1.length; 56 57 for(int i = 0; i < len; i++){ 58 assertEquals(fA1[i], fA2[i]); 59 } 60 61 } 62 testReset()63 public void testReset(){ 64 float[] ret = mColorMatrix.getArray(); 65 preCompare(ret); 66 67 mColorMatrix.reset(); 68 ret = mColorMatrix.getArray(); 69 assertEquals(20, ret.length); 70 71 for(int i = 0; i <= 19; i++){ 72 if(0 == i % 6){ 73 assertEquals(1.0f, ret[i]); 74 continue; 75 } 76 77 assertEquals(0.0f, ret[i]); 78 } 79 } 80 testSet1()81 public void testSet1(){ 82 float[] ret = mColorMatrix.getArray(); 83 preCompare(ret); 84 85 float[] fArray = new float[]{ 86 19, 18, 17, 16, 15, 87 14, 13, 12, 11, 10, 88 9, 8, 7, 6, 5, 89 4, 3, 2, 1, 0 90 }; 91 92 mColorMatrix.set(fArray); 93 94 ret = mColorMatrix.getArray(); 95 assertEquals(20, ret.length); 96 97 for(int i = 19; i >= 0; i--){ 98 assertEquals((float) i, ret[19 - i]); 99 } 100 } 101 testSet2()102 public void testSet2(){ 103 float[] ret = mColorMatrix.getArray(); 104 preCompare(ret); 105 106 float[] fArray = new float[]{ 107 19, 18, 17, 16, 15, 108 14, 13, 12, 11, 10, 109 9, 8, 7, 6, 5, 110 4, 3, 2, 1, 0 111 }; 112 113 mColorMatrix.set(new ColorMatrix(fArray)); 114 115 ret = mColorMatrix.getArray(); 116 assertEquals(20, ret.length); 117 118 for(int i = 19; i >= 0; i--){ 119 assertEquals((float) i, ret[19 - i]); 120 } 121 } 122 testSetRotate()123 public void testSetRotate(){ 124 // abnormal case: IllegalArgument axis 125 try{ 126 mColorMatrix.setRotate(4, 90); 127 fail("shouldn't come to here"); 128 }catch(RuntimeException e){ 129 //expected 130 } 131 132 mColorMatrix.setRotate(0, 180); 133 float[] ret = mColorMatrix.getArray(); 134 assertEquals(-1.0f, ret[6], TOLERANCE); 135 assertEquals(-1.0f, ret[12], TOLERANCE); 136 assertEquals(0, ret[7], TOLERANCE); 137 assertEquals(0, ret[11], TOLERANCE); 138 139 mColorMatrix.setRotate(1, 180); 140 assertEquals(-1.0f, ret[0], TOLERANCE); 141 assertEquals(-1.0f, ret[12], TOLERANCE); 142 assertEquals(0, ret[2], TOLERANCE); 143 assertEquals(0, ret[10], TOLERANCE); 144 145 mColorMatrix.setRotate(2, 180); 146 assertEquals(-1.0f, ret[0], TOLERANCE); 147 assertEquals(-1.0f, ret[6], TOLERANCE); 148 assertEquals(0, ret[1], TOLERANCE); 149 assertEquals(0, ret[5], TOLERANCE); 150 } 151 testSetSaturation()152 public void testSetSaturation(){ 153 mColorMatrix.setSaturation(0.5f); 154 float[] ret = mColorMatrix.getArray(); 155 156 assertEquals(0.6065f, ret[0]); 157 assertEquals(0.3575f, ret[1]); 158 assertEquals(0.036f, ret[2]); 159 assertEquals(0.1065f, ret[5]); 160 assertEquals(0.85749996f, ret[6]); 161 assertEquals(0.036f, ret[7]); 162 assertEquals(0.1065f, ret[10]); 163 assertEquals(0.3575f, ret[11]); 164 assertEquals(0.536f, ret[12]); 165 assertEquals(0.0f, ret[3]); 166 assertEquals(0.0f, ret[4]); 167 assertEquals(0.0f, ret[8]); 168 assertEquals(0.0f, ret[9]); 169 assertEquals(0.0f, ret[13]); 170 assertEquals(0.0f, ret[14]); 171 assertEquals(0.0f, ret[15]); 172 assertEquals(0.0f, ret[16]); 173 assertEquals(0.0f, ret[17]); 174 assertEquals(1.0f, ret[18]); 175 assertEquals(0.0f, ret[19]); 176 } 177 testSetScale()178 public void testSetScale(){ 179 float[] ret = mColorMatrix.getArray(); 180 preCompare(ret); 181 182 mColorMatrix.setScale(2, 3, 4, 5); 183 ret = mColorMatrix.getArray(); 184 185 assertEquals(20, ret.length); 186 assertEquals(2.0f, ret[0]); 187 assertEquals(3.0f, ret[6]); 188 assertEquals(4.0f, ret[12]); 189 assertEquals(5.0f, ret[18]); 190 191 for(int i = 1; i <= 19; i++){ 192 if(0 == i % 6){ 193 continue; 194 } 195 196 assertEquals(0.0f, ret[i]); 197 } 198 } 199 testSetRGB2YUV()200 public void testSetRGB2YUV(){ 201 mColorMatrix.setRGB2YUV(); 202 float[] ret = mColorMatrix.getArray(); 203 204 assertEquals(0.299f, ret[0]); 205 assertEquals(0.587f, ret[1]); 206 assertEquals(0.114f, ret[2]); 207 assertEquals(-0.16874f, ret[5]); 208 assertEquals(-0.33126f, ret[6]); 209 assertEquals(0.5f, ret[7]); 210 assertEquals(0.5f, ret[10]); 211 assertEquals(-0.41869f, ret[11]); 212 assertEquals(-0.08131f, ret[12]); 213 assertEquals(0.0f, ret[3]); 214 assertEquals(0.0f, ret[4]); 215 assertEquals(0.0f, ret[8]); 216 assertEquals(0.0f, ret[9]); 217 assertEquals(0.0f, ret[13]); 218 assertEquals(0.0f, ret[14]); 219 assertEquals(0.0f, ret[15]); 220 assertEquals(0.0f, ret[16]); 221 assertEquals(0.0f, ret[17]); 222 assertEquals(1.0f, ret[18]); 223 assertEquals(0.0f, ret[19]); 224 } 225 testSetYUV2RGB()226 public void testSetYUV2RGB(){ 227 mColorMatrix.setYUV2RGB(); 228 float[] ret = mColorMatrix.getArray(); 229 230 assertEquals(1.402f, ret[2]); 231 assertEquals(1.0f, ret[5]); 232 assertEquals(-0.34414f, ret[6]); 233 assertEquals(-0.71414f, ret[7]); 234 assertEquals(1.0f, ret[10]); 235 assertEquals(1.772f, ret[11]); 236 assertEquals(0.0f, ret[12]); 237 assertEquals(1.0f, ret[0]); 238 assertEquals(0.0f, ret[1]); 239 assertEquals(0.0f, ret[3]); 240 assertEquals(0.0f, ret[4]); 241 assertEquals(0.0f, ret[8]); 242 assertEquals(0.0f, ret[9]); 243 assertEquals(0.0f, ret[13]); 244 assertEquals(0.0f, ret[14]); 245 assertEquals(0.0f, ret[15]); 246 assertEquals(0.0f, ret[16]); 247 assertEquals(0.0f, ret[17]); 248 assertEquals(1.0f, ret[18]); 249 assertEquals(0.0f, ret[19]); 250 } 251 testPostConcat()252 public void testPostConcat(){ 253 mColorMatrix.postConcat(new ColorMatrix()); 254 255 float[] ret = mColorMatrix.getArray(); 256 257 for(int i = 0; i < 20; i++){ 258 assertEquals((float) i, ret[i]); 259 } 260 } 261 testPreConcat()262 public void testPreConcat(){ 263 mColorMatrix.preConcat(new ColorMatrix()); 264 265 float[] ret = mColorMatrix.getArray(); 266 267 for(int i = 0; i < 20; i++){ 268 assertEquals((float) i, ret[i]); 269 } 270 } 271 testSetConcat()272 public void testSetConcat(){ 273 float[] floatA = new float[]{ 274 0, 1, 2, 3, 4, 275 5, 6, 7, 8, 9, 276 9, 8, 7, 6, 5, 277 4, 3, 2, 1, 0, 278 }; 279 280 float[] floatB = new float[]{ 281 1, 1, 1, 1, 1, 282 1, 1, 1, 1, 1, 283 1, 1, 1, 1, 1, 284 1, 1, 1, 1, 1, 285 }; 286 287 mColorMatrix.setConcat(new ColorMatrix(floatA), new ColorMatrix(floatB)); 288 289 float[] ret = mColorMatrix.getArray(); 290 assertEquals(6.0f, ret[0]); 291 assertEquals(6.0f, ret[1]); 292 assertEquals(6.0f, ret[2]); 293 assertEquals(6.0f, ret[3]); 294 assertEquals(10.0f, ret[4]); 295 assertEquals(26.0f, ret[5]); 296 assertEquals(26.0f, ret[6]); 297 assertEquals(26.0f, ret[7]); 298 assertEquals(26.0f, ret[8]); 299 assertEquals(35.0f, ret[9]); 300 assertEquals(30.0f, ret[10]); 301 assertEquals(30.0f, ret[11]); 302 assertEquals(30.0f, ret[12]); 303 assertEquals(30.0f, ret[13]); 304 assertEquals(35.0f, ret[14]); 305 assertEquals(10.0f, ret[15]); 306 assertEquals(10.0f, ret[16]); 307 assertEquals(10.0f, ret[17]); 308 assertEquals(10.0f, ret[18]); 309 assertEquals(10.0f, ret[19]); 310 } 311 preCompare(float[] ret)312 private void preCompare(float[] ret){ 313 assertEquals(20, ret.length); 314 315 for(int i = 0; i < 20; i++){ 316 assertEquals((float) i, ret[i]); 317 } 318 } 319 } 320