1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-01-21 01:58:50 +01:00
upscayl/electron/utils/convert-and-scale.ts

123 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-10-14 13:48:43 +05:30
import fs from "fs";
2023-11-26 18:36:23 +05:30
import sharp, { FormatEnum, Metadata } from "sharp";
2023-09-10 14:44:04 +05:30
import logit from "./logit";
2024-02-08 20:27:35 +05:30
import { compression, customWidth } from "./config-variables";
import { ImageFormat } from "./types";
2023-09-10 14:44:04 +05:30
const convertAndScale = async (
originalImagePath: string,
upscaledImagePath: string,
processedImagePath: string,
scale: string,
saveImageAs: ImageFormat,
2024-02-08 20:27:35 +05:30
isAlpha: boolean,
2023-09-10 14:44:04 +05:30
) => {
2024-02-08 20:27:35 +05:30
if (!isAlpha && !customWidth && scale === "4" && compression === 0) {
2024-01-23 15:00:08 +05:30
logit("Skipping compression for 4x scale and 0% compression");
2023-10-14 14:18:14 +05:30
return;
}
2023-11-26 18:36:23 +05:30
let originalImage: Metadata | undefined;
2023-09-10 23:12:18 +05:30
2023-11-26 18:36:23 +05:30
try {
originalImage = await sharp(originalImagePath).metadata();
} catch (error) {
logit("❌ Error with original Image: ", error, " - ", originalImagePath);
2023-11-26 18:36:23 +05:30
}
2023-09-17 05:29:18 +05:30
2023-10-14 13:48:43 +05:30
fs.access(originalImagePath, fs.constants.F_OK, (err) => {
logit("🖼️ Checking if original image exists: ", originalImagePath);
2023-10-14 13:48:43 +05:30
if (err) {
throw new Error(
2024-02-08 20:27:35 +05:30
"Could not grab the original image from the path provided! - " + err,
);
2023-10-14 13:48:43 +05:30
}
});
if (!originalImage) {
2023-09-10 14:44:04 +05:30
throw new Error("Could not grab the original image!");
}
2024-01-21 12:18:22 +05:30
console.log("🚀 => originalImage:", originalImage);
2023-09-19 00:00:43 +05:30
// Convert compression percentage (0-100) to compressionLevel (0-9)
const compressionLevel = Math.round((compression / 100) * 9);
2023-09-19 00:00:43 +05:30
2023-10-14 14:11:57 +05:30
logit(
"📐 Processing Image: ",
JSON.stringify({
originalWidth: originalImage.width,
originalHeight: originalImage.height,
2024-02-08 20:27:35 +05:30
customWidth,
2023-10-14 14:11:57 +05:30
scale,
saveImageAs,
compressionPercentage: compression,
compressionLevel,
2024-02-08 20:27:35 +05:30
}),
2023-10-14 14:11:57 +05:30
);
2023-09-17 05:29:18 +05:30
2024-02-08 20:27:35 +05:30
// Resize the image to the scale
const newImage = sharp(upscaledImagePath, {
limitInputPixels: false,
})
.resize(
customWidth
? parseInt(customWidth)
: originalImage.width && originalImage.width * parseInt(scale),
customWidth
? null
: originalImage.height && originalImage.height * parseInt(scale),
)
.withMetadata({
density: originalImage.density,
orientation: originalImage.orientation,
});
console.log("🚀 => newImage:", newImage);
2024-01-21 12:18:22 +05:30
const buffer = await newImage
.withMetadata({
density: originalImage.density,
orientation: originalImage.orientation,
})
.toBuffer();
2023-09-13 21:45:42 +05:30
try {
await sharp(buffer, {
limitInputPixels: false,
2023-09-17 05:29:18 +05:30
})
2024-01-21 12:18:22 +05:30
.withMetadata({
density: originalImage.density,
orientation: originalImage.orientation,
})
2023-09-17 05:44:41 +05:30
.toFormat(saveImageAs as keyof FormatEnum, {
2023-09-17 17:07:57 +05:30
...(saveImageAs === "jpg" && {
quality: 100 - (compression === 100 ? 99 : compression),
chromaSubsampling: "4:4:4",
}),
2023-11-23 11:09:46 +05:30
// For PNGs, compression enables indexed colors automatically,
// so we need to warn the user that this will happen
// https://sharp.pixelplumbing.com/api-output#png
...(saveImageAs === "png" &&
compression > 0 && {
...(compression > 0 && {
quality: 100 - (compression === 100 ? 99 : compression),
}),
compressionLevel: 9,
}),
2024-01-15 14:55:29 +05:30
...(saveImageAs === "webp" && {
quality: 100 - (compression === 100 ? 99 : compression),
alphaQuality: 100,
lossless: compression === 0,
smartSubsample: true,
}),
2023-09-17 17:07:57 +05:30
force: true,
2023-09-17 05:44:41 +05:30
})
2023-09-17 05:29:18 +05:30
.toFile(processedImagePath);
2023-09-13 21:45:42 +05:30
} catch (error) {
logit("❌ Error converting to: ", saveImageAs, error);
}
logit("✅ Done converting to: ", upscaledImagePath);
2023-09-10 14:44:04 +05:30
};
export default convertAndScale;