ferrisma-libwebp 0.1.0

A mechanical Rust port of libwebp, the WebP reference implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// libwebp v1.6.0, src/dec/buffer_dec.c:
//
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the COPYING file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
// -----------------------------------------------------------------------------
//
// Everything about WebPDecBuffer
//
// Author: Skal (pascal.massimino@gmail.com)

//! Translation of `src/dec/buffer_dec.c` from libwebp v1.6.0.
//!
//! Work package: WP12 (decoder integration).
//!
//! The samples of a buffer are one [`PlaneStorage`] (ADR 0003, buffers and
//! planes): `Internal` is C's `private_memory`, `External` the caller's memory of
//! `is_external_memory`. A C plane pointer is `NULL` here when the buffer holds
//! no memory of its own (an empty `Internal` storage is C's
//! `private_memory == NULL`) or when its region does not exist in the caller's
//! storage (the C ABI gives a `NULL` plane no region); an empty caller slice is
//! a pointer to no bytes, as in C. The
//! member of the `u` union that C reads is the one that the colorspace selects;
//! a Rust buffer whose `u` holds the other member describes no valid plane, and
//! `CheckDecBuffer` rejects it (`# Deviations from upstream`, item 2).
//!
//! C's `is_external_memory >= 2`, slow external memory, is the buffer's
//! `slow_memory` flag (ADR 0003, amended 2026-11-07).
//!
//! # Deviations from upstream
//!
//! Changes of representation that the ADRs allow, or behavior that the Rust
//! side cannot reproduce exactly. Each records the reason.
//!
//! 1. **Freeing.** `WebPFreeDecBuffer` drops library memory;
//!    `WebPInitDecBuffer` and `WebPInitDecoderConfig` drop the old storage,
//!    where C zeroes the structure without freeing it.
//! 2. **Mismatched union member.** A Rust buffer whose `u` holds the member
//!    that the colorspace does not select reads as a zeroed member (ADR 0003),
//!    so `CheckDecBuffer` rejects it and the incremental getters write zeros,
//!    as C does for its zeroed union before allocation. C would reinterpret
//!    arbitrary bytes of the other member; only Rust callers can build such a
//!    buffer, and the C ABI reads the member that the colorspace selects.
//!
//! # Checks not applicable
//!
//! These checks do not apply, or cannot be made, with the reason:
//!
//! 1. **`WebPGrabDecBuffer`** has no caller in libwebp v1.6.0 and is not
//!    exported (`webpi_dec.h` is internal); it is translated and not compared.

use crate::dec::webp_dec::webp_check_crop_dimensions;
use crate::utils::rescaler_utils::webp_rescaler_get_scaled_dimensions;
use crate::utils::utils::{webp_copy_plane, webp_safe_malloc};
use crate::webp::decode::{
    VP8StatusCode, WEBP_CSP_MODE, WEBP_DECODER_ABI_VERSION, WebPBitstreamFeatures, WebPDecBuffer,
    WebPDecBufferPlanes, WebPDecoderOptions, WebPRGBABuffer, WebPYUVABuffer, webp_is_alpha_mode,
    webp_is_premultiplied_mode, webp_is_rgb_mode,
};
use crate::webp::types::{PlaneOffset, PlaneStorage, webp_abi_is_incompatible};

//------------------------------------------------------------------------------
// WebPDecBuffer

// upstream: src/dec/buffer_dec.c kModeBpp
/// Number of bytes per pixel for the different color-spaces.
const MODE_BPP: [u8; WEBP_CSP_MODE::MODE_LAST.0 as usize] = [
    3, 4, 3, 4, 4, 2, 2, //
    4, 4, 4, 2, // pre-multiplied modes
    1, 1,
];

/// `kModeBpp[mode]` for a valid colorspace.
fn mode_bpp(mode: WEBP_CSP_MODE) -> u8 {
    MODE_BPP[mode.0 as usize]
}

// upstream: src/dec/buffer_dec.c IsValidColorspace
/// Converts to an integer to handle both the unsigned and the signed enum
/// cases, as C does: callers pass the colorspace as an `int` (C declares the
/// function in `src/dec/common_dec.h`).
#[must_use]
pub fn is_valid_colorspace(webp_csp_mode: i32) -> bool {
    webp_csp_mode >= WEBP_CSP_MODE::MODE_RGB.0.cast_signed()
        && webp_csp_mode < WEBP_CSP_MODE::MODE_LAST.0.cast_signed()
}

