2026-04-15 22:56:33 +02:00
|
|
|
import { ImageFormat, parseImage } from "@singularity-forge/native/image";
|
2026-03-12 21:55:17 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert image to PNG format for terminal display.
|
|
|
|
|
* Kitty graphics protocol requires PNG format (f=100).
|
|
|
|
|
*/
|
|
|
|
|
export async function convertToPng(
|
|
|
|
|
base64Data: string,
|
|
|
|
|
mimeType: string,
|
|
|
|
|
): Promise<{ data: string; mimeType: string } | null> {
|
|
|
|
|
// Already PNG, no conversion needed
|
|
|
|
|
if (mimeType === "image/png") {
|
|
|
|
|
return { data: base64Data, mimeType };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const bytes = new Uint8Array(Buffer.from(base64Data, "base64"));
|
2026-03-13 13:39:06 -06:00
|
|
|
const image = await parseImage(bytes);
|
|
|
|
|
const pngBytes = await image.encode(ImageFormat.PNG, 100);
|
|
|
|
|
return {
|
|
|
|
|
data: Buffer.from(new Uint8Array(pngBytes)).toString("base64"),
|
|
|
|
|
mimeType: "image/png",
|
|
|
|
|
};
|
2026-03-12 21:55:17 -06:00
|
|
|
} catch {
|
|
|
|
|
// Conversion failed
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|