JS filter Image (with a canvas) : effect invert, etc...

// Color inverter from https://gist.github.com/373253

by molokoloco

HTML

<!-- //
This do not work cause of cross domain policy :
<img id="jsFiddleLogo" src="http://jsfiddle.net/img/logo.png" />

So the same but binary encoded ^^
//-->

<img...

CSS

body {
    margin: 50px;
}

JavaScript

// Color inverter from https://gist.github.com/373253
var filterImage = function(e, filter, callback) { 
    if (!(e.is('img') || /^(data|url)/.test(e.css('backgroundImage')))) return;
    var invertColors = function(pix) { // Default filter
        for (var i = 0, n = pix.length; i < n; i += 4) { // Loop over each pixel and invert the color.
            //pix[i] = pix[i] * 2; // red
            //pix[i + 1] = 255 - pix[i + 1]; // green
            pix[i + 2] = pix[i + 2] * 2; // blue
            //pix[i + 3] = 255 - pix[i + 3]; // alpha
        }
    };
    var img = new Image();
    $(img).bind('load', function() {
        var elem = document.createElement('canvas');
        elem.width = img.width;
        elem.height = img.height;
        var context = elem.getContext('2d'),
            x = 0,
            y = 0,
            result;
        try {
            context.drawImage(img, x, y); // Draw the image on canvas.
            var imgd = context.getImageData(x, y, img.width, img.height); // Get the pixels.
            if (filter) filter.call(this, imgd.data); // Custom ?
            else invertColors(imgd.data);
            context.putImageData(imgd, x, y); // Draw the ImageData object.
            result = elem.toDataURL();
        }
        catch (e) {
            console.log('invertImage() Error');
        } // image is on a different domain ?
        if (result && e.is('img')) e.attr('src', result);
        else if (result) e.css('backgroundImage', 'url(' + result + ')');
        if (callback) try {
            callback();
        } catch (e) {
            callback;
        }

        if (callback && typeof(callback) == 'function') callback.call(this);
    }).each(function() {
        if (this.complete || this.complete === undefined) {
            var src = this.src; // cached images don't fire load sometimes, so we reset src.
            this.src = 'javascript:void(0)';
            this.src = src;
        }
    });
    var matched =...