/// The colorspace as the `int` that C passes to `IsValidColorspace`.
fn colorspace_int(mode: WEBP_CSP_MODE) -> i32 {
    mode.0.cast_signed()
}

// upstream: src/dec/buffer_dec.c MIN_BUFFER_SIZE
/// Strictly speaking, the very last (or first, if flipped) row doesn't require
/// padding. C computes `(uint64_t)(STRIDE) * ((HEIGHT) - 1) + (WIDTH)`, with
/// the `int` operands converted to `uint64_t`.
fn min_buffer_size(width: u64, height: i32, stride: i32) -> u64 {
    u64::from(stride.cast_unsigned())
        .wrapping_mul(i64::from(height - 1).cast_unsigned())
        .wrapping_add(width)
}

/// An `int` widened to `uint64_t`, as the C conversion does.
fn to_u64(value: i32) -> u64 {
    i64::from(value).cast_unsigned()
}

/// Whether the plane is C's `NULL` pointer (see the module documentation).
#[must_use]
pub fn is_null_plane(storage: &PlaneStorage<'_, u8>, plane: PlaneOffset) -> bool {
    match storage {
        PlaneStorage::Internal(memory) => memory.is_empty(),
        PlaneStorage::External(_) | PlaneStorage::Shared(_) => {
            storage.region(plane.region).is_none()
        }
    }
}

// upstream: src/dec/buffer_dec.c CheckDecBuffer
fn check_dec_buffer(buffer: &WebPDecBuffer<'_>) -> Result<(), VP8StatusCode> {
    let mut ok = true;
    let mode = buffer.colorspace;
    let width = buffer.width;
    let height = buffer.height;
    if !is_valid_colorspace(colorspace_int(mode)) {
        ok = false;
    } else if !webp_is_rgb_mode(mode) {
        // YUV checks
        if let WebPDecBufferPlanes::YUVA(buf) = &buffer.u {
            let uv_width = (width + 1) / 2;
            let uv_height = (height + 1) / 2;
            let y_stride = buf.y_stride.abs();
            let u_stride = buf.u_stride.abs();
            let v_stride = buf.v_stride.abs();
            let a_stride = buf.a_stride.abs();
            let y_size = min_buffer_size(to_u64(width), height, y_stride);
            let u_size = min_buffer_size(to_u64(uv_width), uv_height, u_stride);
            let v_size = min_buffer_size(to_u64(uv_width), uv_height, v_stride);
            let a_size = min_buffer_size(to_u64(width), height, a_stride);
            ok &= y_size <= buf.y_size as u64;
            ok &= u_size <= buf.u_size as u64;
            ok &= v_size <= buf.v_size as u64;
            ok &= y_stride >= width;
            ok &= u_stride >= uv_width;
            ok &= v_stride >= uv_width;
            ok &= !is_null_plane(&buffer.storage, buf.y);
            ok &= !is_null_plane(&buffer.storage, buf.u);
            ok &= !is_null_plane(&buffer.storage, buf.v);
            if mode == WEBP_CSP_MODE::MODE_YUVA {
                ok &= a_stride >= width;
                ok &= a_size <= buf.a_size as u64;
                ok &= buf.a.is_some_and(|a| !is_null_plane(&buffer.storage, a));
            }
        } else {
            ok = false; // the RGBA member describes no YUVA plane
        }
    } else {
        // RGB checks
        if let WebPDecBufferPlanes::RGBA(buf) = &buffer.u {
            let bpp = mode_bpp(mode);
            let stride = buf.stride.abs();
            let size = min_buffer_size(to_u64(width).wrapping_mul(u64::from(bpp)), height, stride);
            ok &= size <= buf.size as u64;
            ok &= stride >= width * i32::from(bpp);
            ok &= !is_null_plane(&buffer.storage, buf.rgba);
        } else {
            ok = false; // the YUVA member describes no RGBA plane
        }
    }
    if ok {
        Ok(())
    } else {
        Err(VP8StatusCode::VP8_STATUS_INVALID_PARAM)
    }
}

