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
|
|
|
savedCompression,
|
|
|
|
savedCustomModelsPath,
|
|
|
|
savedBatchUpscaylFolderPath,
|
2023-09-18 20:30:43 +02:00
|
|
|
noImageProcessing,
|
2024-04-09 20:11:24 +02:00
|
|
|
savedOutputPath,
|
|
|
|
rememberOutputFolder,
|
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,
|
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-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 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
|
|
|
|
2024-04-09 20:11:24 +02:00
|
|
|
// GET VARIABLES
|
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;
|
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 =
|
2024-04-09 20:11:24 +02:00
|
|
|
savedBatchUpscaylFolderPath || (payload.outputPath as string);
|
|
|
|
if (
|
|
|
|
rememberOutputFolder === true &&
|
|
|
|
savedOutputPath &&
|
|
|
|
savedOutputPath?.length > 0
|
|
|
|
) {
|
|
|
|
logit("🧠 Using saved output path");
|
|
|
|
outputDir = savedOutputPath;
|
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-04-20 17:44:42 +02:00
|
|
|
const useCustomWidth = payload.useCustomWidth;
|
2024-04-21 19:14:39 +02:00
|
|
|
const customWidth = useCustomWidth ? payload.customWidth : "";
|
2024-04-20 17:44:42 +02:00
|
|
|
|
2024-04-21 19:39:13 +02:00
|
|
|
const scale = payload.scale;
|
2023-09-18 20:30:43 +02:00
|
|
|
|
2023-09-10 11:14:04 +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-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);
|
|
|
|
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,
|
2024-04-21 19:39:13 +02:00
|
|
|
scale,
|
2023-11-27 03:21:17 +01:00
|
|
|
outFile,
|
2024-04-09 20:11:24 +02:00
|
|
|
compression: savedCompression,
|
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({
|
2023-09-10 11:14:04 +02:00
|
|
|
inputDir,
|
|
|
|
fullfileName,
|
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 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;
|
2024-04-21 19:14:39 +02:00
|
|
|
} else if (data.includes("has alpha channel")) {
|
2023-09-10 11:14:04 +02:00
|
|
|
logit("📢 INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!");
|
|
|
|
isAlpha = 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);
|
|
|
|
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
|
|
|
// Free up memory
|
|
|
|
upscayl.kill();
|
|
|
|
try {
|
|
|
|
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;
|