From c874fb7817332b0c2374ba8bc54d835a8e1b79ba Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 12 May 2013 22:31:00 -0700 Subject: [PATCH] #320: Ensure source rectangle of transfer and copy is clipped to source canvas bounds. --- .../src/main/resources/layer.js | 99 ++++++++++++------- 1 file changed, 61 insertions(+), 38 deletions(-) diff --git a/guacamole-common-js/src/main/resources/layer.js b/guacamole-common-js/src/main/resources/layer.js index 0553aa333..3b4343890 100644 --- a/guacamole-common-js/src/main/resources/layer.js +++ b/guacamole-common-js/src/main/resources/layer.js @@ -555,50 +555,60 @@ Guacamole.Layer = function(width, height) { this.transfer = function(srcLayer, srcx, srcy, srcw, srch, x, y, transferFunction) { scheduleTaskSynced(srcLayer, function() { + var srcCanvas = srcLayer.getCanvas(); + + // If entire rectangle outside source canvas, stop + if (srcx >= srcCanvas.width || srcy >= srcCanvas.height) return; + + // Otherwise, clip rectangle to area + if (srcx + srcw > srcCanvas.width) + srcw = srcCanvas.width - srcx; + + if (srcy + srch > srcCanvas.height) + srch = srcCanvas.height - srcy; + + // Stop if nothing to draw. + if (srcw == 0 || srch == 0) return; + if (layer.autosize != 0) fitRect(x, y, srcw, srch); - var srcCanvas = srcLayer.getCanvas(); - if (srcCanvas.width != 0 && srcCanvas.height != 0) { + // Get image data from src and dst + var src = srcLayer.getCanvas().getContext("2d").getImageData(srcx, srcy, srcw, srch); + var dst = displayContext.getImageData(x , y, srcw, srch); - // Get image data from src and dst - var src = srcLayer.getCanvas().getContext("2d").getImageData(srcx, srcy, srcw, srch); - var dst = displayContext.getImageData(x , y, srcw, srch); + // Apply transfer for each pixel + for (var i=0; i= srcCanvas.width || srcy >= srcCanvas.height) return; + + // Otherwise, clip rectangle to area + if (srcx + srcw > srcCanvas.width) + srcw = srcCanvas.width - srcx; + + if (srcy + srch > srcCanvas.height) + srch = srcCanvas.height - srcy; + + // Stop if nothing to draw. + if (srcw == 0 || srch == 0) return; + + if (layer.autosize != 0) fitRect(x, y, srcw, srch); + displayContext.drawImage(srcCanvas, srcx, srcy, srcw, srch, x, y, srcw, srch); }); };