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 not a number.", width ? "height" : "width")); |
| 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.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 not a number."); |
| 91 return nullptr; |
| 92 } |
| 93 |
| 94 unsigned length = data->length(); |
| 95 if (!length) { |
| 96 exceptionState.throwDOMException(IndexSizeError, "The input data has a z
ero byte length."); |
| 97 return nullptr; |
| 98 } |
| 99 if (length % 4) { |
| 100 exceptionState.throwDOMException(IndexSizeError, "The input data byte le
ngth is not a multiple of 4."); |
| 101 return nullptr; |
| 102 } |
| 103 length /= 4; |
| 104 if (length % width) { |
| 105 exceptionState.throwDOMException(IndexSizeError, "The input data byte le
ngth is not a multiple of (4 * width)."); |
| 106 return nullptr; |
| 107 } |
| 108 if (!height) { |
| 109 height = length / width; |
| 110 } else if (height != length / width) { |
| 111 exceptionState.throwDOMException(IndexSizeError, "The input data byte le
ngth is not equal to (4 * width * height)."); |
| 112 return nullptr; |
| 113 } |
| 114 |
| 115 return adoptRef(new ImageData(IntSize(width, height), data)); |
| 116 } |
| 117 |
60 ImageData::ImageData(const IntSize& size) | 118 ImageData::ImageData(const IntSize& size) |
61 : m_size(size) | 119 : m_size(size) |
62 , m_data(Uint8ClampedArray::createUninitialized(size.width() * size.height()
* 4)) | 120 , m_data(Uint8ClampedArray::createUninitialized(size.width() * size.height()
* 4)) |
63 { | 121 { |
64 ScriptWrappable::init(this); | 122 ScriptWrappable::init(this); |
65 } | 123 } |
66 | 124 |
67 ImageData::ImageData(const IntSize& size, PassRefPtr<Uint8ClampedArray> byteArra
y) | 125 ImageData::ImageData(const IntSize& size, PassRefPtr<Uint8ClampedArray> byteArra
y) |
68 : m_size(size) | 126 : m_size(size) |
69 , m_data(byteArray) | 127 , m_data(byteArray) |
70 { | 128 { |
71 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<unsigned>(size.width() * size.h
eight() * 4) <= m_data->length()); | 129 ASSERT_WITH_SECURITY_IMPLICATION(static_cast<unsigned>(size.width() * size.h
eight() * 4) <= m_data->length()); |
72 ScriptWrappable::init(this); | 130 ScriptWrappable::init(this); |
73 } | 131 } |
74 | 132 |
75 } | 133 } |
76 | 134 |
OLD | NEW |