/
ukrcode.go
138 lines (117 loc) · 2.63 KB
/
ukrcode.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
package main
import (
// "fmt"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/datamatrix"
"image"
"image/color"
"image/draw"
"image/png"
"os"
)
const (
any int = iota
set
skp
emp
)
var (
white = color.White
black = color.RGBA{0x21, 0x21, 0x21, 0xff}
red = color.RGBA{0xf4, 0x43, 0x36, 0xff}
)
type Bitmap struct {
data []bool
width int
height int
}
func (b *Bitmap) valid(x, y int) bool {
return (x >= 0) && (y >= 0) && (x < b.width) && (y < b.height)
}
func (b *Bitmap) Set(x, y int, value bool) {
if b.valid(x, y) {
b.data[y*b.width+x] = value
}
}
func (b *Bitmap) Get(x, y int) bool {
if b.valid(x, y) {
return b.data[y*b.width+x]
}
return false
}
func Match(b *Bitmap, p [][]int, sx, sy int) bool {
for y := 0; y < len(p); y++ {
for x := 0; x < len(p[y]); x++ {
if p[y][x] == any {
continue
}
if (p[y][x] == set || p[y][x] == skp) && (!b.Get(sx + x, sy + y)) {
return false
}
if p[y][x] == emp && (b.Get(sx + x, sy + y)) {
return false
}
}
}
return true
}
func Draw(i *image.RGBA, b *Bitmap, p [][]int, sx, sy, scale int) {
for y := 0; y < len(p); y++ {
for x := 0; x < len(p[y]); x++ {
if p[y][x] != set && p[y][x] != skp {
continue
}
b.Set(sx + x, sy + y, false)
color := red;
if p[y][x] == skp {
color = black
}
pixel := image.Rect((sx + x)*scale, (sy + y)*scale, (sx + x+1)*scale, (sy + y+1)*scale)
draw.Draw(i, pixel, &image.Uniform{color}, image.ZP, draw.Src)
}
}
}
func NewBitmap(code barcode.Barcode) *Bitmap {
dim := code.Bounds().Max
b := &Bitmap{data: make([]bool, dim.X*dim.Y), height: dim.Y, width: dim.X}
for y := 0; y < dim.Y; y++ {
for x := 0; x < dim.X; x++ {
if code.At(x, y) == color.Black {
b.Set(x, y, true)
}
}
}
return b
}
func hor(code barcode.Barcode, scale int) image.Image {
b := NewBitmap(code)
img := image.NewRGBA(image.Rect(0, 0, b.height*scale, b.width*scale))
draw.Draw(img, img.Bounds(), &image.Uniform{white}, image.ZP, draw.Src)
patterns := [][][]int {
{ {any, set, any},
{set, skp, set},
{any, set, any}}}
for _, pattern := range patterns {
for y := 0; y < b.height; y++ {
for x := 0; x < b.width; x++ {
if Match(b, pattern, x, y) {
Draw(img, b, pattern, x, y, scale)
}
}
}
}
for y := 0; y < b.height; y++ {
for x := 0; x < b.width; x++ {
if b.Get(x, y) {
pixel := image.Rect(x*scale, y*scale, (x+1)*scale, (y+1)*scale)
draw.Draw(img, pixel, &image.Uniform{black}, image.ZP, draw.Src)
}
}
}
return img
}
func main() {
text := "http://denyspotapov.com/"
code, _ := datamatrix.Encode(text)
png.Encode(os.Stdout, hor(code, 10))
}