decode.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Main decoding functions for WebP images.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #ifndef WEBP_WEBP_DECODE_H_
  14. #define WEBP_WEBP_DECODE_H_
  15. #include "./types.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #define WEBP_DECODER_ABI_VERSION 0x0208 // MAJOR(8b) + MINOR(8b)
  20. // Note: forward declaring enumerations is not allowed in (strict) C and C++,
  21. // the types are left here for reference.
  22. // typedef enum VP8StatusCode VP8StatusCode;
  23. // typedef enum WEBP_CSP_MODE WEBP_CSP_MODE;
  24. typedef struct WebPRGBABuffer WebPRGBABuffer;
  25. typedef struct WebPYUVABuffer WebPYUVABuffer;
  26. typedef struct WebPDecBuffer WebPDecBuffer;
  27. typedef struct WebPIDecoder WebPIDecoder;
  28. typedef struct WebPBitstreamFeatures WebPBitstreamFeatures;
  29. typedef struct WebPDecoderOptions WebPDecoderOptions;
  30. typedef struct WebPDecoderConfig WebPDecoderConfig;
  31. // Return the decoder's version number, packed in hexadecimal using 8bits for
  32. // each of major/minor/revision. E.g: v2.5.7 is 0x020507.
  33. WEBP_EXTERN int WebPGetDecoderVersion(void);
  34. // Retrieve basic header information: width, height.
  35. // This function will also validate the header, returning true on success,
  36. // false otherwise. '*width' and '*height' are only valid on successful return.
  37. // Pointers 'width' and 'height' can be passed NULL if deemed irrelevant.
  38. // Note: The following chunk sequences (before the raw VP8/VP8L data) are
  39. // considered valid by this function:
  40. // RIFF + VP8(L)
  41. // RIFF + VP8X + (optional chunks) + VP8(L)
  42. // ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.
  43. // VP8(L) <-- Not a valid WebP format: only allowed for internal purpose.
  44. WEBP_EXTERN int WebPGetInfo(const uint8_t* data, size_t data_size,
  45. int* width, int* height);
  46. // Decodes WebP images pointed to by 'data' and returns RGBA samples, along
  47. // with the dimensions in *width and *height. The ordering of samples in
  48. // memory is R, G, B, A, R, G, B, A... in scan order (endian-independent).
  49. // The returned pointer should be deleted calling WebPFree().
  50. // Returns NULL in case of error.
  51. WEBP_EXTERN uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size,
  52. int* width, int* height);
  53. // Same as WebPDecodeRGBA, but returning A, R, G, B, A, R, G, B... ordered data.
  54. WEBP_EXTERN uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size,
  55. int* width, int* height);
  56. // Same as WebPDecodeRGBA, but returning B, G, R, A, B, G, R, A... ordered data.
  57. WEBP_EXTERN uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size,
  58. int* width, int* height);
  59. // Same as WebPDecodeRGBA, but returning R, G, B, R, G, B... ordered data.
  60. // If the bitstream contains transparency, it is ignored.
  61. WEBP_EXTERN uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size,
  62. int* width, int* height);
  63. // Same as WebPDecodeRGB, but returning B, G, R, B, G, R... ordered data.
  64. WEBP_EXTERN uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size,
  65. int* width, int* height);
  66. // Decode WebP images pointed to by 'data' to Y'UV format(*). The pointer
  67. // returned is the Y samples buffer. Upon return, *u and *v will point to
  68. // the U and V chroma data. These U and V buffers need NOT be passed to
  69. // WebPFree(), unlike the returned Y luma one. The dimension of the U and V
  70. // planes are both (*width + 1) / 2 and (*height + 1)/ 2.
  71. // Upon return, the Y buffer has a stride returned as '*stride', while U and V
  72. // have a common stride returned as '*uv_stride'.
  73. // Return NULL in case of error.
  74. // (*) Also named Y'CbCr. See: http://en.wikipedia.org/wiki/YCbCr
  75. WEBP_EXTERN uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size,
  76. int* width, int* height,
  77. uint8_t** u, uint8_t** v,
  78. int* stride, int* uv_stride);
  79. // Releases memory returned by the WebPDecode*() functions above.
  80. WEBP_EXTERN void WebPFree(void* ptr);
  81. // These five functions are variants of the above ones, that decode the image
  82. // directly into a pre-allocated buffer 'output_buffer'. The maximum storage
  83. // available in this buffer is indicated by 'output_buffer_size'. If this
  84. // storage is not sufficient (or an error occurred), NULL is returned.
  85. // Otherwise, output_buffer is returned, for convenience.
  86. // The parameter 'output_stride' specifies the distance (in bytes)
  87. // between scanlines. Hence, output_buffer_size is expected to be at least
  88. // output_stride x picture-height.
  89. WEBP_EXTERN uint8_t* WebPDecodeRGBAInto(
  90. const uint8_t* data, size_t data_size,
  91. uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
  92. WEBP_EXTERN uint8_t* WebPDecodeARGBInto(
  93. const uint8_t* data, size_t data_size,
  94. uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
  95. WEBP_EXTERN uint8_t* WebPDecodeBGRAInto(
  96. const uint8_t* data, size_t data_size,
  97. uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
  98. // RGB and BGR variants. Here too the transparency information, if present,
  99. // will be dropped and ignored.
  100. WEBP_EXTERN uint8_t* WebPDecodeRGBInto(
  101. const uint8_t* data, size_t data_size,
  102. uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
  103. WEBP_EXTERN uint8_t* WebPDecodeBGRInto(
  104. const uint8_t* data, size_t data_size,
  105. uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
  106. // WebPDecodeYUVInto() is a variant of WebPDecodeYUV() that operates directly
  107. // into pre-allocated luma/chroma plane buffers. This function requires the
  108. // strides to be passed: one for the luma plane and one for each of the
  109. // chroma ones. The size of each plane buffer is passed as 'luma_size',
  110. // 'u_size' and 'v_size' respectively.
  111. // Pointer to the luma plane ('*luma') is returned or NULL if an error occurred
  112. // during decoding (or because some buffers were found to be too small).
  113. WEBP_EXTERN uint8_t* WebPDecodeYUVInto(
  114. const uint8_t* data, size_t data_size,
  115. uint8_t* luma, size_t luma_size, int luma_stride,
  116. uint8_t* u, size_t u_size, int u_stride,
  117. uint8_t* v, size_t v_size, int v_stride);
  118. //------------------------------------------------------------------------------
  119. // Output colorspaces and buffer
  120. // Colorspaces
  121. // Note: the naming describes the byte-ordering of packed samples in memory.
  122. // For instance, MODE_BGRA relates to samples ordered as B,G,R,A,B,G,R,A,...
  123. // Non-capital names (e.g.:MODE_Argb) relates to pre-multiplied RGB channels.
  124. // RGBA-4444 and RGB-565 colorspaces are represented by following byte-order:
  125. // RGBA-4444: [r3 r2 r1 r0 g3 g2 g1 g0], [b3 b2 b1 b0 a3 a2 a1 a0], ...
  126. // RGB-565: [r4 r3 r2 r1 r0 g5 g4 g3], [g2 g1 g0 b4 b3 b2 b1 b0], ...
  127. // In the case WEBP_SWAP_16BITS_CSP is defined, the bytes are swapped for
  128. // these two modes:
  129. // RGBA-4444: [b3 b2 b1 b0 a3 a2 a1 a0], [r3 r2 r1 r0 g3 g2 g1 g0], ...
  130. // RGB-565: [g2 g1 g0 b4 b3 b2 b1 b0], [r4 r3 r2 r1 r0 g5 g4 g3], ...
  131. typedef enum WEBP_CSP_MODE {
  132. MODE_RGB = 0, MODE_RGBA = 1,
  133. MODE_BGR = 2, MODE_BGRA = 3,
  134. MODE_ARGB = 4, MODE_RGBA_4444 = 5,
  135. MODE_RGB_565 = 6,
  136. // RGB-premultiplied transparent modes (alpha value is preserved)
  137. MODE_rgbA = 7,
  138. MODE_bgrA = 8,
  139. MODE_Argb = 9,
  140. MODE_rgbA_4444 = 10,
  141. // YUV modes must come after RGB ones.
  142. MODE_YUV = 11, MODE_YUVA = 12, // yuv 4:2:0
  143. MODE_LAST = 13
  144. } WEBP_CSP_MODE;
  145. // Some useful macros:
  146. static WEBP_INLINE int WebPIsPremultipliedMode(WEBP_CSP_MODE mode) {
  147. return (mode == MODE_rgbA || mode == MODE_bgrA || mode == MODE_Argb ||
  148. mode == MODE_rgbA_4444);
  149. }
  150. static WEBP_INLINE int WebPIsAlphaMode(WEBP_CSP_MODE mode) {
  151. return (mode == MODE_RGBA || mode == MODE_BGRA || mode == MODE_ARGB ||
  152. mode == MODE_RGBA_4444 || mode == MODE_YUVA ||
  153. WebPIsPremultipliedMode(mode));
  154. }
  155. static WEBP_INLINE int WebPIsRGBMode(WEBP_CSP_MODE mode) {
  156. return (mode < MODE_YUV);
  157. }
  158. //------------------------------------------------------------------------------
  159. // WebPDecBuffer: Generic structure for describing the output sample buffer.
  160. struct WebPRGBABuffer { // view as RGBA
  161. uint8_t* rgba; // pointer to RGBA samples
  162. int stride; // stride in bytes from one scanline to the next.
  163. size_t size; // total size of the *rgba buffer.
  164. };
  165. struct WebPYUVABuffer { // view as YUVA
  166. uint8_t* y, *u, *v, *a; // pointer to luma, chroma U/V, alpha samples
  167. int y_stride; // luma stride
  168. int u_stride, v_stride; // chroma strides
  169. int a_stride; // alpha stride
  170. size_t y_size; // luma plane size
  171. size_t u_size, v_size; // chroma planes size
  172. size_t a_size; // alpha-plane size
  173. };
  174. // Output buffer
  175. struct WebPDecBuffer {
  176. WEBP_CSP_MODE colorspace; // Colorspace.
  177. int width, height; // Dimensions.
  178. int is_external_memory; // If non-zero, 'internal_memory' pointer is not
  179. // used. If value is '2' or more, the external
  180. // memory is considered 'slow' and multiple
  181. // read/write will be avoided.
  182. union {
  183. WebPRGBABuffer RGBA;
  184. WebPYUVABuffer YUVA;
  185. } u; // Nameless union of buffer parameters.
  186. uint32_t pad[4]; // padding for later use
  187. uint8_t* private_memory; // Internally allocated memory (only when
  188. // is_external_memory is 0). Should not be used
  189. // externally, but accessed via the buffer union.
  190. };
  191. // Internal, version-checked, entry point
  192. WEBP_EXTERN int WebPInitDecBufferInternal(WebPDecBuffer*, int);
  193. // Initialize the structure as empty. Must be called before any other use.
  194. // Returns false in case of version mismatch
  195. static WEBP_INLINE int WebPInitDecBuffer(WebPDecBuffer* buffer) {
  196. return WebPInitDecBufferInternal(buffer, WEBP_DECODER_ABI_VERSION);
  197. }
  198. // Free any memory associated with the buffer. Must always be called last.
  199. // Note: doesn't free the 'buffer' structure itself.
  200. WEBP_EXTERN void WebPFreeDecBuffer(WebPDecBuffer* buffer);
  201. //------------------------------------------------------------------------------
  202. // Enumeration of the status codes
  203. typedef enum VP8StatusCode {
  204. VP8_STATUS_OK = 0,
  205. VP8_STATUS_OUT_OF_MEMORY,
  206. VP8_STATUS_INVALID_PARAM,
  207. VP8_STATUS_BITSTREAM_ERROR,
  208. VP8_STATUS_UNSUPPORTED_FEATURE,
  209. VP8_STATUS_SUSPENDED,
  210. VP8_STATUS_USER_ABORT,
  211. VP8_STATUS_NOT_ENOUGH_DATA
  212. } VP8StatusCode;
  213. //------------------------------------------------------------------------------
  214. // Incremental decoding
  215. //
  216. // This API allows streamlined decoding of partial data.
  217. // Picture can be incrementally decoded as data become available thanks to the
  218. // WebPIDecoder object. This object can be left in a SUSPENDED state if the
  219. // picture is only partially decoded, pending additional input.
  220. // Code example:
  221. //
  222. // WebPInitDecBuffer(&output_buffer);
  223. // output_buffer.colorspace = mode;
  224. // ...
  225. // WebPIDecoder* idec = WebPINewDecoder(&output_buffer);
  226. // while (additional_data_is_available) {
  227. // // ... (get additional data in some new_data[] buffer)
  228. // status = WebPIAppend(idec, new_data, new_data_size);
  229. // if (status != VP8_STATUS_OK && status != VP8_STATUS_SUSPENDED) {
  230. // break; // an error occurred.
  231. // }
  232. //
  233. // // The above call decodes the current available buffer.
  234. // // Part of the image can now be refreshed by calling
  235. // // WebPIDecGetRGB()/WebPIDecGetYUVA() etc.
  236. // }
  237. // WebPIDelete(idec);
  238. // Creates a new incremental decoder with the supplied buffer parameter.
  239. // This output_buffer can be passed NULL, in which case a default output buffer
  240. // is used (with MODE_RGB). Otherwise, an internal reference to 'output_buffer'
  241. // is kept, which means that the lifespan of 'output_buffer' must be larger than
  242. // that of the returned WebPIDecoder object.
  243. // The supplied 'output_buffer' content MUST NOT be changed between calls to
  244. // WebPIAppend() or WebPIUpdate() unless 'output_buffer.is_external_memory' is
  245. // not set to 0. In such a case, it is allowed to modify the pointers, size and
  246. // stride of output_buffer.u.RGBA or output_buffer.u.YUVA, provided they remain
  247. // within valid bounds.
  248. // All other fields of WebPDecBuffer MUST remain constant between calls.
  249. // Returns NULL if the allocation failed.
  250. WEBP_EXTERN WebPIDecoder* WebPINewDecoder(WebPDecBuffer* output_buffer);
  251. // This function allocates and initializes an incremental-decoder object, which
  252. // will output the RGB/A samples specified by 'csp' into a preallocated
  253. // buffer 'output_buffer'. The size of this buffer is at least
  254. // 'output_buffer_size' and the stride (distance in bytes between two scanlines)
  255. // is specified by 'output_stride'.
  256. // Additionally, output_buffer can be passed NULL in which case the output
  257. // buffer will be allocated automatically when the decoding starts. The
  258. // colorspace 'csp' is taken into account for allocating this buffer. All other
  259. // parameters are ignored.
  260. // Returns NULL if the allocation failed, or if some parameters are invalid.
  261. WEBP_EXTERN WebPIDecoder* WebPINewRGB(
  262. WEBP_CSP_MODE csp,
  263. uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
  264. // This function allocates and initializes an incremental-decoder object, which
  265. // will output the raw luma/chroma samples into a preallocated planes if
  266. // supplied. The luma plane is specified by its pointer 'luma', its size
  267. // 'luma_size' and its stride 'luma_stride'. Similarly, the chroma-u plane
  268. // is specified by the 'u', 'u_size' and 'u_stride' parameters, and the chroma-v
  269. // plane by 'v' and 'v_size'. And same for the alpha-plane. The 'a' pointer
  270. // can be pass NULL in case one is not interested in the transparency plane.
  271. // Conversely, 'luma' can be passed NULL if no preallocated planes are supplied.
  272. // In this case, the output buffer will be automatically allocated (using
  273. // MODE_YUVA) when decoding starts. All parameters are then ignored.
  274. // Returns NULL if the allocation failed or if a parameter is invalid.
  275. WEBP_EXTERN WebPIDecoder* WebPINewYUVA(
  276. uint8_t* luma, size_t luma_size, int luma_stride,
  277. uint8_t* u, size_t u_size, int u_stride,
  278. uint8_t* v, size_t v_size, int v_stride,
  279. uint8_t* a, size_t a_size, int a_stride);
  280. // Deprecated version of the above, without the alpha plane.
  281. // Kept for backward compatibility.
  282. WEBP_EXTERN WebPIDecoder* WebPINewYUV(
  283. uint8_t* luma, size_t luma_size, int luma_stride,
  284. uint8_t* u, size_t u_size, int u_stride,
  285. uint8_t* v, size_t v_size, int v_stride);
  286. // Deletes the WebPIDecoder object and associated memory. Must always be called
  287. // if WebPINewDecoder, WebPINewRGB or WebPINewYUV succeeded.
  288. WEBP_EXTERN void WebPIDelete(WebPIDecoder* idec);
  289. // Copies and decodes the next available data. Returns VP8_STATUS_OK when
  290. // the image is successfully decoded. Returns VP8_STATUS_SUSPENDED when more
  291. // data is expected. Returns error in other cases.
  292. WEBP_EXTERN VP8StatusCode WebPIAppend(
  293. WebPIDecoder* idec, const uint8_t* data, size_t data_size);
  294. // A variant of the above function to be used when data buffer contains
  295. // partial data from the beginning. In this case data buffer is not copied
  296. // to the internal memory.
  297. // Note that the value of the 'data' pointer can change between calls to
  298. // WebPIUpdate, for instance when the data buffer is resized to fit larger data.
  299. WEBP_EXTERN VP8StatusCode WebPIUpdate(
  300. WebPIDecoder* idec, const uint8_t* data, size_t data_size);
  301. // Returns the RGB/A image decoded so far. Returns NULL if output params
  302. // are not initialized yet. The RGB/A output type corresponds to the colorspace
  303. // specified during call to WebPINewDecoder() or WebPINewRGB().
  304. // *last_y is the index of last decoded row in raster scan order. Some pointers
  305. // (*last_y, *width etc.) can be NULL if corresponding information is not
  306. // needed. The values in these pointers are only valid on successful (non-NULL)
  307. // return.
  308. WEBP_EXTERN uint8_t* WebPIDecGetRGB(
  309. const WebPIDecoder* idec, int* last_y,
  310. int* width, int* height, int* stride);
  311. // Same as above function to get a YUVA image. Returns pointer to the luma
  312. // plane or NULL in case of error. If there is no alpha information
  313. // the alpha pointer '*a' will be returned NULL.
  314. WEBP_EXTERN uint8_t* WebPIDecGetYUVA(
  315. const WebPIDecoder* idec, int* last_y,
  316. uint8_t** u, uint8_t** v, uint8_t** a,
  317. int* width, int* height, int* stride, int* uv_stride, int* a_stride);
  318. // Deprecated alpha-less version of WebPIDecGetYUVA(): it will ignore the
  319. // alpha information (if present). Kept for backward compatibility.
  320. static WEBP_INLINE uint8_t* WebPIDecGetYUV(
  321. const WebPIDecoder* idec, int* last_y, uint8_t** u, uint8_t** v,
  322. int* width, int* height, int* stride, int* uv_stride) {
  323. return WebPIDecGetYUVA(idec, last_y, u, v, NULL, width, height,
  324. stride, uv_stride, NULL);
  325. }
  326. // Generic call to retrieve information about the displayable area.
  327. // If non NULL, the left/right/width/height pointers are filled with the visible
  328. // rectangular area so far.
  329. // Returns NULL in case the incremental decoder object is in an invalid state.
  330. // Otherwise returns the pointer to the internal representation. This structure
  331. // is read-only, tied to WebPIDecoder's lifespan and should not be modified.
  332. WEBP_EXTERN const WebPDecBuffer* WebPIDecodedArea(
  333. const WebPIDecoder* idec, int* left, int* top, int* width, int* height);
  334. //------------------------------------------------------------------------------
  335. // Advanced decoding parametrization
  336. //
  337. // Code sample for using the advanced decoding API
  338. /*
  339. // A) Init a configuration object
  340. WebPDecoderConfig config;
  341. CHECK(WebPInitDecoderConfig(&config));
  342. // B) optional: retrieve the bitstream's features.
  343. CHECK(WebPGetFeatures(data, data_size, &config.input) == VP8_STATUS_OK);
  344. // C) Adjust 'config', if needed
  345. config.no_fancy_upsampling = 1;
  346. config.output.colorspace = MODE_BGRA;
  347. // etc.
  348. // Note that you can also make config.output point to an externally
  349. // supplied memory buffer, provided it's big enough to store the decoded
  350. // picture. Otherwise, config.output will just be used to allocate memory
  351. // and store the decoded picture.
  352. // D) Decode!
  353. CHECK(WebPDecode(data, data_size, &config) == VP8_STATUS_OK);
  354. // E) Decoded image is now in config.output (and config.output.u.RGBA)
  355. // F) Reclaim memory allocated in config's object. It's safe to call
  356. // this function even if the memory is external and wasn't allocated
  357. // by WebPDecode().
  358. WebPFreeDecBuffer(&config.output);
  359. */
  360. // Features gathered from the bitstream
  361. struct WebPBitstreamFeatures {
  362. int width; // Width in pixels, as read from the bitstream.
  363. int height; // Height in pixels, as read from the bitstream.
  364. int has_alpha; // True if the bitstream contains an alpha channel.
  365. int has_animation; // True if the bitstream is an animation.
  366. int format; // 0 = undefined (/mixed), 1 = lossy, 2 = lossless
  367. uint32_t pad[5]; // padding for later use
  368. };
  369. // Internal, version-checked, entry point
  370. WEBP_EXTERN VP8StatusCode WebPGetFeaturesInternal(
  371. const uint8_t*, size_t, WebPBitstreamFeatures*, int);
  372. // Retrieve features from the bitstream. The *features structure is filled
  373. // with information gathered from the bitstream.
  374. // Returns VP8_STATUS_OK when the features are successfully retrieved. Returns
  375. // VP8_STATUS_NOT_ENOUGH_DATA when more data is needed to retrieve the
  376. // features from headers. Returns error in other cases.
  377. // Note: The following chunk sequences (before the raw VP8/VP8L data) are
  378. // considered valid by this function:
  379. // RIFF + VP8(L)
  380. // RIFF + VP8X + (optional chunks) + VP8(L)
  381. // ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.
  382. // VP8(L) <-- Not a valid WebP format: only allowed for internal purpose.
  383. static WEBP_INLINE VP8StatusCode WebPGetFeatures(
  384. const uint8_t* data, size_t data_size,
  385. WebPBitstreamFeatures* features) {
  386. return WebPGetFeaturesInternal(data, data_size, features,
  387. WEBP_DECODER_ABI_VERSION);
  388. }
  389. // Decoding options
  390. struct WebPDecoderOptions {
  391. int bypass_filtering; // if true, skip the in-loop filtering
  392. int no_fancy_upsampling; // if true, use faster pointwise upsampler
  393. int use_cropping; // if true, cropping is applied _first_
  394. int crop_left, crop_top; // top-left position for cropping.
  395. // Will be snapped to even values.
  396. int crop_width, crop_height; // dimension of the cropping area
  397. int use_scaling; // if true, scaling is applied _afterward_
  398. int scaled_width, scaled_height; // final resolution
  399. int use_threads; // if true, use multi-threaded decoding
  400. int dithering_strength; // dithering strength (0=Off, 100=full)
  401. int flip; // flip output vertically
  402. int alpha_dithering_strength; // alpha dithering strength in [0..100]
  403. uint32_t pad[5]; // padding for later use
  404. };
  405. // Main object storing the configuration for advanced decoding.
  406. struct WebPDecoderConfig {
  407. WebPBitstreamFeatures input; // Immutable bitstream features (optional)
  408. WebPDecBuffer output; // Output buffer (can point to external mem)
  409. WebPDecoderOptions options; // Decoding options
  410. };
  411. // Internal, version-checked, entry point
  412. WEBP_EXTERN int WebPInitDecoderConfigInternal(WebPDecoderConfig*, int);
  413. // Initialize the configuration as empty. This function must always be
  414. // called first, unless WebPGetFeatures() is to be called.
  415. // Returns false in case of mismatched version.
  416. static WEBP_INLINE int WebPInitDecoderConfig(WebPDecoderConfig* config) {
  417. return WebPInitDecoderConfigInternal(config, WEBP_DECODER_ABI_VERSION);
  418. }
  419. // Instantiate a new incremental decoder object with the requested
  420. // configuration. The bitstream can be passed using 'data' and 'data_size'
  421. // parameter, in which case the features will be parsed and stored into
  422. // config->input. Otherwise, 'data' can be NULL and no parsing will occur.
  423. // Note that 'config' can be NULL too, in which case a default configuration
  424. // is used. If 'config' is not NULL, it must outlive the WebPIDecoder object
  425. // as some references to its fields will be used. No internal copy of 'config'
  426. // is made.
  427. // The return WebPIDecoder object must always be deleted calling WebPIDelete().
  428. // Returns NULL in case of error (and config->status will then reflect
  429. // the error condition, if available).
  430. WEBP_EXTERN WebPIDecoder* WebPIDecode(const uint8_t* data, size_t data_size,
  431. WebPDecoderConfig* config);
  432. // Non-incremental version. This version decodes the full data at once, taking
  433. // 'config' into account. Returns decoding status (which should be VP8_STATUS_OK
  434. // if the decoding was successful). Note that 'config' cannot be NULL.
  435. WEBP_EXTERN VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,
  436. WebPDecoderConfig* config);
  437. #ifdef __cplusplus
  438. } // extern "C"
  439. #endif
  440. #endif // WEBP_WEBP_DECODE_H_