Removed Lurking Global Variables

This commit is contained in:
Chinmay Pandhare
2017-07-17 14:53:27 +05:30
parent 3b335f1ff2
commit 94e9860470
14 changed files with 75 additions and 76 deletions

View File

@@ -3,7 +3,7 @@ function AddStep(ref, image, name, o) {
function addStep(image, name, o_) { function addStep(image, name, o_) {
ref.log('\x1b[36m%s\x1b[0m','adding step \"' + name + '\" to \"' + image + '\".'); ref.log('\x1b[36m%s\x1b[0m','adding step \"' + name + '\" to \"' + image + '\".');
o = ref.copy(o_); var o = ref.copy(o_);
o.id = ref.options.sequencerCounter++; //Gives a Unique ID to each step o.id = ref.options.sequencerCounter++; //Gives a Unique ID to each step
o.name = o_.name || name; o.name = o_.name || name;
o.selector = o_.selector || 'ismod-' + name; o.selector = o_.selector || 'ismod-' + name;

View File

@@ -15,7 +15,7 @@ function copy(a) {
if (objTypeOf(a) == "Array") return a.slice(); if (objTypeOf(a) == "Array") return a.slice();
if (objTypeOf(a) == "Object") { if (objTypeOf(a) == "Object") {
var b = {}; var b = {};
for (v in a) { for (var v in a) {
b[v] = copy(a[v]); b[v] = copy(a[v]);
} }
return b; return b;
@@ -25,11 +25,11 @@ function copy(a) {
function formatInput(args,format,images) { function formatInput(args,format,images) {
images = []; images = [];
for (image in this.images) { for (var image in this.images) {
images.push(image); images.push(image);
} }
json_q = {}; var json_q = {};
format_i = format; var format_i = format;
if (format == "+") if (format == "+")
format = ['o_string_a', 'string_a', 'o_object']; format = ['o_string_a', 'string_a', 'o_object'];
else if (format == "-") else if (format == "-")
@@ -76,8 +76,8 @@ function formatInput(args,format,images) {
if(format[0] == "o_string_a") { if(format[0] == "o_string_a") {
if(args.length == format.length - 1) { if(args.length == format.length - 1) {
insert = false; var insert = false;
for (i in args) { for (var i in args) {
if (format[parseInt(i)+1].includes( typeof(getPrimitive(args[i])) )){ if (format[parseInt(i)+1].includes( typeof(getPrimitive(args[i])) )){
insert = true; insert = true;
} }
@@ -89,15 +89,15 @@ function formatInput(args,format,images) {
} }
else if (format[0] == "o_string" && format_i == "l" && args.length == 2) { else if (format[0] == "o_string" && format_i == "l" && args.length == 2) {
if (typeof(args[0]) == "string") { if (typeof(args[0]) == "string") {
identifier = "image"; var identifier = "image";
number = 1; var number = 1;
while (this.images.hasOwnProperty(identifier+number)) number++; while (this.images.hasOwnProperty(identifier+number)) number++;
args.splice(0,0,identifier+number); args.splice(0,0,identifier+number);
} }
} }
if(args.length == format.length) { if(args.length == format.length) {
for (i in format) { for (var i in format) {
if (format[i].substr(format[i].length-2,2)=="_a") if (format[i].substr(format[i].length-2,2)=="_a")
args[i] = makeArray(args[i]); args[i] = makeArray(args[i]);
} }
@@ -106,12 +106,12 @@ function formatInput(args,format,images) {
if (args.length == 1) { if (args.length == 1) {
json_q = copy(args[0]); json_q = copy(args[0]);
if(!(format_i == "r" || format_i == "l")) { if(!(format_i == "r" || format_i == "l")) {
for (img in json_q) for (var img in json_q)
json_q[img] = makeArray(json_q[img]); json_q[img] = makeArray(json_q[img]);
} }
} }
else if (format_i == "r") { else if (format_i == "r") {
for (img in args[0]) json_q[args[0][img]] = args[1]; for (var img in args[0]) json_q[args[0][img]] = args[1];
} }
else if (format_i == "l") { else if (format_i == "l") {
json_q = { json_q = {
@@ -121,12 +121,12 @@ function formatInput(args,format,images) {
json_q.images[args[0]] = args[1]; json_q.images[args[0]] = args[1];
} }
else { else {
for (img in args[0]) { for (var img in args[0]) {
image = args[0][img]; var image = args[0][img];
json_q[image] = []; json_q[image] = [];
if(format_i == "+") { if(format_i == "+") {
for(s in args[1]) { for(var s in args[1]) {
json_q[image].push({ json_q[image].push({
name: args[1][s], name: args[1][s],
o: args[2] o: args[2]
@@ -139,8 +139,8 @@ function formatInput(args,format,images) {
} }
if(format_i == "^") { if(format_i == "^") {
size = this.images[image].steps.length; var size = this.images[image].steps.length;
index = args[1]; var index = args[1];
index = (index==size)?index:index%size; index = (index==size)?index:index%size;
if (index<0) index += size+1; if (index<0) index += size+1;
json_q[image].push({ json_q[image].push({
@@ -155,7 +155,7 @@ function formatInput(args,format,images) {
if(format_i == "l") { if(format_i == "l") {
json_q.loadedimages = []; json_q.loadedimages = [];
for (i in json_q.images) json_q.loadedimages.push(i); for (var i in json_q.images) json_q.loadedimages.push(i);
} }
return json_q; return json_q;

View File

@@ -24,7 +24,7 @@ ImageSequencer = function ImageSequencer(options) {
if (objTypeOf(a) == "Array") return a.slice(); if (objTypeOf(a) == "Array") return a.slice();
if (objTypeOf(a) == "Object") { if (objTypeOf(a) == "Object") {
var b = {}; var b = {};
for (v in a) { for (var v in a) {
b[v] = copy(a[v]); b[v] = copy(a[v]);
} }
return b; return b;
@@ -36,11 +36,10 @@ ImageSequencer = function ImageSequencer(options) {
return (objTypeOf(input)=="Array")?input:[input]; return (objTypeOf(input)=="Array")?input:[input];
} }
formatInput = require('./FormatInput');
var image, var image,
steps = [], steps = [],
modules = require('./Modules'), modules = require('./Modules'),
formatInput = require('./FormatInput'),
images = {}, images = {},
inputlog = []; inputlog = [];
@@ -50,15 +49,15 @@ ImageSequencer = function ImageSequencer(options) {
function addSteps(){ function addSteps(){
const this_ = (this.name == "ImageSequencer")?this:this.sequencer; const this_ = (this.name == "ImageSequencer")?this:this.sequencer;
args = (this.name == "ImageSequencer")?[]:[this.images]; var args = (this.name == "ImageSequencer")?[]:[this.images];
json_q = {}; var json_q = {};
for(arg in arguments){args.push(copy(arguments[arg]));} for(var arg in arguments){args.push(copy(arguments[arg]));}
json_q = formatInput.call(this_,args,"+"); json_q = formatInput.call(this_,args,"+");
inputlog.push({method:"addSteps", json_q:copy(json_q)}); inputlog.push({method:"addSteps", json_q:copy(json_q)});
for (i in json_q) for (var i in json_q)
for (j in json_q[i]) for (var j in json_q[i])
require("./AddStep")(this_,i,json_q[i][j].name,json_q[i][j].o); require("./AddStep")(this_,i,json_q[i][j].name,json_q[i][j].o);
return this; return this;
@@ -74,18 +73,18 @@ ImageSequencer = function ImageSequencer(options) {
} }
function removeSteps(image,index) { function removeSteps(image,index) {
run = {}; var run = {}, indices;
const this_ = (this.name == "ImageSequencer")?this:this.sequencer; const this_ = (this.name == "ImageSequencer")?this:this.sequencer;
args = (this.name == "ImageSequencer")?[]:[this.images]; var args = (this.name == "ImageSequencer")?[]:[this.images];
for(arg in arguments) args.push(copy(arguments[arg])); for(var arg in arguments) args.push(copy(arguments[arg]));
json_q = formatInput.call(this_,args,"-"); var json_q = formatInput.call(this_,args,"-");
inputlog.push({method:"removeSteps", json_q:copy(json_q)}); inputlog.push({method:"removeSteps", json_q:copy(json_q)});
for (img in json_q) { for (var img in json_q) {
indices = json_q[img].sort(function(a,b){return b-a}); indices = json_q[img].sort(function(a,b){return b-a});
run[img] = indices[indices.length-1]; run[img] = indices[indices.length-1];
for (i in indices) for (var i in indices)
removeStep(img,indices[i]); removeStep(img,indices[i]);
} }
// this.run(run); // This is creating problems // this.run(run); // This is creating problems
@@ -93,18 +92,18 @@ ImageSequencer = function ImageSequencer(options) {
} }
function insertSteps(image, index, name, o) { function insertSteps(image, index, name, o) {
run = {}; var run = {};
const this_ = (this.name == "ImageSequencer")?this:this.sequencer; const this_ = (this.name == "ImageSequencer")?this:this.sequencer;
args = (this.name == "ImageSequencer")?[]:[this.images]; var args = (this.name == "ImageSequencer")?[]:[this.images];
for (arg in arguments) args.push(arguments[arg]); for (var arg in arguments) args.push(arguments[arg]);
json_q = formatInput.call(this_,args,"^"); var json_q = formatInput.call(this_,args,"^");
inputlog.push({method:"insertSteps", json_q:copy(json_q)}); inputlog.push({method:"insertSteps", json_q:copy(json_q)});
for (img in json_q) { for (var img in json_q) {
var details = json_q[img]; var details = json_q[img];
details = details.sort(function(a,b){return b.index-a.index}); details = details.sort(function(a,b){return b.index-a.index});
for (i in details) for (var i in details)
require("./InsertStep")(this_,img,details[i].index,details[i].name,details[i].o); require("./InsertStep")(this_,img,details[i].index,details[i].name,details[i].o);
run[img] = details[details.length-1].index; run[img] = details[details.length-1].index;
} }
@@ -115,15 +114,15 @@ ImageSequencer = function ImageSequencer(options) {
function run(t_image,t_from) { function run(t_image,t_from) {
log('\x1b[32m%s\x1b[0m',"Running the Sequencer!"); log('\x1b[32m%s\x1b[0m',"Running the Sequencer!");
const this_ = (this.name == "ImageSequencer")?this:this.sequencer; const this_ = (this.name == "ImageSequencer")?this:this.sequencer;
args = (this.name == "ImageSequencer")?[]:[this.images]; var args = (this.name == "ImageSequencer")?[]:[this.images];
for (var arg in arguments) args.push(copy(arguments[arg])); for (var arg in arguments) args.push(copy(arguments[arg]));
callback = function() {}; var callback = function() {};
for (var arg in args) for (var arg in args)
if(objTypeOf(args[arg]) == "Function") if(objTypeOf(args[arg]) == "Function")
callback = args.splice(arg,1)[0]; callback = args.splice(arg,1)[0];
json_q = formatInput.call(this_,args,"r"); var json_q = formatInput.call(this_,args,"r");
require('./Run')(this_, json_q, callback); require('./Run')(this_, json_q, callback);
@@ -131,14 +130,14 @@ ImageSequencer = function ImageSequencer(options) {
} }
function loadImages() { function loadImages() {
args = []; var args = [];
for (arg in arguments) args.push(copy(arguments[arg])); for (var arg in arguments) args.push(copy(arguments[arg]));
json_q = formatInput.call(this,args,"l"); var json_q = formatInput.call(this,args,"l");
inputlog.push({method:"loadImages", json_q:copy(json_q)}); inputlog.push({method:"loadImages", json_q:copy(json_q)});
loadedimages = this.copy(json_q.loadedimages); var loadedimages = this.copy(json_q.loadedimages);
for (i in json_q.images) for (var i in json_q.images)
require('./LoadImage')(this,i,json_q.images[i]) require('./LoadImage')(this,i,json_q.images[i])
json_q.callback(); json_q.callback();

View File

@@ -3,7 +3,7 @@ function InsertStep(ref, image, index, name, o) {
function insertStep(image, index, name, o_) { function insertStep(image, index, name, o_) {
ref.log('\x1b[36m%s\x1b[0m','inserting step \"' + name + '\" to \"' + image + '\" at \"'+index+'\".'); ref.log('\x1b[36m%s\x1b[0m','inserting step \"' + name + '\" to \"' + image + '\" at \"'+index+'\".');
o = ref.copy(o_); var o = ref.copy(o_);
o.id = ref.options.sequencerCounter++; //Gives a Unique ID to each step o.id = ref.options.sequencerCounter++; //Gives a Unique ID to each step
o.name = o.name || name; o.name = o.name || name;
o.selector = o.selector || 'ismod-' + name; o.selector = o.selector || 'ismod-' + name;

View File

@@ -1,7 +1,7 @@
function LoadImage(ref, name, src) { function LoadImage(ref, name, src) {
function CImage(src) { function CImage(src) {
datauri = (ref.options.inBrowser || src.substring(0,11) == "data:image/")?(src):require('urify')(src); var datauri = (ref.options.inBrowser || src.substring(0,11) == "data:image/")?(src):require('urify')(src);
image = { var image = {
src: datauri, src: datauri,
format: datauri.split(':')[1].split(';')[0].split('/')[1] format: datauri.split(':')[1].split(';')[0].split('/')[1]
} }
@@ -9,7 +9,7 @@ function LoadImage(ref, name, src) {
} }
function loadImage(name, src) { function loadImage(name, src) {
image = { var image = {
src: src, src: src,
steps: [{ steps: [{
options: { options: {

View File

@@ -1,12 +1,12 @@
function ReplaceImage(ref,selector,steps,options) { function ReplaceImage(ref,selector,steps,options) {
if(!ref.options.inBrowser) return false; // This isn't for Node.js if(!ref.options.inBrowser) return false; // This isn't for Node.js
this_ = ref; var this_ = ref;
var input = document.querySelectorAll(selector); var input = document.querySelectorAll(selector);
var images = []; var images = [];
for (i = 0; i < input.length; i++) for (var i = 0; i < input.length; i++)
if (input[i] instanceof HTMLImageElement) images.push(input[i]); if (input[i] instanceof HTMLImageElement) images.push(input[i]);
for (i in images) { for (var i in images) {
the_image = images[i]; var the_image = images[i];
var url = images[i].src; var url = images[i].src;
var ext = url.split('.').pop(); var ext = url.split('.').pop();

View File

@@ -1,47 +1,47 @@
function Run(ref, json_q, callback) { function Run(ref, json_q, callback) {
function drawStep(drawarray,pos) { function drawStep(drawarray,pos) {
if(pos==drawarray.length) { if(pos==drawarray.length) {
image = drawarray[pos-1].image; var image = drawarray[pos-1].image;
if(ref.objTypeOf(callback)=='Function'){ if(ref.objTypeOf(callback)=='Function'){
steps = ref.images[image].steps; var steps = ref.images[image].steps;
out = steps[steps.length-1].output.src; var out = steps[steps.length-1].output.src;
callback(out); callback(out);
return true; return true;
} }
} }
image = drawarray[pos].image; var image = drawarray[pos].image;
i = drawarray[pos].i; var i = drawarray[pos].i;
input = ref.images[image].steps[i-1].output; var input = ref.images[image].steps[i-1].output;
ref.images[image].steps[i].draw(ref.copy(input),function(){ ref.images[image].steps[i].draw(ref.copy(input),function(){
drawStep(drawarray,++pos); drawStep(drawarray,++pos);
}); });
} }
function drawSteps(json_q) { function drawSteps(json_q) {
drawarray = []; var drawarray = [];
for (image in json_q) { for (var image in json_q) {
no_steps = ref.images[image].steps.length; var no_steps = ref.images[image].steps.length;
init = json_q[image]; var init = json_q[image];
for(i = 0; i < no_steps-init; i++) { for(var i = 0; i < no_steps-init; i++) {
drawarray.push({image: image,i: init+i}); drawarray.push({image: image,i: init+i});
} }
} }
drawStep(drawarray,0); drawStep(drawarray,0);
} }
function filter(json_q){ function filter(json_q){
for (image in json_q) { for (var image in json_q) {
if (json_q[image]==0 && ref.images[image].steps.length==1) if (json_q[image]==0 && ref.images[image].steps.length==1)
delete json_q[image]; delete json_q[image];
else if (json_q[image]==0) json_q[image]++; else if (json_q[image]==0) json_q[image]++;
} }
for (image in json_q) { for (var image in json_q) {
prevstep = ref.images[image].steps[json_q[image]-1]; var prevstep = ref.images[image].steps[json_q[image]-1];
while (typeof(prevstep) == "undefined" || typeof(prevstep.output) == "undefined") { while (typeof(prevstep) == "undefined" || typeof(prevstep.output) == "undefined") {
prevstep = ref.images[image].steps[(--json_q[image]) - 1]; prevstep = ref.images[image].steps[(--json_q[image]) - 1];
} }
} }
return json_q; return json_q;
} }
json_q = filter(json_q); var json_q = filter(json_q);
return drawSteps(json_q); return drawSteps(json_q);
} }
module.exports = Run; module.exports = Run;

View File

@@ -16,7 +16,7 @@
module.exports = function CropModule(options) { module.exports = function CropModule(options) {
options = options || {}; options = options || {};
options.title = "Crop Image"; options.title = "Crop Image";
this_ = this; var this_ = this;
var output var output
function draw(input,callback) { function draw(input,callback) {

View File

@@ -4,7 +4,7 @@
module.exports = function DoNothing(options) { module.exports = function DoNothing(options) {
options = options || {}; options = options || {};
options.title = "Do Nothing"; options.title = "Do Nothing";
this_ = this; var this_ = this;
var output var output
function draw(input,callback) { function draw(input,callback) {

View File

@@ -8,7 +8,7 @@ module.exports = function DoNothingPix(options) {
var output; var output;
function draw(input,callback) { function draw(input,callback) {
this_ = this; var this_ = this;
function changePixel(r, g, b, a) { function changePixel(r, g, b, a) {
return [r, g, b, a]; return [r, g, b, a];
} }

View File

@@ -11,7 +11,7 @@ module.exports = function GreenChannel(options) {
//function setup() {} // optional //function setup() {} // optional
function draw(input,callback) { function draw(input,callback) {
this_ = this; var this_ = this;
function changePixel(r, g, b, a) { function changePixel(r, g, b, a) {
return [0, g, 0, a]; return [0, g, 0, a];
} }

View File

@@ -11,7 +11,7 @@ module.exports = function GreenChannel(options) {
//function setup() {} // optional //function setup() {} // optional
function draw(input,callback) { function draw(input,callback) {
this_ = this; var this_ = this;
function changePixel(r, g, b, a) { function changePixel(r, g, b, a) {
return [255-r, 255-g, 255-b, a]; return [255-r, 255-g, 255-b, a];
} }

View File

@@ -8,7 +8,7 @@ module.exports = function NdviRed(options) {
var output; var output;
function draw(input,callback) { function draw(input,callback) {
this_ = this; var this_ = this;
function changePixel(r, g, b, a) { function changePixel(r, g, b, a) {
var ndvi = (b - r) / (1.00 * b + r); var ndvi = (b - r) / (1.00 * b + r);
var x = 255 * (ndvi + 1) / 2; var x = 255 * (ndvi + 1) / 2;

View File

@@ -5,7 +5,7 @@ module.exports = function SegmentedColormap(options) {
var output; var output;
function draw(input,callback) { function draw(input,callback) {
this_ = this; var this_ = this;
function changePixel(r, g, b, a) { function changePixel(r, g, b, a) {
var ndvi = (b - r) / (r + b); var ndvi = (b - r) / (r + b);
var normalized = (ndvi + 1) / 2; var normalized = (ndvi + 1) / 2;