


(function(root) {
  'use strict';

  function Sobel(imageData) {
    if (!(this instanceof Sobel)) {
      return new Sobel(imageData);
    }

    var w = imageData.width;
    var h = imageData.height;

    var kernelX = [
      [-1,0,1],
      [-2,0,2],
      [-1,0,1]
    ];

    var kernelY = [
      [-1,-2,-1],
      [0,0,0],
      [1,2,1]
    ];

    var sobelData = [];
    var grayscaleData = [];

    function bindPixelAt(data) {
      return function(x, y, i) {
        i = i || 0;
        return data[((w * y) + x) * 4 + i];
      };
    }

    var data = imageData.data;
    var pixelAt = bindPixelAt(data);
    var x, y;

    for (y = 0; y < h; y++) {
      for (x = 0; x < w; x++) {
        var r = pixelAt(x, y, 0);
        var g = pixelAt(x, y, 1);
        var b = pixelAt(x, y, 2);

        var avg = (r + g + b) / 3;
        grayscaleData.push(avg, avg, avg, 255);
      }
    }

    pixelAt = bindPixelAt(grayscaleData);

    for (y = 0; y < h; y++) {
      for (x = 0; x < w; x++) {
        var pixelX = (
            (kernelX[0][0] * pixelAt(x - 1, y - 1)) +
            (kernelX[0][1] * pixelAt(x, y - 1)) +
            (kernelX[0][2] * pixelAt(x + 1, y - 1)) +
            (kernelX[1][0] * pixelAt(x - 1, y)) +
            (kernelX[1][1] * pixelAt(x, y)) +
            (kernelX[1][2] * pixelAt(x + 1, y)) +
            (kernelX[2][0] * pixelAt(x - 1, y + 1)) +
            (kernelX[2][1] * pixelAt(x, y + 1)) +
            (kernelX[2][2] * pixelAt(x + 1, y + 1))
        );

        var pixelY = (
          (kernelY[0][0] * pixelAt(x - 1, y - 1)) +
          (kernelY[0][1] * pixelAt(x, y - 1)) +
          (kernelY[0][2] * pixelAt(x + 1, y - 1)) +
          (kernelY[1][0] * pixelAt(x - 1, y)) +
          (kernelY[1][1] * pixelAt(x, y)) +
          (kernelY[1][2] * pixelAt(x + 1, y)) +
          (kernelY[2][0] * pixelAt(x - 1, y + 1)) +
          (kernelY[2][1] * pixelAt(x, y + 1)) +
          (kernelY[2][2] * pixelAt(x + 1, y + 1))
        );

        var magnitude = Math.sqrt((pixelX * pixelX) + (pixelY * pixelY))>>0;

        sobelData.push(magnitude, magnitude, magnitude, 255);
      }
    }

    var clampedArray = new Uint8ClampedArray(sobelData);
    clampedArray.toImageData = function() {
      try {
        return new ImageData(clampedArray, w, h);
      } catch(error) {
        var canvas = document.createElement('canvas');
        var context =  canvas.getContext('2d');
        var imageData = context.createImageData(w, h);
        imageData.data.set(clampedArray);
        return imageData;
      }
    };

    return clampedArray;
  }

  if (typeof exports !== 'undefined') {
    if (typeof module !== 'undefined' && module.exports) {
      exports = module.exports = Sobel;
    }
    exports.Sobel = Sobel;
  } else if (typeof define === 'function' && define.amd) {
    define([], function() {
      return Sobel;
    });
  } else {
    root.Sobel = Sobel;
  }

})(this);

@@ -2148,16 +2148,101 @@
 dArray =
+ sobelData;%0A%0A    if (typeof Uint8ClampedArray !== 'undefined') %7B%0A      clampedArray =
  new Uin
@@ -2264,24 +2264,31 @@
 sobelData);%0A
+    %7D%0A%0A
     clampedA
@@ -2329,14 +2329,208 @@
     
-try %7B%0A
+if (typeof window === 'undefined') %7B%0A        return %7B%0A          width: w,%0A          height: h,%0A          data: clampedArray%0A        %7D;%0A      %7D else%C2%A0%7B%0A        if (typeof ImageData === 'undefined') %7B%0A  
     
@@ -2585,22 +2585,16 @@
     
-%7D catch(error)
+  %7D else
  %7B%0A 
@@ -2596,24 +2596,26 @@
 e %7B%0A        
+ 
+ 
 var canvas =
@@ -2653,24 +2653,26 @@
 ');%0A        
+  
 var context 
@@ -2695,24 +2695,26 @@
 text('2d');%0A
+  
         var 
@@ -2750,26 +2750,27 @@
 geData(w, h)
-;
 %0A
+  
         imag
@@ -2799,16 +2799,18 @@
 Array);%0A
+  
         
@@ -2823,24 +2823,34 @@
  imageData;%0A
+        %7D%0A
       %7D%0A    

add fix for environments without DOM access (webworkers, node)