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