Compare commits

...

3 Commits

Author SHA1 Message Date
1bc85f4c2d
change noblankpage by noblankimage
detect blank image even if crop is disabled
2023-04-28 17:25:43 +02:00
fd796be892
fix double page render
if spread is used, we need 2 pages when left or right are used

if that case we add a special layout blank to fill the gap
2023-04-28 13:41:18 +02:00
341fd2649e
remove blur on title image 2023-04-28 13:17:35 +02:00
10 changed files with 77 additions and 59 deletions

View File

@ -107,7 +107,7 @@ func (c *Converter) InitParse() {
c.AddBoolParam(&c.Options.AutoRotate, "autorotate", c.Options.AutoRotate, "Auto Rotate page when width > height")
c.AddBoolParam(&c.Options.Auto, "auto", false, "Activate all automatic options")
c.AddBoolParam(&c.Options.AutoSplitDoublePage, "autosplitdoublepage", c.Options.AutoSplitDoublePage, "Auto Split double page when width > height")
c.AddBoolParam(&c.Options.NoBlankPage, "noblankpage", c.Options.NoBlankPage, "Remove blank pages")
c.AddBoolParam(&c.Options.NoBlankImage, "noblankimage", c.Options.NoBlankImage, "Remove blank image")
c.AddBoolParam(&c.Options.Manga, "manga", c.Options.Manga, "Manga mode (right to left)")
c.AddBoolParam(&c.Options.HasCover, "hascover", c.Options.HasCover, "Has cover. Indicate if your comic have a cover. The first page will be used as a cover and include after the title.")
c.AddIntParam(&c.Options.LimitMb, "limitmb", c.Options.LimitMb, "Limit size of the ePub: Default nolimit (0), Minimum 20")

View File

@ -33,7 +33,7 @@ type Options struct {
Auto bool `yaml:"-"`
AutoRotate bool `yaml:"auto_rotate"`
AutoSplitDoublePage bool `yaml:"auto_split_double_page"`
NoBlankPage bool `yaml:"no_blank_page"`
NoBlankImage bool `yaml:"no_blank_image"`
Manga bool `yaml:"manga"`
HasCover bool `yaml:"has_cover"`
LimitMb int `yaml:"limit_mb"`
@ -71,7 +71,7 @@ func New() *Options {
Contrast: 0,
AutoRotate: false,
AutoSplitDoublePage: false,
NoBlankPage: false,
NoBlankImage: true,
Manga: false,
HasCover: true,
LimitMb: 0,
@ -172,7 +172,7 @@ func (o *Options) ShowConfig() string {
Contrast : %d
AutoRotate : %v
AutoSplitDoublePage : %v
NoBlankPage : %v
NoBlankImage : %v
Manga : %v
HasCover : %v
LimitMb : %s
@ -188,7 +188,7 @@ func (o *Options) ShowConfig() string {
o.Contrast,
o.AutoRotate,
o.AutoSplitDoublePage,
o.NoBlankPage,
o.NoBlankImage,
o.Manga,
o.HasCover,
limitmb,

View File

@ -70,7 +70,7 @@ func (e *ePub) render(templateString string, data map[string]any) string {
// write image to the zip
func (e *ePub) writeImage(wz *epubzip.EpubZip, img *epubimageprocessor.LoadedImage) error {
err := wz.WriteContent(
fmt.Sprintf("OEBPS/%s", img.Image.TextPath()),
fmt.Sprintf("OEBPS/%s", img.Image.PagePath()),
[]byte(e.render(epubtemplates.Text, map[string]any{
"Title": fmt.Sprintf("Image %d Part %d", img.Image.Id, img.Image.Part),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height),
@ -289,14 +289,14 @@ func (e *ePub) Write() error {
}
}
for i, img := range part.LoadedImages {
lastImage := part.LoadedImages[len(part.LoadedImages)-1]
for _, img := range part.LoadedImages {
if err := e.writeImage(wz, img); err != nil {
return err
}
// Double Page or Last Image that is not a double page
if img.Image.DoublePage ||
(img.Image.Part == 0 && i+1 == len(part.LoadedImages)) {
if img.Image.DoublePage || (img.Image.Part == 0 && img == lastImage) {
if err := e.writeBlank(wz, img.Image); err != nil {
return err
}

View File

@ -15,34 +15,40 @@ type Image struct {
Width int
Height int
IsCover bool
IsBlank bool
DoublePage bool
Path string
Name string
}
// key name of the image
func (i *Image) Key(prefix string) string {
return fmt.Sprintf("%s_%d_p%d", prefix, i.Id, i.Part)
}
// key name of the blank plage after the image
func (i *Image) SpaceKey(prefix string) string {
return fmt.Sprintf("%s_%d_sp", prefix, i.Id)
func (i *Image) SpaceKey() string {
return fmt.Sprintf("space_%d", i.Id)
}
// path of the blank page
func (i *Image) SpacePath() string {
return fmt.Sprintf("Text/%d_sp.xhtml", i.Id)
return fmt.Sprintf("Text/%s.xhtml", i.SpaceKey())
}
// text path linked to the image
func (i *Image) TextPath() string {
return fmt.Sprintf("Text/%d_p%d.xhtml", i.Id, i.Part)
// key for page
func (i *Image) PageKey() string {
return fmt.Sprintf("page_%d_p%d", i.Id, i.Part)
}
// page path linked to the image
func (i *Image) PagePath() string {
return fmt.Sprintf("Text/%s.xhtml", i.PageKey())
}
// key for image
func (i *Image) ImgKey() string {
return fmt.Sprintf("img_%d_p%d", i.Id, i.Part)
}
// image path
func (i *Image) ImgPath() string {
return fmt.Sprintf("Images/%d_p%d.jpg", i.Id, i.Part)
return fmt.Sprintf("Images/%s.jpg", i.ImgKey())
}
// style to apply to the image.

View File

@ -27,9 +27,7 @@ func (p *coverTitle) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangl
// blur the src image, and create a box with the title in the middle
func (p *coverTitle) Draw(dst draw.Image, src image.Image, options *gift.Options) {
// Create a blur version of the cover
g := gift.New(gift.GaussianBlur(4))
g.Draw(dst, src)
draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Src)
srcWidth, srcHeight := src.Bounds().Dx(), src.Bounds().Dy()

View File

@ -96,6 +96,7 @@ func (e *EpubImageProcessor) Load() (LoadedImages, error) {
Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(),
IsCover: input.Id == 0 && part == 0,
IsBlank: dst.Bounds().Dx() == 1 && dst.Bounds().Dy() == 1,
DoublePage: part == 0 && src.Bounds().Dx() > src.Bounds().Dy(),
Path: input.Path,
Name: input.Name,
@ -118,7 +119,7 @@ func (e *EpubImageProcessor) Load() (LoadedImages, error) {
if output.Image.Part == 0 {
bar.Add(1)
}
if e.Image.NoBlankPage && output.Image.Width == 1 && output.Image.Height == 1 {
if e.Image.NoBlankImage && output.Image.IsBlank {
continue
}
images = append(images, output)
@ -138,7 +139,8 @@ func (e *EpubImageProcessor) transformImage(src image.Image, srcId int) []image.
var filters, splitFilter []gift.Filter
var images []image.Image
if e.Image.Crop.Enabled {
// Lookup for margin if crop is enable or if we want to remove blank image
if e.Image.Crop.Enabled || e.Image.NoBlankImage {
f := epubimagefilters.AutoCrop(
src,
e.Image.Crop.Left,
@ -146,8 +148,16 @@ func (e *EpubImageProcessor) transformImage(src image.Image, srcId int) []image.
e.Image.Crop.Right,
e.Image.Crop.Bottom,
)
filters = append(filters, f)
splitFilter = append(splitFilter, f)
// detect if blank image
size := f.Bounds(src.Bounds())
isBlank := size.Dx() == 0 && size.Dy() == 0
// crop is enable or if blank image with noblankimage options
if e.Image.Crop.Enabled || (e.Image.NoBlankImage && isBlank) {
filters = append(filters, f)
splitFilter = append(splitFilter, f)
}
}
if e.Image.AutoRotate && src.Bounds().Dx() > src.Bounds().Dy() {

View File

@ -19,7 +19,7 @@ type Image struct {
Contrast int
AutoRotate bool
AutoSplitDoublePage bool
NoBlankPage bool
NoBlankImage bool
Manga bool
HasCover bool
View *View

View File

@ -109,7 +109,7 @@ func getMeta(o *ContentOptions) []tag {
}
if o.Cover != nil {
metas = append(metas, tag{"meta", tagAttrs{"name": "cover", "content": o.Cover.Key("img")}, ""})
metas = append(metas, tag{"meta", tagAttrs{"name": "cover", "content": o.Cover.ImgKey()}, ""})
}
if o.Total > 1 {
@ -124,15 +124,21 @@ func getMeta(o *ContentOptions) []tag {
}
func getManifest(o *ContentOptions) []tag {
itag := func(img *epubimage.Image) tag {
return tag{"item", tagAttrs{"id": img.Key("img"), "href": img.ImgPath(), "media-type": "image/jpeg"}, ""}
}
htag := func(img *epubimage.Image) tag {
return tag{"item", tagAttrs{"id": img.Key("page"), "href": img.TextPath(), "media-type": "application/xhtml+xml"}, ""}
}
stag := func(img *epubimage.Image) tag {
return tag{"item", tagAttrs{"id": img.SpaceKey("page"), "href": img.SpacePath(), "media-type": "application/xhtml+xml"}, ""}
var imageTags, pageTags, spaceTags []tag
addTag := func(img *epubimage.Image, withSpace bool) {
imageTags = append(imageTags,
tag{"item", tagAttrs{"id": img.ImgKey(), "href": img.ImgPath(), "media-type": "image/jpeg"}, ""},
)
pageTags = append(pageTags,
tag{"item", tagAttrs{"id": img.PageKey(), "href": img.PagePath(), "media-type": "application/xhtml+xml"}, ""},
)
if withSpace {
spaceTags = append(spaceTags,
tag{"item", tagAttrs{"id": img.SpaceKey(), "href": img.SpacePath(), "media-type": "application/xhtml+xml"}, ""},
)
}
}
items := []tag{
{"item", tagAttrs{"id": "toc", "href": "toc.xhtml", "properties": "nav", "media-type": "application/xhtml+xml"}, ""},
{"item", tagAttrs{"id": "css", "href": "Text/style.css", "media-type": "text/css"}, ""},
@ -141,20 +147,18 @@ func getManifest(o *ContentOptions) []tag {
}
if o.ImageOptions.HasCover || o.Current > 1 {
items = append(items, itag(o.Cover), htag(o.Cover))
addTag(o.Cover, false)
}
for _, img := range o.Images {
if img.Part == 1 {
items = append(items, stag(img))
}
items = append(items, itag(img), htag(img))
}
lastImage := o.Images[len(o.Images)-1]
if lastImage.Part == 0 {
items = append(items, stag(lastImage))
for _, img := range o.Images {
addTag(img, img.DoublePage || (img.Part == 0 && img == lastImage))
}
items = append(items, imageTags...)
items = append(items, pageTags...)
items = append(items, spaceTags...)
return items
}
@ -179,23 +183,23 @@ func getSpine(o *ContentOptions) []tag {
{"itemref", tagAttrs{"idref": "page_title", "properties": getSpread(true)}, ""},
}
for _, img := range o.Images {
spine = append(spine, tag{
"itemref",
tagAttrs{"idref": img.Key("page"), "properties": getSpread(img.DoublePage && o.ImageOptions.NoBlankPage)},
"",
})
if img.DoublePage && isOnTheRight && !o.ImageOptions.NoBlankPage {
if img.DoublePage && isOnTheRight {
spine = append(spine, tag{
"itemref",
tagAttrs{"idref": img.SpaceKey("page"), "properties": getSpread(false)},
tagAttrs{"idref": img.SpaceKey(), "properties": getSpread(false) + " layout-blank"},
"",
})
}
spine = append(spine, tag{
"itemref",
tagAttrs{"idref": img.PageKey(), "properties": getSpread(img.DoublePage)},
"",
})
}
if o.ImageOptions.Manga == isOnTheRight {
spine = append(spine, tag{
"itemref",
tagAttrs{"idref": o.Images[len(o.Images)-1].SpaceKey("page"), "properties": getSpread(false)},
tagAttrs{"idref": o.Images[len(o.Images)-1].SpaceKey(), "properties": getSpread(false)},
"",
})
}
@ -207,8 +211,8 @@ func getSpine(o *ContentOptions) []tag {
func getGuide(o *ContentOptions) []tag {
guide := []tag{}
if o.Cover != nil {
guide = append(guide, tag{"reference", tagAttrs{"type": "cover", "title": "cover", "href": o.Cover.TextPath()}, ""})
guide = append(guide, tag{"reference", tagAttrs{"type": "cover", "title": "cover", "href": o.Cover.PagePath()}, ""})
}
guide = append(guide, tag{"reference", tagAttrs{"type": "text", "title": "content", "href": o.Images[0].TextPath()}, ""})
guide = append(guide, tag{"reference", tagAttrs{"type": "text", "title": "content", "href": o.Images[0].PagePath()}, ""})
return guide
}

View File

@ -37,7 +37,7 @@ func Toc(title string, stripFirstDirectoryFromToc bool, images []*epubimage.Imag
}
t := paths[parentPath].CreateElement("li")
link := t.CreateElement("a")
link.CreateAttr("href", img.TextPath())
link.CreateAttr("href", img.PagePath())
link.CreateText(path)
paths[currentPath] = t.CreateElement("ol")
}

View File

@ -126,7 +126,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
Contrast: cmd.Options.Contrast,
AutoRotate: cmd.Options.AutoRotate,
AutoSplitDoublePage: cmd.Options.AutoSplitDoublePage,
NoBlankPage: cmd.Options.NoBlankPage,
NoBlankImage: cmd.Options.NoBlankImage,
Manga: cmd.Options.Manga,
HasCover: cmd.Options.HasCover,
View: &epuboptions.View{