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 "platform/image-decoders/ImageDecoder.h" | |
10 #include "third_party/skia/include/core/SkSurface.h" | 11 #include "third_party/skia/include/core/SkSurface.h" |
11 #include "wtf/RefPtr.h" | 12 #include "wtf/RefPtr.h" |
12 | 13 |
13 namespace blink { | 14 namespace blink { |
14 | 15 |
15 static const char* imageOrientationFlipY = "flipY"; | 16 static const char* imageOrientationFlipY = "flipY"; |
17 static const char* imageBitmapOptionNone = "none"; | |
16 | 18 |
19 static void parseOptions(const ImageBitmapOptions& options, bool& imageOrientati onFlipYFlag, bool& premultiplyAlphaEnabledFlag) | |
20 { | |
21 if (options.imageOrientation() == imageOrientationFlipY) | |
22 imageOrientationFlipYFlag = true; | |
23 else | |
24 imageOrientationFlipYFlag = false; | |
Justin Novosad
2016/02/05 14:46:41
ASSERT(options.imageOrientation() == imageBitmapOp
xidachen
2016/02/05 16:47:36
Acknowledged.
| |
25 if (options.premultiplyAlpha() == imageBitmapOptionNone) | |
26 premultiplyAlphaEnabledFlag = false; | |
27 else | |
28 premultiplyAlphaEnabledFlag = true; | |
Justin Novosad
2016/02/05 14:46:41
ASSERT(options.premultiplyAlpha() == premultiplyAl
xidachen
2016/02/05 16:47:36
Acknowledged.
| |
29 } | |
30 | |
31 // The following two functions are helpers used in cropImage | |
17 static inline IntRect normalizeRect(const IntRect& rect) | 32 static inline IntRect normalizeRect(const IntRect& rect) |
18 { | 33 { |
19 return IntRect(std::min(rect.x(), rect.maxX()), | 34 return IntRect(std::min(rect.x(), rect.maxX()), |
20 std::min(rect.y(), rect.maxY()), | 35 std::min(rect.y(), rect.maxY()), |
21 std::max(rect.width(), -rect.width()), | 36 std::max(rect.width(), -rect.width()), |
22 std::max(rect.height(), -rect.height())); | 37 std::max(rect.height(), -rect.height())); |
23 } | 38 } |
24 | 39 |
25 // TODO(xidachen): this function needs to be changed later on when implementing premultiplyAlpha option | 40 static bool frameIsValid(const SkBitmap& frameBitmap) |
41 { | |
42 return !frameBitmap.isNull() | |
43 && !frameBitmap.empty() | |
44 && frameBitmap.isImmutable() | |
45 && frameBitmap.colorType() == kN32_SkColorType; | |
Justin Novosad
2016/02/05 14:46:41
Should some of these conditions be ASSERTs (i.e. t
xidachen
2016/02/05 16:47:36
Acknowledged.
| |
46 } | |
47 | |
26 static SkImage* flipSkImageVertically(SkImage* input) | 48 static SkImage* flipSkImageVertically(SkImage* input) |
27 { | 49 { |
28 int width = input->width(); | 50 int width = input->width(); |
29 int height = input->height(); | 51 int height = input->height(); |
30 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); | 52 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
31 OwnPtr<uint8_t[]> imagePixels = adoptArrayPtr(new uint8_t[width * height * i nfo.bytesPerPixel()]); | 53 OwnPtr<uint8_t[]> imagePixels = adoptArrayPtr(new uint8_t[width * height * i nfo.bytesPerPixel()]); |
32 int imageRowBytes = info.bytesPerPixel() * width; | 54 int imageRowBytes = info.bytesPerPixel() * width; |
33 input->readPixels(info, imagePixels.get(), imageRowBytes, 0, 0); | 55 input->readPixels(info, imagePixels.get(), imageRowBytes, 0, 0); |
34 | 56 |
35 for (int i = 0; i < height / 2; i++) { | 57 for (int i = 0; i < height / 2; i++) { |
36 int topFirstElement = i * imageRowBytes; | 58 int topFirstElement = i * imageRowBytes; |
37 int topLastElement = (i + 1) * imageRowBytes; | 59 int topLastElement = (i + 1) * imageRowBytes; |
38 int bottomFirstElement = (height - 1 - i) * imageRowBytes; | 60 int bottomFirstElement = (height - 1 - i) * imageRowBytes; |
39 std::swap_ranges(imagePixels.get() + topFirstElement, imagePixels.get() + topLastElement, imagePixels.get() + bottomFirstElement); | 61 std::swap_ranges(imagePixels.get() + topFirstElement, imagePixels.get() + topLastElement, imagePixels.get() + bottomFirstElement); |
40 } | 62 } |
41 return SkImage::NewRasterCopy(info, imagePixels.get(), imageRowBytes); | 63 return SkImage::NewRasterCopy(info, imagePixels.get(), imageRowBytes); |
42 } | 64 } |
43 | 65 |
44 static PassRefPtr<StaticBitmapImage> cropImage(Image* image, const IntRect& crop Rect, bool flipYEnabled) | 66 // TODO(xidachen): the part of read an SkImage to a OwnPtr<uint8_t[]> has been u sed in multiple places, |
67 // we should write it as a utility function here. | |
68 static SkImage* premulSkImageToUnPremul(SkImage* input) | |
69 { | |
70 int width = input->width(); | |
71 int height = input->height(); | |
72 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kUnpre mul_SkAlphaType); | |
73 OwnPtr<uint8_t[]> dstPixels = adoptArrayPtr(new uint8_t[width * height * inf o.bytesPerPixel()]); | |
74 size_t dstRowBytes = info.bytesPerPixel() * width; | |
75 input->readPixels(info, dstPixels.get(), dstRowBytes, 0, 0); | |
76 return SkImage::NewRasterCopy(info, dstPixels.get(), dstRowBytes); | |
77 } | |
78 | |
79 static PassRefPtr<StaticBitmapImage> cropImage(Image* image, const IntRect& crop Rect, bool flipYEnabled, bool premultiplyAlphaEnabled) | |
45 { | 80 { |
46 ASSERT(image); | 81 ASSERT(image); |
47 | 82 |
48 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); | 83 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); |
49 const IntRect srcRect = intersection(imgRect, cropRect); | 84 const IntRect srcRect = intersection(imgRect, cropRect); |
50 | 85 |
86 RefPtr<SkImage> skiaImage = image->imageForCurrentFrame(); | |
87 // Attempt to get raw unpremultiplied image data. | |
88 if (((!premultiplyAlphaEnabled && !skiaImage->isOpaque()) || !skiaImage) && image->data()) { | |
89 // TODO(xidachen): GammaAndColorProfileApplied needs to be changed when working on color-space conversion | |
90 OwnPtr<ImageDecoder> decoder(ImageDecoder::create( | |
91 *(image->data()), ImageDecoder::AlphaNotPremultiplied, | |
92 ImageDecoder::GammaAndColorProfileApplied)); | |
93 if (!decoder) | |
94 return nullptr; | |
95 decoder->setData(image->data(), true); | |
96 if (!decoder->frameCount()) | |
97 return nullptr; | |
98 ImageFrame* frame = decoder->frameBufferAtIndex(0); | |
99 if (!frame || frame->status() != ImageFrame::FrameComplete) | |
100 return nullptr; | |
101 SkBitmap bitmap = frame->bitmap(); | |
102 if (!frameIsValid(bitmap)) | |
103 return nullptr; | |
104 ASSERT(bitmap.isImmutable()); | |
105 skiaImage = adoptRef(SkImage::NewFromBitmap(bitmap)); | |
106 } | |
107 | |
51 if (cropRect == srcRect) { | 108 if (cropRect == srcRect) { |
52 if (flipYEnabled) | 109 if (flipYEnabled) |
53 return StaticBitmapImage::create(adoptRef(flipSkImageVertically(imag e->imageForCurrentFrame()->newSubset(srcRect)))); | 110 return StaticBitmapImage::create(adoptRef(flipSkImageVertically(skia Image->newSubset(srcRect)))); |
54 return StaticBitmapImage::create(adoptRef(image->imageForCurrentFrame()- >newSubset(srcRect))); | 111 return StaticBitmapImage::create(adoptRef(skiaImage->newSubset(srcRect)) ); |
55 } | 112 } |
56 | 113 |
57 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect. width(), cropRect.height())); | 114 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect. width(), cropRect.height())); |
58 | 115 // In the case where cropRect doesn't intesect the source image, we return a premultiplied transparent black SkImage. |
116 // If we decide we want to grab meta data from m_image, we have to change th is. | |
59 if (srcRect.isEmpty()) | 117 if (srcRect.isEmpty()) |
60 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); | 118 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); |
61 | 119 |
62 SkScalar dstLeft = std::min(0, -cropRect.x()); | 120 SkScalar dstLeft = std::min(0, -cropRect.x()); |
63 SkScalar dstTop = std::min(0, -cropRect.y()); | 121 SkScalar dstTop = std::min(0, -cropRect.y()); |
64 if (cropRect.x() < 0) | 122 if (cropRect.x() < 0) |
65 dstLeft = -cropRect.x(); | 123 dstLeft = -cropRect.x(); |
66 if (cropRect.y() < 0) | 124 if (cropRect.y() < 0) |
67 dstTop = -cropRect.y(); | 125 dstTop = -cropRect.y(); |
68 surface->getCanvas()->drawImage(image->imageForCurrentFrame().get(), dstLeft , dstTop); | 126 surface->getCanvas()->drawImage(skiaImage.get(), dstLeft, dstTop); |
69 if (flipYEnabled) | 127 if (flipYEnabled) |
70 return StaticBitmapImage::create(adoptRef(flipSkImageVertically(surface- >newImageSnapshot()))); | 128 skiaImage = adoptRef(flipSkImageVertically(surface->newImageSnapshot())) ; |
71 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); | 129 else |
130 skiaImage = adoptRef(surface->newImageSnapshot()); | |
131 if (premultiplyAlphaEnabled) | |
132 return StaticBitmapImage::create(skiaImage); | |
133 return StaticBitmapImage::create(adoptRef(premulSkImageToUnPremul(skiaImage. get()))); | |
72 } | 134 } |
73 | 135 |
74 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) | 136 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) |
75 { | 137 { |
76 if (options.imageOrientation() == imageOrientationFlipY) | 138 bool imageOrientationFlipYFlag; |
77 m_image = cropImage(image->cachedImage()->image(), cropRect, true); | 139 bool premultiplyAlphaEnabledFlag; |
78 else | 140 parseOptions(options, imageOrientationFlipYFlag, premultiplyAlphaEnabledFlag ); |
79 m_image = cropImage(image->cachedImage()->image(), cropRect, false); | 141 |
142 m_image = cropImage(image->cachedImage()->image(), cropRect, imageOrientatio nFlipYFlag, premultiplyAlphaEnabledFlag); | |
80 m_image->setOriginClean(!image->wouldTaintOrigin(document->securityOrigin()) ); | 143 m_image->setOriginClean(!image->wouldTaintOrigin(document->securityOrigin()) ); |
81 } | 144 } |
82 | 145 |
83 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) | 146 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) |
84 { | 147 { |
85 IntSize playerSize; | 148 IntSize playerSize; |
86 if (video->webMediaPlayer()) | 149 if (video->webMediaPlayer()) |
87 playerSize = video->webMediaPlayer()->naturalSize(); | 150 playerSize = video->webMediaPlayer()->naturalSize(); |
88 | 151 |
89 IntRect videoRect = IntRect(IntPoint(), playerSize); | 152 IntRect videoRect = IntRect(IntPoint(), playerSize); |
90 IntRect srcRect = intersection(cropRect, videoRect); | 153 IntRect srcRect = intersection(cropRect, videoRect); |
91 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); | 154 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); |
92 if (!buffer) | 155 if (!buffer) |
93 return; | 156 return; |
94 | 157 |
95 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe ct.y())); | 158 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe ct.y())); |
96 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size()) , nullptr); | 159 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size()) , nullptr); |
97 if (options.imageOrientation() == imageOrientationFlipY) | 160 if (options.imageOrientation() == imageOrientationFlipY) |
98 m_image = StaticBitmapImage::create(adoptRef(flipSkImageVertically(buffe r->newSkImageSnapshot(PreferNoAcceleration).get()))); | 161 m_image = StaticBitmapImage::create(adoptRef(flipSkImageVertically(buffe r->newSkImageSnapshot(PreferNoAcceleration).get()))); |
99 else | 162 else |
100 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration)); | 163 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration)); |
101 m_image->setOriginClean(!video->wouldTaintOrigin(document->securityOrigin()) ); | 164 m_image->setOriginClean(!video->wouldTaintOrigin(document->securityOrigin()) ); |
102 } | 165 } |
103 | 166 |
104 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect, con st ImageBitmapOptions& options) | 167 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect, con st ImageBitmapOptions& options) |
105 { | 168 { |
106 ASSERT(canvas->isPaintable()); | 169 ASSERT(canvas->isPaintable()); |
107 if (options.imageOrientation() == imageOrientationFlipY) | 170 bool imageOrientationFlipYFlag; |
108 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration). get(), cropRect, true); | 171 bool premultiplyAlphaEnabledFlag; |
109 else | 172 parseOptions(options, imageOrientationFlipYFlag, premultiplyAlphaEnabledFlag ); |
110 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration). get(), cropRect, false); | 173 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration).get( ), cropRect, imageOrientationFlipYFlag, premultiplyAlphaEnabledFlag); |
111 m_image->setOriginClean(canvas->originClean()); | 174 m_image->setOriginClean(canvas->originClean()); |
112 } | 175 } |
113 | 176 |
114 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect, const ImageBi tmapOptions& options) | 177 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect, const ImageBi tmapOptions& options) |
115 { | 178 { |
116 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 179 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
117 | 180 |
118 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); | 181 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); |
119 if (!buffer) | 182 if (!buffer) |
120 return; | 183 return; |
(...skipping 10 matching lines...) Expand all Loading... | |
131 dstPoint.setY(-cropRect.y()); | 194 dstPoint.setY(-cropRect.y()); |
132 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe ct, dstPoint); | 195 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe ct, dstPoint); |
133 if (options.imageOrientation() == imageOrientationFlipY) | 196 if (options.imageOrientation() == imageOrientationFlipY) |
134 m_image = StaticBitmapImage::create(adoptRef(flipSkImageVertically(buffe r->newSkImageSnapshot(PreferNoAcceleration).get()))); | 197 m_image = StaticBitmapImage::create(adoptRef(flipSkImageVertically(buffe r->newSkImageSnapshot(PreferNoAcceleration).get()))); |
135 else | 198 else |
136 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration)); | 199 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration)); |
137 } | 200 } |
138 | 201 |
139 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect, const Ima geBitmapOptions& options) | 202 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect, const Ima geBitmapOptions& options) |
140 { | 203 { |
141 if (options.imageOrientation() == imageOrientationFlipY) | 204 bool imageOrientationFlipYFlag; |
142 m_image = cropImage(bitmap->bitmapImage(), cropRect, true); | 205 bool premultiplyAlphaEnabledFlag; |
143 else | 206 parseOptions(options, imageOrientationFlipYFlag, premultiplyAlphaEnabledFlag ); |
144 m_image = cropImage(bitmap->bitmapImage(), cropRect, false); | 207 m_image = cropImage(bitmap->bitmapImage(), cropRect, imageOrientationFlipYFl ag, true); |
145 m_image->setOriginClean(bitmap->originClean()); | 208 m_image->setOriginClean(bitmap->originClean()); |
146 } | 209 } |
147 | 210 |
148 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image, const IntRect& cro pRect, const ImageBitmapOptions& options) | 211 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image, const IntRect& cro pRect, const ImageBitmapOptions& options) |
149 { | 212 { |
150 if (options.imageOrientation() == imageOrientationFlipY) | 213 bool imageOrientationFlipYFlag; |
151 m_image = cropImage(image.get(), cropRect, true); | 214 bool premultiplyAlphaEnabledFlag; |
152 else | 215 parseOptions(options, imageOrientationFlipYFlag, premultiplyAlphaEnabledFlag ); |
153 m_image = cropImage(image.get(), cropRect, false); | 216 m_image = cropImage(image.get(), cropRect, imageOrientationFlipYFlag, premul tiplyAlphaEnabledFlag); |
154 m_image->setOriginClean(image->originClean()); | 217 m_image->setOriginClean(image->originClean()); |
155 } | 218 } |
156 | 219 |
157 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image) | 220 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image) |
158 { | 221 { |
159 m_image = image; | 222 m_image = image; |
160 } | 223 } |
161 | 224 |
162 PassRefPtr<StaticBitmapImage> ImageBitmap::transfer() | 225 PassRefPtr<StaticBitmapImage> ImageBitmap::transfer() |
163 { | 226 { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 { | 342 { |
280 return FloatSize(width(), height()); | 343 return FloatSize(width(), height()); |
281 } | 344 } |
282 | 345 |
283 DEFINE_TRACE(ImageBitmap) | 346 DEFINE_TRACE(ImageBitmap) |
284 { | 347 { |
285 ImageLoaderClient::trace(visitor); | 348 ImageLoaderClient::trace(visitor); |
286 } | 349 } |
287 | 350 |
288 } // namespace blink | 351 } // namespace blink |
OLD | NEW |