~dricottone/image2ascii

a482eea7896db93889a7e8191c3f5bf0b0b20edc — qeesung 6 years ago 95a015a
refactor the convert package
2 files changed, 34 insertions(+), 10 deletions(-)

M convert/convert.go
M convert/resize.go
M convert/convert.go => convert/convert.go +22 -3
@@ 6,6 6,8 @@ import (
	"github.com/qeesung/image2ascii/ascii"
	"image"
	"image/color"
	_ "image/jpeg"
	_ "image/png"
	"log"
	"os"
)


@@ 19,6 21,14 @@ type Options struct {
	Colored        bool
}

var defaultOptions = Options{
	Ratio:          1,
	ExpectedWidth:  -1,
	ExpectedHeight: -1,
	FitScreen:      true,
	Colored:        true,
}

// Image2ASCIIMatrix converts a image to ASCII matrix
func Image2ASCIIMatrix(image image.Image, imageConvertOptions *Options) []string {
	// Resize the convert first


@@ 54,6 64,15 @@ func Image2ASCIIString(image image.Image, options *Options) string {

// ImageFile2ASCIIString converts a image file to ascii string
func ImageFile2ASCIIString(imageFilename string, option *Options) string {
	img, err := OpenImageFile(imageFilename)
	if err != nil {
		log.Fatal("open image failed : " + err.Error())
	}
	return Image2ASCIIString(img, option)
}

// OpenImageFile open a image and return a image object
func OpenImageFile(imageFilename string) (image.Image, error) {
	f, err := os.Open(imageFilename)
	if err != nil {
		log.Fatal(err)


@@ 61,9 80,9 @@ func ImageFile2ASCIIString(imageFilename string, option *Options) string {

	img, _, err := image.Decode(f)
	if err != nil {
		log.Fatal(err)
		return nil, err
	}

	f.Close()
	return Image2ASCIIString(img, option)
	defer f.Close()
	return img, nil
}

M convert/resize.go => convert/resize.go +12 -7
@@ 1,6 1,7 @@
package convert

import (
	"errors"
	"github.com/mattn/go-isatty"
	"github.com/nfnt/resize"
	terminal "github.com/wayneashleyberry/terminal-dimensions"


@@ 14,8 15,8 @@ import (
func ScaleImage(image image.Image, options *Options) (newImage image.Image) {
	sz := image.Bounds()
	ratio := options.Ratio
	newHeight := sz.Max.X
	newWidth := sz.Max.Y
	newHeight := sz.Max.Y
	newWidth := sz.Max.X

	if options.ExpectedWidth != -1 {
		newWidth = options.ExpectedWidth


@@ 37,7 38,10 @@ func ScaleImage(image image.Image, options *Options) (newImage image.Image) {
		options.ExpectedWidth == -1 &&
		options.ExpectedHeight == -1 &&
		options.FitScreen {
		screenWidth, screenHeight := getFitScreenSize()
		screenWidth, screenHeight, err := getTerminalScreenSize()
		if err != nil {
			log.Fatal(err)
		}
		newWidth = int(screenWidth)
		newHeight = int(screenHeight)
	}


@@ 59,14 63,15 @@ func isWindows() bool {
	return runtime.GOOS == "windows"
}

// getFitScreenSize get the current terminal screen size
func getFitScreenSize() (newWidth, newHeight uint) {
// getTerminalScreenSize get the current terminal screen size
func getTerminalScreenSize() (newWidth, newHeight uint, err error) {
	if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
		log.Fatal("Can not detect the terminal, please disable the '-s fitScreen' option")
		return 0, 0,
			errors.New("can not detect the terminal, please disable the '-s fitScreen' option")
	}

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

	return x, y
	return x, y, nil
}