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

scan_only.cpp

v0.1.0-build.85 Latest Commit ede38f5

Size
1.2 KB
Lines
23
Language
cpp
SHA-256
79b557cc43a6…
View on GitHub

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

1// Links CKey::scan only: no renderer, no telemetry. Reads a binary PGM (P5).
2#include <ckey/ckey.h>
3#include <cstdio>
4#include <vector>
5
6int main(int argc, char **argv) {
7 if (argc != 2) { std::fprintf(stderr, "usage: scan_only image.pgm\n"); return 2; }
8 FILE *f = std::fopen(argv[1], "rb"); if (!f) return 2;
9 int w = 0, h = 0, max = 0;
10 if (std::fscanf(f, "P5 %d %d %d", &w, &h, &max) != 3 || w <= 0 || h <= 0 || max != 255) return 2;
11 std::fgetc(f);
12 std::vector<uint8_t> gray(static_cast<size_t>(w) * h);
13 if (std::fread(gray.data(), 1, gray.size(), f) != gray.size()) return 2;
14 std::fclose(f);
15 double borders[8 * 6], quad[8]; std::vector<uint8_t> out(65535); size_t len = 0; uint32_t version = 0; ckey_diagnostics d{};
16 const size_t n = ckey_find_borders_gray8(gray.data(), w, h, w, borders, 6);
17 for (size_t i = 0; i < n; ++i)
18 if (ckey_refine_quad_gray8(gray.data(), w, h, w, borders + 8 * i, CKEY_FIELD_SCALABLE, quad) > 0 &&
19 ckey_decode_scalable_gray8(gray.data(), w, h, w, quad, out.data(), out.size(), &len, &version, nullptr, &d) == CKEY_OK) {
20 std::fwrite(out.data(), 1, len, stdout); std::puts(""); return 0;
21 }
22 return 1;
23}