-
Notifications
You must be signed in to change notification settings - Fork 1
/
main-wasm.zig
42 lines (31 loc) · 1.38 KB
/
main-wasm.zig
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
const std = @import("std");
const qr_code = @import("src/index.zig");
const ErrorCorrectionLevel = @import("src/error-correction.zig").ErrorCorrectionLevel;
extern fn createQRCodeCallback(matrixPtr: [*]const u8, size: usize) void;
export fn allocUint8(length: usize) [*]u8 {
const slice = std.heap.page_allocator.alloc(u8, length) catch @panic("failed to allocate memory");
return slice.ptr;
}
export fn freeUint8(slice: [*]const u8, length: usize) void {
std.heap.page_allocator.free(slice[0..length]);
}
export fn createQRCode(messagePtr: [*:0]const u8, ecLevelInt: u8, qzoneSize: usize) void {
const allocator = std.heap.page_allocator;
const message = std.mem.span(messagePtr);
const ecLevel: ErrorCorrectionLevel = @enumFromInt(ecLevelInt);
const options = qr_code.CreateOptions{
.content = message,
.quietZoneSize = qzoneSize,
.ecLevel = ecLevel,
};
const matrix = qr_code.create(allocator, options) catch @panic("failed to create QR code");
defer matrix.deinit();
// var buffer = std.heap.page_allocator.alloc(u8, length) catch @panic("failed to allocate memory");
var buffer = allocUint8(matrix.size * matrix.size);
for (0..matrix.size) |r| {
for (0..matrix.size) |c| {
buffer[r * matrix.size + c] = @intCast(matrix.get(r, c));
}
}
createQRCodeCallback(buffer, matrix.size);
}