1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-12-14 00:22:59 +01:00
upscayl/electron/commands/double-upscayl.ts

239 lines
6.6 KiB
TypeScript
Raw Normal View History

import path, { parse } from "path";
import { getMainWindow } from "../main-window";
import {
childProcesses,
customModelsFolderPath,
2023-09-19 17:04:31 +02:00
noImageProcessing,
outputFolderPath,
saveOutputFolder,
setStopped,
stopped,
} from "../utils/config-variables";
import DEFAULT_MODELS from "../constants/models";
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";
import COMMAND from "../constants/commands";
2023-09-11 05:57:16 +02:00
import convertAndScale from "../utils/convert-and-scale";
const doubleUpscayl = async (event, payload) => {
const mainWindow = getMainWindow();
if (!mainWindow) return;
const model = payload.model as string;
const imagePath = payload.imagePath;
let inputDir = (imagePath.match(/(.*)[\/\\]/) || [""])[1];
let outputDir = path.normalize(payload.outputPath);
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
}
const gpuId = payload.gpuId as string;
const saveImageAs = payload.saveImageAs as string;
const isDefaultModel = DEFAULT_MODELS.includes(model);
// COPY IMAGE TO TMP FOLDER
const fullfileName = imagePath.split(slash).slice(-1)[0] as string;
const fileName = parse(fullfileName).name;
2023-09-19 17:04:31 +02:00
let initialScale = "4";
if (model.includes("x2")) {
2023-09-19 17:04:31 +02:00
initialScale = "2";
} else if (model.includes("x3")) {
2023-09-19 17:04:31 +02:00
initialScale = "3";
} else {
2023-09-19 17:04:31 +02:00
initialScale = "4";
}
2023-09-19 17:04:31 +02:00
const desiredScale = parseInt(payload.scale) * parseInt(payload.scale);
2023-09-11 05:57:16 +02:00
const outFile =
outputDir +
slash +
fileName +
"_upscayl_" +
2023-09-19 17:04:31 +02:00
(noImageProcessing
? parseInt(initialScale) * parseInt(initialScale)
: desiredScale) +
2023-09-11 05:57:16 +02:00
"x_" +
model +
"." +
saveImageAs;
// UPSCALE
let upscayl = spawnUpscayl(
"realesrgan",
getDoubleUpscaleArguments(
inputDir,
fullfileName,
outFile,
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
model,
gpuId,
saveImageAs,
2023-09-19 17:04:31 +02:00
initialScale
),
logit
);
childProcesses.push(upscayl);
setStopped(false);
let failed = false;
let isAlpha = false;
let failed2 = false;
const onData = (data) => {
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
if (data.includes("invalid gpu") || data.includes("failed")) {
upscayl.kill();
failed = true;
}
if (data.includes("has alpha channel")) {
isAlpha = true;
}
};
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,
"Error upscaling image. Error: " + data
);
upscayl.kill();
return;
};
const onClose2 = async (code) => {
if (!mainWindow) return;
if (!failed2 && !stopped) {
logit("💯 Done upscaling");
logit("♻ Scaling and converting now...");
mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
2023-09-19 17:04:31 +02:00
if (noImageProcessing) {
logit("🚫 Skipping scaling and converting");
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
COMMAND.DOUBLE_UPSCAYL_DONE,
isAlpha
? (outFile + ".png").replace(
/([^/\\]+)$/i,
encodeURIComponent((outFile + ".png").match(/[^/\\]+$/i)![0])
)
: outFile.replace(
/([^/\\]+)$/i,
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0])
)
);
return;
}
try {
2023-09-11 05:57:16 +02:00
await convertAndScale(
inputDir + slash + fullfileName,
isAlpha ? outFile + ".png" : outFile,
outFile,
2023-09-22 15:02:59 +02:00
desiredScale.toString(),
2023-09-11 05:57:16 +02:00
saveImageAs,
onError
);
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
COMMAND.DOUBLE_UPSCAYL_DONE,
isAlpha
? (outFile + ".png").replace(
/([^/\\]+)$/i,
encodeURIComponent((outFile + ".png").match(/[^/\\]+$/i)![0])
)
: outFile.replace(
/([^/\\]+)$/i,
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0])
)
);
} catch (error) {
logit("❌ Error reading original image metadata", error);
mainWindow &&
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" +
error
);
upscayl.kill();
}
}
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", (code) => {
// IF NOT FAILED
if (!failed && !stopped) {
// UPSCALE
let upscayl2 = spawnUpscayl(
"realesrgan",
getDoubleUpscaleSecondPassArguments(
isAlpha,
outFile,
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
model,
gpuId,
saveImageAs,
2023-09-19 17:04:31 +02:00
initialScale
),
logit
);
childProcesses.push(upscayl2);
upscayl2.process.stderr.on("data", (data) => {
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
if (data.includes("invalid gpu") || data.includes("failed")) {
upscayl2.kill();
failed2 = true;
}
});
upscayl2.process.on("error", (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
);
upscayl2.kill();
return;
});
upscayl2.process.on("close", onClose2);
}
});
};
export default doubleUpscayl;