Skip to content

armgit / VERT

Public Imported from GitHub
0

The next-generation file converter. Open source, fully local* and free forever.

feat: libvips settings

JovannMC08eb09d8c2b6
diff --git a/src/lib/converters/vips/vips.svelte.ts b/src/lib/converters/vips/vips.svelte.ts
index cc64c0b..97ff01c 100644
--- a/src/lib/converters/vips/vips.svelte.ts
+++ b/src/lib/converters/vips/vips.svelte.ts
@@ -69,7 +69,6 @@ export class VipsConverter extends Converter {
input: VertFile,
): Promise<SettingCategories> {
const to = input.to.toLowerCase();
-
const settings: SettingDefinition[] = [];
if (VIPS_QUALITY_FORMATS.has(to)) {
@@ -83,6 +82,38 @@ export class VipsConverter extends Converter {
});
}
+ settings.push({
+ key: "resolution",
+ label: m["convert.settings.image.resolution.label"](),
+ type: "text",
+ default: "",
+ placeholder: m["convert.settings.image.resolution.placeholder"](),
+ });
+
+ settings.push({
+ key: "depth",
+ label: m["convert.settings.image.depth.label"](),
+ type: "text",
+ default: "",
+ placeholder: m["convert.settings.image.depth.placeholder"](),
+ });
+
+ settings.push({
+ key: "colorSpace",
+ label: m["convert.settings.image.color_space"](),
+ type: "select",
+ default: "auto",
+ options: [
+ { value: "auto", label: "Auto" },
+ { value: "srgb", label: "sRGB" },
+ { value: "rgb", label: "RGB" },
+ { value: "cmyk", label: "CMYK" },
+ { value: "lab", label: "CIELAB" },
+ { value: "xyz", label: "CIEXYZ" },
+ { value: "gray", label: "Grayscale" },
+ ],
+ });
+
if (!VIPS_OPAQUE_FORMATS.has(to)) {
settings.push({
key: "transparency",
diff --git a/src/lib/workers/vips.ts b/src/lib/workers/vips.ts
index a1731fd..179e060 100644
--- a/src/lib/workers/vips.ts
+++ b/src/lib/workers/vips.ts
@@ -25,6 +25,21 @@ const DYNAMIC_LIBRARIES = [
const NO_QUALITY = new Set([".gif", ".svg", ".ppm", ".pnm"]);
const OPAQUE = new Set([".jpg", ".jpeg", ".jpe", ".jfif", ".ppm", ".pnm"]);
+const COLOURSPACES: Record<string, string> = {
+ srgb: "srgb",
+ rgb: "rgb",
+ cmyk: "cmyk",
+ lab: "lab",
+ xyz: "xyz",
+ gray: "b-w",
+};
+
+const BAND_FORMATS: Record<string, string> = {
+ 8: "uchar",
+ 16: "ushort",
+ 32: "float",
+};
+
const isWasmException = (err: unknown): err is WebAssembly.Exception =>
typeof WebAssembly !== "undefined" &&
typeof WebAssembly.Exception === "function" &&
@@ -112,6 +127,9 @@ const handleMessage = async (
let image: VipsImage | null = null;
let flattened: VipsImage | null = null;
+ let resized: VipsImage | null = null;
+ let colourspace: VipsImage | null = null;
+ let cast: VipsImage | null = null;
try {
image = vips.Image.newFromBuffer(buffer);
@@ -122,12 +140,38 @@ const handleMessage = async (
image = vips.Image.newFromBuffer(buffer, "[n=-1]");
}
- let source = image;
+ // resize image
+ const resolution = String(settings.resolution ?? "").trim();
+ if (resolution && resolution !== "auto") {
+ const [width, height] = resolution
+ .split("x")
+ .map((dim: string) => Number.parseInt(dim, 10));
+ if (width && width > 0) {
+ resized = image.thumbnailImage(width, {
+ ...(height && height > 0 ? { height } : {}),
+ });
+ }
+ }
+ let source = resized ?? image;
+
+ // colour space conversion
+ const space = COLOURSPACES[settings.colorSpace as string];
+ if (space) {
+ colourspace = source.colourspace(space);
+ source = colourspace;
+ }
+
+ // bit depth / band format
+ const bandFormat = BAND_FORMATS[String(settings.depth ?? "")];
+ if (bandFormat) {
+ cast = source.cast(bandFormat);
+ source = cast;
+ }
// flatten transparency when the target can't store alpha or the user opted out
const keepAlpha = settings.transparency !== false;
- if ((!keepAlpha || OPAQUE.has(to)) && image.hasAlpha()) {
- flattened = image.flatten({ background: [255, 255, 255] });
+ if ((!keepAlpha || OPAQUE.has(to)) && source.hasAlpha()) {
+ flattened = source.flatten({ background: [255, 255, 255] });
source = flattened;
}
@@ -143,7 +187,10 @@ const handleMessage = async (
error: `vips failed to convert ${from} to ${to}: ${formatError(err)}`,
};
} finally {
+ cast?.delete();
+ colourspace?.delete();
flattened?.delete();
+ resized?.delete();
image?.delete();
}
}