2023-09-11 04:25:14 +02:00
|
|
|
import path, { parse } from "path";
|
|
|
|
import { getMainWindow } from "../main-window";
|
|
|
|
import {
|
|
|
|
childProcesses,
|
2024-04-09 20:11:24 +02:00
|
|
|
savedCustomModelsPath,
|
2023-09-19 17:04:31 +02:00
|
|
|
noImageProcessing,
|
2024-04-09 20:11:24 +02:00
|
|
|
savedOutputPath,
|
|
|
|
rememberOutputFolder,
|
2023-11-23 06:39:46 +01:00
|
|
|
setCompression,
|
2023-11-22 16:54:02 +01:00
|
|
|
setNoImageProcessing,
|
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-01-15 10:07:22 +01:00
|
|
|
import { ImageFormat } from "../utils/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";
|
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;
|
|
|
|
|
|
|
|
const model = payload.model as string;
|
|
|
|
const imagePath = payload.imagePath;
|
|
|
|
let inputDir = (imagePath.match(/(.*)[\/\\]/) || [""])[1];
|
|
|
|
let outputDir = path.normalize(payload.outputPath);
|
|
|
|
|
2024-04-09 20:11:24 +02:00
|
|
|
if (rememberOutputFolder === true && savedOutputPath) {
|
|
|
|
outputDir = savedOutputPath;
|
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;
|
2023-09-11 04:25:14 +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-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
|
|
|
|
|
|
|
|
const fullfileName = imagePath.split(slash).slice(-1)[0] as string;
|
|
|
|
const fileName = parse(fullfileName).name;
|
|
|
|
|
2024-04-09 20:11:24 +02:00
|
|
|
const scale = parseInt(payload.scale) * parseInt(payload.scale);
|
2024-04-20 17:44:42 +02:00
|
|
|
const useCustomWidth = payload.useCustomWidth;
|
2024-04-21 19:14:39 +02:00
|
|
|
const customWidth = useCustomWidth ? payload.customWidth : "";
|
2023-09-11 04:25:14 +02:00
|
|
|
|
2023-09-11 05:57:16 +02:00
|
|
|
const outFile =
|
2024-04-21 19:39:13 +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,
|
|
|
|
fullfileName,
|
|
|
|
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-04-09 20:11:24 +02:00
|
|
|
scale: scale.toString(),
|
2024-04-21 19:14:39 +02:00
|
|
|
customWidth,
|
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
|
|
|
);
|
|
|
|
|
|
|
|
childProcesses.push(upscayl);
|
|
|
|
|
|
|
|
setStopped(false);
|
|
|
|
let failed = 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
|
2024-04-08 20:47:08 +02:00
|
|
|
if (data.includes("Error") || data.includes("failed")) {
|
2023-09-11 04:25:14 +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-11 04:25:14 +02:00
|
|
|
}
|
2024-04-21 19:14:39 +02:00
|
|
|
// if (data.includes("alpha channel")) {
|
|
|
|
// isAlpha = true;
|
|
|
|
// }
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
const onClose2 = async (code) => {
|
|
|
|
if (!mainWindow) return;
|
|
|
|
if (!failed2 && !stopped) {
|
|
|
|
logit("💯 Done upscaling");
|
2023-09-19 17:04:31 +02:00
|
|
|
|
2023-09-11 04:25:14 +02:00
|
|
|
try {
|
|
|
|
mainWindow.setProgressBar(-1);
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.DOUBLE_UPSCAYL_DONE,
|
2024-01-15 14:09:46 +01:00
|
|
|
outFile.replace(
|
|
|
|
/([^/\\]+)$/i,
|
2024-02-08 15:57:35 +01:00
|
|
|
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),
|
|
|
|
),
|
2023-09-11 04:25:14 +02:00
|
|
|
);
|
2024-01-15 12:30:59 +01:00
|
|
|
showNotification("Upscayled", "Image upscayled successfully!");
|
2023-09-11 04:25:14 +02:00
|
|
|
} 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" +
|
2024-02-08 15:57:35 +01:00
|
|
|
error,
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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(
|
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-04-09 20:11:24 +02:00
|
|
|
scale: scale.toString(),
|
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-11 04:25:14 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
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,
|
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
|
|
|
upscayl2.kill();
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
upscayl2.process.on("close", onClose2);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export default doubleUpscayl;
|