Skip to content

Commit 054e0a5

Browse files
committed
avif support
1 parent 1de21fe commit 054e0a5

File tree

7 files changed

+2875
-10973
lines changed

7 files changed

+2875
-10973
lines changed

components/AdvancedSettings.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const AdvancedSettings: React.FunctionComponent = (props) => {
106106
setGIFTransparency(true)
107107
setGIFQuality(10)
108108
setPNGCompression(3)
109-
setJPGQuality(100)
109+
setJPGQuality(95)
110110
setParallelFrames(2)
111111
setThreads(4)
112112
setNoise(2)
@@ -426,7 +426,7 @@ const AdvancedSettings: React.FunctionComponent = (props) => {
426426
<input className="settings-input" type="text" spellCheck="false" value={pngCompression} onChange={changePNGCompression} onKeyDown={changePNGCompressionKey}/>
427427
</div>
428428
<div className="settings-row">
429-
<p className="settings-text">JPG/WEBP Quality: </p>
429+
<p className="settings-text">JPG/WEBP/AVIF Quality: </p>
430430
<input className="settings-input" type="text" spellCheck="false" value={jpgQuality} onChange={changeJPGQuality} onKeyDown={changeJPGQualityKey}/>
431431
</div>
432432
<div className="settings-row">

main.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import Store from "electron-store"
33
import {autoUpdater} from "electron-updater"
44
import sharp from "sharp"
55
import fs from "fs"
6-
import imageSize from "image-size"
76
import path from "path"
87
import process from "process"
98
import waifu2x from "waifu2x"
@@ -301,10 +300,31 @@ const upscale = async (info: any) => {
301300
meta[i].value = meta[i].value.replaceAll("UNICODE", "").replaceAll(/\u0000/g, "")
302301
}
303302
} catch {}
303+
let avif = false
304+
let jxl = false
305+
let sourceExt = path.extname(info.source)
306+
if (path.extname(info.source) === ".avif" || path.extname(info.source) === ".jxl") {
307+
if (path.extname(info.source) === ".avif") avif = true
308+
if (path.extname(info.source) === ".jxl") jxl = true
309+
const buffer = await sharp(fs.readFileSync(info.source), {limitInputPixels: false}).png().toBuffer()
310+
const newDest = dest.replace(path.extname(dest), ".png")
311+
fs.writeFileSync(newDest, buffer)
312+
info.source = newDest
313+
dest = newDest
314+
}
304315
output = await waifu2x.upscaleImage(info.source, dest, options, action)
316+
if (avif || jxl) {
317+
let buffer = sharp(fs.readFileSync(dest), {limitInputPixels: false})
318+
if (avif) buffer = buffer.avif({quality: options.jpgWebpQuality})
319+
if (jxl) buffer = buffer.jxl({quality: options.jpgWebpQuality})
320+
const newDest = dest.replace(path.extname(dest), sourceExt)
321+
fs.renameSync(dest, newDest)
322+
fs.writeFileSync(newDest, await buffer.toBuffer())
323+
output = newDest
324+
}
305325
if (info.compress) {
306326
const inputBuffer = fs.readFileSync(output)
307-
const outputBuffer = await sharp(inputBuffer, {limitInputPixels: false}).jpeg().toBuffer()
327+
const outputBuffer = await sharp(inputBuffer, {limitInputPixels: false}).jpeg({optimiseScans: true, trellisQuantisation: true, quality: options.jpgWebpQuality}).toBuffer()
308328
fs.writeFileSync(output, outputBuffer)
309329
const renamePath = path.join(path.dirname(output), `${path.basename(output, path.extname(output))}.jpg`)
310330
fs.renameSync(output, renamePath)
@@ -391,7 +411,7 @@ ipcMain.handle("get-dimensions", async (event, path: string, type: string, optio
391411
const dimensions = await waifu2x.pdfDimensions(path, {downscaleHeight: undefined})
392412
return {width: dimensions.width, height: dimensions.height, image: dimensions.image}
393413
} else {
394-
const dimensions = imageSize(path)
414+
const dimensions = await sharp(fs.readFileSync(path), {limitInputPixels: false}).metadata()
395415
return {width: dimensions.width, height: dimensions.height}
396416
}
397417
})
@@ -417,8 +437,8 @@ ipcMain.handle("select-files", async () => {
417437
const files = await dialog.showOpenDialog(window, {
418438
filters: [
419439
{name: "All Files", extensions: ["*"]},
420-
{name: "Images", extensions: ["png", "jpg", "jpeg", "webp", "tiff"]},
421-
{name: "GIF", extensions: ["gif"]},
440+
{name: "Images", extensions: ["png", "jpg", "jpeg", "webp", "avif", "jxl", "tiff"]},
441+
{name: "Animations", extensions: ["gif", "webp"]},
422442
{name: "Videos", extensions: ["mp4", "ogv", "webm", "avi", "mov", "mkv", "flv"]},
423443
{name: "PDF", extensions: ["pdf"]},
424444
],
@@ -464,7 +484,7 @@ if (!singleLock) {
464484
})
465485

466486
app.on("ready", () => {
467-
window = new BrowserWindow({width: 800, height: 600, minWidth: 720, minHeight: 450, frame: false, backgroundColor: "#5ea8da", center: true, webPreferences: {nodeIntegration: true, contextIsolation: false}})
487+
window = new BrowserWindow({width: 800, height: 600, minWidth: 720, minHeight: 450, frame: false, backgroundColor: "#5ea8da", center: true, roundedCorners: false, webPreferences: {nodeIntegration: true, contextIsolation: false}})
468488
window.loadFile(path.join(__dirname, "index.html"))
469489
window.removeMenu()
470490
if (process.platform !== "win32") {

0 commit comments

Comments
 (0)