Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 | 10 |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 bmp->allocPixels(SkImageInfo::MakeA8(w, h)); | 272 bmp->allocPixels(SkImageInfo::MakeA8(w, h)); |
| 273 | 273 |
| 274 uint8_t* src_data = bmp->getAddr8(0, 0); | 274 uint8_t* src_data = bmp->getAddr8(0, 0); |
| 275 for (int i = 0; i < w * h; i++) | 275 for (int i = 0; i < w * h; i++) |
| 276 src_data[i] = i % 255; | 276 src_data[i] = i % 255; |
| 277 } | 277 } |
| 278 | 278 |
| 279 TEST(PNGCodec, EncodeDecodeRGBA) { | 279 TEST(PNGCodec, EncodeDecodeRGBA) { |
| 280 const int w = 20, h = 20; | 280 const int w = 20, h = 20; |
| 281 | 281 |
| 282 // create an image with known values, a must be opaque because it will be | 282 // create an image with known values, a must be opaque because it will be |
|
scroggo_chromium
2017/06/16 18:56:51
This comment is strange. Why can we not have trans
liyuqian
2017/06/23 13:54:26
I've no idea what this comment means... Anyway, I
| |
| 283 // lost during encoding | 283 // lost during encoding |
| 284 std::vector<unsigned char> original; | 284 std::vector<unsigned char> original; |
| 285 MakeRGBAImage(w, h, true, &original); | 285 MakeRGBAImage(w, h, true, &original); |
| 286 | 286 |
| 287 // encode | 287 // encode |
| 288 std::vector<unsigned char> encoded; | 288 std::vector<unsigned char> encoded; |
| 289 ASSERT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGBA, | 289 SkImageInfo info = |
| 290 Size(w, h), w * 4, false, | 290 SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType); |
| 291 std::vector<PNGCodec::Comment>(), | 291 SkPixmap src(info, &original[0], w * 4); |
| 292 &encoded)); | 292 ASSERT_TRUE( |
| 293 PNGCodec::Encode(src, std::vector<PNGCodec::Comment>(), &encoded)); | |
| 293 | 294 |
| 294 // decode, it should have the same size as the original | 295 // decode, it should have the same size as the original |
| 295 std::vector<unsigned char> decoded; | 296 std::vector<unsigned char> decoded; |
| 296 int outw, outh; | 297 int outw, outh; |
| 297 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), | 298 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), |
| 298 PNGCodec::FORMAT_RGBA, &decoded, | 299 PNGCodec::FORMAT_RGBA, &decoded, |
| 299 &outw, &outh)); | 300 &outw, &outh)); |
| 300 ASSERT_EQ(w, outw); | 301 ASSERT_EQ(w, outw); |
| 301 ASSERT_EQ(h, outh); | 302 ASSERT_EQ(h, outh); |
| 302 ASSERT_EQ(original.size(), decoded.size()); | 303 ASSERT_EQ(original.size(), decoded.size()); |
| 303 | 304 |
| 304 // Images must be exactly equal | 305 // Images must be exactly equal |
| 305 ASSERT_TRUE(original == decoded); | 306 ASSERT_TRUE(original == decoded); |
| 306 } | 307 } |
| 307 | 308 |
| 308 TEST(PNGCodec, EncodeDecodeBGRA) { | 309 TEST(PNGCodec, EncodeDecodeBGRA) { |
| 309 const int w = 20, h = 20; | 310 const int w = 20, h = 20; |
| 310 | 311 |
| 311 // Create an image with known values, alpha must be opaque because it will be | 312 // Create an image with known values, alpha must be opaque because it will be |
| 312 // lost during encoding. | 313 // lost during encoding. |
| 313 std::vector<unsigned char> original; | 314 std::vector<unsigned char> original; |
| 314 MakeRGBAImage(w, h, true, &original); | 315 MakeRGBAImage(w, h, true, &original); |
| 315 | 316 |
| 316 // Encode. | 317 // Encode. |
| 317 std::vector<unsigned char> encoded; | 318 std::vector<unsigned char> encoded; |
| 318 ASSERT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_BGRA, | 319 SkImageInfo info = |
| 319 Size(w, h), w * 4, false, | 320 SkImageInfo::Make(w, h, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType); |
| 320 std::vector<PNGCodec::Comment>(), | 321 SkPixmap src(info, &original[0], w * 4); |
| 321 &encoded)); | 322 ASSERT_TRUE( |
| 323 PNGCodec::Encode(src, std::vector<PNGCodec::Comment>(), &encoded)); | |
| 322 | 324 |
| 323 // Decode, it should have the same size as the original. | 325 // Decode, it should have the same size as the original. |
| 324 std::vector<unsigned char> decoded; | 326 std::vector<unsigned char> decoded; |
| 325 int outw, outh; | 327 int outw, outh; |
| 326 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), | 328 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), |
| 327 PNGCodec::FORMAT_BGRA, &decoded, | 329 PNGCodec::FORMAT_BGRA, &decoded, |
| 328 &outw, &outh)); | 330 &outw, &outh)); |
| 329 ASSERT_EQ(w, outw); | 331 ASSERT_EQ(w, outw); |
| 330 ASSERT_EQ(h, outh); | 332 ASSERT_EQ(h, outh); |
| 331 ASSERT_EQ(original.size(), decoded.size()); | 333 ASSERT_EQ(original.size(), decoded.size()); |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 742 MakeRGBAImage(w, h, false, &original); | 744 MakeRGBAImage(w, h, false, &original); |
| 743 | 745 |
| 744 // It should fail when given non-PNG compressed data. | 746 // It should fail when given non-PNG compressed data. |
| 745 std::vector<unsigned char> output; | 747 std::vector<unsigned char> output; |
| 746 int outw, outh; | 748 int outw, outh; |
| 747 EXPECT_FALSE(PNGCodec::Decode(&original[0], original.size(), | 749 EXPECT_FALSE(PNGCodec::Decode(&original[0], original.size(), |
| 748 PNGCodec::FORMAT_RGBA, &output, &outw, &outh)); | 750 PNGCodec::FORMAT_RGBA, &output, &outw, &outh)); |
| 749 | 751 |
| 750 // Make some compressed data. | 752 // Make some compressed data. |
| 751 std::vector<unsigned char> compressed; | 753 std::vector<unsigned char> compressed; |
| 752 ASSERT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGBA, Size(w, h), | 754 SkImageInfo info = |
| 753 w * 4, false, std::vector<PNGCodec::Comment>(), | 755 SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType); |
| 754 &compressed)); | 756 SkPixmap src(info, &original[0], w * 4); |
| 757 ASSERT_TRUE( | |
| 758 PNGCodec::Encode(src, std::vector<PNGCodec::Comment>(), &compressed)); | |
| 755 | 759 |
| 756 // Try decompressing a truncated version. | 760 // Try decompressing a truncated version. |
| 757 EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size() / 2, | 761 EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size() / 2, |
| 758 PNGCodec::FORMAT_RGBA, &output, &outw, &outh)); | 762 PNGCodec::FORMAT_RGBA, &output, &outw, &outh)); |
| 759 | 763 |
| 760 // Corrupt it and try decompressing that. | 764 // Corrupt it and try decompressing that. |
| 761 for (int i = 10; i < 30; i++) | 765 for (int i = 10; i < 30; i++) |
| 762 compressed[i] = i; | 766 compressed[i] = i; |
| 763 EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size(), | 767 EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size(), |
| 764 PNGCodec::FORMAT_RGBA, &output, &outw, &outh)); | 768 PNGCodec::FORMAT_RGBA, &output, &outw, &outh)); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 901 const int w = 10, h = 10; | 905 const int w = 10, h = 10; |
| 902 | 906 |
| 903 std::vector<unsigned char> original; | 907 std::vector<unsigned char> original; |
| 904 MakeRGBAImage(w, h, true, &original); | 908 MakeRGBAImage(w, h, true, &original); |
| 905 | 909 |
| 906 std::vector<unsigned char> encoded; | 910 std::vector<unsigned char> encoded; |
| 907 std::vector<PNGCodec::Comment> comments; | 911 std::vector<PNGCodec::Comment> comments; |
| 908 comments.push_back(PNGCodec::Comment("key", "text")); | 912 comments.push_back(PNGCodec::Comment("key", "text")); |
| 909 comments.push_back(PNGCodec::Comment("test", "something")); | 913 comments.push_back(PNGCodec::Comment("test", "something")); |
| 910 comments.push_back(PNGCodec::Comment("have some", "spaces in both")); | 914 comments.push_back(PNGCodec::Comment("have some", "spaces in both")); |
| 911 EXPECT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGBA, Size(w, h), | 915 SkImageInfo info = |
| 912 w * 4, false, comments, &encoded)); | 916 SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType); |
| 917 SkPixmap src(info, &original[0], w * 4); | |
| 918 EXPECT_TRUE(PNGCodec::Encode(src, comments, &encoded)); | |
| 913 | 919 |
| 914 // Each chunk is of the form length (4 bytes), chunk type (tEXt), data, | 920 // Each chunk is of the form length (4 bytes), chunk type (tEXt), data, |
| 915 // checksum (4 bytes). Make sure we find all of them in the encoded | 921 // checksum (4 bytes). Make sure we find all of them in the encoded |
| 916 // results. | 922 // results. |
| 917 const unsigned char kExpected1[] = | 923 const unsigned char kExpected1[] = |
| 918 "\x00\x00\x00\x08tEXtkey\x00text\x9e\xe7\x66\x51"; | 924 "\x00\x00\x00\x08tEXtkey\x00text\x9e\xe7\x66\x51"; |
| 919 const unsigned char kExpected2[] = | 925 const unsigned char kExpected2[] = |
| 920 "\x00\x00\x00\x0etEXttest\x00something\x29\xba\xef\xac"; | 926 "\x00\x00\x00\x0etEXttest\x00something\x29\xba\xef\xac"; |
| 921 const unsigned char kExpected3[] = | 927 const unsigned char kExpected3[] = |
| 922 "\x00\x00\x00\x18tEXthave some\x00spaces in both\x8d\x69\x34\x2d"; | 928 "\x00\x00\x00\x18tEXthave some\x00spaces in both\x8d\x69\x34\x2d"; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 959 PNGCodec::Decode(&encoded_normal[0], encoded_normal.size(), &decoded)); | 965 PNGCodec::Decode(&encoded_normal[0], encoded_normal.size(), &decoded)); |
| 960 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); | 966 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); |
| 961 | 967 |
| 962 EXPECT_TRUE( | 968 EXPECT_TRUE( |
| 963 PNGCodec::Decode(&encoded_fast[0], encoded_fast.size(), &decoded)); | 969 PNGCodec::Decode(&encoded_fast[0], encoded_fast.size(), &decoded)); |
| 964 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); | 970 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); |
| 965 } | 971 } |
| 966 | 972 |
| 967 | 973 |
| 968 } // namespace gfx | 974 } // namespace gfx |
| OLD | NEW |