@@ -11,6 +11,7 @@ import (
1111
1212 "github.com/BourgeoisBear/rasterm"
1313 "github.com/hilli/icat/util"
14+ "github.com/nfnt/resize"
1415
1516 ascii "github.com/qeesung/image2ascii/convert"
1617
@@ -76,11 +77,12 @@ func PrintImageURL(imageURL string) error {
7677}
7778
7879func PrintImage (img * image.Image , imageConfig * image.Config , filename string , imageSize int64 ) error {
80+ var img2 image.Image
7981 sixelCapable , _ := rasterm .IsSixelCapable ()
8082
8183 _ , _ , pw , ph := TermSize () // Get terminal height and width in pixels
8284
83- kittyOpts := rasterm.KittyImgOpts {SrcWidth : uint32 (imageConfig . Width ), SrcHeight : uint32 (imageConfig . Height )}
85+ kittyOpts := rasterm.KittyImgOpts {SrcWidth : uint32 (pw ), SrcHeight : uint32 (ph )}
8486
8587 if pw < uint16 (imageConfig .Width ) {
8688 kittyOpts .SrcWidth = uint32 (pw )
@@ -89,14 +91,22 @@ func PrintImage(img *image.Image, imageConfig *image.Config, filename string, im
8991 kittyOpts .SrcHeight = uint32 (ph )
9092 }
9193
92- // resizedImage := resizeImage(*img, kittyOpts.SrcHeight)
94+ if pw < uint16 (imageConfig .Width ) {
95+ img2 = resize .Resize (uint (pw ), 0 , * img , resize .NearestNeighbor )
96+ }
97+
98+ img2Height := img2 .Bounds ().Max .Y
99+
100+ if ph < uint16 (img2Height ) {
101+ img2 = resize .Resize (0 , uint (ph ), img2 , resize .NearestNeighbor )
102+ }
93103
94104 switch {
95105 case rasterm .IsKittyCapable ():
96- return rasterm .KittyWriteImage (os .Stdout , * img , kittyOpts )
106+ return rasterm .KittyWriteImage (os .Stdout , img2 , kittyOpts )
97107
98108 case rasterm .IsItermCapable ():
99- return rasterm .ItermWriteImage (os .Stdout , * img )
109+ return rasterm .ItermWriteImage (os .Stdout , img2 )
100110
101111 case sixelCapable :
102112 // TODO: Convert image to a paletted format
@@ -111,7 +121,7 @@ func PrintImage(img *image.Image, imageConfig *image.Config, filename string, im
111121 // Ascii art fallback
112122 converter := ascii .NewImageConverter ()
113123 convertOptions := ascii .DefaultOptions
114- fmt .Print ("\n " , converter .Image2ASCIIString (* img , & convertOptions )) // Align image at the initial position instead of \n first?
124+ fmt .Print ("\n " , converter .Image2ASCIIString (img2 , & convertOptions )) // Align image at the initial position instead of \n first?
115125 }
116126 return nil
117127}
0 commit comments