|
| 1 | +package rasterm |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "image" |
| 6 | + _ "image/gif" |
| 7 | + _ "image/jpeg" |
| 8 | + _ "image/png" |
| 9 | + "io" |
| 10 | + "os" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +// godoc -http=:8099 -goroot="$HOME/go" |
| 15 | + |
| 16 | +var testFiles []string |
| 17 | + |
| 18 | +func init() { |
| 19 | + |
| 20 | + files, e := os.ReadDir("./test_images") |
| 21 | + if e != nil { |
| 22 | + panic(e) |
| 23 | + } |
| 24 | + |
| 25 | + for ix := range files { |
| 26 | + testFiles = append(testFiles, files[ix].Name()) |
| 27 | + } |
| 28 | + |
| 29 | + os.Stdout.Write([]byte(ESC_ERASE_DISPLAY)) |
| 30 | +} |
| 31 | + |
| 32 | +func loadImage(path string) (iImg image.Image, imgFmt string, E error) { |
| 33 | + |
| 34 | + pF, E := os.Open(path) |
| 35 | + if E != nil { |
| 36 | + return |
| 37 | + } |
| 38 | + defer pF.Close() |
| 39 | + |
| 40 | + return image.Decode(pF) |
| 41 | +} |
| 42 | + |
| 43 | +func getFile(fpath string) (*os.File, int64, error) { |
| 44 | + |
| 45 | + pF, E := os.Open(fpath) |
| 46 | + if E != nil { |
| 47 | + return nil, 0, E |
| 48 | + } |
| 49 | + |
| 50 | + fInf, E := pF.Stat() |
| 51 | + if E != nil { |
| 52 | + pF.Close() |
| 53 | + return nil, 0, E |
| 54 | + } |
| 55 | + |
| 56 | + return pF, fInf.Size(), nil |
| 57 | +} |
| 58 | + |
| 59 | +func getImgInfo(pF *os.File) (imgCfg image.Config, fmtName string, E error) { |
| 60 | + |
| 61 | + if imgCfg, fmtName, E = image.DecodeConfig(pF); E != nil { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + // REWIND FILE |
| 66 | + _, E = pF.Seek(0, 0) |
| 67 | + return |
| 68 | +} |
| 69 | + |
| 70 | +func testEx(pT *testing.T, out io.Writer, mode string, testFiles []string) error { |
| 71 | + |
| 72 | + S := Settings{ |
| 73 | + EscapeTmux: false, |
| 74 | + } |
| 75 | + |
| 76 | + for _, file := range testFiles { |
| 77 | + |
| 78 | + fpath := "./test_images/" + file |
| 79 | + pT.Log(fpath) |
| 80 | + |
| 81 | + fIn, nImgLen, e2 := getFile(fpath) |
| 82 | + if e2 != nil { |
| 83 | + pT.Log(e2) |
| 84 | + continue |
| 85 | + } |
| 86 | + defer fIn.Close() |
| 87 | + |
| 88 | + pT.Logf("IMAGE SIZE %d", nImgLen) |
| 89 | + |
| 90 | + imgCfg, fmtName, e2 := getImgInfo(fIn) |
| 91 | + if e2 != nil { |
| 92 | + pT.Log(e2) |
| 93 | + continue |
| 94 | + } |
| 95 | + |
| 96 | + pT.Logf("FMT: %s, W: %d, H: %d", fmtName, imgCfg.Width, imgCfg.Height) |
| 97 | + |
| 98 | + iImg, _, e2 := loadImage(fpath) |
| 99 | + if e2 != nil { |
| 100 | + pT.Log(e2) |
| 101 | + continue |
| 102 | + } |
| 103 | + |
| 104 | + var e3 error = nil |
| 105 | + switch mode { |
| 106 | + case "iterm": |
| 107 | + |
| 108 | + // WEZ/ITERM SUPPORT ALL FORMATS, SO NO NEED TO RE-ENCODE TO PNG |
| 109 | + e3 = S.ItermCopyFileInline(out, fIn, nImgLen) |
| 110 | + |
| 111 | + case "sixel": |
| 112 | + |
| 113 | + if iPaletted, bOK := iImg.(*image.Paletted); bOK { |
| 114 | + |
| 115 | + e3 = S.SixelWriteImage(out, iPaletted) |
| 116 | + |
| 117 | + } else { |
| 118 | + |
| 119 | + pT.Logf("%s is type [%T], not *image.Paletted\n", file, iImg) |
| 120 | + continue |
| 121 | + } |
| 122 | + |
| 123 | + case "kitty": |
| 124 | + |
| 125 | + if fmtName == "png" { |
| 126 | + |
| 127 | + e3 = S.KittyCopyPNGInline(out, fIn, nImgLen) |
| 128 | + |
| 129 | + } else { |
| 130 | + |
| 131 | + e3 = S.KittyWriteImage(out, iImg) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if e3 != nil { |
| 136 | + pT.Log(e3) |
| 137 | + } |
| 138 | + fmt.Println("") |
| 139 | + } |
| 140 | + |
| 141 | + return nil |
| 142 | +} |
| 143 | + |
| 144 | +// NOTE |
| 145 | +// |
| 146 | +// can't query terminal attributes here (i.e. sixel support) since golang |
| 147 | +// testbed intermediates itself between stdin/stdout with buffers |
| 148 | +func TestSixel(pT *testing.T) { |
| 149 | + |
| 150 | + if IsTermItermWez() || IsTermKitty() { |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + if E := testEx(pT, os.Stdout, "sixel", testFiles); E != nil { |
| 155 | + pT.Fatal(E) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestItermWez(pT *testing.T) { |
| 160 | + |
| 161 | + if IsTermItermWez() { |
| 162 | + pT.Log("TESTING ITERM2/WEZ:") |
| 163 | + if E := testEx(pT, os.Stdout, "iterm", testFiles); E != nil { |
| 164 | + pT.Fatal(E) |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestKitty(pT *testing.T) { |
| 170 | + |
| 171 | + if IsTermKitty() { |
| 172 | + pT.Log("TESTING KITTY TERM") |
| 173 | + if E := testEx(pT, os.Stdout, "kitty", testFiles); E != nil { |
| 174 | + pT.Fatal(E) |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments