The next-generation file converter. Open source, fully local* and free forever.
feat: mpo format
JovannMC1ee0931e9b11
diff --git a/src/lib/components/functional/FormatDropdown.svelte b/src/lib/components/functional/FormatDropdown.svelteindex 5b60a6e..a4ef16b 100644--- a/src/lib/components/functional/FormatDropdown.svelte+++ b/src/lib/components/functional/FormatDropdown.svelte@@ -54,6 +54,8 @@file?.conversionSettings?.imageSequenceTransparency ?? false,);+ const sequenceFormats = [".webp", ".gif", ".apng", ".mpo"]; // .apng not supported by magick-wasm rn+$effect(() => {if (!file) return;file.conversionSettings.imageSequence = imageSequence;@@ -75,10 +77,9 @@(f) => !shouldExclude(f),);- // if imageSequence is checked, filter image category to animated formats only+ // if imageSequence is checked, filter image category to sequence formats onlyif (imageSequence && cat === "image") {- const animatedFormats = [".webp", ".gif", ".apng"]; // .apng not supported by magick-wasm rn- formats = formats.filter((f) => animatedFormats.includes(f));+ formats = formats.filter((f) => sequenceFormats.includes(f));}return formats;@@ -145,6 +146,9 @@//cats.push("image"); // -- buggy, magick can't convert from or to apng properlycats = cats.filter((cat) => cat !== "audio");}+ if (from === ".mpo") {+ cats = cats.filter((cat) => cat !== "audio");+ }// large videos can't be extracted to audio (browser/device limitations)if (file && file.isLarge() && rootCategory === "video")@@ -200,7 +204,6 @@}const query = normalize(searchQuery);- const animatedFormats = [".webp", ".apng", ".gif"];const matches = (f: string, cat?: string) => {if (@@ -211,7 +214,7 @@return false;// if imageSequence and image category, only show animated formatsif (imageSequence && (cat ?? activeCategory) === "image") {- return animatedFormats.includes(f);+ return sequenceFormats.includes(f);}return true;};diff --git a/src/lib/converters/magick/magick.svelte.ts b/src/lib/converters/magick/magick.svelte.tsindex fc3c8d2..a65836d 100644--- a/src/lib/converters/magick/magick.svelte.ts+++ b/src/lib/converters/magick/magick.svelte.ts@@ -32,6 +32,7 @@ export class MagickConverter extends Converter {new FormatInfo("avif", true, true),new FormatInfo("heic", true, false), // seems to be unreliable? HEIC/HEIF is very weird if it will actually worknew FormatInfo("heif", true, false),+ new FormatInfo("mpo", true, false),new FormatInfo("ico", true, true),new FormatInfo("bmp", true, true),new FormatInfo("cur", true, true),diff --git a/src/lib/types/file.svelte.ts b/src/lib/types/file.svelte.tsindex 8426be0..7a482f6 100644--- a/src/lib/types/file.svelte.ts+++ b/src/lib/types/file.svelte.ts@@ -169,6 +169,7 @@ export class VertFile {if (!this.fileType) return;+ const forceKeep = ["mpo"];const aliases: Record<string, string> = {// original: aliasjpg: "jpeg",@@ -178,8 +179,9 @@ export class VertFile {// TODO: is there more stuff};const fileExtension = this.from.slice(1);- const detectedExtension =- aliases[this.fileType.ext] ?? this.fileType.ext;+ const detectedExtension = forceKeep.includes(fileExtension)+ ? fileExtension+ : aliases[this.fileType.ext] ?? this.fileType.ext;const expectedExtension = aliases[fileExtension] ?? fileExtension;if (detectedExtension !== expectedExtension) {diff --git a/src/lib/workers/magick.ts b/src/lib/workers/magick.tsindex 7288868..ff13634 100644--- a/src/lib/workers/magick.ts+++ b/src/lib/workers/magick.ts@@ -116,6 +116,37 @@ const readToEnd = async (reader: ReadableStreamDefaultReader<Uint8Array>) => {return new Uint8Array(arrayBuffer);};+const convertCollectionToZip = async (+ buffer: ArrayBuffer,+ format: MagickFormat,+ to: string,+ conversionSettings: ConversionSettings,+): Promise<Uint8Array> => {+ const collection = MagickImageCollection.create();+ try {+ collection.read(+ new Uint8Array(buffer),+ new MagickReadSettings({ format }),+ );+ console.log(`read ${collection.length} images from ${format}`);++ const convertedImages = await Promise.all(+ collection.map((img) => magickConvert(img, to, conversionSettings)),+ );+ const zip = makeZip(+ convertedImages.map(+ (img, i) =>+ new File([new Uint8Array(img)], `image${i}.${to.slice(1)}`),+ ),+ "images.zip",+ );++ return await readToEnd(zip.getReader());+ } finally {+ collection.dispose();+ }+};+const loadDicomHelpers = async () =>(dicomPromise ??= import("$lib/util/parse/dicom"));@@ -145,15 +176,37 @@ const handleSpecialInput = async (return null;};-// formats that have special handling for output (like multiple frames/images)const handleSpecialOutput = async (from: string,to: string,buffer: ArrayBuffer,conversionSettings: ConversionSettings,): Promise<Partial<WorkerMessage> | null> => {- // build frames of animated formats (webp/gif)- // APNG does not work on magick-wasm since it needs ffmpeg built-in (not in magick-wasm) - handle in ffmpeg+ // converting multi-image formats to individual files+ const collectionFormats: Record<string, MagickFormat> = {+ ".gif": MagickFormat.Gif,+ ".webp": MagickFormat.WebP,+ ".ico": MagickFormat.Ico,+ ".mpo": MagickFormat.Mpo,+ };+ const collectionFormat = collectionFormats[from];+ if (collectionFormat !== undefined) {+ const zipBytes = await convertCollectionToZip(+ buffer,+ collectionFormat,+ to,+ conversionSettings,+ );++ return {+ type: "finished",+ output: zipBytes,+ zip: true,+ };+ }++ // converting from animated format to another animated format+ // APNG does not work on magick-wasm since it needs ffmpeg built-in (not in magick-wasm) - should pass into ffmpeg.wasm insteadif ((from === ".webp" || from === ".gif") &&(to === ".gif" || to === ".webp")@@ -173,47 +226,6 @@ const handleSpecialOutput = async (};}- if (from === ".ico") {- const imgs = MagickImageCollection.create();- imgs.read(- new Uint8Array(buffer),- new MagickReadSettings({ format: MagickFormat.Ico }),- );-- if (imgs.length === 0) {- return {- type: "error",- error: `Failed to read ICO -- no images found inside?`,- };- }-- const convertedImgs: Uint8Array[] = [];- await Promise.all(- imgs.map(async (img, i) => {- const output = await magickConvert(img, to, conversionSettings);- convertedImgs[i] = output;- }),- );-- const zip = makeZip(- convertedImgs.map(- (img, i) =>- new File([new Uint8Array(img)], `image${i}.${to.slice(1)}`),- ),- "images.zip",- );-- // read the ReadableStream to the end- const zipBytes = await readToEnd(zip.getReader());- imgs.dispose();-- return {- type: "finished",- output: zipBytes,- zip: true,- };- }-if (from === ".ani") {console.log("Parsing ANI file");try {