1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-11-27 17:00:52 +01:00

Revert Refactor

This commit is contained in:
Nayam Amarshe 2023-08-10 15:14:44 +05:30
parent 28114efdc6
commit f43b7e53db
17 changed files with 793 additions and 720 deletions

View File

@ -1,7 +1,7 @@
// Native
import { autoUpdater } from "electron-updater";
import getPlatform from "./getPlatform";
import { join } from "path";
import { join, parse } from "path";
import log from "electron-log";
import { format } from "url";
import fs from "fs";
@ -22,13 +22,14 @@ import prepareNext from "electron-next";
import isDev from "electron-is-dev";
import commands from "./commands";
import { ChildProcessWithoutNullStreams } from "child_process";
import doubleUpscayl from "./utils/listener/doubleUpscayl";
import folderUpscayl from "./utils/listener/folderUpscayl";
import imageUpscayl from "./utils/listener/imageUpscayl";
import customModelsSelect from "./utils/listener/customModelsSelect";
import getModelsList from "./utils/listener/getModelsList";
import selectFile from "./utils/listener/selectFile";
import selectFolder from "./utils/listener/selectFolder";
import {
getBatchArguments,
getDoubleUpscaleArguments,
getDoubleUpscaleSecondPassArguments,
getSingleImageArguments,
} from "./utils/getArguments";
import { spawnUpscayl } from "./upscayl";
import Jimp from "jimp";
let childProcesses: {
process: ChildProcessWithoutNullStreams;
@ -241,68 +242,501 @@ ipcMain.on(commands.STOP, async (event, payload) => {
});
});
if (mainWindow) {
selectFolder({
folderPath,
logit,
//------------------------Select Folder-----------------------------//
ipcMain.handle(commands.SELECT_FOLDER, async (event, message) => {
const { canceled, filePaths: folderPaths } = await dialog.showOpenDialog({
properties: ["openDirectory"],
defaultPath: folderPath,
});
selectFile({
mainWindow,
imagePath,
logit,
if (canceled) {
logit("🚫 Select Folder Operation Cancelled");
return null;
} else {
folderPath = folderPaths[0];
logit("📁 Selected Folder Path: ", folderPath);
return folderPaths[0];
}
});
//------------------------Select File-----------------------------//
ipcMain.handle(commands.SELECT_FILE, async () => {
if (!mainWindow) return;
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ["openFile", "multiSelections"],
title: "Select Image",
defaultPath: imagePath,
});
getModelsList({
mainWindow,
customModelsFolderPath,
logit,
getModels,
if (canceled) {
logit("🚫 File Operation Cancelled");
return null;
} else {
imagePath = filePaths[0];
let isValid = false;
// 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) {
logit("❌ Invalid File Detected");
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;
}
logit("📄 Selected File Path: ", filePaths[0]);
// CREATE input AND upscaled FOLDER
return filePaths[0];
}
});
//------------------------Get Models List-----------------------------//
ipcMain.on(commands.GET_MODELS_LIST, async (event, payload) => {
if (!mainWindow) return;
if (payload) {
customModelsFolderPath = payload;
logit("📁 Custom Models Folder Path: ", customModelsFolderPath);
mainWindow.webContents.send(
commands.CUSTOM_MODEL_FILES_LIST,
getModels(payload)
);
}
});
//------------------------Custom Models Select-----------------------------//
ipcMain.handle(commands.SELECT_CUSTOM_MODEL_FOLDER, async (event, message) => {
if (!mainWindow) return;
const { canceled, filePaths: folderPaths } = await dialog.showOpenDialog({
properties: ["openDirectory"],
title: "Select Custom Models Folder",
defaultPath: customModelsFolderPath,
});
customModelsSelect({
mainWindow,
customModelsFolderPath,
logit,
slash,
getModels,
if (canceled) {
logit("🚫 Select Custom Models Folder Operation Cancelled");
return null;
} else {
customModelsFolderPath = folderPaths[0];
if (
!folderPaths[0].endsWith(slash + "models") &&
!folderPaths[0].endsWith(slash + "models" + slash)
) {
logit("❌ Invalid Custom Models Folder Detected: Not a 'models' folder");
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;
}
mainWindow.webContents.send(
commands.CUSTOM_MODEL_FILES_LIST,
getModels(customModelsFolderPath)
);
logit("📁 Custom Folder Path: ", customModelsFolderPath);
return customModelsFolderPath;
}
});
//------------------------Image Upscayl-----------------------------//
ipcMain.on(commands.UPSCAYL, async (event, payload) => {
if (!mainWindow) return;
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) => {
if (!mainWindow) return;
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) => {
if (!mainWindow) return;
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 {
if (!mainWindow) return;
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);
}
});
//------------------------Folder Upscayl-----------------------------//
ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
if (!mainWindow) return;
// GET THE MODEL
const model = payload.model;
const gpuId = payload.gpuId;
const saveImageAs = payload.saveImageAs;
const scale = payload.scale as string;
// GET THE IMAGE DIRECTORY
let inputDir = payload.batchFolderPath;
// GET THE OUTPUT DIRECTORY
let outputDir = payload.outputPath;
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
}
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const isDefaultModel = defaultModels.includes(model);
// UPSCALE
const upscayl = spawnUpscayl(
"realesrgan",
getBatchArguments(
inputDir,
outputDir,
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
model,
gpuId,
saveImageAs,
scale
),
logit
);
childProcesses.push(upscayl);
stopped = false;
let failed = false;
const onData = (data: any) => {
if (!mainWindow) return;
data = data.toString();
mainWindow.webContents.send(
commands.FOLDER_UPSCAYL_PROGRESS,
data.toString()
);
if (data.includes("invalid gpu") || data.includes("failed")) {
logit("❌ INVALID GPU OR INVALID FILES IN FOLDER - FAILED");
failed = true;
upscayl.kill();
}
};
const onError = (data: any) => {
if (!mainWindow) return;
mainWindow.webContents.send(
commands.FOLDER_UPSCAYL_PROGRESS,
data.toString()
);
failed = true;
upscayl.kill();
return;
};
const onClose = () => {
if (!mainWindow) return;
if (!failed && !stopped) {
logit("💯 Done upscaling");
mainWindow.webContents.send(commands.FOLDER_UPSCAYL_DONE, outputDir);
} else {
upscayl.kill();
}
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
});
//------------------------Double Upscayl-----------------------------//
ipcMain.on(commands.DOUBLE_UPSCAYL, async (event, payload) => {
if (!mainWindow) return;
const model = payload.model as string;
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)[1] || "") as string;
let outputDir = payload.outputPath as string;
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
}
const gpuId = payload.gpuId as string;
const saveImageAs = payload.saveImageAs as string;
const isDefaultModel = defaultModels.includes(model);
// COPY IMAGE TO TMP FOLDER
const fullfileName = payload.imagePath.split(slash).slice(-1)[0] as string;
const fileName = parse(fullfileName).name;
const outFile =
outputDir + slash + fileName + "_upscayl_16x_" + model + "." + saveImageAs;
let scale = "4";
if (model.includes("x2")) {
scale = "2";
} else if (model.includes("x3")) {
scale = "3";
} else {
scale = "4";
}
// UPSCALE
let upscayl = spawnUpscayl(
"realesrgan",
getDoubleUpscaleArguments(
inputDir,
fullfileName,
outFile,
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
model,
gpuId,
saveImageAs,
scale
),
logit
);
childProcesses.push(upscayl);
stopped = 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(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
if (data.includes("invalid gpu") || data.includes("failed")) {
failed = true;
}
if (data.includes("has alpha channel")) {
isAlpha = true;
}
};
const onError = (data) => {
if (!mainWindow) return;
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed = true;
return;
};
const onData2 = (data) => {
if (!mainWindow) return;
// 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;
}
};
const onError2 = (data) => {
if (!mainWindow) return;
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed2 = true;
return;
};
const onClose2 = async (code) => {
if (!mainWindow) return;
if (!failed2 && !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.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);
}
}
};
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,
scale
),
logit
);
childProcesses.push(upscayl2);
upscayl2.process.stderr.on("data", onData2);
upscayl2.process.on("error", onError2);
upscayl2.process.on("close", onClose2);
}
});
imageUpscayl({
mainWindow,
slash,
logit,
childProcesses,
stopped,
modelsPath,
customModelsFolderPath,
saveOutputFolder,
outputFolderPath,
quality,
defaultModels,
folderPath,
});
folderUpscayl({
mainWindow,
logit,
childProcesses,
stopped,
modelsPath,
customModelsFolderPath,
saveOutputFolder,
outputFolderPath,
defaultModels,
});
doubleUpscayl({
mainWindow,
slash,
logit,
childProcesses,
stopped,
modelsPath,
customModelsFolderPath,
saveOutputFolder,
outputFolderPath,
quality,
defaultModels,
});
}
});
//------------------------Auto-Update Code-----------------------------//
autoUpdater.autoInstallOnAppQuit = false;

