passthrough, add png support for dir reading

This commit is contained in:
Celogeek 2025-02-16 16:55:57 +01:00
parent ad614d09b4
commit af261d3f75
Signed by: celogeek
GPG Key ID: 850295F3747870DD

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"image" "image"
"image/jpeg" "image/jpeg"
"image/png"
"io" "io"
"io/fs" "io/fs"
"os" "os"
@ -56,7 +57,7 @@ func (e EPUBImageProcessor) passThroughDir() (images []epubimage.EPUBImage, err
return nil return nil
} }
if slices.Contains([]string{".jpeg", ".jpg"}, strings.ToLower(filepath.Ext(path))) { if slices.Contains([]string{".jpeg", ".jpg", ".png"}, strings.ToLower(filepath.Ext(path))) {
imagesPath = append(imagesPath, path) imagesPath = append(imagesPath, path)
} }
return nil return nil
@ -105,27 +106,44 @@ func (e EPUBImageProcessor) passThroughDir() (images []epubimage.EPUBImage, err
return return
} }
p, fn := filepath.Split(imgPath)
if p == input {
p = ""
} else {
p = p[len(input)+1:]
}
var (
format string
decodeConfig func(r io.Reader) (image.Config, error)
decode func(r io.Reader) (image.Image, error)
)
switch filepath.Ext(fn) {
case ".png":
format = "png"
decodeConfig = png.DecodeConfig
decode = png.Decode
case ".jpg", ".jpeg":
format = "jpeg"
decodeConfig = jpeg.DecodeConfig
decode = jpeg.Decode
}
var config image.Config var config image.Config
config, err = jpeg.DecodeConfig(bytes.NewReader(uncompressedData)) config, err = decodeConfig(bytes.NewReader(uncompressedData))
if err != nil { if err != nil {
return return
} }
var rawImage image.Image var rawImage image.Image
if i == 0 { if i == 0 {
rawImage, err = jpeg.Decode(bytes.NewReader(uncompressedData)) rawImage, err = decode(bytes.NewReader(uncompressedData))
if err != nil { if err != nil {
return return
} }
} }
p, fn := filepath.Split(imgPath)
if p == input {
p = ""
} else {
p = p[len(input)+1:]
}
img := epubimage.EPUBImage{ img := epubimage.EPUBImage{
Id: i, Id: i,
Part: 0, Part: 0,
@ -136,7 +154,7 @@ func (e EPUBImageProcessor) passThroughDir() (images []epubimage.EPUBImage, err
DoublePage: config.Width > config.Height, DoublePage: config.Width > config.Height,
Path: p, Path: p,
Name: fn, Name: fn,
Format: "jpeg", Format: format,
OriginalAspectRatio: float64(config.Height) / float64(config.Width), OriginalAspectRatio: float64(config.Height) / float64(config.Width),
} }