Real-image posters: match the box to the poster's shape and crop the remainder
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -113,8 +113,9 @@ func TestCoverCells(t *testing.T) {
|
||||
if c, r := coverCells(40, 60, 30, 10, 2); c != 13 || r != 10 {
|
||||
t.Errorf("tall limit: %dx%d", c, r)
|
||||
}
|
||||
// In a narrow box the width limits it.
|
||||
if c, r := coverCells(40, 60, 10, 20, 2); c != 10 || r != 7 {
|
||||
// In a narrow box the width limits it, and the box keeps the poster's shape
|
||||
// (8×6 cells is exactly 2:3) instead of 10×7 which would leave bars.
|
||||
if c, r := coverCells(40, 60, 10, 20, 2); c != 8 || r != 6 {
|
||||
t.Errorf("narrow limit: %dx%d", c, r)
|
||||
}
|
||||
}
|
||||
@@ -276,3 +277,28 @@ func TestCoverStyleFor(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoverCellsKeepTheShape(t *testing.T) {
|
||||
// For many poster shapes, cell shapes and boxes the chosen box must match
|
||||
// the poster within the tolerance (or be the closest available).
|
||||
for _, img := range [][2]int{{167, 250}, {500, 750}, {2, 3}, {1000, 1500}, {680, 1000}} {
|
||||
for _, aspect := range []float64{1.9, 2, 2.1, 2.25} {
|
||||
for w := 12; w <= 40; w += 7 {
|
||||
for h := 8; h <= 30; h += 11 {
|
||||
c, r := coverCells(img[0], img[1], w, h, aspect)
|
||||
if c < 1 || r < 1 || c > w || r > h {
|
||||
t.Fatalf("%v aspect %v in %dx%d: %dx%d out of bounds", img, aspect, w, h, c, r)
|
||||
}
|
||||
shape := float64(img[0]) / float64(img[1])
|
||||
err := float64(c)/(float64(r)*aspect)/shape - 1
|
||||
if err < 0 {
|
||||
err = -err
|
||||
}
|
||||
if err > 0.10 { // the tolerance is 3%; tiny boxes may only get within a cell
|
||||
t.Errorf("%v aspect %v in %dx%d: box %dx%d is %.0f%% off", img, aspect, w, h, c, r, err*100)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user