针对 WebP 图片的 RIFF 容器操作。
Mux API
允许处理 WebP 容器映像,其中包含颜色等功能 配置文件、元数据、动画和碎片化图片。
代码示例
使用图片数据、颜色配置文件和 XMP 元数据创建 MUX
int copy_data = 0;
WebPMux* mux = WebPMuxNew();
// ... (Prepare image data).
WebPMuxSetImage(mux, &image, copy_data);
// ... (Prepare ICCP color profile data).
WebPMuxSetChunk(mux, "ICCP", &icc_profile, copy_data);
// ... (Prepare XMP metadata).
WebPMuxSetChunk(mux, "XMP ", &xmp, copy_data);
// Get data from mux in WebP RIFF format.
WebPMuxAssemble(mux, &output_data);
WebPMuxDelete(mux);
// ... (Consume output_data; e.g. write output_data.bytes to file).
WebPDataClear(&output_data);
从 WebP 文件中获取图像和颜色配置文件数据
int copy_data = 0;
// ... (Read data from file).
WebPMux* mux = WebPMuxCreate(&data, copy_data);
WebPMuxGetFrame(mux, 1, &image);
// ... (Consume image; e.g. call WebPDecode() to decode the data).
WebPMuxGetChunk(mux, "ICCP", &icc_profile);
// ... (Consume icc_data).
WebPMuxDelete(mux);
free(data);
Mux 对象的生命周期
枚举
// Error codes
typedef enum WebPMuxError {
  WEBP_MUX_OK                 =  1,
  WEBP_MUX_NOT_FOUND          =  0,
  WEBP_MUX_INVALID_ARGUMENT   = -1,
  WEBP_MUX_BAD_DATA           = -2,
  WEBP_MUX_MEMORY_ERROR       = -3,
  WEBP_MUX_NOT_ENOUGH_DATA    = -4
} WebPMuxError;
// IDs for different types of chunks.
typedef enum WebPChunkId {
  WEBP_CHUNK_VP8X,     // VP8X
  WEBP_CHUNK_ICCP,     // ICCP
  WEBP_CHUNK_ANIM,     // ANIM
  WEBP_CHUNK_ANMF,     // ANMF
  WEBP_CHUNK_ALPHA,    // ALPH
  WEBP_CHUNK_IMAGE,    // VP8/VP8L
  WEBP_CHUNK_EXIF,     // EXIF
  WEBP_CHUNK_XMP,      // XMP
  WEBP_CHUNK_UNKNOWN,  // Other chunks.
  WEBP_CHUNK_NIL
} WebPChunkId;
WebPGetMuxVersion()
返回 mux 库的版本号,使用
8 位(每个主要/次要/修订版本)。例如,v2.5.7 为 0x020507。
int WebPGetMuxVersion(void);
WebPMuxNew()
创建一个空的 mux 对象。
WebPMux* WebPMuxNew(void);
- 返回
- 指向新创建的空多路复用器对象的指针。
WebPMuxDelete()
删除 mux 对象。
void WebPMuxDelete(WebPMux* mux);
- 参数
- mux - 要删除的(输入/输出)对象
Mux 创建
WebPMuxCreate()
根据以 WebP RIFF 格式给出的原始数据创建 mux 对象。
WebPMux* WebPMuxCreate(const WebPData* bitstream, int copy_data);
- 参数
- bitstream -(输入)WebP RIFF 格式的比特流数据 
- copy_data -- (in) 值 1 表示给定数据将复制到多路复用器 (MCM) 值 0 表示不会将数据复制到 mux 对象。 
- 返回
- 指向根据给定数据创建的多路复用器对象的指针(如果成功)。 
- NULL - 如果出现无效数据或内存错误。 
非图片块
WebPMuxSetChunk()
在 mux 对象中添加 ID 为 fourcc 且数据为 chunk_data 的块。不限 具有相同 ID 的现有分块将被移除。
WebPMuxError WebPMuxSetChunk(WebPMux* mux,
                             const char fourcc[4],
                             const WebPData* chunk_data,
                             int copy_data);
