add histogram module (#354)

* add histogram module

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* add option to drop gradient

Signed-off-by: Varun Gupta <varun.gupta1798@gmail.com>

* fix bug

Signed-off-by: Varun Gupta <varun.gupta1798@gmail.com>
This commit is contained in:
Varun Gupta
2018-10-09 01:26:35 +05:30
committed by Jeffrey Warren
parent 1d085c7f20
commit 276a637ac3
6 changed files with 268 additions and 38 deletions

View File

@@ -0,0 +1,93 @@
/*
* Calculates the histogram of the image
*/
module.exports = function Channel(options, UI) {
var output;
function draw(input, callback, progressObj) {
options.gradient = options.gradient || "true";
options.gradient = JSON.parse(options.gradient);
progressObj.stop(true);
progressObj.overrideFlag = true;
var step = this, hist = new Array(256).fill(0);
function changePixel(r, g, b, a) {
let pixVal = Math.round((r + g + b) / 3);
hist[pixVal]++;
return [r, g, b, a];
}
function extraManipulation(pixels) {
// if (!options.inBrowser)
// require('fs').writeFileSync('./output/histo.txt', hist.reduce((tot, cur, idx) => `${tot}\n${idx} : ${cur}`, ``));
var newarray = new Uint8Array(4 * 256 * 256);
pixels.data = newarray;
pixels.shape = [256, 256, 4];
pixels.stride[1] = 4 * 256;
for (let x = 0; x < 256; x++) {
for (let y = 0; y < 256; y++) {
pixels.set(x, y, 0, 255);
pixels.set(x, y, 1, 255);
pixels.set(x, y, 2, 255);
pixels.set(x, y, 3, 255);
}
}
let startY = options.gradient ? 10 : 0;
if (options.gradient) {
for (let x = 0; x < 256; x++) {
for (let y = 0; y < 10; y++) {
pixels.set(x, 255 - y, 0, x);
pixels.set(x, 255 - y, 1, x);
pixels.set(x, 255 - y, 2, x);
}
}
}
let convfactor = (256 - startY) / Math.max(...hist);
for (let x = 0; x < 256; x++) {
let pixCount = Math.round(convfactor * hist[x]);
for (let y = startY; y < pixCount; y++) {
pixels.set(x, 255 - y, 0, 204);
pixels.set(x, 255 - y, 1, 255);
pixels.set(x, 255 - y, 2, 153);
}
}
return pixels;
}
function output(image, datauri, mimetype) {
// This output is accesible by Image Sequencer
step.output = { src: datauri, format: mimetype };
}
return require('../_nomodule/PixelManipulation.js')(input, {
output: output,
changePixel: changePixel,
extraManipulation: extraManipulation,
format: input.format,
image: options.image,
inBrowser: options.inBrowser,
callback: callback
});
}
return {
options: options,
//setup: setup, // optional
draw: draw,
output: output,
UI: UI
}
}