decode.h 23 KB

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