注意:只有与图片无关的数据块才应通过分块 API 进行管理。
(与图片相关的分块为:“ANMF”、“FRGM”、“VP8”、“VP8L”和“ALPH”)。如需添加,
获取和删除映像、使用 WebPMuxSetImage()、
WebPMuxPushFrame(),
WebPMuxGetFrame()和
WebPMuxDeleteFrame()。
- 参数
- mux:要向其中添加数据块的(输入/输出)对象 
- fourcc - (in) 一个字符数组,其中包含给定的 chunk;例如“ICCP”、“XMP”、“EXIF”等等 
- chunk_data -(在)要添加的区块数据 
- copy_data -- (in) 值 1 表示给定数据将复制到多路复用器 (MCM) 值 0 表示不会将数据复制到 mux 对象。 
- 返回
- WEBP_MUX_INVALID_ARGUMENT- 如果 mux、fourcc 或 chunk_data 为 NULL,或者 Fourcc 对应于图像块。
- WEBP_MUX_MEMORY_ERROR- 内存分配错误时。
- WEBP_MUX_OK-- 表示成功。
WebPMuxGetChunk()
获取对 mux 对象中 ID 为 fourcc 的区块数据的引用。 调用方不应释放返回的数据。
WebPMuxError WebPMuxGetChunk(const WebPMux* mux,
                             const char fourcc[4],
                             WebPData* chunk_data);
- 参数
- mux:要从中提取区块数据的 (in) 对象 
- fourcc - 包含块的 Fourcc 的字符数组; 例如“ICCP”、“XMP”、“EXIF”等等 
- chunk_data --(输出)返回的区块数据 
- 返回
- WEBP_MUX_INVALID_ARGUMENT- 如果 mux、fourcc 或 chunk_data 为 NULL,或者 Fourcc 对应于图像块。
- WEBP_MUX_NOT_FOUND- 如果多路复用器不包含具有指定 id。
- WEBP_MUX_OK-- 表示成功。
WebPMuxDeleteChunk()
从多路复用对象中删除具有指定 fourcc 的块。
WebPMuxError WebPMuxDeleteChunk(WebPMux* mux, const char fourcc[4]);
- 参数
- mux:要从中删除区块的(输入/输出)对象 
- fourcc - 包含块的 Fourcc 的字符数组; 例如“ICCP”、“XMP”、“EXIF”等等 
- 返回
- WEBP_MUX_INVALID_ARGUMENT- 如果 mux 或 Fourcc 为 NULL 或 Fourcc 对应于图像块。
- WEBP_MUX_NOT_FOUND- 如果多路复用器不包含具有指定 Fourcc。
- WEBP_MUX_OK-- 表示成功。
图片
封装有关单个帧/fragment 的数据。
struct WebPMuxFrameInfo {
  WebPData    bitstream;  // image data: can be a raw VP8/VP8L bitstream
                          // or a single-image WebP file.
  int         x_offset;   // x-offset of the frame.
  int         y_offset;   // y-offset of the frame.
  int         duration;   // duration of the frame (in milliseconds).
  WebPChunkId id;         // frame type: should be one of WEBP_CHUNK_ANMF,
                          // WEBP_CHUNK_FRGM or WEBP_CHUNK_IMAGE
  WebPMuxAnimDispose dispose_method;  // Disposal method for the frame.
  WebPMuxAnimBlend   blend_method;    // Blend operation for the frame.
};
WebPMuxSetImage()
在 mux 对象中设置(非动画和未碎片化的)图片。 注意:所有现有图片(包括帧/片段)都会被移除。
WebPMuxError WebPMuxSetImage(WebPMux* mux,
                             const WebPData* bitstream,
                             int copy_data);
- 参数
- mux:要在其中设置图像的(输入/输出)对象 
- bitstream - (in) 可以是原始 VP8/VP8L 比特流,也可以是单图 WebP 文件(非动画和非片段) 
- copy_data -- (in) 值 1 表示给定数据将复制到多路复用器 (MCM) 值 0 表示不会将数据复制到 mux 对象。 
- 返回
- WEBP_MUX_INVALID_ARGUMENT- 如果多路复用器为 NULL 或比特流为 NULL。
- WEBP_MUX_MEMORY_ERROR- 内存分配错误时。
- WEBP_MUX_OK-- 表示成功。
WebPMuxPushFrame()
在 mux 对象的结尾添加帧。
WebPMuxError WebPMuxPushFrame(WebPMux* mux,
                              const WebPMuxFrameInfo* frame,
                              int copy_data);
注意:
- frame.id 应为 WEBP_CHUNK_ANMF或WEBP_CHUNK_FRGM
- 如需设置非动画形式的非碎片化图片,请使用
 WebPMuxSetImage()。
