Debian Codegraph @

shiny/iconvg/buffer.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package iconvg
6
7 import (
8 "image/color"
9 "math"
10 )
11
12 // buffer holds an encoded IconVG graphic.
13 //
14 // The decodeXxx methods return the decoded value and an integer n, the number
15 // of bytes that value was encoded in. They return n == 0 if an error occurred.
16 //
17 // The encodeXxx methods append to the buffer, modifying the slice in place.
18 type buffer []byte
19
20 func (b buffer) decodeNatural() (u uint32, n int) {
21 if len(b) < 1 {
22 return 0, 0
23 }
24 x := b[0]
25 if x&0x01 == 0 {
26 return uint32(x) >> 1, 1
27 }
28 if x&0x02 == 0 {
29 if len(b) >= 2 {
30 y := uint16(b[0]) | uint16(b[1])<<8
31 return uint32(y) >> 2, 2
32 }
33 return 0, 0
34 }
35 if len(b) >= 4 {
36 y := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
37 return y >> 2, 4
38 }
39 return 0, 0
40 }
41
42 // decodeNaturalFFV1 is like decodeNatural but for File Format Version 1. See
43 // https://github.com/google/iconvg/issues/33
44 func (b buffer) decodeNaturalFFV1() (u uint32, n int) {
45 if len(b) < 1 {
46 return 0, 0
47 }
48 x := b[0]
49 if x&0x01 != 0 {
50 return uint32(x) >> 1, 1
51 }
52 if x&0x02 != 0 {
53 if len(b) >= 2 {
54 y := uint16(b[0]) | uint16(b[1])<<8
55 return uint32(y) >> 2, 2
56 }
57 return 0, 0
58 }
59 if len(b) >= 4 {
60 y := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
61 return y >> 2, 4
62 }
63 return 0, 0
64 }
65
66 func (b buffer) decodeReal() (f float32, n int) {
67 switch u, n := b.decodeNatural(); n {
68 case 0:
69 return 0, n
70 case 1:
71 return float32(u), n
72 case 2:
73 return float32(u), n
74 default:
75 return math.Float32frombits(u << 2), n
76 }
77 }
78
79 func (b buffer) decodeCoordinate() (f float32, n int) {
80 switch u, n := b.decodeNatural(); n {
81 case 0:
82 return 0, n
83 case 1:
84 return float32(int32(u) - 64), n
85 case 2:
86 return float32(int32(u)-64*128) / 64, n
87 default:
88 return math.Float32frombits(u << 2), n
89 }
90 }
91
92 func (b buffer) decodeZeroToOne() (f float32, n int) {
93 switch u, n := b.decodeNatural(); n {
94 case 0:
95 return 0, n
96 case 1:
97 return float32(u) / 120, n
98 case 2:
99 return float32(u) / 15120, n
100 default:
101 return math.Float32frombits(u << 2), n
102 }
103 }
104
105 func (b buffer) decodeColor1() (c Color, n int) {
106 if len(b) < 1 {
107 return Color{}, 0
108 }
109 return decodeColor1(b[0]), 1
110 }
111
112 func (b buffer) decodeColor2() (c Color, n int) {
113 if len(b) < 2 {
114 return Color{}, 0
115 }
116 return RGBAColor(color.RGBA{
117 R: 0x11 * (b[0] >> 4),
118 G: 0x11 * (b[0] & 0x0f),
119 B: 0x11 * (b[1] >> 4),
120 A: 0x11 * (b[1] & 0x0f),
121 }), 2
122 }
123
124 func (b buffer) decodeColor3Direct() (c Color, n int) {
125 if len(b) < 3 {
126 return Color{}, 0
127 }
128 return RGBAColor(color.RGBA{
129 R: b[0],
130 G: b[1],
131 B: b[2],
132 A: 0xff,
133 }), 3
134 }
135
136 func (b buffer) decodeColor4() (c Color, n int) {
137 if len(b) < 4 {
138 return Color{}, 0
139 }
140 return RGBAColor(color.RGBA{
141 R: b[0],
142 G: b[1],
143 B: b[2],
144 A: b[3],
145 }), 4
146 }
147
148 func (b buffer) decodeColor3Indirect() (c Color, n int) {
149 if len(b) < 3 {
150 return Color{}, 0
151 }
152 return BlendColor(b[0], b[1], b[2]), 3
153 }
154
155 func (b *buffer) encodeNatural(u uint32) {
156 if u < 1<<7 {
157 u = (u << 1)
158 *b = append(*b, uint8(u))
159 return
160 }
161 if u < 1<<14 {
162 u = (u << 2) | 1
163 *b = append(*b, uint8(u), uint8(u>>8))
164 return
165 }
166 u = (u << 2) | 3
167 *b = append(*b, uint8(u), uint8(u>>8), uint8(u>>16), uint8(u>>24))
168 }
169
170 // encodeNaturalFFV1 is like encodeNatural but for File Format Version 1. See
171 // https://github.com/google/iconvg/issues/33
172 func (b *buffer) encodeNaturalFFV1(u uint32) {
173 if u < 1<<7 {
174 u = (u << 1) | 0x01
175 *b = append(*b, uint8(u))
176 return
177 }
178 if u < 1<<14 {
179 u = (u << 2) | 0x02
180 *b = append(*b, uint8(u), uint8(u>>8))
181 return
182 }
183 u = (u << 2)
184 *b = append(*b, uint8(u), uint8(u>>8), uint8(u>>16), uint8(u>>24))
185 }
186
187 func (b *buffer) encodeReal(f float32) int {
188 if u := uint32(f); float32(u) == f && u < 1<<14 {
189 if u < 1<<7 {
190 u = (u << 1)
191 *b = append(*b, uint8(u))
192 return 1
193 }
194 u = (u << 2) | 1
195 *b = append(*b, uint8(u), uint8(u>>8))
196 return 2
197 }
198 b.encode4ByteReal(f)
199 return 4
200 }
201
202 func (b *buffer) encode4ByteReal(f float32) {
203 u := math.Float32bits(f)
204
205 // Round the fractional bits (the low 23 bits) to the nearest multiple of
206 // 4, being careful not to overflow into the upper bits.
207 v := u & 0x007fffff
208 if v < 0x007ffffe {
209 v += 2
210 }
211 u = (u & 0xff800000) | v
212
213 // A 4 byte encoding has the low two bits set.
214 u |= 0x03
215 *b = append(*b, uint8(u), uint8(u>>8), uint8(u>>16), uint8(u>>24))
216 }
217
218 // encode4ByteRealFFV1 is like encode4ByteReal but for File Format Version 1.
219 // See https://github.com/google/iconvg/issues/33
220 func (b *buffer) encode4ByteRealFFV1(f float32) {
221 u := math.Float32bits(f)
222
223 // Round the fractional bits (the low 23 bits) to the nearest multiple of
224 // 4, being careful not to overflow into the upper bits.
225 v := u & 0x007fffff
226 if v < 0x007ffffe {
227 v += 2
228 }
229 u = (u & 0xff800000) | v
230
231 // A 4 byte encoding has the low two bits unset.
232 u &= 0xfffffffc
233 *b = append(*b, uint8(u), uint8(u>>8), uint8(u>>16), uint8(u>>24))
234 }
235
236 func (b *buffer) encodeCoordinate(f float32) int {
237 if i := int32(f); -64 <= i && i < +64 && float32(i) == f {
238 u := uint32(i + 64)
239 u = (u << 1)
240 *b = append(*b, uint8(u))
241 return 1
242 }
243 if i := int32(f * 64); -128*64 <= i && i < +128*64 && float32(i) == f*64 {
244 u := uint32(i + 128*64)
245 u = (u << 2) | 1
246 *b = append(*b, uint8(u), uint8(u>>8))
247 return 2
248 }
249 b.encode4ByteReal(f)
250 return 4
251 }
252
253 // encodeCoordinateFFV1 is like encodeCoordinate but for File Format Version 1.
254 // See https://github.com/google/iconvg/issues/33
255 func (b *buffer) encodeCoordinateFFV1(f float32) int {
256 if i := int32(f); -64 <= i && i < +64 && float32(i) == f {
257 u := uint32(i + 64)
258 u = (u << 1) | 0x01
259 *b = append(*b, uint8(u))
260 return 1
261 }
262 if i := int32(f * 64); -128*64 <= i && i < +128*64 && float32(i) == f*64 {
263 u := uint32(i + 128*64)
264 u = (u << 2) | 0x02
265 *b = append(*b, uint8(u), uint8(u>>8))
266 return 2
267 }
268 b.encode4ByteRealFFV1(f)
269 return 4
270 }
271
272 func (b *buffer) encodeCoordinatePairFFV1(f [2]float32) int {
273 n0 := b.encodeCoordinateFFV1(f[0])
274 n1 := b.encodeCoordinateFFV1(f[1])
275 return n0 + n1
276 }
277
278 func (b *buffer) encodeAngle(f float32) int {
279 // Normalize f to the range [0, 1).
280 g := float64(f)
281 g -= math.Floor(g)
282 return b.encodeZeroToOne(float32(g))
283 }
284
285 func (b *buffer) encodeZeroToOne(f float32) int {
286 if u := uint32(f * 15120); float32(u) == f*15120 && u < 15120 {
287 if u%126 == 0 {
288 u = ((u / 126) << 1)
289 *b = append(*b, uint8(u))
290 return 1
291 }
292 u = (u << 2) | 1
293 *b = append(*b, uint8(u), uint8(u>>8))
294 return 2
295 }
296 b.encode4ByteReal(f)
297 return 4
298 }
299
300 func (b *buffer) encodeColor1(c Color) {
301 if x, ok := encodeColor1(c); ok {
302 *b = append(*b, x)
303 return
304 }
305 // Default to opaque black.
306 *b = append(*b, 0x00)
307 }
308
309 func (b *buffer) encodeColor2(c Color) {
310 if x, ok := encodeColor2(c); ok {
311 *b = append(*b, x[0], x[1])
312 return
313 }
314 // Default to opaque black.
315 *b = append(*b, 0x00, 0x0f)
316 }
317
318 func (b *buffer) encodeColor3Direct(c Color) {
319 if x, ok := encodeColor3Direct(c); ok {
320 *b = append(*b, x[0], x[1], x[2])
321 return
322 }
323 // Default to opaque black.
324 *b = append(*b, 0x00, 0x00, 0x00)
325 }
326
327 func (b *buffer) encodeColor4(c Color) {
328 if x, ok := encodeColor4(c); ok {
329 *b = append(*b, x[0], x[1], x[2], x[3])
330 return
331 }
332 // Default to opaque black.
333 *b = append(*b, 0x00, 0x00, 0x00, 0xff)
334 }
335
336 func (b *buffer) encodeColor3Indirect(c Color) {
337 if x, ok := encodeColor3Indirect(c); ok {
338 *b = append(*b, x[0], x[1], x[2])
339 return
340 }
341 // Default to opaque black.
342 *b = append(*b, 0x00, 0x00, 0x00)
343 }