View File

@ -1,61 +0,0 @@
import { MessageBoxOptions, dialog, ipcMain } from "electron";
import commands from "../../commands";
export type CustomModelsSelectProps = {
mainWindow: Electron.BrowserWindow;
customModelsFolderPath: string | undefined;
logit: (message: string, ...optionalParams: any[]) => void;
slash: string;
getModels: (folderPath: string) => string[] | null;
};
export default function ({
mainWindow,
customModelsFolderPath,
logit,
slash,
getModels,
}: CustomModelsSelectProps) {
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) {
logit("🚫 Select Custom Models Folder Operation Cancelled");
return null;
} else {
customModelsFolderPath = folderPaths[0];
if (
!folderPaths[0].endsWith(slash + "models") &&
!folderPaths[0].endsWith(slash + "models" + slash)
) {
logit(
"❌ Invalid Custom Models Folder Detected: Not a 'models' folder"
);
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;
}
mainWindow.webContents.send(
commands.CUSTOM_MODEL_FILES_LIST,
getModels(customModelsFolderPath)
);
logit("📁 Custom Folder Path: ", customModelsFolderPath);
return customModelsFolderPath;
}
}
);
}

View File

@ -1,198 +0,0 @@
import { ipcMain } from "electron";
import commands from "../../commands";
import { parse } from "path";
import { spawnUpscayl } from "../../upscayl";
import {
getDoubleUpscaleArguments,
getDoubleUpscaleSecondPassArguments,
} from "../getArguments";
import Jimp from "jimp";
export default function ({
mainWindow,
slash,
logit,
childProcesses,
stopped,
modelsPath,
customModelsFolderPath,
saveOutputFolder,
outputFolderPath,
quality,
defaultModels,
}: {
mainWindow: Electron.BrowserWindow;
slash: string;
logit: (message: string, ...optionalParams: any[]) => void;
childProcesses: any[];
stopped: boolean;
modelsPath: string;
customModelsFolderPath: string | undefined;
saveOutputFolder: boolean;
outputFolderPath: string | undefined;
quality: number;
defaultModels: string[];
}) {
ipcMain.on(commands.DOUBLE_UPSCAYL, async (event, payload) => {
const model = payload.model as string;
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)[1] || "") as string;
let outputDir = payload.outputPath as string;
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
}
const gpuId = payload.gpuId as string;
const saveImageAs = payload.saveImageAs as string;
const isDefaultModel = defaultModels.includes(model);
// COPY IMAGE TO TMP FOLDER
const fullfileName = payload.imagePath.split(slash).slice(-1)[0] as string;
const fileName = parse(fullfileName).name;
const outFile =
outputDir +
slash +
fileName +
"_upscayl_16x_" +
model +
"." +
saveImageAs;
let scale = "4";
if (model.includes("x2")) {
scale = "2";
} else if (model.includes("x3")) {
scale = "3";
} else {
scale = "4";
}
// UPSCALE
let upscayl = spawnUpscayl(
"realesrgan",
getDoubleUpscaleArguments(
inputDir,
fullfileName,
outFile,
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
model,
gpuId,
saveImageAs,
scale
),
logit
);
childProcesses.push(upscayl);
stopped = false;
let failed = false;
let isAlpha = false;
let failed2 = false;
const onData = (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")) {
failed = true;
}
if (data.includes("has alpha channel")) {
isAlpha = true;
}
};
const onError = (data) => {
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed = true;
return;
};
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;
}
};
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;
};
const onClose2 = async (code) => {
if (!failed2 && !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.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);
}
}
};
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,
scale
),
logit
);
childProcesses.push(upscayl2);
upscayl2.process.stderr.on("data", onData2);
upscayl2.process.on("error", onError2);
upscayl2.process.on("close", onClose2);
}
});
});
}