- 推送的帧的类型必须与 mux 中的帧相同。
- 由于 WebP 仅支持偶数偏移,因此任何奇数偏移都将被贴靠 使用偏移值 &= ~1
- 参数
- mux:要向其中添加帧的(输入/输出)对象。 
- frame - (in) 帧数据。 
- copy_data -- (in) 值 1 表示给定数据将复制到多路复用器 (MCM) 值 0 表示不会将数据复制到 mux 对象。 
- 返回
- WEBP_MUX_INVALID_ARGUMENT- 如果多路复用器或帧为 NULL,或者 frame 无效。
- WEBP_MUX_MEMORY_ERROR- 内存分配错误时。
- WEBP_MUX_OK-- 表示成功。
- WEBP_MUX_MEMORY_ERROR- 内存分配错误时。
WebPMuxGetFrame()
从多路复用对象获取第 n 帧。frame->bitstream 的内容是
使用 malloc() 分配,并且不归 mux 对象所有。它必须是
由调用方通过调用 WebPDataClear() 取消分配。n=0
具有特殊含义 - 最后的位置。
WebPMuxError WebPMuxGetFrame(const WebPMux* mux,
                             uint32_t nth,
                             WebPMuxFrameInfo* frame);
- 参数
- mux:要从中提取信息的对象(输入) 
- nth -- (in) mux 对象中帧的索引 
- frame - 所返回帧的数据 
- 返回
- WEBP_MUX_INVALID_ARGUMENT-- if mux or frame is NULL.
- WEBP_MUX_NOT_FOUND- 如果多路复用器中的帧数少于 n 帧 对象。
- WEBP_MUX_BAD_DATA- 如果多路复用器中的第 n 个帧数据块无效。
- WEBP_MUX_OK-- 表示成功。
WebPMuxDeleteFrame()
从 mux 对象中删除帧。nth=0 具有特殊含义 - last 排名。
WebPMuxError WebPMuxDeleteFrame(WebPMux* mux, uint32_t nth);
- 参数
- mux:要从中删除帧的(输入/输出)对象 
- nth - (in) 要从中删除帧的位置。 
- 返回
- WEBP_MUX_INVALID_ARGUMENT-- if mux is NULL.
- WEBP_MUX_NOT_FOUND- 如果多路复用器中的帧数少于 n 帧 对象。
- WEBP_MUX_OK-- 表示成功。
动画
动画参数
struct WebPMuxAnimParams {
  uint32_t bgcolor;  // Background color of the canvas stored (in MSB order) as:
                     // Bits 00 to 07: Alpha.
                     // Bits 08 to 15: Red.
                     // Bits 16 to 23: Green.
                     // Bits 24 to 31: Blue.
  int loop_count;    // Number of times to repeat the animation [0 = infinite].
};
WebPMuxSetAnimationParams()
设置 mux 对象中的动画参数。任何现有的 ANIM 块 删除。
WebPMuxError WebPMuxSetAnimationParams(WebPMux* mux,
                                       const WebPMuxAnimParams* params);
- 参数
- mux:要在其中设置/添加 ANIM 分块的(输入/输出)对象 
- params - (in) 动画参数。 
- 返回
- WEBP_MUX_INVALID_ARGUMENT- 如果多路复用器或参数为 NULL,则为 。
- WEBP_MUX_MEMORY_ERROR- 内存分配错误时。
- WEBP_MUX_OK-- 表示成功。
WebPMuxGetAnimationParams()
从 mux 对象中获取动画参数。
WebPMuxError WebPMuxGetAnimationParams(const WebPMux* mux,
                                       WebPMuxAnimParams* params);
- 参数
- mux - 要从中提取动画参数的 (in) 对象 
- params - (输出)从 ANIM 分块中提取的动画参数 
- 返回
- WEBP_MUX_INVALID_ARGUMENT- 如果多路复用器或参数为 NULL,则为 。
- WEBP_MUX_NOT_FOUND- 如果多路复用对象中不存在 ANIM 区块,
- WEBP_MUX_OK-- 表示成功。
其他公用事业
WebPMuxGetCanvasSize()
从 mux 对象获取画布尺寸。
WebPMuxError WebPMuxGetCanvasSize(const WebPMux* mux,
                                  int* width,
                                  int* height);
注意:此方法假定 VP8X 分块(如果存在)是最新的。
也就是说,自上次调用
WebPMuxAssemble() 或 WebPMuxCreate()。
- 参数
- mux:要从中提取画布尺寸的 (in) 对象 
- width -(输出)画布宽度 
- height - (输出)画布高度 
- 返回
- WEBP_MUX_INVALID_ARGUMENT-- if mux, width or height is NULL.
- WEBP_MUX_BAD_DATA- 如果 VP8X/VP8/VP8L 区块或画布大小无效。
- WEBP_MUX_OK-- 表示成功。
WebPMuxGetFeatures()
从 mux 对象获取功能标记。
WebPMuxError WebPMuxGetFeatures(const WebPMux* mux, uint32_t* flags);
注意:此方法假定 VP8X 分块(如果存在)是最新的。
也就是说,自上次调用
WebPMuxAssemble() 或 WebPMuxCreate()。
- 参数
- mux:要从中提取特征的 (in) 对象 
- flags --(输出)用于指定多路复用器中存在哪些功能的标记 对象。这将是各种标志值的 OR。枚举 - WebPFeatureFlags可用于测试各个标志值。
- 返回
- WEBP_MUX_INVALID_ARGUMENT-- if mux or flags is NULL.
- WEBP_MUX_BAD_DATA- 如果 VP8X/VP8/VP8L 区块或画布大小无效。
- WEBP_MUX_OK-- 表示成功。
WebPMuxNumChunks()
获取 mux 对象中具有指定标记值的分块的数量。
WebPMuxError WebPMuxNumChunks(const WebPMux* mux,
                              WebPChunkId id,
                              int* num_elements);
