okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-5b adfcf9028 into f-own-int (round 16: the strata-image test program and its parts, DiT runner, sampler, noise, PNG writer; hooked into no build)e4309aaf3

main
3.6 KiB67 linesraw
1// src/image/png/png_write_main.cpp - test program strata-png-write (not shipped) for include/strata/image/png_write.hpp.
2//
3//   strata-png-write --vectors                              CRC-32 / Adler-32 against published check values
4//   strata-png-write --pattern W H C OUT.png [PARAMETERS]   a fixed test pattern (byte = (7x + 13y + 29c) mod 256)
5//   strata-png-write --raw IN W H C OUT.png [PARAMETERS]    IN: W*H*C bytes, row-major
6#include "strata/image/png_write.hpp"
7
8#include <cstdio>
9#include <cstdlib>
10#include <cstring>
11#include <fstream>
12#include <string>
13#include <vector>
14
15using namespace strata::image;
16
17int main(int argc, char** argv) {
18    if (argc >= 2 && std::strcmp(argv[1], "--vectors") == 0) {
19        struct V { const char* s; uint32_t crc, adler; };
20        // CRC-32/ISO-HDLC check value (the CRC catalogue), Adler-32 of "Wikipedia" (RFC 1950's algorithm), empty input
21        const V vs[] = {{"123456789", 0xCBF43926u, 0x091E01DEu}, {"Wikipedia", 0xADAAC02Eu, 0x11E60398u}, {"", 0u, 1u},
22                        {"The quick brown fox jumps over the lazy dog", 0x414FA339u, 0x5BDC0FDAu}};
23        int bad = 0;
24        for (const V& v : vs) {
25            const uint32_t c = png_crc32((const uint8_t*) v.s, std::strlen(v.s));
26            const uint32_t a = png_adler32((const uint8_t*) v.s, std::strlen(v.s));
27            std::printf("\"%s\": crc32 %08x (%s), adler32 %08x (%s)\n", v.s, c, c == v.crc ? "ok" : "WRONG", a,
28                        a == v.adler ? "ok" : "WRONG");
29            bad += c != v.crc || a != v.adler;
30        }
31        // continuing over two parts gives the whole
32        const char* s = "123456789";
33        const uint32_t c2 = png_crc32((const uint8_t*) s + 4, 5, png_crc32((const uint8_t*) s, 4));
34        const uint32_t a2 = png_adler32((const uint8_t*) s + 4, 5, png_adler32((const uint8_t*) s, 4));
35        std::printf("split: crc32 %s, adler32 %s\n", c2 == 0xCBF43926u ? "ok" : "WRONG", a2 == 0x091E01DEu ? "ok" : "WRONG");
36        bad += c2 != 0xCBF43926u || a2 != 0x091E01DEu;
37        // Adler-32 over 1 MB of 0xFF (the sums' modulo path)
38        std::vector<uint8_t> ff(1 << 20, 0xFF);
39        std::printf("adler32 of 1 MiB of 0xff: %08x\n", png_adler32(ff.data(), ff.size()));
40        std::printf("VECTORS %s\n", bad ? "FAIL" : "PASS");
41        return bad ? 1 : 0;
42    }
43    if (argc >= 6 && std::strcmp(argv[1], "--pattern") == 0) {
44        const int w = std::atoi(argv[2]), h = std::atoi(argv[3]), c = std::atoi(argv[4]);
45        std::vector<uint8_t> px((size_t) w * h * c);
46        for (int y = 0; y < h; ++y)
47            for (int x = 0; x < w; ++x)
48                for (int k = 0; k < c; ++k) px[((size_t) y * w + x) * c + k] = (uint8_t) ((7 * x + 13 * y + 29 * k) & 255);
49        const std::string e = png_write_file(argv[5], px.data(), w, h, c, argc > 6 ? argv[6] : "");
50        if (!e.empty()) std::fprintf(stderr, "%s\n", e.c_str());
51        return e.empty() ? 0 : 1;
52    }
53    if (argc >= 7 && std::strcmp(argv[1], "--raw") == 0) {
54        const int w = std::atoi(argv[3]), h = std::atoi(argv[4]), c = std::atoi(argv[5]);
55        std::ifstream f(argv[2], std::ios::binary);
56        std::vector<uint8_t> px((std::istreambuf_iterator<char>(f)), {});
57        if (px.size() != (size_t) w * h * c) {
58            std::fprintf(stderr, "raw size %zu != %d x %d x %d\n", px.size(), w, h, c);
59            return 1;
60        }
61        const std::string e = png_write_file(argv[6], px.data(), w, h, c, argc > 7 ? argv[7] : "");
62        if (!e.empty()) std::fprintf(stderr, "%s\n", e.c_str());
63        return e.empty() ? 0 : 1;
64    }
65    std::fprintf(stderr, "usage: strata-png-write --vectors | --pattern W H C OUT [PARAMS] | --raw IN W H C OUT [PARAMS]\n");
66    return 2;
67}