Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / html / ImageData.cpp
blobf0cec65d2ecd00cf4d5c6b57a1d593615b16ecda
1 /*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
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.
29 #include "config.h"
30 #include "core/html/ImageData.h"
32 #include "bindings/core/v8/ExceptionState.h"
33 #include "bindings/core/v8/V8Uint8ClampedArray.h"
34 #include "core/dom/ExceptionCode.h"
35 #include "platform/RuntimeEnabledFeatures.h"
37 namespace blink {
39 ImageData* ImageData::create(const IntSize& size)
41 Checked<int, RecordOverflow> dataSize = 4;
42 dataSize *= size.width();
43 dataSize *= size.height();
44 if (dataSize.hasOverflowed() || dataSize.unsafeGet() < 0)
45 return nullptr;
47 RefPtr<DOMUint8ClampedArray> byteArray =
48 DOMUint8ClampedArray::createOrNull(dataSize.unsafeGet());
49 if (!byteArray)
50 return nullptr;
52 return new ImageData(size, byteArray.release());
55 ImageData* ImageData::create(const IntSize& size, PassRefPtr<DOMUint8ClampedArray> byteArray)
57 Checked<int, RecordOverflow> dataSize = 4;
58 dataSize *= size.width();
59 dataSize *= size.height();
60 if (dataSize.hasOverflowed())
61 return nullptr;
63 if (dataSize.unsafeGet() < 0
64 || static_cast<unsigned>(dataSize.unsafeGet()) > byteArray->length())
65 return nullptr;
67 return new ImageData(size, byteArray);
70 ImageData* ImageData::create(unsigned width, unsigned height, ExceptionState& exceptionState)
72 if (!width || !height) {
73 exceptionState.throwDOMException(IndexSizeError, String::format("The source %s is zero or not a number.", width ? "height" : "width"));
74 return nullptr;
77 Checked<unsigned, RecordOverflow> dataSize = 4;
78 dataSize *= width;
79 dataSize *= height;
80 if (dataSize.hasOverflowed()
81 || static_cast<int>(width) < 0
82 || static_cast<int>(height) < 0) {
83 exceptionState.throwDOMException(IndexSizeError, "The requested image size exceeds the supported range.");
84 return nullptr;
87 RefPtr<DOMUint8ClampedArray> byteArray =
88 DOMUint8ClampedArray::createOrNull(dataSize.unsafeGet());
89 if (!byteArray) {
90 exceptionState.throwDOMException(V8GeneralError, "Out of memory at ImageData creation");
91 return nullptr;
94 return new ImageData(IntSize(width, height), byteArray.release());
97 bool ImageData::validateConstructorArguments(DOMUint8ClampedArray* data, unsigned width, unsigned& lengthInPixels, ExceptionState& exceptionState)
99 if (!width) {
100 exceptionState.throwDOMException(IndexSizeError, "The source width is zero or not a number.");
101 return false;
103 ASSERT(data);
104 unsigned length = data->length();
105 if (!length) {
106 exceptionState.throwDOMException(IndexSizeError, "The input data has a zero byte length.");
107 return false;
109 if (length % 4) {
110 exceptionState.throwDOMException(IndexSizeError, "The input data byte length is not a multiple of 4.");
111 return false;
113 length /= 4;
114 if (length % width) {
115 exceptionState.throwDOMException(IndexSizeError, "The input data byte length is not a multiple of (4 * width).");
116 return false;
118 lengthInPixels = length;
119 return true;
122 ImageData* ImageData::create(DOMUint8ClampedArray* data, unsigned width, ExceptionState& exceptionState)
124 unsigned lengthInPixels = 0;
125 if (!validateConstructorArguments(data, width, lengthInPixels, exceptionState)) {
126 ASSERT(exceptionState.hadException());
127 return nullptr;
129 ASSERT(lengthInPixels && width);
130 unsigned height = lengthInPixels / width;
131 return new ImageData(IntSize(width, height), data);
134 ImageData* ImageData::create(DOMUint8ClampedArray* data, unsigned width, unsigned height, ExceptionState& exceptionState)
136 unsigned lengthInPixels = 0;
137 if (!validateConstructorArguments(data, width, lengthInPixels, exceptionState)) {
138 ASSERT(exceptionState.hadException());
139 return nullptr;
141 ASSERT(lengthInPixels && width);
142 if (height != lengthInPixels / width) {
143 exceptionState.throwDOMException(IndexSizeError, "The input data byte length is not equal to (4 * width * height).");
144 return nullptr;
146 return new ImageData(IntSize(width, height), data);
149 v8::Local<v8::Object> ImageData::associateWithWrapper(v8::Isolate* isolate, const WrapperTypeInfo* wrapperType, v8::Local<v8::Object> wrapper)
151 wrapper = ScriptWrappable::associateWithWrapper(isolate, wrapperType, wrapper);
153 if (!wrapper.IsEmpty() && m_data.get()) {
154 // Create a V8 Uint8ClampedArray object and set the "data" property
155 // of the ImageData object to the created v8 object, eliminating the
156 // C++ callback when accessing the "data" property.
157 v8::Local<v8::Value> pixelArray = toV8(m_data.get(), wrapper, isolate);
158 if (pixelArray.IsEmpty() || !v8CallBoolean(wrapper->DefineOwnProperty(isolate->GetCurrentContext(), v8AtomicString(isolate, "data"), pixelArray, v8::ReadOnly)))
159 return v8::Local<v8::Object>();
161 return wrapper;
164 ImageData::ImageData(const IntSize& size, PassRefPtr<DOMUint8ClampedArray> byteArray)
165 : m_size(size)
166 , m_data(byteArray)
168 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<unsigned>(size.width() * size.height() * 4) <= m_data->length());
171 void ImageData::dispose()
173 m_data.clear();
176 } // namespace blink