libwebp_sys/encode.rs
1use std::os::raw::*;
2
3// MAJOR(8b) + MINOR(8b)
4cfg_if! {
5 if #[cfg(feature = "1_5")] {
6 pub const WEBP_ENCODER_ABI_VERSION: c_int = 0x0210;
7 } else if #[cfg(feature = "1_1")] {
8 pub const WEBP_ENCODER_ABI_VERSION: c_int = 0x020F;
9 } else if #[cfg(feature = "0_6")] {
10 pub const WEBP_ENCODER_ABI_VERSION: c_int = 0x020E;
11 } else if #[cfg(feature = "0_5")] {
12 pub const WEBP_ENCODER_ABI_VERSION: c_int = 0x0209;
13 } else {
14 pub const WEBP_ENCODER_ABI_VERSION: c_int = 0x0202;
15 }
16}
17
18/// Image characteristics hint for the underlying encoder.
19#[allow(non_camel_case_types)]
20pub type WebPImageHint = u32;
21
22/// default preset.
23pub const WEBP_HINT_DEFAULT: WebPImageHint = 0;
24/// digital picture, like portrait, inner shot
25pub const WEBP_HINT_PICTURE: WebPImageHint = 1;
26/// outdoor photograph, with natural lighting
27pub const WEBP_HINT_PHOTO: WebPImageHint = 2;
28/// Discrete tone image (graph, map-tile etc).
29pub const WEBP_HINT_GRAPH: WebPImageHint = 3;
30pub const WEBP_HINT_LAST: WebPImageHint = 4;
31
32/// Compression parameters.
33#[allow(non_snake_case)]
34#[repr(C)]
35#[derive(Debug, Clone, Copy)]
36pub struct WebPConfig {
37 /// Lossless encoding (0=lossy(default), 1=lossless).
38 pub lossless: c_int,
39 /// between 0 and 100. For lossy, 0 gives the smallest
40 /// size and 100 the largest. For lossless, this
41 /// parameter is the amount of effort put into the
42 /// compression: 0 is the fastest but gives larger
43 /// files compared to the slowest, but best, 100.
44 pub quality: c_float,
45 /// quality/speed trade-off (0=fast, 6=slower-better)
46 pub method: c_int,
47 /// Hint for image type (lossless only for now).
48 pub image_hint: WebPImageHint,
49 /// if non-zero, set the desired target size in bytes.
50 /// Takes precedence over the `compression` parameter.
51 pub target_size: c_int,
52 /// if non-zero, specifies the minimal distortion to
53 /// try to achieve. Takes precedence over target_size.
54 pub target_PSNR: c_float,
55 /// maximum number of segments to use, in \[1..4\]
56 pub segments: c_int,
57 /// Spatial Noise Shaping. 0=off, 100=maximum.
58 pub sns_strength: c_int,
59 /// range: \[0 = off .. 100 = strongest\]
60 pub filter_strength: c_int,
61 /// range: \[0 = off .. 7 = least sharp\]
62 pub filter_sharpness: c_int,
63 /// filtering type: 0 = simple, 1 = strong (only used
64 /// if filter_strength > 0 or autofilter > 0)
65 pub filter_type: c_int,
66 /// Auto adjust filter's strength \[0 = off, 1 = on\]
67 pub autofilter: c_int,
68 /// Algorithm for encoding the alpha plane (0 = none,
69 /// 1 = compressed with WebP lossless). Default is 1.
70 pub alpha_compression: c_int,
71 /// Predictive filtering method for alpha plane.
72 /// 0: none, 1: fast, 2: best. Default if 1.
73 pub alpha_filtering: c_int,
74 /// Between 0 (smallest size) and 100 (lossless).
75 /// Default is 100.
76 pub alpha_quality: c_int,
77 /// number of entropy-analysis passes (in \[1..10\]).
78 pub pass: c_int,
79 /// if true, export the compressed picture back.
80 /// In-loop filtering is not applied.
81 pub show_compressed: c_int,
82 /// preprocessing filter:
83 /// 0=none, 1=segment-smooth, 2=pseudo-random dithering
84 pub preprocessing: c_int,
85 /// log2(number of token partitions) in \[0..3\]. Default
86 /// is set to 0 for easier progressive decoding.
87 pub partitions: c_int,
88 /// quality degradation allowed to fit the 512k limit
89 /// on prediction modes coding (0: no degradation,
90 /// 100: maximum possible degradation).
91 pub partition_limit: c_int,
92 /// If true, compression parameters will be remapped
93 /// to better match the expected output size from
94 /// JPEG compression. Generally, the output size will
95 /// be similar but the degradation will be lower.
96 pub emulate_jpeg_size: c_int,
97 /// If non-zero, try and use multi-threaded encoding.
98 pub thread_level: c_int,
99 /// If set, reduce memory usage (but increase CPU use).
100 pub low_memory: c_int,
101 /// Near lossless encoding \[0 = max loss .. 100 = off
102 /// (default)\].
103 #[cfg(feature = "0_5")]
104 pub near_lossless: c_int,
105 /// if non-zero, preserve the exact RGB values under
106 /// transparent area. Otherwise, discard this invisible
107 /// RGB information for better compression. The default
108 /// value is 0.
109 #[cfg(feature = "0_5")]
110 pub exact: c_int,
111 /// reserved
112 #[cfg(feature = "0_6")]
113 pub use_delta_palette: c_int,
114 /// if needed, use sharp (and slow) RGB->YUV conversion
115 #[cfg(feature = "0_6")]
116 pub use_sharp_yuv: c_int,
117 /// minimum permissible quality factor
118 #[cfg(feature = "1_2")]
119 pub qmin: c_int,
120 /// maximum permissible quality factor
121 #[cfg(feature = "1_2")]
122 pub qmax: c_int,
123 #[cfg(not(feature = "0_5"))]
124 #[doc(hidden)]
125 pub pad: [u32; 5],
126 #[cfg(all(feature = "0_5", not(feature = "0_6")))]
127 #[doc(hidden)]
128 pub pad: [u32; 3],
129 #[cfg(all(feature = "0_6", not(feature = "1_2")))]
130 #[doc(hidden)]
131 pub pad: [u32; 2],
132}
133
134/// Enumerate some predefined settings for WebPConfig, depending on the type
135/// of source picture. These presets are used when calling WebPConfigPreset().
136#[allow(non_camel_case_types)]
137pub type WebPPreset = u32;
138
139/// default preset.
140pub const WEBP_PRESET_DEFAULT: WebPPreset = 0;
141/// digital picture, like portrait, inner shot
142pub const WEBP_PRESET_PICTURE: WebPPreset = 1;
143/// outdoor photograph, with natural lighting
144pub const WEBP_PRESET_PHOTO: WebPPreset = 2;
145/// hand or line drawing, with high-contrast details
146pub const WEBP_PRESET_DRAWING: WebPPreset = 3;
147/// small-sized colorful images
148pub const WEBP_PRESET_ICON: WebPPreset = 4;
149/// text-like
150pub const WEBP_PRESET_TEXT: WebPPreset = 5;
151
152/// Structure for storing auxiliary statistics.
153#[allow(non_snake_case)]
154#[repr(C)]
155#[derive(Debug, Clone, Copy)]
156pub struct WebPAuxStats {
157 /// final size
158 pub coded_size: c_int,
159 /// peak-signal-to-noise ratio for Y/U/V/All/Alpha
160 pub PSNR: [c_float; 5],
161 /// number of intra4/intra16/skipped macroblocks
162 pub block_count: [c_int; 3],
163 /// approximate number of bytes spent for header
164 /// and mode-partition #0
165 pub header_bytes: [c_int; 2],
166 /// approximate number of bytes spent for
167 /// DC/AC/uv coefficients for each (0..3) segments.
168 pub residual_bytes: [[c_int; 4]; 3],
169 /// number of macroblocks in each segments
170 pub segment_size: [c_int; 4],
171 /// quantizer values for each segments
172 pub segment_quant: [c_int; 4],
173 /// filtering strength for each segments \[0..63\]
174 pub segment_level: [c_int; 4],
175 /// size of the transparency data
176 pub alpha_data_size: c_int,
177 /// size of the enhancement layer data
178 pub layer_data_size: c_int,
179 // lossless encoder statistics
180 /// bit0:predictor bit1:cross-color transform
181 /// bit2:subtract-green bit3:color indexing
182 pub lossless_features: u32,
183 /// number of precision bits of histogram
184 pub histogram_bits: c_int,
185 /// precision bits for predictor transform
186 pub transform_bits: c_int,
187 /// number of bits for color cache lookup
188 pub cache_bits: c_int,
189 /// number of color in palette, if used
190 pub palette_size: c_int,
191 /// final lossless size
192 pub lossless_size: c_int,
193 /// lossless header (transform, huffman etc) size
194 #[cfg(feature = "0_5")]
195 pub lossless_hdr_size: c_int,
196 /// lossless image data size
197 #[cfg(feature = "0_5")]
198 pub lossless_data_size: c_int,
199 /// precision bits for cross-color transform
200 #[cfg(feature = "1_5")]
201 pub cross_color_transform_bits: c_int,
202 /// padding for later use
203 #[cfg(not(feature = "0_5"))]
204 #[doc(hidden)]
205 pub pad: [u32; 4],
206 /// padding for later use
207 #[cfg(all(feature = "0_5", not(feature = "1_5")))]
208 #[doc(hidden)]
209 pub pad: [u32; 2],
210 /// padding for later use
211 #[cfg(feature = "1_5")]
212 #[doc(hidden)]
213 pub pad: [u32; 1],
214}
215
216/// Signature for output function. Should return true if writing was successful.
217/// data/data_size is the segment of data to write, and `picture` is for
218/// reference (and so one can make use of picture->custom_ptr).
219pub type WebPWriterFunction = Option<extern "C" fn(*const u8, usize, *const WebPPicture) -> c_int>;
220
221/// Progress hook, called from time to time to report progress. It can return
222/// false to request an abort of the encoding process, or true otherwise if
223/// everything is OK.
224pub type WebPProgressHook = Option<extern "C" fn(c_int, *const WebPPicture) -> c_int>;
225
226/// Color spaces.
227#[allow(non_camel_case_types)]
228pub type WebPEncCSP = u32;
229
230// chroma sampling
231/// 4:2:0
232pub const WEBP_YUV420: WebPEncCSP = 0;
233/// alpha channel variant
234pub const WEBP_YUV420A: WebPEncCSP = 4;
235
236/// bit-mask to get the UV sampling factors
237pub const WEBP_CSP_UV_MASK: WebPEncCSP = 3;
238/// bit that is set if alpha is present
239pub const WEBP_CSP_ALPHA_BIT: WebPEncCSP = 4;
240
241/// Encoding error conditions.
242#[allow(non_camel_case_types)]
243pub type WebPEncodingError = u32;
244
245pub const VP8_ENC_OK: WebPEncodingError = 0;
246/// memory error allocating objects
247pub const VP8_ENC_ERROR_OUT_OF_MEMORY: WebPEncodingError = 1;
248/// memory error while flushing bits
249pub const VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY: WebPEncodingError = 2;
250/// a pointer parameter is NULL
251pub const VP8_ENC_ERROR_NULL_PARAMETER: WebPEncodingError = 3;
252/// configuration is invalid
253pub const VP8_ENC_ERROR_INVALID_CONFIGURATION: WebPEncodingError = 4;
254/// picture has invalid width/height
255pub const VP8_ENC_ERROR_BAD_DIMENSION: WebPEncodingError = 5;
256/// partition is bigger than 512k
257pub const VP8_ENC_ERROR_PARTITION0_OVERFLOW: WebPEncodingError = 6;
258/// partition is bigger than 16M
259pub const VP8_ENC_ERROR_PARTITION_OVERFLOW: WebPEncodingError = 7;
260/// error while flushing bytes
261pub const VP8_ENC_ERROR_BAD_WRITE: WebPEncodingError = 8;
262/// file is bigger than 4G
263pub const VP8_ENC_ERROR_FILE_TOO_BIG: WebPEncodingError = 9;
264/// abort request by user
265pub const VP8_ENC_ERROR_USER_ABORT: WebPEncodingError = 10;
266/// list terminator. always last.
267pub const VP8_ENC_ERROR_LAST: WebPEncodingError = 11;
268
269/// WebPMemoryWrite: a special WebPWriterFunction that writes to memory using
270/// the following WebPMemoryWriter object (to be set as a custom_ptr).
271#[repr(C)]
272#[derive(Debug, Clone, Copy)]
273pub struct WebPMemoryWriter {
274 pub mem: *mut u8,
275 pub size: usize,
276 pub max_size: usize,
277 #[doc(hidden)]
278 pub pad: [u32; 1],
279}
280
281// maximum width/height allowed (inclusive), in pixels
282pub const WEBP_MAX_DIMENSION: c_int = 16383;
283
284/// Main exchange structure (input samples, output bytes, statistics)
285///
286/// Once WebPPictureInit() has been called, it's ok to make all the INPUT fields
287/// (use_argb, y/u/v, argb, ...) point to user-owned data, even if
288/// WebPPictureAlloc() has been called. Depending on the value use_argb,
289/// it's guaranteed that either \*argb or \*y/\*u/\*v content will be kept untouched.
290#[repr(C)]
291#[derive(Debug, Clone, Copy)]
292pub struct WebPPicture {
293 // INPUT
294 //////////////
295 /// Main flag for encoder selecting between ARGB or YUV input.
296 /// It is recommended to use ARGB input (\*argb, argb_stride) for lossless
297 /// compression, and YUV input (\*y, \*u, \*v, etc.) for lossy compression
298 /// since these are the respective native colorspace for these formats.
299 pub use_argb: c_int,
300
301 // YUV input (mostly used for input to lossy compression)
302 /// colorspace: should be YUV420 for now (=Y'CbCr).
303 pub colorspace: WebPEncCSP,
304 /// dimensions (less or equal to WEBP_MAX_DIMENSION)
305 pub width: c_int,
306 /// dimensions (less or equal to WEBP_MAX_DIMENSION)
307 pub height: c_int,
308 /// pointers to luma/chroma planes.
309 pub y: *mut u8,
310 /// pointers to luma/chroma planes.
311 pub u: *mut u8,
312 /// pointers to luma/chroma planes.
313 pub v: *mut u8,
314 /// luma/chroma strides.
315 pub y_stride: c_int,
316 /// luma/chroma strides.
317 pub uv_stride: c_int,
318 /// pointer to the alpha plane
319 pub a: *mut u8,
320 /// stride of the alpha plane
321 pub a_stride: c_int,
322 /// padding for later use
323 #[doc(hidden)]
324 pub pad1: [u32; 2],
325
326 // ARGB input (mostly used for input to lossless compression)
327 /// Pointer to argb (32 bit) plane.
328 pub argb: *mut u32,
329 /// This is stride in pixels units, not bytes.
330 pub argb_stride: c_int,
331 /// padding for later use
332 #[doc(hidden)]
333 pub pad2: [u32; 3],
334
335 // OUTPUT
336 ///////////////
337 // Byte-emission hook, to store compressed bytes as they are ready.
338 /// can be NULL
339 pub writer: WebPWriterFunction,
340 /// can be used by the writer.
341 pub custom_ptr: *mut c_void,
342
343 // map for extra information (only for lossy compression mode)
344 /// 1: intra type, 2: segment, 3: quant
345 /// 4: intra-16 prediction mode,
346 /// 5: chroma prediction mode,
347 /// 6: bit cost, 7: distortion
348 pub extra_info_type: c_int,
349 /// if not NULL, points to an array of size
350 /// ((width + 15) / 16) \* ((height + 15) / 16) that
351 /// will be filled with a macroblock map, depending
352 /// on extra_info_type.
353 pub extra_info: *mut u8,
354
355 // STATS AND REPORTS
356 ///////////////////////////
357 /// Pointer to side statistics (updated only if not NULL)
358 pub stats: *mut WebPAuxStats,
359 /// Error code for the latest error encountered during encoding
360 pub error_code: WebPEncodingError,
361 /// If not NULL, report progress during encoding.
362 pub progress_hook: WebPProgressHook,
363 /// this field is free to be set to any value and
364 /// used during callbacks (like progress-report e.g.).
365 pub user_data: *mut c_void,
366 /// padding for later use
367 #[doc(hidden)]
368 pub pad3: [u32; 3],
369 /// Unused for now
370 #[doc(hidden)]
371 pub pad4: *mut u8,
372 /// Unused for now
373 #[doc(hidden)]
374 pub pad5: *mut u8,
375 /// padding for later use
376 #[doc(hidden)]
377 pub pad6: [u32; 8],
378
379 // PRIVATE FIELDS
380 ////////////////////
381 /// row chunk of memory for yuva planes
382 #[doc(hidden)]
383 pub memory_: *mut c_void,
384 /// and for argb too.
385 #[doc(hidden)]
386 pub memory_argb_: *mut c_void,
387 /// padding for later use
388 #[doc(hidden)]
389 pub pad7: [*mut c_void; 2],
390}
391
392unsafe extern "C" {
393 /// Return the encoder's version number, packed in hexadecimal using 8bits for
394 /// each of major/minor/revision. E.g: v2.5.7 is 0x020507.
395 pub fn WebPGetEncoderVersion() -> c_int;
396 /// Returns the size of the compressed data (pointed to by `*output``), or 0 if
397 /// an error occurred. The compressed data must be released by the caller
398 /// using the call `WebPFree(*output)`.
399 /// These functions compress using the lossy format, and the quality_factor
400 /// can go from 0 (smaller output, lower quality) to 100 (best quality,
401 /// larger output).
402 pub fn WebPEncodeRGB(
403 rgb: *const u8,
404 width: c_int,
405 height: c_int,
406 stride: c_int,
407 quality_factor: c_float,
408 output: *mut *mut u8,
409 ) -> usize;
410 /// Returns the size of the compressed data (pointed to by `*output``), or 0 if
411 /// an error occurred. The compressed data must be released by the caller
412 /// using the call `WebPFree(*output)`.
413 /// These functions compress using the lossy format, and the quality_factor
414 /// can go from 0 (smaller output, lower quality) to 100 (best quality,
415 /// larger output).
416 pub fn WebPEncodeBGR(
417 bgr: *const u8,
418 width: c_int,
419 height: c_int,
420 stride: c_int,
421 quality_factor: c_float,
422 output: *mut *mut u8,
423 ) -> usize;
424 /// Returns the size of the compressed data (pointed to by `*output``), or 0 if
425 /// an error occurred. The compressed data must be released by the caller
426 /// using the call `WebPFree(*output)`.
427 /// These functions compress using the lossy format, and the quality_factor
428 /// can go from 0 (smaller output, lower quality) to 100 (best quality,
429 /// larger output).
430 pub fn WebPEncodeRGBA(
431 rgba: *const u8,
432 width: c_int,
433 height: c_int,
434 stride: c_int,
435 quality_factor: c_float,
436 output: *mut *mut u8,
437 ) -> usize;
438 /// Returns the size of the compressed data (pointed to by `*output``), or 0 if
439 /// an error occurred. The compressed data must be released by the caller
440 /// using the call `WebPFree(*output)`.
441 /// These functions compress using the lossy format, and the quality_factor
442 /// can go from 0 (smaller output, lower quality) to 100 (best quality,
443 /// larger output).
444 pub fn WebPEncodeBGRA(
445 bgra: *const u8,
446 width: c_int,
447 height: c_int,
448 stride: c_int,
449 quality_factor: c_float,
450 output: *mut *mut u8,
451 ) -> usize;
452 /// Equivalent to [WebPEncodeRGB], but compressing in a
453 /// lossless manner. Files are usually larger than lossy format, but will
454 /// not suffer any compression loss.
455 /// Note these functions, like the lossy versions, use the library's default
456 /// settings. For lossless this means `exact` is disabled. RGB values in
457 /// transparent areas will be modified to improve compression. To avoid this,
458 /// use WebPEncode() and set WebPConfig::exact to 1.
459 pub fn WebPEncodeLosslessRGB(
460 rgb: *const u8,
461 width: c_int,
462 height: c_int,
463 stride: c_int,
464 output: *mut *mut u8,
465 ) -> usize;
466 /// Equivalent to [WebPEncodeBGR], but compressing in a
467 /// lossless manner. Files are usually larger than lossy format, but will
468 /// not suffer any compression loss.
469 /// Note these functions, like the lossy versions, use the library's default
470 /// settings. For lossless this means `exact` is disabled. RGB values in
471 /// transparent areas will be modified to improve compression. To avoid this,
472 /// use WebPEncode() and set WebPConfig::exact to 1.
473 pub fn WebPEncodeLosslessBGR(
474 bgr: *const u8,
475 width: c_int,
476 height: c_int,
477 stride: c_int,
478 output: *mut *mut u8,
479 ) -> usize;
480 /// Equivalent to [WebPEncodeRGBA], but compressing in a
481 /// lossless manner. Files are usually larger than lossy format, but will
482 /// not suffer any compression loss.
483 /// Note these functions, like the lossy versions, use the library's default
484 /// settings. For lossless this means `exact` is disabled. RGB values in
485 /// transparent areas will be modified to improve compression. To avoid this,
486 /// use WebPEncode() and set WebPConfig::exact to 1.
487 pub fn WebPEncodeLosslessRGBA(
488 rgba: *const u8,
489 width: c_int,
490 height: c_int,
491 stride: c_int,
492 output: *mut *mut u8,
493 ) -> usize;
494 /// Equivalent to [WebPEncodeBGRA], but compressing in a
495 /// lossless manner. Files are usually larger than lossy format, but will
496 /// not suffer any compression loss.
497 /// Note these functions, like the lossy versions, use the library's default
498 /// settings. For lossless this means `exact` is disabled. RGB values in
499 /// transparent areas will be modified to improve compression. To avoid this,
500 /// use WebPEncode() and set WebPConfig::exact to 1.
501 pub fn WebPEncodeLosslessBGRA(
502 bgra: *const u8,
503 width: c_int,
504 height: c_int,
505 stride: c_int,
506 output: *mut *mut u8,
507 ) -> usize;
508 /// Internal, version-checked, entry point
509 #[doc(hidden)]
510 #[must_use]
511 pub fn WebPConfigInitInternal(_: *mut WebPConfig, _: WebPPreset, _: c_float, _: c_int)
512 -> c_int;
513 /// Activate the lossless compression mode with the desired efficiency level
514 /// between 0 (fastest, lowest compression) and 9 (slower, best compression).
515 /// A good default level is `6`, providing a fair tradeoff between compression
516 /// speed and final compressed size.
517 /// This function will overwrite several fields from config: `method`, `quality`
518 /// and `lossless`. Returns false in case of parameter error.
519 #[cfg(feature = "0_5")]
520 #[must_use]
521 pub fn WebPConfigLosslessPreset(config: *mut WebPConfig, level: c_int) -> c_int;
522 /// Returns true if `config` is non-NULL and all configuration parameters are
523 /// within their valid ranges.
524 #[must_use]
525 pub fn WebPValidateConfig(config: *const WebPConfig) -> c_int;
526 /// The following must be called first before any use.
527 pub fn WebPMemoryWriterInit(writer: *mut WebPMemoryWriter);
528 /// The following must be called to deallocate writer->mem memory. The `writer`
529 /// object itself is not deallocated.
530 #[cfg(feature = "0_5")]
531 pub fn WebPMemoryWriterClear(writer: *mut WebPMemoryWriter);
532 /// The custom writer to be used with WebPMemoryWriter as custom_ptr. Upon
533 /// completion, writer.mem and writer.size will hold the coded data.
534 /// writer.mem must be freed by calling WebPMemoryWriterClear.
535 #[must_use]
536 pub fn WebPMemoryWrite(data: *const u8, data_size: usize, picture: *const WebPPicture)
537 -> c_int;
538 /// Internal, version-checked, entry point
539 #[doc(hidden)]
540 #[must_use]
541 pub fn WebPPictureInitInternal(_: *mut WebPPicture, _: c_int) -> c_int;
542 /// Convenience allocation / deallocation based on picture->width/height:
543 /// Allocate y/u/v buffers as per colorspace/width/height specification.
544 /// Note! This function will free the previous buffer if needed.
545 /// Returns false in case of memory error.
546 #[must_use]
547 pub fn WebPPictureAlloc(picture: *mut WebPPicture) -> c_int;
548 /// Release the memory allocated by WebPPictureAlloc() or WebPPictureImport\*().
549 /// Note that this function does _not_ free the memory used by the `picture`
550 /// object itself.
551 /// Besides memory (which is reclaimed) all other fields of `picture` are
552 /// preserved.
553 pub fn WebPPictureFree(picture: *mut WebPPicture);
554 /// Copy the pixels of \*src into \*dst, using WebPPictureAlloc. Upon return, \*dst
555 /// will fully own the copied pixels (this is not a view). The `dst` picture need
556 /// not be initialized as its content is overwritten.
557 /// Returns false in case of memory allocation error.
558 #[must_use]
559 pub fn WebPPictureCopy(src: *const WebPPicture, dst: *mut WebPPicture) -> c_int;
560 /// Compute the single distortion for packed planes of samples.
561 /// `src` will be compared to `ref`, and the raw distortion stored into
562 /// `*distortion`. The refined metric (log(MSE), log(1 - ssim),...) will be
563 /// stored in `*result`.
564 /// `x_step` is the horizontal stride (in bytes) between samples.
565 /// `src/ref_stride` is the byte distance between rows.
566 /// Returns false in case of error (bad parameter, memory allocation error, ...).
567 #[cfg(feature = "0_6")]
568 #[must_use]
569 pub fn WebPPlaneDistortion(
570 src: *const u8,
571 src_stride: usize,
572 ref_: *const u8,
573 ref_stride: usize,
574 width: c_int,
575 height: c_int,
576 x_step: usize,
577 type_: c_int,
578 distortion: *mut c_float,
579 result: *mut c_float,
580 ) -> c_int;
581 /// Compute PSNR, SSIM or LSIM distortion metric between two pictures. Results
582 /// are in dB, stored in result\[\] in the B/G/R/A/All order. The distortion is
583 /// always performed using ARGB samples. Hence if the input is YUV(A), the
584 /// picture will be internally converted to ARGB (just for the measurement).
585 /// Warning: this function is rather CPU-intensive.
586 #[must_use]
587 pub fn WebPPictureDistortion(
588 src: *const WebPPicture,
589 ref_: *const WebPPicture,
590 metric_type: c_int,
591 result: *mut c_float,
592 ) -> c_int;
593 /// self-crops a picture to the rectangle defined by top/left/width/height.
594 /// Returns false in case of memory allocation error, or if the rectangle is
595 /// outside of the source picture.
596 /// The rectangle for the view is defined by the top-left corner pixel
597 /// coordinates (left, top) as well as its width and height. This rectangle
598 /// must be fully be comprised inside the `src` source picture. If the source
599 /// picture uses the YUV420 colorspace, the top and left coordinates will be
600 /// snapped to even values.
601 #[must_use]
602 pub fn WebPPictureCrop(
603 picture: *mut WebPPicture,
604 left: c_int,
605 top: c_int,
606 width: c_int,
607 height: c_int,
608 ) -> c_int;
609 /// Extracts a view from `src` picture into `dst`. The rectangle for the view
610 /// is defined by the top-left corner pixel coordinates (left, top) as well
611 /// as its width and height. This rectangle must be fully be comprised inside
612 /// the `src` source picture. If the source picture uses the YUV420 colorspace,
613 /// the top and left coordinates will be snapped to even values.
614 /// Picture `src` must out-live `dst` picture. Self-extraction of view is allowed
615 /// (`src` equal to `dst`) as a mean of fast-cropping (but note that doing so,
616 /// the original dimension will be lost). Picture `dst` need not be initialized
617 /// with WebPPictureInit() if it is different from `src`, since its content will
618 /// be overwritten.
619 /// Returns false in case of invalid parameters.
620 #[must_use]
621 pub fn WebPPictureView(
622 src: *const WebPPicture,
623 left: c_int,
624 top: c_int,
625 width: c_int,
626 height: c_int,
627 dst: *mut WebPPicture,
628 ) -> c_int;
629 /// Returns true if the `picture` is actually a view and therefore does
630 /// not own the memory for pixels.
631 pub fn WebPPictureIsView(picture: *const WebPPicture) -> c_int;
632 /// Rescale a picture to new dimension width x height.
633 /// If either `width` or `height` (but not both) is 0 the corresponding
634 /// dimension will be calculated preserving the aspect ratio.
635 /// No gamma correction is applied.
636 /// Returns false in case of error (invalid parameter or insufficient memory).
637 #[must_use]
638 pub fn WebPPictureRescale(picture: *mut WebPPicture, width: c_int, height: c_int) -> c_int;
639 /// Colorspace conversion function to import RGB samples.
640 /// Previous buffer will be free'd, if any.
641 /// \*rgb buffer should have a size of at least height \* rgb_stride.
642 /// Returns false in case of memory error.
643 #[must_use]
644 pub fn WebPPictureImportRGB(
645 picture: *mut WebPPicture,
646 rgb: *const u8,
647 rgb_stride: c_int,
648 ) -> c_int;
649 /// Same as [WebPPictureImportRGB], but for RGBA buffer.
650 #[must_use]
651 pub fn WebPPictureImportRGBA(
652 picture: *mut WebPPicture,
653 rgba: *const u8,
654 rgba_stride: c_int,
655 ) -> c_int;
656 /// Same as [WebPPictureImportRGB], but for RGBA buffer. Imports the RGB direct from the 32-bit format
657 /// input buffer ignoring the alpha channel. Avoids needing to copy the data
658 /// to a temporary 24-bit RGB buffer to import the RGB only.
659 #[must_use]
660 pub fn WebPPictureImportRGBX(
661 picture: *mut WebPPicture,
662 rgbx: *const u8,
663 rgbx_stride: c_int,
664 ) -> c_int;
665 /// Variants of the above [WebPPictureImportRGB], but taking BGR(A|X) input.
666 #[must_use]
667 pub fn WebPPictureImportBGR(
668 picture: *mut WebPPicture,
669 bgr: *const u8,
670 bgr_stride: c_int,
671 ) -> c_int;
672 /// Variants of the above [WebPPictureImportRGB], but taking BGR(A|X) input.
673 #[must_use]
674 pub fn WebPPictureImportBGRA(
675 picture: *mut WebPPicture,
676 bgra: *const u8,
677 bgra_stride: c_int,
678 ) -> c_int;
679 /// Variants of the above [WebPPictureImportRGB], but taking BGR(A|X) input.
680 #[must_use]
681 pub fn WebPPictureImportBGRX(
682 picture: *mut WebPPicture,
683 bgrx: *const u8,
684 bgrx_stride: c_int,
685 ) -> c_int;
686 /// Converts picture->argb data to the YUV420A format. The `colorspace`
687 /// parameter is deprecated and should be equal to WEBP_YUV420.
688 /// Upon return, picture->use_argb is set to false. The presence of real
689 /// non-opaque transparent values is detected, and `colorspace` will be
690 /// adjusted accordingly. Note that this method is lossy.
691 /// Returns false in case of error.
692 #[must_use]
693 pub fn WebPPictureARGBToYUVA(picture: *mut WebPPicture, colorspace: WebPEncCSP) -> c_int;
694 /// Same as WebPPictureARGBToYUVA(), but the conversion is done using
695 /// pseudo-random dithering with a strength `dithering` between
696 /// 0.0 (no dithering) and 1.0 (maximum dithering). This is useful
697 /// for photographic picture.
698 #[must_use]
699 pub fn WebPPictureARGBToYUVADithered(
700 picture: *mut WebPPicture,
701 colorspace: WebPEncCSP,
702 dithering: c_float,
703 ) -> c_int;
704 /// Performs `sharp` RGBA->YUVA420 downsampling and colorspace conversion
705 /// Downsampling is handled with extra care in case of color clipping. This
706 /// method is roughly 2x slower than WebPPictureARGBToYUVA() but produces better
707 /// and sharper YUV representation.
708 /// Returns false in case of error.
709 #[cfg(feature = "0_6")]
710 #[must_use]
711 pub fn WebPPictureSharpARGBToYUVA(picture: *mut WebPPicture) -> c_int;
712 /// kept for backward compatibility:
713 #[cfg(feature = "0_5")]
714 #[must_use]
715 pub fn WebPPictureSmartARGBToYUVA(picture: *mut WebPPicture) -> c_int;
716 /// Converts picture->yuv to picture->argb and sets picture->use_argb to true.
717 /// The input format must be YUV_420 or YUV_420A. The conversion from YUV420 to
718 /// ARGB incurs a small loss too.
719 /// Note that the use of this colorspace is discouraged if one has access to the
720 /// raw ARGB samples, since using YUV420 is comparatively lossy.
721 /// Returns false in case of error.
722 #[must_use]
723 pub fn WebPPictureYUVAToARGB(picture: *mut WebPPicture) -> c_int;
724 /// Helper function: given a width x height plane of RGBA or YUV(A) samples
725 /// clean-up or smoothen the YUV or RGB samples under fully transparent area,
726 /// to help compressibility (no guarantee, though).
727 pub fn WebPCleanupTransparentArea(picture: *mut WebPPicture);
728 /// Scan the picture `picture` for the presence of non fully opaque alpha values.
729 /// Returns true in such case. Otherwise returns false (indicating that the
730 /// alpha plane can be ignored altogether e.g.).
731 pub fn WebPPictureHasTransparency(picture: *const WebPPicture) -> c_int;
732 /// Remove the transparency information (if present) by blending the color with
733 /// the background color `background_rgb` (specified as 24bit RGB triplet).
734 /// After this call, all alpha values are reset to 0xff.
735 pub fn WebPBlendAlpha(picture: *mut WebPPicture, background_rgb: u32);
736 /// Main encoding call, after config and picture have been initialized.
737 /// `picture` must be less than 16384x16384 in dimension (cf WEBP_MAX_DIMENSION),
738 /// and the `config` object must be a valid one.
739 /// Returns false in case of error, true otherwise.
740 /// In case of error, picture->error_code is updated accordingly.
741 /// `picture` can hold the source samples in both YUV(A) or ARGB input, depending
742 /// on the value of `picture->use_argb`. It is highly recommended to use
743 /// the former for lossy encoding, and the latter for lossless encoding
744 /// (when config.lossless is true). Automatic conversion from one format to
745 /// another is provided but they both incur some loss.
746 #[must_use]
747 pub fn WebPEncode(config: *const WebPConfig, picture: *mut WebPPicture) -> c_int;
748}
749
750/// Should always be called, to initialize a fresh WebPConfig structure before
751/// modification. Returns false in case of version mismatch. WebPConfigInit()
752/// must have succeeded before using the `config` object.
753/// Note that the default values are lossless=0 and quality=75.
754#[allow(non_snake_case)]
755#[must_use]
756#[inline]
757pub unsafe extern "C" fn WebPConfigInit(config: *mut WebPConfig) -> c_int {
758 unsafe {
759 WebPConfigInitInternal(
760 config,
761 WEBP_PRESET_DEFAULT,
762 75_f32 as c_float,
763 WEBP_ENCODER_ABI_VERSION,
764 )
765 }
766}
767
768/// This function will initialize the configuration according to a predefined
769/// set of parameters (referred to by `preset`) and a given quality factor.
770/// This function can be called as a replacement to WebPConfigInit(). Will
771/// return false in case of error.
772#[allow(non_snake_case)]
773#[must_use]
774#[inline]
775pub unsafe extern "C" fn WebPConfigPreset(
776 config: *mut WebPConfig,
777 preset: WebPPreset,
778 quality: c_float,
779) -> c_int {
780 unsafe { WebPConfigInitInternal(config, preset, quality, WEBP_ENCODER_ABI_VERSION) }
781}
782
783/// Should always be called, to initialize the structure. Returns false in case
784/// of version mismatch. WebPPictureInit() must have succeeded before using the
785/// `picture` object.
786/// Note that, by default, use_argb is false and colorspace is WEBP_YUV420.
787#[allow(non_snake_case)]
788#[must_use]
789#[inline]
790pub unsafe extern "C" fn WebPPictureInit(picture: *mut WebPPicture) -> c_int {
791 unsafe { WebPPictureInitInternal(picture, WEBP_ENCODER_ABI_VERSION) }
792}