encode.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. // Copyright 2011 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. // WebP encoder: main interface
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #ifndef WEBP_WEBP_ENCODE_H_
  14. #define WEBP_WEBP_ENCODE_H_
  15. #include "./types.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #define WEBP_ENCODER_ABI_VERSION 0x020f // 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 WebPImageHint WebPImageHint;
  23. // typedef enum WebPEncCSP WebPEncCSP;
  24. // typedef enum WebPPreset WebPPreset;
  25. // typedef enum WebPEncodingError WebPEncodingError;
  26. typedef struct WebPConfig WebPConfig;
  27. typedef struct WebPPicture WebPPicture; // main structure for I/O
  28. typedef struct WebPAuxStats WebPAuxStats;
  29. typedef struct WebPMemoryWriter WebPMemoryWriter;
  30. // Return the encoder's version number, packed in hexadecimal using 8bits for
  31. // each of major/minor/revision. E.g: v2.5.7 is 0x020507.
  32. WEBP_EXTERN int WebPGetEncoderVersion(void);
  33. //------------------------------------------------------------------------------
  34. // One-stop-shop call! No questions asked:
  35. // Returns the size of the compressed data (pointed to by *output), or 0 if
  36. // an error occurred. The compressed data must be released by the caller
  37. // using the call 'WebPFree(*output)'.
  38. // These functions compress using the lossy format, and the quality_factor
  39. // can go from 0 (smaller output, lower quality) to 100 (best quality,
  40. // larger output).
  41. WEBP_EXTERN size_t WebPEncodeRGB(const uint8_t* rgb,
  42. int width, int height, int stride,
  43. float quality_factor, uint8_t** output);
  44. WEBP_EXTERN size_t WebPEncodeBGR(const uint8_t* bgr,
  45. int width, int height, int stride,
  46. float quality_factor, uint8_t** output);
  47. WEBP_EXTERN size_t WebPEncodeRGBA(const uint8_t* rgba,
  48. int width, int height, int stride,
  49. float quality_factor, uint8_t** output);
  50. WEBP_EXTERN size_t WebPEncodeBGRA(const uint8_t* bgra,
  51. int width, int height, int stride,
  52. float quality_factor, uint8_t** output);
  53. // These functions are the equivalent of the above, but compressing in a
  54. // lossless manner. Files are usually larger than lossy format, but will
  55. // not suffer any compression loss.
  56. // Note these functions, like the lossy versions, use the library's default
  57. // settings. For lossless this means 'exact' is disabled. RGB values in
  58. // transparent areas will be modified to improve compression. To avoid this,
  59. // use WebPEncode() and set WebPConfig::exact to 1.
  60. WEBP_EXTERN size_t WebPEncodeLosslessRGB(const uint8_t* rgb,
  61. int width, int height, int stride,
  62. uint8_t** output);
  63. WEBP_EXTERN size_t WebPEncodeLosslessBGR(const uint8_t* bgr,
  64. int width, int height, int stride,
  65. uint8_t** output);
  66. WEBP_EXTERN size_t WebPEncodeLosslessRGBA(const uint8_t* rgba,
  67. int width, int height, int stride,
  68. uint8_t** output);
  69. WEBP_EXTERN size_t WebPEncodeLosslessBGRA(const uint8_t* bgra,
  70. int width, int height, int stride,
  71. uint8_t** output);
  72. //------------------------------------------------------------------------------
  73. // Coding parameters
  74. // Image characteristics hint for the underlying encoder.
  75. typedef enum WebPImageHint {
  76. WEBP_HINT_DEFAULT = 0, // default preset.
  77. WEBP_HINT_PICTURE, // digital picture, like portrait, inner shot
  78. WEBP_HINT_PHOTO, // outdoor photograph, with natural lighting
  79. WEBP_HINT_GRAPH, // Discrete tone image (graph, map-tile etc).
  80. WEBP_HINT_LAST
  81. } WebPImageHint;
  82. // Compression parameters.
  83. struct WebPConfig {
  84. int lossless; // Lossless encoding (0=lossy(default), 1=lossless).
  85. float quality; // between 0 and 100. For lossy, 0 gives the smallest
  86. // size and 100 the largest. For lossless, this
  87. // parameter is the amount of effort put into the
  88. // compression: 0 is the fastest but gives larger
  89. // files compared to the slowest, but best, 100.
  90. int method; // quality/speed trade-off (0=fast, 6=slower-better)
  91. WebPImageHint image_hint; // Hint for image type (lossless only for now).
  92. int target_size; // if non-zero, set the desired target size in bytes.
  93. // Takes precedence over the 'compression' parameter.
  94. float target_PSNR; // if non-zero, specifies the minimal distortion to
  95. // try to achieve. Takes precedence over target_size.
  96. int segments; // maximum number of segments to use, in [1..4]
  97. int sns_strength; // Spatial Noise Shaping. 0=off, 100=maximum.
  98. int filter_strength; // range: [0 = off .. 100 = strongest]
  99. int filter_sharpness; // range: [0 = off .. 7 = least sharp]
  100. int filter_type; // filtering type: 0 = simple, 1 = strong (only used
  101. // if filter_strength > 0 or autofilter > 0)
  102. int autofilter; // Auto adjust filter's strength [0 = off, 1 = on]
  103. int alpha_compression; // Algorithm for encoding the alpha plane (0 = none,
  104. // 1 = compressed with WebP lossless). Default is 1.
  105. int alpha_filtering; // Predictive filtering method for alpha plane.
  106. // 0: none, 1: fast, 2: best. Default if 1.
  107. int alpha_quality; // Between 0 (smallest size) and 100 (lossless).
  108. // Default is 100.
  109. int pass; // number of entropy-analysis passes (in [1..10]).
  110. int show_compressed; // if true, export the compressed picture back.
  111. // In-loop filtering is not applied.
  112. int preprocessing; // preprocessing filter:
  113. // 0=none, 1=segment-smooth, 2=pseudo-random dithering
  114. int partitions; // log2(number of token partitions) in [0..3]. Default
  115. // is set to 0 for easier progressive decoding.
  116. int partition_limit; // quality degradation allowed to fit the 512k limit
  117. // on prediction modes coding (0: no degradation,
  118. // 100: maximum possible degradation).
  119. int emulate_jpeg_size; // If true, compression parameters will be remapped
  120. // to better match the expected output size from
  121. // JPEG compression. Generally, the output size will
  122. // be similar but the degradation will be lower.
  123. int thread_level; // If non-zero, try and use multi-threaded encoding.
  124. int low_memory; // If set, reduce memory usage (but increase CPU use).
  125. int near_lossless; // Near lossless encoding [0 = max loss .. 100 = off
  126. // (default)].
  127. int exact; // if non-zero, preserve the exact RGB values under
  128. // transparent area. Otherwise, discard this invisible
  129. // RGB information for better compression. The default
  130. // value is 0.
  131. int use_delta_palette; // reserved for future lossless feature
  132. int use_sharp_yuv; // if needed, use sharp (and slow) RGB->YUV conversion
  133. int qmin; // minimum permissible quality factor
  134. int qmax; // maximum permissible quality factor
  135. };
  136. // Enumerate some predefined settings for WebPConfig, depending on the type
  137. // of source picture. These presets are used when calling WebPConfigPreset().
  138. typedef enum WebPPreset {
  139. WEBP_PRESET_DEFAULT = 0, // default preset.
  140. WEBP_PRESET_PICTURE, // digital picture, like portrait, inner shot
  141. WEBP_PRESET_PHOTO, // outdoor photograph, with natural lighting
  142. WEBP_PRESET_DRAWING, // hand or line drawing, with high-contrast details
  143. WEBP_PRESET_ICON, // small-sized colorful images
  144. WEBP_PRESET_TEXT // text-like
  145. } WebPPreset;
  146. // Internal, version-checked, entry point
  147. WEBP_EXTERN int WebPConfigInitInternal(WebPConfig*, WebPPreset, float, int);
  148. // Should always be called, to initialize a fresh WebPConfig structure before
  149. // modification. Returns false in case of version mismatch. WebPConfigInit()
  150. // must have succeeded before using the 'config' object.
  151. // Note that the default values are lossless=0 and quality=75.
  152. static WEBP_INLINE int WebPConfigInit(WebPConfig* config) {
  153. return WebPConfigInitInternal(config, WEBP_PRESET_DEFAULT, 75.f,
  154. WEBP_ENCODER_ABI_VERSION);
  155. }
  156. // This function will initialize the configuration according to a predefined
  157. // set of parameters (referred to by 'preset') and a given quality factor.
  158. // This function can be called as a replacement to WebPConfigInit(). Will
  159. // return false in case of error.
  160. static WEBP_INLINE int WebPConfigPreset(WebPConfig* config,
  161. WebPPreset preset, float quality) {
  162. return WebPConfigInitInternal(config, preset, quality,
  163. WEBP_ENCODER_ABI_VERSION);
  164. }
  165. // Activate the lossless compression mode with the desired efficiency level
  166. // between 0 (fastest, lowest compression) and 9 (slower, best compression).
  167. // A good default level is '6', providing a fair tradeoff between compression
  168. // speed and final compressed size.
  169. // This function will overwrite several fields from config: 'method', 'quality'
  170. // and 'lossless'. Returns false in case of parameter error.
  171. WEBP_EXTERN int WebPConfigLosslessPreset(WebPConfig* config, int level);
  172. // Returns true if 'config' is non-NULL and all configuration parameters are
  173. // within their valid ranges.
  174. WEBP_EXTERN int WebPValidateConfig(const WebPConfig* config);
  175. //------------------------------------------------------------------------------
  176. // Input / Output
  177. // Structure for storing auxiliary statistics.
  178. struct WebPAuxStats {
  179. int coded_size; // final size
  180. float PSNR[5]; // peak-signal-to-noise ratio for Y/U/V/All/Alpha
  181. int block_count[3]; // number of intra4/intra16/skipped macroblocks
  182. int header_bytes[2]; // approximate number of bytes spent for header
  183. // and mode-partition #0
  184. int residual_bytes[3][4]; // approximate number of bytes spent for
  185. // DC/AC/uv coefficients for each (0..3) segments.
  186. int segment_size[4]; // number of macroblocks in each segments
  187. int segment_quant[4]; // quantizer values for each segments
  188. int segment_level[4]; // filtering strength for each segments [0..63]
  189. int alpha_data_size; // size of the transparency data
  190. int layer_data_size; // size of the enhancement layer data
  191. // lossless encoder statistics
  192. uint32_t lossless_features; // bit0:predictor bit1:cross-color transform
  193. // bit2:subtract-green bit3:color indexing
  194. int histogram_bits; // number of precision bits of histogram
  195. int transform_bits; // precision bits for transform
  196. int cache_bits; // number of bits for color cache lookup
  197. int palette_size; // number of color in palette, if used
  198. int lossless_size; // final lossless size
  199. int lossless_hdr_size; // lossless header (transform, huffman etc) size
  200. int lossless_data_size; // lossless image data size
  201. uint32_t pad[2]; // padding for later use
  202. };
  203. // Signature for output function. Should return true if writing was successful.
  204. // data/data_size is the segment of data to write, and 'picture' is for
  205. // reference (and so one can make use of picture->custom_ptr).
  206. typedef int (*WebPWriterFunction)(const uint8_t* data, size_t data_size,
  207. const WebPPicture* picture);
  208. // WebPMemoryWrite: a special WebPWriterFunction that writes to memory using
  209. // the following WebPMemoryWriter object (to be set as a custom_ptr).
  210. struct WebPMemoryWriter {
  211. uint8_t* mem; // final buffer (of size 'max_size', larger than 'size').
  212. size_t size; // final size
  213. size_t max_size; // total capacity
  214. uint32_t pad[1]; // padding for later use
  215. };
  216. // The following must be called first before any use.
  217. WEBP_EXTERN void WebPMemoryWriterInit(WebPMemoryWriter* writer);
  218. // The following must be called to deallocate writer->mem memory. The 'writer'
  219. // object itself is not deallocated.
  220. WEBP_EXTERN void WebPMemoryWriterClear(WebPMemoryWriter* writer);
  221. // The custom writer to be used with WebPMemoryWriter as custom_ptr. Upon
  222. // completion, writer.mem and writer.size will hold the coded data.
  223. // writer.mem must be freed by calling WebPMemoryWriterClear.
  224. WEBP_EXTERN int WebPMemoryWrite(const uint8_t* data, size_t data_size,
  225. const WebPPicture* picture);
  226. // Progress hook, called from time to time to report progress. It can return
  227. // false to request an abort of the encoding process, or true otherwise if
  228. // everything is OK.
  229. typedef int (*WebPProgressHook)(int percent, const WebPPicture* picture);
  230. // Color spaces.
  231. typedef enum WebPEncCSP {
  232. // chroma sampling
  233. WEBP_YUV420 = 0, // 4:2:0
  234. WEBP_YUV420A = 4, // alpha channel variant
  235. WEBP_CSP_UV_MASK = 3, // bit-mask to get the UV sampling factors
  236. WEBP_CSP_ALPHA_BIT = 4 // bit that is set if alpha is present
  237. } WebPEncCSP;
  238. // Encoding error conditions.
  239. typedef enum WebPEncodingError {
  240. VP8_ENC_OK = 0,
  241. VP8_ENC_ERROR_OUT_OF_MEMORY, // memory error allocating objects
  242. VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY, // memory error while flushing bits
  243. VP8_ENC_ERROR_NULL_PARAMETER, // a pointer parameter is NULL
  244. VP8_ENC_ERROR_INVALID_CONFIGURATION, // configuration is invalid
  245. VP8_ENC_ERROR_BAD_DIMENSION, // picture has invalid width/height
  246. VP8_ENC_ERROR_PARTITION0_OVERFLOW, // partition is bigger than 512k
  247. VP8_ENC_ERROR_PARTITION_OVERFLOW, // partition is bigger than 16M
  248. VP8_ENC_ERROR_BAD_WRITE, // error while flushing bytes
  249. VP8_ENC_ERROR_FILE_TOO_BIG, // file is bigger than 4G
  250. VP8_ENC_ERROR_USER_ABORT, // abort request by user
  251. VP8_ENC_ERROR_LAST // list terminator. always last.
  252. } WebPEncodingError;
  253. // maximum width/height allowed (inclusive), in pixels
  254. #define WEBP_MAX_DIMENSION 16383
  255. // Main exchange structure (input samples, output bytes, statistics)
  256. //
  257. // Once WebPPictureInit() has been called, it's ok to make all the INPUT fields
  258. // (use_argb, y/u/v, argb, ...) point to user-owned data, even if
  259. // WebPPictureAlloc() has been called. Depending on the value use_argb,
  260. // it's guaranteed that either *argb or *y/*u/*v content will be kept untouched.
  261. struct WebPPicture {
  262. // INPUT
  263. //////////////
  264. // Main flag for encoder selecting between ARGB or YUV input.
  265. // It is recommended to use ARGB input (*argb, argb_stride) for lossless
  266. // compression, and YUV input (*y, *u, *v, etc.) for lossy compression
  267. // since these are the respective native colorspace for these formats.
  268. int use_argb;
  269. // YUV input (mostly used for input to lossy compression)
  270. WebPEncCSP colorspace; // colorspace: should be YUV420 for now (=Y'CbCr).
  271. int width, height; // dimensions (less or equal to WEBP_MAX_DIMENSION)
  272. uint8_t* y, *u, *v; // pointers to luma/chroma planes.
  273. int y_stride, uv_stride; // luma/chroma strides.
  274. uint8_t* a; // pointer to the alpha plane
  275. int a_stride; // stride of the alpha plane
  276. uint32_t pad1[2]; // padding for later use
  277. // ARGB input (mostly used for input to lossless compression)
  278. uint32_t* argb; // Pointer to argb (32 bit) plane.
  279. int argb_stride; // This is stride in pixels units, not bytes.
  280. uint32_t pad2[3]; // padding for later use
  281. // OUTPUT
  282. ///////////////
  283. // Byte-emission hook, to store compressed bytes as they are ready.
  284. WebPWriterFunction writer; // can be NULL
  285. void* custom_ptr; // can be used by the writer.
  286. // map for extra information (only for lossy compression mode)
  287. int extra_info_type; // 1: intra type, 2: segment, 3: quant
  288. // 4: intra-16 prediction mode,
  289. // 5: chroma prediction mode,
  290. // 6: bit cost, 7: distortion
  291. uint8_t* extra_info; // if not NULL, points to an array of size
  292. // ((width + 15) / 16) * ((height + 15) / 16) that
  293. // will be filled with a macroblock map, depending
  294. // on extra_info_type.
  295. // STATS AND REPORTS
  296. ///////////////////////////
  297. // Pointer to side statistics (updated only if not NULL)
  298. WebPAuxStats* stats;
  299. // Error code for the latest error encountered during encoding
  300. WebPEncodingError error_code;
  301. // If not NULL, report progress during encoding.
  302. WebPProgressHook progress_hook;
  303. void* user_data; // this field is free to be set to any value and
  304. // used during callbacks (like progress-report e.g.).
  305. uint32_t pad3[3]; // padding for later use
  306. // Unused for now
  307. uint8_t* pad4, *pad5;
  308. uint32_t pad6[8]; // padding for later use
  309. // PRIVATE FIELDS
  310. ////////////////////
  311. void* memory_; // row chunk of memory for yuva planes
  312. void* memory_argb_; // and for argb too.
  313. void* pad7[2]; // padding for later use
  314. };
  315. // Internal, version-checked, entry point
  316. WEBP_EXTERN int WebPPictureInitInternal(WebPPicture*, int);
  317. // Should always be called, to initialize the structure. Returns false in case
  318. // of version mismatch. WebPPictureInit() must have succeeded before using the
  319. // 'picture' object.
  320. // Note that, by default, use_argb is false and colorspace is WEBP_YUV420.
  321. static WEBP_INLINE int WebPPictureInit(WebPPicture* picture) {
  322. return WebPPictureInitInternal(picture, WEBP_ENCODER_ABI_VERSION);
  323. }
  324. //------------------------------------------------------------------------------
  325. // WebPPicture utils
  326. // Convenience allocation / deallocation based on picture->width/height:
  327. // Allocate y/u/v buffers as per colorspace/width/height specification.
  328. // Note! This function will free the previous buffer if needed.
  329. // Returns false in case of memory error.
  330. WEBP_EXTERN int WebPPictureAlloc(WebPPicture* picture);
  331. // Release the memory allocated by WebPPictureAlloc() or WebPPictureImport*().
  332. // Note that this function does _not_ free the memory used by the 'picture'
  333. // object itself.
  334. // Besides memory (which is reclaimed) all other fields of 'picture' are
  335. // preserved.
  336. WEBP_EXTERN void WebPPictureFree(WebPPicture* picture);
  337. // Copy the pixels of *src into *dst, using WebPPictureAlloc. Upon return, *dst
  338. // will fully own the copied pixels (this is not a view). The 'dst' picture need
  339. // not be initialized as its content is overwritten.
  340. // Returns false in case of memory allocation error.
  341. WEBP_EXTERN int WebPPictureCopy(const WebPPicture* src, WebPPicture* dst);
  342. // Compute the single distortion for packed planes of samples.
  343. // 'src' will be compared to 'ref', and the raw distortion stored into
  344. // '*distortion'. The refined metric (log(MSE), log(1 - ssim),...' will be
  345. // stored in '*result'.
  346. // 'x_step' is the horizontal stride (in bytes) between samples.
  347. // 'src/ref_stride' is the byte distance between rows.
  348. // Returns false in case of error (bad parameter, memory allocation error, ...).
  349. WEBP_EXTERN int WebPPlaneDistortion(const uint8_t* src, size_t src_stride,
  350. const uint8_t* ref, size_t ref_stride,
  351. int width, int height,
  352. size_t x_step,
  353. int type, // 0 = PSNR, 1 = SSIM, 2 = LSIM
  354. float* distortion, float* result);
  355. // Compute PSNR, SSIM or LSIM distortion metric between two pictures. Results
  356. // are in dB, stored in result[] in the B/G/R/A/All order. The distortion is
  357. // always performed using ARGB samples. Hence if the input is YUV(A), the
  358. // picture will be internally converted to ARGB (just for the measurement).
  359. // Warning: this function is rather CPU-intensive.
  360. WEBP_EXTERN int WebPPictureDistortion(
  361. const WebPPicture* src, const WebPPicture* ref,
  362. int metric_type, // 0 = PSNR, 1 = SSIM, 2 = LSIM
  363. float result[5]);
  364. // self-crops a picture to the rectangle defined by top/left/width/height.
  365. // Returns false in case of memory allocation error, or if the rectangle is
  366. // outside of the source picture.
  367. // The rectangle for the view is defined by the top-left corner pixel
  368. // coordinates (left, top) as well as its width and height. This rectangle
  369. // must be fully be comprised inside the 'src' source picture. If the source
  370. // picture uses the YUV420 colorspace, the top and left coordinates will be
  371. // snapped to even values.
  372. WEBP_EXTERN int WebPPictureCrop(WebPPicture* picture,
  373. int left, int top, int width, int height);
  374. // Extracts a view from 'src' picture into 'dst'. The rectangle for the view
  375. // is defined by the top-left corner pixel coordinates (left, top) as well
  376. // as its width and height. This rectangle must be fully be comprised inside
  377. // the 'src' source picture. If the source picture uses the YUV420 colorspace,
  378. // the top and left coordinates will be snapped to even values.
  379. // Picture 'src' must out-live 'dst' picture. Self-extraction of view is allowed
  380. // ('src' equal to 'dst') as a mean of fast-cropping (but note that doing so,
  381. // the original dimension will be lost). Picture 'dst' need not be initialized
  382. // with WebPPictureInit() if it is different from 'src', since its content will
  383. // be overwritten.
  384. // Returns false in case of memory allocation error or invalid parameters.
  385. WEBP_EXTERN int WebPPictureView(const WebPPicture* src,
  386. int left, int top, int width, int height,
  387. WebPPicture* dst);
  388. // Returns true if the 'picture' is actually a view and therefore does
  389. // not own the memory for pixels.
  390. WEBP_EXTERN int WebPPictureIsView(const WebPPicture* picture);
  391. // Rescale a picture to new dimension width x height.
  392. // If either 'width' or 'height' (but not both) is 0 the corresponding
  393. // dimension will be calculated preserving the aspect ratio.
  394. // No gamma correction is applied.
  395. // Returns false in case of error (invalid parameter or insufficient memory).
  396. WEBP_EXTERN int WebPPictureRescale(WebPPicture* pic, int width, int height);
  397. // Colorspace conversion function to import RGB samples.
  398. // Previous buffer will be free'd, if any.
  399. // *rgb buffer should have a size of at least height * rgb_stride.
  400. // Returns false in case of memory error.
  401. WEBP_EXTERN int WebPPictureImportRGB(
  402. WebPPicture* picture, const uint8_t* rgb, int rgb_stride);
  403. // Same, but for RGBA buffer.
  404. WEBP_EXTERN int WebPPictureImportRGBA(
  405. WebPPicture* picture, const uint8_t* rgba, int rgba_stride);
  406. // Same, but for RGBA buffer. Imports the RGB direct from the 32-bit format
  407. // input buffer ignoring the alpha channel. Avoids needing to copy the data
  408. // to a temporary 24-bit RGB buffer to import the RGB only.
  409. WEBP_EXTERN int WebPPictureImportRGBX(
  410. WebPPicture* picture, const uint8_t* rgbx, int rgbx_stride);
  411. // Variants of the above, but taking BGR(A|X) input.
  412. WEBP_EXTERN int WebPPictureImportBGR(
  413. WebPPicture* picture, const uint8_t* bgr, int bgr_stride);
  414. WEBP_EXTERN int WebPPictureImportBGRA(
  415. WebPPicture* picture, const uint8_t* bgra, int bgra_stride);
  416. WEBP_EXTERN int WebPPictureImportBGRX(
  417. WebPPicture* picture, const uint8_t* bgrx, int bgrx_stride);
  418. // Converts picture->argb data to the YUV420A format. The 'colorspace'
  419. // parameter is deprecated and should be equal to WEBP_YUV420.
  420. // Upon return, picture->use_argb is set to false. The presence of real
  421. // non-opaque transparent values is detected, and 'colorspace' will be
  422. // adjusted accordingly. Note that this method is lossy.
  423. // Returns false in case of error.
  424. WEBP_EXTERN int WebPPictureARGBToYUVA(WebPPicture* picture,
  425. WebPEncCSP /*colorspace = WEBP_YUV420*/);
  426. // Same as WebPPictureARGBToYUVA(), but the conversion is done using
  427. // pseudo-random dithering with a strength 'dithering' between
  428. // 0.0 (no dithering) and 1.0 (maximum dithering). This is useful
  429. // for photographic picture.
  430. WEBP_EXTERN int WebPPictureARGBToYUVADithered(
  431. WebPPicture* picture, WebPEncCSP colorspace, float dithering);
  432. // Performs 'sharp' RGBA->YUVA420 downsampling and colorspace conversion.
  433. // Downsampling is handled with extra care in case of color clipping. This
  434. // method is roughly 2x slower than WebPPictureARGBToYUVA() but produces better
  435. // and sharper YUV representation.
  436. // Returns false in case of error.
  437. WEBP_EXTERN int WebPPictureSharpARGBToYUVA(WebPPicture* picture);
  438. // kept for backward compatibility:
  439. WEBP_EXTERN int WebPPictureSmartARGBToYUVA(WebPPicture* picture);
  440. // Converts picture->yuv to picture->argb and sets picture->use_argb to true.
  441. // The input format must be YUV_420 or YUV_420A. The conversion from YUV420 to
  442. // ARGB incurs a small loss too.
  443. // Note that the use of this colorspace is discouraged if one has access to the
  444. // raw ARGB samples, since using YUV420 is comparatively lossy.
  445. // Returns false in case of error.
  446. WEBP_EXTERN int WebPPictureYUVAToARGB(WebPPicture* picture);
  447. // Helper function: given a width x height plane of RGBA or YUV(A) samples
  448. // clean-up or smoothen the YUV or RGB samples under fully transparent area,
  449. // to help compressibility (no guarantee, though).
  450. WEBP_EXTERN void WebPCleanupTransparentArea(WebPPicture* picture);
  451. // Scan the picture 'picture' for the presence of non fully opaque alpha values.
  452. // Returns true in such case. Otherwise returns false (indicating that the
  453. // alpha plane can be ignored altogether e.g.).
  454. WEBP_EXTERN int WebPPictureHasTransparency(const WebPPicture* picture);
  455. // Remove the transparency information (if present) by blending the color with
  456. // the background color 'background_rgb' (specified as 24bit RGB triplet).
  457. // After this call, all alpha values are reset to 0xff.
  458. WEBP_EXTERN void WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb);
  459. //------------------------------------------------------------------------------
  460. // Main call
  461. // Main encoding call, after config and picture have been initialized.
  462. // 'picture' must be less than 16384x16384 in dimension (cf WEBP_MAX_DIMENSION),
  463. // and the 'config' object must be a valid one.
  464. // Returns false in case of error, true otherwise.
  465. // In case of error, picture->error_code is updated accordingly.
  466. // 'picture' can hold the source samples in both YUV(A) or ARGB input, depending
  467. // on the value of 'picture->use_argb'. It is highly recommended to use
  468. // the former for lossy encoding, and the latter for lossless encoding
  469. // (when config.lossless is true). Automatic conversion from one format to
  470. // another is provided but they both incur some loss.
  471. WEBP_EXTERN int WebPEncode(const WebPConfig* config, WebPPicture* picture);
  472. //------------------------------------------------------------------------------
  473. #ifdef __cplusplus
  474. } // extern "C"
  475. #endif
  476. #endif // WEBP_WEBP_ENCODE_H_