Chromium Code Reviews| 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/graphics/skia/SkiaUtils.h" | 10 #include "platform/graphics/skia/SkiaUtils.h" |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 | 269 |
| 270 // The parameter imageFormat indicates whether the first parameter "image" is | 270 // The parameter imageFormat indicates whether the first parameter "image" is |
| 271 // unpremultiplied or not. imageFormat = PremultiplyAlpha means the image is in | 271 // unpremultiplied or not. imageFormat = PremultiplyAlpha means the image is in |
| 272 // premuliplied format For example, if the image is already in unpremultiplied | 272 // premuliplied format For example, if the image is already in unpremultiplied |
| 273 // format and we want the created ImageBitmap in the same format, then we don't | 273 // format and we want the created ImageBitmap in the same format, then we don't |
| 274 // need to use the ImageDecoder to decode the image. | 274 // need to use the ImageDecoder to decode the image. |
| 275 static PassRefPtr<StaticBitmapImage> cropImage( | 275 static PassRefPtr<StaticBitmapImage> cropImage( |
| 276 Image* image, | 276 Image* image, |
| 277 const ParsedOptions& parsedOptions, | 277 const ParsedOptions& parsedOptions, |
| 278 AlphaDisposition imageFormat = PremultiplyAlpha, | 278 AlphaDisposition imageFormat = PremultiplyAlpha, |
| 279 ImageDecoder::ColorSpaceOption colorSpaceOp = | 279 bool ignoreColorSpace = false) { |
| 280 ImageDecoder::ColorSpaceTransformed) { | |
| 281 ASSERT(image); | 280 ASSERT(image); |
| 282 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); | 281 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); |
| 283 const IntRect srcRect = intersection(imgRect, parsedOptions.cropRect); | 282 const IntRect srcRect = intersection(imgRect, parsedOptions.cropRect); |
| 284 | 283 |
| 285 // In the case when cropRect doesn't intersect the source image and it | 284 // In the case when cropRect doesn't intersect the source image and it |
| 286 // requires a umpremul image We immediately return a transparent black image | 285 // requires a umpremul image We immediately return a transparent black image |
| 287 // with cropRect.size() | 286 // with cropRect.size() |
| 288 if (srcRect.isEmpty() && !parsedOptions.premultiplyAlpha) { | 287 if (srcRect.isEmpty() && !parsedOptions.premultiplyAlpha) { |
| 289 SkImageInfo info = | 288 SkImageInfo info = |
| 290 SkImageInfo::Make(parsedOptions.resizeWidth, parsedOptions.resizeHeight, | 289 SkImageInfo::Make(parsedOptions.resizeWidth, parsedOptions.resizeHeight, |
| 291 kN32_SkColorType, kUnpremul_SkAlphaType); | 290 kN32_SkColorType, kUnpremul_SkAlphaType); |
| 292 RefPtr<ArrayBuffer> dstBuffer = ArrayBuffer::createOrNull( | 291 RefPtr<ArrayBuffer> dstBuffer = ArrayBuffer::createOrNull( |
| 293 static_cast<unsigned>(info.width()) * info.height(), | 292 static_cast<unsigned>(info.width()) * info.height(), |
| 294 info.bytesPerPixel()); | 293 info.bytesPerPixel()); |
| 295 if (!dstBuffer) | 294 if (!dstBuffer) |
| 296 return nullptr; | 295 return nullptr; |
| 297 RefPtr<Uint8Array> dstPixels = | 296 RefPtr<Uint8Array> dstPixels = |
| 298 Uint8Array::create(dstBuffer, 0, dstBuffer->byteLength()); | 297 Uint8Array::create(dstBuffer, 0, dstBuffer->byteLength()); |
| 299 return StaticBitmapImage::create(newSkImageFromRaster( | 298 return StaticBitmapImage::create(newSkImageFromRaster( |
| 300 info, std::move(dstPixels), | 299 info, std::move(dstPixels), |
| 301 static_cast<unsigned>(info.width()) * info.bytesPerPixel())); | 300 static_cast<unsigned>(info.width()) * info.bytesPerPixel())); |
| 302 } | 301 } |
| 303 | 302 |
| 304 sk_sp<SkImage> skiaImage = image->imageForCurrentFrame(); | 303 sk_sp<SkImage> skiaImage = image->imageForCurrentFrame(); |
| 305 // Attempt to get raw unpremultiplied image data, executed only when skiaImage | 304 // Attempt to get raw unpremultiplied image data, executed only when skiaImage |
| 306 // is premultiplied. | 305 // is premultiplied. |
| 307 if ((((!parsedOptions.premultiplyAlpha && !skiaImage->isOpaque()) || | 306 if ((((!parsedOptions.premultiplyAlpha && !skiaImage->isOpaque()) || |
| 308 !skiaImage) && | 307 !skiaImage) && |
| 309 image->data() && imageFormat == PremultiplyAlpha) || | 308 image->data() && imageFormat == PremultiplyAlpha) || |
| 310 colorSpaceOp == ImageDecoder::ColorSpaceIgnored) { | 309 ignoreColorSpace) { |
| 311 std::unique_ptr<ImageDecoder> decoder(ImageDecoder::create( | 310 std::unique_ptr<ImageDecoder> decoder(ImageDecoder::create( |
| 312 image->data(), true, | 311 image->data(), true, |
| 313 parsedOptions.premultiplyAlpha ? ImageDecoder::AlphaPremultiplied | 312 parsedOptions.premultiplyAlpha ? ImageDecoder::AlphaPremultiplied |
| 314 : ImageDecoder::AlphaNotPremultiplied, | 313 : ImageDecoder::AlphaNotPremultiplied, |
| 315 colorSpaceOp, colorSpaceOp == ImageDecoder::ColorSpaceTransformed | 314 ignoreColorSpace ? ColorBehavior::ignore() |
| 316 ? ImageDecoder::globalTargetColorSpace() | 315 : ColorBehavior::transformToGlobalTarget())); |
| 317 : nullptr)); | |
| 318 if (!decoder) | 316 if (!decoder) |
| 319 return nullptr; | 317 return nullptr; |
| 320 skiaImage = ImageBitmap::getSkImageFromDecoder(std::move(decoder)); | 318 skiaImage = ImageBitmap::getSkImageFromDecoder(std::move(decoder)); |
| 321 if (!skiaImage) | 319 if (!skiaImage) |
| 322 return nullptr; | 320 return nullptr; |
| 323 } | 321 } |
| 324 | 322 |
| 325 if (parsedOptions.cropRect == srcRect && !parsedOptions.shouldScaleInput) { | 323 if (parsedOptions.cropRect == srcRect && !parsedOptions.shouldScaleInput) { |
| 326 sk_sp<SkImage> croppedSkImage = skiaImage->makeSubset(srcRect); | 324 sk_sp<SkImage> croppedSkImage = skiaImage->makeSubset(srcRect); |
| 327 if (parsedOptions.flipY) | 325 if (parsedOptions.flipY) |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 Optional<IntRect> cropRect, | 382 Optional<IntRect> cropRect, |
| 385 Document* document, | 383 Document* document, |
| 386 const ImageBitmapOptions& options) { | 384 const ImageBitmapOptions& options) { |
| 387 RefPtr<Image> input = image->cachedImage()->getImage(); | 385 RefPtr<Image> input = image->cachedImage()->getImage(); |
| 388 ParsedOptions parsedOptions = | 386 ParsedOptions parsedOptions = |
| 389 parseOptions(options, cropRect, image->bitmapSourceSize()); | 387 parseOptions(options, cropRect, image->bitmapSourceSize()); |
| 390 if (dstBufferSizeHasOverflow(parsedOptions)) | 388 if (dstBufferSizeHasOverflow(parsedOptions)) |
| 391 return; | 389 return; |
| 392 | 390 |
| 393 if (options.colorSpaceConversion() == "none") { | 391 if (options.colorSpaceConversion() == "none") { |
| 394 m_image = cropImage(input.get(), parsedOptions, PremultiplyAlpha, | 392 m_image = cropImage(input.get(), parsedOptions, PremultiplyAlpha, true); |
|
pdr.
2016/12/07 19:29:36
I think passing a bool here is a small regression
ccameron
2016/12/07 20:31:38
It'll need to be a ColorBehavior eventually (a fut
| |
| 395 ImageDecoder::ColorSpaceIgnored); | |
| 396 } else { | 393 } else { |
| 397 m_image = cropImage(input.get(), parsedOptions, PremultiplyAlpha, | 394 m_image = cropImage(input.get(), parsedOptions, PremultiplyAlpha, false); |
| 398 ImageDecoder::ColorSpaceTransformed); | |
| 399 } | 395 } |
| 400 if (!m_image) | 396 if (!m_image) |
| 401 return; | 397 return; |
| 402 // In the case where the source image is lazy-decoded, m_image may not be in | 398 // In the case where the source image is lazy-decoded, m_image may not be in |
| 403 // a decoded state, we trigger it here. | 399 // a decoded state, we trigger it here. |
| 404 sk_sp<SkImage> skImage = m_image->imageForCurrentFrame(); | 400 sk_sp<SkImage> skImage = m_image->imageForCurrentFrame(); |
| 405 SkPixmap pixmap; | 401 SkPixmap pixmap; |
| 406 if (!skImage->isTextureBacked() && !skImage->peekPixels(&pixmap)) { | 402 if (!skImage->isTextureBacked() && !skImage->peekPixels(&pixmap)) { |
| 407 sk_sp<SkSurface> surface = | 403 sk_sp<SkSurface> surface = |
| 408 SkSurface::MakeRasterN32Premul(skImage->width(), skImage->height()); | 404 SkSurface::MakeRasterN32Premul(skImage->width(), skImage->height()); |
| (...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 864 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, | 860 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, |
| 865 FloatRect* dstRect) const {} | 861 FloatRect* dstRect) const {} |
| 866 | 862 |
| 867 FloatSize ImageBitmap::elementSize(const FloatSize&) const { | 863 FloatSize ImageBitmap::elementSize(const FloatSize&) const { |
| 868 return FloatSize(width(), height()); | 864 return FloatSize(width(), height()); |
| 869 } | 865 } |
| 870 | 866 |
| 871 DEFINE_TRACE(ImageBitmap) {} | 867 DEFINE_TRACE(ImageBitmap) {} |
| 872 | 868 |
| 873 } // namespace blink | 869 } // namespace blink |
| OLD | NEW |