diff --git a/package-lock.json b/package-lock.json index 9eb14f26..2f9b6327 100644 --- a/package-lock.json +++ b/package-lock.json @@ -482,6 +482,11 @@ "integrity": "sha1-vPEwUspURj8w+fx+lbmkdjCpSSE=", "dev": true }, + "bluebird": { + "version": "3.5.2", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.2.tgz", + "integrity": "sha512-dhHTWMI7kMx5whMQntl7Vr9C6BvV10lFXDAasnqnrMYhXVCzzk6IO9Fo2L75jXHT07WrOngL1WDXOp+yYS91Yg==" + }, "bn.js": { "version": "4.11.8", "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.8.tgz", @@ -4314,6 +4319,24 @@ "resolved": "https://registry.npmjs.org/image-sequencer-invert/-/image-sequencer-invert-1.0.0.tgz", "integrity": "sha512-LL9jwCnSbUy676q9ULJ6M04mp1TVOxpjlckMGN2SyFOgPE4SGL2/IRcO33BDwafmFiupqDdgs7gUHEbsFcCqgQ==" }, + "imagejs": { + "version": "0.0.9", + "resolved": "https://registry.npmjs.org/imagejs/-/imagejs-0.0.9.tgz", + "integrity": "sha1-JoBkWQD211+/D2e7wAY+aIiufr4=", + "requires": { + "bluebird": "*", + "jpeg-js": "0.1.1", + "node-png": "0.4.3", + "underscore": "1.4.4" + }, + "dependencies": { + "jpeg-js": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/jpeg-js/-/jpeg-js-0.1.1.tgz", + "integrity": "sha1-Kl/xljb+J+O2Za2UI0dctraYaY0=" + } + } + }, "imgareaselect": { "version": "git://github.com/jywarren/imgareaselect.git#db8ae869ca0fcb289252678cebd17d6f40711f61", "from": "git://github.com/jywarren/imgareaselect.git#v1.0.0-rc.2" @@ -5496,6 +5519,11 @@ "resolved": "https://registry.npmjs.org/node-bitmap/-/node-bitmap-0.0.1.tgz", "integrity": "sha1-GA6scAPgxwdhjvMTaPYvhLKmkJE=" }, + "node-png": { + "version": "0.4.3", + "resolved": "https://registry.npmjs.org/node-png/-/node-png-0.4.3.tgz", + "integrity": "sha1-RQIjeWuC08yg/+Sl1cf6l0hZdOc=" + }, "nopt": { "version": "3.0.6", "resolved": "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz", @@ -8100,6 +8128,11 @@ "xtend": "^4.0.1" } }, + "underscore": { + "version": "1.4.4", + "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.4.4.tgz", + "integrity": "sha1-YaajIBBiKvoHljvzJSA88SI51gQ=" + }, "underscore.string": { "version": "3.3.5", "resolved": "https://registry.npmjs.org/underscore.string/-/underscore.string-3.3.5.tgz", diff --git a/package.json b/package.json index 805da0e5..f72fecde 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "font-awesome": "~4.5.0", "get-pixels": "~3.3.0", "image-sequencer-invert": "^1.0.0", + "imagejs": "0.0.9", "imgareaselect": "git://github.com/jywarren/imgareaselect.git#v1.0.0-rc.2", "jquery": "^3.3.1", "jsqr": "^0.2.2", diff --git a/src/Modules.js b/src/Modules.js index cd378f34..b08a2c7f 100644 --- a/src/Modules.js +++ b/src/Modules.js @@ -24,4 +24,5 @@ module.exports = { 'histogram': require('./modules/Histogram'), 'gamma-correction': require('./modules/GammaCorrection'), 'convolution': require('./modules/Convolution'), + 'resize': require('./modules/Resize') } \ No newline at end of file diff --git a/src/modules/Resize/Module.js b/src/modules/Resize/Module.js new file mode 100644 index 00000000..8a79d859 --- /dev/null +++ b/src/modules/Resize/Module.js @@ -0,0 +1,71 @@ +/* + * Resize the image by given percentage value + */ +module.exports = function Resize(options, UI) { + + var output; + + function draw(input, callback, progressObj) { + + options.resize = options.resize || "125%"; + + progressObj.stop(true); + progressObj.overrideFlag = true; + + var step = this; + + var imagejs = require('imagejs'); + + function changePixel(r, g, b, a) { + return [r, g, b, a] + } + + function extraManipulation(pixels) { + // value above 100% scales up, and below 100% scales down + var resize_value = parseInt(options.resize.slice(0, -1)); + + var new_width, + new_height; + + new_width = Math.round(pixels.shape[0] * (resize_value / 100)); + new_height = Math.round(pixels.shape[1] * (resize_value / 100)); + + var bitmap = new imagejs.Bitmap({width: pixels.shape[0], height: pixels.shape[1]}); + bitmap._data.data = pixels.data; + + + var resized = bitmap.resize({ + width: new_width, height: new_height, + algorithm: "bicubicInterpolation" + }); + + pixels.data = resized._data.data; + pixels.shape = [new_width,new_height,4]; + pixels.stride[1] = 4 * new_width; + + return pixels; + } + + function output(image, datauri, mimetype) { + // This output is accesible by Image Sequencer + step.output = { src: datauri, format: mimetype }; + } + + return require('../_nomodule/PixelManipulation.js')(input, { + output: output, + changePixel: changePixel, + extraManipulation: extraManipulation, + format: input.format, + image: options.image, + inBrowser: options.inBrowser, + callback: callback + }); + } + + return { + options: options, + draw: draw, + output: output, + UI: UI + } +} diff --git a/src/modules/Resize/index.js b/src/modules/Resize/index.js new file mode 100644 index 00000000..11ef888f --- /dev/null +++ b/src/modules/Resize/index.js @@ -0,0 +1,4 @@ +module.exports = [ + require('./Module'), + require('./info.json') +] \ No newline at end of file diff --git a/src/modules/Resize/info.json b/src/modules/Resize/info.json new file mode 100644 index 00000000..5e3ffc53 --- /dev/null +++ b/src/modules/Resize/info.json @@ -0,0 +1,11 @@ +{ + "name": "Resize", + "description": "Resize image by given percentage value", + "inputs": { + "resize": { + "type": "string", + "desc": "Percentage value of the resize", + "default": "125%" + } + } +} \ No newline at end of file diff --git a/test/modules/image-sequencer.js b/test/modules/image-sequencer.js index de1843cb..7e2d7e00 100644 --- a/test/modules/image-sequencer.js +++ b/test/modules/image-sequencer.js @@ -189,16 +189,15 @@ test('toCliString() returns the CLI command for the sequence', function(t) { }); test('blend returns different output depending on the set offset', function(t) { - var blend_2; - sequencer.addSteps('test', 'invert', {}); - sequencer.addSteps('test', 'invert', {}); - sequencer.addSteps('test', 'blend', {}); - // because we've added blend before, so instead of -3 we set it to -4 - sequencer.addSteps('test', 'blend', {'offset': -4}); - sequencer.run({ mode: 'test' }, function(out) { - t.notStrictEqual(out, sequencer.images.test.steps[sequencer.images.test.steps.length - 2].output.src, 'different offsets give different output'); - t.end(); - }); + sequencer.addSteps('test', 'invert', {}); + sequencer.addSteps('test', 'invert', {}); + sequencer.addSteps('test', 'blend', {}); + // because we've added blend before, so instead of -3 we set it to -4 + sequencer.addSteps('test', 'blend', {'offset': -4}); + sequencer.run({ mode: 'test' }, function(out) { + t.notStrictEqual(out, sequencer.images.test.steps[sequencer.images.test.steps.length - 2].output.src, 'different offsets give different output'); + t.end(); + }); }); test('replaceImage returns false in NodeJS', function(t) {