- 参数
- mux:要从中提取信息的对象(输入) 
- id -- (in) 分块 ID,用于指定分块的类型 
- num_elements -- 具有指定分块 ID 的分块数量 
- 返回
- WEBP_MUX_INVALID_ARGUMENT-- if mux, or num_elements is NULL.
- WEBP_MUX_OK-- 表示成功。
WebPMuxAssemble()
以 WebP RIFF 格式汇编所有分块,并以 assembled_data 返回。 此函数还会验证 mux 对象。
WebPMuxError WebPMuxAssemble(WebPMux* mux, WebPData* assembled_data);
注意:assembled_data 的内容将被忽略和覆盖。
此外,assembled_data 的内容是使用 malloc() 而非 NOT 分配
mux 对象拥有的对象。该内存块必须由调用方通过调用
WebPDataClear()。
- 参数
- mux:要组合其分块的(输入/输出)对象。 
- assembled_data - (输出)汇编的 WebP 数据 
- 返回
- WEBP_MUX_BAD_DATA- 如果多路复用对象无效。
- WEBP_MUX_INVALID_ARGUMENT-- if mux or assembled_data is NULL.
- WEBP_MUX_MEMORY_ERROR- 内存分配错误时。
- WEBP_MUX_OK-- 表示成功。
WebPAnimEncoder API
此 API 允许对动画 WebP 图像进行编码(可能)。
代码示例
WebPAnimEncoderOptions enc_options;
WebPAnimEncoderOptionsInit(&enc_options);
// Tune 'enc_options' as needed.
WebPAnimEncoder* enc = WebPAnimEncoderNew(width, height, &enc_options);
while(<there are more frames>) {
  WebPConfig config;
  WebPConfigInit(&config);
  // Tune 'config' as needed.
  WebPAnimEncoderAdd(enc, frame, timestamp_ms, &config);
}
WebPAnimEncoderAdd(enc, NULL, timestamp_ms, NULL);
WebPAnimEncoderAssemble(enc, webp_data);
WebPAnimEncoderDelete(enc);
// Write the 'webp_data' to a file, or re-mux it further.
typedef struct WebPAnimEncoder WebPAnimEncoder;  // Main opaque object.
全局选项
struct WebPAnimEncoderOptions {
  WebPMuxAnimParams anim_params;  // Animation parameters.
  int minimize_size;    // If true, minimize the output size (slow). Implicitly
                        // disables key-frame insertion.
  int kmin;
  int kmax;             // Minimum and maximum distance between consecutive key
                        // frames in the output. The library may insert some key
                        // frames as needed to satisfy this criteria.
                        // Note that these conditions should hold: kmax > kmin
                        // and kmin >= kmax / 2 + 1. Also, if kmax <= 0, then
                        // key-frame insertion is disabled; and if kmax == 1,
                        // then all frames will be key-frames (kmin value does
                        // not matter for these special cases).
  int allow_mixed;      // If true, use mixed compression mode; may choose
                        // either lossy and lossless for each frame.
  int verbose;          // If true, print info and warning messages to stderr.
};
WebPAnimEncoderOptionsInit()
应始终进行调用,以初始化新的 WebPAnimEncoderOptions 修改之前的结构如果版本不匹配,则返回 false。 WebPAnimEncoderOptionsInit() 必须先成功,然后才能使用 enc_options 对象。
- 参数
- enc_options -(输入/输出)用于对动画进行编码的选项
- 返回
- 如果成功,则为 true。
int WebPAnimEncoderOptionsInit(
    WebPAnimEncoderOptions* enc_options);
WebPAnimEncoderNew()
创建并初始化 WebPAnimEncoder 对象。
- 参数
- width/height - (in) 动画的画布宽度和高度。 
- enc_options - (in) 编码选项;可以传递 NULL 来选择 设置合理的默认值 
- 返回
- 指向新创建的 WebPAnimEncoder 对象的指针,如果为 NULL,则为 NULL 错误。 
WebPAnimEncoder* WebPAnimEncoderNew(
    int width, int height, const WebPAnimEncoderOptions* enc_options);
