diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2026-06-09 21:40:56 -0700 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2026-06-09 21:40:56 -0700 |
| commit | ecf8a23af28fcba6a48e35e72e0b6d9d57bbd440 (patch) | |
| tree | 01706f25cca6935dc76171ab9d9937c3393da4bf | |
| parent | 86b515a6bfcb9b5a162e9bac6473c9886d2911f2 (diff) | |
| download | imgutil-ecf8a23af28fcba6a48e35e72e0b6d9d57bbd440.tar.zst | |
Fix rounding
| -rw-r--r-- | ils.go | 16 |
1 files changed, 10 insertions, 6 deletions
@@ -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 |
