Chromium Code Reviews| Index: third_party/WebKit/Source/core/html/ImageData.cpp |
| diff --git a/third_party/WebKit/Source/core/html/ImageData.cpp b/third_party/WebKit/Source/core/html/ImageData.cpp |
| index 32726d5aef74e76fa1f9a1d4a83ba2c63ad13014..3a8be83671c153d4b45774fdb548699f63f3001e 100644 |
| --- a/third_party/WebKit/Source/core/html/ImageData.cpp |
| +++ b/third_party/WebKit/Source/core/html/ImageData.cpp |
| @@ -99,6 +99,39 @@ ImageData* ImageData::create(unsigned width, |
| return new ImageData(IntSize(width, height), byteArray); |
| } |
| +ImageData* ImageData::create(unsigned width, |
| + unsigned height, |
| + String colorSpace, |
| + ExceptionState& exceptionState) { |
| + if (!width || !height) { |
| + exceptionState.throwDOMException( |
| + IndexSizeError, String::format("The source %s is zero or not a number.", |
| + width ? "height" : "width")); |
| + return nullptr; |
| + } |
| + |
| + CheckedNumeric<unsigned> dataSize = 4; |
| + dataSize *= width; |
| + dataSize *= height; |
| + if (!dataSize.IsValid() || static_cast<int>(width) < 0 || |
| + static_cast<int>(height) < 0) { |
| + exceptionState.throwDOMException( |
| + IndexSizeError, |
| + "The requested image size exceeds the supported range."); |
| + return nullptr; |
| + } |
| + |
| + DOMUint8ClampedArray* byteArray = |
| + DOMUint8ClampedArray::createOrNull(dataSize.ValueOrDie()); |
| + if (!byteArray) { |
| + exceptionState.throwDOMException(V8Error, |
|
Justin Novosad
2016/12/08 20:58:59
this should be V8RangeError
zakerinasab
2016/12/08 22:13:52
Fixed for this and similar occurrences.
|
| + "Out of memory at ImageData creation"); |
| + return nullptr; |
| + } |
| + |
| + return new ImageData(IntSize(width, height), byteArray, colorSpace); |
| +} |
| + |
| bool ImageData::validateConstructorArguments(DOMUint8ClampedArray* data, |
| unsigned width, |
| unsigned& lengthInPixels, |
| @@ -131,6 +164,23 @@ bool ImageData::validateConstructorArguments(DOMUint8ClampedArray* data, |
| return true; |
| } |
| +bool ImageData::validateConstructorArguments(DOMUint8ClampedArray* data, |
| + unsigned width, |
| + unsigned& lengthInPixels, |
| + String colorSpace, |
| + ExceptionState& exceptionState) { |
| + if (colorSpace != kLegacyImageDataColorSpaceName && |
| + colorSpace != kSRGBImageDataColorSpaceName) { |
| + exceptionState.throwDOMException(NotSupportedError, |
| + "The input color space is not supported " |
| + "in Uint8ClampedArraye-backed image " |
| + "data."); |
| + return false; |
| + } |
| + return validateConstructorArguments(data, width, lengthInPixels, |
| + exceptionState); |
| +} |
| + |
| ImageData* ImageData::create(DOMUint8ClampedArray* data, |
| unsigned width, |
| ExceptionState& exceptionState) { |
| @@ -167,6 +217,55 @@ ImageData* ImageData::create(DOMUint8ClampedArray* data, |
| return new ImageData(IntSize(width, height), data); |
| } |
| +ImageData* ImageData::create(DOMUint8ClampedArray* data, |
| + unsigned width, |
| + unsigned height, |
| + String colorSpace, |
| + ExceptionState& exceptionState) { |
| + unsigned lengthInPixels = 0; |
| + if (!validateConstructorArguments(data, width, lengthInPixels, colorSpace, |
| + exceptionState)) { |
| + DCHECK(exceptionState.hadException()); |
| + return nullptr; |
| + } |
| + DCHECK_GT(lengthInPixels, 0u); |
| + DCHECK_GT(width, 0u); |
| + if (height != lengthInPixels / width) { |
| + exceptionState.throwDOMException( |
| + IndexSizeError, |
| + "The input data byte length is not equal to (4 * width * height)."); |
| + return nullptr; |
| + } |
| + return new ImageData(IntSize(width, height), data, colorSpace); |
| +} |
| + |
| +ImageDataColorSpace ImageData::getImageDataColorSpace(String colorSpaceName) { |
| + if (colorSpaceName == kLegacyImageDataColorSpaceName) |
| + return kLegacyImageDataColorSpace; |
| + if (colorSpaceName == kSRGBImageDataColorSpaceName) |
| + return kSRGBImageDataColorSpace; |
| + if (colorSpaceName == kLinearRGBImageDataColorSpaceName) |
| + return kLinearRGBImageDataColorSpace; |
| + NOTREACHED(); |
| + return kLegacyImageDataColorSpace; |
| +} |
| + |
| +String ImageData::getImageDataColorSpaceName(ImageDataColorSpace colorSpace) { |
| + switch (colorSpace) { |
| + case kLegacyImageDataColorSpace: |
| + return kLegacyImageDataColorSpaceName; |
| + case kSRGBImageDataColorSpace: |
| + return kSRGBImageDataColorSpaceName; |
| + case kLinearRGBImageDataColorSpace: |
| + return kLinearRGBImageDataColorSpaceName; |
| + } |
| + NOTREACHED(); |
| + return String(); |
| +} |
| + |
| +// TODO(zakerinasab): Fix this when ImageBitmap color correction code is landed. |
| +// Tip: If the source Image Data has a color space, createImageBitmap must |
| +// respect this color space even when no color space tag is passed to it. |
| ScriptPromise ImageData::createImageBitmap(ScriptState* scriptState, |
| EventTarget& eventTarget, |
| Optional<IntRect> cropRect, |
| @@ -211,8 +310,12 @@ v8::Local<v8::Object> ImageData::associateWithWrapper( |
| return wrapper; |
| } |
| -ImageData::ImageData(const IntSize& size, DOMUint8ClampedArray* byteArray) |
| - : m_size(size), m_data(byteArray) { |
| +ImageData::ImageData(const IntSize& size, |
| + DOMUint8ClampedArray* byteArray, |
| + String colorSpaceName) |
| + : m_size(size), |
| + m_colorSpace(getImageDataColorSpace(colorSpaceName)), |
| + m_data(byteArray) { |
| DCHECK_GE(size.width(), 0); |
| DCHECK_GE(size.height(), 0); |
| SECURITY_CHECK(static_cast<unsigned>(size.width() * size.height() * 4) <= |