| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package coding |
| 7 |
|
| 8 |
import ( |
| 9 |
"fmt" |
| 10 |
"strconv" |
| 11 |
"strings" |
| 12 |
"github.com/vitrun/qart/gf256" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
var Field = gf256.NewField(0x11d, 2) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
type Version int |
| 24 |
|
| 25 |
const MinVersion = 1 |
| 26 |
const MaxVersion = 40 |
| 27 |
|
| 28 |
func (v Version) String() string { |
| 29 |
return strconv.Itoa(int(v)) |
| 30 |
} |
| 31 |
|
| 32 |
func (v Version) sizeClass() int { |
| 33 |
if v <= 9 { |
| 34 |
return 0 |
| 35 |
} |
| 36 |
if v <= 26 { |
| 37 |
return 1 |
| 38 |
} |
| 39 |
return 2 |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
func (v Version) DataBytes(l Level) int { |
| 45 |
vt := &vtab[v] |
| 46 |
lev := &vt.level[l] |
| 47 |
return vt.bytes - lev.nblock*lev.check |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
type Encoding interface { |
| 55 |
Check() error |
| 56 |
Bits(v Version) int |
| 57 |
Encode(b *Bits, v Version) |
| 58 |
} |
| 59 |
|
| 60 |
type Bits struct { |
| 61 |
b []byte |
| 62 |
nbit int |
| 63 |
} |
| 64 |
|
| 65 |
func (b *Bits) Reset() { |
| 66 |
b.b = b.b[:0] |
| 67 |
b.nbit = 0 |
| 68 |
} |
| 69 |
|
| 70 |
func (b *Bits) Bits() int { |
| 71 |
return b.nbit |
| 72 |
} |
| 73 |
|
| 74 |
func (b *Bits) Bytes() []byte { |
| 75 |
if b.nbit%8 != 0 { |
| 76 |
panic("fractional byte") |
| 77 |
} |
| 78 |
return b.b |
| 79 |
} |
| 80 |
|
| 81 |
func (b *Bits) Append(p []byte) { |
| 82 |
if b.nbit%8 != 0 { |
| 83 |
panic("fractional byte") |
| 84 |
} |
| 85 |
b.b = append(b.b, p...) |
| 86 |
b.nbit += 8 * len(p) |
| 87 |
} |
| 88 |
|
| 89 |
func (b *Bits) Write(v uint, nbit int) { |
| 90 |
for nbit > 0 { |
| 91 |
n := nbit |
| 92 |
if n > 8 { |
| 93 |
n = 8 |
| 94 |
} |
| 95 |
if b.nbit%8 == 0 { |
| 96 |
b.b = append(b.b, 0) |
| 97 |
} else { |
| 98 |
m := -b.nbit & 7 |
| 99 |
if n > m { |
| 100 |
n = m |
| 101 |
} |
| 102 |
} |
| 103 |
b.nbit += n |
| 104 |
sh := uint(nbit - n) |
| 105 |
b.b[len(b.b)-1] |= uint8(v >> sh << uint(-b.nbit&7)) |
| 106 |
v -= v >> sh << sh |
| 107 |
nbit -= n |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
type Num string |
| 114 |
|
| 115 |
func (s Num) String() string { |
| 116 |
return fmt.Sprintf("Num(%#q)", string(s)) |
| 117 |
} |
| 118 |
|
| 119 |
func (s Num) Check() error { |
| 120 |
for _, c := range s { |
| 121 |
if c < '0' || '9' < c { |
| 122 |
return fmt.Errorf("non-numeric string %#q", string(s)) |
| 123 |
} |
| 124 |
} |
| 125 |
return nil |
| 126 |
} |
| 127 |
|
| 128 |
var numLen = [3]int{10, 12, 14} |
| 129 |
|
| 130 |
func (s Num) Bits(v Version) int { |
| 131 |
return 4 + numLen[v.sizeClass()] + (10*len(s)+2)/3 |
| 132 |
} |
| 133 |
|
| 134 |
func (s Num) Encode(b *Bits, v Version) { |
| 135 |
b.Write((uint)(1), 4) |
| 136 |
b.Write(uint(len(s)), numLen[v.sizeClass()]) |
| 137 |
var i int |
| 138 |
for i = 0; i+3 <= len(s); i += 3 { |
| 139 |
w := uint(s[i]-'0')*100 + uint(s[i+1]-'0')*10 + uint(s[i+2]-'0') |
| 140 |
b.Write(w, 10) |
| 141 |
} |
| 142 |
switch len(s) - i { |
| 143 |
case 1: |
| 144 |
w := uint(s[i] - '0') |
| 145 |
b.Write(w, 4) |
| 146 |
case 2: |
| 147 |
w := uint(s[i]-'0')*10 + uint(s[i+1]-'0') |
| 148 |
b.Write(w, 7) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
type Alpha string |
| 155 |
|
| 156 |
const alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:" |
| 157 |
|
| 158 |
func (s Alpha) String() string { |
| 159 |
return fmt.Sprintf("Alpha(%#q)", string(s)) |
| 160 |
} |
| 161 |
|
| 162 |
func (s Alpha) Check() error { |
| 163 |
for _, c := range s { |
| 164 |
if strings.IndexRune(alphabet, c) < 0 { |
| 165 |
return fmt.Errorf("non-alphanumeric string %#q", string(s)) |
| 166 |
} |
| 167 |
} |
| 168 |
return nil |
| 169 |
} |
| 170 |
|
| 171 |
var alphaLen = [3]int{9, 11, 13} |
| 172 |
|
| 173 |
func (s Alpha) Bits(v Version) int { |
| 174 |
return 4 + alphaLen[v.sizeClass()] + (11*len(s)+1)/2 |
| 175 |
} |
| 176 |
|
| 177 |
func (s Alpha) Encode(b *Bits, v Version) { |
| 178 |
b.Write((uint)(2), 4) |
| 179 |
b.Write(uint(len(s)), alphaLen[v.sizeClass()]) |
| 180 |
var i int |
| 181 |
for i = 0; i+2 <= len(s); i += 2 { |
| 182 |
w := uint(strings.IndexRune(alphabet, rune(s[i])))*45 + |
| 183 |
uint(strings.IndexRune(alphabet, rune(s[i+1]))) |
| 184 |
b.Write(w, 11) |
| 185 |
} |
| 186 |
|
| 187 |
if i < len(s) { |
| 188 |
w := uint(strings.IndexRune(alphabet, rune(s[i]))) |
| 189 |
b.Write(w, 6) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
type String string |
| 195 |
|
| 196 |
func (s String) String() string { |
| 197 |
return fmt.Sprintf("String(%#q)", string(s)) |
| 198 |
} |
| 199 |
|
| 200 |
func (s String) Check() error { |
| 201 |
return nil |
| 202 |
} |
| 203 |
|
| 204 |
var stringLen = [3]int{8, 16, 16} |
| 205 |
|
| 206 |
func (s String) Bits(v Version) int { |
| 207 |
return 4 + stringLen[v.sizeClass()] + 8*len(s) |
| 208 |
} |
| 209 |
|
| 210 |
func (s String) Encode(b *Bits, v Version) { |
| 211 |
b.Write((uint)(4), 4) |
| 212 |
b.Write(uint(len(s)), stringLen[v.sizeClass()]) |
| 213 |
for i := 0; i < len(s); i++ { |
| 214 |
b.Write(uint(s[i]), 8) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
type Pixel uint32 |
| 220 |
|
| 221 |
const ( |
| 222 |
Black Pixel = 1 << iota |
| 223 |
Invert |
| 224 |
) |
| 225 |
|
| 226 |
func (p Pixel) Offset() uint { |
| 227 |
return uint(p >> 6) |
| 228 |
} |
| 229 |
|
| 230 |
func OffsetPixel(o uint) Pixel { |
| 231 |
return Pixel(o << 6) |
| 232 |
} |
| 233 |
|
| 234 |
func (r PixelRole) Pixel() Pixel { |
| 235 |
return Pixel(r << 2) |
| 236 |
} |
| 237 |
|
| 238 |
func (p Pixel) Role() PixelRole { |
| 239 |
return PixelRole(p>>2) & 15 |
| 240 |
} |
| 241 |
|
| 242 |
func (p Pixel) String() string { |
| 243 |
s := p.Role().String() |
| 244 |
if p&Black != 0 { |
| 245 |
s += "+black" |
| 246 |
} |
| 247 |
if p&Invert != 0 { |
| 248 |
s += "+invert" |
| 249 |
} |
| 250 |
s += "+" + strconv.FormatUint(uint64(p.Offset()), 10) |
| 251 |
return s |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
type PixelRole uint32 |
| 256 |
|
| 257 |
const ( |
| 258 |
_ PixelRole = iota |
| 259 |
Position |
| 260 |
Alignment |
| 261 |
Timing |
| 262 |
Format |
| 263 |
PVersion |
| 264 |
Unused |
| 265 |
Data |
| 266 |
Check |
| 267 |
Extra |
| 268 |
) |
| 269 |
|
| 270 |
var roles = []string{ |
| 271 |
"", |
| 272 |
"position", |
| 273 |
"alignment", |
| 274 |
"timing", |
| 275 |
"format", |
| 276 |
"pversion", |
| 277 |
"unused", |
| 278 |
"data", |
| 279 |
"check", |
| 280 |
"extra", |
| 281 |
} |
| 282 |
|
| 283 |
func (r PixelRole) String() string { |
| 284 |
if Position <= r && r <= Check { |
| 285 |
return roles[r] |
| 286 |
} |
| 287 |
return strconv.Itoa(int(r)) |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
type Level int |
| 293 |
|
| 294 |
const ( |
| 295 |
L Level = iota |
| 296 |
M |
| 297 |
Q |
| 298 |
H |
| 299 |
) |
| 300 |
|
| 301 |
func (l Level) String() string { |
| 302 |
if L <= l && l <= H { |
| 303 |
return "LMQH"[l : l+1] |
| 304 |
} |
| 305 |
return strconv.Itoa(int(l)) |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
type Code struct { |
| 310 |
Bitmap []byte |
| 311 |
Size int |
| 312 |
Stride int |
| 313 |
} |
| 314 |
|
| 315 |
func (c *Code) Black(x, y int) bool { |
| 316 |
return 0 <= x && x < c.Size && 0 <= y && y < c.Size && |
| 317 |
c.Bitmap[y*c.Stride+x/8]&(1<<uint(7-x&7)) != 0 |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
type Mask int |
| 325 |
|
| 326 |
|
| 327 |
var mfunc = []func(int, int) bool{ |
| 328 |
func(i, j int) bool { return (i+j)%2 == 0 }, |
| 329 |
func(i, _ int) bool { return i%2 == 0 }, |
| 330 |
func(_, j int) bool { return j%3 == 0 }, |
| 331 |
func(i, j int) bool { return (i+j)%3 == 0 }, |
| 332 |
func(i, j int) bool { return (i/2+j/3)%2 == 0 }, |
| 333 |
func(i, j int) bool { return i*j%2+i*j%3 == 0 }, |
| 334 |
func(i, j int) bool { return (i*j%2+i*j%3)%2 == 0 }, |
| 335 |
func(i, j int) bool { return (i*j%3+(i+j)%2)%2 == 0 }, |
| 336 |
} |
| 337 |
|
| 338 |
func (m Mask) Invert(y, x int) bool { |
| 339 |
if m < 0 { |
| 340 |
return false |
| 341 |
} |
| 342 |
return mfunc[m](y, x) |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
type Plan struct { |
| 348 |
Version Version |
| 349 |
Level Level |
| 350 |
Mask Mask |
| 351 |
|
| 352 |
DataBytes int |
| 353 |
CheckBytes int |
| 354 |
Blocks int |
| 355 |
|
| 356 |
Pixel [][]Pixel |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
func NewPlan(version Version, level Level, mask Mask) (*Plan, error) { |
| 362 |
p, err := vplan(version) |
| 363 |
if err != nil { |
| 364 |
return nil, err |
| 365 |
} |
| 366 |
if err := fplan(level, mask, p); err != nil { |
| 367 |
return nil, err |
| 368 |
} |
| 369 |
if err := lplan(version, level, p); err != nil { |
| 370 |
return nil, err |
| 371 |
} |
| 372 |
if err := mplan(mask, p); err != nil { |
| 373 |
return nil, err |
| 374 |
} |
| 375 |
return p, nil |
| 376 |
} |
| 377 |
|
| 378 |
func (b *Bits) Pad(n int) { |
| 379 |
if n < 0 { |
| 380 |
panic("qr: invalid pad size") |
| 381 |
} |
| 382 |
if n <= 4 { |
| 383 |
b.Write((uint)(0), n) |
| 384 |
} else { |
| 385 |
b.Write((uint)(0), 4) |
| 386 |
n -= 4 |
| 387 |
n -= -b.Bits() & 7 |
| 388 |
b.Write((uint)(0), -b.Bits()&7) |
| 389 |
pad := n / 8 |
| 390 |
for i := 0; i < pad; i += 2 { |
| 391 |
b.Write((uint)(0xec), 8) |
| 392 |
if i+1 >= pad { |
| 393 |
break |
| 394 |
} |
| 395 |
b.Write((uint)(0x11), 8) |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
func (b *Bits) AddCheckBytes(v Version, l Level) { |
| 401 |
nd := v.DataBytes(l) |
| 402 |
if b.nbit < nd*8 { |
| 403 |
b.Pad(nd*8 - b.nbit) |
| 404 |
} |
| 405 |
if b.nbit != nd*8 { |
| 406 |
panic("qr: too much data") |
| 407 |
} |
| 408 |
|
| 409 |
dat := b.Bytes() |
| 410 |
vt := &vtab[v] |
| 411 |
lev := &vt.level[l] |
| 412 |
db := nd / lev.nblock |
| 413 |
extra := nd % lev.nblock |
| 414 |
chk := make([]byte, lev.check) |
| 415 |
rs := gf256.NewRSEncoder(Field, lev.check) |
| 416 |
for i := 0; i < lev.nblock; i++ { |
| 417 |
if i == lev.nblock-extra { |
| 418 |
db++ |
| 419 |
} |
| 420 |
rs.ECC(dat[:db], chk) |
| 421 |
b.Append(chk) |
| 422 |
dat = dat[db:] |
| 423 |
} |
| 424 |
|
| 425 |
if len(b.Bytes()) != vt.bytes { |
| 426 |
panic("qr: internal error") |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
func (p *Plan) Encode(text ...Encoding) (*Code, error) { |
| 431 |
var b Bits |
| 432 |
for _, t := range text { |
| 433 |
if err := t.Check(); err != nil { |
| 434 |
return nil, err |
| 435 |
} |
| 436 |
t.Encode(&b, p.Version) |
| 437 |
} |
| 438 |
if b.Bits() > p.DataBytes*8 { |
| 439 |
return nil, fmt.Errorf("cannot encode %d bits into %d-bit code", b.Bits(), p.DataBytes*8) |
| 440 |
} |
| 441 |
b.AddCheckBytes(p.Version, p.Level) |
| 442 |
bytes := b.Bytes() |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
c := &Code{Size: len(p.Pixel), Stride: (len(p.Pixel) + 7) &^ 7} |
| 447 |
c.Bitmap = make([]byte, c.Stride*c.Size) |
| 448 |
crow := c.Bitmap |
| 449 |
for _, row := range p.Pixel { |
| 450 |
for x, pix := range row { |
| 451 |
switch pix.Role() { |
| 452 |
case Data, Check: |
| 453 |
o := pix.Offset() |
| 454 |
if bytes[o/8]&(1<<uint(7-o&7)) != 0 { |
| 455 |
pix ^= Black |
| 456 |
} |
| 457 |
} |
| 458 |
if pix&Black != 0 { |
| 459 |
crow[x/8] |= 1 << uint(7-x&7) |
| 460 |
} |
| 461 |
} |
| 462 |
crow = crow[c.Stride:] |
| 463 |
} |
| 464 |
return c, nil |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
type version struct { |
| 469 |
apos int |
| 470 |
astride int |
| 471 |
bytes int |
| 472 |
pattern int |
| 473 |
level [4]level |
| 474 |
} |
| 475 |
|
| 476 |
type level struct { |
| 477 |
nblock int |
| 478 |
check int |
| 479 |
} |
| 480 |
|
| 481 |
var vtab = []version{ |
| 482 |
{}, |
| 483 |
{100, 100, 26, 0x0, [4]level{{1, 7}, {1, 10}, {1, 13}, {1, 17}}}, |
| 484 |
{16, 100, 44, 0x0, [4]level{{1, 10}, {1, 16}, {1, 22}, {1, 28}}}, |
| 485 |
{20, 100, 70, 0x0, [4]level{{1, 15}, {1, 26}, {2, 18}, {2, 22}}}, |
| 486 |
{24, 100, 100, 0x0, [4]level{{1, 20}, {2, 18}, {2, 26}, {4, 16}}}, |
| 487 |
{28, 100, 134, 0x0, [4]level{{1, 26}, {2, 24}, {4, 18}, {4, 22}}}, |
| 488 |
{32, 100, 172, 0x0, [4]level{{2, 18}, {4, 16}, {4, 24}, {4, 28}}}, |
| 489 |
{20, 16, 196, 0x7c94, [4]level{{2, 20}, {4, 18}, {6, 18}, {5, 26}}}, |
| 490 |
{22, 18, 242, 0x85bc, [4]level{{2, 24}, {4, 22}, {6, 22}, {6, 26}}}, |
| 491 |
{24, 20, 292, 0x9a99, [4]level{{2, 30}, {5, 22}, {8, 20}, {8, 24}}}, |
| 492 |
{26, 22, 346, 0xa4d3, [4]level{{4, 18}, {5, 26}, {8, 24}, {8, 28}}}, |
| 493 |
{28, 24, 404, 0xbbf6, [4]level{{4, 20}, {5, 30}, {8, 28}, {11, 24}}}, |
| 494 |
{30, 26, 466, 0xc762, [4]level{{4, 24}, {8, 22}, {10, 26}, {11, 28}}}, |
| 495 |
{32, 28, 532, 0xd847, [4]level{{4, 26}, {9, 22}, {12, 24}, {16, 22}}}, |
| 496 |
{24, 20, 581, 0xe60d, [4]level{{4, 30}, {9, 24}, {16, 20}, {16, 24}}}, |
| 497 |
{24, 22, 655, 0xf928, [4]level{{6, 22}, {10, 24}, {12, 30}, {18, 24}}}, |
| 498 |
{24, 24, 733, 0x10b78, [4]level{{6, 24}, {10, 28}, {17, 24}, {16, 30}}}, |
| 499 |
{28, 24, 815, 0x1145d, [4]level{{6, 28}, {11, 28}, {16, 28}, {19, 28}}}, |
| 500 |
{28, 26, 901, 0x12a17, [4]level{{6, 30}, {13, 26}, {18, 28}, {21, 28}}}, |
| 501 |
{28, 28, 991, 0x13532, [4]level{{7, 28}, {14, 26}, {21, 26}, {25, 26}}}, |
| 502 |
{32, 28, 1085, 0x149a6, [4]level{{8, 28}, {16, 26}, {20, 30}, {25, 28}}}, |
| 503 |
{26, 22, 1156, 0x15683, [4]level{{8, 28}, {17, 26}, {23, 28}, {25, 30}}}, |
| 504 |
{24, 24, 1258, 0x168c9, [4]level{{9, 28}, {17, 28}, {23, 30}, {34, 24}}}, |
| 505 |
{28, 24, 1364, 0x177ec, [4]level{{9, 30}, {18, 28}, {25, 30}, {30, 30}}}, |
| 506 |
{26, 26, 1474, 0x18ec4, [4]level{{10, 30}, {20, 28}, {27, 30}, {32, 30}}}, |
| 507 |
{30, 26, 1588, 0x191e1, [4]level{{12, 26}, {21, 28}, {29, 30}, {35, 30}}}, |
| 508 |
{28, 28, 1706, 0x1afab, [4]level{{12, 28}, {23, 28}, {34, 28}, {37, 30}}}, |
| 509 |
{32, 28, 1828, 0x1b08e, [4]level{{12, 30}, {25, 28}, {34, 30}, {40, 30}}}, |
| 510 |
{24, 24, 1921, 0x1cc1a, [4]level{{13, 30}, {26, 28}, {35, 30}, {42, 30}}}, |
| 511 |
{28, 24, 2051, 0x1d33f, [4]level{{14, 30}, {28, 28}, {38, 30}, {45, 30}}}, |
| 512 |
{24, 26, 2185, 0x1ed75, [4]level{{15, 30}, {29, 28}, {40, 30}, {48, 30}}}, |
| 513 |
{28, 26, 2323, 0x1f250, [4]level{{16, 30}, {31, 28}, {43, 30}, {51, 30}}}, |
| 514 |
{32, 26, 2465, 0x209d5, [4]level{{17, 30}, {33, 28}, {45, 30}, {54, 30}}}, |
| 515 |
{28, 28, 2611, 0x216f0, [4]level{{18, 30}, {35, 28}, {48, 30}, {57, 30}}}, |
| 516 |
{32, 28, 2761, 0x228ba, [4]level{{19, 30}, {37, 28}, {51, 30}, {60, 30}}}, |
| 517 |
{28, 24, 2876, 0x2379f, [4]level{{19, 30}, {38, 28}, {53, 30}, {63, 30}}}, |
| 518 |
{22, 26, 3034, 0x24b0b, [4]level{{20, 30}, {40, 28}, {56, 30}, {66, 30}}}, |
| 519 |
{26, 26, 3196, 0x2542e, [4]level{{21, 30}, {43, 28}, {59, 30}, {70, 30}}}, |
| 520 |
{30, 26, 3362, 0x26a64, [4]level{{22, 30}, {45, 28}, {62, 30}, {74, 30}}}, |
| 521 |
{24, 28, 3532, 0x27541, [4]level{{24, 30}, {47, 28}, {65, 30}, {77, 30}}}, |
| 522 |
{28, 28, 3706, 0x28c69, [4]level{{25, 30}, {49, 28}, {68, 30}, {81, 30}}}, |
| 523 |
} |
| 524 |
|
| 525 |
func grid(siz int) [][]Pixel { |
| 526 |
m := make([][]Pixel, siz) |
| 527 |
pix := make([]Pixel, siz*siz) |
| 528 |
for i := range m { |
| 529 |
m[i], pix = pix[:siz], pix[siz:] |
| 530 |
} |
| 531 |
return m |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
func vplan(v Version) (*Plan, error) { |
| 536 |
p := &Plan{Version: v} |
| 537 |
if v < 1 || v > 40 { |
| 538 |
return nil, fmt.Errorf("invalid QR version %d", int(v)) |
| 539 |
} |
| 540 |
siz := 17 + int(v)*4 |
| 541 |
m := grid(siz) |
| 542 |
p.Pixel = m |
| 543 |
|
| 544 |
|
| 545 |
const ti = 6 |
| 546 |
for i := range m { |
| 547 |
p := Timing.Pixel() |
| 548 |
if i&1 == 0 { |
| 549 |
p |= Black |
| 550 |
} |
| 551 |
m[i][ti] = p |
| 552 |
m[ti][i] = p |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
posBox(m, 0, 0) |
| 557 |
posBox(m, siz-7, 0) |
| 558 |
posBox(m, 0, siz-7) |
| 559 |
|
| 560 |
|
| 561 |
info := &vtab[v] |
| 562 |
for x := 4; x+5 < siz; { |
| 563 |
for y := 4; y+5 < siz; { |
| 564 |
|
| 565 |
if (x < 7 && y < 7) || (x < 7 && y+5 >= siz-7) || (x+5 >= siz-7 && y < 7) { |
| 566 |
} else { |
| 567 |
alignBox(m, x, y) |
| 568 |
} |
| 569 |
if y == 4 { |
| 570 |
y = info.apos |
| 571 |
} else { |
| 572 |
y += info.astride |
| 573 |
} |
| 574 |
} |
| 575 |
if x == 4 { |
| 576 |
x = info.apos |
| 577 |
} else { |
| 578 |
x += info.astride |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
pat := vtab[v].pattern |
| 584 |
if pat != 0 { |
| 585 |
v := pat |
| 586 |
for x := 0; x < 6; x++ { |
| 587 |
for y := 0; y < 3; y++ { |
| 588 |
p := PVersion.Pixel() |
| 589 |
if v&1 != 0 { |
| 590 |
p |= Black |
| 591 |
} |
| 592 |
m[siz-11+y][x] = p |
| 593 |
m[x][siz-11+y] = p |
| 594 |
v >>= 1 |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
m[siz-8][8] = Unused.Pixel() | Black |
| 601 |
|
| 602 |
return p, nil |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
func fplan(l Level, m Mask, p *Plan) error { |
| 607 |
|
| 608 |
fb := uint32(l^1) << 13 |
| 609 |
fb |= uint32(m) << 10 |
| 610 |
const formatPoly = 0x537 |
| 611 |
rem := fb |
| 612 |
for i := 14; i >= 10; i-- { |
| 613 |
if rem&(1<<uint(i)) != 0 { |
| 614 |
rem ^= formatPoly << uint(i-10) |
| 615 |
} |
| 616 |
} |
| 617 |
fb |= rem |
| 618 |
invert := uint32(0x5412) |
| 619 |
siz := len(p.Pixel) |
| 620 |
for i := uint(0); i < 15; i++ { |
| 621 |
pix := Format.Pixel() + OffsetPixel(i) |
| 622 |
if (fb>>i)&1 == 1 { |
| 623 |
pix |= Black |
| 624 |
} |
| 625 |
if (invert>>i)&1 == 1 { |
| 626 |
pix ^= Invert | Black |
| 627 |
} |
| 628 |
|
| 629 |
switch { |
| 630 |
case i < 6: |
| 631 |
p.Pixel[i][8] = pix |
| 632 |
case i < 8: |
| 633 |
p.Pixel[i+1][8] = pix |
| 634 |
case i < 9: |
| 635 |
p.Pixel[8][7] = pix |
| 636 |
default: |
| 637 |
p.Pixel[8][14-i] = pix |
| 638 |
} |
| 639 |
|
| 640 |
switch { |
| 641 |
case i < 8: |
| 642 |
p.Pixel[8][siz-1-int(i)] = pix |
| 643 |
default: |
| 644 |
p.Pixel[siz-1-int(14-i)][8] = pix |
| 645 |
} |
| 646 |
} |
| 647 |
return nil |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
func lplan(v Version, l Level, p *Plan) error { |
| 653 |
p.Level = l |
| 654 |
|
| 655 |
nblock := vtab[v].level[l].nblock |
| 656 |
ne := vtab[v].level[l].check |
| 657 |
nde := (vtab[v].bytes - ne*nblock) / nblock |
| 658 |
extra := (vtab[v].bytes - ne*nblock) % nblock |
| 659 |
dataBits := (nde*nblock + extra) * 8 |
| 660 |
checkBits := ne * nblock * 8 |
| 661 |
|
| 662 |
p.DataBytes = vtab[v].bytes - ne*nblock |
| 663 |
p.CheckBytes = ne * nblock |
| 664 |
p.Blocks = nblock |
| 665 |
|
| 666 |
|
| 667 |
data := make([]Pixel, dataBits) |
| 668 |
for i := range data { |
| 669 |
data[i] = Data.Pixel() | OffsetPixel(uint(i)) |
| 670 |
} |
| 671 |
check := make([]Pixel, checkBits) |
| 672 |
for i := range check { |
| 673 |
check[i] = Check.Pixel() | OffsetPixel(uint(i+dataBits)) |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
dataList := make([][]Pixel, nblock) |
| 678 |
checkList := make([][]Pixel, nblock) |
| 679 |
for i := 0; i < nblock; i++ { |
| 680 |
|
| 681 |
nd := nde |
| 682 |
if i >= nblock-extra { |
| 683 |
nd++ |
| 684 |
} |
| 685 |
dataList[i], data = data[0:nd*8], data[nd*8:] |
| 686 |
checkList[i], check = check[0:ne*8], check[ne*8:] |
| 687 |
} |
| 688 |
if len(data) != 0 || len(check) != 0 { |
| 689 |
panic("data/check math") |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
bits := make([]Pixel, dataBits+checkBits) |
| 695 |
dst := bits |
| 696 |
for i := 0; i < nde+1; i++ { |
| 697 |
for _, b := range dataList { |
| 698 |
if i*8 < len(b) { |
| 699 |
copy(dst, b[i*8:(i+1)*8]) |
| 700 |
dst = dst[8:] |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
for i := 0; i < ne; i++ { |
| 705 |
for _, b := range checkList { |
| 706 |
if i*8 < len(b) { |
| 707 |
copy(dst, b[i*8:(i+1)*8]) |
| 708 |
dst = dst[8:] |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
if len(dst) != 0 { |
| 713 |
panic("dst math") |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
siz := len(p.Pixel) |
| 721 |
rem := make([]Pixel, 7) |
| 722 |
for i := range rem { |
| 723 |
rem[i] = Extra.Pixel() |
| 724 |
} |
| 725 |
src := append(bits, rem...) |
| 726 |
for x := siz; x > 0; { |
| 727 |
for y := siz - 1; y >= 0; y-- { |
| 728 |
if p.Pixel[y][x-1].Role() == 0 { |
| 729 |
p.Pixel[y][x-1], src = src[0], src[1:] |
| 730 |
} |
| 731 |
if p.Pixel[y][x-2].Role() == 0 { |
| 732 |
p.Pixel[y][x-2], src = src[0], src[1:] |
| 733 |
} |
| 734 |
} |
| 735 |
x -= 2 |
| 736 |
if x == 7 { |
| 737 |
x-- |
| 738 |
} |
| 739 |
for y := 0; y < siz; y++ { |
| 740 |
if p.Pixel[y][x-1].Role() == 0 { |
| 741 |
p.Pixel[y][x-1], src = src[0], src[1:] |
| 742 |
} |
| 743 |
if p.Pixel[y][x-2].Role() == 0 { |
| 744 |
p.Pixel[y][x-2], src = src[0], src[1:] |
| 745 |
} |
| 746 |
} |
| 747 |
x -= 2 |
| 748 |
} |
| 749 |
return nil |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
func mplan(m Mask, p *Plan) error { |
| 754 |
p.Mask = m |
| 755 |
for y, row := range p.Pixel { |
| 756 |
for x, pix := range row { |
| 757 |
if r := pix.Role(); (r == Data || r == Check || r == Extra) && p.Mask.Invert(y, x) { |
| 758 |
row[x] ^= Black | Invert |
| 759 |
} |
| 760 |
} |
| 761 |
} |
| 762 |
return nil |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
func posBox(m [][]Pixel, x, y int) { |
| 767 |
pos := Position.Pixel() |
| 768 |
|
| 769 |
for dy := 0; dy < 7; dy++ { |
| 770 |
for dx := 0; dx < 7; dx++ { |
| 771 |
p := pos |
| 772 |
if dx == 0 || dx == 6 || dy == 0 || dy == 6 || 2 <= dx && dx <= 4 && 2 <= dy && dy <= 4 { |
| 773 |
p |= Black |
| 774 |
} |
| 775 |
m[y+dy][x+dx] = p |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
for dy := -1; dy < 8; dy++ { |
| 780 |
if 0 <= y+dy && y+dy < len(m) { |
| 781 |
if x > 0 { |
| 782 |
m[y+dy][x-1] = pos |
| 783 |
} |
| 784 |
if x+7 < len(m) { |
| 785 |
m[y+dy][x+7] = pos |
| 786 |
} |
| 787 |
} |
| 788 |
} |
| 789 |
for dx := -1; dx < 8; dx++ { |
| 790 |
if 0 <= x+dx && x+dx < len(m) { |
| 791 |
if y > 0 { |
| 792 |
m[y-1][x+dx] = pos |
| 793 |
} |
| 794 |
if y+7 < len(m) { |
| 795 |
m[y+7][x+dx] = pos |
| 796 |
} |
| 797 |
} |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
func alignBox(m [][]Pixel, x, y int) { |
| 803 |
|
| 804 |
align := Alignment.Pixel() |
| 805 |
for dy := 0; dy < 5; dy++ { |
| 806 |
for dx := 0; dx < 5; dx++ { |
| 807 |
p := align |
| 808 |
if dx == 0 || dx == 4 || dy == 0 || dy == 4 || dx == 2 && dy == 2 { |
| 809 |
p |= Black |
| 810 |
} |
| 811 |
m[y+dy][x+dx] = p |
| 812 |
} |
| 813 |
} |
| 814 |
} |
| 815 |
|