| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/skbitmap_operations.h" | 5 #include "ui/gfx/skbitmap_operations.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "third_party/skia/include/core/SkBitmap.h" | 8 #include "third_party/skia/include/core/SkBitmap.h" |
| 9 #include "third_party/skia/include/core/SkCanvas.h" | 9 #include "third_party/skia/include/core/SkCanvas.h" |
| 10 #include "third_party/skia/include/core/SkColorPriv.h" | 10 #include "third_party/skia/include/core/SkColorPriv.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 SkColor a_pixel = *a.getAddr32(x, y); | 37 SkColor a_pixel = *a.getAddr32(x, y); |
| 38 SkColor b_pixel = *b.getAddr32(x, y); | 38 SkColor b_pixel = *b.getAddr32(x, y); |
| 39 if (!ColorsClose(a_pixel, b_pixel)) | 39 if (!ColorsClose(a_pixel, b_pixel)) |
| 40 return false; | 40 return false; |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 return true; | 43 return true; |
| 44 } | 44 } |
| 45 | 45 |
| 46 void FillDataToBitmap(int w, int h, SkBitmap* bmp) { | 46 void FillDataToBitmap(int w, int h, SkBitmap* bmp) { |
| 47 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); | 47 bmp->allocN32Pixels(w, h); |
| 48 bmp->allocPixels(); | |
| 49 | 48 |
| 50 unsigned char* src_data = | 49 unsigned char* src_data = |
| 51 reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0)); | 50 reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0)); |
| 52 for (int i = 0; i < w * h; i++) { | 51 for (int i = 0; i < w * h; i++) { |
| 53 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255); | 52 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255); |
| 54 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255); | 53 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255); |
| 55 src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255); | 54 src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255); |
| 56 src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255); | 55 src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255); |
| 57 } | 56 } |
| 58 } | 57 } |
| 59 | 58 |
| 60 // The reference (i.e., old) implementation of |CreateHSLShiftedBitmap()|. | 59 // The reference (i.e., old) implementation of |CreateHSLShiftedBitmap()|. |
| 61 SkBitmap ReferenceCreateHSLShiftedBitmap( | 60 SkBitmap ReferenceCreateHSLShiftedBitmap( |
| 62 const SkBitmap& bitmap, | 61 const SkBitmap& bitmap, |
| 63 color_utils::HSL hsl_shift) { | 62 color_utils::HSL hsl_shift) { |
| 64 SkBitmap shifted; | 63 SkBitmap shifted; |
| 65 shifted.setConfig(SkBitmap::kARGB_8888_Config, bitmap.width(), | 64 shifted.allocN32Pixels(bitmap.width(), bitmap.height()); |
| 66 bitmap.height()); | |
| 67 shifted.allocPixels(); | |
| 68 shifted.eraseARGB(0, 0, 0, 0); | 65 shifted.eraseARGB(0, 0, 0, 0); |
| 69 | 66 |
| 70 SkAutoLockPixels lock_bitmap(bitmap); | 67 SkAutoLockPixels lock_bitmap(bitmap); |
| 71 SkAutoLockPixels lock_shifted(shifted); | 68 SkAutoLockPixels lock_shifted(shifted); |
| 72 | 69 |
| 73 // Loop through the pixels of the original bitmap. | 70 // Loop through the pixels of the original bitmap. |
| 74 for (int y = 0; y < bitmap.height(); ++y) { | 71 for (int y = 0; y < bitmap.height(); ++y) { |
| 75 SkPMColor* pixels = bitmap.getAddr32(0, y); | 72 SkPMColor* pixels = bitmap.getAddr32(0, y); |
| 76 SkPMColor* tinted_pixels = shifted.getAddr32(0, y); | 73 SkPMColor* tinted_pixels = shifted.getAddr32(0, y); |
| 77 | 74 |
| 78 for (int x = 0; x < bitmap.width(); ++x) { | 75 for (int x = 0; x < bitmap.width(); ++x) { |
| 79 tinted_pixels[x] = SkPreMultiplyColor(color_utils::HSLShift( | 76 tinted_pixels[x] = SkPreMultiplyColor(color_utils::HSLShift( |
| 80 SkUnPreMultiply::PMColorToColor(pixels[x]), hsl_shift)); | 77 SkUnPreMultiply::PMColorToColor(pixels[x]), hsl_shift)); |
| 81 } | 78 } |
| 82 } | 79 } |
| 83 | 80 |
| 84 return shifted; | 81 return shifted; |
| 85 } | 82 } |
| 86 | 83 |
| 87 } // namespace | 84 } // namespace |
| 88 | 85 |
| 89 // Invert bitmap and verify the each pixel is inverted and the alpha value is | 86 // Invert bitmap and verify the each pixel is inverted and the alpha value is |
| 90 // not changed. | 87 // not changed. |
| 91 TEST(SkBitmapOperationsTest, CreateInvertedBitmap) { | 88 TEST(SkBitmapOperationsTest, CreateInvertedBitmap) { |
| 92 int src_w = 16, src_h = 16; | 89 int src_w = 16, src_h = 16; |
| 93 SkBitmap src; | 90 SkBitmap src; |
| 94 src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); | 91 src.allocN32Pixels(src_w, src_h); |
| 95 src.allocPixels(); | |
| 96 | 92 |
| 97 for (int y = 0; y < src_h; y++) { | 93 for (int y = 0; y < src_h; y++) { |
| 98 for (int x = 0; x < src_w; x++) { | 94 for (int x = 0; x < src_w; x++) { |
| 99 int i = y * src_w + x; | 95 int i = y * src_w + x; |
| 100 *src.getAddr32(x, y) = | 96 *src.getAddr32(x, y) = |
| 101 SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0); | 97 SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0); |
| 102 } | 98 } |
| 103 } | 99 } |
| 104 | 100 |
| 105 SkBitmap inverted = SkBitmapOperations::CreateInvertedBitmap(src); | 101 SkBitmap inverted = SkBitmapOperations::CreateInvertedBitmap(src); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 119 SkColorGetB(*inverted.getAddr32(x, y))); | 115 SkColorGetB(*inverted.getAddr32(x, y))); |
| 120 } | 116 } |
| 121 } | 117 } |
| 122 } | 118 } |
| 123 | 119 |
| 124 // Blend two bitmaps together at 50% alpha and verify that the result | 120 // Blend two bitmaps together at 50% alpha and verify that the result |
| 125 // is the middle-blend of the two. | 121 // is the middle-blend of the two. |
| 126 TEST(SkBitmapOperationsTest, CreateBlendedBitmap) { | 122 TEST(SkBitmapOperationsTest, CreateBlendedBitmap) { |
| 127 int src_w = 16, src_h = 16; | 123 int src_w = 16, src_h = 16; |
| 128 SkBitmap src_a; | 124 SkBitmap src_a; |
| 129 src_a.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); | 125 src_a.allocN32Pixels(src_w, src_h); |
| 130 src_a.allocPixels(); | |
| 131 | 126 |
| 132 SkBitmap src_b; | 127 SkBitmap src_b; |
| 133 src_b.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); | 128 src_b.allocN32Pixels(src_w, src_h); |
| 134 src_b.allocPixels(); | |
| 135 | 129 |
| 136 for (int y = 0, i = 0; y < src_h; y++) { | 130 for (int y = 0, i = 0; y < src_h; y++) { |
| 137 for (int x = 0; x < src_w; x++) { | 131 for (int x = 0; x < src_w; x++) { |
| 138 *src_a.getAddr32(x, y) = SkColorSetARGB(255, 0, i * 2 % 255, i % 255); | 132 *src_a.getAddr32(x, y) = SkColorSetARGB(255, 0, i * 2 % 255, i % 255); |
| 139 *src_b.getAddr32(x, y) = | 133 *src_b.getAddr32(x, y) = |
| 140 SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0); | 134 SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0); |
| 141 i++; | 135 i++; |
| 142 } | 136 } |
| 143 } | 137 } |
| 144 | 138 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 166 | 160 |
| 167 // Test our masking functions. | 161 // Test our masking functions. |
| 168 TEST(SkBitmapOperationsTest, CreateMaskedBitmap) { | 162 TEST(SkBitmapOperationsTest, CreateMaskedBitmap) { |
| 169 int src_w = 16, src_h = 16; | 163 int src_w = 16, src_h = 16; |
| 170 | 164 |
| 171 SkBitmap src; | 165 SkBitmap src; |
| 172 FillDataToBitmap(src_w, src_h, &src); | 166 FillDataToBitmap(src_w, src_h, &src); |
| 173 | 167 |
| 174 // Generate alpha mask | 168 // Generate alpha mask |
| 175 SkBitmap alpha; | 169 SkBitmap alpha; |
| 176 alpha.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); | 170 alpha.allocN32Pixels(src_w, src_h); |
| 177 alpha.allocPixels(); | |
| 178 for (int y = 0, i = 0; y < src_h; y++) { | 171 for (int y = 0, i = 0; y < src_h; y++) { |
| 179 for (int x = 0; x < src_w; x++) { | 172 for (int x = 0; x < src_w; x++) { |
| 180 *alpha.getAddr32(x, y) = SkColorSetARGB((i + 128) % 255, | 173 *alpha.getAddr32(x, y) = SkColorSetARGB((i + 128) % 255, |
| 181 (i + 128) % 255, | 174 (i + 128) % 255, |
| 182 (i + 64) % 255, | 175 (i + 64) % 255, |
| 183 (i + 0) % 255); | 176 (i + 0) % 255); |
| 184 i++; | 177 i++; |
| 185 } | 178 } |
| 186 } | 179 } |
| 187 | 180 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 211 } | 204 } |
| 212 } | 205 } |
| 213 } | 206 } |
| 214 | 207 |
| 215 // Make sure that when shifting a bitmap without any shift parameters, | 208 // Make sure that when shifting a bitmap without any shift parameters, |
| 216 // the end result is close enough to the original (rounding errors | 209 // the end result is close enough to the original (rounding errors |
| 217 // notwithstanding). | 210 // notwithstanding). |
| 218 TEST(SkBitmapOperationsTest, CreateHSLShiftedBitmapToSame) { | 211 TEST(SkBitmapOperationsTest, CreateHSLShiftedBitmapToSame) { |
| 219 int src_w = 16, src_h = 16; | 212 int src_w = 16, src_h = 16; |
| 220 SkBitmap src; | 213 SkBitmap src; |
| 221 src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); | 214 src.allocN32Pixels(src_w, src_h); |
| 222 src.allocPixels(); | |
| 223 | 215 |
| 224 for (int y = 0, i = 0; y < src_h; y++) { | 216 for (int y = 0, i = 0; y < src_h; y++) { |
| 225 for (int x = 0; x < src_w; x++) { | 217 for (int x = 0; x < src_w; x++) { |
| 226 *src.getAddr32(x, y) = SkPreMultiplyColor(SkColorSetARGB((i + 128) % 255, | 218 *src.getAddr32(x, y) = SkPreMultiplyColor(SkColorSetARGB((i + 128) % 255, |
| 227 (i + 128) % 255, (i + 64) % 255, (i + 0) % 255)); | 219 (i + 128) % 255, (i + 64) % 255, (i + 0) % 255)); |
| 228 i++; | 220 i++; |
| 229 } | 221 } |
| 230 } | 222 } |
| 231 | 223 |
| 232 color_utils::HSL hsl = { -1, -1, -1 }; | 224 color_utils::HSL hsl = { -1, -1, -1 }; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 249 SkColorGetG(shifted_pixel) << "," << | 241 SkColorGetG(shifted_pixel) << "," << |
| 250 SkColorGetB(shifted_pixel) << ")"; | 242 SkColorGetB(shifted_pixel) << ")"; |
| 251 } | 243 } |
| 252 } | 244 } |
| 253 } | 245 } |
| 254 | 246 |
| 255 // Shift a blue bitmap to red. | 247 // Shift a blue bitmap to red. |
| 256 TEST(SkBitmapOperationsTest, CreateHSLShiftedBitmapHueOnly) { | 248 TEST(SkBitmapOperationsTest, CreateHSLShiftedBitmapHueOnly) { |
| 257 int src_w = 16, src_h = 16; | 249 int src_w = 16, src_h = 16; |
| 258 SkBitmap src; | 250 SkBitmap src; |
| 259 src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); | 251 src.allocN32Pixels(src_w, src_h); |
| 260 src.allocPixels(); | |
| 261 | 252 |
| 262 for (int y = 0, i = 0; y < src_h; y++) { | 253 for (int y = 0, i = 0; y < src_h; y++) { |
| 263 for (int x = 0; x < src_w; x++) { | 254 for (int x = 0; x < src_w; x++) { |
| 264 *src.getAddr32(x, y) = SkColorSetARGB(255, 0, 0, i % 255); | 255 *src.getAddr32(x, y) = SkColorSetARGB(255, 0, 0, i % 255); |
| 265 i++; | 256 i++; |
| 266 } | 257 } |
| 267 } | 258 } |
| 268 | 259 |
| 269 // Shift to red. | 260 // Shift to red. |
| 270 color_utils::HSL hsl = { 0, -1, -1 }; | 261 color_utils::HSL hsl = { 0, -1, -1 }; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 282 } | 273 } |
| 283 } | 274 } |
| 284 } | 275 } |
| 285 | 276 |
| 286 // Validate HSL shift. | 277 // Validate HSL shift. |
| 287 TEST(SkBitmapOperationsTest, ValidateHSLShift) { | 278 TEST(SkBitmapOperationsTest, ValidateHSLShift) { |
| 288 // Note: 255/51 = 5 (exactly) => 6 including 0! | 279 // Note: 255/51 = 5 (exactly) => 6 including 0! |
| 289 const int inc = 51; | 280 const int inc = 51; |
| 290 const int dim = 255 / inc + 1; | 281 const int dim = 255 / inc + 1; |
| 291 SkBitmap src; | 282 SkBitmap src; |
| 292 src.setConfig(SkBitmap::kARGB_8888_Config, dim*dim, dim*dim); | 283 src.allocN32Pixels(dim*dim, dim*dim); |
| 293 src.allocPixels(); | |
| 294 | 284 |
| 295 for (int a = 0, y = 0; a <= 255; a += inc) { | 285 for (int a = 0, y = 0; a <= 255; a += inc) { |
| 296 for (int r = 0; r <= 255; r += inc, y++) { | 286 for (int r = 0; r <= 255; r += inc, y++) { |
| 297 for (int g = 0, x = 0; g <= 255; g += inc) { | 287 for (int g = 0, x = 0; g <= 255; g += inc) { |
| 298 for (int b = 0; b <= 255; b+= inc, x++) { | 288 for (int b = 0; b <= 255; b+= inc, x++) { |
| 299 *src.getAddr32(x, y) = | 289 *src.getAddr32(x, y) = |
| 300 SkPreMultiplyColor(SkColorSetARGB(a, r, g, b)); | 290 SkPreMultiplyColor(SkColorSetARGB(a, r, g, b)); |
| 301 } | 291 } |
| 302 } | 292 } |
| 303 } | 293 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 // 50% transparent red opaque 50% gray black | 362 // 50% transparent red opaque 50% gray black |
| 373 // 80800000 80808080 FF000000 | 363 // 80800000 80808080 FF000000 |
| 374 // | 364 // |
| 375 // black white 50% gray | 365 // black white 50% gray |
| 376 // FF000000 FFFFFFFF FF808080 | 366 // FF000000 FFFFFFFF FF808080 |
| 377 // | 367 // |
| 378 // The result of this computation should be: | 368 // The result of this computation should be: |
| 379 // A0404040 FF808080 | 369 // A0404040 FF808080 |
| 380 // FF808080 FF808080 | 370 // FF808080 FF808080 |
| 381 SkBitmap input; | 371 SkBitmap input; |
| 382 input.setConfig(SkBitmap::kARGB_8888_Config, 3, 3); | 372 input.allocN32Pixels(3, 3); |
| 383 input.allocPixels(); | |
| 384 | 373 |
| 385 // The color order may be different, but we don't care (the channels are | 374 // The color order may be different, but we don't care (the channels are |
| 386 // trated the same). | 375 // trated the same). |
| 387 *input.getAddr32(0, 0) = 0x80008000; | 376 *input.getAddr32(0, 0) = 0x80008000; |
| 388 *input.getAddr32(1, 0) = 0xFF000080; | 377 *input.getAddr32(1, 0) = 0xFF000080; |
| 389 *input.getAddr32(2, 0) = 0xFFFFFFFF; | 378 *input.getAddr32(2, 0) = 0xFFFFFFFF; |
| 390 *input.getAddr32(0, 1) = 0x80800000; | 379 *input.getAddr32(0, 1) = 0x80800000; |
| 391 *input.getAddr32(1, 1) = 0x80808080; | 380 *input.getAddr32(1, 1) = 0x80808080; |
| 392 *input.getAddr32(2, 1) = 0xFF000000; | 381 *input.getAddr32(2, 1) = 0xFF000000; |
| 393 *input.getAddr32(0, 2) = 0xFF000000; | 382 *input.getAddr32(0, 2) = 0xFF000000; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 405 EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(0, 1)); | 394 EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(0, 1)); |
| 406 EXPECT_EQ(0xFF808080, *result.getAddr32(1, 1)); | 395 EXPECT_EQ(0xFF808080, *result.getAddr32(1, 1)); |
| 407 } | 396 } |
| 408 | 397 |
| 409 // Test edge cases for DownsampleByTwo. | 398 // Test edge cases for DownsampleByTwo. |
| 410 TEST(SkBitmapOperationsTest, DownsampleByTwoSmall) { | 399 TEST(SkBitmapOperationsTest, DownsampleByTwoSmall) { |
| 411 SkPMColor reference = 0xFF4080FF; | 400 SkPMColor reference = 0xFF4080FF; |
| 412 | 401 |
| 413 // Test a 1x1 bitmap. | 402 // Test a 1x1 bitmap. |
| 414 SkBitmap one_by_one; | 403 SkBitmap one_by_one; |
| 415 one_by_one.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); | 404 one_by_one.allocN32Pixels(1, 1); |
| 416 one_by_one.allocPixels(); | |
| 417 *one_by_one.getAddr32(0, 0) = reference; | 405 *one_by_one.getAddr32(0, 0) = reference; |
| 418 SkBitmap result = SkBitmapOperations::DownsampleByTwo(one_by_one); | 406 SkBitmap result = SkBitmapOperations::DownsampleByTwo(one_by_one); |
| 419 SkAutoLockPixels lock1(result); | 407 SkAutoLockPixels lock1(result); |
| 420 EXPECT_EQ(1, result.width()); | 408 EXPECT_EQ(1, result.width()); |
| 421 EXPECT_EQ(1, result.height()); | 409 EXPECT_EQ(1, result.height()); |
| 422 EXPECT_EQ(reference, *result.getAddr32(0, 0)); | 410 EXPECT_EQ(reference, *result.getAddr32(0, 0)); |
| 423 | 411 |
| 424 // Test an n by 1 bitmap. | 412 // Test an n by 1 bitmap. |
| 425 SkBitmap one_by_n; | 413 SkBitmap one_by_n; |
| 426 one_by_n.setConfig(SkBitmap::kARGB_8888_Config, 300, 1); | 414 one_by_n.allocN32Pixels(300, 1); |
| 427 one_by_n.allocPixels(); | |
| 428 result = SkBitmapOperations::DownsampleByTwo(one_by_n); | 415 result = SkBitmapOperations::DownsampleByTwo(one_by_n); |
| 429 SkAutoLockPixels lock2(result); | 416 SkAutoLockPixels lock2(result); |
| 430 EXPECT_EQ(300, result.width()); | 417 EXPECT_EQ(300, result.width()); |
| 431 EXPECT_EQ(1, result.height()); | 418 EXPECT_EQ(1, result.height()); |
| 432 | 419 |
| 433 // Test a 1 by n bitmap. | 420 // Test a 1 by n bitmap. |
| 434 SkBitmap n_by_one; | 421 SkBitmap n_by_one; |
| 435 n_by_one.setConfig(SkBitmap::kARGB_8888_Config, 1, 300); | 422 n_by_one.allocN32Pixels(1, 300); |
| 436 n_by_one.allocPixels(); | |
| 437 result = SkBitmapOperations::DownsampleByTwo(n_by_one); | 423 result = SkBitmapOperations::DownsampleByTwo(n_by_one); |
| 438 SkAutoLockPixels lock3(result); | 424 SkAutoLockPixels lock3(result); |
| 439 EXPECT_EQ(1, result.width()); | 425 EXPECT_EQ(1, result.width()); |
| 440 EXPECT_EQ(300, result.height()); | 426 EXPECT_EQ(300, result.height()); |
| 441 | 427 |
| 442 // Test an empty bitmap | 428 // Test an empty bitmap |
| 443 SkBitmap empty; | 429 SkBitmap empty; |
| 444 result = SkBitmapOperations::DownsampleByTwo(empty); | 430 result = SkBitmapOperations::DownsampleByTwo(empty); |
| 445 EXPECT_TRUE(result.isNull()); | 431 EXPECT_TRUE(result.isNull()); |
| 446 EXPECT_EQ(0, result.width()); | 432 EXPECT_EQ(0, result.width()); |
| 447 EXPECT_EQ(0, result.height()); | 433 EXPECT_EQ(0, result.height()); |
| 448 } | 434 } |
| 449 | 435 |
| 450 // Here we assume DownsampleByTwo works correctly (it's tested above) and | 436 // Here we assume DownsampleByTwo works correctly (it's tested above) and |
| 451 // just make sure that the wrapper function does the right thing. | 437 // just make sure that the wrapper function does the right thing. |
| 452 TEST(SkBitmapOperationsTest, DownsampleByTwoUntilSize) { | 438 TEST(SkBitmapOperationsTest, DownsampleByTwoUntilSize) { |
| 453 // First make sure a "too small" bitmap doesn't get modified at all. | 439 // First make sure a "too small" bitmap doesn't get modified at all. |
| 454 SkBitmap too_small; | 440 SkBitmap too_small; |
| 455 too_small.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); | 441 too_small.allocN32Pixels(10, 10); |
| 456 too_small.allocPixels(); | |
| 457 SkBitmap result = SkBitmapOperations::DownsampleByTwoUntilSize( | 442 SkBitmap result = SkBitmapOperations::DownsampleByTwoUntilSize( |
| 458 too_small, 16, 16); | 443 too_small, 16, 16); |
| 459 EXPECT_EQ(10, result.width()); | 444 EXPECT_EQ(10, result.width()); |
| 460 EXPECT_EQ(10, result.height()); | 445 EXPECT_EQ(10, result.height()); |
| 461 | 446 |
| 462 // Now make sure giving it a 0x0 target returns something reasonable. | 447 // Now make sure giving it a 0x0 target returns something reasonable. |
| 463 result = SkBitmapOperations::DownsampleByTwoUntilSize(too_small, 0, 0); | 448 result = SkBitmapOperations::DownsampleByTwoUntilSize(too_small, 0, 0); |
| 464 EXPECT_EQ(1, result.width()); | 449 EXPECT_EQ(1, result.width()); |
| 465 EXPECT_EQ(1, result.height()); | 450 EXPECT_EQ(1, result.height()); |
| 466 | 451 |
| 467 // Test multiple steps of downsampling. | 452 // Test multiple steps of downsampling. |
| 468 SkBitmap large; | 453 SkBitmap large; |
| 469 large.setConfig(SkBitmap::kARGB_8888_Config, 100, 43); | 454 large.allocN32Pixels(100, 43); |
| 470 large.allocPixels(); | |
| 471 result = SkBitmapOperations::DownsampleByTwoUntilSize(large, 6, 6); | 455 result = SkBitmapOperations::DownsampleByTwoUntilSize(large, 6, 6); |
| 472 | 456 |
| 473 // The result should be divided in half 100x43 -> 50x22 -> 25x11 | 457 // The result should be divided in half 100x43 -> 50x22 -> 25x11 |
| 474 EXPECT_EQ(25, result.width()); | 458 EXPECT_EQ(25, result.width()); |
| 475 EXPECT_EQ(11, result.height()); | 459 EXPECT_EQ(11, result.height()); |
| 476 } | 460 } |
| 477 | 461 |
| 478 TEST(SkBitmapOperationsTest, UnPreMultiply) { | 462 TEST(SkBitmapOperationsTest, UnPreMultiply) { |
| 479 SkBitmap input; | 463 SkBitmap input; |
| 480 input.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | 464 input.allocN32Pixels(2, 2); |
| 481 input.allocPixels(); | |
| 482 | 465 |
| 483 // Set PMColors into the bitmap | 466 // Set PMColors into the bitmap |
| 484 *input.getAddr32(0, 0) = SkPackARGB32NoCheck(0x80, 0x00, 0x00, 0x00); | 467 *input.getAddr32(0, 0) = SkPackARGB32NoCheck(0x80, 0x00, 0x00, 0x00); |
| 485 *input.getAddr32(1, 0) = SkPackARGB32NoCheck(0x80, 0x80, 0x80, 0x80); | 468 *input.getAddr32(1, 0) = SkPackARGB32NoCheck(0x80, 0x80, 0x80, 0x80); |
| 486 *input.getAddr32(0, 1) = SkPackARGB32NoCheck(0xFF, 0x00, 0xCC, 0x88); | 469 *input.getAddr32(0, 1) = SkPackARGB32NoCheck(0xFF, 0x00, 0xCC, 0x88); |
| 487 *input.getAddr32(1, 1) = SkPackARGB32NoCheck(0x00, 0x00, 0xCC, 0x88); | 470 *input.getAddr32(1, 1) = SkPackARGB32NoCheck(0x00, 0x00, 0xCC, 0x88); |
| 488 | 471 |
| 489 SkBitmap result = SkBitmapOperations::UnPreMultiply(input); | 472 SkBitmap result = SkBitmapOperations::UnPreMultiply(input); |
| 490 EXPECT_EQ(2, result.width()); | 473 EXPECT_EQ(2, result.width()); |
| 491 EXPECT_EQ(2, result.height()); | 474 EXPECT_EQ(2, result.height()); |
| 492 | 475 |
| 493 SkAutoLockPixels lock(result); | 476 SkAutoLockPixels lock(result); |
| 494 EXPECT_EQ(0x80000000, *result.getAddr32(0, 0)); | 477 EXPECT_EQ(0x80000000, *result.getAddr32(0, 0)); |
| 495 EXPECT_EQ(0x80FFFFFF, *result.getAddr32(1, 0)); | 478 EXPECT_EQ(0x80FFFFFF, *result.getAddr32(1, 0)); |
| 496 EXPECT_EQ(0xFF00CC88, *result.getAddr32(0, 1)); | 479 EXPECT_EQ(0xFF00CC88, *result.getAddr32(0, 1)); |
| 497 EXPECT_EQ(0x00000000u, *result.getAddr32(1, 1)); // "Division by zero". | 480 EXPECT_EQ(0x00000000u, *result.getAddr32(1, 1)); // "Division by zero". |
| 498 } | 481 } |
| 499 | 482 |
| 500 TEST(SkBitmapOperationsTest, CreateTransposedBitmap) { | 483 TEST(SkBitmapOperationsTest, CreateTransposedBitmap) { |
| 501 SkBitmap input; | 484 SkBitmap input; |
| 502 input.setConfig(SkBitmap::kARGB_8888_Config, 2, 3); | 485 input.allocN32Pixels(2, 3); |
| 503 input.allocPixels(); | |
| 504 | 486 |
| 505 for (int x = 0; x < input.width(); ++x) { | 487 for (int x = 0; x < input.width(); ++x) { |
| 506 for (int y = 0; y < input.height(); ++y) { | 488 for (int y = 0; y < input.height(); ++y) { |
| 507 *input.getAddr32(x, y) = x * input.width() + y; | 489 *input.getAddr32(x, y) = x * input.width() + y; |
| 508 } | 490 } |
| 509 } | 491 } |
| 510 | 492 |
| 511 SkBitmap result = SkBitmapOperations::CreateTransposedBitmap(input); | 493 SkBitmap result = SkBitmapOperations::CreateTransposedBitmap(input); |
| 512 EXPECT_EQ(3, result.width()); | 494 EXPECT_EQ(3, result.width()); |
| 513 EXPECT_EQ(2, result.height()); | 495 EXPECT_EQ(2, result.height()); |
| 514 | 496 |
| 515 SkAutoLockPixels lock(result); | 497 SkAutoLockPixels lock(result); |
| 516 for (int x = 0; x < input.width(); ++x) { | 498 for (int x = 0; x < input.width(); ++x) { |
| 517 for (int y = 0; y < input.height(); ++y) { | 499 for (int y = 0; y < input.height(); ++y) { |
| 518 EXPECT_EQ(*input.getAddr32(x, y), *result.getAddr32(y, x)); | 500 EXPECT_EQ(*input.getAddr32(x, y), *result.getAddr32(y, x)); |
| 519 } | 501 } |
| 520 } | 502 } |
| 521 } | 503 } |
| 522 | 504 |
| 523 // Check that Rotate provides the desired results | 505 // Check that Rotate provides the desired results |
| 524 TEST(SkBitmapOperationsTest, RotateImage) { | 506 TEST(SkBitmapOperationsTest, RotateImage) { |
| 525 const int src_w = 6, src_h = 4; | 507 const int src_w = 6, src_h = 4; |
| 526 SkBitmap src; | 508 SkBitmap src; |
| 527 // Create a simple 4 color bitmap: | 509 // Create a simple 4 color bitmap: |
| 528 // RRRBBB | 510 // RRRBBB |
| 529 // RRRBBB | 511 // RRRBBB |
| 530 // GGGYYY | 512 // GGGYYY |
| 531 // GGGYYY | 513 // GGGYYY |
| 532 src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); | 514 src.allocN32Pixels(src_w, src_h); |
| 533 src.allocPixels(); | |
| 534 | 515 |
| 535 SkCanvas canvas(src); | 516 SkCanvas canvas(src); |
| 536 src.eraseARGB(0, 0, 0, 0); | 517 src.eraseARGB(0, 0, 0, 0); |
| 537 SkRegion region; | 518 SkRegion region; |
| 538 | 519 |
| 539 region.setRect(0, 0, src_w / 2, src_h / 2); | 520 region.setRect(0, 0, src_w / 2, src_h / 2); |
| 540 canvas.setClipRegion(region); | 521 canvas.setClipRegion(region); |
| 541 // This region is a semi-transparent red to test non-opaque pixels. | 522 // This region is a semi-transparent red to test non-opaque pixels. |
| 542 canvas.drawColor(0x1FFF0000, SkXfermode::kSrc_Mode); | 523 canvas.drawColor(0x1FFF0000, SkXfermode::kSrc_Mode); |
| 543 region.setRect(src_w / 2, 0, src_w, src_h / 2); | 524 region.setRect(src_w / 2, 0, src_w, src_h / 2); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 573 | 554 |
| 574 for (int x=0; x < src_w; ++x) { | 555 for (int x=0; x < src_w; ++x) { |
| 575 for (int y=0; y < src_h; ++y) { | 556 for (int y=0; y < src_h; ++y) { |
| 576 ASSERT_EQ(*src.getAddr32(x,y), *rotate90.getAddr32(src_h - (y+1),x)); | 557 ASSERT_EQ(*src.getAddr32(x,y), *rotate90.getAddr32(src_h - (y+1),x)); |
| 577 ASSERT_EQ(*src.getAddr32(x,y), *rotate270.getAddr32(y, src_w - (x+1))); | 558 ASSERT_EQ(*src.getAddr32(x,y), *rotate270.getAddr32(y, src_w - (x+1))); |
| 578 ASSERT_EQ(*src.getAddr32(x,y), | 559 ASSERT_EQ(*src.getAddr32(x,y), |
| 579 *rotate180.getAddr32(src_w - (x+1), src_h - (y+1))); | 560 *rotate180.getAddr32(src_w - (x+1), src_h - (y+1))); |
| 580 } | 561 } |
| 581 } | 562 } |
| 582 } | 563 } |
| OLD | NEW |