From 0f3d55279ebe3c120db4ac1db3eaece09860be37 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 15 Jan 2023 14:35:53 +0100 Subject: [PATCH] add pixel to avoid empty image --- internal/epub/filters/pixel.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 internal/epub/filters/pixel.go diff --git a/internal/epub/filters/pixel.go b/internal/epub/filters/pixel.go new file mode 100644 index 0000000..c9bc071 --- /dev/null +++ b/internal/epub/filters/pixel.go @@ -0,0 +1,32 @@ +package filters + +import ( + "image" + "image/color" + "image/draw" + + "github.com/disintegration/gift" +) + +func Pixel() gift.Filter { + return &pixel{} +} + +type pixel struct { +} + +func (p *pixel) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) { + if srcBounds.Dx() == 0 || srcBounds.Dy() == 0 { + dstBounds = image.Rect(0, 0, 1, 1) + } else { + dstBounds = srcBounds + } + return +} + +func (p *pixel) Draw(dst draw.Image, src image.Image, options *gift.Options) { + if dst.Bounds().Dx() == 1 && dst.Bounds().Dy() == 1 { + dst.Set(0, 0, color.White) + } + draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Src) +}