Bug 630040 - Implement createImageData(ImageData); r=bz

This commit is contained in:
Yury 2011-04-02 20:45:26 +02:00
Родитель c28204bb2d
Коммит 16ac8772df
2 изменённых файлов: 165 добавлений и 15 удалений

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

@ -23,6 +23,7 @@
* Contributor(s):
* Vladimir Vukicevic <vladimir@pobox.com> (original author)
* Ms2ger <ms2ger@gmail.com>
* Yury <async.processingjs@yahoo.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
@ -213,6 +214,32 @@ CreateImageData(JSContext* cx,
return true;
}
static bool
GetImageDataDimensions(JSContext *cx, JSObject *dataObject, uint32 *width, uint32 *height)
{
jsval temp;
int32 wi, hi;
// Need to check that dataObject is ImageData object. That's hard for the moment
// because they're just vanilla objects in our implementation.
// Let's guess, if the object has valid width and height then it's suitable
// for this operation.
if (!JS_GetProperty(cx, dataObject, "width", &temp) ||
!JS_ValueToECMAInt32(cx, temp, &wi))
return false;
if (!JS_GetProperty(cx, dataObject, "height", &temp) ||
!JS_ValueToECMAInt32(cx, temp, &hi))
return false;
if (wi <= 0 || hi <= 0)
return xpc_qsThrow(cx, NS_ERROR_DOM_INDEX_SIZE_ERR);
*width = (uint32)wi;
*height = (uint32)hi;
return true;
}
static JSBool
nsIDOMCanvasRenderingContext2D_CreateImageData(JSContext *cx, uintN argc, jsval *vp)
{
@ -220,11 +247,26 @@ nsIDOMCanvasRenderingContext2D_CreateImageData(JSContext *cx, uintN argc, jsval
/* Note: this doesn't need JS_THIS_OBJECT */
if (argc < 2)
if (argc < 1)
return xpc_qsThrow(cx, NS_ERROR_XPC_NOT_ENOUGH_ARGS);
jsval *argv = JS_ARGV(cx, vp);
if (argc == 1) {
// The specification asks to throw NOT_SUPPORTED if first argument is NULL,
// An object is expected, so throw an exception for all primitives.
if (JSVAL_IS_PRIMITIVE(argv[0]))
return xpc_qsThrow(cx, NS_ERROR_DOM_NOT_SUPPORTED_ERR);
JSObject *dataObject = JSVAL_TO_OBJECT(argv[0]);
uint32 data_width, data_height;
if (!GetImageDataDimensions(cx, dataObject, &data_width, &data_height))
return false;
return CreateImageData(cx, data_width, data_height, NULL, 0, 0, vp);
}
jsdouble width, height;
if (!JS_ValueToNumber(cx, argv[0], &width) ||
!JS_ValueToNumber(cx, argv[1], &height))
@ -332,26 +374,15 @@ nsIDOMCanvasRenderingContext2D_PutImageData(JSContext *cx, uintN argc, jsval *vp
!JS_ValueToECMAInt32(cx, argv[2], &y))
return JS_FALSE;
int32 wi, hi;
uint32 w, h;
JSObject *darray;
// grab width, height, and the dense array from the dataObject
js::AutoValueRooter tv(cx);
if (!JS_GetProperty(cx, dataObject, "width", tv.jsval_addr()) ||
!JS_ValueToECMAInt32(cx, tv.jsval_value(), &wi))
if (!GetImageDataDimensions(cx, dataObject, &w, &h))
return JS_FALSE;
if (!JS_GetProperty(cx, dataObject, "height", tv.jsval_addr()) ||
!JS_ValueToECMAInt32(cx, tv.jsval_value(), &hi))
return JS_FALSE;
if (wi <= 0 || hi <= 0)
return xpc_qsThrow(cx, NS_ERROR_DOM_INDEX_SIZE_ERR);
uint32 w = (uint32) wi;
uint32 h = (uint32) hi;
// the optional dirty rect
PRBool hasDirtyRect = PR_FALSE;
int32 dirtyX = 0,

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

@ -7227,6 +7227,24 @@ var ctx = canvas.getContext('2d');
ok(ctx.createImageData(1, 1) !== null, "ctx.createImageData(1, 1) !== null");
}
</script>
<!-- [[[ test_2d.imageData.create1.basic.html ]]] -->
<p>Canvas test: 2d.imageData.create1.basic - bug 630040</p>
<!-- Testing: createImageData(imgdata) exists and returns something -->
<canvas id="c254a" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
<script>
function test_2d_imageData_create1_basic() {
var canvas = document.getElementById('c254a');
var ctx = canvas.getContext('2d');
ok(ctx.createImageData(ctx.createImageData(1, 1)) != null, "ctx.createImageData(ctx.createImageData(1, 1)) != null");
}
</script>
@ -7253,6 +7271,35 @@ for (var i = 0; i < imgdata.data.length; ++i)
ok(isTransparentBlack, "isTransparentBlack");
}
</script>
<!-- [[[ test_2d.imageData.create1.initial.html ]]] -->
<p>Canvas test: 2d.imageData.create1.initial - bug 630040</p>
<!-- Testing: createImageData(imgdata) returns transparent black data of the right size -->
<canvas id="c255a" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
<script>
function test_2d_imageData_create1_initial() {
var canvas = document.getElementById('c255a');
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 100, 50);
var imgdata1 = ctx.getImageData(0, 0, 10, 20);
var imgdata2 = ctx.createImageData(imgdata1);
ok(imgdata2.data.length == imgdata1.data.length, "imgdata2.data.length == imgdata1.data.length");
ok(imgdata2.width == imgdata1.width, "imgdata2.width == imgdata1.width");
ok(imgdata2.height == imgdata1.height, "imgdata2.height == imgdata1.height");
var isTransparentBlack = true;
for (var i = 0; i < imgdata2.data.length; ++i)
if (imgdata2.data[i] !== 0)
isTransparentBlack = false;
ok(isTransparentBlack, "isTransparentBlack");
}
</script>
@ -7461,6 +7508,38 @@ ok(imgdata.data.thisImplementsCanvasPixelArray, "imgdata.data.thisImplementsCanv
todo(!_thrown_outer, 'should not throw exception');
}
</script>
<!-- [[[ test_2d.imageData.create1.type.html ]]] -->
<p>Canvas test: 2d.imageData.create1.type - bug 630040</p>
<!-- Testing: createImageData(imgdata) returns an ImageData object containing a CanvasPixelArray object -->
<canvas id="c261a" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
<script>
function test_2d_imageData_create1_type() {
var canvas = document.getElementById('c261a');
var ctx = canvas.getContext('2d');
var _thrown_outer = false;
try {
todo(window.ImageData !== undefined, "window.ImageData !== undefined");
todo(window.CanvasPixelArray !== undefined, "window.CanvasPixelArray !== undefined");
window.ImageData.prototype.thisImplementsImageData = true;
window.CanvasPixelArray.prototype.thisImplementsCanvasPixelArray = true;
var imgdata = ctx.createImageData(ctx.createImageData(1, 1));
todo(imgdata.thisImplementsImageData, "imgdata.thisImplementsImageData");
todo(imgdata.data.thisImplementsCanvasPixelArray, "imgdata.data.thisImplementsCanvasPixelArray");
} catch (e) {
_thrown_outer = true;
}
todo(!_thrown_outer, 'should not throw exception');
}
</script>
@ -7487,6 +7566,26 @@ var _thrown = undefined; try {
} catch (e) { _thrown = e }; ok(_thrown && _thrown.code == DOMException.INDEX_SIZE_ERR, "should throw INDEX_SIZE_ERR");
}
</script>
<!-- [[[ test_2d.imageData.create1.zero.html ]]] -->
<p>Canvas test: 2d.imageData.create1.zero - bug 630040</p>
<!-- Testing: createImageData(null) throws NOT_SUPPORTED_ERR -->
<canvas id="c262a" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
<script>
function test_2d_imageData_create1_zero() {
var canvas = document.getElementById('c262a');
var ctx = canvas.getContext('2d');
var _thrown = undefined; try {
ctx.createImageData(null);
} catch (e) { _thrown = e }; ok(_thrown && _thrown.code == DOMException.NOT_SUPPORTED_ERR, "should throw NOT_SUPPORTED_ERR");
}
</script>
@ -10552,7 +10651,7 @@ if (ctx.createImageData) {
} catch (e) { _thrown = e }; todo(_thrown && _thrown.code == DOMException.NOT_SUPPORTED_ERR, "should throw NOT_SUPPORTED_ERR");
var _thrown = undefined; try {
ctx.createImageData(1);
} catch (e) { _thrown = e }; todo(_thrown && _thrown.code == DOMException.NOT_SUPPORTED_ERR, "should throw NOT_SUPPORTED_ERR");
} catch (e) { _thrown = e }; ok(_thrown && _thrown.code == DOMException.NOT_SUPPORTED_ERR, "should throw NOT_SUPPORTED_ERR");
}
if (ctx.getImageData) {
var _thrown = undefined; try {
@ -22236,11 +22335,21 @@ function runTests() {
} catch (e) {
ok(false, "unexpected exception thrown in: test_2d_imageData_create_basic");
}
try {
test_2d_imageData_create1_basic();
} catch (e) {
ok(false, "unexpected exception thrown in: test_2d_imageData_create1_basic");
}
try {
test_2d_imageData_create_initial();
} catch (e) {
ok(false, "unexpected exception thrown in: test_2d_imageData_create_initial");
}
try {
test_2d_imageData_create1_initial();
} catch (e) {
ok(false, "unexpected exception thrown in: test_2d_imageData_create1_initial");
}
try {
test_2d_imageData_create_large();
} catch (e) {
@ -22271,11 +22380,21 @@ function runTests() {
} catch (e) {
ok(false, "unexpected exception thrown in: test_2d_imageData_create_type");
}
try {
test_2d_imageData_create1_type();
} catch (e) {
ok(false, "unexpected exception thrown in: test_2d_imageData_create1_type");
}
try {
test_2d_imageData_create_zero();
} catch (e) {
ok(false, "unexpected exception thrown in: test_2d_imageData_create_zero");
}
try {
test_2d_imageData_create1_zero();
} catch (e) {
ok(false, "unexpected exception thrown in: test_2d_imageData_create1_zero");
}
try {
test_2d_imageData_get_basic();
} catch (e) {