-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitmap_wrap.go
176 lines (166 loc) · 3.61 KB
/
bitmap_wrap.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package gotrace
import (
"fmt"
"image"
"image/color"
"io"
"math"
"os"
"github.com/gotranspile/cxgo/runtime/stdio"
)
func BitmapFromGray(img *image.Gray, fnc func(c color.Gray) bool) *Bitmap {
if fnc == nil {
fnc = func(c color.Gray) bool {
return c.Y < math.MaxUint8/2
}
}
sz := img.Rect.Size()
bm := NewBitmap(sz.X, sz.Y)
for y := 0; y < sz.Y; y++ {
for x := 0; x < sz.X; x++ {
bm.Put(x, sz.Y-1-y, fnc(img.GrayAt(img.Rect.Min.X+x, img.Rect.Min.Y+y)))
}
}
return bm
}
func BitmapFromGray16(img *image.Gray16, fnc func(c color.Gray16) bool) *Bitmap {
if fnc == nil {
fnc = func(c color.Gray16) bool {
return c.Y < math.MaxUint16/2
}
}
sz := img.Rect.Size()
bm := NewBitmap(sz.X, sz.Y)
for y := 0; y < sz.Y; y++ {
for x := 0; x < sz.X; x++ {
bm.Put(x, sz.Y-1-y, fnc(img.Gray16At(img.Rect.Min.X+x, img.Rect.Min.Y+y)))
}
}
return bm
}
func BitmapFromNRGBA(img *image.NRGBA, fnc func(c color.NRGBA) bool) *Bitmap {
if fnc == nil {
fnc = func(c color.NRGBA) bool {
if c.A < 128 {
return true
}
return c.R+c.G+c.B < 128
}
}
sz := img.Rect.Size()
bm := NewBitmap(sz.X, sz.Y)
for y := 0; y < sz.Y; y++ {
for x := 0; x < sz.X; x++ {
bm.Put(x, sz.Y-1-y, fnc(img.NRGBAAt(img.Rect.Min.X+x, img.Rect.Min.Y+y)))
}
}
return bm
}
func BitmapFromRGBA(img *image.RGBA, fnc func(c color.RGBA) bool) *Bitmap {
if fnc == nil {
fnc = func(c color.RGBA) bool {
if c.A < 128 {
return true
}
return c.R+c.G+c.B < 128
}
}
sz := img.Rect.Size()
bm := NewBitmap(sz.X, sz.Y)
for y := 0; y < sz.Y; y++ {
for x := 0; x < sz.X; x++ {
bm.Put(x, sz.Y-1-y, fnc(img.RGBAAt(img.Rect.Min.X+x, img.Rect.Min.Y+y)))
}
}
return bm
}
func BitmapFromImage(img image.Image, fnc func(c color.Color) bool) *Bitmap {
switch img := img.(type) {
case *image.Gray:
if fnc == nil {
return BitmapFromGray(img, nil)
}
return BitmapFromGray(img, func(c color.Gray) bool {
return fnc(c)
})
case *image.Gray16:
if fnc == nil {
return BitmapFromGray16(img, nil)
}
return BitmapFromGray16(img, func(c color.Gray16) bool {
return fnc(c)
})
case *image.NRGBA:
if fnc == nil {
return BitmapFromNRGBA(img, nil)
}
return BitmapFromNRGBA(img, func(c color.NRGBA) bool {
return fnc(c)
})
case *image.RGBA:
if fnc == nil {
return BitmapFromRGBA(img, nil)
}
return BitmapFromRGBA(img, func(c color.RGBA) bool {
return fnc(c)
})
}
if fnc == nil {
fnc = func(c color.Color) bool {
r, g, b, a := c.RGBA()
if a < 128 {
return true
}
return r+g+b < 128
}
}
rect := img.Bounds()
sz := rect.Size()
bm := NewBitmap(sz.X, sz.Y)
for y := 0; y < sz.Y; y++ {
for x := 0; x < sz.X; x++ {
bm.Put(x, sz.Y-1-y, fnc(img.At(rect.Min.X+x, rect.Min.Y+y)))
}
}
return bm
}
func BitmapReadFile(path string, threshold float64) (*Bitmap, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return bitmapReadFile(f, threshold)
}
func BitmapRead(r io.Reader, threshold float64) (*Bitmap, error) {
if f, ok := r.(*os.File); ok {
return bitmapReadFile(f, threshold)
}
pr, pw, err := os.Pipe()
if err != nil {
return nil, err
}
errc := make(chan error, 1)
go func() {
defer close(errc)
defer pw.Close()
if _, err := io.Copy(pw, r); err != nil {
errc <- err
}
}()
defer pr.Close()
bm, err := bitmapReadFile(pr, threshold)
if err != nil {
return nil, err
}
err = <-errc
return bm, err
}
func bitmapReadFile(f *os.File, threshold float64) (*Bitmap, error) {
var bm *Bitmap
e := bitmapRead(stdio.OpenFrom(f), threshold, &bm)
if e != 0 {
return nil, fmt.Errorf("error code: %d", e)
}
return bm, nil
}