From 13103b0eba44e259f9b982e5094731dc0973bcb1 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Mon, 1 May 2023 14:11:44 +0200 Subject: [PATCH] do not enlarge smaller images all width/height are computed based on viewport dimension. images will enlarge if smaller, and resize to fit if larger. --- internal/epub/image/epub_image.go | 47 +++++++++++++----- .../imagefilters/epub_image_filters_resize.go | 48 ------------------- .../imageprocessor/epub_image_processor.go | 4 +- .../templates/epub_templates_text.xhtml.tmpl | 4 +- 4 files changed, 39 insertions(+), 64 deletions(-) delete mode 100644 internal/epub/imagefilters/epub_image_filters_resize.go diff --git a/internal/epub/image/epub_image.go b/internal/epub/image/epub_image.go index e88a414..8f74281 100644 --- a/internal/epub/image/epub_image.go +++ b/internal/epub/image/epub_image.go @@ -6,6 +6,7 @@ package epubimage import ( "fmt" "image" + "strings" ) type Image struct { @@ -72,24 +73,48 @@ func (i *Image) EPUBImgPath() string { // center by default. // align to left or right if it's part of the splitted double page. func (i *Image) ImgStyle(viewWidth, viewHeight int, align string) string { - marginW, marginH := float64(viewWidth-i.Width)/2, float64(viewHeight-i.Height)/2 + relWidth, relHeight := i.RelSize(viewWidth, viewHeight) + marginW, marginH := float64(viewWidth-relWidth)/2, float64(viewHeight-relHeight)/2 + style := []string{} + + style = append(style, fmt.Sprintf("width:%dpx", relWidth)) + style = append(style, fmt.Sprintf("height:%dpx", relHeight)) + style = append(style, fmt.Sprintf("top:%.2f%%", marginH*100/float64(viewHeight))) if align == "" { switch i.Position { case "rendition:page-spread-left": - align = "right:0" + style = append(style, "right:0") case "rendition:page-spread-right": - align = "left:0" + style = append(style, "left:0") default: - align = fmt.Sprintf("left:%.2f%%", marginW*100/float64(viewWidth)) + style = append(style, fmt.Sprintf("left:%.2f%%", marginW*100/float64(viewWidth))) } + } else { + style = append(style, align) } - return fmt.Sprintf( - "width:%dpx; height:%dpx; top:%.2f%%; %s;", - i.Width, - i.Height, - marginH*100/float64(viewHeight), - align, - ) + return strings.Join(style, "; ") +} + +func (i *Image) RelSize(viewWidth, viewHeight int) (relWidth, relHeight int) { + w, h := viewWidth, viewHeight + srcw, srch := i.Width, i.Height + + if w <= 0 || h <= 0 || srcw <= 0 || srch <= 0 { + return + } + + wratio := float64(srcw) / float64(w) + hratio := float64(srch) / float64(h) + + if wratio > hratio { + relWidth = w + relHeight = int(float64(srch)/wratio + 0.5) + } else { + relHeight = h + relWidth = int(float64(srcw)/hratio + 0.5) + } + + return } diff --git a/internal/epub/imagefilters/epub_image_filters_resize.go b/internal/epub/imagefilters/epub_image_filters_resize.go deleted file mode 100644 index 3e910fb..0000000 --- a/internal/epub/imagefilters/epub_image_filters_resize.go +++ /dev/null @@ -1,48 +0,0 @@ -package epubimagefilters - -import ( - "image" - "image/draw" - - "github.com/disintegration/gift" -) - -// Resize image by keeping aspect ratio. -// This will reduce or enlarge image to fit into the viewWidth and viewHeight. -func Resize(viewWidth, viewHeight int, resampling gift.Resampling) gift.Filter { - return &resizeFilter{ - viewWidth, viewHeight, resampling, - } -} - -type resizeFilter struct { - viewWidth, viewHeight int - resampling gift.Resampling -} - -func (p *resizeFilter) Bounds(srcBounds image.Rectangle) image.Rectangle { - w, h := p.viewWidth, p.viewHeight - srcw, srch := srcBounds.Dx(), srcBounds.Dy() - - if w <= 0 || h <= 0 || srcw <= 0 || srch <= 0 { - return image.Rect(0, 0, 0, 0) - } - - wratio := float64(srcw) / float64(w) - hratio := float64(srch) / float64(h) - - var dstw, dsth int - if wratio > hratio { - dstw = w - dsth = int(float64(srch)/wratio + 0.5) - } else { - dsth = h - dstw = int(float64(srcw)/hratio + 0.5) - } - - return image.Rect(0, 0, dstw, dsth) -} - -func (p *resizeFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) { - gift.Resize(dst.Bounds().Dx(), dst.Bounds().Dy(), p.resampling).Draw(dst, src, options) -} diff --git a/internal/epub/imageprocessor/epub_image_processor.go b/internal/epub/imageprocessor/epub_image_processor.go index 44a794e..948c757 100644 --- a/internal/epub/imageprocessor/epub_image_processor.go +++ b/internal/epub/imageprocessor/epub_image_processor.go @@ -202,7 +202,7 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image. } filters = append(filters, - epubimagefilters.Resize(e.Image.View.Width, e.Image.View.Height, gift.LanczosResampling), + gift.ResizeToFit(e.Image.View.Width, e.Image.View.Height, gift.LanczosResampling), epubimagefilters.Pixel(), ) @@ -234,7 +234,7 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image. g := gift.New(splitFilter...) g.Add( epubimagefilters.CropSplitDoublePage(b), - epubimagefilters.Resize(e.Image.View.Width, e.Image.View.Height, gift.LanczosResampling), + gift.ResizeToFit(e.Image.View.Width, e.Image.View.Height, gift.LanczosResampling), ) dst := e.createImage(src, g.Bounds(src.Bounds())) g.Draw(dst, src) diff --git a/internal/epub/templates/epub_templates_text.xhtml.tmpl b/internal/epub/templates/epub_templates_text.xhtml.tmpl index a88b9e1..21cea8f 100644 --- a/internal/epub/templates/epub_templates_text.xhtml.tmpl +++ b/internal/epub/templates/epub_templates_text.xhtml.tmpl @@ -8,8 +8,6 @@
-