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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) <= 2 && | 24 abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) <= 2 && |
25 abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) <= 2; | 25 abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) <= 2; |
26 } | 26 } |
27 | 27 |
28 inline bool MultipliedColorsClose(uint32_t a, uint32_t b) { | 28 inline bool MultipliedColorsClose(uint32_t a, uint32_t b) { |
29 return ColorsClose(SkUnPreMultiply::PMColorToColor(a), | 29 return ColorsClose(SkUnPreMultiply::PMColorToColor(a), |
30 SkUnPreMultiply::PMColorToColor(b)); | 30 SkUnPreMultiply::PMColorToColor(b)); |
31 } | 31 } |
32 | 32 |
33 bool BitmapsClose(const SkBitmap& a, const SkBitmap& b) { | 33 bool BitmapsClose(const SkBitmap& a, const SkBitmap& b) { |
34 SkAutoLockPixels a_lock(a); | |
35 SkAutoLockPixels b_lock(b); | |
36 | |
37 for (int y = 0; y < a.height(); y++) { | 34 for (int y = 0; y < a.height(); y++) { |
38 for (int x = 0; x < a.width(); x++) { | 35 for (int x = 0; x < a.width(); x++) { |
39 SkColor a_pixel = *a.getAddr32(x, y); | 36 SkColor a_pixel = *a.getAddr32(x, y); |
40 SkColor b_pixel = *b.getAddr32(x, y); | 37 SkColor b_pixel = *b.getAddr32(x, y); |
41 if (!ColorsClose(a_pixel, b_pixel)) | 38 if (!ColorsClose(a_pixel, b_pixel)) |
42 return false; | 39 return false; |
43 } | 40 } |
44 } | 41 } |
45 return true; | 42 return true; |
46 } | 43 } |
(...skipping 13 matching lines...) Expand all Loading... |
60 } | 57 } |
61 | 58 |
62 // The reference (i.e., old) implementation of |CreateHSLShiftedBitmap()|. | 59 // The reference (i.e., old) implementation of |CreateHSLShiftedBitmap()|. |
63 SkBitmap ReferenceCreateHSLShiftedBitmap( | 60 SkBitmap ReferenceCreateHSLShiftedBitmap( |
64 const SkBitmap& bitmap, | 61 const SkBitmap& bitmap, |
65 color_utils::HSL hsl_shift) { | 62 color_utils::HSL hsl_shift) { |
66 SkBitmap shifted; | 63 SkBitmap shifted; |
67 shifted.allocN32Pixels(bitmap.width(), bitmap.height()); | 64 shifted.allocN32Pixels(bitmap.width(), bitmap.height()); |
68 shifted.eraseARGB(0, 0, 0, 0); | 65 shifted.eraseARGB(0, 0, 0, 0); |
69 | 66 |
70 SkAutoLockPixels lock_bitmap(bitmap); | |
71 SkAutoLockPixels lock_shifted(shifted); | |
72 | |
73 // Loop through the pixels of the original bitmap. | 67 // Loop through the pixels of the original bitmap. |
74 for (int y = 0; y < bitmap.height(); ++y) { | 68 for (int y = 0; y < bitmap.height(); ++y) { |
75 SkPMColor* pixels = bitmap.getAddr32(0, y); | 69 SkPMColor* pixels = bitmap.getAddr32(0, y); |
76 SkPMColor* tinted_pixels = shifted.getAddr32(0, y); | 70 SkPMColor* tinted_pixels = shifted.getAddr32(0, y); |
77 | 71 |
78 for (int x = 0; x < bitmap.width(); ++x) { | 72 for (int x = 0; x < bitmap.width(); ++x) { |
79 tinted_pixels[x] = SkPreMultiplyColor(color_utils::HSLShift( | 73 tinted_pixels[x] = SkPreMultiplyColor(color_utils::HSLShift( |
80 SkUnPreMultiply::PMColorToColor(pixels[x]), hsl_shift)); | 74 SkUnPreMultiply::PMColorToColor(pixels[x]), hsl_shift)); |
81 } | 75 } |
82 } | 76 } |
(...skipping 12 matching lines...) Expand all Loading... |
95 | 89 |
96 for (int y = 0; y < src_h; y++) { | 90 for (int y = 0; y < src_h; y++) { |
97 for (int x = 0; x < src_w; x++) { | 91 for (int x = 0; x < src_w; x++) { |
98 int i = y * src_w + x; | 92 int i = y * src_w + x; |
99 *src.getAddr32(x, y) = | 93 *src.getAddr32(x, y) = |
100 SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0); | 94 SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0); |
101 } | 95 } |
102 } | 96 } |
103 | 97 |
104 SkBitmap inverted = SkBitmapOperations::CreateInvertedBitmap(src); | 98 SkBitmap inverted = SkBitmapOperations::CreateInvertedBitmap(src); |
105 SkAutoLockPixels src_lock(src); | |
106 SkAutoLockPixels inverted_lock(inverted); | |
107 | 99 |
108 for (int y = 0; y < src_h; y++) { | 100 for (int y = 0; y < src_h; y++) { |
109 for (int x = 0; x < src_w; x++) { | 101 for (int x = 0; x < src_w; x++) { |
110 int i = y * src_w + x; | 102 int i = y * src_w + x; |
111 EXPECT_EQ(static_cast<unsigned int>((255 - i) % 255), | 103 EXPECT_EQ(static_cast<unsigned int>((255 - i) % 255), |
112 SkColorGetA(*inverted.getAddr32(x, y))); | 104 SkColorGetA(*inverted.getAddr32(x, y))); |
113 EXPECT_EQ(static_cast<unsigned int>(255 - (i % 255)), | 105 EXPECT_EQ(static_cast<unsigned int>(255 - (i % 255)), |
114 SkColorGetR(*inverted.getAddr32(x, y))); | 106 SkColorGetR(*inverted.getAddr32(x, y))); |
115 EXPECT_EQ(static_cast<unsigned int>(255 - (i * 4 % 255)), | 107 EXPECT_EQ(static_cast<unsigned int>(255 - (i * 4 % 255)), |
116 SkColorGetG(*inverted.getAddr32(x, y))); | 108 SkColorGetG(*inverted.getAddr32(x, y))); |
(...skipping 18 matching lines...) Expand all Loading... |
135 *src_a.getAddr32(x, y) = SkColorSetARGB(255, 0, i * 2 % 255, i % 255); | 127 *src_a.getAddr32(x, y) = SkColorSetARGB(255, 0, i * 2 % 255, i % 255); |
136 *src_b.getAddr32(x, y) = | 128 *src_b.getAddr32(x, y) = |
137 SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0); | 129 SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0); |
138 i++; | 130 i++; |
139 } | 131 } |
140 } | 132 } |
141 | 133 |
142 // Shift to red. | 134 // Shift to red. |
143 SkBitmap blended = SkBitmapOperations::CreateBlendedBitmap( | 135 SkBitmap blended = SkBitmapOperations::CreateBlendedBitmap( |
144 src_a, src_b, 0.5); | 136 src_a, src_b, 0.5); |
145 SkAutoLockPixels srca_lock(src_a); | |
146 SkAutoLockPixels srcb_lock(src_b); | |
147 SkAutoLockPixels blended_lock(blended); | |
148 | 137 |
149 for (int y = 0; y < src_h; y++) { | 138 for (int y = 0; y < src_h; y++) { |
150 for (int x = 0; x < src_w; x++) { | 139 for (int x = 0; x < src_w; x++) { |
151 int i = y * src_w + x; | 140 int i = y * src_w + x; |
152 EXPECT_EQ(static_cast<unsigned int>((255 + ((255 - i) % 255)) / 2), | 141 EXPECT_EQ(static_cast<unsigned int>((255 + ((255 - i) % 255)) / 2), |
153 SkColorGetA(*blended.getAddr32(x, y))); | 142 SkColorGetA(*blended.getAddr32(x, y))); |
154 EXPECT_EQ(static_cast<unsigned int>(i % 255 / 2), | 143 EXPECT_EQ(static_cast<unsigned int>(i % 255 / 2), |
155 SkColorGetR(*blended.getAddr32(x, y))); | 144 SkColorGetR(*blended.getAddr32(x, y))); |
156 EXPECT_EQ((static_cast<unsigned int>((i * 2) % 255 + (i * 4) % 255) / 2), | 145 EXPECT_EQ((static_cast<unsigned int>((i * 2) % 255 + (i * 4) % 255) / 2), |
157 SkColorGetG(*blended.getAddr32(x, y))); | 146 SkColorGetG(*blended.getAddr32(x, y))); |
(...skipping 14 matching lines...) Expand all Loading... |
172 alpha.allocN32Pixels(src_w, src_h); | 161 alpha.allocN32Pixels(src_w, src_h); |
173 for (int y = 0, i = 0; y < src_h; y++) { | 162 for (int y = 0, i = 0; y < src_h; y++) { |
174 for (int x = 0; x < src_w; x++) { | 163 for (int x = 0; x < src_w; x++) { |
175 *alpha.getAddr32(x, y) = SkPackARGB32(i % 256, 0, 0, 0); | 164 *alpha.getAddr32(x, y) = SkPackARGB32(i % 256, 0, 0, 0); |
176 i++; | 165 i++; |
177 } | 166 } |
178 } | 167 } |
179 | 168 |
180 SkBitmap masked = SkBitmapOperations::CreateMaskedBitmap(src, alpha); | 169 SkBitmap masked = SkBitmapOperations::CreateMaskedBitmap(src, alpha); |
181 | 170 |
182 SkAutoLockPixels src_lock(src); | |
183 SkAutoLockPixels alpha_lock(alpha); | |
184 SkAutoLockPixels masked_lock(masked); | |
185 | |
186 for (int y = 0; y < src_h; y++) { | 171 for (int y = 0; y < src_h; y++) { |
187 for (int x = 0; x < src_w; x++) { | 172 for (int x = 0; x < src_w; x++) { |
188 int alpha_pixel = *alpha.getAddr32(x, y); | 173 int alpha_pixel = *alpha.getAddr32(x, y); |
189 int src_pixel = *src.getAddr32(x, y); | 174 int src_pixel = *src.getAddr32(x, y); |
190 int masked_pixel = *masked.getAddr32(x, y); | 175 int masked_pixel = *masked.getAddr32(x, y); |
191 | 176 |
192 int scale = SkAlpha255To256(SkGetPackedA32(alpha_pixel)); | 177 int scale = SkAlpha255To256(SkGetPackedA32(alpha_pixel)); |
193 | 178 |
194 int src_a = (src_pixel >> SK_A32_SHIFT) & 0xFF; | 179 int src_a = (src_pixel >> SK_A32_SHIFT) & 0xFF; |
195 int src_r = (src_pixel >> SK_R32_SHIFT) & 0xFF; | 180 int src_r = (src_pixel >> SK_R32_SHIFT) & 0xFF; |
(...skipping 25 matching lines...) Expand all Loading... |
221 for (int x = 0; x < src_w; x++) { | 206 for (int x = 0; x < src_w; x++) { |
222 *src.getAddr32(x, y) = SkPreMultiplyColor(SkColorSetARGB((i + 128) % 255, | 207 *src.getAddr32(x, y) = SkPreMultiplyColor(SkColorSetARGB((i + 128) % 255, |
223 (i + 128) % 255, (i + 64) % 255, (i + 0) % 255)); | 208 (i + 128) % 255, (i + 64) % 255, (i + 0) % 255)); |
224 i++; | 209 i++; |
225 } | 210 } |
226 } | 211 } |
227 | 212 |
228 color_utils::HSL hsl = { -1, -1, -1 }; | 213 color_utils::HSL hsl = { -1, -1, -1 }; |
229 SkBitmap shifted = ReferenceCreateHSLShiftedBitmap(src, hsl); | 214 SkBitmap shifted = ReferenceCreateHSLShiftedBitmap(src, hsl); |
230 | 215 |
231 SkAutoLockPixels src_lock(src); | |
232 SkAutoLockPixels shifted_lock(shifted); | |
233 | |
234 for (int y = 0; y < src_h; y++) { | 216 for (int y = 0; y < src_h; y++) { |
235 for (int x = 0; x < src_w; x++) { | 217 for (int x = 0; x < src_w; x++) { |
236 SkColor src_pixel = *src.getAddr32(x, y); | 218 SkColor src_pixel = *src.getAddr32(x, y); |
237 SkColor shifted_pixel = *shifted.getAddr32(x, y); | 219 SkColor shifted_pixel = *shifted.getAddr32(x, y); |
238 EXPECT_TRUE(MultipliedColorsClose(src_pixel, shifted_pixel)) << | 220 EXPECT_TRUE(MultipliedColorsClose(src_pixel, shifted_pixel)) << |
239 "source: (a,r,g,b) = (" << SkColorGetA(src_pixel) << "," << | 221 "source: (a,r,g,b) = (" << SkColorGetA(src_pixel) << "," << |
240 SkColorGetR(src_pixel) << "," << | 222 SkColorGetR(src_pixel) << "," << |
241 SkColorGetG(src_pixel) << "," << | 223 SkColorGetG(src_pixel) << "," << |
242 SkColorGetB(src_pixel) << "); " << | 224 SkColorGetB(src_pixel) << "); " << |
243 "shifted: (a,r,g,b) = (" << SkColorGetA(shifted_pixel) << "," << | 225 "shifted: (a,r,g,b) = (" << SkColorGetA(shifted_pixel) << "," << |
(...skipping 15 matching lines...) Expand all Loading... |
259 *src.getAddr32(x, y) = SkColorSetARGB(255, 0, 0, i % 255); | 241 *src.getAddr32(x, y) = SkColorSetARGB(255, 0, 0, i % 255); |
260 i++; | 242 i++; |
261 } | 243 } |
262 } | 244 } |
263 | 245 |
264 // Shift to red. | 246 // Shift to red. |
265 color_utils::HSL hsl = { 0, -1, -1 }; | 247 color_utils::HSL hsl = { 0, -1, -1 }; |
266 | 248 |
267 SkBitmap shifted = SkBitmapOperations::CreateHSLShiftedBitmap(src, hsl); | 249 SkBitmap shifted = SkBitmapOperations::CreateHSLShiftedBitmap(src, hsl); |
268 | 250 |
269 SkAutoLockPixels src_lock(src); | |
270 SkAutoLockPixels shifted_lock(shifted); | |
271 | |
272 for (int y = 0, i = 0; y < src_h; y++) { | 251 for (int y = 0, i = 0; y < src_h; y++) { |
273 for (int x = 0; x < src_w; x++) { | 252 for (int x = 0; x < src_w; x++) { |
274 EXPECT_TRUE(ColorsClose(shifted.getColor(x, y), | 253 EXPECT_TRUE(ColorsClose(shifted.getColor(x, y), |
275 SkColorSetARGB(255, i % 255, 0, 0))); | 254 SkColorSetARGB(255, i % 255, 0, 0))); |
276 i++; | 255 i++; |
277 } | 256 } |
278 } | 257 } |
279 } | 258 } |
280 | 259 |
281 // Validate HSL shift. | 260 // Validate HSL shift. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 TEST(SkBitmapOperationsTest, CreateCroppedBitmap) { | 295 TEST(SkBitmapOperationsTest, CreateCroppedBitmap) { |
317 int src_w = 16, src_h = 16; | 296 int src_w = 16, src_h = 16; |
318 SkBitmap src; | 297 SkBitmap src; |
319 FillDataToBitmap(src_w, src_h, &src); | 298 FillDataToBitmap(src_w, src_h, &src); |
320 | 299 |
321 SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap(src, 4, 4, | 300 SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap(src, 4, 4, |
322 8, 8); | 301 8, 8); |
323 ASSERT_EQ(8, cropped.width()); | 302 ASSERT_EQ(8, cropped.width()); |
324 ASSERT_EQ(8, cropped.height()); | 303 ASSERT_EQ(8, cropped.height()); |
325 | 304 |
326 SkAutoLockPixels src_lock(src); | |
327 SkAutoLockPixels cropped_lock(cropped); | |
328 for (int y = 4; y < 12; y++) { | 305 for (int y = 4; y < 12; y++) { |
329 for (int x = 4; x < 12; x++) { | 306 for (int x = 4; x < 12; x++) { |
330 EXPECT_EQ(*src.getAddr32(x, y), | 307 EXPECT_EQ(*src.getAddr32(x, y), |
331 *cropped.getAddr32(x - 4, y - 4)); | 308 *cropped.getAddr32(x - 4, y - 4)); |
332 } | 309 } |
333 } | 310 } |
334 } | 311 } |
335 | 312 |
336 // Test whether our cropping correctly wraps across image boundaries. | 313 // Test whether our cropping correctly wraps across image boundaries. |
337 TEST(SkBitmapOperationsTest, CreateCroppedBitmapWrapping) { | 314 TEST(SkBitmapOperationsTest, CreateCroppedBitmapWrapping) { |
338 int src_w = 16, src_h = 16; | 315 int src_w = 16, src_h = 16; |
339 SkBitmap src; | 316 SkBitmap src; |
340 FillDataToBitmap(src_w, src_h, &src); | 317 FillDataToBitmap(src_w, src_h, &src); |
341 | 318 |
342 SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap( | 319 SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap( |
343 src, src_w / 2, src_h / 2, src_w, src_h); | 320 src, src_w / 2, src_h / 2, src_w, src_h); |
344 ASSERT_EQ(src_w, cropped.width()); | 321 ASSERT_EQ(src_w, cropped.width()); |
345 ASSERT_EQ(src_h, cropped.height()); | 322 ASSERT_EQ(src_h, cropped.height()); |
346 | 323 |
347 SkAutoLockPixels src_lock(src); | |
348 SkAutoLockPixels cropped_lock(cropped); | |
349 for (int y = 0; y < src_h; y++) { | 324 for (int y = 0; y < src_h; y++) { |
350 for (int x = 0; x < src_w; x++) { | 325 for (int x = 0; x < src_w; x++) { |
351 EXPECT_EQ(*src.getAddr32(x, y), | 326 EXPECT_EQ(*src.getAddr32(x, y), |
352 *cropped.getAddr32((x + src_w / 2) % src_w, | 327 *cropped.getAddr32((x + src_w / 2) % src_w, |
353 (y + src_h / 2) % src_h)); | 328 (y + src_h / 2) % src_h)); |
354 } | 329 } |
355 } | 330 } |
356 } | 331 } |
357 | 332 |
358 TEST(SkBitmapOperationsTest, DownsampleByTwo) { | 333 TEST(SkBitmapOperationsTest, DownsampleByTwo) { |
(...skipping 26 matching lines...) Expand all Loading... |
385 *input.getAddr32(2, 1) = 0xFF000000; | 360 *input.getAddr32(2, 1) = 0xFF000000; |
386 *input.getAddr32(0, 2) = 0xFF000000; | 361 *input.getAddr32(0, 2) = 0xFF000000; |
387 *input.getAddr32(1, 2) = 0xFFFFFFFF; | 362 *input.getAddr32(1, 2) = 0xFFFFFFFF; |
388 *input.getAddr32(2, 2) = 0xFF808080; | 363 *input.getAddr32(2, 2) = 0xFF808080; |
389 | 364 |
390 SkBitmap result = SkBitmapOperations::DownsampleByTwo(input); | 365 SkBitmap result = SkBitmapOperations::DownsampleByTwo(input); |
391 EXPECT_EQ(2, result.width()); | 366 EXPECT_EQ(2, result.width()); |
392 EXPECT_EQ(2, result.height()); | 367 EXPECT_EQ(2, result.height()); |
393 | 368 |
394 // Some of the values are off-by-one due to rounding. | 369 // Some of the values are off-by-one due to rounding. |
395 SkAutoLockPixels lock(result); | |
396 EXPECT_EQ(0x9f404040, *result.getAddr32(0, 0)); | 370 EXPECT_EQ(0x9f404040, *result.getAddr32(0, 0)); |
397 EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(1, 0)); | 371 EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(1, 0)); |
398 EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(0, 1)); | 372 EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(0, 1)); |
399 EXPECT_EQ(0xFF808080, *result.getAddr32(1, 1)); | 373 EXPECT_EQ(0xFF808080, *result.getAddr32(1, 1)); |
400 } | 374 } |
401 | 375 |
402 // Test edge cases for DownsampleByTwo. | 376 // Test edge cases for DownsampleByTwo. |
403 TEST(SkBitmapOperationsTest, DownsampleByTwoSmall) { | 377 TEST(SkBitmapOperationsTest, DownsampleByTwoSmall) { |
404 SkPMColor reference = 0xFF4080FF; | 378 SkPMColor reference = 0xFF4080FF; |
405 | 379 |
406 // Test a 1x1 bitmap. | 380 // Test a 1x1 bitmap. |
407 SkBitmap one_by_one; | 381 SkBitmap one_by_one; |
408 one_by_one.allocN32Pixels(1, 1); | 382 one_by_one.allocN32Pixels(1, 1); |
409 *one_by_one.getAddr32(0, 0) = reference; | 383 *one_by_one.getAddr32(0, 0) = reference; |
410 SkBitmap result = SkBitmapOperations::DownsampleByTwo(one_by_one); | 384 SkBitmap result = SkBitmapOperations::DownsampleByTwo(one_by_one); |
411 SkAutoLockPixels lock1(result); | |
412 EXPECT_EQ(1, result.width()); | 385 EXPECT_EQ(1, result.width()); |
413 EXPECT_EQ(1, result.height()); | 386 EXPECT_EQ(1, result.height()); |
414 EXPECT_EQ(reference, *result.getAddr32(0, 0)); | 387 EXPECT_EQ(reference, *result.getAddr32(0, 0)); |
415 | 388 |
416 // Test an n by 1 bitmap. | 389 // Test an n by 1 bitmap. |
417 SkBitmap one_by_n; | 390 SkBitmap one_by_n; |
418 one_by_n.allocN32Pixels(300, 1); | 391 one_by_n.allocN32Pixels(300, 1); |
419 result = SkBitmapOperations::DownsampleByTwo(one_by_n); | 392 result = SkBitmapOperations::DownsampleByTwo(one_by_n); |
420 SkAutoLockPixels lock2(result); | |
421 EXPECT_EQ(300, result.width()); | 393 EXPECT_EQ(300, result.width()); |
422 EXPECT_EQ(1, result.height()); | 394 EXPECT_EQ(1, result.height()); |
423 | 395 |
424 // Test a 1 by n bitmap. | 396 // Test a 1 by n bitmap. |
425 SkBitmap n_by_one; | 397 SkBitmap n_by_one; |
426 n_by_one.allocN32Pixels(1, 300); | 398 n_by_one.allocN32Pixels(1, 300); |
427 result = SkBitmapOperations::DownsampleByTwo(n_by_one); | 399 result = SkBitmapOperations::DownsampleByTwo(n_by_one); |
428 SkAutoLockPixels lock3(result); | |
429 EXPECT_EQ(1, result.width()); | 400 EXPECT_EQ(1, result.width()); |
430 EXPECT_EQ(300, result.height()); | 401 EXPECT_EQ(300, result.height()); |
431 | 402 |
432 // Test an empty bitmap | 403 // Test an empty bitmap |
433 SkBitmap empty; | 404 SkBitmap empty; |
434 result = SkBitmapOperations::DownsampleByTwo(empty); | 405 result = SkBitmapOperations::DownsampleByTwo(empty); |
435 EXPECT_TRUE(result.isNull()); | 406 EXPECT_TRUE(result.isNull()); |
436 EXPECT_EQ(0, result.width()); | 407 EXPECT_EQ(0, result.width()); |
437 EXPECT_EQ(0, result.height()); | 408 EXPECT_EQ(0, result.height()); |
438 } | 409 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 // Set PMColors into the bitmap | 441 // Set PMColors into the bitmap |
471 *input.getAddr32(0, 0) = SkPackARGB32NoCheck(0x80, 0x00, 0x00, 0x00); | 442 *input.getAddr32(0, 0) = SkPackARGB32NoCheck(0x80, 0x00, 0x00, 0x00); |
472 *input.getAddr32(1, 0) = SkPackARGB32NoCheck(0x80, 0x80, 0x80, 0x80); | 443 *input.getAddr32(1, 0) = SkPackARGB32NoCheck(0x80, 0x80, 0x80, 0x80); |
473 *input.getAddr32(0, 1) = SkPackARGB32NoCheck(0xFF, 0x00, 0xCC, 0x88); | 444 *input.getAddr32(0, 1) = SkPackARGB32NoCheck(0xFF, 0x00, 0xCC, 0x88); |
474 *input.getAddr32(1, 1) = SkPackARGB32NoCheck(0x00, 0x00, 0xCC, 0x88); | 445 *input.getAddr32(1, 1) = SkPackARGB32NoCheck(0x00, 0x00, 0xCC, 0x88); |
475 | 446 |
476 SkBitmap result = SkBitmapOperations::UnPreMultiply(input); | 447 SkBitmap result = SkBitmapOperations::UnPreMultiply(input); |
477 EXPECT_EQ(2, result.width()); | 448 EXPECT_EQ(2, result.width()); |
478 EXPECT_EQ(2, result.height()); | 449 EXPECT_EQ(2, result.height()); |
479 | 450 |
480 SkAutoLockPixels lock(result); | |
481 EXPECT_EQ(0x80000000, *result.getAddr32(0, 0)); | 451 EXPECT_EQ(0x80000000, *result.getAddr32(0, 0)); |
482 EXPECT_EQ(0x80FFFFFF, *result.getAddr32(1, 0)); | 452 EXPECT_EQ(0x80FFFFFF, *result.getAddr32(1, 0)); |
483 EXPECT_EQ(0xFF00CC88, *result.getAddr32(0, 1)); | 453 EXPECT_EQ(0xFF00CC88, *result.getAddr32(0, 1)); |
484 EXPECT_EQ(0x00000000u, *result.getAddr32(1, 1)); // "Division by zero". | 454 EXPECT_EQ(0x00000000u, *result.getAddr32(1, 1)); // "Division by zero". |
485 } | 455 } |
486 | 456 |
487 TEST(SkBitmapOperationsTest, CreateTransposedBitmap) { | 457 TEST(SkBitmapOperationsTest, CreateTransposedBitmap) { |
488 SkBitmap input; | 458 SkBitmap input; |
489 input.allocN32Pixels(2, 3); | 459 input.allocN32Pixels(2, 3); |
490 | 460 |
491 for (int x = 0; x < input.width(); ++x) { | 461 for (int x = 0; x < input.width(); ++x) { |
492 for (int y = 0; y < input.height(); ++y) { | 462 for (int y = 0; y < input.height(); ++y) { |
493 *input.getAddr32(x, y) = x * input.width() + y; | 463 *input.getAddr32(x, y) = x * input.width() + y; |
494 } | 464 } |
495 } | 465 } |
496 | 466 |
497 SkBitmap result = SkBitmapOperations::CreateTransposedBitmap(input); | 467 SkBitmap result = SkBitmapOperations::CreateTransposedBitmap(input); |
498 EXPECT_EQ(3, result.width()); | 468 EXPECT_EQ(3, result.width()); |
499 EXPECT_EQ(2, result.height()); | 469 EXPECT_EQ(2, result.height()); |
500 | 470 |
501 SkAutoLockPixels lock(result); | |
502 for (int x = 0; x < input.width(); ++x) { | 471 for (int x = 0; x < input.width(); ++x) { |
503 for (int y = 0; y < input.height(); ++y) { | 472 for (int y = 0; y < input.height(); ++y) { |
504 EXPECT_EQ(*input.getAddr32(x, y), *result.getAddr32(y, x)); | 473 EXPECT_EQ(*input.getAddr32(x, y), *result.getAddr32(y, x)); |
505 } | 474 } |
506 } | 475 } |
507 } | 476 } |
508 | 477 |
509 void DrawRectWithColor(SkCanvas* canvas, | 478 void DrawRectWithColor(SkCanvas* canvas, |
510 int left, | 479 int left, |
511 int top, | 480 int top, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 rotate270 = SkBitmapOperations::Rotate(src, | 519 rotate270 = SkBitmapOperations::Rotate(src, |
551 SkBitmapOperations::ROTATION_270_CW); | 520 SkBitmapOperations::ROTATION_270_CW); |
552 | 521 |
553 ASSERT_EQ(rotate90.width(), src.height()); | 522 ASSERT_EQ(rotate90.width(), src.height()); |
554 ASSERT_EQ(rotate90.height(), src.width()); | 523 ASSERT_EQ(rotate90.height(), src.width()); |
555 ASSERT_EQ(rotate180.width(), src.width()); | 524 ASSERT_EQ(rotate180.width(), src.width()); |
556 ASSERT_EQ(rotate180.height(), src.height()); | 525 ASSERT_EQ(rotate180.height(), src.height()); |
557 ASSERT_EQ(rotate270.width(), src.height()); | 526 ASSERT_EQ(rotate270.width(), src.height()); |
558 ASSERT_EQ(rotate270.height(), src.width()); | 527 ASSERT_EQ(rotate270.height(), src.width()); |
559 | 528 |
560 SkAutoLockPixels lock_src(src); | |
561 SkAutoLockPixels lock_90(rotate90); | |
562 SkAutoLockPixels lock_180(rotate180); | |
563 SkAutoLockPixels lock_270(rotate270); | |
564 | |
565 for (int x=0; x < src_w; ++x) { | 529 for (int x=0; x < src_w; ++x) { |
566 for (int y=0; y < src_h; ++y) { | 530 for (int y=0; y < src_h; ++y) { |
567 ASSERT_EQ(*src.getAddr32(x,y), *rotate90.getAddr32(src_h - (y+1),x)); | 531 ASSERT_EQ(*src.getAddr32(x,y), *rotate90.getAddr32(src_h - (y+1),x)); |
568 ASSERT_EQ(*src.getAddr32(x,y), *rotate270.getAddr32(y, src_w - (x+1))); | 532 ASSERT_EQ(*src.getAddr32(x,y), *rotate270.getAddr32(y, src_w - (x+1))); |
569 ASSERT_EQ(*src.getAddr32(x,y), | 533 ASSERT_EQ(*src.getAddr32(x,y), |
570 *rotate180.getAddr32(src_w - (x+1), src_h - (y+1))); | 534 *rotate180.getAddr32(src_w - (x+1), src_h - (y+1))); |
571 } | 535 } |
572 } | 536 } |
573 } | 537 } |
OLD | NEW |