ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
epub_img_import.c
Go to the documentation of this file.
1
24
25#if __has_include("ra8_tile_cache.h") && __has_include("jof.h")
26
27#include <stddef.h>
28#include <stdint.h>
29#include <string.h>
30
31#include "epub.h"
32#include "epub_entry.h"
33#include "epub_img_tiles.h"
34#include "jof.h"
35#include "jof_produce.h"
36#include "ra8_attributes.h"
37#include "ra8_check.h"
38#include "ra8_err.h"
39
44typedef enum : uint8_t {
45 k_epub_import_magic_len = 4U,
46} epub_import_const_t;
47
58typedef struct {
59 epub_entry_reader_t reader;
60} epub_entry_pull_t;
61
79static ra8_err_t internal_entry_pull(void* ctx, uint8_t* buf, size_t cap, size_t* got)
80{
81 epub_entry_pull_t* pull = (epub_entry_pull_t*)ctx;
82 return epub_entry_read(&pull->reader, buf, cap, got);
83}
84
105static ra8_err_t internal_classify(epub_book_t* book, const char* href, bool* out_is_atlas)
106{
108 static const uint8_t s_atlas_magic[k_epub_import_magic_len] = {'J', 'O', 'F', '1'};
109 *out_is_atlas = false;
110 uint8_t magic[k_epub_import_magic_len] = {};
111 size_t got = 0U;
112 const ra8_err_t err = epub_entry_pread(book, href, 0U, magic, sizeof(magic), &got);
113 if (err == k_ra8_err_not_supported) {
114 return k_ra8_ok; /* deflated entry: cannot be an in-place atlas */
115 }
116 if (err != k_ra8_ok) {
117 return err;
118 }
119 if ((got == sizeof(magic)) && (memcmp(magic, s_atlas_magic, sizeof(magic)) == 0)) {
120 *out_is_atlas = true;
121 }
122 return k_ra8_ok;
123}
124
144static ra8_err_t internal_transcode(epub_book_t* book,
145 const char* href,
146 const epub_atlas_import_cfg_t* cfg,
147 jof_info_t* out_info)
148{
149 epub_entry_pull_t pull = {};
150 uint64_t size = 0U;
151 ra8_err_t err = epub_entry_open(book, href, &pull.reader, &size);
152 if (err != k_ra8_ok) {
153 return err;
154 }
155 const jof_produce_cfg_t pcfg = {
156 .pull = internal_entry_pull,
157 .pull_ctx = &pull,
158 .sink = cfg->store.sink,
159 .sink_ctx = cfg->store.sink_ctx,
160 .tile_w = cfg->tile_w,
161 .tile_h = cfg->tile_h,
162 .codec = cfg->codec,
163 .max_width = cfg->max_width,
164 .max_height = cfg->max_height,
165 .work = cfg->work,
166 .work_cap = cfg->work_cap,
167 .webp_work = cfg->webp_work,
168 .webp_work_cap = cfg->webp_work_cap,
169 };
170 err = jof_produce(&pcfg, out_info);
171 const ra8_err_t close_err = epub_entry_close(&pull.reader);
172 return (err != k_ra8_ok) ? err : close_err;
173}
174
193static ra8_err_t internal_import_args_ok(const epub_tile_binder_t* binder,
194 const epub_book_t* book,
195 const char* href,
196 const epub_atlas_import_cfg_t* cfg)
197{
198 static const char* const s_tag = "epub_img_import";
199 RA8_CHECK_NULL_PTR(binder, s_tag, "binder must not be nullptr");
200 RA8_CHECK_NULL_PTR(book, s_tag, "book must not be nullptr");
201 RA8_CHECK_NULL_PTR(href, s_tag, "href must not be nullptr");
202 RA8_CHECK_NULL_PTR(cfg, s_tag, "cfg must not be nullptr");
203 RA8_CHECK_NULL_PTR(cfg->store.sink, s_tag, "store.sink must not be nullptr");
204 RA8_CHECK_NULL_PTR(cfg->store.pread, s_tag, "store.pread must not be nullptr");
205 return k_ra8_ok;
206}
207
209 epub_book_t* book,
210 const char* href,
211 uint32_t image_id,
212 const epub_atlas_import_cfg_t* cfg)
213{
214 const ra8_err_t nz = internal_import_args_ok(binder, book, href, cfg);
215 if (nz != k_ra8_ok) {
216 return nz;
217 }
218 const size_t hlen = strlen(href);
219 if ((hlen == 0U) || (hlen >= (size_t)k_epub_max_path_len)) {
221 }
222 bool is_atlas = false;
223 ra8_err_t err = internal_classify(book, href, &is_atlas);
224 if (err != k_ra8_ok) {
225 return err;
226 }
227 if (is_atlas) {
228 return epub_tile_binder_add(binder, book, href, image_id); /* in-place */
229 }
230 jof_info_t info = {};
231 err = internal_transcode(book, href, cfg, &info);
232 if (err != k_ra8_ok) {
233 return err;
234 }
235 return epub_tile_binder_add_ext(binder,
236 cfg->store.pread,
237 cfg->store.pread_ctx,
238 (uint64_t)info.total_size,
239 image_id);
240}
241
242#else
243/* ra8_mem tile cache / atlas producer not on the include path for this app:
244 * the import wiring is unused here. A single typedef keeps the translation
245 * unit non-empty. */
247#endif /* __has_include("ra8_tile_cache.h") && __has_include("jof.h") */
EPUB (.epub) reader and chapter iterator for ra8-firmware.
@ k_epub_max_path_len
Max href length (incl.
Definition epub.h:89
Iterative, bounded-RAM ZIP-entry extraction for the EPUB reader (#231).
ra8_err_t epub_entry_pread(epub_book_t *book, const char *path, uint64_t offset, uint8_t *buf, size_t len, size_t *got)
Positioned read of a stored (uncompressed) archive entry – windowed random access in bounded RAM (#23...
Definition epub_entry.c:446
ra8_err_t epub_entry_read(epub_entry_reader_t *reader, uint8_t *buf, size_t cap, size_t *got)
Pull the next chunk of a streaming entry into a bounded caller buffer (#231).
Definition epub_entry.c:360
ra8_err_t epub_entry_close(epub_entry_reader_t *reader)
Tear down a streaming-entry cursor and release its inflate state (#231).
Definition epub_entry.c:392
ra8_err_t epub_entry_open(epub_book_t *book, const char *path, epub_entry_reader_t *out_reader, uint64_t *out_size)
Begin a bounded-RAM streaming extraction of one archive entry (#231).
Definition epub_entry.c:331
int epub_img_import_unused_translation_unit_t
Page a large in-EPUB image through ra8_tile_cache + a real reflow <img> loader off the streaming read...
ra8_err_t epub_tile_binder_add(epub_tile_binder_t *binder, epub_book_t *book, const char *path, uint32_t image_id)
Register a stored in-archive JOF atlas entry under image_id (#231).
ra8_err_t epub_tile_binder_add_ext(epub_tile_binder_t *binder, jof_pread_fn pread, void *pread_ctx, uint64_t total_size, uint32_t image_id)
Register an externally-backed JOF atlas under image_id (#231).
ra8_err_t epub_tile_binder_import(epub_tile_binder_t *binder, epub_book_t *book, const char *href, uint32_t image_id, const epub_atlas_import_cfg_t *cfg)
Import a manifest image through the transcode producer and register it for tile paging (#231 – the op...
JOF band-tile atlas: the display-native normalized image format (#231, shared with the longstrip scro...
Import-time transcode producer: JPEG/PNG/WebP -> JOF band-tile atlas in bounded RAM (#231,...
ra8_err_t jof_produce(const jof_produce_cfg_t *cfg, jof_info_t *out_info)
Transcode one encoded JPEG/PNG/WebP source into a JOF atlas (#231, #290).
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Annotation-attribute framework macros for ra8-firmware.
#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.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ k_ra8_err_not_supported
Requested feature not compiled in, not wired, or not supported by this MCU variant.
Definition ra8_err.h:180
@ k_ra8_err_invalid_arg
Invalid function argument.
Definition ra8_err.h:152
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
int memcmp(const void *a, const void *b, size_t n)
Compare bytes in two memory areas.
size_t strlen(const char *s)
Calculate string length.
static ra8_sdcard_card_type_t internal_classify(uint8_t high_capacity, uint32_t blocks)
Classify card type from OCR.CCS and decoded block count.
Definition ra8_sdcard.c:434
Import-time transcode knobs: tile geometry, budget caps, work arena and the destination store.
size_t webp_work_cap
WebP arena size (jof_webp_work_bytes()).
uint16_t tile_h
Tile height, pixels (>= 1).
uint16_t max_height
Height budget cap (0 = format cap).
epub_atlas_store_t store
Destination atlas store.
size_t work_cap
Arena size (jof_work_bytes()).
uint16_t max_width
Width budget cap (0 = format cap).
uint16_t tile_w
Tile width, pixels (>= 1).
uint8_t codec
jof_codec_t member.
uint8_t * webp_work
WebP whole-frame arena, or NULL to reject WebP.
void * pread_ctx
Context for pread.
jof_pread_fn pread
Read-back seam for the same bytes.
void * sink_ctx
Context for sink.
jof_sink_fn sink
Append-only atlas writer.
Opened EPUB book.
Definition epub.h:286
Forward streaming cursor over one ZIP entry – inflate in bounded RAM (#231).
Definition epub_entry.h:93
Binds up to k_epub_tile_max_sources images to one tile cache.
Parsed + validated geometry of one JOF atlas.
Definition jof.h:208
uint32_t total_size
Whole atlas byte length (footer included).
Definition jof.h:219
Producer configuration: source, sink, tile geometry and work arena.