~dricottone/image2ascii

c6c0ddbf7f62431e24f645349b5914ec125bf2f7 — qeesung 6 years ago 7f59513
Add the method to fit the screen
2 files changed, 21 insertions(+), 24 deletions(-)

M convert/convert.go
M convert/resize.go
M convert/convert.go => convert/convert.go +5 -3
@@ 4,7 4,6 @@ package convert
import (
	"bytes"
	"github.com/qeesung/image2ascii/ascii"
	"github.com/qeesung/image2ascii/resize"
	"image"
	"image/color"
	"log"


@@ 12,13 11,16 @@ import (
)

type Options struct {
	Ratio float64
	Ratio          float64
	ExpectedWidth  int
	ExpectedHeight int
	FitScreen      bool
}

// Convert a image to ascii matrix
func Image2ASCIIMatrix(image image.Image, options *Options) []string {
	// Resize the convert first
	newImage := resize.ScaleImage(image, options.Ratio)
	newImage := ScaleImage(image, options)
	sz := newImage.Bounds()
	newWidth := sz.Max.Y
	newHeight := sz.Max.X

M convert/resize.go => convert/resize.go +16 -21
@@ 1,11 1,11 @@
// The resize package resize the image to expected size
// base on the ratio, for the most matched display
package resize
package convert

import (
	"fmt"
	"github.com/mattn/go-isatty"
	"github.com/nfnt/resize"
	"github.com/qeesung/image2ascii/convert"
	terminal "github.com/wayneashleyberry/terminal-dimensions"
	"image"
	"log"


@@ 14,7 14,7 @@ import (
)

// Resize the convert to expected size base on the ratio
func ScaleImage(image image.Image, options *convert.Options) (newImage image.Image) {
func ScaleImage(image image.Image, options *Options) (newImage image.Image) {
	sz := image.Bounds()
	ratio := options.Ratio
	newHeight := sz.Max.X


@@ 28,21 28,23 @@ func ScaleImage(image image.Image, options *convert.Options) (newImage image.Ima
		newHeight = options.ExpectedHeight
	}

	// get the fit the screen size
	if ratio == 1 &&
		options.ExpectedWidth == -1 &&
		options.ExpectedHeight == -1 &&
		options.FitScreen {
		ratio = getFitScreenRatio(uint(newWidth), uint(newHeight))
	}

	// use the ratio the scale the image
	if options.ExpectedHeight == -1 && options.ExpectedWidth == -1 && ratio != 1 {
		fmt.Println(ratio)
		newWidth = int(float64(sz.Max.X) * ratio)
		newHeight = int(float64(sz.Max.Y) * ratio * charWidth())
	}

	// fit the screen
	// get the fit the screen size
	if ratio == 1 &&
		options.ExpectedWidth == -1 &&
		options.ExpectedHeight == -1 &&
		options.FitScreen {
		screenWidth, screenHeight := getFitScreenSize()
		newWidth = int(screenWidth)
		newHeight = int(screenHeight)
	}

	newImage = resize.Resize(uint(newWidth), uint(newHeight), image, resize.Lanczos3)
	return


@@ 62,20 64,13 @@ func isWindows() bool {
	return runtime.GOOS == "windows"
}

func getFitScreenRatio(imageWidth, imageHeight uint) float64 {
	if !isatty.IsTerminal(os.Stdout.Fd()) || !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
func getFitScreenSize() (newWidth, newHeight uint) {
	if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
		log.Fatal("Can not detect the terminal, please disable the '-s fitScreen' option")
	}

	x, _ := terminal.Width()
	y, _ := terminal.Height()

	verticalRatio := float64(y / imageHeight)
	horizontalRatio := float64(x / imageWidth)

	if verticalRatio > horizontalRatio {
		return horizontalRatio
	} else {
		return verticalRatio
	}
	return x, y
}