| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2016 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer in the | |
| 12 * documentation and/or other materials provided with the distribution. | |
| 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | |
| 14 * its contributors may be used to endorse or promote products derived | |
| 15 * from this software without specific prior written permission. | |
| 16 * | |
| 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
| 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
| 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
| 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
| 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 */ | |
| 28 | |
| 29 #include "core/html/Float32ImageData.h" | |
| 30 | |
| 31 #include "bindings/core/v8/ExceptionState.h" | |
| 32 #include "bindings/core/v8/V8Float32Array.h" | |
| 33 #include "core/dom/ExceptionCode.h" | |
| 34 #include "core/frame/ImageBitmap.h" | |
| 35 #include "core/imagebitmap/ImageBitmapOptions.h" | |
| 36 #include "platform/RuntimeEnabledFeatures.h" | |
| 37 #include "wtf/CheckedNumeric.h" | |
| 38 | |
| 39 namespace blink { | |
| 40 | |
| 41 bool Float32ImageData::validateConstructorArguments( | |
| 42 const unsigned& paramFlags, | |
| 43 const IntSize* size, | |
| 44 const unsigned& width, | |
| 45 const unsigned& height, | |
| 46 const DOMFloat32Array* data, | |
| 47 const String* colorSpace, | |
| 48 ExceptionState* exceptionState) { | |
| 49 return ImageData::validateConstructorArguments( | |
| 50 paramFlags, size, width, height, data, colorSpace, exceptionState, | |
| 51 kFloat32ImageData); | |
| 52 } | |
| 53 | |
| 54 DOMFloat32Array* Float32ImageData::allocateAndValidateFloat32Array( | |
| 55 const unsigned& length, | |
| 56 ExceptionState* exceptionState) { | |
| 57 if (!length) | |
| 58 return nullptr; | |
| 59 DOMFloat32Array* dataArray = DOMFloat32Array::createOrNull(length); | |
| 60 if (!dataArray || length != dataArray->length()) { | |
| 61 if (exceptionState) { | |
| 62 exceptionState->throwDOMException( | |
| 63 V8RangeError, "Out of memory at Float32ImageData creation"); | |
| 64 } | |
| 65 return nullptr; | |
| 66 } | |
| 67 return dataArray; | |
| 68 } | |
| 69 | |
| 70 Float32ImageData* Float32ImageData::create(const IntSize& size) { | |
| 71 if (!Float32ImageData::validateConstructorArguments(kParamSize, &size)) | |
| 72 return nullptr; | |
| 73 DOMFloat32Array* dataArray = | |
| 74 Float32ImageData::allocateAndValidateFloat32Array(4 * size.width() * | |
| 75 size.height()); | |
| 76 return dataArray ? new Float32ImageData(size, dataArray) : nullptr; | |
| 77 } | |
| 78 | |
| 79 Float32ImageData* Float32ImageData::create(const IntSize& size, | |
| 80 DOMFloat32Array* dataArray) { | |
| 81 if (!Float32ImageData::validateConstructorArguments(kParamSize | kParamData, | |
| 82 &size, 0, 0, dataArray)) | |
| 83 return nullptr; | |
| 84 return new Float32ImageData(size, dataArray); | |
| 85 } | |
| 86 | |
| 87 Float32ImageData* Float32ImageData::create(unsigned width, | |
| 88 unsigned height, | |
| 89 ExceptionState& exceptionState) { | |
| 90 if (!Float32ImageData::validateConstructorArguments( | |
| 91 kParamWidth | kParamHeight, nullptr, width, height, nullptr, nullptr, | |
| 92 &exceptionState)) | |
| 93 return nullptr; | |
| 94 DOMFloat32Array* dataArray = | |
| 95 Float32ImageData::allocateAndValidateFloat32Array(4 * width * height, | |
| 96 &exceptionState); | |
| 97 return dataArray ? new Float32ImageData(IntSize(width, height), dataArray) | |
| 98 : nullptr; | |
| 99 } | |
| 100 | |
| 101 Float32ImageData* Float32ImageData::create(DOMFloat32Array* data, | |
| 102 unsigned width, | |
| 103 ExceptionState& exceptionState) { | |
| 104 if (!Float32ImageData::validateConstructorArguments(kParamData | kParamWidth, | |
| 105 nullptr, width, 0, data, | |
| 106 nullptr, &exceptionState)) | |
| 107 return nullptr; | |
| 108 unsigned height = data->length() / (width * 4); | |
| 109 return new Float32ImageData(IntSize(width, height), data); | |
| 110 } | |
| 111 | |
| 112 Float32ImageData* Float32ImageData::create(DOMFloat32Array* data, | |
| 113 unsigned width, | |
| 114 unsigned height, | |
| 115 ExceptionState& exceptionState) { | |
| 116 if (!Float32ImageData::validateConstructorArguments( | |
| 117 kParamData | kParamWidth | kParamHeight, nullptr, width, height, data, | |
| 118 nullptr, &exceptionState)) | |
| 119 return nullptr; | |
| 120 return new Float32ImageData(IntSize(width, height), data); | |
| 121 } | |
| 122 | |
| 123 Float32ImageData* Float32ImageData::create(unsigned width, | |
| 124 unsigned height, | |
| 125 String colorSpace, | |
| 126 ExceptionState& exceptionState) { | |
| 127 if (!Float32ImageData::validateConstructorArguments( | |
| 128 kParamWidth | kParamHeight | kParamColorSpace, nullptr, width, height, | |
| 129 nullptr, &colorSpace, &exceptionState)) | |
| 130 return nullptr; | |
| 131 | |
| 132 DOMFloat32Array* dataArray = | |
| 133 Float32ImageData::allocateAndValidateFloat32Array(4 * width * height, | |
| 134 &exceptionState); | |
| 135 return dataArray ? new Float32ImageData(IntSize(width, height), dataArray, | |
| 136 colorSpace) | |
| 137 : nullptr; | |
| 138 } | |
| 139 | |
| 140 Float32ImageData* Float32ImageData::create(DOMFloat32Array* data, | |
| 141 unsigned width, | |
| 142 String colorSpace, | |
| 143 ExceptionState& exceptionState) { | |
| 144 if (!Float32ImageData::validateConstructorArguments( | |
| 145 kParamData | kParamWidth | kParamColorSpace, nullptr, width, 0, data, | |
| 146 &colorSpace, &exceptionState)) | |
| 147 return nullptr; | |
| 148 unsigned height = data->length() / (width * 4); | |
| 149 return new Float32ImageData(IntSize(width, height), data, colorSpace); | |
| 150 } | |
| 151 | |
| 152 Float32ImageData* Float32ImageData::create(DOMFloat32Array* data, | |
| 153 unsigned width, | |
| 154 unsigned height, | |
| 155 String colorSpace, | |
| 156 ExceptionState& exceptionState) { | |
| 157 if (!Float32ImageData::validateConstructorArguments( | |
| 158 kParamData | kParamWidth | kParamHeight | kParamColorSpace, nullptr, | |
| 159 width, height, data, &colorSpace, &exceptionState)) | |
| 160 return nullptr; | |
| 161 return new Float32ImageData(IntSize(width, height), data, colorSpace); | |
| 162 } | |
| 163 | |
| 164 v8::Local<v8::Object> Float32ImageData::associateWithWrapper( | |
| 165 v8::Isolate* isolate, | |
| 166 const WrapperTypeInfo* wrapperType, | |
| 167 v8::Local<v8::Object> wrapper) { | |
| 168 wrapper = | |
| 169 ScriptWrappable::associateWithWrapper(isolate, wrapperType, wrapper); | |
| 170 | |
| 171 if (!wrapper.IsEmpty() && m_data.get()) { | |
| 172 // Create a V8 Float32Array object and set the "data" property | |
| 173 // of the Float32ImageData object to the created v8 object, eliminating the | |
| 174 // C++ callback when accessing the "data" property. | |
| 175 v8::Local<v8::Value> pixelArray = ToV8(m_data.get(), wrapper, isolate); | |
| 176 if (pixelArray.IsEmpty() || | |
| 177 !v8CallBoolean(wrapper->DefineOwnProperty( | |
| 178 isolate->GetCurrentContext(), v8AtomicString(isolate, "data"), | |
| 179 pixelArray, v8::ReadOnly))) | |
| 180 return v8::Local<v8::Object>(); | |
| 181 } | |
| 182 return wrapper; | |
| 183 } | |
| 184 | |
| 185 Float32ImageData::Float32ImageData(const IntSize& size, | |
| 186 DOMFloat32Array* dataArray, | |
| 187 String colorSpaceName) | |
| 188 : m_size(size), | |
| 189 m_colorSpace(ImageData::getImageDataColorSpace(colorSpaceName)), | |
| 190 m_data(dataArray) { | |
| 191 DCHECK_GE(size.width(), 0); | |
| 192 DCHECK_GE(size.height(), 0); | |
| 193 SECURITY_CHECK(static_cast<unsigned>(size.width() * size.height() * 4) <= | |
| 194 m_data->length()); | |
| 195 } | |
| 196 | |
| 197 } // namespace blink | |
| OLD | NEW |