图库: 阅读器页槽改 RGB565 常驻,页图内存减半(M32)

- 页槽从 RGBA 改成「已落框的 RGB565 位图」(2B/px):真机 480×853 页 1599KB → 800KB,
  两页常驻 3.2MB → 1.6MB。解码仍在 worker 里产出 RGBA,当场转 565 并释放 RGBA。
- SDK 新增 565 原语(meow_ui_draw.h / meow_ui_gfx.c):meow_gfx_blit_rgb565 命中页每行
  一次 memcpy(单次拷贝、无每帧重采样),缩放兜底走 565 最近邻。量化与 back_buf 用同一个
  rgb565(),565 屏上落下的像素与旧 RGBA 路径逐像素一致(不引入色偏/新增色带)。
- 预算按 565 重算:单页像素上限沿用旧 3MB@RGBA 的同几何口径(=1.5MB@565),
  MR_RESIDENT_CAP = 3 槽 + 1 张在途 = 3.2MB;reader_estimate_peak 新增 convert_bytes
  (落槽暂存 2B/px)与 peak_bytes 一起参与门控,避免实测超预算。
- 结果队列(已解码、未落槽)字节计入总预算(pending_bytes),不再「偷吃」最多 2 页。
- CBZ meta() 改读条目 4KB 前缀(mz_read_prefix;deflate 解到 4KB 即停,输入缓冲走栈),
  500 页排版不再触发 500 次全量解压。
- 测试:B3/B8 的 565 常驻与在途预算断言、CBZ 500 页 stored/deflate 前后对比、
  P12 565 与 RGBA 路径的逐像素颜色对比(量化误差/RMSE/灰阶梯)。
This commit is contained in:
AskaEth committed 2026-11-06 18:43:11 +08:00
1 parent bbc73635a6
commit 7036020005
11 files changed
+910 -111

No files matched your search

