ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_rabook_raster.c File Reference

Caller-arena raster normalizer for RABOOK image records. More...

#include "ra8_rabook_raster.h"
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "ra8_attributes.h"
#include "ra8_img_arena.h"
#include "ra8_rabook_gray4.h"
#include "ra8_webp.h"
#include "stb_image.h"
Include dependency graph for ra8_rabook_raster.c:

Go to the source code of this file.

Data Structures

struct  internal_decoded_raster_t
 One decoded source view and the ownership needed to release it. More...

Typedefs

typedef struct internal_decoded_raster_t internal_decoded_raster_t
 One decoded source view and the ownership needed to release it.

Enumerations

enum  ra8_rabook_raster_consts_t : uint16_t {
  k_raster_webp_signature_size = 12U ,
  k_raster_webp_form_offset = 8U ,
  k_raster_webp_bytes_per_px = 4U ,
  k_raster_luma_r = 77U ,
  k_raster_luma_g = 150U ,
  k_raster_luma_b = 29U ,
  k_raster_luma_shift = 8U
}
 Raster signature and color-conversion constants. More...

Functions

static bool internal_is_webp (const uint8_t *source, size_t source_size)
 Return true only for a complete RIFF/WEBP signature.
static void internal_rgba_to_gray (uint8_t *rgba, uint32_t pixels)
 Convert an in-place RGBA frame to a packed grayscale prefix.
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.
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.
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_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 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.
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.
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.

Detailed Description

Caller-arena raster normalizer for RABOOK image records.

Decodes JPEG, PNG, GIF, BMP, or WebP bytes through caller-supplied codec arenas, optionally bounds the longer edge, and emits deterministic gray4 or gray8 payloads. Codec bindings are released on every path, and no filesystem service or heap allocation is required by the normalization pipeline.

Since
0.1.0

Definition in file ra8_rabook_raster.c.

Typedef Documentation

◆ internal_decoded_raster_t

typedef struct internal_decoded_raster_t internal_decoded_raster_t

One decoded source view and the ownership needed to release it.

Enumeration Type Documentation

◆ ra8_rabook_raster_consts_t

enum ra8_rabook_raster_consts_t : uint16_t

Raster signature and color-conversion constants.

Enumerator
k_raster_webp_signature_size 

RIFF size plus WEBP form tag.

k_raster_webp_form_offset 

WEBP form tag byte offset.

k_raster_webp_bytes_per_px 

Decoded RGBA bytes per pixel.

k_raster_luma_r 

stb-compatible red weight.

k_raster_luma_g 

stb-compatible green weight.

k_raster_luma_b 

stb-compatible blue weight.

k_raster_luma_shift 

Luminance normalization shift.

Definition at line 30 of file ra8_rabook_raster.c.

Function Documentation

◆ internal_decode_source()

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 )
static

Decode one supported source into a normalized grayscale view.

Detects WebP by signature and selects the bounded WebP or stb arena decoder, then normalizes ownership and sample access in decoded.

Parameters
[in]sourceEncoded raster bytes.
[in]source_sizeAccessible source length in bytes.
[in,out]workspaceCaller-owned decoder arenas.
[out]decodedReceives normalized pixels, dimensions, and ownership.
Returns
Decoder status.
Return values
k_ra8_okThe source decoded to a grayscale view.
k_ra8_err_validation_failedThe encoded source is malformed.
k_ra8_err_invalid_sizeSource or decoded dimensions are unsupported.
k_ra8_err_no_memA selected bounded arena is unavailable or too small.
Precondition
All pointer arguments are non-NULL and capacities describe accessible spans.
decoded does not alias source bytes or decoder arenas.
Postcondition
Success initializes every field of decoded.
Failure does not publish a successful normalized view.
Note
Not thread-safe; decoder arena binding is process-global.
Since
Version 0.1.0

Definition at line 392 of file ra8_rabook_raster.c.

References internal_decoded_raster_t::gray, internal_decoded_raster_t::height, internal_decode_stb(), internal_decode_webp(), internal_is_webp(), internal_decoded_raster_t::stb_pixels, internal_decoded_raster_t::webp, and internal_decoded_raster_t::width.

Referenced by internal_encode_source().

◆ internal_decode_stb()

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 )
static

Decode an stb-supported raster and expose arena-owned gray pixels.