View File

@ -1,106 +0,0 @@
import { ipcMain } from "electron";
import commands from "../../commands";
import { spawnUpscayl } from "../../upscayl";
import { getBatchArguments } from "../getArguments";
import fs from "fs";
export type FolderUpscaylProps = {
mainWindow: Electron.BrowserWindow;
logit: (message: string, ...optionalParams: any[]) => void;
childProcesses: any[];
stopped: boolean;
modelsPath: string;
customModelsFolderPath: string | undefined;
saveOutputFolder: boolean;
outputFolderPath: string | undefined;
defaultModels: string[];
};
export default function ({
mainWindow,
logit,
childProcesses,
stopped,
modelsPath,
customModelsFolderPath,
saveOutputFolder,
outputFolderPath,
defaultModels,
}: FolderUpscaylProps) {
ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
// GET THE MODEL
const model = payload.model;
const gpuId = payload.gpuId;
const saveImageAs = payload.saveImageAs;
const scale = payload.scale as string;
// GET THE IMAGE DIRECTORY
let inputDir = payload.batchFolderPath;
// GET THE OUTPUT DIRECTORY
let outputDir = payload.outputPath;
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
}
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const isDefaultModel = defaultModels.includes(model);
// UPSCALE
const upscayl = spawnUpscayl(
"realesrgan",
getBatchArguments(
inputDir,
outputDir,
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
model,
gpuId,
saveImageAs,
scale
),
logit
);
childProcesses.push(upscayl);
stopped = false;
let failed = false;
const onData = (data: any) => {
data = data.toString();
mainWindow.webContents.send(
commands.FOLDER_UPSCAYL_PROGRESS,
data.toString()
);
if (data.includes("invalid gpu") || data.includes("failed")) {
logit("❌ INVALID GPU OR INVALID FILES IN FOLDER - FAILED");
failed = true;
upscayl.kill();
}
};
const onError = (data: any) => {
mainWindow.webContents.send(
commands.FOLDER_UPSCAYL_PROGRESS,
data.toString()
);
failed = true;
upscayl.kill();
return;
};
const onClose = () => {
if (!failed && !stopped) {
logit("💯 Done upscaling");
mainWindow.webContents.send(commands.FOLDER_UPSCAYL_DONE, outputDir);
} else {
upscayl.kill();
}
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
});
}