WebPAnimEncoderAdd()
针对 WebP 优化给定帧,对其进行编码并将其添加到 WebPAnimEncoder 对象。
最后一次调用 WebPAnimEncoderAdd 应该使用 frame = NULL,
表示不再添加更多帧。此调用还用于
确定最后一帧的时长。
- 参数
- enc:要向其中添加帧的 (in/out) 对象。 
- frame - ARGB 或 YUV(A) 格式的(输入/输出)帧数据。如果 YUV(A) 格式,因此会将其转换为 ARGB,这会造成轻微的损失。 
- timestamp_ms - 此帧的时间戳(以毫秒为单位)。时长 帧的时间戳的计算方法为“下一帧的时间戳 - 下一帧的时间戳” 这一帧”。因此,时间戳应按非降序排列。 
- config - (in) 编码选项;可以传递 NULL 来选择合理的 默认值。 
- 返回
- 如果发生错误,则返回 false,并适当设置 - frame->error_code。 否则,返回 true。
int WebPAnimEncoderAdd(
    WebPAnimEncoder* enc, struct WebPPicture* frame, int timestamp_ms,
    const struct WebPConfig* config);
WebPAnimEncoderAssemble()
将迄今为止添加的所有帧汇总到一个 WebP 比特流中。此调用应为
后跟使用 frame = NULL 调用 WebPAnimEncoderAdd;否则,
最后一帧的时长将在内部估算。
- 参数
- enc --(输入/输出)对象,用于组合框架。 
- webp_data - (输出)生成的 WebP 比特流。 
- 返回
- 成功时为 true。 
int WebPAnimEncoderAssemble(WebPAnimEncoder* enc, WebPData* webp_data);
WebPAnimEncoderGetError()
使用 enc 获取与最近一次调用相对应的错误字符串。通过
返回的字符串由 enc 拥有,并且仅在下次调用
WebPAnimEncoderAdd() 或
WebPAnimEncoderAssemble() 或
WebPAnimEncoderDelete()。
- 参数
- enc - 要从中提取错误字符串的(输入/输出)对象。
- 返回
- NULL if enc is NULL.否则,如果最后一个 对 enc 的调用发生了错误;如果上次调用是 成功。
const char* WebPAnimEncoderGetError(WebPAnimEncoder* enc);
WebPAnimEncoderDelete()
删除 WebPAnimEncoder 对象。
- 参数
- enc - 要删除的 (in/out) 对象
void WebPAnimEncoderDelete(WebPAnimEncoder* enc);
Demux API
支持从 WebP 文件中提取图片和扩展格式数据。
代码示例
对 WebP 数据进行多路分配以提取所有帧、ICC 配置文件和 EXIF/XMP 元数据
WebPDemuxer* demux = WebPDemux(&webp_data);
uint32_t width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
uint32_t height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
// ... (Get information about the features present in the WebP file).
uint32_t flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);
// ... (Iterate over all frames).
WebPIterator iter;
if (WebPDemuxGetFrame(demux, 1, &iter)) {
  do {
    // ... (Consume 'iter'; e.g. Decode 'iter.fragment' with WebPDecode(),
    // ... and get other frame properties like width, height, offsets etc.
    // ... see 'struct WebPIterator' below for more info).
  } while (WebPDemuxNextFrame(&iter));
  WebPDemuxReleaseIterator(&iter);
}
// ... (Extract metadata).
WebPChunkIterator chunk_iter;
if (flags & ICCP_FLAG) WebPDemuxGetChunk(demux, "ICCP", 1, &chunk_iter);
// ... (Consume the ICC profile in 'chunk_iter.chunk').
WebPDemuxReleaseChunkIterator(&chunk_iter);
if (flags & EXIF_FLAG) WebPDemuxGetChunk(demux, "EXIF", 1, &chunk_iter);
// ... (Consume the EXIF metadata in 'chunk_iter.chunk').
WebPDemuxReleaseChunkIterator(&chunk_iter);
if (flags & XMP_FLAG) WebPDemuxGetChunk(demux, "XMP ", 1, &chunk_iter);
// ... (Consume the XMP metadata in 'chunk_iter.chunk').
WebPDemuxReleaseChunkIterator(&chunk_iter);
WebPDemuxDelete(demux);
多路分配器对象的生命周期
枚举
typedef enum WebPDemuxState {
  WEBP_DEMUX_PARSE_ERROR    = -1,  // An error occurred while parsing.
  WEBP_DEMUX_PARSING_HEADER =  0,  // Not enough data to parse full header.
  WEBP_DEMUX_PARSED_HEADER  =  1,  // Header parsing complete,
                                   // data may be available.
  WEBP_DEMUX_DONE           =  2   // Entire file has been parsed.
} WebPDemuxState;
WebPGetDemuxVersion()
返回多路分配库的版本号,使用
8 位(每个主要/次要/修订版本)。例如,v2.5.7 为 0x020507。
int WebPGetDemuxVersion(void);
WebPDemux()
解析 data 提供的完整 WebP 文件。
WebPDemuxer WebPDemux(const WebPData* data);
解析成功时返回 WebPDemuxer 对象,否则返回 NULL。
WebPDemuxPartial()
解析由 data 提供的可能不完整的 WebP 文件。如果 state 为 非 NULL 值,则系统将设置该参数以指示多路分配器的状态。
WebPDemuxer WebPDemuxPartial(const WebPData* data, WebPDemuxState* state);
如果发生错误或数据不足,无法开始解析,则返回 NULL;
以及 WebPDemuxer 对象。
请注意,WebPDemuxer 会保留指向“数据”内存段的内部指针。如果
此数据是易变的,应删除多路分配器对象(通过调用
WebPDemuxDelete())和
对新数据再次调用了 WebPDemuxPartial()。这是
通常是一项开销很低的操作。
WebPDemuxDelete()
释放与 dmux 关联的内存。
void WebPDemuxDelete(WebPDemuxer* dmux);
数据/信息提取
typedef enum WebPFormatFeature {
  WEBP_FF_FORMAT_FLAGS,      // bit-wise combination of WebPFeatureFlags
                             // corresponding to the 'VP8X' chunk (if present).
  WEBP_FF_CANVAS_WIDTH,
  WEBP_FF_CANVAS_HEIGHT,
  WEBP_FF_LOOP_COUNT,        // only relevant for animated file
  WEBP_FF_BACKGROUND_COLOR,  // idem.
  WEBP_FF_FRAME_COUNT        // Number of frames present in the demux object.
                             // In case of a partial demux, this is the number
                             // of frames seen so far, with the last frame
                             // possibly being partial.
} WebPFormatFeature;
WebPDemuxGetI()
从 dmux 获取 feature 值。
uint32_t WebPDemuxGetI(const WebPDemuxer* dmux, WebPFormatFeature feature);
注意:值仅在使用 WebPDemux() 或
WebPDemuxPartial() 返回了一个状态 >
WEBP_DEMUX_PARSING_HEADER。
帧迭代
struct WebPIterator {
  int frame_num;
  int num_frames;          // equivalent to WEBP_FF_FRAME_COUNT.
  int fragment_num;
  int num_fragments;
  int x_offset, y_offset;  // offset relative to the canvas.
  int width, height;       // dimensions of this frame or fragment.
  int duration;            // display duration in milliseconds.
  WebPMuxAnimDispose dispose_method;  // dispose method for the frame.
  int complete;   // true if 'fragment' contains a full frame. partial images
                  // may still be decoded with the WebP incremental decoder.
  WebPData fragment;  // The frame or fragment given by 'frame_num' and
                      // 'fragment_num'.
  int has_alpha;      // True if the frame or fragment contains transparency.
  WebPMuxAnimBlend blend_method;  // Blend operation for the frame.
};
WebPDemuxGetFrame()
从 dmux 检索帧 frame_number。
int WebPDemuxGetFrame(const WebPDemuxer* dmux,
                      int frame_number,
                      WebPIterator* iter);