Binds the caller arena, asks stb_image for one gray channel, and validates positive dimensions against the RABOOK uint16 fields. The successful allocation deliberately remains live for the caller to scale/encode before it frees and unbinds it.

Parameters
[in]sourceEncoded JPEG/PNG/GIF/BMP bytes.
[in]source_sizeReadable source length.
[in,out]workspaceCaller stb arena descriptor.
[out]out_grayReceives arena-owned gray pixels.
[out]out_widthReceives source width.
[out]out_heightReceives source height.
Returns
Decode status.
Return values
k_ra8_okGray pixels remain live in the bound arena.
k_ra8_err_invalid_sizeSource length or dimensions exceed an API bound.
k_ra8_err_no_memNo stb arena was supplied.
k_ra8_err_validation_failedstb rejected or could not decode the source.
Precondition
All pointer arguments are non-NULL.
workspace's stb arena backing spans its declared capacity.
Postcondition
On success the image arena remains bound until the public caller frees it.
On failure the image arena is unbound before return.
Note
Not thread-safe because the stb allocator binding is global.
Since
0.1.0

Definition at line 314 of file ra8_rabook_raster.c.

References k_ra8_err_invalid_size, k_ra8_err_no_mem, k_ra8_err_validation_failed, k_ra8_ok, ra8_img_arena_bind(), ra8_img_arena_unbind(), RA8_INTERNAL, and ra8_rabook_raster_workspace_t::stb_arena.

Referenced by internal_decode_source().

◆ internal_decode_webp()

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 )
static

Decode WebP into caller RGBA storage and expose its gray prefix.

Validates bounded dimensions, proves the full RGBA frame fits, runs the arena-backed WebP facade, then converts the frame prefix in place to deterministic grayscale.

Parameters
[in]sourceEncoded WebP bytes.
[in]source_sizeReadable source length.
[in,out]workspaceCaller WebP arena and RGBA frame storage.
[out]out_grayReceives the grayscale frame prefix.
[out]out_widthReceives source width.
[out]out_heightReceives source height.
Returns
Decode status.
Return values
k_ra8_okA complete gray frame is available.
k_ra8_err_invalid_sizeDimensions exceed the RABOOK wire fields.
k_ra8_err_no_memRequired caller storage is absent or undersized.
Returns
Other WebP validation/decode errors propagate unchanged.
Precondition
All pointer arguments are non-NULL.
Non-NULL arena/buffer members span their declared capacities.
Postcondition
On success the WebP arena is drained and outputs are initialized.
On error the output pointer/dimensions remain untouched.
Note
Not thread-safe because the WebP allocator binding is global.
Since
0.1.0

Definition at line 241 of file ra8_rabook_raster.c.

References internal_rgba_to_gray(), k_ra8_err_invalid_size, k_ra8_err_no_mem, k_ra8_ok, k_raster_webp_bytes_per_px, RA8_INTERNAL, ra8_webp_decode_rgba(), ra8_webp_get_info(), ra8_rabook_raster_workspace_t::rgba, ra8_rabook_raster_workspace_t::rgba_cap, and ra8_rabook_raster_workspace_t::webp_arena.

Referenced by internal_decode_source().

◆ internal_encode()

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 )
static

Encode scaled grayscale pixels at the requested device depth.

Saturates a host-sized capacity to the uint32 firmware API and dispatches to gray8 copy or gray4 nibble packing.

Parameters
[in]grayScaled grayscale pixels.
[in]widthPixel width.
[in]heightPixel height.
[in]pixel_formatValid RABOOK gray depth.
[out]encodedEncoded destination bytes.
[in]encoded_capWritable destination capacity.
[out]out_sizeReceives the exact encoded byte count.
Returns
Encoder status.
Return values
k_ra8_okPayload encoded.
k_ra8_err_no_memDestination is too small.
Returns
Other encoder argument errors propagate unchanged.
Precondition
Pointers are non-NULL and gray spans width * height bytes.
pixel_format is gray4 or gray8.
Postcondition
On success encoded contains exactly *out_size payload bytes.
The input grayscale buffer is not modified.
Note
Thread-safe for distinct buffers.
Since
0.1.0

Definition at line 203 of file ra8_rabook_raster.c.

References k_book_pixfmt_gray8, RA8_INTERNAL, ra8_rabook_gray4_encode(), and ra8_rabook_gray8_encode().

