chore(vendor): import pi 0.85.1
Imported as a linear vendor commit so forge rebases do not replay the upstream root tree. git-subtree-dir: third_party/pi git-subtree-split: d981de1229ef899957bbe968bc8dcda02a21f477
This commit is contained in:
49
third_party/pi/packages/coding-agent/src/utils/image-convert.ts
vendored
Normal file
49
third_party/pi/packages/coding-agent/src/utils/image-convert.ts
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
import { applyExifOrientation } from "./exif-orientation.ts";
|
||||
import { loadPhoton } from "./photon.ts";
|
||||
|
||||
export async function convertImageBytesToPng(bytes: Uint8Array): Promise<Uint8Array | null> {
|
||||
const photon = await loadPhoton();
|
||||
if (!photon) {
|
||||
// Photon not available, can't convert
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const rawImage = photon.PhotonImage.new_from_byteslice(bytes);
|
||||
const image = applyExifOrientation(photon, rawImage, bytes);
|
||||
if (image !== rawImage) rawImage.free();
|
||||
try {
|
||||
return new Uint8Array(image.get_bytes());
|
||||
} finally {
|
||||
image.free();
|
||||
}
|
||||
} catch {
|
||||
// Conversion failed
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 };
|
||||
}
|
||||
|
||||
const bytes = new Uint8Array(Buffer.from(base64Data, "base64"));
|
||||
const pngBytes = await convertImageBytesToPng(bytes);
|
||||
if (!pngBytes) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
data: Buffer.from(pngBytes).toString("base64"),
|
||||
mimeType: "image/png",
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user