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 {
|
2024-04-09 20:11:24 +02:00
|
|
|
savedCustomModelsPath,
|
2023-09-10 11:14:04 +02:00
|
|
|
setChildProcesses,
|
2023-09-10 19:42:18 +02:00
|
|
|
setStopped,
|
|
|
|
stopped,
|
2023-09-10 11:14:04 +02:00
|
|
|
} from "../utils/config-variables";
|
|
|
|
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-04-24 12:34:32 +02:00
|
|
|
import { ImageFormat } from "../types/types";
|
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";
|
2024-04-24 21:29:51 +02:00
|
|
|
import getFilenameFromPath from "../../common/get-file-name";
|
|
|
|
import decodePath from "../../common/decode-path";
|
|
|
|
import getDirectoryFromPath from "../../common/get-directory-from-path";
|
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
|
|
|
|
2024-04-09 20:11:24 +02:00
|
|
|
// GET VARIABLES
|
2024-04-24 21:29:51 +02:00
|
|
|
const compression = payload.compression;
|
|
|
|
const scale = payload.scale;
|
|
|
|
const useCustomWidth = payload.useCustomWidth;
|
|
|
|
const customWidth = useCustomWidth ? payload.customWidth : "";
|
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;
|
|
|
|
const overwrite = payload.overwrite as boolean;
|
2024-04-24 21:29:51 +02:00
|
|
|
const imagePath = decodePath(payload.imagePath);
|
|
|
|
let inputDir = getDirectoryFromPath(imagePath);
|
|
|
|
let outputDir = decodePath(payload.outputPath);
|
|
|
|
const fileNameWithExt = getFilenameFromPath(imagePath);
|
|
|
|
const fileName = parse(fileNameWithExt).name;
|
2023-09-18 20:30:43 +02:00
|
|
|
|
2023-09-10 11:14:04 +02:00
|
|
|
const outFile =
|
2024-04-21 19:40:54 +02:00
|
|
|
outputDir +
|
|
|
|
slash +
|
|
|
|
fileName +
|
|
|
|
"_upscayl_" +
|
|
|
|
(useCustomWidth ? `${customWidth}px_` : `${scale}x_`) +
|
|
|
|
model +
|
|
|
|
"." +
|
|
|
|
saveImageAs;
|
2023-09-10 11:14:04 +02:00
|
|
|
|
2024-04-24 21:29:51 +02:00
|
|
|
const isDefaultModel = DEFAULT_MODELS.includes(model);
|
|
|
|
|
2023-09-10 11:14:04 +02:00
|
|
|
// 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);
|
2024-04-24 21:29:51 +02:00
|
|
|
mainWindow.webContents.send(COMMAND.UPSCAYL_DONE, outFile);
|
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,
|
2024-04-24 21:29:51 +02:00
|
|
|
fileNameWithExt,
|
2023-11-27 03:21:17 +01:00
|
|
|
outputDir,
|
2024-04-24 21:29:51 +02:00
|
|
|
outFile,
|
2023-11-27 03:21:17 +01:00
|
|
|
fileName,
|
2024-04-21 19:39:13 +02:00
|
|
|
scale,
|
2024-04-24 21:29:51 +02:00
|
|
|
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(
|
2024-04-09 20:11:24 +02:00
|
|
|
getSingleImageArguments({
|
2024-04-24 21:29:51 +02:00
|
|
|
inputDir: decodeURIComponent(inputDir),
|
|
|
|
fileNameWithExt: decodeURIComponent(fileNameWithExt),
|
2024-01-15 13:38:47 +01:00
|
|
|
outFile,
|
2024-04-09 20:11:24 +02:00
|
|
|
modelsPath: isDefaultModel
|
|
|
|
? modelsPath
|
|
|
|
: savedCustomModelsPath ?? modelsPath,
|
2023-09-10 11:14:04 +02:00
|
|
|
model,
|
2024-04-21 19:39:13 +02:00
|
|
|
scale,
|
2023-09-10 11:14:04 +02:00
|
|
|
gpuId,
|
2024-02-08 15:57:35 +01:00
|
|
|
saveImageAs,
|
2024-04-20 17:44:42 +02:00
|
|
|
customWidth,
|
2024-04-09 20:11:24 +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 failed = false;
|
|
|
|
|
|
|
|
const onData = (data: string) => {
|
2024-04-24 15:16:35 +02:00
|
|
|
logit(data.toString());
|
2023-09-10 11:14:04 +02:00
|
|
|
mainWindow.setProgressBar(parseFloat(data.slice(0, data.length)) / 100);
|
|
|
|
data = data.toString();
|
|
|
|
mainWindow.webContents.send(COMMAND.UPSCAYL_PROGRESS, data.toString());
|
2024-04-24 15:44:51 +02:00
|
|
|
if (data.includes("Error")) {
|
2023-09-10 11:14:04 +02:00
|
|
|
upscayl.kill();
|
|
|
|
failed = true;
|
2024-04-21 19:14:39 +02:00
|
|
|
} else if (data.includes("Resizing")) {
|
|
|
|
mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
|
2023-09-10 11:14:04 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const onError = (data) => {
|
|
|
|
if (!mainWindow) return;
|
|
|
|
mainWindow.setProgressBar(-1);
|
2024-04-24 15:16:35 +02:00
|
|
|
mainWindow.webContents.send(COMMAND.UPSCAYL_ERROR, data.toString());
|
2023-09-10 11:14:04 +02:00
|
|
|
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");
|
2023-09-19 17:04:31 +02:00
|
|
|
// Free up memory
|
|
|
|
upscayl.kill();
|
2024-04-24 15:16:35 +02:00
|
|
|
mainWindow.setProgressBar(-1);
|
2024-04-24 21:29:51 +02:00
|
|
|
mainWindow.webContents.send(COMMAND.UPSCAYL_DONE, outFile);
|
2024-04-24 15:16:35 +02:00
|
|
|
showNotification("Upscayl", "Image upscayled successfully!");
|
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;
|