// upstream: src/dec/buffer_dec.c AllocateBuffer
#[expect(
    clippy::cast_possible_truncation,
    reason = "C converts the uint64_t sizes to size_t, which holds them after WebPSafeMalloc"
)]
fn allocate_buffer(buffer: &mut WebPDecBuffer<'_>) -> Result<(), VP8StatusCode> {
    let w = buffer.width;
    let h = buffer.height;
    let mode = buffer.colorspace;

    if w <= 0 || h <= 0 || !is_valid_colorspace(colorspace_int(mode)) {
        return Err(VP8StatusCode::VP8_STATUS_INVALID_PARAM);
    }

    if matches!(&buffer.storage, PlaneStorage::Internal(memory) if memory.is_empty()) {
        let mut uv_stride = 0;
        let mut a_stride = 0;
        let mut uv_size: u64 = 0;
        let mut a_size: u64 = 0;
        // We need memory and it hasn't been allocated yet.
        // => initialize output buffer, now that dimensions are known.
        let bpp = mode_bpp(mode);
        if to_u64(w) * u64::from(bpp) >= (1u64 << 31) {
            return Err(VP8StatusCode::VP8_STATUS_INVALID_PARAM);
        }
        let stride = w * i32::from(bpp);
        let size = to_u64(stride) * to_u64(h);
        if !webp_is_rgb_mode(mode) {
            uv_stride = (w + 1) / 2;
            uv_size = to_u64(uv_stride) * to_u64((h + 1) / 2);
            if mode == WEBP_CSP_MODE::MODE_YUVA {
                a_stride = w;
                a_size = to_u64(a_stride) * to_u64(h);
            }
        }
        let total_size = size + 2 * uv_size + a_size;

        let Some(mut output) = webp_safe_malloc::<u8>(total_size, 1) else {
            return Err(VP8StatusCode::VP8_STATUS_OUT_OF_MEMORY);
        };
        // Rust initializes the allocation (ADR 0003, allocation limits).
        output.resize(total_size as usize, 0);
        buffer.storage = PlaneStorage::Internal(output);

        let plane = |offset: u64| PlaneOffset {
            region: 0,
            offset: offset as usize,
        };
        if webp_is_rgb_mode(mode) {
            // RGBA initialization
            buffer.u = WebPDecBufferPlanes::RGBA(WebPRGBABuffer {
                rgba: plane(0),
                stride,
                size: size as usize,
            });
        } else {
            // YUVA initialization. C leaves `a` alone for MODE_YUV; the RGBA
            // member that the union held before has no `a`, which reads as NULL.
            let a = match &buffer.u {
                WebPDecBufferPlanes::YUVA(buf) => buf.a,
                WebPDecBufferPlanes::RGBA(_) => None,
            };
            buffer.u = WebPDecBufferPlanes::YUVA(WebPYUVABuffer {
                y: plane(0),
                y_stride: stride,
                y_size: size as usize,
                u: plane(size),
                u_stride: uv_stride,
                u_size: uv_size as usize,
                v: plane(size + uv_size),
                v_stride: uv_stride,
                v_size: uv_size as usize,
                a: if mode == WEBP_CSP_MODE::MODE_YUVA {
                    Some(plane(size + 2 * uv_size))
                } else {
                    a
                },
                a_size: a_size as usize,
                a_stride,
            });
        }
    }
    check_dec_buffer(buffer)
}

/// `pointer += delta` for a plane, as C moves a plane pointer by a signed
/// number of bytes.
///
/// # Panics
///
/// Panics when the plane would start before its region, where the C pointer
/// would leave the buffer.
fn move_plane(plane: &mut PlaneOffset, delta: i64) {
    plane.offset = plane
        .offset
        .checked_add_signed(isize::try_from(delta).expect("the plane offset fits"))
        .expect("the flipped plane starts inside its region");
}

