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 1 commit
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
13 changes: 13 additions & 0 deletions kobo-uncaged/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"image"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -74,3 +75,15 @@ func readJSON(fn string, out interface{}) (emptyOrNotExist bool, err error) {

return false, json.NewDecoder(f).Decode(out)
}

// resizeKeepAspectRatioByExpanding resizes a sz to fill bounds while keeping
// the aspect ratio. It is based on Qt::KeepAspectRatioByExpanding.
func resizeKeepAspectRatioByExpanding(sz image.Point, bounds image.Point) image.Point {
if sz.X == 0 || sz.Y == 0 {
return sz
}
if rw := bounds.Y * sz.X / sz.Y; rw >= bounds.X {
return image.Pt(rw, bounds.Y)
pgaskin marked this conversation as resolved.
Show resolved Hide resolved
}
return image.Pt(bounds.X, bounds.Y*sz.Y/sz.X)
pgaskin marked this conversation as resolved.
Show resolved Hide resolved
}
33 changes: 33 additions & 0 deletions kobo-uncaged/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"image"
"testing"
)

func TestResizeKeepAspectRatioByExpanding(t *testing.T) {
for _, tc := range []struct{ sz, bounds, res image.Point }{
// don't resize if width or height is zero
{image.Pt(0, 0), image.Pt(0, 0), image.Pt(0, 0)},
{image.Pt(1, 0), image.Pt(0, 0), image.Pt(1, 0)},
{image.Pt(0, 1), image.Pt(0, 0), image.Pt(0, 1)},
// same aspect ratio
{image.Pt(1, 1), image.Pt(1, 1), image.Pt(1, 1)},
{image.Pt(1, 1), image.Pt(5, 5), image.Pt(5, 5)},
{image.Pt(5, 5), image.Pt(1, 1), image.Pt(1, 1)},
// limited by width
{image.Pt(2, 3), image.Pt(6, 6), image.Pt(6, 9)},
{image.Pt(2, 4), image.Pt(6, 6), image.Pt(6, 12)},
{image.Pt(6, 9), image.Pt(2, 3), image.Pt(2, 3)},
{image.Pt(6, 12), image.Pt(2, 4), image.Pt(2, 4)},
// limited by height
{image.Pt(3, 2), image.Pt(6, 6), image.Pt(9, 6)},
{image.Pt(4, 2), image.Pt(6, 6), image.Pt(12, 6)},
{image.Pt(9, 6), image.Pt(3, 2), image.Pt(3, 2)},
{image.Pt(12, 6), image.Pt(4, 2), image.Pt(4, 2)},
} {
if rz := resizeKeepAspectRatioByExpanding(tc.sz, tc.bounds); !rz.Eq(tc.res) {
t.Errorf("resize %s to %s: expected %s, got %s", tc.sz, tc.bounds, tc.res, rz)
}
}
}