Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resize module #451

Merged
merged 8 commits into from
Nov 3, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -31,6 +31,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')
}
79 changes: 79 additions & 0 deletions src/modules/Resize/Module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Resize the image by given percentage value
*/
module.exports = function Resize(options, UI) {

var output;

function draw(input, callback, progressObj) {

options.resize = options.resize || "25%";

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) {
var resize_value = parseInt(options.resize.slice(0, -1));

var new_width,
new_height;

// scaling up
if(resize_value >= 0) {
new_width = Math.round(pixels.shape[0] * (1 + resize_value / 100));
new_height = Math.round(pixels.shape[1] * (1 + resize_value / 100));
}

// scaling down
if(resize_value < 0) {
new_width = Math.round(pixels.shape[0] / (1 - resize_value / 100));
new_height = Math.round(pixels.shape[1] / (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": "25%"
}
}
}
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