64namespace ca_png_detail {
68 out.append(
static_cast<std::uint8_t
>((
value >> 24) & 0xffu));
69 out.append(
static_cast<std::uint8_t
>((
value >> 16) & 0xffu));
70 out.append(
static_cast<std::uint8_t
>((
value >> 8) & 0xffu));
71 out.append(
static_cast<std::uint8_t
>(
value & 0xffu));
77 = {
static_cast<char>((
value >> 24) & 0xffu),
static_cast<char>((
value >> 16) & 0xffu),
78 static_cast<char>((
value >> 8) & 0xffu),
static_cast<char>(
value & 0xffu)};
82[[
nodiscard]]
inline std::uint32_t
crc32(
const std::uint8_t *data,
const std::size_t n)
84 std::uint32_t
crc = 0xffffffffu;
85 for (std::size_t i = 0; i < n; ++i)
88 for (
int k = 0;
k < 8; ++
k)
89 crc = (
crc & 1u) != 0 ? 0xedb88320u ^ (
crc >> 1) : (
crc >> 1);
91 return crc ^ 0xffffffffu;
96 constexpr std::uint32_t mod = 65521u;
99 for (std::size_t i = 0; i < data.
size(); ++i)
101 a = (a + data(i)) % mod;
104 return (b << 16) | a;
111 out.append(std::uint8_t{0x78});
112 out.append(std::uint8_t{0x01});
117 const std::size_t remaining = raw.
size() - pos;
118 const std::uint16_t block =
static_cast<std::uint16_t
>(remaining > 65535u ? 65535u : remaining);
120 out.append(
static_cast<std::uint8_t
>(
is_final ? 0x01 : 0x00));
121 out.append(
static_cast<std::uint8_t
>(block & 0xffu));
122 out.append(
static_cast<std::uint8_t
>((block >> 8) & 0xffu));
123 const std::uint16_t
nlen =
static_cast<std::uint16_t
>(
~block);
124 out.append(
static_cast<std::uint8_t
>(
nlen & 0xffu));
125 out.append(
static_cast<std::uint8_t
>((
nlen >> 8) & 0xffu));
126 for (std::size_t i = 0; i < block; ++i)
127 out.append(raw(pos + i));
129 }
while (pos < raw.
size());
140 out.write(
reinterpret_cast<const char *
>(&payload.
base()),
141 static_cast<std::streamsize
>(payload.
size()));
145 for (
int i = 0; i < 4; ++i)
146 crc_data.append(
static_cast<std::uint8_t
>(type[i]));
147 for (std::size_t i = 0; i < payload.
size(); ++i)
170template <
typename Lattice,
typename Mapper>
173 {
m(v) } -> std::convertible_to<RGB8>;
177 static_assert(
Lattice::rank == 2,
"write_png requires a rank-2 frame");
188 raw.
append(std::uint8_t{0});
192 =
mapper(frame.at(
coord_t{static_cast<ca_index_t>(r), static_cast<ca_index_t>(c)}));
199 static constexpr unsigned char signature[8] = {0x89,
'P',
'N',
'G',
'\r',
'\n', 0x1a,
'\n'};
200 out.write(
reinterpret_cast<const char *
>(
signature), 8);
205 ihdr.append(std::uint8_t{8});
206 ihdr.append(std::uint8_t{2});
207 ihdr.append(std::uint8_t{0});
208 ihdr.append(std::uint8_t{0});
209 ihdr.append(std::uint8_t{0});
225template <
typename Lattice>
239template <
typename Mapper>
261 template <
typename Lattice>
265 if (
const auto parent = path.parent_path();
not parent.empty())
266 std::filesystem::create_directories(parent);
267 std::ofstream
out(path, std::ios::binary);
276template <
typename Mapper>
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
size_t size_t int32_t value
size_t size_t int32_t * out
Directory-backed frame sinks for CA trajectories.
File-format readers and writers for cellular-automata frames.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
T & base()
Return a reference to the first element of array.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Lattice that adds boundary-aware access on top of a storage.
typename Storage::state_type state_type
typename Storage::coord_type coord_type
static constexpr std::size_t rank
Path-pattern sink that writes one PNG file per accepted frame.
Png_Frame_Sink(std::filesystem::path path_pattern, Mapper mapper, const std::size_t zero_pad=6)
Build a PNG frame sink.
void accept(const std::size_t step, const Lattice &frame)
Write one PNG frame.
static void flush()
PNG frames are written eagerly; this is a no-op.
std::filesystem::path pattern_
constexpr size_t size() const noexcept
Returns the number of entries in the table.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
void append_be32(Array< std::uint8_t > &out, const std::uint32_t value)
Array< std::uint8_t > zlib_store(const Array< std::uint8_t > &raw)
std::uint32_t crc32(const std::uint8_t *data, const std::size_t n)
void write_chunk(std::ostream &out, const char type[4], const Array< std::uint8_t > &payload)
std::uint32_t adler32(const Array< std::uint8_t > &data)
void write_be32(std::ostream &out, const std::uint32_t value)
std::filesystem::path format_step_path(const std::filesystem::path &pattern, const std::size_t step, const std::size_t zero_pad=6)
Format a step-indexed file path.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
void write_png(std::ostream &out, const Lattice &frame, Mapper &&mapper)
Write a rank-2 frame as an 8-bit RGB PNG image.
Main namespace for Aleph-w library functions.
Default binary RGB mapper.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.
C++20 concepts for the Cellular Automata module.