1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-02-16 02:42:36 +01:00
upscayl/electron/commands/image-upscayl.ts

203 lines
5.9 KiB
TypeScript
Raw Normal View History

2023-09-10 14:44:04 +05:30
import fs from "fs";
2023-09-10 23:30:49 +05:30
import { modelsPath } from "../utils/get-resource-paths";
import COMMAND from "../../common/commands";
2023-09-10 14:44:04 +05:30
import {
2024-04-09 23:41:24 +05:30
savedCompression,
savedCustomModelsPath,
2024-02-08 20:27:35 +05:30
customWidth,
2024-04-09 23:41:24 +05:30
savedBatchUpscaylFolderPath,
2023-09-19 00:00:43 +05:30
noImageProcessing,
2024-04-09 23:41:24 +05:30
savedOutputPath,
rememberOutputFolder,
2023-09-10 14:44:04 +05:30
setChildProcesses,
2023-11-23 11:09:46 +05:30
setCompression,
2023-11-22 21:24:02 +05:30
setNoImageProcessing,
2023-09-10 23:12:18 +05:30
setStopped,
stopped,
2024-02-08 20:27:35 +05:30
useCustomWidth,
2023-09-10 14:44:04 +05:30
} 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 23:12:18 +05:30
import { getMainWindow } from "../main-window";
2023-11-22 21:24:02 +05:30
import { ImageUpscaylPayload } from "../../common/types/types";
import { ImageFormat } from "../utils/types";
2024-01-15 14:55:29 +05:30
import getModelScale from "../../common/check-model-scale";
import showNotification from "../utils/show-notification";
import { DEFAULT_MODELS } from "../../common/models-list";
2023-09-10 14:44:04 +05:30
2023-11-22 21:24:02 +05:30
const imageUpscayl = async (event, payload: ImageUpscaylPayload) => {
2023-09-10 23:12:18 +05:30
const mainWindow = getMainWindow();
if (!mainWindow) {
2023-09-11 08:00:50 +05:30
logit("No main window found");
2023-09-10 23:12:18 +05:30
return;
}
2023-09-10 23:29:35 +05:30
2023-11-22 21:24:02 +05:30
setNoImageProcessing(payload.noImageProcessing);
2023-11-23 11:09:46 +05:30
setCompression(parseInt(payload.compression));
2023-09-10 23:12:18 +05:30
2024-04-09 23:41:24 +05:30
// GET VARIABLES
2023-09-10 14:44:04 +05:30
const model = payload.model as string;
const gpuId = payload.gpuId as string;
const saveImageAs = payload.saveImageAs as ImageFormat;
const overwrite = payload.overwrite as boolean;
2023-11-22 21:24:02 +05:30
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)?.[1] || "") as string;
2023-09-10 14:44:04 +05:30
let outputDir: string | undefined =
2024-04-09 23:41:24 +05:30
savedBatchUpscaylFolderPath || (payload.outputPath as string);
if (
rememberOutputFolder === true &&
savedOutputPath &&
savedOutputPath?.length > 0
) {
logit("🧠 Using saved output path");
outputDir = savedOutputPath;
2023-09-10 14:44:04 +05:30
}
const isDefaultModel = DEFAULT_MODELS.includes(model);
logit("Is Default Model? : ", isDefaultModel);
2023-09-10 14:44:04 +05:30
const fullfileName = payload.imagePath.replace(/^.*[\\\/]/, "") as string;
const fileName = parse(fullfileName).name;
const fileExt = parse(fullfileName).ext;
2024-02-08 20:27:35 +05:30
const desiredScale = useCustomWidth
? customWidth || payload.scale
: payload.scale;
2023-09-19 00:00:43 +05:30
2023-09-10 14:44:04 +05:30
const outFile =
outputDir +
slash +
fileName +
"_upscayl_" +
2024-04-09 23:41:24 +05:30
desiredScale +
2024-02-08 20:27:35 +05:30
(useCustomWidth ? "px_" : "x_") +
2023-09-10 14:44:04 +05:30
model +
"." +
saveImageAs;
// UPSCALE
2023-09-10 23:24:08 +05:30
if (fs.existsSync(outFile) && !overwrite) {
2023-09-10 14:44:04 +05:30
// If already upscayled, just output that file
logit("✅ Already upscayled at: ", outFile);
mainWindow.webContents.send(
COMMAND.UPSCAYL_DONE,
outFile.replace(
/([^/\\]+)$/i,
2024-02-08 20:27:35 +05:30
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),
),
2023-09-10 14:44:04 +05:30
);
} else {
2023-11-27 07:51:17 +05:30
logit(
"✅ Upscayl Variables: ",
JSON.stringify({
model,
gpuId,
saveImageAs,
inputDir,
outputDir,
fullfileName,
fileName,
2024-04-09 23:41:24 +05:30
scale: desiredScale,
2023-11-27 07:51:17 +05:30
desiredScale,
outFile,
2024-04-09 23:41:24 +05:30
compression: savedCompression,
2024-02-08 20:27:35 +05:30
}),
2023-11-27 07:51:17 +05:30
);
2023-09-10 14:44:04 +05:30
const upscayl = spawnUpscayl(
2024-04-09 23:41:24 +05:30
getSingleImageArguments({
2023-09-10 14:44:04 +05:30
inputDir,
fullfileName,
outFile,
2024-04-09 23:41:24 +05:30
modelsPath: isDefaultModel
? modelsPath
: savedCustomModelsPath ?? modelsPath,
2023-09-10 14:44:04 +05:30
model,
2024-04-09 23:41:24 +05:30
scale: desiredScale,
2023-09-10 14:44:04 +05:30
gpuId,
2024-02-08 20:27:35 +05:30
saveImageAs,
2024-04-09 23:41:24 +05:30
}),
2024-02-08 20:27:35 +05:30
logit,
2023-09-10 14:44:04 +05:30
);
setChildProcesses(upscayl);
2023-09-10 23:12:18 +05:30
setStopped(false);
2023-09-10 14:44:04 +05:30
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;
}
if (data.includes("has alpha channel")) {
logit("📢 INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!");
isAlpha = true;
}
};
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 23:12:18 +05:30
if (!failed && !stopped) {
2023-09-10 14:44:04 +05:30
logit("💯 Done upscaling");
logit("♻ Scaling and converting now...");
2023-09-19 20:34:31 +05:30
if (noImageProcessing) {
2023-09-19 00:00:43 +05:30
logit("🚫 Skipping scaling and converting");
2023-09-10 14:44:04 +05:30
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
COMMAND.UPSCAYL_DONE,
outFile.replace(
/([^/\\]+)$/i,
2024-02-08 20:27:35 +05:30
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),
),
2023-09-10 14:44:04 +05:30
);
2023-09-19 20:44:15 +05:30
return;
2023-09-10 14:44:04 +05:30
}
2023-09-19 20:34:31 +05:30
mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
// Free up memory
upscayl.kill();
try {
mainWindow.setProgressBar(-1);
mainWindow.webContents.send(
COMMAND.UPSCAYL_DONE,
outFile.replace(
/([^/\\]+)$/i,
2024-02-08 20:27:35 +05:30
encodeURIComponent(outFile.match(/[^/\\]+$/i)![0]),
),
2023-09-19 20:34:31 +05:30
);
showNotification("Upscayl", "Image upscayled successfully!");
2023-09-19 20:34:31 +05:30
} catch (error) {
logit(
"❌ Error processing (scaling and converting) the image. Please report this error on GitHub.",
2024-02-08 20:27:35 +05:30
error,
2023-09-19 20:34:31 +05:30
);
upscayl.kill();
mainWindow.webContents.send(
COMMAND.UPSCAYL_ERROR,
2023-10-14 14:11:57 +05:30
"Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +
2024-02-08 20:27:35 +05:30
error,
2023-09-19 20:34:31 +05:30
);
showNotification("Upscayl Failure", "Failed to upscale image!");
2023-09-19 20:34:31 +05:30
}
2023-09-10 14:44:04 +05:30
}
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
}
};
export default imageUpscayl;