Referenced by internal_encode_source().

◆ internal_encode_source()

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 )
static

Scale, encode, and release one successfully validated source.

Decodes to gray, derives aspect-preserving bounded dimensions, scales when required, packs the requested depth, and releases stb arena ownership on every post-decode path.

Parameters
[in]sourceEncoded raster bytes.
[in]source_sizeAccessible source length in bytes.
[in]max_edgeMaximum output width or height in pixels.
[in]pixel_formatRequested gray4 or gray8 output format.
[in,out]workspaceCaller-owned decode and scale arenas.
[out]encodedDestination for packed output bytes.
[in]encoded_capWritable bytes at encoded.
[out]resultReceives dimensions, format, and encoded length on success.
Returns
Complete raster pipeline status.
Return values
k_ra8_okThe raster was decoded, scaled, and encoded.
k_ra8_err_validation_failedSource decoding failed.
k_ra8_err_invalid_sizeGeometry or destination capacity is invalid.
k_ra8_err_no_memA caller-owned arena cannot satisfy the operation.
Precondition
Pointer arguments and workspace arenas passed public validation.
encoded addresses encoded_cap writable bytes disjoint from source.
Postcondition
Success fully initializes result and the reported encoded prefix.
Failure zeros result and releases any temporary stb ownership.
Note
Not thread-safe; decoder arena binding is process-global.
Since
Version 0.1.0

Definition at line 442 of file ra8_rabook_raster.c.

References ra8_rabook_raster_result_t::encoded_size, internal_decoded_raster_t::gray, internal_decoded_raster_t::height, ra8_rabook_raster_result_t::height, internal_decode_source(), internal_encode(), internal_output_dims(), internal_scale(), k_ra8_ok, ra8_rabook_raster_result_t::pixel_format, ra8_img_arena_unbind(), internal_decoded_raster_t::stb_pixels, internal_decoded_raster_t::webp, internal_decoded_raster_t::width, and ra8_rabook_raster_result_t::width.

Referenced by ra8_rabook_raster_encode().

◆ internal_is_webp()

bool internal_is_webp ( const uint8_t * source,
size_t source_size )
static

Return true only for a complete RIFF/WEBP signature.

Checks both the RIFF container tag and WEBP form tag after proving the fixed header is readable; other RIFF formats remain on the stb path and fail its decoder normally.

Parameters
[in]sourceEncoded candidate bytes.
[in]source_sizeReadable byte count at source.
Returns
Whether the candidate carries the WebP signature.
Return values
trueBoth required tags match.
falseThe header is short or either tag differs.
Precondition
source is non-NULL and spans source_size bytes.
source_size is the complete candidate byte length.
Postcondition
No input or codec state is modified.
No byte beyond the twelve-byte signature window is inspected.
Note
Pure and thread-safe.
Since
0.1.0

Definition at line 57 of file ra8_rabook_raster.c.

References k_raster_webp_form_offset, k_raster_webp_signature_size, memcmp(), and RA8_INTERNAL.

Referenced by internal_decode_source().

◆ internal_output_dims()

void internal_output_dims ( uint16_t source_width,
uint16_t source_height,
uint16_t max_edge,
uint16_t * out_width,
uint16_t * out_height )
static

Compute output geometry, preserving dimensions when unclamped.

A zero clamp copies the source dimensions. A nonzero clamp delegates to the shared fixed-point RABOOK dimension helper.

Parameters
[in]source_widthSource width in pixels.
[in]source_heightSource height in pixels.
[in]max_edgeOptional longer-edge limit; zero disables scaling.
[out]out_widthReceives the output width.
[out]out_heightReceives the output height.
Precondition
Output pointers are non-NULL and source dimensions are nonzero.
max_edge is zero or a valid nonzero pixel edge.
Postcondition
Outputs preserve the source or fit within max_edge.
Both output dimensions are nonzero.
Note
Pure and thread-safe.
Since
0.1.0

Definition at line 111 of file ra8_rabook_raster.c.

References RA8_INTERNAL, and ra8_rabook_gray4_output_dims().

Referenced by internal_encode_source().

◆ internal_rgba_to_gray()

void internal_rgba_to_gray ( uint8_t * rgba,
uint32_t pixels )
static

Convert an in-place RGBA frame to a packed grayscale prefix.

Reads each four-byte pixel before replacing the corresponding byte in the frame prefix with stb's deterministic integer luminance.

