forked from hwhw/img2r0ket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
img2r0ket.go
168 lines (149 loc) · 3.91 KB
/
img2r0ket.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
/*
* Program to convert PBM images to r0ket (http://r0ket.badge.events.ccc.de/) format images
*
* (c) 2011 Hans-Werner Hilse
*
* this software is published under the know-what-you're-doing licence:
*
* 1. you may use this software for whatever you want, given that you know what you're doing.
* 2. author of this software isn't responsible for anything since you knew what you're doing.
* 3. if you have still questions, chances are you do not fullfill requirements
*----------------------------------------------------------------------------------------------
*
* Changelog:
*
* 0.1: accepts raw PBM images
*/
package main
import (
"flag"
"log"
"os"
"io"
"io/ioutil"
"strconv"
"github.com/hwhw/golzo"
)
var inputFile = flag.String("i", "/proc/self/fd/0", "input file (PBM format)")
var outputFile = flag.String("o", "/proc/self/fd/1", "output file (r0ket format)")
var invert = flag.Bool("x", false, "invert output bits")
var compress = flag.Bool("L", false, "compress with LZO, outputs 16bits of compressed block length + compressed block")
const (
/* software will enforce these dimensions: */
IMG_WIDTH = 96
IMG_HEIGHT = 68
)
func readImg(imgReader io.Reader) (r0ketimg []byte) {
input, err := ioutil.ReadAll(imgReader) // I'm lazy
if err != nil {
log.Fatal(err)
}
log.Printf("got %d bytes of data", len(input))
// check header:
if input[0] != 'P' || input[1] != '4' || (input[2] >= '0' && input[2] <= '9') {
log.Fatal("is not a raw PBM image")
}
// scan for beginning of a number:
p1 := 3
for input[p1] < '0' || input[p1] > '9' {
if input[p1] == '#' {
// comment line, ignore until next EOL
for input[p1] != '\n' {
p1++ // TODO: check length!
}
} else {
p1++
}
}
// number starts at <p1>
p2 := p1
for input[p2] >= '0' && input[p2] <= '9' {
p2++
}
width, _ := strconv.Atoi(string(input[p1:p2]))
p1 = p2
for input[p1] < '0' || input[p1] > '9' {
if input[p1] == '#' {
for input[p1] != '\n' {
p1++
}
} else {
p1++
}
}
p2 = p1
for input[p2] >= '0' && input[p2] <= '9' {
p2++
}
height, _ := strconv.Atoi(string(input[p1:p2]))
if width != IMG_WIDTH || height != IMG_HEIGHT {
log.Fatal("Error, wrong size")
}
p2++
if len(input) < (p2 + ((width + 7) >> 3)*height) {
log.Fatal("Not enough data in the image, image file broken?")
}
bitmap := make([]byte, width * height) // TODO: optimize this out into a single parse?
stride := (width + 7) >> 3
log.Printf("width: %d, stride: %d", width, stride)
/* parse input data into bitmap */
for y := 0; y < height; y++ {
for x := 0; x < stride; x++ {
for b := 0; b < 8; b++ {
tx := (x+1)*8 - 1 - b
if tx < width {
bitmap[y*width + tx] = input[p2] & 1
}
input[p2] >>= 1
}
p2++
}
}
r0ketimg = make([]byte, width * ((height+7) >> 3))
/* create output data from bitmap: */
for y:=0; y < (height+7) >> 3; y++ {
for x:=0; x < width; x++ {
for b:=uint(0); b < 8 ; b++ {
ox := ((width - x)) - 1
oy := ((height - y*8) - 1) - int(b)
px := ox + oy * width
if px >= 0 {
if (bitmap[px] > 0 && !*invert) || (bitmap[px] == 0 && *invert) {
r0ketimg[y*width + x] |= 1 << b
}
}
}
}
}
return
}
func main() {
flag.Parse()
f, err := os.Open(*inputFile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
fout, err := os.OpenFile(*outputFile, os.O_WRONLY | os.O_TRUNC | os.O_CREATE, 0644)
if err != nil {
log.Fatal(err)
}
defer fout.Close()
out := readImg(f)
if *compress {
ret := golzo.Lzo_init()
if ret != 0 {
log.Fatal("LZO error: ", ret)
}
comp := golzo.Lzo1x_999_compress(out)
if len(comp) > len(out) {
log.Printf("not compressable: %d bytes of output for %d bytes input after trying", len(comp), len(out))
fout.Write([]byte{0,0})
} else {
log.Printf("compressed: %d bytes of output for %d bytes input", len(comp), len(out))
fout.Write([]byte{byte(len(comp) & 0xff), byte((len(comp) >> 8) & 0xff)})
out = comp
}
}
fout.Write(out)
}