From 829e19e58d123dc733e1986a32a5daf5af068bc3 Mon Sep 17 00:00:00 2001 From: Jeffrey Warren Date: Wed, 4 Oct 2017 14:31:43 -0400 Subject: [PATCH] various fixes and changes to SegmentedColormap module (#128) --- dist/image-sequencer.js | 68 +++++++++++++------ src/modules/SegmentedColormap/Module.js | 7 +- .../SegmentedColormap/SegmentedColormap.js | 64 ++++++++++++----- 3 files changed, 98 insertions(+), 41 deletions(-) diff --git a/dist/image-sequencer.js b/dist/image-sequencer.js index a8ef039d..986e1de6 100644 --- a/dist/image-sequencer.js +++ b/dist/image-sequencer.js @@ -38688,9 +38688,8 @@ module.exports = function SegmentedColormap(options,UI) { var step = this; function changePixel(r, g, b, a) { - var ndvi = (b - r) / (r + b); - var normalized = (ndvi + 1) / 2; - var res = require('./SegmentedColormap')(normalized,options); + var combined = (r + g + b) / 3.000; + var res = require('./SegmentedColormap')(combined,options); return [res[0], res[1], res[2], 255]; } @@ -38726,18 +38725,28 @@ module.exports = function SegmentedColormap(options,UI) { },{"../_nomodule/PixelManipulation.js":146,"./SegmentedColormap":144}],144:[function(require,module,exports){ /* - * Accepts a normalized ndvi and returns the new color-mapped pixel + * Accepts a value from 0-255 and returns the new color-mapped pixel + * from a lookup table, which can be specified as an array of [begin, end] + * gradients, where begin and end are represented as [r, g, b] colors. In + * combination, a lookup table which maps values from 0 - 255 smoothly from black to white looks like: + * [ + * [0, [0, 0, 0], [255, 255, 255]], + * [1, [255, 255, 255], [255, 255, 255]] + * ] + * + * Adapted from bgamari's work in Infragram: https://github.com/p-v-o-s/infragram-js/commit/346c97576a07b71a55671d17e0153b7df74e803b */ -module.exports = function SegmentedColormap(normalized,options) { - options.colormap = options.colormap || "default"; +module.exports = function SegmentedColormap(value, options) { + options.colormap = options.colormap || colormaps.default; + // if a lookup table is provided as an array: if(typeof(options.colormap) == "object") colormapFunction = segmented_colormap(options.colormap); + // if a stored colormap is named with a string like "fastie": else if(colormaps.hasOwnProperty(options.colormap)) colormapFunction = colormaps[options.colormap]; else colormapFunction = colormaps.default; - - return colormapFunction(normalized); + return colormapFunction(value / 255.00); } function segmented_colormap(segments) { @@ -38768,19 +38777,38 @@ function segmented_colormap(segments) { }; }; -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 + greyscale: segmented_colormap([ + [0, [0, 0, 0], [255, 255, 255] ], + [1, [255, 255, 255], [255, 255, 255] ] + ]), + default: segmented_colormap([ + [0, [0, 0, 255], [0, 255, 255] ], + [0.33, [0, 255, 255], [255, 255, 0] ], + [0.66, [255, 255, 0], [255, 0, 0] ] + ]), + ndvi: 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] ] + ]), + stretched: 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] ] + ]), + fastie: 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] ] + ]) } },{}],145:[function(require,module,exports){ diff --git a/src/modules/SegmentedColormap/Module.js b/src/modules/SegmentedColormap/Module.js index c4f6facd..2400dd2d 100644 --- a/src/modules/SegmentedColormap/Module.js +++ b/src/modules/SegmentedColormap/Module.js @@ -15,16 +15,15 @@ module.exports = function SegmentedColormap(options,UI) { var step = this; function changePixel(r, g, b, a) { - var ndvi = (b - r) / (r + b); - var normalized = (ndvi + 1) / 2; - var res = require('./SegmentedColormap')(normalized,options); + var combined = (r + g + b) / 3.000; + var res = require('./SegmentedColormap')(combined, options); return [res[0], res[1], res[2], 255]; } function output(image,datauri,mimetype){ // This output is accessible by Image Sequencer - step.output = {src:datauri,format:mimetype}; + step.output = { src: datauri, format: mimetype }; // This output is accessible by the UI options.step.output = datauri; diff --git a/src/modules/SegmentedColormap/SegmentedColormap.js b/src/modules/SegmentedColormap/SegmentedColormap.js index e0783700..42d491d0 100644 --- a/src/modules/SegmentedColormap/SegmentedColormap.js +++ b/src/modules/SegmentedColormap/SegmentedColormap.js @@ -1,16 +1,26 @@ /* - * Accepts a normalized ndvi and returns the new color-mapped pixel + * Accepts a value from 0-255 and returns the new color-mapped pixel + * from a lookup table, which can be specified as an array of [begin, end] + * gradients, where begin and end are represented as [r, g, b] colors. In + * combination, a lookup table which maps values from 0 - 255 smoothly from black to white looks like: + * [ + * [0, [0, 0, 0], [255, 255, 255]], + * [1, [255, 255, 255], [255, 255, 255]] + * ] + * + * Adapted from bgamari's work in Infragram: https://github.com/p-v-o-s/infragram-js/commit/346c97576a07b71a55671d17e0153b7df74e803b */ -module.exports = function SegmentedColormap(normalized,options) { - options.colormap = options.colormap || "default"; +module.exports = function SegmentedColormap(value, options) { + options.colormap = options.colormap || colormaps.default; + // if a lookup table is provided as an array: if(typeof(options.colormap) == "object") colormapFunction = segmented_colormap(options.colormap); + // if a stored colormap is named with a string like "fastie": else if(colormaps.hasOwnProperty(options.colormap)) colormapFunction = colormaps[options.colormap]; else colormapFunction = colormaps.default; - - return colormapFunction(normalized); + return colormapFunction(value / 255.00); } function segmented_colormap(segments) { @@ -41,17 +51,37 @@ function segmented_colormap(segments) { }; }; -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 + greyscale: segmented_colormap([ + [0, [0, 0, 0], [255, 255, 255] ], + [1, [255, 255, 255], [255, 255, 255] ] + ]), + default: segmented_colormap([ + [0, [0, 0, 255], [0, 255, 0] ], + [0.25, [0, 255, 0], [255, 255, 0] ], + [0.50, [0, 255, 255], [255, 255, 0] ], + [0.75, [255, 255, 0], [255, 0, 0] ] + ]), + ndvi: 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] ] + ]), + stretched: 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] ] + ]), + fastie: 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] ] + ]) }