diff options
-rw-r--r-- | AUTHORS | 1 | ||||
-rw-r--r-- | ui/gfx/codec/png_codec.cc | 3 | ||||
-rw-r--r-- | ui/gfx/codec/png_codec_unittest.cc | 42 |
3 files changed, 45 insertions, 1 deletions
@@ -228,3 +228,4 @@ Horia Olaru <olaru@adobe.com> Horia Olaru <horia.olaru@gmail.com> Opera Software ASA <*@opera.com> Johannes Rudolph <johannes.rudolph@googlemail.com> +Sam Larison <qufighter@gmail.com> diff --git a/ui/gfx/codec/png_codec.cc b/ui/gfx/codec/png_codec.cc index 1b8b5c7..135c548 100644 --- a/ui/gfx/codec/png_codec.cc +++ b/ui/gfx/codec/png_codec.cc @@ -768,10 +768,11 @@ bool PNGCodec::EncodeBGRASkBitmap(const SkBitmap& input, if (input.empty()) return false; DCHECK(input.bytesPerPixel() == bbp); + DCHECK(static_cast<int>(input.rowBytes()) >= input.width() * bbp); return Encode(reinterpret_cast<unsigned char*>(input.getAddr32(0, 0)), FORMAT_SkBitmap, Size(input.width(), input.height()), - input.width() * bbp, discard_transparency, + static_cast<int>(input.rowBytes()), discard_transparency, std::vector<Comment>(), output); } diff --git a/ui/gfx/codec/png_codec_unittest.cc b/ui/gfx/codec/png_codec_unittest.cc index c22e7b1..e3540a2 100644 --- a/ui/gfx/codec/png_codec_unittest.cc +++ b/ui/gfx/codec/png_codec_unittest.cc @@ -970,6 +970,48 @@ TEST(PNGCodec, StripAddAlpha) { ASSERT_EQ(original_rgb, decoded); } +TEST(PNGCodec, EncodeBGRASkBitmapStridePadded) { + const int kWidth = 20; + const int kHeight = 20; + const int kPaddedWidth = 32; + const int kBytesPerPixel = 4; + const int kPaddedSize = kPaddedWidth * kHeight; + const int kRowBytes = kPaddedWidth * kBytesPerPixel; + + SkBitmap original_bitmap; + original_bitmap.setConfig(SkBitmap::kARGB_8888_Config, + kWidth, kHeight, kRowBytes); + original_bitmap.allocPixels(); + + // Write data over the source bitmap. + // We write on the pad area here too. + // The encoder should ignore the pad area. + uint32_t* src_data = original_bitmap.getAddr32(0, 0); + for (int i = 0; i < kPaddedSize; i++) { + src_data[i] = SkPreMultiplyARGB(i % 255, i % 250, i % 245, i % 240); + } + + // Encode the bitmap. + std::vector<unsigned char> encoded; + PNGCodec::EncodeBGRASkBitmap(original_bitmap, false, &encoded); + + // Decode the encoded string. + SkBitmap decoded_bitmap; + EXPECT_TRUE(PNGCodec::Decode(&encoded.front(), encoded.size(), + &decoded_bitmap)); + + // Compare the original bitmap and the output bitmap. We use ColorsClose + // as SkBitmaps are considered to be pre-multiplied, the unpremultiplication + // (in Encode) and repremultiplication (in Decode) can be lossy. + for (int x = 0; x < kWidth; x++) { + for (int y = 0; y < kHeight; y++) { + uint32_t original_pixel = original_bitmap.getAddr32(0, y)[x]; + uint32_t decoded_pixel = decoded_bitmap.getAddr32(0, y)[x]; + EXPECT_TRUE(ColorsClose(original_pixel, decoded_pixel)); + } + } +} + TEST(PNGCodec, EncodeBGRASkBitmap) { const int w = 20, h = 20; |