// upstream: src/dec/buffer_dec.c WebPFlipBuffer
/// Flips the buffer vertically by negating the various strides. C returns
/// `VP8_STATUS_INVALID_PARAM` for a `NULL` buffer, which exists only at the C
/// ABI.
///
/// # Errors
///
/// None: the result is always `Ok`, as C returns `VP8_STATUS_OK` for every
/// buffer.
///
/// # Panics
///
/// Panics when a plane would start before its region, where C would compute a
/// pointer outside the buffer.
pub fn webp_flip_buffer(buffer: &mut WebPDecBuffer<'_>) -> Result<(), VP8StatusCode> {
    let height = i64::from(buffer.height);
    if webp_is_rgb_mode(buffer.colorspace) {
        if let WebPDecBufferPlanes::RGBA(buf) = &mut buffer.u {
            move_plane(&mut buf.rgba, (height - 1) * i64::from(buf.stride));
            buf.stride = -buf.stride;
        }
    } else if let WebPDecBufferPlanes::YUVA(buf) = &mut buffer.u {
        let h = height;
        move_plane(&mut buf.y, (h - 1) * i64::from(buf.y_stride));
        buf.y_stride = -buf.y_stride;
        move_plane(&mut buf.u, ((h - 1) >> 1) * i64::from(buf.u_stride));
        buf.u_stride = -buf.u_stride;
        move_plane(&mut buf.v, ((h - 1) >> 1) * i64::from(buf.v_stride));
        buf.v_stride = -buf.v_stride;
        if let Some(a) = &mut buf.a {
            move_plane(a, (h - 1) * i64::from(buf.a_stride));
            buf.a_stride = -buf.a_stride;
        }
    }
    Ok(())
}

// upstream: src/dec/buffer_dec.c WebPAllocateDecBuffer
/// Prepares `buffer` with the requested initial dimensions `width` and
/// `height`. If no external storage is supplied, initializes the buffer by
/// allocating output memory and setting up the stride information. Validates
/// the parameters. If `options` is not `None`, also verifies that the options'
/// parameters are valid and applies them to the width and height of the output
/// buffer: cropping, scaling and the vertical flip. C returns
/// `VP8_STATUS_INVALID_PARAM` for a `NULL` buffer, which exists only at the C
/// ABI.
///
/// # Errors
///
/// Returns `VP8_STATUS_INVALID_PARAM` for invalid dimensions, options, strides
/// or sizes and `VP8_STATUS_OUT_OF_MEMORY` when the allocation fails. The width
/// and height are written before the buffer is checked, as in C.
pub fn webp_allocate_dec_buffer(
    width: i32,
    height: i32,
    options: Option<&WebPDecoderOptions>,
    buffer: &mut WebPDecBuffer<'_>,
) -> Result<(), VP8StatusCode> {
    let mut width = width;
    let mut height = height;
    if width <= 0 || height <= 0 {
        return Err(VP8StatusCode::VP8_STATUS_INVALID_PARAM);
    }
    if let Some(options) = options {
        // First, apply options if there is any.
        if options.use_cropping != 0 {
            let cw = options.crop_width;
            let ch = options.crop_height;
            let x = options.crop_left & !1;
            let y = options.crop_top & !1;
            if !webp_check_crop_dimensions(width, height, x, y, cw, ch) {
                return Err(VP8StatusCode::VP8_STATUS_INVALID_PARAM); // out of frame boundary.
            }
            width = cw;
            height = ch;
        }

        if options.use_scaling != 0 {
            // WEBP_REDUCE_SIZE is not defined in the reference configuration.
            let mut scaled_width = options.scaled_width;
            let mut scaled_height = options.scaled_height;
            if !webp_rescaler_get_scaled_dimensions(
                width,
                height,
                &mut scaled_width,
                &mut scaled_height,
            ) {
                return Err(VP8StatusCode::VP8_STATUS_INVALID_PARAM);
            }
            width = scaled_width;
            height = scaled_height;
        }
    }
    buffer.width = width;
    buffer.height = height;

    // Then, allocate buffer for real.
    allocate_buffer(buffer)?;

    // Use the stride trick if vertical flip is needed.
    if options.is_some_and(|options| options.flip != 0) {
        webp_flip_buffer(buffer)?;
    }
    Ok(())
}

//------------------------------------------------------------------------------
// constructors / destructors

