From a482eea7896db93889a7e8191c3f5bf0b0b20edc Mon Sep 17 00:00:00 2001 From: qeesung <1245712564@qq.com> Date: Thu, 25 Oct 2018 01:02:10 +0800 Subject: [PATCH] refactor the convert package --- convert/convert.go | 25 ++++++++++++++++++++++--- convert/resize.go | 19 ++++++++++++------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/convert/convert.go b/convert/convert.go index 16c6412..c14a512 100644 --- a/convert/convert.go +++ b/convert/convert.go @@ -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 } diff --git a/convert/resize.go b/convert/resize.go index d9e8135..0129df3 100644 --- a/convert/resize.go +++ b/convert/resize.go @@ -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 } -- 2.45.2