Path: blob/master/3rdparty/libwebp/src/webp/decode.h
16348 views
// Copyright 2010 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Main decoding functions for WebP images.10//11// Author: Skal ([email protected])1213#ifndef WEBP_WEBP_DECODE_H_14#define WEBP_WEBP_DECODE_H_1516#include "./types.h"1718#ifdef __cplusplus19extern "C" {20#endif2122#define WEBP_DECODER_ABI_VERSION 0x0208 // MAJOR(8b) + MINOR(8b)2324// Note: forward declaring enumerations is not allowed in (strict) C and C++,25// the types are left here for reference.26// typedef enum VP8StatusCode VP8StatusCode;27// typedef enum WEBP_CSP_MODE WEBP_CSP_MODE;28typedef struct WebPRGBABuffer WebPRGBABuffer;29typedef struct WebPYUVABuffer WebPYUVABuffer;30typedef struct WebPDecBuffer WebPDecBuffer;31typedef struct WebPIDecoder WebPIDecoder;32typedef struct WebPBitstreamFeatures WebPBitstreamFeatures;33typedef struct WebPDecoderOptions WebPDecoderOptions;34typedef struct WebPDecoderConfig WebPDecoderConfig;3536// Return the decoder's version number, packed in hexadecimal using 8bits for37// each of major/minor/revision. E.g: v2.5.7 is 0x020507.38WEBP_EXTERN int WebPGetDecoderVersion(void);3940// Retrieve basic header information: width, height.41// This function will also validate the header, returning true on success,42// false otherwise. '*width' and '*height' are only valid on successful return.43// Pointers 'width' and 'height' can be passed NULL if deemed irrelevant.44WEBP_EXTERN int WebPGetInfo(const uint8_t* data, size_t data_size,45int* width, int* height);4647// Decodes WebP images pointed to by 'data' and returns RGBA samples, along48// with the dimensions in *width and *height. The ordering of samples in49// memory is R, G, B, A, R, G, B, A... in scan order (endian-independent).50// The returned pointer should be deleted calling WebPFree().51// Returns NULL in case of error.52WEBP_EXTERN uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size,53int* width, int* height);5455// Same as WebPDecodeRGBA, but returning A, R, G, B, A, R, G, B... ordered data.56WEBP_EXTERN uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size,57int* width, int* height);5859// Same as WebPDecodeRGBA, but returning B, G, R, A, B, G, R, A... ordered data.60WEBP_EXTERN uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size,61int* width, int* height);6263// Same as WebPDecodeRGBA, but returning R, G, B, R, G, B... ordered data.64// If the bitstream contains transparency, it is ignored.65WEBP_EXTERN uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size,66int* width, int* height);6768// Same as WebPDecodeRGB, but returning B, G, R, B, G, R... ordered data.69WEBP_EXTERN uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size,70int* width, int* height);717273// Decode WebP images pointed to by 'data' to Y'UV format(*). The pointer74// returned is the Y samples buffer. Upon return, *u and *v will point to75// the U and V chroma data. These U and V buffers need NOT be passed to76// WebPFree(), unlike the returned Y luma one. The dimension of the U and V77// planes are both (*width + 1) / 2 and (*height + 1)/ 2.78// Upon return, the Y buffer has a stride returned as '*stride', while U and V79// have a common stride returned as '*uv_stride'.80// Return NULL in case of error.81// (*) Also named Y'CbCr. See: http://en.wikipedia.org/wiki/YCbCr82WEBP_EXTERN uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size,83int* width, int* height,84uint8_t** u, uint8_t** v,85int* stride, int* uv_stride);8687// Releases memory returned by the WebPDecode*() functions above.88WEBP_EXTERN void WebPFree(void* ptr);8990// These five functions are variants of the above ones, that decode the image91// directly into a pre-allocated buffer 'output_buffer'. The maximum storage92// available in this buffer is indicated by 'output_buffer_size'. If this93// storage is not sufficient (or an error occurred), NULL is returned.94// Otherwise, output_buffer is returned, for convenience.95// The parameter 'output_stride' specifies the distance (in bytes)96// between scanlines. Hence, output_buffer_size is expected to be at least97// output_stride x picture-height.98WEBP_EXTERN uint8_t* WebPDecodeRGBAInto(99const uint8_t* data, size_t data_size,100uint8_t* output_buffer, size_t output_buffer_size, int output_stride);101WEBP_EXTERN uint8_t* WebPDecodeARGBInto(102const uint8_t* data, size_t data_size,103uint8_t* output_buffer, size_t output_buffer_size, int output_stride);104WEBP_EXTERN uint8_t* WebPDecodeBGRAInto(105const uint8_t* data, size_t data_size,106uint8_t* output_buffer, size_t output_buffer_size, int output_stride);107108// RGB and BGR variants. Here too the transparency information, if present,109// will be dropped and ignored.110WEBP_EXTERN uint8_t* WebPDecodeRGBInto(111const uint8_t* data, size_t data_size,112uint8_t* output_buffer, size_t output_buffer_size, int output_stride);113WEBP_EXTERN uint8_t* WebPDecodeBGRInto(114const uint8_t* data, size_t data_size,115uint8_t* output_buffer, size_t output_buffer_size, int output_stride);116117// WebPDecodeYUVInto() is a variant of WebPDecodeYUV() that operates directly118// into pre-allocated luma/chroma plane buffers. This function requires the119// strides to be passed: one for the luma plane and one for each of the120// chroma ones. The size of each plane buffer is passed as 'luma_size',121// 'u_size' and 'v_size' respectively.122// Pointer to the luma plane ('*luma') is returned or NULL if an error occurred123// during decoding (or because some buffers were found to be too small).124WEBP_EXTERN uint8_t* WebPDecodeYUVInto(125const uint8_t* data, size_t data_size,126uint8_t* luma, size_t luma_size, int luma_stride,127uint8_t* u, size_t u_size, int u_stride,128uint8_t* v, size_t v_size, int v_stride);129130//------------------------------------------------------------------------------131// Output colorspaces and buffer132133// Colorspaces134// Note: the naming describes the byte-ordering of packed samples in memory.135// For instance, MODE_BGRA relates to samples ordered as B,G,R,A,B,G,R,A,...136// Non-capital names (e.g.:MODE_Argb) relates to pre-multiplied RGB channels.137// RGBA-4444 and RGB-565 colorspaces are represented by following byte-order:138// RGBA-4444: [r3 r2 r1 r0 g3 g2 g1 g0], [b3 b2 b1 b0 a3 a2 a1 a0], ...139// RGB-565: [r4 r3 r2 r1 r0 g5 g4 g3], [g2 g1 g0 b4 b3 b2 b1 b0], ...140// In the case WEBP_SWAP_16BITS_CSP is defined, the bytes are swapped for141// these two modes:142// RGBA-4444: [b3 b2 b1 b0 a3 a2 a1 a0], [r3 r2 r1 r0 g3 g2 g1 g0], ...143// RGB-565: [g2 g1 g0 b4 b3 b2 b1 b0], [r4 r3 r2 r1 r0 g5 g4 g3], ...144145typedef enum WEBP_CSP_MODE {146MODE_RGB = 0, MODE_RGBA = 1,147MODE_BGR = 2, MODE_BGRA = 3,148MODE_ARGB = 4, MODE_RGBA_4444 = 5,149MODE_RGB_565 = 6,150// RGB-premultiplied transparent modes (alpha value is preserved)151MODE_rgbA = 7,152MODE_bgrA = 8,153MODE_Argb = 9,154MODE_rgbA_4444 = 10,155// YUV modes must come after RGB ones.156MODE_YUV = 11, MODE_YUVA = 12, // yuv 4:2:0157MODE_LAST = 13158} WEBP_CSP_MODE;159160// Some useful macros:161static WEBP_INLINE int WebPIsPremultipliedMode(WEBP_CSP_MODE mode) {162return (mode == MODE_rgbA || mode == MODE_bgrA || mode == MODE_Argb ||163mode == MODE_rgbA_4444);164}165166static WEBP_INLINE int WebPIsAlphaMode(WEBP_CSP_MODE mode) {167return (mode == MODE_RGBA || mode == MODE_BGRA || mode == MODE_ARGB ||168mode == MODE_RGBA_4444 || mode == MODE_YUVA ||169WebPIsPremultipliedMode(mode));170}171172static WEBP_INLINE int WebPIsRGBMode(WEBP_CSP_MODE mode) {173return (mode < MODE_YUV);174}175176//------------------------------------------------------------------------------177// WebPDecBuffer: Generic structure for describing the output sample buffer.178179struct WebPRGBABuffer { // view as RGBA180uint8_t* rgba; // pointer to RGBA samples181int stride; // stride in bytes from one scanline to the next.182size_t size; // total size of the *rgba buffer.183};184185struct WebPYUVABuffer { // view as YUVA186uint8_t* y, *u, *v, *a; // pointer to luma, chroma U/V, alpha samples187int y_stride; // luma stride188int u_stride, v_stride; // chroma strides189int a_stride; // alpha stride190size_t y_size; // luma plane size191size_t u_size, v_size; // chroma planes size192size_t a_size; // alpha-plane size193};194195// Output buffer196struct WebPDecBuffer {197WEBP_CSP_MODE colorspace; // Colorspace.198int width, height; // Dimensions.199int is_external_memory; // If non-zero, 'internal_memory' pointer is not200// used. If value is '2' or more, the external201// memory is considered 'slow' and multiple202// read/write will be avoided.203union {204WebPRGBABuffer RGBA;205WebPYUVABuffer YUVA;206} u; // Nameless union of buffer parameters.207uint32_t pad[4]; // padding for later use208209uint8_t* private_memory; // Internally allocated memory (only when210// is_external_memory is 0). Should not be used211// externally, but accessed via the buffer union.212};213214// Internal, version-checked, entry point215WEBP_EXTERN int WebPInitDecBufferInternal(WebPDecBuffer*, int);216217// Initialize the structure as empty. Must be called before any other use.218// Returns false in case of version mismatch219static WEBP_INLINE int WebPInitDecBuffer(WebPDecBuffer* buffer) {220return WebPInitDecBufferInternal(buffer, WEBP_DECODER_ABI_VERSION);221}222223// Free any memory associated with the buffer. Must always be called last.224// Note: doesn't free the 'buffer' structure itself.225WEBP_EXTERN void WebPFreeDecBuffer(WebPDecBuffer* buffer);226227//------------------------------------------------------------------------------228// Enumeration of the status codes229230typedef enum VP8StatusCode {231VP8_STATUS_OK = 0,232VP8_STATUS_OUT_OF_MEMORY,233VP8_STATUS_INVALID_PARAM,234VP8_STATUS_BITSTREAM_ERROR,235VP8_STATUS_UNSUPPORTED_FEATURE,236VP8_STATUS_SUSPENDED,237VP8_STATUS_USER_ABORT,238VP8_STATUS_NOT_ENOUGH_DATA239} VP8StatusCode;240241//------------------------------------------------------------------------------242// Incremental decoding243//244// This API allows streamlined decoding of partial data.245// Picture can be incrementally decoded as data become available thanks to the246// WebPIDecoder object. This object can be left in a SUSPENDED state if the247// picture is only partially decoded, pending additional input.248// Code example:249//250// WebPInitDecBuffer(&output_buffer);251// output_buffer.colorspace = mode;252// ...253// WebPIDecoder* idec = WebPINewDecoder(&output_buffer);254// while (additional_data_is_available) {255// // ... (get additional data in some new_data[] buffer)256// status = WebPIAppend(idec, new_data, new_data_size);257// if (status != VP8_STATUS_OK && status != VP8_STATUS_SUSPENDED) {258// break; // an error occurred.259// }260//261// // The above call decodes the current available buffer.262// // Part of the image can now be refreshed by calling263// // WebPIDecGetRGB()/WebPIDecGetYUVA() etc.264// }265// WebPIDelete(idec);266267// Creates a new incremental decoder with the supplied buffer parameter.268// This output_buffer can be passed NULL, in which case a default output buffer269// is used (with MODE_RGB). Otherwise, an internal reference to 'output_buffer'270// is kept, which means that the lifespan of 'output_buffer' must be larger than271// that of the returned WebPIDecoder object.272// The supplied 'output_buffer' content MUST NOT be changed between calls to273// WebPIAppend() or WebPIUpdate() unless 'output_buffer.is_external_memory' is274// not set to 0. In such a case, it is allowed to modify the pointers, size and275// stride of output_buffer.u.RGBA or output_buffer.u.YUVA, provided they remain276// within valid bounds.277// All other fields of WebPDecBuffer MUST remain constant between calls.278// Returns NULL if the allocation failed.279WEBP_EXTERN WebPIDecoder* WebPINewDecoder(WebPDecBuffer* output_buffer);280281// This function allocates and initializes an incremental-decoder object, which282// will output the RGB/A samples specified by 'csp' into a preallocated283// buffer 'output_buffer'. The size of this buffer is at least284// 'output_buffer_size' and the stride (distance in bytes between two scanlines)285// is specified by 'output_stride'.286// Additionally, output_buffer can be passed NULL in which case the output287// buffer will be allocated automatically when the decoding starts. The288// colorspace 'csp' is taken into account for allocating this buffer. All other289// parameters are ignored.290// Returns NULL if the allocation failed, or if some parameters are invalid.291WEBP_EXTERN WebPIDecoder* WebPINewRGB(292WEBP_CSP_MODE csp,293uint8_t* output_buffer, size_t output_buffer_size, int output_stride);294295// This function allocates and initializes an incremental-decoder object, which296// will output the raw luma/chroma samples into a preallocated planes if297// supplied. The luma plane is specified by its pointer 'luma', its size298// 'luma_size' and its stride 'luma_stride'. Similarly, the chroma-u plane299// is specified by the 'u', 'u_size' and 'u_stride' parameters, and the chroma-v300// plane by 'v' and 'v_size'. And same for the alpha-plane. The 'a' pointer301// can be pass NULL in case one is not interested in the transparency plane.302// Conversely, 'luma' can be passed NULL if no preallocated planes are supplied.303// In this case, the output buffer will be automatically allocated (using304// MODE_YUVA) when decoding starts. All parameters are then ignored.305// Returns NULL if the allocation failed or if a parameter is invalid.306WEBP_EXTERN WebPIDecoder* WebPINewYUVA(307uint8_t* luma, size_t luma_size, int luma_stride,308uint8_t* u, size_t u_size, int u_stride,309uint8_t* v, size_t v_size, int v_stride,310uint8_t* a, size_t a_size, int a_stride);311312// Deprecated version of the above, without the alpha plane.313// Kept for backward compatibility.314WEBP_EXTERN WebPIDecoder* WebPINewYUV(315uint8_t* luma, size_t luma_size, int luma_stride,316uint8_t* u, size_t u_size, int u_stride,317uint8_t* v, size_t v_size, int v_stride);318319// Deletes the WebPIDecoder object and associated memory. Must always be called320// if WebPINewDecoder, WebPINewRGB or WebPINewYUV succeeded.321WEBP_EXTERN void WebPIDelete(WebPIDecoder* idec);322323// Copies and decodes the next available data. Returns VP8_STATUS_OK when324// the image is successfully decoded. Returns VP8_STATUS_SUSPENDED when more325// data is expected. Returns error in other cases.326WEBP_EXTERN VP8StatusCode WebPIAppend(327WebPIDecoder* idec, const uint8_t* data, size_t data_size);328329// A variant of the above function to be used when data buffer contains330// partial data from the beginning. In this case data buffer is not copied331// to the internal memory.332// Note that the value of the 'data' pointer can change between calls to333// WebPIUpdate, for instance when the data buffer is resized to fit larger data.334WEBP_EXTERN VP8StatusCode WebPIUpdate(335WebPIDecoder* idec, const uint8_t* data, size_t data_size);336337// Returns the RGB/A image decoded so far. Returns NULL if output params338// are not initialized yet. The RGB/A output type corresponds to the colorspace339// specified during call to WebPINewDecoder() or WebPINewRGB().340// *last_y is the index of last decoded row in raster scan order. Some pointers341// (*last_y, *width etc.) can be NULL if corresponding information is not342// needed. The values in these pointers are only valid on successful (non-NULL)343// return.344WEBP_EXTERN uint8_t* WebPIDecGetRGB(345const WebPIDecoder* idec, int* last_y,346int* width, int* height, int* stride);347348// Same as above function to get a YUVA image. Returns pointer to the luma349// plane or NULL in case of error. If there is no alpha information350// the alpha pointer '*a' will be returned NULL.351WEBP_EXTERN uint8_t* WebPIDecGetYUVA(352const WebPIDecoder* idec, int* last_y,353uint8_t** u, uint8_t** v, uint8_t** a,354int* width, int* height, int* stride, int* uv_stride, int* a_stride);355356// Deprecated alpha-less version of WebPIDecGetYUVA(): it will ignore the357// alpha information (if present). Kept for backward compatibility.358static WEBP_INLINE uint8_t* WebPIDecGetYUV(359const WebPIDecoder* idec, int* last_y, uint8_t** u, uint8_t** v,360int* width, int* height, int* stride, int* uv_stride) {361return WebPIDecGetYUVA(idec, last_y, u, v, NULL, width, height,362stride, uv_stride, NULL);363}364365// Generic call to retrieve information about the displayable area.366// If non NULL, the left/right/width/height pointers are filled with the visible367// rectangular area so far.368// Returns NULL in case the incremental decoder object is in an invalid state.369// Otherwise returns the pointer to the internal representation. This structure370// is read-only, tied to WebPIDecoder's lifespan and should not be modified.371WEBP_EXTERN const WebPDecBuffer* WebPIDecodedArea(372const WebPIDecoder* idec, int* left, int* top, int* width, int* height);373374//------------------------------------------------------------------------------375// Advanced decoding parametrization376//377// Code sample for using the advanced decoding API378/*379// A) Init a configuration object380WebPDecoderConfig config;381CHECK(WebPInitDecoderConfig(&config));382383// B) optional: retrieve the bitstream's features.384CHECK(WebPGetFeatures(data, data_size, &config.input) == VP8_STATUS_OK);385386// C) Adjust 'config', if needed387config.no_fancy_upsampling = 1;388config.output.colorspace = MODE_BGRA;389// etc.390391// Note that you can also make config.output point to an externally392// supplied memory buffer, provided it's big enough to store the decoded393// picture. Otherwise, config.output will just be used to allocate memory394// and store the decoded picture.395396// D) Decode!397CHECK(WebPDecode(data, data_size, &config) == VP8_STATUS_OK);398399// E) Decoded image is now in config.output (and config.output.u.RGBA)400401// F) Reclaim memory allocated in config's object. It's safe to call402// this function even if the memory is external and wasn't allocated403// by WebPDecode().404WebPFreeDecBuffer(&config.output);405*/406407// Features gathered from the bitstream408struct WebPBitstreamFeatures {409int width; // Width in pixels, as read from the bitstream.410int height; // Height in pixels, as read from the bitstream.411int has_alpha; // True if the bitstream contains an alpha channel.412int has_animation; // True if the bitstream is an animation.413int format; // 0 = undefined (/mixed), 1 = lossy, 2 = lossless414415uint32_t pad[5]; // padding for later use416};417418// Internal, version-checked, entry point419WEBP_EXTERN VP8StatusCode WebPGetFeaturesInternal(420const uint8_t*, size_t, WebPBitstreamFeatures*, int);421422// Retrieve features from the bitstream. The *features structure is filled423// with information gathered from the bitstream.424// Returns VP8_STATUS_OK when the features are successfully retrieved. Returns425// VP8_STATUS_NOT_ENOUGH_DATA when more data is needed to retrieve the426// features from headers. Returns error in other cases.427static WEBP_INLINE VP8StatusCode WebPGetFeatures(428const uint8_t* data, size_t data_size,429WebPBitstreamFeatures* features) {430return WebPGetFeaturesInternal(data, data_size, features,431WEBP_DECODER_ABI_VERSION);432}433434// Decoding options435struct WebPDecoderOptions {436int bypass_filtering; // if true, skip the in-loop filtering437int no_fancy_upsampling; // if true, use faster pointwise upsampler438int use_cropping; // if true, cropping is applied _first_439int crop_left, crop_top; // top-left position for cropping.440// Will be snapped to even values.441int crop_width, crop_height; // dimension of the cropping area442int use_scaling; // if true, scaling is applied _afterward_443int scaled_width, scaled_height; // final resolution444int use_threads; // if true, use multi-threaded decoding445int dithering_strength; // dithering strength (0=Off, 100=full)446int flip; // flip output vertically447int alpha_dithering_strength; // alpha dithering strength in [0..100]448449uint32_t pad[5]; // padding for later use450};451452// Main object storing the configuration for advanced decoding.453struct WebPDecoderConfig {454WebPBitstreamFeatures input; // Immutable bitstream features (optional)455WebPDecBuffer output; // Output buffer (can point to external mem)456WebPDecoderOptions options; // Decoding options457};458459// Internal, version-checked, entry point460WEBP_EXTERN int WebPInitDecoderConfigInternal(WebPDecoderConfig*, int);461462// Initialize the configuration as empty. This function must always be463// called first, unless WebPGetFeatures() is to be called.464// Returns false in case of mismatched version.465static WEBP_INLINE int WebPInitDecoderConfig(WebPDecoderConfig* config) {466return WebPInitDecoderConfigInternal(config, WEBP_DECODER_ABI_VERSION);467}468469// Instantiate a new incremental decoder object with the requested470// configuration. The bitstream can be passed using 'data' and 'data_size'471// parameter, in which case the features will be parsed and stored into472// config->input. Otherwise, 'data' can be NULL and no parsing will occur.473// Note that 'config' can be NULL too, in which case a default configuration474// is used. If 'config' is not NULL, it must outlive the WebPIDecoder object475// as some references to its fields will be used. No internal copy of 'config'476// is made.477// The return WebPIDecoder object must always be deleted calling WebPIDelete().478// Returns NULL in case of error (and config->status will then reflect479// the error condition, if available).480WEBP_EXTERN WebPIDecoder* WebPIDecode(const uint8_t* data, size_t data_size,481WebPDecoderConfig* config);482483// Non-incremental version. This version decodes the full data at once, taking484// 'config' into account. Returns decoding status (which should be VP8_STATUS_OK485// if the decoding was successful). Note that 'config' cannot be NULL.486WEBP_EXTERN VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,487WebPDecoderConfig* config);488489#ifdef __cplusplus490} // extern "C"491#endif492493#endif /* WEBP_WEBP_DECODE_H_ */494495496