Spaces:
Sleeping
Sleeping
File size: 1,618 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 | import { PNG, PNGOptions as PNGJSOptions } from "pngjs";
import { Format } from "@jimp/types";
import { PNGFilterType, PNGColorType } from "./constants.js";
export type { PNGOptions as PNGJSOptions } from "pngjs";
export type PNGOptions = Omit<
PNGJSOptions,
"filterType" | "colorType" | "inputColorType"
> & {
filterType?: PNGFilterType;
colorType?: PNGColorType;
inputColorType?: PNGColorType;
};
export interface DecodePngOptions {
checkCRC?: boolean | undefined;
skipRescale?: boolean | undefined;
}
export * from "./constants.js";
export default function png() {
return {
mime: "image/png",
hasAlpha: true,
encode: (
bitmap,
{
deflateLevel = 9,
deflateStrategy = 3,
filterType = PNGFilterType.AUTO,
colorType,
inputHasAlpha = true,
...options
}: PNGOptions = {}
) => {
const png = new PNG({
width: bitmap.width,
height: bitmap.height,
});
png.data = bitmap.data;
return PNG.sync.write(png, {
...options,
deflateLevel,
deflateStrategy,
filterType,
colorType:
typeof colorType !== "undefined"
? colorType
: inputHasAlpha
? PNGColorType.COLOR_ALPHA
: PNGColorType.COLOR,
inputHasAlpha,
});
},
decode: (data, options?: DecodePngOptions) => {
const result = PNG.sync.read(data, options);
return {
data: result.data,
width: result.width,
height: result.height,
};
},
} satisfies Format<"image/png">;
}
|