2024-04-24 21:29:51 +02:00
|
|
|
import { parse } from "path";
|
2023-09-11 04:25:14 +02:00
|
|
|
import { getMainWindow } from "../main-window";
|
|
|
|
import {
|
|
|
|
childProcesses,
|
2024-04-09 20:11:24 +02:00
|
|
|
savedCustomModelsPath,
|
2023-09-11 04:25:14 +02:00
|
|
|
setStopped,
|
|
|
|
stopped,
|
|
|
|
} from "../utils/config-variables";
|
|
|
|
import slash from "../utils/slash";
|
|
|
|
import { spawnUpscayl } from "../utils/spawn-upscayl";
|
|
|
|
import {
|
|
|
|
getDoubleUpscaleArguments,
|
|
|
|
getDoubleUpscaleSecondPassArguments,
|
|
|
|
} from "../utils/get-arguments";
|
|
|
|
import { modelsPath } from "../utils/get-resource-paths";
|
|
|
|
import logit from "../utils/logit";
|
2024-01-16 07:30:07 +01:00
|
|
|
import COMMAND from "../../common/commands";
|
2023-11-22 16:54:02 +01:00
|
|
|
import { DoubleUpscaylPayload } 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-11 04:25:14 +02:00
|
|
|
|
2023-11-22 16:54:02 +01:00
|
|
|
const doubleUpscayl = async (event, payload: DoubleUpscaylPayload) => {
|
2023-09-11 04:25:14 +02:00
|
|
|
const mainWindow = getMainWindow();
|
|
|
|
if (!mainWindow) return;
|
|
|
|
|
2024-04-24 21:40:48 +02:00
|
|
|
const tileSize = payload.tileSize;
|
2024-04-24 21:29:51 +02:00
|
|
|
const compression = payload.compression;
|
2024-05-29 19:48:13 +02:00
|
|
|
const scale = payload.scale;
|
2024-04-24 21:29:51 +02:00
|
|
|
const useCustomWidth = payload.useCustomWidth;
|
|
|
|
const customWidth = useCustomWidth ? payload.customWidth : "";
|
|
|
|
const model = payload.model;
|
2023-09-11 04:25:14 +02:00
|
|
|
const gpuId = payload.gpuId as string;
|
2024-01-15 10:07:22 +01:00
|
|
|
const saveImageAs = payload.saveImageAs as ImageFormat;
|
2024-04-24 21:29:51 +02:00
|
|
|
const imagePath = decodePath(payload.imagePath);
|
|
|
|
let inputDir = getDirectoryFromPath(imagePath);
|
|
|
|
let outputDir = decodePath(payload.outputPath);
|
|
|
|
const fullfileName = getFilenameFromPath(imagePath);
|
|
|
|
const fileName = parse(fullfileName).name;
|
2023-11-22 16:54:02 +01:00
|
|
|
|
2023-09-11 04:25:14 +02:00
|
|
|
const isDefaultModel = DEFAULT_MODELS.includes(model);
|
|
|
|
|
|
|
|
// COPY IMAGE TO TMP FOLDER
|
|
|
|
|
2023-09-11 05:57:16 +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-11 05:57:16 +02:00
|
|
|
|
2023-09-11 04:25:14 +02:00
|
|
|
// UPSCALE
|
|
|
|
let upscayl = spawnUpscayl(
|
2024-04-09 20:11:24 +02:00
|
|
|
getDoubleUpscaleArguments({
|
2023-09-11 04:25:14 +02:00
|
|
|
inputDir,
|
2024-04-24 21:29:51 +02:00
|
|
|
fullfileName: decodeURIComponent(fullfileName),
|
2023-09-11 04:25:14 +02:00
|
|
|
outFile,
|
2024-04-09 20:11:24 +02:00
|
|
|
modelsPath: isDefaultModel
|
|
|
|
? modelsPath
|
|
|
|
: savedCustomModelsPath ?? modelsPath,
|
2023-09-11 04:25:14 +02:00
|
|
|
model,
|
2024-05-29 19:48:13 +02:00
|
|
|
scale,
|
|
|
|
customWidth,
|
2023-09-11 04:25:14 +02:00
|
|
|
gpuId,
|
|
|
|
saveImageAs,
|
2024-04-24 21:40:48 +02:00
|
|
|
tileSize,
|
2024-04-09 20:11:24 +02:00
|
|
|
}),
|
2024-02-08 15:57:35 +01:00
|
|
|
logit,
|
2023-09-11 04:25:14 +02:00
|
|
|
);
|
|
|
|
|
2024-04-24 15:16:35 +02:00
|
|
|
let upscayl2: ReturnType<typeof spawnUpscayl>;
|
|
|
|
|
2023-09-11 04:25:14 +02:00
|
|
|
childProcesses.push(upscayl);
|
|
|
|
|
|
|
|
setStopped(false);
|
|
|
|
let failed = false;
|
|
|
|
let failed2 = false;
|
|
|
|
|
2024-04-24 15:16:35 +02:00
|
|
|
// SECOND PASS FUNCTIONS
|
|
|
|
const onError2 = (data) => {
|
|
|
|
if (!mainWindow) return;
|
|
|
|
data.toString();
|
|
|
|
// SEND UPSCAYL PROGRESS TO RENDERER
|
|
|
|
mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);
|
|
|
|
// SET FAILED TO TRUE
|
|
|
|
failed2 = true;
|
|
|
|
mainWindow &&
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.UPSCAYL_ERROR,
|
|
|
|
"Error upscaling image. Error: " + data,
|
|
|
|
);
|
|
|
|
showNotification("Upscayl Failure", "Failed to upscale image!");
|
|
|
|
upscayl2.kill();
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
const onData2 = (data) => {
|
2023-09-11 04:25:14 +02:00
|
|
|
if (!mainWindow) return;
|
|
|
|
// CONVERT DATA TO STRING
|
|
|
|
data = data.toString();
|
|
|
|
// SEND UPSCAYL PROGRESS TO RENDERER
|
|
|
|
mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);
|
|
|
|
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
|
2024-04-24 15:44:51 +02:00
|
|
|
if (data.includes("Error")) {
|
2024-04-24 15:16:35 +02:00
|
|
|
upscayl2.kill();
|
|
|
|
failed2 = true;
|
2024-04-24 15:44:51 +02:00
|
|
|
} else if (data.includes("Resizing")) {
|
|
|
|
mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
|
2024-04-24 15:16:35 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onClose2 = async (code) => {
|
|
|
|
if (!mainWindow) return;
|
|
|
|
if (!failed2 && !stopped) {
|
|
|
|
logit("💯 Done upscaling");
|
|
|
|
|
|
|
|
mainWindow.setProgressBar(-1);
|
2024-04-24 21:29:51 +02:00
|
|
|
mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_DONE, outFile);
|
2024-04-24 15:16:35 +02:00
|
|
|
showNotification("Upscayled", "Image upscayled successfully!");
|
2023-09-11 04:25:14 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-04-24 15:16:35 +02:00
|
|
|
// FIRST PASS FUNCTIONS
|
2023-09-11 04:25:14 +02:00
|
|
|
const onError = (data) => {
|
|
|
|
if (!mainWindow) return;
|
|
|
|
mainWindow.setProgressBar(-1);
|
|
|
|
data.toString();
|
|
|
|
// SEND UPSCAYL PROGRESS TO RENDERER
|
|
|
|
mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);
|
|
|
|
// SET FAILED TO TRUE
|
|
|
|
failed = true;
|
|
|
|
mainWindow &&
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.UPSCAYL_ERROR,
|
2024-02-08 15:57:35 +01:00
|
|
|
"Error upscaling image. Error: " + data,
|
2023-09-11 04:25:14 +02:00
|
|
|
);
|
2024-01-15 12:30:59 +01:00
|
|
|
showNotification("Upscayl Failure", "Failed to upscale image!");
|
2023-09-11 04:25:14 +02:00
|
|
|
upscayl.kill();
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2024-04-24 15:16:35 +02:00
|
|
|
const onData = (data) => {
|
2023-09-11 04:25:14 +02:00
|
|
|
if (!mainWindow) return;
|
2024-04-24 15:16:35 +02:00
|
|
|
// CONVERT DATA TO STRING
|
|
|
|
data = data.toString();
|
|
|
|
// SEND UPSCAYL PROGRESS TO RENDERER
|
|
|
|
mainWindow.webContents.send(COMMAND.DOUBLE_UPSCAYL_PROGRESS, data);
|
|
|
|
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
|
|
|
|
if (data.includes("Error") || data.includes("failed")) {
|
|
|
|
upscayl.kill();
|
|
|
|
failed = true;
|
|
|
|
} else if (data.includes("Resizing")) {
|
|
|
|
mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
|
2023-09-11 04:25:14 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-04-24 15:16:35 +02:00
|
|
|
const onClose = (code) => {
|
2023-09-11 04:25:14 +02:00
|
|
|
// IF NOT FAILED
|
|
|
|
if (!failed && !stopped) {
|
2024-04-24 15:16:35 +02:00
|
|
|
// SPAWN A SECOND PASS
|
|
|
|
upscayl2 = spawnUpscayl(
|
2024-04-09 20:11:24 +02:00
|
|
|
getDoubleUpscaleSecondPassArguments({
|
2023-09-11 04:25:14 +02:00
|
|
|
outFile,
|
2024-04-09 20:11:24 +02:00
|
|
|
modelsPath: isDefaultModel
|
|
|
|
? modelsPath
|
|
|
|
: savedCustomModelsPath ?? modelsPath,
|
2023-09-11 04:25:14 +02:00
|
|
|
model,
|
|
|
|
gpuId,
|
|
|
|
saveImageAs,
|
2024-05-29 19:48:13 +02:00
|
|
|
scale,
|
2024-04-20 17:44:42 +02:00
|
|
|
customWidth,
|
2024-04-24 21:29:51 +02:00
|
|
|
compression,
|
2024-04-24 21:40:48 +02:00
|
|
|
tileSize,
|
2024-04-09 20:11:24 +02:00
|
|
|
}),
|
2024-02-08 15:57:35 +01:00
|
|
|
logit,
|
2023-09-11 04:25:14 +02:00
|
|
|
);
|
2024-04-24 15:44:51 +02:00
|
|
|
logit("🚀 Upscaling Second Pass");
|
2023-09-11 04:25:14 +02:00
|
|
|
childProcesses.push(upscayl2);
|
2024-04-24 15:16:35 +02:00
|
|
|
upscayl2.process.stderr.on("data", onData2);
|
|
|
|
upscayl2.process.on("error", onError2);
|
2023-09-11 04:25:14 +02:00
|
|
|
upscayl2.process.on("close", onClose2);
|
|
|
|
}
|
2024-04-24 15:16:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
upscayl.process.stderr.on("data", onData);
|
|
|
|
upscayl.process.on("error", onError);
|
|
|
|
upscayl.process.on("close", onClose);
|
2023-09-11 04:25:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default doubleUpscayl;
|