move epubimageprocessor outside epub

This commit is contained in:
Celogeek 2024-01-04 19:22:08 +01:00
parent 55b8bbe6b3
commit db58244946
Signed by: celogeek
SSH Key Fingerprint: SHA256:DEDfxIK2nUWXbslbRkww3zsauDjhWHlTXar+ak4lDJ4
4 changed files with 62 additions and 54 deletions

View File

@ -16,8 +16,8 @@ import (
"time"
epubimage "github.com/celogeek/go-comic-converter/v2/pkg/epub/image"
epubimageprocessor "github.com/celogeek/go-comic-converter/v2/pkg/epub/imageprocessor"
epubtemplates "github.com/celogeek/go-comic-converter/v2/pkg/epub/templates"
"github.com/celogeek/go-comic-converter/v2/pkg/epubimageprocessor"
"github.com/celogeek/go-comic-converter/v2/pkg/epuboptions"
"github.com/celogeek/go-comic-converter/v2/pkg/epubprogress"
"github.com/celogeek/go-comic-converter/v2/pkg/epubtree"

View File

@ -6,7 +6,6 @@ package epubimageprocessor
import (
"fmt"
"image"
"image/color"
"image/draw"
"os"
"sync"
@ -289,55 +288,3 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image.
return images
}
type CoverTitleDataOptions struct {
Src image.Image
Name string
Text string
Align string
PctWidth int
PctMargin int
MaxFontSize int
BorderSize int
}
func (e *EPUBImageProcessor) Cover16LevelOfGray(bounds image.Rectangle) draw.Image {
return image.NewPaletted(bounds, color.Palette{
color.Gray{0x00},
color.Gray{0x11},
color.Gray{0x22},
color.Gray{0x33},
color.Gray{0x44},
color.Gray{0x55},
color.Gray{0x66},
color.Gray{0x77},
color.Gray{0x88},
color.Gray{0x99},
color.Gray{0xAA},
color.Gray{0xBB},
color.Gray{0xCC},
color.Gray{0xDD},
color.Gray{0xEE},
color.Gray{0xFF},
})
}
// create a title page with the cover
func (e *EPUBImageProcessor) CoverTitleData(o *CoverTitleDataOptions) (*epubzip.EPUBZipImage, error) {
// Create a blur version of the cover
g := gift.New(epubimagefilters.CoverTitle(o.Text, o.Align, o.PctWidth, o.PctMargin, o.MaxFontSize, o.BorderSize))
var dst draw.Image
if o.Name == "cover" && e.Image.GrayScale {
dst = e.Cover16LevelOfGray(o.Src.Bounds())
} else {
dst = e.createImage(o.Src, g.Bounds(o.Src.Bounds()))
}
g.Draw(dst, o.Src)
return epubzip.CompressImage(
fmt.Sprintf("OEBPS/Images/%s.%s", o.Name, e.Image.Format),
e.Image.Format,
dst,
e.Image.Quality,
)
}

View File

@ -0,0 +1,61 @@
package epubimageprocessor
import (
"fmt"
"image"
"image/color"
"image/draw"
epubimagefilters "github.com/celogeek/go-comic-converter/v2/pkg/epub/imagefilters"
"github.com/celogeek/go-comic-converter/v2/pkg/epubzip"
"github.com/disintegration/gift"
)
type CoverTitleDataOptions struct {
Src image.Image
Name string
Text string
Align string
PctWidth int
PctMargin int
MaxFontSize int
BorderSize int
}
// create a title page with the cover
func (e *EPUBImageProcessor) CoverTitleData(o *CoverTitleDataOptions) (*epubzip.EPUBZipImage, error) {
// Create a blur version of the cover
g := gift.New(epubimagefilters.CoverTitle(o.Text, o.Align, o.PctWidth, o.PctMargin, o.MaxFontSize, o.BorderSize))
var dst draw.Image
if o.Name == "cover" && e.Image.GrayScale {
// 16 shade of gray
dst = image.NewPaletted(o.Src.Bounds(), color.Palette{
color.Gray{0x00},
color.Gray{0x11},
color.Gray{0x22},
color.Gray{0x33},
color.Gray{0x44},
color.Gray{0x55},
color.Gray{0x66},
color.Gray{0x77},
color.Gray{0x88},
color.Gray{0x99},
color.Gray{0xAA},
color.Gray{0xBB},
color.Gray{0xCC},
color.Gray{0xDD},
color.Gray{0xEE},
color.Gray{0xFF},
})
} else {
dst = e.createImage(o.Src, g.Bounds(o.Src.Bounds()))
}
g.Draw(dst, o.Src)
return epubzip.CompressImage(
fmt.Sprintf("OEBPS/Images/%s.%s", o.Name, e.Image.Format),
e.Image.Format,
dst,
e.Image.Quality,
)
}