OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2008 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 11 matching lines...) Expand all Loading... | |
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 | 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 | 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 | 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. | 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 */ | 27 */ |
28 | 28 |
29 #include "config.h" | 29 #include "config.h" |
30 #include "core/html/ImageData.h" | 30 #include "core/html/ImageData.h" |
31 | 31 |
32 #include "bindings/v8/ExceptionState.h" | |
33 #include "core/dom/ExceptionCode.h" | |
34 | |
32 namespace WebCore { | 35 namespace WebCore { |
33 | 36 |
34 PassRefPtr<ImageData> ImageData::create(const IntSize& size) | 37 PassRefPtr<ImageData> ImageData::create(const IntSize& size) |
35 { | 38 { |
36 Checked<int, RecordOverflow> dataSize = 4; | 39 Checked<int, RecordOverflow> dataSize = 4; |
37 dataSize *= size.width(); | 40 dataSize *= size.width(); |
38 dataSize *= size.height(); | 41 dataSize *= size.height(); |
39 if (dataSize.hasOverflowed()) | 42 if (dataSize.hasOverflowed()) |
40 return nullptr; | 43 return nullptr; |
41 | 44 |
42 return adoptRef(new ImageData(size)); | 45 return adoptRef(new ImageData(size)); |
43 } | 46 } |
44 | 47 |
45 PassRefPtr<ImageData> ImageData::create(const IntSize& size, PassRefPtr<Uint8Cla mpedArray> byteArray) | 48 PassRefPtr<ImageData> ImageData::create(const IntSize& size, PassRefPtr<Uint8Cla mpedArray> byteArray) |
46 { | 49 { |
47 Checked<int, RecordOverflow> dataSize = 4; | 50 Checked<int, RecordOverflow> dataSize = 4; |
48 dataSize *= size.width(); | 51 dataSize *= size.width(); |
49 dataSize *= size.height(); | 52 dataSize *= size.height(); |
50 if (dataSize.hasOverflowed()) | 53 if (dataSize.hasOverflowed()) |
51 return nullptr; | 54 return nullptr; |
52 | 55 |
53 if (dataSize.unsafeGet() < 0 | 56 if (dataSize.unsafeGet() < 0 |
54 || static_cast<unsigned>(dataSize.unsafeGet()) > byteArray->length()) | 57 || static_cast<unsigned>(dataSize.unsafeGet()) > byteArray->length()) |
55 return nullptr; | 58 return nullptr; |
56 | 59 |
57 return adoptRef(new ImageData(size, byteArray)); | 60 return adoptRef(new ImageData(size, byteArray)); |
58 } | 61 } |
59 | 62 |
63 PassRefPtr<ImageData> ImageData::create(unsigned width, unsigned height, Excepti onState& exceptionState) | |
64 { | |
65 if (!width || !height) { | |
66 exceptionState.throwDOMException(IndexSizeError, String::format("The sou rce %s is zero or non-finite.", width ? "height" : "width")); | |
Justin Novosad
2014/03/19 19:35:54
how can an unsigned be non-finite?
sof
2014/03/19 19:41:10
ToUInt32() maps Inf and NaNs (including non-number
| |
67 return nullptr; | |
68 } | |
69 | |
70 Checked<unsigned, RecordOverflow> dataSize = 4; | |
71 dataSize *= width; | |
72 dataSize *= height; | |
73 if (dataSize.hasOverflowed()) { | |
74 exceptionState.throwDOMException(IndexSizeError, "The requested image si ze exceeds the supported range."); | |
75 return nullptr; | |
76 } | |
77 | |
78 RefPtr<ImageData> imageData = adoptRef(new ImageData(IntSize(width, height)) ); | |
79 imageData->data()->zeroFill(); | |
80 return imageData; | |
Inactive
2014/03/19 19:07:14
nit: .release()
| |
81 } | |
82 | |
83 PassRefPtr<ImageData> ImageData::create(Uint8ClampedArray* data, unsigned width, unsigned height, ExceptionState& exceptionState) | |
84 { | |
85 if (!data) { | |
86 exceptionState.throwTypeError("Expected a Uint8ClampedArray as first arg ument."); | |
87 return nullptr; | |
88 } | |
89 if (!width) { | |
90 exceptionState.throwDOMException(IndexSizeError, "The source width is ze ro or non-finite."); | |
91 return nullptr; | |
92 } | |
93 | |
94 Uint8ClampedArray* clampedData = static_cast<Uint8ClampedArray*>(data); | |
Inactive
2014/03/19 19:07:14
Why is this cast needed?
I actually don't underst
sof
2014/03/19 22:19:16
Yes, doesn't make much sense; gone. [I belatedly d
| |
95 unsigned clampedLength = clampedData->length() / 4; | |
Justin Novosad
2014/03/19 19:35:54
According to the spec you need to throw an Invalid
sof
2014/03/19 22:19:16
Thanks, quite right. Addressed + expanded the test
| |
96 if (!clampedLength) { | |
97 exceptionState.throwDOMException(IndexSizeError, "The input data has a z ero byte length."); | |
98 return nullptr; | |
99 } | |
100 if (clampedLength % width) { | |
101 exceptionState.throwDOMException(IndexSizeError, "The byte length of the input data is not a multiple of (4 * width)."); | |
102 return nullptr; | |
103 } | |
104 if (!height) { | |
105 height = clampedLength / width; | |
106 } else if (height != clampedLength / width) { | |
107 exceptionState.throwDOMException(IndexSizeError, "The byte length of the input data is not equal to (4 * width * height)."); | |
108 return nullptr; | |
109 } | |
110 | |
111 return adoptRef(new ImageData(IntSize(width, height), clampedData)); | |
112 } | |
113 | |
60 ImageData::ImageData(const IntSize& size) | 114 ImageData::ImageData(const IntSize& size) |
61 : m_size(size) | 115 : m_size(size) |
62 , m_data(Uint8ClampedArray::createUninitialized(size.width() * size.height() * 4)) | 116 , m_data(Uint8ClampedArray::createUninitialized(size.width() * size.height() * 4)) |
63 { | 117 { |
64 ScriptWrappable::init(this); | 118 ScriptWrappable::init(this); |
65 } | 119 } |
66 | 120 |
67 ImageData::ImageData(const IntSize& size, PassRefPtr<Uint8ClampedArray> byteArra y) | 121 ImageData::ImageData(const IntSize& size, PassRefPtr<Uint8ClampedArray> byteArra y) |
68 : m_size(size) | 122 : m_size(size) |
69 , m_data(byteArray) | 123 , m_data(byteArray) |
70 { | 124 { |
71 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<unsigned>(size.width() * size.h eight() * 4) <= m_data->length()); | 125 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<unsigned>(size.width() * size.h eight() * 4) <= m_data->length()); |
72 ScriptWrappable::init(this); | 126 ScriptWrappable::init(this); |
73 } | 127 } |
74 | 128 |
75 } | 129 } |
76 | 130 |
OLD | NEW |