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 13 matching lines...) Expand all Loading... | |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 struct ParsedOptions { | 26 struct ParsedOptions { |
| 27 bool flipY = false; | 27 bool flipY = false; |
| 28 bool premultiplyAlpha = true; | 28 bool premultiplyAlpha = true; |
| 29 bool shouldScaleInput = false; | 29 bool shouldScaleInput = false; |
| 30 unsigned resizeWidth = 0; | 30 unsigned resizeWidth = 0; |
| 31 unsigned resizeHeight = 0; | 31 unsigned resizeHeight = 0; |
| 32 IntRect cropRect; | 32 IntRect cropRect; |
| 33 SkFilterQuality resizeQuality = kLow_SkFilterQuality; | 33 SkFilterQuality resizeQuality = kLow_SkFilterQuality; |
| 34 // This value should be changed in the future when we support | |
| 35 // createImageBitmap with higher bit depth, in the parseOptions() function. | |
| 36 // For now, it is always 4. | |
| 37 int bytesPerPixel = 4; | 34 int bytesPerPixel = 4; |
| 35 sk_sp<SkColorSpace> dstColorSpace = nullptr; | |
| 36 SkColorType dstColorType = SkColorType::kUnknown_SkColorType; | |
| 38 }; | 37 }; |
| 39 | 38 |
| 40 // The following two functions are helpers used in cropImage | 39 // The following two functions are helpers used in cropImage |
| 41 static inline IntRect normalizeRect(const IntRect& rect) { | 40 static inline IntRect normalizeRect(const IntRect& rect) { |
| 42 return IntRect(std::min(rect.x(), rect.maxX()), | 41 return IntRect(std::min(rect.x(), rect.maxX()), |
| 43 std::min(rect.y(), rect.maxY()), | 42 std::min(rect.y(), rect.maxY()), |
| 44 std::max(rect.width(), -rect.width()), | 43 std::max(rect.width(), -rect.width()), |
| 45 std::max(rect.height(), -rect.height())); | 44 std::max(rect.height(), -rect.height())); |
| 46 } | 45 } |
| 47 | 46 |
| 48 ParsedOptions parseOptions(const ImageBitmapOptions& options, | 47 ParsedOptions parseOptions(const ImageBitmapOptions& options, |
| 49 Optional<IntRect> cropRect, | 48 Optional<IntRect> cropRect, |
| 50 IntSize sourceSize) { | 49 IntSize sourceSize) { |
| 51 ParsedOptions parsedOptions; | 50 ParsedOptions parsedOptions; |
| 52 if (options.imageOrientation() == imageOrientationFlipY) { | 51 if (options.imageOrientation() == imageOrientationFlipY) { |
| 53 parsedOptions.flipY = true; | 52 parsedOptions.flipY = true; |
| 54 } else { | 53 } else { |
| 55 parsedOptions.flipY = false; | 54 parsedOptions.flipY = false; |
| 56 DCHECK(options.imageOrientation() == imageBitmapOptionNone); | 55 DCHECK(options.imageOrientation() == imageBitmapOptionNone); |
| 57 } | 56 } |
| 58 if (options.premultiplyAlpha() == imageBitmapOptionNone) { | 57 if (options.premultiplyAlpha() == imageBitmapOptionNone) { |
| 59 parsedOptions.premultiplyAlpha = false; | 58 parsedOptions.premultiplyAlpha = false; |
| 60 } else { | 59 } else { |
| 61 parsedOptions.premultiplyAlpha = true; | 60 parsedOptions.premultiplyAlpha = true; |
| 62 DCHECK(options.premultiplyAlpha() == "default" || | 61 DCHECK(options.premultiplyAlpha() == "default" || |
| 63 options.premultiplyAlpha() == "premultiply"); | 62 options.premultiplyAlpha() == "premultiply"); |
| 64 } | 63 } |
| 65 | 64 |
| 65 if (options.colorSpaceConversion() != "none") { | |
| 66 if (!RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled() || | |
| 67 !RuntimeEnabledFeatures::colorCorrectRenderingEnabled()) { | |
| 68 DCHECK_EQ(options.colorSpaceConversion(), "default"); | |
| 69 //parsedOptions.dstColorSpace = ImageDecoder::globalTargetColorSpace(); | |
|
zakerinasab
2016/11/24 20:13:42
@ccameron Can you take a look at this? When I do t
xidachen
2016/11/24 20:27:31
I am fine with setting this to SkColorSpace::MakeN
zakerinasab
2016/11/24 20:51:12
Done.
| |
| 70 parsedOptions.dstColorSpace = | |
| 71 SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); | |
| 72 parsedOptions.dstColorType = SkColorType::kN32_SkColorType; | |
| 73 } else { | |
| 74 if (options.colorSpaceConversion() == "default" || | |
| 75 options.colorSpaceConversion() == "srgb") { | |
| 76 parsedOptions.dstColorSpace = | |
| 77 SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); | |
| 78 parsedOptions.dstColorType = SkColorType::kN32_SkColorType; | |
| 79 } else if (options.colorSpaceConversion() == "linear-rgb") { | |
| 80 parsedOptions.bytesPerPixel = 8; | |
| 81 parsedOptions.dstColorSpace = | |
| 82 SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named); | |
| 83 parsedOptions.dstColorType = SkColorType::kRGBA_F16_SkColorType; | |
| 84 } else { | |
| 85 NOTREACHED() | |
| 86 << "Invalid ImageBitmap creation attribute colorSpaceConversion: " | |
| 87 << options.colorSpaceConversion(); | |
| 88 } | |
| 89 } | |
| 90 } | |
| 91 | |
| 66 int sourceWidth = sourceSize.width(); | 92 int sourceWidth = sourceSize.width(); |
| 67 int sourceHeight = sourceSize.height(); | 93 int sourceHeight = sourceSize.height(); |
| 68 if (!cropRect) { | 94 if (!cropRect) { |
| 69 parsedOptions.cropRect = IntRect(0, 0, sourceWidth, sourceHeight); | 95 parsedOptions.cropRect = IntRect(0, 0, sourceWidth, sourceHeight); |
| 70 } else { | 96 } else { |
| 71 parsedOptions.cropRect = normalizeRect(*cropRect); | 97 parsedOptions.cropRect = normalizeRect(*cropRect); |
| 72 } | 98 } |
| 73 if (!options.hasResizeWidth() && !options.hasResizeHeight()) { | 99 if (!options.hasResizeWidth() && !options.hasResizeHeight()) { |
| 74 parsedOptions.resizeWidth = parsedOptions.cropRect.width(); | 100 parsedOptions.resizeWidth = parsedOptions.cropRect.width(); |
| 75 parsedOptions.resizeHeight = parsedOptions.cropRect.height(); | 101 parsedOptions.resizeHeight = parsedOptions.cropRect.height(); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 SkImageInfo info = SkImageInfo::Make(input->width(), input->height(), | 251 SkImageInfo info = SkImageInfo::Make(input->width(), input->height(), |
| 226 kN32_SkColorType, kPremul_SkAlphaType); | 252 kN32_SkColorType, kPremul_SkAlphaType); |
| 227 RefPtr<Uint8Array> dstPixels = copySkImageData(input, info); | 253 RefPtr<Uint8Array> dstPixels = copySkImageData(input, info); |
| 228 if (!dstPixels) | 254 if (!dstPixels) |
| 229 return nullptr; | 255 return nullptr; |
| 230 return newSkImageFromRaster( | 256 return newSkImageFromRaster( |
| 231 info, std::move(dstPixels), | 257 info, std::move(dstPixels), |
| 232 static_cast<unsigned>(input->width()) * info.bytesPerPixel()); | 258 static_cast<unsigned>(input->width()) * info.bytesPerPixel()); |
| 233 } | 259 } |
| 234 | 260 |
| 261 static sk_sp<SkImage> applyColorSpaceConversion( | |
| 262 sk_sp<SkImage> image, | |
| 263 const ParsedOptions& parsedOptions) { | |
| 264 if (!parsedOptions.dstColorSpace) | |
| 265 return image; | |
| 266 | |
| 267 SkImageInfo imageInfo = SkImageInfo::Make( | |
| 268 image->width(), image->height(), parsedOptions.dstColorType, | |
| 269 image->alphaType(), parsedOptions.dstColorSpace); | |
| 270 sk_sp<SkSurface> surface = SkSurface::MakeRaster(imageInfo); | |
| 271 surface->getCanvas()->drawImage(image, 0, 0); | |
| 272 return surface->makeImageSnapshot(); | |
| 273 } | |
| 274 | |
| 235 sk_sp<SkImage> ImageBitmap::getSkImageFromDecoder( | 275 sk_sp<SkImage> ImageBitmap::getSkImageFromDecoder( |
| 236 std::unique_ptr<ImageDecoder> decoder) { | 276 std::unique_ptr<ImageDecoder> decoder) { |
| 237 if (!decoder->frameCount()) | 277 if (!decoder->frameCount()) |
| 238 return nullptr; | 278 return nullptr; |
| 239 ImageFrame* frame = decoder->frameBufferAtIndex(0); | 279 ImageFrame* frame = decoder->frameBufferAtIndex(0); |
| 240 if (!frame || frame->getStatus() != ImageFrame::FrameComplete) | 280 if (!frame || frame->getStatus() != ImageFrame::FrameComplete) |
| 241 return nullptr; | 281 return nullptr; |
| 242 DCHECK(!frame->bitmap().isNull() && !frame->bitmap().empty()); | 282 DCHECK(!frame->bitmap().isNull() && !frame->bitmap().empty()); |
| 243 return frame->finalizePixelsAndGetImage(); | 283 return frame->finalizePixelsAndGetImage(); |
| 244 } | 284 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 315 colorSpaceOp)); | 355 colorSpaceOp)); |
| 316 if (!decoder) | 356 if (!decoder) |
| 317 return nullptr; | 357 return nullptr; |
| 318 skiaImage = ImageBitmap::getSkImageFromDecoder(std::move(decoder)); | 358 skiaImage = ImageBitmap::getSkImageFromDecoder(std::move(decoder)); |
| 319 if (!skiaImage) | 359 if (!skiaImage) |
| 320 return nullptr; | 360 return nullptr; |
| 321 } | 361 } |
| 322 | 362 |
| 323 if (parsedOptions.cropRect == srcRect && !parsedOptions.shouldScaleInput) { | 363 if (parsedOptions.cropRect == srcRect && !parsedOptions.shouldScaleInput) { |
| 324 sk_sp<SkImage> croppedSkImage = skiaImage->makeSubset(srcRect); | 364 sk_sp<SkImage> croppedSkImage = skiaImage->makeSubset(srcRect); |
| 365 if (parsedOptions.dstColorSpace) { | |
| 366 croppedSkImage = applyColorSpaceConversion(croppedSkImage, parsedOptions); | |
| 367 } | |
| 325 if (parsedOptions.flipY) | 368 if (parsedOptions.flipY) |
| 326 return StaticBitmapImage::create(flipSkImageVertically( | 369 return StaticBitmapImage::create(flipSkImageVertically( |
| 327 croppedSkImage.get(), parsedOptions.premultiplyAlpha | 370 croppedSkImage.get(), parsedOptions.premultiplyAlpha |
| 328 ? PremultiplyAlpha | 371 ? PremultiplyAlpha |
| 329 : DontPremultiplyAlpha)); | 372 : DontPremultiplyAlpha)); |
| 330 // Special case: The first parameter image is unpremul but we need to turn | 373 // Special case: The first parameter image is unpremul but we need to turn |
| 331 // it into premul. | 374 // it into premul. |
| 332 if (parsedOptions.premultiplyAlpha && imageFormat == DontPremultiplyAlpha) | 375 if (parsedOptions.premultiplyAlpha && imageFormat == DontPremultiplyAlpha) |
| 333 return StaticBitmapImage::create( | 376 return StaticBitmapImage::create( |
| 334 unPremulSkImageToPremul(croppedSkImage.get())); | 377 unPremulSkImageToPremul(croppedSkImage.get())); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 361 SkRect drawDstRect = SkRect::MakeXYWH(0, 0, parsedOptions.resizeWidth, | 404 SkRect drawDstRect = SkRect::MakeXYWH(0, 0, parsedOptions.resizeWidth, |
| 362 parsedOptions.resizeHeight); | 405 parsedOptions.resizeHeight); |
| 363 SkPaint paint; | 406 SkPaint paint; |
| 364 paint.setFilterQuality(parsedOptions.resizeQuality); | 407 paint.setFilterQuality(parsedOptions.resizeQuality); |
| 365 surface->getCanvas()->drawImageRect(skiaImage, drawSrcRect, drawDstRect, | 408 surface->getCanvas()->drawImageRect(skiaImage, drawSrcRect, drawDstRect, |
| 366 &paint); | 409 &paint); |
| 367 } else { | 410 } else { |
| 368 surface->getCanvas()->drawImage(skiaImage, dstLeft, dstTop); | 411 surface->getCanvas()->drawImage(skiaImage, dstLeft, dstTop); |
| 369 } | 412 } |
| 370 skiaImage = surface->makeImageSnapshot(); | 413 skiaImage = surface->makeImageSnapshot(); |
| 414 if (parsedOptions.dstColorSpace) | |
| 415 skiaImage = applyColorSpaceConversion(skiaImage, parsedOptions); | |
| 371 | 416 |
| 372 if (parsedOptions.premultiplyAlpha) { | 417 if (parsedOptions.premultiplyAlpha) { |
| 373 if (imageFormat == DontPremultiplyAlpha) | 418 if (imageFormat == DontPremultiplyAlpha) |
| 374 return StaticBitmapImage::create( | 419 return StaticBitmapImage::create( |
| 375 unPremulSkImageToPremul(skiaImage.get())); | 420 unPremulSkImageToPremul(skiaImage.get())); |
| 376 return StaticBitmapImage::create(std::move(skiaImage)); | 421 return StaticBitmapImage::create(std::move(skiaImage)); |
| 377 } | 422 } |
| 378 return StaticBitmapImage::create(premulSkImageToUnPremul(skiaImage.get())); | 423 return StaticBitmapImage::create(premulSkImageToUnPremul(skiaImage.get())); |
| 379 } | 424 } |
| 380 | 425 |
| 381 ImageBitmap::ImageBitmap(HTMLImageElement* image, | 426 ImageBitmap::ImageBitmap(HTMLImageElement* image, |
| 382 Optional<IntRect> cropRect, | 427 Optional<IntRect> cropRect, |
| 383 Document* document, | 428 Document* document, |
| 384 const ImageBitmapOptions& options) { | 429 const ImageBitmapOptions& options) { |
| 385 RefPtr<Image> input = image->cachedImage()->getImage(); | 430 RefPtr<Image> input = image->cachedImage()->getImage(); |
| 386 ParsedOptions parsedOptions = | 431 ParsedOptions parsedOptions = |
| 387 parseOptions(options, cropRect, image->bitmapSourceSize()); | 432 parseOptions(options, cropRect, image->bitmapSourceSize()); |
| 388 if (dstBufferSizeHasOverflow(parsedOptions)) | 433 if (dstBufferSizeHasOverflow(parsedOptions)) |
| 389 return; | 434 return; |
| 390 | 435 |
| 391 if (options.colorSpaceConversion() == "none") { | 436 m_image = |
| 392 m_image = cropImage(input.get(), parsedOptions, PremultiplyAlpha, | 437 cropImage(input.get(), parsedOptions, PremultiplyAlpha, |
| 393 ImageDecoder::ColorSpaceIgnored); | 438 parsedOptions.dstColorSpace ? ImageDecoder::ColorSpaceApplied |
| 394 } else { | 439 : ImageDecoder::ColorSpaceIgnored); |
| 395 m_image = cropImage(input.get(), parsedOptions, PremultiplyAlpha, | |
| 396 ImageDecoder::ColorSpaceApplied); | |
| 397 } | |
| 398 if (!m_image) | 440 if (!m_image) |
| 399 return; | 441 return; |
| 400 // In the case where the source image is lazy-decoded, m_image may not be in | 442 // In the case where the source image is lazy-decoded, m_image may not be in |
| 401 // a decoded state, we trigger it here. | 443 // a decoded state, we trigger it here. |
| 402 sk_sp<SkImage> skImage = m_image->imageForCurrentFrame(); | 444 sk_sp<SkImage> skImage = m_image->imageForCurrentFrame(); |
| 403 SkPixmap pixmap; | 445 SkPixmap pixmap; |
| 404 if (!skImage->isTextureBacked() && !skImage->peekPixels(&pixmap)) { | 446 if (!skImage->isTextureBacked() && !skImage->peekPixels(&pixmap)) { |
| 405 sk_sp<SkSurface> surface = | 447 sk_sp<SkSurface> surface = |
| 406 SkSurface::MakeRasterN32Premul(skImage->width(), skImage->height()); | 448 SkSurface::MakeRasterN32Premul(skImage->width(), skImage->height()); |
| 407 surface->getCanvas()->drawImage(skImage, 0, 0); | 449 surface->getCanvas()->drawImage(skImage, 0, 0); |
| (...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 862 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, | 904 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, |
| 863 FloatRect* dstRect) const {} | 905 FloatRect* dstRect) const {} |
| 864 | 906 |
| 865 FloatSize ImageBitmap::elementSize(const FloatSize&) const { | 907 FloatSize ImageBitmap::elementSize(const FloatSize&) const { |
| 866 return FloatSize(width(), height()); | 908 return FloatSize(width(), height()); |
| 867 } | 909 } |
| 868 | 910 |
| 869 DEFINE_TRACE(ImageBitmap) {} | 911 DEFINE_TRACE(ImageBitmap) {} |
| 870 | 912 |
| 871 } // namespace blink | 913 } // namespace blink |
| OLD | NEW |