| ... | @@ -22,9 +22,6 @@ const GF = blk: { | ... | @@ -22,9 +22,6 @@ const GF = blk: { |
| 22 | }; | 22 | }; |
| 23 | | 23 | |
| 24 | pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, input: []const u8) BitGrid(4 * version + 17) { | 24 | pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, input: []const u8) BitGrid(4 * version + 17) { |
| 25 | std.log.warn("mode: {s}", .{@tagName(mode)}); | | |
| 26 | std.log.warn("level: {s}", .{@tagName(level)}); | | |
| 27 | | | |
| 28 | if (mode == .numeric) std.debug.assert(extras.matchesAll(u8, input, isNumeric)); | 25 | if (mode == .numeric) std.debug.assert(extras.matchesAll(u8, input, isNumeric)); |
| 29 | if (mode == .alphanumeric) std.debug.assert(extras.matchesAll(u8, input, isAlphanumeric)); | 26 | if (mode == .alphanumeric) std.debug.assert(extras.matchesAll(u8, input, isAlphanumeric)); |
| 30 | // latin1 is a full 1-byte format, some readers can detect utf-8 | 27 | // latin1 is a full 1-byte format, some readers can detect utf-8 |
| ... | @@ -32,17 +29,9 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, | ... | @@ -32,17 +29,9 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, |
| 32 | | 29 | |
| 33 | const max_lengths = comptime maxLenPerModeLevelVersion(mode, level); | 30 | const max_lengths = comptime maxLenPerModeLevelVersion(mode, level); |
| 34 | const max_length = max_lengths[version]; | 31 | const max_length = max_lengths[version]; |
| 35 | std.log.warn("actual version: {d},{d},{d}", .{ version, max_length, input.len }); | | |
| 36 | std.debug.assert(input.len <= max_length); | 32 | std.debug.assert(input.len <= max_length); |
| 37 | for (max_lengths, 0..) |l, i| if (l > input.len) break std.log.warn("reccom version: {d},{d}", .{ i, l }); | | |
| 38 | | 33 | |
| 39 | const dataCodewordsCount, const ecCodewordsPerBlock, const groupOneBlockCount, const groupOneDataCodewordsCountPerBlock, const groupTwoBlockCount, const groupTwoDataCodewordsCountPerBlock = comptime errorCorrection(level, version); | 34 | const dataCodewordsCount, const ecCodewordsPerBlock, const groupOneBlockCount, const groupOneDataCodewordsCountPerBlock, const groupTwoBlockCount, const groupTwoDataCodewordsCountPerBlock = comptime errorCorrection(level, version); |
| 40 | std.log.warn("dataCodewordsCount: {d}", .{dataCodewordsCount}); | | |
| 41 | std.log.warn("ecCodewordsPerBlock: {d}", .{ecCodewordsPerBlock}); | | |
| 42 | std.log.warn("groupOneBlockCount: {d}", .{groupOneBlockCount}); | | |
| 43 | std.log.warn("groupOneDataCodewordsCountPerBlock: {d}", .{groupOneDataCodewordsCountPerBlock}); | | |
| 44 | std.log.warn("groupTwoBlockCount: {?d}", .{groupTwoBlockCount}); | | |
| 45 | std.log.warn("groupTwoDataCodewordsCountPerBlock: {?d}", .{groupTwoDataCodewordsCountPerBlock}); | | |
| 46 | | 35 | |
| 47 | const mode_indicator: u4 = switch (mode) { | 36 | const mode_indicator: u4 = switch (mode) { |
| 48 | .numeric => 0b0001, | 37 | .numeric => 0b0001, |
| ... | @@ -50,7 +39,6 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, | ... | @@ -50,7 +39,6 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, |
| 50 | .latin1 => 0b0100, | 39 | .latin1 => 0b0100, |
| 51 | .shiftjis => 0b1000, | 40 | .shiftjis => 0b1000, |
| 52 | }; | 41 | }; |
| 53 | std.log.warn("mode_indicator: {d}", .{mode_indicator}); | | |
| 54 | | 42 | |
| 55 | const LenIndicator = switch (mode) { | 43 | const LenIndicator = switch (mode) { |
| 56 | .numeric => switch (version) { | 44 | .numeric => switch (version) { |
| ... | @@ -79,10 +67,8 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, | ... | @@ -79,10 +67,8 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, |
| 79 | }, | 67 | }, |
| 80 | }; | 68 | }; |
| 81 | const len_indicator: LenIndicator = @intCast(input.len); | 69 | const len_indicator: LenIndicator = @intCast(input.len); |
| 82 | std.log.warn("len_indicator: {d}", .{len_indicator}); | | |
| 83 | | 70 | |
| 84 | const total_bits = dataCodewordsCount * 8; | 71 | const total_bits = dataCodewordsCount * 8; |
| 85 | std.log.warn("total_bits: {d}", .{total_bits}); | | |
| 86 | | 72 | |
| 87 | var scratch: std.bit_set.ArrayBitSet(u8, total_bits) = .initEmpty(); | 73 | var scratch: std.bit_set.ArrayBitSet(u8, total_bits) = .initEmpty(); |
| 88 | var scratchw: BitsetWriter(u8, total_bits) = .{ .set = &scratch }; | 74 | var scratchw: BitsetWriter(u8, total_bits) = .{ .set = &scratch }; |
| ... | @@ -92,7 +78,6 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, | ... | @@ -92,7 +78,6 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, |
| 92 | for (input) |x| scratchw.writeInt(x); | 78 | for (input) |x| scratchw.writeInt(x); |
| 93 | | 79 | |
| 94 | const data_bits = input.len * 8 + @bitSizeOf(@TypeOf(mode_indicator)) + @bitSizeOf(@TypeOf(len_indicator)); | 80 | const data_bits = input.len * 8 + @bitSizeOf(@TypeOf(mode_indicator)) + @bitSizeOf(@TypeOf(len_indicator)); |
| 95 | std.log.warn("data_bits: {d}", .{data_bits}); | | |
| 96 | std.debug.assert(scratchw.idx == data_bits); | 81 | std.debug.assert(scratchw.idx == data_bits); |
| 97 | | 82 | |
| 98 | // write @max(4, padding_bits) 0s | 83 | // write @max(4, padding_bits) 0s |
| ... | @@ -107,15 +92,8 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, | ... | @@ -107,15 +92,8 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, |
| 107 | scratchw.writeInt(@as(u8, 0x11)); | 92 | scratchw.writeInt(@as(u8, 0x11)); |
| 108 | } | 93 | } |
| 109 | | 94 | |
| 110 | std.log.warn("{b:0>8}", .{&bitReverse(scratch.masks)}); | | |
| 111 | std.log.warn("{d}", .{&bitReverse(scratch.masks)}); | | |
| 112 | std.log.warn("{x}", .{&bitReverse(scratch.masks)}); | | |
| 113 | | | |
| 114 | const generator_polynomial = reverseArray(generatorPolynomial(ecCodewordsPerBlock)); | 95 | const generator_polynomial = reverseArray(generatorPolynomial(ecCodewordsPerBlock)); |
| 115 | std.log.warn("generator_polynomial: {d}", .{generator_polynomial}); | | |
| 116 | | | |
| 117 | const generator_polynomial_alpha = to_alpha(generator_polynomial); | 96 | const generator_polynomial_alpha = to_alpha(generator_polynomial); |
| 118 | std.log.warn("generator_polynomial_alpha: {d}", .{generator_polynomial_alpha}); | | |
| 119 | | 97 | |
| 120 | // Now that the message and generator polynomials have been created, divide them to get the error codewords. | 98 | // Now that the message and generator polynomials have been created, divide them to get the error codewords. |
| 121 | { | 99 | { |
| ... | @@ -134,7 +112,6 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, | ... | @@ -134,7 +112,6 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, |
| 134 | | 112 | |
| 135 | for (&group1_ec, 0..) |*g, gn| { | 113 | for (&group1_ec, 0..) |*g, gn| { |
| 136 | const message_polynomial = scratch.masks[groupOneDataCodewordsCountPerBlock * gn ..][0..groupOneDataCodewordsCountPerBlock]; | 114 | const message_polynomial = scratch.masks[groupOneDataCodewordsCountPerBlock * gn ..][0..groupOneDataCodewordsCountPerBlock]; |
| 137 | std.log.warn("message_polynomial: 1,{d},{d},{d}", .{ gn, message_polynomial.len, &bitReverse(message_polynomial.*) }); | | |
| 138 | | 115 | |
| 139 | var mp: [groupOneDataCodewordsCountPerBlock + ecCodewordsPerBlock]?u8 = @splat(null); | 116 | var mp: [groupOneDataCodewordsCountPerBlock + ecCodewordsPerBlock]?u8 = @splat(null); |
| 140 | for (message_polynomial, 0..) |x, i| mp[i] = to_alpha(.{@bitReverse(x)})[0]; | 117 | for (message_polynomial, 0..) |x, i| mp[i] = to_alpha(.{@bitReverse(x)})[0]; |
| ... | @@ -153,7 +130,6 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, | ... | @@ -153,7 +130,6 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, |
| 153 | } | 130 | } |
| 154 | for (&group2_ec, 0..) |*g, gn| { | 131 | for (&group2_ec, 0..) |*g, gn| { |
| 155 | const message_polynomial = scratch.masks[groupOneBlockCount * groupOneDataCodewordsCountPerBlock ..][group2_datacodewords_per_block * gn ..][0..group2_datacodewords_per_block]; | 132 | const message_polynomial = scratch.masks[groupOneBlockCount * groupOneDataCodewordsCountPerBlock ..][group2_datacodewords_per_block * gn ..][0..group2_datacodewords_per_block]; |
| 156 | std.log.warn("message_polynomial: 2,{d},{d},{d}", .{ gn, message_polynomial.len, &bitReverse(message_polynomial.*) }); | | |
| 157 | | 133 | |
| 158 | var mp: [group2_datacodewords_per_block + ecCodewordsPerBlock]?u8 = @splat(null); | 134 | var mp: [group2_datacodewords_per_block + ecCodewordsPerBlock]?u8 = @splat(null); |
| 159 | for (message_polynomial, 0..) |x, i| mp[i] = to_alpha(.{@bitReverse(x)})[0]; | 135 | for (message_polynomial, 0..) |x, i| mp[i] = to_alpha(.{@bitReverse(x)})[0]; |
| ... | @@ -208,11 +184,11 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, | ... | @@ -208,11 +184,11 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, |
| 208 | imi += 1; | 184 | imi += 1; |
| 209 | } | 185 | } |
| 210 | } | 186 | } |
| 211 | std.log.warn("interleaved_message: {b:0>8}", .{&bitReverse(interleaved_message)}); | | |
| 212 | std.debug.assert(imi == interleaved_message.len); | 187 | std.debug.assert(imi == interleaved_message.len); |
| 213 | | 188 | |
| 214 | // remainder bits | 189 | // remainder bits |
| 215 | const remainder_bits_count = comptime remainderBitsCount(version); | 190 | const remainder_bits_count = comptime remainderBitsCount(version); |
| | 191 | _ = remainder_bits_count; |
| 216 | | 192 | |
| 217 | // | 193 | // |
| 218 | | 194 | |
| ... | @@ -295,21 +271,8 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, | ... | @@ -295,21 +271,8 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, |
| 295 | const x = idx % size; | 271 | const x = idx % size; |
| 296 | const y = (idx - x) / size; | 272 | const y = (idx - x) / size; |
| 297 | if (data_mask.isSet(x, y)) continue; | 273 | if (data_mask.isSet(x, y)) continue; |
| 298 | grid.debug.set((y * size) + x); | | |
| 299 | if (@bitCast(data_iter.next() orelse break)) grid.set(x, y); | 274 | if (@bitCast(data_iter.next() orelse break)) grid.set(x, y); |
| 300 | } | 275 | } |
| 301 | for (0..remainder_bits_count) |_| { | | |
| 302 | const idx = griditer.next().?; | | |
| 303 | const x = idx % size; | | |
| 304 | const y = (idx - x) / size; | | |
| 305 | grid.debug.set((y * size) + x); | | |
| 306 | } | | |
| 307 | | | |
| 308 | for (0..8) |y| for (0..8) |x| grid.debug.set((y * size) + x); | | |
| 309 | for (0..8) |y| for (0..8) |x| grid.debug.set((y * size) + size - 8 + x); | | |
| 310 | for (0..8) |y| for (0..8) |x| grid.debug.set(((size - 8 + y) * size) + x); | | |
| 311 | for (0..size) |i| grid.debug.set((6 * size) + i); | | |
| 312 | for (0..size) |i| grid.debug.set((i * size) + 6); | | |
| 313 | | 276 | |
| 314 | // format info | 277 | // format info |
| 315 | var masks = [_]std.bit_set.IntegerBitSet(15){ | 278 | var masks = [_]std.bit_set.IntegerBitSet(15){ |
| ... | @@ -466,30 +429,23 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, | ... | @@ -466,30 +429,23 @@ pub fn encode(comptime mode: Mode, comptime level: Level, comptime version: u16, |
| 466 | const div = [_]u32{ @divFloor(sub[0], 5), @divFloor(sub[1], 5) }; | 429 | const div = [_]u32{ @divFloor(sub[0], 5), @divFloor(sub[1], 5) }; |
| 467 | penaltys[i][3] += std.mem.min(u32, &div) * 10; | 430 | penaltys[i][3] += std.mem.min(u32, &div) * 10; |
| 468 | } | 431 | } |
| 469 | for (&penaltys) |x| std.log.warn("penaltys: {d}", .{&x}); | | |
| 470 | | 432 | |
| 471 | var penalty: [8]u32 = @splat(0); | 433 | var penalty: [8]u32 = @splat(0); |
| 472 | for (0..8) |i| penalty[i] = extras.sum(u32, &penaltys[i]); | 434 | for (0..8) |i| penalty[i] = extras.sum(u32, &penaltys[i]); |
| 473 | | 435 | |
| 474 | const min_penalty = std.mem.min(u32, &penalty); | 436 | const min_penalty = std.mem.min(u32, &penalty); |
| 475 | std.log.warn("min_penalty: {d}", .{min_penalty}); | | |
| 476 | const min_mask = std.mem.indexOfScalar(u32, &penalty, min_penalty).?; | 437 | const min_mask = std.mem.indexOfScalar(u32, &penalty, min_penalty).?; |
| 477 | std.log.warn("min_mask: {d}", .{min_mask}); | | |
| 478 | | | |
| 479 | std.log.warn("", .{}); | | |
| 480 | return grids[min_mask]; | 438 | return grids[min_mask]; |
| 481 | } | 439 | } |
| 482 | | 440 | |
| 483 | fn BitGrid(comptime size: usize) type { | 441 | fn BitGrid(comptime size: usize) type { |
| 484 | return struct { | 442 | return struct { |
| 485 | bset: std.bit_set.ArrayBitSet(usize, size * size), | 443 | bset: std.bit_set.ArrayBitSet(usize, size * size), |
| 486 | debug: std.bit_set.ArrayBitSet(usize, size * size), | | |
| 487 | comptime size: usize = size, | 444 | comptime size: usize = size, |
| 488 | | 445 | |
| 489 | pub fn initEmpty() @This() { | 446 | pub fn initEmpty() @This() { |
| 490 | return .{ | 447 | return .{ |
| 491 | .bset = .initEmpty(), | 448 | .bset = .initEmpty(), |
| 492 | .debug = .initEmpty(), | | |
| 493 | }; | 449 | }; |
| 494 | } | 450 | } |
| 495 | | 451 | |
| ... | @@ -499,30 +455,21 @@ fn BitGrid(comptime size: usize) type { | ... | @@ -499,30 +455,21 @@ fn BitGrid(comptime size: usize) type { |
| 499 | | 455 | |
| 500 | pub fn set(self: *@This(), x: usize, y: usize) void { | 456 | pub fn set(self: *@This(), x: usize, y: usize) void { |
| 501 | self.bset.set((y * size) + x); | 457 | self.bset.set((y * size) + x); |
| 502 | self.debug.set((y * size) + x); | | |
| 503 | } | 458 | } |
| 504 | | 459 | |
| 505 | pub fn setF(self: *@This(), x: usize, y: usize) void { | 460 | pub fn setF(self: *@This(), x: usize, y: usize) void { |
| 506 | self.bset.setValue((y * size) + x, false); | 461 | self.bset.setValue((y * size) + x, false); |
| 507 | self.debug.setValue((y * size) + x, true); | | |
| 508 | } | 462 | } |
| 509 | | 463 | |
| 510 | pub fn setT(self: *@This(), x: usize, y: usize) void { | 464 | pub fn setT(self: *@This(), x: usize, y: usize) void { |
| 511 | self.bset.setValue((y * size) + x, true); | 465 | self.bset.setValue((y * size) + x, true); |
| 512 | self.debug.setValue((y * size) + x, true); | | |
| 513 | } | 466 | } |
| 514 | | 467 | |
| 515 | pub fn setV(self: *@This(), x: usize, y: usize, value: bool) void { | 468 | pub fn setV(self: *@This(), x: usize, y: usize, value: bool) void { |
| 516 | self.bset.setValue((y * size) + x, value); | 469 | self.bset.setValue((y * size) + x, value); |
| 517 | self.debug.setValue((y * size) + x, true); | | |
| 518 | } | | |
| 519 | | | |
| 520 | pub fn hasSet(self: *const @This(), x: usize, y: usize) bool { | | |
| 521 | return self.debug.isSet((y * size) + x); | | |
| 522 | } | 470 | } |
| 523 | | 471 | |
| 524 | pub fn flip(self: *@This(), x: usize, y: usize) void { | 472 | pub fn flip(self: *@This(), x: usize, y: usize) void { |
| 525 | std.debug.assert(self.debug.isSet((y * size) + x)); | | |
| 526 | self.bset.toggle((y * size) + x); | 473 | self.bset.toggle((y * size) + x); |
| 527 | } | 474 | } |
| 528 | | 475 | |