Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(577)

Side by Side Diff: skia/ext/image_operations_unittest.cc

Issue 118341: Add a mipmap-like divide-by-two image scaling algorithm. I am going to use th... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« skia/ext/image_operations.cc ('K') | « skia/ext/image_operations.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 <stdlib.h> 5 #include <stdlib.h>
6 6
7 #include "skia/ext/image_operations.h" 7 #include "skia/ext/image_operations.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "third_party/skia/include/core/SkColorPriv.h" 9 #include "third_party/skia/include/core/SkColorPriv.h"
10 #include "third_party/skia/include/core/SkBitmap.h" 10 #include "third_party/skia/include/core/SkBitmap.h"
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 SkAutoLockPixels cropped_lock(cropped); 392 SkAutoLockPixels cropped_lock(cropped);
393 for (int y = 0; y < src_h; y++) { 393 for (int y = 0; y < src_h; y++) {
394 for (int x = 0; x < src_w; x++) { 394 for (int x = 0; x < src_w; x++) {
395 EXPECT_EQ(*src.getAddr32(x, y), 395 EXPECT_EQ(*src.getAddr32(x, y),
396 *cropped.getAddr32((x + src_w / 2) % src_w, 396 *cropped.getAddr32((x + src_w / 2) % src_w,
397 (y + src_h / 2) % src_h)); 397 (y + src_h / 2) % src_h));
398 } 398 }
399 } 399 }
400 } 400 }
401 401
402 TEST(ImageOperations, DownsampleByTwo) {
403 // Use an odd-sized bitmap to make sure the edge cases where there isn't a
404 // 2x2 block of pixels is handled correctly.
405 // Here's the ARGB example
406 //
407 // 50% transparent green opaque 50% blue white
408 // 80008000 FF000080 FFFFFFFF
409 //
410 // 50% transparent red opaque 50% gray black
411 // 80800000 80808080 FF000000
412 //
413 // black white 50% gray
414 // FF000000 FFFFFFFF FF808080
415 //
416 // The result of this computation should be:
417 // A0404040 FF808080
418 // FF808080 FF808080
419 SkBitmap input;
420 input.setConfig(SkBitmap::kARGB_8888_Config, 3, 3);
421 input.allocPixels();
422
423 // The color order may be different, but we don't care (the channels are
424 // trated the same).
425 *input.getAddr32(0, 0) = 0x80008000;
426 *input.getAddr32(1, 0) = 0xFF000080;
427 *input.getAddr32(2, 0) = 0xFFFFFFFF;
428 *input.getAddr32(0, 1) = 0x80800000;
429 *input.getAddr32(1, 1) = 0x80808080;
430 *input.getAddr32(2, 1) = 0xFF000000;
431 *input.getAddr32(0, 2) = 0xFF000000;
432 *input.getAddr32(1, 2) = 0xFFFFFFFF;
433 *input.getAddr32(2, 2) = 0xFF808080;
434
435 SkBitmap result = skia::ImageOperations::DownsampleByTwo(input);
436 EXPECT_EQ(2, result.width());
437 EXPECT_EQ(2, result.height());
438
439 // Some of the values are off-by-one due to rounding.
440 SkAutoLockPixels lock(result);
441 EXPECT_EQ(0x9f404040, *result.getAddr32(0, 0));
442 EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(1, 0));
443 EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(0, 1));
444 EXPECT_EQ(0xFF808080, *result.getAddr32(1, 1));
445 }
446
447 // Test edge cases for DownsampleByTwo.
448 TEST(ImageOperations, DownsampleByTwoSmall) {
449 SkPMColor reference = 0xFF4080FF;
450
451 // Test a 1x1 bitmap.
452 SkBitmap one_by_one;
453 one_by_one.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
454 one_by_one.allocPixels();
455 *one_by_one.getAddr32(0, 0) = reference;
456 SkBitmap result = skia::ImageOperations::DownsampleByTwo(one_by_one);
457 SkAutoLockPixels lock1(result);
458 EXPECT_EQ(1, result.width());
459 EXPECT_EQ(1, result.height());
460 EXPECT_EQ(reference, *result.getAddr32(0, 0));
461
462 // Test an n by 1 bitmap.
463 SkBitmap one_by_n;
464 one_by_n.setConfig(SkBitmap::kARGB_8888_Config, 300, 1);
465 one_by_n.allocPixels();
466 result = skia::ImageOperations::DownsampleByTwo(one_by_n);
467 SkAutoLockPixels lock2(result);
468 EXPECT_EQ(300, result.width());
469 EXPECT_EQ(1, result.height());
470
471 // Test a 1 by n bitmap.
472 SkBitmap n_by_one;
473 n_by_one.setConfig(SkBitmap::kARGB_8888_Config, 1, 300);
474 n_by_one.allocPixels();
475 result = skia::ImageOperations::DownsampleByTwo(n_by_one);
476 SkAutoLockPixels lock3(result);
477 EXPECT_EQ(1, result.width());
478 EXPECT_EQ(300, result.height());
479
480 // Test an empty bitmap
481 SkBitmap empty;
482 result = skia::ImageOperations::DownsampleByTwo(empty);
483 EXPECT_TRUE(result.isNull());
484 EXPECT_EQ(0, result.width());
485 EXPECT_EQ(0, result.height());
486 }
487
488 // Here we assume DownsampleByTwo works correctly (it's tested above) and
489 // just make sure that the
490 TEST(ImageOperations, DownsampleByTwoUntilSize) {
491 // First make sure a "too small" bitmap doesn't get modified at all.
492 SkBitmap too_small;
493 too_small.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
494 too_small.allocPixels();
495 SkBitmap result = skia::ImageOperations::DownsampleByTwoUntilSize(
496 too_small, 16, 16);
497 EXPECT_EQ(10, result.width());
498 EXPECT_EQ(10, result.height());
499
500 // Now make sure giving it a 0x0 target returns something reasonable.
501 result = skia::ImageOperations::DownsampleByTwoUntilSize(too_small, 0, 0);
502 EXPECT_EQ(1, result.width());
503 EXPECT_EQ(1, result.height());
504
505 // Test multiple steps of downsampling.
506 SkBitmap large;
507 large.setConfig(SkBitmap::kARGB_8888_Config, 100, 43);
508 large.allocPixels();
509 result = skia::ImageOperations::DownsampleByTwoUntilSize(large, 6, 6);
510
511 // The result should be divided in half 100x43 -> 50x22 -> 25x11
512 EXPECT_EQ(25, result.width());
513 EXPECT_EQ(11, result.height());
514 }
OLDNEW
« skia/ext/image_operations.cc ('K') | « skia/ext/image_operations.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698