Skip to content

Commit

Permalink
filters
Browse files Browse the repository at this point in the history
  • Loading branch information
lachie committed Jan 19, 2012
1 parent 3830b08 commit 9b315d9
Show file tree
Hide file tree
Showing 4 changed files with 629 additions and 0 deletions.
244 changes: 244 additions & 0 deletions public/javascripts/vendor/easel/filters/BoxBlurFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
/**
* BoxBlurFilter by Mario Klingemann. March 2, 2011
* Visit http://easeljs.com/ for documentation, updates and examples.
*
*
* Copyright (c) 2011 Mario Klingemann
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
**/

(function(window) {

/**
* BoxBlurFilter applies a box blur to DisplayObjects
* @class BoxBlurFilter
* @augments Filter
* @constructor
* @param {Number} blurX
* @param {Number} blurY
* @param {Number} quality
**/
var BoxBlurFilter = function( blurX, blurY, quality ) {
this.initialize( blurX, blurY, quality );
}
var p = BoxBlurFilter.prototype = new Filter();

// constructor:
/** @ignore */
p.initialize = function( blurX, blurY, quality ) {
if ( isNaN(blurX) || blurX < 0 ) blurX = 0;
this.blurX = blurX | 0;
if ( isNaN(blurY) || blurY < 0 ) blurY = 0;
this.blurY = blurY | 0;
if ( isNaN(quality) || quality < 1 ) quality = 1;
this.quality = quality | 0;
}

// public properties:

/** Horizontal blur radius */
p.blurX = 0;

/** Horizontal blur radius */
p.blurY = 0;

/** Amount of blur iterations */
p.quality = 1;

// public methods:
/**
* Returns a rectangle with values indicating the margins required to draw the filter.
* For example, a filter that will extend the drawing area 4 pixels to the left, and 7 pixels to the right
* (but no pixels up or down) would return a rectangle with (x=-4, y=0, width=11, height=0).
* @method getBounds
* @return {Rectangle} a rectangle object indicating the margins required to draw the filter.
**/
p.getBounds = function() {
// TODO: this doesn't properly account for blur quality.
return new Rectangle(-this.blurX,-this.blurY,2*this.blurX,2*this.blurY);
}

/**
* Applies the filter to the specified context.
* @method applyFilter
* @param ctx The 2D context to use as the source.
* @param x The x position to use for the source rect.
* @param y The y position to use for the source rect.
* @param width The width to use for the source rect.
* @param height The height to use for the source rect.
* @param targetCtx Optional. The 2D context to draw the result to. Defaults to the context passed to ctx.
* @param targetX Optional. The x position to draw the result to. Defaults to the value passed to x.
* @param targetY Optional. The y position to draw the result to. Defaults to the value passed to y.
**/
p.applyFilter = function(ctx, x, y, width, height, targetCtx, targetX, targetY) {
targetCtx = targetCtx || ctx;
if (targetX == null) { targetX = x; }
if (targetY == null) { targetY = y; }
try {
var imageData = ctx.getImageData(x, y, width, height);
} catch(e) {
//if (!this.suppressCrossDomainErrors) throw new Error("unable to access local image data: " + e);
return false;
}

var radiusX = this.blurX;
if ( isNaN(radiusX) || radiusX < 0 ) return false;
radiusX |= 0;

var radiusY = this.blurY;
if ( isNaN(radiusY) || radiusY < 0 ) return false;
radiusY |= 0;

if ( radiusX == 0 && radiusY == 0 ) return false;

var iterations = this.quality;
if ( isNaN(iterations) || iterations < 1 ) iterations = 1;
iterations |= 0;
if ( iterations > 3 ) iterations = 3;
if ( iterations < 1 ) iterations = 1;

var pixels = imageData.data;

var rsum,gsum,bsum,asum,x,y,i,p,p1,p2,yp,yi,yw;
var wm = width - 1;
var hm = height - 1;
var rad1x = radiusX + 1;
var divx = radiusX + rad1x;
var rad1y = radiusY + 1;
var divy = radiusY + rad1y;
var div2 = 1 / (divx * divy);

var r = [];
var g = [];
var b = [];
var a = [];

var vmin = [];
var vmax = [];

while ( iterations-- > 0 ) {
yw = yi = 0;

for ( y=0; y < height; y++ ){
rsum = pixels[yw] * rad1x;
gsum = pixels[yw+1] * rad1x;
bsum = pixels[yw+2] * rad1x;
asum = pixels[yw+3] * rad1x;


for( i = 1; i <= radiusX; i++ ) {
p = yw + (((i > wm ? wm : i )) << 2 );
rsum += pixels[p++];
gsum += pixels[p++];
bsum += pixels[p++];
asum += pixels[p]
}

for ( x = 0; x < width; x++ ) {
r[yi] = rsum;
g[yi] = gsum;
b[yi] = bsum;
a[yi] = asum;

if(y==0){
vmin[x] = Math.min( x + rad1x, wm ) << 2;
vmax[x] = Math.max( x - radiusX, 0 ) << 2;
}

p1 = yw + vmin[x];
p2 = yw + vmax[x];

rsum += pixels[p1++] - pixels[p2++];
gsum += pixels[p1++] - pixels[p2++];
bsum += pixels[p1++] - pixels[p2++];
asum += pixels[p1] - pixels[p2];

yi++;
}
yw += ( width << 2 );
}

for ( x = 0; x < width; x++ ) {
yp = x;
rsum = r[yp] * rad1y;
gsum = g[yp] * rad1y;
bsum = b[yp] * rad1y;
asum = a[yp] * rad1y;

for( i = 1; i <= radiusY; i++ ) {
yp += ( i > hm ? 0 : width );
rsum += r[yp];
gsum += g[yp];
bsum += b[yp];
asum += a[yp];
}

yi = x << 2;
for ( y = 0; y < height; y++) {
pixels[yi] = (rsum * div2 + 0.5) | 0;
pixels[yi+1] = (gsum * div2 + 0.5) | 0;
pixels[yi+2] = (bsum * div2 + 0.5) | 0;
pixels[yi+3] = (asum * div2 + 0.5) | 0;

if( x == 0 ){
vmin[y] = Math.min( y + rad1y, hm ) * width;
vmax[y] = Math.max( y - radiusY,0 ) * width;
}

p1 = x + vmin[y];
p2 = x + vmax[y];

rsum += r[p1] - r[p2];
gsum += g[p1] - g[p2];
bsum += b[p1] - b[p2];
asum += a[p1] - a[p2];

yi += width << 2;
}
}
}

targetCtx.putImageData(imageData, targetX, targetY);
return true;
}

/**
* Returns a clone of this DisplayObject. Some properties that are specific to this instance's current context are reverted to their defaults (for example .parent).
**/
p.clone = function() {
return new BoxBlurFilter(this.blurX, this.blurY, this.quality);
}

/**
* Returns a string representation of this object.
**/
p.toString = function() {
return "[BoxBlurFilter (name="+ this.name +")]";
}

// private methods:



window.BoxBlurFilter = BoxBlurFilter;
}(window));
149 changes: 149 additions & 0 deletions public/javascripts/vendor/easel/filters/ColorFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* ColorFilter by Grant Skinner. Mar 7, 2011
* Visit http://easeljs.com/ for documentation, updates and examples.
*
*
* Copyright (c) 2010 Grant Skinner
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

