diff options
| -rw-r--r-- | icat.go | 10 |
1 files changed, 7 insertions, 3 deletions
@@ -73,6 +73,10 @@ func flagPrintMode(fs *flag.FlagSet, name string, value printMode, usage string) return &value } +func divRound[N ~int64 | ~int](n, d N) N { + return (n + d/2) / d +} + func load(filename string) (image.Image, error) { file := os.Stdin if filename != "-" { @@ -186,7 +190,7 @@ func colorAvg(colors []color.RGBA) color.RGBA { b += int64(c.B) } n := int64(len(colors)) - return color.RGBA{R: uint8(r / n), G: uint8(g / n), B: uint8(b / n), A: 0xff} + return color.RGBA{R: uint8(divRound(r, n)), G: uint8(divRound(g, n)), B: uint8(divRound(b, n)), A: 0xff} } func medianCut(img image.Image) color.Palette { @@ -230,9 +234,9 @@ func medianCut(img image.Image) color.Palette { func printImg(img image.Image, x, y, pixelX, pixelY int) error { // Try not to stretch the image. if y == 0 || x != 0 && img.Bounds().Dy()*x <= img.Bounds().Dx()*y*pixelY/pixelX { - y = img.Bounds().Dy() * x / (img.Bounds().Dx() * pixelY / pixelX) + y = divRound(img.Bounds().Dy()*x*pixelX, img.Bounds().Dx()*pixelY) } else { - x = img.Bounds().Dx() * y * pixelY / pixelX / img.Bounds().Dy() + x = divRound(img.Bounds().Dx()*y*pixelY, pixelX*img.Bounds().Dy()) } dst := image.NewRGBA(image.Rect(0, 0, x, y)) |
