2023-09-10 11:14:04 +02:00
|
|
|
import fs from "fs";
|
2023-09-10 20:00:49 +02:00
|
|
|
import { modelsPath } from "../utils/get-resource-paths";
|
2024-01-16 07:30:07 +01:00
|
|
|
import COMMAND from "../../common/commands";
|
2023-09-10 11:14:04 +02:00
|
|
|
import {
|
2023-09-17 13:37:57 +02:00
|
|
|
compression,
|
2023-09-10 19:42:18 +02:00
|
|
|
customModelsFolderPath,
|
2024-02-08 15:57:35 +01:00
|
|
|
customWidth,
|
2023-09-10 19:42:18 +02:00
|
|
|
folderPath,
|
2023-09-18 20:30:43 +02:00
|
|
|
noImageProcessing,
|
2023-09-10 19:42:18 +02:00
|
|
|
outputFolderPath,
|
|
|
|
saveOutputFolder,
|
2023-09-10 11:14:04 +02:00
|
|
|
setChildProcesses,
|
2023-11-23 06:39:46 +01:00
|
|
|
setCompression,
|
2023-11-22 16:54:02 +01:00
|
|
|
setNoImageProcessing,
|
2023-09-10 19:42:18 +02:00
|
|
|
setStopped,
|
|
|
|
stopped,
|
2024-02-08 15:57:35 +01:00
|
|
|
useCustomWidth,
|
2023-09-10 11:14:04 +02:00
|
|
|
} from "../utils/config-variables";
|
|
|
|
import convertAndScale from "../utils/convert-and-scale";
|
|
|
|
import { getSingleImageArguments } from "../utils/get-arguments";
|
|
|
|
import logit from "../utils/logit";
|
|
|
|
import slash from "../utils/slash";
|
|
|
|
import { spawnUpscayl } from "../utils/spawn-upscayl";
|
|
|
|
import { parse } from "path";
|
2023-09-10 19:42:18 +02:00
|
|
|
import { getMainWindow } from "../main-window";
|
2023-11-22 16:54:02 +01:00
|
|
|
import { ImageUpscaylPayload } from "../../common/types/types";
|
2024-01-15 10:07:22 +01:00
|
|
|
import { ImageFormat } from "../utils/types";
|
2024-01-15 10:25:29 +01:00
|
|
|
import getModelScale from "../../common/check-model-scale";
|
2024-01-15 11:04:11 +01:00
|
|
|
import removeFileExtension from "../utils/remove-file-extension";
|
2024-01-15 12:30:59 +01:00
|
|
|
import showNotification from "../utils/show-notification";
|
2024-01-16 07:30:07 +01:00
|
|
|
import { DEFAULT_MODELS } from "../../common/models-list";
|
2023-09-10 11:14:04 +02:00
|
|
|
|
2023-11-22 16:54:02 +01:00
|
|
|
const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {
|
2023-09-10 19:42:18 +02:00
|
|
|
const mainWindow = getMainWindow();
|
|
|
|
|
|
|
|
if (!mainWindow) {
|
2023-09-11 04:30:50 +02:00
|
|
|
logit("No main window found");
|
2023-09-10 19:42:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-09-10 19:59:35 +02:00
|
|
|
|
2023-11-22 16:54:02 +01:00
|
|
|
setNoImageProcessing(payload.noImageProcessing);
|
2023-11-23 06:39:46 +01:00
|
|
|
setCompression(parseInt(payload.compression));
|
2023-09-10 19:42:18 +02:00
|
|
|
|
2023-09-10 11:14:04 +02:00
|
|
|
const model = payload.model as string;
|
|
|
|
const gpuId = payload.gpuId as string;
|
2024-01-15 10:07:22 +01:00
|
|
|
const saveImageAs = payload.saveImageAs as ImageFormat;
|
2024-01-15 13:38:47 +01:00
|
|
|
console.log("🚀 => saveImageAs:", saveImageAs);
|
|
|
|
|
2024-01-15 10:07:22 +01:00
|
|
|
const overwrite = payload.overwrite as boolean;
|
2023-09-10 11:14:04 +02:00
|
|
|
|
2023-11-22 16:54:02 +01:00
|
|
|
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)?.[1] || "") as string;
|
2023-09-10 11:14:04 +02:00
|
|
|
let outputDir: string | undefined =
|
2023-09-10 19:42:18 +02:00
|
|
|
folderPath || (payload.outputPath as string);
|
2023-09-10 11:14:04 +02:00
|
|
|
|
2023-09-10 19:42:18 +02:00
|
|
|
if (saveOutputFolder === true && outputFolderPath) {
|
|
|
|
outputDir = outputFolderPath;
|
2023-09-10 11:14:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const isDefaultModel = DEFAULT_MODELS.includes(model);
|
2024-01-16 07:30:07 +01:00
|
|
|
logit("Is Default Model? : ", isDefaultModel);
|
2023-09-10 11:14:04 +02:00
|
|
|
const fullfileName = payload.imagePath.replace(/^.*[\\\/]/, "") as string;
|
|
|
|
const fileName = parse(fullfileName).name;
|
|
|
|
const fileExt = parse(fullfileName).ext;
|
|
|
|
|
2024-01-15 10:25:29 +01:00
|
|
|
let initialScale = getModelScale(model);
|
2023-09-10 11:14:04 +02:00
|
|
|
|
2024-02-08 15:57:35 +01:00
|
|
|
const desiredScale = useCustomWidth
|
|
|
|
? customWidth || payload.scale
|
|
|
|
: payload.scale;
|
2023-09-18 20:30:43 +02:00
|
|
|
|
2023-09-10 11:14:04 +02:00
|
|
|
const outFile =
|
|
|
|
outputDir +
|
|
|
|
slash +
|
|
|
|
fileName +
|
|
|
|
"_upscayl_" +
|
2023-09-19 16:51:38 +02:00
|
|
|
(noImageProcessing ? initialScale : desiredScale) +
|
2024-02-08 15:57:35 +01:00
|
|
|
(useCustomWidth ? "px_" : "x_") +
|
2023-09-10 11:14:04 +02:00
|
|
|
model +
|
|
|
|
"." +
|
|
|
|
saveImageAs;
|
|
|
|
|
|
|
|
// UPSCALE
|
2023-09-10 19:54:08 +02:00
|
|
|
if (fs.existsSync(outFile) && !overwrite) {
|
2023-09-10 11:14:04 +02:00
|
|
|
// If already upscayled, just output that file
|
|
|
|
logit("✅ Already upscayled at: ", outFile);
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.UPSCAYL_DONE,
|
|
|
|
outFile.replace(
|
|
|
|
/([^/\\]+)$/i,
|
2024-02-08 15:57:35 +01:00
|
|
|
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),
|
|
|
|
),
|
2023-09-10 11:14:04 +02:00
|
|
|
);
|
|
|
|
} else {
|
2023-11-27 03:21:17 +01:00
|
|
|
logit(
|
|
|
|
"✅ Upscayl Variables: ",
|
|
|
|
JSON.stringify({
|
|
|
|
model,
|
|
|
|
gpuId,
|
|
|
|
saveImageAs,
|
|
|
|
inputDir,
|
|
|
|
outputDir,
|
|
|
|
fullfileName,
|
|
|
|
fileName,
|
|
|
|
initialScale: initialScale,
|
|
|
|
desiredScale,
|
|
|
|
outFile,
|
|
|
|
compression,
|
2024-02-08 15:57:35 +01:00
|
|
|
}),
|
2023-11-27 03:21:17 +01:00
|
|
|
);
|
2023-09-10 11:14:04 +02:00
|
|
|
const upscayl = spawnUpscayl(
|
|
|
|
getSingleImageArguments(
|
|
|
|
inputDir,
|
|
|
|
fullfileName,
|
2024-01-15 13:38:47 +01:00
|
|
|
outFile,
|
2023-09-10 19:42:18 +02:00
|
|
|
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
|
2023-09-10 11:14:04 +02:00
|
|
|
model,
|
2023-09-18 20:30:43 +02:00
|
|
|
initialScale,
|
2023-09-10 11:14:04 +02:00
|
|
|
gpuId,
|
2024-02-08 15:57:35 +01:00
|
|
|
saveImageAs,
|
2023-09-10 11:14:04 +02:00
|
|
|
),
|
2024-02-08 15:57:35 +01:00
|
|
|
logit,
|
2023-09-10 11:14:04 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
setChildProcesses(upscayl);
|
|
|
|
|
2023-09-10 19:42:18 +02:00
|
|
|
setStopped(false);
|
2023-09-10 11:14:04 +02:00
|
|
|
let isAlpha = false;
|
|
|
|
let failed = false;
|
|
|
|
|
|
|
|
const onData = (data: string) => {
|
|
|
|
logit("image upscayl: ", data.toString());
|
|
|
|
mainWindow.setProgressBar(parseFloat(data.slice(0, data.length)) / 100);
|
|
|
|
data = data.toString();
|
|
|
|
mainWindow.webContents.send(COMMAND.UPSCAYL_PROGRESS, data.toString());
|
|
|
|
if (data.includes("invalid gpu") || data.includes("failed")) {
|
|
|
|
logit("❌ INVALID GPU OR FAILED");
|
|
|
|
upscayl.kill();
|
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
if (data.includes("has alpha channel")) {
|
|
|
|
logit("📢 INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!");
|
|
|
|
isAlpha = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const onError = (data) => {
|
|
|
|
if (!mainWindow) return;
|
|
|
|
mainWindow.setProgressBar(-1);
|
|
|
|
mainWindow.webContents.send(COMMAND.UPSCAYL_PROGRESS, data.toString());
|
|
|
|
failed = true;
|
|
|
|
upscayl.kill();
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
const onClose = async () => {
|
2023-09-10 19:42:18 +02:00
|
|
|
if (!failed && !stopped) {
|
2023-09-10 11:14:04 +02:00
|
|
|
logit("💯 Done upscaling");
|
|
|
|
logit("♻ Scaling and converting now...");
|
2023-09-19 17:04:31 +02:00
|
|
|
if (noImageProcessing) {
|
2023-09-18 20:30:43 +02:00
|
|
|
logit("🚫 Skipping scaling and converting");
|
2023-09-10 11:14:04 +02:00
|
|
|
mainWindow.setProgressBar(-1);
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.UPSCAYL_DONE,
|
|
|
|
outFile.replace(
|
|
|
|
/([^/\\]+)$/i,
|
2024-02-08 15:57:35 +01:00
|
|
|
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),
|
|
|
|
),
|
2023-09-10 11:14:04 +02:00
|
|
|
);
|
2023-09-19 17:14:15 +02:00
|
|
|
return;
|
2023-09-10 11:14:04 +02:00
|
|
|
}
|
2023-09-19 17:04:31 +02:00
|
|
|
mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
|
|
|
|
// Free up memory
|
|
|
|
upscayl.kill();
|
|
|
|
try {
|
|
|
|
await convertAndScale(
|
|
|
|
inputDir + slash + fullfileName,
|
2024-01-15 16:06:13 +01:00
|
|
|
isAlpha ? outFile + ".png" : outFile,
|
2023-09-19 17:04:31 +02:00
|
|
|
outFile,
|
|
|
|
desiredScale,
|
|
|
|
saveImageAs,
|
2024-02-08 15:57:35 +01:00
|
|
|
isAlpha,
|
2023-09-19 17:04:31 +02:00
|
|
|
);
|
2023-10-14 10:49:57 +02:00
|
|
|
// Remove the png file (default) if the saveImageAs is not png
|
2024-01-15 13:38:47 +01:00
|
|
|
// fs.access(
|
|
|
|
// removeFileExtension(outFile) + ".png",
|
|
|
|
// fs.constants.F_OK,
|
|
|
|
// (err) => {
|
|
|
|
// if (!err && saveImageAs !== "png") {
|
|
|
|
// logit("🗑 Removing png file");
|
|
|
|
// fs.unlinkSync(removeFileExtension(outFile) + ".png");
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// );
|
2023-09-19 17:04:31 +02:00
|
|
|
mainWindow.setProgressBar(-1);
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.UPSCAYL_DONE,
|
|
|
|
outFile.replace(
|
|
|
|
/([^/\\]+)$/i,
|
2024-02-08 15:57:35 +01:00
|
|
|
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),
|
|
|
|
),
|
2023-09-19 17:04:31 +02:00
|
|
|
);
|
2024-01-15 12:30:59 +01:00
|
|
|
showNotification("Upscayl", "Image upscayled successfully!");
|
2023-09-19 17:04:31 +02:00
|
|
|
} catch (error) {
|
|
|
|
logit(
|
|
|
|
"❌ Error processing (scaling and converting) the image. Please report this error on GitHub.",
|
2024-02-08 15:57:35 +01:00
|
|
|
error,
|
2023-09-19 17:04:31 +02:00
|
|
|
);
|
|
|
|
upscayl.kill();
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.UPSCAYL_ERROR,
|
2023-10-14 10:41:57 +02:00
|
|
|
"Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +
|
2024-02-08 15:57:35 +01:00
|
|
|
error,
|
2023-09-19 17:04:31 +02:00
|
|
|
);
|
2024-01-15 12:30:59 +01:00
|
|
|
showNotification("Upscayl Failure", "Failed to upscale image!");
|
2023-09-19 17:04:31 +02:00
|
|
|
}
|
2023-09-10 11:14:04 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
upscayl.process.stderr.on("data", onData);
|
|
|
|
upscayl.process.on("error", onError);
|
|
|
|
upscayl.process.on("close", onClose);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default imageUpscayl;
|