从此函数返回时,iter->fragment 指向第一个片段。
您可以使用
WebPDemuxSelectFragment()。设置
如果 frame_number 等于 0,则系统会返回图片的最后一帧。
如果 dmux 为 NULL 或帧 dmux 不存在,则返回 false。
使用 WebPDemuxReleaseIterator()
完成。
注意:dmux 必须在 iter 的生命周期内持续存在。
WebPDemuxNextFrame()、WebPDemuxPrevFrame()
将 iter->fragment 设置为指向下一个 (iter->fragment + 1) 或 上一 (iter->frame_num - 1) 帧。这些函数不会循环。
int WebPDemuxNextFrame(WebPIterator* iter);
int WebPDemuxPrevFrame(WebPIterator* iter);
成功时返回 true,否则返回 false。
WebPDemuxSelectFragment()
设置 iter->fragment 以反映 fragment 编号 iter->fragment。
int WebPDemuxSelectFragment(WebPIterator* iter, int fragment_num);
如果存在 fragment fragment_num,则返回 true,否则返回 false。
WebPDemuxReleaseIterator()
释放与 iter 关联的所有内存。
void WebPDemuxReleaseIterator(WebPIterator* iter);
必须在对
WebPDemuxGetChunk()。此外,必须
在使用以下参数销毁关联的 WebPDemuxer 之前调用:
WebPDemuxDelete()。
文本块迭代
struct WebPChunkIterator {
  // The current and total number of chunks with the fourcc given to
  // WebPDemuxGetChunk().
  int chunk_num;
  int num_chunks;
  WebPData chunk;    // The payload of the chunk.
};
WebPDemuxGetChunk()
从以下位置检索 ID 为 chunk_number 的区块的 chunk_number 实例: dmux。
int WebPDemuxGetChunk(const WebPDemuxer* dmux,
                      const char fourcc[4], int chunk_number,
                      WebPChunkIterator* iter);
