Compare commits

..

No commits in common. "721d373a7f4b3b0b4158c5335963c83bdfb33eb8" and "0263a643215f983030d52b03b79c43a0d56f92a6" have entirely different histories.

15 changed files with 196 additions and 207 deletions

View File

@ -98,10 +98,6 @@ func (c *Converter) InitParse() {
c.AddStringParam(&c.Options.Profile, "profile", c.Options.Profile, fmt.Sprintf("Profile to use: \n%s", c.Options.AvailableProfiles()))
c.AddIntParam(&c.Options.Quality, "quality", c.Options.Quality, "Quality of the image")
c.AddBoolParam(&c.Options.Crop, "crop", c.Options.Crop, "Crop images")
c.AddIntParam(&c.Options.CropRatioLeft, "crop-ratio-left", c.Options.CropRatioLeft, "Crop ratio left: ratio of pixels allow to be non blank while cutting on the left.")
c.AddIntParam(&c.Options.CropRatioUp, "crop-ratio-up", c.Options.CropRatioUp, "Crop ratio up: ratio of pixels allow to be non blank while cutting on the top.")
c.AddIntParam(&c.Options.CropRatioRight, "crop-ratio-right", c.Options.CropRatioRight, "Crop ratio right: ratio of pixels allow to be non blank while cutting on the right.")
c.AddIntParam(&c.Options.CropRatioBottom, "crop-ratio-bottom", c.Options.CropRatioBottom, "Crop ratio bottom: ratio of pixels allow to be non blank while cutting on the bottom.")
c.AddIntParam(&c.Options.Brightness, "brightness", c.Options.Brightness, "Brightness readjustement: between -100 and 100, > 0 lighter, < 0 darker")
c.AddIntParam(&c.Options.Contrast, "contrast", c.Options.Contrast, "Contrast readjustement: between -100 and 100, > 0 more contrast, < 0 less contrast")
c.AddBoolParam(&c.Options.AutoRotate, "autorotate", c.Options.AutoRotate, "Auto Rotate page when width > height")

View File

@ -24,10 +24,6 @@ type Options struct {
Profile string `yaml:"profile"`
Quality int `yaml:"quality"`
Crop bool `yaml:"crop"`
CropRatioLeft int `yaml:"crop_ratio_left"`
CropRatioUp int `yaml:"crop_ratio_up"`
CropRatioRight int `yaml:"crop_ratio_right"`
CropRatioBottom int `yaml:"crop_ratio_bottom"`
Brightness int `yaml:"brightness"`
Contrast int `yaml:"contrast"`
Auto bool `yaml:"-"`
@ -63,10 +59,6 @@ func New() *Options {
Profile: "",
Quality: 85,
Crop: true,
CropRatioLeft: 1,
CropRatioUp: 1,
CropRatioRight: 1,
CropRatioBottom: 3,
Brightness: 0,
Contrast: 0,
AutoRotate: false,
@ -167,7 +159,6 @@ func (o *Options) ShowConfig() string {
View : %s
Quality : %d
Crop : %v
CropRatio : %d Left - %d Up - %d Right - %d Bottom
Brightness : %d
Contrast : %d
AutoRotate : %v
@ -183,7 +174,6 @@ func (o *Options) ShowConfig() string {
viewDesc,
o.Quality,
o.Crop,
o.CropRatioLeft, o.CropRatioUp, o.CropRatioRight, o.CropRatioBottom,
o.Brightness,
o.Contrast,
o.AutoRotate,

View File

@ -237,7 +237,6 @@ func (e *ePub) Write() error {
Description: "Writing Part",
CurrentJob: 2,
TotalJob: 2,
Quiet: e.Quiet,
})
for i, part := range epubParts {
ext := filepath.Ext(e.Output)

View File

@ -0,0 +1,35 @@
/*
Rotate image if the source width > height.
*/
package epubfilters
import (
"image"
"image/draw"
"github.com/disintegration/gift"
)
func AutoRotate() gift.Filter {
return &autoRotateFilter{}
}
type autoRotateFilter struct {
}
func (p *autoRotateFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) {
if srcBounds.Dx() > srcBounds.Dy() {
dstBounds = gift.Rotate90().Bounds(srcBounds)
} else {
dstBounds = srcBounds
}
return
}
func (p *autoRotateFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
if src.Bounds().Dx() > src.Bounds().Dy() {
gift.Rotate90().Draw(dst, src, options)
} else {
draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Src)
}
}

View File

@ -1,4 +1,7 @@
package epubimagefilters
/*
Create a title with the cover image
*/
package epubfilters
import (
"image"
@ -11,7 +14,6 @@ import (
"golang.org/x/image/font/gofont/gomonobold"
)
// Create a title with the cover image
func CoverTitle(title string) gift.Filter {
return &coverTitle{title}
}

View File

@ -1,4 +1,9 @@
package epubimagefilters
/*
cut a double page in 2 part: left and right.
this will cut in the middle of the page.
*/
package epubfilters
import (
"image"
@ -7,8 +12,6 @@ import (
"github.com/disintegration/gift"
)
// Cut a double page in 2 part: left and right.
// This will cut in the middle of the page.
func CropSplitDoublePage(right bool) gift.Filter {
return &cropSplitDoublePage{right}
}

View File

@ -1,4 +1,9 @@
package epubimagefilters
/*
generate a blank pixel 1x1, if the size of the image is 0x0.
An image 0x0 is not a valid image, and failed to read.
*/
package epubfilters
import (
"image"
@ -8,8 +13,6 @@ import (
"github.com/disintegration/gift"
)
// Generate a blank pixel 1x1, if the size of the image is 0x0.
// An image 0x0 is not a valid image, and failed to read.
func Pixel() gift.Filter {
return &pixel{}
}

View File

@ -1,4 +1,9 @@
package epubimagefilters
/*
Resize image by keeping aspect ratio.
This will reduce or enlarge image to fit into the viewWidth and viewHeight.
*/
package epubfilters
import (
"image"
@ -7,8 +12,6 @@ import (
"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,

View File

@ -0,0 +1,55 @@
package epubimage
import (
epubfilters "github.com/celogeek/go-comic-converter/v2/internal/epub/filters"
"github.com/disintegration/gift"
)
// create filter to apply to the source
func NewGift(options *Options) *gift.GIFT {
g := gift.New()
g.SetParallelization(false)
if options.AutoRotate {
g.Add(epubfilters.AutoRotate())
}
if options.Contrast != 0 {
g.Add(gift.Contrast(float32(options.Contrast)))
}
if options.Brightness != 0 {
g.Add(gift.Brightness(float32(options.Brightness)))
}
g.Add(
epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
epubfilters.Pixel(),
)
return g
}
// create filters to cut image into 2 equal pieces
func NewGiftSplitDoublePage(options *Options) []*gift.GIFT {
gifts := make([]*gift.GIFT, 2)
gifts[0] = gift.New(
epubfilters.CropSplitDoublePage(options.Manga),
)
gifts[1] = gift.New(
epubfilters.CropSplitDoublePage(!options.Manga),
)
for _, g := range gifts {
if options.Contrast != 0 {
g.Add(gift.Contrast(float32(options.Contrast)))
}
if options.Brightness != 0 {
g.Add(gift.Brightness(float32(options.Brightness)))
}
g.Add(
epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
)
}
return gifts
}

View File

@ -3,10 +3,6 @@ package epubimage
// options for image transformation
type Options struct {
Crop bool
CropRatioLeft int
CropRatioUp int
CropRatioRight int
CropRatioBottom int
ViewWidth int
ViewHeight int
Quality int

View File

@ -1,70 +0,0 @@
package epubimagefilters
import (
"image"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
"github.com/disintegration/gift"
)
// create filter to apply to the source
func NewGift(img image.Image, options *epubimage.Options) *gift.GIFT {
g := gift.New()
g.SetParallelization(false)
if options.Crop {
g.Add(AutoCrop(
img,
options.CropRatioLeft,
options.CropRatioUp,
options.CropRatioRight,
options.CropRatioBottom,
))
}
if options.AutoRotate && img.Bounds().Dx() > img.Bounds().Dy() {
g.Add(gift.Rotate90())
}
if options.Contrast != 0 {
g.Add(gift.Contrast(float32(options.Contrast)))
}
if options.Brightness != 0 {
g.Add(gift.Brightness(float32(options.Brightness)))
}
g.Add(
Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
Pixel(),
)
return g
}
// create filters to cut image into 2 equal pieces
func NewGiftSplitDoublePage(options *epubimage.Options) []*gift.GIFT {
gifts := make([]*gift.GIFT, 2)
gifts[0] = gift.New(
CropSplitDoublePage(options.Manga),
)
gifts[1] = gift.New(
CropSplitDoublePage(!options.Manga),
)
for _, g := range gifts {
g.SetParallelization(false)
if options.Contrast != 0 {
g.Add(gift.Contrast(float32(options.Contrast)))
}
if options.Brightness != 0 {
g.Add(gift.Brightness(float32(options.Brightness)))
}
g.Add(
Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
)
}
return gifts
}

View File

@ -1,88 +0,0 @@
package epubimagefilters
import (
"image"
"image/color"
"github.com/disintegration/gift"
)
// Lookup for margin and crop
func AutoCrop(img image.Image, cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom int) gift.Filter {
return gift.Crop(
findMarging(img, cutRatioOptions{cutRatioLeft, cutRatioUp, cutRatioRight, cutRatioBottom}),
)
}
// check if the color is blank enough
func colorIsBlank(c color.Color) bool {
g := color.GrayModel.Convert(c).(color.Gray)
return g.Y >= 0xe0
}
// lookup for margin (blank) around the image
type cutRatioOptions struct {
Left, Up, Right, Bottom int
}
func findMarging(img image.Image, cutRatio cutRatioOptions) image.Rectangle {
imgArea := img.Bounds()
LEFT:
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
allowNonBlank := imgArea.Dy() * cutRatio.Left / 100
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
if !colorIsBlank(img.At(x, y)) {
allowNonBlank--
if allowNonBlank <= 0 {
break LEFT
}
}
}
imgArea.Min.X++
}
UP:
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
allowNonBlank := imgArea.Dx() * cutRatio.Up / 100
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
if !colorIsBlank(img.At(x, y)) {
allowNonBlank--
if allowNonBlank <= 0 {
break UP
}
}
}
imgArea.Min.Y++
}
RIGHT:
for x := imgArea.Max.X - 1; x >= imgArea.Min.X; x-- {
allowNonBlank := imgArea.Dy() * cutRatio.Right / 100
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
if !colorIsBlank(img.At(x, y)) {
allowNonBlank--
if allowNonBlank <= 0 {
break RIGHT
}
}
}
imgArea.Max.X--
}
BOTTOM:
for y := imgArea.Max.Y - 1; y >= imgArea.Min.Y; y-- {
allowNonBlank := imgArea.Dx() * cutRatio.Bottom / 100
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
if !colorIsBlank(img.At(x, y)) {
allowNonBlank--
if allowNonBlank <= 0 {
break BOTTOM
}
}
}
imgArea.Max.Y--
}
return imgArea
}

View File

@ -14,9 +14,9 @@ import (
"strings"
"sync"
epubfilters "github.com/celogeek/go-comic-converter/v2/internal/epub/filters"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
epubimagedata "github.com/celogeek/go-comic-converter/v2/internal/epub/imagedata"
epubimagefilters "github.com/celogeek/go-comic-converter/v2/internal/epub/imagefilters"
epubprogress "github.com/celogeek/go-comic-converter/v2/internal/epub/progress"
"github.com/disintegration/gift"
_ "golang.org/x/image/webp"
@ -29,17 +29,6 @@ type tasks struct {
Name string
}
// only accept jpg, png and webp as source file
func isSupportedImage(path string) bool {
switch strings.ToLower(filepath.Ext(path)) {
case ".jpg", ".jpeg", ".png", ".webp":
{
return true
}
}
return false
}
// extract and convert images
func LoadImages(o *Options) ([]*epubimage.Image, error) {
images := make([]*epubimage.Image, 0)
@ -112,7 +101,15 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
os.Exit(1)
}
g := epubimagefilters.NewGift(src, o.Image)
if o.Image.Crop {
g := gift.New(gift.Crop(findMarging(src)))
newSrc := image.NewNRGBA(g.Bounds(src.Bounds()))
g.Draw(newSrc, src)
src = newSrc
}
g := epubimage.NewGift(o.Image)
// Convert image
dst := image.NewGray(g.Bounds(src.Bounds()))
g.Draw(dst, src)
@ -141,7 +138,7 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
if (!o.Image.HasCover || img.Id > 0) &&
o.Image.AutoSplitDoublePage &&
src.Bounds().Dx() > src.Bounds().Dy() {
gifts := epubimagefilters.NewGiftSplitDoublePage(o.Image)
gifts := epubimage.NewGiftSplitDoublePage(o.Image)
for i, g := range gifts {
part := i + 1
dst := image.NewGray(g.Bounds(src.Bounds()))
@ -190,7 +187,7 @@ func LoadImages(o *Options) ([]*epubimage.Image, error) {
// create a title page with the cover
func LoadCoverTitleData(img *epubimage.Image, title string, quality int) *epubimagedata.ImageData {
// Create a blur version of the cover
g := gift.New(epubimagefilters.CoverTitle(title))
g := gift.New(epubfilters.CoverTitle(title))
dst := image.NewGray(g.Bounds(img.Raw.Bounds()))
g.Draw(dst, img.Raw)

View File

@ -0,0 +1,72 @@
package epubimageprocessing
import (
"image"
"image/color"
"path/filepath"
"strings"
)
// only accept jpg, png and webp as source file
func isSupportedImage(path string) bool {
switch strings.ToLower(filepath.Ext(path)) {
case ".jpg", ".jpeg", ".png", ".webp":
{
return true
}
}
return false
}
// check if the color is blank enough
func colorIsBlank(c color.Color) bool {
g := color.GrayModel.Convert(c).(color.Gray)
return g.Y >= 0xf0
}
// lookup for margin (blank) around the image
func findMarging(img image.Image) image.Rectangle {
imgArea := img.Bounds()
LEFT:
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
if !colorIsBlank(img.At(x, y)) {
break LEFT
}
}
imgArea.Min.X++
}
UP:
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
if !colorIsBlank(img.At(x, y)) {
break UP
}
}
imgArea.Min.Y++
}
RIGHT:
for x := imgArea.Max.X - 1; x >= imgArea.Min.X; x-- {
for y := imgArea.Min.Y; y < imgArea.Max.Y; y++ {
if !colorIsBlank(img.At(x, y)) {
break RIGHT
}
}
imgArea.Max.X--
}
BOTTOM:
for y := imgArea.Max.Y - 1; y >= imgArea.Min.Y; y-- {
for x := imgArea.Min.X; x < imgArea.Max.X; x++ {
if !colorIsBlank(img.At(x, y)) {
break BOTTOM
}
}
imgArea.Max.Y--
}
return imgArea
}

View File

@ -114,10 +114,6 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
ViewHeight: perfectHeight,
Quality: cmd.Options.Quality,
Crop: cmd.Options.Crop,
CropRatioLeft: cmd.Options.CropRatioLeft,
CropRatioUp: cmd.Options.CropRatioUp,
CropRatioRight: cmd.Options.CropRatioRight,
CropRatioBottom: cmd.Options.CropRatioBottom,
Brightness: cmd.Options.Brightness,
Contrast: cmd.Options.Contrast,
AutoRotate: cmd.Options.AutoRotate,