g_in_sum += (stackIn.g = pixels[p + 1]);
b_in_sum += (stackIn.b = pixels[p + 2]);

r_sum += r_in_sum;
g_sum += g_in_sum;
b_sum += b_in_sum;

stackIn = stackIn.next;

r_out_sum += (pr = stackOut.r);
g_out_sum += (pg = stackOut.g);
b_out_sum += (pb = stackOut.b);

r_in_sum -= pr;
g_in_sum -= pg;
b_in_sum -= pb;

stackOut = stackOut.next;

yi += 4;
}
yw += width;
}


for (x = 0; x < width; x++) {
g_in_sum = b_in_sum = r_in_sum = g_sum = b_sum = r_sum = 0;

yi = x << 2;
r_out_sum = radiusPlus1 * (pr = pixels[yi]);
g_out_sum = radiusPlus1 * (pg = pixels[yi + 1]);
b_out_sum = radiusPlus1 * (pb = pixels[yi + 2]);

r_sum += sumFactor * pr;
g_sum += sumFactor * pg;
b_sum += sumFactor * pb;

stack = stackStart;

for (i = 0; i < radiusPlus1; i++) {
stack.r = pr;
stack.g = pg;
stack.b = pb;
stack = stack.next;
}

yp = width;

for (i = 1; i <= radius; i++) {
yi = (yp + x) << 2;

r_sum += (stack.r = (pr = pixels[yi])) * (rbs = radiusPlus1 - i);
g_sum += (stack.g = (pg = pixels[yi + 1])) * rbs;
b_sum += (stack.b = (pb = pixels[yi + 2])) * rbs;

r_in_sum += pr;
g_in_sum += pg;
b_in_sum += pb;

stack = stack.next;

if (i < heightMinus1) {
yp += width;
}
}

yi = x;
stackIn = stackStart;
stackOut = stackEnd;
for (y = 0; y < height; y++) {
p = yi << 2;
pixels[p] = (r_sum * mul_sum) >> shg_sum;
pixels[p + 1] = (g_sum * mul_sum) >> shg_sum;
pixels[p + 2] = (b_sum * mul_sum) >> shg_sum;

r_sum -= r_out_sum;
g_sum -= g_out_sum;
b_sum -= b_out_sum;

r_out_sum -= stackIn.r;
g_out_sum -= stackIn.g;
b_out_sum -= stackIn.b;

p = (x + (((p = y + radiusPlus1) < heightMinus1 ? p : heightMinus1) * width)) << 2;

r_sum += (r_in_sum += (stackIn.r = pixels[p]));
g_sum += (g_in_sum += (stackIn.g = pixels[p + 1]));
b_sum += (b_in_sum += (stackIn.b = pixels[p + 2]));

stackIn = stackIn.next;

r_out_sum += (pr = stackOut.r);
g_out_sum += (pg = stackOut.g);
b_out_sum += (pb = stackOut.b);

r_in_sum -= pr;
g_in_sum -= pg;
b_in_sum -= pb;

stackOut = stackOut.next;

yi += width;
}
}

context.putImageData(imageData, top_x, top_y);

};

/**
* Defines a new helper object for Stack Blur Algorithm.
*/

function BlurStack() {
this.r = 0;
this.g = 0;
this.b = 0;
this.a = 0;
this.next = null;
}

/**
* Defines a gravity matrix object which handles collision detection.
* @param x number of columns in the matrix
* @param y number of rows in the matrix
* @param r grid size
*/

function CollisionMatrix(x, y, r) {
this.resolution = r;
this.xc = x;
this.yc = y;
this.matrix = new Array(x);
for (var i = 0; i <= (x + 5); i++) {
this.matrix[i] = Array(y);
for (var j = 0; j <= (y + 5); ++j) {
this.matrix[i][j] = new DropItem(null);
}
}
}

/**
* Updates position of the given drop on the collision matrix.
* @param drop raindrop to be positioned/repositioned
* @forceDelete if true the raindrop will be removed from the matrix
* @returns collisions if any
*/
CollisionMatrix.prototype.update = function(drop, forceDelete) {
if (drop.gid) {
this.matrix[drop.gmx][drop.gmy].remove(drop);
if (forceDelete) {
return null;
}

drop.gmx = Math.floor(drop.x / this.resolution);
drop.gmy = Math.floor(drop.y / this.resolution);
this.matrix[drop.gmx][drop.gmy].add(drop);

var collisions = this.collisions(drop);
if (collisions && collisions.next != null) {
return collisions.next;
}
} else {
drop.gid = Math.random().toString(36).substr(2, 9);
drop.gmx = Math.floor(drop.x / this.resolution);
drop.gmy = Math.floor(drop.y / this.resolution);
this.matrix[drop.gmx][drop.gmy].add(drop);
}
return null;
};

/**
* Looks for collisions with the given raindrop.
* @param drop raindrop to be checked
* @returns list of drops that collide with it
*/
CollisionMatrix.prototype.collisions = function(drop) {
var item = new DropItem(null);
var first = item;

item = this.addAll(item, drop.gmx - 1, drop.gmy);
item = this.addAll(item, drop.gmx - 1, drop.gmy + 1);
item = this.addAll(item, drop.gmx, drop.gmy + 1);
item = this.addAll(item, drop.gmx + 1, drop.gmy + 1);
item = this.addAll(item, drop.gmx + 1, drop.gmy);

return first;
};

/**
* Appends all found drop at a given location to the given item.
* @param to item to which the results will be appended to
* @param x x position in the matrix
* @param y y position in the matrix
* @returns last discovered item on the list
*/
CollisionMatrix.prototype.addAll = function(to, x, y) {
if (x > 0 && y > 0 && x < this.xc && y < this.yc) {
var items = this.matrix[x][y];
while (items.next != null) {
items = items.next;
to.next = new DropItem(items.drop);
to = to.next;
}
}
return to;
};

/**
* Defines a linked list item.
*/

function DropItem(drop) {
this.drop = drop;
this.next = null;
}

/**
* Adds the raindrop to the end of the list.
* @param drop raindrop to be added
*/
DropItem.prototype.add = function(drop) {
var item = this;
while (item.next != null) {
item = item.next;
}
item.next = new DropItem(drop);
};

/**
* Removes the raindrop from the list.
* @param drop raindrop to be removed
*/
DropItem.prototype.remove = function(drop) {
var item = this;
var prevItem = null;
while (item.next != null) {
prevItem = item;
item = item.next;
if (item.drop.gid == drop.gid) {
prevItem.next = item.next;
}
}

Prev | Next
Pg.: 1 2 3 4 5


Back to home | File page

Subscribe | Register | Login | N