Help optimising a script to make a visualisation of total ink coverage
I have written a script where it creates a new layer from merged pixels and samples each pixel calclulates ink coverage and marks the pixel with one color if it is above the cutoff value and another if it is below. The only issue is it is really slow. I need help optimising this to improve performance. I am using photoshop CS6 JS. Below is the script
//samples each point in image and shows where the TAC is higher than normal (> 240)
//makes a new layer off all visible and performs the test
//CONSTANSTS
var C_ACCEPTABLE_TAC = 236
var C_ABOVE_TAC_COLOR = new SolidColor();
C_ABOVE_TAC_COLOR.rgb.hexValue = "f449ff"// bright magenta
var C_WITHIN_TAC_COLOR = new SolidColor();
C_WITHIN_TAC_COLOR.rgb.hexValue = "ffffff"// white
//clear ES Toolkit console
if (app.name === "ExtendScript Toolkit") {
app.clc();
}
/*
// uncomment if you want ES to start even when photoshop launches the script
else {
var estApp= BridgeTalk.getSpecifier("estoolkit");
if(estApp) {
var bt = new BridgeTalk;
bt.target = estApp;
bt.body = "app.clc()";
bt.send();
}
}
*/
var docRef = app.activeDocument;
function makeSelection(x,y,sw,sh){
app.activeDocument.selection.select([ [x,y], [x,y+sh], [x+sw,y+sh], [x+sw,y] ]);
}
//make a new layer empty layer
var newlayerRef = docRef.artLayers.add();
newlayerRef.kind = LayerKind.NORMAL;
newlayerRef.name = "IG TAC";
//set active layer to new layer
docRef.activeLayer = newlayerRef;
//copy merged (step afterwards in case only 1 layer)
docRef.selection.selectAll();
docRef.selection.copy(true) // copy merged;
//paste the selection into active layer
docRef.selection.selectAll();
docRef.paste();
//deselect selection
docRef.selection.deselect();
//process selection pixel by pixel to check TAC
app.preferences.rulerUnits = Units.PIXELS;
var docHeightPix = docRef.height
var docWidthPix = docRef.width
//$.writeln (docHeightPix)
//$.writeln (docWidthPix)
//$.writeln ("==")
for (var row=0; row <docHeightPix; row = row + 1) {
if (row < (docHeightPix -1)) {
adjustedRow = row + 0.1
}else {
adjustedRow = row
}
for (var col=0; col <docWidthPix; col = col + 1) {
//var adjustedCol = (row == app.activeDocument.width -1) ? col : col + 0.1;
if (col < (docWidthPix -1)) {
adjustedCol = col + 0.1
} else {
adjustedCol = col;
}
//$.writeln("[" + row + ", " + col + "]");
//$.writeln("[" + adjustedRow + ", " + adjustedCol + "]");
docRef.colorSamplers.removeAll();
docRef.colorSamplers.add([adjustedCol, adjustedRow]);//workaround for reliable pixel color picking.
var cs = docRef.colorSamplers[0];
var totalInk = cs.color.cmyk.cyan + cs.color.cmyk.magenta + cs.color.cmyk.yellow + cs.color.cmyk.black;
totalInk = Math.round(totalInk);
//$.writeln(totalInk);
// color the pixels
makeSelection(col,row,1,1)
if (totalInk > C_ACCEPTABLE_TAC){
docRef.selection.fill(C_ABOVE_TAC_COLOR)
} else {
docRef.selection.fill(C_WITHIN_TAC_COLOR)
}
}
//$.writeln("#^");
}
app.activeDocument.colorSamplers.removeAll();.Any help would be appreaciated.
