Skip to content

Commit

Permalink
Add resize module (#451)
Browse files Browse the repository at this point in the history
* added resize module - without test

* Added resize test, deleted unused value in blend test (which I've added before)

* Fixed test

* Fixed test

* added modules file

* added imagejs to package

* removed test - not able to check width and height of the element

* Changed resize calculations
  • Loading branch information
KusioDev authored and jywarren committed Nov 3, 2018
1 parent d2ff6f2 commit 5f57123
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 10 deletions.
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/Modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ module.exports = {
'histogram': require('./modules/Histogram'),
'gamma-correction': require('./modules/GammaCorrection'),
'convolution': require('./modules/Convolution'),
'resize': require('./modules/Resize')
}
71 changes: 71 additions & 0 deletions src/modules/Resize/Module.js
Original file line number Diff line number Diff line change
@@ -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
}
}
4 changes: 4 additions & 0 deletions src/modules/Resize/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = [
require('./Module'),
require('./info.json')
]
11 changes: 11 additions & 0 deletions src/modules/Resize/info.json
Original file line number Diff line number Diff line change
@@ -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%"
}
}
}
19 changes: 9 additions & 10 deletions test/modules/image-sequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5f57123

Please sign in to comment.