add color histogram and flip actions

This commit is contained in:
root 2009-08-11 12:58:58 +02:00
parent fdf3968f84
commit f7234c0087
2 changed files with 116 additions and 0 deletions

82
actions/colorhistogram.js Executable file
View file

@ -0,0 +1,82 @@
/*
* Pixastic Lib - Histogram - v0.1.0
* Copyright (c) 2008 Jacob Seidelin, jseidelin@nihilogic.dk, http://blog.nihilogic.dk/
* License: [http://www.pixastic.com/lib/license.txt]
*/
Pixastic.Actions.colorhistogram = {
array256 : function(default_value) {
arr = [];
for (var i=0; i<256; i++) { arr[i] = default_value; }
return arr
},
process : function(params) {
var values = [];
if (typeof params.options.returnValue != "object") {
params.options.returnValue = {rvals:[], gvals:[], bvals:[]};
}
var paint = !!(params.options.paint);
var returnValue = params.options.returnValue;
if (typeof returnValue.values != "array") {
returnValue.rvals = [];
returnValue.gvals = [];
returnValue.bvals = [];
}
if (Pixastic.Client.hasCanvasImageData()) {
var data = Pixastic.prepareData(params);
params.useData = false;
var rvals = this.array256(0);
var gvals = this.array256(0);
var bvals = this.array256(0);
var rect = params.options.rect;
var p = rect.width*rect.height;
var pix = p*4;
while (p--) {
rvals[data[pix-=4]]++;
gvals[data[pix+1]]++;
bvals[data[pix+2]]++;
}
returnValue.rvals = rvals;
returnValue.gvals = gvals;
returnValue.bvals = bvals;
if (paint) {
var ctx = params.canvas.getContext("2d");
var vals = [rvals, gvals, bvals];
for (var v=0;v<3;v++) {
var yoff = (v+1) * params.height / 3;
var maxValue = 0;
for (var i=0;i<256;i++) {
if (vals[v][i] > maxValue)
maxValue = vals[v][i];
}
var heightScale = params.height / 3 / maxValue;
var widthScale = params.width / 256;
if (v==0) ctx.fillStyle = "rgba(255,0,0,0.5)";
else if (v==1) ctx.fillStyle = "rgba(0,255,0,0.5)";
else if (v==2) ctx.fillStyle = "rgba(0,0,255,0.5)";
for (var i=0;i<256;i++) {
ctx.fillRect(
i * widthScale, params.height - heightScale * vals[v][i] - params.height + yoff,
widthScale, vals[v][i] * heightScale
);
}
}
}
return true;
}
},
checkSupport : function() {
return Pixastic.Client.hasCanvasImageData();
}
}

34
actions/flip.js Executable file
View file

@ -0,0 +1,34 @@
/*
* Pixastic Lib - Flip - v0.1.0
* Copyright (c) 2008 Jacob Seidelin, jseidelin@nihilogic.dk, http://blog.nihilogic.dk/
* License: [http://www.pixastic.com/lib/license.txt]
*/
Pixastic.Actions.flip = {
process : function(params) {
var rect = params.options.rect;
var copyCanvas = document.createElement("canvas");
copyCanvas.width = rect.width;
copyCanvas.height = rect.height;
copyCanvas.getContext("2d").drawImage(params.image, rect.left, rect.top, rect.width, rect.height, 0, 0, rect.width, rect.height);
var ctx = params.canvas.getContext("2d");
ctx.clearRect(rect.left, rect.top, rect.width, rect.height);
if (params.options.axis == "horizontal") {
ctx.scale(-1,1);
ctx.drawImage(copyCanvas, -rect.left-rect.width, rect.top, rect.width, rect.height)
} else {
ctx.scale(1,-1);
ctx.drawImage(copyCanvas, rect.left, -rect.top-rect.height, rect.width, rect.height)
}
params.useData = false;
return true;
},
checkSupport : function() {
return Pixastic.Client.hasCanvas();
}
}