shermp / Kobo-UNCaGED

UNCaGED, for Kobo devices
GNU Affero General Public License v3.0
95 stars 7 forks source link

Switch thumbnail image resize library #18

Closed shermp closed 5 years ago

shermp commented 5 years ago

This PR changes the image resizing library used from disintegration/imaging to bamiaux/rez, as discussed in #16

The change is primarily around performance. An initial "full thumbnail" test using the bicubic algorithm seems very promising performance wise.

I have also exposed some thumbnail generation configuration to the user in ku.toml. See that file for available options. This allows us to test various options and combinations to come up with a sane set of defaults, and to allow easy experimentation.

@NiLuJe would you mind testing this please? @geek1011 would you mind reviewing the code on this please?

NiLuJe commented 5 years ago

That is indeed much better :).

May 25 14:15:05 KoboUNCaGED[2031]: 2019/05/25 14:15:05 Resizing (887,1429) cover to (887,1429) (target (1080,1429)) for N3_FULL
May 25 14:15:05 KoboUNCaGED[2031]: 2019/05/25 14:15:05  -- Skipped resize: already correct size
May 25 14:15:06 KoboUNCaGED[2031]: 2019/05/25 14:15:06 Resizing (887,1429) cover to (355,571) (target (355,530)) for N3_LIBRARY_FULL
May 25 14:15:06 KoboUNCaGED[2031]: 2019/05/25 14:15:06  -- Resized to (355,571)
May 25 14:15:07 KoboUNCaGED[2031]: 2019/05/25 14:15:07 Resizing (887,1429) cover to (149,240) (target (149,223)) for N3_LIBRARY_GRID
May 25 14:15:07 KoboUNCaGED[2031]: 2019/05/25 14:15:07  -- Resized to (149,240)
May 25 14:15:07 KoboUNCaGED[2031]: 2019/05/25 14:15:07 Resizing (903,1429) cover to (903,1429) (target (1080,1429)) for N3_FULL
May 25 14:15:07 KoboUNCaGED[2031]: 2019/05/25 14:15:07  -- Skipped resize: already correct size
May 25 14:15:08 KoboUNCaGED[2031]: 2019/05/25 14:15:08 Resizing (903,1429) cover to (355,561) (target (355,530)) for N3_LIBRARY_FULL
May 25 14:15:08 KoboUNCaGED[2031]: 2019/05/25 14:15:08  -- Resized to (355,561)
May 25 14:15:09 KoboUNCaGED[2031]: 2019/05/25 14:15:09 Resizing (903,1429) cover to (149,235) (target (149,223)) for N3_LIBRARY_GRID
May 25 14:15:09 KoboUNCaGED[2031]: 2019/05/25 14:15:09  -- Resized to (149,235)
May 25 14:15:12 KoboUNCaGED[2031]: 2019/05/25 14:15:12 Resizing (943,1429) cover to (943,1429) (target (1080,1429)) for N3_FULL
May 25 14:15:12 KoboUNCaGED[2031]: 2019/05/25 14:15:12  -- Skipped resize: already correct size
May 25 14:15:13 KoboUNCaGED[2031]: 2019/05/25 14:15:13 Resizing (943,1429) cover to (355,537) (target (355,530)) for N3_LIBRARY_FULL
May 25 14:15:14 KoboUNCaGED[2031]: 2019/05/25 14:15:14  -- Resized to (355,537)
May 25 14:15:14 KoboUNCaGED[2031]: 2019/05/25 14:15:14 Resizing (943,1429) cover to (149,225) (target (149,223)) for N3_LIBRARY_GRID
May 25 14:15:14 KoboUNCaGED[2031]: 2019/05/25 14:15:14  -- Resized to (149,225)

That's with a tiny bit of extra logging just to make things obvious ;).

diff --git a/kobo-uncaged/main.go b/kobo-uncaged/main.go
index 95cecd4..660cf25 100644
--- a/kobo-uncaged/main.go
+++ b/kobo-uncaged/main.go
@@ -518,6 +518,7 @@ func (ku *KoboUncaged) saveCoverImage(contentID string, size image.Point, imgB64
                if !sz.Eq(nsz) {
                        nimg = image.NewYCbCr(image.Rect(0, 0, nsz.X, nsz.Y), img.(*image.YCbCr).SubsampleRatio)
                        rez.Convert(nimg, img, ku.KuConfig.Thumbnail.rezFilter)
+                       log.Printf(" -- Resized to %s\n", nimg.Bounds().Size())
                } else {
                        nimg = img
                        log.Println(" -- Skipped resize: already correct size")
shermp commented 5 years ago

How's the quality? Seemed fine to me, but I'm not picky...

NiLuJe commented 5 years ago

Yup, it's in the same ballpark as what Calibre would do.

shermp commented 5 years ago

Yup, it's in the same ballpark as what Calibre would do.

Cool.

It's never going to be great anyway. IIRC calibre sends the thumbnail at 75 JPEG quality to begin with.

NiLuJe commented 5 years ago

FWIW, the original cover (the one stored in the Library) is encoded @ 95, and the ones sent over USB @ 90.

shermp commented 5 years ago

FWIW, the original cover (the one stored in the Library) is encoded @ 95, and the ones sent over USB @ 90.

Just double checked. The default quality for the wireless server is indeed 75. It's configurable by the user though in the "device options" when KU (or CC) is connected

shermp commented 5 years ago

Well, I've heard no screams, so unless there are any concern, I'll merge this one.

shermp commented 5 years ago

My only slight concern is I'm not 100% sure whether Calibre always sends the thumbnail as a JPEG. Current code doesn't deal with PNG at all, although it will error gracefully if a PNG is encountered.

Trying to navigate the Calibre source code is an exercise of frustration, i have to admit...

pgaskin commented 5 years ago

Try using sourcegraph, as it really helps navigate large codebases. Based on a quick glance, I'm pretty certain Calibre only sends JPEGs.

Update: Here's some proof: scale_image, cover_to_thumbnail, THUMBNAIL_WIDTH.

Update 2: And here's where it uses PNG thumbnails (content server, generating comic thumbnail, hanvon).

NiLuJe commented 5 years ago

Yup, pretty sure it'll only ever send JPGs, and that the source (i.e., cover.jpg in the Library's book folder) can only ever be a JPG, too.

In fact, even if you feed the metadata editor a PNG cover, it'll re-encode it as a JPG and store that.

shermp commented 5 years ago

Sounds good to me. Won't worry about it then, Thanks