Parameters
[in,out]rgbaFrame containing pixels RGBA8888 pixels.
[in]pixelsNumber of pixels to convert.
Precondition
rgba is non-NULL and spans pixels * 4 readable/writable bytes.
pixels * 4 is representable by size_t.
Postcondition
The first pixels bytes hold grayscale samples; later bytes are unspecified scratch and no byte outside the original frame is written.
The alpha channel does not influence any grayscale sample.
Note
Pure with respect to global state; thread-safe for distinct frames.
Since
0.1.0

Definition at line 84 of file ra8_rabook_raster.c.

References k_raster_luma_b, k_raster_luma_g, k_raster_luma_r, k_raster_luma_shift, k_raster_webp_bytes_per_px, and RA8_INTERNAL.

Referenced by internal_decode_webp().

◆ internal_scale()

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 )
static

Select the source or caller downscale buffer for final encoding.

Reuses source_gray when geometry is unchanged; otherwise checks the caller's gray capacity and fills it through the shared bilinear kernel before returning that buffer.

Parameters
[in]source_graySource grayscale pixels.
[in]source_widthSource width in pixels.
[in]source_heightSource height in pixels.
[in]out_widthRequested output width.
[in]out_heightRequested output height.
[in,out]workspaceExclusive caller scratch descriptor.
[out]out_grayReceives the selected grayscale buffer.
Returns
Scaling status.
Return values
k_ra8_okSource or scaled pixels are ready.
k_ra8_err_no_memThe gray scratch is absent or undersized.
Returns
Other downscale errors propagate unchanged.
Precondition
Pointer arguments are non-NULL and geometries are nonzero.
source_gray spans source_width * source_height readable bytes.
Postcondition
On success out_gray spans out_width * out_height readable bytes.
The source buffer is not modified.
Note
Thread-safe for distinct workspaces.
Since
0.1.0

Definition at line 148 of file ra8_rabook_raster.c.

References ra8_rabook_raster_workspace_t::gray, ra8_rabook_raster_workspace_t::gray_cap, k_ra8_err_no_mem, k_ra8_ok, RA8_INTERNAL, and ra8_rabook_gray4_downscale().

Referenced by internal_encode_source().

◆ ra8_rabook_raster_encode()

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 )
nodiscard

Decode and normalize one encoded raster into a RABOOK image payload.

WebP is selected only by its RIFF/WEBP signature; every other source is passed to stb_image, which validates its own JPEG/PNG/GIF/BMP structure. RGBA WebP pixels use stb_image's integer luminance rule (77R + 150G + 29B) >> 8, keeping codec results deterministic. A zero max_edge preserves source geometry; otherwise the longer edge is bounded through ra8_rabook_gray4_output_dims.

Parameters
[in]sourceEncoded image bytes.
[in]source_sizeNumber of readable source bytes.
[in]max_edgeOptional longer-edge clamp; zero preserves size.
[in]pixel_formatk_book_pixfmt_gray4 or k_book_pixfmt_gray8.
[in,out]workspaceExclusive initialized scratch descriptor.
[out]encodedDestination for normalized gray payload bytes.
[in]encoded_capCapacity of encoded in bytes.
[out]resultReceives geometry, format, and encoded byte count.
Returns
Conversion status.
Return values
k_ra8_okPayload produced.
k_ra8_err_null_ptrA required pointer is NULL.
k_ra8_err_invalid_argSource is empty or pixel format invalid.
k_ra8_err_invalid_sizeA source/dimension exceeds a bounded API.
k_ra8_err_no_memCaller workspace/output is too small.
k_ra8_err_validation_failedThe encoded source is malformed.
Returns
Other WebP facade errors propagate unchanged.
Precondition
All workspace buffers and arena backings remain live for the call.
Source, scratch, and destination regions do not overlap.
Postcondition
On success encoded contains exactly result->encoded_size bytes.
On failure result is zeroed and neither codec arena remains bound.
Note
Not thread-safe because the underlying codec arena bindings are global.
Since
0.1.0

Definition at line 490 of file ra8_rabook_raster.c.

References internal_encode_source(), k_book_pixfmt_gray4, k_book_pixfmt_gray8, k_ra8_err_invalid_arg, and k_ra8_err_null_ptr.

Referenced by ra8_rabook_comic_add_page().