| OLD | NEW |
| 1 | |
| 2 /* | 1 /* |
| 3 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 4 * | 3 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 7 */ | 6 */ |
| 8 | 7 |
| 9 /** | 8 /** Tests for ARGBImageEncoder. */ |
| 10 * Tests for SkBitmapTransformer.h and SkBitmapTransformer.cpp | |
| 11 */ | |
| 12 | 9 |
| 13 #include "Test.h" | 10 #include "Test.h" |
| 14 #include "SkBitmap.h" | 11 #include "SkBitmap.h" |
| 15 #include "SkBitmapTransformer.h" | 12 #include "SkCanvas.h" |
| 13 #include "SkImageEncoder.h" |
| 14 #include "SkStream.h" |
| 16 | 15 |
| 17 namespace skiatest { | 16 namespace skiatest { |
| 18 class BitmapTransformerTestClass : public Test { | |
| 19 public: | |
| 20 static Test* Factory(void*) {return SkNEW(BitmapTransformerTestClass); } | |
| 21 protected: | |
| 22 virtual void onGetName(SkString* name) { name->set("BitmapTransformer");
} | |
| 23 virtual void onRun(Reporter* reporter) { | |
| 24 this->fReporter = reporter; | |
| 25 RunTest(); | |
| 26 } | |
| 27 private: | |
| 28 void RunTest() { | |
| 29 SkBitmap bitmap; | |
| 30 SkBitmap::Config supportedConfig = SkBitmap::kARGB_8888_Config; | |
| 31 SkBitmap::Config unsupportedConfig = SkBitmap::kARGB_4444_Config; | |
| 32 SkBitmapTransformer::PixelFormat supportedPixelFormat = | |
| 33 SkBitmapTransformer::kARGB_8888_Premul_PixelFormat; | |
| 34 const int kWidth = 55; | |
| 35 const int kHeight = 333; | |
| 36 | 17 |
| 37 // Transformations that we know are unsupported: | 18 class BitmapTransformerTestClass : public Test { |
| 38 { | 19 public: |
| 39 bitmap.setConfig(unsupportedConfig, kWidth, kHeight); | 20 static Test* Factory(void*) { return SkNEW(BitmapTransformerTestClass); } |
| 40 SkBitmapTransformer transformer = SkBitmapTransformer(bitmap, su
pportedPixelFormat); | 21 protected: |
| 41 REPORTER_ASSERT(fReporter, !transformer.isValid()); | 22 virtual void onGetName(SkString* name) SK_OVERRIDE { name->set("ARGBImageEnc
oder"); } |
| 42 } | 23 virtual void onRun(Reporter* reporter) SK_OVERRIDE; |
| 24 }; |
| 43 | 25 |
| 44 // Valid transformations: | 26 static SkBitmap::Config configs[] = { |
| 45 { | 27 SkBitmap::kRGB_565_Config, |
| 46 // Bytes we expect to get: | 28 SkBitmap::kARGB_4444_Config, |
| 47 const int kWidth = 3; | 29 SkBitmap::kARGB_8888_Config, |
| 48 const int kHeight = 5; | 30 }; |
| 49 const unsigned char comparisonBuffer[] = { | |
| 50 // kHeight rows, each with kWidth pixels, premultiplied ARGB
for each pixel | |
| 51 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x0
0, // red | |
| 52 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x0
0, // green | |
| 53 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xf
f, // blue | |
| 54 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xf
f, // blue | |
| 55 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xf
f, // blue | |
| 56 }; | |
| 57 | 31 |
| 58 // A bitmap that should generate the above bytes: | 32 void BitmapTransformerTestClass::onRun(Reporter* reporter) { |
| 59 bitmap.setConfig(supportedConfig, kWidth, kHeight); | 33 // Bytes we expect to get: |
| 60 REPORTER_ASSERT(fReporter, bitmap.allocPixels()); | 34 const int kWidth = 3; |
| 61 bitmap.setIsOpaque(true); | 35 const int kHeight = 5; |
| 62 bitmap.eraseColor(SK_ColorBLUE); | 36 const unsigned char comparisonBuffer[] = { |
| 63 bitmap.lockPixels(); | 37 // kHeight rows, each with kWidth pixels, premultiplied ARGB for each pi
xel |
| 64 // Change rows [0,1] from blue to [red,green]. | 38 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, // red |
| 65 SkColor oldColor = SK_ColorBLUE; | 39 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, // green |
| 66 SkColor newColors[] = {SK_ColorRED, SK_ColorGREEN}; | 40 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue |
| 67 for (int y = 0; y <= 1; y++) { | 41 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue |
| 68 for (int x = 0; x < kWidth; x++) { | 42 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue |
| 69 REPORTER_ASSERT(fReporter, bitmap.getColor(x, y) == oldC
olor); | 43 }; |
| 70 SkPMColor* pixel = static_cast<SkPMColor *>(bitmap.getAd
dr(x, y)); | |
| 71 *pixel = SkPreMultiplyColor(newColors[y]); | |
| 72 REPORTER_ASSERT(fReporter, bitmap.getColor(x, y) == newC
olors[y]); | |
| 73 } | |
| 74 } | |
| 75 bitmap.unlockPixels(); | |
| 76 | 44 |
| 77 // Transform the bitmap and confirm we got the expected results. | 45 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); |
| 78 SkBitmapTransformer transformer = SkBitmapTransformer(bitmap, su
pportedPixelFormat); | 46 for (size_t configIndex = 0; configIndex < SK_ARRAY_COUNT(configs); ++config
Index) { |
| 79 REPORTER_ASSERT(fReporter, transformer.isValid()); | 47 // A bitmap that should generate the above bytes: |
| 80 REPORTER_ASSERT(fReporter, transformer.bytesNeededPerRow() == kW
idth * 4); | 48 SkBitmap bitmap; |
| 81 REPORTER_ASSERT(fReporter, transformer.bytesNeededTotal() == kWi
dth * kHeight * 4); | 49 { |
| 82 int bufferSize = transformer.bytesNeededTotal(); | 50 bitmap.setConfig(configs[configIndex], kWidth, kHeight); |
| 83 SkAutoMalloc pixelBufferManager(bufferSize); | 51 REPORTER_ASSERT(reporter, bitmap.allocPixels()); |
| 84 char *pixelBuffer = static_cast<char *>(pixelBufferManager.get()
); | 52 bitmap.setIsOpaque(true); |
| 85 REPORTER_ASSERT(fReporter, | 53 bitmap.eraseColor(SK_ColorBLUE); |
| 86 transformer.copyBitmapToPixelBuffer(pixelBuffer,
bufferSize)); | 54 // Change rows [0,1] from blue to [red,green]. |
| 87 REPORTER_ASSERT(fReporter, bufferSize == sizeof(comparisonBuffer
)); | 55 SkCanvas canvas(bitmap); |
| 88 REPORTER_ASSERT(fReporter, memcmp(pixelBuffer, comparisonBuffer,
bufferSize) == 0); | 56 SkPaint paint; |
| 89 } | 57 paint.setColor(SK_ColorRED); |
| 90 | 58 canvas.drawIRect(SkIRect::MakeLTRB(0, 0, kWidth, 1), paint); |
| 59 paint.setColor(SK_ColorGREEN); |
| 60 canvas.drawIRect(SkIRect::MakeLTRB(0, 1, kWidth, 2), paint); |
| 91 } | 61 } |
| 92 | 62 |
| 93 Reporter* fReporter; | 63 // Transform the bitmap. |
| 94 }; | 64 int bufferSize = bitmap.width() * bitmap.height() * 4; |
| 65 SkAutoMalloc pixelBufferManager(bufferSize); |
| 66 char *pixelBuffer = static_cast<char *>(pixelBufferManager.get()); |
| 67 SkMemoryWStream out(pixelBuffer, bufferSize); |
| 68 REPORTER_ASSERT(reporter, enc->encodeStream(&out, bitmap, SkImageEncoder
::kDefaultQuality)); |
| 95 | 69 |
| 96 static TestRegistry gReg(BitmapTransformerTestClass::Factory); | 70 // Confirm we got the expected results. |
| 71 REPORTER_ASSERT(reporter, bufferSize == sizeof(comparisonBuffer)); |
| 72 REPORTER_ASSERT(reporter, memcmp(pixelBuffer, comparisonBuffer, bufferSi
ze) == 0); |
| 73 } |
| 97 } | 74 } |
| 75 |
| 76 static TestRegistry gReg(BitmapTransformerTestClass::Factory); |
| 77 |
| 78 } |
| OLD | NEW |