diff options
author | epoger@google.com <epoger@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-12-12 17:22:23 +0000 |
---|---|---|
committer | epoger@google.com <epoger@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-12-12 17:22:23 +0000 |
commit | 31114c69f39befd50d2b755b7d0dd1cda2c6d2ab (patch) | |
tree | bf8479ad3a372b64f34f719622290b4afbd9312b /tests | |
parent | ca47aae7ecfdafb5e88baee13737908b79a4c716 (diff) |
Create SkBitmapChecksummer and associated SkBitmapTransformer
As needed to start capturing gm image checksums.
Review URL: https://codereview.appspot.com/6920050
git-svn-id: http://skia.googlecode.com/svn/trunk@6759 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tests')
-rw-r--r-- | tests/BitmapTransformerTest.cpp | 97 | ||||
-rw-r--r-- | tests/ChecksumTest.cpp | 32 |
2 files changed, 129 insertions, 0 deletions
diff --git a/tests/BitmapTransformerTest.cpp b/tests/BitmapTransformerTest.cpp new file mode 100644 index 0000000000..17d0a00da8 --- /dev/null +++ b/tests/BitmapTransformerTest.cpp @@ -0,0 +1,97 @@ + +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * Tests for SkBitmapTransformer.h and SkBitmapTransformer.cpp + */ + +#include "Test.h" +#include "SkBitmap.h" +#include "SkBitmapTransformer.h" + +namespace skiatest { + class BitmapTransformerTestClass : public Test { + public: + static Test* Factory(void*) {return SkNEW(BitmapTransformerTestClass); } + protected: + virtual void onGetName(SkString* name) { name->set("BitmapTransformer"); } + virtual void onRun(Reporter* reporter) { + this->fReporter = reporter; + RunTest(); + } + private: + void RunTest() { + SkBitmap bitmap; + SkBitmap::Config supportedConfig = SkBitmap::kARGB_8888_Config; + SkBitmap::Config unsupportedConfig = SkBitmap::kARGB_4444_Config; + SkBitmapTransformer::PixelFormat supportedPixelFormat = + SkBitmapTransformer::kARGB_8888_Premul_PixelFormat; + const int kWidth = 55; + const int kHeight = 333; + + // Transformations that we know are unsupported: + { + bitmap.setConfig(unsupportedConfig, kWidth, kHeight); + SkBitmapTransformer transformer = SkBitmapTransformer(bitmap, supportedPixelFormat); + REPORTER_ASSERT(fReporter, !transformer.isValid()); + } + + // Valid transformations: + { + // Bytes we expect to get: + const int kWidth = 3; + const int kHeight = 5; + const char comparisonBuffer[] = { + // kHeight rows, each with kWidth pixels, premultiplied ARGB for each pixel + 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, // red + 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, // green + 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue + 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue + 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue + }; + + // A bitmap that should generate the above bytes: + bitmap.setConfig(supportedConfig, kWidth, kHeight); + REPORTER_ASSERT(fReporter, bitmap.allocPixels()); + bitmap.setIsOpaque(true); + bitmap.eraseColor(SK_ColorBLUE); + bitmap.lockPixels(); + // Change rows [0,1] from blue to [red,green]. + SkColor oldColor = SK_ColorBLUE; + SkColor newColors[] = {SK_ColorRED, SK_ColorGREEN}; + for (int y = 0; y <= 1; y++) { + for (int x = 0; x < kWidth; x++) { + REPORTER_ASSERT(fReporter, bitmap.getColor(x, y) == oldColor); + SkPMColor* pixel = static_cast<SkPMColor *>(bitmap.getAddr(x, y)); + *pixel = SkPreMultiplyColor(newColors[y]); + REPORTER_ASSERT(fReporter, bitmap.getColor(x, y) == newColors[y]); + } + } + bitmap.unlockPixels(); + + // Transform the bitmap and confirm we got the expected results. + SkBitmapTransformer transformer = SkBitmapTransformer(bitmap, supportedPixelFormat); + REPORTER_ASSERT(fReporter, transformer.isValid()); + REPORTER_ASSERT(fReporter, transformer.bytesNeededPerRow() == kWidth * 4); + REPORTER_ASSERT(fReporter, transformer.bytesNeededTotal() == kWidth * kHeight * 4); + int bufferSize = transformer.bytesNeededTotal(); + SkAutoMalloc pixelBufferManager(bufferSize); + char *pixelBuffer = static_cast<char *>(pixelBufferManager.get()); + REPORTER_ASSERT(fReporter, + transformer.copyBitmapToPixelBuffer(pixelBuffer, bufferSize)); + REPORTER_ASSERT(fReporter, bufferSize == sizeof(comparisonBuffer)); + REPORTER_ASSERT(fReporter, memcmp(pixelBuffer, comparisonBuffer, bufferSize) == 0); + } + + } + + Reporter* fReporter; + }; + + static TestRegistry gReg(BitmapTransformerTestClass::Factory); +} diff --git a/tests/ChecksumTest.cpp b/tests/ChecksumTest.cpp index e8a2f253b0..03194907f5 100644 --- a/tests/ChecksumTest.cpp +++ b/tests/ChecksumTest.cpp @@ -6,8 +6,12 @@ * found in the LICENSE file. */ #include "Test.h" + +#include "SkBitmap.h" +#include "SkBitmapChecksummer.h" #include "SkChecksum.h" #include "SkCityHash.h" +#include "SkColor.h" // Word size that is large enough to hold results of any checksum type. typedef uint64_t checksum_result; @@ -103,6 +107,15 @@ namespace skiatest { return result; } + // Fill in bitmap with test data. + void CreateTestBitmap(SkBitmap &bitmap, SkBitmap::Config config, int width, int height, + SkColor color) { + bitmap.setConfig(config, width, height); + REPORTER_ASSERT(fReporter, bitmap.allocPixels()); + bitmap.setIsOpaque(true); + bitmap.eraseColor(color); + } + void RunTest() { // Test self-consistency of checksum algorithms. fWhichAlgorithm = kSkChecksum; @@ -143,6 +156,25 @@ namespace skiatest { GetTestDataChecksum(128) == GetTestDataChecksum(256)); REPORTER_ASSERT(fReporter, GetTestDataChecksum(132) == GetTestDataChecksum(260)); + + // Test SkBitmapChecksummer + SkBitmap bitmap; + // initial test case + CreateTestBitmap(bitmap, SkBitmap::kARGB_8888_Config, 333, 555, SK_ColorBLUE); + REPORTER_ASSERT(fReporter, + SkBitmapChecksummer::Compute64(bitmap) == 0x18f9df68b1b02f38ULL); + // same pixel data but different dimensions should yield a different checksum + CreateTestBitmap(bitmap, SkBitmap::kARGB_8888_Config, 555, 333, SK_ColorBLUE); + REPORTER_ASSERT(fReporter, + SkBitmapChecksummer::Compute64(bitmap) == 0x6b0298183f786c8eULL); + // same dimensions but different color should yield a different checksum + CreateTestBitmap(bitmap, SkBitmap::kARGB_8888_Config, 555, 333, SK_ColorGREEN); + REPORTER_ASSERT(fReporter, + SkBitmapChecksummer::Compute64(bitmap) == 0xc6b4b3f6fadaaf37ULL); + // same pixel colors in a different config should yield the same checksum + CreateTestBitmap(bitmap, SkBitmap::kARGB_4444_Config, 555, 333, SK_ColorGREEN); + REPORTER_ASSERT(fReporter, + SkBitmapChecksummer::Compute64(bitmap) == 0xc6b4b3f6fadaaf37ULL); } Reporter* fReporter; |