1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 et tw=78: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/ImageData.h"
9 #include "mozilla/CheckedInt.h"
10 #include "mozilla/HoldDropJSObjects.h"
11 #include "mozilla/dom/ImageDataBinding.h"
18 NS_IMPL_CYCLE_COLLECTING_ADDREF(ImageData
)
19 NS_IMPL_CYCLE_COLLECTING_RELEASE(ImageData
)
21 NS_IMPL_CYCLE_COLLECTION_CLASS(ImageData
)
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageData
)
24 NS_INTERFACE_MAP_ENTRY(nsISupports
)
27 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ImageData
)
28 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mData
)
29 NS_IMPL_CYCLE_COLLECTION_TRACE_END
31 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ImageData
)
32 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
33 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
35 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ImageData
)
37 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
40 already_AddRefed
<ImageData
>
41 ImageData::Constructor(const GlobalObject
& aGlobal
,
42 const uint32_t aWidth
,
43 const uint32_t aHeight
,
46 if (aWidth
== 0 || aHeight
== 0) {
47 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
50 CheckedInt
<uint32_t> length
= CheckedInt
<uint32_t>(aWidth
) * aHeight
* 4;
51 if (!length
.isValid()) {
52 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
55 js::AssertSameCompartment(aGlobal
.Context(), aGlobal
.Get());
56 JSObject
* data
= Uint8ClampedArray::Create(aGlobal
.Context(),
59 aRv
.Throw(NS_ERROR_OUT_OF_MEMORY
);
62 nsRefPtr
<ImageData
> imageData
= new ImageData(aWidth
, aHeight
, *data
);
63 return imageData
.forget();
67 already_AddRefed
<ImageData
>
68 ImageData::Constructor(const GlobalObject
& aGlobal
,
69 const Uint8ClampedArray
& aData
,
70 const uint32_t aWidth
,
71 const Optional
<uint32_t>& aHeight
,
74 aData
.ComputeLengthAndData();
76 uint32_t length
= aData
.Length();
77 if (length
== 0 || length
% 4) {
78 aRv
.Throw(NS_ERROR_DOM_INVALID_STATE_ERR
);
83 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
86 uint32_t height
= length
/ aWidth
;
87 if (length
!= aWidth
* height
||
88 (aHeight
.WasPassed() && aHeight
.Value() != height
)) {
89 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
92 nsRefPtr
<ImageData
> imageData
= new ImageData(aWidth
, height
, *aData
.Obj());
93 return imageData
.forget();
99 mozilla::HoldJSObjects(this);
103 ImageData::DropData()
107 mozilla::DropJSObjects(this);
112 ImageData::WrapObject(JSContext
* cx
)
114 return ImageDataBinding::Wrap(cx
, this);
118 } // namespace mozilla