Skip to content

Commit a1fac89

Browse files
authoredSep 7, 2024
Add support for image decoder options (#1336)
1 parent c1b91e9 commit a1fac89

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed
 

‎packages/core/src/index.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,20 @@ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
9898
type Constructor<T> = new (...args: any[]) => T;
9999

100100
type JimpFormat<
101-
M extends string = string,
102-
O extends Record<string, any> | undefined = undefined,
103-
T extends Format<M, O> = Format<M, O>,
101+
MimeType extends string = string,
102+
EncodeOptions extends Record<string, any> | undefined = undefined,
103+
DecodeOptions extends Record<string, any> | undefined = undefined,
104+
T extends Format<MimeType, EncodeOptions, DecodeOptions> = Format<
105+
MimeType,
106+
EncodeOptions,
107+
DecodeOptions
108+
>,
104109
> = () => T;
105110

106111
type CreateMimeTypeToExportOptions<T extends Format<string, any>> =
107112
T extends Format<infer M, infer O> ? Record<M, O> : never;
113+
type CreateMimeTypeToDecodeOptions<T extends Format<string, any>> =
114+
T extends Format<infer M, any, infer O> ? Record<M, O> : never;
108115
type GetOptionsForMimeType<Mime extends string, MimeTypeMap> =
109116
MimeTypeMap extends Record<Mime, infer O> ? O : never;
110117

@@ -138,6 +145,9 @@ export function createJimp<
138145
type MimeTypeToExportOptions = CreateMimeTypeToExportOptions<
139146
ReturnType<Formats[number]>
140147
>;
148+
type MimeTypeToDecodeOptions = CreateMimeTypeToDecodeOptions<
149+
ReturnType<Formats[number]>
150+
>;
141151
type ExtensionToMimeType = CreateExtensionToMimeType<SupportedMimeTypes>;
142152

143153
const plugins = pluginsArg || [];
@@ -213,7 +223,10 @@ export function createJimp<
213223
* const image = await Jimp.read("https://upload.wikimedia.org/wikipedia/commons/0/01/Bot-Test.jpg");
214224
* ```
215225
*/
216-
static async read(url: string | Buffer | ArrayBuffer) {
226+
static async read(
227+
url: string | Buffer | ArrayBuffer,
228+
options?: MimeTypeToDecodeOptions
229+
) {
217230
if (Buffer.isBuffer(url) || url instanceof ArrayBuffer) {
218231
return this.fromBuffer(url);
219232
}
@@ -239,7 +252,7 @@ export function createJimp<
239252
}
240253

241254
const buffer = bufferFromArrayBuffer(data);
242-
return this.fromBuffer(buffer);
255+
return this.fromBuffer(buffer, options);
243256
}
244257

245258
/**
@@ -314,7 +327,10 @@ export function createJimp<
314327
* const image = await Jimp.fromBuffer(buffer);
315328
* ```
316329
*/
317-
static async fromBuffer(buffer: Buffer | ArrayBuffer) {
330+
static async fromBuffer(
331+
buffer: Buffer | ArrayBuffer,
332+
options?: MimeTypeToDecodeOptions
333+
) {
318334
const actualBuffer =
319335
buffer instanceof ArrayBuffer ? bufferFromArrayBuffer(buffer) : buffer;
320336

@@ -331,7 +347,7 @@ export function createJimp<
331347
}
332348

333349
const image = new CustomJimp(
334-
await format.decode(actualBuffer)
350+
await format.decode(actualBuffer, options?.[format.mime])
335351
) as InstanceType<typeof CustomJimp> & ExtraMethodMap;
336352

337353
image.mime = mime.mime;

‎packages/types/src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ export interface Format<
1616
Mime extends string = string,
1717
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1818
ExportOptions extends Record<string, any> | undefined = undefined,
19+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20+
DecodeOptions extends Record<string, any> | undefined = undefined,
1921
> {
2022
mime: Mime;
2123
hasAlpha?: boolean;
2224
encode: (image: Bitmap, options?: ExportOptions) => Promise<Buffer> | Buffer;
23-
decode: (data: Buffer) => Promise<Bitmap> | Bitmap;
25+
decode: (data: Buffer, options?: DecodeOptions) => Promise<Bitmap> | Bitmap;
2426
}
2527

2628
export interface RGBColor {

‎plugins/js-bmp/src/index.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export type EncodeOptions = Pretty<
3030
>
3131
>;
3232

33+
export interface DecodeBmpOptions {
34+
toRGBA?: boolean;
35+
}
36+
3337
function encode(image: Bitmap, options: EncodeOptions = {}) {
3438
scan(
3539
{ bitmap: image },
@@ -47,14 +51,14 @@ function encode(image: Bitmap, options: EncodeOptions = {}) {
4751
image.data[index + 1] = blue;
4852
image.data[index + 2] = green;
4953
image.data[index + 3] = red;
50-
},
54+
}
5155
);
5256

5357
return BMP.encode({ ...image, ...options }).data;
5458
}
5559

56-
function decode(data: Buffer) {
57-
const result = BMP.decode(data);
60+
function decode(data: Buffer, options?: DecodeBmpOptions) {
61+
const result = BMP.decode(data, options);
5862

5963
scan(
6064
{ bitmap: result },
@@ -72,7 +76,7 @@ function decode(data: Buffer) {
7276
result.data[index + 1] = green;
7377
result.data[index + 2] = blue;
7478
result.data[index + 3] = 0xff;
75-
},
79+
}
7680
);
7781

7882
return result as Bitmap;

‎plugins/js-jpeg/src/index.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@ export interface JPEGOptions {
55
quality?: number;
66
}
77

8+
export interface DecodeJpegOptions {
9+
useTArray?: false;
10+
colorTransform?: boolean;
11+
formatAsRGBA?: boolean;
12+
tolerantDecoding?: boolean;
13+
maxResolutionInMP?: number;
14+
maxMemoryUsageInMB?: number;
15+
}
16+
817
export default function jpeg() {
918
return {
1019
mime: "image/jpeg",
1120
encode: (bitmap, { quality = 100 }: JPEGOptions = {}) =>
1221
JPEG.encode(bitmap, quality).data,
13-
decode: (data) => JPEG.decode(data),
22+
decode: (data, options?: DecodeJpegOptions) => JPEG.decode(data, options),
1423
} satisfies Format<"image/jpeg">;
1524
}

‎plugins/js-png/src/index.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export type PNGOptions = Omit<
1212
inputColorType?: PNGColorType;
1313
};
1414

15+
export interface DecodePngOptions {
16+
checkCRC?: boolean | undefined;
17+
skipRescale?: boolean | undefined;
18+
}
19+
1520
export * from "./constants.js";
1621

1722
export default function png() {
@@ -27,7 +32,7 @@ export default function png() {
2732
colorType,
2833
inputHasAlpha = true,
2934
...options
30-
}: PNGOptions = {},
35+
}: PNGOptions = {}
3136
) => {
3237
const png = new PNG({
3338
width: bitmap.width,
@@ -50,8 +55,8 @@ export default function png() {
5055
inputHasAlpha,
5156
});
5257
},
53-
decode: (data) => {
54-
const result = PNG.sync.read(data);
58+
decode: (data, options?: DecodePngOptions) => {
59+
const result = PNG.sync.read(data, options);
5560

5661
return {
5762
data: result.data,

0 commit comments

Comments
 (0)