/**
* The Easel Javascript library provides a retained graphics mode for canvas
* including a full, hierarchical display list, a core interaction model, and
* helper classes to make working with Canvas much easier.
* @module EaselJS
**/

(function(window) {

/**
* Applies color transforms.
* @class ColorFilter
* @constructor
* @augments Filter
* @param {Number} redMultiplier
* @param {Number} greenMultiplier
* @param {Number} blueMultiplier
* @param {Number} alphaMultiplier
* @param {Number} redOffset
* @param {Number} greenOffset
* @param {Number} blueOffset
* @param {Number} alphaOffset
**/
var ColorFilter = function(redMultiplier, greenMultiplier, blueMultiplier, alphaMultiplier, redOffset, greenOffset, blueOffset, alphaOffset) {
this.initialize(redMultiplier, greenMultiplier, blueMultiplier, alphaMultiplier, redOffset, greenOffset, blueOffset, alphaOffset);
}
var p = ColorFilter.prototype = new Filter();

// public properties:
/** Red channel multiplier. */
p.redMultiplier = 1;
/** Green channel multiplier. */
p.greenMultiplier = 1;
/** Blue channel multiplier. */
p.blueMultiplier = 1;
/** Alpha channel multiplier. */
p.alphaMultiplier = 1;
/** Red channel offset (added to value). */
p.redOffset = 0;
/** Green channel offset (added to value). */
p.greenOffset = 0;
/** Blue channel offset (added to value). */
p.blueOffset = 0;
/** Alpha channel offset (added to value). */
p.alphaOffset = 0;

// constructor:
/**
* Initialization method.
* @method initialize
* @protected
**/
p.initialize = function(redMultiplier, greenMultiplier, blueMultiplier, alphaMultiplier, redOffset, greenOffset, blueOffset, alphaOffset) {
this.redMultiplier = redMultiplier != null ? redMultiplier : 1;
this.greenMultiplier = greenMultiplier != null ? greenMultiplier : 1;
this.blueMultiplier = blueMultiplier != null ? blueMultiplier : 1;
this.alphaMultiplier = alphaMultiplier != null ? alphaMultiplier : 1;
this.redOffset = redOffset || 0;
this.greenOffset = greenOffset || 0;
this.blueOffset = blueOffset || 0;
this.alphaOffset = alphaOffset || 0;
}

// public methods:
/**
* Applies the filter to the specified context.
* @method applyFilter
* @param ctx The 2D context to use as the source.
* @param x The x position to use for the source rect.
* @param y The y position to use for the source rect.
* @param width The width to use for the source rect.
* @param height The height to use for the source rect.
* @param targetCtx Optional. The 2D context to draw the result to. Defaults to the context passed to ctx.
* @param targetX Optional. The x position to draw the result to. Defaults to the value passed to x.
* @param targetY Optional. The y position to draw the result to. Defaults to the value passed to y.
**/
p.applyFilter = function(ctx, x, y, width, height, targetCtx, targetX, targetY) {
targetCtx = targetCtx || ctx;
if (targetX == null) { targetX = x; }
if (targetY == null) { targetY = y; }
try {
var imageData = ctx.getImageData(x, y, width, height);
} catch(e) {
//if (!this.suppressCrossDomainErrors) throw new Error("unable to access local image data: " + e);
return false;
}
var data = imageData.data;
var l = data.length;
for (var i=0; i<l; i+=4) {
data[i] = data[i]*this.redMultiplier+this.redOffset;
data[i+1] = data[i+1]*this.greenMultiplier+this.greenOffset;
data[i+2] = data[i+2]*this.blueMultiplier+this.blueOffset;
data[i+3] = data[i+3]*this.alphaMultiplier+this.alphaOffset;
}
imageData.data = data;
targetCtx.putImageData(imageData, targetX, targetY);
return true;
}

/**
* Returns a string representation of this object.
* @method toString
* @return {String} a string representation of the instance.
**/
p.toString = function() {
return "[ColorFilter]";
}


/**
* Returns a clone of this ColorFilter instance.
* @method clone
@return {ColorFilter} A clone of the current ColorFilter instance.
**/
p.clone = function() {
return new ColorFilter(this.redMultiplier, this.greenMultiplier, this.blueMultiplier, this.alphaMultiplier, this.redOffset, this.greenOffset, this.blueOffset, this.alphaOffset);
}

window.ColorFilter = ColorFilter;
}(window));
Loading

0 comments on commit 9b315d9

Please sign in to comment.