summaryrefslogtreecommitdiffstats
path: root/skia/ext/image_operations_unittest.cc
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-09 17:45:03 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-09 17:45:03 +0000
commitcd5749d607e9edec80d648723360aa1e8ef181c1 (patch)
tree7f2c7cd4cbd81122b38e846e19b0b037d00b07bd /skia/ext/image_operations_unittest.cc
parent9a556e3307dacc7e6d84517d3b78b86c6dbd84f1 (diff)
downloadchromium_src-cd5749d607e9edec80d648723360aa1e8ef181c1.zip
chromium_src-cd5749d607e9edec80d648723360aa1e8ef181c1.tar.gz
chromium_src-cd5749d607e9edec80d648723360aa1e8ef181c1.tar.bz2
Add a mipmap-like divide-by-two image scaling algorithm. I am going to use this
for on-the-fly generated thumbnails. Review URL: http://codereview.chromium.org/118341 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@17957 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia/ext/image_operations_unittest.cc')
-rw-r--r--skia/ext/image_operations_unittest.cc113
1 files changed, 113 insertions, 0 deletions
diff --git a/skia/ext/image_operations_unittest.cc b/skia/ext/image_operations_unittest.cc
index 06bd4fc..3699685 100644
--- a/skia/ext/image_operations_unittest.cc
+++ b/skia/ext/image_operations_unittest.cc
@@ -399,3 +399,116 @@ TEST(ImageOperations, CreateCroppedBitmapWrapping) {
}
}
+TEST(ImageOperations, DownsampleByTwo) {
+ // Use an odd-sized bitmap to make sure the edge cases where there isn't a
+ // 2x2 block of pixels is handled correctly.
+ // Here's the ARGB example
+ //
+ // 50% transparent green opaque 50% blue white
+ // 80008000 FF000080 FFFFFFFF
+ //
+ // 50% transparent red opaque 50% gray black
+ // 80800000 80808080 FF000000
+ //
+ // black white 50% gray
+ // FF000000 FFFFFFFF FF808080
+ //
+ // The result of this computation should be:
+ // A0404040 FF808080
+ // FF808080 FF808080
+ SkBitmap input;
+ input.setConfig(SkBitmap::kARGB_8888_Config, 3, 3);
+ input.allocPixels();
+
+ // The color order may be different, but we don't care (the channels are
+ // trated the same).
+ *input.getAddr32(0, 0) = 0x80008000;
+ *input.getAddr32(1, 0) = 0xFF000080;
+ *input.getAddr32(2, 0) = 0xFFFFFFFF;
+ *input.getAddr32(0, 1) = 0x80800000;
+ *input.getAddr32(1, 1) = 0x80808080;
+ *input.getAddr32(2, 1) = 0xFF000000;
+ *input.getAddr32(0, 2) = 0xFF000000;
+ *input.getAddr32(1, 2) = 0xFFFFFFFF;
+ *input.getAddr32(2, 2) = 0xFF808080;
+
+ SkBitmap result = skia::ImageOperations::DownsampleByTwo(input);
+ EXPECT_EQ(2, result.width());
+ EXPECT_EQ(2, result.height());
+
+ // Some of the values are off-by-one due to rounding.
+ SkAutoLockPixels lock(result);
+ EXPECT_EQ(0x9f404040, *result.getAddr32(0, 0));
+ EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(1, 0));
+ EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(0, 1));
+ EXPECT_EQ(0xFF808080, *result.getAddr32(1, 1));
+}
+
+// Test edge cases for DownsampleByTwo.
+TEST(ImageOperations, DownsampleByTwoSmall) {
+ SkPMColor reference = 0xFF4080FF;
+
+ // Test a 1x1 bitmap.
+ SkBitmap one_by_one;
+ one_by_one.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
+ one_by_one.allocPixels();
+ *one_by_one.getAddr32(0, 0) = reference;
+ SkBitmap result = skia::ImageOperations::DownsampleByTwo(one_by_one);
+ SkAutoLockPixels lock1(result);
+ EXPECT_EQ(1, result.width());
+ EXPECT_EQ(1, result.height());
+ EXPECT_EQ(reference, *result.getAddr32(0, 0));
+
+ // Test an n by 1 bitmap.
+ SkBitmap one_by_n;
+ one_by_n.setConfig(SkBitmap::kARGB_8888_Config, 300, 1);
+ one_by_n.allocPixels();
+ result = skia::ImageOperations::DownsampleByTwo(one_by_n);
+ SkAutoLockPixels lock2(result);
+ EXPECT_EQ(300, result.width());
+ EXPECT_EQ(1, result.height());
+
+ // Test a 1 by n bitmap.
+ SkBitmap n_by_one;
+ n_by_one.setConfig(SkBitmap::kARGB_8888_Config, 1, 300);
+ n_by_one.allocPixels();
+ result = skia::ImageOperations::DownsampleByTwo(n_by_one);
+ SkAutoLockPixels lock3(result);
+ EXPECT_EQ(1, result.width());
+ EXPECT_EQ(300, result.height());
+
+ // Test an empty bitmap
+ SkBitmap empty;
+ result = skia::ImageOperations::DownsampleByTwo(empty);
+ EXPECT_TRUE(result.isNull());
+ EXPECT_EQ(0, result.width());
+ EXPECT_EQ(0, result.height());
+}
+
+// Here we assume DownsampleByTwo works correctly (it's tested above) and
+// just make sure that the
+TEST(ImageOperations, DownsampleByTwoUntilSize) {
+ // First make sure a "too small" bitmap doesn't get modified at all.
+ SkBitmap too_small;
+ too_small.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
+ too_small.allocPixels();
+ SkBitmap result = skia::ImageOperations::DownsampleByTwoUntilSize(
+ too_small, 16, 16);
+ EXPECT_EQ(10, result.width());
+ EXPECT_EQ(10, result.height());
+
+ // Now make sure giving it a 0x0 target returns something reasonable.
+ result = skia::ImageOperations::DownsampleByTwoUntilSize(too_small, 0, 0);
+ EXPECT_EQ(1, result.width());
+ EXPECT_EQ(1, result.height());
+
+ // Test multiple steps of downsampling.
+ SkBitmap large;
+ large.setConfig(SkBitmap::kARGB_8888_Config, 100, 43);
+ large.allocPixels();
+ result = skia::ImageOperations::DownsampleByTwoUntilSize(large, 6, 6);
+
+ // The result should be divided in half 100x43 -> 50x22 -> 25x11
+ EXPECT_EQ(25, result.width());
+ EXPECT_EQ(11, result.height());
+} \ No newline at end of file