Skip to content

Commit

Permalink
Added aspect ratio module (#1454)
Browse files Browse the repository at this point in the history
* Base file

* Added aspect ratio module

* Compatiable with Experimental GIF Manipulation

* some refactoring

* Changed the name from aspect-ratio to Constrained Crop

* cleanup

* Changes requested

* Added test module

Co-authored-by: Harsh Khandeparkar <34770591+HarshKhandeparkar@users.noreply.github.com>
Co-authored-by: Jeffrey Warren <jeff@unterbahn.com>
  • Loading branch information
3 people committed Jan 16, 2020
1 parent 5ae5459 commit 2736b48
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
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

0 comments on commit 2736b48

Please sign in to comment.