Chromium Code Reviews| Index: Source/core/html/ImageData.cpp |
| diff --git a/Source/core/html/ImageData.cpp b/Source/core/html/ImageData.cpp |
| index bcba908180f35739d9b4140b77e6a1d7968b8a86..bf11b580a6ffa6856582b849bf465a66436f1ed9 100644 |
| --- a/Source/core/html/ImageData.cpp |
| +++ b/Source/core/html/ImageData.cpp |
| @@ -29,6 +29,9 @@ |
| #include "config.h" |
| #include "core/html/ImageData.h" |
| +#include "bindings/v8/ExceptionState.h" |
| +#include "core/dom/ExceptionCode.h" |
| + |
| namespace WebCore { |
| PassRefPtr<ImageData> ImageData::create(const IntSize& size) |
| @@ -57,6 +60,57 @@ PassRefPtr<ImageData> ImageData::create(const IntSize& size, PassRefPtr<Uint8Cla |
| return adoptRef(new ImageData(size, byteArray)); |
| } |
| +PassRefPtr<ImageData> ImageData::create(unsigned width, unsigned height, ExceptionState& exceptionState) |
| +{ |
| + if (!width || !height) { |
| + exceptionState.throwDOMException(IndexSizeError, String::format("The source %s is zero or non-finite.", width ? "height" : "width")); |
|
Justin Novosad
2014/03/19 19:35:54
how can an unsigned be non-finite?
sof
2014/03/19 19:41:10
ToUInt32() maps Inf and NaNs (including non-number
|
| + return nullptr; |
| + } |
| + |
| + Checked<unsigned, RecordOverflow> dataSize = 4; |
| + dataSize *= width; |
| + dataSize *= height; |
| + if (dataSize.hasOverflowed()) { |
| + exceptionState.throwDOMException(IndexSizeError, "The requested image size exceeds the supported range."); |
| + return nullptr; |
| + } |
| + |
| + RefPtr<ImageData> imageData = adoptRef(new ImageData(IntSize(width, height))); |
| + imageData->data()->zeroFill(); |
| + return imageData; |
|
Inactive
2014/03/19 19:07:14
nit: .release()
|
| +} |
| + |
| +PassRefPtr<ImageData> ImageData::create(Uint8ClampedArray* data, unsigned width, unsigned height, ExceptionState& exceptionState) |
| +{ |
| + if (!data) { |
| + exceptionState.throwTypeError("Expected a Uint8ClampedArray as first argument."); |
| + return nullptr; |
| + } |
| + if (!width) { |
| + exceptionState.throwDOMException(IndexSizeError, "The source width is zero or non-finite."); |
| + return nullptr; |
| + } |
| + |
| + Uint8ClampedArray* clampedData = static_cast<Uint8ClampedArray*>(data); |
|
Inactive
2014/03/19 19:07:14
Why is this cast needed?
I actually don't underst
sof
2014/03/19 22:19:16
Yes, doesn't make much sense; gone. [I belatedly d
|
| + unsigned clampedLength = clampedData->length() / 4; |
|
Justin Novosad
2014/03/19 19:35:54
According to the spec you need to throw an Invalid
sof
2014/03/19 22:19:16
Thanks, quite right. Addressed + expanded the test
|
| + if (!clampedLength) { |
| + exceptionState.throwDOMException(IndexSizeError, "The input data has a zero byte length."); |
| + return nullptr; |
| + } |
| + if (clampedLength % width) { |
| + exceptionState.throwDOMException(IndexSizeError, "The byte length of the input data is not a multiple of (4 * width)."); |
| + return nullptr; |
| + } |
| + if (!height) { |
| + height = clampedLength / width; |
| + } else if (height != clampedLength / width) { |
| + exceptionState.throwDOMException(IndexSizeError, "The byte length of the input data is not equal to (4 * width * height)."); |
| + return nullptr; |
| + } |
| + |
| + return adoptRef(new ImageData(IntSize(width, height), clampedData)); |
| +} |
| + |
| ImageData::ImageData(const IntSize& size) |
| : m_size(size) |
| , m_data(Uint8ClampedArray::createUninitialized(size.width() * size.height() * 4)) |