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

Added aspect ratio module #1454

Merged
merged 17 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/Modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
'colorbar': require('./modules/Colorbar'),
'color-temperature': require('./modules/ColorTemperature'),
'colormap': require('./modules/Colormap'),
'constrained-crop': require('./modules/ConstrainedCrop'),
'contrast': require('./modules/Contrast'),
'convolution': require('./modules/Convolution'),
'crop': require('./modules/Crop'),
Expand Down
56 changes: 56 additions & 0 deletions src/modules/ConstrainedCrop/Module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Crops an Image on the basis of the ratio provided
*/
module.exports = function ConstrainedCrop(options, UI) {

var defaults = require('./../../util/getDefaults.js')(require('./info.json'));
var output;

function draw(input, callback) {

var step = this,
startingX = Number(options.startingX || defaults.startingX),
startingY = Number(options.startingY || defaults.startingY),
aspectRatio = (options.aspectRatio || defaults.aspectRatio).split(':'),
widthRatio = Number(aspectRatio[0]),
heightRatio = Number(aspectRatio[1]);

function extraManipulation(pixels) {
var width = pixels.shape[0],
height = pixels.shape[1];
var endX, endY;
if(((width - startingX) / widthRatio) * heightRatio <= (height - startingY)) {
endX = width;
endY = (((width - startingX) / widthRatio) * heightRatio) + startingY;
}
else {
endX = (((height - startingY) / heightRatio) * widthRatio) + startingX;
endY = height;
}
const newPixels = require('../Crop/Crop')(pixels, {'x': startingX, 'y': startingY, 'w': endX - startingX, 'h': endY - startingY}, function() {
});
return newPixels;
}


function output(image, datauri, mimetype, wasmSuccess) {
step.output = { src: datauri, format: mimetype, wasmSuccess, useWasm: options.useWasm };
}
return require('../_nomodule/PixelManipulation')(input, {
output: output,
ui: options.step.ui,
extraManipulation: extraManipulation,
format: input.format,
image: options.image,
inBrowser: options.inBrowser,
callback: callback,
useWasm:options.useWasm
});
}
return {
options: options,
draw: draw,
output: output,
UI: UI
};
};
4 changes: 4 additions & 0 deletions src/modules/ConstrainedCrop/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = [
require('./Module'),
require('./info.json')
];
23 changes: 23 additions & 0 deletions src/modules/ConstrainedCrop/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "constrained-crop",
"description": "Crops an image in a particular aspect-ratio",
"inputs": {
"startingX": {
"type": "integer",
"desc": "X-position (measured from left) from where cropping starts",
"default": 0
},
"startingY": {
"type": "integer",
"desc": "Y-position (measured from top) from where cropping starts",
"default": 0
},
"aspectRatio":{
"type": "string",
"desc": "Enter aspect ratio in following format width:height",
"default": "1:1"
}
},
"docs-link":""
}

Loading