+62 -6
View File
@@ -18,6 +18,7 @@
#define MZ_EOCD_MIN 22u
#define MZ_COMMENT_MAX 65535u
#define MZ_IN_CHUNK 65536u
#define MZ_PREFIX_IN_CHUNK 4096u /* prefix 读的栈缓冲(M32,见 inflate_entry) */
#define MZ_CRC_CHUNK (1u << 24)
struct mz_archive {
@@ -322,15 +323,21 @@ static uint32_t crc_buf(const uint8_t *p, size_t n)
}
/* 从 data_off 流式 inflate comp_size 字节到 buf(容量 buf_cap = 声明解压尺寸)。
* 输出多于声明尺寸或流提前结束都算 FORMAT/TRUNCATED,不整块读取压缩数据。 */
* 输出多于声明尺寸或流提前结束都算 FORMAT/TRUNCATED,不整块读取压缩数据。
* prefix=1(M32):装满 buf_cap 就停——只取前缀,不把整条目解压完;此时不判 CRC
* (调用方没读完整条目,见 mz_read_prefix)。prefix=0 的语义与旧实现逐字一致。 */
static int inflate_entry(FILE *fp, uint64_t data_off, uint64_t comp_size,
uint8_t *buf, size_t buf_cap, size_t *out_len)
uint8_t *buf, size_t buf_cap, size_t *out_len, int prefix)
{
z_stream z;
memset(&z, 0, sizeof z);
if (inflateInit2(&z, -MAX_WBITS) != Z_OK) return MZ_ERR_MEM;
uint8_t *in = malloc(MZ_IN_CHUNK);
/* prefix 模式最多只要 buf_cap 字节(4KB 文件头),输入侧用 4KB 栈缓冲:
* 不为一次前缀读分配 64KB(500 页排版 = 500 次 64KB 分配/释放)。 */
uint8_t in_stack[MZ_PREFIX_IN_CHUNK];
size_t in_chunk = prefix ? sizeof in_stack : MZ_IN_CHUNK;
uint8_t *in = prefix ? in_stack : (uint8_t *)malloc(MZ_IN_CHUNK);
if (!in) {
inflateEnd(&z);
return MZ_ERR_MEM;
@@ -343,13 +350,14 @@ static int inflate_entry(FILE *fp, uint64_t data_off, uint64_t comp_size,
int stalls = 0;
while (rc == MZ_OK) {
if (prefix && produced == buf_cap) break; /* 前缀够了:不再解压 */
if (z.avail_in == 0) {
if (consumed >= comp_size) {
rc = MZ_ERR_TRUNCATED;
break;
}
uint64_t remain = comp_size - consumed;
size_t want = remain > MZ_IN_CHUNK ? MZ_IN_CHUNK : (size_t)remain;
size_t want = remain > in_chunk ? in_chunk : (size_t)remain;
rc = read_exact(fp, in, want);
if (rc != MZ_OK) break;
z.next_in = in;
@@ -399,7 +407,7 @@ static int inflate_entry(FILE *fp, uint64_t data_off, uint64_t comp_size,
}
inflateEnd(&z);
free(in);
if (!prefix) free(in);
if (rc == MZ_OK) *out_len = produced;
return rc;
}
@@ -442,7 +450,7 @@ int mz_read(mz_archive *z, const mz_entry *e, void *buf, size_t buf_size,
if (crc_buf((const uint8_t *)buf, need) != e->crc) return MZ_ERR_CRC;
} else {
size_t made = 0;
rc = inflate_entry(z->fp, data_off, e->comp_size, (uint8_t *)buf, need, &made);
rc = inflate_entry(z->fp, data_off, e->comp_size, (uint8_t *)buf, need, &made, 0);
if (rc != MZ_OK) return rc;
if (made != need) return MZ_ERR_FORMAT;
if (crc_buf((const uint8_t *)buf, made) != e->crc) return MZ_ERR_CRC;
@@ -476,6 +484,54 @@ int mz_read_alloc(mz_archive *z, const mz_entry *e, void **out_buf,
return MZ_OK;
}
int mz_read_prefix(mz_archive *z, const mz_entry *e, void *buf, size_t buf_size,
size_t *out_size)
{
if (out_size) *out_size = 0;
if (!z || !z->fp || !e) return MZ_ERR_ARG;
if (!buf && buf_size > 0) return MZ_ERR_ARG;
if (buf_size == 0) return MZ_OK;
if (e->flags & 1u) return MZ_ERR_UNSUPPORTED;
if (e->method != 0 && e->method != 8) return MZ_ERR_UNSUPPORTED;
if (e->uncomp_size > (uint64_t)SIZE_MAX) return MZ_ERR_UNSUPPORTED;
/* 与 mz_read 同一套局部头/数据范围校验(结构非法一律拒绝)。 */
uint8_t lh[MZ_LOCAL_MIN];
int rc = read_at(z->fp, e->local_offset, lh, sizeof lh);
if (rc != MZ_OK) return rc;
if (rd32(lh) != MZ_SIG_LOCAL) return MZ_ERR_FORMAT;
if (rd16(lh + 8) != e->method) return MZ_ERR_FORMAT;
uint64_t data_off = 0;
uint64_t data_end = 0;
if (!u64_add(e->local_offset, MZ_LOCAL_MIN, &data_off) ||
!u64_add(data_off, rd16(lh + 26), &data_off) ||
!u64_add(data_off, rd16(lh + 28), &data_off) ||
!u64_add(data_off, e->comp_size, &data_end) ||
data_end > z->file_size) {
return MZ_ERR_TRUNCATED;
}
size_t want = buf_size;
if (e->uncomp_size < (uint64_t)want) want = (size_t)e->uncomp_size;
if (e->method == 0) {
if (e->comp_size != e->uncomp_size) return MZ_ERR_FORMAT;
if (want > 0) {
rc = read_at(z->fp, data_off, buf, want);
if (rc != MZ_OK) return rc;
}
} else {
size_t made = 0;
rc = inflate_entry(z->fp, data_off, e->comp_size, (uint8_t *)buf, want, &made, 1);
if (rc != MZ_OK) return rc;
if (made != want) return MZ_ERR_FORMAT; /* 声明尺寸内提前结束 = 坏流 */
}
if (out_size) *out_size = want;
return MZ_OK;
}
const char *mz_strerror(int err)
{
switch (err) {