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