fourcc 是一个字符数组,其中包含要返回的分块的 Fourcc; 例如“ICCP”、“XMP”、“EXIF”等
如果将 chunk_number 设为 0,系统会返回集合中的最后一个分块。
如果找到数据块,则返回 true,否则返回 false。图片相关数据块
载荷通过 WebPDemuxGetFrame() 访问
以及相关函数。致电
WebPDemuxReleaseChunkIterator()(使用时)
完成。
注意:dmux 必须在迭代器的生命周期内持续存在。
WebPDemuxNextChunk()、WebPDemuxPrevChunk()
将 iter->block 设置为指向下一个 (iter->chunk_num + 1) 或 前一个 (iter->chunk_num - 1) 块。这些函数不会循环。
int WebPDemuxNextChunk(WebPChunkIterator* iter);
int WebPDemuxPrevChunk(WebPChunkIterator* iter);
成功时返回 true,否则返回 false。
WebPDemuxReleaseChunkIterator()
释放与 iter 关联的所有内存。
void WebPDemuxReleaseChunkIterator(WebPChunkIterator* iter);
必须在使用WebPDemuxer
WebPDemuxDelete()。
WebPAnimDecoder API
此 API 允许解码(可能)动画 WebP 图片。
代码示例
WebPAnimDecoderOptions dec_options;
WebPAnimDecoderOptionsInit(&dec_options);
// Tune 'dec_options' as needed.
WebPAnimDecoder* dec = WebPAnimDecoderNew(webp_data, &dec_options);
WebPAnimInfo anim_info;
WebPAnimDecoderGetInfo(dec, &anim_info);
for (uint32_t i = 0; i < anim_info.loop_count; ++i) {
  while (WebPAnimDecoderHasMoreFrames(dec)) {
    uint8_t* buf;
    int timestamp;
    WebPAnimDecoderGetNext(dec, &buf, ×tamp);
    // ... (Render 'buf' based on 'timestamp').
    // ... (Do NOT free 'buf', as it is owned by 'dec').
  }
  WebPAnimDecoderReset(dec);
}
const WebPDemuxer* demuxer = WebPAnimDecoderGetDemuxer(dec);
// ... (Do something using 'demuxer'; e.g. get EXIF/XMP/ICC data).
WebPAnimDecoderDelete(dec);
typedef struct WebPAnimDecoder WebPAnimDecoder;  // Main opaque object.
全局选项
struct WebPAnimDecoderOptions {
  // Output colorspace. Only the following modes are supported:
  // MODE_RGBA, MODE_BGRA, MODE_rgbA and MODE_bgrA.
  WEBP_CSP_MODE color_mode;
  int use_threads;           // If true, use multi-threaded decoding.
};
WebPAnimDecoderOptionsInit()
应始终进行调用,以初始化新的 WebPAnimDecoderOptions 修改之前的结构如果版本不匹配,则返回 false。 WebPAnimDecoderOptionsInit() 只有在调用成功后才能使用 dec_options 对象表示)。
参数
dec_options -(输入/输出)用于对动画进行解码的选项
- 返回
- 成功时为 true
int WebPAnimDecoderOptionsInit(
    WebPAnimDecoderOptions* dec_options);
WebPAnimDecoderNew()
创建并初始化 WebPAnimDecoder 对象。
- 参数
- webp_data -(输入)WebP 比特流。在实验期间 输出 WebPAnimDecoder 对象的生命周期。 
- dec_options - (in) 解码选项。可以传递 NULL 进行选择 合理的默认值(具体而言,应选择颜色模式 MODE_RGBA)。 
- 返回
- 指向新创建的 WebPAnimDecoder 对象的指针;如果为 NULL,则为 NULL 解析错误、无效选项或内存错误。 
WebPAnimDecoder* WebPAnimDecoderNew(
    const WebPData* webp_data, const WebPAnimDecoderOptions* dec_options);
