diff --git a/cmd/wuffs-c/internal/cgen/base-header.h b/cmd/wuffs-c/internal/cgen/base-header.h index 2e1aa3da..852af1b2 100644 --- a/cmd/wuffs-c/internal/cgen/base-header.h +++ b/cmd/wuffs-c/internal/cgen/base-header.h @@ -45,6 +45,8 @@ // TODO: don't hard code this in base-header.h. #define WUFFS_VERSION (0x00001) +// ---------------- I/O + // wuffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length). // // A value with all fields NULL or zero is a valid, empty slice. @@ -102,4 +104,72 @@ typedef struct { } private_impl; } wuffs_base__writer1; +// ---------------- Images + +typedef struct { + // Do not access the private_impl's fields directly. There is no API/ABI + // compatibility or safety guarantee if you do so. + struct { + uint32_t flags; + uint32_t w; + uint32_t h; + // TODO: color model, including both packed RGBA and planar, + // chroma-subsampled YCbCr. + } private_impl; +} wuffs_base__image_config; + +static inline void wuffs_base__image_config__invalidate( + wuffs_base__image_config* c) { + if (c) { + *c = ((wuffs_base__image_config){}); + } +} + +static inline bool wuffs_base__image_config__valid( + wuffs_base__image_config* c) { + if (!c || !(c->private_impl.flags & 1)) { + return false; + } + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return wh <= ((uint64_t)SIZE_MAX); +} + +static inline uint32_t wuffs_base__image_config__width( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.w : 0; +} + +static inline uint32_t wuffs_base__image_config__height( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.h : 0; +} + +// TODO: this is the right API for planar (not packed) pixbufs? Should it allow +// decoding into a color model different from the format's intrinsic one? For +// example, decoding a JPEG image straight to RGBA instead of to YCbCr? +static inline size_t wuffs_base__image_config__pixbuf_size( + wuffs_base__image_config* c) { + if (wuffs_base__image_config__valid(c)) { + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return (size_t)wh; + } + return 0; +} + +static inline void wuffs_base__image_config__initialize( + wuffs_base__image_config* c, + uint32_t width, + uint32_t height, + uint32_t TODO_color_model) { + if (!c) { + return; + } + c->private_impl.flags = 1; + c->private_impl.w = width; + c->private_impl.h = height; + // TODO: color model. +} + #endif // WUFFS_BASE_HEADER_H diff --git a/cmd/wuffs-c/internal/cgen/data.go b/cmd/wuffs-c/internal/cgen/data.go index 45208791..33999093 100644 --- a/cmd/wuffs-c/internal/cgen/data.go +++ b/cmd/wuffs-c/internal/cgen/data.go @@ -18,9 +18,11 @@ package cgen const baseHeader = "" + "#ifndef WUFFS_BASE_HEADER_H\n#define WUFFS_BASE_HEADER_H\n\n// Copyright 2017 The Wuffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n#include \n#include \n#include \n\n// Wuffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n//\n// Si" + - "milarly, the word size is at most 64 bits because it assumes that\n// converting a usize to u64 will never overflow.\n#if __WORDSIZE < 32\n#error \"Wuffs requires a word size of at least 32 bits\"\n#elif __WORDSIZE > 64\n#error \"Wuffs requires a word size of at most 64 bits\"\n#endif\n\n// WUFFS_VERSION is the major.minor version number as a uint32. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API / ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in base-header.h.\n#define WUFFS_VERSION (0x00001)\n\n// wuffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length).\n//\n// A value with all fields NULL or zero is a valid, empty slice.\ntypedef struct {\n uint8_t* ptr;\n size_t len;\n} wuffs_base__slice_u8;\n\n// wuffs_base__buf1 is a 1-dimensional buffer (a pointer and length), plus\n// additional indexes into that buffer, p" + - "lus an opened / closed flag.\n//\n// A value with all fields NULL or zero is a valid, empty buffer.\ntypedef 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} wuffs_base__buf1;\n\n// wuffs_base__limit1 provides a limited view of a 1-dimensional byte stream:\n// its first N bytes. That N can be greater than a buffer's current read or\n// write capacity. N decreases naturally over time as bytes are read from or\n// written to the stream.\n//\n// A value with all fields NULL or zero is a valid, unlimited view.\ntypedef struct wuffs_base__limit1 {\n uint64_t* ptr_to_len; // Pointer to N.\n struct wuffs_base__limit1* next; // Linked list of limits.\n} wuffs_base__limit1;\n\ntypedef struct {\n // TODO: move buf into private_impl? As it is, it looks like users can modify\n // the buf field to point to a different buffer, which can turn the l" + - "imit and\n // mark fields into dangling pointers.\n wuffs_base__buf1* buf;\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__limit1 limit;\n uint8_t* mark;\n } private_impl;\n} wuffs_base__reader1;\n\ntypedef struct {\n // TODO: move buf into private_impl? As it is, it looks like users can modify\n // the buf field to point to a different buffer, which can turn the limit and\n // mark fields into dangling pointers.\n wuffs_base__buf1* buf;\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__limit1 limit;\n uint8_t* mark;\n } private_impl;\n} wuffs_base__writer1;\n\n#endif // WUFFS_BASE_HEADER_H\n" + + "milarly, the word size is at most 64 bits because it assumes that\n// converting a usize to u64 will never overflow.\n#if __WORDSIZE < 32\n#error \"Wuffs requires a word size of at least 32 bits\"\n#elif __WORDSIZE > 64\n#error \"Wuffs requires a word size of at most 64 bits\"\n#endif\n\n// WUFFS_VERSION is the major.minor version number as a uint32. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API / ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in base-header.h.\n#define WUFFS_VERSION (0x00001)\n\n// ---------------- I/O\n\n// wuffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length).\n//\n// A value with all fields NULL or zero is a valid, empty slice.\ntypedef struct {\n uint8_t* ptr;\n size_t len;\n} wuffs_base__slice_u8;\n\n// wuffs_base__buf1 is a 1-dimensional buffer (a pointer and length), plus\n// additional in" + + "dexes 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 {\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} wuffs_base__buf1;\n\n// wuffs_base__limit1 provides a limited view of a 1-dimensional byte stream:\n// its first N bytes. That N can be greater than a buffer's current read or\n// write capacity. N decreases naturally over time as bytes are read from or\n// written to the stream.\n//\n// A value with all fields NULL or zero is a valid, unlimited view.\ntypedef struct wuffs_base__limit1 {\n uint64_t* ptr_to_len; // Pointer to N.\n struct wuffs_base__limit1* next; // Linked list of limits.\n} wuffs_base__limit1;\n\ntypedef struct {\n // TODO: move buf into private_impl? As it is, it looks like users can modify\n // the buf field to point to a different buf" + + "fer, which can turn the limit and\n // mark fields into dangling pointers.\n wuffs_base__buf1* buf;\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__limit1 limit;\n uint8_t* mark;\n } private_impl;\n} wuffs_base__reader1;\n\ntypedef struct {\n // TODO: move buf into private_impl? As it is, it looks like users can modify\n // the buf field to point to a different buffer, which can turn the limit and\n // mark fields into dangling pointers.\n wuffs_base__buf1* buf;\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__limit1 limit;\n uint8_t* mark;\n } private_impl;\n} wuffs_base__writer1;\n\n// ---------------- Images\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 uint32_t flags;\n uint32_t w;\n " + + " uint32_t h;\n // TODO: color model, including both packed RGBA and planar,\n // chroma-subsampled YCbCr.\n } private_impl;\n} wuffs_base__image_config;\n\nstatic inline void wuffs_base__image_config__invalidate(\n wuffs_base__image_config* c) {\n if (c) {\n *c = ((wuffs_base__image_config){});\n }\n}\n\nstatic inline bool wuffs_base__image_config__valid(\n wuffs_base__image_config* c) {\n if (!c || !(c->private_impl.flags & 1)) {\n return false;\n }\n uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h);\n // TODO: handle things other than 1 byte per pixel.\n return wh <= ((uint64_t)SIZE_MAX);\n}\n\nstatic inline uint32_t wuffs_base__image_config__width(\n wuffs_base__image_config* c) {\n return wuffs_base__image_config__valid(c) ? c->private_impl.w : 0;\n}\n\nstatic inline uint32_t wuffs_base__image_config__height(\n wuffs_base__image_config* c) {\n return wuffs_base__image_config__valid(c) ? c->private_impl.h : 0;\n}\n\n// TODO: this is the right API for planar (not packed) pixbuf" + + "s? Should it allow\n// decoding into a color model different from the format's intrinsic one? For\n// example, decoding a JPEG image straight to RGBA instead of to YCbCr?\nstatic inline size_t wuffs_base__image_config__pixbuf_size(\n wuffs_base__image_config* c) {\n if (wuffs_base__image_config__valid(c)) {\n uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h);\n // TODO: handle things other than 1 byte per pixel.\n return (size_t)wh;\n }\n return 0;\n}\n\nstatic inline void wuffs_base__image_config__initialize(\n wuffs_base__image_config* c,\n uint32_t width,\n uint32_t height,\n uint32_t TODO_color_model) {\n if (!c) {\n return;\n }\n c->private_impl.flags = 1;\n c->private_impl.w = width;\n c->private_impl.h = height;\n // TODO: color model.\n}\n\n#endif // WUFFS_BASE_HEADER_H\n" + "" const baseImpl = "" + diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index 33016875..b819e3b0 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -581,6 +581,11 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var // See gen.writeInitializerImpl for a similar use of otherPkg. prefix = "wuffs_" + otherPkg + "__" } + // TODO: remove this hack when "image_config" in Wuffs code becomes + // "base.image_config". + if qid[1] == t.IDImageConfig { + prefix = "wuffs_base__" + } b.printf("%s%s", prefix, qid[1].Str(g.tm)) } diff --git a/example/peterface/peterface.c b/example/peterface/peterface.c index 7bb676e1..15267810 100644 --- a/example/peterface/peterface.c +++ b/example/peterface/peterface.c @@ -49,6 +49,9 @@ size_t pjw_len; static void ignore_return_value(int ignored) {} static const char* decode() { + wuffs_gif__decoder dec; + wuffs_gif__decoder__initialize(&dec, WUFFS_VERSION, 0); + wuffs_base__image_config ic = {{0}}; uint8_t dst_buffer[DST_BUFFER_SIZE]; wuffs_base__buf1 dst = {.ptr = dst_buffer, .len = DST_BUFFER_SIZE}; wuffs_base__buf1 src = { @@ -56,10 +59,8 @@ static const char* decode() { wuffs_base__writer1 dst_writer = {.buf = &dst}; wuffs_base__reader1 src_reader = {.buf = &src}; - wuffs_gif__decoder dec; - wuffs_gif__decoder__initialize(&dec, WUFFS_VERSION, 0); wuffs_gif__status s = - wuffs_gif__decoder__decode_config(&dec, dst_writer, src_reader); + wuffs_gif__decoder__decode_config(&dec, &ic, src_reader); if (s) { return wuffs_gif__status__string(s); } diff --git a/gen/c/std/deflate.c b/gen/c/std/deflate.c index e9d4a2c2..037e983f 100644 --- a/gen/c/std/deflate.c +++ b/gen/c/std/deflate.c @@ -48,6 +48,8 @@ // TODO: don't hard code this in base-header.h. #define WUFFS_VERSION (0x00001) +// ---------------- I/O + // wuffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length). // // A value with all fields NULL or zero is a valid, empty slice. @@ -105,6 +107,74 @@ typedef struct { } private_impl; } wuffs_base__writer1; +// ---------------- Images + +typedef struct { + // Do not access the private_impl's fields directly. There is no API/ABI + // compatibility or safety guarantee if you do so. + struct { + uint32_t flags; + uint32_t w; + uint32_t h; + // TODO: color model, including both packed RGBA and planar, + // chroma-subsampled YCbCr. + } private_impl; +} wuffs_base__image_config; + +static inline void wuffs_base__image_config__invalidate( + wuffs_base__image_config* c) { + if (c) { + *c = ((wuffs_base__image_config){}); + } +} + +static inline bool wuffs_base__image_config__valid( + wuffs_base__image_config* c) { + if (!c || !(c->private_impl.flags & 1)) { + return false; + } + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return wh <= ((uint64_t)SIZE_MAX); +} + +static inline uint32_t wuffs_base__image_config__width( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.w : 0; +} + +static inline uint32_t wuffs_base__image_config__height( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.h : 0; +} + +// TODO: this is the right API for planar (not packed) pixbufs? Should it allow +// decoding into a color model different from the format's intrinsic one? For +// example, decoding a JPEG image straight to RGBA instead of to YCbCr? +static inline size_t wuffs_base__image_config__pixbuf_size( + wuffs_base__image_config* c) { + if (wuffs_base__image_config__valid(c)) { + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return (size_t)wh; + } + return 0; +} + +static inline void wuffs_base__image_config__initialize( + wuffs_base__image_config* c, + uint32_t width, + uint32_t height, + uint32_t TODO_color_model) { + if (!c) { + return; + } + c->private_impl.flags = 1; + c->private_impl.w = width; + c->private_impl.h = height; + // TODO: color model. +} + #endif // WUFFS_BASE_HEADER_H // ---------------- Use Declarations diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 38e08f49..e72f873e 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -48,6 +48,8 @@ // TODO: don't hard code this in base-header.h. #define WUFFS_VERSION (0x00001) +// ---------------- I/O + // wuffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length). // // A value with all fields NULL or zero is a valid, empty slice. @@ -105,6 +107,74 @@ typedef struct { } private_impl; } wuffs_base__writer1; +// ---------------- Images + +typedef struct { + // Do not access the private_impl's fields directly. There is no API/ABI + // compatibility or safety guarantee if you do so. + struct { + uint32_t flags; + uint32_t w; + uint32_t h; + // TODO: color model, including both packed RGBA and planar, + // chroma-subsampled YCbCr. + } private_impl; +} wuffs_base__image_config; + +static inline void wuffs_base__image_config__invalidate( + wuffs_base__image_config* c) { + if (c) { + *c = ((wuffs_base__image_config){}); + } +} + +static inline bool wuffs_base__image_config__valid( + wuffs_base__image_config* c) { + if (!c || !(c->private_impl.flags & 1)) { + return false; + } + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return wh <= ((uint64_t)SIZE_MAX); +} + +static inline uint32_t wuffs_base__image_config__width( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.w : 0; +} + +static inline uint32_t wuffs_base__image_config__height( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.h : 0; +} + +// TODO: this is the right API for planar (not packed) pixbufs? Should it allow +// decoding into a color model different from the format's intrinsic one? For +// example, decoding a JPEG image straight to RGBA instead of to YCbCr? +static inline size_t wuffs_base__image_config__pixbuf_size( + wuffs_base__image_config* c) { + if (wuffs_base__image_config__valid(c)) { + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return (size_t)wh; + } + return 0; +} + +static inline void wuffs_base__image_config__initialize( + wuffs_base__image_config* c, + uint32_t width, + uint32_t height, + uint32_t TODO_color_model) { + if (!c) { + return; + } + c->private_impl.flags = 1; + c->private_impl.w = width; + c->private_impl.h = height; + // TODO: color model. +} + #endif // WUFFS_BASE_HEADER_H // ---------------- Use Declarations @@ -261,9 +331,10 @@ void wuffs_gif__decoder__initialize(wuffs_gif__decoder* self, // ---------------- Public Function Prototypes -wuffs_gif__status wuffs_gif__decoder__decode_config(wuffs_gif__decoder* self, - wuffs_base__writer1 a_dst, - wuffs_base__reader1 a_src); +wuffs_gif__status wuffs_gif__decoder__decode_config( + wuffs_gif__decoder* self, + wuffs_base__image_config* a_dst, + wuffs_base__reader1 a_src); wuffs_gif__status wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self, wuffs_base__writer1 a_dst, @@ -746,9 +817,10 @@ void wuffs_gif__decoder__initialize(wuffs_gif__decoder* self, // ---------------- Function Implementations -wuffs_gif__status wuffs_gif__decoder__decode_config(wuffs_gif__decoder* self, - wuffs_base__writer1 a_dst, - wuffs_base__reader1 a_src) { +wuffs_gif__status wuffs_gif__decoder__decode_config( + wuffs_gif__decoder* self, + wuffs_base__image_config* a_dst, + wuffs_base__reader1 a_src) { if (!self) { return WUFFS_GIF__ERROR_BAD_RECEIVER; } @@ -759,6 +831,10 @@ wuffs_gif__status wuffs_gif__decoder__decode_config(wuffs_gif__decoder* self, return self->private_impl.status; } wuffs_gif__status status = WUFFS_GIF__STATUS_OK; + if (!a_dst) { + status = WUFFS_GIF__ERROR_BAD_ARGUMENT; + goto exit; + } uint32_t coro_susp_point = self->private_impl.c_decode_config[0].coro_susp_point; diff --git a/gen/c/std/zlib.c b/gen/c/std/zlib.c index dee2ba3b..7533acc9 100644 --- a/gen/c/std/zlib.c +++ b/gen/c/std/zlib.c @@ -48,6 +48,8 @@ // TODO: don't hard code this in base-header.h. #define WUFFS_VERSION (0x00001) +// ---------------- I/O + // wuffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length). // // A value with all fields NULL or zero is a valid, empty slice. @@ -105,6 +107,74 @@ typedef struct { } private_impl; } wuffs_base__writer1; +// ---------------- Images + +typedef struct { + // Do not access the private_impl's fields directly. There is no API/ABI + // compatibility or safety guarantee if you do so. + struct { + uint32_t flags; + uint32_t w; + uint32_t h; + // TODO: color model, including both packed RGBA and planar, + // chroma-subsampled YCbCr. + } private_impl; +} wuffs_base__image_config; + +static inline void wuffs_base__image_config__invalidate( + wuffs_base__image_config* c) { + if (c) { + *c = ((wuffs_base__image_config){}); + } +} + +static inline bool wuffs_base__image_config__valid( + wuffs_base__image_config* c) { + if (!c || !(c->private_impl.flags & 1)) { + return false; + } + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return wh <= ((uint64_t)SIZE_MAX); +} + +static inline uint32_t wuffs_base__image_config__width( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.w : 0; +} + +static inline uint32_t wuffs_base__image_config__height( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.h : 0; +} + +// TODO: this is the right API for planar (not packed) pixbufs? Should it allow +// decoding into a color model different from the format's intrinsic one? For +// example, decoding a JPEG image straight to RGBA instead of to YCbCr? +static inline size_t wuffs_base__image_config__pixbuf_size( + wuffs_base__image_config* c) { + if (wuffs_base__image_config__valid(c)) { + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return (size_t)wh; + } + return 0; +} + +static inline void wuffs_base__image_config__initialize( + wuffs_base__image_config* c, + uint32_t width, + uint32_t height, + uint32_t TODO_color_model) { + if (!c) { + return; + } + c->private_impl.flags = 1; + c->private_impl.w = width; + c->private_impl.h = height; + // TODO: color model. +} + #endif // WUFFS_BASE_HEADER_H // ---------------- Use Declarations diff --git a/gen/h/std/deflate.h b/gen/h/std/deflate.h index 5b5b46fc..e4cdf638 100644 --- a/gen/h/std/deflate.h +++ b/gen/h/std/deflate.h @@ -48,6 +48,8 @@ // TODO: don't hard code this in base-header.h. #define WUFFS_VERSION (0x00001) +// ---------------- I/O + // wuffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length). // // A value with all fields NULL or zero is a valid, empty slice. @@ -105,6 +107,74 @@ typedef struct { } private_impl; } wuffs_base__writer1; +// ---------------- Images + +typedef struct { + // Do not access the private_impl's fields directly. There is no API/ABI + // compatibility or safety guarantee if you do so. + struct { + uint32_t flags; + uint32_t w; + uint32_t h; + // TODO: color model, including both packed RGBA and planar, + // chroma-subsampled YCbCr. + } private_impl; +} wuffs_base__image_config; + +static inline void wuffs_base__image_config__invalidate( + wuffs_base__image_config* c) { + if (c) { + *c = ((wuffs_base__image_config){}); + } +} + +static inline bool wuffs_base__image_config__valid( + wuffs_base__image_config* c) { + if (!c || !(c->private_impl.flags & 1)) { + return false; + } + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return wh <= ((uint64_t)SIZE_MAX); +} + +static inline uint32_t wuffs_base__image_config__width( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.w : 0; +} + +static inline uint32_t wuffs_base__image_config__height( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.h : 0; +} + +// TODO: this is the right API for planar (not packed) pixbufs? Should it allow +// decoding into a color model different from the format's intrinsic one? For +// example, decoding a JPEG image straight to RGBA instead of to YCbCr? +static inline size_t wuffs_base__image_config__pixbuf_size( + wuffs_base__image_config* c) { + if (wuffs_base__image_config__valid(c)) { + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return (size_t)wh; + } + return 0; +} + +static inline void wuffs_base__image_config__initialize( + wuffs_base__image_config* c, + uint32_t width, + uint32_t height, + uint32_t TODO_color_model) { + if (!c) { + return; + } + c->private_impl.flags = 1; + c->private_impl.w = width; + c->private_impl.h = height; + // TODO: color model. +} + #endif // WUFFS_BASE_HEADER_H // ---------------- Use Declarations diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index 7f6ae3ce..a14a83a7 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -48,6 +48,8 @@ // TODO: don't hard code this in base-header.h. #define WUFFS_VERSION (0x00001) +// ---------------- I/O + // wuffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length). // // A value with all fields NULL or zero is a valid, empty slice. @@ -105,6 +107,74 @@ typedef struct { } private_impl; } wuffs_base__writer1; +// ---------------- Images + +typedef struct { + // Do not access the private_impl's fields directly. There is no API/ABI + // compatibility or safety guarantee if you do so. + struct { + uint32_t flags; + uint32_t w; + uint32_t h; + // TODO: color model, including both packed RGBA and planar, + // chroma-subsampled YCbCr. + } private_impl; +} wuffs_base__image_config; + +static inline void wuffs_base__image_config__invalidate( + wuffs_base__image_config* c) { + if (c) { + *c = ((wuffs_base__image_config){}); + } +} + +static inline bool wuffs_base__image_config__valid( + wuffs_base__image_config* c) { + if (!c || !(c->private_impl.flags & 1)) { + return false; + } + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return wh <= ((uint64_t)SIZE_MAX); +} + +static inline uint32_t wuffs_base__image_config__width( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.w : 0; +} + +static inline uint32_t wuffs_base__image_config__height( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.h : 0; +} + +// TODO: this is the right API for planar (not packed) pixbufs? Should it allow +// decoding into a color model different from the format's intrinsic one? For +// example, decoding a JPEG image straight to RGBA instead of to YCbCr? +static inline size_t wuffs_base__image_config__pixbuf_size( + wuffs_base__image_config* c) { + if (wuffs_base__image_config__valid(c)) { + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return (size_t)wh; + } + return 0; +} + +static inline void wuffs_base__image_config__initialize( + wuffs_base__image_config* c, + uint32_t width, + uint32_t height, + uint32_t TODO_color_model) { + if (!c) { + return; + } + c->private_impl.flags = 1; + c->private_impl.w = width; + c->private_impl.h = height; + // TODO: color model. +} + #endif // WUFFS_BASE_HEADER_H // ---------------- Use Declarations @@ -261,9 +331,10 @@ void wuffs_gif__decoder__initialize(wuffs_gif__decoder* self, // ---------------- Public Function Prototypes -wuffs_gif__status wuffs_gif__decoder__decode_config(wuffs_gif__decoder* self, - wuffs_base__writer1 a_dst, - wuffs_base__reader1 a_src); +wuffs_gif__status wuffs_gif__decoder__decode_config( + wuffs_gif__decoder* self, + wuffs_base__image_config* a_dst, + wuffs_base__reader1 a_src); wuffs_gif__status wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self, wuffs_base__writer1 a_dst, diff --git a/gen/h/std/zlib.h b/gen/h/std/zlib.h index 6a995ddc..f1b53ca3 100644 --- a/gen/h/std/zlib.h +++ b/gen/h/std/zlib.h @@ -48,6 +48,8 @@ // TODO: don't hard code this in base-header.h. #define WUFFS_VERSION (0x00001) +// ---------------- I/O + // wuffs_base__slice_u8 is a 1-dimensional buffer (a pointer and length). // // A value with all fields NULL or zero is a valid, empty slice. @@ -105,6 +107,74 @@ typedef struct { } private_impl; } wuffs_base__writer1; +// ---------------- Images + +typedef struct { + // Do not access the private_impl's fields directly. There is no API/ABI + // compatibility or safety guarantee if you do so. + struct { + uint32_t flags; + uint32_t w; + uint32_t h; + // TODO: color model, including both packed RGBA and planar, + // chroma-subsampled YCbCr. + } private_impl; +} wuffs_base__image_config; + +static inline void wuffs_base__image_config__invalidate( + wuffs_base__image_config* c) { + if (c) { + *c = ((wuffs_base__image_config){}); + } +} + +static inline bool wuffs_base__image_config__valid( + wuffs_base__image_config* c) { + if (!c || !(c->private_impl.flags & 1)) { + return false; + } + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return wh <= ((uint64_t)SIZE_MAX); +} + +static inline uint32_t wuffs_base__image_config__width( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.w : 0; +} + +static inline uint32_t wuffs_base__image_config__height( + wuffs_base__image_config* c) { + return wuffs_base__image_config__valid(c) ? c->private_impl.h : 0; +} + +// TODO: this is the right API for planar (not packed) pixbufs? Should it allow +// decoding into a color model different from the format's intrinsic one? For +// example, decoding a JPEG image straight to RGBA instead of to YCbCr? +static inline size_t wuffs_base__image_config__pixbuf_size( + wuffs_base__image_config* c) { + if (wuffs_base__image_config__valid(c)) { + uint64_t wh = ((uint64_t)c->private_impl.w) * ((uint64_t)c->private_impl.h); + // TODO: handle things other than 1 byte per pixel. + return (size_t)wh; + } + return 0; +} + +static inline void wuffs_base__image_config__initialize( + wuffs_base__image_config* c, + uint32_t width, + uint32_t height, + uint32_t TODO_color_model) { + if (!c) { + return; + } + c->private_impl.flags = 1; + c->private_impl.w = width; + c->private_impl.h = height; + // TODO: color model. +} + #endif // WUFFS_BASE_HEADER_H // ---------------- Use Declarations diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 7d31e9ae..fd28cfef 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -72,6 +72,7 @@ var Types = []string{ "status", "reader1", "writer1", + "image_config", } var Funcs = []string{ diff --git a/lang/check/resolve.go b/lang/check/resolve.go index af21e097..15055a24 100644 --- a/lang/check/resolve.go +++ b/lang/check/resolve.go @@ -38,14 +38,15 @@ var ( typeExprIdeal = a.NewTypeExpr(0, 0, t.IDDoubleZ, nil, nil, nil) typeExprList = a.NewTypeExpr(0, 0, t.IDDollar, nil, nil, nil) - typeExprU8 = a.NewTypeExpr(0, 0, t.IDU8, nil, nil, nil) - typeExprU16 = a.NewTypeExpr(0, 0, t.IDU16, nil, nil, nil) - typeExprU32 = a.NewTypeExpr(0, 0, t.IDU32, nil, nil, nil) - typeExprU64 = a.NewTypeExpr(0, 0, t.IDU64, nil, nil, nil) - typeExprBool = a.NewTypeExpr(0, 0, t.IDBool, nil, nil, nil) - typeExprStatus = a.NewTypeExpr(0, 0, t.IDStatus, nil, nil, nil) - typeExprReader1 = a.NewTypeExpr(0, 0, t.IDReader1, nil, nil, nil) - typeExprWriter1 = a.NewTypeExpr(0, 0, t.IDWriter1, nil, nil, nil) + typeExprU8 = a.NewTypeExpr(0, 0, t.IDU8, nil, nil, nil) + typeExprU16 = a.NewTypeExpr(0, 0, t.IDU16, nil, nil, nil) + typeExprU32 = a.NewTypeExpr(0, 0, t.IDU32, nil, nil, nil) + typeExprU64 = a.NewTypeExpr(0, 0, t.IDU64, nil, nil, nil) + typeExprBool = a.NewTypeExpr(0, 0, t.IDBool, nil, nil, nil) + typeExprStatus = a.NewTypeExpr(0, 0, t.IDStatus, nil, nil, nil) + typeExprReader1 = a.NewTypeExpr(0, 0, t.IDReader1, nil, nil, nil) + typeExprWriter1 = a.NewTypeExpr(0, 0, t.IDWriter1, nil, nil, nil) + typeExprImageConfig = a.NewTypeExpr(0, 0, t.IDImageConfig, nil, nil, nil) typeExprSliceU8 = a.NewTypeExpr(t.IDColon, 0, 0, nil, nil, typeExprU8) @@ -59,14 +60,15 @@ var ( type typeMap map[t.ID]*a.TypeExpr var builtInTypeMap = typeMap{ - t.IDU8: typeExprU8, - t.IDU16: typeExprU16, - t.IDU32: typeExprU32, - t.IDU64: typeExprU64, - t.IDBool: typeExprBool, - t.IDStatus: typeExprStatus, - t.IDReader1: typeExprReader1, - t.IDWriter1: typeExprWriter1, + t.IDU8: typeExprU8, + t.IDU16: typeExprU16, + t.IDU32: typeExprU32, + t.IDU64: typeExprU64, + t.IDBool: typeExprBool, + t.IDStatus: typeExprStatus, + t.IDReader1: typeExprReader1, + t.IDWriter1: typeExprWriter1, + t.IDImageConfig: typeExprImageConfig, } func (c *Checker) builtInFunc(qqid t.QQID) (*a.Func, error) { diff --git a/lang/token/list.go b/lang/token/list.go index f04ede29..1eecacef 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -274,21 +274,22 @@ const ( KeyOut = Key(IDOut >> KeyShift) KeyCapitalT = Key(IDCapitalT >> KeyShift) - KeyI8 = Key(IDI8 >> KeyShift) - KeyI16 = Key(IDI16 >> KeyShift) - KeyI32 = Key(IDI32 >> KeyShift) - KeyI64 = Key(IDI64 >> KeyShift) - KeyU8 = Key(IDU8 >> KeyShift) - KeyU16 = Key(IDU16 >> KeyShift) - KeyU32 = Key(IDU32 >> KeyShift) - KeyU64 = Key(IDU64 >> KeyShift) - KeyUsize = Key(IDUsize >> KeyShift) // TODO: unused? - KeyBool = Key(IDBool >> KeyShift) - KeyBuf1 = Key(IDBuf1 >> KeyShift) // TODO: unused? - KeyReader1 = Key(IDReader1 >> KeyShift) - KeyWriter1 = Key(IDWriter1 >> KeyShift) - KeyBuf2 = Key(IDBuf2 >> KeyShift) // TODO: unused? - KeyStatus = Key(IDStatus >> KeyShift) + KeyI8 = Key(IDI8 >> KeyShift) + KeyI16 = Key(IDI16 >> KeyShift) + KeyI32 = Key(IDI32 >> KeyShift) + KeyI64 = Key(IDI64 >> KeyShift) + KeyU8 = Key(IDU8 >> KeyShift) + KeyU16 = Key(IDU16 >> KeyShift) + KeyU32 = Key(IDU32 >> KeyShift) + KeyU64 = Key(IDU64 >> KeyShift) + KeyUsize = Key(IDUsize >> KeyShift) // TODO: unused? + KeyBool = Key(IDBool >> KeyShift) + KeyBuf1 = Key(IDBuf1 >> KeyShift) // TODO: unused? + KeyReader1 = Key(IDReader1 >> KeyShift) + KeyWriter1 = Key(IDWriter1 >> KeyShift) + KeyBuf2 = Key(IDBuf2 >> KeyShift) // TODO: unused? + KeyStatus = Key(IDStatus >> KeyShift) + KeyImageConfig = Key(IDImageConfig >> KeyShift) KeyMark = Key(IDMark >> KeyShift) KeyReadU8 = Key(IDReadU8 >> KeyShift) @@ -465,21 +466,22 @@ const ( IDOut = ID(0x7B<= 1 { return error "invalid call sequence" } this.decode_header?(src:in.src) this.decode_lsd?(src:in.src) + // TODO: fill in dst. this.call_sequence = 1 } diff --git a/test/c/std/gif.c b/test/c/std/gif.c index 21b8b541..0211dd5d 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -50,9 +50,9 @@ the first "./a.out" with "./a.out -bench". Combine these changes with the void test_basic_bad_receiver() { CHECK_FOCUS(__func__); - wuffs_base__writer1 dst = {0}; + wuffs_base__image_config ic = {{0}}; wuffs_base__reader1 src = {0}; - wuffs_gif__status status = wuffs_gif__decoder__decode_config(NULL, dst, src); + wuffs_gif__status status = wuffs_gif__decoder__decode_config(NULL, &ic, src); if (status != WUFFS_GIF__ERROR_BAD_RECEIVER) { FAIL("status: got %d, want %d", status, WUFFS_GIF__ERROR_BAD_RECEIVER); } @@ -61,9 +61,9 @@ void test_basic_bad_receiver() { void test_basic_initializer_not_called() { CHECK_FOCUS(__func__); wuffs_gif__decoder dec = {{0}}; - wuffs_base__writer1 dst = {0}; + wuffs_base__image_config ic = {{0}}; wuffs_base__reader1 src = {0}; - wuffs_gif__status status = wuffs_gif__decoder__decode_config(&dec, dst, src); + wuffs_gif__status status = wuffs_gif__decoder__decode_config(&dec, &ic, src); if (status != WUFFS_GIF__ERROR_INITIALIZER_NOT_CALLED) { FAIL("status: got %d, want %d", status, WUFFS_GIF__ERROR_INITIALIZER_NOT_CALLED); @@ -362,10 +362,11 @@ void bench_wuffs_lzw_decode_100k() { const char* wuffs_gif_decode(wuffs_base__buf1* dst, wuffs_base__buf1* src) { wuffs_gif__decoder dec; wuffs_gif__decoder__initialize(&dec, WUFFS_VERSION, 0); + wuffs_base__image_config ic = {{0}}; wuffs_base__writer1 dst_writer = {.buf = dst}; wuffs_base__reader1 src_reader = {.buf = src}; wuffs_gif__status s = - wuffs_gif__decoder__decode_config(&dec, dst_writer, src_reader); + wuffs_gif__decoder__decode_config(&dec, &ic, src_reader); if (s) { return wuffs_gif__status__string(s); } @@ -392,10 +393,10 @@ bool do_test_wuffs_gif_decode(const char* filename, wuffs_gif__decoder__initialize(&dec, WUFFS_VERSION, 0); { - wuffs_base__writer1 got_writer = {.buf = &got}; + wuffs_base__image_config ic = {{0}}; wuffs_base__reader1 src_reader = {.buf = &src}; wuffs_gif__status status = - wuffs_gif__decoder__decode_config(&dec, got_writer, src_reader); + wuffs_gif__decoder__decode_config(&dec, &ic, src_reader); if (status != WUFFS_GIF__STATUS_OK) { FAIL("status: got %" PRIi32 " (%s)", status, wuffs_gif__status__string(status)); @@ -555,7 +556,6 @@ void test_wuffs_gif_decode_input_is_a_gif_many_small_writes_reads() { void test_wuffs_gif_decode_input_is_a_png() { CHECK_FOCUS(__func__); - wuffs_base__buf1 got = {.ptr = global_got_buffer, .len = BUFFER_SIZE}; wuffs_base__buf1 src = {.ptr = global_src_buffer, .len = BUFFER_SIZE}; if (!read_file(&src, "../../testdata/bricks-dither.png")) { @@ -564,11 +564,11 @@ void test_wuffs_gif_decode_input_is_a_png() { wuffs_gif__decoder dec; wuffs_gif__decoder__initialize(&dec, WUFFS_VERSION, 0); - wuffs_base__writer1 got_writer = {.buf = &got}; + wuffs_base__image_config ic = {{0}}; wuffs_base__reader1 src_reader = {.buf = &src}; wuffs_gif__status status = - wuffs_gif__decoder__decode_config(&dec, got_writer, src_reader); + wuffs_gif__decoder__decode_config(&dec, &ic, src_reader); if (status != WUFFS_GIF__ERROR_BAD_GIF_HEADER) { FAIL("status: got %" PRIi32 " (%s), want %" PRIi32 " (%s)", status, wuffs_gif__status__string(status), WUFFS_GIF__ERROR_BAD_GIF_HEADER,