Skip to content

Commit 426733f

Browse files
harshkhandeparkarjywarren
authored andcommitted
[GCI] Standardised Core code comments (#1343)
* standardise comments * fix * fixes * add docs links * remove extra . * ., this and other changes
1 parent 0ce2cf9 commit 426733f

File tree

2 files changed

+124
-25
lines changed

2 files changed

+124
-25
lines changed

src/ImageSequencer.js

Lines changed: 108 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ if (typeof window !== 'undefined') { isBrowser = true; }
22
else { var isBrowser = false; }
33
require('./util/getStep.js');
44

5+
/**
6+
* @method ImageSequencer
7+
* @param {Object|Float32Array} options Optional options
8+
* @returns {Object}
9+
*/
510
ImageSequencer = function ImageSequencer(options) {
611

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

22+
/**
23+
* @method log
24+
* @description Logs colored messages to the console using ASCII color codes
25+
* @param {String} color ASCII color code
26+
* @param {String} msg Message to be logged to the console
27+
*/
1728
function log(color, msg) {
1829
if (options.ui != 'none') {
1930
if (arguments.length == 1) console.log(arguments[0]);
2031
else if (arguments.length == 2) console.log(color, msg);
2132
}
2233
}
2334

35+
/**
36+
* @method copy
37+
* @description Returns a clone of the input object.
38+
* @param {Object|Float32Array} a The Object/Array to be cloned
39+
* @returns {Object|Float32Array}
40+
*/
2441
function copy(a) {
2542
if (!typeof (a) == 'object') return a;
2643
if (objTypeOf(a) == 'Array') return a.slice();
@@ -53,22 +70,27 @@ ImageSequencer = function ImageSequencer(options) {
5370
for (o in sequencer) {
5471
modules[o] = sequencer[o];
5572
}
56-
sequences = JSON.parse(window.localStorage.getItem('sequences'));
73+
sequences = JSON.parse(window.localStorage.getItem('sequences')); // Get saved sequences from localStorage
5774
if (!sequences) {
5875
sequences = {};
59-
window.localStorage.setItem('sequences', JSON.stringify(sequences));
76+
window.localStorage.setItem('sequences', JSON.stringify(sequences)); // Set the localStorage entry as an empty Object by default
6077
}
6178
}
6279

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

84+
/**
85+
* @method addSteps
86+
* @description Adds one of more steps to the sequence.
87+
* @return {Object}
88+
*/
6789
function addSteps() {
6890
var this_ = (this.name == 'ImageSequencer') ? this : this.sequencer;
6991
var args = [];
7092
var json_q = {};
71-
for (var arg in arguments) { args.push(copy(arguments[arg])); }
93+
for (var arg in arguments) { args.push(copy(arguments[arg])); } // Get all the module names from the arguments
7294
json_q = formatInput.call(this_, args, '+');
7395

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

102+
/**
103+
* @method removeStep
104+
* @description Removes the step at the specified index from the sequence.
105+
* @param {Object} ref ImageSequencer instance
106+
* @param {Number} index Index of the step to be removed
107+
* @returns {Null}
108+
*/
80109
function removeStep(ref, index) {
81-
//remove the step from images[image].steps and redraw remaining images
110+
// Remove the step from images[image].steps and redraw remaining images
82111
if (index > 0) {
83-
//var this_ = (this.name == "ImageSequencer") ? this : this.sequencer;
112+
// var this_ = (this.name == "ImageSequencer") ? this : this.sequencer;
84113
thisStep = ref.steps[index];
85114
thisStep.UI.onRemove(thisStep.options.step);
86115
ref.steps.splice(index, 1);
87116
}
88-
//tell the UI a step has been removed
89117
}
90118

119+
/**
120+
* @method removeSteps
121+
* @description Removes one or more steps from the sequence
122+
* @returns {Object}
123+
*/
91124
function removeSteps() {
92125
var indices;
93126
var this_ = (this.name == 'ImageSequencer') ? this : this.sequencer;
@@ -103,6 +136,11 @@ ImageSequencer = function ImageSequencer(options) {
103136
return this;
104137
}
105138

139+
/**
140+
* @method insertSteps
141+
* @description Inserts steps at the specified index
142+
* @returns {Object}
143+
*/
106144
function insertSteps() {
107145
var this_ = (this.name == 'ImageSequencer') ? this : this.sequencer;
108146
var args = [];
@@ -118,8 +156,11 @@ ImageSequencer = function ImageSequencer(options) {
118156
return this;
119157
}
120158

121-
// Config is an object which contains the runtime configuration like progress bar
122-
// information and index from which the sequencer should run
159+
/**
160+
* @method run
161+
* @param {Object} config Object which contains the runtime configuration like progress bar information and index from which the sequencer should run.
162+
* @returns {Boolean}
163+
*/
123164
function run(config) {
124165
var progressObj, index = 0;
125166
config = config || { mode: 'no-arg' };
@@ -137,7 +178,7 @@ ImageSequencer = function ImageSequencer(options) {
137178
var callback = function() { };
138179
for (var arg in args)
139180
if (objTypeOf(args[arg]) == 'Function')
140-
callback = args.splice(arg, 1)[0]; //callback is formed
181+
callback = args.splice(arg, 1)[0]; // Callback is formed
141182

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

@@ -146,6 +187,11 @@ ImageSequencer = function ImageSequencer(options) {
146187
return true;
147188
}
148189

190+
/**
191+
* @method loadImages
192+
* @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.
193+
* @returns {Null}
194+
*/
149195
function loadImages() {
150196
var args = [];
151197
var prevSteps = this.getSteps().slice(1).map(step=>step.options.name);
@@ -182,17 +228,35 @@ ImageSequencer = function ImageSequencer(options) {
182228

183229
}
184230

231+
/**
232+
* @method replaceImage
233+
* @description Replaces the current image in the sequencer
234+
* @param {String} selector DOM selector string for the image input
235+
* @param {*} steps Current steps Object
236+
* @param {Object} options
237+
* @returns {*}
238+
*/
185239
function replaceImage(selector, steps, options) {
186240
options = options || {};
187241
options.callback = options.callback || function() { };
188242
return require('./ReplaceImage')(this, selector, steps, options);
189243
}
190244

191-
//returns the steps added
245+
/**
246+
* @method getSteps
247+
* @description Returns the current sequence of steps
248+
* @returns {Object}
249+
*/
192250
function getSteps(){
193251
return this.steps;
194252
}
195253

254+
/**
255+
* @method setUI
256+
* @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.
257+
* @param {Object} UI Object containing UI callback methods. Read the docs(https://github.com/publiclab/image-sequencer/blob/main/README.md) for more info.
258+
* @returns {Null}
259+
*/
196260
function setUI(UI) {
197261
this.events = require('./ui/UserInterface')(UI);
198262
}
@@ -201,6 +265,12 @@ ImageSequencer = function ImageSequencer(options) {
201265
return require('./ExportBin')(dir, this, basic, filename);
202266
};
203267

268+
/**
269+
* @method modulesInfo
270+
* @description Returns information about the given module or all the available modules
271+
* @param {String} name Module name
272+
* @returns {Object}
273+
*/
204274
function modulesInfo(name) {
205275
var modulesdata = {};
206276
if (name == 'load-image') return {};
@@ -222,23 +292,30 @@ ImageSequencer = function ImageSequencer(options) {
222292
return modulesdata;
223293
}
224294

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

227304
if (!options) {
228305
return this;
229306

230307
} else if (Array.isArray(options)) {
231-
// contains the array of module and info
308+
// Contains the array of module and info
232309
this.modules[name] = options;
233310

234311
} else if (options.func && options.info) {
235-
// passed in options object
312+
// Passed in options object
236313
this.modules[name] = [
237314
options.func, options.info
238315
];
239316

240317
} else if (options.path && !this.inBrowser) {
241-
// load from path(only in node)
318+
// Load from path(only in node)
242319
const module = [
243320
require(`${options.path}/Module.js`),
244321
require(`${options.path}/info.json`)
@@ -248,6 +325,13 @@ ImageSequencer = function ImageSequencer(options) {
248325
return this;
249326
}
250327

328+
/**
329+
* @method saveNewModule
330+
* @description Saves a new local module to ImageSequencer
331+
* @param {String} name Name of the new module
332+
* @param {String} path Path to the new module
333+
* @returns {Null}
334+
*/
251335
function saveNewModule(name, path) {
252336
if (options.inBrowser) {
253337
// Not for browser context
@@ -258,6 +342,13 @@ ImageSequencer = function ImageSequencer(options) {
258342
fs.writeFileSync('./src/Modules.js', mods);
259343
}
260344

345+
/**
346+
* @method saveSequence
347+
* @description Saves a sequence on the browser localStorage.
348+
* @param {String} name Name for the sequence
349+
* @param {String} sequenceString Sequence data as a string
350+
* @returns {Null}
351+
*/
261352
function saveSequence(name, sequenceString) { // 4. save sequence
262353
const sequence = str.stringToJSON(sequenceString);
263354
// Save the given sequence string as a module
@@ -276,7 +367,7 @@ ImageSequencer = function ImageSequencer(options) {
276367
}
277368

278369
function loadModules() {
279-
// This function loads the modules and saved sequences
370+
// loadModules function loads the modules and saved sequences.
280371
this.modules = require('./Modules');
281372
if (options.inBrowser)
282373
this.sequences = JSON.parse(window.localStorage.getItem('sequences'));
@@ -286,7 +377,7 @@ ImageSequencer = function ImageSequencer(options) {
286377

287378

288379
return {
289-
//literals and objects
380+
// Literals and objects
290381
name: 'ImageSequencer',
291382
options: options,
292383
inputlog: inputlog,
@@ -296,7 +387,7 @@ ImageSequencer = function ImageSequencer(options) {
296387
steps: steps,
297388
image: image,
298389

299-
//user functions
390+
// User functions
300391
loadImages: loadImages,
301392
loadImage: loadImages,
302393
addSteps: addSteps,
@@ -325,7 +416,7 @@ ImageSequencer = function ImageSequencer(options) {
325416
loadModules: loadModules,
326417
getSteps:getSteps,
327418

328-
//other functions
419+
// Other functions
329420
log: log,
330421
objTypeOf: objTypeOf,
331422
copy: copy,

src/modules/_nomodule/PixelManipulation.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
* General purpose per-pixel manipulation
33
* accepting a changePixel() method to remix a pixel's channels
44
*/
5+
6+
/**
7+
* @method PixelManipulation
8+
* @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.
9+
* @param {Object} image ndarray of pixels of the image
10+
* @param {Object} options object containing callbacks for manipulating pixels.
11+
* @returns {Null}
12+
*/
513
module.exports = function PixelManipulation(image, options) {
614
// To handle the case where pixelmanipulation is called on the input object itself
715
// like input.pixelManipulation(options)
@@ -30,18 +38,18 @@ module.exports = function PixelManipulation(image, options) {
3038
};
3139
}
3240

33-
// iterate through pixels;
41+
// Iterate through pixels
3442
// TODO: this could possibly be more efficient; see
3543
// https://github.com/p-v-o-s/infragram-js/blob/master/public/infragram.js#L173-L181
3644

3745

38-
if (options.preProcess) pixels = options.preProcess(pixels); // Allow for preprocessing
46+
if (options.preProcess) pixels = options.preProcess(pixels); // Allow for preprocessing of pixels.
3947

4048
function extraOperation() {
4149
var res;
42-
if (options.extraManipulation) res = options.extraManipulation(pixels, generateOutput);
43-
// there may be a more efficient means to encode an image object,
44-
// but node modules and their documentation are essentially arcane on this point
50+
if (options.extraManipulation) res = options.extraManipulation(pixels, generateOutput); // extraManipulation is used to manipulate each pixel individually.
51+
// There may be a more efficient means to encode an image object,
52+
// but node modules and their documentation are essentially arcane on this point.
4553
function generateOutput() {
4654
var chunks = [];
4755
var totalLength = 0;
@@ -80,7 +88,7 @@ module.exports = function PixelManipulation(image, options) {
8088
env: {
8189
consoleLog: console.log,
8290
perform: function (x, y) {
83-
let pixel = options.changePixel(
91+
let pixel = options.changePixel( // changePixel function is run over every pixel.
8492
pixels.get(x, y, 0),
8593
pixels.get(x, y, 1),
8694
pixels.get(x, y, 2),
@@ -95,7 +103,7 @@ module.exports = function PixelManipulation(image, options) {
95103
}
96104
};
97105

98-
function perPixelManipulation() { // pure JavaScript code
106+
function perPixelManipulation() {
99107
for (var x = 0; x < pixels.shape[0]; x++) {
100108
for (var y = 0; y < pixels.shape[1]; y++) {
101109
imports.env.perform(x, y);
@@ -145,4 +153,4 @@ module.exports = function PixelManipulation(image, options) {
145153
}
146154
}
147155
});
148-
};
156+
};

0 commit comments

Comments
 (0)