Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GCI] Standardised Core code comments #1343

Merged
merged 10 commits into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 112 additions & 21 deletions src/ImageSequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ if (typeof window !== 'undefined') { isBrowser = true; }
else { var isBrowser = false; }
require('./util/getStep.js');

/**
* @method ImageSequencer
* @param {Object|Float32Array} options Optional options
* @returns {Object}
*/
ImageSequencer = function ImageSequencer(options) {

var str = require('./Strings.js')(this.steps, modulesInfo, addSteps, copy);
Expand All @@ -14,13 +19,25 @@ ImageSequencer = function ImageSequencer(options) {
return Object.prototype.toString.call(object).split(' ')[1].slice(0, -1);
}

/**
* @method log
* @description Logs colored messages to the console using ASCII color codes.
SidharthBansal marked this conversation as resolved.
Show resolved Hide resolved
* @param {String} color ASCII color code
* @param {String} msg Message to be logged to the console.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
*/
function log(color, msg) {
if (options.ui != 'none') {
if (arguments.length == 1) console.log(arguments[0]);
else if (arguments.length == 2) console.log(color, msg);
}
}

/**
* @method copy
* @description Returns a clone of the input object.
* @param {Object|Float32Array} a The Object/Array to be cloned
* @returns {Object|Float32Array}
*/
function copy(a) {
if (!typeof (a) == 'object') return a;
if (objTypeOf(a) == 'Array') return a.slice();
Expand Down Expand Up @@ -53,22 +70,27 @@ ImageSequencer = function ImageSequencer(options) {
for (o in sequencer) {
modules[o] = sequencer[o];
}
sequences = JSON.parse(window.localStorage.getItem('sequences'));
sequences = JSON.parse(window.localStorage.getItem('sequences')); // Get saved sequences from localStorage.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
if (!sequences) {
sequences = {};
window.localStorage.setItem('sequences', JSON.stringify(sequences));
window.localStorage.setItem('sequences', JSON.stringify(sequences)); // Set the localStorage entry as an empty Object by default.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
}
}

// if in browser, prompt for an image
// if (options.imageSelect || options.inBrowser) addStep('image-select');
// else if (options.imageUrl) loadImage(imageUrl);

/**
* @method addSteps
* @description Adds one of more steps to the sequence.
* @return {Object}
*/
function addSteps() {
var this_ = (this.name == 'ImageSequencer') ? this : this.sequencer;
var args = [];
var json_q = {};
for (var arg in arguments) { args.push(copy(arguments[arg])); }
for (var arg in arguments) { args.push(copy(arguments[arg])); } // Get all the module names from the arguments.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
json_q = formatInput.call(this_, args, '+');

inputlog.push({ method: 'addSteps', json_q: copy(json_q) });
Expand All @@ -77,17 +99,28 @@ ImageSequencer = function ImageSequencer(options) {
return this;
}

/**
* @method removeStep
* @description Removes the step at the specified index from the sequence.
* @param {Object} ref ImageSequencer instance
* @param {Number} index Index of the step to be removed
* @returns {Null}
*/
function removeStep(ref, index) {
//remove the step from images[image].steps and redraw remaining images
// Remove the step from images[image].steps and redraw remaining images.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
if (index > 0) {
//var this_ = (this.name == "ImageSequencer") ? this : this.sequencer;
// var this_ = (this.name == "ImageSequencer") ? this : this.sequencer;
thisStep = ref.steps[index];
thisStep.UI.onRemove(thisStep.options.step);
ref.steps.splice(index, 1);
}
//tell the UI a step has been removed
}

/**
* @method removeSteps
* @description Removes one or more steps from the sequence.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Object}
*/
function removeSteps() {
var indices;
var this_ = (this.name == 'ImageSequencer') ? this : this.sequencer;
Expand All @@ -103,6 +136,11 @@ ImageSequencer = function ImageSequencer(options) {
return this;
}

/**
* @method insertSteps
* @description Inserts steps at the specified index.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Object}
*/
function insertSteps() {
var this_ = (this.name == 'ImageSequencer') ? this : this.sequencer;
var args = [];
Expand All @@ -118,8 +156,11 @@ ImageSequencer = function ImageSequencer(options) {
return this;
}

// Config is an object which contains the runtime configuration like progress bar
// information and index from which the sequencer should run
/**
* @method run
* @param {Object} config Object which contains the runtime configuration like progress bar information and index from which the sequencer should run.
* @returns {Boolean}
*/
function run(config) {
var progressObj, index = 0;
config = config || { mode: 'no-arg' };
Expand All @@ -137,7 +178,7 @@ ImageSequencer = function ImageSequencer(options) {
var callback = function() { };
for (var arg in args)
if (objTypeOf(args[arg]) == 'Function')
callback = args.splice(arg, 1)[0]; //callback is formed
callback = args.splice(arg, 1)[0]; // Callback is formed

var json_q = formatInput.call(this_, args, 'r');

Expand All @@ -146,6 +187,11 @@ ImageSequencer = function ImageSequencer(options) {
return true;
}

/**
* @method loadImages
* @description Loads an image via dataURL or normal URL. Read the docs(https://github.com/publiclab/image-sequencer/blob/main/README.md) for more info.
* @returns {Null}
*/
function loadImages() {
var args = [];
var prevSteps = this.getSteps().slice(1).map(step=>step.options.name);
Expand Down Expand Up @@ -182,17 +228,35 @@ ImageSequencer = function ImageSequencer(options) {

}

/**
* @method replaceImage
* @description Replaces the current image in the sequencer.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @param {String} selector DOM selector string for the image input.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @param {*} steps Current steps Object
* @param {Object} options
* @returns {*}
*/
function replaceImage(selector, steps, options) {
options = options || {};
options.callback = options.callback || function() { };
return require('./ReplaceImage')(this, selector, steps, options);
}

//returns the steps added
/**
* @method getSteps
* @description Returns the current sequence of steps.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Object}
*/
function getSteps(){
return this.steps;
}

/**
* @method setUI
* @description To set up a UI for ImageSequencer via different callback methods. Read the docs(https://github.com/publiclab/image-sequencer/blob/main/README.md) for more info.
* @param {Object} UI Object containing UI callback methods. Read the docs(https://github.com/publiclab/image-sequencer/blob/main/README.md) for more info.
* @returns {Null}
*/
function setUI(UI) {
this.events = require('./ui/UserInterface')(UI);
}
Expand All @@ -201,6 +265,12 @@ ImageSequencer = function ImageSequencer(options) {
return require('./ExportBin')(dir, this, basic, filename);
};

/**
* @method modulesInfo
* @description Returns information about the given module or all the available modules.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @param {String} name Module name
* @returns {Object}
*/
function modulesInfo(name) {
var modulesdata = {};
if (name == 'load-image') return {};
Expand All @@ -222,23 +292,30 @@ ImageSequencer = function ImageSequencer(options) {
return modulesdata;
}

/**
* @method loadNewModule
* @description Adds a new local module to sequencer. Read the docs(https://github.com/publiclab/image-sequencer/blob/main/README.md) for mode info.
* @param {String} name Name of the new module
* @param {Object} options An Object containing path and info about the new module.
* @returns {Object}
*/
function loadNewModule(name, options) {

if (!options) {
return this;

} else if (Array.isArray(options)) {
// contains the array of module and info
// Contains the array of module and info.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many extra .
Please correct it

harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
this.modules[name] = options;

} else if (options.func && options.info) {
// passed in options object
// Passed in options object
this.modules[name] = [
options.func, options.info
];

} else if (options.path && !this.inBrowser) {
// load from path(only in node)
// Load from path(only in node)
const module = [
require(`${options.path}/Module.js`),
require(`${options.path}/info.json`)
Expand All @@ -248,21 +325,35 @@ ImageSequencer = function ImageSequencer(options) {
return this;
}

/**
* @method saveNewModule
* @description Saves a new local module to ImageSequencer.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
* @param {String} name Name of the new module
* @param {String} path Path to the new module
* @returns {Null}
*/
function saveNewModule(name, path) {
if (options.inBrowser) {
// Not for browser context
// Not for browser context.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
return;
}
var mods = fs.readFileSync('./src/Modules.js').toString();
mods = mods.substr(0, mods.length - 1) + ' \'' + name + '\': require(\'' + path + '\'),\n}';
fs.writeFileSync('./src/Modules.js', mods);
}

/**
* @method saveSequence
* @description Saves a sequence on the browser localStorage.
* @param {String} name Name for the sequence
* @param {String} sequenceString Sequence data as a string
* @returns {Null}
*/
function saveSequence(name, sequenceString) { // 4. save sequence
const sequence = str.stringToJSON(sequenceString);
// Save the given sequence string as a module
// Save the given sequence string as a module.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
if (options.inBrowser) {
// Inside the browser we save the meta-modules using the Web Storage API
// Inside the browser we save the meta-modules using the Web Storage API.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
var sequences = JSON.parse(window.localStorage.getItem('sequences'));
sequences[name] = sequence;
window.localStorage.setItem('sequences', JSON.stringify(sequences));
Expand All @@ -276,7 +367,7 @@ ImageSequencer = function ImageSequencer(options) {
}

function loadModules() {
// This function loads the modules and saved sequences
// This function loads the modules and saved sequences.
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
this.modules = require('./Modules');
if (options.inBrowser)
this.sequences = JSON.parse(window.localStorage.getItem('sequences'));
Expand All @@ -286,7 +377,7 @@ ImageSequencer = function ImageSequencer(options) {


return {
//literals and objects
// Literals and objects
name: 'ImageSequencer',
options: options,
inputlog: inputlog,
Expand All @@ -296,7 +387,7 @@ ImageSequencer = function ImageSequencer(options) {
steps: steps,
image: image,

//user functions
// User functions
loadImages: loadImages,
loadImage: loadImages,
addSteps: addSteps,
Expand Down Expand Up @@ -325,7 +416,7 @@ ImageSequencer = function ImageSequencer(options) {
loadModules: loadModules,
getSteps:getSteps,

//other functions
// Other functions
log: log,
objTypeOf: objTypeOf,
copy: copy,
Expand All @@ -335,4 +426,4 @@ ImageSequencer = function ImageSequencer(options) {
};

};
module.exports = ImageSequencer;
module.exports = ImageSequencer;
22 changes: 15 additions & 7 deletions src/modules/_nomodule/PixelManipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
* General purpose per-pixel manipulation
* accepting a changePixel() method to remix a pixel's channels
*/

/**
* @method PixelManipulation
* @description Function for changing pixel values of the image via different callback functions. Read the docs(https://github.com/publiclab/image-sequencer/blob/main/CONTRIBUTING.md) for more info.
* @param {Object} image ndarray of pixels of the image
* @param {Object} options object containing callbacks for manipulating pixels.
* @returns {Null}
*/
module.exports = function PixelManipulation(image, options) {
// To handle the case where pixelmanipulation is called on the input object itself
// like input.pixelManipulation(options)
Expand Down Expand Up @@ -30,18 +38,18 @@ module.exports = function PixelManipulation(image, options) {
};
}

// iterate through pixels;
// Iterate through pixels;
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
// TODO: this could possibly be more efficient; see
// https://github.com/p-v-o-s/infragram-js/blob/master/public/infragram.js#L173-L181


if (options.preProcess) pixels = options.preProcess(pixels); // Allow for preprocessing
if (options.preProcess) pixels = options.preProcess(pixels); // Allow for preprocessing of pixels.

function extraOperation() {
var res;
if (options.extraManipulation) res = options.extraManipulation(pixels, generateOutput);
// there may be a more efficient means to encode an image object,
// but node modules and their documentation are essentially arcane on this point
if (options.extraManipulation) res = options.extraManipulation(pixels, generateOutput); // extraManipulation is used to manipulate each pixel individually.
// There may be a more efficient means to encode an image object,
// but node modules and their documentation are essentially arcane on this point.
function generateOutput() {
var chunks = [];
var totalLength = 0;
Expand Down Expand Up @@ -80,7 +88,7 @@ module.exports = function PixelManipulation(image, options) {
env: {
consoleLog: console.log,
perform: function (x, y) {
let pixel = options.changePixel(
let pixel = options.changePixel( // changePixel function is run over every pixel.
pixels.get(x, y, 0),
pixels.get(x, y, 1),
pixels.get(x, y, 2),
Expand All @@ -95,7 +103,7 @@ module.exports = function PixelManipulation(image, options) {
}
};

function perPixelManipulation() { // pure JavaScript code
function perPixelManipulation() {
for (var x = 0; x < pixels.shape[0]; x++) {
for (var y = 0; y < pixels.shape[1]; y++) {
imports.env.perform(x, y);
Expand Down