30typedef enum : uint16_t {
59 static const uint8_t k_riff[] = {
'R',
'I',
'F',
'F'};
60 static const uint8_t k_webp[] = {
'W',
'E',
'B',
'P'};
64 if (
memcmp(source, k_riff,
sizeof k_riff) != 0) {
86 for (uint32_t i = 0U; i < pixels; ++i) {
112 uint16_t source_height,
115 uint16_t* out_height)
117 if (max_edge == 0U) {
118 *out_width = source_width;
119 *out_height = source_height;
149 uint16_t source_width,
150 uint16_t source_height,
154 const uint8_t** out_gray)
156 if (source_width == out_width) {
157 if (source_height == out_height) {
158 *out_gray = source_gray;
162 const uint32_t pixels = (uint32_t)out_width * (uint32_t)out_height;
163 if (workspace->
gray ==
nullptr) {
166 if ((
size_t)pixels > workspace->
gray_cap) {
176 *out_gray = workspace->
gray;
206 uint8_t pixel_format,
211 const uint32_t bounded_cap = (encoded_cap > UINT32_MAX) ? UINT32_MAX : (uint32_t)encoded_cap;
244 const uint8_t** out_gray,
246 uint16_t* out_height)
249 uint32_t height = 0U;
254 if (width > UINT16_MAX) {
257 if (height > UINT16_MAX) {
260 const uint64_t frame_size =
265 if (workspace->
rgba ==
nullptr) {
268 if (frame_size > (uint64_t)workspace->
rgba_cap) {
282 const uint32_t pixels = width * height;
284 *out_gray = workspace->
rgba;
285 *out_width = (uint16_t)width;
286 *out_height = (uint16_t)height;
319 uint16_t* out_height)
321 if (source_size > (
size_t)INT_MAX) {
331 uint8_t* pixels = stbi_load_from_memory(source,
337 if (pixels ==
nullptr) {
341 bool dimensions_valid = width > 0;
343 dimensions_valid =
false;
345 if (width > (
int)UINT16_MAX) {
346 dimensions_valid =
false;
348 if (height > (
int)UINT16_MAX) {
349 dimensions_valid =
false;
351 if (!dimensions_valid) {
352 stbi_image_free(pixels);
357 *out_width = (uint16_t)width;
358 *out_height = (uint16_t)height;
410 if (!decoded->
webp) {
445 uint8_t pixel_format,
456 uint16_t out_width = 0U;
457 uint16_t out_height = 0U;
459 const uint8_t* scaled =
nullptr;
484 result->
width = out_width;
485 result->
height = out_height;
493 uint8_t pixel_format,
499 if (source ==
nullptr) {
502 if (workspace ==
nullptr) {
505 if (encoded ==
nullptr) {
508 if (result ==
nullptr) {
512 if (source_size == 0U) {
@ k_book_pixfmt_gray4
4bpp packed grayscale, 2px/byte (default; every pre-field blob).
@ k_book_pixfmt_gray8
8bpp grayscale, 1px/byte (lossless against any grey panel).
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
@ k_ra8_err_no_mem
Static buffer exhausted (no dynamic memory on this project).
@ k_ra8_err_invalid_arg
Invalid function argument.
@ 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_null_ptr
Pointer was NULL where a valid pointer was required.
@ 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.
int memcmp(const void *a, const void *b, size_t n)
Compare bytes in two memory areas.
Heap-free scratch allocator hooks for the stb_image single-TU build.
void ra8_img_arena_bind(ra8_img_arena_t *arena)
Bind arena as the active scratch for subsequent decode allocations.
void ra8_img_arena_unbind(void)
Unbind the active arena; subsequent allocation hooks fail with nullptr.
Grayscale image transcode stage (4-bpp / 8-bpp) for the on-device EPUB compiler (#149).
ra8_err_t ra8_rabook_gray4_encode(const uint8_t *gray_pixels, uint16_t w, uint16_t h, uint8_t *out, uint32_t out_cap, uint32_t *out_size)
Quantise a grayscale buffer to 16 levels and pack as 4-bpp nibbles.
ra8_err_t ra8_rabook_gray4_downscale(const uint8_t *src, uint16_t src_w, uint16_t src_h, uint8_t *dst, uint16_t dst_w, uint16_t dst_h)
Bilinear-interpolate a grayscale image from (src_w x src_h) to (dst_w x dst_h).
ra8_err_t ra8_rabook_gray8_encode(const uint8_t *gray_pixels, uint16_t w, uint16_t h, uint8_t *out, uint32_t out_cap, uint32_t *out_size)
Copy a grayscale buffer out verbatim as 8-bpp (one byte per pixel).
void ra8_rabook_gray4_output_dims(uint16_t src_w, uint16_t src_h, uint16_t max_edge, uint16_t *out_w, uint16_t *out_h)
Compute scaled output dimensions keeping the longer edge within max_edge.
static ra8_err_t internal_decode_source(const uint8_t *source, size_t source_size, ra8_rabook_raster_workspace_t *workspace, internal_decoded_raster_t *decoded)
Decode one supported source into a normalized grayscale view.
ra8_err_t ra8_rabook_raster_encode(const uint8_t *source, size_t source_size, uint16_t max_edge, uint8_t pixel_format, ra8_rabook_raster_workspace_t *workspace, uint8_t *encoded, size_t encoded_cap, ra8_rabook_raster_result_t *result)
Decode and normalize one encoded raster into a RABOOK image payload.
static bool internal_is_webp(const uint8_t *source, size_t source_size)
Return true only for a complete RIFF/WEBP signature.
static ra8_err_t internal_encode(const uint8_t *gray, uint16_t width, uint16_t height, uint8_t pixel_format, uint8_t *encoded, size_t encoded_cap, uint32_t *out_size)
Encode scaled grayscale pixels at the requested device depth.
static ra8_err_t internal_decode_webp(const uint8_t *source, size_t source_size, ra8_rabook_raster_workspace_t *workspace, const uint8_t **out_gray, uint16_t *out_width, uint16_t *out_height)
Decode WebP into caller RGBA storage and expose its gray prefix.
static ra8_err_t internal_encode_source(const uint8_t *source, size_t source_size, uint16_t max_edge, uint8_t pixel_format, ra8_rabook_raster_workspace_t *workspace, uint8_t *encoded, size_t encoded_cap, ra8_rabook_raster_result_t *result)
Scale, encode, and release one successfully validated source.
static ra8_err_t internal_scale(const uint8_t *source_gray, uint16_t source_width, uint16_t source_height, uint16_t out_width, uint16_t out_height, ra8_rabook_raster_workspace_t *workspace, const uint8_t **out_gray)
Select the source or caller downscale buffer for final encoding.
ra8_rabook_raster_consts_t
Raster signature and color-conversion constants.
@ k_raster_luma_shift
Luminance normalization shift.
@ k_raster_webp_signature_size
RIFF size plus WEBP form tag.
@ k_raster_luma_r
stb-compatible red weight.
@ k_raster_luma_g
stb-compatible green weight.
@ k_raster_webp_form_offset
WEBP form tag byte offset.
@ k_raster_luma_b
stb-compatible blue weight.
@ k_raster_webp_bytes_per_px
Decoded RGBA bytes per pixel.
static void internal_rgba_to_gray(uint8_t *rgba, uint32_t pixels)
Convert an in-place RGBA frame to a packed grayscale prefix.
static ra8_err_t internal_decode_stb(const uint8_t *source, size_t source_size, ra8_rabook_raster_workspace_t *workspace, uint8_t **out_gray, uint16_t *out_width, uint16_t *out_height)
Decode an stb-supported raster and expose arena-owned gray pixels.
static void internal_output_dims(uint16_t source_width, uint16_t source_height, uint16_t max_edge, uint16_t *out_width, uint16_t *out_height)
Compute output geometry, preserving dimensions when unclamped.
Caller-arena raster normalizer for RABOOK image records.
Zero-heap WebP (VP8 / VP8L) decode facade over the vendored libwebp.
ra8_err_t ra8_webp_decode_rgba(const uint8_t *data, size_t size, ra8_webp_arena_t *arena, uint8_t *out_rgba, size_t out_stride, size_t out_capacity, uint32_t *out_w, uint32_t *out_h)
Decode a WebP into a caller-owned RGBA8888 buffer, heap-free.
ra8_err_t ra8_webp_get_info(const uint8_t *data, size_t size, uint32_t *out_w, uint32_t *out_h)
Read a WebP header's pixel dimensions without decoding the body.
One decoded source view and the ownership needed to release it.
uint16_t height
Source height in pixels.
uint16_t width
Source width in pixels.
uint8_t * stb_pixels
stb-owned arena span, or null for WebP.
bool webp
Whether the WebP caller arena owns it.
const uint8_t * gray
Decoded grayscale samples.
Geometry and byte count of a normalized RABOOK raster payload.
uint16_t height
Output height in pixels.
uint32_t encoded_size
Bytes written to the encoded destination.
uint8_t pixel_format
book_image_pixfmt_t emitted.
uint16_t width
Output width in pixels.
Exclusive caller-owned scratch used by one raster conversion.
size_t gray_cap
Capacity of gray in bytes.
uint8_t * rgba
WebP RGBA frame storage.
uint8_t * gray
Downscaled grayscale storage.
ra8_webp_arena_t * webp_arena
WebP decoder scratch arena.
ra8_img_arena_t * stb_arena
JPEG/PNG/GIF/BMP decoder arena.
size_t rgba_cap
Capacity of rgba in bytes.