From ecf8a23af28fcba6a48e35e72e0b6d9d57bbd440 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Tue, 9 Jun 2026 21:40:56 -0700 Subject: Fix rounding --- ils.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'ils.go') diff --git a/ils.go b/ils.go index 71701c9..e40c6f7 100644 --- a/ils.go +++ b/ils.go @@ -125,6 +125,10 @@ func printImg(w io.Writer, img image.Image) { } } +func divRound(n, d int) int { + return (n + d/2) / d +} + type pixelAspectRatio struct { x, y int } @@ -142,7 +146,7 @@ func compositeImageRow(imagePaths []string, width, rowWidth, cellX int, aspectRa if err != nil { return nil, err } - maxHeight = max(maxHeight, imageHeight*width/(imageWidth*aspectRatio.y/aspectRatio.x)) + maxHeight = max(maxHeight, divRound(imageHeight*width*aspectRatio.x, imageWidth*aspectRatio.y)) } bounds := image.Rect(0, 0, rowWidth, maxHeight) result := image.NewRGBA(bounds) @@ -152,10 +156,10 @@ func compositeImageRow(imagePaths []string, width, rowWidth, cellX int, aspectRa if err != nil { return nil, err } - height := img.Bounds().Dy() * width / (img.Bounds().Dx() * aspectRatio.y / aspectRatio.x) + height := divRound(img.Bounds().Dy()*width*aspectRatio.x, img.Bounds().Dx()*aspectRatio.y) imageWidth := max(width, cellX*runewidth.StringWidth(filepath.Base(filename))) - start := (offset*rowWidth + logicalWidth - 1) / logicalWidth - end := ((offset+imageWidth)*rowWidth + logicalWidth - 1) / logicalWidth + start := divRound(offset*rowWidth, logicalWidth) + end := divRound((offset+imageWidth)*rowWidth, logicalWidth) offsetStart := start + (end-start-width)/2 dstBounds := image.Rect(offsetStart, (maxHeight-height)/2, offsetStart+width, (maxHeight-height)/2+height) draw.BiLinear.Scale(result, dstBounds, img, img.Bounds(), draw.Src, nil) @@ -244,12 +248,12 @@ func thumbnailFiles(w io.Writer, imagePaths []string, width, rowWidth, cellX, ce if i > 0 { offset += 2 } - start := (offset*rowWidth + thisRowWidth - 1) / thisRowWidth + start := divRound(offset*rowWidth, thisRowWidth) fmt.Fprintf(w, "%*s", start-realOffset, "") realOffset = start fileNameWidth := runewidth.StringWidth(filepath.Base(filename)) offset += max(width, fileNameWidth) - end := (offset*rowWidth + thisRowWidth - 1) / thisRowWidth + end := divRound(offset*rowWidth, thisRowWidth) padding := (end - start - fileNameWidth) / 2 fmt.Fprintf(w, "%*s%s", padding, "", filepath.Base(filename)) realOffset += padding + fileNameWidth -- cgit v1.3.1