1a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Copyright 2010 Google Inc. All Rights Reserved. 29aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 30406ce1417f76f2034833414dcecc9f56253640cVikas Arora// Use of this source code is governed by a BSD-style license 40406ce1417f76f2034833414dcecc9f56253640cVikas Arora// that can be found in the COPYING file in the root of the source 50406ce1417f76f2034833414dcecc9f56253640cVikas Arora// tree. An additional intellectual property rights grant can be found 60406ce1417f76f2034833414dcecc9f56253640cVikas Arora// in the file PATENTS. All contributing project authors may 70406ce1417f76f2034833414dcecc9f56253640cVikas Arora// be found in the AUTHORS file in the root of the source tree. 89aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// ----------------------------------------------------------------------------- 99aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 10a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Main decoding functions for WebP images. 119aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 129aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Author: Skal (pascal.massimino@gmail.com) 139aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 1403d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora#ifndef WEBP_WEBP_DECODE_H_ 1503d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora#define WEBP_WEBP_DECODE_H_ 169aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 17466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora#include "./types.h" 189aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 198b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora#ifdef __cplusplus 209aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldextern "C" { 219aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#endif 229aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 237c8da7ce66017295a65ec028084b90800be377f8James Zern#define WEBP_DECODER_ABI_VERSION 0x0208 // MAJOR(8b) + MINOR(8b) 24466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 250406ce1417f76f2034833414dcecc9f56253640cVikas Arora// Note: forward declaring enumerations is not allowed in (strict) C and C++, 260406ce1417f76f2034833414dcecc9f56253640cVikas Arora// the types are left here for reference. 270406ce1417f76f2034833414dcecc9f56253640cVikas Arora// typedef enum VP8StatusCode VP8StatusCode; 280406ce1417f76f2034833414dcecc9f56253640cVikas Arora// typedef enum WEBP_CSP_MODE WEBP_CSP_MODE; 291e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPRGBABuffer WebPRGBABuffer; 301e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPYUVABuffer WebPYUVABuffer; 311e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPDecBuffer WebPDecBuffer; 321e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPIDecoder WebPIDecoder; 331e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPBitstreamFeatures WebPBitstreamFeatures; 341e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPDecoderOptions WebPDecoderOptions; 351e7bf8805bd030c19924a5306837ecd72c295751Vikas Aroratypedef struct WebPDecoderConfig WebPDecoderConfig; 361e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora 3703d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// Return the decoder's version number, packed in hexadecimal using 8bits for 3803d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// each of major/minor/revision. E.g: v2.5.7 is 0x020507. 39466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(int) WebPGetDecoderVersion(void); 4003d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora 419aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Retrieve basic header information: width, height. 429aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// This function will also validate the header and return 0 in 439aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// case of formatting error. 44a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Pointers 'width' and 'height' can be passed NULL if deemed irrelevant. 45a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPGetInfo(const uint8_t* data, size_t data_size, 46466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* width, int* height); 479aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 48a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Decodes WebP images pointed to by 'data' and returns RGBA samples, along 49a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// with the dimensions in *width and *height. The ordering of samples in 50a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// memory is R, G, B, A, R, G, B, A... in scan order (endian-independent). 517c8da7ce66017295a65ec028084b90800be377f8James Zern// The returned pointer should be deleted calling WebPFree(). 529aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Returns NULL in case of error. 53a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(uint8_t*) WebPDecodeRGBA(const uint8_t* data, size_t data_size, 54a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int* width, int* height); 559aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 56a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Same as WebPDecodeRGBA, but returning A, R, G, B, A, R, G, B... ordered data. 57a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(uint8_t*) WebPDecodeARGB(const uint8_t* data, size_t data_size, 58466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* width, int* height); 59466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 60a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Same as WebPDecodeRGBA, but returning B, G, R, A, B, G, R, A... ordered data. 61a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(uint8_t*) WebPDecodeBGRA(const uint8_t* data, size_t data_size, 62466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* width, int* height); 639aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 64a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Same as WebPDecodeRGBA, but returning R, G, B, R, G, B... ordered data. 65a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// If the bitstream contains transparency, it is ignored. 66a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(uint8_t*) WebPDecodeRGB(const uint8_t* data, size_t data_size, 67466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* width, int* height); 689aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 69a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Same as WebPDecodeRGB, but returning B, G, R, B, G, R... ordered data. 70a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(uint8_t*) WebPDecodeBGR(const uint8_t* data, size_t data_size, 71a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int* width, int* height); 72a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 73a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 74a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Decode WebP images pointed to by 'data' to Y'UV format(*). The pointer 75a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// returned is the Y samples buffer. Upon return, *u and *v will point to 767c8da7ce66017295a65ec028084b90800be377f8James Zern// the U and V chroma data. These U and V buffers need NOT be passed to 777c8da7ce66017295a65ec028084b90800be377f8James Zern// WebPFree(), unlike the returned Y luma one. The dimension of the U and V 787c8da7ce66017295a65ec028084b90800be377f8James Zern// planes are both (*width + 1) / 2 and (*height + 1)/ 2. 799aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Upon return, the Y buffer has a stride returned as '*stride', while U and V 809aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// have a common stride returned as '*uv_stride'. 819aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Return NULL in case of error. 829aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// (*) Also named Y'CbCr. See: http://en.wikipedia.org/wiki/YCbCr 83a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(uint8_t*) WebPDecodeYUV(const uint8_t* data, size_t data_size, 84466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* width, int* height, 85466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora uint8_t** u, uint8_t** v, 86466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* stride, int* uv_stride); 879aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 887c8da7ce66017295a65ec028084b90800be377f8James Zern// Releases memory returned by the WebPDecode*() functions above. 897c8da7ce66017295a65ec028084b90800be377f8James ZernWEBP_EXTERN(void) WebPFree(void* ptr); 907c8da7ce66017295a65ec028084b90800be377f8James Zern 91466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// These five functions are variants of the above ones, that decode the image 929aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// directly into a pre-allocated buffer 'output_buffer'. The maximum storage 939aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// available in this buffer is indicated by 'output_buffer_size'. If this 949aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// storage is not sufficient (or an error occurred), NULL is returned. 959aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Otherwise, output_buffer is returned, for convenience. 969aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// The parameter 'output_stride' specifies the distance (in bytes) 979aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// between scanlines. Hence, output_buffer_size is expected to be at least 989aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// output_stride x picture-height. 99466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(uint8_t*) WebPDecodeRGBAInto( 100a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* data, size_t data_size, 101a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* output_buffer, size_t output_buffer_size, int output_stride); 102466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(uint8_t*) WebPDecodeARGBInto( 103a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* data, size_t data_size, 104a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* output_buffer, size_t output_buffer_size, int output_stride); 105466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(uint8_t*) WebPDecodeBGRAInto( 106a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* data, size_t data_size, 107a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* output_buffer, size_t output_buffer_size, int output_stride); 108a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 109a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// RGB and BGR variants. Here too the transparency information, if present, 110a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// will be dropped and ignored. 111a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(uint8_t*) WebPDecodeRGBInto( 112a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* data, size_t data_size, 113a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* output_buffer, size_t output_buffer_size, int output_stride); 114a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(uint8_t*) WebPDecodeBGRInto( 115a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* data, size_t data_size, 116a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* output_buffer, size_t output_buffer_size, int output_stride); 1179aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 1189aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// WebPDecodeYUVInto() is a variant of WebPDecodeYUV() that operates directly 1199aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// into pre-allocated luma/chroma plane buffers. This function requires the 1209aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// strides to be passed: one for the luma plane and one for each of the 1219aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// chroma ones. The size of each plane buffer is passed as 'luma_size', 1229aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 'u_size' and 'v_size' respectively. 1239aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Pointer to the luma plane ('*luma') is returned or NULL if an error occurred 1249aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// during decoding (or because some buffers were found to be too small). 125466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(uint8_t*) WebPDecodeYUVInto( 126a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* data, size_t data_size, 127a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* luma, size_t luma_size, int luma_stride, 128a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* u, size_t u_size, int u_stride, 129a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* v, size_t v_size, int v_stride); 1309aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 131a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 132466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Output colorspaces and buffer 1339aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 134466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Colorspaces 135a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note: the naming describes the byte-ordering of packed samples in memory. 136a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// For instance, MODE_BGRA relates to samples ordered as B,G,R,A,B,G,R,A,... 137a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Non-capital names (e.g.:MODE_Argb) relates to pre-multiplied RGB channels. 1381e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// RGBA-4444 and RGB-565 colorspaces are represented by following byte-order: 1391e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// RGBA-4444: [r3 r2 r1 r0 g3 g2 g1 g0], [b3 b2 b1 b0 a3 a2 a1 a0], ... 1401e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// RGB-565: [r4 r3 r2 r1 r0 g5 g4 g3], [g2 g1 g0 b4 b3 b2 b1 b0], ... 1411e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// In the case WEBP_SWAP_16BITS_CSP is defined, the bytes are swapped for 1421e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// these two modes: 1431e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// RGBA-4444: [b3 b2 b1 b0 a3 a2 a1 a0], [r3 r2 r1 r0 g3 g2 g1 g0], ... 1441e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// RGB-565: [g2 g1 g0 b4 b3 b2 b1 b0], [r4 r3 r2 r1 r0 g5 g4 g3], ... 1451e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora 1460406ce1417f76f2034833414dcecc9f56253640cVikas Aroratypedef enum WEBP_CSP_MODE { 1471e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora MODE_RGB = 0, MODE_RGBA = 1, 1481e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora MODE_BGR = 2, MODE_BGRA = 3, 1491e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora MODE_ARGB = 4, MODE_RGBA_4444 = 5, 1501e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora MODE_RGB_565 = 6, 1511e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora // RGB-premultiplied transparent modes (alpha value is preserved) 1521e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora MODE_rgbA = 7, 1531e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora MODE_bgrA = 8, 1541e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora MODE_Argb = 9, 1551e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora MODE_rgbA_4444 = 10, 1561e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora // YUV modes must come after RGB ones. 1571e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora MODE_YUV = 11, MODE_YUVA = 12, // yuv 4:2:0 1581e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora MODE_LAST = 13 1590406ce1417f76f2034833414dcecc9f56253640cVikas Arora} WEBP_CSP_MODE; 160466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 161a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Some useful macros: 162a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE int WebPIsPremultipliedMode(WEBP_CSP_MODE mode) { 163a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return (mode == MODE_rgbA || mode == MODE_bgrA || mode == MODE_Argb || 164a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora mode == MODE_rgbA_4444); 165a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} 166a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 167a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE int WebPIsAlphaMode(WEBP_CSP_MODE mode) { 168a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return (mode == MODE_RGBA || mode == MODE_BGRA || mode == MODE_ARGB || 169a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora mode == MODE_RGBA_4444 || mode == MODE_YUVA || 170a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPIsPremultipliedMode(mode)); 171a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} 172a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 173a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE int WebPIsRGBMode(WEBP_CSP_MODE mode) { 174a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return (mode < MODE_YUV); 175a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} 176a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 177a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 178a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// WebPDecBuffer: Generic structure for describing the output sample buffer. 179a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 1801e7bf8805bd030c19924a5306837ecd72c295751Vikas Arorastruct WebPRGBABuffer { // view as RGBA 181466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora uint8_t* rgba; // pointer to RGBA samples 182466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int stride; // stride in bytes from one scanline to the next. 183a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t size; // total size of the *rgba buffer. 1841e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora}; 185466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 1861e7bf8805bd030c19924a5306837ecd72c295751Vikas Arorastruct WebPYUVABuffer { // view as YUVA 187466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora uint8_t* y, *u, *v, *a; // pointer to luma, chroma U/V, alpha samples 188466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int y_stride; // luma stride 189466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int u_stride, v_stride; // chroma strides 190466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int a_stride; // alpha stride 191a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t y_size; // luma plane size 192a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t u_size, v_size; // chroma planes size 193a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t a_size; // alpha-plane size 1941e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora}; 195466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 196466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Output buffer 1971e7bf8805bd030c19924a5306837ecd72c295751Vikas Arorastruct WebPDecBuffer { 198466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WEBP_CSP_MODE colorspace; // Colorspace. 199466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int width, height; // Dimensions. 20088fe2b83c4b9232cd08729556fd0485d6a6a92cdVikas Arora int is_external_memory; // If true, 'internal_memory' pointer is not used. 201466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora union { 202466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPRGBABuffer RGBA; 203466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPYUVABuffer YUVA; 20488fe2b83c4b9232cd08729556fd0485d6a6a92cdVikas Arora } u; // Nameless union of buffer parameters. 205a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t pad[4]; // padding for later use 206a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 20788fe2b83c4b9232cd08729556fd0485d6a6a92cdVikas Arora uint8_t* private_memory; // Internally allocated memory (only when 20888fe2b83c4b9232cd08729556fd0485d6a6a92cdVikas Arora // is_external_memory is false). Should not be used 20988fe2b83c4b9232cd08729556fd0485d6a6a92cdVikas Arora // externally, but accessed via the buffer union. 2101e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora}; 211466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 212466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Internal, version-checked, entry point 213a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPInitDecBufferInternal(WebPDecBuffer*, int); 214466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 215466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Initialize the structure as empty. Must be called before any other use. 216466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Returns false in case of version mismatch 217a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE int WebPInitDecBuffer(WebPDecBuffer* buffer) { 218466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return WebPInitDecBufferInternal(buffer, WEBP_DECODER_ABI_VERSION); 219466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora} 220466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 221466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Free any memory associated with the buffer. Must always be called last. 222466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Note: doesn't free the 'buffer' structure itself. 223a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(void) WebPFreeDecBuffer(WebPDecBuffer* buffer); 22403d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora 225a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 22603d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// Enumeration of the status codes 227466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 2280406ce1417f76f2034833414dcecc9f56253640cVikas Aroratypedef enum VP8StatusCode { 22903d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora VP8_STATUS_OK = 0, 23003d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora VP8_STATUS_OUT_OF_MEMORY, 23103d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora VP8_STATUS_INVALID_PARAM, 23203d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora VP8_STATUS_BITSTREAM_ERROR, 23303d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora VP8_STATUS_UNSUPPORTED_FEATURE, 23403d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora VP8_STATUS_SUSPENDED, 23503d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora VP8_STATUS_USER_ABORT, 23603d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora VP8_STATUS_NOT_ENOUGH_DATA 2370406ce1417f76f2034833414dcecc9f56253640cVikas Arora} VP8StatusCode; 23803d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora 239a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 24003d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// Incremental decoding 24103d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// 242466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// This API allows streamlined decoding of partial data. 243466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Picture can be incrementally decoded as data become available thanks to the 24403d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// WebPIDecoder object. This object can be left in a SUSPENDED state if the 24503d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// picture is only partially decoded, pending additional input. 24603d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// Code example: 24703d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// 248a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// WebPInitDecBuffer(&buffer); 249a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// buffer.colorspace = mode; 250a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// ... 251a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// WebPIDecoder* idec = WebPINewDecoder(&buffer); 25203d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// while (has_more_data) { 25303d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// // ... (get additional data) 25403d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// status = WebPIAppend(idec, new_data, new_data_size); 25503d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// if (status != VP8_STATUS_SUSPENDED || 25603d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// break; 25703d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// } 25803d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// 25903d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// // The above call decodes the current available buffer. 26003d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// // Part of the image can now be refreshed by calling to 261a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// // WebPIDecGetRGB()/WebPIDecGetYUVA() etc. 26203d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// } 26303d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// WebPIDelete(idec); 26403d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora 265466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Creates a new incremental decoder with the supplied buffer parameter. 266466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// This output_buffer can be passed NULL, in which case a default output buffer 267466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// is used (with MODE_RGB). Otherwise, an internal reference to 'output_buffer' 268466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// is kept, which means that the lifespan of 'output_buffer' must be larger than 269466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// that of the returned WebPIDecoder object. 2700406ce1417f76f2034833414dcecc9f56253640cVikas Arora// The supplied 'output_buffer' content MUST NOT be changed between calls to 2710406ce1417f76f2034833414dcecc9f56253640cVikas Arora// WebPIAppend() or WebPIUpdate() unless 'output_buffer.is_external_memory' is 2720406ce1417f76f2034833414dcecc9f56253640cVikas Arora// set to 1. In such a case, it is allowed to modify the pointers, size and 2730406ce1417f76f2034833414dcecc9f56253640cVikas Arora// stride of output_buffer.u.RGBA or output_buffer.u.YUVA, provided they remain 2740406ce1417f76f2034833414dcecc9f56253640cVikas Arora// within valid bounds. 2750406ce1417f76f2034833414dcecc9f56253640cVikas Arora// All other fields of WebPDecBuffer MUST remain constant between calls. 276466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Returns NULL if the allocation failed. 277a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(WebPIDecoder*) WebPINewDecoder(WebPDecBuffer* output_buffer); 27803d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora 27903d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// This function allocates and initializes an incremental-decoder object, which 280a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// will output the RGB/A samples specified by 'csp' into a preallocated 28103d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// buffer 'output_buffer'. The size of this buffer is at least 28203d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// 'output_buffer_size' and the stride (distance in bytes between two scanlines) 2831e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// is specified by 'output_stride'. 2841e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// Additionally, output_buffer can be passed NULL in which case the output 2851e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// buffer will be allocated automatically when the decoding starts. The 2861e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// colorspace 'csp' is taken into account for allocating this buffer. All other 2871e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// parameters are ignored. 2881e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// Returns NULL if the allocation failed, or if some parameters are invalid. 289466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(WebPIDecoder*) WebPINewRGB( 290a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WEBP_CSP_MODE csp, 291a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* output_buffer, size_t output_buffer_size, int output_stride); 29203d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora 29303d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// This function allocates and initializes an incremental-decoder object, which 2941e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// will output the raw luma/chroma samples into a preallocated planes if 2951e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// supplied. The luma plane is specified by its pointer 'luma', its size 2961e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// 'luma_size' and its stride 'luma_stride'. Similarly, the chroma-u plane 2971e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// is specified by the 'u', 'u_size' and 'u_stride' parameters, and the chroma-v 2981e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// plane by 'v' and 'v_size'. And same for the alpha-plane. The 'a' pointer 2991e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// can be pass NULL in case one is not interested in the transparency plane. 3001e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// Conversely, 'luma' can be passed NULL if no preallocated planes are supplied. 3011e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// In this case, the output buffer will be automatically allocated (using 3021e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// MODE_YUVA) when decoding starts. All parameters are then ignored. 3031e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// Returns NULL if the allocation failed or if a parameter is invalid. 304a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(WebPIDecoder*) WebPINewYUVA( 305a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* luma, size_t luma_size, int luma_stride, 306a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* u, size_t u_size, int u_stride, 307a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* v, size_t v_size, int v_stride, 308a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* a, size_t a_size, int a_stride); 309a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 310a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Deprecated version of the above, without the alpha plane. 311a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Kept for backward compatibility. 312466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(WebPIDecoder*) WebPINewYUV( 313a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* luma, size_t luma_size, int luma_stride, 314a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* u, size_t u_size, int u_stride, 315a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* v, size_t v_size, int v_stride); 31603d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora 31788fe2b83c4b9232cd08729556fd0485d6a6a92cdVikas Arora// Deletes the WebPIDecoder object and associated memory. Must always be called 318a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// if WebPINewDecoder, WebPINewRGB or WebPINewYUV succeeded. 319a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(void) WebPIDelete(WebPIDecoder* idec); 32003d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora 32103d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// Copies and decodes the next available data. Returns VP8_STATUS_OK when 32203d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// the image is successfully decoded. Returns VP8_STATUS_SUSPENDED when more 32303d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// data is expected. Returns error in other cases. 324466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(VP8StatusCode) WebPIAppend( 325a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPIDecoder* idec, const uint8_t* data, size_t data_size); 32603d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora 32703d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// A variant of the above function to be used when data buffer contains 32803d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// partial data from the beginning. In this case data buffer is not copied 32903d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// to the internal memory. 33003d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// Note that the value of the 'data' pointer can change between calls to 33103d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora// WebPIUpdate, for instance when the data buffer is resized to fit larger data. 332466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(VP8StatusCode) WebPIUpdate( 333a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPIDecoder* idec, const uint8_t* data, size_t data_size); 334a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 335a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns the RGB/A image decoded so far. Returns NULL if output params 336a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// are not initialized yet. The RGB/A output type corresponds to the colorspace 337a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// specified during call to WebPINewDecoder() or WebPINewRGB(). 338a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// *last_y is the index of last decoded row in raster scan order. Some pointers 339a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// (*last_y, *width etc.) can be NULL if corresponding information is not 340a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// needed. 341466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(uint8_t*) WebPIDecGetRGB( 342a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const WebPIDecoder* idec, int* last_y, 343466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* width, int* height, int* stride); 34403d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora 345a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Same as above function to get a YUVA image. Returns pointer to the luma 346a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// plane or NULL in case of error. If there is no alpha information 347a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// the alpha pointer '*a' will be returned NULL. 348a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(uint8_t*) WebPIDecGetYUVA( 349a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const WebPIDecoder* idec, int* last_y, 350a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t** u, uint8_t** v, uint8_t** a, 351a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int* width, int* height, int* stride, int* uv_stride, int* a_stride); 352a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 353a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Deprecated alpha-less version of WebPIDecGetYUVA(): it will ignore the 354a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// alpha information (if present). Kept for backward compatibility. 355a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE uint8_t* WebPIDecGetYUV( 356a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const WebPIDecoder* idec, int* last_y, uint8_t** u, uint8_t** v, 357a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int* width, int* height, int* stride, int* uv_stride) { 358a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return WebPIDecGetYUVA(idec, last_y, u, v, NULL, width, height, 359a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora stride, uv_stride, NULL); 360a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} 361466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 362466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Generic call to retrieve information about the displayable area. 363466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// If non NULL, the left/right/width/height pointers are filled with the visible 364466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// rectangular area so far. 365466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Returns NULL in case the incremental decoder object is in an invalid state. 366466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Otherwise returns the pointer to the internal representation. This structure 367466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// is read-only, tied to WebPIDecoder's lifespan and should not be modified. 368466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(const WebPDecBuffer*) WebPIDecodedArea( 369a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const WebPIDecoder* idec, int* left, int* top, int* width, int* height); 370466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 371a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 372466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Advanced decoding parametrization 373466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// 374466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Code sample for using the advanced decoding API 375466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora/* 376466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // A) Init a configuration object 377466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPDecoderConfig config; 378466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora CHECK(WebPInitDecoderConfig(&config)); 379466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 380466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // B) optional: retrieve the bitstream's features. 381466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora CHECK(WebPGetFeatures(data, data_size, &config.input) == VP8_STATUS_OK); 382466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 383466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // C) Adjust 'config', if needed 3841e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora config.no_fancy_upsampling = 1; 385466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora config.output.colorspace = MODE_BGRA; 386466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // etc. 387466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 388466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // Note that you can also make config.output point to an externally 389466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // supplied memory buffer, provided it's big enough to store the decoded 390466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // picture. Otherwise, config.output will just be used to allocate memory 391466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // and store the decoded picture. 392466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 393466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // D) Decode! 394466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora CHECK(WebPDecode(data, data_size, &config) == VP8_STATUS_OK); 395466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 396466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // E) Decoded image is now in config.output (and config.output.u.RGBA) 397466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 398466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // F) Reclaim memory allocated in config's object. It's safe to call 399466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // this function even if the memory is external and wasn't allocated 400466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // by WebPDecode(). 401466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPFreeDecBuffer(&config.output); 402466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora*/ 403466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 404466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Features gathered from the bitstream 4051e7bf8805bd030c19924a5306837ecd72c295751Vikas Arorastruct WebPBitstreamFeatures { 4060406ce1417f76f2034833414dcecc9f56253640cVikas Arora int width; // Width in pixels, as read from the bitstream. 4070406ce1417f76f2034833414dcecc9f56253640cVikas Arora int height; // Height in pixels, as read from the bitstream. 4080406ce1417f76f2034833414dcecc9f56253640cVikas Arora int has_alpha; // True if the bitstream contains an alpha channel. 4090406ce1417f76f2034833414dcecc9f56253640cVikas Arora int has_animation; // True if the bitstream is an animation. 4108b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora int format; // 0 = undefined (/mixed), 1 = lossy, 2 = lossless 411a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 4127c8da7ce66017295a65ec028084b90800be377f8James Zern uint32_t pad[5]; // padding for later use 4131e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora}; 414466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 415466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Internal, version-checked, entry point 416466727975bcc57c0c5597bcd0747a2fe4777b303Vikas AroraWEBP_EXTERN(VP8StatusCode) WebPGetFeaturesInternal( 417a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t*, size_t, WebPBitstreamFeatures*, int); 418466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 419466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Retrieve features from the bitstream. The *features structure is filled 42088fe2b83c4b9232cd08729556fd0485d6a6a92cdVikas Arora// with information gathered from the bitstream. 4211e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// Returns VP8_STATUS_OK when the features are successfully retrieved. Returns 4221e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// VP8_STATUS_NOT_ENOUGH_DATA when more data is needed to retrieve the 4231e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// features from headers. Returns error in other cases. 424a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE VP8StatusCode WebPGetFeatures( 425a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* data, size_t data_size, 426a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPBitstreamFeatures* features) { 427466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return WebPGetFeaturesInternal(data, data_size, features, 428466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WEBP_DECODER_ABI_VERSION); 429466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora} 430466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 431466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Decoding options 4321e7bf8805bd030c19924a5306837ecd72c295751Vikas Arorastruct WebPDecoderOptions { 433466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int bypass_filtering; // if true, skip the in-loop filtering 434466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int no_fancy_upsampling; // if true, use faster pointwise upsampler 435466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int use_cropping; // if true, cropping is applied _first_ 436466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int crop_left, crop_top; // top-left position for cropping. 437466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // Will be snapped to even values. 438466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int crop_width, crop_height; // dimension of the cropping area 439466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int use_scaling; // if true, scaling is applied _afterward_ 440466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int scaled_width, scaled_height; // final resolution 441a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int use_threads; // if true, use multi-threaded decoding 4428b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora int dithering_strength; // dithering strength (0=Off, 100=full) 44333f74dabbc7920a65ed435d7417987589febdc16Vikas Arora int flip; // flip output vertically 44433f74dabbc7920a65ed435d7417987589febdc16Vikas Arora int alpha_dithering_strength; // alpha dithering strength in [0..100] 445a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 4468b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora uint32_t pad[5]; // padding for later use 4471e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora}; 448466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 449466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Main object storing the configuration for advanced decoding. 4501e7bf8805bd030c19924a5306837ecd72c295751Vikas Arorastruct WebPDecoderConfig { 451466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPBitstreamFeatures input; // Immutable bitstream features (optional) 452466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPDecBuffer output; // Output buffer (can point to external mem) 453466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPDecoderOptions options; // Decoding options 4541e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora}; 455466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 456466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Internal, version-checked, entry point 457a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) WebPInitDecoderConfigInternal(WebPDecoderConfig*, int); 458466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 459466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Initialize the configuration as empty. This function must always be 460466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// called first, unless WebPGetFeatures() is to be called. 461466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Returns false in case of mismatched version. 462a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE int WebPInitDecoderConfig(WebPDecoderConfig* config) { 463466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return WebPInitDecoderConfigInternal(config, WEBP_DECODER_ABI_VERSION); 464466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora} 465466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 466a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Instantiate a new incremental decoder object with the requested 467a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// configuration. The bitstream can be passed using 'data' and 'data_size' 468a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// parameter, in which case the features will be parsed and stored into 469a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// config->input. Otherwise, 'data' can be NULL and no parsing will occur. 470a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note that 'config' can be NULL too, in which case a default configuration 471a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// is used. 472466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// The return WebPIDecoder object must always be deleted calling WebPIDelete(). 473466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Returns NULL in case of error (and config->status will then reflect 474466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// the error condition). 475a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(WebPIDecoder*) WebPIDecode(const uint8_t* data, size_t data_size, 476a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPDecoderConfig* config); 477466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 478466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Non-incremental version. This version decodes the full data at once, taking 479a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 'config' into account. Returns decoding status (which should be VP8_STATUS_OK 480a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// if the decoding was successful). 481a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(VP8StatusCode) WebPDecode(const uint8_t* data, size_t data_size, 482a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPDecoderConfig* config); 48303d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora 4848b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora#ifdef __cplusplus 4859aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} // extern "C" 4869aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#endif 4879aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 48803d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora#endif /* WEBP_WEBP_DECODE_H_ */ 489