1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-02-15 18:32:33 +01:00
upscayl/renderer/pages/index.jsx

256 lines
7.6 KiB
React
Raw Normal View History

import { useState, useEffect, useRef, useCallback } from "react";
2022-08-22 14:01:16 +05:30
import commands from "../../main/commands";
2022-08-18 15:23:23 +05:30
import {
ReactCompareSlider,
ReactCompareSliderImage,
} from "react-compare-slider";
2022-08-15 10:23:14 +05:30
2022-08-23 18:47:34 +05:30
import Animated from "../public/loading.svg";
import Image from "next/image";
2022-08-22 14:01:16 +05:30
2022-08-15 10:23:14 +05:30
const Home = () => {
2022-08-18 15:23:23 +05:30
const [imagePath, SetImagePath] = useState("");
const [upscaledImagePath, setUpscaledImagePath] = useState("");
const [outputPath, SetOutputPath] = useState("");
const [scaleFactor, setScaleFactor] = useState(4);
const [progress, setProgress] = useState("");
const [model, setModel] = useState("realesrgan-x4plus");
2022-08-17 08:07:50 +05:30
const [loaded, setLoaded] = useState(false);
2022-08-23 20:07:21 +05:30
const [version, setVersion] = useState("");
useEffect(() => {
setVersion(navigator.userAgent.match(/Upscayl\/([\d\.]+\d+)/)[1]);
}, []);
2022-08-16 07:47:27 +05:30
2022-08-15 10:23:14 +05:30
useEffect(() => {
2022-08-17 08:07:50 +05:30
setLoaded(true);
2022-08-18 15:23:23 +05:30
window.electron.on(commands.UPSCAYL_PROGRESS, (_, data) => {
if (data.includes("invalid gpu")){
alert("Error!!! Please make sure your GPU supports vulkan")
}
else if (data.length > 0 && data.length < 10) setProgress(data);
2022-08-17 17:06:19 +05:30
});
2022-08-18 15:23:23 +05:30
window.electron.on(commands.UPSCAYL_DONE, (_, data) => {
setUpscaledImagePath(data);
2022-08-17 08:07:50 +05:30
});
2022-08-15 10:23:14 +05:30
}, []);
2022-08-16 07:47:27 +05:30
2022-08-18 15:23:23 +05:30
const selectImageHandler = async () => {
SetImagePath("");
setUpscaledImagePath("");
setProgress("");
var path = await window.electron.invoke(commands.SELECT_FILE);
if (path !== "cancelled") {
SetImagePath(path);
2022-08-18 15:23:23 +05:30
var dirname = path.match(/(.*)[\/\\]/)[1] || "";
SetOutputPath(dirname);
}
};
2022-08-18 15:23:23 +05:30
2022-08-23 08:43:08 +05:30
const handleModelChange = (e) => {
setModel(e.target.value);
};
2022-08-19 14:27:05 +05:30
const outputHandler = async () => {
2022-08-18 15:23:23 +05:30
var path = await window.electron.invoke(commands.SELECT_FOLDER);
if (path !== "cancelled") {
SetOutputPath(path);
} else {
console.log("Getting output path from input file");
}
};
2022-08-18 15:23:23 +05:30
const upscaylHandler = async () => {
if (imagePath !== "") {
setUpscaledImagePath("");
setProgress("Hold on...");
await window.electron.send(commands.UPSCAYL, {
scaleFactor,
imagePath,
outputPath,
model,
});
}
else {
alert("Please select an image!!!")
}
2022-08-16 07:47:27 +05:30
};
2022-08-15 10:23:14 +05:30
return (
2022-08-18 15:23:23 +05:30
<div className="flex h-screen w-screen flex-row overflow-hidden bg-neutral-900">
<div className="flex h-screen w-96 flex-col bg-neutral-800">
{/* HEADER */}
<h1 className="pl-5 pt-5 text-3xl font-bold text-neutral-50">
Upscayl
</h1>
<p className="mb-2 pl-5 text-neutral-400">AI Image Upscaler</p>
{/* LEFT PANE */}
<div className="h-screen overflow-auto p-5">
{/* STEP 1 */}
<div className="mt-5">
<p className="mb-2 font-medium text-neutral-100">Step 1</p>
<button
className="rounded-lg bg-rose-400 p-3"
onClick={selectImageHandler}
>
Select Image
</button>
</div>
{/* STEP 2 */}
<div className="mt-10">
<p className="font-medium text-neutral-100">Step 2</p>
2022-08-23 08:43:08 +05:30
<p className="mb-2 text-sm text-neutral-400">
Select Upscaling Type
</p>
<select
name="select-model"
className="rounded-lg bg-slate-300 p-3"
onChange={handleModelChange}
>
<option value="realesrgan-x4plus">General Image</option>
<option value="realesrgan-x4plus-anime">Digital Image</option>
</select>
</div>
2022-08-23 18:47:34 +05:30
{/* STEP 3
2022-08-18 15:23:23 +05:30
<div className="mt-10">
2022-08-23 17:55:51 +05:30
<p className="font-medium text-neutral-100">Step 3</p>
2022-08-18 15:23:23 +05:30
<p className="mb-2 text-sm text-neutral-400">Select Scale Factor</p>
<div className="animate flex flex-row gap-2">
<button
className={`h-12 w-12 rounded-lg ${
scaleFactor === 2 ? "bg-yellow-400" : "bg-neutral-400"
}`}
onClick={() => setScaleFactor(2)}
>
2x
</button>
<button
className={`h-12 w-12 rounded-lg ${
scaleFactor === 3 ? "bg-yellow-400" : "bg-neutral-400"
}`}
onClick={() => setScaleFactor(3)}
>
3x
</button>
<button
className={`h-12 w-12 rounded-lg ${
scaleFactor === 4 ? "bg-yellow-400" : "bg-neutral-400"
}`}
2022-08-23 08:43:08 +05:30
onClick={() => setScaleFactor(4)}
2022-08-18 15:23:23 +05:30
>
4x
</button>
</div>
2022-08-23 18:47:34 +05:30
</div> */}
2022-08-18 15:23:23 +05:30
2022-08-23 17:55:51 +05:30
{/* STEP 4 */}
2022-08-18 15:23:23 +05:30
<div className="mt-10">
2022-08-23 18:47:34 +05:30
<p className="font-medium text-neutral-100">Step 3</p>
2022-08-18 15:23:23 +05:30
<p className="mb-2 text-sm text-neutral-400">
Defaults to Image's path
</p>
<button
className="rounded-lg bg-teal-400 p-3"
onClick={outputHandler}
>
Set Output Folder
</button>
</div>
2022-08-23 18:47:34 +05:30
{/* STEP 4 */}
2022-08-18 15:23:23 +05:30
<div className="mt-10">
2022-08-23 17:55:51 +05:30
<p className="mb-2 font-medium text-neutral-100">Step 5</p>
2022-08-18 15:23:23 +05:30
<button
className="rounded-lg bg-sky-400 p-3"
onClick={upscaylHandler}
>
Upscayl
</button>
2022-08-15 12:51:12 +05:30
</div>
</div>
2022-08-18 15:23:23 +05:30
<div className="p-2 text-center text-sm text-neutral-500">
<p>
Copyright © 2022 -{" "}
<a
className="font-bold"
href="https://github.com/TGS963/upscayl"
target="_blank"
>
Upscayl
</a>
</p>
<p>
Made by{" "}
<a
href="https://github.com/TGS963"
className="font-bold"
target="_blank"
>
TGS963
</a>{" "}
and{" "}
<a
href="https://github.com/NayamAmarshe"
className="font-bold"
target="_blank"
>
Nayam Amarshe
</a>
</p>
2022-08-15 12:51:12 +05:30
</div>
</div>
2022-08-18 15:23:23 +05:30
{/* RIGHT PANE */}
<div className="relative flex h-screen w-full flex-col items-center justify-center">
{progress.length > 0 && (
<div className="absolute flex h-full w-full flex-col items-center justify-center bg-black/50 backdrop-blur-lg">
<div className="flex flex-col items-center gap-2">
2022-08-22 14:01:16 +05:30
<Image src={Animated} />
2022-08-18 15:23:23 +05:30
<p className="font-bold text-neutral-50">{progress}</p>
</div>
</div>
)}
{imagePath.length === 0 ? (
2022-08-23 20:07:21 +05:30
<>
<p className="p-5 text-lg font-medium text-neutral-400">
Select an Image to Upscale
</p>
<p className="text-neutral-600">Upscale v{version}</p>
</>
2022-08-18 15:23:23 +05:30
) : upscaledImagePath.length === 0 ? (
<img
className="h-full w-full object-contain"
src={
"file://" + `${upscaledImagePath ? upscaledImagePath : imagePath}`
}
alt=""
/>
) : (
<ReactCompareSlider
itemOne={
<ReactCompareSliderImage
src={"file://" + imagePath}
alt="Original"
/>
}
itemTwo={
<ReactCompareSliderImage
src={"file://" + upscaledImagePath}
alt="Upscayl"
/>
}
2022-08-23 18:47:34 +05:30
className="object-contain"
2022-08-18 15:23:23 +05:30
/>
)}
2022-08-15 10:23:14 +05:30
</div>
</div>
);
};
export default Home;