-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender.go
More file actions
53 lines (45 loc) · 1.26 KB
/
Copy pathrender.go
File metadata and controls
53 lines (45 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package ansipx
import (
"bufio"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"os"
)
// Render converts an image.Image to an ANSI art string using the given options.
func Render(img image.Image, opts Options) (string, error) {
r := newRenderer(opts)
return r.process(img)
}
// RenderFile opens an image file, decodes it, and renders it to an ANSI art string.
func RenderFile(path string, opts Options) (string, error) {
if path == "" {
return "", fmt.Errorf("no image file path provided")
}
imgFile, err := os.Open(path)
if err != nil {
return "", fmt.Errorf("could not open image %s: %w", path, err)
}
defer imgFile.Close()
img, _, err := image.Decode(bufio.NewReader(imgFile))
if err != nil {
return "", fmt.Errorf("could not decode image %s: %w", path, err)
}
return Render(img, opts)
}
func (m *renderer) process(input image.Image) (string, error) {
if !m.opts.TrueColor && len(m.opts.Palette) == 0 {
return "", fmt.Errorf("palette mode requires a non-empty palette")
}
switch m.opts.CharacterMode {
case Ascii:
return m.processAscii(input), nil
case Unicode:
return m.processUnicode(input), nil
case Custom:
return m.processCustom(input), nil
}
return "", fmt.Errorf("unknown character mode: %d", m.opts.CharacterMode)
}