Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ports/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.o
prism
examples/encode_decode
hello.png
26 changes: 26 additions & 0 deletions ports/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Optional CMake build for the Comb Code C++ port (the Makefile is the primary
# build; this is an equivalent alternative).
cmake_minimum_required(VERSION 3.16)
project(CombCode VERSION 1.0.0 LANGUAGES C CXX)
# Comb Code, Copyright (c) 2026 Avi Shor. Free to use, with or without credit.

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

find_package(ZLIB REQUIRED)

add_library(combcode STATIC
prism.cpp prism_capacity_shim.cpp
prism_crypto.c prism_ecdsa.c
prism_secure.c prism_secure_wrap.cpp
prism_parts.c prism_parts_wrap.cpp
prism_cards.c prism_cards_wrap.cpp
prism_issuers.c prism_issuers_wrap.cpp)
target_link_libraries(combcode PUBLIC ZLIB::ZLIB m)
target_include_directories(combcode PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

add_executable(prism cli.cpp)
target_link_libraries(prism PRIVATE combcode)

add_executable(encode_decode_example examples/encode_decode.cpp)
target_link_libraries(encode_decode_example PRIVATE combcode)
4 changes: 4 additions & 0 deletions ports/cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ clean:
rm -f prism *.o

.PHONY: clean

example: examples/encode_decode.cpp prism.o
$(CXX) $(CXXFLAGS) -I. -o examples/encode_decode examples/encode_decode.cpp prism.o -lz
./examples/encode_decode
34 changes: 31 additions & 3 deletions ports/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,38 @@ comb-issuers.js, comb-parts.js and comb-cards.js. CLI verbs:
bit-for-bit; splits and joins interoperate with the C and C# ports and with
the live JS module.

## Not in this port (yet)
## Library use

The public C++ API is `prism.hpp` (encoder/decoder), `prism_secure.hpp` (Comb
Code Secure), `prism_parts.hpp` (multi-part files), `prism_cards.hpp`
(Wi-Fi / vCard / ticket payloads) and `prism_issuers.hpp` (ticket issuers).
Everything lives in namespace `prism` and throws `prism::Error` on failure;
all functions are documented at their declarations. Example
(`examples/encode_decode.cpp`, build and run with `make example`):

```cpp
#include "prism.hpp"

const std::string text = "Hello from Comb Code";
prism::encodePng(std::vector<uint8_t>(text.begin(), text.end()),
"hello.png", -1, 1, prism::MODE_COLOR, prism::TYPE_TEXT, 12);
prism::DecodeResult r = prism::decodePng("hello.png");
std::cout << std::string(r.bytes.begin(), r.bytes.end()) << "\n";
```

### API reference (encoder/decoder, `prism.hpp`)

- `prism::encode(payload, size, ecc, mode, type, scale)` -> `prism::Image` —
encode payload bytes to an in-memory RGBA image. `size` -1 = automatic or
0..5; `ecc` 0..3 (10/20/30/40% repair data); mode color or black-and-white.
- `prism::encodePng(payload, path, size, ecc, mode, type, scale)` — encode
and write a PNG file.
- `prism::decodePng(path)` -> `prism::DecodeResult` — decode a clean PNG; the
payload is only returned when its CRC-32 passes.
- `prism::capacityPayload(size, ecc, mode)` — maximum payload bytes.

The phone-camera reading pipeline (glare, blur, perspective — that stays on
the website's reader pages) is being ported next.
The phone-camera reader (photos: glare, blur, perspective) is in `reader/`,
with its own README and a `prism_reader.hpp` wrapper.

## License

Expand Down
29 changes: 29 additions & 0 deletions ports/cpp/examples/encode_decode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Comb Code C++ example: encode a text payload to a PNG, decode it back.
#include <iostream>
#include <string>
#include <vector>
#include "../prism.hpp"

int main() {
try {
const std::string text = "Hello from Comb Code";

// Encode: automatic size, 20% repair data, color mode, 12 px per cell unit.
prism::encodePng(std::vector<uint8_t>(text.begin(), text.end()),
"hello.png", -1, 1, prism::MODE_COLOR,
prism::TYPE_TEXT, 12);
std::cout << "encoded " << text.size() << " bytes -> hello.png\n";

// Decode it back.
prism::DecodeResult r = prism::decodePng("hello.png");
std::string out(r.bytes.begin(), r.bytes.end());
std::cout << "decoded " << r.bytes.size() << " bytes: " << out << "\n";

bool ok = out == text;
std::cout << "round-trip " << (ok ? "OK" : "MISMATCH") << "\n";
return ok ? 0 : 1;
} catch (const prism::Error &e) {
std::cerr << "error: " << e.what() << "\n";
return 1;
}
}
6 changes: 6 additions & 0 deletions ports/cpp/reader/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.o
*.a
obj/
bin/
cli
chain772*
28 changes: 28 additions & 0 deletions ports/cpp/reader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CXX ?= c++
CC ?= cc
CXXFLAGS ?= -O2 -ffp-contract=off -std=c++17 -Wall -Wextra
CFLAGS ?= -O2 -ffp-contract=off -std=gnu11 -Wall -Wextra

# Prism reader: the proven C11 core (same sources as the C port, verified
# bit-exact against the live JS reader), compiled as C, wrapped by prism_reader.hpp.
CORE = prism_foundations.o prism_jsmath.o prism_detect.o prism_image.o prism_classify.o \
prism_decode.o prism_mix.o prism_read.o prism_distfit.o prism_chain.o

all: libprism_reader.a cli

%.o: %.c
$(CC) $(CFLAGS) -c $<

prism_reader_wrap.o: prism_reader_wrap.cpp prism_reader.hpp ../prism.hpp prism_reader.h prism_chain.h
$(CXX) $(CXXFLAGS) -c prism_reader_wrap.cpp

libprism_reader.a: $(CORE) prism_reader_wrap.o
ar rcs $@ $^

cli: cli.cpp libprism_reader.a
$(CXX) $(CXXFLAGS) -o $@ cli.cpp libprism_reader.a

clean:
rm -f *.o *.a cli

.PHONY: all clean
38 changes: 38 additions & 0 deletions ports/cpp/reader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Comb Code camera reader — C++ (C++17) port

A C++17 port of the live Comb Code photo/camera reader: detection, hypothesis
search, palette-mix recovery, distance-fit recovery and the full retry chain.
It reads real photos, not only clean PNGs. The clean-PNG encoder/decoder is in
the parent folder.

Build: `make` (produces `cli` and `libprism_reader.a`; the C++ wrapper is
`prism_reader.hpp`). The Makefile passes `-ffp-contract=off` so floating point
stays bit-identical with the JavaScript reader.

Run: `./cli scene.raw`, where the file is 8 bytes little-endian width and
height followed by RGBA bytes. Prints `OK <text>`, `OKBIN <hex>` or `FAIL`.
A result is only returned when the header CRC and payload CRC-32 pass.

## Verified status

Method: 772 synthetic scenes from the bench kit, run sequentially, each result
compared to the live JavaScript reader's result for the same image
(`../../reader-verify/verify_vs_live.py`).

| Result | Scenes |
|---|---|
| Read, payload identical to live reader | 728 |
| Both fail | 44 |
| Live reader reads, port does not | 0 |
| Wrong payload | 0 |
| Port reads, live reader does not | 0 |

The live reader reads 728 of the 772 scenes; the port matches it on every
scene, with per-scene classification identical to the C port. Timing: the
reader stops a page read after 6 seconds, as the live one does. Run
verification one process at a time; under heavy parallel load a few borderline
scenes time out. This is a synthetic-scene result, not a physical-camera
result.

Comb Code, Copyright (c) 2026 Avi Shor. Free to use, with or without credit.
No warranty.
27 changes: 27 additions & 0 deletions ports/cpp/reader/cli.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* prism-reader-cli: read a Comb Code from a raw RGBA file (8-byte LE w,h + RGBA).
* usage: cli <raw> [--chain] Prints OK <text> / OKBIN <hex> / FAIL <reason>.
* Runs the full retry chain; --chain is accepted for explicitness. */
#include "prism_reader.hpp"
#include "../prism.hpp"
#include <cstdio>
#include <cstring>

int main(int argc, char** argv) {
if (argc < 2) { fprintf(stderr, "usage: cli <raw> [--chain]\n"); return 2; }
bool chain = true;
FILE* f = fopen(argv[1], "rb");
if (!f) { perror("open"); return 2; }
uint8_t hdr[8];
if (fread(hdr, 1, 8, f) != 8) return 2;
prism::Image img;
img.width = hdr[0] | hdr[1] << 8 | hdr[2] << 16 | (int)hdr[3] << 24;
img.height = hdr[4] | hdr[5] << 8 | hdr[6] << 16 | (int)hdr[7] << 24;
img.rgba.resize((size_t)img.width * img.height * 4);
if (fread(img.rgba.data(), 1, img.rgba.size(), f) != img.rgba.size()) return 2;
fclose(f);
prism::ReadResult r = chain ? prism::readChain(img) : prism::readFast(img);
if (!r.ok) { printf("FAIL %s\n", r.error.c_str()); return 0; }
if (!r.text.empty()) printf("OK %s\n", r.text.c_str());
else { printf("OKBIN "); for (uint8_t b : r.bytes) printf("%02x", b); printf("\n"); }
return 0;
}
Loading