From 16ac8772df9544bea804cea9bfc32fe5173520fc Mon Sep 17 00:00:00 2001 From: Yury Date: Sat, 2 Apr 2011 20:45:26 +0200 Subject: [PATCH] Bug 630040 - Implement createImageData(ImageData); r=bz --- content/canvas/src/CustomQS_Canvas2D.h | 59 +++++++++--- content/canvas/test/test_canvas.html | 121 ++++++++++++++++++++++++- 2 files changed, 165 insertions(+), 15 deletions(-) diff --git a/content/canvas/src/CustomQS_Canvas2D.h b/content/canvas/src/CustomQS_Canvas2D.h index 32e593f4dc57..631d5b9555a4 100644 --- a/content/canvas/src/CustomQS_Canvas2D.h +++ b/content/canvas/src/CustomQS_Canvas2D.h @@ -23,6 +23,7 @@ * Contributor(s): * Vladimir Vukicevic (original author) * Ms2ger + * Yury * * 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, diff --git a/content/canvas/test/test_canvas.html b/content/canvas/test/test_canvas.html index cebd3d49b6fb..c1e76a9492ee 100644 --- a/content/canvas/test/test_canvas.html +++ b/content/canvas/test/test_canvas.html @@ -7227,6 +7227,24 @@ var ctx = canvas.getContext('2d'); ok(ctx.createImageData(1, 1) !== null, "ctx.createImageData(1, 1) !== null"); +} + + + + +

Canvas test: 2d.imageData.create1.basic - bug 630040

+ +

FAIL (fallback content)

+ @@ -7253,6 +7271,35 @@ for (var i = 0; i < imgdata.data.length; ++i) ok(isTransparentBlack, "isTransparentBlack"); +} + + + + +

Canvas test: 2d.imageData.create1.initial - bug 630040

+ +

FAIL (fallback content)

+ @@ -7461,6 +7508,38 @@ ok(imgdata.data.thisImplementsCanvasPixelArray, "imgdata.data.thisImplementsCanv todo(!_thrown_outer, 'should not throw exception'); +} + + + + +

Canvas test: 2d.imageData.create1.type - bug 630040

+ +

FAIL (fallback content)

+ @@ -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"); +} + + + + +

Canvas test: 2d.imageData.create1.zero - bug 630040

+ +

FAIL (fallback content)

+ @@ -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) {