Bug 959958 - Implement ImageData constructor. r=bz

This commit is contained in:
Masatoshi Kimura 2014-01-28 22:04:40 +09:00
Родитель d535a65cce
Коммит 6cde7484de
6 изменённых файлов: 160 добавлений и 3 удалений

Просмотреть файл

@ -6,6 +6,7 @@
#include "mozilla/dom/ImageData.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/HoldDropJSObjects.h"
#include "mozilla/dom/ImageDataBinding.h"
@ -35,6 +36,59 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ImageData)
tmp->DropData();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
//static
ImageData*
ImageData::Constructor(const GlobalObject& aGlobal,
const uint32_t aWidth,
const uint32_t aHeight,
ErrorResult& aRv)
{
if (aWidth == 0 || aHeight == 0) {
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
return nullptr;
}
CheckedInt<uint32_t> length = CheckedInt<uint32_t>(aWidth) * aHeight * 4;
if (!length.isValid()) {
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
return nullptr;
}
JS::Rooted<JSObject*> obj(aGlobal.GetContext(), aGlobal.Get());
JSObject* data = Uint8ClampedArray::Create(aGlobal.GetContext(), obj,
length.value());
if (!data) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return nullptr;
}
return new ImageData(aWidth, aHeight, *data);
}
//static
ImageData*
ImageData::Constructor(const GlobalObject& aGlobal,
const Uint8ClampedArray& aData,
const uint32_t aWidth,
const Optional<uint32_t>& aHeight,
ErrorResult& aRv)
{
uint32_t length = aData.Length();
if (length == 0 || length % 4) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return nullptr;
}
length /= 4;
if (aWidth == 0) {
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
return nullptr;
}
uint32_t height = length / aWidth;
if (length != aWidth * height ||
(aHeight.WasPassed() && aHeight.Value() != height)) {
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
return nullptr;
}
return new ImageData(aWidth, height, *aData.Obj());
}
void
ImageData::HoldData()
{

Просмотреть файл

@ -10,6 +10,8 @@
#include "nsIDOMCanvasRenderingContext2D.h"
#include "mozilla/Attributes.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/TypedArray.h"
#include <stdint.h>
#include "nsCycleCollectionParticipant.h"
@ -40,6 +42,17 @@ public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(ImageData)
static ImageData* Constructor(const GlobalObject& aGlobal,
const uint32_t aWidth,
const uint32_t aHeight,
ErrorResult& aRv);
static ImageData* Constructor(const GlobalObject& aGlobal,
const Uint8ClampedArray& aData,
const uint32_t aWidth,
const Optional<uint32_t>& aHeight,
ErrorResult& aRv);
uint32_t Width() const
{
return mWidth;

Просмотреть файл

@ -97,6 +97,7 @@ support-files =
[test_drawImageIncomplete.html]
[test_drawImage_document_domain.html]
[test_drawImage_edge_cases.html]
[test_ImageData_ctor.html]
[test_isPointInStroke.html]
[test_mozDashOffset.html]
[test_mozGetAsFile.html]

Просмотреть файл

@ -0,0 +1,80 @@
<!DOCTYPE HTML><meta charset=utf-8>
<title>Canvas test: ImageData</title>
<script type="text/javascript" src="/resources/testharness.js"></script>
<script type="text/javascript" src="/resources/testharnessreport.js"></script>
<body>
<div id="log"></div>
<script>
test(function() {
assert_throws(new TypeError(), function(){ new ImageData(); });
assert_throws(new TypeError(), function(){ new ImageData(1); });
assert_throws(new TypeError(), function(){ new ImageData(new Uint8ClampedArray([1,2,3,4])); });
assert_throws("IndexSizeError", function(){ new ImageData(0,0); });
assert_throws("IndexSizeError", function(){ new ImageData(0,1); });
assert_throws("IndexSizeError", function(){ new ImageData(1,0); });
new ImageData(1,1);
new ImageData(1,2);
new ImageData(1,3);
assert_throws("IndexSizeError", function(){ new ImageData(2,0); });
new ImageData(2,1);
new ImageData(2,2);
assert_throws("IndexSizeError", function(){ new ImageData(32768,32768); });
assert_throws("IndexSizeError", function(){ new ImageData(32768,32769); });
assert_throws("IndexSizeError", function(){ new ImageData(32769,32768); });
assert_throws("IndexSizeError", function(){ new ImageData(2,536870912); });
assert_throws("IndexSizeError", function(){ new ImageData(2,536870913); });
assert_throws("IndexSizeError", function(){ new ImageData(536870912,2); });
assert_throws("IndexSizeError", function(){ new ImageData(536870913,2); });
assert_throws("InvalidStateError", function(){ new ImageData(new Uint8ClampedArray([]),0); });
assert_throws("InvalidStateError", function(){ new ImageData(new Uint8ClampedArray([]),0,0); });
assert_throws("InvalidStateError", function(){ new ImageData(new Uint8ClampedArray([]),1); });
assert_throws("InvalidStateError", function(){ new ImageData(new Uint8ClampedArray([1]),1); });
assert_throws("InvalidStateError", function(){ new ImageData(new Uint8ClampedArray([1,2]),1); });
assert_throws("InvalidStateError", function(){ new ImageData(new Uint8ClampedArray([1,2,3]),1); });
assert_throws("InvalidStateError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4,5]),1); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4]),0); });
new ImageData(new Uint8ClampedArray([1,2,3,4]),1);
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4]),2); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),0); });
new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),1);
new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),2);
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),3); });
assert_throws("InvalidStateError", function(){ new ImageData(new Uint8ClampedArray([]),1,1); });
assert_throws("InvalidStateError", function(){ new ImageData(new Uint8ClampedArray([1]),1,1); });
assert_throws("InvalidStateError", function(){ new ImageData(new Uint8ClampedArray([1,2]),1,1); });
assert_throws("InvalidStateError", function(){ new ImageData(new Uint8ClampedArray([1,2,3]),1,1); });
assert_throws("InvalidStateError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4,5]),1,1); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4]),0,0); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4]),0,1); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4]),0,2); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4]),1,0); });
new ImageData(new Uint8ClampedArray([1,2,3,4]),1,1);
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4]),1,2); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4]),2,0); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4]),2,1); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4]),2,2); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),1,1); });
new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),1,2);
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),1,3); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),2,0); });
new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),2,1);
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),2,2); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),2,536870912); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),2,536870913); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),536870912,2); });
assert_throws("IndexSizeError", function(){ new ImageData(new Uint8ClampedArray([1,2,3,4,5,6,7,8]),536870913,2); });
}, "Test constructor arguments");
test(function() {
var data = new Uint8ClampedArray([1,2,3,4,5,6,7,8]);
var imgData = new ImageData(data,1);
assert_equals(imgData.width, 1);
assert_equals(imgData.height, 2);
assert_array_equals(imgData.data, [1,2,3,4,5,6,7,8]);
data.set([8,7,6,5,4,3,2,1]);
assert_array_equals(imgData.data, [8,7,6,5,4,3,2,1]);
}, "The data argument is not copied");
</script>

Просмотреть файл

@ -8363,9 +8363,16 @@ var canvas = document.getElementById('c280');
var ctx = canvas.getContext('2d');
ok(window.ImageData !== undefined, "window.ImageData !== undefined");
try { var _thrown = false;
new window.ImageData(1,1);
} catch (e) { _thrown = true; } finally { ok(_thrown, "should throw exception"); }
var _thrown_outer = false;
try {
new window.ImageData(1,1);
} catch (e) {
_thrown_outer = true;
}
ok(!_thrown_outer, ctx.canvas.id + ' should not throw exception');
}

Просмотреть файл

@ -10,6 +10,8 @@
* You are granted a license to use, reproduce and create derivative works of this document.
*/
[Constructor(unsigned long sw, unsigned long sh),
Constructor(Uint8ClampedArray data, unsigned long sw, optional unsigned long sh)]
interface ImageData {
[Constant]
readonly attribute unsigned long width;