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 RAWARGBImageEncoder. */ |
epoger
2013/04/19 21:13:02
"RAWARGBImageEncoder" -> "SkARGBImageEncoder" ?
A
bungeman-skia
2013/04/22 18:31:14
Done.
| |
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 "SkImageEncoder.h" |
13 #include "SkStream.h" | |
16 | 14 |
17 namespace skiatest { | 15 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 | 16 |
37 // Transformations that we know are unsupported: | 17 class BitmapTransformerTestClass : public Test { |
38 { | 18 public: |
39 bitmap.setConfig(unsupportedConfig, kWidth, kHeight); | 19 static Test* Factory(void*) { return SkNEW(BitmapTransformerTestClass); } |
40 SkBitmapTransformer transformer = SkBitmapTransformer(bitmap, su pportedPixelFormat); | 20 protected: |
41 REPORTER_ASSERT(fReporter, !transformer.isValid()); | 21 virtual void onGetName(SkString* name) SK_OVERRIDE { name->set("BitmapTransf ormer"); } |
42 } | 22 virtual void onRun(Reporter* reporter) SK_OVERRIDE; |
23 }; | |
43 | 24 |
44 // Valid transformations: | 25 void BitmapTransformerTestClass::onRun(Reporter* reporter) { |
45 { | 26 // Bytes we expect to get: |
46 // Bytes we expect to get: | 27 const int kWidth = 3; |
47 const int kWidth = 3; | 28 const int kHeight = 5; |
48 const int kHeight = 5; | 29 const unsigned char comparisonBuffer[] = { |
49 const unsigned char comparisonBuffer[] = { | 30 // kHeight rows, each with kWidth pixels, premultiplied ARGB for each pi xel |
50 // kHeight rows, each with kWidth pixels, premultiplied ARGB for each pixel | 31 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, // red |
51 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x0 0, // red | 32 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, // green |
52 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x0 0, // green | 33 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue |
53 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xf f, // blue | 34 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue |
54 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xf f, // blue | 35 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue |
55 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xf f, // blue | |
56 }; | |
57 | |
58 // A bitmap that should generate the above bytes: | |
59 bitmap.setConfig(supportedConfig, kWidth, kHeight); | |
60 REPORTER_ASSERT(fReporter, bitmap.allocPixels()); | |
61 bitmap.setIsOpaque(true); | |
62 bitmap.eraseColor(SK_ColorBLUE); | |
63 bitmap.lockPixels(); | |
64 // Change rows [0,1] from blue to [red,green]. | |
65 SkColor oldColor = SK_ColorBLUE; | |
66 SkColor newColors[] = {SK_ColorRED, SK_ColorGREEN}; | |
67 for (int y = 0; y <= 1; y++) { | |
68 for (int x = 0; x < kWidth; x++) { | |
69 REPORTER_ASSERT(fReporter, bitmap.getColor(x, y) == oldC olor); | |
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 | |
77 // Transform the bitmap and confirm we got the expected results. | |
78 SkBitmapTransformer transformer = SkBitmapTransformer(bitmap, su pportedPixelFormat); | |
79 REPORTER_ASSERT(fReporter, transformer.isValid()); | |
80 REPORTER_ASSERT(fReporter, transformer.bytesNeededPerRow() == kW idth * 4); | |
81 REPORTER_ASSERT(fReporter, transformer.bytesNeededTotal() == kWi dth * kHeight * 4); | |
82 int bufferSize = transformer.bytesNeededTotal(); | |
83 SkAutoMalloc pixelBufferManager(bufferSize); | |
84 char *pixelBuffer = static_cast<char *>(pixelBufferManager.get() ); | |
85 REPORTER_ASSERT(fReporter, | |
86 transformer.copyBitmapToPixelBuffer(pixelBuffer, bufferSize)); | |
87 REPORTER_ASSERT(fReporter, bufferSize == sizeof(comparisonBuffer )); | |
88 REPORTER_ASSERT(fReporter, memcmp(pixelBuffer, comparisonBuffer, bufferSize) == 0); | |
89 } | |
90 | |
91 } | |
92 | |
93 Reporter* fReporter; | |
94 }; | 36 }; |
95 | 37 |
96 static TestRegistry gReg(BitmapTransformerTestClass::Factory); | 38 // A bitmap that should generate the above bytes: |
39 SkBitmap bitmap; | |
40 { | |
41 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.
| |
42 REPORTER_ASSERT(reporter, bitmap.allocPixels()); | |
43 bitmap.setIsOpaque(true); | |
44 bitmap.eraseColor(SK_ColorBLUE); | |
45 SkAutoLockPixels alp(bitmap); | |
46 // Change rows [0,1] from blue to [red,green]. | |
47 SkColor oldColor = SK_ColorBLUE; | |
48 SkColor newColors[] = {SK_ColorRED, SK_ColorGREEN}; | |
49 for (int y = 0; y <= 1; y++) { | |
50 for (int x = 0; x < kWidth; x++) { | |
51 REPORTER_ASSERT(reporter, bitmap.getColor(x, y) == oldColor); | |
52 SkPMColor* pixel = static_cast<SkPMColor *>(bitmap.getAddr(x, y) ); | |
53 *pixel = SkPreMultiplyColor(newColors[y]); | |
54 REPORTER_ASSERT(reporter, bitmap.getColor(x, y) == newColors[y]) ; | |
55 } | |
56 } | |
57 } | |
58 | |
59 // Transform the bitmap. | |
60 int bufferSize = bitmap.width() * bitmap.height() * 4; | |
61 SkAutoMalloc pixelBufferManager(bufferSize); | |
62 char *pixelBuffer = static_cast<char *>(pixelBufferManager.get()); | |
63 SkWStream* out = new SkMemoryWStream(pixelBuffer, bufferSize); | |
64 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); | |
65 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.
| |
66 | |
67 // Confirm we got the expected results. | |
68 REPORTER_ASSERT(reporter, bufferSize == sizeof(comparisonBuffer)); | |
69 REPORTER_ASSERT(reporter, memcmp(pixelBuffer, comparisonBuffer, bufferSize) == 0); | |
97 } | 70 } |
71 | |
72 static TestRegistry gReg(BitmapTransformerTestClass::Factory); | |
73 | |
74 } | |
OLD | NEW |