| 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 createImageBitma
p with higher | 34 // This value should be changed in the future when we support |
| 35 // bit depth, in the parseOptions() function. For now, it is always 4. | 35 // createImageBitmap with higher bit depth, in the parseOptions() function. |
| 36 // For now, it is always 4. |
| 36 int bytesPerPixel = 4; | 37 int bytesPerPixel = 4; |
| 37 }; | 38 }; |
| 38 | 39 |
| 39 // The following two functions are helpers used in cropImage | 40 // The following two functions are helpers used in cropImage |
| 40 static inline IntRect normalizeRect(const IntRect& rect) { | 41 static inline IntRect normalizeRect(const IntRect& rect) { |
| 41 return IntRect(std::min(rect.x(), rect.maxX()), | 42 return IntRect(std::min(rect.x(), rect.maxX()), |
| 42 std::min(rect.y(), rect.maxY()), | 43 std::min(rect.y(), rect.maxY()), |
| 43 std::max(rect.width(), -rect.width()), | 44 std::max(rect.width(), -rect.width()), |
| 44 std::max(rect.height(), -rect.height())); | 45 std::max(rect.height(), -rect.height())); |
| 45 } | 46 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 if (!totalBytes.IsValid()) | 122 if (!totalBytes.IsValid()) |
| 122 return true; | 123 return true; |
| 123 | 124 |
| 124 return false; | 125 return false; |
| 125 } | 126 } |
| 126 | 127 |
| 127 } // namespace | 128 } // namespace |
| 128 | 129 |
| 129 static PassRefPtr<Uint8Array> copySkImageData(SkImage* input, | 130 static PassRefPtr<Uint8Array> copySkImageData(SkImage* input, |
| 130 const SkImageInfo& info) { | 131 const SkImageInfo& info) { |
| 131 // The function dstBufferSizeHasOverflow() is being called at the beginning of
each | 132 // The function dstBufferSizeHasOverflow() is being called at the beginning of |
| 132 // ImageBitmap() constructor, which makes sure that doing width * height * byt
esPerPixel | 133 // each ImageBitmap() constructor, which makes sure that doing |
| 133 // will never overflow size_t. | 134 // width * height * bytesPerPixel will never overflow size_t. |
| 134 size_t width = static_cast<size_t>(input->width()); | 135 size_t width = static_cast<size_t>(input->width()); |
| 135 RefPtr<ArrayBuffer> dstBuffer = | 136 RefPtr<ArrayBuffer> dstBuffer = |
| 136 ArrayBuffer::createOrNull(width * input->height(), info.bytesPerPixel()); | 137 ArrayBuffer::createOrNull(width * input->height(), info.bytesPerPixel()); |
| 137 if (!dstBuffer) | 138 if (!dstBuffer) |
| 138 return nullptr; | 139 return nullptr; |
| 139 RefPtr<Uint8Array> dstPixels = | 140 RefPtr<Uint8Array> dstPixels = |
| 140 Uint8Array::create(dstBuffer, 0, dstBuffer->byteLength()); | 141 Uint8Array::create(dstBuffer, 0, dstBuffer->byteLength()); |
| 141 input->readPixels(info, dstPixels->data(), width * info.bytesPerPixel(), 0, | 142 input->readPixels(info, dstPixels->data(), width * info.bytesPerPixel(), 0, |
| 142 0); | 143 0); |
| 143 return dstPixels; | 144 return dstPixels; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 ExceptionState& exceptionState) { | 260 ExceptionState& exceptionState) { |
| 260 if (!sourceWidth || !sourceHeight) { | 261 if (!sourceWidth || !sourceHeight) { |
| 261 exceptionState.throwDOMException( | 262 exceptionState.throwDOMException( |
| 262 IndexSizeError, String::format("The source %s provided is 0.", | 263 IndexSizeError, String::format("The source %s provided is 0.", |
| 263 sourceWidth ? "height" : "width")); | 264 sourceWidth ? "height" : "width")); |
| 264 return false; | 265 return false; |
| 265 } | 266 } |
| 266 return true; | 267 return true; |
| 267 } | 268 } |
| 268 | 269 |
| 269 // The parameter imageFormat indicates whether the first parameter "image" is un
premultiplied or not. | 270 // The parameter imageFormat indicates whether the first parameter "image" is |
| 270 // imageFormat = PremultiplyAlpha means the image is in premuliplied format | 271 // unpremultiplied or not. imageFormat = PremultiplyAlpha means the image is in |
| 271 // For example, if the image is already in unpremultiplied format and we want th
e created ImageBitmap | 272 // premuliplied format For example, if the image is already in unpremultiplied |
| 272 // in the same format, then we don't need to use the ImageDecoder to decode the
image. | 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. |
| 273 static PassRefPtr<StaticBitmapImage> cropImage( | 275 static PassRefPtr<StaticBitmapImage> cropImage( |
| 274 Image* image, | 276 Image* image, |
| 275 const ParsedOptions& parsedOptions, | 277 const ParsedOptions& parsedOptions, |
| 276 AlphaDisposition imageFormat = PremultiplyAlpha, | 278 AlphaDisposition imageFormat = PremultiplyAlpha, |
| 277 ImageDecoder::GammaAndColorProfileOption colorSpaceOp = | 279 ImageDecoder::GammaAndColorProfileOption colorSpaceOp = |
| 278 ImageDecoder::GammaAndColorProfileApplied) { | 280 ImageDecoder::GammaAndColorProfileApplied) { |
| 279 ASSERT(image); | 281 ASSERT(image); |
| 280 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); | 282 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); |
| 281 const IntRect srcRect = intersection(imgRect, parsedOptions.cropRect); | 283 const IntRect srcRect = intersection(imgRect, parsedOptions.cropRect); |
| 282 | 284 |
| 283 // In the case when cropRect doesn't intersect the source image and it require
s a umpremul image | 285 // In the case when cropRect doesn't intersect the source image and it |
| 284 // We immediately return a transparent black image with cropRect.size() | 286 // requires a umpremul image We immediately return a transparent black image |
| 287 // with cropRect.size() |
| 285 if (srcRect.isEmpty() && !parsedOptions.premultiplyAlpha) { | 288 if (srcRect.isEmpty() && !parsedOptions.premultiplyAlpha) { |
| 286 SkImageInfo info = | 289 SkImageInfo info = |
| 287 SkImageInfo::Make(parsedOptions.resizeWidth, parsedOptions.resizeHeight, | 290 SkImageInfo::Make(parsedOptions.resizeWidth, parsedOptions.resizeHeight, |
| 288 kN32_SkColorType, kUnpremul_SkAlphaType); | 291 kN32_SkColorType, kUnpremul_SkAlphaType); |
| 289 RefPtr<ArrayBuffer> dstBuffer = ArrayBuffer::createOrNull( | 292 RefPtr<ArrayBuffer> dstBuffer = ArrayBuffer::createOrNull( |
| 290 static_cast<size_t>(info.width()) * info.height(), | 293 static_cast<size_t>(info.width()) * info.height(), |
| 291 info.bytesPerPixel()); | 294 info.bytesPerPixel()); |
| 292 if (!dstBuffer) | 295 if (!dstBuffer) |
| 293 return nullptr; | 296 return nullptr; |
| 294 RefPtr<Uint8Array> dstPixels = | 297 RefPtr<Uint8Array> dstPixels = |
| 295 Uint8Array::create(dstBuffer, 0, dstBuffer->byteLength()); | 298 Uint8Array::create(dstBuffer, 0, dstBuffer->byteLength()); |
| 296 return StaticBitmapImage::create(newSkImageFromRaster( | 299 return StaticBitmapImage::create(newSkImageFromRaster( |
| 297 info, std::move(dstPixels), | 300 info, std::move(dstPixels), |
| 298 static_cast<size_t>(info.width()) * info.bytesPerPixel())); | 301 static_cast<size_t>(info.width()) * info.bytesPerPixel())); |
| 299 } | 302 } |
| 300 | 303 |
| 301 sk_sp<SkImage> skiaImage = image->imageForCurrentFrame(); | 304 sk_sp<SkImage> skiaImage = image->imageForCurrentFrame(); |
| 302 // Attempt to get raw unpremultiplied image data, executed only when skiaImage
is premultiplied. | 305 // Attempt to get raw unpremultiplied image data, executed only when skiaImage |
| 306 // is premultiplied. |
| 303 if ((((!parsedOptions.premultiplyAlpha && !skiaImage->isOpaque()) || | 307 if ((((!parsedOptions.premultiplyAlpha && !skiaImage->isOpaque()) || |
| 304 !skiaImage) && | 308 !skiaImage) && |
| 305 image->data() && imageFormat == PremultiplyAlpha) || | 309 image->data() && imageFormat == PremultiplyAlpha) || |
| 306 colorSpaceOp == ImageDecoder::GammaAndColorProfileIgnored) { | 310 colorSpaceOp == ImageDecoder::GammaAndColorProfileIgnored) { |
| 307 std::unique_ptr<ImageDecoder> decoder(ImageDecoder::create( | 311 std::unique_ptr<ImageDecoder> decoder(ImageDecoder::create( |
| 308 image->data(), true, | 312 image->data(), true, |
| 309 parsedOptions.premultiplyAlpha ? ImageDecoder::AlphaPremultiplied | 313 parsedOptions.premultiplyAlpha ? ImageDecoder::AlphaPremultiplied |
| 310 : ImageDecoder::AlphaNotPremultiplied, | 314 : ImageDecoder::AlphaNotPremultiplied, |
| 311 colorSpaceOp)); | 315 colorSpaceOp)); |
| 312 if (!decoder) | 316 if (!decoder) |
| 313 return nullptr; | 317 return nullptr; |
| 314 skiaImage = ImageBitmap::getSkImageFromDecoder(std::move(decoder)); | 318 skiaImage = ImageBitmap::getSkImageFromDecoder(std::move(decoder)); |
| 315 if (!skiaImage) | 319 if (!skiaImage) |
| 316 return nullptr; | 320 return nullptr; |
| 317 } | 321 } |
| 318 | 322 |
| 319 if (parsedOptions.cropRect == srcRect && !parsedOptions.shouldScaleInput) { | 323 if (parsedOptions.cropRect == srcRect && !parsedOptions.shouldScaleInput) { |
| 320 sk_sp<SkImage> croppedSkImage = skiaImage->makeSubset(srcRect); | 324 sk_sp<SkImage> croppedSkImage = skiaImage->makeSubset(srcRect); |
| 321 if (parsedOptions.flipY) | 325 if (parsedOptions.flipY) |
| 322 return StaticBitmapImage::create(flipSkImageVertically( | 326 return StaticBitmapImage::create(flipSkImageVertically( |
| 323 croppedSkImage.get(), parsedOptions.premultiplyAlpha | 327 croppedSkImage.get(), parsedOptions.premultiplyAlpha |
| 324 ? PremultiplyAlpha | 328 ? PremultiplyAlpha |
| 325 : DontPremultiplyAlpha)); | 329 : DontPremultiplyAlpha)); |
| 326 // Special case: The first parameter image is unpremul but we need to turn i
t into premul. | 330 // Special case: The first parameter image is unpremul but we need to turn |
| 331 // it into premul. |
| 327 if (parsedOptions.premultiplyAlpha && imageFormat == DontPremultiplyAlpha) | 332 if (parsedOptions.premultiplyAlpha && imageFormat == DontPremultiplyAlpha) |
| 328 return StaticBitmapImage::create( | 333 return StaticBitmapImage::create( |
| 329 unPremulSkImageToPremul(croppedSkImage.get())); | 334 unPremulSkImageToPremul(croppedSkImage.get())); |
| 330 // Call preroll to trigger image decoding. | 335 // Call preroll to trigger image decoding. |
| 331 croppedSkImage->preroll(); | 336 croppedSkImage->preroll(); |
| 332 return StaticBitmapImage::create(std::move(croppedSkImage)); | 337 return StaticBitmapImage::create(std::move(croppedSkImage)); |
| 333 } | 338 } |
| 334 | 339 |
| 335 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul( | 340 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul( |
| 336 parsedOptions.resizeWidth, parsedOptions.resizeHeight); | 341 parsedOptions.resizeWidth, parsedOptions.resizeHeight); |
| (...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 851 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, | 856 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, |
| 852 FloatRect* dstRect) const {} | 857 FloatRect* dstRect) const {} |
| 853 | 858 |
| 854 FloatSize ImageBitmap::elementSize(const FloatSize&) const { | 859 FloatSize ImageBitmap::elementSize(const FloatSize&) const { |
| 855 return FloatSize(width(), height()); | 860 return FloatSize(width(), height()); |
| 856 } | 861 } |
| 857 | 862 |
| 858 DEFINE_TRACE(ImageBitmap) {} | 863 DEFINE_TRACE(ImageBitmap) {} |
| 859 | 864 |
| 860 } // namespace blink | 865 } // namespace blink |
| OLD | NEW |