From 96005f2ac323d55c6879817f2dd49b2c4121677e Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 13:02:08 -0800 Subject: [PATCH 01/18] Started putImageData() --- src/CanvasRenderingContext2d.cc | 39 +++++++++++++++++++++++++++++++++ src/CanvasRenderingContext2d.h | 1 + 2 files changed, 40 insertions(+) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 83a80a7..0fd1cfc 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -10,6 +10,7 @@ #include #include "Canvas.h" #include "Image.h" +#include "ImageData.h" #include "CanvasRenderingContext2d.h" #include "CanvasGradient.h" @@ -65,6 +66,7 @@ Context2d::Initialize(Handle target) { // Prototype Local proto = t->PrototypeTemplate(); NODE_SET_PROTOTYPE_METHOD(t, "drawImage", DrawImage); + NODE_SET_PROTOTYPE_METHOD(t, "putImageData", PutImageData); NODE_SET_PROTOTYPE_METHOD(t, "save", Save); NODE_SET_PROTOTYPE_METHOD(t, "restore", Restore); NODE_SET_PROTOTYPE_METHOD(t, "rotate", Rotate); @@ -381,6 +383,43 @@ Context2d::New(const Arguments &args) { return args.This(); } +/* + * Put image data. + */ + +Handle +Context2d::PutImageData(const Arguments &args) { + HandleScope scope; + // TODO: validate + Context2d *context = ObjectWrap::Unwrap(args.This()); + ImageData *imageData = ObjectWrap::Unwrap(args[0]->ToObject()); + PixelArray *arr = imageData->pixelArray(); + + uint8_t *src = arr->data(); + uint8_t *dst = context->canvas()->data(); + + int srcStride = arr->stride() + , dstStride = context->canvas()->stride(); + + int dx = args[1]->NumberValue() + , dy = args[1]->NumberValue() + , dw = arr->width() + , dh = arr->height(); + + + for (int y = 0; y < dh; ++y) { + uint32_t *srcRow = (uint32_t *)(src + srcStride * (y + dy)); + uint32_t *dstRow = (uint32_t *)(dst + dstStride * (y + dy)); + for (int x = 0; x < dw; ++x) { + uint32_t *srcPixel = srcRow + x + dx; + uint32_t *dstPixel = dstRow + x + dx; + *dstPixel = *srcPixel; + } + } + + return Undefined(); +} + /* * Draw image src image to the destination (context). * diff --git a/src/CanvasRenderingContext2d.h b/src/CanvasRenderingContext2d.h index f8f413b..b702301 100644 --- a/src/CanvasRenderingContext2d.h +++ b/src/CanvasRenderingContext2d.h @@ -48,6 +48,7 @@ class Context2d: public node::ObjectWrap { static void Initialize(Handle target); static Handle New(const Arguments &args); static Handle DrawImage(const Arguments &args); + static Handle PutImageData(const Arguments &args); static Handle Save(const Arguments &args); static Handle Restore(const Arguments &args); static Handle Rotate(const Arguments &args); From ca101f9fa6afec6f8cd80e1365faca42a8cfec79 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 13:30:06 -0800 Subject: [PATCH 02/18] Misc refactoring --- src/PixelArray.cc | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/PixelArray.cc b/src/PixelArray.cc index 1f9ad7a..1200431 100644 --- a/src/PixelArray.cc +++ b/src/PixelArray.cc @@ -94,12 +94,17 @@ PixelArray::PixelArray(Canvas *canvas, int sx, int sy, int width, int height): int bx = x * 4; uint32_t *pixel = row + x; - // premultiplied + // ARGB uint8_t a = *pixel >> 24; + uint8_t r = *pixel >> 16; + uint8_t g = *pixel >> 8; + uint8_t b = *pixel; + + // undo premultiplication dst[bx + 3] = a; - dst[bx + 0] = (*pixel >> 16) * 255 / a; - dst[bx + 1] = (*pixel >> 8) * 255 / a; - dst[bx + 2] = *pixel * 255 / a; + dst[bx + 0] = r * 255 / a; + dst[bx + 1] = g * 255 / a; + dst[bx + 2] = b * 255 / a; } dst += s; } From 089e5ed8e7db58d71dfea0967adc91c296d65192 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 13:31:08 -0800 Subject: [PATCH 03/18] Todo --- src/CanvasRenderingContext2d.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 0fd1cfc..2cae803 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -417,6 +417,8 @@ Context2d::PutImageData(const Arguments &args) { } } + // TODO: cairo_surface_mark_dirty_rectangle + return Undefined(); } From e153fd97f87e5bf5e9ff3a4c91091ade50aa607d Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 13:41:02 -0800 Subject: [PATCH 04/18] Fixed getImageData() --- src/PixelArray.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/PixelArray.cc b/src/PixelArray.cc index 1200431..0c98bae 100644 --- a/src/PixelArray.cc +++ b/src/PixelArray.cc @@ -85,11 +85,12 @@ PixelArray::PixelArray(Canvas *canvas, int sx, int sy, int width, int height): // Alloc space for our new data uint8_t *dst = alloc(); uint8_t *src = canvas->data(); - int s = stride(); + int srcStride = canvas->stride() + , dstStride = stride(); // Normalize data (argb -> rgba) for (int y = 0; y < height; ++y) { - uint32_t *row = (uint32_t *)(src + s * y); + uint32_t *row = (uint32_t *)(src + srcStride * y); for (int x = 0; x < width; ++x) { int bx = x * 4; uint32_t *pixel = row + x; @@ -106,7 +107,7 @@ PixelArray::PixelArray(Canvas *canvas, int sx, int sy, int width, int height): dst[bx + 1] = g * 255 / a; dst[bx + 2] = b * 255 / a; } - dst += s; + dst += dstStride; } } From 9dcda666e4f020081a3bde31b0e040f8a24abda1 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 14:02:26 -0800 Subject: [PATCH 05/18] Fixing putImageData() --- src/CanvasRenderingContext2d.cc | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 2cae803..947b3b0 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -401,20 +401,25 @@ Context2d::PutImageData(const Arguments &args) { int srcStride = arr->stride() , dstStride = context->canvas()->stride(); - int dx = args[1]->NumberValue() - , dy = args[1]->NumberValue() + int sx = 0 + , sy = 0 + , dx = args[1]->NumberValue() + , dy = args[2]->NumberValue() , dw = arr->width() , dh = arr->height(); - + uint8_t *srcRows = src + sy + srcStride + sx * 4; for (int y = 0; y < dh; ++y) { - uint32_t *srcRow = (uint32_t *)(src + srcStride * (y + dy)); - uint32_t *dstRow = (uint32_t *)(dst + dstStride * (y + dy)); + uint32_t *row = (uint32_t *)(dst + dstStride * (y + dy)); for (int x = 0; x < dw; ++x) { - uint32_t *srcPixel = srcRow + x + dx; - uint32_t *dstPixel = dstRow + x + dx; - *dstPixel = *srcPixel; + int bx = x * 4; + uint32_t *pixel = row + x + dx; + *pixel = srcRows[bx + 3] << 24 + | srcRows[bx + 0] << 16 + | srcRows[bx + 1] << 8 + | srcRows[bx + 2]; } + srcRows += srcStride; } // TODO: cairo_surface_mark_dirty_rectangle From 71a6ed5c065ba987c3e7aff28141759318f95ea6 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 14:11:10 -0800 Subject: [PATCH 06/18] Misc refactoring of PutImageData() --- src/CanvasRenderingContext2d.cc | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 947b3b0..0c2e55c 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -414,10 +414,18 @@ Context2d::PutImageData(const Arguments &args) { for (int x = 0; x < dw; ++x) { int bx = x * 4; uint32_t *pixel = row + x + dx; - *pixel = srcRows[bx + 3] << 24 - | srcRows[bx + 0] << 16 - | srcRows[bx + 1] << 8 - | srcRows[bx + 2]; + + // RGBA + uint8_t a = srcRows[bx + 3]; + uint8_t r = srcRows[bx + 0]; + uint8_t g = srcRows[bx + 1]; + uint8_t b = srcRows[bx + 3]; + + // ARGB + *pixel = a << 24 + | r << 16 + | g << 8 + | b; } srcRows += srcStride; } From 345e87319b49c79b2ff34256be0768309124090f Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 14:11:43 -0800 Subject: [PATCH 07/18] typo --- src/CanvasRenderingContext2d.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 0c2e55c..a11d3c2 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -419,7 +419,7 @@ Context2d::PutImageData(const Arguments &args) { uint8_t a = srcRows[bx + 3]; uint8_t r = srcRows[bx + 0]; uint8_t g = srcRows[bx + 1]; - uint8_t b = srcRows[bx + 3]; + uint8_t b = srcRows[bx + 2]; // ARGB *pixel = a << 24 From 27ae323c8deba990e531e8aff2a61eb410f90384 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 14:23:04 -0800 Subject: [PATCH 08/18] Fixed source x/y support for getImageData() --- src/CanvasRenderingContext2d.cc | 2 +- src/PixelArray.cc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index a11d3c2..5f6cd5b 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -408,7 +408,7 @@ Context2d::PutImageData(const Arguments &args) { , dw = arr->width() , dh = arr->height(); - uint8_t *srcRows = src + sy + srcStride + sx * 4; + uint8_t *srcRows = src; for (int y = 0; y < dh; ++y) { uint32_t *row = (uint32_t *)(dst + dstStride * (y + dy)); for (int x = 0; x < dw; ++x) { diff --git a/src/PixelArray.cc b/src/PixelArray.cc index 0c98bae..fb83eef 100644 --- a/src/PixelArray.cc +++ b/src/PixelArray.cc @@ -90,10 +90,10 @@ PixelArray::PixelArray(Canvas *canvas, int sx, int sy, int width, int height): // Normalize data (argb -> rgba) for (int y = 0; y < height; ++y) { - uint32_t *row = (uint32_t *)(src + srcStride * y); + uint32_t *row = (uint32_t *)(src + srcStride * (y + sy)); for (int x = 0; x < width; ++x) { int bx = x * 4; - uint32_t *pixel = row + x; + uint32_t *pixel = row + x + sx; // ARGB uint8_t a = *pixel >> 24; From bfa1108de9af261f6095a8b823ed78a27a8f6a99 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 14:38:25 -0800 Subject: [PATCH 09/18] More putImageData() --- src/CanvasRenderingContext2d.cc | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 5f6cd5b..18aee4c 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -385,6 +385,10 @@ Context2d::New(const Arguments &args) { /* * Put image data. + * + * - imageData, dx, dy + * - imageData, dx, dy, sx, sy, dw, dh + * */ Handle @@ -405,13 +409,30 @@ Context2d::PutImageData(const Arguments &args) { , sy = 0 , dx = args[1]->NumberValue() , dy = args[2]->NumberValue() - , dw = arr->width() - , dh = arr->height(); + , dw + , dh + , rows + , cols; + + switch (args.Length()) { + case 3: + cols = arr->width(); + rows = arr->height(); + break; + case 7: + sx = args[3]->NumberValue(); + sx = args[4]->NumberValue(); + dw = args[5]->NumberValue(); + dh = args[6]->NumberValue(); + break; + default: + return ThrowException(Exception::Error(String::New("invalid arguments"))); + } uint8_t *srcRows = src; - for (int y = 0; y < dh; ++y) { + for (int y = 0; y < rows; ++y) { uint32_t *row = (uint32_t *)(dst + dstStride * (y + dy)); - for (int x = 0; x < dw; ++x) { + for (int x = 0; x < cols; ++x) { int bx = x * 4; uint32_t *pixel = row + x + dx; From 9d424cb1c8f3f90520ea87aff3d225f7ed6dbc70 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 16:47:55 -0800 Subject: [PATCH 10/18] Fixed floating point exception in PixelArray() --- src/PixelArray.cc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/PixelArray.cc b/src/PixelArray.cc index fb83eef..528ac98 100644 --- a/src/PixelArray.cc +++ b/src/PixelArray.cc @@ -103,9 +103,16 @@ PixelArray::PixelArray(Canvas *canvas, int sx, int sy, int width, int height): // undo premultiplication dst[bx + 3] = a; - dst[bx + 0] = r * 255 / a; - dst[bx + 1] = g * 255 / a; - dst[bx + 2] = b * 255 / a; + // TODO: abstract + if (a) { + dst[bx + 0] = r * 255 / a; + dst[bx + 1] = g * 255 / a; + dst[bx + 2] = b * 255 / a; + } else { + dst[bx + 0] = r; + dst[bx + 1] = g; + dst[bx + 2] = b; + } } dst += dstStride; } From 3cb4d9cc92490e3fe6bbdefde1f51749926ebc69 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 16:53:22 -0800 Subject: [PATCH 11/18] Started putImageData() tests --- test/public/tests.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/public/tests.js b/test/public/tests.js index 9803589..f1e320e 100644 --- a/test/public/tests.js +++ b/test/public/tests.js @@ -1440,4 +1440,40 @@ tests['drawImage(img,sx,sy,sw,sh,x,y,w,h)'] = function(ctx, done){ done(); }; img.src = 'state.png'; +}; + +tests['putImageData(dx,dy)'] = function(ctx){ + for (i=0;i<6;i++){ + for (j=0;j<6;j++){ + ctx.fillStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' + + Math.floor(255-42.5*j) + ',0)'; + ctx.fillRect(j*25,i*25,25,25); + } + } + var data = ctx.getImageData(0,0,50,50); + ctx.putImageData(data,10,10); +}; + +tests['putImageData(dx,dy) 2'] = function(ctx){ + for (i=0;i<6;i++){ + for (j=0;j<6;j++){ + ctx.fillStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' + + Math.floor(255-42.5*j) + ',0)'; + ctx.fillRect(j*25,i*25,25,25); + } + } + var data = ctx.getImageData(25,25,50,50); + ctx.putImageData(data,10,10); +}; + +tests['putImageData(dx,dy) 3'] = function(ctx){ + for (i=0;i<6;i++){ + for (j=0;j<6;j++){ + ctx.fillStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' + + Math.floor(255-42.5*j) + ',0)'; + ctx.fillRect(j*25,i*25,25,25); + } + } + var data = ctx.getImageData(10,25,10,50); + ctx.putImageData(data,50,10); }; \ No newline at end of file From 63ac5f52a1f507e2bb3d7900758ba895e66d8722 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 16:53:43 -0800 Subject: [PATCH 12/18] Misc --- test/public/tests.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/test/public/tests.js b/test/public/tests.js index f1e320e..3ecf3fc 100644 --- a/test/public/tests.js +++ b/test/public/tests.js @@ -1442,7 +1442,7 @@ tests['drawImage(img,sx,sy,sw,sh,x,y,w,h)'] = function(ctx, done){ img.src = 'state.png'; }; -tests['putImageData(dx,dy)'] = function(ctx){ +tests['putImageData()'] = function(ctx){ for (i=0;i<6;i++){ for (j=0;j<6;j++){ ctx.fillStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' + @@ -1454,7 +1454,7 @@ tests['putImageData(dx,dy)'] = function(ctx){ ctx.putImageData(data,10,10); }; -tests['putImageData(dx,dy) 2'] = function(ctx){ +tests['putImageData() 2'] = function(ctx){ for (i=0;i<6;i++){ for (j=0;j<6;j++){ ctx.fillStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' + @@ -1466,7 +1466,19 @@ tests['putImageData(dx,dy) 2'] = function(ctx){ ctx.putImageData(data,10,10); }; -tests['putImageData(dx,dy) 3'] = function(ctx){ +tests['putImageData() 3'] = function(ctx){ + for (i=0;i<6;i++){ + for (j=0;j<6;j++){ + ctx.fillStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' + + Math.floor(255-42.5*j) + ',0)'; + ctx.fillRect(j*25,i*25,25,25); + } + } + var data = ctx.getImageData(10,25,10,50); + ctx.putImageData(data,50,10); +}; + +tests['putImageData() 4'] = function(ctx){ for (i=0;i<6;i++){ for (j=0;j<6;j++){ ctx.fillStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' + From 59adcbe4a41c71a0a25a225ee25286c0dc9f2074 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 17:05:30 -0800 Subject: [PATCH 13/18] Added another putImageData() test --- test/public/tests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/public/tests.js b/test/public/tests.js index 3ecf3fc..8a82524 100644 --- a/test/public/tests.js +++ b/test/public/tests.js @@ -1486,6 +1486,6 @@ tests['putImageData() 4'] = function(ctx){ ctx.fillRect(j*25,i*25,25,25); } } - var data = ctx.getImageData(10,25,10,50); - ctx.putImageData(data,50,10); + var data = ctx.getImageData(0,0,50,50); + ctx.putImageData(data,30,30,10,10,30,30); }; \ No newline at end of file From d4a60bb8abdf786df3ab8765286c960a82d0a8a6 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 17:26:58 -0800 Subject: [PATCH 14/18] More tests --- src/CanvasRenderingContext2d.cc | 8 +++----- test/public/tests.js | 12 ++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 18aee4c..7d57282 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -409,8 +409,6 @@ Context2d::PutImageData(const Arguments &args) { , sy = 0 , dx = args[1]->NumberValue() , dy = args[2]->NumberValue() - , dw - , dh , rows , cols; @@ -421,9 +419,9 @@ Context2d::PutImageData(const Arguments &args) { break; case 7: sx = args[3]->NumberValue(); - sx = args[4]->NumberValue(); - dw = args[5]->NumberValue(); - dh = args[6]->NumberValue(); + sy = args[4]->NumberValue(); + cols = args[5]->NumberValue(); + rows = args[6]->NumberValue(); break; default: return ThrowException(Exception::Error(String::New("invalid arguments"))); diff --git a/test/public/tests.js b/test/public/tests.js index 8a82524..dc86f43 100644 --- a/test/public/tests.js +++ b/test/public/tests.js @@ -1488,4 +1488,16 @@ tests['putImageData() 4'] = function(ctx){ } var data = ctx.getImageData(0,0,50,50); ctx.putImageData(data,30,30,10,10,30,30); +}; + +tests['putImageData() 5'] = function(ctx){ + for (i=0;i<6;i++){ + for (j=0;j<6;j++){ + ctx.fillStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' + + Math.floor(255-42.5*j) + ',0)'; + ctx.fillRect(j*25,i*25,25,25); + } + } + var data = ctx.getImageData(0,0,50,50); + ctx.putImageData(data,60,60,30,10,50,30); }; \ No newline at end of file From 00059e167332db013bf16dd78c59cee8a067fe87 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 17:50:09 -0800 Subject: [PATCH 15/18] Strokes for tests --- test/public/tests.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/public/tests.js b/test/public/tests.js index dc86f43..9d3b344 100644 --- a/test/public/tests.js +++ b/test/public/tests.js @@ -1486,6 +1486,7 @@ tests['putImageData() 4'] = function(ctx){ ctx.fillRect(j*25,i*25,25,25); } } + ctx.strokeRect(30,30,30,30); var data = ctx.getImageData(0,0,50,50); ctx.putImageData(data,30,30,10,10,30,30); }; @@ -1498,6 +1499,7 @@ tests['putImageData() 5'] = function(ctx){ ctx.fillRect(j*25,i*25,25,25); } } + ctx.strokeRect(60,60,50,30); var data = ctx.getImageData(0,0,50,50); ctx.putImageData(data,60,60,30,10,50,30); }; \ No newline at end of file From 3a414be06dcceae2295a75ebef9afedd6bd7c0be Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 17:53:33 -0800 Subject: [PATCH 16/18] More tests --- test/public/tests.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/public/tests.js b/test/public/tests.js index 9d3b344..8b82300 100644 --- a/test/public/tests.js +++ b/test/public/tests.js @@ -1492,6 +1492,19 @@ tests['putImageData() 4'] = function(ctx){ }; tests['putImageData() 5'] = function(ctx){ + for (i=0;i<6;i++){ + for (j=0;j<6;j++){ + ctx.fillStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' + + Math.floor(255-42.5*j) + ',0)'; + ctx.fillRect(j*25,i*25,25,25); + } + } + ctx.strokeRect(60,60,50,30); + var data = ctx.getImageData(0,0,50,50); + ctx.putImageData(data,60,60,0,0,50,30); +}; + +tests['putImageData() 6'] = function(ctx){ for (i=0;i<6;i++){ for (j=0;j<6;j++){ ctx.fillStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' + From f266fc3e5ec756f3fbdfab7155f6a1b1400bdc9e Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 18:08:48 -0800 Subject: [PATCH 17/18] Fixed putImageData() src --- src/CanvasRenderingContext2d.cc | 14 ++++++++++---- test/public/tests.js | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 7d57282..2cb3489 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -407,6 +407,8 @@ Context2d::PutImageData(const Arguments &args) { int sx = 0 , sy = 0 + , sw = 0 + , sh = 0 , dx = args[1]->NumberValue() , dy = args[2]->NumberValue() , rows @@ -417,17 +419,21 @@ Context2d::PutImageData(const Arguments &args) { cols = arr->width(); rows = arr->height(); break; - case 7: + case 7: { sx = args[3]->NumberValue(); sy = args[4]->NumberValue(); - cols = args[5]->NumberValue(); - rows = args[6]->NumberValue(); + sw = args[5]->NumberValue(); + sh = args[6]->NumberValue(); + cols = sw; + rows = sh; + //dx += sx + sw; + } break; default: return ThrowException(Exception::Error(String::New("invalid arguments"))); } - uint8_t *srcRows = src; + uint8_t *srcRows = src + sy * srcStride + sx * 4; for (int y = 0; y < rows; ++y) { uint32_t *row = (uint32_t *)(dst + dstStride * (y + dy)); for (int x = 0; x < cols; ++x) { diff --git a/test/public/tests.js b/test/public/tests.js index 8b82300..cc6cd41 100644 --- a/test/public/tests.js +++ b/test/public/tests.js @@ -1514,5 +1514,5 @@ tests['putImageData() 6'] = function(ctx){ } ctx.strokeRect(60,60,50,30); var data = ctx.getImageData(0,0,50,50); - ctx.putImageData(data,60,60,30,10,50,30); + ctx.putImageData(data,60,60,10,0,35,30); }; \ No newline at end of file From 721c5a5e5a1dffa5392250680d16ba92dd4e3aa0 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 15 Nov 2010 18:20:29 -0800 Subject: [PATCH 18/18] Marking dirty rect --- src/CanvasRenderingContext2d.cc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index 2cb3489..964c229 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -414,11 +414,14 @@ Context2d::PutImageData(const Arguments &args) { , rows , cols; + // TODO: spec boundaries switch (args.Length()) { + // imageData, dx, dy case 3: cols = arr->width(); rows = arr->height(); break; + // imageData, dx, dy, sx, sy, dw, dh case 7: { sx = args[3]->NumberValue(); sy = args[4]->NumberValue(); @@ -426,7 +429,8 @@ Context2d::PutImageData(const Arguments &args) { sh = args[6]->NumberValue(); cols = sw; rows = sh; - //dx += sx + sw; + dx += sx; + dy += sy; } break; default: @@ -455,7 +459,12 @@ Context2d::PutImageData(const Arguments &args) { srcRows += srcStride; } - // TODO: cairo_surface_mark_dirty_rectangle + cairo_surface_mark_dirty_rectangle( + context->canvas()->surface() + , dx + , dy + , cols + , rows); return Undefined(); }