properly support tga and webp images
This commit is contained in:
25
main.go
25
main.go
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"image"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -11,7 +12,9 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.yetaga.in/alazyreader/why/filetypes"
|
||||
"github.com/dblezek/tga"
|
||||
"github.com/disintegration/imaging"
|
||||
"golang.org/x/image/webp"
|
||||
tk "modernc.org/tk9.0"
|
||||
)
|
||||
|
||||
@@ -39,6 +42,10 @@ func must[T any](t T, err error) T {
|
||||
return t
|
||||
}
|
||||
|
||||
func checkErr[T any](_ T, err error) bool {
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func isImage(entry os.DirEntry) bool {
|
||||
if entry.IsDir() {
|
||||
return false
|
||||
@@ -108,11 +115,27 @@ func newDirectory(img *tk.LabelWidget) func() {
|
||||
}
|
||||
|
||||
func updateImage(file string, img *tk.LabelWidget) {
|
||||
i, err := imaging.Open(file, imaging.AutoOrientation(true))
|
||||
var i image.Image
|
||||
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
log.Printf("error opening image: %v", err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(file), "."))
|
||||
if checkErr(imaging.FormatFromExtension(ext)) {
|
||||
i, err = imaging.Decode(f, imaging.AutoOrientation(true))
|
||||
} else if ext == "webp" {
|
||||
i, err = webp.Decode(f)
|
||||
} else if ext == "tga" {
|
||||
i, err = tga.Decode(f)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("error decoding image: %v", err)
|
||||
return
|
||||
}
|
||||
i = imaging.Fit(i,
|
||||
// -50 to give some space to breathe around the edges
|
||||
must(strconv.Atoi(tk.WinfoScreenWidth(tk.App)))-50,
|
||||
|
Reference in New Issue
Block a user