diff --git a/cmd/wuffs-c/internal/cgen/base/base-public.h b/cmd/wuffs-c/internal/cgen/base/base-public.h index 00982806..fd481d5e 100644 --- a/cmd/wuffs-c/internal/cgen/base/base-public.h +++ b/cmd/wuffs-c/internal/cgen/base/base-public.h @@ -990,7 +990,8 @@ typedef struct { } wuffs_base__io_writer; // wuffs_base__io_buffer is a 1-dimensional buffer (a pointer and length), plus -// additional indexes into that buffer, plus an opened / closed flag. +// additional indexes into that buffer, plus a buffer position and an opened / +// closed flag. // // A value with all fields NULL or zero is a valid, empty buffer. typedef struct wuffs_base__io_buffer__struct { @@ -998,6 +999,7 @@ typedef struct wuffs_base__io_buffer__struct { size_t len; // Length. size_t wi; // Write index. Invariant: wi <= len. size_t ri; // Read index. Invariant: ri <= wi. + uint64_t pos; // Position of the buffer start relative to the stream start. bool closed; // No further writes are expected. #ifdef __cplusplus @@ -1015,6 +1017,7 @@ wuffs_base__io_buffer__compact(wuffs_base__io_buffer* buf) { if (!buf || (buf->ri == 0)) { return; } + buf->pos = wuffs_base__u64__sat_add(buf->pos, buf->ri); size_t n = buf->wi - buf->ri; if (n != 0) { memmove(buf->ptr, buf->ptr + buf->ri, n); diff --git a/cmd/wuffs-c/internal/cgen/base/image-public.h b/cmd/wuffs-c/internal/cgen/base/image-public.h index 961c4029..b703fdb2 100644 --- a/cmd/wuffs-c/internal/cgen/base/image-public.h +++ b/cmd/wuffs-c/internal/cgen/base/image-public.h @@ -591,6 +591,8 @@ typedef struct { struct { wuffs_base__rect_ie_u32 bounds; wuffs_base__flicks duration; + uint64_t index; + uint64_t io_position; wuffs_base__animation_blend blend; wuffs_base__animation_disposal disposal; bool palette_changed; @@ -599,6 +601,8 @@ typedef struct { #ifdef __cplusplus inline void update(wuffs_base__rect_ie_u32 bounds, wuffs_base__flicks duration, + uint64_t index, + uint64_t io_position, wuffs_base__animation_blend blend, wuffs_base__animation_disposal disposal, bool palette_changed); @@ -606,6 +610,8 @@ typedef struct { inline uint32_t width(); inline uint32_t height(); inline wuffs_base__flicks duration(); + inline uint64_t index(); + inline uint64_t io_position(); inline wuffs_base__animation_blend blend(); inline wuffs_base__animation_disposal disposal(); inline bool palette_changed(); @@ -617,6 +623,8 @@ static inline void // wuffs_base__frame_config__update(wuffs_base__frame_config* c, wuffs_base__rect_ie_u32 bounds, wuffs_base__flicks duration, + uint64_t index, + uint64_t io_position, wuffs_base__animation_blend blend, wuffs_base__animation_disposal disposal, bool palette_changed) { @@ -626,6 +634,8 @@ wuffs_base__frame_config__update(wuffs_base__frame_config* c, c->private_impl.bounds = bounds; c->private_impl.duration = duration; + c->private_impl.index = index; + c->private_impl.io_position = io_position; c->private_impl.blend = blend; c->private_impl.disposal = disposal; c->private_impl.palette_changed = palette_changed; @@ -653,6 +663,20 @@ wuffs_base__frame_config__duration(wuffs_base__frame_config* c) { return c ? c->private_impl.duration : 0; } +// wuffs_base__frame_config__index returns the index of this frame. The first +// frame in an image has index 0, the second frame has index 1, and so on. +static inline uint64_t // +wuffs_base__frame_config__index(wuffs_base__frame_config* c) { + return c ? c->private_impl.index : 0; +} + +// wuffs_base__frame_config__io_position returns the I/O stream position after +// the frame config but before the frame's pixel data. +static inline uint64_t // +wuffs_base__frame_config__io_position(wuffs_base__frame_config* c) { + return c ? c->private_impl.io_position : 0; +} + // wuffs_base__frame_config__blend returns, for an animated image, how to blend // the transparent pixels of this frame with the existing canvas. static inline wuffs_base__animation_blend // @@ -680,10 +704,13 @@ wuffs_base__frame_config__palette_changed(wuffs_base__frame_config* c) { inline void // wuffs_base__frame_config::update(wuffs_base__rect_ie_u32 bounds, wuffs_base__flicks duration, + uint64_t index, + uint64_t io_position, wuffs_base__animation_blend blend, wuffs_base__animation_disposal disposal, bool palette_changed) { - wuffs_base__frame_config__update(this, bounds, duration, blend, disposal, + wuffs_base__frame_config__update(this, bounds, duration, index, + io_position, blend, disposal, palette_changed); } @@ -707,6 +734,16 @@ wuffs_base__frame_config::duration() { return wuffs_base__frame_config__duration(this); } +inline uint64_t // +wuffs_base__frame_config::index() { + return wuffs_base__frame_config__index(this); +} + +inline uint64_t // +wuffs_base__frame_config::io_position() { + return wuffs_base__frame_config__io_position(this); +} + inline wuffs_base__animation_blend // wuffs_base__frame_config::blend() { return wuffs_base__frame_config__blend(this); diff --git a/cmd/wuffs-c/internal/cgen/builtin.go b/cmd/wuffs-c/internal/cgen/builtin.go index a81e61ee..9617fce0 100644 --- a/cmd/wuffs-c/internal/cgen/builtin.go +++ b/cmd/wuffs-c/internal/cgen/builtin.go @@ -122,6 +122,11 @@ func (g *gen) writeBuiltinIOReader(b *buffer, recv *a.Expr, method t.ID, args [] b.writes("(iop_a_src > io0_a_src)") return nil + case t.IDPosition: + b.printf("(a_src.private_impl.buf ? wuffs_base__u64__sat_add(" + + "a_src.private_impl.buf->pos, iop_a_src - a_src.private_impl.buf->ptr) : 0)") + return nil + case t.IDSetLimit: b.printf("wuffs_base__io_reader__set_limit(&%ssrc, iop_a_src,", aPrefix) // TODO: update the iop variables? @@ -200,6 +205,11 @@ func (g *gen) writeBuiltinIOWriter(b *buffer, recv *a.Expr, method t.ID, args [] b.printf("wuffs_base__io_writer__copy_n_from_slice(&iop_a_dst, io1_a_dst,") return g.writeArgs(b, args, rp, depth) + case t.IDPosition: + b.printf("(a_dst.private_impl.buf ? wuffs_base__u64__sat_add(" + + "a_dst.private_impl.buf->pos, iop_a_dst - a_dst.private_impl.buf->ptr) : 0)") + return nil + case t.IDSetMark: // TODO: is a private_impl.mark the right representation? What // if the function is passed a (ptr io_writer) instead of a diff --git a/cmd/wuffs-c/internal/cgen/data.go b/cmd/wuffs-c/internal/cgen/data.go index 5aa7ae9c..a24a1dfe 100644 --- a/cmd/wuffs-c/internal/cgen/data.go +++ b/cmd/wuffs-c/internal/cgen/data.go @@ -66,8 +66,8 @@ const baseBasePublicH = "" + "base__rect_ie_u32::width() {\n return wuffs_base__rect_ie_u32__width(this);\n}\n\ninline uint32_t //\nwuffs_base__rect_ie_u32::height() {\n return wuffs_base__rect_ie_u32__height(this);\n}\n\n#endif // __cplusplus\n\n" + "" + "// ---------------- I/O\n\nstruct wuffs_base__io_buffer__struct;\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n struct wuffs_base__io_buffer__struct* buf;\n // The bounds values are typically NULL, when created by the Wuffs public\n // API. NULL means that the callee substitutes the implicit bounds derived\n // from buf.\n uint8_t* mark;\n uint8_t* limit;\n } private_impl;\n} wuffs_base__io_reader;\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n struct wuffs_base__io_buffer__struct* buf;\n // The bounds values are typically NULL, when created by the Wuffs public\n // API. NULL means that the callee substitutes the implicit bounds derived\n // from buf.\n uint8_t* mark;\n uint8_t* limit;\n } private_impl;\n} wuffs_base__io_writer;\n\n// wuffs_base__io_buffer is a 1-dimensio" + - "nal buffer (a pointer and length), plus\n// additional indexes into that buffer, plus an opened / closed flag.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct wuffs_base__io_buffer__struct {\n uint8_t* ptr; // Pointer.\n size_t len; // Length.\n size_t wi; // Write index. Invariant: wi <= len.\n size_t ri; // Read index. Invariant: ri <= wi.\n bool closed; // No further writes are expected.\n\n#ifdef __cplusplus\n inline void compact();\n inline wuffs_base__io_reader reader();\n inline wuffs_base__io_writer writer();\n#endif // __cplusplus\n\n} wuffs_base__io_buffer;\n\n// wuffs_base__io_buffer__compact moves any written but unread bytes to the\n// start of the buffer.\nstatic inline void //\nwuffs_base__io_buffer__compact(wuffs_base__io_buffer* buf) {\n if (!buf || (buf->ri == 0)) {\n return;\n }\n size_t n = buf->wi - buf->ri;\n if (n != 0) {\n memmove(buf->ptr, buf->ptr + buf->ri, n);\n }\n buf->wi = n;\n buf->ri = 0;\n}\n\nstatic inline wuffs_base__io_reader //\n" + - "wuffs_base__io_buffer__reader(wuffs_base__io_buffer* buf) {\n wuffs_base__io_reader ret = ((wuffs_base__io_reader){});\n ret.private_impl.buf = buf;\n return ret;\n}\n\nstatic inline wuffs_base__io_writer //\nwuffs_base__io_buffer__writer(wuffs_base__io_buffer* buf) {\n wuffs_base__io_writer ret = ((wuffs_base__io_writer){});\n ret.private_impl.buf = buf;\n return ret;\n}\n\n#ifdef __cplusplus\n\ninline void //\nwuffs_base__io_buffer__struct::compact() {\n wuffs_base__io_buffer__compact(this);\n}\n\ninline wuffs_base__io_reader //\nwuffs_base__io_buffer__struct::reader() {\n return wuffs_base__io_buffer__reader(this);\n}\n\ninline wuffs_base__io_writer //\nwuffs_base__io_buffer__struct::writer() {\n return wuffs_base__io_buffer__writer(this);\n}\n\n#endif // __cplusplus\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n" + + "nal buffer (a pointer and length), plus\n// additional indexes into that buffer, plus a buffer position and an opened /\n// closed flag.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef struct wuffs_base__io_buffer__struct {\n uint8_t* ptr; // Pointer.\n size_t len; // Length.\n size_t wi; // Write index. Invariant: wi <= len.\n size_t ri; // Read index. Invariant: ri <= wi.\n uint64_t pos; // Position of the buffer start relative to the stream start.\n bool closed; // No further writes are expected.\n\n#ifdef __cplusplus\n inline void compact();\n inline wuffs_base__io_reader reader();\n inline wuffs_base__io_writer writer();\n#endif // __cplusplus\n\n} wuffs_base__io_buffer;\n\n// wuffs_base__io_buffer__compact moves any written but unread bytes to the\n// start of the buffer.\nstatic inline void //\nwuffs_base__io_buffer__compact(wuffs_base__io_buffer* buf) {\n if (!buf || (buf->ri == 0)) {\n return;\n }\n buf->pos = wuffs_base__u64__sat_add(buf->pos, buf->ri);\n size_t " + + "n = buf->wi - buf->ri;\n if (n != 0) {\n memmove(buf->ptr, buf->ptr + buf->ri, n);\n }\n buf->wi = n;\n buf->ri = 0;\n}\n\nstatic inline wuffs_base__io_reader //\nwuffs_base__io_buffer__reader(wuffs_base__io_buffer* buf) {\n wuffs_base__io_reader ret = ((wuffs_base__io_reader){});\n ret.private_impl.buf = buf;\n return ret;\n}\n\nstatic inline wuffs_base__io_writer //\nwuffs_base__io_buffer__writer(wuffs_base__io_buffer* buf) {\n wuffs_base__io_writer ret = ((wuffs_base__io_writer){});\n ret.private_impl.buf = buf;\n return ret;\n}\n\n#ifdef __cplusplus\n\ninline void //\nwuffs_base__io_buffer__struct::compact() {\n wuffs_base__io_buffer__compact(this);\n}\n\ninline wuffs_base__io_reader //\nwuffs_base__io_buffer__struct::reader() {\n return wuffs_base__io_buffer__reader(this);\n}\n\ninline wuffs_base__io_writer //\nwuffs_base__io_buffer__struct::writer() {\n return wuffs_base__io_buffer__writer(this);\n}\n\n#endif // __cplusplus\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n" + "" const baseBasePrivateH = "" + @@ -146,11 +146,12 @@ const baseImagePublicH = "" + "" + "// --------\n\n// wuffs_base__animation_disposal encodes, for an animated image, how to\n// dispose of a frame after displaying it:\n// - None means to draw the next frame on top of this one.\n// - Restore Background means to clear the frame's dirty rectangle to \"the\n// background color\" (in practice, this means transparent black) before\n// drawing the next frame.\n// - Restore Previous means to undo the current frame, so that the next frame\n// is drawn on top of the previous one.\ntypedef uint8_t wuffs_base__animation_disposal;\n\n#define WUFFS_BASE__ANIMATION_DISPOSAL__NONE ((wuffs_base__animation_disposal)0)\n#define WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_BACKGROUND \\\n ((wuffs_base__animation_disposal)1)\n#define WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS \\\n ((wuffs_base__animation_disposal)2)\n\n" + "" + - "// --------\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__rect_ie_u32 bounds;\n wuffs_base__flicks duration;\n wuffs_base__animation_blend blend;\n wuffs_base__animation_disposal disposal;\n bool palette_changed;\n } private_impl;\n\n#ifdef __cplusplus\n inline void update(wuffs_base__rect_ie_u32 bounds,\n wuffs_base__flicks duration,\n wuffs_base__animation_blend blend,\n wuffs_base__animation_disposal disposal,\n bool palette_changed);\n inline wuffs_base__rect_ie_u32 bounds();\n inline uint32_t width();\n inline uint32_t height();\n inline wuffs_base__flicks duration();\n inline wuffs_base__animation_blend blend();\n inline wuffs_base__animation_disposal disposal();\n inline bool palette_changed();\n#endif // __cplusplus\n\n} wuffs_base__frame_config;\n\nstatic inline void //\nwuffs_base__frame_config__up" + - "date(wuffs_base__frame_config* c,\n wuffs_base__rect_ie_u32 bounds,\n wuffs_base__flicks duration,\n wuffs_base__animation_blend blend,\n wuffs_base__animation_disposal disposal,\n bool palette_changed) {\n if (!c) {\n return;\n }\n\n c->private_impl.bounds = bounds;\n c->private_impl.duration = duration;\n c->private_impl.blend = blend;\n c->private_impl.disposal = disposal;\n c->private_impl.palette_changed = palette_changed;\n}\n\nstatic inline wuffs_base__rect_ie_u32 //\nwuffs_base__frame_config__bounds(wuffs_base__frame_config* c) {\n return c ? c->private_impl.bounds : ((wuffs_base__rect_ie_u32){});\n}\n\nstatic inline uint32_t //\nwuffs_base__frame_config__width(wuffs_base__frame_config* c) {\n return c ? wuffs_base__rect_ie_u32__width(&c->private_impl.bounds) : 0;\n}\n\nstatic inline uint32_t //\nwuffs_base__frame_config__height(wuffs_base__frame_config* c)" + - " {\n return c ? wuffs_base__rect_ie_u32__height(&c->private_impl.bounds) : 0;\n}\n\n// wuffs_base__frame_config__duration returns the amount of time to display\n// this frame. Zero means to display forever - a still (non-animated) image.\nstatic inline wuffs_base__flicks //\nwuffs_base__frame_config__duration(wuffs_base__frame_config* c) {\n return c ? c->private_impl.duration : 0;\n}\n\n// wuffs_base__frame_config__blend returns, for an animated image, how to blend\n// the transparent pixels of this frame with the existing canvas.\nstatic inline wuffs_base__animation_blend //\nwuffs_base__frame_config__blend(wuffs_base__frame_config* c) {\n return c ? c->private_impl.blend : 0;\n}\n\n// wuffs_base__frame_config__disposal returns, for an animated image, how to\n// dispose of this frame after displaying it.\nstatic inline wuffs_base__animation_disposal //\nwuffs_base__frame_config__disposal(wuffs_base__frame_config* c) {\n return c ? c->private_impl.disposal : 0;\n}\n\n// wuffs_base__frame_config__palette_changed returns whethe" + - "r this frame's\n// palette differs from the previous frame. It is conservative and may return\n// false positives (but never false negatives).\nstatic inline bool //\nwuffs_base__frame_config__palette_changed(wuffs_base__frame_config* c) {\n return c && c->private_impl.palette_changed;\n}\n\n#ifdef __cplusplus\n\ninline void //\nwuffs_base__frame_config::update(wuffs_base__rect_ie_u32 bounds,\n wuffs_base__flicks duration,\n wuffs_base__animation_blend blend,\n wuffs_base__animation_disposal disposal,\n bool palette_changed) {\n wuffs_base__frame_config__update(this, bounds, duration, blend, disposal,\n palette_changed);\n}\n\ninline wuffs_base__rect_ie_u32 //\nwuffs_base__frame_config::bounds() {\n return wuffs_base__frame_config__bounds(this);\n}\n\ninline uint32_t //\nwuffs_base__frame_config::width() {\n return wuffs_base__frame_config__width(this);\n}\n\ninline uint3" + - "2_t //\nwuffs_base__frame_config::height() {\n return wuffs_base__frame_config__height(this);\n}\n\ninline wuffs_base__flicks //\nwuffs_base__frame_config::duration() {\n return wuffs_base__frame_config__duration(this);\n}\n\ninline wuffs_base__animation_blend //\nwuffs_base__frame_config::blend() {\n return wuffs_base__frame_config__blend(this);\n}\n\ninline wuffs_base__animation_disposal //\nwuffs_base__frame_config::disposal() {\n return wuffs_base__frame_config__disposal(this);\n}\n\ninline bool //\nwuffs_base__frame_config::palette_changed() {\n return wuffs_base__frame_config__palette_changed(this);\n}\n\n#endif // __cplusplus\n\n" + + "// --------\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__rect_ie_u32 bounds;\n wuffs_base__flicks duration;\n uint64_t index;\n uint64_t io_position;\n wuffs_base__animation_blend blend;\n wuffs_base__animation_disposal disposal;\n bool palette_changed;\n } private_impl;\n\n#ifdef __cplusplus\n inline void update(wuffs_base__rect_ie_u32 bounds,\n wuffs_base__flicks duration,\n uint64_t index,\n uint64_t io_position,\n wuffs_base__animation_blend blend,\n wuffs_base__animation_disposal disposal,\n bool palette_changed);\n inline wuffs_base__rect_ie_u32 bounds();\n inline uint32_t width();\n inline uint32_t height();\n inline wuffs_base__flicks duration();\n inline uint64_t index();\n inline uint64_t io_position();\n inline wuffs_base__animation_blend blend();\n i" + + "nline wuffs_base__animation_disposal disposal();\n inline bool palette_changed();\n#endif // __cplusplus\n\n} wuffs_base__frame_config;\n\nstatic inline void //\nwuffs_base__frame_config__update(wuffs_base__frame_config* c,\n wuffs_base__rect_ie_u32 bounds,\n wuffs_base__flicks duration,\n uint64_t index,\n uint64_t io_position,\n wuffs_base__animation_blend blend,\n wuffs_base__animation_disposal disposal,\n bool palette_changed) {\n if (!c) {\n return;\n }\n\n c->private_impl.bounds = bounds;\n c->private_impl.duration = duration;\n c->private_impl.index = index;\n c->private_impl.io_position = io_position;\n c->private_impl.blend = blend;\n c->private_impl.disposal = disposal;\n c->private_impl.palette_changed = palette_changed;\n}\n\nstatic inline wuffs_base__rect_ie_u32 //\nwuffs_base__frame_confi" + + "g__bounds(wuffs_base__frame_config* c) {\n return c ? c->private_impl.bounds : ((wuffs_base__rect_ie_u32){});\n}\n\nstatic inline uint32_t //\nwuffs_base__frame_config__width(wuffs_base__frame_config* c) {\n return c ? wuffs_base__rect_ie_u32__width(&c->private_impl.bounds) : 0;\n}\n\nstatic inline uint32_t //\nwuffs_base__frame_config__height(wuffs_base__frame_config* c) {\n return c ? wuffs_base__rect_ie_u32__height(&c->private_impl.bounds) : 0;\n}\n\n// wuffs_base__frame_config__duration returns the amount of time to display\n// this frame. Zero means to display forever - a still (non-animated) image.\nstatic inline wuffs_base__flicks //\nwuffs_base__frame_config__duration(wuffs_base__frame_config* c) {\n return c ? c->private_impl.duration : 0;\n}\n\n// wuffs_base__frame_config__index returns the index of this frame. The first\n// frame in an image has index 0, the second frame has index 1, and so on.\nstatic inline uint64_t //\nwuffs_base__frame_config__index(wuffs_base__frame_config* c) {\n return c ? c->private_impl.i" + + "ndex : 0;\n}\n\n// wuffs_base__frame_config__io_position returns the I/O stream position after\n// the frame config but before the frame's pixel data.\nstatic inline uint64_t //\nwuffs_base__frame_config__io_position(wuffs_base__frame_config* c) {\n return c ? c->private_impl.io_position : 0;\n}\n\n// wuffs_base__frame_config__blend returns, for an animated image, how to blend\n// the transparent pixels of this frame with the existing canvas.\nstatic inline wuffs_base__animation_blend //\nwuffs_base__frame_config__blend(wuffs_base__frame_config* c) {\n return c ? c->private_impl.blend : 0;\n}\n\n// wuffs_base__frame_config__disposal returns, for an animated image, how to\n// dispose of this frame after displaying it.\nstatic inline wuffs_base__animation_disposal //\nwuffs_base__frame_config__disposal(wuffs_base__frame_config* c) {\n return c ? c->private_impl.disposal : 0;\n}\n\n// wuffs_base__frame_config__palette_changed returns whether this frame's\n// palette differs from the previous frame. It is conservative and may retur" + + "n\n// false positives (but never false negatives).\nstatic inline bool //\nwuffs_base__frame_config__palette_changed(wuffs_base__frame_config* c) {\n return c && c->private_impl.palette_changed;\n}\n\n#ifdef __cplusplus\n\ninline void //\nwuffs_base__frame_config::update(wuffs_base__rect_ie_u32 bounds,\n wuffs_base__flicks duration,\n uint64_t index,\n uint64_t io_position,\n wuffs_base__animation_blend blend,\n wuffs_base__animation_disposal disposal,\n bool palette_changed) {\n wuffs_base__frame_config__update(this, bounds, duration, index,\n io_position, blend, disposal,\n palette_changed);\n}\n\ninline wuffs_base__rect_ie_u32 //\nwuffs_base__frame_config::bounds() {\n return wuffs_base__frame_config__bounds(this);\n}\n\ninline uint32_t //\nwuffs_base__frame_config::width" + + "() {\n return wuffs_base__frame_config__width(this);\n}\n\ninline uint32_t //\nwuffs_base__frame_config::height() {\n return wuffs_base__frame_config__height(this);\n}\n\ninline wuffs_base__flicks //\nwuffs_base__frame_config::duration() {\n return wuffs_base__frame_config__duration(this);\n}\n\ninline uint64_t //\nwuffs_base__frame_config::index() {\n return wuffs_base__frame_config__index(this);\n}\n\ninline uint64_t //\nwuffs_base__frame_config::io_position() {\n return wuffs_base__frame_config__io_position(this);\n}\n\ninline wuffs_base__animation_blend //\nwuffs_base__frame_config::blend() {\n return wuffs_base__frame_config__blend(this);\n}\n\ninline wuffs_base__animation_disposal //\nwuffs_base__frame_config::disposal() {\n return wuffs_base__frame_config__disposal(this);\n}\n\ninline bool //\nwuffs_base__frame_config::palette_changed() {\n return wuffs_base__frame_config__palette_changed(this);\n}\n\n#endif // __cplusplus\n\n" + "" + "// --------\n\ntypedef struct {\n wuffs_base__pixel_config pixcfg;\n\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__table_u8 planes[WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX];\n uint8_t palette[1024];\n // TODO: color spaces.\n } private_impl;\n\n#ifdef __cplusplus\n inline wuffs_base__status set_from_slice(wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory);\n inline void set_palette(wuffs_base__slice_u8 palette);\n inline wuffs_base__table_u8 plane(uint32_t p);\n inline wuffs_base__slice_u8 palette();\n#endif // __cplusplus\n\n} wuffs_base__pixel_buffer;\n\nstatic inline wuffs_base__status //\nwuffs_base__pixel_buffer__set_from_slice(wuffs_base__pixel_buffer* b,\n wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory) {\n if (!b) {\n return wuffs_base_" + "_error__bad_receiver;\n }\n *b = ((wuffs_base__pixel_buffer){});\n if (!pixcfg) {\n return wuffs_base__error__bad_argument;\n }\n // TODO: don't assume 1 byte per pixel. Don't assume packed.\n uint64_t wh = ((uint64_t)pixcfg->private_impl.width) *\n ((uint64_t)pixcfg->private_impl.height);\n if (wh > pixbuf_memory.len) {\n return wuffs_base__error__bad_argument_length_too_short;\n }\n b->pixcfg = *pixcfg;\n wuffs_base__table_u8* tab = &b->private_impl.planes[0];\n tab->ptr = pixbuf_memory.ptr;\n tab->width = pixcfg->private_impl.width;\n tab->height = pixcfg->private_impl.height;\n tab->stride = pixcfg->private_impl.width;\n return NULL;\n}\n\n// The palette argument is ignored unless its length is exactly 1024.\nstatic inline void //\nwuffs_base__pixel_buffer__set_palette(wuffs_base__pixel_buffer* b,\n wuffs_base__slice_u8 palette) {\n if (b && palette.ptr && (palette.len == 1024)) {\n memmove(b->private_impl.palette, palette.ptr, 1024);\n }\n}\n\nstatic inlin" + diff --git a/doc/changelog.md b/doc/changelog.md index 2157555b..0ff0993b 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -29,6 +29,7 @@ - Added `{frame,image,pixel}_config` and `pixel_buffer` types. - Added a `reset` method. - Added `peek_uxx`, `skip_fast` and `write_fast_uxx` methods. +- Added I/O positions. - Tweaked how marks and limits work. - Supported animated (not just single frame) and interlaced GIFs. - Marked the `std/gif` LZW decoder as private. diff --git a/example/gifplayer/gifplayer.c b/example/gifplayer/gifplayer.c index 4f198240..e23f113b 100644 --- a/example/gifplayer/gifplayer.c +++ b/example/gifplayer/gifplayer.c @@ -278,7 +278,12 @@ const char* play() { } wuffs_base__io_buffer src = ((wuffs_base__io_buffer){ - .ptr = src_buffer, .len = src_len, .wi = src_len, .closed = true}); + .ptr = src_buffer, + .len = src_len, + .wi = src_len, + .pos = 0, + .closed = true, + }); wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src); if (first_play) { diff --git a/example/library/library.c b/example/library/library.c index 7991f50d..310c15e7 100644 --- a/example/library/library.c +++ b/example/library/library.c @@ -54,6 +54,7 @@ static const char* decode() { .ptr = lgtm_ptr, .len = lgtm_len, .wi = lgtm_len, + .pos = 0, .closed = true, }); wuffs_base__io_writer dst_writer = wuffs_base__io_buffer__writer(&dst); diff --git a/example/zcat/zcat.c b/example/zcat/zcat.c index 2918891e..badfe498 100644 --- a/example/zcat/zcat.c +++ b/example/zcat/zcat.c @@ -81,6 +81,8 @@ static const char* decode() { return z; } + wuffs_base__io_buffer dst = + ((wuffs_base__io_buffer){.ptr = dst_buffer, .len = DST_BUFFER_SIZE}); wuffs_base__io_buffer src = ((wuffs_base__io_buffer){.ptr = src_buffer, .len = SRC_BUFFER_SIZE}); @@ -99,9 +101,6 @@ static const char* decode() { } while (true) { - wuffs_base__io_buffer dst = - ((wuffs_base__io_buffer){.ptr = dst_buffer, .len = DST_BUFFER_SIZE}); - wuffs_base__status z = wuffs_gzip__decoder__decode(&dec, wuffs_base__io_buffer__writer(&dst), wuffs_base__io_buffer__reader(&src)); @@ -110,6 +109,8 @@ static const char* decode() { // TODO: handle EINTR and other write errors; see "man 2 write". const int stdout_fd = 1; ignore_return_value(write(stdout_fd, dst_buffer, dst.wi)); + dst.ri = dst.wi; + wuffs_base__io_buffer__compact(&dst); } if (z == wuffs_base__suspension__short_read) { diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index eb6fd500..55d43eb6 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -148,6 +148,7 @@ var Funcs = []string{ "io_reader.peek_u64le() u64", "io_reader.available() u64", + "io_reader.position() u64", "io_reader.set!(s slice u8, closed bool)", "io_reader.set_limit!(l u64)", "io_reader.set_mark!()", @@ -201,6 +202,7 @@ var Funcs = []string{ "io_writer.write_fast_u64le!(x u64)", "io_writer.available() u64", + "io_writer.position() u64", "io_writer.set!(s slice u8)", "io_writer.set_limit!(l u64)", "io_writer.set_mark!()", @@ -228,7 +230,7 @@ var Funcs = []string{ // Duration's upper bound is the maximum possible i64 value. "frame_config.update!(bounds rect_ie_u32, duration u64[..0x7FFFFFFFFFFFFFFF], " + - "blend u8, disposal u8, palette_changed bool)", + "index u64, io_position u64, blend u8, disposal u8, palette_changed bool)", // ---- image_config diff --git a/lang/token/list.go b/lang/token/list.go index 9957112d..59702a4a 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -513,11 +513,12 @@ const ( IDWriteFastU64LE = ID(0x17F) IDCanUndoByte = ID(0x180) - IDSetLimit = ID(0x181) - IDSetMark = ID(0x182) - IDSinceMark = ID(0x183) - IDSkip = ID(0x184) - IDSkipFast = ID(0x185) + IDPosition = ID(0x181) + IDSetLimit = ID(0x182) + IDSetMark = ID(0x183) + IDSinceMark = ID(0x184) + IDSkip = ID(0x185) + IDSkipFast = ID(0x186) IDCopyFromSlice = ID(0x190) IDCopyNFromHistory = ID(0x191) @@ -796,6 +797,7 @@ var builtInsByID = [nBuiltInIDs]string{ IDWriteFastU64LE: "write_fast_u64le", IDCanUndoByte: "can_undo_byte", + IDPosition: "position", IDSetLimit: "set_limit", IDSetMark: "set_mark", IDSinceMark: "since_mark", diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index eb60bdbe..dda29a1a 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -1026,7 +1026,8 @@ typedef struct { } wuffs_base__io_writer; // wuffs_base__io_buffer is a 1-dimensional buffer (a pointer and length), plus -// additional indexes into that buffer, plus an opened / closed flag. +// additional indexes into that buffer, plus a buffer position and an opened / +// closed flag. // // A value with all fields NULL or zero is a valid, empty buffer. typedef struct wuffs_base__io_buffer__struct { @@ -1034,6 +1035,7 @@ typedef struct wuffs_base__io_buffer__struct { size_t len; // Length. size_t wi; // Write index. Invariant: wi <= len. size_t ri; // Read index. Invariant: ri <= wi. + uint64_t pos; // Position of the buffer start relative to the stream start. bool closed; // No further writes are expected. #ifdef __cplusplus @@ -1051,6 +1053,7 @@ wuffs_base__io_buffer__compact(wuffs_base__io_buffer* buf) { if (!buf || (buf->ri == 0)) { return; } + buf->pos = wuffs_base__u64__sat_add(buf->pos, buf->ri); size_t n = buf->wi - buf->ri; if (n != 0) { memmove(buf->ptr, buf->ptr + buf->ri, n); @@ -1687,6 +1690,8 @@ typedef struct { struct { wuffs_base__rect_ie_u32 bounds; wuffs_base__flicks duration; + uint64_t index; + uint64_t io_position; wuffs_base__animation_blend blend; wuffs_base__animation_disposal disposal; bool palette_changed; @@ -1695,6 +1700,8 @@ typedef struct { #ifdef __cplusplus inline void update(wuffs_base__rect_ie_u32 bounds, wuffs_base__flicks duration, + uint64_t index, + uint64_t io_position, wuffs_base__animation_blend blend, wuffs_base__animation_disposal disposal, bool palette_changed); @@ -1702,6 +1709,8 @@ typedef struct { inline uint32_t width(); inline uint32_t height(); inline wuffs_base__flicks duration(); + inline uint64_t index(); + inline uint64_t io_position(); inline wuffs_base__animation_blend blend(); inline wuffs_base__animation_disposal disposal(); inline bool palette_changed(); @@ -1713,6 +1722,8 @@ static inline void // wuffs_base__frame_config__update(wuffs_base__frame_config* c, wuffs_base__rect_ie_u32 bounds, wuffs_base__flicks duration, + uint64_t index, + uint64_t io_position, wuffs_base__animation_blend blend, wuffs_base__animation_disposal disposal, bool palette_changed) { @@ -1722,6 +1733,8 @@ wuffs_base__frame_config__update(wuffs_base__frame_config* c, c->private_impl.bounds = bounds; c->private_impl.duration = duration; + c->private_impl.index = index; + c->private_impl.io_position = io_position; c->private_impl.blend = blend; c->private_impl.disposal = disposal; c->private_impl.palette_changed = palette_changed; @@ -1749,6 +1762,20 @@ wuffs_base__frame_config__duration(wuffs_base__frame_config* c) { return c ? c->private_impl.duration : 0; } +// wuffs_base__frame_config__index returns the index of this frame. The first +// frame in an image has index 0, the second frame has index 1, and so on. +static inline uint64_t // +wuffs_base__frame_config__index(wuffs_base__frame_config* c) { + return c ? c->private_impl.index : 0; +} + +// wuffs_base__frame_config__io_position returns the I/O stream position after +// the frame config but before the frame's pixel data. +static inline uint64_t // +wuffs_base__frame_config__io_position(wuffs_base__frame_config* c) { + return c ? c->private_impl.io_position : 0; +} + // wuffs_base__frame_config__blend returns, for an animated image, how to blend // the transparent pixels of this frame with the existing canvas. static inline wuffs_base__animation_blend // @@ -1776,11 +1803,13 @@ wuffs_base__frame_config__palette_changed(wuffs_base__frame_config* c) { inline void // wuffs_base__frame_config::update(wuffs_base__rect_ie_u32 bounds, wuffs_base__flicks duration, + uint64_t index, + uint64_t io_position, wuffs_base__animation_blend blend, wuffs_base__animation_disposal disposal, bool palette_changed) { - wuffs_base__frame_config__update(this, bounds, duration, blend, disposal, - palette_changed); + wuffs_base__frame_config__update(this, bounds, duration, index, io_position, + blend, disposal, palette_changed); } inline wuffs_base__rect_ie_u32 // @@ -1803,6 +1832,16 @@ wuffs_base__frame_config::duration() { return wuffs_base__frame_config__duration(this); } +inline uint64_t // +wuffs_base__frame_config::index() { + return wuffs_base__frame_config__index(this); +} + +inline uint64_t // +wuffs_base__frame_config::io_position() { + return wuffs_base__frame_config__io_position(this); +} + inline wuffs_base__animation_blend // wuffs_base__frame_config::blend() { return wuffs_base__frame_config__blend(this); @@ -6441,6 +6480,22 @@ wuffs_gif__decoder__decode_frame_config(wuffs_gif__decoder* self, uint8_t v_blend; + uint8_t* iop_a_src = NULL; + uint8_t* io0_a_src = NULL; + uint8_t* io1_a_src = NULL; + WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(io0_a_src); + WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(io1_a_src); + if (a_src.private_impl.buf) { + iop_a_src = a_src.private_impl.buf->ptr + a_src.private_impl.buf->ri; + if (!a_src.private_impl.mark) { + a_src.private_impl.mark = iop_a_src; + a_src.private_impl.limit = + a_src.private_impl.buf->ptr + a_src.private_impl.buf->wi; + } + io0_a_src = a_src.private_impl.mark; + io1_a_src = a_src.private_impl.limit; + } + uint32_t coro_susp_point = self->private_impl.c_decode_frame_config[0].coro_susp_point; if (coro_susp_point) { @@ -6453,20 +6508,40 @@ wuffs_gif__decoder__decode_frame_config(wuffs_gif__decoder* self, if (!self->private_impl.f_end_of_data) { if (self->private_impl.f_call_sequence == 0) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + if (a_src.private_impl.buf) { + a_src.private_impl.buf->ri = iop_a_src - a_src.private_impl.buf->ptr; + } status = wuffs_gif__decoder__decode_image_config(self, NULL, a_src); + if (a_src.private_impl.buf) { + iop_a_src = a_src.private_impl.buf->ptr + a_src.private_impl.buf->ri; + } if (status) { goto suspend; } } else if (self->private_impl.f_call_sequence != 1) { if (self->private_impl.f_call_sequence == 2) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + if (a_src.private_impl.buf) { + a_src.private_impl.buf->ri = + iop_a_src - a_src.private_impl.buf->ptr; + } status = wuffs_gif__decoder__skip_frame(self, a_src); + if (a_src.private_impl.buf) { + iop_a_src = + a_src.private_impl.buf->ptr + a_src.private_impl.buf->ri; + } if (status) { goto suspend; } } WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); + if (a_src.private_impl.buf) { + a_src.private_impl.buf->ri = iop_a_src - a_src.private_impl.buf->ptr; + } status = wuffs_gif__decoder__decode_up_to_id_part1(self, a_src); + if (a_src.private_impl.buf) { + iop_a_src = a_src.private_impl.buf->ptr + a_src.private_impl.buf->ri; + } if (status) { goto suspend; } @@ -6501,8 +6576,13 @@ wuffs_gif__decoder__decode_frame_config(wuffs_gif__decoder* self, self->private_impl.f_frame_rect_y0, self->private_impl.f_frame_rect_x1, self->private_impl.f_frame_rect_y1), - self->private_impl.f_gc_duration, v_blend, - self->private_impl.f_gc_disposal, + self->private_impl.f_gc_duration, + self->private_impl.f_num_decoded_frame_configs_value, + (a_src.private_impl.buf ? wuffs_base__u64__sat_add( + a_src.private_impl.buf->pos, + iop_a_src - a_src.private_impl.buf->ptr) + : 0), + v_blend, self->private_impl.f_gc_disposal, (self->private_impl.f_which_palette != 2)); } wuffs_base__u64__sat_add_indirect( @@ -6522,6 +6602,10 @@ suspend: goto exit; exit: + if (a_src.private_impl.buf) { + a_src.private_impl.buf->ri = iop_a_src - a_src.private_impl.buf->ptr; + } + if (wuffs_base__status__is_error(status)) { self->private_impl.magic = WUFFS_BASE__DISABLED; } diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 5c163c7f..fc7fde28 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -213,6 +213,8 @@ pub func decoder.decode_frame_config!??(dst nptr base.frame_config, src base.io_ max_excl_x:this.frame_rect_x1, max_excl_y:this.frame_rect_y1), duration:this.gc_duration, + index:this.num_decoded_frame_configs_value, + io_position:args.src.position(), blend:blend, disposal:this.gc_disposal, palette_changed:this.which_palette != 2) diff --git a/test/c/std/gif.c b/test/c/std/gif.c index 4a5e142c..9f12eabc 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -791,6 +791,86 @@ void test_wuffs_gif_num_decoded_frames() { do_test_wuffs_gif_num_decoded(false); } +bool do_test_wuffs_gif_io_position(bool chunked) { + wuffs_base__io_buffer src = + ((wuffs_base__io_buffer){.ptr = global_src_buffer, .len = BUFFER_SIZE}); + if (!read_file(&src, "../../data/animated-red-blue.gif")) { + return false; + } + + wuffs_gif__decoder dec = ((wuffs_gif__decoder){}); + wuffs_base__status z = + wuffs_gif__decoder__check_wuffs_version(&dec, sizeof dec, WUFFS_VERSION); + if (z) { + FAIL("check_wuffs_version: \"%s\"", z); + return false; + } + + wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src); + + if (chunked) { + if (src.wi < 20) { + FAIL("src is too short"); + return false; + } + size_t saved_wi = src.wi; + bool saved_closed = src.closed; + src.wi = 10; + src.closed = false; + + z = wuffs_gif__decoder__decode_image_config(&dec, NULL, src_reader); + if (z != wuffs_base__suspension__short_read) { + FAIL("decode_image_config (chunked): \"%s\"", z); + return false; + } + + src.wi = saved_wi; + src.closed = saved_closed; + wuffs_base__io_buffer__compact(&src); + } + + z = wuffs_gif__decoder__decode_image_config(&dec, NULL, src_reader); + if (z) { + FAIL("decode_image_config: \"%s\"", z); + return false; + } + + uint64_t wants[4] = {1586, 2144, 2205, 2560}; + int i; + for (i = 0; i < 4; i++) { + wuffs_base__frame_config fc = ((wuffs_base__frame_config){}); + z = wuffs_gif__decoder__decode_frame_config(&dec, &fc, src_reader); + if (z) { + FAIL("decode_frame_config #%d: \"%s\"", i, z); + return false; + } + uint64_t got = wuffs_base__frame_config__io_position(&fc); + uint64_t want = wants[i]; + if (got != want) { + FAIL("decode_frame_config #%d: got %" PRIu64 ", want %" PRIu64, i, got, + want); + return false; + } + } + + z = wuffs_gif__decoder__decode_frame_config(&dec, NULL, src_reader); + if (z != wuffs_base__suspension__end_of_data) { + FAIL("decode_frame_config EOD: \"%s\"", z); + return false; + } + return true; +} + +void test_wuffs_gif_io_position_one_chunk() { + CHECK_FOCUS(__func__); + do_test_wuffs_gif_io_position(false); +} + +void test_wuffs_gif_io_position_two_chunks() { + CHECK_FOCUS(__func__); + do_test_wuffs_gif_io_position(true); +} + // ---------------- Mimic Tests #ifdef WUFFS_MIMIC @@ -824,7 +904,8 @@ bool do_test_mimic_gif_decode(const char* filename) { return false; } - // TODO: check the palette RGB values, not just the palette indexes (pixels). + // TODO: check the palette RGB values, not just the palette indexes + // (pixels). return true; } @@ -1011,6 +1092,8 @@ proc tests[] = { test_wuffs_gif_decode_input_is_a_png, // test_wuffs_gif_num_decoded_frame_configs, // test_wuffs_gif_num_decoded_frames, // + test_wuffs_gif_io_position_one_chunk, // + test_wuffs_gif_io_position_two_chunks, // #ifdef WUFFS_MIMIC diff --git a/test/c/testlib/testlib.c b/test/c/testlib/testlib.c index 4b058cc3..e0b52245 100644 --- a/test/c/testlib/testlib.c +++ b/test/c/testlib/testlib.c @@ -407,6 +407,7 @@ bool read_file(wuffs_base__io_buffer* dst, const char* path) { return false; } fclose(f); + dst->pos = 0; dst->closed = true; return true; }