View File

@ -1,29 +0,0 @@
import { ipcMain } from "electron";
import commands from "../../commands";
export type GetModelsListProps = {
mainWindow: Electron.BrowserWindow;
customModelsFolderPath: string | undefined;
logit: (message: string, ...optionalParams: any[]) => void;
getModels: (folderPath: string) => string[] | null;
};
export default function ({
mainWindow,
customModelsFolderPath,
logit,
getModels,
}: GetModelsListProps) {
ipcMain.on(commands.GET_MODELS_LIST, async (event, payload) => {
if (payload) {
customModelsFolderPath = payload;
logit("📁 Custom Models Folder Path: ", customModelsFolderPath);
mainWindow.webContents.send(
commands.CUSTOM_MODEL_FILES_LIST,
getModels(payload)
);
}
});
}

View File

@ -1,162 +0,0 @@
import { ipcMain } from "electron";
import commands from "../../commands";
import { parse } from "path";
import { spawnUpscayl } from "../../upscayl";
import Jimp from "jimp";
import { getSingleImageArguments } from "../getArguments";
import fs from "fs";
export type ImageUpscaylProps = {
mainWindow: Electron.BrowserWindow;
slash: string;
logit: (...args: any) => void;
childProcesses: any[];
stopped: boolean;
modelsPath: string;
customModelsFolderPath: string | undefined;
saveOutputFolder: boolean;
outputFolderPath: string | undefined;
quality: number;
defaultModels: string[];
folderPath: string | undefined;
};
export default function ({
mainWindow,
slash,
logit,
childProcesses,
stopped,
modelsPath,
customModelsFolderPath,
saveOutputFolder,
outputFolderPath,
quality,
defaultModels,
folderPath,
}: ImageUpscaylProps) {
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);
}
});
}

