Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: third_party/WebKit/Source/core/frame/ImageBitmap.cpp

Issue 2575693005: Add unit test for color managed ImageBitamp(ImageBitmap) constructor (Closed)
Patch Set: Moving ImageBitmap(HTMLCanvasElement) test to CanvasRenderingContext2DTest Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "core/frame/ImageBitmap.h" 5 #include "core/frame/ImageBitmap.h"
6 6
7 #include "core/html/HTMLCanvasElement.h" 7 #include "core/html/HTMLCanvasElement.h"
8 #include "core/html/HTMLVideoElement.h" 8 #include "core/html/HTMLVideoElement.h"
9 #include "core/html/ImageData.h" 9 #include "core/html/ImageData.h"
10 #include "platform/graphics/skia/SkiaUtils.h" 10 #include "platform/graphics/skia/SkiaUtils.h"
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 IntSize playerSize; 603 IntSize playerSize;
604 if (video->webMediaPlayer()) 604 if (video->webMediaPlayer())
605 playerSize = video->webMediaPlayer()->naturalSize(); 605 playerSize = video->webMediaPlayer()->naturalSize();
606 ParsedOptions parsedOptions = 606 ParsedOptions parsedOptions =
607 parseOptions(options, cropRect, video->bitmapSourceSize()); 607 parseOptions(options, cropRect, video->bitmapSourceSize());
608 if (dstBufferSizeHasOverflow(parsedOptions)) 608 if (dstBufferSizeHasOverflow(parsedOptions))
609 return; 609 return;
610 610
611 std::unique_ptr<ImageBuffer> buffer = ImageBuffer::create( 611 std::unique_ptr<ImageBuffer> buffer = ImageBuffer::create(
612 IntSize(parsedOptions.resizeWidth, parsedOptions.resizeHeight), NonOpaque, 612 IntSize(parsedOptions.resizeWidth, parsedOptions.resizeHeight), NonOpaque,
613 DoNotInitializeImagePixels); 613 DoNotInitializeImagePixels, parsedOptions.dstColorSpace);
614 if (!buffer) 614 if (!buffer)
615 return; 615 return;
616 616
617 IntPoint dstPoint = 617 IntPoint dstPoint =
618 IntPoint(-parsedOptions.cropRect.x(), -parsedOptions.cropRect.y()); 618 IntPoint(-parsedOptions.cropRect.x(), -parsedOptions.cropRect.y());
619 if (parsedOptions.flipY) { 619 if (parsedOptions.flipY) {
620 buffer->canvas()->translate(0, buffer->size().height()); 620 buffer->canvas()->translate(0, buffer->size().height());
621 buffer->canvas()->scale(1, -1); 621 buffer->canvas()->scale(1, -1);
622 } 622 }
623 SkPaint paint; 623 SkPaint paint;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 m_image = StaticBitmapImage::create(premulSkImageToUnPremul( 680 m_image = StaticBitmapImage::create(premulSkImageToUnPremul(
681 m_image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget()) 681 m_image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget())
682 .get())); 682 .get()));
683 } 683 }
684 if (!m_image) 684 if (!m_image)
685 return; 685 return;
686 m_image->setOriginClean(canvas->originClean()); 686 m_image->setOriginClean(canvas->originClean());
687 m_image->setPremultiplied(parsedOptions.premultiplyAlpha); 687 m_image->setPremultiplied(parsedOptions.premultiplyAlpha);
688 } 688 }
689 689
690 // TODO(zakerinasab): Support color space conversion in this constructor when
691 // the new serialization module is ready. crbug.com/670703
690 ImageBitmap::ImageBitmap(const void* pixelData, 692 ImageBitmap::ImageBitmap(const void* pixelData,
691 uint32_t width, 693 uint32_t width,
692 uint32_t height, 694 uint32_t height,
693 bool isImageBitmapPremultiplied, 695 bool isImageBitmapPremultiplied,
694 bool isImageBitmapOriginClean) { 696 bool isImageBitmapOriginClean) {
695 SkImageInfo info = SkImageInfo::MakeN32( 697 SkImageInfo info = SkImageInfo::MakeN32(
696 width, height, 698 width, height,
697 isImageBitmapPremultiplied ? kPremul_SkAlphaType : kUnpremul_SkAlphaType); 699 isImageBitmapPremultiplied ? kPremul_SkAlphaType : kUnpremul_SkAlphaType);
698 SkPixmap pixmap(info, pixelData, info.bytesPerPixel() * width); 700 SkPixmap pixmap(info, pixelData, info.bytesPerPixel() * width);
699 m_image = StaticBitmapImage::create(SkImage::MakeRasterCopy(pixmap)); 701 m_image = StaticBitmapImage::create(SkImage::MakeRasterCopy(pixmap));
700 if (!m_image) 702 if (!m_image)
701 return; 703 return;
702 m_image->setPremultiplied(isImageBitmapPremultiplied); 704 m_image->setPremultiplied(isImageBitmapPremultiplied);
703 m_image->setOriginClean(isImageBitmapOriginClean); 705 m_image->setOriginClean(isImageBitmapOriginClean);
704 } 706 }
705 707
706 static sk_sp<SkImage> scaleSkImage(sk_sp<SkImage> skImage, 708 static sk_sp<SkImage> scaleSkImage(sk_sp<SkImage> skImage,
707 unsigned resizeWidth, 709 unsigned resizeWidth,
708 unsigned resizeHeight, 710 unsigned resizeHeight,
709 SkFilterQuality resizeQuality) { 711 SkFilterQuality resizeQuality,
712 SkColorType colorType = kN32_SkColorType,
713 sk_sp<SkColorSpace> colorSpace = nullptr) {
710 SkImageInfo resizedInfo = SkImageInfo::Make( 714 SkImageInfo resizedInfo = SkImageInfo::Make(
711 resizeWidth, resizeHeight, kN32_SkColorType, kUnpremul_SkAlphaType); 715 resizeWidth, resizeHeight, colorType, kUnpremul_SkAlphaType, colorSpace);
712 RefPtr<ArrayBuffer> dstBuffer = ArrayBuffer::createOrNull( 716 RefPtr<ArrayBuffer> dstBuffer = ArrayBuffer::createOrNull(
713 resizeWidth * resizeHeight, resizedInfo.bytesPerPixel()); 717 resizeWidth * resizeHeight, resizedInfo.bytesPerPixel());
714 if (!dstBuffer) 718 if (!dstBuffer)
715 return nullptr; 719 return nullptr;
716 RefPtr<Uint8Array> resizedPixels = 720 RefPtr<Uint8Array> resizedPixels =
717 Uint8Array::create(dstBuffer, 0, dstBuffer->byteLength()); 721 Uint8Array::create(dstBuffer, 0, dstBuffer->byteLength());
718 SkPixmap pixmap( 722 SkPixmap pixmap(
719 resizedInfo, resizedPixels->data(), 723 resizedInfo, resizedPixels->data(),
720 static_cast<unsigned>(resizeWidth) * resizedInfo.bytesPerPixel()); 724 static_cast<unsigned>(resizeWidth) * resizedInfo.bytesPerPixel());
721 skImage->scalePixels(pixmap, resizeQuality); 725 skImage->scalePixels(pixmap, resizeQuality);
722 return SkImage::MakeFromRaster(pixmap, 726 return SkImage::MakeFromRaster(pixmap,
723 [](const void*, void* pixels) { 727 [](const void*, void* pixels) {
724 static_cast<Uint8Array*>(pixels)->deref(); 728 static_cast<Uint8Array*>(pixels)->deref();
725 }, 729 },
726 resizedPixels.release().leakRef()); 730 resizedPixels.release().leakRef());
727 } 731 }
728 732
733 // TODO(zakerinasab): Add color space conversion to this constructor when
734 // ImageData supports color spaces. crbug.com/670715.
Justin Novosad 2016/12/14 20:53:46 It seems this constructor is now in a half fixed s
zakerinasab1 2016/12/16 20:25:34 No, but it can be if we pass the color space param
729 ImageBitmap::ImageBitmap(ImageData* data, 735 ImageBitmap::ImageBitmap(ImageData* data,
730 Optional<IntRect> cropRect, 736 Optional<IntRect> cropRect,
731 const ImageBitmapOptions& options) { 737 const ImageBitmapOptions& options) {
732 // TODO(xidachen): implement the resize option 738 // TODO(xidachen): implement the resize option
733 IntRect dataSrcRect = IntRect(IntPoint(), data->size()); 739 IntRect dataSrcRect = IntRect(IntPoint(), data->size());
734 ParsedOptions parsedOptions = 740 ParsedOptions parsedOptions =
735 parseOptions(options, cropRect, data->bitmapSourceSize()); 741 parseOptions(options, cropRect, data->bitmapSourceSize());
736 if (dstBufferSizeHasOverflow(parsedOptions)) 742 if (dstBufferSizeHasOverflow(parsedOptions))
737 return; 743 return;
738 IntRect srcRect = cropRect ? intersection(parsedOptions.cropRect, dataSrcRect) 744 IntRect srcRect = cropRect ? intersection(parsedOptions.cropRect, dataSrcRect)
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
984 m_isNeutered = true; 990 m_isNeutered = true;
985 } 991 }
986 992
987 // static 993 // static
988 ImageBitmap* ImageBitmap::take(ScriptPromiseResolver*, sk_sp<SkImage> image) { 994 ImageBitmap* ImageBitmap::take(ScriptPromiseResolver*, sk_sp<SkImage> image) {
989 return ImageBitmap::create(StaticBitmapImage::create(std::move(image))); 995 return ImageBitmap::create(StaticBitmapImage::create(std::move(image)));
990 } 996 }
991 997
992 PassRefPtr<Uint8Array> ImageBitmap::copyBitmapData(AlphaDisposition alphaOp, 998 PassRefPtr<Uint8Array> ImageBitmap::copyBitmapData(AlphaDisposition alphaOp,
993 DataColorFormat format) { 999 DataColorFormat format) {
994 SkImageInfo info = SkImageInfo::Make( 1000 SkColorType colorType = kRGBA_8888_SkColorType;
995 width(), height(), 1001 if (format == N32ColorType)
996 (format == RGBAColorType) ? kRGBA_8888_SkColorType : kN32_SkColorType, 1002 colorType = kN32_SkColorType;
997 (alphaOp == PremultiplyAlpha) ? kPremul_SkAlphaType 1003 else if (format == F16ColorType)
998 : kUnpremul_SkAlphaType); 1004 colorType = kRGBA_F16_SkColorType;
999 // TODO(ccameron): Canvas should operate in sRGB and not display space. 1005
1000 // https://crbug.com/667431 1006 // TODO(zakerinasab): Fix this to use the SkColorSpace from
1007 // m_image->imageForCurrentFrame() instead of nullptr. crbug.com/671356.
1008 sk_sp<SkColorSpace> colorSpace = nullptr;
1009 SkImageInfo info =
1010 SkImageInfo::Make(width(), height(), colorType,
1011 (alphaOp == PremultiplyAlpha) ? kPremul_SkAlphaType
1012 : kUnpremul_SkAlphaType,
1013 colorSpace);
1014 // TODO(zakerinasab): Bitmap data must be returned in the current color space
Justin Novosad 2016/12/14 20:53:46 Note: This will require making StaticBitmapImage a
zakerinasab1 2016/12/16 20:25:34 I added your comments to the TODO to be considered
1015 // of the ImageBitmap and not display space. crbug.com/671356.
1016 // When this is fixed, also fix ShapeDetector::detect(...).
1001 RefPtr<Uint8Array> dstPixels = copySkImageData( 1017 RefPtr<Uint8Array> dstPixels = copySkImageData(
1002 m_image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget()) 1018 m_image->imageForCurrentFrame(ColorBehavior::transformToGlobalTarget())
1003 .get(), 1019 .get(),
1004 info); 1020 info);
1005 return dstPixels.release(); 1021 return dstPixels.release();
1006 } 1022 }
1007 1023
1008 unsigned long ImageBitmap::width() const { 1024 unsigned long ImageBitmap::width() const {
1009 if (!m_image) 1025 if (!m_image)
1010 return 0; 1026 return 0;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1058 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, 1074 void ImageBitmap::adjustDrawRects(FloatRect* srcRect,
1059 FloatRect* dstRect) const {} 1075 FloatRect* dstRect) const {}
1060 1076
1061 FloatSize ImageBitmap::elementSize(const FloatSize&) const { 1077 FloatSize ImageBitmap::elementSize(const FloatSize&) const {
1062 return FloatSize(width(), height()); 1078 return FloatSize(width(), height());
1063 } 1079 }
1064 1080
1065 DEFINE_TRACE(ImageBitmap) {} 1081 DEFINE_TRACE(ImageBitmap) {}
1066 1082
1067 } // namespace blink 1083 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698