// upstream: src/dec/buffer_dec.c WebPInitDecBufferInternal
/// Internal, version-checked, entry point of `WebPInitDecBuffer`: resets the
/// buffer to empty unless the ABI versions are incompatible. Returns false in
/// case of version mismatch. C also returns 0 for a `NULL` buffer, which exists
/// only at the C ABI. C zeroes the structure without freeing it; here the old
/// storage is dropped.
#[must_use]
pub fn webp_init_dec_buffer_internal(buffer: &mut WebPDecBuffer<'_>, version: i32) -> bool {
    if webp_abi_is_incompatible(version, WEBP_DECODER_ABI_VERSION) {
        return false; // version mismatch
    }
    *buffer = WebPDecBuffer::default();
    true
}

// upstream: src/dec/buffer_dec.c WebPFreeDecBuffer
/// Frees any memory associated with the buffer: the memory that the buffer
/// allocated itself. External memory stays the caller's, and the buffer keeps
/// describing it, as C keeps the plane pointers. C accepts a `NULL` buffer,
/// which exists only at the C ABI.
pub fn webp_free_dec_buffer(buffer: &mut WebPDecBuffer<'_>) {
    if let PlaneStorage::Internal(memory) = &mut buffer.storage {
        *memory = Vec::new();
    }
}

// upstream: src/dec/buffer_dec.c WebPCopyDecBuffer
/// Copies `src` into `dst`, making sure `dst` does not own the memory, which
/// `src` still holds. No pixels are copied: `dst` gets a shared view of every
/// region of `src`, with the same plane offsets, where C copies the pointers
/// and marks `dst` as external. C accepts `NULL` buffers, which exist only at
/// the C ABI.
pub fn webp_copy_dec_buffer<'s>(src: &'s WebPDecBuffer<'_>, dst: &mut WebPDecBuffer<'s>) {
    dst.colorspace = src.colorspace;
    dst.width = src.width;
    dst.height = src.height;
    // C copies `is_external_memory`; the view is not external storage, so the
    // flag that only external storage carries is false.
    dst.slow_memory = false;
    dst.u = src.u;
    dst.pad = src.pad;
    dst.storage = PlaneStorage::Shared(match &src.storage {
        PlaneStorage::Internal(memory) => vec![memory.as_slice()],
        PlaneStorage::External(slices) => slices.iter().map(|slice| &**slice).collect(),
        PlaneStorage::Shared(slices) => slices.clone(),
    });
}

// upstream: src/dec/buffer_dec.c WebPGrabDecBuffer
/// Copies and transfers ownership from `src` to `dst` (beware of the parameter
/// order). `dst` receives the storage of `src`. In C, `src` keeps pointers to
/// the memory, now marked external; safe Rust cannot keep a second view of
/// memory that `dst` owns or borrows mutably, so `src` keeps its plane
/// descriptions with an empty storage of the same kind. C accepts `NULL`
/// buffers, which exist only at the C ABI.
pub fn webp_grab_dec_buffer<'a>(src: &mut WebPDecBuffer<'a>, dst: &mut WebPDecBuffer<'a>) {
    dst.colorspace = src.colorspace;
    dst.width = src.width;
    dst.height = src.height;
    dst.slow_memory = src.slow_memory;
    dst.u = src.u;
    dst.pad = src.pad;
    let empty = match &src.storage {
        PlaneStorage::Internal(_) => PlaneStorage::Internal(Vec::new()),
        PlaneStorage::External(_) => PlaneStorage::External(Vec::new()),
        PlaneStorage::Shared(_) => PlaneStorage::Shared(Vec::new()),
    };
    dst.storage = std::mem::replace(&mut src.storage, empty);
}

/// The samples of `plane` for reading.
///
/// # Panics
///
/// Panics when the region does not exist.
fn region<'b>(storage: &'b PlaneStorage<'_, u8>, plane: PlaneOffset) -> &'b [u8] {
    storage
        .region(plane.region)
        .expect("the plane's region exists")
}

/// The samples of `plane` for writing.
///
/// # Panics
///
/// Panics when the region does not exist or is shared.
fn region_mut<'b>(storage: &'b mut PlaneStorage<'_, u8>, plane: PlaneOffset) -> &'b mut [u8] {
    storage
        .region_mut(plane.region)
        .expect("the plane's region exists")
}

