| 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 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 #include "core/frame/ImageBitmap.h" | 31 #include "core/frame/ImageBitmap.h" |
| 32 | 32 |
| 33 #include "SkPixelRef.h" // FIXME: qualify this skia header file. | 33 #include "SkPixelRef.h" // FIXME: qualify this skia header file. |
| 34 #include "core/dom/Document.h" | 34 #include "core/dom/Document.h" |
| 35 #include "core/fetch/MemoryCache.h" | 35 #include "core/fetch/MemoryCache.h" |
| 36 #include "core/frame/FrameView.h" | 36 #include "core/frame/FrameView.h" |
| 37 #include "core/html/HTMLCanvasElement.h" | 37 #include "core/html/HTMLCanvasElement.h" |
| 38 #include "core/html/HTMLImageElement.h" | 38 #include "core/html/HTMLImageElement.h" |
| 39 #include "core/html/HTMLVideoElement.h" | 39 #include "core/html/HTMLVideoElement.h" |
| 40 #include "core/html/ImageData.h" |
| 40 #include "core/loader/resource/ImageResourceContent.h" | 41 #include "core/loader/resource/ImageResourceContent.h" |
| 41 #include "platform/graphics/StaticBitmapImage.h" | 42 #include "platform/graphics/StaticBitmapImage.h" |
| 42 #include "platform/graphics/skia/SkiaUtils.h" | 43 #include "platform/graphics/skia/SkiaUtils.h" |
| 43 #include "platform/heap/Handle.h" | 44 #include "platform/heap/Handle.h" |
| 44 #include "platform/image-decoders/ImageDecoder.h" | 45 #include "platform/image-decoders/ImageDecoder.h" |
| 45 #include "platform/network/ResourceRequest.h" | 46 #include "platform/network/ResourceRequest.h" |
| 46 #include "testing/gtest/include/gtest/gtest.h" | 47 #include "testing/gtest/include/gtest/gtest.h" |
| 47 #include "third_party/skia/include/core/SkCanvas.h" | 48 #include "third_party/skia/include/core/SkCanvas.h" |
| 48 #include "third_party/skia/include/core/SkColorSpaceXform.h" | 49 #include "third_party/skia/include/core/SkColorSpaceXform.h" |
| 49 #include "third_party/skia/include/core/SkImage.h" | 50 #include "third_party/skia/include/core/SkImage.h" |
| 50 #include "third_party/skia/include/core/SkSurface.h" | 51 #include "third_party/skia/include/core/SkSurface.h" |
| 52 #include "third_party/skia/include/core/SkSwizzle.h" |
| 51 | 53 |
| 52 namespace blink { | 54 namespace blink { |
| 53 | 55 |
| 56 class ExceptionState; |
| 57 |
| 54 class ImageBitmapTest : public ::testing::Test { | 58 class ImageBitmapTest : public ::testing::Test { |
| 55 protected: | 59 protected: |
| 56 virtual void SetUp() { | 60 virtual void SetUp() { |
| 57 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(10, 10); | 61 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(10, 10); |
| 58 surface->getCanvas()->clear(0xFFFFFFFF); | 62 surface->getCanvas()->clear(0xFFFFFFFF); |
| 59 m_image = surface->makeImageSnapshot(); | 63 m_image = surface->makeImageSnapshot(); |
| 60 | 64 |
| 61 sk_sp<SkSurface> surface2 = SkSurface::MakeRasterN32Premul(5, 5); | 65 sk_sp<SkSurface> surface2 = SkSurface::MakeRasterN32Premul(5, 5); |
| 62 surface2->getCanvas()->clear(0xAAAAAAAA); | 66 surface2->getCanvas()->clear(0xAAAAAAAA); |
| 63 m_image2 = surface2->makeImageSnapshot(); | 67 m_image2 = surface2->makeImageSnapshot(); |
| (...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 new uint8_t[imageInfo.bytesPerPixel()]()); | 525 new uint8_t[imageInfo.bytesPerPixel()]()); |
| 522 colorSpaceXform->apply(colorFormat, transformedPixel.get(), colorFormat32, | 526 colorSpaceXform->apply(colorFormat, transformedPixel.get(), colorFormat32, |
| 523 srcPixel.get(), 1, SkAlphaType::kPremul_SkAlphaType); | 527 srcPixel.get(), 1, SkAlphaType::kPremul_SkAlphaType); |
| 524 | 528 |
| 525 int compare = std::memcmp(convertedPixel.get(), transformedPixel.get(), | 529 int compare = std::memcmp(convertedPixel.get(), transformedPixel.get(), |
| 526 imageInfo.bytesPerPixel()); | 530 imageInfo.bytesPerPixel()); |
| 527 ASSERT_EQ(compare, 0); | 531 ASSERT_EQ(compare, 0); |
| 528 } | 532 } |
| 529 } | 533 } |
| 530 | 534 |
| 535 TEST_F(ImageBitmapTest, ImageBitmapColorSpaceConversionImageData) { |
| 536 unsigned char dataBuffer[4] = {255, 0, 0, 255}; |
| 537 DOMUint8ClampedArray* data = DOMUint8ClampedArray::create(dataBuffer, 4); |
| 538 ImageData* imageData = |
| 539 ImageData::create(IntSize(1, 1), data, kLegacyImageDataColorSpaceName); |
| 540 std::unique_ptr<uint8_t[]> srcPixel(new uint8_t[4]()); |
| 541 memcpy(srcPixel.get(), imageData->data()->data(), 4); |
| 542 |
| 543 Optional<IntRect> cropRect = IntRect(0, 0, 1, 1); |
| 544 sk_sp<SkColorSpace> colorSpace = nullptr; |
| 545 SkColorSpaceXform::ColorFormat colorFormat32 = |
| 546 (SkColorType::kN32_SkColorType == kBGRA_8888_SkColorType) |
| 547 ? SkColorSpaceXform::ColorFormat::kBGRA_8888_ColorFormat |
| 548 : SkColorSpaceXform::ColorFormat::kRGBA_8888_ColorFormat; |
| 549 SkColorType colorType = SkColorType::kN32_SkColorType; |
| 550 SkColorSpaceXform::ColorFormat colorFormat = colorFormat32; |
| 551 |
| 552 for (uint8_t i = |
| 553 static_cast<uint8_t>(ColorSpaceConversion::DEFAULT_COLOR_CORRECTED); |
| 554 i <= static_cast<uint8_t>(ColorSpaceConversion::LAST); i++) { |
| 555 ColorSpaceConversion colorSpaceConversion = |
| 556 static_cast<ColorSpaceConversion>(i); |
| 557 ImageBitmapOptions options = |
| 558 prepareBitmapOptionsAndSetRuntimeFlags(colorSpaceConversion); |
| 559 ImageBitmap* imageBitmap = |
| 560 ImageBitmap::create(imageData, cropRect, options); |
| 561 |
| 562 // ColorBehavior::ignore() is used instead of |
| 563 // ColorBehavior::transformToTargetForTesting() to avoid color conversion to |
| 564 // display color profile, as we want to solely rely on the color correction |
| 565 // that happens in ImageBitmap create method. |
| 566 SkImage* convertedImage = |
| 567 imageBitmap->bitmapImage() |
| 568 ->imageForCurrentFrame(ColorBehavior::ignore()) |
| 569 .get(); |
| 570 |
| 571 switch (colorSpaceConversion) { |
| 572 case ColorSpaceConversion::NONE: |
| 573 NOTREACHED(); |
| 574 break; |
| 575 case ColorSpaceConversion::DEFAULT_COLOR_CORRECTED: |
| 576 case ColorSpaceConversion::SRGB: |
| 577 colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
| 578 colorFormat = colorFormat32; |
| 579 break; |
| 580 case ColorSpaceConversion::LINEAR_RGB: |
| 581 colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named); |
| 582 colorType = SkColorType::kRGBA_F16_SkColorType; |
| 583 colorFormat = SkColorSpaceXform::ColorFormat::kRGBA_F16_ColorFormat; |
| 584 break; |
| 585 default: |
| 586 NOTREACHED(); |
| 587 } |
| 588 |
| 589 SkImageInfo imageInfo = SkImageInfo::Make( |
| 590 1, 1, colorType, SkAlphaType::kUnpremul_SkAlphaType, colorSpace); |
| 591 std::unique_ptr<uint8_t[]> convertedPixel( |
| 592 new uint8_t[imageInfo.bytesPerPixel()]()); |
| 593 convertedImage->readPixels( |
| 594 imageInfo, convertedPixel.get(), |
| 595 convertedImage->width() * imageInfo.bytesPerPixel(), 0, 0); |
| 596 |
| 597 // Transform the source pixel and check if the pixel from image bitmap has |
| 598 // the same color information. |
| 599 std::unique_ptr<SkColorSpaceXform> colorSpaceXform = SkColorSpaceXform::New( |
| 600 imageData->getSkColorSpace().get(), colorSpace.get()); |
| 601 std::unique_ptr<uint8_t[]> transformedPixel( |
| 602 new uint8_t[imageInfo.bytesPerPixel()]()); |
| 603 colorSpaceXform->apply(colorFormat, transformedPixel.get(), colorFormat32, |
| 604 srcPixel.get(), 1, |
| 605 SkAlphaType::kUnpremul_SkAlphaType); |
| 606 int compare = std::memcmp(convertedPixel.get(), transformedPixel.get(), |
| 607 imageInfo.bytesPerPixel()); |
| 608 ASSERT_EQ(compare, 0); |
| 609 } |
| 610 } |
| 611 |
| 531 } // namespace blink | 612 } // namespace blink |
| OLD | NEW |