Fixed incorrect image height calculations

This commit is contained in:
Zoraiz 2021-05-13 00:07:54 +05:00
parent d47e68abb3
commit f81cc08f83
1 changed files with 9 additions and 15 deletions

View File

@ -42,28 +42,22 @@ func ConvertToTerminalSizedSlices(img image.Image, dimensions []int) [][]uint32
var terminalWidth, terminalHeight int var terminalWidth, terminalHeight int
var smallImg image.Image
// Get dimensions of current terminal // Get dimensions of current terminal
if len(dimensions) == 0 { if len(dimensions) == 0 {
terminalWidth, terminalHeight = consolesize.GetConsoleSize()
// To avoid cases where empty lines get printed between ascii lines terminalWidth, _ = consolesize.GetConsoleSize()
terminalWidth -= 1 smallImg = resize.Resize(uint(terminalWidth), 0, img, resize.Lanczos3)
terminalHeight -= 1 terminalHeight = smallImg.Bounds().Max.Y
// Fix height
var ratio float32
imgHeight := img.Bounds().Max.Y
imgWidth := img.Bounds().Max.X
if imgHeight > imgWidth {
ratio = float32(imgHeight) / float32(imgWidth)
terminalHeight = int(ratio * float32(terminalWidth))
terminalHeight -= terminalHeight / 2 terminalHeight -= terminalHeight / 2
}
smallImg = resize.Resize(uint(terminalWidth), uint(terminalHeight), img, resize.Lanczos3)
} else { } else {
terminalWidth = dimensions[0] terminalWidth = dimensions[0]
terminalHeight = dimensions[1] terminalHeight = dimensions[1]
smallImg = resize.Resize(uint(terminalWidth), uint(terminalHeight), img, resize.Lanczos3)
} }
if len(dimensions) > 0 { if len(dimensions) > 0 {
@ -81,7 +75,7 @@ func ConvertToTerminalSizedSlices(img image.Image, dimensions []int) [][]uint32
imgSet[i] = make([]uint32, terminalWidth) imgSet[i] = make([]uint32, terminalWidth)
} }
smallImg := resize.Resize(uint(terminalWidth), uint(terminalHeight), img, resize.Lanczos3) // smallImg := resize.Resize(uint(terminalWidth), 0, img, resize.Lanczos3)
b := smallImg.Bounds() b := smallImg.Bounds()
for y := b.Min.Y; y < b.Max.Y; y++ { for y := b.Min.Y; y < b.Max.Y; y++ {