/// One plane copy of `WebPCopyDecBufferPixels`.
#[expect(
    clippy::too_many_arguments,
    reason = "the arguments of WebPCopyPlane with the two storages"
)]
fn copy_plane(
    src: &PlaneStorage<'_, u8>,
    src_plane: PlaneOffset,
    src_stride: i32,
    dst: &mut PlaneStorage<'_, u8>,
    dst_plane: PlaneOffset,
    dst_stride: i32,
    width: i32,
    height: i32,
) {
    webp_copy_plane(
        region(src, src_plane),
        src_plane.offset,
        src_stride,
        region_mut(dst, dst_plane),
        dst_plane.offset,
        dst_stride,
        width,
        height,
    );
}

// upstream: src/dec/buffer_dec.c WebPCopyDecBufferPixels
/// Copies the pixels of `src_buf` into the preallocated `dst_buf`.
///
/// # Errors
///
/// Returns `VP8_STATUS_INVALID_PARAM` if `dst_buf` is not set up correctly for
/// the copy; its width and height are set to those of `src_buf` first, as in C.
///
/// # Panics
///
/// Debug builds panic when the colorspaces differ, as C asserts. A plane of
/// `src_buf` outside its region panics, where C would read outside the buffer.
pub fn webp_copy_dec_buffer_pixels(
    src_buf: &WebPDecBuffer<'_>,
    dst_buf: &mut WebPDecBuffer<'_>,
) -> Result<(), VP8StatusCode> {
    debug_assert_eq!(src_buf.colorspace, dst_buf.colorspace);

    dst_buf.width = src_buf.width;
    dst_buf.height = src_buf.height;
    if check_dec_buffer(dst_buf).is_err() {
        return Err(VP8StatusCode::VP8_STATUS_INVALID_PARAM);
    }
    match (&src_buf.u, &dst_buf.u) {
        (WebPDecBufferPlanes::RGBA(src), WebPDecBufferPlanes::RGBA(dst))
            if webp_is_rgb_mode(src_buf.colorspace) =>
        {
            let (src, dst) = (*src, *dst);
            copy_plane(
                &src_buf.storage,
                src.rgba,
                src.stride,
                &mut dst_buf.storage,
                dst.rgba,
                dst.stride,
                src_buf.width * i32::from(mode_bpp(src_buf.colorspace)),
                src_buf.height,
            );
        }
        (WebPDecBufferPlanes::YUVA(src), WebPDecBufferPlanes::YUVA(dst))
            if !webp_is_rgb_mode(src_buf.colorspace) =>
        {
            let (src, dst) = (*src, *dst);
            copy_plane(
                &src_buf.storage,
                src.y,
                src.y_stride,
                &mut dst_buf.storage,
                dst.y,
                dst.y_stride,
                src_buf.width,
                src_buf.height,
            );
            copy_plane(
                &src_buf.storage,
                src.u,
                src.u_stride,
                &mut dst_buf.storage,
                dst.u,
                dst.u_stride,
                (src_buf.width + 1) / 2,
                (src_buf.height + 1) / 2,
            );
            copy_plane(
                &src_buf.storage,
                src.v,
                src.v_stride,
                &mut dst_buf.storage,
                dst.v,
                dst.v_stride,
                (src_buf.width + 1) / 2,
                (src_buf.height + 1) / 2,
            );
            if webp_is_alpha_mode(src_buf.colorspace) {
                copy_plane(
                    &src_buf.storage,
                    src.a.expect("a YUVA buffer has an alpha plane"),
                    src.a_stride,
                    &mut dst_buf.storage,
                    dst.a.expect("the checked YUVA buffer has an alpha plane"),
                    dst.a_stride,
                    src_buf.width,
                    src_buf.height,
                );
            }
        }
        _ => panic!("the source buffer's member matches its colorspace"),
    }
    Ok(())
}

// upstream: src/dec/buffer_dec.c WebPAvoidSlowMemory
/// Returns true if decoding will be slow with the current configuration and
/// bitstream features: slow external memory, a premultiplied colorspace and an
/// image with alpha.
#[must_use]
pub fn webp_avoid_slow_memory(
    output: &WebPDecBuffer<'_>,
    features: Option<&WebPBitstreamFeatures>,
) -> bool {
    // C: output->is_external_memory >= 2.
    output.slow_memory
        && webp_is_premultiplied_mode(output.colorspace)
        && features.is_some_and(|features| features.has_alpha != 0)
}