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

Unified 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 side-by-side diff with in-line comments
Download patch
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..389896de8ff1cdd62a62c214a920d59f072a796e
--- /dev/null
+++ b/third_party/WebKit/Source/core/html/Float32ImageData.cpp
@@ -0,0 +1,215 @@
+/*
+ * Copyright (C) 2016 Google Inc. All rights reserved.
+ *
+ * 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<DOMFloat32Array>(
+ paramFlags, size, width, height, data, colorSpace, exceptionState,
+ kFloat32ImageData);
+}
+
+DOMFloat32Array* Float32ImageData::allocateAndValidateFloat32Array(
+ const unsigned& length,
+ ExceptionState* exceptionState) {
+ return ImageData::allocateAndValidateDataArray<DOMFloat32Array>(
+ length, exceptionState, kFloat32ImageData);
+}
+
+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);
+}
+
+// TODO(zakerinasab): Fix this when ImageBitmap color correction code is landed.
+// Tip: If the source Image Data has a color space, createImageBitmap must
+// respect this color space even when no color space tag is passed to it.
+ScriptPromise Float32ImageData::createImageBitmap(
+ ScriptState* scriptState,
+ EventTarget& eventTarget,
+ Optional<IntRect> cropRect,
+ const ImageBitmapOptions& options,
+ ExceptionState& exceptionState) {
+ if ((cropRect &&
+ !ImageBitmap::isSourceSizeValid(cropRect->width(), cropRect->height(),
+ exceptionState)) ||
+ !ImageBitmap::isSourceSizeValid(bitmapSourceSize().width(),
+ bitmapSourceSize().height(),
+ exceptionState))
+ return ScriptPromise();
+ if (data()->bufferBase()->isNeutered()) {
+ exceptionState.throwDOMException(InvalidStateError,
+ "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.
+ return ScriptPromise();
+ }
+ if (!ImageBitmap::isResizeOptionValid(options, exceptionState))
+ return ScriptPromise();
+ return ImageBitmapSource::fulfillImageBitmap(
+ scriptState, ImageBitmap::create(this, cropRect, options));
+}
+
+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

Powered by Google App Engine
This is Rietveld 408576698