diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 95097985..ccdb4d91 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -98,13 +98,20 @@ type UnionToIntersection = (U extends any ? (k: U) => void : never) extends ( type Constructor = new (...args: any[]) => T; type JimpFormat< - M extends string = string, - O extends Record | undefined = undefined, - T extends Format = Format, + MimeType extends string = string, + EncodeOptions extends Record | undefined = undefined, + DecodeOptions extends Record | undefined = undefined, + T extends Format = Format< + MimeType, + EncodeOptions, + DecodeOptions + >, > = () => T; type CreateMimeTypeToExportOptions> = T extends Format ? Record : never; +type CreateMimeTypeToDecodeOptions> = + T extends Format ? Record : never; type GetOptionsForMimeType = MimeTypeMap extends Record ? O : never; @@ -138,6 +145,9 @@ export function createJimp< type MimeTypeToExportOptions = CreateMimeTypeToExportOptions< ReturnType >; + type MimeTypeToDecodeOptions = CreateMimeTypeToDecodeOptions< + ReturnType + >; type ExtensionToMimeType = CreateExtensionToMimeType; const plugins = pluginsArg || []; @@ -213,7 +223,10 @@ export function createJimp< * const image = await Jimp.read("https://upload.wikimedia.org/wikipedia/commons/0/01/Bot-Test.jpg"); * ``` */ - static async read(url: string | Buffer | ArrayBuffer) { + static async read( + url: string | Buffer | ArrayBuffer, + options?: MimeTypeToDecodeOptions + ) { if (Buffer.isBuffer(url) || url instanceof ArrayBuffer) { return this.fromBuffer(url); } @@ -239,7 +252,7 @@ export function createJimp< } const buffer = bufferFromArrayBuffer(data); - return this.fromBuffer(buffer); + return this.fromBuffer(buffer, options); } /** @@ -314,7 +327,10 @@ export function createJimp< * const image = await Jimp.fromBuffer(buffer); * ``` */ - static async fromBuffer(buffer: Buffer | ArrayBuffer) { + static async fromBuffer( + buffer: Buffer | ArrayBuffer, + options?: MimeTypeToDecodeOptions + ) { const actualBuffer = buffer instanceof ArrayBuffer ? bufferFromArrayBuffer(buffer) : buffer; @@ -331,7 +347,7 @@ export function createJimp< } const image = new CustomJimp( - await format.decode(actualBuffer) + await format.decode(actualBuffer, options?.[format.mime]) ) as InstanceType & ExtraMethodMap; image.mime = mime.mime; diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index f716262f..6a446267 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -16,11 +16,13 @@ export interface Format< Mime extends string = string, // eslint-disable-next-line @typescript-eslint/no-explicit-any ExportOptions extends Record | undefined = undefined, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + DecodeOptions extends Record | undefined = undefined, > { mime: Mime; hasAlpha?: boolean; encode: (image: Bitmap, options?: ExportOptions) => Promise | Buffer; - decode: (data: Buffer) => Promise | Bitmap; + decode: (data: Buffer, options?: DecodeOptions) => Promise | Bitmap; } export interface RGBColor { diff --git a/plugins/js-bmp/src/index.ts b/plugins/js-bmp/src/index.ts index 146fcddb..8dc8d366 100644 --- a/plugins/js-bmp/src/index.ts +++ b/plugins/js-bmp/src/index.ts @@ -30,6 +30,10 @@ export type EncodeOptions = Pretty< > >; +export interface DecodeBmpOptions { + toRGBA?: boolean; +} + function encode(image: Bitmap, options: EncodeOptions = {}) { scan( { bitmap: image }, @@ -47,14 +51,14 @@ function encode(image: Bitmap, options: EncodeOptions = {}) { image.data[index + 1] = blue; image.data[index + 2] = green; image.data[index + 3] = red; - }, + } ); return BMP.encode({ ...image, ...options }).data; } -function decode(data: Buffer) { - const result = BMP.decode(data); +function decode(data: Buffer, options?: DecodeBmpOptions) { + const result = BMP.decode(data, options); scan( { bitmap: result }, @@ -72,7 +76,7 @@ function decode(data: Buffer) { result.data[index + 1] = green; result.data[index + 2] = blue; result.data[index + 3] = 0xff; - }, + } ); return result as Bitmap; diff --git a/plugins/js-jpeg/src/index.ts b/plugins/js-jpeg/src/index.ts index d9baaac7..bc24291a 100644 --- a/plugins/js-jpeg/src/index.ts +++ b/plugins/js-jpeg/src/index.ts @@ -5,11 +5,20 @@ export interface JPEGOptions { quality?: number; } +export interface DecodeJpegOptions { + useTArray?: false; + colorTransform?: boolean; + formatAsRGBA?: boolean; + tolerantDecoding?: boolean; + maxResolutionInMP?: number; + maxMemoryUsageInMB?: number; +} + export default function jpeg() { return { mime: "image/jpeg", encode: (bitmap, { quality = 100 }: JPEGOptions = {}) => JPEG.encode(bitmap, quality).data, - decode: (data) => JPEG.decode(data), + decode: (data, options?: DecodeJpegOptions) => JPEG.decode(data, options), } satisfies Format<"image/jpeg">; } diff --git a/plugins/js-png/src/index.ts b/plugins/js-png/src/index.ts index 97506ae8..84596d63 100644 --- a/plugins/js-png/src/index.ts +++ b/plugins/js-png/src/index.ts @@ -12,6 +12,11 @@ export type PNGOptions = Omit< inputColorType?: PNGColorType; }; +export interface DecodePngOptions { + checkCRC?: boolean | undefined; + skipRescale?: boolean | undefined; +} + export * from "./constants.js"; export default function png() { @@ -27,7 +32,7 @@ export default function png() { colorType, inputHasAlpha = true, ...options - }: PNGOptions = {}, + }: PNGOptions = {} ) => { const png = new PNG({ width: bitmap.width, @@ -50,8 +55,8 @@ export default function png() { inputHasAlpha, }); }, - decode: (data) => { - const result = PNG.sync.read(data); + decode: (data, options?: DecodePngOptions) => { + const result = PNG.sync.read(data, options); return { data: result.data,