diff --git a/main/commands.js b/main/commands.js index 49b87e5..03b48b3 100644 --- a/main/commands.js +++ b/main/commands.js @@ -4,6 +4,8 @@ const commands = { UPSCAYL: "Upscale the Image", UPSCAYL_DONE: "Upscaling Done", UPSCAYL_PROGRESS: "Send Progress from Main to Renderer", + SHARPEN: "Sharpen the Image First", + SHARPEN_PROGRESS: "Send Sharpening Progress from Main to Renderer", }; module.exports = commands; diff --git a/main/index.js b/main/index.js index 7e008c1..790db16 100644 --- a/main/index.js +++ b/main/index.js @@ -105,12 +105,85 @@ ipcMain.handle(commands.SELECT_FOLDER, async (event, message) => { } }); -ipcMain.on(commands.UPSCAYL, async (event, payload) => { +ipcMain.on(commands.SHARPEN, async (event, payload) => { const model = payload.model; const scale = payload.scaleFactor; + let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || ""; + let outputDir = "./sharpened"; + if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir); + } + + // COPY IMAGE TO TMP FOLDER + const platform = getPlatform(); + const fullfileName = + platform === "win" + ? payload.imagePath.split("\\").slice(-1)[0] + : payload.imagePath.split("/").slice(-1)[0]; + const fileName = parse(fullfileName).name; + const fileExt = parse(fullfileName).ext; + + const inputFile = inputDir + "/" + fullfileName; + const outFile = outputDir + "/" + fileName + "_sharpen" + fileExt; + + fs.copyFile(inputFile, outFile, (err) => { + if (err) throw err; + }); + + // UPSCALE + if (fs.existsSync(outFile)) { + // If already upscayled, just output that file + return outFile; + } else { + let upscayl = spawn( + execPath + "-realesr", + [ + "-i", + inputDir + "/" + fullfileName, + "-o", + outFile, + "-s", + 4, + "-x", + "-m", + modelsPath + "/" + model, + ], + { + cwd: null, + detached: false, + } + ); + + let failed = false; + upscayl.stderr.on("data", (stderr) => { + console.log(stderr.toString()); + stderr = stderr.toString(); + mainWindow.webContents.send(commands.SHARPEN_PROGRESS, stderr.toString()); + if (stderr.includes("invalid gpu") || stderr.includes("failed")) { + failed = true; + return null; + } + }); + + // Send done comamnd when + upscayl.on("close", (code) => { + if (failed !== true) { + console.log("Done upscaling"); + return outFile; + } + }); + } +}); + +ipcMain.on(commands.UPSCAYL, async (event, payload) => { + const model = payload.model; + const scale = payload.scaleFactor; + const sharpen = payload.sharpen; + let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || ""; let outputDir = payload.outputPath; + console.log("🚀 => ipcMain => outputDir", outputDir); // COPY IMAGE TO TMP FOLDER diff --git a/renderer/components/LeftPaneSteps.jsx b/renderer/components/LeftPaneSteps.jsx index 07ebb8d..d7a993c 100644 --- a/renderer/components/LeftPaneSteps.jsx +++ b/renderer/components/LeftPaneSteps.jsx @@ -49,16 +49,30 @@ function LeftPaneSteps(props) {

Select Upscaling Type

+
+ { + props.setSharpen(e.target.checked); + }} + /> +

+ Sharpen Image +

+
@@ -124,7 +138,7 @@ function LeftPaneSteps(props) {

Step 3

- Defaults to Folder's Path + Defaults to Folder's path