Chromium Code Reviews| Index: ui/gfx/codec/png_codec_unittest.cc |
| diff --git a/ui/gfx/codec/png_codec_unittest.cc b/ui/gfx/codec/png_codec_unittest.cc |
| index f1654d9d363bfbf15a5594c6ea3cfa9b66067a2e..f332173d6f9458ce37cd944adc039e6bab7c3c32 100644 |
| --- a/ui/gfx/codec/png_codec_unittest.cc |
| +++ b/ui/gfx/codec/png_codec_unittest.cc |
| @@ -249,7 +249,15 @@ bool NonAlphaColorsClose(uint32_t a, uint32_t b) { |
| abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) < 2; |
| } |
| -void MakeTestSkBitmap(int w, int h, SkBitmap* bmp) { |
| +// Returns true if the BGRA 4-bit SkColor is "close" to the Gray 8-bit SkColor. |
|
Peter Kasting
2014/02/04 01:37:15
Nit: Don't you mean 32-bit (or 4-byte)?
|
| +bool BGRAGrayColorsClose(uint32_t a, uint8_t b) { |
| + return abs(static_cast<int>(SkColorGetB(a) - b)) < 2 && |
| + abs(static_cast<int>(SkColorGetG(a) - b)) < 2 && |
| + abs(static_cast<int>(SkColorGetR(a) - b)) < 2 && |
| + abs(static_cast<int>(SkColorGetA(a) - 255)) < 2; |
| +} |
| + |
| +void MakeTestBGRASkBitmap(int w, int h, SkBitmap* bmp) { |
| bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| bmp->allocPixels(); |
| @@ -259,6 +267,16 @@ void MakeTestSkBitmap(int w, int h, SkBitmap* bmp) { |
| } |
| } |
| +void MakeTestA8SkBitmap(int w, int h, SkBitmap* bmp) { |
| + bmp->setConfig(SkBitmap::kA8_Config, w, h); |
| + bmp->allocPixels(); |
| + |
| + uint8_t* src_data = bmp->getAddr8(0, 0); |
| + for (int i = 0; i < w * h; i++) { |
|
Peter Kasting
2014/02/04 01:37:15
Nit: {} unnecessary
|
| + src_data[i] = i % 255; |
| + } |
| +} |
| + |
| TEST(PNGCodec, EncodeDecodeRGB) { |
| const int w = 20, h = 20; |
| @@ -1017,7 +1035,7 @@ TEST(PNGCodec, EncodeBGRASkBitmap) { |
| const int w = 20, h = 20; |
| SkBitmap original_bitmap; |
| - MakeTestSkBitmap(w, h, &original_bitmap); |
| + MakeTestBGRASkBitmap(w, h, &original_bitmap); |
| // Encode the bitmap. |
| std::vector<unsigned char> encoded; |
| @@ -1040,11 +1058,35 @@ TEST(PNGCodec, EncodeBGRASkBitmap) { |
| } |
| } |
| +TEST(PNGCodec, EncodeA8SkBitmap) { |
| + const int w = 20, h = 20; |
| + |
| + SkBitmap original_bitmap; |
| + MakeTestA8SkBitmap(w, h, &original_bitmap); |
| + |
| + // Encode the bitmap. |
| + std::vector<unsigned char> encoded; |
| + EXPECT_TRUE(PNGCodec::EncodeA8SkBitmap(original_bitmap, &encoded)); |
| + |
| + // Decode the encoded string. |
| + SkBitmap decoded_bitmap; |
| + EXPECT_TRUE(PNGCodec::Decode(&encoded.front(), encoded.size(), |
| + &decoded_bitmap)); |
| + |
| + for (int x = 0; x < w; x++) { |
| + for (int y = 0; y < h; y++) { |
| + uint8_t original_pixel = *original_bitmap.getAddr8(x, y); |
| + uint32_t decoded_pixel = *decoded_bitmap.getAddr32(x, y); |
| + EXPECT_TRUE(BGRAGrayColorsClose(decoded_pixel, original_pixel)); |
|
Peter Kasting
2014/02/04 01:37:15
How come we can't just check for direct equality?
|
| + } |
| + } |
| +} |
| + |
| TEST(PNGCodec, EncodeBGRASkBitmapDiscardTransparency) { |
| const int w = 20, h = 20; |
| SkBitmap original_bitmap; |
| - MakeTestSkBitmap(w, h, &original_bitmap); |
| + MakeTestBGRASkBitmap(w, h, &original_bitmap); |
| // Encode the bitmap. |
| std::vector<unsigned char> encoded; |
| @@ -1121,7 +1163,7 @@ TEST(PNGCodec, EncodeDecodeWithVaryingCompressionLevels) { |
| // create an image with known values, a must be opaque because it will be |
| // lost during encoding |
| SkBitmap original_bitmap; |
| - MakeTestSkBitmap(w, h, &original_bitmap); |
| + MakeTestBGRASkBitmap(w, h, &original_bitmap); |
| // encode |
| std::vector<unsigned char> encoded_normal; |