OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "platform/image-decoders/png/PNGImageDecoder.h" | 5 #include "platform/image-decoders/png/PNGImageDecoder.h" |
6 | 6 |
7 #include "platform/image-decoders/ImageDecoderTestHelpers.h" | 7 #include "platform/image-decoders/ImageDecoderTestHelpers.h" |
| 8 #include "png.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 #include <memory> | 10 #include <memory> |
10 | 11 |
| 12 // /LayoutTests/images/resources/png-animated-idat-part-of-animation.png |
| 13 // is modified in multiple tests to simulate erroneous PNGs. As a reference, |
| 14 // the table below shows how the file is structured. |
| 15 // |
| 16 // Offset | 8 33 95 133 172 210 241 279 314 352 422 |
| 17 // ------------------------------------------------------------------------- |
| 18 // Chunk | IHDR acTL fcTL IDAT fcTL fdAT fcTL fdAT fcTL fdAT IEND |
| 19 // |
| 20 // In between the acTL and fcTL there are two other chunks, PLTE and tRNS, but |
| 21 // those are not specifically used in this test suite. The same holds for a |
| 22 // tEXT chunk in between the last fdAT and IEND. |
| 23 // |
| 24 // In the current behavior of PNG image decoders, the 4 frames are detected when |
| 25 // respectively 141, 249, 322 and 430 bytes are received. The first frame should |
| 26 // be detected when the IDAT has been received, and non-first frames when the |
| 27 // next fcTL or IEND chunk has been received. Note that all offsets are +8, |
| 28 // because a chunk is identified by byte 4-7. |
| 29 |
| 30 // @TODO(joostouwerling) extend test image set with other image encodings, such |
| 31 // as first frame fcTL and multiple chunks per frame. |
| 32 |
11 namespace blink { | 33 namespace blink { |
12 | 34 |
13 namespace { | 35 namespace { |
14 | 36 |
15 std::unique_ptr<ImageDecoder> createDecoder( | 37 std::unique_ptr<ImageDecoder> createDecoder( |
16 ImageDecoder::AlphaOption alphaOption) { | 38 ImageDecoder::AlphaOption alphaOption) { |
17 return wrapUnique( | 39 return wrapUnique( |
18 new PNGImageDecoder(alphaOption, ImageDecoder::ColorSpaceTransformed, | 40 new PNGImageDecoder(alphaOption, ImageDecoder::ColorSpaceTransformed, |
19 ImageDecoder::targetColorSpaceForTesting(), | 41 ImageDecoder::targetColorSpaceForTesting(), |
20 ImageDecoder::noDecodedImageByteLimit)); | 42 ImageDecoder::noDecodedImageByteLimit)); |
(...skipping 10 matching lines...) Expand all Loading... |
31 decoder->setData(data.get(), true); | 53 decoder->setData(data.get(), true); |
32 return decoder; | 54 return decoder; |
33 } | 55 } |
34 | 56 |
35 void testSize(const char* pngFile, IntSize expectedSize) { | 57 void testSize(const char* pngFile, IntSize expectedSize) { |
36 auto decoder = createDecoderWithPngData(pngFile); | 58 auto decoder = createDecoderWithPngData(pngFile); |
37 EXPECT_TRUE(decoder->isSizeAvailable()); | 59 EXPECT_TRUE(decoder->isSizeAvailable()); |
38 EXPECT_EQ(expectedSize, decoder->size()); | 60 EXPECT_EQ(expectedSize, decoder->size()); |
39 } | 61 } |
40 | 62 |
| 63 // Test whether querying for the size of the image works if we present the |
| 64 // data byte by byte. |
| 65 void testSizeByteByByte(const char* pngFile, |
| 66 size_t bytesNeededToDecodeSize, |
| 67 IntSize expectedSize) { |
| 68 auto decoder = createDecoder(); |
| 69 auto data = readFile(pngFile); |
| 70 ASSERT_FALSE(data->isEmpty()); |
| 71 ASSERT_LT(bytesNeededToDecodeSize, data->size()); |
| 72 |
| 73 for (size_t length = 1; length <= bytesNeededToDecodeSize; length++) { |
| 74 RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), length); |
| 75 decoder->setData(tempData.get(), false); |
| 76 |
| 77 if (length < bytesNeededToDecodeSize) { |
| 78 EXPECT_FALSE(decoder->isSizeAvailable()); |
| 79 EXPECT_TRUE(decoder->size().isEmpty()); |
| 80 EXPECT_FALSE(decoder->failed()); |
| 81 } else { |
| 82 EXPECT_TRUE(decoder->isSizeAvailable()); |
| 83 EXPECT_EQ(expectedSize, decoder->size()); |
| 84 } |
| 85 } |
| 86 EXPECT_FALSE(decoder->failed()); |
| 87 } |
| 88 |
| 89 void writeUint32(uint32_t val, png_byte* data) { |
| 90 data[0] = val >> 24; |
| 91 data[1] = val >> 16; |
| 92 data[2] = val >> 8; |
| 93 data[3] = val; |
| 94 } |
| 95 |
41 void testRepetitionCount(const char* pngFile, int expectedRepetitionCount) { | 96 void testRepetitionCount(const char* pngFile, int expectedRepetitionCount) { |
42 auto decoder = createDecoderWithPngData(pngFile); | 97 auto decoder = createDecoderWithPngData(pngFile); |
43 // Decoding the frame count sets the repetition count as well. | 98 // Decode frame count should see the number of repetitions as well. |
44 decoder->frameCount(); | 99 decoder->frameCount(); |
45 EXPECT_FALSE(decoder->failed()); | 100 EXPECT_FALSE(decoder->failed()); |
46 EXPECT_EQ(expectedRepetitionCount, decoder->repetitionCount()); | 101 EXPECT_EQ(expectedRepetitionCount, decoder->repetitionCount()); |
47 } | 102 } |
48 | 103 |
| 104 struct PublicFrameInfo { |
| 105 size_t duration; |
| 106 IntRect frameRect; |
| 107 ImageFrame::AlphaBlendSource alphaBlend; |
| 108 ImageFrame::DisposalMethod disposalMethod; |
| 109 }; |
| 110 |
| 111 // This is the frame data for the following PNG image: |
| 112 // /LayoutTests/images/resources/png-animated-idat-part-of-animation.png |
| 113 static PublicFrameInfo pngAnimatedFrameInfo[] = { |
| 114 {500, |
| 115 {IntPoint(0, 0), IntSize(5, 5)}, |
| 116 ImageFrame::BlendAtopBgcolor, |
| 117 ImageFrame::DisposeKeep}, |
| 118 {900, |
| 119 {IntPoint(1, 1), IntSize(3, 1)}, |
| 120 ImageFrame::BlendAtopBgcolor, |
| 121 ImageFrame::DisposeOverwriteBgcolor}, |
| 122 {2000, |
| 123 {IntPoint(1, 2), IntSize(3, 2)}, |
| 124 ImageFrame::BlendAtopPreviousFrame, |
| 125 ImageFrame::DisposeKeep}, |
| 126 {1500, |
| 127 {IntPoint(1, 2), IntSize(3, 1)}, |
| 128 ImageFrame::BlendAtopBgcolor, |
| 129 ImageFrame::DisposeKeep}, |
| 130 }; |
| 131 |
| 132 void compareFrameWithExpectation(const PublicFrameInfo& expected, |
| 133 const ImageFrame* frame) { |
| 134 EXPECT_EQ(expected.duration, frame->duration()); |
| 135 EXPECT_EQ(expected.frameRect, frame->originalFrameRect()); |
| 136 EXPECT_EQ(expected.disposalMethod, frame->getDisposalMethod()); |
| 137 EXPECT_EQ(expected.alphaBlend, frame->getAlphaBlendSource()); |
| 138 } |
| 139 |
| 140 // This function removes |length| bytes at |offset|, and then calls frameCount. |
| 141 // It assumes the missing bytes should result in a failed decode because the |
| 142 // parser jumps |length| bytes too far in the next chunk. |
| 143 void testMissingDataBreaksDecoding(const char* pngFile, |
| 144 size_t offset, |
| 145 size_t length) { |
| 146 auto decoder = createDecoder(); |
| 147 auto data = readFile(pngFile); |
| 148 ASSERT_FALSE(data->isEmpty()); |
| 149 |
| 150 RefPtr<SharedBuffer> invalidData = SharedBuffer::create(data->data(), offset); |
| 151 invalidData->append(SharedBuffer::create(data->data() + offset + length, |
| 152 data->size() - offset - length)); |
| 153 ASSERT_EQ(data->size() - length, invalidData->size()); |
| 154 |
| 155 decoder->setData(invalidData, true); |
| 156 decoder->frameCount(); |
| 157 EXPECT_TRUE(decoder->failed()); |
| 158 } |
| 159 |
| 160 // Decoding up to the indicated fcTL offset and then provide an fcTL with |
| 161 // the wrong chunk size (20 instead of 26). It should break the decoder. |
| 162 void testInvalidFctlSize(const char* pngFile, |
| 163 size_t offsetFctl, |
| 164 size_t expectedFrameCountBeforeFail, |
| 165 bool expectedDecoderFailure) { |
| 166 auto data = readFile(pngFile); |
| 167 ASSERT_FALSE(data->isEmpty()); |
| 168 |
| 169 auto decoder = createDecoder(); |
| 170 RefPtr<SharedBuffer> invalidData = |
| 171 SharedBuffer::create(data->data(), offsetFctl); |
| 172 |
| 173 // Test if this gives the correct frame count, before the fcTL is parsed. |
| 174 decoder->setData(invalidData, false); |
| 175 EXPECT_EQ(expectedFrameCountBeforeFail, decoder->frameCount()); |
| 176 ASSERT_FALSE(decoder->failed()); |
| 177 |
| 178 // Append the wrong size to the data stream |
| 179 png_byte sizeChunk[4]; |
| 180 writeUint32(20, sizeChunk); |
| 181 invalidData->append(reinterpret_cast<char*>(sizeChunk), 4u); |
| 182 |
| 183 // Skip the size in the original data, but provide the rest of the fcTL, |
| 184 // which is 4B of tag, 26B of data and 4B of CRC, totalling 34B. |
| 185 invalidData->append(data->data() + offsetFctl + 4, 34u); |
| 186 |
| 187 decoder->setData(invalidData, false); |
| 188 decoder->frameCount(); |
| 189 EXPECT_EQ(expectedDecoderFailure, decoder->failed()); |
| 190 } |
| 191 |
| 192 void testDifferentActlFrameCountIsIgnored(const char* pngFile, |
| 193 size_t offsetActl, |
| 194 size_t injectedFrameCount, |
| 195 size_t expectedFrameCount) { |
| 196 // First make sure that this tests makes sense. |
| 197 ASSERT_NE(injectedFrameCount, expectedFrameCount); |
| 198 |
| 199 auto data = readFile(pngFile); |
| 200 auto decoder = createDecoder(); |
| 201 ASSERT_FALSE(data->isEmpty()); |
| 202 |
| 203 RefPtr<SharedBuffer> diffActlData = |
| 204 SharedBuffer::create(data->data(), offsetActl + 8); |
| 205 // Write the injectedFrameCount to the stream |
| 206 png_byte sizeChunk[4]; |
| 207 writeUint32(injectedFrameCount, sizeChunk); |
| 208 diffActlData->append(reinterpret_cast<char*>(sizeChunk), 4u); |
| 209 // Append the rest of the data. The first |offsetActl + 12| bytes that are |
| 210 // already in diffActlData should not be appended again. |
| 211 diffActlData->append(data->data() + offsetActl + 12, |
| 212 data->size() - offsetActl - 12); |
| 213 |
| 214 decoder->setData(diffActlData, true); |
| 215 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
| 216 } |
| 217 |
49 // Verify that the decoder can successfully decode the first frame when | 218 // Verify that the decoder can successfully decode the first frame when |
50 // initially only half of the frame data is received, resulting in a partially | 219 // initially only half of the frame data is received, resulting in a partially |
51 // decoded image, and then the rest of the image data is received. Verify that | 220 // decoded image, and then the rest of the image data is received. Verify that |
52 // the bitmap hashes of the two stages are different. Also verify that the final | 221 // the bitmap hashes of the two stages are different. Also verify that the final |
53 // bitmap hash is equivalent to the hash when all data is provided at once. | 222 // bitmap hash is equivalent to the hash when all data is provided at once. |
54 // | 223 // |
55 // This verifies that decoder correctly keeps track of where it stopped | 224 // This verifies that decoder correctly keeps track of where it stopped |
56 // decoding when the image was not yet fully received. | 225 // decoding when the image was not yet fully received. |
57 void testProgressiveDecodingContinuesAfterFullData(const char* pngFile, | 226 void testProgressiveDecodingContinuesAfterFullData(const char* pngFile, |
58 size_t offsetMidFirstFrame) { | 227 size_t offsetMidFirstFrame) { |
59 auto fullData = readFile(pngFile); | 228 auto fullData = readFile(pngFile); |
60 ASSERT_FALSE(fullData->isEmpty()); | 229 ASSERT_FALSE(fullData->isEmpty()); |
61 | 230 |
62 auto decoderUpfront = createDecoder(); | 231 auto decoderUpfront = createDecoder(); |
63 decoderUpfront->setData(fullData.get(), true); | 232 decoderUpfront->setData(fullData.get(), true); |
64 EXPECT_GE(1u, decoderUpfront->frameCount()); | 233 EXPECT_GE(decoderUpfront->frameCount(), 1u); |
65 const ImageFrame* const frameUpfront = decoderUpfront->frameBufferAtIndex(0); | 234 const ImageFrame* const frameUpfront = decoderUpfront->frameBufferAtIndex(0); |
66 ASSERT_EQ(ImageFrame::FrameComplete, frameUpfront->getStatus()); | 235 ASSERT_EQ(ImageFrame::FrameComplete, frameUpfront->getStatus()); |
67 const unsigned hashUpfront = hashBitmap(frameUpfront->bitmap()); | 236 const unsigned hashUpfront = hashBitmap(frameUpfront->bitmap()); |
68 | 237 |
69 auto decoder = createDecoder(); | 238 auto decoder = createDecoder(); |
70 RefPtr<SharedBuffer> partialData = | 239 RefPtr<SharedBuffer> partialData = |
71 SharedBuffer::create(fullData->data(), offsetMidFirstFrame); | 240 SharedBuffer::create(fullData->data(), offsetMidFirstFrame); |
72 decoder->setData(partialData, false); | 241 decoder->setData(partialData, false); |
73 | 242 |
74 EXPECT_EQ(1u, decoder->frameCount()); | 243 EXPECT_EQ(1u, decoder->frameCount()); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 const ImageFrame* const frame = decoder->frameBufferAtIndex(frameIndex); | 287 const ImageFrame* const frame = decoder->frameBufferAtIndex(frameIndex); |
119 EXPECT_EQ(expectFailure, decoder->failed()); | 288 EXPECT_EQ(expectFailure, decoder->failed()); |
120 if (!expectFailure) { | 289 if (!expectFailure) { |
121 EXPECT_EQ(expectedFrameCountAfter, decoder->frameCount()); | 290 EXPECT_EQ(expectedFrameCountAfter, decoder->frameCount()); |
122 EXPECT_EQ(ImageFrame::FrameEmpty, frame->getStatus()); | 291 EXPECT_EQ(ImageFrame::FrameEmpty, frame->getStatus()); |
123 } | 292 } |
124 } | 293 } |
125 | 294 |
126 } // Anonymous namespace | 295 } // Anonymous namespace |
127 | 296 |
| 297 // Animated PNG Tests |
| 298 |
| 299 TEST(AnimatedPNGTests, sizeTest) { |
| 300 testSize( |
| 301 "/LayoutTests/images/resources/" |
| 302 "png-animated-idat-part-of-animation.png", |
| 303 IntSize(5, 5)); |
| 304 testSize( |
| 305 "/LayoutTests/images/resources/" |
| 306 "png-animated-idat-not-part-of-animation.png", |
| 307 IntSize(227, 35)); |
| 308 } |
| 309 |
| 310 TEST(AnimatedPNGTests, repetitionCountTest) { |
| 311 testRepetitionCount( |
| 312 "/LayoutTests/images/resources/" |
| 313 "png-animated-idat-part-of-animation.png", |
| 314 7u); |
| 315 // This is an "animated" image with only one frame, that is, the IDAT is |
| 316 // ignored and there is one fdAT frame. so it should be considered |
| 317 // non-animated. |
| 318 testRepetitionCount( |
| 319 "/LayoutTests/images/resources/" |
| 320 "png-animated-idat-not-part-of-animation.png", |
| 321 cAnimationNone); |
| 322 } |
| 323 |
| 324 // Test if the decoded metdata corresponds to the defined expectations |
| 325 TEST(AnimatedPNGTests, MetaDataTest) { |
| 326 const char* pngFile = |
| 327 "/LayoutTests/images/resources/" |
| 328 "png-animated-idat-part-of-animation.png"; |
| 329 constexpr size_t expectedFrameCount = 4; |
| 330 |
| 331 auto decoder = createDecoderWithPngData(pngFile); |
| 332 ASSERT_EQ(expectedFrameCount, decoder->frameCount()); |
| 333 for (size_t i = 0; i < expectedFrameCount; i++) { |
| 334 compareFrameWithExpectation(pngAnimatedFrameInfo[i], |
| 335 decoder->frameBufferAtIndex(i)); |
| 336 } |
| 337 } |
| 338 |
| 339 TEST(AnimatedPNGTests, ByteByByteSizeAvailable) { |
| 340 testSizeByteByByte( |
| 341 "/LayoutTests/images/resources/" |
| 342 "png-animated-idat-part-of-animation.png", |
| 343 141u, IntSize(5, 5)); |
| 344 testSizeByteByByte( |
| 345 "/LayoutTests/images/resources/" |
| 346 "png-animated-idat-not-part-of-animation.png", |
| 347 79u, IntSize(227, 35)); |
| 348 } |
| 349 |
| 350 // Test whether the frame metadata decoding also works when we provide the data |
| 351 // byte by byte. This should cover the case when the client does not provide |
| 352 // all data at once. At given offsets, we expect frames to become available. |
| 353 // This test checks whether that is the case, and if so, if the frame data is |
| 354 // equal to what we expected. |
| 355 TEST(AnimatedPNGTests, ByteByByteMetaData) { |
| 356 const char* pngFile = |
| 357 "/LayoutTests/images/resources/" |
| 358 "png-animated-idat-part-of-animation.png"; |
| 359 constexpr size_t expectedFrameCount = 4; |
| 360 |
| 361 // These are the byte offsets where each frame should have been parsed. |
| 362 // It boils down to the offset of the first fcTL / IEND after the last |
| 363 // frame data chunk, plus 8 bytes for recognition. The exception on this is |
| 364 // the first frame, which is reported when its first framedata is seen. |
| 365 size_t frameOffsets[expectedFrameCount] = {141, 249, 322, 430}; |
| 366 |
| 367 auto decoder = createDecoder(); |
| 368 auto data = readFile(pngFile); |
| 369 ASSERT_FALSE(data->isEmpty()); |
| 370 size_t framesParsed = 0; |
| 371 |
| 372 for (size_t length = 1; length <= frameOffsets[expectedFrameCount - 1]; |
| 373 length++) { |
| 374 RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), length); |
| 375 decoder->setData(tempData.get(), false); |
| 376 EXPECT_FALSE(decoder->failed()); |
| 377 if (length < frameOffsets[framesParsed]) { |
| 378 EXPECT_EQ(framesParsed, decoder->frameCount()); |
| 379 } else { |
| 380 ASSERT_EQ(framesParsed + 1, decoder->frameCount()); |
| 381 compareFrameWithExpectation(pngAnimatedFrameInfo[framesParsed], |
| 382 decoder->frameBufferAtIndex(framesParsed)); |
| 383 framesParsed++; |
| 384 } |
| 385 } |
| 386 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
| 387 EXPECT_FALSE(decoder->failed()); |
| 388 } |
| 389 |
| 390 TEST(AnimatedPNGTests, TestRandomFrameDecode) { |
| 391 testRandomFrameDecode(&createDecoder, |
| 392 "/LayoutTests/images/resources/" |
| 393 "png-animated-idat-part-of-animation.png", |
| 394 2u); |
| 395 } |
| 396 |
| 397 TEST(AnimatedPNGTests, TestDecodeAfterReallocation) { |
| 398 testDecodeAfterReallocatingData(&createDecoder, |
| 399 "/LayoutTests/images/resources/" |
| 400 "png-animated-idat-part-of-animation.png"); |
| 401 } |
| 402 |
| 403 TEST(AnimatedPNGTests, ProgressiveDecode) { |
| 404 testProgressiveDecoding(&createDecoder, |
| 405 "/LayoutTests/images/resources/" |
| 406 "png-animated-idat-part-of-animation.png", |
| 407 13u); |
| 408 } |
| 409 |
| 410 TEST(AnimatedPNGTests, ParseAndDecodeByteByByte) { |
| 411 testByteByByteDecode(&createDecoder, |
| 412 "/LayoutTests/images/resources/" |
| 413 "png-animated-idat-part-of-animation.png", |
| 414 4u, 7u); |
| 415 } |
| 416 |
| 417 TEST(AnimatedPNGTests, FailureDuringParsing) { |
| 418 const char* pngFile = |
| 419 "/LayoutTests/images/resources/" |
| 420 "png-animated-idat-part-of-animation.png"; |
| 421 auto data = readFile(pngFile); |
| 422 ASSERT_FALSE(data->isEmpty()); |
| 423 |
| 424 // Test the first fcTL in the stream. Because no frame data has been set at |
| 425 // this point, the expected frame count is zero. 95 bytes is just before the |
| 426 // first fcTL chunk, at which the first frame is detected. The decoder should |
| 427 // be in the failed state since no frames were parsed before this error. |
| 428 testInvalidFctlSize( |
| 429 "/LayoutTests/images/resources/" |
| 430 "png-animated-idat-part-of-animation.png", |
| 431 95u, 0u, true); |
| 432 |
| 433 // Test for the third fcTL in the stream. This should be tested as well, |
| 434 // since the first fcTL is parsed in PNGImageReader::parseSize() whereas |
| 435 // later fcTLs are parsed in PNGImageReader::parse() as part of framecount. |
| 436 // The expected frame count before the fcTL chunk is 1u, since the second |
| 437 // frame is registered when the third fcTL (or IEND) is seen. |
| 438 // |
| 439 // This should *not* set the decoder to the failed state since the first |
| 440 // frame was succesfully parsed, and we still want to show that frame. |
| 441 testInvalidFctlSize( |
| 442 "/LayoutTests/images/resources/" |
| 443 "png-animated-idat-part-of-animation.png", |
| 444 241u, 1u, false); |
| 445 } |
| 446 |
| 447 TEST(AnimatedPNGTests, FailureDuringParsingDueToMissingFctlData) { |
| 448 // The fcTL chunk starts at 95u, add 10u to get to the content data, and |
| 449 // remove 5 bytes of data. This sets the decoder to the failed state since |
| 450 // no frames were parsed yet. |
| 451 testMissingDataBreaksDecoding( |
| 452 "/LayoutTests/images/resources/" |
| 453 "png-animated-idat-part-of-animation.png", |
| 454 105u, 5u); |
| 455 } |
| 456 |
| 457 TEST(AnimatedPNGTests, MissingActlResultsInNonAnimated) { |
| 458 const char* pngFile = |
| 459 "/LayoutTests/images/resources/" |
| 460 "png-animated-idat-part-of-animation.png"; |
| 461 auto data = readFile(pngFile); |
| 462 auto decoder = createDecoder(); |
| 463 ASSERT_FALSE(data->isEmpty()); |
| 464 |
| 465 // Remove the acTL chunk from the stream. |
| 466 const size_t offsetActl = 33; |
| 467 RefPtr<SharedBuffer> noActlData = |
| 468 SharedBuffer::create(data->data(), offsetActl); |
| 469 noActlData->append(data->data() + offsetActl + 20, |
| 470 data->size() - offsetActl - 20); |
| 471 |
| 472 decoder->setData(noActlData, true); |
| 473 EXPECT_EQ(1u, decoder->frameCount()); |
| 474 EXPECT_FALSE(decoder->failed()); |
| 475 } |
| 476 |
| 477 // Test if the indicated frame count by the acTL is ignored if the actual |
| 478 // number of frames is different. Test for higher and lower indicated number. |
| 479 TEST(AnimatedPNGTests, differentActlFrameCountIsIgnored) { |
| 480 const char* pngFile = |
| 481 "/LayoutTests/images/resources/" |
| 482 "png-animated-idat-part-of-animation.png"; |
| 483 testDifferentActlFrameCountIsIgnored(pngFile, 33u, 2u, 4u); |
| 484 testDifferentActlFrameCountIsIgnored(pngFile, 33u, 8u, 4u); |
| 485 } |
| 486 |
| 487 // Originally, the third frame has an offset of (1,2) and a size of (3,2). By |
| 488 // changing the offset to (0,0) and the size to (4,4), a frame with not enough |
| 489 // data is simulated. The expected result is that the decoder will only receive |
| 490 // 1 row of data, since the data contains 6 pixels and each row is 4 pixels. |
| 491 // |
| 492 // When the decoder receives not enough data for a frame, the expected behavior |
| 493 // is that nothing changes: the decoder should not be invalidated, and the |
| 494 // frame should be considered successfully decoded. The reason for this is that |
| 495 // the error could either be an invalid frame size, or an invalid data chunk. |
| 496 // For now, the decoder does not take any action if not enough data is |
| 497 // provided, as described in PNGImageDecoder::complete(). |
| 498 TEST(AnimatedPNGTests, VerifyIncompleteFrameDataDoesNotInvalidateDecoder) { |
| 499 const char* pngFile = |
| 500 "/LayoutTests/images/resources/" |
| 501 "png-animated-idat-part-of-animation.png"; |
| 502 auto data = readFile(pngFile); |
| 503 auto decoder = createDecoder(); |
| 504 ASSERT_FALSE(data->isEmpty()); |
| 505 |
| 506 // Change the size and the offset of the frame, as described above. The size |
| 507 // is stored in byte 12-19 of the fcTL chunk, and the offset in byte 20-27. |
| 508 const size_t offsetThirdFctl = 241 + 12; |
| 509 RefPtr<SharedBuffer> modifiedData = |
| 510 SharedBuffer::create(data->data(), offsetThirdFctl); |
| 511 png_byte rectChunk[16]; |
| 512 writeUint32(4, rectChunk); |
| 513 writeUint32(4, rectChunk + 4); |
| 514 writeUint32(0, rectChunk + 8); |
| 515 writeUint32(0, rectChunk + 12); |
| 516 modifiedData->append( |
| 517 const_cast<const char*>(reinterpret_cast<char*>(rectChunk)), 16u); |
| 518 modifiedData->append(data->data() + offsetThirdFctl + 16, |
| 519 data->size() - offsetThirdFctl - 16); |
| 520 |
| 521 decoder->setData(modifiedData, true); |
| 522 |
| 523 // Verify that parsing the image results in the expected meta data. |
| 524 IntSize expectedSize(5, 5); |
| 525 const size_t expectedFrameCount = 4; |
| 526 IntRect expectedFrameRect(IntPoint(0, 0), IntSize(4, 4)); |
| 527 EXPECT_TRUE(decoder->isSizeAvailable()); |
| 528 EXPECT_EQ(expectedSize, decoder->size()); |
| 529 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
| 530 ASSERT_FALSE(decoder->failed()); |
| 531 |
| 532 // Try to decode frame 3. This should not fail the decoder, and the frame |
| 533 // should be FrameComplete. |
| 534 const ImageFrame* const buffer = decoder->frameBufferAtIndex(2); |
| 535 EXPECT_EQ(expectedFrameRect, buffer->originalFrameRect()); |
| 536 EXPECT_FALSE(decoder->failed()); |
| 537 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
| 538 EXPECT_EQ(ImageFrame::FrameComplete, buffer->getStatus()); |
| 539 } |
| 540 |
| 541 // Originally, the third frame has an offset of (1,2) and a size of (3,2). By |
| 542 // changing the offset to (4,4), the frame rect will extend the image size of |
| 543 // 5x5. Verify that the frame is correctly clipped to a size of (1,1) without |
| 544 // any decoding errors. Since this is the third frame, a decoding failure would |
| 545 // result in the frame count being adjusted to 2, and the decoder would not be |
| 546 // invalidated. Therefore, it is verified that after decoding frame 3 the |
| 547 // decoder is still in a valid state and the frame count is still 4. |
| 548 TEST(AnimatedPNGTests, VerifyFrameExtendsImageSizeClipsCorrectly) { |
| 549 const char* pngFile = |
| 550 "/LayoutTests/images/resources/" |
| 551 "png-animated-idat-part-of-animation.png"; |
| 552 auto data = readFile(pngFile); |
| 553 auto decoder = createDecoder(); |
| 554 ASSERT_FALSE(data->isEmpty()); |
| 555 |
| 556 // The offset of the frame rect is located 20 bytes into the fcTL chunk, after |
| 557 // the length, tag, sequence number, width and height fields (all 4B). |
| 558 const size_t offsetThirdFctl = 241 + 20; |
| 559 RefPtr<SharedBuffer> modifiedData = |
| 560 SharedBuffer::create(data->data(), offsetThirdFctl); |
| 561 png_byte offsetChunk[8]; |
| 562 writeUint32(4, offsetChunk); |
| 563 writeUint32(4, offsetChunk + 4); |
| 564 modifiedData->append( |
| 565 const_cast<const char*>(reinterpret_cast<char*>(offsetChunk)), 8u); |
| 566 modifiedData->append(data->data() + offsetThirdFctl + 8, |
| 567 data->size() - offsetThirdFctl - 8); |
| 568 |
| 569 decoder->setData(modifiedData, true); |
| 570 |
| 571 // Verify that parsing the image results in the expected meta data. |
| 572 IntSize expectedSize(5, 5); |
| 573 const size_t expectedFrameCount = 4; |
| 574 IntRect expectedFrameRect(IntPoint(4, 4), IntSize(1, 1)); |
| 575 EXPECT_TRUE(decoder->isSizeAvailable()); |
| 576 EXPECT_EQ(expectedSize, decoder->size()); |
| 577 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
| 578 ASSERT_FALSE(decoder->failed()); |
| 579 |
| 580 // Try to decode frame 3. This should not fail the decoder, and the frame |
| 581 // should be FrameComplete. |
| 582 const ImageFrame* const buffer = decoder->frameBufferAtIndex(2); |
| 583 EXPECT_EQ(expectedFrameRect, buffer->originalFrameRect()); |
| 584 EXPECT_FALSE(decoder->failed()); |
| 585 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
| 586 EXPECT_EQ(ImageFrame::FrameComplete, buffer->getStatus()); |
| 587 } |
| 588 |
| 589 TEST(AnimatedPNGTests, ProgressiveDecodingContinuesAfterFullData) { |
| 590 // 160u is a randomly chosen offset in the IDAT chunk of the first frame. |
| 591 testProgressiveDecodingContinuesAfterFullData( |
| 592 "/LayoutTests/images/resources/" |
| 593 "png-animated-idat-part-of-animation.png", |
| 594 160u); |
| 595 } |
| 596 |
| 597 TEST(AnimatedPNGTests, RandomDecodeAfterClearFrameBufferCache) { |
| 598 testRandomDecodeAfterClearFrameBufferCache( |
| 599 &createDecoder, |
| 600 "/LayoutTests/images/resources/" |
| 601 "png-animated-idat-part-of-animation.png", |
| 602 2u); |
| 603 } |
| 604 |
| 605 TEST(AnimatedPNGTests, VerifyAlphaBlending) { |
| 606 testAlphaBlending(&createDecoder, |
| 607 "/LayoutTests/images/resources/" |
| 608 "png-animated-idat-part-of-animation.png"); |
| 609 } |
| 610 |
| 611 // This tests if the frame count gets set correctly when parsing frameCount |
| 612 // fails in one of the parsing queries. |
| 613 // |
| 614 // First, enough data is provided such that two frames should be registered. |
| 615 // The decoder should at this point not be in the failed status. |
| 616 // |
| 617 // Then, we provide the rest of the data except for the last IEND chunk, but |
| 618 // tell the decoder that this is all the data we have. The frame count should |
| 619 // be three, since one extra frame should be discovered. The fourth frame |
| 620 // should *not* be registered since the reader should not be able to determine |
| 621 // where the frame ends. The decoder should *not* be in the failed state since |
| 622 // there are three frames which can be shown. |
| 623 TEST(AnimatedPNGTests, FailureMissingIendChunk) { |
| 624 RefPtr<SharedBuffer> fullData = readFile( |
| 625 "/LayoutTests/images/resources/" |
| 626 "png-animated-idat-part-of-animation.png"); |
| 627 ASSERT_FALSE(fullData->isEmpty()); |
| 628 auto decoder = createDecoder(); |
| 629 |
| 630 const size_t offsetTwoFrames = 249; |
| 631 const size_t expectedFramesAfter249Bytes = 2; |
| 632 RefPtr<SharedBuffer> tempData = |
| 633 SharedBuffer::create(fullData->data(), offsetTwoFrames); |
| 634 decoder->setData(tempData.get(), false); |
| 635 EXPECT_EQ(expectedFramesAfter249Bytes, decoder->frameCount()); |
| 636 EXPECT_FALSE(decoder->failed()); |
| 637 |
| 638 // Provide the rest of the data except for the last IEND chunk. |
| 639 const size_t expectedFramesAfterAllExpect12Bytes = 3; |
| 640 tempData = SharedBuffer::create(fullData->data(), fullData->size() - 12); |
| 641 decoder->setData(tempData.get(), true); |
| 642 EXPECT_EQ(expectedFramesAfterAllExpect12Bytes, decoder->frameCount()); |
| 643 EXPECT_FALSE(decoder->failed()); |
| 644 } |
| 645 |
| 646 TEST(AnimatedPNGTests, VerifyFrameCountChangesOnDecodingFailure) { |
| 647 testFailureDuringDecode( |
| 648 "/LayoutTests/images/resources/" |
| 649 "png-animated-idat-part-of-animation.png", |
| 650 279u, // idat offset for frame index 2 |
| 651 2u, // try to decode frame index 2 |
| 652 false, // expect the decoder to *not* be invalidated after the failure |
| 653 4u, // expected frame count before failure |
| 654 2u); // expected frame count after failure |
| 655 |
| 656 testFailureDuringDecode( |
| 657 "/LayoutTests/images/resources/" |
| 658 "png-animated-idat-part-of-animation.png", |
| 659 133u, // idat offset for frame index 0 |
| 660 0u, // try to decode frame index 0 |
| 661 true, // expect the decoder to be invalidated after the failure |
| 662 4u); // expected frame count before failure |
| 663 } |
| 664 |
| 665 // Verify that a malformatted PNG, where the IEND appears before any frame data |
| 666 // (IDAT), invalidates the decoder. |
| 667 TEST(AnimatedPNGTests, VerifyIENDBeforeIDATInvalidatesDecoder) { |
| 668 RefPtr<SharedBuffer> fullData = readFile( |
| 669 "/LayoutTests/images/resources/" |
| 670 "png-animated-idat-part-of-animation.png"); |
| 671 ASSERT_FALSE(fullData->isEmpty()); |
| 672 auto decoder = createDecoder(); |
| 673 |
| 674 const size_t offsetIDAT = 133; |
| 675 RefPtr<SharedBuffer> data = |
| 676 SharedBuffer::create(fullData->data(), offsetIDAT); |
| 677 data->append(fullData->data() + fullData->size() - 12u, 12u); |
| 678 data->append(fullData->data() + offsetIDAT, fullData->size() - offsetIDAT); |
| 679 decoder->setData(data.get(), true); |
| 680 |
| 681 const size_t expectedFrameCount = 0u; |
| 682 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
| 683 EXPECT_TRUE(decoder->failed()); |
| 684 } |
| 685 |
| 686 // For animated images, frameIsCompleteAtIndex(i) should return true if the |
| 687 // i-th frame is fully received. The frames don't need to be successfully |
| 688 // decoded. |
| 689 TEST(AnimatedPNGTests, VerifyFrameCompleteBehavior) { |
| 690 const char* pngFile = |
| 691 "/LayoutTests/images/resources/" |
| 692 "png-animated-idat-part-of-animation.png"; |
| 693 RefPtr<SharedBuffer> fullData = readFile(pngFile); |
| 694 ASSERT_FALSE(fullData->isEmpty()); |
| 695 auto decoder = createDecoder(); |
| 696 |
| 697 // When not all data for the first frame has been received, |
| 698 // frameIsCompleteAtIndex(0) should return false. |
| 699 const size_t offsetMidwayFirstFrame = 160; |
| 700 RefPtr<SharedBuffer> data = |
| 701 SharedBuffer::create(fullData->data(), offsetMidwayFirstFrame); |
| 702 decoder->setData(data.get(), false); |
| 703 EXPECT_EQ(1u, decoder->frameCount()); |
| 704 ASSERT_FALSE(decoder->failed()); |
| 705 EXPECT_FALSE(decoder->frameIsCompleteAtIndex(0)); |
| 706 |
| 707 // When all image data is received, every frame should be complete. |
| 708 const size_t expectedFrameCount = 4; |
| 709 decoder->setData(fullData.get(), true); |
| 710 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
| 711 for (size_t index = 0; index < expectedFrameCount; index++) |
| 712 EXPECT_TRUE(decoder->frameIsCompleteAtIndex(index)); |
| 713 } |
| 714 |
| 715 // Verify that erroneous values for the disposal method and alpha blending |
| 716 // are parsed into the expected default values, respectively DisposeNotSpecified |
| 717 // and BlendAtopBgcolor. |
| 718 TEST(AnimatedPNGTests, VerifyInvalidDisposalAndBlendingDefaultCorrectly) { |
| 719 const char* pngFile = |
| 720 "/LayoutTests/images/resources/" |
| 721 "png-animated-idat-part-of-animation.png"; |
| 722 RefPtr<SharedBuffer> fullData = readFile(pngFile); |
| 723 ASSERT_FALSE(fullData->isEmpty()); |
| 724 auto decoder = createDecoder(); |
| 725 |
| 726 // The disposal byte in the frame control chunk is the 24th byte, alpha |
| 727 // blending the 25th. |offsetDisposalOp| is 241 bytes to get to the third |
| 728 // fctl chunk, 8 bytes to skip the length and tag bytes, and 24 bytes to get |
| 729 // to the disposal op. |
| 730 // |
| 731 // Write random values to the disposal and alpha blending byte, and append |
| 732 // the rest of the buffer. |
| 733 const size_t offsetDisposalOp = 241 + 8 + 24; |
| 734 RefPtr<SharedBuffer> data = |
| 735 SharedBuffer::create(fullData->data(), offsetDisposalOp); |
| 736 png_byte disposalAndBlending[2]; |
| 737 disposalAndBlending[0] = 7; |
| 738 disposalAndBlending[1] = 9; |
| 739 data->append(reinterpret_cast<char*>(disposalAndBlending), 2u); |
| 740 data->append(fullData->data() + offsetDisposalOp + 2u, |
| 741 fullData->size() - offsetDisposalOp - 2u); |
| 742 |
| 743 decoder->setData(data.get(), true); |
| 744 decoder->frameCount(); |
| 745 ASSERT_FALSE(decoder->failed()); |
| 746 EXPECT_EQ(ImageFrame::DisposalMethod::DisposeNotSpecified, |
| 747 decoder->frameBufferAtIndex(2)->getDisposalMethod()); |
| 748 EXPECT_EQ(ImageFrame::AlphaBlendSource::BlendAtopBgcolor, |
| 749 decoder->frameBufferAtIndex(2)->getAlphaBlendSource()); |
| 750 } |
| 751 |
| 752 // This test verifies that the following situation does not invalidate the |
| 753 // decoder: |
| 754 // - Frame 0 is decoded progressively, but there's not enough data to fully |
| 755 // decode it. |
| 756 // - The rest of the image data is received. |
| 757 // - Frame X, with X > 0, and X does not depend on frame 0, is decoded. |
| 758 // - Frame 0 is decoded. |
| 759 // This is a tricky case since the decoder resets the png struct for each frame, |
| 760 // and this test verifies that it does not break the decoding of frame 0, even |
| 761 // though it already started in the first call. |
| 762 TEST(AnimatedPNGTests, VerifySuccessfulFirstFrameDecodeAfterLaterFrame) { |
| 763 const char* pngFile = |
| 764 "/LayoutTests/images/resources/" |
| 765 "png-animated-three-independent-frames.png"; |
| 766 auto decoder = createDecoder(); |
| 767 auto fullData = readFile(pngFile); |
| 768 ASSERT_FALSE(fullData->isEmpty()); |
| 769 |
| 770 // 160u is a randomly chosen offset in the IDAT chunk of the first frame. |
| 771 const size_t middleFirstFrame = 160u; |
| 772 RefPtr<SharedBuffer> data = |
| 773 SharedBuffer::create(fullData->data(), middleFirstFrame); |
| 774 decoder->setData(data.get(), false); |
| 775 |
| 776 ASSERT_EQ(1u, decoder->frameCount()); |
| 777 ImageFrame* frame0 = decoder->frameBufferAtIndex(0); |
| 778 ASSERT_EQ(ImageFrame::FramePartial, frame0->getStatus()); |
| 779 |
| 780 decoder->setData(fullData.get(), true); |
| 781 ASSERT_EQ(3u, decoder->frameCount()); |
| 782 ASSERT_EQ(ImageFrame::FrameComplete, |
| 783 decoder->frameBufferAtIndex(1)->getStatus()); |
| 784 ASSERT_EQ(ImageFrame::FramePartial, frame0->getStatus()); |
| 785 |
| 786 EXPECT_EQ(ImageFrame::FrameComplete, |
| 787 decoder->frameBufferAtIndex(0)->getStatus()); |
| 788 EXPECT_FALSE(decoder->failed()); |
| 789 } |
| 790 |
128 // Static PNG tests | 791 // Static PNG tests |
129 | 792 |
130 TEST(StaticPNGTests, repetitionCountTest) { | 793 TEST(StaticPNGTests, repetitionCountTest) { |
131 testRepetitionCount("/LayoutTests/images/resources/png-simple.png", | 794 testRepetitionCount("/LayoutTests/images/resources/png-simple.png", |
132 cAnimationNone); | 795 cAnimationNone); |
133 } | 796 } |
134 | 797 |
135 TEST(StaticPNGTests, sizeTest) { | 798 TEST(StaticPNGTests, sizeTest) { |
136 testSize("/LayoutTests/images/resources/png-simple.png", IntSize(111, 29)); | 799 testSize("/LayoutTests/images/resources/png-simple.png", IntSize(111, 29)); |
137 } | 800 } |
138 | 801 |
139 TEST(StaticPNGTests, MetaDataTest) { | 802 TEST(StaticPNGTests, MetaDataTest) { |
140 const size_t expectedFrameCount = 1; | 803 const size_t expectedFrameCount = 1; |
141 const size_t expectedDuration = 0; | 804 const size_t expectedDuration = 0; |
142 auto decoder = | 805 auto decoder = |
143 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png"); | 806 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png"); |
144 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); | 807 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
145 EXPECT_EQ(expectedDuration, decoder->frameDurationAtIndex(0)); | 808 EXPECT_EQ(expectedDuration, decoder->frameDurationAtIndex(0)); |
146 } | 809 } |
147 | 810 |
| 811 TEST(StaticPNGTests, InvalidIHDRChunk) { |
| 812 testMissingDataBreaksDecoding("/LayoutTests/images/resources/png-simple.png", |
| 813 20u, 2u); |
| 814 } |
| 815 |
148 TEST(StaticPNGTests, ProgressiveDecoding) { | 816 TEST(StaticPNGTests, ProgressiveDecoding) { |
149 testProgressiveDecoding(&createDecoder, | 817 testProgressiveDecoding(&createDecoder, |
150 "/LayoutTests/images/resources/png-simple.png", 11u); | 818 "/LayoutTests/images/resources/png-simple.png", 11u); |
151 } | 819 } |
152 | 820 |
153 TEST(StaticPNGTests, ProgressiveDecodingContinuesAfterFullData) { | 821 TEST(StaticPNGTests, ProgressiveDecodingContinuesAfterFullData) { |
154 testProgressiveDecodingContinuesAfterFullData( | 822 testProgressiveDecodingContinuesAfterFullData( |
155 "/LayoutTests/images/resources/png-simple.png", 1000u); | 823 "/LayoutTests/images/resources/png-simple.png", 1000u); |
156 } | 824 } |
157 | 825 |
(...skipping 12 matching lines...) Expand all Loading... |
170 auto decoder = | 838 auto decoder = |
171 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png"); | 839 createDecoderWithPngData("/LayoutTests/images/resources/png-simple.png"); |
172 EXPECT_EQ(1u, decoder->frameCount()); | 840 EXPECT_EQ(1u, decoder->frameCount()); |
173 EXPECT_FALSE(decoder->frameIsCompleteAtIndex(0)); | 841 EXPECT_FALSE(decoder->frameIsCompleteAtIndex(0)); |
174 EXPECT_EQ(ImageFrame::FrameComplete, | 842 EXPECT_EQ(ImageFrame::FrameComplete, |
175 decoder->frameBufferAtIndex(0)->getStatus()); | 843 decoder->frameBufferAtIndex(0)->getStatus()); |
176 EXPECT_TRUE(decoder->frameIsCompleteAtIndex(0)); | 844 EXPECT_TRUE(decoder->frameIsCompleteAtIndex(0)); |
177 } | 845 } |
178 | 846 |
179 }; // namespace blink | 847 }; // namespace blink |
OLD | NEW |