From 04e39a8d3d11f24693cbb6fca72681a2be389b3b Mon Sep 17 00:00:00 2001 From: Chinmay Pandhare Date: Fri, 14 Jul 2017 09:27:07 +0530 Subject: [PATCH] Modified Modules Folder Structure --- index.js | 16 ++-- src/Modules.js | 14 ++-- src/modules/Crop.js | 63 --------------- src/modules/Crop/Crop.js | 33 ++++++++ src/modules/Crop/Module.js | 42 ++++++++++ .../{DoNothing.js => DoNothing/Module.js} | 2 +- .../Module.js} | 5 +- src/modules/FishEye.js | 23 ------ .../Module.js} | 2 +- src/modules/ImageSelect.js | 81 ------------------- src/modules/{Invert.js => Invert/Module.js} | 2 +- src/modules/{NdviRed.js => NdviRed/Module.js} | 10 +-- src/modules/Plot.js | 73 ----------------- src/modules/Resize.js | 25 ------ src/modules/SegmentedColormap/Module.js | 32 ++++++++ .../SegmentedColormap.js | 70 ++++++---------- src/modules/{ => Threshold}/ImageThreshold.js | 0 src/modules/_Step.js | 9 --- .../{ => _nomodule}/PixelManipulation.js | 0 19 files changed, 154 insertions(+), 348 deletions(-) delete mode 100644 src/modules/Crop.js create mode 100644 src/modules/Crop/Crop.js create mode 100644 src/modules/Crop/Module.js rename src/modules/{DoNothing.js => DoNothing/Module.js} (80%) rename src/modules/{DoNothingPix.js => DoNothingPix/Module.js} (84%) delete mode 100644 src/modules/FishEye.js rename src/modules/{GreenChannel.js => GreenChannel/Module.js} (92%) delete mode 100644 src/modules/ImageSelect.js rename src/modules/{Invert.js => Invert/Module.js} (92%) rename src/modules/{NdviRed.js => NdviRed/Module.js} (74%) delete mode 100644 src/modules/Plot.js delete mode 100644 src/modules/Resize.js create mode 100644 src/modules/SegmentedColormap/Module.js rename src/modules/{ => SegmentedColormap}/SegmentedColormap.js (68%) rename src/modules/{ => Threshold}/ImageThreshold.js (100%) delete mode 100644 src/modules/_Step.js rename src/modules/{ => _nomodule}/PixelManipulation.js (100%) diff --git a/index.js b/index.js index 23b0f83e..07304e01 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,11 @@ console.log('\x1b[31m%s\x1b[0m',"This is the output of the module"); require('./src/ImageSequencer'); sequencer = ImageSequencer(); -sequencer.loadImages({images:{red:'examples/red.jpg'},callback:function(){ - sequencer.addSteps(['do-nothing-pix','ndvi-red','invert']); - sequencer.removeSteps(1); - sequencer.insertSteps({ - red: [{index: -1, name: 'do-nothing-pix', o:{}}] - }); - sequencer.run(); -}}); +// sequencer.loadImages({images:{red:'examples/red.jpg'},callback:function(){ +// sequencer.addSteps(['do-nothing-pix','ndvi-red','invert']); +// sequencer.removeSteps(1); +// sequencer.insertSteps({ +// red: [{index: -1, name: 'do-nothing-pix', o:{}}] +// }); +// sequencer.run(); +// }}); diff --git a/src/Modules.js b/src/Modules.js index b37b64e9..3c5df5ba 100644 --- a/src/Modules.js +++ b/src/Modules.js @@ -2,11 +2,11 @@ * Core modules */ module.exports = { - 'do-nothing': require('./modules/DoNothing'), - 'green-channel': require('./modules/GreenChannel'), - 'ndvi-red': require('./modules/NdviRed'), - 'do-nothing-pix': require('./modules/DoNothingPix'), - 'invert': require('./modules/Invert'), - 'crop': require('./modules/Crop'), - 'segmented-colormap': require('./modules/SegmentedColormap') + 'do-nothing': require('./modules/DoNothing/Module'), + 'green-channel': require('./modules/GreenChannel/Module'), + 'ndvi-red': require('./modules/NdviRed/Module'), + 'do-nothing-pix': require('./modules/DoNothingPix/Module.js'), + 'invert': require('./modules/Invert/Module'), + 'crop': require('./modules/Crop/Module'), + 'segmented-colormap': require('./modules/SegmentedColormap/Module') } diff --git a/src/modules/Crop.js b/src/modules/Crop.js deleted file mode 100644 index be5909c6..00000000 --- a/src/modules/Crop.js +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Image Cropping module - * Usage: - * Expected Inputs: - * options.x : x-coordinate of image where the modules starts cropping | default : 0 - * options.y : y-coordinate of image where the modules starts cropping | default : 0 - * options.w : width of the resulting cropped image | default : 50% of input image width - * options.h : height of the resulting cropped image | default : 50% of input image height - * Output: - * The cropped image, which is essentially a rectangle bounded by the lines: - * x = options.x - * x = options.x + options.w - * y = options.y - * y = options.y + options.h - */ - module.exports = function Crop(options) { - options = options || {}; - options.title = "Do Nothing"; - this_ = this; - var output - var getPixels = require("get-pixels"), - savePixels = require("save-pixels"), - base64 = require('base64-stream'); - - function draw(input,callback) { - - const this_ = this; - - getPixels(input.src,function(err,pixels){ - var newdata = []; - var ox = options.x || 0; - var oy = options.y || 0; - var w = options.w || Math.floor(0.5*pixels.shape[0]); - var h = options.h || Math.floor(0.5*pixels.shape[1]); - var iw = pixels.shape[0]; //Width of Original Image - newarray = new Uint8Array(4*w*h); - for (var n = oy; n < oy + h; n++) { - newarray.set(pixels.data.slice(n*4*iw + ox, n*4*iw + ox + 4*w),4*w*(n-oy)); - } - pixels.data = newarray; - pixels.shape = [w,h,4]; - pixels.stride[1] = 4*w; - - options.format = "jpeg"; - - w = base64.encode(); - var r = savePixels(pixels, options.format); - r.pipe(w).on('finish',function(){ - data = w.read().toString(); - datauri = 'data:image/' + options.format + ';base64,' + data; - this_.output = {src:datauri,format:options.format}; - callback(); - }); - }); - - } - - return { - options: options, - draw: draw, - output: output - } - } diff --git a/src/modules/Crop/Crop.js b/src/modules/Crop/Crop.js new file mode 100644 index 00000000..ec9ad1c3 --- /dev/null +++ b/src/modules/Crop/Crop.js @@ -0,0 +1,33 @@ +module.exports = function Crop(input,options,callback) { + + var getPixels = require("get-pixels"), + savePixels = require("save-pixels"), + base64 = require('base64-stream'); + + getPixels(input.src,function(err,pixels){ + var newdata = []; + var ox = options.x || 0; + var oy = options.y || 0; + var w = options.w || Math.floor(0.5*pixels.shape[0]); + var h = options.h || Math.floor(0.5*pixels.shape[1]); + var iw = pixels.shape[0]; //Width of Original Image + newarray = new Uint8Array(4*w*h); + for (var n = oy; n < oy + h; n++) { + newarray.set(pixels.data.slice(n*4*iw + ox, n*4*iw + ox + 4*w),4*w*(n-oy)); + } + pixels.data = newarray; + pixels.shape = [w,h,4]; + pixels.stride[1] = 4*w; + + options.format = "jpeg"; + + w = base64.encode(); + var r = savePixels(pixels, options.format); + r.pipe(w).on('finish',function(){ + data = w.read().toString(); + datauri = 'data:image/' + options.format + ';base64,' + data; + callback(datauri,options.format); + }); + }); + +} diff --git a/src/modules/Crop/Module.js b/src/modules/Crop/Module.js new file mode 100644 index 00000000..61ae9e13 --- /dev/null +++ b/src/modules/Crop/Module.js @@ -0,0 +1,42 @@ +/* + * Image Cropping module + * Usage: + * Expected Inputs: + * options.x : x-coordinate of image where the modules starts cropping | default : 0 + * options.y : y-coordinate of image where the modules starts cropping | default : 0 + * options.w : width of the resulting cropped image | default : 50% of input image width + * options.h : height of the resulting cropped image | default : 50% of input image height + * Output: + * The cropped image, which is essentially a rectangle bounded by the lines: + * x = options.x + * x = options.x + options.w + * y = options.y + * y = options.y + options.h + */ + module.exports = function CropModule(options) { + options = options || {}; + options.title = "Crop Image"; + this_ = this; + var output + + function draw(input,callback) { + + const this_ = this; + + require('./Crop')(input,options,function(out,format){ + this_.output = { + src: out, + format: format + } + callback(); + }); + + + } + + return { + options: options, + draw: draw, + output: output + } + } diff --git a/src/modules/DoNothing.js b/src/modules/DoNothing/Module.js similarity index 80% rename from src/modules/DoNothing.js rename to src/modules/DoNothing/Module.js index 7b761529..fbebb713 100644 --- a/src/modules/DoNothing.js +++ b/src/modules/DoNothing/Module.js @@ -1,5 +1,5 @@ /* - * Demo Module. Does nothing. + * Demo Module. Does nothing. Adds a step where output is equal to input. */ module.exports = function DoNothing(options) { options = options || {}; diff --git a/src/modules/DoNothingPix.js b/src/modules/DoNothingPix/Module.js similarity index 84% rename from src/modules/DoNothingPix.js rename to src/modules/DoNothingPix/Module.js index 4c28662b..135b84bc 100644 --- a/src/modules/DoNothingPix.js +++ b/src/modules/DoNothingPix/Module.js @@ -7,8 +7,6 @@ module.exports = function DoNothingPix(options) { options.title = "Do Nothing with pixels"; var output; - //function setup() {} // optional - function draw(input,callback) { this_ = this; function changePixel(r, g, b, a) { @@ -17,7 +15,7 @@ module.exports = function DoNothingPix(options) { function output(image,datauri,mimetype){ this_.output = {src:datauri,format:mimetype} } - return require('./PixelManipulation.js')(input, { + return require('../_nomodule/PixelManipulation.js')(input, { output: output, changePixel: changePixel, format: input.format, @@ -28,7 +26,6 @@ module.exports = function DoNothingPix(options) { return { options: options, - //setup: setup, // optional draw: draw, output: output } diff --git a/src/modules/FishEye.js b/src/modules/FishEye.js deleted file mode 100644 index 1045ee80..00000000 --- a/src/modules/FishEye.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Display only the green channel - */ -module.exports = function FishEye(options) { - - options = options || {}; - options.title = "Fish Eye"; - options.description = "Corrects fish eye or barrel distortion."; - var output; - - //function setup() {} // optional - - function draw(input,callback) { - - } - - return { - options: options, - //setup: setup, // optional - draw: draw, - output: output - } -} diff --git a/src/modules/GreenChannel.js b/src/modules/GreenChannel/Module.js similarity index 92% rename from src/modules/GreenChannel.js rename to src/modules/GreenChannel/Module.js index 962c1a5e..9bb585cb 100644 --- a/src/modules/GreenChannel.js +++ b/src/modules/GreenChannel/Module.js @@ -18,7 +18,7 @@ module.exports = function GreenChannel(options) { function output(image,datauri,mimetype){ this_.output = {src:datauri,format:mimetype} } - return require('./PixelManipulation.js')(input, { + return require('../_nomodule/PixelManipulation.js')(input, { output: output, changePixel: changePixel, format: input.format, diff --git a/src/modules/ImageSelect.js b/src/modules/ImageSelect.js deleted file mode 100644 index a4d14251..00000000 --- a/src/modules/ImageSelect.js +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Special module to kick off the sequence - * -- depends on jQuery for interface setup & drag & drop - */ -module.exports = function ImageSelect(options) { - - if (!window.hasOwnProperty('$')) window.$ = window.jQuery = require('jquery'); - - options = options || {}; - options.title = "Select image"; - options.inputSelector = options.inputSelector || "#file-select"; - - var image, - el = $('.' + options.selector + ' .mod-drop'); - - function setup() { - - // CSS UI - el.on('dragenter', function(e) { - el.addClass('hover'); - }); - - el.on('dragleave', function(e) { - el.removeClass('hover'); - }); - - // Drag & Drop behavior - function onImage(e) { - e.preventDefault(); - e.stopPropagation(); // stops the browser from redirecting. - - var files; - if (e.target && e.target.files) files = e.target.files; - else files = e.dataTransfer.files; - - for (var i = 0, f; f = files[i]; i++) { - // Read the File objects in this FileList. - - reader = new FileReader(); - reader.onload = function(e) { - // we should trigger "load" event here - - image = new Image(); - image.src = e.target.result; - options.initialImage = image; - el.html(image); // may be redundant - - // this is done once per image: - options.output(image); - } - reader.readAsDataURL(f); - - } - } - - function onDragOver(e) { - e.stopPropagation(); - e.preventDefault(); - e.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy. - } - - el.on('dragover', onDragOver, false); - el[0].addEventListener('drop', onImage, false); - $(options.inputSelector).change(onImage); - - } - - // this module is unique because it creates the image - function draw(image) { - el.html(image); - options.initialImage = image; - if (options.output) options.output(image); - } - - return { - options: options, - draw: draw, - setup: setup - } - -} diff --git a/src/modules/Invert.js b/src/modules/Invert/Module.js similarity index 92% rename from src/modules/Invert.js rename to src/modules/Invert/Module.js index f93b5d8f..c2294ca1 100644 --- a/src/modules/Invert.js +++ b/src/modules/Invert/Module.js @@ -18,7 +18,7 @@ module.exports = function GreenChannel(options) { function output(image,datauri,mimetype){ this_.output = {src:datauri,format:mimetype} } - return require('./PixelManipulation.js')(input, { + return require('../_nomodule/PixelManipulation.js')(input, { output: output, changePixel: changePixel, format: input.format, diff --git a/src/modules/NdviRed.js b/src/modules/NdviRed/Module.js similarity index 74% rename from src/modules/NdviRed.js rename to src/modules/NdviRed/Module.js index dd746946..08f23fff 100644 --- a/src/modules/NdviRed.js +++ b/src/modules/NdviRed/Module.js @@ -7,18 +7,17 @@ module.exports = function NdviRed(options) { options.title = "NDVI for red-filtered cameras (blue is infrared)"; var output; - //function setup() {} // optional - function draw(input,callback) { this_ = this; function changePixel(r, g, b, a) { - var ndvi = 255 * (b - r) / (1.00 * b + r); - return [ndvi, ndvi, ndvi, a]; + var ndvi = (b - r) / (1.00 * b + r); + var x = 255 * (ndvi + 1) / 2; + return [x, x, x, a]; } function output(image,datauri,mimetype){ this_.output = {src:datauri,format:mimetype} } - return require('./PixelManipulation.js')(input, { + return require('../_nomodule/PixelManipulation.js')(input, { output: output, changePixel: changePixel, format: input.format, @@ -29,7 +28,6 @@ module.exports = function NdviRed(options) { return { options: options, - //setup: setup, // optional draw: draw } } diff --git a/src/modules/Plot.js b/src/modules/Plot.js deleted file mode 100644 index 9e2af792..00000000 --- a/src/modules/Plot.js +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Plot image on a graph with color bar - */ -module.exports = function Plot(options) { - - options = options || {}; - options.title = "Plot with colorbar"; - options.colorscale = options.colorscale || 'Jet',//'RdBu'; - options.type = options.type || 'contour'; // or 'heatmap' - - var image; - - function draw(_image) { - - /* https://plot.ly/javascript/heatmap-and-contour-colorscales/#custom-colorscale - type: 'contour', - colorscale: [[0, 'rgb(166,206,227)'], - [0.25, 'rgb(31,120,180)'], - [0.45, 'rgb(178,223,138)'], - [0.65, 'rgb(51,160,44)'], - [0.85, 'rgb(251,154,153)'], - [1, 'rgb(227,26,28)']] - */ - - var Plotly = require('plotly.js'), - getPixels = require("get-pixels"); - - var data = [{ - z: [], - colorscale: options.colorscale, - type: options.type - }]; - - getPixels(_image.src, function(err, pixels) { - - if(err) { - console.log("Bad image path") - return - } - - for(var y = pixels.shape[1]; y > 0; y--) { - data[0].z.push([]); - for(var x = 1; x < pixels.shape[0]; x++) { - - data[0].z[data[0].z.length - 1].push(pixels.get(x, y, 0) / 255.00); - - } - } - - var layout = { title: '' }; - random = parseInt(Math.random() * (new Date()).getTime() / 1000000); - - options.ui.el.append('
'); - Plotly.newPlot('plot-' + random, data, layout) -/* .then(function afterPlot(graphData) { - - options.output(Plotly.toImage(graphData, { - format: 'jpeg', - height: _image.height, - width: _image.width - })); - - }); -*/ - }); - - } - - return { - options: options, - draw: draw - } -} diff --git a/src/modules/Resize.js b/src/modules/Resize.js deleted file mode 100644 index f80da85e..00000000 --- a/src/modules/Resize.js +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Resize image - */ -module.exports = function Resize(options) { - options.title = "Resize image"; - options.description = "Resizes image to given width"; - - options = options || {}; - - function setup() { - - } - - function draw(image) { - - - - } - - return { - options: options, - setup: setup, - draw: draw - } -} diff --git a/src/modules/SegmentedColormap/Module.js b/src/modules/SegmentedColormap/Module.js new file mode 100644 index 00000000..da85daa4 --- /dev/null +++ b/src/modules/SegmentedColormap/Module.js @@ -0,0 +1,32 @@ +module.exports = function SegmentedColormap(options) { + + options = options || {}; + options.title = "Segmented Colormap"; + var output; + + function draw(input,callback) { + this_ = this; + function changePixel(r, g, b, a) { + var ndvi = (b - r) / (r + b); + var normalized = (ndvi + 1) / 2; + var res = require('./SegmentedColormap')(normalized,options); + return [res[0], res[1], res[2], 255]; + } + function output(image,datauri,mimetype){ + this_.output = {src:datauri,format:mimetype} + } + return require('../_nomodule/PixelManipulation.js')(input, { + output: output, + changePixel: changePixel, + format: input.format, + image: options.image, + callback: callback + }); + } + + return { + options: options, + draw: draw, + output: output + } +} diff --git a/src/modules/SegmentedColormap.js b/src/modules/SegmentedColormap/SegmentedColormap.js similarity index 68% rename from src/modules/SegmentedColormap.js rename to src/modules/SegmentedColormap/SegmentedColormap.js index 66145410..e0783700 100644 --- a/src/modules/SegmentedColormap.js +++ b/src/modules/SegmentedColormap/SegmentedColormap.js @@ -1,53 +1,16 @@ -module.exports = function SegmentedColormap(options) { +/* + * Accepts a normalized ndvi and returns the new color-mapped pixel + */ - options = options || {}; - options.title = "Segmented Colormap"; +module.exports = function SegmentedColormap(normalized,options) { options.colormap = options.colormap || "default"; if(typeof(options.colormap) == "object") - options.colormap = segmented_colormap(options.colormap); - else options.colormap = colormaps[options.colormap]; - var output; + colormapFunction = segmented_colormap(options.colormap); + else if(colormaps.hasOwnProperty(options.colormap)) + colormapFunction = colormaps[options.colormap]; + else colormapFunction = colormaps.default; - function draw(input,callback) { - this_ = this; - function changePixel(r, g, b, a) { - var ndvi = (b - r) / (r + b); - var normalized = (ndvi + 1) / 2; - var res = options.colormap(normalized); - return [res[0], res[1], res[2], 255]; - } - function output(image,datauri,mimetype){ - this_.output = {src:datauri,format:mimetype} - } - return require('./PixelManipulation.js')(input, { - output: output, - changePixel: changePixel, - format: input.format, - image: options.image, - callback: callback - }); - } - - return { - options: options, - draw: draw, - output: output - } -} - -var greyscale_colormap = segmented_colormap([[0, [0, 0, 0], [255, 255, 255]], [1, [255, 255, 255], [255, 255, 255]]]); - -var default_colormap = segmented_colormap([[0, [0, 0, 255], [38, 195, 195]], [0.5, [0, 150, 0], [255, 255, 0]], [0.75, [255, 255, 0], [255, 50, 50]]]); - -var stretched_colormap = segmented_colormap([[0, [0, 0, 255], [0, 0, 255]], [0.1, [0, 0, 255], [38, 195, 195]], [0.5, [0, 150, 0], [255, 255, 0]], [0.7, [255, 255, 0], [255, 50, 50]], [0.9, [255, 50, 50], [255, 50, 50]]]); - -var fastie_colormap = segmented_colormap([[0, [255, 255, 255], [0, 0, 0]], [0.167, [0, 0, 0], [255, 255, 255]], [0.33, [255, 255, 255], [0, 0, 0]], [0.5, [0, 0, 0], [140, 140, 255]], [0.55, [140, 140, 255], [0, 255, 0]], [0.63, [0, 255, 0], [255, 255, 0]], [0.75, [255, 255, 0], [255, 0, 0]], [0.95, [255, 0, 0], [255, 0, 255]]]); - -var colormaps = { - greyscale: greyscale_colormap, - default: default_colormap, - stretched: stretched_colormap, - fastie: fastie_colormap + return colormapFunction(normalized); } function segmented_colormap(segments) { @@ -77,3 +40,18 @@ function segmented_colormap(segments) { return result; }; }; + +var greyscale_colormap = segmented_colormap([[0, [0, 0, 0], [255, 255, 255]], [1, [255, 255, 255], [255, 255, 255]]]); + +var default_colormap = segmented_colormap([[0, [0, 0, 255], [38, 195, 195]], [0.5, [0, 150, 0], [255, 255, 0]], [0.75, [255, 255, 0], [255, 50, 50]]]); + +var stretched_colormap = segmented_colormap([[0, [0, 0, 255], [0, 0, 255]], [0.1, [0, 0, 255], [38, 195, 195]], [0.5, [0, 150, 0], [255, 255, 0]], [0.7, [255, 255, 0], [255, 50, 50]], [0.9, [255, 50, 50], [255, 50, 50]]]); + +var fastie_colormap = segmented_colormap([[0, [255, 255, 255], [0, 0, 0]], [0.167, [0, 0, 0], [255, 255, 255]], [0.33, [255, 255, 255], [0, 0, 0]], [0.5, [0, 0, 0], [140, 140, 255]], [0.55, [140, 140, 255], [0, 255, 0]], [0.63, [0, 255, 0], [255, 255, 0]], [0.75, [255, 255, 0], [255, 0, 0]], [0.95, [255, 0, 0], [255, 0, 255]]]); + +var colormaps = { + greyscale: greyscale_colormap, + default: default_colormap, + stretched: stretched_colormap, + fastie: fastie_colormap +} diff --git a/src/modules/ImageThreshold.js b/src/modules/Threshold/ImageThreshold.js similarity index 100% rename from src/modules/ImageThreshold.js rename to src/modules/Threshold/ImageThreshold.js diff --git a/src/modules/_Step.js b/src/modules/_Step.js deleted file mode 100644 index 8bbb7827..00000000 --- a/src/modules/_Step.js +++ /dev/null @@ -1,9 +0,0 @@ -module.exports = function(ref,image,pos) { - images = ref.images; - thisimage = images[image]; - var thisstep = thisimage[pos]; - console.log(thisstep); - return function(img) { - thisstep.output = img; - } -} diff --git a/src/modules/PixelManipulation.js b/src/modules/_nomodule/PixelManipulation.js similarity index 100% rename from src/modules/PixelManipulation.js rename to src/modules/_nomodule/PixelManipulation.js