Chromium Code Reviews| Index: tests/BitmapTransformerTest.cpp |
| =================================================================== |
| --- tests/BitmapTransformerTest.cpp (revision 8777) |
| +++ tests/BitmapTransformerTest.cpp (working copy) |
| @@ -1,4 +1,3 @@ |
| - |
| /* |
| * Copyright 2012 Google Inc. |
| * |
| @@ -6,92 +5,70 @@ |
| * found in the LICENSE file. |
| */ |
| -/** |
| - * Tests for SkBitmapTransformer.h and SkBitmapTransformer.cpp |
| - */ |
| +/** Tests for RAWARGBImageEncoder. */ |
|
epoger
2013/04/19 21:13:02
"RAWARGBImageEncoder" -> "SkARGBImageEncoder" ?
A
bungeman-skia
2013/04/22 18:31:14
Done.
|
| #include "Test.h" |
| #include "SkBitmap.h" |
| -#include "SkBitmapTransformer.h" |
| +#include "SkImageEncoder.h" |
| +#include "SkStream.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()); |
| - } |
| +class BitmapTransformerTestClass : public Test { |
| +public: |
| + static Test* Factory(void*) { return SkNEW(BitmapTransformerTestClass); } |
| +protected: |
| + virtual void onGetName(SkString* name) SK_OVERRIDE { name->set("BitmapTransformer"); } |
| + virtual void onRun(Reporter* reporter) SK_OVERRIDE; |
| +}; |
| - // Valid transformations: |
| - { |
| - // Bytes we expect to get: |
| - const int kWidth = 3; |
| - const int kHeight = 5; |
| - const unsigned 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 |
| - }; |
| +void BitmapTransformerTestClass::onRun(Reporter* reporter) { |
| + // Bytes we expect to get: |
| + const int kWidth = 3; |
| + const int kHeight = 5; |
| + const unsigned 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); |
| + // A bitmap that should generate the above bytes: |
| + SkBitmap bitmap; |
| + { |
| + bitmap.setConfig(SkBitmap::kARGB_8888_Config, kWidth, kHeight); |
|
epoger
2013/04/19 21:13:02
Since SkARGBImageEncoder supports input configs ot
bungeman-skia
2013/04/22 18:31:14
Done.
|
| + REPORTER_ASSERT(reporter, bitmap.allocPixels()); |
| + bitmap.setIsOpaque(true); |
| + bitmap.eraseColor(SK_ColorBLUE); |
| + SkAutoLockPixels alp(bitmap); |
| + // 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(reporter, bitmap.getColor(x, y) == oldColor); |
| + SkPMColor* pixel = static_cast<SkPMColor *>(bitmap.getAddr(x, y)); |
| + *pixel = SkPreMultiplyColor(newColors[y]); |
| + REPORTER_ASSERT(reporter, bitmap.getColor(x, y) == newColors[y]); |
| } |
| - |
| } |
| + } |
| - Reporter* fReporter; |
| - }; |
| + // Transform the bitmap. |
| + int bufferSize = bitmap.width() * bitmap.height() * 4; |
| + SkAutoMalloc pixelBufferManager(bufferSize); |
| + char *pixelBuffer = static_cast<char *>(pixelBufferManager.get()); |
| + SkWStream* out = new SkMemoryWStream(pixelBuffer, bufferSize); |
| + SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); |
| + REPORTER_ASSERT(reporter, enc->encodeStream(out, bitmap, 100)); |
|
epoger
2013/04/19 21:13:02
magic "100" again, please use a constant variable
bungeman-skia
2013/04/22 18:31:14
Done.
|
| - static TestRegistry gReg(BitmapTransformerTestClass::Factory); |
| + // Confirm we got the expected results. |
| + REPORTER_ASSERT(reporter, bufferSize == sizeof(comparisonBuffer)); |
| + REPORTER_ASSERT(reporter, memcmp(pixelBuffer, comparisonBuffer, bufferSize) == 0); |
| } |
| + |
| +static TestRegistry gReg(BitmapTransformerTestClass::Factory); |
| + |
| +} |