1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "ui/gfx/skbitmap_operations.h" 6 7 #include "testing/gtest/include/gtest/gtest.h" 8 #include "third_party/skia/include/core/SkBitmap.h" 9 #include "third_party/skia/include/core/SkCanvas.h" 10 #include "third_party/skia/include/core/SkColorPriv.h" 11 #include "third_party/skia/include/core/SkRect.h" 12 #include "third_party/skia/include/core/SkRegion.h" 13 #include "third_party/skia/include/core/SkUnPreMultiply.h" 14 15 namespace { 16 17 // Returns true if each channel of the given two colors are "close." This is 18 // used for comparing colors where rounding errors may cause off-by-one. 19 inline bool ColorsClose(uint32_t a, uint32_t b) { 20 return abs(static_cast<int>(SkColorGetB(a) - SkColorGetB(b))) <= 2 && 21 abs(static_cast<int>(SkColorGetG(a) - SkColorGetG(b))) <= 2 && 22 abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) <= 2 && 23 abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) <= 2; 24 } 25 26 inline bool MultipliedColorsClose(uint32_t a, uint32_t b) { 27 return ColorsClose(SkUnPreMultiply::PMColorToColor(a), 28 SkUnPreMultiply::PMColorToColor(b)); 29 } 30 31 bool BitmapsClose(const SkBitmap& a, const SkBitmap& b) { 32 SkAutoLockPixels a_lock(a); 33 SkAutoLockPixels b_lock(b); 34 35 for (int y = 0; y < a.height(); y++) { 36 for (int x = 0; x < a.width(); x++) { 37 SkColor a_pixel = *a.getAddr32(x, y); 38 SkColor b_pixel = *b.getAddr32(x, y); 39 if (!ColorsClose(a_pixel, b_pixel)) 40 return false; 41 } 42 } 43 return true; 44 } 45 46 void FillDataToBitmap(int w, int h, SkBitmap* bmp) { 47 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); 48 bmp->allocPixels(); 49 50 unsigned char* src_data = 51 reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0)); 52 for (int i = 0; i < w * h; i++) { 53 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255); 54 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255); 55 src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255); 56 src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255); 57 } 58 } 59 60 // The reference (i.e., old) implementation of |CreateHSLShiftedBitmap()|. 61 SkBitmap ReferenceCreateHSLShiftedBitmap( 62 const SkBitmap& bitmap, 63 color_utils::HSL hsl_shift) { 64 SkBitmap shifted; 65 shifted.setConfig(SkBitmap::kARGB_8888_Config, bitmap.width(), 66 bitmap.height(), 0); 67 shifted.allocPixels(); 68 shifted.eraseARGB(0, 0, 0, 0); 69 shifted.setIsOpaque(false); 70 71 SkAutoLockPixels lock_bitmap(bitmap); 72 SkAutoLockPixels lock_shifted(shifted); 73 74 // Loop through the pixels of the original bitmap. 75 for (int y = 0; y < bitmap.height(); ++y) { 76 SkPMColor* pixels = bitmap.getAddr32(0, y); 77 SkPMColor* tinted_pixels = shifted.getAddr32(0, y); 78 79 for (int x = 0; x < bitmap.width(); ++x) { 80 tinted_pixels[x] = SkPreMultiplyColor(color_utils::HSLShift( 81 SkUnPreMultiply::PMColorToColor(pixels[x]), hsl_shift)); 82 } 83 } 84 85 return shifted; 86 } 87 88 } // namespace 89 90 // Invert bitmap and verify the each pixel is inverted and the alpha value is 91 // not changed. 92 TEST(SkBitmapOperationsTest, CreateInvertedBitmap) { 93 int src_w = 16, src_h = 16; 94 SkBitmap src; 95 src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); 96 src.allocPixels(); 97 98 for (int y = 0; y < src_h; y++) { 99 for (int x = 0; x < src_w; x++) { 100 int i = y * src_w + x; 101 *src.getAddr32(x, y) = 102 SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0); 103 } 104 } 105 106 SkBitmap inverted = SkBitmapOperations::CreateInvertedBitmap(src); 107 SkAutoLockPixels src_lock(src); 108 SkAutoLockPixels inverted_lock(inverted); 109 110 for (int y = 0; y < src_h; y++) { 111 for (int x = 0; x < src_w; x++) { 112 int i = y * src_w + x; 113 EXPECT_EQ(static_cast<unsigned int>((255 - i) % 255), 114 SkColorGetA(*inverted.getAddr32(x, y))); 115 EXPECT_EQ(static_cast<unsigned int>(255 - (i % 255)), 116 SkColorGetR(*inverted.getAddr32(x, y))); 117 EXPECT_EQ(static_cast<unsigned int>(255 - (i * 4 % 255)), 118 SkColorGetG(*inverted.getAddr32(x, y))); 119 EXPECT_EQ(static_cast<unsigned int>(255), 120 SkColorGetB(*inverted.getAddr32(x, y))); 121 } 122 } 123 } 124 125 // Blend two bitmaps together at 50% alpha and verify that the result 126 // is the middle-blend of the two. 127 TEST(SkBitmapOperationsTest, CreateBlendedBitmap) { 128 int src_w = 16, src_h = 16; 129 SkBitmap src_a; 130 src_a.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); 131 src_a.allocPixels(); 132 133 SkBitmap src_b; 134 src_b.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); 135 src_b.allocPixels(); 136 137 for (int y = 0, i = 0; y < src_h; y++) { 138 for (int x = 0; x < src_w; x++) { 139 *src_a.getAddr32(x, y) = SkColorSetARGB(255, 0, i * 2 % 255, i % 255); 140 *src_b.getAddr32(x, y) = 141 SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0); 142 i++; 143 } 144 } 145 146 // Shift to red. 147 SkBitmap blended = SkBitmapOperations::CreateBlendedBitmap( 148 src_a, src_b, 0.5); 149 SkAutoLockPixels srca_lock(src_a); 150 SkAutoLockPixels srcb_lock(src_b); 151 SkAutoLockPixels blended_lock(blended); 152 153 for (int y = 0; y < src_h; y++) { 154 for (int x = 0; x < src_w; x++) { 155 int i = y * src_w + x; 156 EXPECT_EQ(static_cast<unsigned int>((255 + ((255 - i) % 255)) / 2), 157 SkColorGetA(*blended.getAddr32(x, y))); 158 EXPECT_EQ(static_cast<unsigned int>(i % 255 / 2), 159 SkColorGetR(*blended.getAddr32(x, y))); 160 EXPECT_EQ((static_cast<unsigned int>((i * 2) % 255 + (i * 4) % 255) / 2), 161 SkColorGetG(*blended.getAddr32(x, y))); 162 EXPECT_EQ(static_cast<unsigned int>(i % 255 / 2), 163 SkColorGetB(*blended.getAddr32(x, y))); 164 } 165 } 166 } 167 168 // Test our masking functions. 169 TEST(SkBitmapOperationsTest, CreateMaskedBitmap) { 170 int src_w = 16, src_h = 16; 171 172 SkBitmap src; 173 FillDataToBitmap(src_w, src_h, &src); 174 175 // Generate alpha mask 176 SkBitmap alpha; 177 alpha.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); 178 alpha.allocPixels(); 179 for (int y = 0, i = 0; y < src_h; y++) { 180 for (int x = 0; x < src_w; x++) { 181 *alpha.getAddr32(x, y) = SkColorSetARGB((i + 128) % 255, 182 (i + 128) % 255, 183 (i + 64) % 255, 184 (i + 0) % 255); 185 i++; 186 } 187 } 188 189 SkBitmap masked = SkBitmapOperations::CreateMaskedBitmap(src, alpha); 190 191 SkAutoLockPixels src_lock(src); 192 SkAutoLockPixels alpha_lock(alpha); 193 SkAutoLockPixels masked_lock(masked); 194 for (int y = 0; y < src_h; y++) { 195 for (int x = 0; x < src_w; x++) { 196 // Test that the alpha is equal. 197 SkColor src_pixel = SkUnPreMultiply::PMColorToColor(*src.getAddr32(x, y)); 198 SkColor alpha_pixel = 199 SkUnPreMultiply::PMColorToColor(*alpha.getAddr32(x, y)); 200 SkColor masked_pixel = *masked.getAddr32(x, y); 201 202 int alpha_value = SkAlphaMul(SkColorGetA(src_pixel), 203 SkAlpha255To256(SkColorGetA(alpha_pixel))); 204 int alpha_value_256 = SkAlpha255To256(alpha_value); 205 SkColor expected_pixel = SkColorSetARGB( 206 alpha_value, 207 SkAlphaMul(SkColorGetR(src_pixel), alpha_value_256), 208 SkAlphaMul(SkColorGetG(src_pixel), alpha_value_256), 209 SkAlphaMul(SkColorGetB(src_pixel), alpha_value_256)); 210 211 EXPECT_EQ(expected_pixel, masked_pixel); 212 } 213 } 214 } 215 216 // Make sure that when shifting a bitmap without any shift parameters, 217 // the end result is close enough to the original (rounding errors 218 // notwithstanding). 219 TEST(SkBitmapOperationsTest, CreateHSLShiftedBitmapToSame) { 220 int src_w = 16, src_h = 16; 221 SkBitmap src; 222 src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); 223 src.allocPixels(); 224 225 for (int y = 0, i = 0; y < src_h; y++) { 226 for (int x = 0; x < src_w; x++) { 227 *src.getAddr32(x, y) = SkPreMultiplyColor(SkColorSetARGB((i + 128) % 255, 228 (i + 128) % 255, (i + 64) % 255, (i + 0) % 255)); 229 i++; 230 } 231 } 232 233 color_utils::HSL hsl = { -1, -1, -1 }; 234 SkBitmap shifted = ReferenceCreateHSLShiftedBitmap(src, hsl); 235 236 SkAutoLockPixels src_lock(src); 237 SkAutoLockPixels shifted_lock(shifted); 238 239 for (int y = 0; y < src_h; y++) { 240 for (int x = 0; x < src_w; x++) { 241 SkColor src_pixel = *src.getAddr32(x, y); 242 SkColor shifted_pixel = *shifted.getAddr32(x, y); 243 EXPECT_TRUE(MultipliedColorsClose(src_pixel, shifted_pixel)) << 244 "source: (a,r,g,b) = (" << SkColorGetA(src_pixel) << "," << 245 SkColorGetR(src_pixel) << "," << 246 SkColorGetG(src_pixel) << "," << 247 SkColorGetB(src_pixel) << "); " << 248 "shifted: (a,r,g,b) = (" << SkColorGetA(shifted_pixel) << "," << 249 SkColorGetR(shifted_pixel) << "," << 250 SkColorGetG(shifted_pixel) << "," << 251 SkColorGetB(shifted_pixel) << ")"; 252 } 253 } 254 } 255 256 // Shift a blue bitmap to red. 257 TEST(SkBitmapOperationsTest, CreateHSLShiftedBitmapHueOnly) { 258 int src_w = 16, src_h = 16; 259 SkBitmap src; 260 src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); 261 src.allocPixels(); 262 263 for (int y = 0, i = 0; y < src_h; y++) { 264 for (int x = 0; x < src_w; x++) { 265 *src.getAddr32(x, y) = SkColorSetARGB(255, 0, 0, i % 255); 266 i++; 267 } 268 } 269 270 // Shift to red. 271 color_utils::HSL hsl = { 0, -1, -1 }; 272 273 SkBitmap shifted = SkBitmapOperations::CreateHSLShiftedBitmap(src, hsl); 274 275 SkAutoLockPixels src_lock(src); 276 SkAutoLockPixels shifted_lock(shifted); 277 278 for (int y = 0, i = 0; y < src_h; y++) { 279 for (int x = 0; x < src_w; x++) { 280 EXPECT_TRUE(ColorsClose(shifted.getColor(x, y), 281 SkColorSetARGB(255, i % 255, 0, 0))); 282 i++; 283 } 284 } 285 } 286 287 // Validate HSL shift. 288 TEST(SkBitmapOperationsTest, ValidateHSLShift) { 289 // Note: 255/51 = 5 (exactly) => 6 including 0! 290 const int inc = 51; 291 const int dim = 255 / inc + 1; 292 SkBitmap src; 293 src.setConfig(SkBitmap::kARGB_8888_Config, dim*dim, dim*dim); 294 src.allocPixels(); 295 296 for (int a = 0, y = 0; a <= 255; a += inc) { 297 for (int r = 0; r <= 255; r += inc, y++) { 298 for (int g = 0, x = 0; g <= 255; g += inc) { 299 for (int b = 0; b <= 255; b+= inc, x++) { 300 *src.getAddr32(x, y) = 301 SkPreMultiplyColor(SkColorSetARGB(a, r, g, b)); 302 } 303 } 304 } 305 } 306 307 // Shhhh. The spec says I should set things to -1 for "no change", but 308 // actually -0.1 will do. Don't tell anyone I did this. 309 for (double h = -0.1; h <= 1.0001; h += 0.1) { 310 for (double s = -0.1; s <= 1.0001; s += 0.1) { 311 for (double l = -0.1; l <= 1.0001; l += 0.1) { 312 color_utils::HSL hsl = { h, s, l }; 313 SkBitmap ref_shifted = ReferenceCreateHSLShiftedBitmap(src, hsl); 314 SkBitmap shifted = SkBitmapOperations::CreateHSLShiftedBitmap(src, hsl); 315 EXPECT_TRUE(BitmapsClose(ref_shifted, shifted)) 316 << "h = " << h << ", s = " << s << ", l = " << l; 317 } 318 } 319 } 320 } 321 322 // Test our cropping. 323 TEST(SkBitmapOperationsTest, CreateCroppedBitmap) { 324 int src_w = 16, src_h = 16; 325 SkBitmap src; 326 FillDataToBitmap(src_w, src_h, &src); 327 328 SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap(src, 4, 4, 329 8, 8); 330 ASSERT_EQ(8, cropped.width()); 331 ASSERT_EQ(8, cropped.height()); 332 333 SkAutoLockPixels src_lock(src); 334 SkAutoLockPixels cropped_lock(cropped); 335 for (int y = 4; y < 12; y++) { 336 for (int x = 4; x < 12; x++) { 337 EXPECT_EQ(*src.getAddr32(x, y), 338 *cropped.getAddr32(x - 4, y - 4)); 339 } 340 } 341 } 342 343 // Test whether our cropping correctly wraps across image boundaries. 344 TEST(SkBitmapOperationsTest, CreateCroppedBitmapWrapping) { 345 int src_w = 16, src_h = 16; 346 SkBitmap src; 347 FillDataToBitmap(src_w, src_h, &src); 348 349 SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap( 350 src, src_w / 2, src_h / 2, src_w, src_h); 351 ASSERT_EQ(src_w, cropped.width()); 352 ASSERT_EQ(src_h, cropped.height()); 353 354 SkAutoLockPixels src_lock(src); 355 SkAutoLockPixels cropped_lock(cropped); 356 for (int y = 0; y < src_h; y++) { 357 for (int x = 0; x < src_w; x++) { 358 EXPECT_EQ(*src.getAddr32(x, y), 359 *cropped.getAddr32((x + src_w / 2) % src_w, 360 (y + src_h / 2) % src_h)); 361 } 362 } 363 } 364 365 TEST(SkBitmapOperationsTest, DownsampleByTwo) { 366 // Use an odd-sized bitmap to make sure the edge cases where there isn't a 367 // 2x2 block of pixels is handled correctly. 368 // Here's the ARGB example 369 // 370 // 50% transparent green opaque 50% blue white 371 // 80008000 FF000080 FFFFFFFF 372 // 373 // 50% transparent red opaque 50% gray black 374 // 80800000 80808080 FF000000 375 // 376 // black white 50% gray 377 // FF000000 FFFFFFFF FF808080 378 // 379 // The result of this computation should be: 380 // A0404040 FF808080 381 // FF808080 FF808080 382 SkBitmap input; 383 input.setConfig(SkBitmap::kARGB_8888_Config, 3, 3); 384 input.allocPixels(); 385 386 // The color order may be different, but we don't care (the channels are 387 // trated the same). 388 *input.getAddr32(0, 0) = 0x80008000; 389 *input.getAddr32(1, 0) = 0xFF000080; 390 *input.getAddr32(2, 0) = 0xFFFFFFFF; 391 *input.getAddr32(0, 1) = 0x80800000; 392 *input.getAddr32(1, 1) = 0x80808080; 393 *input.getAddr32(2, 1) = 0xFF000000; 394 *input.getAddr32(0, 2) = 0xFF000000; 395 *input.getAddr32(1, 2) = 0xFFFFFFFF; 396 *input.getAddr32(2, 2) = 0xFF808080; 397 398 SkBitmap result = SkBitmapOperations::DownsampleByTwo(input); 399 EXPECT_EQ(2, result.width()); 400 EXPECT_EQ(2, result.height()); 401 402 // Some of the values are off-by-one due to rounding. 403 SkAutoLockPixels lock(result); 404 EXPECT_EQ(0x9f404040, *result.getAddr32(0, 0)); 405 EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(1, 0)); 406 EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(0, 1)); 407 EXPECT_EQ(0xFF808080, *result.getAddr32(1, 1)); 408 } 409 410 // Test edge cases for DownsampleByTwo. 411 TEST(SkBitmapOperationsTest, DownsampleByTwoSmall) { 412 SkPMColor reference = 0xFF4080FF; 413 414 // Test a 1x1 bitmap. 415 SkBitmap one_by_one; 416 one_by_one.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); 417 one_by_one.allocPixels(); 418 *one_by_one.getAddr32(0, 0) = reference; 419 SkBitmap result = SkBitmapOperations::DownsampleByTwo(one_by_one); 420 SkAutoLockPixels lock1(result); 421 EXPECT_EQ(1, result.width()); 422 EXPECT_EQ(1, result.height()); 423 EXPECT_EQ(reference, *result.getAddr32(0, 0)); 424 425 // Test an n by 1 bitmap. 426 SkBitmap one_by_n; 427 one_by_n.setConfig(SkBitmap::kARGB_8888_Config, 300, 1); 428 one_by_n.allocPixels(); 429 result = SkBitmapOperations::DownsampleByTwo(one_by_n); 430 SkAutoLockPixels lock2(result); 431 EXPECT_EQ(300, result.width()); 432 EXPECT_EQ(1, result.height()); 433 434 // Test a 1 by n bitmap. 435 SkBitmap n_by_one; 436 n_by_one.setConfig(SkBitmap::kARGB_8888_Config, 1, 300); 437 n_by_one.allocPixels(); 438 result = SkBitmapOperations::DownsampleByTwo(n_by_one); 439 SkAutoLockPixels lock3(result); 440 EXPECT_EQ(1, result.width()); 441 EXPECT_EQ(300, result.height()); 442 443 // Test an empty bitmap 444 SkBitmap empty; 445 result = SkBitmapOperations::DownsampleByTwo(empty); 446 EXPECT_TRUE(result.isNull()); 447 EXPECT_EQ(0, result.width()); 448 EXPECT_EQ(0, result.height()); 449 } 450 451 // Here we assume DownsampleByTwo works correctly (it's tested above) and 452 // just make sure that the wrapper function does the right thing. 453 TEST(SkBitmapOperationsTest, DownsampleByTwoUntilSize) { 454 // First make sure a "too small" bitmap doesn't get modified at all. 455 SkBitmap too_small; 456 too_small.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); 457 too_small.allocPixels(); 458 SkBitmap result = SkBitmapOperations::DownsampleByTwoUntilSize( 459 too_small, 16, 16); 460 EXPECT_EQ(10, result.width()); 461 EXPECT_EQ(10, result.height()); 462 463 // Now make sure giving it a 0x0 target returns something reasonable. 464 result = SkBitmapOperations::DownsampleByTwoUntilSize(too_small, 0, 0); 465 EXPECT_EQ(1, result.width()); 466 EXPECT_EQ(1, result.height()); 467 468 // Test multiple steps of downsampling. 469 SkBitmap large; 470 large.setConfig(SkBitmap::kARGB_8888_Config, 100, 43); 471 large.allocPixels(); 472 result = SkBitmapOperations::DownsampleByTwoUntilSize(large, 6, 6); 473 474 // The result should be divided in half 100x43 -> 50x22 -> 25x11 475 EXPECT_EQ(25, result.width()); 476 EXPECT_EQ(11, result.height()); 477 } 478 479 TEST(SkBitmapOperationsTest, UnPreMultiply) { 480 SkBitmap input; 481 input.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); 482 input.allocPixels(); 483 484 // Set PMColors into the bitmap 485 *input.getAddr32(0, 0) = SkPackARGB32NoCheck(0x80, 0x00, 0x00, 0x00); 486 *input.getAddr32(1, 0) = SkPackARGB32NoCheck(0x80, 0x80, 0x80, 0x80); 487 *input.getAddr32(0, 1) = SkPackARGB32NoCheck(0xFF, 0x00, 0xCC, 0x88); 488 *input.getAddr32(1, 1) = SkPackARGB32NoCheck(0x00, 0x00, 0xCC, 0x88); 489 490 SkBitmap result = SkBitmapOperations::UnPreMultiply(input); 491 EXPECT_EQ(2, result.width()); 492 EXPECT_EQ(2, result.height()); 493 494 SkAutoLockPixels lock(result); 495 EXPECT_EQ(0x80000000, *result.getAddr32(0, 0)); 496 EXPECT_EQ(0x80FFFFFF, *result.getAddr32(1, 0)); 497 EXPECT_EQ(0xFF00CC88, *result.getAddr32(0, 1)); 498 EXPECT_EQ(0x00000000u, *result.getAddr32(1, 1)); // "Division by zero". 499 } 500 501 TEST(SkBitmapOperationsTest, CreateTransposedBitmap) { 502 SkBitmap input; 503 input.setConfig(SkBitmap::kARGB_8888_Config, 2, 3); 504 input.allocPixels(); 505 506 for (int x = 0; x < input.width(); ++x) { 507 for (int y = 0; y < input.height(); ++y) { 508 *input.getAddr32(x, y) = x * input.width() + y; 509 } 510 } 511 512 SkBitmap result = SkBitmapOperations::CreateTransposedBitmap(input); 513 EXPECT_EQ(3, result.width()); 514 EXPECT_EQ(2, result.height()); 515 516 SkAutoLockPixels lock(result); 517 for (int x = 0; x < input.width(); ++x) { 518 for (int y = 0; y < input.height(); ++y) { 519 EXPECT_EQ(*input.getAddr32(x, y), *result.getAddr32(y, x)); 520 } 521 } 522 } 523 524 // Check that Rotate provides the desired results 525 TEST(SkBitmapOperationsTest, RotateImage) { 526 const int src_w = 6, src_h = 4; 527 SkBitmap src; 528 // Create a simple 4 color bitmap: 529 // RRRBBB 530 // RRRBBB 531 // GGGYYY 532 // GGGYYY 533 src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); 534 src.allocPixels(); 535 536 SkCanvas canvas(src); 537 src.eraseARGB(0, 0, 0, 0); 538 SkRegion region; 539 540 region.setRect(0, 0, src_w / 2, src_h / 2); 541 canvas.setClipRegion(region); 542 // This region is a semi-transparent red to test non-opaque pixels. 543 canvas.drawColor(0x1FFF0000, SkXfermode::kSrc_Mode); 544 region.setRect(src_w / 2, 0, src_w, src_h / 2); 545 canvas.setClipRegion(region); 546 canvas.drawColor(SK_ColorBLUE, SkXfermode::kSrc_Mode); 547 region.setRect(0, src_h / 2, src_w / 2, src_h); 548 canvas.setClipRegion(region); 549 canvas.drawColor(SK_ColorGREEN, SkXfermode::kSrc_Mode); 550 region.setRect(src_w / 2, src_h / 2, src_w, src_h); 551 canvas.setClipRegion(region); 552 canvas.drawColor(SK_ColorYELLOW, SkXfermode::kSrc_Mode); 553 canvas.flush(); 554 555 SkBitmap rotate90, rotate180, rotate270; 556 rotate90 = SkBitmapOperations::Rotate(src, 557 SkBitmapOperations::ROTATION_90_CW); 558 rotate180 = SkBitmapOperations::Rotate(src, 559 SkBitmapOperations::ROTATION_180_CW); 560 rotate270 = SkBitmapOperations::Rotate(src, 561 SkBitmapOperations::ROTATION_270_CW); 562 563 ASSERT_EQ(rotate90.width(), src.height()); 564 ASSERT_EQ(rotate90.height(), src.width()); 565 ASSERT_EQ(rotate180.width(), src.width()); 566 ASSERT_EQ(rotate180.height(), src.height()); 567 ASSERT_EQ(rotate270.width(), src.height()); 568 ASSERT_EQ(rotate270.height(), src.width()); 569 570 SkAutoLockPixels lock_src(src); 571 SkAutoLockPixels lock_90(rotate90); 572 SkAutoLockPixels lock_180(rotate180); 573 SkAutoLockPixels lock_270(rotate270); 574 575 for (int x=0; x < src_w; ++x) { 576 for (int y=0; y < src_h; ++y) { 577 ASSERT_EQ(*src.getAddr32(x,y), *rotate90.getAddr32(src_h - (y+1),x)); 578 ASSERT_EQ(*src.getAddr32(x,y), *rotate270.getAddr32(y, src_w - (x+1))); 579 ASSERT_EQ(*src.getAddr32(x,y), 580 *rotate180.getAddr32(src_w - (x+1), src_h - (y+1))); 581 } 582 } 583 } 584