| 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 "platform/image-decoders/ImageDecoder.h" |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 return nullptr; | 95 return nullptr; |
| 96 ImageFrame* frame = decoder->frameBufferAtIndex(0); | 96 ImageFrame* frame = decoder->frameBufferAtIndex(0); |
| 97 if (!frame || frame->getStatus() != ImageFrame::FrameComplete) | 97 if (!frame || frame->getStatus() != ImageFrame::FrameComplete) |
| 98 return nullptr; | 98 return nullptr; |
| 99 SkBitmap bitmap = frame->bitmap(); | 99 SkBitmap bitmap = frame->bitmap(); |
| 100 if (!frameIsValid(bitmap)) | 100 if (!frameIsValid(bitmap)) |
| 101 return nullptr; | 101 return nullptr; |
| 102 return adoptRef(SkImage::NewFromBitmap(bitmap)); | 102 return adoptRef(SkImage::NewFromBitmap(bitmap)); |
| 103 } | 103 } |
| 104 | 104 |
| 105 static PassRefPtr<StaticBitmapImage> cropImage(Image* image, const IntRect& crop
Rect, bool flipY, bool premultiplyAlpha, bool isBitmapPremultiplied = true) | 105 static PassRefPtr<StaticBitmapImage> cropImage(Image* image, const IntRect& crop
Rect, bool flipY, bool premultiplyAlpha, AlphaDisposition alphaOp = DontPremulti
plyAlpha) |
| 106 { | 106 { |
| 107 ASSERT(image); | 107 ASSERT(image); |
| 108 | 108 |
| 109 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); | 109 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); |
| 110 const IntRect srcRect = intersection(imgRect, cropRect); | 110 const IntRect srcRect = intersection(imgRect, cropRect); |
| 111 | 111 |
| 112 // In the case when cropRect doesn't intersect the source image and it requi
res a umpremul image | 112 // In the case when cropRect doesn't intersect the source image and it requi
res a umpremul image |
| 113 // We immediately return a transparent black image with cropRect.size() | 113 // We immediately return a transparent black image with cropRect.size() |
| 114 if (srcRect.isEmpty() && !premultiplyAlpha) { | 114 if (srcRect.isEmpty() && !premultiplyAlpha) { |
| 115 SkImageInfo info = SkImageInfo::Make(cropRect.width(), cropRect.height()
, kN32_SkColorType, kUnpremul_SkAlphaType); | 115 SkImageInfo info = SkImageInfo::Make(cropRect.width(), cropRect.height()
, kN32_SkColorType, kUnpremul_SkAlphaType); |
| 116 OwnPtr<uint8_t[]> dstPixels = adoptArrayPtr(new uint8_t[cropRect.width()
* cropRect.height() * info.bytesPerPixel()]()); | 116 OwnPtr<uint8_t[]> dstPixels = adoptArrayPtr(new uint8_t[cropRect.width()
* cropRect.height() * info.bytesPerPixel()]()); |
| 117 return StaticBitmapImage::create(newSkImageFromRaster(info, dstPixels.re
lease(), cropRect.width() * info.bytesPerPixel())); | 117 return StaticBitmapImage::create(newSkImageFromRaster(info, dstPixels.re
lease(), cropRect.width() * info.bytesPerPixel())); |
| 118 } | 118 } |
| 119 | 119 |
| 120 RefPtr<SkImage> skiaImage = image->imageForCurrentFrame(); | 120 RefPtr<SkImage> skiaImage = image->imageForCurrentFrame(); |
| 121 // Attempt to get raw unpremultiplied image data, executed only when skiaIma
ge is premultiplied. | 121 // Attempt to get raw unpremultiplied image data, executed only when skiaIma
ge is premultiplied. |
| 122 if (((!premultiplyAlpha && !skiaImage->isOpaque()) || !skiaImage) && image->
data() && isBitmapPremultiplied) { | 122 if (((!premultiplyAlpha && !skiaImage->isOpaque()) || !skiaImage) && image->
data() && alphaOp == DontPremultiplyAlpha) { |
| 123 // TODO(xidachen): GammaAndColorProfileApplied needs to be changed when
working on color-space conversion | 123 // TODO(xidachen): GammaAndColorProfileApplied needs to be changed when
working on color-space conversion |
| 124 OwnPtr<ImageDecoder> decoder(ImageDecoder::create( | 124 OwnPtr<ImageDecoder> decoder(ImageDecoder::create( |
| 125 *(image->data()), ImageDecoder::AlphaNotPremultiplied, | 125 *(image->data()), ImageDecoder::AlphaNotPremultiplied, |
| 126 ImageDecoder::GammaAndColorProfileApplied)); | 126 ImageDecoder::GammaAndColorProfileApplied)); |
| 127 if (!decoder) | 127 if (!decoder) |
| 128 return nullptr; | 128 return nullptr; |
| 129 decoder->setData(image->data(), true); | 129 decoder->setData(image->data(), true); |
| 130 skiaImage = ImageBitmap::getSkImageFromDecoder(decoder.release()); | 130 skiaImage = ImageBitmap::getSkImageFromDecoder(decoder.release()); |
| 131 if (!skiaImage) | 131 if (!skiaImage) |
| 132 return nullptr; | 132 return nullptr; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 158 if (premultiplyAlpha) | 158 if (premultiplyAlpha) |
| 159 return StaticBitmapImage::create(skiaImage.release()); | 159 return StaticBitmapImage::create(skiaImage.release()); |
| 160 return StaticBitmapImage::create(premulSkImageToUnPremul(skiaImage.get())); | 160 return StaticBitmapImage::create(premulSkImageToUnPremul(skiaImage.get())); |
| 161 } | 161 } |
| 162 | 162 |
| 163 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect, Docum
ent* document, const ImageBitmapOptions& options) | 163 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect, Docum
ent* document, const ImageBitmapOptions& options) |
| 164 { | 164 { |
| 165 bool flipY; | 165 bool flipY; |
| 166 parseOptions(options, flipY); | 166 parseOptions(options, flipY); |
| 167 | 167 |
| 168 m_image = cropImage(image->cachedImage()->image(), cropRect, flipY, m_isPrem
ultiplied); | 168 m_image = cropImage(image->cachedImage()->image(), cropRect, flipY, m_isPrem
ultiplied, PremultiplyAlpha); |
| 169 if (!m_image) | 169 if (!m_image) |
| 170 return; | 170 return; |
| 171 m_image->setOriginClean(!image->wouldTaintOrigin(document->securityOrigin())
); | 171 m_image->setOriginClean(!image->wouldTaintOrigin(document->securityOrigin())
); |
| 172 } | 172 } |
| 173 | 173 |
| 174 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect, Docum
ent* document, const ImageBitmapOptions& options) | 174 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect, Docum
ent* document, const ImageBitmapOptions& options) |
| 175 { | 175 { |
| 176 IntSize playerSize; | 176 IntSize playerSize; |
| 177 if (video->webMediaPlayer()) | 177 if (video->webMediaPlayer()) |
| 178 playerSize = video->webMediaPlayer()->naturalSize(); | 178 playerSize = video->webMediaPlayer()->naturalSize(); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 if (flipY) | 290 if (flipY) |
| 291 m_image = StaticBitmapImage::create(flipSkImageVertically(buffer->newSkI
mageSnapshot(PreferNoAcceleration, SnapshotReasonUnknown).get())); | 291 m_image = StaticBitmapImage::create(flipSkImageVertically(buffer->newSkI
mageSnapshot(PreferNoAcceleration, SnapshotReasonUnknown).get())); |
| 292 else | 292 else |
| 293 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA
cceleration, SnapshotReasonUnknown)); | 293 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA
cceleration, SnapshotReasonUnknown)); |
| 294 } | 294 } |
| 295 | 295 |
| 296 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect, const Ima
geBitmapOptions& options) | 296 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect, const Ima
geBitmapOptions& options) |
| 297 { | 297 { |
| 298 bool flipY; | 298 bool flipY; |
| 299 parseOptions(options, flipY); | 299 parseOptions(options, flipY); |
| 300 m_image = cropImage(bitmap->bitmapImage(), cropRect, flipY, m_isPremultiplie
d, bitmap->isPremultiplied()); | 300 m_image = cropImage(bitmap->bitmapImage(), cropRect, flipY, m_isPremultiplie
d, bitmap->isPremultiplied() ? DontPremultiplyAlpha : PremultiplyAlpha); |
| 301 if (!m_image) | 301 if (!m_image) |
| 302 return; | 302 return; |
| 303 m_image->setOriginClean(bitmap->originClean()); | 303 m_image->setOriginClean(bitmap->originClean()); |
| 304 } | 304 } |
| 305 | 305 |
| 306 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image, const IntRect& cro
pRect, const ImageBitmapOptions& options) | 306 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image, const IntRect& cro
pRect, const ImageBitmapOptions& options) |
| 307 { | 307 { |
| 308 bool flipY; | 308 bool flipY; |
| 309 parseOptions(options, flipY); | 309 parseOptions(options, flipY); |
| 310 m_image = cropImage(image.get(), cropRect, flipY, m_isPremultiplied); | 310 m_image = cropImage(image.get(), cropRect, flipY, m_isPremultiplied); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 FloatSize ImageBitmap::elementSize() const | 446 FloatSize ImageBitmap::elementSize() const |
| 447 { | 447 { |
| 448 return FloatSize(width(), height()); | 448 return FloatSize(width(), height()); |
| 449 } | 449 } |
| 450 | 450 |
| 451 DEFINE_TRACE(ImageBitmap) | 451 DEFINE_TRACE(ImageBitmap) |
| 452 { | 452 { |
| 453 } | 453 } |
| 454 | 454 |
| 455 } // namespace blink | 455 } // namespace blink |
| OLD | NEW |