Merge branch 'master' of github.com:jseidelin/pixastic

This commit is contained in:
Jacob Seidelin 2014-09-04 11:17:48 +02:00
commit fa4b9283f0
3 changed files with 102 additions and 5 deletions

View file

@ -137,6 +137,17 @@
options : {
amount : 0.5
}
},
{
effect : 'mosaic',
options : {
blockSize : 8
}
},
{
effect : 'equalize',
options : {
}
}
];

View file

@ -1184,8 +1184,93 @@ Pixastic.Effects = (function() {
}
}
}
}
},
mosaic : function(inData, outData, width, height, options, progress) {
var blockSize = clamp(options.blockSize, 1, Math.max(width, height)),
yBlocks = Math.ceil(height / blockSize),
xBlocks = Math.ceil(width / blockSize),
y0, y1, x0, x1, idx, pidx,
n = yBlocks * xBlocks,
prog, lastProg = 0;
for (i=0, y0=0, bidx=0;i<yBlocks;i++) {
y1 = clamp(y0 + blockSize, 0, height);
for(j=0, x0=0;j<xBlocks;j++,bidx++) {
x1 = clamp(x0 + blockSize, 0, width);
idx = (y0 * width + x0) << 2;
var r = inData[idx], g = inData[idx+1], b = inData[idx+2];
for(bi=y0;bi<y1;bi++) {
for(bj=x0;bj<x1;bj++) {
pidx = (bi*width+bj) << 2;
outData[pidx] = r, outData[pidx+1] = g, outData[pidx+2] = b;
outData[pidx+3] = inData[pidx+3];
}
}
x0 = x1;
if (progress) {
prog = (bidx/n*100 >> 0) / 100;
if (prog > lastProg) {
lastProg = progress(prog);
}
}
}
y0 = y1;
}
},
equalize : function(inData, outData, width, height, options, progress) {
var n = width * height, p, i, level, ratio,
prog, lastProg;
var round = Math.round;
// build histogram
var pdf = new Array(256);
for (i=0;i<256;i++) {
pdf[i] = 0;
}
for (i=0;i<n;i++) {
p = i * 4;
level = clamp(round(inData[p] * 0.3 + inData[p+1] * 0.59 + inData[p+2] * 0.11), 0, 255);
outData[p+3] = level;
pdf[ level ]++;
}
// build cdf
var cdf = new Array(256);
cdf[0] = pdf[0];
for(i=1;i<256;i++) {
cdf[i] = cdf[i-1] + pdf[i];
}
// normalize cdf
for(i=0;i<256;i++) {
cdf[i] = cdf[i] / n * 255.0;
}
// map the pixel values
for (i=0;i<n;i++) {
p = i * 4;
level = outData[p+3];
ratio = cdf[level] / (level || 1);
outData[p] = clamp(round(inData[p] * ratio), 0, 255);
outData[p+1] = clamp(round(inData[p+1] * ratio), 0, 255);
outData[p+2] = clamp(round(inData[p+2] * ratio), 0, 255);
outData[p+3] = inData[p+3];
if (progress) {
prog = (i/n*100 >> 0) / 100;
if (prog > lastProg) {
lastProg = progress(prog);
}
}
}
}
};
})();

View file

@ -21,17 +21,18 @@
}
}
function Pixastic(ctx) {
function Pixastic(ctx, workerControlPath) {
var P = {},
width = ctx.canvas.width,
height = ctx.canvas.height,
queue = [];
queue = [],
workerControlPath = workerControlPath || "";
if (!worker) {
if (typeof window.Worker != "undefined") {
try {
worker = new window.Worker("pixastic.worker.control.js");
worker = new window.Worker(workerControlPath + "pixastic.worker.control.js");
} catch(e) {
if (location.protocol == "file:") {
Pixastic.log("Could not create native worker, running from file://")