Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 ImageDecoder::noDecodedImageByteLimit)); | 52 ImageDecoder::noDecodedImageByteLimit)); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void testRepetitionCount(const char* dir, | 55 void testRepetitionCount(const char* dir, |
| 56 const char* file, | 56 const char* file, |
| 57 int expectedRepetitionCount) { | 57 int expectedRepetitionCount) { |
| 58 std::unique_ptr<ImageDecoder> decoder = createDecoder(); | 58 std::unique_ptr<ImageDecoder> decoder = createDecoder(); |
| 59 RefPtr<SharedBuffer> data = readFile(dir, file); | 59 RefPtr<SharedBuffer> data = readFile(dir, file); |
| 60 ASSERT_TRUE(data.get()); | 60 ASSERT_TRUE(data.get()); |
| 61 decoder->setData(data.get(), true); | 61 decoder->setData(data.get(), true); |
| 62 EXPECT_EQ(cAnimationLoopOnce, | |
| 63 decoder->repetitionCount()); // Default value before decode. | |
| 64 | 62 |
| 65 for (size_t i = 0; i < decoder->frameCount(); ++i) { | 63 // Decode a frame to verify the decoder had a chance to update its state |
| 66 ImageFrame* frame = decoder->frameBufferAtIndex(i); | 64 decoder->frameBufferAtIndex(0); |
|
cblume
2017/03/28 08:35:00
Previously, decode->repetitionCount() would end up
| |
| 67 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus()); | |
| 68 } | |
| 69 | 65 |
| 70 EXPECT_EQ(expectedRepetitionCount, | 66 EXPECT_EQ(expectedRepetitionCount, decoder->repetitionCount()); |
| 71 decoder->repetitionCount()); // Expected value after decode. | |
| 72 } | 67 } |
| 73 | 68 |
| 74 } // anonymous namespace | 69 } // anonymous namespace |
| 75 | 70 |
| 76 TEST(GIFImageDecoderTest, decodeTwoFrames) { | 71 TEST(GIFImageDecoderTest, decodeTwoFrames) { |
| 77 std::unique_ptr<ImageDecoder> decoder = createDecoder(); | 72 std::unique_ptr<ImageDecoder> decoder = createDecoder(); |
| 78 | 73 |
| 79 RefPtr<SharedBuffer> data = readFile(layoutTestResourcesDir, "animated.gif"); | 74 RefPtr<SharedBuffer> data = readFile(layoutTestResourcesDir, "animated.gif"); |
| 80 ASSERT_TRUE(data.get()); | 75 ASSERT_TRUE(data.get()); |
| 81 decoder->setData(data.get(), true); | 76 decoder->setData(data.get(), true); |
| 82 EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount()); | |
| 83 | 77 |
| 84 ImageFrame* frame = decoder->frameBufferAtIndex(0); | 78 ImageFrame* frame = decoder->frameBufferAtIndex(0); |
| 85 uint32_t generationID0 = frame->bitmap().getGenerationID(); | 79 uint32_t generationID0 = frame->bitmap().getGenerationID(); |
| 86 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus()); | 80 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus()); |
| 87 EXPECT_EQ(16, frame->bitmap().width()); | 81 EXPECT_EQ(16, frame->bitmap().width()); |
| 88 EXPECT_EQ(16, frame->bitmap().height()); | 82 EXPECT_EQ(16, frame->bitmap().height()); |
| 89 | 83 |
| 90 frame = decoder->frameBufferAtIndex(1); | 84 frame = decoder->frameBufferAtIndex(1); |
| 91 uint32_t generationID1 = frame->bitmap().getGenerationID(); | 85 uint32_t generationID1 = frame->bitmap().getGenerationID(); |
| 92 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus()); | 86 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus()); |
| 93 EXPECT_EQ(16, frame->bitmap().width()); | 87 EXPECT_EQ(16, frame->bitmap().width()); |
| 94 EXPECT_EQ(16, frame->bitmap().height()); | 88 EXPECT_EQ(16, frame->bitmap().height()); |
| 95 EXPECT_TRUE(generationID0 != generationID1); | 89 EXPECT_TRUE(generationID0 != generationID1); |
| 96 | 90 |
| 97 EXPECT_EQ(2u, decoder->frameCount()); | 91 EXPECT_EQ(2u, decoder->frameCount()); |
| 98 EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount()); | 92 EXPECT_EQ(cAnimationLoopInfinite, decoder->repetitionCount()); |
| 99 } | 93 } |
| 100 | 94 |
| 101 TEST(GIFImageDecoderTest, parseAndDecode) { | 95 TEST(GIFImageDecoderTest, parseAndDecode) { |
| 102 std::unique_ptr<ImageDecoder> decoder = createDecoder(); | 96 std::unique_ptr<ImageDecoder> decoder = createDecoder(); |
| 103 | 97 |
| 104 RefPtr<SharedBuffer> data = readFile(layoutTestResourcesDir, "animated.gif"); | 98 RefPtr<SharedBuffer> data = readFile(layoutTestResourcesDir, "animated.gif"); |
| 105 ASSERT_TRUE(data.get()); | 99 ASSERT_TRUE(data.get()); |
| 106 decoder->setData(data.get(), true); | 100 decoder->setData(data.get(), true); |
| 107 EXPECT_EQ(cAnimationLoopOnce, decoder->repetitionCount()); | |
| 108 | |
| 109 // This call will parse the entire file. | |
| 110 EXPECT_EQ(2u, decoder->frameCount()); | |
| 111 | 101 |
| 112 ImageFrame* frame = decoder->frameBufferAtIndex(0); | 102 ImageFrame* frame = decoder->frameBufferAtIndex(0); |
| 113 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus()); | 103 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus()); |
| 114 EXPECT_EQ(16, frame->bitmap().width()); | 104 EXPECT_EQ(16, frame->bitmap().width()); |
| 115 EXPECT_EQ(16, frame->bitmap().height()); | 105 EXPECT_EQ(16, frame->bitmap().height()); |
| 116 | 106 |
| 117 frame = decoder->frameBufferAtIndex(1); | 107 frame = decoder->frameBufferAtIndex(1); |
| 118 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus()); | 108 EXPECT_EQ(ImageFrame::FrameComplete, frame->getStatus()); |
| 119 EXPECT_EQ(16, frame->bitmap().width()); | 109 EXPECT_EQ(16, frame->bitmap().width()); |
| 120 EXPECT_EQ(16, frame->bitmap().height()); | 110 EXPECT_EQ(16, frame->bitmap().height()); |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 RefPtr<SharedBuffer> testData = readFile(decodersTestingDir, "bad-code.gif"); | 290 RefPtr<SharedBuffer> testData = readFile(decodersTestingDir, "bad-code.gif"); |
| 301 ASSERT_TRUE(testData.get()); | 291 ASSERT_TRUE(testData.get()); |
| 302 | 292 |
| 303 std::unique_ptr<ImageDecoder> testDecoder = createDecoder(); | 293 std::unique_ptr<ImageDecoder> testDecoder = createDecoder(); |
| 304 testDecoder->setData(testData.get(), true); | 294 testDecoder->setData(testData.get(), true); |
| 305 EXPECT_EQ(1u, testDecoder->frameCount()); | 295 EXPECT_EQ(1u, testDecoder->frameCount()); |
| 306 ASSERT_TRUE(testDecoder->frameBufferAtIndex(0)); | 296 ASSERT_TRUE(testDecoder->frameBufferAtIndex(0)); |
| 307 EXPECT_TRUE(testDecoder->failed()); | 297 EXPECT_TRUE(testDecoder->failed()); |
| 308 } | 298 } |
| 309 | 299 |
| 310 TEST(GIFImageDecoderTest, invalidDisposalMethod) { | |
| 311 std::unique_ptr<ImageDecoder> decoder = createDecoder(); | |
| 312 | |
| 313 // The image has 2 frames, with disposal method 4 and 5, respectively. | |
| 314 RefPtr<SharedBuffer> data = | |
| 315 readFile(decodersTestingDir, "invalid-disposal-method.gif"); | |
| 316 ASSERT_TRUE(data.get()); | |
| 317 decoder->setData(data.get(), true); | |
| 318 | |
| 319 EXPECT_EQ(2u, decoder->frameCount()); | |
| 320 // Disposal method 4 is converted to ImageFrame::DisposeOverwritePrevious. | |
| 321 EXPECT_EQ(ImageFrame::DisposeOverwritePrevious, | |
| 322 decoder->frameBufferAtIndex(0)->getDisposalMethod()); | |
| 323 // Disposal method 5 is ignored. | |
| 324 EXPECT_EQ(ImageFrame::DisposeNotSpecified, | |
| 325 decoder->frameBufferAtIndex(1)->getDisposalMethod()); | |
| 326 } | |
| 327 | |
| 328 TEST(GIFImageDecoderTest, firstFrameHasGreaterSizeThanScreenSize) { | 300 TEST(GIFImageDecoderTest, firstFrameHasGreaterSizeThanScreenSize) { |
| 329 RefPtr<SharedBuffer> fullData = readFile( | 301 RefPtr<SharedBuffer> fullData = readFile( |
| 330 decodersTestingDir, "first-frame-has-greater-size-than-screen-size.gif"); | 302 decodersTestingDir, "first-frame-has-greater-size-than-screen-size.gif"); |
| 331 ASSERT_TRUE(fullData.get()); | 303 ASSERT_TRUE(fullData.get()); |
| 332 | 304 |
| 333 std::unique_ptr<ImageDecoder> decoder; | 305 std::unique_ptr<ImageDecoder> decoder; |
| 334 IntSize frameSize; | 306 IntSize frameSize; |
| 335 | 307 |
| 336 // Compute hashes when the file is truncated. | 308 // Compute hashes when the file is truncated. |
| 337 for (size_t i = 1; i <= fullData->size(); ++i) { | 309 for (size_t i = 1; i <= fullData->size(); ++i) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 352 | 324 |
| 353 TEST(GIFImageDecoderTest, verifyRepetitionCount) { | 325 TEST(GIFImageDecoderTest, verifyRepetitionCount) { |
| 354 testRepetitionCount(layoutTestResourcesDir, "full2loop.gif", 2); | 326 testRepetitionCount(layoutTestResourcesDir, "full2loop.gif", 2); |
| 355 testRepetitionCount(decodersTestingDir, "radient.gif", cAnimationNone); | 327 testRepetitionCount(decodersTestingDir, "radient.gif", cAnimationNone); |
| 356 } | 328 } |
| 357 | 329 |
| 358 TEST(GIFImageDecoderTest, bitmapAlphaType) { | 330 TEST(GIFImageDecoderTest, bitmapAlphaType) { |
| 359 RefPtr<SharedBuffer> fullData = readFile(decodersTestingDir, "radient.gif"); | 331 RefPtr<SharedBuffer> fullData = readFile(decodersTestingDir, "radient.gif"); |
| 360 ASSERT_TRUE(fullData.get()); | 332 ASSERT_TRUE(fullData.get()); |
| 361 | 333 |
| 362 // Empirically chosen truncation size: | |
| 363 // a) large enough to produce a partial frame && | |
| 364 // b) small enough to not fully decode the frame | |
| 365 const size_t kTruncateSize = 800; | |
| 366 ASSERT_TRUE(kTruncateSize < fullData->size()); | |
| 367 RefPtr<SharedBuffer> partialData = | |
| 368 SharedBuffer::create(fullData->data(), kTruncateSize); | |
| 369 | |
| 370 std::unique_ptr<ImageDecoder> premulDecoder = WTF::wrapUnique( | 334 std::unique_ptr<ImageDecoder> premulDecoder = WTF::wrapUnique( |
| 371 new GIFImageDecoder(ImageDecoder::AlphaPremultiplied, | 335 new GIFImageDecoder(ImageDecoder::AlphaPremultiplied, |
| 372 ColorBehavior::transformToTargetForTesting(), | 336 ColorBehavior::transformToTargetForTesting(), |
| 373 ImageDecoder::noDecodedImageByteLimit)); | 337 ImageDecoder::noDecodedImageByteLimit)); |
| 374 std::unique_ptr<ImageDecoder> unpremulDecoder = WTF::wrapUnique( | 338 std::unique_ptr<ImageDecoder> unpremulDecoder = WTF::wrapUnique( |
| 375 new GIFImageDecoder(ImageDecoder::AlphaNotPremultiplied, | 339 new GIFImageDecoder(ImageDecoder::AlphaNotPremultiplied, |
| 376 ColorBehavior::transformToTargetForTesting(), | 340 ColorBehavior::transformToTargetForTesting(), |
| 377 ImageDecoder::noDecodedImageByteLimit)); | 341 ImageDecoder::noDecodedImageByteLimit)); |
| 378 | 342 |
| 379 // Partially decoded frame => the frame alpha type is unknown and should | |
| 380 // reflect the requested format. | |
| 381 premulDecoder->setData(partialData.get(), false); | |
| 382 ASSERT_TRUE(premulDecoder->frameCount()); | |
| 383 unpremulDecoder->setData(partialData.get(), false); | |
| 384 ASSERT_TRUE(unpremulDecoder->frameCount()); | |
| 385 ImageFrame* premulFrame = premulDecoder->frameBufferAtIndex(0); | |
| 386 EXPECT_TRUE(premulFrame && | |
| 387 premulFrame->getStatus() != ImageFrame::FrameComplete); | |
| 388 EXPECT_EQ(premulFrame->bitmap().alphaType(), kPremul_SkAlphaType); | |
| 389 ImageFrame* unpremulFrame = unpremulDecoder->frameBufferAtIndex(0); | |
| 390 EXPECT_TRUE(unpremulFrame && | |
| 391 unpremulFrame->getStatus() != ImageFrame::FrameComplete); | |
| 392 EXPECT_EQ(unpremulFrame->bitmap().alphaType(), kUnpremul_SkAlphaType); | |
| 393 | |
| 394 // Fully decoded frame => the frame alpha type is known (opaque). | |
| 395 premulDecoder->setData(fullData.get(), true); | 343 premulDecoder->setData(fullData.get(), true); |
| 396 ASSERT_TRUE(premulDecoder->frameCount()); | 344 ASSERT_TRUE(premulDecoder->frameCount()); |
| 397 unpremulDecoder->setData(fullData.get(), true); | 345 unpremulDecoder->setData(fullData.get(), true); |
| 398 ASSERT_TRUE(unpremulDecoder->frameCount()); | 346 ASSERT_TRUE(unpremulDecoder->frameCount()); |
| 399 premulFrame = premulDecoder->frameBufferAtIndex(0); | 347 ImageFrame* premulFrame = premulDecoder->frameBufferAtIndex(0); |
| 400 EXPECT_TRUE(premulFrame && | 348 EXPECT_TRUE(premulFrame && |
| 401 premulFrame->getStatus() == ImageFrame::FrameComplete); | 349 premulFrame->getStatus() == ImageFrame::FrameComplete); |
| 402 EXPECT_EQ(premulFrame->bitmap().alphaType(), kOpaque_SkAlphaType); | 350 EXPECT_EQ(premulFrame->bitmap().alphaType(), kPremul_SkAlphaType); |
| 403 unpremulFrame = unpremulDecoder->frameBufferAtIndex(0); | 351 ImageFrame* unpremulFrame = unpremulDecoder->frameBufferAtIndex(0); |
| 404 EXPECT_TRUE(unpremulFrame && | 352 EXPECT_TRUE(unpremulFrame && |
| 405 unpremulFrame->getStatus() == ImageFrame::FrameComplete); | 353 unpremulFrame->getStatus() == ImageFrame::FrameComplete); |
| 406 EXPECT_EQ(unpremulFrame->bitmap().alphaType(), kOpaque_SkAlphaType); | 354 EXPECT_EQ(unpremulFrame->bitmap().alphaType(), kUnpremul_SkAlphaType); |
| 407 } | 355 } |
| 408 | 356 |
| 409 namespace { | 357 namespace { |
| 410 // Needed to exercise ImageDecoder::setMemoryAllocator, but still does the | 358 // Needed to exercise ImageDecoder::setMemoryAllocator, but still does the |
| 411 // default allocation. | 359 // default allocation. |
| 412 class Allocator final : public SkBitmap::Allocator { | 360 class Allocator final : public SkBitmap::Allocator { |
| 413 bool allocPixelRef(SkBitmap* dst, SkColorTable* ctable) override { | 361 bool allocPixelRef(SkBitmap* dst, SkColorTable* ctable) override { |
| 414 return dst->tryAllocPixels(ctable); | 362 return dst->tryAllocPixels(ctable); |
| 415 } | 363 } |
| 416 }; | 364 }; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 430 EXPECT_EQ(1u, decoder->frameCount()); | 378 EXPECT_EQ(1u, decoder->frameCount()); |
| 431 ImageFrame* frame = decoder->frameBufferAtIndex(0); | 379 ImageFrame* frame = decoder->frameBufferAtIndex(0); |
| 432 decoder->setMemoryAllocator(nullptr); | 380 decoder->setMemoryAllocator(nullptr); |
| 433 | 381 |
| 434 ASSERT_TRUE(frame); | 382 ASSERT_TRUE(frame); |
| 435 EXPECT_EQ(IntRect(IntPoint(), decoder->size()), frame->originalFrameRect()); | 383 EXPECT_EQ(IntRect(IntPoint(), decoder->size()), frame->originalFrameRect()); |
| 436 EXPECT_FALSE(frame->hasAlpha()); | 384 EXPECT_FALSE(frame->hasAlpha()); |
| 437 } | 385 } |
| 438 | 386 |
| 439 } // namespace blink | 387 } // namespace blink |
| OLD | NEW |