@eris/image
Advanced tools
Comparing version
@@ -55,3 +55,3 @@ import { Colorspace, BufferLike, IPixel, ColorChannel, IFormatOptions, ICalibrationProfile, IPixelCoordinate, IGreyscalePixel, MapPixelFn } from './types'; | ||
static getPixelsForAngle(imageData: IAnnotatedImageData, srcX: number, srcY: number, angle: number, radius?: number): IGreyscalePixel[]; | ||
static rotateArray(srcArray: number[] | Uint8Array, dstArray: number[] | Uint8Array, width: number, height: number, angle: number, channels?: number): void; | ||
static rotateSquareArray(srcArray: number[] | Uint8Array, dstArray: number[] | Uint8Array, width: number, height: number, angle: number, channels?: number): void; | ||
/** | ||
@@ -58,0 +58,0 @@ * Rotates the image data counter-clockwise by the specified angle. |
@@ -201,3 +201,5 @@ "use strict"; | ||
} | ||
static rotateArray(srcArray, dstArray, width, height, angle, channels = 1) { | ||
static rotateSquareArray(srcArray, dstArray, width, height, angle, channels = 1) { | ||
if (width !== height) | ||
throw new Error('Only works on squares'); | ||
// tslint:disable-next-line | ||
@@ -217,2 +219,3 @@ const fakeImageData = { width, height, channels }; | ||
const yPrime = Math.round(yPrimeRelative + originY); | ||
// Check if new coordinates are out of bounds | ||
if (ImageData.isBorder(fakeImageData, xPrime, yPrime, 0)) { | ||
@@ -238,3 +241,35 @@ continue; | ||
const dstData = new Uint8Array(numPixels * srcImageData.channels); | ||
ImageData.rotateArray(srcImageData.data, dstData, srcImageData.width, srcImageData.height, angle, srcImageData.channels); | ||
if (srcImageData.width === srcImageData.height) { | ||
ImageData.rotateSquareArray(srcImageData.data, dstData, srcImageData.width, srcImageData.height, angle, srcImageData.channels); | ||
} | ||
else { | ||
if (angle % 90 !== 0) | ||
throw new Error('Can only rotate by 90 degree increments'); | ||
if (angle === 90 || angle === 270) { | ||
dstImageData.width = srcImageData.height; | ||
dstImageData.height = srcImageData.width; | ||
} | ||
for (let srcX = 0; srcX < srcImageData.width; srcX++) { | ||
for (let srcY = 0; srcY < srcImageData.height; srcY++) { | ||
let dstX, dstY; | ||
if (angle === 90) { | ||
dstX = srcY; | ||
dstY = srcImageData.width - srcX - 1; | ||
} | ||
else if (angle === 180) { | ||
dstX = srcImageData.width - srcX - 1; | ||
dstY = srcImageData.height - srcY - 1; | ||
} | ||
else if (angle === 270) { | ||
dstX = srcImageData.height - srcY - 1; | ||
dstY = srcX; | ||
} | ||
const srcIndex = ImageData.indexFor(srcImageData, srcX, srcY); | ||
const dstIndex = ImageData.indexFor(dstImageData, dstX, dstY); | ||
for (let c = 0; c < srcImageData.channels; c++) { | ||
dstData[dstIndex + c] = srcImageData.data[srcIndex + c]; | ||
} | ||
} | ||
} | ||
} | ||
dstImageData.data = dstData; | ||
@@ -241,0 +276,0 @@ return dstImageData; |
@@ -35,3 +35,3 @@ "use strict"; | ||
const rotatedMatrix = new Array(matrix.length); | ||
image_data_1.ImageData.rotateArray(matrix, rotatedMatrix, matrixWidth, matrixWidth, 90); | ||
image_data_1.ImageData.rotateSquareArray(matrix, rotatedMatrix, matrixWidth, matrixWidth, 90); | ||
matrix = rotatedMatrix; | ||
@@ -38,0 +38,0 @@ } |
@@ -306,3 +306,3 @@ import { | ||
public static rotateArray( | ||
public static rotateSquareArray( | ||
srcArray: number[] | Uint8Array, | ||
@@ -315,2 +315,4 @@ dstArray: number[] | Uint8Array, | ||
): void { | ||
if (width !== height) throw new Error('Only works on squares') | ||
// tslint:disable-next-line | ||
@@ -332,2 +334,3 @@ const fakeImageData = {width, height, channels} as IAnnotatedImageData | ||
const yPrime = Math.round(yPrimeRelative + originY) | ||
// Check if new coordinates are out of bounds | ||
if (ImageData.isBorder(fakeImageData, xPrime, yPrime, 0)) { | ||
@@ -356,11 +359,43 @@ continue | ||
ImageData.rotateArray( | ||
srcImageData.data, | ||
dstData, | ||
srcImageData.width, | ||
srcImageData.height, | ||
angle, | ||
srcImageData.channels, | ||
) | ||
if (srcImageData.width === srcImageData.height) { | ||
ImageData.rotateSquareArray( | ||
srcImageData.data, | ||
dstData, | ||
srcImageData.width, | ||
srcImageData.height, | ||
angle, | ||
srcImageData.channels, | ||
) | ||
} else { | ||
if (angle % 90 !== 0) throw new Error('Can only rotate by 90 degree increments') | ||
if (angle === 90 || angle === 270) { | ||
dstImageData.width = srcImageData.height | ||
dstImageData.height = srcImageData.width | ||
} | ||
for (let srcX = 0; srcX < srcImageData.width; srcX++) { | ||
for (let srcY = 0; srcY < srcImageData.height; srcY++) { | ||
let dstX: number, dstY: number | ||
if (angle === 90) { | ||
dstX = srcY | ||
dstY = srcImageData.width - srcX - 1 | ||
} else if (angle === 180) { | ||
dstX = srcImageData.width - srcX - 1 | ||
dstY = srcImageData.height - srcY - 1 | ||
} else if (angle === 270) { | ||
dstX = srcImageData.height - srcY - 1 | ||
dstY = srcX | ||
} | ||
const srcIndex = ImageData.indexFor(srcImageData, srcX, srcY) | ||
const dstIndex = ImageData.indexFor(dstImageData, dstX!, dstY!) | ||
for (let c = 0; c < srcImageData.channels; c++) { | ||
dstData[dstIndex + c] = srcImageData.data[srcIndex + c] | ||
} | ||
} | ||
} | ||
} | ||
dstImageData.data = dstData | ||
@@ -367,0 +402,0 @@ return dstImageData |
@@ -40,3 +40,3 @@ /* tslint:disable */ | ||
const rotatedMatrix = new Array(matrix.length) | ||
ImageData.rotateArray(matrix, rotatedMatrix, matrixWidth, matrixWidth, 90) | ||
ImageData.rotateSquareArray(matrix, rotatedMatrix, matrixWidth, matrixWidth, 90) | ||
matrix = rotatedMatrix | ||
@@ -43,0 +43,0 @@ } |
{ | ||
"name": "@eris/image", | ||
"version": "0.1.1-alpha.12", | ||
"version": "0.1.1-alpha.13", | ||
"description": "Collection of image manipulation libraries for node and the browser.", | ||
@@ -38,3 +38,3 @@ "main": "./dist/node-index.js", | ||
"dependencies": { | ||
"@eris/exif": "0.1.1-alpha.12", | ||
"@eris/exif": "0.1.1-alpha.13", | ||
"buffer": "^5.2.0", | ||
@@ -71,3 +71,3 @@ "file-type": "^7.0.1", | ||
}, | ||
"gitHead": "8cc9a1c79d111b6cb679c9c71c9b76e30573e30f" | ||
"gitHead": "cff4b73263e5709c1ccd742433f4636e88ea85c3" | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1612394
0.7%13717
0.74%+ Added
- Removed
Updated