View File

@ -1,59 +0,0 @@
import { MessageBoxOptions, dialog, ipcMain } from "electron";
import commands from "../../commands";
export type SelectFileProps = {
mainWindow: Electron.BrowserWindow;
imagePath: string;
logit: (...args: any) => void;
};
export default function ({ mainWindow, imagePath, logit }) {
ipcMain.handle(commands.SELECT_FILE, async () => {
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ["openFile", "multiSelections"],
title: "Select Image",
defaultPath: imagePath,
});
if (canceled) {
logit("🚫 File Operation Cancelled");
return null;
} else {
imagePath = filePaths[0];
let isValid = false;
// 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) {
logit("❌ Invalid File Detected");
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;
}
logit("📄 Selected File Path: ", filePaths[0]);
// CREATE input AND upscaled FOLDER
return filePaths[0];
}
});
}

View File

@ -1,25 +0,0 @@
import { dialog, ipcMain } from "electron";
import commands from "../../commands";
export type SelectFolderProps = {
folderPath: string;
logit: (...args: any[]) => void;
};
export default function ({ folderPath, logit }) {
ipcMain.handle(commands.SELECT_FOLDER, async (event, message) => {
const { canceled, filePaths: folderPaths } = await dialog.showOpenDialog({
properties: ["openDirectory"],
defaultPath: folderPath,
});
if (canceled) {
logit("🚫 Select Folder Operation Cancelled");
return null;
} else {
folderPath = folderPaths[0];
logit("📁 Selected Folder Path: ", folderPath);
return folderPaths[0];
}
});
}

View File

@ -0,0 +1,48 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const electron_1 = require("electron");
const commands_1 = __importDefault(require("../../commands"));
function default_1({ mainWindow, customModelsFolderPath, logit, slash, getModels, }) {
electron_1.ipcMain.handle(commands_1.default.SELECT_CUSTOM_MODEL_FOLDER, (event, message) => __awaiter(this, void 0, void 0, function* () {
const { canceled, filePaths: folderPaths } = yield electron_1.dialog.showOpenDialog({
properties: ["openDirectory"],
title: "Select Custom Models Folder",
defaultPath: customModelsFolderPath,
});
if (canceled) {
logit("🚫 Select Custom Models Folder Operation Cancelled");
return null;
}
else {
customModelsFolderPath = folderPaths[0];
if (!folderPaths[0].endsWith(slash + "models") &&
!folderPaths[0].endsWith(slash + "models" + slash)) {
logit("❌ Invalid Custom Models Folder Detected: Not a 'models' folder");
const options = {
type: "error",
title: "Invalid Folder",
message: "Please make sure that the folder name is 'models' and nothing else.",
buttons: ["OK"],
};
electron_1.dialog.showMessageBoxSync(options);
return null;
}
mainWindow.webContents.send(commands_1.default.CUSTOM_MODEL_FILES_LIST, getModels(customModelsFolderPath));
logit("📁 Custom Folder Path: ", customModelsFolderPath);
return customModelsFolderPath;
}
}));
}
exports.default = default_1;

View File

