Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Switch image conversion library to rez (fixes #32)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Dec 30, 2017
1 parent 7597c7b commit 62c360f
Show file tree
Hide file tree
Showing 27 changed files with 4,705 additions and 1,966 deletions.
43 changes: 40 additions & 3 deletions modules/booklist/booklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package booklist

import (
"fmt"
"image"
"image/jpeg"
"log"
"math"
"os"
"path/filepath"
"runtime/debug"
"sort"
"strings"

"github.com/bamiaux/rez"
"github.com/geek1011/BookBrowser/formats"
"github.com/geek1011/BookBrowser/models"
zglob "github.com/mattn/go-zglob"
"github.com/nfnt/resize"
)

// BookList represents a list of Books
Expand Down Expand Up @@ -92,8 +94,43 @@ func NewBookListFromDir(dir, coverOutDir string, verbose, nocovers bool) (*BookL
continue
}

// Better quality: thumb := resize.Resize(200, 0, img, resize.Lanczos2)
thumb := resize.Resize(200, 0, cover, resize.Bicubic)
coverBounds := cover.Bounds()
coverWidth := coverBounds.Dx()
coverHeight := coverBounds.Dy()

if coverWidth <= 200 {
continue
}

// Scale to fit in 200x900
scale := math.Min(float64(200.0/float64(coverWidth)), float64(900.0/float64(coverHeight)))

// Scale and round down
coverWidth = int(float64(coverWidth) * scale)
coverHeight = int(float64(coverHeight) * scale)

r := image.Rect(0, 0, coverWidth, coverHeight)
var thumb image.Image
switch t := cover.(type) {
case *image.YCbCr:
thumb = image.NewYCbCr(r, t.SubsampleRatio)
case *image.RGBA:
thumb = image.NewRGBA(r)
case *image.NRGBA:
thumb = image.NewNRGBA(r)
case *image.Gray:
thumb = image.NewGray(r)
default:
continue
}

// rez.NewLanczos(2.0) is faster, but slower
err = rez.Convert(thumb, cover, rez.NewBicubicFilter())
if err != nil {
fmt.Println(coverWidth, coverHeight, scale, err)
continue
}

thumbFile, err := os.Create(thumbPath)
if err != nil {
continue
Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/bamiaux/rez/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions vendor/github.com/bamiaux/rez/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/bamiaux/rez/README.md.template

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions vendor/github.com/bamiaux/rez/filters.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 62c360f

Please sign in to comment.