rotate module (#1218)

* this PR solves the problem of clipping of the image while rotating

* a little clipping was still present .so made the changes

* Splitted into two files

* ommitted extra declared variables

* Update Module.js

* Update Rotate.js

* Update Rotate.js
This commit is contained in:
keshav234156
2019-09-25 02:15:02 +05:30
committed by Jeffrey Warren
parent 33f32251ec
commit 3eb741c6d9
2 changed files with 47 additions and 12 deletions

View File

@@ -23,21 +23,18 @@ module.exports = function Rotate(options, UI) {
function extraManipulation(pixels) { function extraManipulation(pixels) {
var rotate_value = (options.rotate) % 360; var rotate_value = (options.rotate) % 360;
var radians = (Math.PI) * rotate_value / 180;
if (rotate_value % 360 == 0) var width = pixels.shape[0];
return pixels; var height = pixels.shape[1];
var cos = Math.cos(radians);
var bitmap = new imagejs.Bitmap({ width: pixels.shape[0], height: pixels.shape[1] }); var sin = Math.sin(radians);
bitmap._data.data = pixels.data; //final dimensions after rotation
var pixels2 = require('ndarray')(new Uint8Array(4 * (Math.floor(Math.abs(width * cos) + Math.abs(height * sin) + 5) * (Math.floor(Math.abs(width * sin) + Math.abs(height * cos)) + 5))).fill(0), [Math.floor(Math.abs(width * cos) + Math.abs(height * sin)) + 5, Math.floor(Math.abs(width * sin) + Math.abs(height * cos)) + 4, 4]);
var rotated = bitmap.rotate({ pixels = require('./Rotate')(pixels, pixels2, options, rotate_value, width, height, cos, sin);
degrees: rotate_value,
});
pixels.data = rotated._data.data;
return pixels; return pixels;
} }
function output(image, datauri, mimetype) { function output(image, datauri, mimetype) {
// This output is accesible by Image Sequencer // This output is accesible by Image Sequencer
step.output = { src: datauri, format: mimetype }; step.output = { src: datauri, format: mimetype };

View File

@@ -0,0 +1,38 @@
module.exports = function Rotate(pixels, pixels2, options, rotate_value, width, height, cos, sin){
var imagejs = require('imagejs');
var height_half = Math.floor(height / 2);
var width_half = Math.floor(width / 2);
var dimension = width + height;
if (rotate_value % 360 == 0)
return pixels;
function copyPixel(x1, y1, x2, y2,pixel_set,pixel_get){
pixel_set.set(x1, y1, 0, pixel_get.get(x2, y2, 0));
pixel_set.set(x1, y1, 1, pixel_get.get(x2, y2, 1));
pixel_set.set(x1, y1, 2, pixel_get.get(x2, y2, 2));
pixel_set.set(x1, y1, 3, pixel_get.get(x2, y2, 3));
}
pixels1 = require('ndarray')(new Uint8Array(4 * dimension * dimension).fill(0), [dimension, dimension, 4]);
//copying all the pixels from image to pixels1
for (var n = 0; n < pixels.shape[0]; n++){
for (var m = 0; m < pixels.shape[1]; m++){
copyPixel(n + height_half, m + width_half, n, m,pixels1,pixels);
}
}
//rotating pixels1
var bitmap = new imagejs.Bitmap({ width: pixels1.shape[0], height: pixels1.shape[1] });
bitmap._data.data = pixels1.data;
var rotated = bitmap.rotate({
degrees: rotate_value,
});
pixels1.data = rotated._data.data;
//cropping extra whitespace
for (var n = 0; n < pixels2.shape[0]; n++){
for (var m = 0; m < pixels2.shape[1]; m++){
copyPixel(n, m, n + Math.floor(dimension / 2 - Math.abs(width * cos / 2) - Math.abs(height * sin / 2)) - 1, m + Math.floor(dimension / 2 - Math.abs(height * cos / 2) - Math.abs(width * sin / 2)) - 1,pixels2,pixels1);
}
}
return pixels2;
};