| 1 |
package sixel |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"image" |
| 7 |
"image/png" |
| 8 |
"io" |
| 9 |
"os" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/charmbracelet/x/ansi" |
| 13 |
// gosixel "github.com/mattn/go-sixel" |
| 14 |
) |
| 15 |
|
| 16 |
// func BenchmarkEncodingGoSixel(b *testing.B) { |
| 17 |
// for i := 0; i < b.N; i++ { |
| 18 |
// raw, err := loadImage("./../fixtures/graphics/JigokudaniMonkeyPark.png") |
| 19 |
// if err != nil { |
| 20 |
// os.Exit(1) |
| 21 |
// } |
| 22 |
|
| 23 |
// b := bytes.NewBuffer(nil) |
| 24 |
// enc := gosixel.NewEncoder(b) |
| 25 |
// if err := enc.Encode(raw); err != nil { |
| 26 |
// fmt.Fprintln(os.Stderr, err) |
| 27 |
// os.Exit(1) |
| 28 |
// } |
| 29 |
|
| 30 |
// // fmt.Println(b) |
| 31 |
// } |
| 32 |
// } |
| 33 |
|
| 34 |
func writeSixelGraphics(w io.Writer, m image.Image) error { |
| 35 |
e := &Encoder{} |
| 36 |
|
| 37 |
data := bytes.NewBuffer(nil) |
| 38 |
if err := e.Encode(data, m); err != nil { |
| 39 |
return fmt.Errorf("failed to encode sixel image: %w", err) |
| 40 |
} |
| 41 |
|
| 42 |
_, err := io.WriteString(w, ansi.SixelGraphics(0, 1, 0, data.Bytes())) |
| 43 |
return err //nolint:wrapcheck |
| 44 |
} |
| 45 |
|
| 46 |
func BenchmarkEncodingXSixel(b *testing.B) { |
| 47 |
for i := 0; i < b.N; i++ { |
| 48 |
raw, err := loadImage("./../fixtures/graphics/JigokudaniMonkeyPark.png") |
| 49 |
if err != nil { |
| 50 |
os.Exit(1) |
| 51 |
} |
| 52 |
|
| 53 |
b := bytes.NewBuffer(nil) |
| 54 |
if err := writeSixelGraphics(b, raw); err != nil { |
| 55 |
fmt.Fprintln(os.Stderr, err) |
| 56 |
os.Exit(1) |
| 57 |
} |
| 58 |
|
| 59 |
// fmt.Println(b) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func loadImage(path string) (image.Image, error) { |
| 64 |
f, err := os.Open(path) |
| 65 |
if err != nil { |
| 66 |
return nil, err //nolint:wrapcheck |
| 67 |
} |
| 68 |
return png.Decode(f) //nolint:wrapcheck |
| 69 |
} |