Chromium Code Reviews| Index: third_party/WebKit/Source/core/html/Float32ImageData.cpp |
| diff --git a/third_party/WebKit/Source/core/html/Float32ImageData.cpp b/third_party/WebKit/Source/core/html/Float32ImageData.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..665a265ede159806473f9e4715aa1a82576f364b |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/html/Float32ImageData.cpp |
| @@ -0,0 +1,197 @@ |
| +/* |
| + * Copyright (C) 2016 Google Inc. All rights reserved. |
|
esprehn
2017/01/13 22:39:11
Please use the short 4 line copyright for all new
|
| + * |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions |
| + * are met: |
| + * |
| + * 1. Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * 2. Redistributions in binary form must reproduce the above copyright |
| + * notice, this list of conditions and the following disclaimer in the |
| + * documentation and/or other materials provided with the distribution. |
| + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| + * its contributors may be used to endorse or promote products derived |
| + * from this software without specific prior written permission. |
| + * |
| + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + */ |
| + |
| +#include "core/html/Float32ImageData.h" |
| + |
| +#include "bindings/core/v8/ExceptionState.h" |
| +#include "bindings/core/v8/V8Float32Array.h" |
| +#include "core/dom/ExceptionCode.h" |
| +#include "core/frame/ImageBitmap.h" |
| +#include "core/imagebitmap/ImageBitmapOptions.h" |
| +#include "platform/RuntimeEnabledFeatures.h" |
| +#include "wtf/CheckedNumeric.h" |
| + |
| +namespace blink { |
| + |
| +bool Float32ImageData::validateConstructorArguments( |
| + const unsigned& paramFlags, |
| + const IntSize* size, |
| + const unsigned& width, |
| + const unsigned& height, |
| + const DOMFloat32Array* data, |
| + const String* colorSpace, |
| + ExceptionState* exceptionState) { |
| + return ImageData::validateConstructorArguments( |
| + paramFlags, size, width, height, data, colorSpace, exceptionState, |
| + kFloat32ImageData); |
| +} |
| + |
| +DOMFloat32Array* Float32ImageData::allocateAndValidateFloat32Array( |
| + const unsigned& length, |
| + ExceptionState* exceptionState) { |
| + if (!length) |
| + return nullptr; |
| + DOMFloat32Array* dataArray = DOMFloat32Array::createOrNull(length); |
| + if (!dataArray || length != dataArray->length()) { |
| + if (exceptionState) { |
| + exceptionState->throwDOMException( |
| + V8RangeError, "Out of memory at Float32ImageData creation"); |
| + } |
| + return nullptr; |
| + } |
| + return dataArray; |
| +} |
| + |
| +Float32ImageData* Float32ImageData::create(const IntSize& size) { |
| + if (!Float32ImageData::validateConstructorArguments(kParamSize, &size)) |
| + return nullptr; |
| + DOMFloat32Array* dataArray = |
| + Float32ImageData::allocateAndValidateFloat32Array(4 * size.width() * |
| + size.height()); |
| + return dataArray ? new Float32ImageData(size, dataArray) : nullptr; |
| +} |
| + |
| +Float32ImageData* Float32ImageData::create(const IntSize& size, |
| + DOMFloat32Array* dataArray) { |
| + if (!Float32ImageData::validateConstructorArguments(kParamSize | kParamData, |
| + &size, 0, 0, dataArray)) |
| + return nullptr; |
| + return new Float32ImageData(size, dataArray); |
| +} |
| + |
| +Float32ImageData* Float32ImageData::create(unsigned width, |
| + unsigned height, |
| + ExceptionState& exceptionState) { |
| + if (!Float32ImageData::validateConstructorArguments( |
| + kParamWidth | kParamHeight, nullptr, width, height, nullptr, nullptr, |
| + &exceptionState)) |
| + return nullptr; |
| + DOMFloat32Array* dataArray = |
| + Float32ImageData::allocateAndValidateFloat32Array(4 * width * height, |
| + &exceptionState); |
| + return dataArray ? new Float32ImageData(IntSize(width, height), dataArray) |
| + : nullptr; |
| +} |
| + |
| +Float32ImageData* Float32ImageData::create(DOMFloat32Array* data, |
| + unsigned width, |
| + ExceptionState& exceptionState) { |
| + if (!Float32ImageData::validateConstructorArguments(kParamData | kParamWidth, |
| + nullptr, width, 0, data, |
| + nullptr, &exceptionState)) |
| + return nullptr; |
| + unsigned height = data->length() / (width * 4); |
| + return new Float32ImageData(IntSize(width, height), data); |
| +} |
| + |
| +Float32ImageData* Float32ImageData::create(DOMFloat32Array* data, |
| + unsigned width, |
| + unsigned height, |
| + ExceptionState& exceptionState) { |
| + if (!Float32ImageData::validateConstructorArguments( |
| + kParamData | kParamWidth | kParamHeight, nullptr, width, height, data, |
| + nullptr, &exceptionState)) |
| + return nullptr; |
| + return new Float32ImageData(IntSize(width, height), data); |
| +} |
| + |
| +Float32ImageData* Float32ImageData::create(unsigned width, |
| + unsigned height, |
| + String colorSpace, |
| + ExceptionState& exceptionState) { |
| + if (!Float32ImageData::validateConstructorArguments( |
| + kParamWidth | kParamHeight | kParamColorSpace, nullptr, width, height, |
| + nullptr, &colorSpace, &exceptionState)) |
| + return nullptr; |
| + |
| + DOMFloat32Array* dataArray = |
| + Float32ImageData::allocateAndValidateFloat32Array(4 * width * height, |
| + &exceptionState); |
| + return dataArray ? new Float32ImageData(IntSize(width, height), dataArray, |
| + colorSpace) |
| + : nullptr; |
| +} |
| + |
| +Float32ImageData* Float32ImageData::create(DOMFloat32Array* data, |
| + unsigned width, |
| + String colorSpace, |
| + ExceptionState& exceptionState) { |
| + if (!Float32ImageData::validateConstructorArguments( |
| + kParamData | kParamWidth | kParamColorSpace, nullptr, width, 0, data, |
| + &colorSpace, &exceptionState)) |
| + return nullptr; |
| + unsigned height = data->length() / (width * 4); |
| + return new Float32ImageData(IntSize(width, height), data, colorSpace); |
| +} |
| + |
| +Float32ImageData* Float32ImageData::create(DOMFloat32Array* data, |
| + unsigned width, |
| + unsigned height, |
| + String colorSpace, |
| + ExceptionState& exceptionState) { |
| + if (!Float32ImageData::validateConstructorArguments( |
| + kParamData | kParamWidth | kParamHeight | kParamColorSpace, nullptr, |
| + width, height, data, &colorSpace, &exceptionState)) |
| + return nullptr; |
| + return new Float32ImageData(IntSize(width, height), data, colorSpace); |
| +} |
| + |
| +v8::Local<v8::Object> Float32ImageData::associateWithWrapper( |
| + v8::Isolate* isolate, |
| + const WrapperTypeInfo* wrapperType, |
| + v8::Local<v8::Object> wrapper) { |
| + wrapper = |
| + ScriptWrappable::associateWithWrapper(isolate, wrapperType, wrapper); |
| + |
| + if (!wrapper.IsEmpty() && m_data.get()) { |
| + // Create a V8 Float32Array object and set the "data" property |
| + // of the Float32ImageData object to the created v8 object, eliminating the |
| + // C++ callback when accessing the "data" property. |
| + v8::Local<v8::Value> pixelArray = ToV8(m_data.get(), wrapper, isolate); |
| + if (pixelArray.IsEmpty() || |
| + !v8CallBoolean(wrapper->DefineOwnProperty( |
| + isolate->GetCurrentContext(), v8AtomicString(isolate, "data"), |
| + pixelArray, v8::ReadOnly))) |
| + return v8::Local<v8::Object>(); |
| + } |
| + return wrapper; |
| +} |
| + |
| +Float32ImageData::Float32ImageData(const IntSize& size, |
| + DOMFloat32Array* dataArray, |
| + String colorSpaceName) |
| + : m_size(size), |
| + m_colorSpace(ImageData::getImageDataColorSpace(colorSpaceName)), |
| + m_data(dataArray) { |
| + DCHECK_GE(size.width(), 0); |
| + DCHECK_GE(size.height(), 0); |
| + SECURITY_CHECK(static_cast<unsigned>(size.width() * size.height() * 4) <= |
| + m_data->length()); |
| +} |
| + |
| +} // namespace blink |