From 2301c763c36f77b1a2fa42b4fae915ddb94e3db0 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 18 May 2018 09:42:24 +1000 Subject: [PATCH] Add a base.image_buffer arg to decode_frame --- example/gifplayer/gifplayer.c | 2 +- fuzz/c/std/gif_fuzzer.c | 2 +- gen/c/std/gif.c | 17 +++++++++++++---- gen/h/std/gif.h | 8 +++++--- lang/builtin/builtin.go | 20 ++++++++++++-------- lang/check/resolve.go | 21 +++++++++++++-------- lang/token/list.go | 4 +++- std/gif/decode_gif.wuffs | 6 +++--- test/c/std/gif.c | 14 ++++++++------ 9 files changed, 59 insertions(+), 35 deletions(-) diff --git a/example/gifplayer/gifplayer.c b/example/gifplayer/gifplayer.c index 12c0ca4d..a01af5e1 100644 --- a/example/gifplayer/gifplayer.c +++ b/example/gifplayer/gifplayer.c @@ -119,7 +119,7 @@ const char* play() { wuffs_base__io_writer dst_writer = wuffs_base__io_buffer__writer(&dst); // TODO: provide API and support for when the frame rect is different from // the image rect. - s = wuffs_gif__decoder__decode_frame(&dec, dst_writer, src_reader); + s = wuffs_gif__decoder__decode_frame(&dec, &ib, dst_writer, src_reader); if (s) { if (s == WUFFS_GIF__SUSPENSION_END_OF_DATA) { break; diff --git a/fuzz/c/std/gif_fuzzer.c b/fuzz/c/std/gif_fuzzer.c index 148b1981..42e88c41 100644 --- a/fuzz/c/std/gif_fuzzer.c +++ b/fuzz/c/std/gif_fuzzer.c @@ -87,7 +87,7 @@ const char* fuzz(wuffs_base__io_reader src_reader, uint32_t hash) { // and will not e.g. write past the buffer bounds. But the Wuffs GIF API // should somehow pass a subset of pixbuf to decode_frame. dst.wi = 0; - s = wuffs_gif__decoder__decode_frame(&dec, dst_writer, src_reader); + s = wuffs_gif__decoder__decode_frame(&dec, &ib, dst_writer, src_reader); if (s) { if ((s == WUFFS_GIF__SUSPENSION_END_OF_DATA) && seen_ok) { s = WUFFS_GIF__STATUS_OK; diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 555309e1..223d9303 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -1266,9 +1266,11 @@ wuffs_gif__status wuffs_gif__decoder__decode_config( wuffs_base__image_config* a_dst, wuffs_base__io_reader a_src); -wuffs_gif__status wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self, - wuffs_base__io_writer a_dst, - wuffs_base__io_reader a_src); +wuffs_gif__status wuffs_gif__decoder__decode_frame( + wuffs_gif__decoder* self, + wuffs_base__image_buffer* a_ib, + wuffs_base__io_writer a_dst, + wuffs_base__io_reader a_src); #ifdef __cplusplus } // extern "C" @@ -1806,6 +1808,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_ae( static wuffs_gif__status wuffs_gif__decoder__decode_id( wuffs_gif__decoder* self, + wuffs_base__image_buffer* a_ib, wuffs_base__io_writer a_dst, wuffs_base__io_reader a_src); @@ -1993,6 +1996,7 @@ short_read_src: wuffs_gif__status wuffs_gif__decoder__decode_frame( wuffs_gif__decoder* self, + wuffs_base__image_buffer* a_ib, wuffs_base__io_writer a_dst, wuffs_base__io_reader a_src) { if (!self) { @@ -2004,6 +2008,10 @@ wuffs_gif__status wuffs_gif__decoder__decode_frame( if (self->private_impl.status < 0) { return self->private_impl.status; } + if (!a_ib) { + self->private_impl.status = WUFFS_GIF__ERROR_BAD_ARGUMENT; + return WUFFS_GIF__ERROR_BAD_ARGUMENT; + } wuffs_gif__status status = WUFFS_GIF__STATUS_OK; uint8_t* ioptr_src = NULL; @@ -2064,7 +2072,7 @@ wuffs_gif__status wuffs_gif__decoder__decode_frame( if (a_src.private_impl.buf) { a_src.private_impl.buf->ri = ioptr_src - a_src.private_impl.buf->ptr; } - status = wuffs_gif__decoder__decode_id(self, a_dst, a_src); + status = wuffs_gif__decoder__decode_id(self, a_ib, a_dst, a_src); if (a_src.private_impl.buf) { ioptr_src = a_src.private_impl.buf->ptr + a_src.private_impl.buf->ri; } @@ -2720,6 +2728,7 @@ short_read_src: static wuffs_gif__status wuffs_gif__decoder__decode_id( wuffs_gif__decoder* self, + wuffs_base__image_buffer* a_ib, wuffs_base__io_writer a_dst, wuffs_base__io_reader a_src) { wuffs_gif__status status = WUFFS_GIF__STATUS_OK; diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index 06bbbe32..2769626e 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -1266,9 +1266,11 @@ wuffs_gif__status wuffs_gif__decoder__decode_config( wuffs_base__image_config* a_dst, wuffs_base__io_reader a_src); -wuffs_gif__status wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self, - wuffs_base__io_writer a_dst, - wuffs_base__io_reader a_src); +wuffs_gif__status wuffs_gif__decoder__decode_frame( + wuffs_gif__decoder* self, + wuffs_base__image_buffer* a_ib, + wuffs_base__io_writer a_dst, + wuffs_base__io_reader a_src); #ifdef __cplusplus } // extern "C" diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 8e69cafc..76d38e66 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -72,10 +72,12 @@ var Types = []string{ "empty_struct", "bool", - "status", + + "image_buffer", + "image_config", "io_reader", "io_writer", - "image_config", + "status", } var Funcs = []string{ @@ -99,11 +101,11 @@ var Funcs = []string{ "u64.max(x u64)(ret u64)", "u64.min(x u64)(ret u64)", - // ---- status + // ---- image_buffer - "status.is_error()(ret bool)", - "status.is_ok()(ret bool)", - "status.is_suspension()(ret bool)", + // ---- image_config + + "image_config.initialize!(pixfmt u32, pixsub u32, width u32, height u32, num_loops u32)()", // ---- io_reader @@ -146,9 +148,11 @@ var Funcs = []string{ "io_writer.copy_from_slice!(s slice u8)(ret u64)", "io_writer.copy_from_slice32!(s slice u8, length u32)(ret u32)", - // ---- image_config + // ---- status - "image_config.initialize!(pixfmt u32, pixsub u32, width u32, height u32, num_loops u32)()", + "status.is_error()(ret bool)", + "status.is_ok()(ret bool)", + "status.is_suspension()(ret bool)", } // The "T" types here are placeholders for generic "slice U" types. After diff --git a/lang/check/resolve.go b/lang/check/resolve.go index 4a01f785..31e85de8 100644 --- a/lang/check/resolve.go +++ b/lang/check/resolve.go @@ -46,10 +46,12 @@ var ( typeExprEmptyStruct = a.NewTypeExpr(0, t.IDBase, t.IDEmptyStruct, nil, nil, nil) typeExprBool = a.NewTypeExpr(0, t.IDBase, t.IDBool, nil, nil, nil) - typeExprStatus = a.NewTypeExpr(0, t.IDBase, t.IDStatus, nil, nil, nil) + + typeExprImageBuffer = a.NewTypeExpr(0, t.IDBase, t.IDImageBuffer, nil, nil, nil) + typeExprImageConfig = a.NewTypeExpr(0, t.IDBase, t.IDImageConfig, nil, nil, nil) typeExprIOReader = a.NewTypeExpr(0, t.IDBase, t.IDIOReader, nil, nil, nil) typeExprIOWriter = a.NewTypeExpr(0, t.IDBase, t.IDIOWriter, nil, nil, nil) - typeExprImageConfig = a.NewTypeExpr(0, t.IDBase, t.IDImageConfig, nil, nil, nil) + typeExprStatus = a.NewTypeExpr(0, t.IDBase, t.IDStatus, nil, nil, nil) typeExprSliceU8 = a.NewTypeExpr(t.IDSlice, 0, 0, nil, nil, typeExprU8) ) @@ -58,16 +60,19 @@ var ( type typeMap map[t.ID]*a.TypeExpr var builtInTypeMap = typeMap{ - t.IDU8: typeExprU8, - t.IDU16: typeExprU16, - t.IDU32: typeExprU32, - t.IDU64: typeExprU64, + t.IDU8: typeExprU8, + t.IDU16: typeExprU16, + t.IDU32: typeExprU32, + t.IDU64: typeExprU64, + t.IDEmptyStruct: typeExprEmptyStruct, t.IDBool: typeExprBool, - t.IDStatus: typeExprStatus, + + t.IDImageBuffer: typeExprImageBuffer, + t.IDImageConfig: typeExprImageConfig, t.IDIOReader: typeExprIOReader, t.IDIOWriter: typeExprIOWriter, - t.IDImageConfig: typeExprImageConfig, + t.IDStatus: typeExprStatus, } func (c *Checker) builtInFunc(qqid t.QQID) (*a.Func, error) { diff --git a/lang/token/list.go b/lang/token/list.go index 738c3562..c3bf6a2e 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -400,7 +400,8 @@ const ( IDIOReader = ID(0x103) IDIOWriter = ID(0x104) IDCapitalT = ID(0x105) - IDImageConfig = ID(0x106) + IDImageBuffer = ID(0x106) + IDImageConfig = ID(0x107) IDPilcrow = ID(0x10D) IDDiamond = ID(0x10E) @@ -586,6 +587,7 @@ var builtInsByID = [nBuiltInIDs]string{ IDIOReader: "io_reader", IDIOWriter: "io_writer", IDCapitalT: "T", + IDImageBuffer: "image_buffer", IDImageConfig: "image_config", // The next few IDs are never returned by the tokenizer, as it rejects diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index ed659f9e..9a5113e5 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -89,7 +89,7 @@ pub func decoder.decode_config?(dst ptr base.image_config, src base.io_reader)() this.call_sequence = 1 } -pub func decoder.decode_frame?(dst base.io_writer, src base.io_reader)() { +pub func decoder.decode_frame?(ib ptr base.image_buffer, dst base.io_writer, src base.io_reader)() { if this.call_sequence < 1 { return error "invalid call sequence" } @@ -108,7 +108,7 @@ pub func decoder.decode_frame?(dst base.io_writer, src base.io_reader)() { // the API? Should the 0x3B Trailer byte result in a "no more // frames" suspension instead of ok? Do we need additional // destination arguments for timing and palette? - this.decode_id?(dst:in.dst, src:in.src) + this.decode_id?(ib:in.ib, dst:in.dst, src:in.src) return } else if this.block_type == 0x3B { // The spec calls 0x3B the "Trailer". break @@ -281,7 +281,7 @@ pri func decoder.decode_ae?(src base.io_reader)() { // See the spec section 20 "Image Descriptor" on page 11. // // TODO: dst should be a ptr base.image_buffer, not a base.io_writer. -pri func decoder.decode_id?(dst base.io_writer, src base.io_reader)() { +pri func decoder.decode_id?(ib ptr base.image_buffer, dst base.io_writer, src base.io_reader)() { // TODO: check that the frame rect is inside the image rect?? this.frame_left = in.src.read_u16le?() as base.u32 this.frame_top = in.src.read_u16le?() as base.u32 diff --git a/test/c/std/gif.c b/test/c/std/gif.c index 995cba9c..49448fdd 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -372,7 +372,7 @@ const char* wuffs_gif_decode(wuffs_base__io_buffer* dst, wuffs_base__image_buffer__set_from_slice( &ib, ic, wuffs_base__io_buffer__writable(dst)); while (true) { - s = wuffs_gif__decoder__decode_frame(&dec, dst_writer, src_reader); + s = wuffs_gif__decoder__decode_frame(&dec, &ib, dst_writer, src_reader); if (s == WUFFS_GIF__SUSPENSION_END_OF_DATA) { break; } @@ -453,7 +453,7 @@ bool do_test_wuffs_gif_decode(const char* filename, size_t old_ri = src.ri; wuffs_gif__status status = - wuffs_gif__decoder__decode_frame(&dec, got_writer, src_reader); + wuffs_gif__decoder__decode_frame(&dec, &ib, got_writer, src_reader); if (status == WUFFS_GIF__STATUS_OK) { break; } @@ -525,7 +525,7 @@ bool do_test_wuffs_gif_decode(const char* filename, wuffs_base__io_writer got_writer = wuffs_base__io_buffer__writer(&got); wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src); wuffs_gif__status status = - wuffs_gif__decoder__decode_frame(&dec, got_writer, src_reader); + wuffs_gif__decoder__decode_frame(&dec, &ib, got_writer, src_reader); if (status != WUFFS_GIF__SUSPENSION_END_OF_DATA) { FAIL("decode_frame: got %" PRIi32 " (%s), want %" PRIi32 " (%s)", status, wuffs_gif__status__string(status), WUFFS_GIF__SUSPENSION_END_OF_DATA, @@ -554,11 +554,12 @@ void test_wuffs_gif_call_sequence() { wuffs_gif__decoder dec; wuffs_gif__decoder__initialize(&dec, WUFFS_VERSION, 0); + wuffs_base__image_buffer ib = ((wuffs_base__image_buffer){}); wuffs_base__io_writer got_writer = wuffs_base__io_buffer__writer(&got); wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src); wuffs_gif__status status = - wuffs_gif__decoder__decode_frame(&dec, got_writer, src_reader); + wuffs_gif__decoder__decode_frame(&dec, &ib, got_writer, src_reader); if (status != WUFFS_GIF__ERROR_INVALID_CALL_SEQUENCE) { FAIL("decode_frame: got %" PRIi32 " (%s), want %" PRIi32 " (%s)", status, wuffs_gif__status__string(status), @@ -606,7 +607,8 @@ bool do_test_wuffs_gif_decode_animated(const char* filename, uint32_t i; for (i = 0; i < want_num_frames; i++) { got.wi = 0; - status = wuffs_gif__decoder__decode_frame(&dec, got_writer, src_reader); + status = + wuffs_gif__decoder__decode_frame(&dec, &ib, got_writer, src_reader); if (status != WUFFS_GIF__STATUS_OK) { FAIL("decode_frame #%" PRIu32 ": got %" PRIi32 " (%s)", i, status, wuffs_gif__status__string(status)); @@ -622,7 +624,7 @@ bool do_test_wuffs_gif_decode_animated(const char* filename, // There should be no more frames. got.wi = 0; - status = wuffs_gif__decoder__decode_frame(&dec, got_writer, src_reader); + status = wuffs_gif__decoder__decode_frame(&dec, &ib, got_writer, src_reader); if (status != WUFFS_GIF__SUSPENSION_END_OF_DATA) { FAIL("decode_frame: got %" PRIi32 " (%s), want %" PRIi32 " (%s)", status, wuffs_gif__status__string(status), WUFFS_GIF__SUSPENSION_END_OF_DATA,