/
print.go
63 lines (52 loc) · 1.78 KB
/
print.go
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
54
55
56
57
58
59
60
61
62
63
package ocr
import (
"errors"
"fmt"
"image"
"io"
"github.com/BenLubar/dwarfocr"
)
var cp437 = []rune(" ☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ")
var ErrDimensionsMismatch = errors.New("dwarfocr: image dimensions do not match tileset dimensions")
func PrintOCR(w io.Writer, img *image.RGBA, tiles *dwarfocr.Tileset) error {
var oldBg, oldFg, oldBright = -1, -1, -1
setColor := func(bg, fg, bright int) {
if oldFg != fg {
fmt.Fprint(w, "\x1b[3", fg, "m")
oldFg = fg
}
if oldBg != bg {
fmt.Fprint(w, "\x1b[4", bg, "m")
oldBg = bg
}
if oldBright != bright {
if bright == 0 {
fmt.Fprint(w, "\x1b[22m")
} else {
fmt.Fprint(w, "\x1b[1m")
}
oldBright = bright
}
}
defer setColor(9, 9, 0)
tileSize := tiles[0][0][0][0].Rect.Size()
width, height := img.Rect.Dx(), img.Rect.Dy()
if width%tileSize.X != 0 || height%tileSize.Y != 0 {
return ErrDimensionsMismatch
}
width /= tileSize.X
height /= tileSize.Y
tileRect := image.Rectangle{image.ZP, tileSize}.Add(img.Rect.Min)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
bg, fg, bright, ch, err := tiles.Match(img.SubImage(tileRect.Add(image.Pt(x*tileSize.X, y*tileSize.Y))).(*image.RGBA))
if err != nil {
return err
}
setColor(bg, fg, bright)
fmt.Fprint(w, string(cp437[ch]))
}
fmt.Fprintln(w)
}
return nil
}