Initial commit

This commit is contained in:
robert committed 2025-05-30 18:13:30 +08:00
commit e49726988c
5445 files changed
+1089914

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
import { ResizeStrategy } from "@jimp/plugin-resize";
import { JimpClass } from "@jimp/types";
import { z } from "zod";
declare const RotateOptionsSchema: z.ZodUnion<[z.ZodNumber, z.ZodObject<{
/** the number of degrees to rotate the image by */
deg: z.ZodNumber;
/** resize mode or a boolean, if false then the width and height of the image will not be changed */
mode: z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodNativeEnum<typeof ResizeStrategy>]>>;
}, "strip", z.ZodTypeAny, {
deg: number;
mode?: boolean | ResizeStrategy | undefined;
}, {
deg: number;
mode?: boolean | ResizeStrategy | undefined;
}>]>;
export type RotateOptions = z.infer<typeof RotateOptionsSchema>;
export declare const methods: {
/**
* Rotates the image counter-clockwise by a number of degrees. By default the width and height of the image will be resized appropriately.
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.rotate(90);
* ```
*/
rotate<I extends JimpClass>(image: I, options: RotateOptions): I;
};
export {};
//# sourceMappingURL=index.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAA4B,MAAM,qBAAqB,CAAC;AAC/E,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAIxC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,QAAA,MAAM,mBAAmB;IAGrB,mDAAmD;;IAEnD,oGAAoG;;;;;;;;IAGtG,CAAC;AAEH,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAuMhE,eAAO,MAAM,OAAO;IAClB;;;;;;;;;;OAUG;WACI,CAAC,SAAS,SAAS,SAAS,CAAC,WAAW,aAAa;CA2B7D,CAAC"}
+201
View File
@@ -0,0 +1,201 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.methods = void 0;
const plugin_resize_1 = require("@jimp/plugin-resize");
const utils_1 = require("@jimp/utils");
const core_1 = require("@jimp/core");
const plugin_crop_1 = require("@jimp/plugin-crop");
const zod_1 = require("zod");
const RotateOptionsSchema = zod_1.z.union([
zod_1.z.number(),
zod_1.z.object({
/** the number of degrees to rotate the image by */
deg: zod_1.z.number(),
/** resize mode or a boolean, if false then the width and height of the image will not be changed */
mode: zod_1.z.union([zod_1.z.boolean(), zod_1.z.nativeEnum(plugin_resize_1.ResizeStrategy)]).optional(),
}),
]);
/** function to translate the x, y coordinate to the index of the pixel in the buffer */
function createIdxTranslationFunction(w) {
return function (x, y) {
return (y * w + x) << 2;
};
}
/**
* Rotates an image counter-clockwise by multiple of 90 degrees. NB: 'this' must be a Jimp object.
*
* This function is based on matrix rotation. Check this to get an initial idea how it works: https://stackoverflow.com/a/8664879/10561909
*
* @param deg the number of degrees to rotate the image by, it should be a multiple of 90
*/
function matrixRotate(image, deg) {
if (Math.abs(deg) % 90 !== 0) {
throw new Error("Unsupported matrix rotation degree");
}
const w = image.bitmap.width;
const h = image.bitmap.height;
// decide which rotation angle to use
let angle;
switch (deg) {
// 90 degree & -270 degree are same
case 90:
case -270:
angle = 90;
break;
case 180:
case -180:
angle = 180;
break;
case 270:
case -90:
angle = -90;
break;
default:
throw new Error("Unsupported matrix rotation degree");
}
// After this switch block, angle will be 90, 180 or -90
// calculate the new width and height
const nW = angle === 180 ? w : h;
const nH = angle === 180 ? h : w;
const dstBuffer = Buffer.alloc(image.bitmap.data.length);
const srcIdxFunction = createIdxTranslationFunction(w);
const dstIdxFunction = createIdxTranslationFunction(nW);
for (let x = 0; x < w; x++) {
for (let y = 0; y < h; y++) {
const srcIdx = srcIdxFunction(x, y);
const pixelRGBA = image.bitmap.data.readUInt32BE(srcIdx);
let dstIdx;
switch (angle) {
case 90:
dstIdx = dstIdxFunction(y, w - x - 1);
break;
case -90:
dstIdx = dstIdxFunction(h - y - 1, x);
break;
case 180:
dstIdx = dstIdxFunction(w - x - 1, h - y - 1);
break;
default:
throw new Error("Unsupported matrix rotation angle");
}
dstBuffer.writeUInt32BE(pixelRGBA, dstIdx);
}
}
image.bitmap.data = dstBuffer;
image.bitmap.width = nW;
image.bitmap.height = nH;
}
function createTranslationFunction(deltaX, deltaY) {
return function (x, y) {
return {
x: x + deltaX,
y: y + deltaY,
};
};
}
/**
* Rotates an image counter-clockwise by an arbitrary number of degrees. NB: 'this' must be a Jimp object.
* @param {number} deg the number of degrees to rotate the image by
*/
function advancedRotate(image, deg, mode) {
const rad = (deg * Math.PI) / 180;
const cosine = Math.cos(rad);
const sine = Math.sin(rad);
// the final width and height will change if resize == true
let w = image.bitmap.width;
let h = image.bitmap.height;
if (mode === true || typeof mode === "string") {
// resize the image to it maximum dimension and blit the existing image
// onto the center so that when it is rotated the image is kept in bounds
// http://stackoverflow.com/questions/3231176/how-to-get-size-of-a-rotated-rectangle
// Plus 1 border pixel to ensure to show all rotated result for some cases.
w =
Math.ceil(Math.abs(image.bitmap.width * cosine) +
Math.abs(image.bitmap.height * sine)) + 1;
h =
Math.ceil(Math.abs(image.bitmap.width * sine) +
Math.abs(image.bitmap.height * cosine)) + 1;
// Ensure destination to have even size to a better result.
if (w % 2 !== 0) {
w++;
}
if (h % 2 !== 0) {
h++;
}
const c = (0, utils_1.clone)(image);
image.scan((_, __, idx) => {
image.bitmap.data.writeUInt32BE(image.background, idx);
});
const max = Math.max(w, h, image.bitmap.width, image.bitmap.height);
image = plugin_resize_1.methods.resize(image, {
h: max,
w: max,
mode: mode === true ? undefined : mode,
});
image = (0, core_1.composite)(image, c, image.bitmap.width / 2 - c.bitmap.width / 2, image.bitmap.height / 2 - c.bitmap.height / 2);
}
const bW = image.bitmap.width;
const bH = image.bitmap.height;
const dstBuffer = Buffer.alloc(image.bitmap.data.length);
const translate2Cartesian = createTranslationFunction(-(bW / 2), -(bH / 2));
const translate2Screen = createTranslationFunction(bW / 2 + 0.5, bH / 2 + 0.5);
for (let y = 1; y <= bH; y++) {
for (let x = 1; x <= bW; x++) {
const cartesian = translate2Cartesian(x, y);
const source = translate2Screen(cosine * cartesian.x - sine * cartesian.y, cosine * cartesian.y + sine * cartesian.x);
const dstIdx = (bW * (y - 1) + x - 1) << 2;
if (source.x >= 0 && source.x < bW && source.y >= 0 && source.y < bH) {
const srcIdx = ((bW * (source.y | 0) + source.x) | 0) << 2;
const pixelRGBA = image.bitmap.data.readUInt32BE(srcIdx);
dstBuffer.writeUInt32BE(pixelRGBA, dstIdx);
}
else {
// reset off-image pixels
dstBuffer.writeUInt32BE(image.background, dstIdx);
}
}
}
image.bitmap.data = dstBuffer;
if (mode === true || typeof mode === "string") {
// now crop the image to the final size
const x = Math.max(bW / 2 - w / 2, 0);
const y = Math.max(bH / 2 - h / 2, 0);
image = plugin_crop_1.methods.crop(image, { x, y, w, h });
}
}
exports.methods = {
/**
* Rotates the image counter-clockwise by a number of degrees. By default the width and height of the image will be resized appropriately.
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.rotate(90);
* ```
*/
rotate(image, options) {
const parsed = RotateOptionsSchema.parse(options);
const actualOptions = typeof parsed === "number" ? { deg: parsed } : parsed;
const { mode = true } = actualOptions;
let { deg } = actualOptions;
// No need to do extra rotation
deg %= 360;
// no rotation for 0, 360, -360, 720, -720, ...
if (deg % 360 === 0) {
return image;
}
// use matrixRotate if the angle is a multiple of 90 degrees (eg: 180 or -90) and resize is allowed or not needed.
const matrixRotateAllowed = deg % 90 === 0 &&
(mode || image.bitmap.width === image.bitmap.height || deg % 180 === 0);
if (matrixRotateAllowed) {
matrixRotate(image, deg);
}
else {
advancedRotate(image, deg, mode);
}
return image;
},
};
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,uDAA+E;AAE/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AC,EACD,KAAK,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,EAC3C,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAC9C,CAAC;IACJ,CAAC;IAED,MAAM,EAAE,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC;IAC9B,MAAM,EAAE,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;IAC/B,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAEzD,MAAM,mBAAmB,GAAG,yBAAyB,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;IAC5E,MAAM,gBAAgB,GAAG,yBAAyB,CAChD,EAAE,GAAG,CAAC,GAAG,GAAG,EACZ,EAAE,GAAG,CAAC,GAAG,GAAG,CACb,CAAC;IAEF,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC;QAC7B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CALine truncated
+3
View File
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
+32
View File
@@ -0,0 +1,32 @@
import { ResizeStrategy } from "@jimp/plugin-resize";
import { JimpClass } from "@jimp/types";
import { z } from "zod";
declare const RotateOptionsSchema: z.ZodUnion<[z.ZodNumber, z.ZodObject<{
/** the number of degrees to rotate the image by */
deg: z.ZodNumber;
/** resize mode or a boolean, if false then the width and height of the image will not be changed */
mode: z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodNativeEnum<typeof ResizeStrategy>]>>;
}, "strip", z.ZodTypeAny, {
deg: number;
mode?: boolean | ResizeStrategy | undefined;
}, {
deg: number;
mode?: boolean | ResizeStrategy | undefined;
}>]>;
export type RotateOptions = z.infer<typeof RotateOptionsSchema>;
export declare const methods: {
/**
* Rotates the image counter-clockwise by a number of degrees. By default the width and height of the image will be resized appropriately.
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.rotate(90);
* ```
*/
rotate<I extends JimpClass>(image: I, options: RotateOptions): I;
};
export {};
//# sourceMappingURL=index.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAA4B,MAAM,qBAAqB,CAAC;AAC/E,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAIxC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,QAAA,MAAM,mBAAmB;IAGrB,mDAAmD;;IAEnD,oGAAoG;;;;;;;;IAGtG,CAAC;AAEH,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAuMhE,eAAO,MAAM,OAAO;IAClB;;;;;;;;;;OAUG;WACI,CAAC,SAAS,SAAS,SAAS,CAAC,WAAW,aAAa;CA2B7D,CAAC"}
+198
View File
@@ -0,0 +1,198 @@
import { ResizeStrategy, methods as resizeMethods } from "@jimp/plugin-resize";
import { clone } from "@jimp/utils";
import { composite } from "@jimp/core";
import { methods as cropMethods } from "@jimp/plugin-crop";
import { z } from "zod";
const RotateOptionsSchema = z.union([
z.number(),
z.object({
/** the number of degrees to rotate the image by */
deg: z.number(),
/** resize mode or a boolean, if false then the width and height of the image will not be changed */
mode: z.union([z.boolean(), z.nativeEnum(ResizeStrategy)]).optional(),
}),
]);
/** function to translate the x, y coordinate to the index of the pixel in the buffer */
function createIdxTranslationFunction(w) {
return function (x, y) {
return (y * w + x) << 2;
};
}
/**
* Rotates an image counter-clockwise by multiple of 90 degrees. NB: 'this' must be a Jimp object.
*
* This function is based on matrix rotation. Check this to get an initial idea how it works: https://stackoverflow.com/a/8664879/10561909
*
* @param deg the number of degrees to rotate the image by, it should be a multiple of 90
*/
function matrixRotate(image, deg) {
if (Math.abs(deg) % 90 !== 0) {
throw new Error("Unsupported matrix rotation degree");
}
const w = image.bitmap.width;
const h = image.bitmap.height;
// decide which rotation angle to use
let angle;
switch (deg) {
// 90 degree & -270 degree are same
case 90:
case -270:
angle = 90;
break;
case 180:
case -180:
angle = 180;
break;
case 270:
case -90:
angle = -90;
break;
default:
throw new Error("Unsupported matrix rotation degree");
}
// After this switch block, angle will be 90, 180 or -90
// calculate the new width and height
const nW = angle === 180 ? w : h;
const nH = angle === 180 ? h : w;
const dstBuffer = Buffer.alloc(image.bitmap.data.length);
const srcIdxFunction = createIdxTranslationFunction(w);
const dstIdxFunction = createIdxTranslationFunction(nW);
for (let x = 0; x < w; x++) {
for (let y = 0; y < h; y++) {
const srcIdx = srcIdxFunction(x, y);
const pixelRGBA = image.bitmap.data.readUInt32BE(srcIdx);
let dstIdx;
switch (angle) {
case 90:
dstIdx = dstIdxFunction(y, w - x - 1);
break;
case -90:
dstIdx = dstIdxFunction(h - y - 1, x);
break;
case 180:
dstIdx = dstIdxFunction(w - x - 1, h - y - 1);
break;
default:
throw new Error("Unsupported matrix rotation angle");
}
dstBuffer.writeUInt32BE(pixelRGBA, dstIdx);
}
}
image.bitmap.data = dstBuffer;
image.bitmap.width = nW;
image.bitmap.height = nH;
}
function createTranslationFunction(deltaX, deltaY) {
return function (x, y) {
return {
x: x + deltaX,
y: y + deltaY,
};
};
}
/**
* Rotates an image counter-clockwise by an arbitrary number of degrees. NB: 'this' must be a Jimp object.
* @param {number} deg the number of degrees to rotate the image by
*/
function advancedRotate(image, deg, mode) {
const rad = (deg * Math.PI) / 180;
const cosine = Math.cos(rad);
const sine = Math.sin(rad);
// the final width and height will change if resize == true
let w = image.bitmap.width;
let h = image.bitmap.height;
if (mode === true || typeof mode === "string") {
// resize the image to it maximum dimension and blit the existing image
// onto the center so that when it is rotated the image is kept in bounds
// http://stackoverflow.com/questions/3231176/how-to-get-size-of-a-rotated-rectangle
// Plus 1 border pixel to ensure to show all rotated result for some cases.
w =
Math.ceil(Math.abs(image.bitmap.width * cosine) +
Math.abs(image.bitmap.height * sine)) + 1;
h =
Math.ceil(Math.abs(image.bitmap.width * sine) +
Math.abs(image.bitmap.height * cosine)) + 1;
// Ensure destination to have even size to a better result.
if (w % 2 !== 0) {
w++;
}
if (h % 2 !== 0) {
h++;
}
const c = clone(image);
image.scan((_, __, idx) => {
image.bitmap.data.writeUInt32BE(image.background, idx);
});
const max = Math.max(w, h, image.bitmap.width, image.bitmap.height);
image = resizeMethods.resize(image, {
h: max,
w: max,
mode: mode === true ? undefined : mode,
});
image = composite(image, c, image.bitmap.width / 2 - c.bitmap.width / 2, image.bitmap.height / 2 - c.bitmap.height / 2);
}
const bW = image.bitmap.width;
const bH = image.bitmap.height;
const dstBuffer = Buffer.alloc(image.bitmap.data.length);
const translate2Cartesian = createTranslationFunction(-(bW / 2), -(bH / 2));
const translate2Screen = createTranslationFunction(bW / 2 + 0.5, bH / 2 + 0.5);
for (let y = 1; y <= bH; y++) {
for (let x = 1; x <= bW; x++) {
const cartesian = translate2Cartesian(x, y);
const source = translate2Screen(cosine * cartesian.x - sine * cartesian.y, cosine * cartesian.y + sine * cartesian.x);
const dstIdx = (bW * (y - 1) + x - 1) << 2;
if (source.x >= 0 && source.x < bW && source.y >= 0 && source.y < bH) {
const srcIdx = ((bW * (source.y | 0) + source.x) | 0) << 2;
const pixelRGBA = image.bitmap.data.readUInt32BE(srcIdx);
dstBuffer.writeUInt32BE(pixelRGBA, dstIdx);
}
else {
// reset off-image pixels
dstBuffer.writeUInt32BE(image.background, dstIdx);
}
}
}
image.bitmap.data = dstBuffer;
if (mode === true || typeof mode === "string") {
// now crop the image to the final size
const x = Math.max(bW / 2 - w / 2, 0);
const y = Math.max(bH / 2 - h / 2, 0);
image = cropMethods.crop(image, { x, y, w, h });
}
}
export const methods = {
/**
* Rotates the image counter-clockwise by a number of degrees. By default the width and height of the image will be resized appropriately.
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.rotate(90);
* ```
*/
rotate(image, options) {
const parsed = RotateOptionsSchema.parse(options);
const actualOptions = typeof parsed === "number" ? { deg: parsed } : parsed;
const { mode = true } = actualOptions;
let { deg } = actualOptions;
// No need to do extra rotation
deg %= 360;
// no rotation for 0, 360, -360, 720, -720, ...
if (deg % 360 === 0) {
return image;
}
// use matrixRotate if the angle is a multiple of 90 degrees (eg: 180 or -90) and resize is allowed or not needed.
const matrixRotateAllowed = deg % 90 === 0 &&
(mode || image.bitmap.width === image.bitmap.height || deg % 180 === 0);
if (matrixRotateAllowed) {
matrixRotate(image, deg);
}
else {
advancedRotate(image, deg, mode);
}
return image;
},
};
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAE/E,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC3D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,mBAAmB,GAAG,CAAC,CAAC,KAAK,CAAC;IAClC,CAAC,CAAC,MAAM,EAAE;IACV,CAAC,CAAC,MAAM,CAAC;QACP,mDAAmD;QACnD,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE;QACf,oGAAoG;QACpG,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,UAAU,CAAC,cAAc,CAAC,CAAC,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