1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#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
|