QR Code generation #8
@@ -32,9 +32,14 @@
|
||||
"command": "odin test phased_executor -out=out/debug/test_phased_executor",
|
||||
"cwd": "$ZED_WORKTREE_ROOT",
|
||||
},
|
||||
{
|
||||
"label": "Test qrcode",
|
||||
"command": "odin test qrcode -out=out/debug/test_qrcode",
|
||||
"cwd": "$ZED_WORKTREE_ROOT",
|
||||
},
|
||||
{
|
||||
"label": "Test all",
|
||||
"command": "odin test many_bits -out=out/debug/test_many_bits && odin test ring -out=out/debug/test_ring && odin test levsort -out=out/debug/test_levsort && odin test levsync -out=out/debug/test_levsync && odin test levmath -out=out/debug/test_levmath && odin test phased_executor -out=out/debug/test_phased_executor",
|
||||
"command": "odin test many_bits -out=out/debug/test_many_bits && odin test ring -out=out/debug/test_ring && odin test levsort -out=out/debug/test_levsort && odin test levsync -out=out/debug/test_levsync && odin test levmath -out=out/debug/test_levmath && odin test phased_executor -out=out/debug/test_phased_executor && odin test qrcode -out=out/debug/test_qrcode",
|
||||
"cwd": "$ZED_WORKTREE_ROOT",
|
||||
},
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
@@ -65,6 +70,26 @@
|
||||
"command": "odin run draw/examples -debug -out=out/debug/draw-examples -- hellope-custom",
|
||||
"cwd": "$ZED_WORKTREE_ROOT",
|
||||
},
|
||||
{
|
||||
"label": "Run qrcode basic example",
|
||||
"command": "odin run qrcode/examples -debug -out=out/debug/qrcode-examples -- basic",
|
||||
"cwd": "$ZED_WORKTREE_ROOT",
|
||||
},
|
||||
{
|
||||
"label": "Run qrcode variety example",
|
||||
"command": "odin run qrcode/examples -debug -out=out/debug/qrcode-examples -- variety",
|
||||
"cwd": "$ZED_WORKTREE_ROOT",
|
||||
},
|
||||
{
|
||||
"label": "Run qrcode segment example",
|
||||
"command": "odin run qrcode/examples -debug -out=out/debug/qrcode-examples -- segment",
|
||||
"cwd": "$ZED_WORKTREE_ROOT",
|
||||
},
|
||||
{
|
||||
"label": "Run qrcode mask example",
|
||||
"command": "odin run qrcode/examples -debug -out=out/debug/qrcode-examples -- mask",
|
||||
"cwd": "$ZED_WORKTREE_ROOT",
|
||||
},
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Other ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
305
qrcode/examples/examples.odin
Normal file
305
qrcode/examples/examples.odin
Normal file
@@ -0,0 +1,305 @@
|
||||
package examples
|
||||
|
||||
import "core:fmt"
|
||||
import "core:mem"
|
||||
import "core:os"
|
||||
|
||||
import qr ".."
|
||||
|
||||
main :: proc() {
|
||||
//----- Tracking allocator ----------------------------------
|
||||
{
|
||||
tracking_temp_allocator := false
|
||||
// Temp
|
||||
track_temp: mem.Tracking_Allocator
|
||||
if tracking_temp_allocator {
|
||||
mem.tracking_allocator_init(&track_temp, context.temp_allocator)
|
||||
context.temp_allocator = mem.tracking_allocator(&track_temp)
|
||||
}
|
||||
// Default
|
||||
track: mem.Tracking_Allocator
|
||||
mem.tracking_allocator_init(&track, context.allocator)
|
||||
context.allocator = mem.tracking_allocator(&track)
|
||||
defer {
|
||||
// Temp allocator
|
||||
if tracking_temp_allocator {
|
||||
if len(track_temp.allocation_map) > 0 {
|
||||
fmt.eprintf("=== %v allocations not freed - temp allocator: ===\n", len(track_temp.allocation_map))
|
||||
for _, entry in track_temp.allocation_map {
|
||||
fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
|
||||
}
|
||||
}
|
||||
if len(track_temp.bad_free_array) > 0 {
|
||||
fmt.eprintf("=== %v incorrect frees - temp allocator: ===\n", len(track_temp.bad_free_array))
|
||||
for entry in track_temp.bad_free_array {
|
||||
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
|
||||
}
|
||||
}
|
||||
mem.tracking_allocator_destroy(&track_temp)
|
||||
}
|
||||
// Default allocator
|
||||
if len(track.allocation_map) > 0 {
|
||||
fmt.eprintf("=== %v allocations not freed - main allocator: ===\n", len(track.allocation_map))
|
||||
for _, entry in track.allocation_map {
|
||||
fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
|
||||
}
|
||||
}
|
||||
if len(track.bad_free_array) > 0 {
|
||||
fmt.eprintf("=== %v incorrect frees - main allocator: ===\n", len(track.bad_free_array))
|
||||
for entry in track.bad_free_array {
|
||||
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
|
||||
}
|
||||
}
|
||||
mem.tracking_allocator_destroy(&track)
|
||||
}
|
||||
}
|
||||
|
||||
args := os.args
|
||||
if len(args) < 2 {
|
||||
fmt.eprintln("Usage: examples <example_name>")
|
||||
fmt.eprintln("Available examples: basic, variety, segment, mask")
|
||||
os.exit(1)
|
||||
}
|
||||
|
||||
switch args[1] {
|
||||
case "basic": basic()
|
||||
case "variety": variety()
|
||||
case "segment": segment()
|
||||
case "mask": mask()
|
||||
case:
|
||||
fmt.eprintf("Unknown example: %v\n", args[1])
|
||||
fmt.eprintln("Available examples: basic, variety, segment, mask")
|
||||
os.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
// Utilities
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
|
||||
// Prints the given QR Code to the console.
|
||||
print_qr :: proc(qrcode: []u8) {
|
||||
size := qr.get_size(qrcode)
|
||||
border :: 4
|
||||
for y in -border ..< size + border {
|
||||
for x in -border ..< size + border {
|
||||
fmt.print("##" if qr.get_module(qrcode, x, y) else " ")
|
||||
}
|
||||
fmt.println()
|
||||
}
|
||||
fmt.println()
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
// Demo: Basic
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
|
||||
// Creates a single QR Code, then prints it to the console.
|
||||
basic :: proc() {
|
||||
text :: "Hello, world!"
|
||||
ecl :: qr.Ecc.Low
|
||||
|
||||
qrcode: [qr.BUFFER_LEN_MAX]u8
|
||||
ok := qr.encode(text, qrcode[:], ecl)
|
||||
if ok do print_qr(qrcode[:])
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
// Demo: Variety
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
|
||||
// Creates a variety of QR Codes that exercise different features of the library.
|
||||
variety :: proc() {
|
||||
qrcode: [qr.BUFFER_LEN_MAX]u8
|
||||
|
||||
{ // Numeric mode encoding (3.33 bits per digit)
|
||||
ok := qr.encode("314159265358979323846264338327950288419716939937510", qrcode[:], qr.Ecc.Medium)
|
||||
if ok do print_qr(qrcode[:])
|
||||
}
|
||||
|
||||
{ // Alphanumeric mode encoding (5.5 bits per character)
|
||||
ok := qr.encode("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", qrcode[:], qr.Ecc.High)
|
||||
if ok do print_qr(qrcode[:])
|
||||
}
|
||||
|
||||
{ // Unicode text as UTF-8
|
||||
ok := qr.encode(
|
||||
"\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1wa\xE3\x80\x81" +
|
||||
"\xE4\xB8\x96\xE7\x95\x8C\xEF\xBC\x81\x20\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4",
|
||||
qrcode[:],
|
||||
qr.Ecc.Quartile,
|
||||
)
|
||||
if ok do print_qr(qrcode[:])
|
||||
}
|
||||
|
||||
{ // Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)
|
||||
ok := qr.encode(
|
||||
"Alice was beginning to get very tired of sitting by her sister on the bank, " +
|
||||
"and of having nothing to do: once or twice she had peeped into the book her sister was reading, " +
|
||||
"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice " +
|
||||
"'without pictures or conversations?' So she was considering in her own mind (as well as she could, " +
|
||||
"for the hot day made her feel very sleepy and stupid), whether the pleasure of making a " +
|
||||
"daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly " +
|
||||
"a White Rabbit with pink eyes ran close by her.",
|
||||
qrcode[:],
|
||||
qr.Ecc.High,
|
||||
)
|
||||
if ok do print_qr(qrcode[:])
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
// Demo: Segment
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
|
||||
// Creates QR Codes with manually specified segments for better compactness.
|
||||
segment :: proc() {
|
||||
qrcode: [qr.BUFFER_LEN_MAX]u8
|
||||
|
||||
{ // Illustration "silver"
|
||||
silver0 :: "THE SQUARE ROOT OF 2 IS 1."
|
||||
silver1 :: "41421356237309504880168872420969807856967187537694807317667973799"
|
||||
|
||||
// Encode as single text (auto mode selection)
|
||||
{
|
||||
concat :: silver0 + silver1
|
||||
ok := qr.encode(concat, qrcode[:], qr.Ecc.Low)
|
||||
if ok do print_qr(qrcode[:])
|
||||
}
|
||||
|
||||
// Encode as two manual segments (alphanumeric + numeric) for better compactness
|
||||
{
|
||||
seg_buf0: [qr.BUFFER_LEN_MAX]u8
|
||||
seg_buf1: [qr.BUFFER_LEN_MAX]u8
|
||||
segs := [2]qr.Segment{qr.make_alphanumeric(silver0, seg_buf0[:]), qr.make_numeric(silver1, seg_buf1[:])}
|
||||
ok := qr.encode(segs[:], qr.Ecc.Low, qrcode[:])
|
||||
if ok do print_qr(qrcode[:])
|
||||
}
|
||||
}
|
||||
|
||||
{ // Illustration "golden"
|
||||
golden0 :: "Golden ratio \xCF\x86 = 1."
|
||||
golden1 :: "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374"
|
||||
golden2 :: "......"
|
||||
|
||||
// Encode as single text (auto mode selection)
|
||||
{
|
||||
concat :: golden0 + golden1 + golden2
|
||||
ok := qr.encode(concat, qrcode[:], qr.Ecc.Low)
|
||||
if ok do print_qr(qrcode[:])
|
||||
}
|
||||
|
||||
// Encode as three manual segments (byte + numeric + alphanumeric) for better compactness
|
||||
{
|
||||
golden0_str: string = golden0
|
||||
golden0_bytes := transmute([]u8)golden0_str
|
||||
seg_buf0: [qr.BUFFER_LEN_MAX]u8
|
||||
seg_buf1: [qr.BUFFER_LEN_MAX]u8
|
||||
seg_buf2: [qr.BUFFER_LEN_MAX]u8
|
||||
segs := [3]qr.Segment {
|
||||
qr.make_bytes(golden0_bytes, seg_buf0[:]),
|
||||
qr.make_numeric(golden1, seg_buf1[:]),
|
||||
qr.make_alphanumeric(golden2, seg_buf2[:]),
|
||||
}
|
||||
ok := qr.encode(segs[:], qr.Ecc.Low, qrcode[:])
|
||||
if ok do print_qr(qrcode[:])
|
||||
}
|
||||
}
|
||||
|
||||
{ // Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters
|
||||
// Encode as text (auto mode — byte mode)
|
||||
{
|
||||
madoka ::
|
||||
"\xE3\x80\x8C\xE9\xAD\x94\xE6\xB3\x95\xE5" +
|
||||
"\xB0\x91\xE5\xA5\xB3\xE3\x81\xBE\xE3\x81" +
|
||||
"\xA9\xE3\x81\x8B\xE2\x98\x86\xE3\x83\x9E" +
|
||||
"\xE3\x82\xAE\xE3\x82\xAB\xE3\x80\x8D\xE3" +
|
||||
"\x81\xA3\xE3\x81\xA6\xE3\x80\x81\xE3\x80" +
|
||||
"\x80\xD0\x98\xD0\x90\xD0\x98\xE3\x80\x80" +
|
||||
"\xEF\xBD\x84\xEF\xBD\x85\xEF\xBD\x93\xEF" +
|
||||
"\xBD\x95\xE3\x80\x80\xCE\xBA\xCE\xB1\xEF" +
|
||||
"\xBC\x9F"
|
||||
ok := qr.encode(madoka, qrcode[:], qr.Ecc.Low)
|
||||
if ok do print_qr(qrcode[:])
|
||||
}
|
||||
|
||||
// Encode with manual kanji mode (13 bits per character)
|
||||
{
|
||||
//odinfmt: disable
|
||||
kanji_chars :: [29]int{
|
||||
0x0035, 0x1002, 0x0FC0, 0x0AED, 0x0AD7,
|
||||
0x015C, 0x0147, 0x0129, 0x0059, 0x01BD,
|
||||
0x018D, 0x018A, 0x0036, 0x0141, 0x0144,
|
||||
0x0001, 0x0000, 0x0249, 0x0240, 0x0249,
|
||||
0x0000, 0x0104, 0x0105, 0x0113, 0x0115,
|
||||
0x0000, 0x0208, 0x01FF, 0x0008,
|
||||
}
|
||||
//odinfmt: enable
|
||||
|
||||
seg_buf: [qr.BUFFER_LEN_MAX]u8
|
||||
for &b in seg_buf {
|
||||
b = 0
|
||||
}
|
||||
|
||||
seg: qr.Segment
|
||||
seg.mode = .Kanji
|
||||
seg.num_chars = len(kanji_chars)
|
||||
seg.bit_length = 0
|
||||
for ch in kanji_chars {
|
||||
for j := 12; j >= 0; j -= 1 {
|
||||
seg_buf[seg.bit_length >> 3] |= u8(((ch >> uint(j)) & 1)) << uint(7 - (seg.bit_length & 7))
|
||||
seg.bit_length += 1
|
||||
}
|
||||
}
|
||||
seg.data = seg_buf[:(seg.bit_length + 7) / 8]
|
||||
|
||||
segs := [1]qr.Segment{seg}
|
||||
ok := qr.encode(segs[:], qr.Ecc.Low, qrcode[:])
|
||||
if ok do print_qr(qrcode[:])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
// Demo: Mask
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
|
||||
// Creates QR Codes with the same size and contents but different mask patterns.
|
||||
mask :: proc() {
|
||||
qrcode: [qr.BUFFER_LEN_MAX]u8
|
||||
|
||||
{ // Project Nayuki URL
|
||||
ok: bool
|
||||
|
||||
ok = qr.encode("https://www.nayuki.io/", qrcode[:], qr.Ecc.High)
|
||||
if ok do print_qr(qrcode[:])
|
||||
|
||||
ok = qr.encode("https://www.nayuki.io/", qrcode[:], qr.Ecc.High, mask = qr.Mask.M3)
|
||||
if ok do print_qr(qrcode[:])
|
||||
}
|
||||
|
||||
{ // Chinese text as UTF-8
|
||||
text ::
|
||||
"\xE7\xB6\xAD\xE5\x9F\xBA\xE7\x99\xBE\xE7\xA7\x91\xEF\xBC\x88\x57\x69\x6B\x69\x70" +
|
||||
"\x65\x64\x69\x61\xEF\xBC\x8C\xE8\x81\x86\xE8\x81\xBD\x69\x2F\xCB\x8C\x77\xC9\xAA" +
|
||||
"\x6B\xE1\xB5\xBB\xCB\x88\x70\x69\xCB\x90\x64\x69\x2E\xC9\x99\x2F\xEF\xBC\x89\xE6" +
|
||||
"\x98\xAF\xE4\xB8\x80\xE5\x80\x8B\xE8\x87\xAA\xE7\x94\xB1\xE5\x85\xA7\xE5\xAE\xB9" +
|
||||
"\xE3\x80\x81\xE5\x85\xAC\xE9\x96\x8B\xE7\xB7\xA8\xE8\xBC\xAF\xE4\xB8\x94\xE5\xA4" +
|
||||
"\x9A\xE8\xAA\x9E\xE8\xA8\x80\xE7\x9A\x84\xE7\xB6\xB2\xE8\xB7\xAF\xE7\x99\xBE\xE7" +
|
||||
"\xA7\x91\xE5\x85\xA8\xE6\x9B\xB8\xE5\x8D\x94\xE4\xBD\x9C\xE8\xA8\x88\xE7\x95\xAB"
|
||||
|
||||
ok: bool
|
||||
|
||||
ok = qr.encode(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M0)
|
||||
if ok do print_qr(qrcode[:])
|
||||
|
||||
ok = qr.encode(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M1)
|
||||
if ok do print_qr(qrcode[:])
|
||||
|
||||
ok = qr.encode(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M5)
|
||||
if ok do print_qr(qrcode[:])
|
||||
|
||||
ok = qr.encode(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M7)
|
||||
if ok do print_qr(qrcode[:])
|
||||
}
|
||||
}
|
||||
2832
qrcode/generate.odin
Normal file
2832
qrcode/generate.odin
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user