mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-21 00:47:28 +01:00
Add an image_config concept
This commit is contained in:
1 parent
de9000cc70
commit
d750ea7653
15 files changed
+600
-88
No files matched your search
@@ -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
|
||||
@@ -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 <stdbool.h>\n#include <stdint.h>\n#include <string.h>\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 = "" +
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user