-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathbitimage.go
143 lines (112 loc) · 3.02 KB
/
bitimage.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
// stolen and modified from https://github.com/mugli/png2escpos
package escpos
import (
"fmt"
"image"
)
func closestNDivisibleBy8(n int) int {
q := n / 8
n1 := q * 8
return n1
}
func printImage(img image.Image) (xL byte, xH byte, yL byte, yH byte, data []byte) {
width, height, pixels := getPixels(img)
removeTransparency(&pixels)
makeGrayscale(&pixels)
printWidth := closestNDivisibleBy8(width)
printHeight := closestNDivisibleBy8(height)
bytes, _ := rasterize(printWidth, printHeight, &pixels)
return byte((printWidth >> 3) & 0xff), byte(((printWidth >> 3) >> 8) & 0xff), byte(printHeight & 0xff), byte((printHeight >> 8) & 0xff), bytes
}
func makeGrayscale(pixels *[][]pixel) {
height := len(*pixels)
width := len((*pixels)[0])
for y := 0; y < height; y++ {
row := (*pixels)[y]
for x := 0; x < width; x++ {
pixel := row[x]
luminance := (float64(pixel.R) * 0.299) + (float64(pixel.G) * 0.587) + (float64(pixel.B) * 0.114)
var value int
if luminance < 128 {
value = 0
} else {
value = 255
}
pixel.R = value
pixel.G = value
pixel.B = value
row[x] = pixel
}
}
}
func removeTransparency(pixels *[][]pixel) {
height := len(*pixels)
width := len((*pixels)[0])
for y := 0; y < height; y++ {
row := (*pixels)[y]
for x := 0; x < width; x++ {
pixel := row[x]
alpha := pixel.A
invAlpha := 255 - alpha
pixel.R = (alpha*pixel.R + invAlpha*255) / 255
pixel.G = (alpha*pixel.G + invAlpha*255) / 255
pixel.B = (alpha*pixel.B + invAlpha*255) / 255
pixel.A = 255
row[x] = pixel
}
}
}
func rasterize(printWidth int, printHeight int, pixels *[][]pixel) ([]byte, error) {
if printWidth%8 != 0 {
return nil, fmt.Errorf("printWidth must be a multiple of 8")
}
if printHeight%8 != 0 {
return nil, fmt.Errorf("printHeight must be a multiple of 8")
}
bytes := make([]byte, (printWidth*printHeight)>>3)
for y := 0; y < printHeight; y++ {
for x := 0; x < printWidth; x = x + 8 {
i := y*(printWidth>>3) + (x >> 3)
bytes[i] =
byte((getPixelValue(x+0, y, pixels) << 7) |
(getPixelValue(x+1, y, pixels) << 6) |
(getPixelValue(x+2, y, pixels) << 5) |
(getPixelValue(x+3, y, pixels) << 4) |
(getPixelValue(x+4, y, pixels) << 3) |
(getPixelValue(x+5, y, pixels) << 2) |
(getPixelValue(x+6, y, pixels) << 1) |
getPixelValue(x+7, y, pixels))
}
}
return bytes, nil
}
func getPixelValue(x int, y int, pixels *[][]pixel) int {
row := (*pixels)[y]
pixel := row[x]
if pixel.R > 0 {
return 0
}
return 1
}
func rgbaToPixel(r uint32, g uint32, b uint32, a uint32) pixel {
return pixel{int(r >> 8), int(g >> 8), int(b >> 8), int(a >> 8)}
}
type pixel struct {
R int
G int
B int
A int
}
func getPixels(img image.Image) (int, int, [][]pixel) {
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
var pixels [][]pixel
for y := 0; y < height; y++ {
var row []pixel
for x := 0; x < width; x++ {
row = append(row, rgbaToPixel(img.At(x, y).RGBA()))
}
pixels = append(pixels, row)
}
return width, height, pixels
}