mirror of
https://github.com/gen2brain/cbconvert
synced 2026-06-30 09:11:54 +02:00
Optimize cover preview
This commit is contained in:
@@ -70,10 +70,37 @@ func fileDlg(title string, multiple, directory bool, dirKey string) ([]string, e
|
||||
|
||||
const dlgPreviewName = "_FILEDLGPREVIEW_"
|
||||
|
||||
// previewPad insets the cover from the preview pane edges, in pixels per side.
|
||||
const previewPad = 8
|
||||
|
||||
// previewCover returns a FILE_CB handler that draws the highlighted comic's cover in the dialog preview pane.
|
||||
// Extracted covers are cached by path so re-highlighting a file doesn't re-extract it.
|
||||
func previewCover() iup.FileFunc {
|
||||
var image iup.Ihandle
|
||||
var lastFile string
|
||||
const maxCache = 32
|
||||
|
||||
cache := make(map[string]iup.Ihandle)
|
||||
order := make([]string, 0, maxCache)
|
||||
|
||||
cover := func(path string, w, h int) iup.Ihandle {
|
||||
if img, ok := cache[path]; ok {
|
||||
return img
|
||||
}
|
||||
|
||||
img := loadCover(path, w, h)
|
||||
cache[path] = img
|
||||
order = append(order, path)
|
||||
|
||||
if len(order) > maxCache {
|
||||
old := order[0]
|
||||
order = order[1:]
|
||||
if oi := cache[old]; oi != 0 {
|
||||
oi.Destroy()
|
||||
}
|
||||
delete(cache, old)
|
||||
}
|
||||
|
||||
return img
|
||||
}
|
||||
|
||||
return func(ih iup.Ihandle, filename, status string) int {
|
||||
switch status {
|
||||
@@ -82,19 +109,8 @@ func previewCover() iup.FileFunc {
|
||||
cw, ch := iup.DrawGetSize(ih)
|
||||
iup.DrawParentBackground(ih)
|
||||
|
||||
if filename != lastFile {
|
||||
lastFile = filename
|
||||
if image != 0 {
|
||||
image.Destroy()
|
||||
image = 0
|
||||
}
|
||||
if img := loadCover(filename, cw, ch); img != 0 {
|
||||
image = img
|
||||
iup.SetHandle(dlgPreviewName, image)
|
||||
}
|
||||
}
|
||||
|
||||
if image != 0 {
|
||||
if image := cover(filename, cw-2*previewPad, ch-2*previewPad); image != 0 {
|
||||
iup.SetHandle(dlgPreviewName, image)
|
||||
iw, iih, _ := iup.DrawGetImageInfo(dlgPreviewName)
|
||||
iup.DrawImage(ih, dlgPreviewName, (cw-iw)/2, (ch-iih)/2, iw, iih)
|
||||
} else {
|
||||
@@ -106,11 +122,13 @@ func previewCover() iup.FileFunc {
|
||||
|
||||
iup.DrawEnd(ih)
|
||||
case "FINISH":
|
||||
if image != 0 {
|
||||
image.Destroy()
|
||||
image = 0
|
||||
for _, img := range cache {
|
||||
if img != 0 {
|
||||
img.Destroy()
|
||||
}
|
||||
}
|
||||
lastFile = ""
|
||||
cache = make(map[string]iup.Ihandle)
|
||||
order = order[:0]
|
||||
}
|
||||
|
||||
return iup.DEFAULT
|
||||
@@ -131,7 +149,7 @@ func loadCover(path string, w, h int) iup.Ihandle {
|
||||
opts := cbconvert.NewOptions()
|
||||
opts.DPI = 96
|
||||
|
||||
img, err := cbconvert.New(opts).Preview(path, fi, w, h)
|
||||
img, err := cbconvert.New(opts).CoverPreview(path, fi, w, h)
|
||||
if err != nil || img.Image == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ require (
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/ebitengine/purego v0.10.1 // indirect
|
||||
github.com/gen2brain/avif v0.5.1 // indirect
|
||||
github.com/gen2brain/go-fitz v1.24.15 // indirect
|
||||
github.com/gen2brain/go-fitz v1.28.0 // indirect
|
||||
github.com/gen2brain/jpegli v0.4.1 // indirect
|
||||
github.com/gen2brain/jpegxl v0.5.1 // indirect
|
||||
github.com/gen2brain/webp v0.6.1 // indirect
|
||||
@@ -31,7 +31,6 @@ require (
|
||||
github.com/golang/geo v0.0.0-20260625163123-7c0e84413537 // indirect
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
|
||||
github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25 // indirect
|
||||
github.com/jupiterrider/ffi v0.7.0 // indirect
|
||||
github.com/klauspost/compress v1.18.6 // indirect
|
||||
github.com/klauspost/pgzip v1.2.6 // indirect
|
||||
github.com/mholt/archives v0.1.5 // indirect
|
||||
|
||||
@@ -42,8 +42,7 @@ github.com/gen2brain/avif v0.5.1 h1:LQzLsJpWyGlsa4wuZ3D57qEbCiICIK7Yidz5ZPEwzTk=
|
||||
github.com/gen2brain/avif v0.5.1/go.mod h1:QgrYqdVE9y40PCfArK9VakcMIpYeDYpZmCSLkW6C1n8=
|
||||
github.com/gen2brain/cbconvert v1.0.5-0.20260626071631-8155626dbb42 h1:p1K1jOk+rKDhTgh6fiYMKSqXJdIVUWFLK5jodTtwPOU=
|
||||
github.com/gen2brain/cbconvert v1.0.5-0.20260626071631-8155626dbb42/go.mod h1:qHzMhKZ7VBTffwDQ/9rc4yZ9FO5677ZSjSFZ7QNfaLw=
|
||||
github.com/gen2brain/go-fitz v1.24.15 h1:sJNB1MOWkqnzzENPHggFpgxTwW0+S5WF/rM5wUBpJWo=
|
||||
github.com/gen2brain/go-fitz v1.24.15/go.mod h1:SftkiVbTHqF141DuiLwBBM65zP7ig6AVDQpf2WlHamo=
|
||||
github.com/gen2brain/go-fitz v1.28.0 h1:RovqgQPAcOuyv5HZrWsTWl8qwlwbAHSKcAZXZUw0Vlk=
|
||||
github.com/gen2brain/iup-go/iup v0.32.1-0.20260626100855-f328861e3291 h1:ad/nhBGhknOGDpiHnQ0ZLltZccG82t4tAKK94SrQ8OY=
|
||||
github.com/gen2brain/iup-go/iup v0.32.1-0.20260626100855-f328861e3291/go.mod h1:V4f7tHOJAeHtjQ+ju795QKv6DGdLEb4L5cmWB1sjSzU=
|
||||
github.com/gen2brain/jpegli v0.4.1 h1:qc11IQU0jTYFltroulT4MXmbu9YRftqHV0YBZ0Bqz5o=
|
||||
@@ -69,8 +68,6 @@ github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyf
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25 h1:YLvr1eE6cdCqjOe972w/cYF+FjW34v27+9Vo5106B4M=
|
||||
github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25/go.mod h1:kLgvv7o6UM+0QSf0QjAse3wReFDsb9qbZJdfexWlrQw=
|
||||
github.com/jupiterrider/ffi v0.7.0 h1:RKsl6Ascal+3kyAqR5Qcbp83LceQMLc1VZbPfHWoNzs=
|
||||
github.com/jupiterrider/ffi v0.7.0/go.mod h1:9dauhpOfNqrqk28fxuu0kkdeFtT9Qr4vbfigiuIXN7c=
|
||||
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
|
||||
github.com/klauspost/compress v1.18.6 h1:2jupLlAwFm95+YDR+NwD2MEfFO9d4z4Prjl1XXDjuao=
|
||||
github.com/klauspost/compress v1.18.6/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||
|
||||
Reference in New Issue
Block a user