OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "platform/image-decoders/png/PNGImageDecoder.h" |
| 6 |
| 7 #include "png.h" |
| 8 #include "platform/image-decoders/ImageDecoderTestHelpers.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include <memory> |
| 11 |
| 12 // @TODO(joostouwerling) test decoding failure on frame |n|, and check if the |
| 13 // decoder reports having |n-1| frames. |
| 14 // @TODO(joostouwerling) verify IEND before IDAT fails the decode. |
| 15 // @TODO(joostouwerling) extend test image set with other image encodings, such |
| 16 // as first frame fcTL and multiple chunks per frame. |
| 17 |
| 18 namespace blink { |
| 19 |
| 20 namespace { |
| 21 |
| 22 std::unique_ptr<ImageDecoder> createDecoder(ImageDecoder::AlphaOption alphaOptio
n) |
| 23 { |
| 24 return wrapUnique(new PNGImageDecoder(alphaOption, |
| 25 ImageDecoder::ColorSpaceApplied, |
| 26 ImageDecoder::noDecodedImageByteLimit)); |
| 27 } |
| 28 |
| 29 std::unique_ptr<ImageDecoder> createDecoder() |
| 30 { |
| 31 return createDecoder(ImageDecoder::AlphaNotPremultiplied); |
| 32 } |
| 33 |
| 34 std::unique_ptr<ImageDecoder> createDecoderWithPngData(const char* pngFile) |
| 35 { |
| 36 auto decoder = createDecoder(); |
| 37 auto data = readFile(pngFile); |
| 38 EXPECT_FALSE(data->isEmpty()); |
| 39 decoder->setData(data.get(), true); |
| 40 return decoder; |
| 41 } |
| 42 |
| 43 void testSize(const char* pngFile, IntSize expectedSize) |
| 44 { |
| 45 auto decoder = createDecoderWithPngData(pngFile); |
| 46 EXPECT_TRUE(decoder->isSizeAvailable()); |
| 47 EXPECT_EQ(expectedSize, decoder->size()); |
| 48 } |
| 49 |
| 50 void writeUint32(uint32_t val, png_byte* data) |
| 51 { |
| 52 data[0] = val >> 24; |
| 53 data[1] = val >> 16; |
| 54 data[2] = val >> 8; |
| 55 data[3] = val; |
| 56 } |
| 57 |
| 58 void testRepetitionCount(const char* pngFile, int expectedRepetitionCount) |
| 59 { |
| 60 auto decoder = createDecoderWithPngData(pngFile); |
| 61 // Decode frame count should see the number of repetitions as well. |
| 62 decoder->frameCount(); |
| 63 EXPECT_FALSE(decoder->failed()); |
| 64 EXPECT_EQ(expectedRepetitionCount, decoder->repetitionCount()); |
| 65 } |
| 66 |
| 67 // Test whether querying for the size of the image works if we present the |
| 68 // data byte by byte. |
| 69 void testSizeByteByByte(const char *pngFile, size_t bytesNeededToDecodeSize, |
| 70 IntSize expectedSize) |
| 71 { |
| 72 auto decoder = createDecoder(); |
| 73 auto data = readFile(pngFile); |
| 74 ASSERT_FALSE(data->isEmpty()); |
| 75 ASSERT_LT(bytesNeededToDecodeSize, data->size()); |
| 76 |
| 77 for (size_t length = 1; length <= bytesNeededToDecodeSize; length++) { |
| 78 RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), |
| 79 length); |
| 80 decoder->setData(tempData.get(), false); |
| 81 |
| 82 if (length < bytesNeededToDecodeSize) { |
| 83 EXPECT_FALSE(decoder->isSizeAvailable()); |
| 84 EXPECT_TRUE(decoder->size().isEmpty()); |
| 85 EXPECT_FALSE(decoder->failed()); |
| 86 } else { |
| 87 EXPECT_TRUE(decoder->isSizeAvailable()); |
| 88 EXPECT_EQ(expectedSize, decoder->size()); |
| 89 } |
| 90 } |
| 91 EXPECT_FALSE(decoder->failed()); |
| 92 } |
| 93 |
| 94 struct PublicFrameInfo { |
| 95 size_t duration; |
| 96 IntRect frameRect; |
| 97 ImageFrame::AlphaBlendSource alphaBlend; |
| 98 ImageFrame::DisposalMethod disposalMethod; |
| 99 }; |
| 100 |
| 101 // This is the frame data for the following PNG image: |
| 102 // /LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png |
| 103 static PublicFrameInfo pngAnimatedFrameInfo[] = { |
| 104 {500, {IntPoint(0, 0), IntSize(5, 5)}, ImageFrame::BlendAtopBgcolor, |
| 105 ImageFrame::DisposeKeep}, |
| 106 {900, {IntPoint(1, 1), IntSize(3, 1)}, ImageFrame::BlendAtopBgcolor, |
| 107 ImageFrame::DisposeOverwriteBgcolor}, |
| 108 {2000, {IntPoint(1, 2), IntSize(3, 2)}, ImageFrame::BlendAtopPreviousFrame, |
| 109 ImageFrame::DisposeKeep}, |
| 110 {1500, {IntPoint(1, 2), IntSize(3, 1)}, ImageFrame::BlendAtopBgcolor, |
| 111 ImageFrame::DisposeKeep}, |
| 112 }; |
| 113 |
| 114 void compareFrameWithExpectation(const PublicFrameInfo& expected, |
| 115 const ImageFrame* frame) |
| 116 { |
| 117 EXPECT_EQ(expected.duration, frame->duration()); |
| 118 EXPECT_EQ(expected.frameRect, frame->originalFrameRect()); |
| 119 EXPECT_EQ(expected.disposalMethod, frame->getDisposalMethod()); |
| 120 EXPECT_EQ(expected.alphaBlend, frame->getAlphaBlendSource()); |
| 121 } |
| 122 |
| 123 // This function removes |length| bytes at |offset|, and then calls frameCount. |
| 124 // It assumes the missing bytes should result in a failed decode. |
| 125 void testMissingDataBreaksDecoding(const char* pngFile, size_t offset, |
| 126 size_t length) |
| 127 { |
| 128 auto decoder = createDecoder(); |
| 129 auto data = readFile(pngFile); |
| 130 ASSERT_FALSE(data->isEmpty()); |
| 131 |
| 132 RefPtr<SharedBuffer> invalidData = SharedBuffer::create(data->data(), |
| 133 offset); |
| 134 invalidData->append(SharedBuffer::create(data->data() + offset + length, |
| 135 data->size() - offset - length)); |
| 136 ASSERT_EQ(data->size() - length, invalidData->size()); |
| 137 |
| 138 decoder->setData(invalidData, true); |
| 139 decoder->frameCount(); |
| 140 EXPECT_TRUE(decoder->failed()); |
| 141 } |
| 142 |
| 143 // Decoding up to the indicated fcTL offset and then provide an fcTL with |
| 144 // the wrong chunk size (20 instead of 26). It should break the decoder. |
| 145 void testInvalidFctlSize(const char* pngFile, size_t offsetFctl, |
| 146 size_t expectedFrameCountBeforeFail) |
| 147 { |
| 148 auto data = readFile(pngFile); |
| 149 ASSERT_FALSE(data->isEmpty()); |
| 150 |
| 151 auto decoder = createDecoder(); |
| 152 RefPtr<SharedBuffer> invalidData = SharedBuffer::create(data->data(), |
| 153 offsetFctl); |
| 154 |
| 155 // Test if this gives the correct frame count, before the fcTL is parsed. |
| 156 decoder->setData(invalidData, false); |
| 157 EXPECT_EQ(expectedFrameCountBeforeFail, decoder->frameCount()); |
| 158 ASSERT_FALSE(decoder->failed()); |
| 159 |
| 160 // Append the wrong size to the data stream |
| 161 png_byte sizeChunk[4]; |
| 162 writeUint32(20, sizeChunk); |
| 163 invalidData->append(reinterpret_cast<char*>(sizeChunk), 4u); |
| 164 |
| 165 // Skip the size in the original data, but provide the rest of the fcTL, |
| 166 // which is 4B of tag, 26B of data and 4B of CRC, totalling 34B. |
| 167 invalidData->append(data->data() + offsetFctl + 4, 34u); |
| 168 |
| 169 decoder->setData(invalidData, false); |
| 170 decoder->frameCount(); |
| 171 EXPECT_TRUE(decoder->failed()); |
| 172 } |
| 173 |
| 174 void testDifferentActlFrameCountIsIgnored(const char* pngFile, |
| 175 size_t offsetActl, |
| 176 size_t injectedFrameCount, |
| 177 size_t expectedFrameCount) |
| 178 { |
| 179 // First make sure that this tests makes sense. |
| 180 ASSERT_NE(injectedFrameCount, expectedFrameCount); |
| 181 |
| 182 auto data = readFile(pngFile); |
| 183 auto decoder = createDecoder(); |
| 184 ASSERT_FALSE(data->isEmpty()); |
| 185 |
| 186 RefPtr<SharedBuffer> diffActlData = SharedBuffer::create(data->data(), |
| 187 offsetActl + 8); |
| 188 // Write the injectedFrameCount to the stream |
| 189 png_byte sizeChunk[4]; |
| 190 writeUint32(injectedFrameCount, sizeChunk); |
| 191 diffActlData->append(reinterpret_cast<char*>(sizeChunk), 4u); |
| 192 // Append the rest of the data. The first |offsetActl + 12| bytes that are |
| 193 // already in diffActlData should not be appended again. |
| 194 diffActlData->append(data->data() + offsetActl + 12, |
| 195 data->size() - offsetActl - 12); |
| 196 |
| 197 decoder->setData(diffActlData, true); |
| 198 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
| 199 } |
| 200 |
| 201 // Test if the frame bitmap hashes of truncated decoding are equal to the |
| 202 // hashes found by incremental decoding. |
| 203 void testProgressiveDecoding(const char *pngFile) |
| 204 { |
| 205 RefPtr<SharedBuffer> fullData = readFile(pngFile); |
| 206 ASSERT_TRUE(fullData.get()); |
| 207 const size_t fullLength = fullData->size(); |
| 208 |
| 209 std::unique_ptr<ImageDecoder> decoder; |
| 210 ImageFrame* frame; |
| 211 |
| 212 Vector<unsigned> truncatedHashes; |
| 213 Vector<unsigned> progressiveHashes; |
| 214 |
| 215 // Compute hashes when the file is truncated. |
| 216 const size_t increment = 13; |
| 217 for (size_t i = 1; i <= fullLength; i += increment) { |
| 218 decoder = createDecoder(); |
| 219 RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), i); |
| 220 decoder->setData(data.get(), i == fullLength); |
| 221 size_t frameCount = decoder->frameCount(); |
| 222 ASSERT_FALSE(decoder->failed()); |
| 223 if (frameCount == 0) { |
| 224 truncatedHashes.append(0); |
| 225 continue; |
| 226 } |
| 227 frame = decoder->frameBufferAtIndex(frameCount - 1); |
| 228 if (!frame) { |
| 229 truncatedHashes.append(0); |
| 230 continue; |
| 231 } |
| 232 truncatedHashes.append(hashBitmap(frame->bitmap())); |
| 233 } |
| 234 |
| 235 // Compute hashes when the file is progressively decoded. |
| 236 decoder = createDecoder(); |
| 237 for (size_t i = 1; i <= fullLength; i += increment) { |
| 238 RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), i); |
| 239 decoder->setData(data.get(), i == fullLength); |
| 240 ASSERT_FALSE(decoder->failed()); |
| 241 size_t frameCount = decoder->frameCount(); |
| 242 if (frameCount == 0) { |
| 243 progressiveHashes.append(0); |
| 244 continue; |
| 245 } |
| 246 frame = decoder->frameBufferAtIndex(frameCount - 1); |
| 247 if (!frame) { |
| 248 progressiveHashes.append(0); |
| 249 continue; |
| 250 } |
| 251 progressiveHashes.append(hashBitmap(frame->bitmap())); |
| 252 } |
| 253 |
| 254 for (size_t i = 0; i < truncatedHashes.size(); ++i) |
| 255 ASSERT_EQ(truncatedHashes[i], progressiveHashes[i]); |
| 256 } |
| 257 |
| 258 // Check that providing the full data after the first frame was partially |
| 259 // decoded properly continues where it left off. |
| 260 void testProgressiveDecodingContinuesAfterFullData(const char* pngFile, |
| 261 size_t offsetMidFirstFrame) |
| 262 { |
| 263 auto fullData = readFile(pngFile); |
| 264 auto decoder = createDecoder(); |
| 265 ASSERT_FALSE(fullData->isEmpty()); |
| 266 |
| 267 RefPtr<SharedBuffer> partialData = |
| 268 SharedBuffer::create(fullData->data(), offsetMidFirstFrame); |
| 269 decoder->setData(partialData, false); |
| 270 |
| 271 EXPECT_EQ(1u, decoder->frameCount()); |
| 272 ImageFrame* frame = decoder->frameBufferAtIndex(0); |
| 273 EXPECT_EQ(frame->getStatus(), ImageFrame::FramePartial); |
| 274 unsigned hashPartial = hashBitmap(frame->bitmap()); |
| 275 |
| 276 decoder->setData(fullData.get(), true); |
| 277 frame = decoder->frameBufferAtIndex(0); |
| 278 EXPECT_EQ(frame->getStatus(), ImageFrame::FrameComplete); |
| 279 unsigned hashFull = hashBitmap(frame->bitmap()); |
| 280 |
| 281 EXPECT_FALSE(decoder->failed()); |
| 282 EXPECT_NE(hashFull, hashPartial); |
| 283 } |
| 284 |
| 285 // This test verifies that the frame buffer contents change when progressively |
| 286 // decoding the first frame. It should change more than 1 time: once for the |
| 287 // first data, and at least once more thereafter. If |offsetFirstFrameEnd| == 0, |
| 288 // the test uses the full data size of the image for its value. |
| 289 void testProgressiveDecodingChangesFrameBuffer(const char* pngFile, |
| 290 size_t offsetFirstFrameEnd, |
| 291 size_t step = 1u) |
| 292 { |
| 293 auto fullData = readFile(pngFile); |
| 294 auto decoder = createDecoder(); |
| 295 ASSERT_FALSE(fullData->isEmpty()); |
| 296 |
| 297 if (offsetFirstFrameEnd == 0) |
| 298 offsetFirstFrameEnd = fullData->size(); |
| 299 |
| 300 size_t numTimesBufferChanged = 0; |
| 301 unsigned lastHash; |
| 302 |
| 303 for (size_t length = 1; length <= offsetFirstFrameEnd; length += step) { |
| 304 RefPtr<SharedBuffer> data = SharedBuffer::create(fullData->data(), |
| 305 length); |
| 306 decoder->setData(data, false); |
| 307 ImageFrame* frame = decoder->frameBufferAtIndex(0); |
| 308 if (!frame) |
| 309 continue; |
| 310 unsigned newHash = hashBitmap(frame->bitmap()); |
| 311 if (newHash != lastHash) { |
| 312 lastHash = newHash; |
| 313 numTimesBufferChanged++; |
| 314 } |
| 315 } |
| 316 EXPECT_GT(numTimesBufferChanged, 1u); |
| 317 } |
| 318 |
| 319 // @TODO(joostouwerling) Pull this up to ImageDecoderTestHelper since it is also |
| 320 // used in WEBPImageDecoderTest |
| 321 void testRandomDecodeAfterClearFrameBufferCache(const char* pngFile) { |
| 322 SCOPED_TRACE(pngFile); |
| 323 |
| 324 RefPtr<SharedBuffer> data = readFile(pngFile); |
| 325 ASSERT_TRUE(data.get()); |
| 326 Vector<unsigned> baselineHashes; |
| 327 createDecodingBaseline(&createDecoder, data.get(), &baselineHashes); |
| 328 size_t frameCount = baselineHashes.size(); |
| 329 |
| 330 std::unique_ptr<ImageDecoder> decoder = createDecoder(); |
| 331 decoder->setData(data.get(), true); |
| 332 for (size_t clearExceptFrame = 0; clearExceptFrame < frameCount; |
| 333 ++clearExceptFrame) { |
| 334 decoder->clearCacheExceptFrame(clearExceptFrame); |
| 335 const size_t skippingStep = 2; |
| 336 for (size_t i = 0; i < skippingStep; ++i) { |
| 337 for (size_t j = 0; j < frameCount; j += skippingStep) { |
| 338 SCOPED_TRACE(testing::Message() << "Random i:" << i << " j:" << j); |
| 339 ImageFrame* frame = decoder->frameBufferAtIndex(j); |
| 340 EXPECT_EQ(baselineHashes[j], hashBitmap(frame->bitmap())); |
| 341 } |
| 342 } |
| 343 } |
| 344 } |
| 345 |
| 346 uint32_t premultiplyColor(uint32_t c) { |
| 347 return SkPremultiplyARGBInline(SkGetPackedA32(c), SkGetPackedR32(c), |
| 348 SkGetPackedG32(c), SkGetPackedB32(c)); |
| 349 } |
| 350 |
| 351 void verifyFramesMatch(const char* pngFile, |
| 352 const ImageFrame* const a, |
| 353 ImageFrame* const b) { |
| 354 const SkBitmap& bitmapA = a->bitmap(); |
| 355 const SkBitmap& bitmapB = b->bitmap(); |
| 356 ASSERT_EQ(bitmapA.width(), bitmapB.width()); |
| 357 ASSERT_EQ(bitmapA.height(), bitmapB.height()); |
| 358 |
| 359 int maxDifference = 0; |
| 360 for (int y = 0; y < bitmapA.height(); ++y) { |
| 361 for (int x = 0; x < bitmapA.width(); ++x) { |
| 362 uint32_t colorA = *bitmapA.getAddr32(x, y); |
| 363 if (!a->premultiplyAlpha()) |
| 364 colorA = premultiplyColor(colorA); |
| 365 uint32_t colorB = *bitmapB.getAddr32(x, y); |
| 366 if (!b->premultiplyAlpha()) |
| 367 colorB = premultiplyColor(colorB); |
| 368 uint8_t* pixelA = reinterpret_cast<uint8_t*>(&colorA); |
| 369 uint8_t* pixelB = reinterpret_cast<uint8_t*>(&colorB); |
| 370 for (int channel = 0; channel < 4; ++channel) { |
| 371 const int difference = abs(pixelA[channel] - pixelB[channel]); |
| 372 if (difference > maxDifference) |
| 373 maxDifference = difference; |
| 374 } |
| 375 } |
| 376 } |
| 377 |
| 378 // Pre-multiplication could round the RGBA channel values. So, we declare |
| 379 // that the frames match if the RGBA channel values differ by at most 2. |
| 380 EXPECT_GE(2, maxDifference) << pngFile; |
| 381 } |
| 382 |
| 383 // Verifies that result of alpha blending is similar for AlphaPremultiplied and |
| 384 // AlphaNotPremultiplied cases. |
| 385 void testAlphaBlending(const char* pngFile) { |
| 386 RefPtr<SharedBuffer> data = readFile(pngFile); |
| 387 ASSERT_TRUE(data.get()); |
| 388 |
| 389 std::unique_ptr<ImageDecoder> decoderA = |
| 390 createDecoder(ImageDecoder::AlphaPremultiplied); |
| 391 decoderA->setData(data.get(), true); |
| 392 |
| 393 std::unique_ptr<ImageDecoder> decoderB = |
| 394 createDecoder(ImageDecoder::AlphaNotPremultiplied); |
| 395 decoderB->setData(data.get(), true); |
| 396 |
| 397 size_t frameCount = decoderA->frameCount(); |
| 398 ASSERT_EQ(frameCount, decoderB->frameCount()); |
| 399 |
| 400 for (size_t i = 0; i < frameCount; ++i) |
| 401 verifyFramesMatch(pngFile, decoderA->frameBufferAtIndex(i), |
| 402 decoderB->frameBufferAtIndex(i)); |
| 403 } |
| 404 |
| 405 } // Anonymous namespace |
| 406 |
| 407 // Animated PNG Tests |
| 408 |
| 409 TEST(AnimatedPNGTests, sizeTest) |
| 410 { |
| 411 testSize("/LayoutTests/fast/images/resources/png-animated-idat-part-of-animati
on.png", IntSize(5, 5)); |
| 412 testSize("/LayoutTests/fast/images/resources/png-animated-idat-not-part-of-ani
mation.png", IntSize(227, 35)); |
| 413 } |
| 414 |
| 415 TEST(AnimatedPNGTests, repetitionCountTest) |
| 416 { |
| 417 testRepetitionCount("/LayoutTests/fast/images/resources/png-animated-idat-part
-of-animation.png", 7u); |
| 418 // This is an "animated" image with only one frame, that is, the IDAT is |
| 419 // ignored and there is one fdAT frame. so it should be considered |
| 420 // non-animated. |
| 421 testRepetitionCount("/LayoutTests/fast/images/resources/png-animated-idat-not-
part-of-animation.png", cAnimationNone); |
| 422 } |
| 423 |
| 424 // Test if the decoded metdata corresponds to the defined expectations |
| 425 TEST(AnimatedPNGTests, MetaDataTest) |
| 426 { |
| 427 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa
rt-of-animation.png"; |
| 428 constexpr size_t expectedFrameCount = 4; |
| 429 |
| 430 auto decoder = createDecoderWithPngData(pngFile); |
| 431 ASSERT_EQ(expectedFrameCount, decoder->frameCount()); |
| 432 for (size_t i = 0; i < expectedFrameCount; i++) |
| 433 compareFrameWithExpectation(pngAnimatedFrameInfo[i], |
| 434 decoder->frameBufferAtIndex(i)); |
| 435 } |
| 436 |
| 437 TEST(AnimatedPNGTests, ByteByByteSizeAvailable) |
| 438 { |
| 439 testSizeByteByByte("/LayoutTests/fast/images/resources/png-animated-idat-part-
of-animation.png", |
| 440 141u, IntSize(5, 5)); |
| 441 testSizeByteByByte("/LayoutTests/fast/images/resources/png-animated-idat-not-p
art-of-animation.png", |
| 442 79u, IntSize(227, 35)); |
| 443 } |
| 444 |
| 445 // Test whether the frame metadata decoding also works when we provide the data |
| 446 // byte by byte. This should cover the case when the client does not provide |
| 447 // all data at once. At given offsets, we expect frames to become available. |
| 448 // This test checks whether that is the case, and if so, if the frame data is |
| 449 // equal to what we expected. |
| 450 TEST(AnimatedPNGTests, ByteByByteMetaData) |
| 451 { |
| 452 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa
rt-of-animation.png"; |
| 453 constexpr size_t expectedFrameCount = 4; |
| 454 |
| 455 // These are the byte offsets where each frame should have been parsed. |
| 456 // It boils down to the offset of the first fcTL / IEND after the last |
| 457 // frame data chunk, plus 8 bytes for recognition. The exception on this is |
| 458 // the first frame, which is reported when its first framedata is seen. |
| 459 size_t frameOffsets[expectedFrameCount] = {141, 249, 322, 430}; |
| 460 |
| 461 auto decoder = createDecoder(); |
| 462 auto data = readFile(pngFile); |
| 463 ASSERT_FALSE(data->isEmpty()); |
| 464 size_t framesParsed = 0; |
| 465 |
| 466 for (size_t length = 1; length <= frameOffsets[expectedFrameCount - 1]; length
++) { |
| 467 RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), |
| 468 length); |
| 469 decoder->setData(tempData.get(), false); |
| 470 EXPECT_FALSE(decoder->failed()); |
| 471 if (length < frameOffsets[framesParsed]) { |
| 472 EXPECT_EQ(framesParsed, decoder->frameCount()); |
| 473 } else { |
| 474 ASSERT_EQ(framesParsed + 1, decoder->frameCount()); |
| 475 compareFrameWithExpectation(pngAnimatedFrameInfo[framesParsed], |
| 476 decoder->frameBufferAtIndex(framesParsed)); |
| 477 framesParsed++; |
| 478 } |
| 479 } |
| 480 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
| 481 EXPECT_FALSE(decoder->failed()); |
| 482 } |
| 483 |
| 484 // This tests if the frame count gets set correctly when parsing frameCount |
| 485 // fails in one of the parsing queries. |
| 486 // |
| 487 // First, enough data is provided such that two frames should be registered. |
| 488 // The decoder should at this point not be in the failed status. |
| 489 // |
| 490 // Then, we provide the rest of the data except for the last IEND chunk, but |
| 491 // tell the decoder that this is all the data we have. Now, the decoder should |
| 492 // be in the failed state since all data is provided but no IEND chunk has been |
| 493 // seen. The frame count should be three, since one extra frame should be |
| 494 // discovered. The fourth frame should *not* be registered since the reader |
| 495 // should not be able to determine where the frame ends. |
| 496 TEST(AnimatedPNGTests, FrameCountWithTruncatedData) |
| 497 { |
| 498 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa
rt-of-animation.png"; |
| 499 auto decoder = createDecoder(); |
| 500 auto data = readFile(pngFile); |
| 501 ASSERT_FALSE(data->isEmpty()); |
| 502 |
| 503 // Parse up to and including the first two frames |
| 504 const size_t offsetTwoFrames = 249; |
| 505 const size_t expectedFramesAfter249Bytes = 2; |
| 506 RefPtr<SharedBuffer> tempData = SharedBuffer::create(data->data(), |
| 507 offsetTwoFrames); |
| 508 decoder->setData(tempData.get(), false); |
| 509 EXPECT_EQ(expectedFramesAfter249Bytes, decoder->frameCount()); |
| 510 EXPECT_FALSE(decoder->failed()); |
| 511 |
| 512 // Provide the rest of the data except for the last IEND chunk. |
| 513 const size_t expectedFramesAfterAllExpect12Bytes = 3; |
| 514 tempData = SharedBuffer::create(data->data(), data->size() - 12); |
| 515 decoder->setData(tempData.get(), true); |
| 516 EXPECT_EQ(expectedFramesAfterAllExpect12Bytes, decoder->frameCount()); |
| 517 EXPECT_TRUE(decoder->failed()); |
| 518 } |
| 519 |
| 520 TEST(AnimatedPNGTests, TestRandomFrameDecode) |
| 521 { |
| 522 RefPtr<SharedBuffer> fullData = readFile("/LayoutTests/fast/images/resources/p
ng-animated-idat-part-of-animation.png"); |
| 523 ASSERT_TRUE(fullData.get()); |
| 524 Vector<unsigned> baselineHashes; |
| 525 createDecodingBaseline(&createDecoder, fullData.get(), &baselineHashes); |
| 526 size_t frameCount = baselineHashes.size(); |
| 527 |
| 528 // Random decoding should get the same results as sequential decoding. |
| 529 std::unique_ptr<ImageDecoder> decoder = createDecoder(); |
| 530 decoder->setData(fullData.get(), true); |
| 531 const size_t skippingStep = 2; |
| 532 for (size_t i = 0; i < skippingStep; ++i) { |
| 533 for (size_t j = i; j < frameCount; j += skippingStep) { |
| 534 SCOPED_TRACE(testing::Message() << "Random i:" << i << " j:" << j); |
| 535 ImageFrame* frame = decoder->frameBufferAtIndex(j); |
| 536 EXPECT_EQ(baselineHashes[j], hashBitmap(frame->bitmap())); |
| 537 } |
| 538 } |
| 539 |
| 540 // Decoding in reverse order. |
| 541 decoder = createDecoder(); |
| 542 decoder->setData(fullData.get(), true); |
| 543 for (size_t i = frameCount; i; --i) { |
| 544 SCOPED_TRACE(testing::Message() << "Reverse i:" << i); |
| 545 ImageFrame* frame = decoder->frameBufferAtIndex(i - 1); |
| 546 EXPECT_EQ(baselineHashes[i - 1], hashBitmap(frame->bitmap())); |
| 547 } |
| 548 |
| 549 } |
| 550 |
| 551 TEST(AnimatedPNGTests, TestDecodeAfterReallocation) |
| 552 { |
| 553 std::unique_ptr<ImageDecoder> decoder = createDecoder(); |
| 554 RefPtr<SharedBuffer> data = readFile("/LayoutTests/fast/images/resources/png-a
nimated-idat-part-of-animation.png"); |
| 555 ASSERT_TRUE(data.get()); |
| 556 |
| 557 // Parse from |data|. |
| 558 decoder->setData(data.get(), true); |
| 559 size_t frameCount = decoder->frameCount(); |
| 560 |
| 561 // ... and then decode frames from |reallocatedData|. |
| 562 RefPtr<SharedBuffer> reallocatedData = data.get()->copy(); |
| 563 ASSERT_TRUE(reallocatedData.get()); |
| 564 data.clear(); |
| 565 decoder->setData(reallocatedData.get(), true); |
| 566 |
| 567 for (size_t i = 0; i < frameCount; ++i) { |
| 568 const ImageFrame* const frame = decoder->frameBufferAtIndex(i); |
| 569 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus()); |
| 570 } |
| 571 } |
| 572 |
| 573 TEST(AnimatedPNGTests, ProgressiveDecode) |
| 574 { |
| 575 testProgressiveDecoding("/LayoutTests/fast/images/resources/png-animated-idat-
part-of-animation.png"); |
| 576 } |
| 577 |
| 578 TEST(AnimatedPNGTests, ParseAndDecodeByteByByte) |
| 579 { |
| 580 testByteByByteDecode(&createDecoder, |
| 581 "/LayoutTests/fast/images/resources/png-animated-idat-part-of-anima
tion.png", |
| 582 4u, 7u); |
| 583 } |
| 584 |
| 585 TEST(AnimatedPNGTests, FctlWrongSizeBreaksDecoding) |
| 586 { |
| 587 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa
rt-of-animation.png"; |
| 588 auto data = readFile(pngFile); |
| 589 ASSERT_FALSE(data->isEmpty()); |
| 590 |
| 591 // Test the first fcTL in the stream. Because no frame data has been set |
| 592 // at this point, the expected frame count is zero. |
| 593 testInvalidFctlSize("/LayoutTests/fast/images/resources/png-animated-idat-part
-of-animation.png", |
| 594 95u, 0u); |
| 595 |
| 596 // Test for the third fcTL in the stream. This should be tested as well, |
| 597 // since the first fcTL is parsed in PNGImageReader::parseSize() whereas |
| 598 // later fcTLs are parsed in PNGImageReader::parse() as part of framecount. |
| 599 // The expected frame count before the fcTL chunk is 1u, since the second |
| 600 // frame is registered when the third fcTL (or IEND) is seen. |
| 601 testInvalidFctlSize("/LayoutTests/fast/images/resources/png-animated-idat-part
-of-animation.png", |
| 602 241u, 1u); |
| 603 |
| 604 } |
| 605 |
| 606 TEST(AnimatedPNGTests, MissingFctlDataBreaksDecoding) |
| 607 { |
| 608 // The fcTL chunk starts at 95u, add 10u to get to the content data, and |
| 609 // remove 5 bytes of data. |
| 610 testMissingDataBreaksDecoding( |
| 611 "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"
, |
| 612 105u, 5u); |
| 613 } |
| 614 |
| 615 TEST(AnimatedPNGTests, MissingActlResultsInNonAnimated) |
| 616 { |
| 617 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa
rt-of-animation.png"; |
| 618 auto data = readFile(pngFile); |
| 619 auto decoder = createDecoder(); |
| 620 ASSERT_FALSE(data->isEmpty()); |
| 621 |
| 622 // Remove the acTL chunk from the stream. |
| 623 size_t offsetActl = 33; |
| 624 RefPtr<SharedBuffer> noActlData = SharedBuffer::create(data->data(), |
| 625 offsetActl); |
| 626 noActlData->append(data->data() + offsetActl + 20, |
| 627 data->size() - offsetActl - 20); |
| 628 |
| 629 decoder->setData(noActlData, true); |
| 630 EXPECT_EQ(1u, decoder->frameCount()); |
| 631 EXPECT_FALSE(decoder->failed()); |
| 632 } |
| 633 |
| 634 // Test if the indicated frame count by the acTL is ignored if the actual |
| 635 // number of frames is different. Test for higher and lower indicated number. |
| 636 TEST(AnimatedPNGTests, differentActlFrameCountIsIgnored) |
| 637 { |
| 638 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa
rt-of-animation.png"; |
| 639 testDifferentActlFrameCountIsIgnored(pngFile, 33u, 2u, 4u); |
| 640 testDifferentActlFrameCountIsIgnored(pngFile, 33u, 8u, 4u); |
| 641 } |
| 642 |
| 643 // Check if a frame rectangle, that is larger than the image width, gets |
| 644 // clipped correctly. |
| 645 // @TODO(joostouwerling) use an image that also has data that needs to be |
| 646 // clipped, not just report larger dimensions. |
| 647 // @TODO(joostouwerling) determine how libpng handles larger / smaller images |
| 648 // than indicated. |
| 649 TEST(AnimatedPNGTests, frameRectIsClipped) |
| 650 { |
| 651 const char* pngFile = "/LayoutTests/fast/images/resources/png-animated-idat-pa
rt-of-animation.png"; |
| 652 auto data = readFile(pngFile); |
| 653 auto decoder = createDecoder(); |
| 654 ASSERT_FALSE(data->isEmpty()); |
| 655 |
| 656 // Change the width and height of the frame so it falls outside the image. |
| 657 size_t offsetThirdFctl = 241 + 12; |
| 658 RefPtr<SharedBuffer> modifiedData = SharedBuffer::create(data->data(), |
| 659 offsetThirdFctl); |
| 660 png_byte sizeChunk[8]; |
| 661 writeUint32(10, sizeChunk); |
| 662 writeUint32(15, sizeChunk + 4); |
| 663 modifiedData->append(const_cast<const char*>( |
| 664 reinterpret_cast<char*>(sizeChunk)), 8u); |
| 665 modifiedData->append(data->data() + offsetThirdFctl + 8, |
| 666 data->size() - offsetThirdFctl - 8); |
| 667 |
| 668 decoder->setData(modifiedData, true); |
| 669 |
| 670 IntSize expectedSize(5, 5); |
| 671 size_t expectedFrameCount = 4; |
| 672 IntRect expectedFrameRect(IntPoint(1, 2), IntSize(4, 3)); |
| 673 |
| 674 EXPECT_TRUE(decoder->isSizeAvailable()); |
| 675 EXPECT_EQ(expectedSize, decoder->size()); |
| 676 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
| 677 ASSERT_FALSE(decoder->failed()); |
| 678 EXPECT_EQ(expectedFrameRect, |
| 679 decoder->frameBufferAtIndex(2)->originalFrameRect()); |
| 680 } |
| 681 |
| 682 TEST(AnimatedPNGTests, ProgressiveDecodingChangesFrameBuffer) |
| 683 { |
| 684 testProgressiveDecodingChangesFrameBuffer( |
| 685 "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"
, 249u); |
| 686 } |
| 687 |
| 688 TEST(AnimatedPNGTests, ProgressiveDecodingContinuesAfterFullData) |
| 689 { |
| 690 testProgressiveDecodingContinuesAfterFullData( |
| 691 "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"
, 160u); |
| 692 } |
| 693 |
| 694 TEST(AnimatedPNGTests, RandomDecodeAfterClearFrameBufferCache) |
| 695 { |
| 696 testRandomDecodeAfterClearFrameBufferCache( |
| 697 "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"
); |
| 698 } |
| 699 |
| 700 TEST(AnimatedPNGTests, VerifyAlphaBlending) { |
| 701 testAlphaBlending( |
| 702 "/LayoutTests/fast/images/resources/png-animated-idat-part-of-animation.png"
); |
| 703 } |
| 704 |
| 705 // Static PNG tests |
| 706 |
| 707 TEST(StaticPNGTests, repetitionCountTest) |
| 708 { |
| 709 testRepetitionCount("/LayoutTests/fast/images/resources/png-simple.png", |
| 710 cAnimationNone); |
| 711 } |
| 712 |
| 713 TEST(StaticPNGTests, sizeTest) |
| 714 { |
| 715 testSize("/LayoutTests/fast/images/resources/png-simple.png", |
| 716 IntSize(111, 29)); |
| 717 } |
| 718 |
| 719 TEST(StaticPNGTests, MetaDataTest) |
| 720 { |
| 721 const size_t expectedFrameCount = 1; |
| 722 const size_t expectedDuration = 0; |
| 723 auto decoder = createDecoderWithPngData("/LayoutTests/fast/images/resources/pn
g-simple.png"); |
| 724 EXPECT_EQ(expectedFrameCount, decoder->frameCount()); |
| 725 EXPECT_EQ(expectedDuration, decoder->frameDurationAtIndex(0)); |
| 726 } |
| 727 |
| 728 TEST(StaticPNGTests, InvalidIHDRChunk) |
| 729 { |
| 730 testMissingDataBreaksDecoding( |
| 731 "/LayoutTests/fast/images/resources/png-simple.png", 20u, 2u); |
| 732 } |
| 733 |
| 734 TEST(StaticPNGTests, ProgressiveDecoding) |
| 735 { |
| 736 testProgressiveDecoding("/LayoutTests/fast/images/resources/png-simple.png"); |
| 737 } |
| 738 |
| 739 TEST(StaticPNGTests, ProgressiveDecodingChangesFrameBuffer) |
| 740 { |
| 741 testProgressiveDecodingChangesFrameBuffer( |
| 742 "/LayoutTests/fast/images/resources/png-simple.png", 0u, 5u); |
| 743 } |
| 744 |
| 745 TEST(StaticPNGTests, ProgressiveDecodingContinuesAfterFullData) |
| 746 { |
| 747 testProgressiveDecodingContinuesAfterFullData( |
| 748 "/LayoutTests/fast/images/resources/png-simple.png", 1000u); |
| 749 } |
| 750 |
| 751 }; // namespace blink |
OLD | NEW |