Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(463)

Side by Side Diff: third_party/WebKit/Source/core/html/Float32ImageData.cpp

Issue 2555213002: Implement color management for ImageData (Closed)
Patch Set: Addressing issues Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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<DOMFloat32Array>(
50 paramFlags, size, width, height, data, colorSpace, exceptionState,
51 kFloat32ImageData);
52 }
53
54 DOMFloat32Array* Float32ImageData::allocateAndValidateFloat32Array(
55 const unsigned& length,
56 ExceptionState* exceptionState) {
57 return ImageData::allocateAndValidateDataArray<DOMFloat32Array>(
58 length, exceptionState, kFloat32ImageData);
59 }
60
61 Float32ImageData* Float32ImageData::create(const IntSize& size) {
62 if (!Float32ImageData::validateConstructorArguments(kParamSize, &size))
63 return nullptr;
64 DOMFloat32Array* dataArray =
65 Float32ImageData::allocateAndValidateFloat32Array(4 * size.width() *
66 size.height());
67 return dataArray ? new Float32ImageData(size, dataArray) : nullptr;
68 }
69
70 Float32ImageData* Float32ImageData::create(const IntSize& size,
71 DOMFloat32Array* dataArray) {
72 if (!Float32ImageData::validateConstructorArguments(kParamSize | kParamData,
73 &size, 0, 0, dataArray))
74 return nullptr;
75 return new Float32ImageData(size, dataArray);
76 }
77
78 Float32ImageData* Float32ImageData::create(unsigned width,
79 unsigned height,
80 ExceptionState& exceptionState) {
81 if (!Float32ImageData::validateConstructorArguments(
82 kParamWidth | kParamHeight, nullptr, width, height, nullptr, nullptr,
83 &exceptionState))
84 return nullptr;
85 DOMFloat32Array* dataArray =
86 Float32ImageData::allocateAndValidateFloat32Array(4 * width * height,
87 &exceptionState);
88 return dataArray ? new Float32ImageData(IntSize(width, height), dataArray)
89 : nullptr;
90 }
91
92 Float32ImageData* Float32ImageData::create(DOMFloat32Array* data,
93 unsigned width,
94 ExceptionState& exceptionState) {
95 if (!Float32ImageData::validateConstructorArguments(kParamData | kParamWidth,
96 nullptr, width, 0, data,
97 nullptr, &exceptionState))
98 return nullptr;
99 unsigned height = data->length() / (width * 4);
100 return new Float32ImageData(IntSize(width, height), data);
101 }
102
103 Float32ImageData* Float32ImageData::create(DOMFloat32Array* data,
104 unsigned width,
105 unsigned height,
106 ExceptionState& exceptionState) {
107 if (!Float32ImageData::validateConstructorArguments(
108 kParamData | kParamWidth | kParamHeight, nullptr, width, height, data,
109 nullptr, &exceptionState))
110 return nullptr;
111 return new Float32ImageData(IntSize(width, height), data);
112 }
113
114 Float32ImageData* Float32ImageData::create(unsigned width,
115 unsigned height,
116 String colorSpace,
117 ExceptionState& exceptionState) {
118 if (!Float32ImageData::validateConstructorArguments(
119 kParamWidth | kParamHeight | kParamColorSpace, nullptr, width, height,
120 nullptr, &colorSpace, &exceptionState))
121 return nullptr;
122
123 DOMFloat32Array* dataArray =
124 Float32ImageData::allocateAndValidateFloat32Array(4 * width * height,
125 &exceptionState);
126 return dataArray ? new Float32ImageData(IntSize(width, height), dataArray,
127 colorSpace)
128 : nullptr;
129 }
130
131 Float32ImageData* Float32ImageData::create(DOMFloat32Array* data,
132 unsigned width,
133 String colorSpace,
134 ExceptionState& exceptionState) {
135 if (!Float32ImageData::validateConstructorArguments(
136 kParamData | kParamWidth | kParamColorSpace, nullptr, width, 0, data,
137 &colorSpace, &exceptionState))
138 return nullptr;
139 unsigned height = data->length() / (width * 4);
140 return new Float32ImageData(IntSize(width, height), data, colorSpace);
141 }
142
143 Float32ImageData* Float32ImageData::create(DOMFloat32Array* data,
144 unsigned width,
145 unsigned height,
146 String colorSpace,
147 ExceptionState& exceptionState) {
148 if (!Float32ImageData::validateConstructorArguments(
149 kParamData | kParamWidth | kParamHeight | kParamColorSpace, nullptr,
150 width, height, data, &colorSpace, &exceptionState))
151 return nullptr;
152 return new Float32ImageData(IntSize(width, height), data, colorSpace);
153 }
154
155 // TODO(zakerinasab): Fix this when ImageBitmap color correction code is landed.
156 // Tip: If the source Image Data has a color space, createImageBitmap must
157 // respect this color space even when no color space tag is passed to it.
158 ScriptPromise Float32ImageData::createImageBitmap(
159 ScriptState* scriptState,
160 EventTarget& eventTarget,
161 Optional<IntRect> cropRect,
162 const ImageBitmapOptions& options,
163 ExceptionState& exceptionState) {
164 if ((cropRect &&
165 !ImageBitmap::isSourceSizeValid(cropRect->width(), cropRect->height(),
166 exceptionState)) ||
167 !ImageBitmap::isSourceSizeValid(bitmapSourceSize().width(),
168 bitmapSourceSize().height(),
169 exceptionState))
170 return ScriptPromise();
171 if (data()->bufferBase()->isNeutered()) {
172 exceptionState.throwDOMException(InvalidStateError,
173 "The source data has been neutered.");
Justin Novosad 2016/12/16 18:38:11 We now prefer the term "detached" instead of neute
zakerinasab1 2016/12/19 19:57:39 Done.
174 return ScriptPromise();
175 }
176 if (!ImageBitmap::isResizeOptionValid(options, exceptionState))
177 return ScriptPromise();
178 return ImageBitmapSource::fulfillImageBitmap(
179 scriptState, ImageBitmap::create(this, cropRect, options));
180 }
181
182 v8::Local<v8::Object> Float32ImageData::associateWithWrapper(
183 v8::Isolate* isolate,
184 const WrapperTypeInfo* wrapperType,
185 v8::Local<v8::Object> wrapper) {
186 wrapper =
187 ScriptWrappable::associateWithWrapper(isolate, wrapperType, wrapper);
188
189 if (!wrapper.IsEmpty() && m_data.get()) {
190 // Create a V8 Float32Array object and set the "data" property
191 // of the Float32ImageData object to the created v8 object, eliminating the
192 // C++ callback when accessing the "data" property.
193 v8::Local<v8::Value> pixelArray = toV8(m_data.get(), wrapper, isolate);
194 if (pixelArray.IsEmpty() ||
195 !v8CallBoolean(wrapper->DefineOwnProperty(
196 isolate->GetCurrentContext(), v8AtomicString(isolate, "data"),
197 pixelArray, v8::ReadOnly)))
198 return v8::Local<v8::Object>();
199 }
200 return wrapper;
201 }
202
203 Float32ImageData::Float32ImageData(const IntSize& size,
204 DOMFloat32Array* dataArray,
205 String colorSpaceName)
206 : m_size(size),
207 m_colorSpace(ImageData::getImageDataColorSpace(colorSpaceName)),
208 m_data(dataArray) {
209 DCHECK_GE(size.width(), 0);
210 DCHECK_GE(size.height(), 0);
211 SECURITY_CHECK(static_cast<unsigned>(size.width() * size.height() * 4) <=
212 m_data->length());
213 }
214
215 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698