Spaces:
Sleeping
Sleeping
File size: 5,037 Bytes
3530df7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | import { JimpClass } from "@jimp/types";
import { ResizeStrategy } from "./constants.js";
import { z } from "zod";
import Resize from "./modules/resize.js";
import { operations as Resize2 } from "./modules/resize2.js";
export * from "./constants.js";
interface ResizeClass {
// eslint-disable-next-line @typescript-eslint/no-misused-new
new (
widthOriginal: number,
heightOriginal: number,
targetWidth: number,
targetHeight: number,
blendAlpha: boolean,
interpolationPass: boolean,
resizeCallback: (buffer: Buffer) => void
): ResizeClass;
resize(buffer: Buffer): void;
}
type Constructable<T> = new (...args: unknown[]) => T;
const ResizeOptionsSchema = z.union([
z.object({
/** the width to resize the image to */
w: z.number().min(0),
/** the height to resize the image to */
h: z.number().min(0).optional(),
/** a scaling method (e.g. ResizeStrategy.BEZIER) */
mode: z.nativeEnum(ResizeStrategy).optional(),
}),
z.object({
/** the width to resize the image to */
w: z.number().min(0).optional(),
/** the height to resize the image to */
h: z.number().min(0),
/** a scaling method (e.g. ResizeStrategy.BEZIER) */
mode: z.nativeEnum(ResizeStrategy).optional(),
}),
]);
export type ResizeOptions = z.infer<typeof ResizeOptionsSchema>;
const ScaleToFitOptionsSchema = z.object({
/** the width to resize the image to */
w: z.number().min(0),
/** the height to resize the image to */
h: z.number().min(0),
/** a scaling method (e.g. Jimp.RESIZE_BEZIER) */
mode: z.nativeEnum(ResizeStrategy).optional(),
});
export type ScaleToFitOptions = z.infer<typeof ScaleToFitOptionsSchema>;
const ScaleComplexOptionsSchema = z.object({
/** the width to resize the image to */
f: z.number().min(0),
/** a scaling method (e.g. Jimp.RESIZE_BEZIER) */
mode: z.nativeEnum(ResizeStrategy).optional(),
});
export type ScaleComplexOptions = z.infer<typeof ScaleComplexOptionsSchema>;
export type ScaleOptions = number | ScaleComplexOptions;
export const methods = {
/**
* Resizes the image to a set width and height using a 2-pass bilinear algorithm
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.resize({ w: 150 });
* ```
*/
resize<I extends JimpClass>(image: I, options: ResizeOptions) {
const { mode } = ResizeOptionsSchema.parse(options);
let w: number;
let h: number;
if (typeof options.w === "number") {
w = options.w;
h = options.h ?? image.bitmap.height * (w / image.bitmap.width);
} else if (typeof options.h === "number") {
h = options.h;
w = options.w ?? image.bitmap.width * (h / image.bitmap.height);
} else {
throw new Error("w must be a number");
}
// round inputs
w = Math.round(w) || 1;
h = Math.round(h) || 1;
if (mode && typeof Resize2[mode] === "function") {
const dst = {
data: Buffer.alloc(w * h * 4),
width: w,
height: h,
};
Resize2[mode](image.bitmap, dst);
image.bitmap = dst;
} else {
const resize = new (Resize as unknown as Constructable<ResizeClass>)(
image.bitmap.width,
image.bitmap.height,
w,
h,
true,
true,
(buffer: Buffer) => {
image.bitmap.data = Buffer.from(buffer);
image.bitmap.width = w;
image.bitmap.height = h;
}
);
resize.resize(image.bitmap.data);
}
return image;
},
/**
* Uniformly scales the image by a factor.
* @param f the factor to scale the image by
* @param mode (optional) a scaling method (e.g. Jimp.RESIZE_BEZIER)
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.scale(0.5);
* ```
*/
scale<I extends JimpClass>(image: I, options: ScaleOptions) {
const { f, mode } =
typeof options === "number"
? ({ f: options } as ScaleComplexOptions)
: ScaleComplexOptionsSchema.parse(options);
const w = image.bitmap.width * f;
const h = image.bitmap.height * f;
return this.resize(image, { w, h, mode: mode });
},
/**
* Scale the image to the largest size that fits inside the rectangle that has the given width and height.
* @param w the width to resize the image to
* @param h the height to resize the image to
* @param mode a scaling method (e.g. ResizeStrategy.BEZIER)
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
*
* image.scaleToFit(100, 100);
* ```
*/
scaleToFit<I extends JimpClass>(image: I, options: ScaleToFitOptions) {
const { h, w, mode } = ScaleToFitOptionsSchema.parse(options);
const f =
w / h > image.bitmap.width / image.bitmap.height
? h / image.bitmap.height
: w / image.bitmap.width;
return this.scale(image, { f, mode: mode });
},
};
|