Viewing is open to everyone. Downloading and starring need a GitHub sign-in. Sign in with GitHub

main.cpp

v0.1.0-build.85 Latest Commit ede38f5

Size
1.8 KB
Lines
30
Language
cpp
SHA-256
0c4c0f7fd7ea…
View on GitHub

Sign in with GitHub to download. Each download is recorded with your GitHub username.

1// Encode a URL, render native pixels, write a PNG, and scan it back.
2#include <ckey/ckey.h>
3#include <cmath>
4#include <cstdio>
5#include <cstring>
6#include <vector>
7
8int main() {
9 const char *url = "https://openckey.org/tour";
10 ckey_scalable_symbol symbol{}; ckey_diagnostics diag{};
11 if (ckey_encode_scalable(reinterpret_cast<const uint8_t *>(url), std::strlen(url), 0, CKEY_ECC_M, CKEY_LAYOUT_STANDARD, &symbol, &diag) != CKEY_OK) { std::puts(diag.message); return 1; }
12 ckey_raster_options options; ckey_raster_options_default(&options);
13 options.size_px = 512; options.format = CKEY_PIXELS_GRAY8;
14 std::vector<uint8_t> pixels(512 * 512); size_t needed = 0;
15 if (ckey_render_scalable_raster(&symbol, nullptr, &options, pixels.data(), pixels.size(), &needed) != CKEY_OK) return 1;
16 std::vector<uint8_t> png(1 << 20);
17 if (ckey_encode_png(pixels.data(), 512, 512, 0, CKEY_PNG_GRAY8, 0, png.data(), png.size(), &needed) == CKEY_OK)
18 if (FILE *f = std::fopen("ckey.png", "wb")) { std::fwrite(png.data(), 1, needed, f); std::fclose(f); }
19 double borders[8 * 4]; double quad[8];
20 const size_t found = ckey_find_borders_gray8(pixels.data(), 512, 512, 512, borders, 4);
21 std::vector<uint8_t> out(65535); size_t len = 0; uint32_t version = 0;
22 for (size_t i = 0; i < found; ++i)
23 if (ckey_refine_quad_gray8(pixels.data(), 512, 512, 512, borders + 8 * i, CKEY_FIELD_SCALABLE, quad) > 0 &&
24 ckey_decode_scalable_gray8(pixels.data(), 512, 512, 512, quad, out.data(), out.size(), &len, &version, nullptr, &diag) == CKEY_OK) {
25 std::printf("v%u: %.*s (CKey %s)\n", version, static_cast<int>(len), reinterpret_cast<const char *>(out.data()), ckey_get_software_version());
26 return 0;
27 }
28 std::puts("no symbol found");
29 return 1;
30}