OLD | NEW |
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 "core/offscreencanvas/OffscreenCanvas.h" | 10 #include "core/offscreencanvas/OffscreenCanvas.h" |
(...skipping 771 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
782 IntRect srcRect = cropRect ? intersection(parsedOptions.cropRect, dataSrcRect) | 782 IntRect srcRect = cropRect ? intersection(parsedOptions.cropRect, dataSrcRect) |
783 : dataSrcRect; | 783 : dataSrcRect; |
784 | 784 |
785 // treat non-premultiplyAlpha as a special case | 785 // treat non-premultiplyAlpha as a special case |
786 if (!parsedOptions.premultiplyAlpha) { | 786 if (!parsedOptions.premultiplyAlpha) { |
787 unsigned char* srcAddr = data->data()->data(); | 787 unsigned char* srcAddr = data->data()->data(); |
788 | 788 |
789 // Using kN32 type, swizzle input if necessary. | 789 // Using kN32 type, swizzle input if necessary. |
790 SkImageInfo info = SkImageInfo::Make( | 790 SkImageInfo info = SkImageInfo::Make( |
791 parsedOptions.cropRect.width(), parsedOptions.cropRect.height(), | 791 parsedOptions.cropRect.width(), parsedOptions.cropRect.height(), |
792 kN32_SkColorType, kUnpremul_SkAlphaType, data->getSkColorSpace()); | 792 kN32_SkColorType, kUnpremul_SkAlphaType, data->skColorSpace()); |
793 unsigned bytesPerPixel = static_cast<unsigned>(info.bytesPerPixel()); | 793 unsigned bytesPerPixel = static_cast<unsigned>(info.bytesPerPixel()); |
794 unsigned srcPixelBytesPerRow = bytesPerPixel * data->size().width(); | 794 unsigned srcPixelBytesPerRow = bytesPerPixel * data->size().width(); |
795 unsigned dstPixelBytesPerRow = | 795 unsigned dstPixelBytesPerRow = |
796 bytesPerPixel * parsedOptions.cropRect.width(); | 796 bytesPerPixel * parsedOptions.cropRect.width(); |
797 sk_sp<SkImage> skImage; | 797 sk_sp<SkImage> skImage; |
798 if (parsedOptions.cropRect == IntRect(IntPoint(), data->size())) { | 798 if (parsedOptions.cropRect == IntRect(IntPoint(), data->size())) { |
799 swizzleImageData(srcAddr, data->size().height(), srcPixelBytesPerRow, | 799 swizzleImageData(srcAddr, data->size().height(), srcPixelBytesPerRow, |
800 parsedOptions.flipY); | 800 parsedOptions.flipY); |
801 skImage = | 801 skImage = |
802 SkImage::MakeRasterCopy(SkPixmap(info, srcAddr, dstPixelBytesPerRow)); | 802 SkImage::MakeRasterCopy(SkPixmap(info, srcAddr, dstPixelBytesPerRow)); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
859 srcAddr[srcStartCopyPosition + j]; | 859 srcAddr[srcStartCopyPosition + j]; |
860 } | 860 } |
861 } | 861 } |
862 } | 862 } |
863 } | 863 } |
864 skImage = newSkImageFromRaster(info, std::move(copiedDataBuffer), | 864 skImage = newSkImageFromRaster(info, std::move(copiedDataBuffer), |
865 dstPixelBytesPerRow); | 865 dstPixelBytesPerRow); |
866 } | 866 } |
867 if (!skImage) | 867 if (!skImage) |
868 return; | 868 return; |
869 if (data->getSkColorSpace()) { | 869 if (data->skColorSpace()) { |
870 parsedOptions.latestColorSpace = data->getSkColorSpace(); | 870 parsedOptions.latestColorSpace = data->skColorSpace(); |
871 applyColorSpaceConversion(skImage, parsedOptions); | 871 applyColorSpaceConversion(skImage, parsedOptions); |
872 } | 872 } |
873 if (parsedOptions.shouldScaleInput) { | 873 if (parsedOptions.shouldScaleInput) { |
874 m_image = StaticBitmapImage::create( | 874 m_image = StaticBitmapImage::create( |
875 scaleSkImage(skImage, parsedOptions.resizeWidth, | 875 scaleSkImage(skImage, parsedOptions.resizeWidth, |
876 parsedOptions.resizeHeight, parsedOptions.resizeQuality, | 876 parsedOptions.resizeHeight, parsedOptions.resizeQuality, |
877 parsedOptions.latestColorType, data->getSkColorSpace())); | 877 parsedOptions.latestColorType, data->skColorSpace())); |
878 } else { | 878 } else { |
879 m_image = StaticBitmapImage::create(skImage); | 879 m_image = StaticBitmapImage::create(skImage); |
880 } | 880 } |
881 if (!m_image) | 881 if (!m_image) |
882 return; | 882 return; |
883 m_image->setPremultiplied(parsedOptions.premultiplyAlpha); | 883 m_image->setPremultiplied(parsedOptions.premultiplyAlpha); |
884 return; | 884 return; |
885 } | 885 } |
886 | 886 |
887 std::unique_ptr<ImageBuffer> buffer = | 887 std::unique_ptr<ImageBuffer> buffer = |
888 ImageBuffer::create(parsedOptions.cropRect.size(), NonOpaque, | 888 ImageBuffer::create(parsedOptions.cropRect.size(), NonOpaque, |
889 DoNotInitializeImagePixels, data->getSkColorSpace()); | 889 DoNotInitializeImagePixels, data->skColorSpace()); |
890 if (!buffer) | 890 if (!buffer) |
891 return; | 891 return; |
892 | 892 |
893 if (srcRect.isEmpty()) { | 893 if (srcRect.isEmpty()) { |
894 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot( | 894 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot( |
895 PreferNoAcceleration, SnapshotReasonUnknown)); | 895 PreferNoAcceleration, SnapshotReasonUnknown)); |
896 return; | 896 return; |
897 } | 897 } |
898 | 898 |
899 IntPoint dstPoint = IntPoint(std::min(0, -parsedOptions.cropRect.x()), | 899 IntPoint dstPoint = IntPoint(std::min(0, -parsedOptions.cropRect.x()), |
900 std::min(0, -parsedOptions.cropRect.y())); | 900 std::min(0, -parsedOptions.cropRect.y())); |
901 if (parsedOptions.cropRect.x() < 0) | 901 if (parsedOptions.cropRect.x() < 0) |
902 dstPoint.setX(-parsedOptions.cropRect.x()); | 902 dstPoint.setX(-parsedOptions.cropRect.x()); |
903 if (parsedOptions.cropRect.y() < 0) | 903 if (parsedOptions.cropRect.y() < 0) |
904 dstPoint.setY(-parsedOptions.cropRect.y()); | 904 dstPoint.setY(-parsedOptions.cropRect.y()); |
905 | 905 |
906 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), | 906 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), |
907 srcRect, dstPoint); | 907 srcRect, dstPoint); |
908 sk_sp<SkImage> skImage = | 908 sk_sp<SkImage> skImage = |
909 buffer->newSkImageSnapshot(PreferNoAcceleration, SnapshotReasonUnknown); | 909 buffer->newSkImageSnapshot(PreferNoAcceleration, SnapshotReasonUnknown); |
910 if (parsedOptions.flipY) | 910 if (parsedOptions.flipY) |
911 skImage = flipSkImageVertically(skImage.get(), EnforceAlphaPremultiply); | 911 skImage = flipSkImageVertically(skImage.get(), EnforceAlphaPremultiply); |
912 if (!skImage) | 912 if (!skImage) |
913 return; | 913 return; |
914 if (parsedOptions.shouldScaleInput) { | 914 if (parsedOptions.shouldScaleInput) { |
915 sk_sp<SkSurface> surface = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul( | 915 sk_sp<SkSurface> surface = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul( |
916 parsedOptions.resizeWidth, parsedOptions.resizeHeight, | 916 parsedOptions.resizeWidth, parsedOptions.resizeHeight, |
917 data->getSkColorSpace())); | 917 data->skColorSpace())); |
918 if (!surface) | 918 if (!surface) |
919 return; | 919 return; |
920 SkPaint paint; | 920 SkPaint paint; |
921 paint.setFilterQuality(parsedOptions.resizeQuality); | 921 paint.setFilterQuality(parsedOptions.resizeQuality); |
922 SkRect dstDrawRect = | 922 SkRect dstDrawRect = |
923 SkRect::MakeWH(parsedOptions.resizeWidth, parsedOptions.resizeHeight); | 923 SkRect::MakeWH(parsedOptions.resizeWidth, parsedOptions.resizeHeight); |
924 surface->getCanvas()->drawImageRect(skImage, dstDrawRect, &paint); | 924 surface->getCanvas()->drawImageRect(skImage, dstDrawRect, &paint); |
925 skImage = surface->makeImageSnapshot(); | 925 skImage = surface->makeImageSnapshot(); |
926 } | 926 } |
927 if (data->getSkColorSpace()) { | 927 if (data->skColorSpace()) { |
928 parsedOptions.latestColorSpace = data->getSkColorSpace(); | 928 parsedOptions.latestColorSpace = data->skColorSpace(); |
929 applyColorSpaceConversion(skImage, parsedOptions); | 929 applyColorSpaceConversion(skImage, parsedOptions); |
930 } | 930 } |
931 m_image = StaticBitmapImage::create(std::move(skImage)); | 931 m_image = StaticBitmapImage::create(std::move(skImage)); |
932 } | 932 } |
933 | 933 |
934 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, | 934 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, |
935 Optional<IntRect> cropRect, | 935 Optional<IntRect> cropRect, |
936 const ImageBitmapOptions& options) { | 936 const ImageBitmapOptions& options) { |
937 RefPtr<Image> input = bitmap->bitmapImage(); | 937 RefPtr<Image> input = bitmap->bitmapImage(); |
938 if (!input) | 938 if (!input) |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1125 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, | 1125 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, |
1126 FloatRect* dstRect) const {} | 1126 FloatRect* dstRect) const {} |
1127 | 1127 |
1128 FloatSize ImageBitmap::elementSize(const FloatSize&) const { | 1128 FloatSize ImageBitmap::elementSize(const FloatSize&) const { |
1129 return FloatSize(width(), height()); | 1129 return FloatSize(width(), height()); |
1130 } | 1130 } |
1131 | 1131 |
1132 DEFINE_TRACE(ImageBitmap) {} | 1132 DEFINE_TRACE(ImageBitmap) {} |
1133 | 1133 |
1134 } // namespace blink | 1134 } // namespace blink |
OLD | NEW |