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_END
34 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ImageData
)
36 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
39 already_AddRefed
<ImageData
> ImageData::Constructor(const GlobalObject
& aGlobal
,
40 const uint32_t aWidth
,
41 const uint32_t aHeight
,
43 if (aWidth
== 0 || aHeight
== 0) {
44 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
47 CheckedInt
<uint32_t> length
= CheckedInt
<uint32_t>(aWidth
) * aHeight
* 4;
48 if (!length
.isValid()) {
49 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
52 js::AssertSameCompartment(aGlobal
.Context(), aGlobal
.Get());
53 JSObject
* data
= Uint8ClampedArray::Create(aGlobal
.Context(), length
.value());
55 aRv
.Throw(NS_ERROR_OUT_OF_MEMORY
);
58 RefPtr
<ImageData
> imageData
= new ImageData(aWidth
, aHeight
, *data
);
59 return imageData
.forget();
63 already_AddRefed
<ImageData
> ImageData::Constructor(
64 const GlobalObject
& aGlobal
, const Uint8ClampedArray
& aData
,
65 const uint32_t aWidth
, const Optional
<uint32_t>& aHeight
,
69 uint32_t length
= aData
.Length();
70 if (length
== 0 || length
% 4) {
71 aRv
.Throw(NS_ERROR_DOM_INVALID_STATE_ERR
);
76 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
79 uint32_t height
= length
/ aWidth
;
80 if (length
!= aWidth
* height
||
81 (aHeight
.WasPassed() && aHeight
.Value() != height
)) {
82 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
85 RefPtr
<ImageData
> imageData
= new ImageData(aWidth
, height
, *aData
.Obj());
86 return imageData
.forget();
89 void ImageData::HoldData() { mozilla::HoldJSObjects(this); }
91 void ImageData::DropData() {
94 mozilla::DropJSObjects(this);
98 bool ImageData::WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
,
99 JS::MutableHandle
<JSObject
*> aReflector
) {
100 return ImageData_Binding::Wrap(aCx
, this, aGivenProto
, aReflector
);
104 already_AddRefed
<ImageData
> ImageData::ReadStructuredClone(
105 JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
106 JSStructuredCloneReader
* aReader
) {
107 // Read the information out of the stream.
108 uint32_t width
, height
;
109 JS::Rooted
<JS::Value
> dataArray(aCx
);
110 if (!JS_ReadUint32Pair(aReader
, &width
, &height
) ||
111 !JS_ReadTypedArray(aReader
, &dataArray
)) {
114 MOZ_ASSERT(dataArray
.isObject());
116 RefPtr
<ImageData
> imageData
=
117 new ImageData(width
, height
, dataArray
.toObject());
118 return imageData
.forget();
121 bool ImageData::WriteStructuredClone(JSContext
* aCx
,
122 JSStructuredCloneWriter
* aWriter
) const {
123 JS::Rooted
<JS::Value
> arrayValue(aCx
, JS::ObjectValue(*GetDataObject()));
124 if (!JS_WrapValue(aCx
, &arrayValue
)) {
128 return JS_WriteUint32Pair(aWriter
, Width(), Height()) &&
129 JS_WriteTypedArray(aWriter
, arrayValue
);
133 } // namespace mozilla