Spin out base/image-public.h

This commit is contained in:
Nigel Tao committed 2018-07-21 08:02:58 +10:00
1 parent 5084641544
commit 4e70498189
6 files changed
+850 -798

No files matched your search

@@ -1,8 +1,5 @@
// After editing this file, run "go generate" in the parent directory.
#ifndef WUFFS_INCLUDE_GUARD__BASE_PUBLIC
#define WUFFS_INCLUDE_GUARD__BASE_PUBLIC
// Copyright 2017 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -1055,746 +1052,6 @@ wuffs_base__io_buffer__struct::writer() {
#endif // __cplusplus
// ---------------- Images
// wuffs_base__color_u32argb is an 8 bit per channel Alpha, Red, Green, Blue
// color, as a uint32_t value. It is in word order, not byte order: its value
// is always 0xAARRGGBB, regardless of endianness. It uses premultiplied alpha.
typedef uint32_t wuffs_base__color_u32argb;
// --------
// wuffs_base__pixel_format encodes the format of the bytes that constitute an
// image frame's pixel data. Its bits:
// - bit 31 is reserved.
// - bits 30 .. 28 encodes color (and channel order, in terms of memory).
// - bits 27 .. 26 are reserved.
// - bits 25 .. 24 encodes transparency.
// - bit 23 indicates big-endian/MSB-first (as opposed to little/LSB).
// - bit 22 indicates floating point (as opposed to integer).
// - bits 21 .. 20 are the number of planes, minus 1. Zero means packed.
// - bits 19 .. 16 encodes the number of bits (depth) in an index value.
// Zero means direct, not palette-indexed.
// - bits 15 .. 12 encodes the number of bits (depth) in the 3rd channel.
// - bits 11 .. 8 encodes the number of bits (depth) in the 2nd channel.
// - bits 7 .. 4 encodes the number of bits (depth) in the 1st channel.
// - bits 3 .. 0 encodes the number of bits (depth) in the 0th channel.
//
// The bit fields of a wuffs_base__pixel_format are not independent. For
// example, the number of planes should not be greater than the number of
// channels. Similarly, bits 15..4 are unused (and should be zero) if bits
// 31..24 (color and transparency) together imply only 1 channel (gray, no
// alpha) and floating point samples should mean a bit depth of 16, 32 or 64.
//
// Formats hold between 1 and 4 channels. For example: Y (1 channel: gray), YA
// (2 channels: gray and alpha), BGR (3 channels: blue, green, red) or CMYK (4
// channels: cyan, magenta, yellow, black).
//
// For direct formats with N > 1 channels, those channels can be laid out in
// either 1 (packed) or N (planar) planes. For example, RGBA data is usually
// packed, but YUV data is usually planar, due to chroma subsampling (for
// details, see the wuffs_base__pixel_subsampling type). For indexed formats,
// the palette (always 256 × 4 bytes) holds up to 4 packed bytes of color data
// per index value, and there is only 1 plane (for the index). The distance
// between successive palette elements is always 4 bytes.
//
// The color field is encoded in 3 bits:
// - 0 means A (Alpha).
// - 1 means Y or YA (Gray, Alpha).
// - 2 means BGR, BGRX or BGRA (Blue, Green, Red, X-padding or Alpha).
// - 3 means RGB, RGBX or RGBA (Red, Green, Blue, X-padding or Alpha).
// - 4 means YUV or YUVA (Luma, Chroma-blue, Chroma-red, Alpha).
// - 5 means CMY or CMYK (Cyan, Magenta, Yellow, Black).
// - all other values are reserved.
//
// In Wuffs, channels are given in memory order (also known as byte order),
// regardless of endianness, since the C type for the pixel data is an array of
// bytes, not an array of uint32_t. For example, packed BGRA with 8 bits per
// channel means that the bytes in memory are always Blue, Green, Red then
// Alpha. On big-endian systems, that is the uint32_t 0xBBGGRRAA. On
// little-endian, 0xAARRGGBB.
//
// When the color field (3 bits) encodes multiple options, the transparency
// field (2 bits) distinguishes them:
// - 0 means fully opaque, no extra channels
// - 1 means fully opaque, one extra channel (X or K, padding or black).
// - 2 means one extra alpha channel, other channels are non-premultiplied.
// - 3 means one extra alpha channel, other channels are premultiplied.
//
// The zero wuffs_base__pixel_format value is an invalid pixel format, as it is
// invalid to combine the zero color (alpha only) with the zero transparency.
//
// Bit depth is encoded in 4 bits:
// - 0 means the channel or index is unused.
// - x means a bit depth of x, for x in the range 1..8.
// - 9 means a bit depth of 10.
// - 10 means a bit depth of 12.
// - 11 means a bit depth of 16.
// - 12 means a bit depth of 24.
// - 13 means a bit depth of 32.
// - 14 means a bit depth of 48.
// - 15 means a bit depth of 64.
//
// For example, wuffs_base__pixel_format 0x3280BBBB is a natural format for
// decoding a PNG image - network byte order (also known as big-endian),
// packed, non-premultiplied alpha - that happens to be 16-bit-depth truecolor
// with alpha (RGBA). In memory order:
//
// ptr+0 ptr+1 ptr+2 ptr+3 ptr+4 ptr+5 ptr+6 ptr+7
// Rhi Rlo Ghi Glo Bhi Blo Ahi Alo
//
// For example, the value wuffs_base__pixel_format 0x20000565 means BGR with no
// alpha or padding, 5/6/5 bits for blue/green/red, packed 2 bytes per pixel,
// laid out LSB-first in memory order:
//
// ptr+0........... ptr+1...........
// MSB LSB MSB LSB
// G₂G₁G₀B₄B₃B₂B₁B₀ R₄R₃R₂R₁R₀G₅G₄G₃
//
// On little-endian systems (but not big-endian), this Wuffs pixel format value
// (0x20000565) corresponds to the Cairo library's CAIRO_FORMAT_RGB16_565, the
// SDL2 (Simple DirectMedia Layer 2) library's SDL_PIXELFORMAT_RGB565 and the
// Skia library's kRGB_565_SkColorType. Note BGR in Wuffs versus RGB in the
// other libraries.
//
// Regardless of endianness, this Wuffs pixel format value (0x20000565)
// corresponds to the V4L2 (Video For Linux 2) library's V4L2_PIX_FMT_RGB565
// and the Wayland-DRM library's WL_DRM_FORMAT_RGB565.
//
// Different software libraries name their pixel formats (and especially their
// channel order) either according to memory layout or as bits of a native
// integer type like uint32_t. The two conventions differ because of a system's
// endianness. As mentioned earlier, Wuffs pixel formats are always in memory
// order. More detail of other software libraries' naming conventions is in the
// Pixel Format Guide at https://afrantzis.github.io/pixel-format-guide/
//
// Do not manipulate these bits directly; they are private implementation
// details. Use methods such as wuffs_base__pixel_format__num_planes instead.
typedef uint32_t wuffs_base__pixel_format;
// Common 8-bit-depth pixel formats. This list is not exhaustive; not all valid
// wuffs_base__pixel_format values are present.
#define WUFFS_BASE__PIXEL_FORMAT__INVALID ((wuffs_base__pixel_format)0x00000000)
#define WUFFS_BASE__PIXEL_FORMAT__A ((wuffs_base__pixel_format)0x02000008)
#define WUFFS_BASE__PIXEL_FORMAT__Y ((wuffs_base__pixel_format)0x10000008)
#define WUFFS_BASE__PIXEL_FORMAT__YA_NONPREMUL \
((wuffs_base__pixel_format)0x12000008)
#define WUFFS_BASE__PIXEL_FORMAT__YA_PREMUL \
((wuffs_base__pixel_format)0x13000008)
#define WUFFS_BASE__PIXEL_FORMAT__BGR ((wuffs_base__pixel_format)0x20000888)
#define WUFFS_BASE__PIXEL_FORMAT__BGRX ((wuffs_base__pixel_format)0x21008888)
#define WUFFS_BASE__PIXEL_FORMAT__BGRX_INDEXED \
((wuffs_base__pixel_format)0x21088888)
#define WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL \
((wuffs_base__pixel_format)0x22008888)
#define WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_INDEXED \
((wuffs_base__pixel_format)0x22088888)
#define WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL \
((wuffs_base__pixel_format)0x23008888)
#define WUFFS_BASE__PIXEL_FORMAT__RGB ((wuffs_base__pixel_format)0x30000888)
#define WUFFS_BASE__PIXEL_FORMAT__RGBX ((wuffs_base__pixel_format)0x31008888)
#define WUFFS_BASE__PIXEL_FORMAT__RGBX_INDEXED \
((wuffs_base__pixel_format)0x31088888)
#define WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL \
((wuffs_base__pixel_format)0x32008888)
#define WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL_INDEXED \
((wuffs_base__pixel_format)0x32088888)
#define WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL \
((wuffs_base__pixel_format)0x33008888)
#define WUFFS_BASE__PIXEL_FORMAT__YUV ((wuffs_base__pixel_format)0x40200888)
#define WUFFS_BASE__PIXEL_FORMAT__YUVK ((wuffs_base__pixel_format)0x41308888)
#define WUFFS_BASE__PIXEL_FORMAT__YUVA_NONPREMUL \
((wuffs_base__pixel_format)0x42308888)
#define WUFFS_BASE__PIXEL_FORMAT__CMY ((wuffs_base__pixel_format)0x50200888)
#define WUFFS_BASE__PIXEL_FORMAT__CMYK ((wuffs_base__pixel_format)0x51308888)
static inline bool //
wuffs_base__pixel_format__is_valid(wuffs_base__pixel_format f) {
return f != 0;
}
static inline bool //
wuffs_base__pixel_format__is_indexed(wuffs_base__pixel_format f) {
return ((f >> 16) & 0x0F) != 0;
}
#define WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX 4
static inline uint32_t //
wuffs_base__pixel_format__num_planes(wuffs_base__pixel_format f) {
return f ? (((f >> 20) & 0x03) + 1) : 0;
}
typedef struct {
wuffs_base__table_u8 planes[WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX];
} wuffs_base__pixel_buffer;
// --------
// wuffs_base__pixel_subsampling encodes the mapping of pixel space coordinates
// (x, y) to pixel buffer indices (i, j). That mapping can differ for each
// plane p. For a depth of 8 bits (1 byte), the p'th plane's sample starts at
// (planes[p].ptr + (j * planes[p].stride) + i).
//
// For packed pixel formats, the mapping is trivial: i = x and j = y. For
// planar pixel formats, the mapping can differ due to chroma subsampling. For
// example, consider a three plane YUV pixel format with 4:2:2 subsampling. For
// the luma (Y) channel, there is one sample for every pixel, but for the
// chroma (U, V) channels, there is one sample for every two pixels: pairs of
// horizontally adjacent pixels form one macropixel, i = x / 2 and j == y. In
// general, for a given p:
// - i = (x + bias_x) >> shift_x.
// - j = (y + bias_y) >> shift_y.
// where biases and shifts are in the range 0..3 and 0..2 respectively.
//
// In general, the biases will be zero after decoding an image. However, making
// a sub-image may change the bias, since the (x, y) coordinates are relative
// to the sub-image's top-left origin, but the backing pixel buffers were
// created relative to the original image's origin.
//
// For each plane p, each of those four numbers (biases and shifts) are encoded
// in two bits, which combine to form an 8 bit unsigned integer:
//
// e_p = (bias_x << 6) | (shift_x << 4) | (bias_y << 2) | (shift_y << 0)
//
// Those e_p values (e_0 for the first plane, e_1 for the second plane, etc)
// combine to form a wuffs_base__pixel_subsampling value:
//
// pixsub = (e_3 << 24) | (e_2 << 16) | (e_1 << 8) | (e_0 << 0)
//
// Do not manipulate these bits directly; they are private implementation
// details. Use methods such as wuffs_base__pixel_subsampling__bias_x instead.
typedef uint32_t wuffs_base__pixel_subsampling;
#define WUFFS_BASE__PIXEL_SUBSAMPLING__NONE ((wuffs_base__pixel_subsampling)0)
#define WUFFS_BASE__PIXEL_SUBSAMPLING__444 \
((wuffs_base__pixel_subsampling)0x000000)
#define WUFFS_BASE__PIXEL_SUBSAMPLING__440 \
((wuffs_base__pixel_subsampling)0x010100)
#define WUFFS_BASE__PIXEL_SUBSAMPLING__422 \
((wuffs_base__pixel_subsampling)0x101000)
#define WUFFS_BASE__PIXEL_SUBSAMPLING__420 \
((wuffs_base__pixel_subsampling)0x111100)
#define WUFFS_BASE__PIXEL_SUBSAMPLING__411 \
((wuffs_base__pixel_subsampling)0x202000)
#define WUFFS_BASE__PIXEL_SUBSAMPLING__410 \
((wuffs_base__pixel_subsampling)0x212100)
static inline uint32_t //
wuffs_base__pixel_subsampling__bias_x(wuffs_base__pixel_subsampling s,
uint32_t plane) {
uint32_t shift = ((plane & 0x03) * 8) + 6;
return (s >> shift) & 0x03;
}
static inline uint32_t //
wuffs_base__pixel_subsampling__shift_x(wuffs_base__pixel_subsampling s,
uint32_t plane) {
uint32_t shift = ((plane & 0x03) * 8) + 4;
return (s >> shift) & 0x03;
}
static inline uint32_t //
wuffs_base__pixel_subsampling__bias_y(wuffs_base__pixel_subsampling s,
uint32_t plane) {
uint32_t shift = ((plane & 0x03) * 8) + 2;
return (s >> shift) & 0x03;
}
static inline uint32_t //
wuffs_base__pixel_subsampling__shift_y(wuffs_base__pixel_subsampling s,
uint32_t plane) {
uint32_t shift = ((plane & 0x03) * 8) + 0;
return (s >> shift) & 0x03;
}
// --------
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 {
wuffs_base__pixel_format pixfmt;
wuffs_base__pixel_subsampling pixsub;
uint32_t width;
uint32_t height;
uint32_t num_loops;
bool first_frame_is_opaque;
} private_impl;
#ifdef __cplusplus
inline void initialize(wuffs_base__pixel_format pixfmt,
wuffs_base__pixel_subsampling pixsub,
uint32_t width,
uint32_t height,
uint32_t num_loops,
bool first_frame_is_opaque);
inline void invalidate();
inline bool is_valid();
inline wuffs_base__pixel_format pixel_format();
inline wuffs_base__pixel_subsampling pixel_subsampling();
inline wuffs_base__rect_ie_u32 bounds();
inline uint32_t width();
inline uint32_t height();
inline uint32_t num_loops();
inline uint32_t first_frame_is_opaque();
inline size_t pixbuf_size();
#endif // __cplusplus
} wuffs_base__image_config;
// TODO: Should this function return bool? An error type?
static inline void //
wuffs_base__image_config__initialize(wuffs_base__image_config* c,
wuffs_base__pixel_format pixfmt,
wuffs_base__pixel_subsampling pixsub,
uint32_t width,
uint32_t height,
uint32_t num_loops,
bool first_frame_is_opaque) {
if (!c) {
return;
}
if (pixfmt) {
uint64_t wh = ((uint64_t)width) * ((uint64_t)height);
// TODO: handle things other than 1 byte per pixel.
if (wh <= ((uint64_t)SIZE_MAX)) {
c->private_impl.pixfmt = pixfmt;
c->private_impl.pixsub = pixsub;
c->private_impl.width = width;
c->private_impl.height = height;
c->private_impl.num_loops = num_loops;
c->private_impl.first_frame_is_opaque = first_frame_is_opaque;
return;
}
}
*c = ((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__is_valid(wuffs_base__image_config* c) {
return c && c->private_impl.pixfmt;
}
static inline wuffs_base__pixel_format //
wuffs_base__image_config__pixel_format(wuffs_base__image_config* c) {
return c ? c->private_impl.pixfmt : 0;
}
static inline wuffs_base__pixel_subsampling //
wuffs_base__image_config__pixel_subsampling(wuffs_base__image_config* c) {
return c ? c->private_impl.pixsub : 0;
}
static inline wuffs_base__rect_ie_u32 //
wuffs_base__image_config__bounds(wuffs_base__image_config* c) {
return c ? ((wuffs_base__rect_ie_u32){
.min_incl_x = 0,
.min_incl_y = 0,
.max_excl_x = c->private_impl.width,
.max_excl_y = c->private_impl.height,
})
: ((wuffs_base__rect_ie_u32){});
}
static inline uint32_t //
wuffs_base__image_config__width(wuffs_base__image_config* c) {
return c ? c->private_impl.width : 0;
}
static inline uint32_t //
wuffs_base__image_config__height(wuffs_base__image_config* c) {
return c ? c->private_impl.height : 0;
}
static inline uint32_t //
wuffs_base__image_config__num_loops(wuffs_base__image_config* c) {
return c ? c->private_impl.num_loops : 0;
}
static inline uint32_t //
wuffs_base__image_config__first_frame_is_opaque(wuffs_base__image_config* c) {
return c ? c->private_impl.first_frame_is_opaque : false;
}
// 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 (c) {
uint64_t wh =
((uint64_t)c->private_impl.width) * ((uint64_t)c->private_impl.height);
// TODO: handle things other than 1 byte per pixel.
return (size_t)wh;
}
return 0;
}
#ifdef __cplusplus
inline void //
wuffs_base__image_config::initialize(wuffs_base__pixel_format pixfmt,
wuffs_base__pixel_subsampling pixsub,
uint32_t width,
uint32_t height,
uint32_t num_loops,
bool first_frame_is_opaque) {
wuffs_base__image_config__initialize(this, pixfmt, pixsub, width, height,
num_loops, first_frame_is_opaque);
}
inline void //
wuffs_base__image_config::invalidate() {
wuffs_base__image_config__invalidate(this);
}
inline bool //
wuffs_base__image_config::is_valid() {
return wuffs_base__image_config__is_valid(this);
}
inline wuffs_base__pixel_format //
wuffs_base__image_config::pixel_format() {
return wuffs_base__image_config__pixel_format(this);
}
inline wuffs_base__pixel_subsampling //
wuffs_base__image_config::pixel_subsampling() {
return wuffs_base__image_config__pixel_subsampling(this);
}
inline wuffs_base__rect_ie_u32 //
wuffs_base__image_config::bounds() {
return wuffs_base__image_config__bounds(this);
}
inline uint32_t //
wuffs_base__image_config::width() {
return wuffs_base__image_config__width(this);
}
inline uint32_t //
wuffs_base__image_config::height() {
return wuffs_base__image_config__height(this);
}
inline uint32_t //
wuffs_base__image_config::num_loops() {
return wuffs_base__image_config__num_loops(this);
}
inline uint32_t //
wuffs_base__image_config::first_frame_is_opaque() {
return wuffs_base__image_config__first_frame_is_opaque(this);
}
inline size_t //
wuffs_base__image_config::pixbuf_size() {
return wuffs_base__image_config__pixbuf_size(this);
}
#endif // __cplusplus
// --------
// wuffs_base__animation_disposal encodes, for an animated image, how to
// dispose of a frame after displaying it:
// - None means to draw the next frame on top of this one.
// - Restore Background means to clear the frame's dirty rectangle to "the
// background color" (in practice, this means transparent black) before
// drawing the next frame.
// - Restore Previous means to undo the current frame, so that the next frame
// is drawn on top of the previous one.
typedef uint8_t wuffs_base__animation_disposal;
#define WUFFS_BASE__ANIMATION_DISPOSAL__NONE ((wuffs_base__animation_disposal)0)
#define WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_BACKGROUND \
((wuffs_base__animation_disposal)1)
#define WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS \
((wuffs_base__animation_disposal)2)
// --------
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 {
wuffs_base__image_config config;
wuffs_base__pixel_buffer pixbuf;
// TODO: color spaces.
wuffs_base__rect_ie_u32 dirty_rect;
wuffs_base__flicks duration;
bool blend;
wuffs_base__animation_disposal disposal;
bool palette_changed;
uint8_t palette[1024];
} private_impl;
#ifdef __cplusplus
inline wuffs_base__status set_from_pixbuf(wuffs_base__image_config config,
wuffs_base__pixel_buffer pixbuf);
inline wuffs_base__status set_from_slice(wuffs_base__image_config config,
wuffs_base__slice_u8 pixbuf_memory);
inline void update(wuffs_base__rect_ie_u32 dirty_rect,
wuffs_base__flicks duration,
bool blend,
wuffs_base__animation_disposal disposal,
wuffs_base__slice_u8 palette);
inline wuffs_base__image_config* image_config();
inline wuffs_base__rect_ie_u32 bounds();
inline uint32_t width();
inline uint32_t height();
inline wuffs_base__rect_ie_u32 dirty_rect();
inline wuffs_base__flicks duration();
inline bool blend();
inline wuffs_base__animation_disposal disposal();
inline bool palette_changed();
inline wuffs_base__slice_u8 palette();
inline wuffs_base__table_u8 plane(uint32_t p);
#endif // __cplusplus
} wuffs_base__image_buffer;
static inline wuffs_base__status //
wuffs_base__image_buffer__set_from_pixbuf(wuffs_base__image_buffer* b,
wuffs_base__image_config config,
wuffs_base__pixel_buffer pixbuf) {
if (!b) {
return WUFFS_BASE__ERROR_BAD_RECEIVER;
}
*b = ((wuffs_base__image_buffer){});
b->private_impl.config = config;
b->private_impl.pixbuf = pixbuf;
return WUFFS_BASE__STATUS_OK;
}
static inline wuffs_base__status //
wuffs_base__image_buffer__set_from_slice(wuffs_base__image_buffer* b,
wuffs_base__image_config config,
wuffs_base__slice_u8 pixbuf_memory) {
if (!b) {
return WUFFS_BASE__ERROR_BAD_RECEIVER;
}
*b = ((wuffs_base__image_buffer){});
// TODO: don't assume 1 byte per pixel. Don't assume packed.
uint64_t wh = ((uint64_t)config.private_impl.width) *
((uint64_t)config.private_impl.height);
if (wh > pixbuf_memory.len) {
return WUFFS_BASE__ERROR_BAD_ARGUMENT_LENGTH_TOO_SHORT;
}
b->private_impl.config = config;
wuffs_base__table_u8* tab = &b->private_impl.pixbuf.planes[0];
tab->ptr = pixbuf_memory.ptr;
tab->width = config.private_impl.width;
tab->height = config.private_impl.height;
tab->stride = config.private_impl.width;
return WUFFS_BASE__STATUS_OK;
}
// The palette argument is ignored unless its length is exactly 1024.
static inline void //
wuffs_base__image_buffer__update(wuffs_base__image_buffer* b,
wuffs_base__rect_ie_u32 dirty_rect,
wuffs_base__flicks duration,
bool blend,
wuffs_base__animation_disposal disposal,
wuffs_base__slice_u8 palette) {
if (!b) {
return;
}
// Clip the dirty_rect to the image bounds.
dirty_rect.max_excl_x = wuffs_base__u32__min(
dirty_rect.max_excl_x, b->private_impl.config.private_impl.width);
dirty_rect.max_excl_y = wuffs_base__u32__min(
dirty_rect.max_excl_y, b->private_impl.config.private_impl.height);
b->private_impl.dirty_rect = dirty_rect;
b->private_impl.duration = duration;
b->private_impl.blend = blend;
b->private_impl.disposal = disposal;
b->private_impl.palette_changed = palette.ptr && (palette.len == 1024);
if (b->private_impl.palette_changed) {
memmove(b->private_impl.palette, palette.ptr, 1024);
}
}
// wuffs_base__image_config returns the overall configuration for this frame.
static inline wuffs_base__image_config* //
wuffs_base__image_buffer__image_config(wuffs_base__image_buffer* b) {
return b ? &b->private_impl.config : NULL;
}
static inline wuffs_base__rect_ie_u32 //
wuffs_base__image_buffer__bounds(wuffs_base__image_buffer* b) {
return b ? wuffs_base__image_config__bounds(&b->private_impl.config)
: ((wuffs_base__rect_ie_u32){});
}
static inline uint32_t //
wuffs_base__image_buffer__width(wuffs_base__image_buffer* b) {
return b ? wuffs_base__image_config__width(&b->private_impl.config) : 0;
}
static inline uint32_t //
wuffs_base__image_buffer__height(wuffs_base__image_buffer* b) {
return b ? wuffs_base__image_config__height(&b->private_impl.config) : 0;
}
// wuffs_base__image_buffer__dirty_rect returns an upper bound for what part of
// this frame's pixels differs from the previous frame.
static inline wuffs_base__rect_ie_u32 //
wuffs_base__image_buffer__dirty_rect(wuffs_base__image_buffer* b) {
return b ? b->private_impl.dirty_rect : ((wuffs_base__rect_ie_u32){});
}
// wuffs_base__image_buffer__duration returns the amount of time to display
// this frame. Zero means to display forever - a still (non-animated) image.
static inline wuffs_base__flicks //
wuffs_base__image_buffer__duration(wuffs_base__image_buffer* b) {
return b ? b->private_impl.duration : 0;
}
// wuffs_base__image_buffer__blend returns, for a transparent image, whether to
// blend this frame with the existing canvas.
//
// In Porter-Duff compositing operator terminology, false means "src" and true
// means "src over dst".
static inline bool //
wuffs_base__image_buffer__blend(wuffs_base__image_buffer* b) {
return b && b->private_impl.blend;
}
// wuffs_base__image_buffer__disposal returns, for an animated image, how to
// dispose of this frame after displaying it.
static inline wuffs_base__animation_disposal //
wuffs_base__image_buffer__disposal(wuffs_base__image_buffer* b) {
return b ? b->private_impl.disposal : 0;
}
// wuffs_base__image_buffer__palette_changed returns whether this frame's
// palette differs from the previous frame. It is conservative and may return
// false positives (but never false negatives).
static inline bool //
wuffs_base__image_buffer__palette_changed(wuffs_base__image_buffer* b) {
return b && b->private_impl.palette_changed;
}
// wuffs_base__image_buffer__palette returns the palette that the pixel data
// can index. The backing array is inside b and has length 1024.
static inline wuffs_base__slice_u8 //
wuffs_base__image_buffer__palette(wuffs_base__image_buffer* b) {
return b ? ((wuffs_base__slice_u8){.ptr = b->private_impl.palette,
.len = 1024})
: ((wuffs_base__slice_u8){});
}
static inline wuffs_base__table_u8 //
wuffs_base__image_buffer__plane(wuffs_base__image_buffer* b, uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#ifdef __cplusplus
inline wuffs_base__status //
wuffs_base__image_buffer::set_from_pixbuf(wuffs_base__image_config config,
wuffs_base__pixel_buffer pixbuf) {
return wuffs_base__image_buffer__set_from_pixbuf(this, config, pixbuf);
}
inline wuffs_base__status //
wuffs_base__image_buffer::set_from_slice(wuffs_base__image_config config,
wuffs_base__slice_u8 pixbuf_memory) {
return wuffs_base__image_buffer__set_from_slice(this, config, pixbuf_memory);
}
inline void //
wuffs_base__image_buffer::update(wuffs_base__rect_ie_u32 dirty_rect,
wuffs_base__flicks duration,
bool blend,
wuffs_base__animation_disposal disposal,
wuffs_base__slice_u8 palette) {
wuffs_base__image_buffer__update(this, dirty_rect, duration, blend, disposal,
palette);
}
inline wuffs_base__image_config* //
wuffs_base__image_buffer::image_config() {
return wuffs_base__image_buffer__image_config(this);
}
inline wuffs_base__rect_ie_u32 //
wuffs_base__image_buffer::bounds() {
return wuffs_base__image_buffer__bounds(this);
}
inline uint32_t //
wuffs_base__image_buffer::width() {
return wuffs_base__image_buffer__width(this);
}
inline uint32_t //
wuffs_base__image_buffer::height() {
return wuffs_base__image_buffer__height(this);
}
inline wuffs_base__rect_ie_u32 //
wuffs_base__image_buffer::dirty_rect() {
return wuffs_base__image_buffer__dirty_rect(this);
}
inline wuffs_base__flicks //
wuffs_base__image_buffer::duration() {
return wuffs_base__image_buffer__duration(this);
}
inline bool //
wuffs_base__image_buffer::blend() {
return wuffs_base__image_buffer__blend(this);
}
inline wuffs_base__animation_disposal //
wuffs_base__image_buffer::disposal() {
return wuffs_base__image_buffer__disposal(this);
}
inline bool //
wuffs_base__image_buffer::palette_changed() {
return wuffs_base__image_buffer__palette_changed(this);
}
inline wuffs_base__slice_u8 //
wuffs_base__image_buffer::palette() {
return wuffs_base__image_buffer__palette(this);
}
inline wuffs_base__table_u8 //
wuffs_base__image_buffer::plane(uint32_t p) {
return wuffs_base__image_buffer__plane(this, p);
}
#endif // __cplusplus
#ifdef __cplusplus
} // extern "C"
#endif
#endif // WUFFS_INCLUDE_GUARD__BASE_PUBLIC
@@ -0,0 +1,761 @@
// After editing this file, run "go generate" in the parent directory.
// Copyright 2017 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifdef __cplusplus
extern "C" {
#endif
// ---------------- Images
// wuffs_base__color_u32argb is an 8 bit per channel Alpha, Red, Green, Blue
// color, as a uint32_t value. It is in word order, not byte order: its value
// is always 0xAARRGGBB, regardless of endianness. It uses premultiplied alpha.
typedef uint32_t wuffs_base__color_u32argb;
// --------
// wuffs_base__pixel_format encodes the format of the bytes that constitute an
// image frame's pixel data. Its bits:
// - bit 31 is reserved.
// - bits 30 .. 28 encodes color (and channel order, in terms of memory).
// - bits 27 .. 26 are reserved.
// - bits 25 .. 24 encodes transparency.
// - bit 23 indicates big-endian/MSB-first (as opposed to little/LSB).
// - bit 22 indicates floating point (as opposed to integer).
// - bits 21 .. 20 are the number of planes, minus 1. Zero means packed.
// - bits 19 .. 16 encodes the number of bits (depth) in an index value.
// Zero means direct, not palette-indexed.
// - bits 15 .. 12 encodes the number of bits (depth) in the 3rd channel.
// - bits 11 .. 8 encodes the number of bits (depth) in the 2nd channel.
// - bits 7 .. 4 encodes the number of bits (depth) in the 1st channel.
// - bits 3 .. 0 encodes the number of bits (depth) in the 0th channel.
//
// The bit fields of a wuffs_base__pixel_format are not independent. For
// example, the number of planes should not be greater than the number of
// channels. Similarly, bits 15..4 are unused (and should be zero) if bits
// 31..24 (color and transparency) together imply only 1 channel (gray, no
// alpha) and floating point samples should mean a bit depth of 16, 32 or 64.
//
// Formats hold between 1 and 4 channels. For example: Y (1 channel: gray), YA
// (2 channels: gray and alpha), BGR (3 channels: blue, green, red) or CMYK (4
// channels: cyan, magenta, yellow, black).
//
// For direct formats with N > 1 channels, those channels can be laid out in
// either 1 (packed) or N (planar) planes. For example, RGBA data is usually
// packed, but YUV data is usually planar, due to chroma subsampling (for
// details, see the wuffs_base__pixel_subsampling type). For indexed formats,
// the palette (always 256 × 4 bytes) holds up to 4 packed bytes of color data
// per index value, and there is only 1 plane (for the index). The distance
// between successive palette elements is always 4 bytes.
//
// The color field is encoded in 3 bits:
// - 0 means A (Alpha).
// - 1 means Y or YA (Gray, Alpha).
// - 2 means BGR, BGRX or BGRA (Blue, Green, Red, X-padding or Alpha).
// - 3 means RGB, RGBX or RGBA (Red, Green, Blue, X-padding or Alpha).
// - 4 means YUV or YUVA (Luma, Chroma-blue, Chroma-red, Alpha).
// - 5 means CMY or CMYK (Cyan, Magenta, Yellow, Black).
// - all other values are reserved.
//
// In Wuffs, channels are given in memory order (also known as byte order),
// regardless of endianness, since the C type for the pixel data is an array of
// bytes, not an array of uint32_t. For example, packed BGRA with 8 bits per
// channel means that the bytes in memory are always Blue, Green, Red then
// Alpha. On big-endian systems, that is the uint32_t 0xBBGGRRAA. On
// little-endian, 0xAARRGGBB.
//
// When the color field (3 bits) encodes multiple options, the transparency
// field (2 bits) distinguishes them:
// - 0 means fully opaque, no extra channels
// - 1 means fully opaque, one extra channel (X or K, padding or black).
// - 2 means one extra alpha channel, other channels are non-premultiplied.
// - 3 means one extra alpha channel, other channels are premultiplied.
//
// The zero wuffs_base__pixel_format value is an invalid pixel format, as it is
// invalid to combine the zero color (alpha only) with the zero transparency.
//
// Bit depth is encoded in 4 bits:
// - 0 means the channel or index is unused.
// - x means a bit depth of x, for x in the range 1..8.
// - 9 means a bit depth of 10.
// - 10 means a bit depth of 12.
// - 11 means a bit depth of 16.
// - 12 means a bit depth of 24.
// - 13 means a bit depth of 32.
// - 14 means a bit depth of 48.
// - 15 means a bit depth of 64.
//
// For example, wuffs_base__pixel_format 0x3280BBBB is a natural format for
// decoding a PNG image - network byte order (also known as big-endian),
// packed, non-premultiplied alpha - that happens to be 16-bit-depth truecolor
// with alpha (RGBA). In memory order:
//
// ptr+0 ptr+1 ptr+2 ptr+3 ptr+4 ptr+5 ptr+6 ptr+7
// Rhi Rlo Ghi Glo Bhi Blo Ahi Alo
//
// For example, the value wuffs_base__pixel_format 0x20000565 means BGR with no
// alpha or padding, 5/6/5 bits for blue/green/red, packed 2 bytes per pixel,
// laid out LSB-first in memory order:
//
// ptr+0........... ptr+1...........
// MSB LSB MSB LSB
// G₂G₁G₀B₄B₃B₂B₁B₀ R₄R₃R₂R₁R₀G₅G₄G₃
//
// On little-endian systems (but not big-endian), this Wuffs pixel format value
// (0x20000565) corresponds to the Cairo library's CAIRO_FORMAT_RGB16_565, the
// SDL2 (Simple DirectMedia Layer 2) library's SDL_PIXELFORMAT_RGB565 and the
// Skia library's kRGB_565_SkColorType. Note BGR in Wuffs versus RGB in the
// other libraries.
//
// Regardless of endianness, this Wuffs pixel format value (0x20000565)
// corresponds to the V4L2 (Video For Linux 2) library's V4L2_PIX_FMT_RGB565
// and the Wayland-DRM library's WL_DRM_FORMAT_RGB565.
//
// Different software libraries name their pixel formats (and especially their
// channel order) either according to memory layout or as bits of a native
// integer type like uint32_t. The two conventions differ because of a system's
// endianness. As mentioned earlier, Wuffs pixel formats are always in memory
// order. More detail of other software libraries' naming conventions is in the
// Pixel Format Guide at https://afrantzis.github.io/pixel-format-guide/
//
// Do not manipulate these bits directly; they are private implementation
// details. Use methods such as wuffs_base__pixel_format__num_planes instead.
typedef uint32_t wuffs_base__pixel_format;
// Common 8-bit-depth pixel formats. This list is not exhaustive; not all valid
// wuffs_base__pixel_format values are present.
#define WUFFS_BASE__PIXEL_FORMAT__INVALID ((wuffs_base__pixel_format)0x00000000)
#define WUFFS_BASE__PIXEL_FORMAT__A ((wuffs_base__pixel_format)0x02000008)
#define WUFFS_BASE__PIXEL_FORMAT__Y ((wuffs_base__pixel_format)0x10000008)
#define WUFFS_BASE__PIXEL_FORMAT__YA_NONPREMUL \
((wuffs_base__pixel_format)0x12000008)
#define WUFFS_BASE__PIXEL_FORMAT__YA_PREMUL \
((wuffs_base__pixel_format)0x13000008)
#define WUFFS_BASE__PIXEL_FORMAT__BGR ((wuffs_base__pixel_format)0x20000888)
#define WUFFS_BASE__PIXEL_FORMAT__BGRX ((wuffs_base__pixel_format)0x21008888)
#define WUFFS_BASE__PIXEL_FORMAT__BGRX_INDEXED \
((wuffs_base__pixel_format)0x21088888)
#define WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL \
((wuffs_base__pixel_format)0x22008888)
#define WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_INDEXED \
((wuffs_base__pixel_format)0x22088888)
#define WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL \
((wuffs_base__pixel_format)0x23008888)
#define WUFFS_BASE__PIXEL_FORMAT__RGB ((wuffs_base__pixel_format)0x30000888)
#define WUFFS_BASE__PIXEL_FORMAT__RGBX ((wuffs_base__pixel_format)0x31008888)
#define WUFFS_BASE__PIXEL_FORMAT__RGBX_INDEXED \
((wuffs_base__pixel_format)0x31088888)
#define WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL \
((wuffs_base__pixel_format)0x32008888)
#define WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL_INDEXED \
((wuffs_base__pixel_format)0x32088888)
#define WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL \
((wuffs_base__pixel_format)0x33008888)
#define WUFFS_BASE__PIXEL_FORMAT__YUV ((wuffs_base__pixel_format)0x40200888)
#define WUFFS_BASE__PIXEL_FORMAT__YUVK ((wuffs_base__pixel_format)0x41308888)
#define WUFFS_BASE__PIXEL_FORMAT__YUVA_NONPREMUL \
((wuffs_base__pixel_format)0x42308888)
#define WUFFS_BASE__PIXEL_FORMAT__CMY ((wuffs_base__pixel_format)0x50200888)
#define WUFFS_BASE__PIXEL_FORMAT__CMYK ((wuffs_base__pixel_format)0x51308888)
static inline bool //
wuffs_base__pixel_format__is_valid(wuffs_base__pixel_format f) {
return f != 0;
}
static inline bool //
wuffs_base__pixel_format__is_indexed(wuffs_base__pixel_format f) {
return ((f >> 16) & 0x0F) != 0;
}
#define WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX 4
static inline uint32_t //
wuffs_base__pixel_format__num_planes(wuffs_base__pixel_format f) {
return f ? (((f >> 20) & 0x03) + 1) : 0;
}
typedef struct {
wuffs_base__table_u8 planes[WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX];
} wuffs_base__pixel_buffer;
// --------
// wuffs_base__pixel_subsampling encodes the mapping of pixel space coordinates
// (x, y) to pixel buffer indices (i, j). That mapping can differ for each
// plane p. For a depth of 8 bits (1 byte), the p'th plane's sample starts at
// (planes[p].ptr + (j * planes[p].stride) + i).
//
// For packed pixel formats, the mapping is trivial: i = x and j = y. For
// planar pixel formats, the mapping can differ due to chroma subsampling. For
// example, consider a three plane YUV pixel format with 4:2:2 subsampling. For
// the luma (Y) channel, there is one sample for every pixel, but for the
// chroma (U, V) channels, there is one sample for every two pixels: pairs of
// horizontally adjacent pixels form one macropixel, i = x / 2 and j == y. In
// general, for a given p:
// - i = (x + bias_x) >> shift_x.
// - j = (y + bias_y) >> shift_y.
// where biases and shifts are in the range 0..3 and 0..2 respectively.
//
// In general, the biases will be zero after decoding an image. However, making
// a sub-image may change the bias, since the (x, y) coordinates are relative
// to the sub-image's top-left origin, but the backing pixel buffers were
// created relative to the original image's origin.
//
// For each plane p, each of those four numbers (biases and shifts) are encoded
// in two bits, which combine to form an 8 bit unsigned integer:
//
// e_p = (bias_x << 6) | (shift_x << 4) | (bias_y << 2) | (shift_y << 0)
//
// Those e_p values (e_0 for the first plane, e_1 for the second plane, etc)
// combine to form a wuffs_base__pixel_subsampling value:
//
// pixsub = (e_3 << 24) | (e_2 << 16) | (e_1 << 8) | (e_0 << 0)
//
// Do not manipulate these bits directly; they are private implementation
// details. Use methods such as wuffs_base__pixel_subsampling__bias_x instead.
typedef uint32_t wuffs_base__pixel_subsampling;
#define WUFFS_BASE__PIXEL_SUBSAMPLING__NONE ((wuffs_base__pixel_subsampling)0)
#define WUFFS_BASE__PIXEL_SUBSAMPLING__444 \
((wuffs_base__pixel_subsampling)0x000000)
#define WUFFS_BASE__PIXEL_SUBSAMPLING__440 \
((wuffs_base__pixel_subsampling)0x010100)
#define WUFFS_BASE__PIXEL_SUBSAMPLING__422 \
((wuffs_base__pixel_subsampling)0x101000)
#define WUFFS_BASE__PIXEL_SUBSAMPLING__420 \
((wuffs_base__pixel_subsampling)0x111100)
#define WUFFS_BASE__PIXEL_SUBSAMPLING__411 \
((wuffs_base__pixel_subsampling)0x202000)
#define WUFFS_BASE__PIXEL_SUBSAMPLING__410 \
((wuffs_base__pixel_subsampling)0x212100)
static inline uint32_t //
wuffs_base__pixel_subsampling__bias_x(wuffs_base__pixel_subsampling s,
uint32_t plane) {
uint32_t shift = ((plane & 0x03) * 8) + 6;
return (s >> shift) & 0x03;
}
static inline uint32_t //
wuffs_base__pixel_subsampling__shift_x(wuffs_base__pixel_subsampling s,
uint32_t plane) {
uint32_t shift = ((plane & 0x03) * 8) + 4;
return (s >> shift) & 0x03;
}
static inline uint32_t //
wuffs_base__pixel_subsampling__bias_y(wuffs_base__pixel_subsampling s,
uint32_t plane) {
uint32_t shift = ((plane & 0x03) * 8) + 2;
return (s >> shift) & 0x03;
}
static inline uint32_t //
wuffs_base__pixel_subsampling__shift_y(wuffs_base__pixel_subsampling s,
uint32_t plane) {
uint32_t shift = ((plane & 0x03) * 8) + 0;
return (s >> shift) & 0x03;
}
// --------
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 {
wuffs_base__pixel_format pixfmt;
wuffs_base__pixel_subsampling pixsub;
uint32_t width;
uint32_t height;
uint32_t num_loops;
bool first_frame_is_opaque;
} private_impl;
#ifdef __cplusplus
inline void initialize(wuffs_base__pixel_format pixfmt,
wuffs_base__pixel_subsampling pixsub,
uint32_t width,
uint32_t height,
uint32_t num_loops,
bool first_frame_is_opaque);
inline void invalidate();
inline bool is_valid();
inline wuffs_base__pixel_format pixel_format();
inline wuffs_base__pixel_subsampling pixel_subsampling();
inline wuffs_base__rect_ie_u32 bounds();
inline uint32_t width();
inline uint32_t height();
inline uint32_t num_loops();
inline uint32_t first_frame_is_opaque();
inline size_t pixbuf_size();
#endif // __cplusplus
} wuffs_base__image_config;
// TODO: Should this function return bool? An error type?
static inline void //
wuffs_base__image_config__initialize(wuffs_base__image_config* c,
wuffs_base__pixel_format pixfmt,
wuffs_base__pixel_subsampling pixsub,
uint32_t width,
uint32_t height,
uint32_t num_loops,
bool first_frame_is_opaque) {
if (!c) {
return;
}
if (pixfmt) {
uint64_t wh = ((uint64_t)width) * ((uint64_t)height);
// TODO: handle things other than 1 byte per pixel.
if (wh <= ((uint64_t)SIZE_MAX)) {
c->private_impl.pixfmt = pixfmt;
c->private_impl.pixsub = pixsub;
c->private_impl.width = width;
c->private_impl.height = height;
c->private_impl.num_loops = num_loops;
c->private_impl.first_frame_is_opaque = first_frame_is_opaque;
return;
}
}
*c = ((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__is_valid(wuffs_base__image_config* c) {
return c && c->private_impl.pixfmt;
}
static inline wuffs_base__pixel_format //
wuffs_base__image_config__pixel_format(wuffs_base__image_config* c) {
return c ? c->private_impl.pixfmt : 0;
}
static inline wuffs_base__pixel_subsampling //
wuffs_base__image_config__pixel_subsampling(wuffs_base__image_config* c) {
return c ? c->private_impl.pixsub : 0;
}
static inline wuffs_base__rect_ie_u32 //
wuffs_base__image_config__bounds(wuffs_base__image_config* c) {
return c ? ((wuffs_base__rect_ie_u32){
.min_incl_x = 0,
.min_incl_y = 0,
.max_excl_x = c->private_impl.width,
.max_excl_y = c->private_impl.height,
})
: ((wuffs_base__rect_ie_u32){});
}
static inline uint32_t //
wuffs_base__image_config__width(wuffs_base__image_config* c) {
return c ? c->private_impl.width : 0;
}
static inline uint32_t //
wuffs_base__image_config__height(wuffs_base__image_config* c) {
return c ? c->private_impl.height : 0;
}
static inline uint32_t //
wuffs_base__image_config__num_loops(wuffs_base__image_config* c) {
return c ? c->private_impl.num_loops : 0;
}
static inline uint32_t //
wuffs_base__image_config__first_frame_is_opaque(wuffs_base__image_config* c) {
return c ? c->private_impl.first_frame_is_opaque : false;
}
// 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 (c) {
uint64_t wh =
((uint64_t)c->private_impl.width) * ((uint64_t)c->private_impl.height);
// TODO: handle things other than 1 byte per pixel.
return (size_t)wh;
}
return 0;
}
#ifdef __cplusplus
inline void //
wuffs_base__image_config::initialize(wuffs_base__pixel_format pixfmt,
wuffs_base__pixel_subsampling pixsub,
uint32_t width,
uint32_t height,
uint32_t num_loops,
bool first_frame_is_opaque) {
wuffs_base__image_config__initialize(this, pixfmt, pixsub, width, height,
num_loops, first_frame_is_opaque);
}
inline void //
wuffs_base__image_config::invalidate() {
wuffs_base__image_config__invalidate(this);
}
inline bool //
wuffs_base__image_config::is_valid() {
return wuffs_base__image_config__is_valid(this);
}
inline wuffs_base__pixel_format //
wuffs_base__image_config::pixel_format() {
return wuffs_base__image_config__pixel_format(this);
}
inline wuffs_base__pixel_subsampling //
wuffs_base__image_config::pixel_subsampling() {
return wuffs_base__image_config__pixel_subsampling(this);
}
inline wuffs_base__rect_ie_u32 //
wuffs_base__image_config::bounds() {
return wuffs_base__image_config__bounds(this);
}
inline uint32_t //
wuffs_base__image_config::width() {
return wuffs_base__image_config__width(this);
}
inline uint32_t //
wuffs_base__image_config::height() {
return wuffs_base__image_config__height(this);
}
inline uint32_t //
wuffs_base__image_config::num_loops() {
return wuffs_base__image_config__num_loops(this);
}
inline uint32_t //
wuffs_base__image_config::first_frame_is_opaque() {
return wuffs_base__image_config__first_frame_is_opaque(this);
}
inline size_t //
wuffs_base__image_config::pixbuf_size() {
return wuffs_base__image_config__pixbuf_size(this);
}
#endif // __cplusplus
// --------
// wuffs_base__animation_disposal encodes, for an animated image, how to
// dispose of a frame after displaying it:
// - None means to draw the next frame on top of this one.
// - Restore Background means to clear the frame's dirty rectangle to "the
// background color" (in practice, this means transparent black) before
// drawing the next frame.
// - Restore Previous means to undo the current frame, so that the next frame
// is drawn on top of the previous one.
typedef uint8_t wuffs_base__animation_disposal;
#define WUFFS_BASE__ANIMATION_DISPOSAL__NONE ((wuffs_base__animation_disposal)0)
#define WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_BACKGROUND \
((wuffs_base__animation_disposal)1)
#define WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS \
((wuffs_base__animation_disposal)2)
// --------
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 {
wuffs_base__image_config config;
wuffs_base__pixel_buffer pixbuf;
// TODO: color spaces.
wuffs_base__rect_ie_u32 dirty_rect;
wuffs_base__flicks duration;
bool blend;
wuffs_base__animation_disposal disposal;
bool palette_changed;
uint8_t palette[1024];
} private_impl;
#ifdef __cplusplus
inline wuffs_base__status set_from_pixbuf(wuffs_base__image_config config,
wuffs_base__pixel_buffer pixbuf);
inline wuffs_base__status set_from_slice(wuffs_base__image_config config,
wuffs_base__slice_u8 pixbuf_memory);
inline void update(wuffs_base__rect_ie_u32 dirty_rect,
wuffs_base__flicks duration,
bool blend,
wuffs_base__animation_disposal disposal,
wuffs_base__slice_u8 palette);
inline wuffs_base__image_config* image_config();
inline wuffs_base__rect_ie_u32 bounds();
inline uint32_t width();
inline uint32_t height();
inline wuffs_base__rect_ie_u32 dirty_rect();
inline wuffs_base__flicks duration();
inline bool blend();
inline wuffs_base__animation_disposal disposal();
inline bool palette_changed();
inline wuffs_base__slice_u8 palette();
inline wuffs_base__table_u8 plane(uint32_t p);
#endif // __cplusplus
} wuffs_base__image_buffer;
static inline wuffs_base__status //
wuffs_base__image_buffer__set_from_pixbuf(wuffs_base__image_buffer* b,
wuffs_base__image_config config,
wuffs_base__pixel_buffer pixbuf) {
if (!b) {
return WUFFS_BASE__ERROR_BAD_RECEIVER;
}
*b = ((wuffs_base__image_buffer){});
b->private_impl.config = config;
b->private_impl.pixbuf = pixbuf;
return WUFFS_BASE__STATUS_OK;
}
static inline wuffs_base__status //
wuffs_base__image_buffer__set_from_slice(wuffs_base__image_buffer* b,
wuffs_base__image_config config,
wuffs_base__slice_u8 pixbuf_memory) {
if (!b) {
return WUFFS_BASE__ERROR_BAD_RECEIVER;
}
*b = ((wuffs_base__image_buffer){});
// TODO: don't assume 1 byte per pixel. Don't assume packed.
uint64_t wh = ((uint64_t)config.private_impl.width) *
((uint64_t)config.private_impl.height);
if (wh > pixbuf_memory.len) {
return WUFFS_BASE__ERROR_BAD_ARGUMENT_LENGTH_TOO_SHORT;
}
b->private_impl.config = config;
wuffs_base__table_u8* tab = &b->private_impl.pixbuf.planes[0];
tab->ptr = pixbuf_memory.ptr;
tab->width = config.private_impl.width;
tab->height = config.private_impl.height;
tab->stride = config.private_impl.width;
return WUFFS_BASE__STATUS_OK;
}
// The palette argument is ignored unless its length is exactly 1024.
static inline void //
wuffs_base__image_buffer__update(wuffs_base__image_buffer* b,
wuffs_base__rect_ie_u32 dirty_rect,
wuffs_base__flicks duration,
bool blend,
wuffs_base__animation_disposal disposal,
wuffs_base__slice_u8 palette) {
if (!b) {
return;
}
// Clip the dirty_rect to the image bounds.
dirty_rect.max_excl_x = wuffs_base__u32__min(
dirty_rect.max_excl_x, b->private_impl.config.private_impl.width);
dirty_rect.max_excl_y = wuffs_base__u32__min(
dirty_rect.max_excl_y, b->private_impl.config.private_impl.height);
b->private_impl.dirty_rect = dirty_rect;
b->private_impl.duration = duration;
b->private_impl.blend = blend;
b->private_impl.disposal = disposal;
b->private_impl.palette_changed = palette.ptr && (palette.len == 1024);
if (b->private_impl.palette_changed) {
memmove(b->private_impl.palette, palette.ptr, 1024);
}
}
// wuffs_base__image_config returns the overall configuration for this frame.
static inline wuffs_base__image_config* //
wuffs_base__image_buffer__image_config(wuffs_base__image_buffer* b) {
return b ? &b->private_impl.config : NULL;
}
static inline wuffs_base__rect_ie_u32 //
wuffs_base__image_buffer__bounds(wuffs_base__image_buffer* b) {
return b ? wuffs_base__image_config__bounds(&b->private_impl.config)
: ((wuffs_base__rect_ie_u32){});
}
static inline uint32_t //
wuffs_base__image_buffer__width(wuffs_base__image_buffer* b) {
return b ? wuffs_base__image_config__width(&b->private_impl.config) : 0;
}
static inline uint32_t //
wuffs_base__image_buffer__height(wuffs_base__image_buffer* b) {
return b ? wuffs_base__image_config__height(&b->private_impl.config) : 0;
}
// wuffs_base__image_buffer__dirty_rect returns an upper bound for what part of
// this frame's pixels differs from the previous frame.
static inline wuffs_base__rect_ie_u32 //
wuffs_base__image_buffer__dirty_rect(wuffs_base__image_buffer* b) {
return b ? b->private_impl.dirty_rect : ((wuffs_base__rect_ie_u32){});
}
// wuffs_base__image_buffer__duration returns the amount of time to display
// this frame. Zero means to display forever - a still (non-animated) image.
static inline wuffs_base__flicks //
wuffs_base__image_buffer__duration(wuffs_base__image_buffer* b) {
return b ? b->private_impl.duration : 0;
}
// wuffs_base__image_buffer__blend returns, for a transparent image, whether to
// blend this frame with the existing canvas.
//
// In Porter-Duff compositing operator terminology, false means "src" and true
// means "src over dst".
static inline bool //
wuffs_base__image_buffer__blend(wuffs_base__image_buffer* b) {
return b && b->private_impl.blend;
}
// wuffs_base__image_buffer__disposal returns, for an animated image, how to
// dispose of this frame after displaying it.
static inline wuffs_base__animation_disposal //
wuffs_base__image_buffer__disposal(wuffs_base__image_buffer* b) {
return b ? b->private_impl.disposal : 0;
}
// wuffs_base__image_buffer__palette_changed returns whether this frame's
// palette differs from the previous frame. It is conservative and may return
// false positives (but never false negatives).
static inline bool //
wuffs_base__image_buffer__palette_changed(wuffs_base__image_buffer* b) {
return b && b->private_impl.palette_changed;
}
// wuffs_base__image_buffer__palette returns the palette that the pixel data
// can index. The backing array is inside b and has length 1024.
static inline wuffs_base__slice_u8 //
wuffs_base__image_buffer__palette(wuffs_base__image_buffer* b) {
return b ? ((wuffs_base__slice_u8){.ptr = b->private_impl.palette,
.len = 1024})
: ((wuffs_base__slice_u8){});
}
static inline wuffs_base__table_u8 //
wuffs_base__image_buffer__plane(wuffs_base__image_buffer* b, uint32_t p) {
return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))
? b->private_impl.pixbuf.planes[p]
: ((wuffs_base__table_u8){});
}
#ifdef __cplusplus
inline wuffs_base__status //
wuffs_base__image_buffer::set_from_pixbuf(wuffs_base__image_config config,
wuffs_base__pixel_buffer pixbuf) {
return wuffs_base__image_buffer__set_from_pixbuf(this, config, pixbuf);
}
inline wuffs_base__status //
wuffs_base__image_buffer::set_from_slice(wuffs_base__image_config config,
wuffs_base__slice_u8 pixbuf_memory) {
return wuffs_base__image_buffer__set_from_slice(this, config, pixbuf_memory);
}
inline void //
wuffs_base__image_buffer::update(wuffs_base__rect_ie_u32 dirty_rect,
wuffs_base__flicks duration,
bool blend,
wuffs_base__animation_disposal disposal,
wuffs_base__slice_u8 palette) {
wuffs_base__image_buffer__update(this, dirty_rect, duration, blend, disposal,
palette);
}
inline wuffs_base__image_config* //
wuffs_base__image_buffer::image_config() {
return wuffs_base__image_buffer__image_config(this);
}
inline wuffs_base__rect_ie_u32 //
wuffs_base__image_buffer::bounds() {
return wuffs_base__image_buffer__bounds(this);
}
inline uint32_t //
wuffs_base__image_buffer::width() {
return wuffs_base__image_buffer__width(this);
}
inline uint32_t //
wuffs_base__image_buffer::height() {
return wuffs_base__image_buffer__height(this);
}
inline wuffs_base__rect_ie_u32 //
wuffs_base__image_buffer::dirty_rect() {
return wuffs_base__image_buffer__dirty_rect(this);
}
inline wuffs_base__flicks //
wuffs_base__image_buffer::duration() {
return wuffs_base__image_buffer__duration(this);
}
inline bool //
wuffs_base__image_buffer::blend() {
return wuffs_base__image_buffer__blend(this);
}
inline wuffs_base__animation_disposal //
wuffs_base__image_buffer::disposal() {
return wuffs_base__image_buffer__disposal(this);
}
inline bool //
wuffs_base__image_buffer::palette_changed() {
return wuffs_base__image_buffer__palette_changed(this);
}
inline wuffs_base__slice_u8 //
wuffs_base__image_buffer::palette() {
return wuffs_base__image_buffer__palette(this);
}
inline wuffs_base__table_u8 //
wuffs_base__image_buffer::plane(uint32_t p) {
return wuffs_base__image_buffer__plane(this, p);
}
#endif // __cplusplus
#ifdef __cplusplus
} // extern "C"
#endif
+11 -4
View File
@@ -80,7 +80,7 @@ func Do(args []string) error {
return nil, fmt.Errorf("base package shouldn't have any .wuffs files")
}
buf := make(buffer, 0, 128*1024)
if err := expandBangBangInsert(&buf, baseImplC, map[string]func(*buffer) error{
if err := expandBangBangInsert(&buf, baseBaseImplC, map[string]func(*buffer) error{
"// !! INSERT base-private.h.\n": insertBasePrivateH,
"// !! INSERT base-public.h.\n": insertBasePublicH,
"// !! INSERT wuffs_base__status__string data.\n": func(b *buffer) error {
@@ -197,13 +197,16 @@ func expandBangBangInsert(b *buffer, s string, m map[string]func(*buffer) error)
}
func insertBasePrivateH(buf *buffer) error {
buf.writes(basePrivateH)
buf.writes(baseBasePrivateH)
return nil
}
func insertBasePublicH(buf *buffer) error {
buf.writes("// Code generated by wuffs-c. DO NOT EDIT.\n\n")
if err := expandBangBangInsert(buf, basePublicH, map[string]func(*buffer) error{
buf.writes("#ifndef WUFFS_INCLUDE_GUARD__BASE_PUBLIC\n")
buf.writes("#define WUFFS_INCLUDE_GUARD__BASE_PUBLIC\n\n")
if err := expandBangBangInsert(buf, baseBasePublicH, map[string]func(*buffer) error{
"// !! INSERT wuffs_base__status names.\n": func(b *buffer) error {
for _, z := range builtin.StatusList {
code := int32(z.Value) << 24
@@ -216,7 +219,11 @@ func insertBasePublicH(buf *buffer) error {
}); err != nil {
return err
}
buf.writeb('\n')
buf.writes(baseImagePublicH)
buf.writes("\n#endif // WUFFS_INCLUDE_GUARD__BASE_PUBLIC\n\n")
return nil
}
@@ -379,7 +386,7 @@ func (g *gen) genHeader(b *buffer) error {
}
func (g *gen) genImpl(b *buffer) error {
b.writes(basePrivateH)
b.writes(baseBasePrivateH)
b.writeb('\n')
module := "!defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__" +
+52 -48
View File
@@ -16,11 +16,11 @@
package cgen
const basePublicH = "" +
"#ifndef WUFFS_INCLUDE_GUARD__BASE_PUBLIC\n#define WUFFS_INCLUDE_GUARD__BASE_PUBLIC\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#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// Wuffs assumes that:\n// - converting a uint32_t to a size_t will never overflow.\n// - converting a size_t to a uint64_t will never overflow.\n#ifdef __WORDSIZE\n#if (__WORDSIZE != 32) && (__WORDSIZE != 64)\n#error \"Wuffs requires a word size " +
"of either 32 or 64 bits\"\n#endif\n#endif\n\n// WUFFS_VERSION is the major.minor.patch version, as per https://semver.org/,\n// as a uint64_t. The major number is the high 32 bits. The minor number is the\n// middle 16 bits. The patch number is the low 16 bits. The version extension\n// (such as \"\", \"beta\" or \"rc.1\") is part of the string representation (such as\n// \"1.2.3-beta\") but not the uint64_t representation.\n//\n// All three of major, minor and patch being zero means that this is a\n// work-in-progress version, not a release version, and has no backwards or\n// forwards compatibility guarantees.\n//\n// !! Some code generation programs can override WUFFS_VERSION.\n#define WUFFS_VERSION ((uint64_t)0)\n#define WUFFS_VERSION_MAJOR ((uint64_t)0)\n#define WUFFS_VERSION_MINOR ((uint64_t)0)\n#define WUFFS_VERSION_PATCH ((uint64_t)0)\n#define WUFFS_VERSION_EXTENSION \"\"\n#define WUFFS_VERSION_STRING \"0.0.0\"\n\n// Define WUFFS_CONFIG__STATIC_FUNCTIONS to make all of Wuffs' functions have\n// static storage. The motivation is discusse" +
"d in the \"ALLOW STATIC\n// IMPLEMENTATION\" section of\n// https://raw.githubusercontent.com/nothings/stb/master/docs/stb_howto.txt\n#ifdef WUFFS_CONFIG__STATIC_FUNCTIONS\n#define WUFFS_BASE__MAYBE_STATIC static\n#else\n#define WUFFS_BASE__MAYBE_STATIC\n#endif\n\n// wuffs_base__empty_struct is used when a Wuffs function returns an empty\n// struct. In C, if a function f returns void, you can't say \"x = f()\", but in\n// Wuffs, if a function g returns empty, you can say \"y = g()\".\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly used, except that\n // without it, the sizeof a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility if a Wuffs .c file is processed by one compiler and\n // its .h file with another compiler.\n //\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct " +
"is always 1.\n uint8_t private_impl;\n} wuffs_base__empty_struct;\n\n// wuffs_base__utility is a placeholder receiver type. It enables what Java\n// calls static methods, as opposed to regular methods.\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly used, except that\n // without it, the sizeof a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility if a Wuffs .c file is processed by one compiler and\n // its .h file with another compiler.\n //\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct is always 1.\n uint8_t private_impl;\n} wuffs_base__utility;\n\n" +
const baseBasePublicH = "" +
"// 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#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// Wuffs assumes that:\n// - converting a uint32_t to a size_t will never overflow.\n// - converting a size_t to a uint64_t will never overflow.\n#ifdef __WORDSIZE\n#if (__WORDSIZE != 32) && (__WORDSIZE != 64)\n#error \"Wuffs requires a word size of either 32 or 64 bits\"\n#endif\n#endif\n\n// WUFFS_VERSION is the major.minor.patch v" +
"ersion, as per https://semver.org/,\n// as a uint64_t. The major number is the high 32 bits. The minor number is the\n// middle 16 bits. The patch number is the low 16 bits. The version extension\n// (such as \"\", \"beta\" or \"rc.1\") is part of the string representation (such as\n// \"1.2.3-beta\") but not the uint64_t representation.\n//\n// All three of major, minor and patch being zero means that this is a\n// work-in-progress version, not a release version, and has no backwards or\n// forwards compatibility guarantees.\n//\n// !! Some code generation programs can override WUFFS_VERSION.\n#define WUFFS_VERSION ((uint64_t)0)\n#define WUFFS_VERSION_MAJOR ((uint64_t)0)\n#define WUFFS_VERSION_MINOR ((uint64_t)0)\n#define WUFFS_VERSION_PATCH ((uint64_t)0)\n#define WUFFS_VERSION_EXTENSION \"\"\n#define WUFFS_VERSION_STRING \"0.0.0\"\n\n// Define WUFFS_CONFIG__STATIC_FUNCTIONS to make all of Wuffs' functions have\n// static storage. The motivation is discussed in the \"ALLOW STATIC\n// IMPLEMENTATION\" section of\n// https://raw.githubuserconte" +
"nt.com/nothings/stb/master/docs/stb_howto.txt\n#ifdef WUFFS_CONFIG__STATIC_FUNCTIONS\n#define WUFFS_BASE__MAYBE_STATIC static\n#else\n#define WUFFS_BASE__MAYBE_STATIC\n#endif\n\n// wuffs_base__empty_struct is used when a Wuffs function returns an empty\n// struct. In C, if a function f returns void, you can't say \"x = f()\", but in\n// Wuffs, if a function g returns empty, you can say \"y = g()\".\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly used, except that\n // without it, the sizeof a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility if a Wuffs .c file is processed by one compiler and\n // its .h file with another compiler.\n //\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct is always 1.\n uint8_t private_impl;\n} wuffs_base__empty_struct;\n\n// wuffs_base__ut" +
"ility is a placeholder receiver type. It enables what Java\n// calls static methods, as opposed to regular methods.\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly used, except that\n // without it, the sizeof a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility if a Wuffs .c file is processed by one compiler and\n // its .h file with another compiler.\n //\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct is always 1.\n uint8_t private_impl;\n} wuffs_base__utility;\n\n" +
"" +
"// --------\n\n// A status code is either zero (OK), positive (a recoverable suspension or\n// pause in processing) or negative (a non-recoverable error). Its bits:\n// - bit 31 (the sign bit) indicates unrecoverable-ness: an error.\n// - bits 30 .. 24 are a package-namespaced numeric code\n// - bits 23 .. 21 are reserved.\n// - bits 20 .. 0 are the packageid (a namespace) as a base38 value.\n//\n// Do not manipulate these bits directly; they are private implementation\n// details. Use methods such as wuffs_base__status__is_error instead.\ntypedef int32_t wuffs_base__status;\n\n// !! INSERT wuffs_base__status names.\n\nstatic inline bool //\nwuffs_base__status__is_error(wuffs_base__status s) {\n return s < 0;\n}\n\nstatic inline bool //\nwuffs_base__status__is_ok(wuffs_base__status s) {\n return s == 0;\n}\n\nstatic inline bool //\nwuffs_base__status__is_suspension(wuffs_base__status s) {\n return s > 0;\n}\n\nconst char* //\nwuffs_base__status__string(wuffs_base__status s);\n\n" +
"" +
@@ -67,7 +67,52 @@ const basePublicH = "" +
"" +
"// ---------------- 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* bounds[2];\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* bounds[2];\n } private_impl;\n} wuffs_base__io_writer;\n\n// wuffs_base__io_buffer is a 1-dimensional buffer (a pointer and leng" +
"th), 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 //\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" +
"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 = "" +
"#ifndef WUFFS_INCLUDE_GUARD__BASE_PRIVATE\n#define WUFFS_INCLUDE_GUARD__BASE_PRIVATE\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#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\n// WUFFS_BASE__MAGIC is a magic number to check that initializers are called.\n// It's not foolproof, given C doesn't automatically zero memory before use,\n// but it should catch 99.99% of cases.\n//\n// Its (non-zero) value is arbitrary" +
", based on md5sum(\"wuffs\").\n#define WUFFS_BASE__MAGIC ((uint32_t)0x3CCB6C71)\n\n// Denote intentional fallthroughs for -Wimplicit-fallthrough.\n//\n// The order matters here. Clang also defines \"__GNUC__\".\n#if defined(__clang__) && __cplusplus >= 201103L\n#define WUFFS_BASE__FALLTHROUGH [[clang::fallthrough]]\n#elif !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 7)\n#define WUFFS_BASE__FALLTHROUGH __attribute__((fallthrough))\n#else\n#define WUFFS_BASE__FALLTHROUGH\n#endif\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use trivial macros instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n WUFFS_BASE__FALLTHROUGH; \\\n case n:;\n\n#define WUFFS_BASE__CORO" +
"UTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (status < 0) { \\\n goto exit; \\\n } else if (status == 0) { \\\n goto ok; \\\n } \\\n coro_susp_point = n; \\\n goto suspend; \\\n case n:;\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define WUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n#else\n#define WUFFS_BASE__LIKELY(expr) (expr)\n#define WUFFS_BASE__UNLIKELY(expr) (expr)\n#endif\n\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluate more than once.\n//\n// They are static, so that linking multiple wuffs .o files won't complain " +
"about\n// duplicate function definitions.\n//\n// They are explicitly marked inline, even if modern compilers don't use the\n// inline attribute to guide optimizations such as inlining, to avoid the\n// -Wunused-function warning, and we like to compile with -Wall -Werror.\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__return_empty_struct() {\n return ((wuffs_base__empty_struct){});\n}\n\n" +
"" +
"// ---------------- Numeric Types\n\nstatic inline uint8_t //\nwuffs_base__load_u8be(uint8_t* p) {\n return p[0];\n}\n\nstatic inline uint16_t //\nwuffs_base__load_u16be(uint8_t* p) {\n return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0);\n}\n\nstatic inline uint16_t //\nwuffs_base__load_u16le(uint8_t* p) {\n return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u24be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 16) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 0);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u24le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 16);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u32be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u32le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uin" +
"t32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u40be(uint8_t* p) {\n return ((uint64_t)(p[0]) << 32) | ((uint64_t)(p[1]) << 24) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 8) |\n ((uint64_t)(p[4]) << 0);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u40le(uint8_t* p) {\n return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 32);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u48be(uint8_t* p) {\n return ((uint64_t)(p[0]) << 40) | ((uint64_t)(p[1]) << 32) |\n ((uint64_t)(p[2]) << 24) | ((uint64_t)(p[3]) << 16) |\n ((uint64_t)(p[4]) << 8) | ((uint64_t)(p[5]) << 0);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u48le(uint8_t* p) {\n return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40);\n}\n\nstatic inline u" +
"int64_t //\nwuffs_base__load_u56be(uint8_t* p) {\n return ((uint64_t)(p[0]) << 48) | ((uint64_t)(p[1]) << 40) |\n ((uint64_t)(p[2]) << 32) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 16) | ((uint64_t)(p[5]) << 8) |\n ((uint64_t)(p[6]) << 0);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u56le(uint8_t* p) {\n return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40) |\n ((uint64_t)(p[6]) << 48);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u64be(uint8_t* p) {\n return ((uint64_t)(p[0]) << 56) | ((uint64_t)(p[1]) << 48) |\n ((uint64_t)(p[2]) << 40) | ((uint64_t)(p[3]) << 32) |\n ((uint64_t)(p[4]) << 24) | ((uint64_t)(p[5]) << 16) |\n ((uint64_t)(p[6]) << 8) | ((uint64_t)(p[7]) << 0);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u64le(uint8_t* p) {\n return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |\n ((uint64_t)" +
"(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40) |\n ((uint64_t)(p[6]) << 48) | ((uint64_t)(p[7]) << 56);\n}\n\n" +
"" +
"// --------\n\nstatic inline void //\nwuffs_base__store_u8be(uint8_t* p, uint8_t x) {\n p[0] = x;\n}\n\nstatic inline void //\nwuffs_base__store_u16be(uint8_t* p, uint16_t x) {\n p[0] = x >> 8;\n p[1] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u16le(uint8_t* p, uint16_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n}\n\nstatic inline void //\nwuffs_base__store_u24be(uint8_t* p, uint32_t x) {\n p[0] = x >> 16;\n p[1] = x >> 8;\n p[2] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u24le(uint8_t* p, uint32_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n}\n\nstatic inline void //\nwuffs_base__store_u32be(uint8_t* p, uint32_t x) {\n p[0] = x >> 24;\n p[1] = x >> 16;\n p[2] = x >> 8;\n p[3] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u32le(uint8_t* p, uint32_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n}\n\nstatic inline void //\nwuffs_base__store_u40be(uint8_t* p, uint64_t x) {\n p[0] = x >> 32;\n p[1] = x >> 24;\n p[2] = x >> 16;\n p[3] = x >> 8;\n p[4] = x >>" +
" 0;\n}\n\nstatic inline void //\nwuffs_base__store_u40le(uint8_t* p, uint64_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n p[4] = x >> 32;\n}\n\nstatic inline void //\nwuffs_base__store_u48be(uint8_t* p, uint64_t x) {\n p[0] = x >> 40;\n p[1] = x >> 32;\n p[2] = x >> 24;\n p[3] = x >> 16;\n p[4] = x >> 8;\n p[5] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u48le(uint8_t* p, uint64_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n p[4] = x >> 32;\n p[5] = x >> 40;\n}\n\nstatic inline void //\nwuffs_base__store_u56be(uint8_t* p, uint64_t x) {\n p[0] = x >> 48;\n p[1] = x >> 40;\n p[2] = x >> 32;\n p[3] = x >> 24;\n p[4] = x >> 16;\n p[5] = x >> 8;\n p[6] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u56le(uint8_t* p, uint64_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n p[4] = x >> 32;\n p[5] = x >> 40;\n p[6] = x >> 48;\n}\n\nstatic inline void //\nwuffs_base__store_u64be(uint8_t* p, uint64_t x) {\n p[0] = x >> 56" +
";\n p[1] = x >> 48;\n p[2] = x >> 40;\n p[3] = x >> 32;\n p[4] = x >> 24;\n p[5] = x >> 16;\n p[6] = x >> 8;\n p[7] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u64le(uint8_t* p, uint64_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n p[4] = x >> 32;\n p[5] = x >> 40;\n p[6] = x >> 48;\n p[7] = x >> 56;\n}\n\n" +
"" +
"// --------\n\nstatic inline void //\nwuffs_base__u8__sat_add_indirect(uint8_t* x, uint8_t y) {\n *x = wuffs_base__u8__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u8__sat_sub_indirect(uint8_t* x, uint8_t y) {\n *x = wuffs_base__u8__sat_sub(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u16__sat_add_indirect(uint16_t* x, uint16_t y) {\n *x = wuffs_base__u16__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u16__sat_sub_indirect(uint16_t* x, uint16_t y) {\n *x = wuffs_base__u16__sat_sub(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u32__sat_add_indirect(uint32_t* x, uint32_t y) {\n *x = wuffs_base__u32__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u32__sat_sub_indirect(uint32_t* x, uint32_t y) {\n *x = wuffs_base__u32__sat_sub(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u64__sat_add_indirect(uint64_t* x, uint64_t y) {\n *x = wuffs_base__u64__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u64__sat_sub_indirect(uint64_t* x, uint64_t y) {\n *x = wuffs_base__u64__sat_sub(*x, y);\n}\n\n" +
"" +
"// ---------------- Slices and Tables\n\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__subslice_i(wuffs_base__slice_u8 s, uint64_t i) {\n if ((i <= SIZE_MAX) && (i <= s.len)) {\n return ((wuffs_base__slice_u8){\n .ptr = s.ptr + i,\n .len = s.len - i,\n });\n }\n return ((wuffs_base__slice_u8){});\n}\n\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__subslice_j(wuffs_base__slice_u8 s, uint64_t j) {\n if ((j <= SIZE_MAX) && (j <= s.len)) {\n return ((wuffs_base__slice_u8){.ptr = s.ptr, .len = j});\n }\n return ((wuffs_base__slice_u8){});\n}\n\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__subslice_ij(wuffs_base__slice_u8 s,\n uint64_t i,\n uint64_t j) {\n if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {\n return ((wuffs_base__slice_u8){\n .ptr = s.ptr + i,\n .len = j - i,\n });\n }\n return ((wuffs_base__slice_u8){});\n}\n\n// wuffs_base__slice_u8__prefix returns up to the first up" +
"_to bytes of s.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__prefix(wuffs_base__slice_u8 s, uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.len = up_to;\n }\n return s;\n}\n\n// wuffs_base__slice_u8__suffix returns up to the last up_to bytes of s.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__suffix(wuffs_base__slice_u8 s, uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.ptr += (uint64_t)(s.len) - up_to;\n s.len = up_to;\n }\n return s;\n}\n\n// wuffs_base__slice_u8__copy_from_slice calls memmove(dst.ptr, src.ptr,\n// length) where length is the minimum of dst.len and src.len.\n//\n// Passing a wuffs_base__slice_u8 with all fields NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\nstatic inline uint64_t //\nwuffs_base__slice_u8__copy_from_slice(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t length = dst.len < src.len ? dst.len : src.len;\n if (length > 0) {\n memmove(dst.ptr, src.ptr, leng" +
"th);\n }\n return length;\n}\n\n" +
"" +
"// --------\n\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__table_u8__row(wuffs_base__table_u8 t, uint32_t y) {\n if (y < t.height) {\n return ((wuffs_base__slice_u8){\n .ptr = t.ptr + (t.stride * y),\n .len = t.width,\n });\n }\n return ((wuffs_base__slice_u8){});\n}\n\n" +
"" +
"// ---------------- Utility\n\nstatic inline wuffs_base__range_ii_u32 //\nwuffs_base__utility__make_range_ii_u32(wuffs_base__utility* ignored,\n uint32_t min_incl,\n uint32_t max_incl) {\n return ((wuffs_base__range_ii_u32){\n .min_incl = min_incl,\n .max_incl = max_incl,\n });\n}\n\nstatic inline wuffs_base__range_ie_u32 //\nwuffs_base__utility__make_range_ie_u32(wuffs_base__utility* ignored,\n uint32_t min_incl,\n uint32_t max_excl) {\n return ((wuffs_base__range_ie_u32){\n .min_incl = min_incl,\n .max_excl = max_excl,\n });\n}\n\nstatic inline wuffs_base__range_ii_u64 //\nwuffs_base__utility__make_range_ii_u64(wuffs_base__utility* ignored,\n uint64_t min_incl,\n uint64_t max_incl) {\n return ((wuffs_base__range_ii_u64){\n .min_incl = min_incl,\n .max_incl = max_incl,\n });" +
"\n}\n\nstatic inline wuffs_base__range_ie_u64 //\nwuffs_base__utility__make_range_ie_u64(wuffs_base__utility* ignored,\n uint64_t min_incl,\n uint64_t max_excl) {\n return ((wuffs_base__range_ie_u64){\n .min_incl = min_incl,\n .max_excl = max_excl,\n });\n}\n\nstatic inline wuffs_base__rect_ii_u32 //\nwuffs_base__utility__make_rect_ii_u32(wuffs_base__utility* ignored,\n uint32_t min_incl_x,\n uint32_t min_incl_y,\n uint32_t max_incl_x,\n uint32_t max_incl_y) {\n return ((wuffs_base__rect_ii_u32){\n .min_incl_x = min_incl_x,\n .min_incl_y = min_incl_y,\n .max_incl_x = max_incl_x,\n .max_incl_y = max_incl_y,\n });\n}\n\nstatic inline wuffs_base__rect_ie_u32 //\nwuffs_base__utility__make_rect_ie_u32(wuffs_base__utility* ignored,\n uint32_t min_incl" +
"_x,\n uint32_t min_incl_y,\n uint32_t max_excl_x,\n uint32_t max_excl_y) {\n return ((wuffs_base__rect_ie_u32){\n .min_incl_x = min_incl_x,\n .min_incl_y = min_incl_y,\n .max_excl_x = max_excl_x,\n .max_excl_y = max_excl_y,\n });\n}\n\n" +
"" +
"// ---------------- I/O\n\nstatic inline bool //\nwuffs_base__io_buffer__is_valid(wuffs_base__io_buffer buf) {\n return (buf.ptr || (buf.len == 0)) && (buf.len >= buf.wi) &&\n (buf.wi >= buf.ri);\n}\n\nstatic inline bool //\nwuffs_base__io_reader__is_eof(wuffs_base__io_reader o) {\n wuffs_base__io_buffer* buf = o.private_impl.buf;\n return buf && buf->closed && (buf->ptr + buf->wi == o.private_impl.bounds[1]);\n}\n\nstatic inline bool //\nwuffs_base__io_reader__is_valid(wuffs_base__io_reader o) {\n wuffs_base__io_buffer* buf = o.private_impl.buf;\n // Note: if making this function public (i.e. moving it to base-header.h), it\n // also needs to allow NULL (i.e. implicit, callee-calculated) bounds.\n return buf ? ((buf->ptr <= o.private_impl.bounds[0]) &&\n (o.private_impl.bounds[0] <= o.private_impl.bounds[1]) &&\n (o.private_impl.bounds[1] <= buf->ptr + buf->len))\n : ((o.private_impl.bounds[0] == NULL) &&\n (o.private_impl.bounds[1] == NULL));\n}\n\nstatic inl" +
"ine bool //\nwuffs_base__io_writer__is_valid(wuffs_base__io_writer o) {\n wuffs_base__io_buffer* buf = o.private_impl.buf;\n // Note: if making this function public (i.e. moving it to base-header.h), it\n // also needs to allow NULL (i.e. implicit, callee-calculated) bounds.\n return buf ? ((buf->ptr <= o.private_impl.bounds[0]) &&\n (o.private_impl.bounds[0] <= o.private_impl.bounds[1]) &&\n (o.private_impl.bounds[1] <= buf->ptr + buf->len))\n : ((o.private_impl.bounds[0] == NULL) &&\n (o.private_impl.bounds[1] == NULL));\n}\n\nstatic inline uint32_t //\nwuffs_base__io_writer__copy_n_from_history(uint8_t** ptr_ptr,\n uint8_t* start,\n uint8_t* end,\n uint32_t length,\n uint32_t distance) {\n if (!distance) {\n return 0;\n }\n uint8_t* ptr = *ptr_ptr;\n if ((size_t)(ptr - start) < (size_t)(distanc" +
"e)) {\n return 0;\n }\n start = ptr - distance;\n size_t n = end - ptr;\n if ((size_t)(length) > n) {\n length = n;\n } else {\n n = length;\n }\n // TODO: unrolling by 3 seems best for the std/deflate benchmarks, but that\n // is mostly because 3 is the minimum length for the deflate format. This\n // function implementation shouldn't overfit to that one format. Perhaps the\n // copy_n_from_history Wuffs method should also take an unroll hint argument,\n // and the cgen can look if that argument is the constant expression '3'.\n //\n // See also wuffs_base__io_writer__copy_n_from_history_fast below.\n //\n // Alternatively, or additionally, have a sloppy_copy_n_from_history method\n // that copies 8 bytes at a time, possibly writing more than length bytes?\n for (; n >= 3; n -= 3) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; n; n--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\n// wuffs_base__io_writer__copy_n_from_history_fast is like the\n// " +
"wuffs_base__io_writer__copy_n_from_history function above, but has stronger\n// pre-conditions. The caller needs to prove that:\n// - distance > 0\n// - distance <= (*ptr_ptr - start)\n// - length <= (end - *ptr_ptr)\nstatic inline uint32_t //\nwuffs_base__io_writer__copy_n_from_history_fast(uint8_t** ptr_ptr,\n uint8_t* start,\n uint8_t* end,\n uint32_t length,\n uint32_t distance) {\n uint8_t* ptr = *ptr_ptr;\n start = ptr - distance;\n uint32_t n = length;\n for (; n >= 3; n -= 3) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; n; n--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\nstatic inline uint32_t //\nwuffs_base__io_writer__copy_n_from_reader(uint8_t** ptr_ioptr_w,\n uint8_t* iobounds1_w,\n " +
" uint32_t length,\n uint8_t** ptr_ioptr_r,\n uint8_t* iobounds1_r) {\n uint8_t* ioptr_w = *ptr_ioptr_w;\n size_t n = length;\n if (n > ((size_t)(iobounds1_w - ioptr_w))) {\n n = iobounds1_w - ioptr_w;\n }\n uint8_t* ioptr_r = *ptr_ioptr_r;\n if (n > ((size_t)(iobounds1_r - ioptr_r))) {\n n = iobounds1_r - ioptr_r;\n }\n if (n > 0) {\n memmove(ioptr_w, ioptr_r, n);\n *ptr_ioptr_w += n;\n *ptr_ioptr_r += n;\n }\n return n;\n}\n\nstatic inline uint64_t //\nwuffs_base__io_writer__copy_from_slice(uint8_t** ptr_ioptr_w,\n uint8_t* iobounds1_w,\n wuffs_base__slice_u8 src) {\n uint8_t* ioptr_w = *ptr_ioptr_w;\n size_t n = src.len;\n if (n > ((size_t)(iobounds1_w - ioptr_w))) {\n n = iobounds1_w - ioptr_w;\n }\n if (n > 0) {\n memmove(ioptr_w, src.ptr, n);\n *ptr_ioptr_w += n;\n }\n return n;\n}\n\nstatic inline uint32_t //\nwuffs_base__io_writer" +
"__copy_n_from_slice(uint8_t** ptr_ioptr_w,\n uint8_t* iobounds1_w,\n uint32_t length,\n wuffs_base__slice_u8 src) {\n uint8_t* ioptr_w = *ptr_ioptr_w;\n size_t n = src.len;\n if (n > length) {\n n = length;\n }\n if (n > ((size_t)(iobounds1_w - ioptr_w))) {\n n = iobounds1_w - ioptr_w;\n }\n if (n > 0) {\n memmove(ioptr_w, src.ptr, n);\n *ptr_ioptr_w += n;\n }\n return n;\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_reader__set_limit(wuffs_base__io_reader* o,\n uint8_t* ioptr_r,\n uint64_t limit) {\n if (o && (((size_t)(o->private_impl.bounds[1] - ioptr_r)) > limit)) {\n o->private_impl.bounds[1] = ioptr_r + limit;\n }\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_reader__set_mark(wuffs_base__io_reader* o, uint8_t* mark) {\n o->private_impl.bounds[0] = " +
"mark;\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_writer__set(wuffs_base__io_writer* o,\n wuffs_base__io_buffer* b,\n uint8_t** ioptr1_ptr,\n uint8_t** ioptr2_ptr,\n wuffs_base__slice_u8 s) {\n b->ptr = s.ptr;\n b->len = s.len;\n b->wi = 0;\n b->ri = 0;\n b->closed = false;\n o->private_impl.buf = b;\n o->private_impl.bounds[0] = s.ptr;\n o->private_impl.bounds[1] = s.ptr + s.len;\n *ioptr1_ptr = s.ptr;\n *ioptr2_ptr = s.ptr + s.len;\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_writer__set_mark(wuffs_base__io_writer* o, uint8_t* mark) {\n o->private_impl.bounds[0] = mark;\n return ((wuffs_base__empty_struct){});\n}\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n#endif // WUFFS_INCLUDE_GUARD__BASE_PRIVATE\n" +
""
const baseBaseImplC = "" +
"// Copyright 2018 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// !! INSERT base-public.h.\n\n#ifdef WUFFS_IMPLEMENTATION\n\n// !! INSERT base-private.h.\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE)\n\n// !! INSERT wuffs_base__status__string data.\n\nconst char* //\nwuffs_base__status__string(wuffs_base__status s) {\n uint16_t o = wuffs_base__status__string_offsets[(uint8_t)(s >> 24)];\n return o ? wuffs_base__status__string_data + o : \"unknown status\";\n}\n\n#endif " +
" // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE)\n\n#endif // WUFFS_IMPLEMENTATION\n" +
""
const baseImagePublicH = "" +
"// 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#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n" +
"" +
"// ---------------- Images\n\n// wuffs_base__color_u32argb is an 8 bit per channel Alpha, Red, Green, Blue\n// color, as a uint32_t value. It is in word order, not byte order: its value\n// is always 0xAARRGGBB, regardless of endianness. It uses premultiplied alpha.\ntypedef uint32_t wuffs_base__color_u32argb;\n\n" +
"" +
@@ -104,48 +149,7 @@ const basePublicH = "" +
"ge_buffer__palette_changed returns whether 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__image_buffer__palette_changed(wuffs_base__image_buffer* b) {\n return b && b->private_impl.palette_changed;\n}\n\n// wuffs_base__image_buffer__palette returns the palette that the pixel data\n// can index. The backing array is inside b and has length 1024.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__image_buffer__palette(wuffs_base__image_buffer* b) {\n return b ? ((wuffs_base__slice_u8){.ptr = b->private_impl.palette,\n .len = 1024})\n : ((wuffs_base__slice_u8){});\n}\n\nstatic inline wuffs_base__table_u8 //\nwuffs_base__image_buffer__plane(wuffs_base__image_buffer* b, uint32_t p) {\n return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))\n ? b->private_impl.pixbuf.planes[p]\n : ((wuffs_base__table_u8){});\n}\n\n#ifdef __cplusp" +
"lus\n\ninline wuffs_base__status //\nwuffs_base__image_buffer::set_from_pixbuf(wuffs_base__image_config config,\n wuffs_base__pixel_buffer pixbuf) {\n return wuffs_base__image_buffer__set_from_pixbuf(this, config, pixbuf);\n}\n\ninline wuffs_base__status //\nwuffs_base__image_buffer::set_from_slice(wuffs_base__image_config config,\n wuffs_base__slice_u8 pixbuf_memory) {\n return wuffs_base__image_buffer__set_from_slice(this, config, pixbuf_memory);\n}\n\ninline void //\nwuffs_base__image_buffer::update(wuffs_base__rect_ie_u32 dirty_rect,\n wuffs_base__flicks duration,\n bool blend,\n wuffs_base__animation_disposal disposal,\n wuffs_base__slice_u8 palette) {\n wuffs_base__image_buffer__update(this, dirty_rect, duration, blend, disposal,\n palette);\n}\n\ninline wuffs_base__image_config* /" +
"/\nwuffs_base__image_buffer::image_config() {\n return wuffs_base__image_buffer__image_config(this);\n}\n\ninline wuffs_base__rect_ie_u32 //\nwuffs_base__image_buffer::bounds() {\n return wuffs_base__image_buffer__bounds(this);\n}\n\ninline uint32_t //\nwuffs_base__image_buffer::width() {\n return wuffs_base__image_buffer__width(this);\n}\n\ninline uint32_t //\nwuffs_base__image_buffer::height() {\n return wuffs_base__image_buffer__height(this);\n}\n\ninline wuffs_base__rect_ie_u32 //\nwuffs_base__image_buffer::dirty_rect() {\n return wuffs_base__image_buffer__dirty_rect(this);\n}\n\ninline wuffs_base__flicks //\nwuffs_base__image_buffer::duration() {\n return wuffs_base__image_buffer__duration(this);\n}\n\ninline bool //\nwuffs_base__image_buffer::blend() {\n return wuffs_base__image_buffer__blend(this);\n}\n\ninline wuffs_base__animation_disposal //\nwuffs_base__image_buffer::disposal() {\n return wuffs_base__image_buffer__disposal(this);\n}\n\ninline bool //\nwuffs_base__image_buffer::palette_changed() {\n return wuffs_base__image" +
"_buffer__palette_changed(this);\n}\n\ninline wuffs_base__slice_u8 //\nwuffs_base__image_buffer::palette() {\n return wuffs_base__image_buffer__palette(this);\n}\n\ninline wuffs_base__table_u8 //\nwuffs_base__image_buffer::plane(uint32_t p) {\n return wuffs_base__image_buffer__plane(this, p);\n}\n\n#endif // __cplusplus\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n#endif // WUFFS_INCLUDE_GUARD__BASE_PUBLIC\n" +
""
const basePrivateH = "" +
"#ifndef WUFFS_INCLUDE_GUARD__BASE_PRIVATE\n#define WUFFS_INCLUDE_GUARD__BASE_PRIVATE\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#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\n// WUFFS_BASE__MAGIC is a magic number to check that initializers are called.\n// It's not foolproof, given C doesn't automatically zero memory before use,\n// but it should catch 99.99% of cases.\n//\n// Its (non-zero) value is arbitrary" +
", based on md5sum(\"wuffs\").\n#define WUFFS_BASE__MAGIC ((uint32_t)0x3CCB6C71)\n\n// Denote intentional fallthroughs for -Wimplicit-fallthrough.\n//\n// The order matters here. Clang also defines \"__GNUC__\".\n#if defined(__clang__) && __cplusplus >= 201103L\n#define WUFFS_BASE__FALLTHROUGH [[clang::fallthrough]]\n#elif !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 7)\n#define WUFFS_BASE__FALLTHROUGH __attribute__((fallthrough))\n#else\n#define WUFFS_BASE__FALLTHROUGH\n#endif\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use trivial macros instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n WUFFS_BASE__FALLTHROUGH; \\\n case n:;\n\n#define WUFFS_BASE__CORO" +
"UTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (status < 0) { \\\n goto exit; \\\n } else if (status == 0) { \\\n goto ok; \\\n } \\\n coro_susp_point = n; \\\n goto suspend; \\\n case n:;\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define WUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n#else\n#define WUFFS_BASE__LIKELY(expr) (expr)\n#define WUFFS_BASE__UNLIKELY(expr) (expr)\n#endif\n\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluate more than once.\n//\n// They are static, so that linking multiple wuffs .o files won't complain " +
"about\n// duplicate function definitions.\n//\n// They are explicitly marked inline, even if modern compilers don't use the\n// inline attribute to guide optimizations such as inlining, to avoid the\n// -Wunused-function warning, and we like to compile with -Wall -Werror.\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__return_empty_struct() {\n return ((wuffs_base__empty_struct){});\n}\n\n" +
"" +
"// ---------------- Numeric Types\n\nstatic inline uint8_t //\nwuffs_base__load_u8be(uint8_t* p) {\n return p[0];\n}\n\nstatic inline uint16_t //\nwuffs_base__load_u16be(uint8_t* p) {\n return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0);\n}\n\nstatic inline uint16_t //\nwuffs_base__load_u16le(uint8_t* p) {\n return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u24be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 16) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 0);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u24le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 16);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u32be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u32le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uin" +
"t32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u40be(uint8_t* p) {\n return ((uint64_t)(p[0]) << 32) | ((uint64_t)(p[1]) << 24) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 8) |\n ((uint64_t)(p[4]) << 0);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u40le(uint8_t* p) {\n return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 32);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u48be(uint8_t* p) {\n return ((uint64_t)(p[0]) << 40) | ((uint64_t)(p[1]) << 32) |\n ((uint64_t)(p[2]) << 24) | ((uint64_t)(p[3]) << 16) |\n ((uint64_t)(p[4]) << 8) | ((uint64_t)(p[5]) << 0);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u48le(uint8_t* p) {\n return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40);\n}\n\nstatic inline u" +
"int64_t //\nwuffs_base__load_u56be(uint8_t* p) {\n return ((uint64_t)(p[0]) << 48) | ((uint64_t)(p[1]) << 40) |\n ((uint64_t)(p[2]) << 32) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 16) | ((uint64_t)(p[5]) << 8) |\n ((uint64_t)(p[6]) << 0);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u56le(uint8_t* p) {\n return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40) |\n ((uint64_t)(p[6]) << 48);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u64be(uint8_t* p) {\n return ((uint64_t)(p[0]) << 56) | ((uint64_t)(p[1]) << 48) |\n ((uint64_t)(p[2]) << 40) | ((uint64_t)(p[3]) << 32) |\n ((uint64_t)(p[4]) << 24) | ((uint64_t)(p[5]) << 16) |\n ((uint64_t)(p[6]) << 8) | ((uint64_t)(p[7]) << 0);\n}\n\nstatic inline uint64_t //\nwuffs_base__load_u64le(uint8_t* p) {\n return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |\n ((uint64_t)" +
"(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40) |\n ((uint64_t)(p[6]) << 48) | ((uint64_t)(p[7]) << 56);\n}\n\n" +
"" +
"// --------\n\nstatic inline void //\nwuffs_base__store_u8be(uint8_t* p, uint8_t x) {\n p[0] = x;\n}\n\nstatic inline void //\nwuffs_base__store_u16be(uint8_t* p, uint16_t x) {\n p[0] = x >> 8;\n p[1] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u16le(uint8_t* p, uint16_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n}\n\nstatic inline void //\nwuffs_base__store_u24be(uint8_t* p, uint32_t x) {\n p[0] = x >> 16;\n p[1] = x >> 8;\n p[2] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u24le(uint8_t* p, uint32_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n}\n\nstatic inline void //\nwuffs_base__store_u32be(uint8_t* p, uint32_t x) {\n p[0] = x >> 24;\n p[1] = x >> 16;\n p[2] = x >> 8;\n p[3] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u32le(uint8_t* p, uint32_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n}\n\nstatic inline void //\nwuffs_base__store_u40be(uint8_t* p, uint64_t x) {\n p[0] = x >> 32;\n p[1] = x >> 24;\n p[2] = x >> 16;\n p[3] = x >> 8;\n p[4] = x >>" +
" 0;\n}\n\nstatic inline void //\nwuffs_base__store_u40le(uint8_t* p, uint64_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n p[4] = x >> 32;\n}\n\nstatic inline void //\nwuffs_base__store_u48be(uint8_t* p, uint64_t x) {\n p[0] = x >> 40;\n p[1] = x >> 32;\n p[2] = x >> 24;\n p[3] = x >> 16;\n p[4] = x >> 8;\n p[5] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u48le(uint8_t* p, uint64_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n p[4] = x >> 32;\n p[5] = x >> 40;\n}\n\nstatic inline void //\nwuffs_base__store_u56be(uint8_t* p, uint64_t x) {\n p[0] = x >> 48;\n p[1] = x >> 40;\n p[2] = x >> 32;\n p[3] = x >> 24;\n p[4] = x >> 16;\n p[5] = x >> 8;\n p[6] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u56le(uint8_t* p, uint64_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n p[4] = x >> 32;\n p[5] = x >> 40;\n p[6] = x >> 48;\n}\n\nstatic inline void //\nwuffs_base__store_u64be(uint8_t* p, uint64_t x) {\n p[0] = x >> 56" +
";\n p[1] = x >> 48;\n p[2] = x >> 40;\n p[3] = x >> 32;\n p[4] = x >> 24;\n p[5] = x >> 16;\n p[6] = x >> 8;\n p[7] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u64le(uint8_t* p, uint64_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n p[4] = x >> 32;\n p[5] = x >> 40;\n p[6] = x >> 48;\n p[7] = x >> 56;\n}\n\n" +
"" +
"// --------\n\nstatic inline void //\nwuffs_base__u8__sat_add_indirect(uint8_t* x, uint8_t y) {\n *x = wuffs_base__u8__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u8__sat_sub_indirect(uint8_t* x, uint8_t y) {\n *x = wuffs_base__u8__sat_sub(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u16__sat_add_indirect(uint16_t* x, uint16_t y) {\n *x = wuffs_base__u16__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u16__sat_sub_indirect(uint16_t* x, uint16_t y) {\n *x = wuffs_base__u16__sat_sub(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u32__sat_add_indirect(uint32_t* x, uint32_t y) {\n *x = wuffs_base__u32__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u32__sat_sub_indirect(uint32_t* x, uint32_t y) {\n *x = wuffs_base__u32__sat_sub(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u64__sat_add_indirect(uint64_t* x, uint64_t y) {\n *x = wuffs_base__u64__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u64__sat_sub_indirect(uint64_t* x, uint64_t y) {\n *x = wuffs_base__u64__sat_sub(*x, y);\n}\n\n" +
"" +
"// ---------------- Slices and Tables\n\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__subslice_i(wuffs_base__slice_u8 s, uint64_t i) {\n if ((i <= SIZE_MAX) && (i <= s.len)) {\n return ((wuffs_base__slice_u8){\n .ptr = s.ptr + i,\n .len = s.len - i,\n });\n }\n return ((wuffs_base__slice_u8){});\n}\n\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__subslice_j(wuffs_base__slice_u8 s, uint64_t j) {\n if ((j <= SIZE_MAX) && (j <= s.len)) {\n return ((wuffs_base__slice_u8){.ptr = s.ptr, .len = j});\n }\n return ((wuffs_base__slice_u8){});\n}\n\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__subslice_ij(wuffs_base__slice_u8 s,\n uint64_t i,\n uint64_t j) {\n if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {\n return ((wuffs_base__slice_u8){\n .ptr = s.ptr + i,\n .len = j - i,\n });\n }\n return ((wuffs_base__slice_u8){});\n}\n\n// wuffs_base__slice_u8__prefix returns up to the first up" +
"_to bytes of s.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__prefix(wuffs_base__slice_u8 s, uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.len = up_to;\n }\n return s;\n}\n\n// wuffs_base__slice_u8__suffix returns up to the last up_to bytes of s.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__suffix(wuffs_base__slice_u8 s, uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.ptr += (uint64_t)(s.len) - up_to;\n s.len = up_to;\n }\n return s;\n}\n\n// wuffs_base__slice_u8__copy_from_slice calls memmove(dst.ptr, src.ptr,\n// length) where length is the minimum of dst.len and src.len.\n//\n// Passing a wuffs_base__slice_u8 with all fields NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\nstatic inline uint64_t //\nwuffs_base__slice_u8__copy_from_slice(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t length = dst.len < src.len ? dst.len : src.len;\n if (length > 0) {\n memmove(dst.ptr, src.ptr, leng" +
"th);\n }\n return length;\n}\n\n" +
"" +
"// --------\n\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__table_u8__row(wuffs_base__table_u8 t, uint32_t y) {\n if (y < t.height) {\n return ((wuffs_base__slice_u8){\n .ptr = t.ptr + (t.stride * y),\n .len = t.width,\n });\n }\n return ((wuffs_base__slice_u8){});\n}\n\n" +
"" +
"// ---------------- Utility\n\nstatic inline wuffs_base__range_ii_u32 //\nwuffs_base__utility__make_range_ii_u32(wuffs_base__utility* ignored,\n uint32_t min_incl,\n uint32_t max_incl) {\n return ((wuffs_base__range_ii_u32){\n .min_incl = min_incl,\n .max_incl = max_incl,\n });\n}\n\nstatic inline wuffs_base__range_ie_u32 //\nwuffs_base__utility__make_range_ie_u32(wuffs_base__utility* ignored,\n uint32_t min_incl,\n uint32_t max_excl) {\n return ((wuffs_base__range_ie_u32){\n .min_incl = min_incl,\n .max_excl = max_excl,\n });\n}\n\nstatic inline wuffs_base__range_ii_u64 //\nwuffs_base__utility__make_range_ii_u64(wuffs_base__utility* ignored,\n uint64_t min_incl,\n uint64_t max_incl) {\n return ((wuffs_base__range_ii_u64){\n .min_incl = min_incl,\n .max_incl = max_incl,\n });" +
"\n}\n\nstatic inline wuffs_base__range_ie_u64 //\nwuffs_base__utility__make_range_ie_u64(wuffs_base__utility* ignored,\n uint64_t min_incl,\n uint64_t max_excl) {\n return ((wuffs_base__range_ie_u64){\n .min_incl = min_incl,\n .max_excl = max_excl,\n });\n}\n\nstatic inline wuffs_base__rect_ii_u32 //\nwuffs_base__utility__make_rect_ii_u32(wuffs_base__utility* ignored,\n uint32_t min_incl_x,\n uint32_t min_incl_y,\n uint32_t max_incl_x,\n uint32_t max_incl_y) {\n return ((wuffs_base__rect_ii_u32){\n .min_incl_x = min_incl_x,\n .min_incl_y = min_incl_y,\n .max_incl_x = max_incl_x,\n .max_incl_y = max_incl_y,\n });\n}\n\nstatic inline wuffs_base__rect_ie_u32 //\nwuffs_base__utility__make_rect_ie_u32(wuffs_base__utility* ignored,\n uint32_t min_incl" +
"_x,\n uint32_t min_incl_y,\n uint32_t max_excl_x,\n uint32_t max_excl_y) {\n return ((wuffs_base__rect_ie_u32){\n .min_incl_x = min_incl_x,\n .min_incl_y = min_incl_y,\n .max_excl_x = max_excl_x,\n .max_excl_y = max_excl_y,\n });\n}\n\n" +
"" +
"// ---------------- I/O\n\nstatic inline bool //\nwuffs_base__io_buffer__is_valid(wuffs_base__io_buffer buf) {\n return (buf.ptr || (buf.len == 0)) && (buf.len >= buf.wi) &&\n (buf.wi >= buf.ri);\n}\n\nstatic inline bool //\nwuffs_base__io_reader__is_eof(wuffs_base__io_reader o) {\n wuffs_base__io_buffer* buf = o.private_impl.buf;\n return buf && buf->closed && (buf->ptr + buf->wi == o.private_impl.bounds[1]);\n}\n\nstatic inline bool //\nwuffs_base__io_reader__is_valid(wuffs_base__io_reader o) {\n wuffs_base__io_buffer* buf = o.private_impl.buf;\n // Note: if making this function public (i.e. moving it to base-header.h), it\n // also needs to allow NULL (i.e. implicit, callee-calculated) bounds.\n return buf ? ((buf->ptr <= o.private_impl.bounds[0]) &&\n (o.private_impl.bounds[0] <= o.private_impl.bounds[1]) &&\n (o.private_impl.bounds[1] <= buf->ptr + buf->len))\n : ((o.private_impl.bounds[0] == NULL) &&\n (o.private_impl.bounds[1] == NULL));\n}\n\nstatic inl" +
"ine bool //\nwuffs_base__io_writer__is_valid(wuffs_base__io_writer o) {\n wuffs_base__io_buffer* buf = o.private_impl.buf;\n // Note: if making this function public (i.e. moving it to base-header.h), it\n // also needs to allow NULL (i.e. implicit, callee-calculated) bounds.\n return buf ? ((buf->ptr <= o.private_impl.bounds[0]) &&\n (o.private_impl.bounds[0] <= o.private_impl.bounds[1]) &&\n (o.private_impl.bounds[1] <= buf->ptr + buf->len))\n : ((o.private_impl.bounds[0] == NULL) &&\n (o.private_impl.bounds[1] == NULL));\n}\n\nstatic inline uint32_t //\nwuffs_base__io_writer__copy_n_from_history(uint8_t** ptr_ptr,\n uint8_t* start,\n uint8_t* end,\n uint32_t length,\n uint32_t distance) {\n if (!distance) {\n return 0;\n }\n uint8_t* ptr = *ptr_ptr;\n if ((size_t)(ptr - start) < (size_t)(distanc" +
"e)) {\n return 0;\n }\n start = ptr - distance;\n size_t n = end - ptr;\n if ((size_t)(length) > n) {\n length = n;\n } else {\n n = length;\n }\n // TODO: unrolling by 3 seems best for the std/deflate benchmarks, but that\n // is mostly because 3 is the minimum length for the deflate format. This\n // function implementation shouldn't overfit to that one format. Perhaps the\n // copy_n_from_history Wuffs method should also take an unroll hint argument,\n // and the cgen can look if that argument is the constant expression '3'.\n //\n // See also wuffs_base__io_writer__copy_n_from_history_fast below.\n //\n // Alternatively, or additionally, have a sloppy_copy_n_from_history method\n // that copies 8 bytes at a time, possibly writing more than length bytes?\n for (; n >= 3; n -= 3) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; n; n--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\n// wuffs_base__io_writer__copy_n_from_history_fast is like the\n// " +
"wuffs_base__io_writer__copy_n_from_history function above, but has stronger\n// pre-conditions. The caller needs to prove that:\n// - distance > 0\n// - distance <= (*ptr_ptr - start)\n// - length <= (end - *ptr_ptr)\nstatic inline uint32_t //\nwuffs_base__io_writer__copy_n_from_history_fast(uint8_t** ptr_ptr,\n uint8_t* start,\n uint8_t* end,\n uint32_t length,\n uint32_t distance) {\n uint8_t* ptr = *ptr_ptr;\n start = ptr - distance;\n uint32_t n = length;\n for (; n >= 3; n -= 3) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; n; n--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\nstatic inline uint32_t //\nwuffs_base__io_writer__copy_n_from_reader(uint8_t** ptr_ioptr_w,\n uint8_t* iobounds1_w,\n " +
" uint32_t length,\n uint8_t** ptr_ioptr_r,\n uint8_t* iobounds1_r) {\n uint8_t* ioptr_w = *ptr_ioptr_w;\n size_t n = length;\n if (n > ((size_t)(iobounds1_w - ioptr_w))) {\n n = iobounds1_w - ioptr_w;\n }\n uint8_t* ioptr_r = *ptr_ioptr_r;\n if (n > ((size_t)(iobounds1_r - ioptr_r))) {\n n = iobounds1_r - ioptr_r;\n }\n if (n > 0) {\n memmove(ioptr_w, ioptr_r, n);\n *ptr_ioptr_w += n;\n *ptr_ioptr_r += n;\n }\n return n;\n}\n\nstatic inline uint64_t //\nwuffs_base__io_writer__copy_from_slice(uint8_t** ptr_ioptr_w,\n uint8_t* iobounds1_w,\n wuffs_base__slice_u8 src) {\n uint8_t* ioptr_w = *ptr_ioptr_w;\n size_t n = src.len;\n if (n > ((size_t)(iobounds1_w - ioptr_w))) {\n n = iobounds1_w - ioptr_w;\n }\n if (n > 0) {\n memmove(ioptr_w, src.ptr, n);\n *ptr_ioptr_w += n;\n }\n return n;\n}\n\nstatic inline uint32_t //\nwuffs_base__io_writer" +
"__copy_n_from_slice(uint8_t** ptr_ioptr_w,\n uint8_t* iobounds1_w,\n uint32_t length,\n wuffs_base__slice_u8 src) {\n uint8_t* ioptr_w = *ptr_ioptr_w;\n size_t n = src.len;\n if (n > length) {\n n = length;\n }\n if (n > ((size_t)(iobounds1_w - ioptr_w))) {\n n = iobounds1_w - ioptr_w;\n }\n if (n > 0) {\n memmove(ioptr_w, src.ptr, n);\n *ptr_ioptr_w += n;\n }\n return n;\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_reader__set_limit(wuffs_base__io_reader* o,\n uint8_t* ioptr_r,\n uint64_t limit) {\n if (o && (((size_t)(o->private_impl.bounds[1] - ioptr_r)) > limit)) {\n o->private_impl.bounds[1] = ioptr_r + limit;\n }\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_reader__set_mark(wuffs_base__io_reader* o, uint8_t* mark) {\n o->private_impl.bounds[0] = " +
"mark;\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_writer__set(wuffs_base__io_writer* o,\n wuffs_base__io_buffer* b,\n uint8_t** ioptr1_ptr,\n uint8_t** ioptr2_ptr,\n wuffs_base__slice_u8 s) {\n b->ptr = s.ptr;\n b->len = s.len;\n b->wi = 0;\n b->ri = 0;\n b->closed = false;\n o->private_impl.buf = b;\n o->private_impl.bounds[0] = s.ptr;\n o->private_impl.bounds[1] = s.ptr + s.len;\n *ioptr1_ptr = s.ptr;\n *ioptr2_ptr = s.ptr + s.len;\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_writer__set_mark(wuffs_base__io_writer* o, uint8_t* mark) {\n o->private_impl.bounds[0] = mark;\n return ((wuffs_base__empty_struct){});\n}\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n#endif // WUFFS_INCLUDE_GUARD__BASE_PRIVATE\n" +
""
const baseImplC = "" +
"// Copyright 2018 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// !! INSERT base-public.h.\n\n#ifdef WUFFS_IMPLEMENTATION\n\n// !! INSERT base-private.h.\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE)\n\n// !! INSERT wuffs_base__status__string data.\n\nconst char* //\nwuffs_base__status__string(wuffs_base__status s) {\n uint16_t o = wuffs_base__status__string_offsets[(uint8_t)(s >> 24)];\n return o ? wuffs_base__status__string_data + o : \"unknown status\";\n}\n\n#endif " +
" // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE)\n\n#endif // WUFFS_IMPLEMENTATION\n" +
"_buffer__palette_changed(this);\n}\n\ninline wuffs_base__slice_u8 //\nwuffs_base__image_buffer::palette() {\n return wuffs_base__image_buffer__palette(this);\n}\n\ninline wuffs_base__table_u8 //\nwuffs_base__image_buffer::plane(uint32_t p) {\n return wuffs_base__image_buffer__plane(this, p);\n}\n\n#endif // __cplusplus\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n" +
""
type template_args_short_read struct {
+4 -3
View File
@@ -79,9 +79,10 @@ func genBase(out *bytes.Buffer) error {
files := []struct {
filename, varname string
}{
{"base/base-public.h", "basePublicH"},
{"base/base-private.h", "basePrivateH"},
{"base/base-impl.c", "baseImplC"},
{"base/base-public.h", "baseBasePublicH"},
{"base/base-private.h", "baseBasePrivateH"},
{"base/base-impl.c", "baseBaseImplC"},
{"base/image-public.h", "baseImagePublicH"},
}
for _, f := range files {
+22
View File
@@ -1092,6 +1092,28 @@ wuffs_base__io_buffer__struct::writer() {
#endif // __cplusplus
#ifdef __cplusplus
} // extern "C"
#endif
// Copyright 2017 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifdef __cplusplus
extern "C" {
#endif
// ---------------- Images
// wuffs_base__color_u32argb is an 8 bit per channel Alpha, Red, Green, Blue