Skip to content

feat: Allow incremental / chunk-based input in spng stream API #279

@RodrigoDornelles

Description

@RodrigoDornelles

Allow incremental / chunk-based input in spng stream API

spng_set_png_stream() currently assumes blocking input and requires the read callback to fully satisfy each read request or signal EOF.

In loaders such as Lua, the developer explicitly controls how many bytes are fed to the decoder per step, enabling true chunk-based decoding. This model fits naturally with asynchronous scenarios (TCP, sockets, event loops), where PNG data arrives progressively and decoding should advance as buffers are received, without buffering the entire file.

Allowing the stream callback to report how many bytes were actually read—without treating incomplete buffers as EOF—would enable progressive read/write decoding and provide a clear advantage over libpng, which does not support this workflow.

Example (Lua PUC)

static const char *reader(lua_State *L, void *usr size_t *size) {
    buffer_t *data = (buffer_t*) usr;
    size_t n = fread(data->buf, 1, sizeof(data->buf), data->fp);
    *size = n;
    return n ? data->buf : NULL;
}

Example (SPNG)

static int reader(spng_ctx *ctx, void *usr, void *dst, size_t size)
{
    stream_t *s = usr;

    size_t avail = s->len - s->offset;
    if (avail == 0)
        return SPNG_IO_EOF;

    if (size > avail)
        size = avail;

    memcpy(dst, s->data + s->offset, size);
    s->offset += size;

    return size; /* report bytes actually read */
}

related #223 #39 #40 #35

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions