Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thumbnail generation #17

Merged
merged 20 commits into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 83 additions & 41 deletions kobo-uncaged/kutypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

package main

import "image"
import (
"fmt"
"image"
"path/filepath"
)

type cidPrefix string
type koboDevice string
type koboCoverEnding string

type mboxSection int
type kuPrinter interface {
Expand All @@ -35,12 +37,6 @@ const (
footer
)

const (
fullCover koboCoverEnding = " - N3_FULL.parsed"
libFull koboCoverEnding = " - N3_LIBRARY_FULL.parsed"
libGrid koboCoverEnding = " - N3_LIBRARY_GRID.parsed"
)

// KoboMetadata contains the metadata for ebooks on kobo devices.
// It replicates the metadata available in the Kobo USBMS driver.
// Note, pointers are used where necessary to account for null JSON values
Expand Down Expand Up @@ -77,6 +73,8 @@ type KoboMetadata struct {
Identifiers map[string]string `json:"identifiers" mapstructure:"identifiers"`
}

type koboDevice string

// Kobo model ID's from https://github.com/geek1011/KoboStuff/blob/gh-pages/kobofirmware.js#L11
const (
touchAB koboDevice = "00000000-0000-0000-0000-000000000310"
Expand Down Expand Up @@ -131,39 +129,83 @@ func (d koboDevice) Model() string {
}
}

// CoverSize gets the appropriate cover dimensions for the device.
// These values come from https://github.com/kovidgoyal/calibre/blob/master/src/calibre/devices/kobo/driver.py
func (d koboDevice) CoverSize() (fullCover, libFull, libGrid image.Point) {
var fc, lf, lg image.Point
// FullCover gets the appropriate cover dimensions for the device. These values
// come from Image::sizeForType in the Kobo firmware.
// See https://github.com/shermp/Kobo-UNCaGED/issues/16#issuecomment-494229994
// for more details.
func (d koboDevice) FullCover() image.Point {
switch d {
case glo, aura, auraEd2r1, auraEd2r2:
fc = image.Pt(758, 1024)
lf = image.Pt(355, 479)
lg = image.Pt(149, 201)
case gloHD, claraHD:
fc = image.Pt(1072, 1448)
lf = image.Pt(355, 479)
lg = image.Pt(149, 201)
case auraHD, auraH2Oed2r1, auraH2Oed2r2:
fc = image.Pt(1080, 1440)
lf = image.Pt(355, 471)
lg = image.Pt(149, 198)
case auraH2O:
fc = image.Pt(1080, 1429)
lf = image.Pt(355, 473)
lg = image.Pt(149, 198)
case auraOne, auraOneLE:
fc = image.Pt(1404, 1872)
lf = image.Pt(355, 473)
lg = image.Pt(149, 198)
case forma, forma32gb:
fc = image.Pt(1440, 1920)
lf = image.Pt(398, 530)
lg = image.Pt(167, 223)
case auraOne, auraOneLE: // daylight
return image.Pt(1404, 1872)
case gloHD, claraHD: // alyssum, nova
return image.Pt(1072, 1448)
case auraHD, auraH2O, auraH2Oed2r1, auraH2Oed2r2: // dragon
if d == auraH2O {
// Nickel's behaviour is incorrect as of 4.14.12777.
// See https://github.com/shermp/Kobo-UNCaGED/pull/17#pullrequestreview-240281740
return image.Pt(1080, 1429)
}
return image.Pt(1080, 1440)
pgaskin marked this conversation as resolved.
Show resolved Hide resolved
case glo, auraEd2r1, auraEd2r2: // kraken, star
return image.Pt(758, 1024)
case aura: // phoenix
return image.Pt(758, 1014)
case forma, forma32gb: // frost
return image.Pt(1440, 1920)
default: // KoboWifi, KoboTouch, trilogy, KoboTouch2
return image.Pt(600, 800)
}
}

type koboCover int

const (
fullCover koboCover = iota
libFull
libGrid
)

func (k koboCover) String() string {
switch k {
case fullCover:
return "N3_FULL"
case libFull:
return "N3_LIBRARY_FULL"
case libGrid:
return "N3_LIBRARY_GRID"
default:
panic("unknown cover type")
shermp marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Resize returnes the dimensions to resize sz to for the cover type.
func (k koboCover) Resize(d koboDevice, sz image.Point) image.Point {
switch k {
case fullCover:
return resizeKeepAspectRatio(sz, k.Size(d), false)
case libFull, libGrid:
return resizeKeepAspectRatio(sz, k.Size(d), true)
default:
fc = image.Pt(600, 800)
lf = image.Pt(355, 473)
lg = image.Pt(149, 198)
panic("unknown cover type")
}
return fc, lf, lg
}

// Size gets the target image size for the cover type.
func (k koboCover) Size(d koboDevice) image.Point {
switch k {
case fullCover:
return d.FullCover()
case libFull:
return image.Pt(355, 530)
case libGrid:
return image.Pt(149, 223)
default:
panic("unknown cover type")
}
}

// RelPath gets the path to the cover file relative to the images dir.
func (k koboCover) RelPath(imageID string) string {
dir1, dir2, basename := hashedImageParts(imageID)
return filepath.Join(dir1, dir2, fmt.Sprintf("%s - %s.parsed", basename, k.String()))
}
48 changes: 48 additions & 0 deletions kobo-uncaged/kutypes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"path/filepath"
"strings"
"testing"
)

func TestKoboCoverString(t *testing.T) {
for _, cover := range []koboCover{fullCover, libFull, libGrid} {
if cover.String() == "" {
t.Errorf("expected non-empty string for %#v", cover)
}
// it will also fail if it panics due to an unknown cover.
}
}

func TestKoboCoverSize(t *testing.T) {
for _, cover := range []koboCover{fullCover, libFull, libGrid} {
if sz := cover.Size(touchAB); sz.X == 0 || sz.Y == 0 {
t.Errorf("expected non-zero size for %#v, got %s", cover, sz)
}
// it will also fail if it panics due to an unknown cover.
}
}

func TestKoboCoverRelPath(t *testing.T) {
for _, tc := range []struct {
cover koboCover
id, path string
}{
{fullCover, "file____mnt_onboard_perftesting_book0_kepub_epub", "50/74/file____mnt_onboard_perftesting_book0_kepub_epub - N3_FULL.parsed"},
{libFull, "file____mnt_onboard_perftesting_book0_kepub_epub", "50/74/file____mnt_onboard_perftesting_book0_kepub_epub - N3_LIBRARY_FULL.parsed"},
{libGrid, "file____mnt_onboard_perftesting_book0_kepub_epub", "50/74/file____mnt_onboard_perftesting_book0_kepub_epub - N3_LIBRARY_GRID.parsed"},
// note: the actual sharding is tested in util_test.go
} {
rp := filepath.ToSlash(tc.cover.RelPath(tc.id))
if !strings.HasSuffix(rp, ".parsed") {
t.Errorf("cover must end in .parsed, got %#v", rp)
}
if len(strings.Split(rp, "/")) != 3 {
t.Errorf("relpath should have 3 parts, got %#v", rp)
}
if rp != filepath.ToSlash(tc.path) {
t.Errorf("expected cover for %#v to be %#v, got %#v", tc.id, tc.path, rp)
}
}
}
102 changes: 36 additions & 66 deletions kobo-uncaged/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package main

import (
"bytes"
"database/sql"
"encoding/base64"
"encoding/json"
Expand All @@ -31,7 +30,6 @@ import (
"log"
"log/syslog"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
Expand Down Expand Up @@ -196,27 +194,6 @@ func New(dbRootDir, sdRootDir string, updatingMD bool) (*KoboUncaged, error) {
return ku, nil
}

// genImagePath generates the directory structure used by
// kobo to store the cover image files.
// It has been ported from the implementation in the KoboTouch
// driver in Calibre
func (ku *KoboUncaged) genImageDirPath(imageID string) string {
imgID := []byte(imageID)
h := uint32(0x00000000)
for _, x := range imgID {
h = (h << 4) + uint32(x)
h ^= (h & 0xf0000000) >> 23
h &= 0x0fffffff
}
dir1 := h & (0xff * 1)
dir2 := (h & (0xff00 * 1)) >> 8
pathPrefix := ".kobo-images"
if ku.useSDCard {
pathPrefix = "koboExtStorage/images-cache"
}
return fmt.Sprintf("%s/%d/%d", pathPrefix, dir1, dir2)
}

func (ku *KoboUncaged) openNickelDB() error {
var err error
dsn := "file:" + filepath.Join(ku.dbRootDir, koboDBpath) + "?cache=shared&mode=rw"
Expand Down Expand Up @@ -502,60 +479,51 @@ func (ku *KoboUncaged) saveDeviceInfo() error {

func (ku *KoboUncaged) saveCoverImage(contentID string, size image.Point, imgB64 string) {
defer ku.wg.Done()
imgID := imgIDFromContentID(contentID)
imgDir := path.Join(ku.bkRootDir, ku.genImageDirPath(imgID))
_, libFullSize, libGridSize := ku.device.CoverSize()

if err := os.MkdirAll(imgDir, 0744); err != nil {
img, err := imaging.Decode(base64.NewDecoder(base64.StdEncoding, strings.NewReader(imgB64)))
if err != nil {
log.Println(err)
return
}
sz := img.Bounds().Size()

imgBin, err := base64.StdEncoding.DecodeString(imgB64)
if err != nil {
return
imgDir := ".kobo-images"
if ku.useSDCard {
imgDir = "koboExtStorage/images-cache"
}
imgDir = filepath.Join(ku.bkRootDir, imgDir)

// Now we do our resizing
origCover, err := imaging.Decode(bytes.NewReader(imgBin))
if err != nil {
log.Println(err)
return
}
imgID := imgIDFromContentID(contentID)

var libFW, libFH, gridFW, gridFH int
if (float64(size.X) / float64(size.Y)) < 1.0 {
libFH, gridFH = libFullSize.Y, libGridSize.Y
} else {
libFW, gridFW = libFullSize.X, libGridSize.X
}
for _, cover := range []koboCover{fullCover, libFull, libGrid} {
nsz := cover.Resize(ku.device, sz)
nfn := filepath.Join(imgDir, cover.RelPath(imgID))

// No need to perform any image processing for the library thumb if it meets all our requirements
// Note, we asked Calibre to give us a thumbnail with a given height...
if size.X <= libFullSize.X {
err = ioutil.WriteFile(path.Join(imgDir, imgID+string(libFull)), imgBin, 0644)
if err != nil {
log.Printf("Resizing %s cover to %s (target %s) for %s\n", sz, nsz, cover.Size(ku.device), cover)

nimg := img
if !sz.Eq(nsz) {
nimg = imaging.Resize(nimg, nsz.X, nsz.Y, imaging.Linear)
} else {
log.Println(" -- Skipped resize: already correct size")
}

if err := os.MkdirAll(filepath.Dir(nfn), 0755); err != nil {
log.Println(err)
continue
}
} else {
libImg := imaging.Resize(origCover, libFW, libFH, imaging.Linear)
lc, err := os.OpenFile(path.Join(imgDir, imgID+string(libFull)), os.O_WRONLY|os.O_CREATE, 0644)
if err == nil {
defer lc.Close()
imaging.Encode(lc, libImg, imaging.JPEG)
} else {

lf, err := os.OpenFile(nfn, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
log.Println(err)
continue
}
}

// And finally, the "library grid" image
gridImg := imaging.Resize(origCover, gridFW, gridFH, imaging.Linear)
gc, err := os.OpenFile(path.Join(imgDir, imgID+string(libGrid)), os.O_WRONLY|os.O_CREATE, 0644)
if err == nil {
defer gc.Close()
imaging.Encode(gc, gridImg, imaging.JPEG)
} else {
log.Println(err)
if err := imaging.Encode(lf, nimg, imaging.JPEG); err != nil {
log.Println(err)
lf.Close()
}
lf.Close()
}
}

Expand Down Expand Up @@ -610,8 +578,8 @@ func (ku *KoboUncaged) GetClientOptions() uc.ClientOptions {
opts.SupportedExt = append(opts.SupportedExt, ext...)
opts.DeviceName = "Kobo"
opts.DeviceModel = ku.device.Model()
_, lf, _ := ku.device.CoverSize()
opts.CoverDims.Width, opts.CoverDims.Height = lf.X, lf.Y
fc := fullCover.Size(ku.device)
opts.CoverDims.Width, opts.CoverDims.Height = fc.X, fc.Y
return opts
}

Expand Down Expand Up @@ -894,7 +862,6 @@ func mainWithErrCode() returnCode {
}
log.Println("Starting Calibre Connection")
ku.kup.kuPrintln(body, "Finishing up")
ku.wg.Wait()
err = cc.Start()
if err != nil {
if err.Error() == "no password entered" {
Expand All @@ -904,6 +871,9 @@ func mainWithErrCode() returnCode {
log.Print(err)
return kuError
}
// Wait for thumbnail generation to complete
ku.kup.kuPrintln(body, "Waiting for thumbnail generation to complete")
ku.wg.Wait()

if len(ku.updatedMetadata) > 0 {
ku.kup.kuPrintln(body, "Kobo-UNCaGED will restart automatically to update metadata")
Expand Down
Loading