-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathqoi.go
238 lines (200 loc) · 5.37 KB
/
qoi.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package qoi
/*
QOI - The “Quite OK Image” format for fast, lossless image compression
Original version by Dominic Szablewski - https://phoboslab.org
Go version by Xavier-Frédéric Moulet
*/
import (
"bufio"
"encoding/binary"
"errors"
"image"
"image/color"
"io"
)
const (
qoi_INDEX byte = 0b00_000000
qoi_DIFF byte = 0b01_000000
qoi_LUMA byte = 0b10_000000
qoi_RUN byte = 0b11_000000
qoi_RGB byte = 0b1111_1110
qoi_RGBA byte = 0b1111_1111
qoi_MASK_2 byte = 0b11_000000
)
const qoiMagic = "qoif"
const qoiPixelsMax = 400_000_000 // 400 million pixels ought to be enough for anybody
func qoi_COLOR_HASH(r, g, b, a byte) byte {
return byte(r*3 + g*5 + b*7 + a*11)
}
type pixel [4]byte
func Decode(r io.Reader) (image.Image, error) {
cfg, err := DecodeConfig(r)
if err != nil {
return nil, err
}
NBPixels := cfg.Width * cfg.Height
if NBPixels == 0 || NBPixels > qoiPixelsMax {
return nil, errors.New("bad image dimensions")
}
b := bufio.NewReader(r)
img := image.NewNRGBA(image.Rect(0, 0, cfg.Width, cfg.Height))
var index [64]pixel
run := 0
pixels := img.Pix // pixels yet to write
px := pixel{0, 0, 0, 255}
for len(pixels) > 0 {
if run > 0 {
run--
} else {
b1, err := b.ReadByte()
if err == io.EOF {
return img, nil
}
if err != nil {
return nil, err
}
switch {
case b1 == qoi_RGB:
_, err = io.ReadFull(b, px[:3])
if err != nil {
return nil, err
}
case b1 == qoi_RGBA:
_, err = io.ReadFull(b, px[:])
if err != nil {
return nil, err
}
case b1&qoi_MASK_2 == qoi_INDEX:
px = index[b1]
case b1&qoi_MASK_2 == qoi_DIFF:
px[0] += ((b1 >> 4) & 0x03) - 2
px[1] += ((b1 >> 2) & 0x03) - 2
px[2] += (b1 & 0x03) - 2
case b1&qoi_MASK_2 == qoi_LUMA:
b2, err := b.ReadByte()
if err != nil {
return nil, err
}
vg := (b1 & 0b00111111) - 32
px[0] += vg - 8 + ((b2 >> 4) & 0x0f)
px[1] += vg
px[2] += vg - 8 + (b2 & 0x0f)
case b1&qoi_MASK_2 == qoi_RUN:
run = int(b1 & 0b00111111)
default:
px = pixel{255, 0, 255, 255} // should not happen
}
index[int(qoi_COLOR_HASH(px[0], px[1], px[2], px[3]))%len(index)] = px
}
// TODO stride ..
copy(pixels[:4], px[:])
pixels = pixels[4:] // advance
}
return img, nil
}
func Encode(w io.Writer, m image.Image) error {
var out = bufio.NewWriter(w)
minX := m.Bounds().Min.X
maxX := m.Bounds().Max.X
minY := m.Bounds().Min.Y
maxY := m.Bounds().Max.Y
NBPixels := (maxX - minX) * (maxY - minY)
if NBPixels == 0 || NBPixels >= qoiPixelsMax {
return errors.New("Bad image Size")
}
// write header to output
if err := binary.Write(out, binary.BigEndian, []byte(qoiMagic)); err != nil {
return err
}
// width
if err := binary.Write(out, binary.BigEndian, uint32(maxX-minX)); err != nil {
return err
}
// height
if err := binary.Write(out, binary.BigEndian, uint32(maxY-minY)); err != nil {
return err
}
// channels
if err := binary.Write(out, binary.BigEndian, uint8(4)); err != nil {
return err
}
// 0b0000rgba colorspace
if err := binary.Write(out, binary.BigEndian, uint8(0)); err != nil {
return err
}
var index [64]pixel
px_prev := pixel{0, 0, 0, 255}
run := 0
for y := minY; y < maxY; y++ {
for x := minX; x < maxX; x++ {
// extract pixel and convert to non-premultiplied
c := color.NRGBAModel.Convert(m.At(x, y))
c_r, c_g, c_b, c_a := c.RGBA()
px := pixel{byte(c_r >> 8), byte(c_g >> 8), byte(c_b >> 8), byte(c_a >> 8)}
if px == px_prev {
run++
last_pixel := x == (maxX-1) && y == (maxY-1)
if run == 62 || last_pixel {
out.WriteByte(qoi_RUN | byte(run-1))
run = 0
}
} else {
if run > 0 {
out.WriteByte(qoi_RUN | byte(run-1))
run = 0
}
var index_pos byte = qoi_COLOR_HASH(px[0], px[1], px[2], px[3]) % 64
if index[index_pos] == px {
out.WriteByte(qoi_INDEX | index_pos)
} else {
index[index_pos] = px
if px[3] == px_prev[3] {
vr := int8(int(px[0]) - int(px_prev[0]))
vg := int8(int(px[1]) - int(px_prev[1]))
vb := int8(int(px[2]) - int(px_prev[2]))
vg_r := vr - vg
vg_b := vb - vg
if vr > -3 && vr < 2 && vg > -3 && vg < 2 && vb > -3 && vb < 2 {
out.WriteByte(qoi_DIFF | byte((vr+2)<<4|(vg+2)<<2|(vb+2)))
} else if vg_r > -9 && vg_r < 8 && vg > -33 && vg < 32 && vg_b > -9 && vg_b < 8 {
out.WriteByte(qoi_LUMA | byte(vg+32))
out.WriteByte(byte((vg_r+8)<<4) | byte(vg_b+8))
} else {
out.WriteByte(qoi_RGB)
out.WriteByte(px[0])
out.WriteByte(px[1])
out.WriteByte(px[2])
}
} else {
out.WriteByte(qoi_RGBA)
for i := 0; i < 4; i++ {
out.WriteByte(px[i])
}
}
}
}
px_prev = px
}
}
binary.Write(out, binary.BigEndian, uint32(0)) // padding
binary.Write(out, binary.BigEndian, uint32(1)) // padding
return out.Flush()
}
func DecodeConfig(r io.Reader) (cfg image.Config, err error) {
var header [4 + 4 + 4 + 1 + 1]byte
if _, err = io.ReadAtLeast(r, header[:], len(header)); err != nil {
return
}
if string(header[:4]) != qoiMagic {
return cfg, errors.New("Invalid magic")
}
// only decodes as NRGBA images
return image.Config{
Width: int(binary.BigEndian.Uint32(header[4:])),
Height: int(binary.BigEndian.Uint32(header[8:])),
ColorModel: color.NRGBAModel,
}, err
}
func init() {
image.RegisterFormat("qoi", qoiMagic, Decode, DecodeConfig)
}