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

Side by Side Diff: third_party/WebKit/Source/core/html/ImageData.h

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
1 /* 1 /*
2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved. 2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 18 matching lines...) Expand all
29 #ifndef ImageData_h 29 #ifndef ImageData_h
30 #define ImageData_h 30 #define ImageData_h
31 31
32 #include "bindings/core/v8/ScriptWrappable.h" 32 #include "bindings/core/v8/ScriptWrappable.h"
33 #include "core/CoreExport.h" 33 #include "core/CoreExport.h"
34 #include "core/dom/DOMTypedArray.h" 34 #include "core/dom/DOMTypedArray.h"
35 #include "core/imagebitmap/ImageBitmapSource.h" 35 #include "core/imagebitmap/ImageBitmapSource.h"
36 #include "platform/geometry/IntRect.h" 36 #include "platform/geometry/IntRect.h"
37 #include "platform/geometry/IntSize.h" 37 #include "platform/geometry/IntSize.h"
38 #include "platform/heap/Handle.h" 38 #include "platform/heap/Handle.h"
39 #include "wtf/CheckedNumeric.h"
39 #include "wtf/Compiler.h" 40 #include "wtf/Compiler.h"
41 #include "wtf/text/WTFString.h"
40 42
41 namespace blink { 43 namespace blink {
42 44
43 class ExceptionState; 45 class ExceptionState;
44 class ImageBitmapOptions; 46 class ImageBitmapOptions;
45 47
48 enum ConstructorParams {
49 kParamSize = 1,
50 kParamWidth = 1 << 1,
51 kParamHeight = 1 << 2,
52 kParamData = 1 << 3,
53 kParamColorSpace = 1 << 4,
54 };
55
56 enum ImageDataType {
57 kUint8ClampedImageData,
58 kFloat32ImageData,
59 };
60
61 enum ImageDataColorSpace {
62 kLegacyImageDataColorSpace,
63 kSRGBImageDataColorSpace,
64 kLinearRGBImageDataColorSpace,
65 };
66
67 const char* const kLinearRGBImageDataColorSpaceName = "linear-rgb";
68 const char* const kSRGBImageDataColorSpaceName = "srgb";
69 const char* const kLegacyImageDataColorSpaceName = "legacy-srgb";
70
46 class CORE_EXPORT ImageData final : public GarbageCollectedFinalized<ImageData>, 71 class CORE_EXPORT ImageData final : public GarbageCollectedFinalized<ImageData>,
47 public ScriptWrappable, 72 public ScriptWrappable,
48 public ImageBitmapSource { 73 public ImageBitmapSource {
49 DEFINE_WRAPPERTYPEINFO(); 74 DEFINE_WRAPPERTYPEINFO();
50 75
51 public: 76 public:
52 static ImageData* create(const IntSize&); 77 static ImageData* create(const IntSize&);
53 static ImageData* create(const IntSize&, DOMUint8ClampedArray*); 78 static ImageData* create(const IntSize&, DOMUint8ClampedArray*);
54 static ImageData* create(unsigned width, unsigned height, ExceptionState&); 79 static ImageData* create(unsigned width, unsigned height, ExceptionState&);
55 static ImageData* create(DOMUint8ClampedArray*, 80 static ImageData* create(DOMUint8ClampedArray*,
56 unsigned width, 81 unsigned width,
57 ExceptionState&); 82 ExceptionState&);
58 static ImageData* create(DOMUint8ClampedArray*, 83 static ImageData* create(DOMUint8ClampedArray*,
59 unsigned width, 84 unsigned width,
60 unsigned height, 85 unsigned height,
61 ExceptionState&); 86 ExceptionState&);
62 87
88 static ImageData* createForTest(const IntSize&);
89
90 ImageData* createImageData(unsigned width,
91 unsigned height,
92 String colorSpace,
93 ExceptionState&);
94 ImageData* createImageData(DOMUint8ClampedArray*,
95 unsigned width,
96 String colorSpace,
97 ExceptionState&);
98 ImageData* createImageData(DOMUint8ClampedArray*,
99 unsigned width,
100 unsigned height,
101 String colorSpace,
102 ExceptionState&);
103
104 static ImageDataColorSpace getImageDataColorSpace(String);
105 static String getImageDataColorSpaceName(ImageDataColorSpace);
106
63 IntSize size() const { return m_size; } 107 IntSize size() const { return m_size; }
64 int width() const { return m_size.width(); } 108 int width() const { return m_size.width(); }
65 int height() const { return m_size.height(); } 109 int height() const { return m_size.height(); }
110 String colorSpace() const { return getImageDataColorSpaceName(m_colorSpace); }
111 ImageDataColorSpace imageDataColorSpace() { return m_colorSpace; }
66 const DOMUint8ClampedArray* data() const { return m_data.get(); } 112 const DOMUint8ClampedArray* data() const { return m_data.get(); }
67 DOMUint8ClampedArray* data() { return m_data.get(); } 113 DOMUint8ClampedArray* data() { return m_data.get(); }
68 114
69 // ImageBitmapSource implementation 115 // ImageBitmapSource implementation
70 IntSize bitmapSourceSize() const override { return m_size; } 116 IntSize bitmapSourceSize() const override { return m_size; }
71 ScriptPromise createImageBitmap(ScriptState*, 117 ScriptPromise createImageBitmap(ScriptState*,
72 EventTarget&, 118 EventTarget&,
73 Optional<IntRect> cropRect, 119 Optional<IntRect> cropRect,
74 const ImageBitmapOptions&, 120 const ImageBitmapOptions&,
75 ExceptionState&) override; 121 ExceptionState&) override;
76 122
77 DEFINE_INLINE_TRACE() { visitor->trace(m_data); } 123 DEFINE_INLINE_TRACE() { visitor->trace(m_data); }
78 124
79 WARN_UNUSED_RESULT v8::Local<v8::Object> associateWithWrapper( 125 WARN_UNUSED_RESULT v8::Local<v8::Object> associateWithWrapper(
80 v8::Isolate*, 126 v8::Isolate*,
81 const WrapperTypeInfo*, 127 const WrapperTypeInfo*,
82 v8::Local<v8::Object> wrapper) override; 128 v8::Local<v8::Object> wrapper) override;
83 129
130 template <typename DataArrayType>
131 static bool validateConstructorArguments(
132 const unsigned&,
133 const IntSize* = nullptr,
134 const unsigned& = 0,
135 const unsigned& = 0,
136 const DataArrayType* = nullptr,
137 const String* = nullptr,
138 ExceptionState* = nullptr,
139 ImageDataType = kUint8ClampedImageData);
140
141 template <typename DataArrayType>
142 static DataArrayType* allocateAndValidateDataArray(
143 const unsigned&,
144 ExceptionState* = nullptr,
145 ImageDataType = kUint8ClampedImageData);
146
84 private: 147 private:
85 ImageData(const IntSize&, DOMUint8ClampedArray*); 148 ImageData(const IntSize&,
86 149 DOMUint8ClampedArray*,
87 static bool validateConstructorArguments(DOMUint8ClampedArray*, 150 String = kLegacyImageDataColorSpaceName);
88 unsigned width,
89 unsigned&,
90 ExceptionState&);
91 151
92 IntSize m_size; 152 IntSize m_size;
153 ImageDataColorSpace m_colorSpace;
93 Member<DOMUint8ClampedArray> m_data; 154 Member<DOMUint8ClampedArray> m_data;
155
156 static bool validateConstructorArguments(
157 const unsigned&,
158 const IntSize* = nullptr,
159 const unsigned& = 0,
160 const unsigned& = 0,
161 const DOMUint8ClampedArray* = nullptr,
162 const String* = nullptr,
163 ExceptionState* = nullptr);
164
165 static DOMUint8ClampedArray* allocateAndValidateUint8ClampedArray(
166 const unsigned&,
167 ExceptionState* = nullptr);
94 }; 168 };
95 169
170 template <typename DataArrayType>
171 bool ImageData::validateConstructorArguments(const unsigned& paramFlags,
Justin Novosad 2016/12/16 18:38:11 Would be more efficient not to write this method a
zakerinasab1 2016/12/19 19:57:39 Done.
172 const IntSize* size,
173 const unsigned& width,
174 const unsigned& height,
175 const DataArrayType* data,
176 const String* colorSpace,
177 ExceptionState* exceptionState,
178 ImageDataType imageDataType) {
179 // ImageData::create parameters without ExceptionState
180 if (paramFlags & kParamSize) {
181 if (!size->width() || !size->height())
182 return false;
183 if (paramFlags & kParamData) {
184 DCHECK(data);
185 CheckedNumeric<unsigned> dataSize = 4;
186 dataSize *= size->width();
187 dataSize *= size->height();
188 if (!dataSize.IsValid() || dataSize.ValueOrDie() > data->length())
189 return false;
190 }
191 return true;
192 }
193
194 // ImageData::create parameters with ExceptionState
195 if ((paramFlags & kParamWidth) && !width) {
196 exceptionState->throwDOMException(
197 IndexSizeError, "The source width is zero or not a number.");
198 return false;
199 }
200 if ((paramFlags & kParamHeight) && !height) {
201 exceptionState->throwDOMException(
202 IndexSizeError, "The source height is zero or not a number.");
203 return false;
204 }
205 if (paramFlags & (kParamWidth | kParamHeight)) {
206 CheckedNumeric<unsigned> dataSize = 4;
207 dataSize *= width;
208 dataSize *= height;
209 if (!dataSize.IsValid()) {
210 exceptionState->throwDOMException(
211 IndexSizeError,
212 "The requested image size exceeds the supported range.");
213 return false;
214 }
215 }
216 if (paramFlags & kParamData) {
217 DCHECK(data);
218 unsigned length = data->length();
219 if (!length) {
220 exceptionState->throwDOMException(IndexSizeError,
221 "The input data has zero elements.");
222 return false;
223 }
224 if (length % 4) {
225 exceptionState->throwDOMException(
226 IndexSizeError, "The input data length is not a multiple of 4.");
227 return false;
228 }
229 length /= 4;
230 if (length % width) {
231 exceptionState->throwDOMException(
232 IndexSizeError,
233 "The input data length is not a multiple of (4 * width).");
234 return false;
235 }
236 if ((paramFlags & kParamHeight) && height != length / width) {
237 exceptionState->throwDOMException(
238 IndexSizeError,
239 "The input data length is not equal to (4 * width * height).");
240 return false;
241 }
242 }
243 if (paramFlags & kParamColorSpace) {
244 if (!colorSpace || colorSpace->length() == 0) {
245 exceptionState->throwDOMException(
246 NotSupportedError, "The source color space is not defined.");
247 return false;
248 }
249 if (imageDataType == kUint8ClampedImageData &&
250 *colorSpace != kLegacyImageDataColorSpaceName &&
251 *colorSpace != kSRGBImageDataColorSpaceName) {
252 exceptionState->throwDOMException(NotSupportedError,
253 "The input color space is not "
254 "supported in "
255 "Uint8ClampedArray-backed ImageData.");
256 return false;
257 }
258 if (imageDataType == kFloat32ImageData &&
259 *colorSpace != kLinearRGBImageDataColorSpaceName) {
260 exceptionState->throwDOMException(NotSupportedError,
261 "The input color space is not "
262 "supported in "
263 "Float32Array-backed ImageData.");
264 return false;
265 }
266 }
267 return true;
268 }
269
270 template <typename DataArrayType>
271 DataArrayType* ImageData::allocateAndValidateDataArray(
272 const unsigned& length,
273 ExceptionState* exceptionState,
274 ImageDataType imageDataType) {
275 if (!length)
276 return nullptr;
277 DataArrayType* dataArray = DataArrayType::createOrNull(length);
278 if (!dataArray || length != dataArray->length()) {
279 if (exceptionState) {
280 exceptionState->throwDOMException(
281 V8RangeError, imageDataType == kUint8ClampedImageData
282 ? "Out of memory at ImageData creation"
283 : "Out of memory at Float32ImageData creation");
284 }
285 return nullptr;
286 }
287 return dataArray;
288 }
289
96 } // namespace blink 290 } // namespace blink
97 291
98 #endif // ImageData_h 292 #endif // ImageData_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698