@ -18,8 +18,8 @@ const path_1 = require("path");
const upscayl_1 = require("../../upscayl");
const getArguments_1 = require("../getArguments");
const jimp_1 = __importDefault(require("jimp"));
function default_1(mainWindow, slash, logit, childProcesses, stopped, modelsPath, customModelsFolderPath, saveOutputFolder, outputFolderPath, quality, defaultModels) {
return electron_1.ipcMain.on(commands_1.default.DOUBLE_UPSCAYL, (event, payload) => __awaiter(this, void 0, void 0, function* () {
function default_1({ mainWindow, slash, logit, childProcesses, stopped, modelsPath, customModelsFolderPath, saveOutputFolder, outputFolderPath, quality, defaultModels, }) {
electron_1.ipcMain.on(commands_1.default.DOUBLE_UPSCAYL, (event, payload) => __awaiter(this, void 0, void 0, function* () {
const model = payload.model;
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)[1] || "");
let outputDir = payload.outputPath;

View File

@ -17,8 +17,7 @@ const commands_1 = __importDefault(require("../../commands"));
const upscayl_1 = require("../../upscayl");
const getArguments_1 = require("../getArguments");
const fs_1 = __importDefault(require("fs"));
function default_1(mainWindow, logit, childProcesses, stopped, modelsPath, customModelsFolderPath, saveOutputFolder, outputFolderPath, defaultModels) {
//------------------------Upscayl Folder-----------------------------//
function default_1({ mainWindow, logit, childProcesses, stopped, modelsPath, customModelsFolderPath, saveOutputFolder, outputFolderPath, defaultModels, }) {
electron_1.ipcMain.on(commands_1.default.FOLDER_UPSCAYL, (event, payload) => __awaiter(this, void 0, void 0, function* () {
// GET THE MODEL
const model = payload.model;

View File

@ -0,0 +1,26 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const electron_1 = require("electron");
const commands_1 = __importDefault(require("../../commands"));
function default_1({ mainWindow, customModelsFolderPath, logit, getModels, }) {
electron_1.ipcMain.on(commands_1.default.GET_MODELS_LIST, (event, payload) => __awaiter(this, void 0, void 0, function* () {
if (payload) {
customModelsFolderPath = payload;
logit("📁 Custom Models Folder Path: ", customModelsFolderPath);
mainWindow.webContents.send(commands_1.default.CUSTOM_MODEL_FILES_LIST, getModels(payload));
}
}));
}
exports.default = default_1;

View File

@ -0,0 +1,118 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const electron_1 = require("electron");
const commands_1 = __importDefault(require("../../commands"));
const path_1 = require("path");
const upscayl_1 = require("../../upscayl");
const jimp_1 = __importDefault(require("jimp"));
const getArguments_1 = require("../getArguments");
const fs_1 = __importDefault(require("fs"));
function default_1({ mainWindow, slash, logit, childProcesses, stopped, modelsPath, customModelsFolderPath, saveOutputFolder, outputFolderPath, quality, defaultModels, folderPath, }) {
electron_1.ipcMain.on(commands_1.default.UPSCAYL, (event, payload) => __awaiter(this, void 0, void 0, function* () {
const model = payload.model;
const gpuId = payload.gpuId;
const saveImageAs = payload.saveImageAs;
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)[1] || "");
let outputDir = folderPath || payload.outputPath;
if (saveOutputFolder === true && outputFolderPath) {
outputDir = outputFolderPath;
}
const isDefaultModel = defaultModels.includes(model);
const fullfileName = payload.imagePath.replace(/^.*[\\\/]/, "");
const fileName = (0, path_1.parse)(fullfileName).name;
const fileExt = (0, path_1.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_1.default.existsSync(outFile)) {
// If already upscayled, just output that file
logit("✅ Already upscayled at: ", outFile);
mainWindow.webContents.send(commands_1.default.UPSCAYL_DONE, outFile);
}
else {
const upscayl = (0, upscayl_1.spawnUpscayl)("realesrgan", (0, getArguments_1.getSingleImageArguments)(inputDir, fullfileName, outFile, isDefaultModel ? modelsPath : customModelsFolderPath !== null && customModelsFolderPath !== void 0 ? customModelsFolderPath : modelsPath, model, scale, gpuId, saveImageAs), logit);
childProcesses.push(upscayl);
stopped = false;
let isAlpha = false;
let failed = false;
const onData = (data) => {
logit("image upscayl: ", data.toString());
mainWindow.setProgressBar(parseFloat(data.slice(0, data.length)) / 100);
data = data.toString();
mainWindow.webContents.send(commands_1.default.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_1.default.UPSCAYL_PROGRESS, data.toString());
failed = true;
return;
};
const onClose = () => __awaiter(this, void 0, void 0, function* () {
if (!failed && !stopped) {
logit("💯 Done upscaling");
logit("♻ Scaling and converting now...");
const originalImage = yield jimp_1.default.read(inputDir + slash + fullfileName);
try {
const newImage = yield jimp_1.default.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_1.default.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);
}
}));
}
exports.default = default_1;

View File

@ -0,0 +1,61 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const electron_1 = require("electron");
const commands_1 = __importDefault(require("../../commands"));
function default_1({ mainWindow, imagePath, logit }) {
return electron_1.ipcMain.handle(commands_1.default.SELECT_FILE, () => __awaiter(this, void 0, void 0, function* () {
const { canceled, filePaths } = yield electron_1.dialog.showOpenDialog({
properties: ["openFile", "multiSelections"],
title: "Select Image",
defaultPath: imagePath,
});
if (canceled) {
logit("🚫 File Operation Cancelled");
return null;
}
else {
imagePath = filePaths[0];
let isValid = false;
// 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) {
logit("❌ Invalid File Detected");
const options = {
type: "error",
title: "Invalid File",
message: "The selected file is not a valid image. Make sure you select a '.png', '.jpg', or '.webp' file.",
};
electron_1.dialog.showMessageBoxSync(mainWindow, options);
return null;
}
logit("📄 Selected File Path: ", filePaths[0]);
// CREATE input AND upscaled FOLDER
return filePaths[0];
}
}));
}
exports.default = default_1;

View File

@ -0,0 +1,34 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const electron_1 = require("electron");
const commands_1 = __importDefault(require("../../commands"));
function default_1({ folderPath, logit }) {
electron_1.ipcMain.handle(commands_1.default.SELECT_FOLDER, (event, message) => __awaiter(this, void 0, void 0, function* () {
const { canceled, filePaths: folderPaths } = yield electron_1.dialog.showOpenDialog({
properties: ["openDirectory"],
defaultPath: folderPath,
});
if (canceled) {
logit("🚫 Select Folder Operation Cancelled");
return null;
}
else {
folderPath = folderPaths[0];
logit("📁 Selected Folder Path: ", folderPath);
return folderPaths[0];
}
}));
}
exports.default = default_1;

View File

@ -15,17 +15,10 @@ function Footer() {
<p>
By{" "}
<a
href="https://github.com/TGS963"
href="https://github.com/upscayl"
className="font-bold"
target="_blank">
TGS963
</a>{" "}
and{" "}
<a
href="https://github.com/NayamAmarshe"
className="font-bold"
target="_blank">
Nayam Amarshe
TGS963 and Nayam Amarshe
</a>
</p>
</div>

View File

@ -20,7 +20,7 @@ const ImageOptions = ({
}, []);
return (
<div className="animate rounded-btn collapse fixed top-1 z-50 m-2 backdrop-blur-lg">
<div className="animate rounded-btn collapse fixed top-1 m-2 z-50 max-w-lg backdrop-blur-lg">
<input type="checkbox" className="peer" />
{/* <div className="peer-checked:outline-title-none collapse-title bg-opacity-25 text-center text-sm font-semibold uppercase backdrop-blur-2xl peer-checked:bg-base-300 peer-checked:text-base-content"> */}
<div className="outline-title peer-checked:outline-title-none collapse-title text-center text-sm font-semibold uppercase text-black mix-blend-difference outline-2 peer-checked:bg-base-300 peer-checked:text-base-content">