2023-03-18 13:15:48 +01:00
|
|
|
import {
|
|
|
|
getBatchArguments,
|
|
|
|
getDoubleUpscaleArguments,
|
|
|
|
getDoubleUpscaleSecondPassArguments,
|
|
|
|
getSingleImageArguments,
|
|
|
|
} from "./utils/getArguments";
|
2022-08-15 06:53:14 +02:00
|
|
|
// Native
|
2022-11-11 21:39:28 +01:00
|
|
|
import { autoUpdater } from "electron-updater";
|
2022-11-11 21:47:00 +01:00
|
|
|
import getPlatform from "./getPlatform";
|
2023-04-20 20:41:22 +02:00
|
|
|
// import ffmpeg from "upscayl-ffmpeg";
|
2023-03-18 13:21:02 +01:00
|
|
|
import { join, parse } from "path";
|
2023-03-31 12:33:48 +02:00
|
|
|
import log from "electron-log";
|
2023-03-18 13:21:02 +01:00
|
|
|
import { format } from "url";
|
|
|
|
import fs from "fs";
|
2023-07-22 15:00:46 +02:00
|
|
|
import Jimp from "jimp";
|
2022-08-15 06:53:14 +02:00
|
|
|
|
2022-11-11 21:39:28 +01:00
|
|
|
import { execPath, modelsPath } from "./binaries";
|
2022-08-15 06:53:14 +02:00
|
|
|
// Packages
|
2022-11-11 21:39:28 +01:00
|
|
|
import {
|
2022-08-17 04:37:50 +02:00
|
|
|
BrowserWindow,
|
|
|
|
app,
|
|
|
|
ipcMain,
|
|
|
|
dialog,
|
2022-08-18 11:53:23 +02:00
|
|
|
shell,
|
2022-11-11 21:39:28 +01:00
|
|
|
MessageBoxOptions,
|
2023-07-22 15:00:46 +02:00
|
|
|
protocol,
|
2022-11-11 21:39:28 +01:00
|
|
|
} from "electron";
|
2022-09-25 13:15:43 +02:00
|
|
|
|
2023-03-18 13:21:02 +01:00
|
|
|
import { spawnUpscayl } from "./upscayl";
|
2022-11-11 21:39:28 +01:00
|
|
|
import prepareNext from "electron-next";
|
2023-03-18 13:21:02 +01:00
|
|
|
import isDev from "electron-is-dev";
|
2022-11-11 21:39:28 +01:00
|
|
|
import commands from "./commands";
|
2023-04-28 20:21:42 +02:00
|
|
|
import { ChildProcessWithoutNullStreams } from "child_process";
|
|
|
|
|
2023-04-30 03:34:25 +02:00
|
|
|
let childProcesses: {
|
|
|
|
process: ChildProcessWithoutNullStreams;
|
|
|
|
kill: () => boolean;
|
|
|
|
}[] = [];
|
2022-08-15 06:53:14 +02:00
|
|
|
|
2023-03-31 12:33:48 +02:00
|
|
|
log.initialize({ preload: true });
|
|
|
|
|
2023-04-23 09:56:49 +02:00
|
|
|
function escapeRegExp(string) {
|
2023-04-27 20:18:44 +02:00
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
|
2023-04-23 09:56:49 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 14:47:51 +02:00
|
|
|
// Path variables for file and folder selection
|
|
|
|
let imagePath: string | undefined = undefined;
|
|
|
|
let folderPath: string | undefined = undefined;
|
|
|
|
let customModelsFolderPath: string | undefined = undefined;
|
|
|
|
let outputFolderPath: string | undefined = undefined;
|
|
|
|
let saveOutputFolder = false;
|
2023-07-23 11:07:18 +02:00
|
|
|
let quality = 100;
|
2023-04-28 14:47:51 +02:00
|
|
|
|
2023-04-30 03:34:25 +02:00
|
|
|
let stopped = false;
|
|
|
|
|
2023-04-29 02:18:19 +02:00
|
|
|
// Slashes for use in directory names
|
|
|
|
const slash: string = getPlatform() === "win" ? "\\" : "/";
|
|
|
|
|
2022-08-15 06:53:14 +02:00
|
|
|
// Prepare the renderer once the app is ready
|
2023-04-09 07:16:15 +02:00
|
|
|
let mainWindow: BrowserWindow;
|
2022-08-15 06:53:14 +02:00
|
|
|
app.on("ready", async () => {
|
|
|
|
await prepareNext("./renderer");
|
|
|
|
|
2023-03-31 12:33:48 +02:00
|
|
|
log.info("🚀 UPSCAYL EXEC PATH: ", execPath(""));
|
|
|
|
log.info("🚀 MODELS PATH: ", modelsPath);
|
2022-09-07 04:31:28 +02:00
|
|
|
|
2022-08-17 13:36:19 +02:00
|
|
|
mainWindow = new BrowserWindow({
|
2022-09-30 18:39:14 +02:00
|
|
|
icon: join(__dirname, "build", "icon.png"),
|
2022-12-22 15:59:11 +01:00
|
|
|
width: 1300,
|
|
|
|
height: 940,
|
2022-08-18 11:53:23 +02:00
|
|
|
minHeight: 500,
|
|
|
|
minWidth: 500,
|
2022-08-28 02:12:17 +02:00
|
|
|
show: false,
|
2022-08-28 01:49:19 +02:00
|
|
|
backgroundColor: "#171717",
|
2022-08-15 06:53:14 +02:00
|
|
|
webPreferences: {
|
2022-08-16 04:17:27 +02:00
|
|
|
nodeIntegration: true,
|
2023-07-22 15:00:46 +02:00
|
|
|
nodeIntegrationInWorker: true,
|
2022-08-18 11:53:23 +02:00
|
|
|
webSecurity: false,
|
2022-08-15 06:53:14 +02:00
|
|
|
preload: join(__dirname, "preload.js"),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const url = isDev
|
|
|
|
? "http://localhost:8000"
|
|
|
|
: format({
|
|
|
|
pathname: join(__dirname, "../renderer/out/index.html"),
|
|
|
|
protocol: "file:",
|
|
|
|
slashes: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
mainWindow.setMenuBarVisibility(false);
|
|
|
|
mainWindow.loadURL(url);
|
2022-08-18 11:53:23 +02:00
|
|
|
|
|
|
|
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
|
|
|
|
shell.openExternal(url);
|
|
|
|
return { action: "deny" };
|
|
|
|
});
|
2022-08-21 17:11:11 +02:00
|
|
|
|
2022-08-30 10:45:06 +02:00
|
|
|
mainWindow.once("ready-to-show", () => {
|
|
|
|
mainWindow.show();
|
2022-09-30 18:36:16 +02:00
|
|
|
mainWindow.webContents.setZoomFactor(1);
|
2022-08-30 10:45:06 +02:00
|
|
|
});
|
2022-08-28 02:12:17 +02:00
|
|
|
|
2023-07-22 15:00:46 +02:00
|
|
|
app.whenReady().then(() => {
|
|
|
|
protocol.registerFileProtocol("file", (request, callback) => {
|
|
|
|
const pathname = decodeURI(request.url.replace("file:///", ""));
|
|
|
|
callback(pathname);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-21 17:11:11 +02:00
|
|
|
if (!isDev) {
|
|
|
|
autoUpdater.checkForUpdates();
|
2022-08-23 05:13:08 +02:00
|
|
|
}
|
2023-04-15 07:15:58 +02:00
|
|
|
|
2023-04-28 14:47:51 +02:00
|
|
|
// GET LAST IMAGE PATH TO LOCAL STORAGE
|
2023-04-22 19:44:16 +02:00
|
|
|
mainWindow.webContents
|
|
|
|
.executeJavaScript('localStorage.getItem("lastImagePath");', true)
|
|
|
|
.then((lastImagePath: string | null) => {
|
|
|
|
if (lastImagePath && lastImagePath.length > 0) {
|
|
|
|
imagePath = lastImagePath;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-28 14:47:51 +02:00
|
|
|
// GET LAST FOLDER PATH TO LOCAL STORAGE
|
2023-04-22 19:44:16 +02:00
|
|
|
mainWindow.webContents
|
|
|
|
.executeJavaScript('localStorage.getItem("lastFolderPath");', true)
|
|
|
|
.then((lastFolderPath: string | null) => {
|
|
|
|
if (lastFolderPath && lastFolderPath.length > 0) {
|
|
|
|
folderPath = lastFolderPath;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-28 14:47:51 +02:00
|
|
|
// GET LAST CUSTOM MODELS FOLDER PATH TO LOCAL STORAGE
|
2023-04-22 19:44:16 +02:00
|
|
|
mainWindow.webContents
|
|
|
|
.executeJavaScript(
|
|
|
|
'localStorage.getItem("lastCustomModelsFolderPath");',
|
|
|
|
true
|
|
|
|
)
|
|
|
|
.then((lastCustomModelsFolderPath: string | null) => {
|
|
|
|
if (lastCustomModelsFolderPath && lastCustomModelsFolderPath.length > 0) {
|
|
|
|
customModelsFolderPath = lastCustomModelsFolderPath;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-28 14:47:51 +02:00
|
|
|
// GET LAST CUSTOM MODELS FOLDER PATH TO LOCAL STORAGE
|
2023-04-22 19:44:16 +02:00
|
|
|
mainWindow.webContents
|
|
|
|
.executeJavaScript('localStorage.getItem("lastOutputFolderPath");', true)
|
|
|
|
.then((lastOutputFolderPath: string | null) => {
|
|
|
|
if (lastOutputFolderPath && lastOutputFolderPath.length > 0) {
|
|
|
|
outputFolderPath = lastOutputFolderPath;
|
|
|
|
}
|
|
|
|
});
|
2023-04-28 14:47:51 +02:00
|
|
|
|
|
|
|
// GET LAST SAVE OUTPUT FOLDER (BOOLEAN) TO LOCAL STORAGE
|
|
|
|
mainWindow.webContents
|
|
|
|
.executeJavaScript('localStorage.getItem("rememberOutputFolder");', true)
|
|
|
|
.then((lastSaveOutputFolder: boolean | null) => {
|
|
|
|
if (lastSaveOutputFolder !== null) {
|
|
|
|
saveOutputFolder = lastSaveOutputFolder;
|
|
|
|
}
|
|
|
|
});
|
2023-07-23 11:07:18 +02:00
|
|
|
|
|
|
|
// GET IMAGE QUALITY (NUMBER) TO LOCAL STORAGE
|
|
|
|
mainWindow.webContents
|
|
|
|
.executeJavaScript('localStorage.getItem("quality");', true)
|
|
|
|
.then((lastSavedQuality: string | null) => {
|
|
|
|
if (lastSavedQuality !== null) {
|
|
|
|
quality = parseInt(lastSavedQuality);
|
|
|
|
}
|
|
|
|
});
|
2022-08-15 06:53:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Quit the app once all windows are closed
|
|
|
|
app.on("window-all-closed", app.quit);
|
|
|
|
|
2023-05-01 08:55:56 +02:00
|
|
|
log.log("🚃 App Path: ", app.getAppPath());
|
2023-03-31 12:33:48 +02:00
|
|
|
|
2023-04-14 12:31:37 +02:00
|
|
|
const logit = (...args: any) => {
|
|
|
|
log.log(...args);
|
|
|
|
mainWindow.webContents.send(commands.LOG, args.join(" "));
|
|
|
|
};
|
|
|
|
|
2023-04-09 08:04:36 +02:00
|
|
|
// Default models
|
|
|
|
const defaultModels = [
|
|
|
|
"realesrgan-x4plus",
|
|
|
|
"remacri",
|
|
|
|
"ultramix_balanced",
|
|
|
|
"ultrasharp",
|
|
|
|
"realesrgan-x4plus-anime",
|
|
|
|
];
|
|
|
|
|
2022-09-25 13:15:43 +02:00
|
|
|
//------------------------Select File-----------------------------//
|
2022-08-15 09:21:12 +02:00
|
|
|
// ! DONT FORGET TO RESTART THE APP WHEN YOU CHANGE CODE HERE
|
2022-08-18 11:53:23 +02:00
|
|
|
ipcMain.handle(commands.SELECT_FILE, async () => {
|
2022-08-16 04:17:27 +02:00
|
|
|
const { canceled, filePaths } = await dialog.showOpenDialog({
|
|
|
|
properties: ["openFile", "multiSelections"],
|
2023-04-09 05:53:01 +02:00
|
|
|
title: "Select Image",
|
|
|
|
defaultPath: imagePath,
|
2022-08-16 04:17:27 +02:00
|
|
|
});
|
|
|
|
|
2022-08-15 12:12:48 +02:00
|
|
|
if (canceled) {
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("🚫 File Operation Cancelled");
|
2023-04-09 07:16:15 +02:00
|
|
|
return null;
|
2022-08-16 04:17:27 +02:00
|
|
|
} else {
|
2023-04-09 05:53:01 +02:00
|
|
|
imagePath = filePaths[0];
|
2023-04-09 07:16:15 +02:00
|
|
|
|
2023-04-15 07:15:58 +02:00
|
|
|
let isValid = false;
|
2023-04-09 07:16:15 +02:00
|
|
|
// READ SELECTED FILES
|
|
|
|
filePaths.forEach((file) => {
|
|
|
|
// log.log("Files in Folder: ", file);
|
|
|
|
if (
|
|
|
|
file.endsWith(".png") ||
|
|
|
|
file.endsWith(".jpg") ||
|
|
|
|
file.endsWith(".jpeg") ||
|
|
|
|
file.endsWith(".webp") ||
|
|
|
|
file.endsWith(".JPG") ||
|
|
|
|
file.endsWith(".PNG") ||
|
|
|
|
file.endsWith(".JPEG") ||
|
|
|
|
file.endsWith(".WEBP")
|
|
|
|
) {
|
|
|
|
isValid = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!isValid) {
|
2023-05-01 08:55:56 +02:00
|
|
|
logit("❌ Invalid File Detected");
|
2023-04-09 07:16:15 +02:00
|
|
|
const options: MessageBoxOptions = {
|
|
|
|
type: "error",
|
|
|
|
title: "Invalid File",
|
|
|
|
message:
|
|
|
|
"The selected file is not a valid image. Make sure you select a '.png', '.jpg', or '.webp' file.",
|
|
|
|
};
|
|
|
|
dialog.showMessageBoxSync(mainWindow, options);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("📄 Selected File Path: ", filePaths[0]);
|
2022-08-29 15:41:59 +02:00
|
|
|
// CREATE input AND upscaled FOLDER
|
2022-08-17 11:18:49 +02:00
|
|
|
return filePaths[0];
|
|
|
|
}
|
2022-08-18 11:53:23 +02:00
|
|
|
});
|
|
|
|
|
2022-09-25 13:15:43 +02:00
|
|
|
//------------------------Select Folder-----------------------------//
|
2022-08-29 15:41:59 +02:00
|
|
|
ipcMain.handle(commands.SELECT_FOLDER, async (event, message) => {
|
2023-04-09 05:53:01 +02:00
|
|
|
const { canceled, filePaths: folderPaths } = await dialog.showOpenDialog({
|
2022-08-29 15:41:59 +02:00
|
|
|
properties: ["openDirectory"],
|
2023-04-09 05:53:01 +02:00
|
|
|
defaultPath: folderPath,
|
2022-08-17 11:18:49 +02:00
|
|
|
});
|
2023-05-01 08:55:56 +02:00
|
|
|
|
2022-08-17 11:18:49 +02:00
|
|
|
if (canceled) {
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("🚫 Select Folder Operation Cancelled");
|
2023-04-09 07:16:15 +02:00
|
|
|
return null;
|
2022-08-18 11:53:23 +02:00
|
|
|
} else {
|
2023-04-09 05:53:01 +02:00
|
|
|
folderPath = folderPaths[0];
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("📁 Selected Folder Path: ", folderPath);
|
2023-04-09 05:53:01 +02:00
|
|
|
return folderPaths[0];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-09 07:16:15 +02:00
|
|
|
//------------------------Get Model Names-----------------------------//
|
|
|
|
const getModels = (folderPath: string) => {
|
|
|
|
let models: string[] = [];
|
|
|
|
let isValid = false;
|
|
|
|
|
|
|
|
// READ CUSTOM MODELS FOLDER
|
|
|
|
fs.readdirSync(folderPath).forEach((file) => {
|
|
|
|
// log.log("Files in Folder: ", file);
|
|
|
|
if (
|
|
|
|
file.endsWith(".param") ||
|
|
|
|
file.endsWith(".PARAM") ||
|
|
|
|
file.endsWith(".bin") ||
|
|
|
|
file.endsWith(".BIN")
|
|
|
|
) {
|
|
|
|
isValid = true;
|
|
|
|
const modelName = file.substring(0, file.lastIndexOf(".")) || file;
|
|
|
|
if (!models.includes(modelName)) {
|
|
|
|
models.push(modelName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!isValid) {
|
2023-05-01 08:55:56 +02:00
|
|
|
logit("❌ Invalid Custom Model Folder Detected");
|
2023-04-09 07:16:15 +02:00
|
|
|
const options: MessageBoxOptions = {
|
|
|
|
type: "error",
|
|
|
|
title: "Invalid Folder",
|
|
|
|
message:
|
|
|
|
"The selected folder does not contain valid model files. Make sure you select the folder that ONLY contains '.param' and '.bin' files.",
|
|
|
|
buttons: ["OK"],
|
|
|
|
};
|
|
|
|
dialog.showMessageBoxSync(options);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("🔎 Detected Custom Models: ", models);
|
2023-04-09 07:16:15 +02:00
|
|
|
return models;
|
|
|
|
};
|
|
|
|
|
2023-04-09 07:48:53 +02:00
|
|
|
ipcMain.on(commands.GET_MODELS_LIST, async (event, payload) => {
|
|
|
|
if (payload) {
|
2023-04-09 08:04:36 +02:00
|
|
|
customModelsFolderPath = payload;
|
2023-05-01 08:55:56 +02:00
|
|
|
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("📁 Custom Models Folder Path: ", customModelsFolderPath);
|
2023-05-01 08:55:56 +02:00
|
|
|
|
2023-04-09 07:48:53 +02:00
|
|
|
mainWindow.webContents.send(
|
|
|
|
commands.CUSTOM_MODEL_FILES_LIST,
|
|
|
|
getModels(payload)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-09 05:53:01 +02:00
|
|
|
//------------------------Select Custom Models Folder---------------------//
|
|
|
|
ipcMain.handle(commands.SELECT_CUSTOM_MODEL_FOLDER, async (event, message) => {
|
|
|
|
const { canceled, filePaths: folderPaths } = await dialog.showOpenDialog({
|
|
|
|
properties: ["openDirectory"],
|
|
|
|
title: "Select Custom Models Folder",
|
|
|
|
defaultPath: customModelsFolderPath,
|
|
|
|
});
|
|
|
|
if (canceled) {
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("🚫 Select Custom Models Folder Operation Cancelled");
|
2023-04-09 07:16:15 +02:00
|
|
|
return null;
|
2023-04-09 05:53:01 +02:00
|
|
|
} else {
|
|
|
|
customModelsFolderPath = folderPaths[0];
|
2023-04-09 07:16:15 +02:00
|
|
|
|
2023-04-14 12:11:38 +02:00
|
|
|
if (
|
2023-05-09 21:43:05 +02:00
|
|
|
!folderPaths[0].endsWith(slash + "models") &&
|
|
|
|
!folderPaths[0].endsWith(slash + "models" + slash)
|
2023-04-14 12:11:38 +02:00
|
|
|
) {
|
2023-05-01 08:55:56 +02:00
|
|
|
logit("❌ Invalid Custom Models Folder Detected: Not a 'models' folder");
|
2023-04-14 12:11:38 +02:00
|
|
|
const options: MessageBoxOptions = {
|
|
|
|
type: "error",
|
|
|
|
title: "Invalid Folder",
|
|
|
|
message:
|
|
|
|
"Please make sure that the folder name is 'models' and nothing else.",
|
|
|
|
buttons: ["OK"],
|
|
|
|
};
|
|
|
|
dialog.showMessageBoxSync(options);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-04-09 07:16:15 +02:00
|
|
|
mainWindow.webContents.send(
|
|
|
|
commands.CUSTOM_MODEL_FILES_LIST,
|
|
|
|
getModels(customModelsFolderPath)
|
|
|
|
);
|
|
|
|
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("📁 Custom Folder Path: ", customModelsFolderPath);
|
2023-04-09 07:16:15 +02:00
|
|
|
return customModelsFolderPath;
|
2022-08-17 11:18:49 +02:00
|
|
|
}
|
2022-08-18 11:53:23 +02:00
|
|
|
});
|
2022-08-16 04:17:27 +02:00
|
|
|
|
2022-12-02 15:21:42 +01:00
|
|
|
//------------------------Open Folder-----------------------------//
|
|
|
|
ipcMain.on(commands.OPEN_FOLDER, async (event, payload) => {
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("📂 Opening Folder: ", payload);
|
2022-12-02 15:21:42 +01:00
|
|
|
shell.openPath(payload);
|
|
|
|
});
|
|
|
|
|
2023-04-30 03:34:25 +02:00
|
|
|
//------------------------Stop Command-----------------------------//
|
2023-04-28 20:21:42 +02:00
|
|
|
ipcMain.on(commands.STOP, async (event, payload) => {
|
2023-04-30 03:34:25 +02:00
|
|
|
stopped = true;
|
|
|
|
|
|
|
|
childProcesses.forEach((child) => {
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("🛑 Stopping Upscaling Process", child.process.pid);
|
2023-04-28 20:21:42 +02:00
|
|
|
child.kill();
|
2023-04-30 03:34:25 +02:00
|
|
|
});
|
2023-04-28 20:21:42 +02:00
|
|
|
});
|
|
|
|
|
2023-08-05 15:27:58 +02:00
|
|
|
//------------------------Image Upscayl-----------------------------//
|
|
|
|
ipcMain.on(commands.UPSCAYL, async (event, payload) => {
|
|
|
|
const model = payload.model as string;
|
|
|
|
const gpuId = payload.gpuId as string;
|
|
|
|
const saveImageAs = payload.saveImageAs as string;
|
|
|
|
|
|
|
|
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)[1] || "") as string;
|
|
|
|
let outputDir = folderPath || (payload.outputPath as string);
|
|
|
|
|
|
|
|
if (saveOutputFolder === true && outputFolderPath) {
|
|
|
|
outputDir = outputFolderPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isDefaultModel = defaultModels.includes(model);
|
|
|
|
|
|
|
|
const fullfileName = payload.imagePath.replace(/^.*[\\\/]/, "") as string;
|
|
|
|
const fileName = parse(fullfileName).name;
|
|
|
|
const fileExt = parse(fullfileName).ext;
|
|
|
|
|
|
|
|
let scale = "4";
|
|
|
|
if (model.includes("x2")) {
|
|
|
|
scale = "2";
|
|
|
|
} else if (model.includes("x3")) {
|
|
|
|
scale = "3";
|
|
|
|
} else {
|
|
|
|
scale = "4";
|
|
|
|
}
|
|
|
|
|
|
|
|
const outFile =
|
|
|
|
outputDir +
|
|
|
|
slash +
|
|
|
|
fileName +
|
|
|
|
"_upscayl_" +
|
|
|
|
payload.scale +
|
|
|
|
"x_" +
|
|
|
|
model +
|
|
|
|
"." +
|
|
|
|
saveImageAs;
|
|
|
|
|
|
|
|
// UPSCALE
|
|
|
|
if (fs.existsSync(outFile)) {
|
|
|
|
// If already upscayled, just output that file
|
|
|
|
logit("✅ Already upscayled at: ", outFile);
|
|
|
|
mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile);
|
|
|
|
} else {
|
|
|
|
const upscayl = spawnUpscayl(
|
|
|
|
"realesrgan",
|
|
|
|
getSingleImageArguments(
|
|
|
|
inputDir,
|
|
|
|
fullfileName,
|
|
|
|
outFile,
|
|
|
|
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
|
|
|
|
model,
|
|
|
|
scale,
|
|
|
|
gpuId,
|
|
|
|
saveImageAs
|
|
|
|
),
|
|
|
|
logit
|
|
|
|
);
|
|
|
|
|
|
|
|
childProcesses.push(upscayl);
|
|
|
|
|
|
|
|
stopped = false;
|
|
|
|
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(commands.UPSCAYL_PROGRESS, data.toString());
|
|
|
|
if (data.includes("invalid gpu") || data.includes("failed")) {
|
|
|
|
logit("❌ INVALID GPU OR FAILED");
|
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
if (data.includes("has alpha channel")) {
|
|
|
|
logit("📢 INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!");
|
|
|
|
isAlpha = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const onError = (data) => {
|
|
|
|
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString());
|
|
|
|
failed = true;
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
const onClose = async () => {
|
|
|
|
if (!failed && !stopped) {
|
|
|
|
logit("💯 Done upscaling");
|
|
|
|
logit("♻ Scaling and converting now...");
|
|
|
|
const originalImage = await Jimp.read(inputDir + slash + fullfileName);
|
|
|
|
try {
|
|
|
|
const newImage = await Jimp.read(
|
|
|
|
isAlpha ? outFile + ".png" : outFile
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
newImage
|
|
|
|
.scaleToFit(
|
|
|
|
originalImage.getWidth() * parseInt(payload.scale),
|
|
|
|
originalImage.getHeight() * parseInt(payload.scale)
|
|
|
|
)
|
|
|
|
.quality(100 - quality)
|
|
|
|
.write(isAlpha ? outFile + ".png" : outFile);
|
|
|
|
mainWindow.setProgressBar(-1);
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
commands.UPSCAYL_DONE,
|
|
|
|
isAlpha ? outFile + ".png" : outFile
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
logit("❌ Error converting to PNG: ", error);
|
|
|
|
onError(error);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logit("❌ Error reading original image metadata", error);
|
|
|
|
onError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
upscayl.process.stderr.on("data", onData);
|
|
|
|
upscayl.process.on("error", onError);
|
|
|
|
upscayl.process.on("close", onClose);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-18 10:08:55 +02:00
|
|
|
//------------------------Double Upscayl-----------------------------//
|
2022-09-17 11:40:47 +02:00
|
|
|
ipcMain.on(commands.DOUBLE_UPSCAYL, async (event, payload) => {
|
2023-03-18 13:15:48 +01:00
|
|
|
const model = payload.model as string;
|
|
|
|
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)[1] || "") as string;
|
|
|
|
let outputDir = payload.outputPath as string;
|
2023-04-28 14:50:54 +02:00
|
|
|
|
|
|
|
if (saveOutputFolder === true && outputFolderPath) {
|
|
|
|
outputDir = outputFolderPath;
|
|
|
|
}
|
2023-03-18 13:15:48 +01:00
|
|
|
const gpuId = payload.gpuId as string;
|
|
|
|
const saveImageAs = payload.saveImageAs as string;
|
2022-09-10 16:52:53 +02:00
|
|
|
|
2023-04-09 08:04:36 +02:00
|
|
|
const isDefaultModel = defaultModels.includes(model);
|
|
|
|
|
2022-09-10 16:52:53 +02:00
|
|
|
// COPY IMAGE TO TMP FOLDER
|
2023-04-30 03:34:25 +02:00
|
|
|
|
|
|
|
const fullfileName = payload.imagePath.split(slash).slice(-1)[0] as string;
|
2022-09-10 16:52:53 +02:00
|
|
|
const fileName = parse(fullfileName).name;
|
2022-12-16 17:20:46 +01:00
|
|
|
const outFile =
|
2023-04-29 02:18:19 +02:00
|
|
|
outputDir + slash + fileName + "_upscayl_16x_" + model + "." + saveImageAs;
|
2022-09-10 16:52:53 +02:00
|
|
|
|
2023-08-05 15:27:58 +02:00
|
|
|
let scale = "4";
|
|
|
|
if (model.includes("x2")) {
|
|
|
|
scale = "2";
|
|
|
|
} else if (model.includes("x3")) {
|
|
|
|
scale = "3";
|
|
|
|
} else {
|
|
|
|
scale = "4";
|
|
|
|
}
|
|
|
|
|
2022-09-17 11:40:47 +02:00
|
|
|
// UPSCALE
|
2023-03-18 12:58:38 +01:00
|
|
|
let upscayl = spawnUpscayl(
|
|
|
|
"realesrgan",
|
2023-03-18 13:15:48 +01:00
|
|
|
getDoubleUpscaleArguments(
|
2023-03-18 12:58:38 +01:00
|
|
|
inputDir,
|
|
|
|
fullfileName,
|
2023-03-18 13:15:48 +01:00
|
|
|
outFile,
|
2023-04-09 08:04:36 +02:00
|
|
|
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
|
2022-09-17 11:40:47 +02:00
|
|
|
model,
|
2023-03-18 12:58:38 +01:00
|
|
|
gpuId,
|
2023-04-14 12:31:37 +02:00
|
|
|
saveImageAs,
|
|
|
|
scale
|
2023-05-01 08:55:56 +02:00
|
|
|
),
|
|
|
|
logit
|
2022-12-02 15:21:42 +01:00
|
|
|
);
|
|
|
|
|
2023-04-30 03:34:25 +02:00
|
|
|
childProcesses.push(upscayl);
|
|
|
|
|
|
|
|
stopped = false;
|
2022-09-11 12:27:47 +02:00
|
|
|
let failed = false;
|
2022-12-27 07:45:16 +01:00
|
|
|
let isAlpha = false;
|
2023-03-18 12:58:38 +01:00
|
|
|
let failed2 = false;
|
2022-12-27 07:45:16 +01:00
|
|
|
|
2023-03-18 12:58:38 +01:00
|
|
|
const onData = (data) => {
|
2022-09-17 11:40:47 +02:00
|
|
|
// CONVERT DATA TO STRING
|
2022-09-11 12:27:47 +02:00
|
|
|
data = data.toString();
|
2022-09-17 11:40:47 +02:00
|
|
|
// SEND UPSCAYL PROGRESS TO RENDERER
|
|
|
|
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
|
|
|
|
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
|
2022-09-11 12:27:47 +02:00
|
|
|
if (data.includes("invalid gpu") || data.includes("failed")) {
|
|
|
|
failed = true;
|
|
|
|
}
|
2022-12-27 07:45:16 +01:00
|
|
|
if (data.includes("has alpha channel")) {
|
|
|
|
isAlpha = true;
|
|
|
|
}
|
2023-03-18 12:58:38 +01:00
|
|
|
};
|
2023-04-30 03:34:25 +02:00
|
|
|
|
2023-03-18 12:58:38 +01:00
|
|
|
const onError = (data) => {
|
2022-09-17 11:40:47 +02:00
|
|
|
data.toString();
|
|
|
|
// SEND UPSCAYL PROGRESS TO RENDERER
|
|
|
|
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
|
|
|
|
// SET FAILED TO TRUE
|
|
|
|
failed = true;
|
|
|
|
return;
|
2023-03-18 12:58:38 +01:00
|
|
|
};
|
2023-04-30 03:34:25 +02:00
|
|
|
|
2023-03-18 12:58:38 +01:00
|
|
|
const onData2 = (data) => {
|
|
|
|
// CONVERT DATA TO STRING
|
|
|
|
data = data.toString();
|
|
|
|
// SEND UPSCAYL PROGRESS TO RENDERER
|
|
|
|
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
|
|
|
|
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
|
|
|
|
if (data.includes("invalid gpu") || data.includes("failed")) {
|
|
|
|
failed2 = true;
|
|
|
|
}
|
|
|
|
};
|
2023-04-30 03:34:25 +02:00
|
|
|
|
2023-03-18 12:58:38 +01:00
|
|
|
const onError2 = (data) => {
|
|
|
|
data.toString();
|
|
|
|
// SEND UPSCAYL PROGRESS TO RENDERER
|
|
|
|
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
|
|
|
|
// SET FAILED TO TRUE
|
|
|
|
failed2 = true;
|
|
|
|
return;
|
|
|
|
};
|
2023-04-30 03:34:25 +02:00
|
|
|
|
2023-07-23 11:07:18 +02:00
|
|
|
const onClose2 = async (code) => {
|
2023-04-30 03:34:25 +02:00
|
|
|
if (!failed2 && !stopped) {
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("💯 Done upscaling");
|
2023-07-23 11:07:18 +02:00
|
|
|
logit("♻ Scaling and converting now...");
|
|
|
|
const originalImage = await Jimp.read(inputDir + slash + fullfileName);
|
|
|
|
try {
|
|
|
|
const newImage = await Jimp.read(isAlpha ? outFile + ".png" : outFile);
|
|
|
|
try {
|
|
|
|
newImage
|
|
|
|
.scaleToFit(
|
|
|
|
originalImage.getWidth() * parseInt(payload.scale),
|
|
|
|
originalImage.getHeight() * parseInt(payload.scale)
|
|
|
|
)
|
|
|
|
.quality(100 - quality)
|
|
|
|
.write(isAlpha ? outFile + ".png" : outFile);
|
|
|
|
mainWindow.setProgressBar(-1);
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
commands.DOUBLE_UPSCAYL_DONE,
|
|
|
|
isAlpha ? outFile + ".png" : outFile
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
logit("❌ Error converting to PNG: ", error);
|
|
|
|
onError(error);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logit("❌ Error reading original image metadata", error);
|
|
|
|
onError(error);
|
|
|
|
}
|
2023-03-18 12:58:38 +01:00
|
|
|
}
|
|
|
|
};
|
2022-09-17 11:40:47 +02:00
|
|
|
|
2023-03-18 12:58:38 +01:00
|
|
|
upscayl.process.stderr.on("data", onData);
|
|
|
|
upscayl.process.on("error", onError);
|
|
|
|
upscayl.process.on("close", (code) => {
|
2022-09-17 11:40:47 +02:00
|
|
|
// IF NOT FAILED
|
2023-04-30 03:34:25 +02:00
|
|
|
if (!failed && !stopped) {
|
2022-09-17 11:40:47 +02:00
|
|
|
// UPSCALE
|
2023-03-18 12:58:38 +01:00
|
|
|
let upscayl2 = spawnUpscayl(
|
|
|
|
"realesrgan",
|
2023-03-18 13:15:48 +01:00
|
|
|
getDoubleUpscaleSecondPassArguments(
|
|
|
|
isAlpha,
|
2023-03-18 12:58:38 +01:00
|
|
|
outFile,
|
2023-04-09 08:04:36 +02:00
|
|
|
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
|
2022-12-16 17:20:46 +01:00
|
|
|
model,
|
2023-03-18 12:58:38 +01:00
|
|
|
gpuId,
|
2023-04-14 12:31:37 +02:00
|
|
|
saveImageAs,
|
|
|
|
scale
|
2023-05-01 08:55:56 +02:00
|
|
|
),
|
|
|
|
logit
|
2022-09-12 21:24:04 +02:00
|
|
|
);
|
2022-09-17 11:40:47 +02:00
|
|
|
|
2023-04-30 03:34:25 +02:00
|
|
|
childProcesses.push(upscayl2);
|
2023-04-28 20:21:42 +02:00
|
|
|
|
2023-03-18 12:58:38 +01:00
|
|
|
upscayl2.process.stderr.on("data", onData2);
|
|
|
|
upscayl2.process.on("error", onError2);
|
|
|
|
upscayl2.process.on("close", onClose2);
|
2022-09-11 12:27:47 +02:00
|
|
|
}
|
2022-09-17 11:40:47 +02:00
|
|
|
});
|
2022-09-10 16:52:53 +02:00
|
|
|
});
|
|
|
|
|
2022-09-18 10:08:55 +02:00
|
|
|
//------------------------Upscayl Folder-----------------------------//
|
|
|
|
ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
|
2022-12-02 15:21:42 +01:00
|
|
|
// GET THE MODEL
|
2022-09-18 10:08:55 +02:00
|
|
|
const model = payload.model;
|
2022-12-16 17:20:46 +01:00
|
|
|
const gpuId = payload.gpuId;
|
|
|
|
const saveImageAs = payload.saveImageAs;
|
2023-04-14 12:31:37 +02:00
|
|
|
const scale = payload.scale as string;
|
2022-12-02 15:21:42 +01:00
|
|
|
|
|
|
|
// GET THE IMAGE DIRECTORY
|
2022-09-18 14:17:49 +02:00
|
|
|
let inputDir = payload.batchFolderPath;
|
2022-12-02 15:21:42 +01:00
|
|
|
// GET THE OUTPUT DIRECTORY
|
2023-03-18 13:33:17 +01:00
|
|
|
let outputDir = payload.outputPath;
|
2022-12-02 15:21:42 +01:00
|
|
|
|
2023-04-28 14:50:54 +02:00
|
|
|
if (saveOutputFolder === true && outputFolderPath) {
|
|
|
|
outputDir = outputFolderPath;
|
|
|
|
}
|
|
|
|
|
2022-09-18 14:17:49 +02:00
|
|
|
if (!fs.existsSync(outputDir)) {
|
|
|
|
fs.mkdirSync(outputDir, { recursive: true });
|
|
|
|
}
|
2023-04-09 08:04:36 +02:00
|
|
|
|
|
|
|
const isDefaultModel = defaultModels.includes(model);
|
|
|
|
|
2022-09-18 10:08:55 +02:00
|
|
|
// UPSCALE
|
2023-03-18 13:33:17 +01:00
|
|
|
const upscayl = spawnUpscayl(
|
|
|
|
"realesrgan",
|
|
|
|
getBatchArguments(
|
|
|
|
inputDir,
|
|
|
|
outputDir,
|
2023-04-09 08:04:36 +02:00
|
|
|
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
|
2023-03-18 13:33:17 +01:00
|
|
|
model,
|
|
|
|
gpuId,
|
2023-04-14 12:31:37 +02:00
|
|
|
saveImageAs,
|
|
|
|
scale
|
2023-05-01 08:55:56 +02:00
|
|
|
),
|
|
|
|
logit
|
2023-03-18 13:33:17 +01:00
|
|
|
);
|
2022-09-18 10:08:55 +02:00
|
|
|
|
2023-04-30 03:34:25 +02:00
|
|
|
childProcesses.push(upscayl);
|
2023-04-28 20:21:42 +02:00
|
|
|
|
2023-04-30 03:34:25 +02:00
|
|
|
stopped = false;
|
2022-09-18 10:08:55 +02:00
|
|
|
let failed = false;
|
2023-04-30 03:34:25 +02:00
|
|
|
|
2023-03-18 13:33:17 +01:00
|
|
|
const onData = (data: any) => {
|
2022-09-18 10:08:55 +02:00
|
|
|
data = data.toString();
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
commands.FOLDER_UPSCAYL_PROGRESS,
|
|
|
|
data.toString()
|
|
|
|
);
|
|
|
|
if (data.includes("invalid gpu") || data.includes("failed")) {
|
2023-07-23 11:07:18 +02:00
|
|
|
logit("❌ INVALID GPU OR INVALID FILES IN FOLDER - FAILED");
|
2022-09-18 10:08:55 +02:00
|
|
|
failed = true;
|
2023-07-23 11:07:18 +02:00
|
|
|
upscayl.kill();
|
2022-09-18 10:08:55 +02:00
|
|
|
}
|
2023-03-18 12:58:38 +01:00
|
|
|
};
|
2023-03-18 13:33:17 +01:00
|
|
|
const onError = (data: any) => {
|
2022-09-18 10:08:55 +02:00
|
|
|
mainWindow.webContents.send(
|
|
|
|
commands.FOLDER_UPSCAYL_PROGRESS,
|
|
|
|
data.toString()
|
|
|
|
);
|
|
|
|
failed = true;
|
2023-07-23 11:07:18 +02:00
|
|
|
upscayl.kill();
|
2022-09-18 10:08:55 +02:00
|
|
|
return;
|
2023-03-18 12:58:38 +01:00
|
|
|
};
|
2023-03-18 13:17:06 +01:00
|
|
|
const onClose = () => {
|
2023-04-30 03:34:25 +02:00
|
|
|
if (!failed && !stopped) {
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("💯 Done upscaling");
|
2022-09-18 14:17:49 +02:00
|
|
|
mainWindow.webContents.send(commands.FOLDER_UPSCAYL_DONE, outputDir);
|
2023-07-23 11:07:18 +02:00
|
|
|
} else {
|
|
|
|
upscayl.kill();
|
2022-09-18 10:08:55 +02:00
|
|
|
}
|
2023-03-18 12:58:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
upscayl.process.stderr.on("data", onData);
|
|
|
|
upscayl.process.on("error", onError);
|
|
|
|
upscayl.process.on("close", onClose);
|
2022-09-18 10:08:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
//------------------------Auto-Update Code-----------------------------//
|
2023-06-04 09:52:18 +02:00
|
|
|
autoUpdater.autoInstallOnAppQuit = false;
|
|
|
|
|
2022-11-11 21:39:28 +01:00
|
|
|
autoUpdater.on("update-downloaded", (event) => {
|
2023-06-04 09:52:18 +02:00
|
|
|
autoUpdater.autoInstallOnAppQuit = false;
|
2022-11-11 21:39:28 +01:00
|
|
|
const dialogOpts: MessageBoxOptions = {
|
2022-08-23 05:13:08 +02:00
|
|
|
type: "info",
|
2023-06-04 09:52:18 +02:00
|
|
|
buttons: ["Install update", "No Thanks"],
|
2023-03-18 13:20:32 +01:00
|
|
|
title: "New Upscayl Update",
|
2023-03-18 13:17:06 +01:00
|
|
|
message: event.releaseName as string,
|
2022-08-23 05:13:08 +02:00
|
|
|
detail:
|
|
|
|
"A new version has been downloaded. Restart the application to apply the updates.",
|
2022-08-21 17:11:11 +02:00
|
|
|
};
|
2023-05-06 07:17:35 +02:00
|
|
|
logit("✅ Update Downloaded");
|
2022-08-21 17:11:11 +02:00
|
|
|
dialog.showMessageBox(dialogOpts).then((returnValue) => {
|
2023-06-04 09:52:18 +02:00
|
|
|
if (returnValue.response === 0) {
|
|
|
|
autoUpdater.quitAndInstall();
|
|
|
|
} else {
|
|
|
|
logit("🚫 Update Installation Cancelled");
|
|
|
|
}
|
2022-08-23 05:13:08 +02:00
|
|
|
});
|
2022-08-21 17:11:11 +02:00
|
|
|
});
|