3 files changed, 88 insertions(+), 4 deletions(-)
M Gopkg.lock
M Gopkg.toml
R resize/resize.go => convert/resize.go
M Gopkg.lock => Gopkg.lock +29 -1
@@ 2,6 2,14 @@
[[projects]]
+ digest = "1:0981502f9816113c9c8c4ac301583841855c8cf4da8c72f696b3ebedf6d0e4e5"
+ name = "github.com/mattn/go-isatty"
+ packages = ["."]
+ pruneopts = "UT"
+ revision = "6ca4dbf54d38eea1a992b3c722a76a5d1c4cb25c"
+ version = "v0.0.4"
+
+[[projects]]
branch = "master"
digest = "1:34534b73e925d20cc72cf202f8b482fdcbe3a1b113e19375f31aadabd0f0f97d"
name = "github.com/nfnt/resize"
@@ 9,9 17,29 @@
pruneopts = "UT"
revision = "83c6a9932646f83e3267f353373d47347b6036b2"
+[[projects]]
+ digest = "1:9b4fd49bb5908e00ecb36f3199008562d5c9fa7b3ddee554465f4d8bfde52d4c"
+ name = "github.com/wayneashleyberry/terminal-dimensions"
+ packages = ["."]
+ pruneopts = "UT"
+ revision = "29d939246793fbd8100dcf74700637adcb839bc5"
+ version = "v1.0.0"
+
+[[projects]]
+ branch = "master"
+ digest = "1:be4f165c438aa39318fd5c8c180a95f4a52524641c0b714638b0fd9fd912b38d"
+ name = "golang.org/x/sys"
+ packages = ["unix"]
+ pruneopts = "UT"
+ revision = "8e24a49d80f82323e1c4db1b5da3e0f31171a151"
+
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
- input-imports = ["github.com/nfnt/resize"]
+ input-imports = [
+ "github.com/mattn/go-isatty",
+ "github.com/nfnt/resize",
+ "github.com/wayneashleyberry/terminal-dimensions",
+ ]
solver-name = "gps-cdcl"
solver-version = 1
M Gopkg.toml => Gopkg.toml +8 -0
@@ 32,3 32,11 @@
[prune]
go-tests = true
unused-packages = true
+
+[[constraint]]
+ name = "github.com/mattn/go-isatty"
+ version = "0.0.4"
+
+[[constraint]]
+ name = "github.com/wayneashleyberry/terminal-dimensions"
+ version = "1.0.0"
R resize/resize.go => convert/resize.go +51 -3
@@ 3,16 3,46 @@
package resize
import (
+ "github.com/mattn/go-isatty"
"github.com/nfnt/resize"
+ "github.com/qeesung/image2ascii/convert"
+ terminal "github.com/wayneashleyberry/terminal-dimensions"
"image"
+ "log"
+ "os"
"runtime"
)
// Resize the convert to expected size base on the ratio
-func ScaleImage(image image.Image, ratio float64) (newImage image.Image) {
+func ScaleImage(image image.Image, options *convert.Options) (newImage image.Image) {
sz := image.Bounds()
- newWidth := int(float64(sz.Max.X) * ratio)
- newHeight := int(float64(sz.Max.Y) * ratio * charWidth())
+ ratio := options.Ratio
+ newHeight := sz.Max.X
+ newWidth := sz.Max.Y
+
+ if options.ExpectedWidth != -1 {
+ newWidth = options.ExpectedWidth
+ }
+
+ if options.ExpectedHeight != -1 {
+ 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 {
+ newWidth = int(float64(sz.Max.X) * ratio)
+ newHeight = int(float64(sz.Max.Y) * ratio * charWidth())
+ }
+
+ // fit the screen
newImage = resize.Resize(uint(newWidth), uint(newHeight), image, resize.Lanczos3)
return
@@ 31,3 61,21 @@ func charWidth() float64 {
func isWindows() bool {
return runtime.GOOS == "windows"
}
+
+func getFitScreenRatio(imageWidth, imageHeight uint) float64 {
+ 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
+ }
+}