/*
* BoxBlurFilter
* Visit http://createjs.com/ for documentation, updates and examples.
*
* Copyright (c) 2010 gskinner.com, inc.
*
* 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.
*/
// namespace:
this.createjs = this.createjs||{};
(function() {
/**
* BoxBlurFilter applies a box blur to DisplayObjects
*
* See {{#crossLink "Filter"}}{{/crossLink}} for an example of how to apply filters.
* @class BoxBlurFilter
* @extends 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 createjs.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;
// Create WebGL canvas for fast rendering.
this._createWebGLRenderer();
}
// public properties:
/**
* Horizontal blur radius
* @property blurX
* @type Number
**/
p.blurX = 0;
/**
* Vertical blur radius
* @property blurY
* @type Number
**/
p.blurY = 0;
/**
* Number of blur iterations. For example, a value of 1 will produce a rough blur.
* A value of 2 will produce a smoother blur, but take twice as long to run.
* @property quality
* @type Number
**/
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 createjs.Rectangle(-this.blurX,-this.blurY,2*this.blurX,2*this.blurY);
}
/**
* Applies the filter to the specified context.
* @method applyFilter
* @param {CanvasRenderingContext2D} ctx The 2D context to use as the source.
* @param {Number} x The x position to use for the source rect.
* @param {Number} y The y position to use for the source rect.
* @param {Number} width The width to use for the source rect.
* @param {Number} height The height to use for the source rect.
* @param {CanvasRenderingContext2D} targetCtx Optional. The 2D context to draw the result to. Defaults to the context passed to ctx.
* @param {Number} targetX Optional. The x position to draw the result to. Defaults to the value passed to x.
* @param {Number} targetY Optional. The y position to draw the result to. Defaults to the value passed to y.
* @return {Boolean}
**/
p.applyFilter = function(ctx, x, y, width, height, targetCtx, targetX, targetY) {
if(this.quality <= 0) { return false; }
targetCtx = targetCtx || ctx;
if (targetX == null) { targetX = x; }
if (targetY == null) { targetY = y; }
var wctx = this._workContext;
if(this.useGL && wctx) {
var source = ctx.canvas;
// Because the draw happens an even number of times, the image will always be inverted. So invert the image once beforehand.
ctx.save();
ctx.scale(1, -1);
ctx.drawImage(source, 0, -height);
ctx.restore();
this._initViewport(wctx, source.width, source.height);
var radiusX = this.blurX;
if ( isNaN(radiusX) || radiusX < 0 ) radiusX = 0;
radiusX |= 0;
var radiusY = this.blurY;
if ( isNaN(radiusY) || radiusY < 0 ) radiusY = 0;
radiusY |= 0;
var iterations = this.quality;
if ( isNaN(iterations) || iterations < 1 ) iterations = 1;
iterations |= 0;
if ( iterations > 3 ) iterations = 3;
if ( iterations < 1 ) iterations = 1;
wctx.uniform1i(wctx.blurXUniform, radiusX);
wctx.uniform1i(wctx.blurYUniform, radiusY);
wctx.uniform2f(wctx.dimensionsUniform, wctx.canvas.width, wctx.canvas.height);
for(var i = 0; i < this.quality*2; i++) {
wctx.uniform1i(wctx.blurHorizontalUniform, (i%2==0));
this._initTexture(wctx, source);
wctx.drawArrays(wctx.TRIANGLE_STRIP, 0, 4);
source = wctx.canvas;
}
var glData = new Uint8Array(width*height*4);
wctx.readPixels(0,0,width,height, wctx.RGBA, wctx.UNSIGNED_BYTE, glData);
var imageData = targetCtx.createImageData(width, height);
imageData.data.set(glData);
targetCtx.putImageData(imageData, targetX, targetY);
return true;
}
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; // Get current pixel color * x blur - 1
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++]; // Add all pixel colors in the horizontal line to the sums.
gsum += pixels[p++];
bsum += pixels[p++];
asum += pixels[p]
}
for ( x = 0; x < width; x++ ) {
r[yi] = rsum; // Store added pixels in arrays.
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 object.
* @return {BoxBlurFilter}
**/
p.clone = function() {
return new BoxBlurFilter(this.blurX, this.blurY, this.quality);
}
/**
* Returns a string representation of this object.
* @return {String}
**/
p.toString = function() {
return "[BoxBlurFilter]";
}
// private methods:
p._initUniforms = function(ctx, program) {
// Blur Box uniform references.
ctx.blurXUniform = ctx.getUniformLocation(program, "uBlurX");
ctx.blurYUniform = ctx.getUniformLocation(program, "uBlurY");
ctx.blurHorizontalUniform = ctx.getUniformLocation(program, "uBlurHorizontal");
ctx.dimensionsUniform = ctx.getUniformLocation(program, "uDimensions");
}
/**
* @method _getFragmentShader
* Get fragment shader code in GLSL.
* @returns {String}
* @protected
*/
p._getFragmentShader = function() {
return "" +
"precision mediump float; \n" +
"const int MAX_QUALITY = 10000; \n" +
"varying vec2 vTextureCoord; \n" +
"uniform sampler2D uSampler; \n" +
"uniform int uBlurX; \n" +
"uniform int uBlurY; \n" +
"uniform vec2 uDimensions; \n" +
"uniform bool uBlurHorizontal; \n" +
// Main
"void main(void) { \n" +
" vec4 color = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t)); \n" +
" vec4 newColor = vec4(0,0,0,0); \n" +
" int radius; \n" +
" float blur; \n" +
// Blur the fragment.
" if(uBlurHorizontal) { \n" +
" blur = 1. / uDimensions.x; \n" +
" radius = (2 * uBlurX) + 1; \n" +
" for(int x = 0; x < MAX_QUALITY; x++) { \n" +
" if(radius - x <= 0) { break; } \n" +
" newColor += texture2D(uSampler, vec2(vTextureCoord.s + blur * float(x - uBlurX), vTextureCoord.t)); \n" +
" } \n" +
" } else { \n" +
" blur = 1. / uDimensions.y; \n" +
" radius = (2 * uBlurY) + 1; \n" +
" for(int y = 0; y < MAX_QUALITY; y++) { \n" +
" if(radius - y <= 0) { break; } \n" +
" newColor += texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t + blur * float(y - uBlurY))); \n" +
" } \n" +
" } \n" +
" gl_FragColor = newColor / float(radius); \n" +
"}";
};
createjs.BoxBlurFilter = BoxBlurFilter;
}());