动画的全局信息。
struct WebPAnimInfo {
  uint32_t canvas_width;
  uint32_t canvas_height;
  uint32_t loop_count;
  uint32_t bgcolor;
  uint32_t frame_count;
};
WebPAnimDecoderGetInfo()
获取有关动画的全局信息。
- 参数
- dec - 用于获取信息的解码器实例(输入)。 
- info - 从动画中提取的全局信息。 
- 返回
- 成功时为 true。 
int WebPAnimDecoderGetInfo(const WebPAnimDecoder* dec,
                           WebPAnimInfo* info);
WebPAnimDecoderGetNext()
根据提供给以下对象的选项,从 dec 获取下一帧:
WebPAnimDecoderNew()。这将是
大小为 canvas_width * 4 * canvas_height 的重建画布,不仅仅
边框子矩形。返回的缓冲区 buf 仅在
给WebPAnimDecoderGetNext()的下一次通话,
WebPAnimDecoderReset() 或
WebPAnimDecoderDelete()。
- 参数
- dec - 作为下一帧的(输入/输出)解码器实例。 已提取。 
- buf - (输出)已解码的帧。 
- timestamp - 帧的时间戳(以毫秒为单位)。 
- 返回
- 如果任何参数为 NULL,或者存在解析或 解码错误或帧数都没有变化的情况。否则,返回 true。 
int WebPAnimDecoderGetNext(WebPAnimDecoder* dec,
                           uint8_t** buf, int* timestamp);
WebPAnimDecoderHasMoreFrames()
检查是否还有更多帧要解码。
- 参数
- dec -- (in) 要检查的解码器实例。
- 返回
- 如果 dec 不为 NULL 且某些帧尚未解码,则为 true。 否则,返回 false。
int WebPAnimDecoderHasMoreFrames(const WebPAnimDecoder* dec);
WebPAnimDecoderReset()
重置 WebPAnimDecoder 对象,以便系统下次调用
WebPAnimDecoderGetNext() 将重新开始解码
从第 1 帧开始当所有帧都需要解码时,这会非常有用
多次(例如 info.loop_count 次),而不会销毁并重新创建
dec 对象。
- 参数
- dec -- 要重置的(输入/输出)解码器实例
void WebPAnimDecoderReset(WebPAnimDecoder* dec);
WebPAnimDecoderGetDemuxer()
获取内部多路分配器对象。
如果要仅使用运算,获取多路分配器对象会很有用
可通过多路分配器获得;例如以获取 XMP/EXIF/ICC 元数据。返回的
多路分配器对象归 dec 所有,且仅在下次调用
WebPAnimDecoderDelete()。
- 参数
- dec -- (输入)解码器实例,要从中复制多路复用器对象 已提取。
const WebPDemuxer* WebPAnimDecoderGetDemuxer(const WebPAnimDecoder* dec);
WebPAnimDecoderDelete()
删除 WebPAnimDecoder 对象。
- 参数
- dec - 要删除的(输入/输出)解码器实例。
- 返回
- 如果成功,则为 true。
void WebPAnimDecoderDelete(WebPAnimDecoder* dec);
常见数据类型
枚举
typedef enum WebPFeatureFlags {
  FRAGMENTS_FLAG  = 0x00000001,
  ANIMATION_FLAG  = 0x00000002,
  XMP_FLAG        = 0x00000004,
  EXIF_FLAG       = 0x00000008,
  ALPHA_FLAG      = 0x00000010,
  ICCP_FLAG       = 0x00000020
} WebPFeatureFlags;
// Dispose method (animation only). Indicates how the area used by the current
// frame is to be treated before rendering the next frame on the canvas.
typedef enum WebPMuxAnimDispose {
  WEBP_MUX_DISPOSE_NONE,       // Do not dispose.
  WEBP_MUX_DISPOSE_BACKGROUND  // Dispose to background color.
} WebPMuxAnimDispose;
// Blend operation (animation only). Indicates how transparent pixels of the
// current frame are blended with those of the previous canvas.
typedef enum WebPMuxAnimBlend {
  WEBP_MUX_BLEND,              // Blend.
  WEBP_MUX_NO_BLEND            // Do not blend.
} WebPMuxAnimBlend;
WebPDataInit()
使用默认值初始化 webp_data 对象的内容。
void WebPDataInit(WebPData* webp_data);
WebPDataClear()
通过调用 free() 清除 webp_data 对象的内容。不符合
取消分配对象本身。
void WebPDataClear(WebPData* webp_data);
WebPDataCopy()
为 dst 分配必要的存储空间并复制 src 的内容。 成功时返回 true。
int WebPDataCopy(const WebPData* src, WebPData* dst);
