67 const int32_t p = (int32_t)a + (int32_t)b - (int32_t)c;
68 const int32_t pa = (p > (int32_t)a) ? (p - (int32_t)a) : ((int32_t)a - p);
69 const int32_t pb = (p > (int32_t)b) ? (p - (int32_t)b) : ((int32_t)b - p);
70 const int32_t pc = (p > (int32_t)c) ? (p - (int32_t)c) : ((int32_t)c - p);
71 if ((pa <= pb) && (pa <= pc)) {
111 for (uint32_t i = (uint32_t)bpp; i < nbytes; i++) {
112 row[i] = (uint8_t)(row[i] + row[i - bpp]);
116 for (uint32_t i = 0U; i < nbytes; i++) {
117 row[i] = (uint8_t)(row[i] + prev[i]);
121 for (uint32_t i = 0U; i < nbytes; i++) {
122 const uint32_t left = (i >= (uint32_t)bpp) ? (uint32_t)row[i - bpp] : 0U;
123 row[i] = (uint8_t)(row[i] + (uint8_t)((left + (uint32_t)prev[i]) / 2U));
127 for (uint32_t i = 0U; i < nbytes; i++) {
128 const uint8_t a = (i >= (uint32_t)bpp) ? row[i - bpp] : 0U;
129 const uint8_t c = (i >= (uint32_t)bpp) ? prev[i - bpp] : 0U;
159 const uint32_t w = (uint32_t)st->
w;
161 for (uint32_t x = 0U; x < w; x++) {
162 const uint8_t idx = row[x];
167 const uint32_t dst = x * (uint32_t)st->
dst_ch;
179 for (uint32_t x = 0U; x < w; x++) {
184 st->
xlat[dst + 1U] = g;
185 st->
xlat[dst + 2U] = g;
186 st->
xlat[dst + 3U] = a;
220 while (pos < avail) {
222 const uint32_t left = avail - pos;
223 const uint32_t take = (left < need) ? left : need;
233 const uint32_t nbytes = st->
rowlen - 1U;
306 if ((st->
inflator ==
nullptr) || (st->
ring ==
nullptr) || (st->
inbuf ==
nullptr) ||
422 const mz_uint flags = (mz_uint)TINFL_FLAG_PARSE_ZLIB_HEADER |
423 ((st->
source_done == 0U) ? (mz_uint)TINFL_FLAG_HAS_MORE_INPUT : 0U);
424 const tinfl_status status = tinfl_decompress(st->
inflator,
431 if (status < TINFL_STATUS_DONE) {
435 if ((in_sz == 0U) && (out_sz == 0U)) {
451 it->
in_pos += (uint32_t)in_sz;
452 it->
done = (status == TINFL_STATUS_DONE) ? 1U : 0U;
481 while (it.
done == 0U) {
588 static const char*
const s_tag =
"jof_png";
595 (void)
memset(st, 0,
sizeof(*st));
Module-private seams shared by the producer translation units.
void * priv_jof_bump_take(jof_bump_t *bump, size_t len)
Carve len 8-byte-aligned bytes from the bump arena.
ra8_err_t(* jof_geom_fn)(void *ctx, uint16_t width, uint16_t height, uint8_t channels)
Producer geometry hook: fires once when the source dimensions and output channel count are known,...
ra8_err_t(* jof_rows_fn)(void *ctx, const uint8_t *px, uint16_t width, uint16_t y0, uint16_t nrows, uint8_t channels)
Producer row sink: receives decoded pixel rows strictly in order.
static uint8_t internal_png_paeth(uint8_t a, uint8_t b, uint8_t c)
Paeth predictor (PNG sec 9.4 "Filter type 4: Paeth").
static ra8_err_t internal_png_run_idat(ra8_png_state_t *st, jof_bump_t *bump, uint32_t len)
The IDAT arm: bind geometry, inflate the stream, finish the walk.
static ra8_err_t internal_png_unfilter(uint8_t filter, uint8_t *row, const uint8_t *prev, uint32_t nbytes, uint8_t bpp)
Reconstruct one filtered scanline in place (PNG sec 9 filters).
static ra8_err_t internal_png_walk_chunks(ra8_png_state_t *st, jof_bump_t *bump, uint16_t max_w, uint16_t max_h)
Walk the chunk stream: pre-IDAT chunks, then the IDAT arm.
ra8_err_t priv_jof_png_rows(jof_pull_fn pull, void *pull_ctx, jof_bump_t *bump, uint16_t max_w, uint16_t max_h, jof_geom_fn on_geom, jof_rows_fn on_rows, void *cb_ctx)
Streaming PNG scanline decode: pull bytes in, emit rows in order.
static ra8_err_t internal_png_inflate_idat(ra8_png_state_t *st, uint32_t first_len)
Inflate the whole IDAT stream, emitting every scanline.
static ra8_err_t internal_png_consume_rows(ra8_png_state_t *st, const uint8_t *src, uint32_t avail)
Assemble, reconstruct and emit rows from freshly inflated bytes.
static ra8_err_t internal_png_inflate_step(ra8_png_state_t *st, ra8_png_iter_t *it)
One inflate iteration: refill, tinfl, progress guard, drain rows.
static ra8_err_t internal_png_bind_geometry(ra8_png_state_t *st, jof_bump_t *bump)
Fire the geometry hook and carve the pixel-path buffers.
static ra8_err_t internal_png_refill_input(ra8_png_state_t *st, uint32_t *out_avail)
Refill the compressed-input buffer from the IDAT chunk stream.
static ra8_err_t internal_png_translate(ra8_png_state_t *st, const uint8_t *row)
Translate one reconstructed source row into the output layout.
ra8_err_t priv_jof_png_chunk_hdr(ra8_png_state_t *st, uint32_t *out_len, uint32_t *out_type)
Read one 8-byte chunk header (length + type, both big-endian).
ra8_err_t priv_jof_png_pre_idat(ra8_png_state_t *st, uint32_t len, uint32_t type)
Dispatch one pre-IDAT chunk (PLTE / tRNS / ancillary / stray IEND).
ra8_err_t priv_jof_png_prologue(ra8_png_state_t *st, uint16_t max_w, uint16_t max_h)
Verify the 8-byte signature, then parse + validate the IHDR.
ra8_err_t priv_jof_png_pull_exact(ra8_png_state_t *st, uint8_t *buf, uint32_t len)
Pull exactly len bytes; EOF mid-read is a protocol error.
ra8_err_t priv_jof_png_skip(ra8_png_state_t *st, uint32_t len)
Discard exactly len source bytes (unknown / ancillary chunks).
ra8_err_t priv_jof_png_finish(ra8_png_state_t *st)
Walk the post-IDAT chunks until IEND (ancillary chunks skipped).
Module-private declarations shared by the two PNG decoder units.
@ k_ra8_png_opaque
Fully-opaque alpha value.
@ k_ra8_png_filter_none
Row filter: none.
@ k_ra8_png_filter_avg
Row filter: average.
@ k_ra8_png_ch_3
3 bytes per pixel.
@ k_ra8_png_filter_sub
Row filter: sub.
@ k_ra8_png_color_gray
Grayscale.
@ k_ra8_png_filter_paeth
Row filter: Paeth.
@ k_ra8_png_color_ga
Grayscale + alpha.
@ k_ra8_png_color_pal
Indexed-colour.
@ k_ra8_png_ch_2
2 bytes per pixel.
@ k_ra8_png_filter_up
Row filter: up.
@ k_ra8_png_ch_1
1 byte per pixel.
@ k_ra8_png_ch_4
4 bytes per pixel.
@ k_ra8_png_max_chunks
Chunk-walk budget (hostile cap).
@ k_ra8_png_type_idat
"IDAT" big-endian.
@ k_ra8_png_inbuf_bytes
Compressed-input chunk buffer.
@ k_ra8_png_ring_bytes
Inflate ring (power of 2).
@ k_ra8_png_crc_bytes
Trailing chunk CRC field.
ra8_err_t(* jof_pull_fn)(void *ctx, uint8_t *buf, size_t cap, size_t *got)
Forward byte source for the producer (DIP seam).
static const char * s_tag
Logging / check tag.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_PRIV
Module-private helper: shared across TUs but only inside one library.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Error Code Definitions for ra8-firmware.
@ k_ra8_err_validation_failed
Validation rule failed (caller-supplied invariant not satisfied).
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
@ k_ra8_err_protocol_error
Protocol-level error (e.g.
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
void * memset(void *dst, int value, size_t n)
Fill memory with a constant byte value.
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
Lightweight Logging Interface for ra8-firmware.
Linear (bump) allocator over the caller's work arena.
Inflate-loop cursor: compressed-input window + progress guard.
uint8_t stalls
Consecutive zero-progress iterations.
uint8_t done
1 once tinfl reported stream end.
uint32_t in_pos
Consumed compressed bytes.
uint32_t in_avail
Valid compressed bytes in inbuf.
Whole streaming PNG decode state (module-static instance).
uint16_t trns_count
tRNS entries present.
uint16_t rows_done
Rows emitted so far.
uint32_t pending_type
Pending chunk type (big-endian value).
uint8_t * prevrow
Carved previous unfiltered row.
tinfl_decompressor * inflator
Carved inflate context.
jof_geom_fn on_geom
Producer geometry hook.
uint8_t pending_valid
1 when a pending chunk header is held.
uint8_t src_ch
Source bytes per pixel (filter bpp).
void * pull_ctx
Context for pull.
uint8_t * xlat
Carved translated output row.
uint32_t idat_rem
Bytes left in the current IDAT chunk.
uint8_t source_done
1 once a non-IDAT chunk ended the data.
uint32_t pending_len
Pending chunk payload length.
uint8_t * inbuf
Carved compressed-input buffer.
void * cb_ctx
Producer context.
jof_pull_fn pull
Sequential byte source.
uint8_t * ring
Carved 64 KiB inflate ring.
uint8_t trns[k_ra8_png_trns_max]
Palette alpha (tRNS).
uint8_t has_trns
1 once tRNS parsed.
uint16_t h
Image height, pixels.
jof_rows_fn on_rows
Producer row sink.
uint8_t * rowbuf
Carved scanline (filter + pixels).
uint32_t rowfill
Scanline bytes assembled so far.
uint16_t plte_count
Palette entries present.
uint32_t ring_wr
Ring write offset.
uint8_t palette[k_ra8_png_plte_max]
PLTE payload (RGB triples).
uint8_t has_plte
1 once PLTE parsed.
uint32_t rowlen
Scanline bytes (1 + w * src_ch).
uint8_t color_type
IHDR colour type.
uint8_t dst_ch
Output bytes per pixel (1, 3 or 4).
uint16_t w
Image width, pixels.