mirror of
https://github.com/publiclab/image-sequencer.git
synced 2025-12-12 03:10:03 +01:00
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:
committed by
Jeffrey Warren
parent
33f32251ec
commit
3eb741c6d9
@@ -23,21 +23,18 @@ module.exports = function Rotate(options, UI) {
|
||||
|
||||
function extraManipulation(pixels) {
|
||||
var rotate_value = (options.rotate) % 360;
|
||||
|
||||
if (rotate_value % 360 == 0)
|
||||
return pixels;
|
||||
|
||||
var bitmap = new imagejs.Bitmap({ width: pixels.shape[0], height: pixels.shape[1] });
|
||||
bitmap._data.data = pixels.data;
|
||||
|
||||
var rotated = bitmap.rotate({
|
||||
degrees: rotate_value,
|
||||
});
|
||||
pixels.data = rotated._data.data;
|
||||
|
||||
var radians = (Math.PI) * rotate_value / 180;
|
||||
var width = pixels.shape[0];
|
||||
var height = pixels.shape[1];
|
||||
var cos = Math.cos(radians);
|
||||
var sin = Math.sin(radians);
|
||||
//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]);
|
||||
pixels = require('./Rotate')(pixels, pixels2, options, rotate_value, width, height, cos, sin);
|
||||
return pixels;
|
||||
}
|
||||
|
||||
|
||||
function output(image, datauri, mimetype) {
|
||||
// This output is accesible by Image Sequencer
|
||||
step.output = { src: datauri, format: mimetype };
|
||||
|
||||
38
src/modules/Rotate/Rotate.js
Normal file
38
src/modules/Rotate/Rotate.js
Normal 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;
|
||||
};
|
||||
Reference in New Issue
Block a user