Coverage Report

Created: 2026-01-10 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/image-webp-0.2.4/src/yuv.rs
Line
Count
Source
1
//! Utilities for doing the YUV -> RGB conversion
2
//! The images are encoded in the Y'CbCr format as detailed here: <https://en.wikipedia.org/wiki/YCbCr>
3
//! so need to be converted to RGB to be displayed
4
//! To do the YUV -> RGB conversion we need to first decide how to map the yuv values to the pixels
5
//! The y buffer is the same size as the pixel buffer so that maps 1-1 but the
6
//! u and v buffers are half the size of the pixel buffer so we need to scale it up
7
//! The simple way to upscale is just to take each u/v value and associate it with the 4
8
//! pixels around it e.g. for a 4x4 image:
9
//!
10
//! ||||||
11
//! |yyyy|
12
//! |yyyy|
13
//! |yyyy|
14
//! |yyyy|
15
//! ||||||
16
//!
17
//! |||||||
18
//! |uu|vv|
19
//! |uu|vv|
20
//! |||||||
21
//!
22
//! Then each of the 2x2 pixels would match the u/v from the same quadrant
23
//!
24
//! However fancy upsampling is the default for libwebp which does a little more work to make the values smoother
25
//! It interpolates u and v so that for e.g. the pixel 1 down and 1 from the left the u value
26
//! would be (9*u0 + 3*u1 + 3*u2 + u3 + 8) / 16 and similar for the other pixels
27
//! The edges are mirrored, so for the pixel 1 down and 0 from the left it uses (9*u0 + 3*u2 + 3*u0 + u2 + 8) / 16
28
29
/// `_mm_mulhi_epu16` emulation
30
1.93G
fn mulhi(v: u8, coeff: u16) -> i32 {
31
1.93G
    ((u32::from(v) * u32::from(coeff)) >> 8) as i32
32
1.93G
}
33
34
/// This function has been rewritten to encourage auto-vectorization.
35
///
36
/// Based on [src/dsp/yuv.h](https://github.com/webmproject/libwebp/blob/8534f53960befac04c9631e6e50d21dcb42dfeaf/src/dsp/yuv.h#L79)
37
/// from the libwebp source.
38
/// ```text
39
/// const YUV_FIX2: i32 = 6;
40
/// const YUV_MASK2: i32 = (256 << YUV_FIX2) - 1;
41
/// fn clip(v: i32) -> u8 {
42
///     if (v & !YUV_MASK2) == 0 {
43
///         (v >> YUV_FIX2) as u8
44
///     } else if v < 0 {
45
///         0
46
///     } else {
47
///         255
48
///     }
49
/// }
50
/// ```
51
// Clippy suggests the clamp method, but it seems to optimize worse as of rustc 1.82.0 nightly.
52
#[allow(clippy::manual_clamp)]
53
830M
fn clip(v: i32) -> u8 {
54
    const YUV_FIX2: i32 = 6;
55
830M
    (v >> YUV_FIX2).max(0).min(255) as u8
56
830M
}
57
58
#[inline(always)]
59
276M
fn yuv_to_r(y: u8, v: u8) -> u8 {
60
276M
    clip(mulhi(y, 19077) + mulhi(v, 26149) - 14234)
61
276M
}
62
63
#[inline(always)]
64
276M
fn yuv_to_g(y: u8, u: u8, v: u8) -> u8 {
65
276M
    clip(mulhi(y, 19077) - mulhi(u, 6419) - mulhi(v, 13320) + 8708)
66
276M
}
67
68
#[inline(always)]
69
276M
fn yuv_to_b(y: u8, u: u8) -> u8 {
70
276M
    clip(mulhi(y, 19077) + mulhi(u, 33050) - 17685)
71
276M
}
72
73
/// Fills an rgb buffer with the image from the yuv buffers
74
/// Size of the buffer is assumed to be correct
75
/// BPP is short for bytes per pixel, allows both rgb and rgba to be decoded
76
704
pub(crate) fn fill_rgb_buffer_fancy<const BPP: usize>(
77
704
    buffer: &mut [u8],
78
704
    y_buffer: &[u8],
79
704
    u_buffer: &[u8],
80
704
    v_buffer: &[u8],
81
704
    width: usize,
82
704
    height: usize,
83
704
    buffer_width: usize,
84
704
) {
85
    // buffer width is always even so don't need to do div_ceil
86
704
    let chroma_buffer_width = buffer_width / 2;
87
704
    let chroma_width = width.div_ceil(2);
88
89
    // fill top row first since it only uses the top u/v row
90
704
    let top_row_y = &y_buffer[..width];
91
704
    let top_row_u = &u_buffer[..chroma_width];
92
704
    let top_row_v = &v_buffer[..chroma_width];
93
704
    let top_row_buffer = &mut buffer[..width * BPP];
94
704
    fill_row_fancy_with_1_uv_row::<BPP>(top_row_buffer, top_row_y, top_row_u, top_row_v);
95
96
704
    let mut main_row_chunks = buffer[width * BPP..].chunks_exact_mut(width * BPP * 2);
97
    // the y buffer iterator limits the end of the row iterator so we need this end index
98
704
    let end_y_index = height * buffer_width;
99
704
    let mut main_y_chunks = y_buffer[buffer_width..end_y_index].chunks_exact(buffer_width * 2);
100
704
    let mut main_u_windows = u_buffer
101
704
        .windows(chroma_buffer_width * 2)
102
704
        .step_by(chroma_buffer_width);
103
704
    let mut main_v_windows = v_buffer
104
704
        .windows(chroma_buffer_width * 2)
105
704
        .step_by(chroma_buffer_width);
106
107
231k
    for (((row_buffer, y_rows), u_rows), v_rows) in (&mut main_row_chunks)
108
704
        .zip(&mut main_y_chunks)
109
704
        .zip(&mut main_u_windows)
110
704
        .zip(&mut main_v_windows)
111
231k
    {
112
231k
        let (u_row_1, u_row_2) = u_rows.split_at(chroma_buffer_width);
113
231k
        let (v_row_1, v_row_2) = v_rows.split_at(chroma_buffer_width);
114
231k
        let (row_buf_1, row_buf_2) = row_buffer.split_at_mut(width * BPP);
115
231k
        let (y_row_1, y_row_2) = y_rows.split_at(buffer_width);
116
231k
        fill_row_fancy_with_2_uv_rows::<BPP>(
117
231k
            row_buf_1,
118
231k
            &y_row_1[..width],
119
231k
            &u_row_1[..chroma_width],
120
231k
            &u_row_2[..chroma_width],
121
231k
            &v_row_1[..chroma_width],
122
231k
            &v_row_2[..chroma_width],
123
231k
        );
124
231k
        fill_row_fancy_with_2_uv_rows::<BPP>(
125
231k
            row_buf_2,
126
231k
            &y_row_2[..width],
127
231k
            &u_row_2[..chroma_width],
128
231k
            &u_row_1[..chroma_width],
129
231k
            &v_row_2[..chroma_width],
130
231k
            &v_row_1[..chroma_width],
131
231k
        );
132
231k
    }
133
134
704
    let final_row_buffer = main_row_chunks.into_remainder();
135
136
    // if the image has even height there will be one final row with only one u/v row matching it
137
704
    if !final_row_buffer.is_empty() {
138
479
        let final_y_row = main_y_chunks.remainder();
139
479
140
479
        let chroma_height = height.div_ceil(2);
141
479
        let start_chroma_index = (chroma_height - 1) * chroma_buffer_width;
142
479
143
479
        let final_u_row = &u_buffer[start_chroma_index..];
144
479
        let final_v_row = &v_buffer[start_chroma_index..];
145
479
        fill_row_fancy_with_1_uv_row::<BPP>(
146
479
            final_row_buffer,
147
479
            &final_y_row[..width],
148
479
            &final_u_row[..chroma_width],
149
479
            &final_v_row[..chroma_width],
150
479
        );
151
479
    }
152
704
}
image_webp::yuv::fill_rgb_buffer_fancy::<3>
Line
Count
Source
76
274
pub(crate) fn fill_rgb_buffer_fancy<const BPP: usize>(
77
274
    buffer: &mut [u8],
78
274
    y_buffer: &[u8],
79
274
    u_buffer: &[u8],
80
274
    v_buffer: &[u8],
81
274
    width: usize,
82
274
    height: usize,
83
274
    buffer_width: usize,
84
274
) {
85
    // buffer width is always even so don't need to do div_ceil
86
274
    let chroma_buffer_width = buffer_width / 2;
87
274
    let chroma_width = width.div_ceil(2);
88
89
    // fill top row first since it only uses the top u/v row
90
274
    let top_row_y = &y_buffer[..width];
91
274
    let top_row_u = &u_buffer[..chroma_width];
92
274
    let top_row_v = &v_buffer[..chroma_width];
93
274
    let top_row_buffer = &mut buffer[..width * BPP];
94
274
    fill_row_fancy_with_1_uv_row::<BPP>(top_row_buffer, top_row_y, top_row_u, top_row_v);
95
96
274
    let mut main_row_chunks = buffer[width * BPP..].chunks_exact_mut(width * BPP * 2);
97
    // the y buffer iterator limits the end of the row iterator so we need this end index
98
274
    let end_y_index = height * buffer_width;
99
274
    let mut main_y_chunks = y_buffer[buffer_width..end_y_index].chunks_exact(buffer_width * 2);
100
274
    let mut main_u_windows = u_buffer
101
274
        .windows(chroma_buffer_width * 2)
102
274
        .step_by(chroma_buffer_width);
103
274
    let mut main_v_windows = v_buffer
104
274
        .windows(chroma_buffer_width * 2)
105
274
        .step_by(chroma_buffer_width);
106
107
210k
    for (((row_buffer, y_rows), u_rows), v_rows) in (&mut main_row_chunks)
108
274
        .zip(&mut main_y_chunks)
109
274
        .zip(&mut main_u_windows)
110
274
        .zip(&mut main_v_windows)
111
210k
    {
112
210k
        let (u_row_1, u_row_2) = u_rows.split_at(chroma_buffer_width);
113
210k
        let (v_row_1, v_row_2) = v_rows.split_at(chroma_buffer_width);
114
210k
        let (row_buf_1, row_buf_2) = row_buffer.split_at_mut(width * BPP);
115
210k
        let (y_row_1, y_row_2) = y_rows.split_at(buffer_width);
116
210k
        fill_row_fancy_with_2_uv_rows::<BPP>(
117
210k
            row_buf_1,
118
210k
            &y_row_1[..width],
119
210k
            &u_row_1[..chroma_width],
120
210k
            &u_row_2[..chroma_width],
121
210k
            &v_row_1[..chroma_width],
122
210k
            &v_row_2[..chroma_width],
123
210k
        );
124
210k
        fill_row_fancy_with_2_uv_rows::<BPP>(
125
210k
            row_buf_2,
126
210k
            &y_row_2[..width],
127
210k
            &u_row_2[..chroma_width],
128
210k
            &u_row_1[..chroma_width],
129
210k
            &v_row_2[..chroma_width],
130
210k
            &v_row_1[..chroma_width],
131
210k
        );
132
210k
    }
133
134
274
    let final_row_buffer = main_row_chunks.into_remainder();
135
136
    // if the image has even height there will be one final row with only one u/v row matching it
137
274
    if !final_row_buffer.is_empty() {
138
128
        let final_y_row = main_y_chunks.remainder();
139
128
140
128
        let chroma_height = height.div_ceil(2);
141
128
        let start_chroma_index = (chroma_height - 1) * chroma_buffer_width;
142
128
143
128
        let final_u_row = &u_buffer[start_chroma_index..];
144
128
        let final_v_row = &v_buffer[start_chroma_index..];
145
128
        fill_row_fancy_with_1_uv_row::<BPP>(
146
128
            final_row_buffer,
147
128
            &final_y_row[..width],
148
128
            &final_u_row[..chroma_width],
149
128
            &final_v_row[..chroma_width],
150
128
        );
151
146
    }
152
274
}
image_webp::yuv::fill_rgb_buffer_fancy::<4>
Line
Count
Source
76
430
pub(crate) fn fill_rgb_buffer_fancy<const BPP: usize>(
77
430
    buffer: &mut [u8],
78
430
    y_buffer: &[u8],
79
430
    u_buffer: &[u8],
80
430
    v_buffer: &[u8],
81
430
    width: usize,
82
430
    height: usize,
83
430
    buffer_width: usize,
84
430
) {
85
    // buffer width is always even so don't need to do div_ceil
86
430
    let chroma_buffer_width = buffer_width / 2;
87
430
    let chroma_width = width.div_ceil(2);
88
89
    // fill top row first since it only uses the top u/v row
90
430
    let top_row_y = &y_buffer[..width];
91
430
    let top_row_u = &u_buffer[..chroma_width];
92
430
    let top_row_v = &v_buffer[..chroma_width];
93
430
    let top_row_buffer = &mut buffer[..width * BPP];
94
430
    fill_row_fancy_with_1_uv_row::<BPP>(top_row_buffer, top_row_y, top_row_u, top_row_v);
95
96
430
    let mut main_row_chunks = buffer[width * BPP..].chunks_exact_mut(width * BPP * 2);
97
    // the y buffer iterator limits the end of the row iterator so we need this end index
98
430
    let end_y_index = height * buffer_width;
99
430
    let mut main_y_chunks = y_buffer[buffer_width..end_y_index].chunks_exact(buffer_width * 2);
100
430
    let mut main_u_windows = u_buffer
101
430
        .windows(chroma_buffer_width * 2)
102
430
        .step_by(chroma_buffer_width);
103
430
    let mut main_v_windows = v_buffer
104
430
        .windows(chroma_buffer_width * 2)
105
430
        .step_by(chroma_buffer_width);
106
107
21.0k
    for (((row_buffer, y_rows), u_rows), v_rows) in (&mut main_row_chunks)
108
430
        .zip(&mut main_y_chunks)
109
430
        .zip(&mut main_u_windows)
110
430
        .zip(&mut main_v_windows)
111
21.0k
    {
112
21.0k
        let (u_row_1, u_row_2) = u_rows.split_at(chroma_buffer_width);
113
21.0k
        let (v_row_1, v_row_2) = v_rows.split_at(chroma_buffer_width);
114
21.0k
        let (row_buf_1, row_buf_2) = row_buffer.split_at_mut(width * BPP);
115
21.0k
        let (y_row_1, y_row_2) = y_rows.split_at(buffer_width);
116
21.0k
        fill_row_fancy_with_2_uv_rows::<BPP>(
117
21.0k
            row_buf_1,
118
21.0k
            &y_row_1[..width],
119
21.0k
            &u_row_1[..chroma_width],
120
21.0k
            &u_row_2[..chroma_width],
121
21.0k
            &v_row_1[..chroma_width],
122
21.0k
            &v_row_2[..chroma_width],
123
21.0k
        );
124
21.0k
        fill_row_fancy_with_2_uv_rows::<BPP>(
125
21.0k
            row_buf_2,
126
21.0k
            &y_row_2[..width],
127
21.0k
            &u_row_2[..chroma_width],
128
21.0k
            &u_row_1[..chroma_width],
129
21.0k
            &v_row_2[..chroma_width],
130
21.0k
            &v_row_1[..chroma_width],
131
21.0k
        );
132
21.0k
    }
133
134
430
    let final_row_buffer = main_row_chunks.into_remainder();
135
136
    // if the image has even height there will be one final row with only one u/v row matching it
137
430
    if !final_row_buffer.is_empty() {
138
351
        let final_y_row = main_y_chunks.remainder();
139
351
140
351
        let chroma_height = height.div_ceil(2);
141
351
        let start_chroma_index = (chroma_height - 1) * chroma_buffer_width;
142
351
143
351
        let final_u_row = &u_buffer[start_chroma_index..];
144
351
        let final_v_row = &v_buffer[start_chroma_index..];
145
351
        fill_row_fancy_with_1_uv_row::<BPP>(
146
351
            final_row_buffer,
147
351
            &final_y_row[..width],
148
351
            &final_u_row[..chroma_width],
149
351
            &final_v_row[..chroma_width],
150
351
        );
151
351
    }
152
430
}
153
154
/// Fills a row with the fancy interpolation as detailed
155
463k
fn fill_row_fancy_with_2_uv_rows<const BPP: usize>(
156
463k
    row_buffer: &mut [u8],
157
463k
    y_row: &[u8],
158
463k
    u_row_1: &[u8],
159
463k
    u_row_2: &[u8],
160
463k
    v_row_1: &[u8],
161
463k
    v_row_2: &[u8],
162
463k
) {
163
    // need to do left pixel separately since it will only have one u/v value
164
463k
    {
165
463k
        let rgb1 = &mut row_buffer[0..3];
166
463k
        let y_value = y_row[0];
167
463k
        // first pixel uses the first u/v as the main one
168
463k
        let u_value = get_fancy_chroma_value(u_row_1[0], u_row_1[0], u_row_2[0], u_row_2[0]);
169
463k
        let v_value = get_fancy_chroma_value(v_row_1[0], v_row_1[0], v_row_2[0], v_row_2[0]);
170
463k
        set_pixel(rgb1, y_value, u_value, v_value);
171
463k
    }
172
173
463k
    let rest_row_buffer = &mut row_buffer[BPP..];
174
463k
    let rest_y_row = &y_row[1..];
175
176
    // we do two pixels at a time since they share the same u/v values
177
463k
    let mut main_row_chunks = rest_row_buffer.chunks_exact_mut(BPP * 2);
178
463k
    let mut main_y_chunks = rest_y_row.chunks_exact(2);
179
180
137M
    for (((((rgb, y_val), u_val_1), u_val_2), v_val_1), v_val_2) in (&mut main_row_chunks)
181
463k
        .zip(&mut main_y_chunks)
182
463k
        .zip(u_row_1.windows(2))
183
463k
        .zip(u_row_2.windows(2))
184
463k
        .zip(v_row_1.windows(2))
185
463k
        .zip(v_row_2.windows(2))
186
    {
187
137M
        {
188
137M
            let rgb1 = &mut rgb[0..3];
189
137M
            let y_value = y_val[0];
190
137M
            // first pixel uses the first u/v as the main one
191
137M
            let u_value = get_fancy_chroma_value(u_val_1[0], u_val_1[1], u_val_2[0], u_val_2[1]);
192
137M
            let v_value = get_fancy_chroma_value(v_val_1[0], v_val_1[1], v_val_2[0], v_val_2[1]);
193
137M
            set_pixel(rgb1, y_value, u_value, v_value);
194
137M
        }
195
137M
        {
196
137M
            let rgb2 = &mut rgb[BPP..];
197
137M
            let y_value = y_val[1];
198
137M
            let u_value = get_fancy_chroma_value(u_val_1[1], u_val_1[0], u_val_2[1], u_val_2[0]);
199
137M
            let v_value = get_fancy_chroma_value(v_val_1[1], v_val_1[0], v_val_2[1], v_val_2[0]);
200
137M
            set_pixel(rgb2, y_value, u_value, v_value);
201
137M
        }
202
    }
203
204
463k
    let final_pixel = main_row_chunks.into_remainder();
205
463k
    let final_y = main_y_chunks.remainder();
206
207
463k
    if let (rgb, [y_value]) = (final_pixel, final_y) {
208
176k
        let final_u_1 = *u_row_1.last().unwrap();
209
176k
        let final_u_2 = *u_row_2.last().unwrap();
210
176k
211
176k
        let final_v_1 = *v_row_1.last().unwrap();
212
176k
        let final_v_2 = *v_row_2.last().unwrap();
213
176k
214
176k
        let rgb1 = &mut rgb[0..3];
215
176k
        // first pixel uses the first u/v as the main one
216
176k
        let u_value = get_fancy_chroma_value(final_u_1, final_u_1, final_u_2, final_u_2);
217
176k
        let v_value = get_fancy_chroma_value(final_v_1, final_v_1, final_v_2, final_v_2);
218
176k
        set_pixel(rgb1, *y_value, u_value, v_value);
219
287k
    }
220
463k
}
image_webp::yuv::fill_row_fancy_with_2_uv_rows::<3>
Line
Count
Source
155
421k
fn fill_row_fancy_with_2_uv_rows<const BPP: usize>(
156
421k
    row_buffer: &mut [u8],
157
421k
    y_row: &[u8],
158
421k
    u_row_1: &[u8],
159
421k
    u_row_2: &[u8],
160
421k
    v_row_1: &[u8],
161
421k
    v_row_2: &[u8],
162
421k
) {
163
    // need to do left pixel separately since it will only have one u/v value
164
421k
    {
165
421k
        let rgb1 = &mut row_buffer[0..3];
166
421k
        let y_value = y_row[0];
167
421k
        // first pixel uses the first u/v as the main one
168
421k
        let u_value = get_fancy_chroma_value(u_row_1[0], u_row_1[0], u_row_2[0], u_row_2[0]);
169
421k
        let v_value = get_fancy_chroma_value(v_row_1[0], v_row_1[0], v_row_2[0], v_row_2[0]);
170
421k
        set_pixel(rgb1, y_value, u_value, v_value);
171
421k
    }
172
173
421k
    let rest_row_buffer = &mut row_buffer[BPP..];
174
421k
    let rest_y_row = &y_row[1..];
175
176
    // we do two pixels at a time since they share the same u/v values
177
421k
    let mut main_row_chunks = rest_row_buffer.chunks_exact_mut(BPP * 2);
178
421k
    let mut main_y_chunks = rest_y_row.chunks_exact(2);
179
180
132M
    for (((((rgb, y_val), u_val_1), u_val_2), v_val_1), v_val_2) in (&mut main_row_chunks)
181
421k
        .zip(&mut main_y_chunks)
182
421k
        .zip(u_row_1.windows(2))
183
421k
        .zip(u_row_2.windows(2))
184
421k
        .zip(v_row_1.windows(2))
185
421k
        .zip(v_row_2.windows(2))
186
    {
187
132M
        {
188
132M
            let rgb1 = &mut rgb[0..3];
189
132M
            let y_value = y_val[0];
190
132M
            // first pixel uses the first u/v as the main one
191
132M
            let u_value = get_fancy_chroma_value(u_val_1[0], u_val_1[1], u_val_2[0], u_val_2[1]);
192
132M
            let v_value = get_fancy_chroma_value(v_val_1[0], v_val_1[1], v_val_2[0], v_val_2[1]);
193
132M
            set_pixel(rgb1, y_value, u_value, v_value);
194
132M
        }
195
132M
        {
196
132M
            let rgb2 = &mut rgb[BPP..];
197
132M
            let y_value = y_val[1];
198
132M
            let u_value = get_fancy_chroma_value(u_val_1[1], u_val_1[0], u_val_2[1], u_val_2[0]);
199
132M
            let v_value = get_fancy_chroma_value(v_val_1[1], v_val_1[0], v_val_2[1], v_val_2[0]);
200
132M
            set_pixel(rgb2, y_value, u_value, v_value);
201
132M
        }
202
    }
203
204
421k
    let final_pixel = main_row_chunks.into_remainder();
205
421k
    let final_y = main_y_chunks.remainder();
206
207
421k
    if let (rgb, [y_value]) = (final_pixel, final_y) {
208
136k
        let final_u_1 = *u_row_1.last().unwrap();
209
136k
        let final_u_2 = *u_row_2.last().unwrap();
210
136k
211
136k
        let final_v_1 = *v_row_1.last().unwrap();
212
136k
        let final_v_2 = *v_row_2.last().unwrap();
213
136k
214
136k
        let rgb1 = &mut rgb[0..3];
215
136k
        // first pixel uses the first u/v as the main one
216
136k
        let u_value = get_fancy_chroma_value(final_u_1, final_u_1, final_u_2, final_u_2);
217
136k
        let v_value = get_fancy_chroma_value(final_v_1, final_v_1, final_v_2, final_v_2);
218
136k
        set_pixel(rgb1, *y_value, u_value, v_value);
219
285k
    }
220
421k
}
image_webp::yuv::fill_row_fancy_with_2_uv_rows::<4>
Line
Count
Source
155
42.0k
fn fill_row_fancy_with_2_uv_rows<const BPP: usize>(
156
42.0k
    row_buffer: &mut [u8],
157
42.0k
    y_row: &[u8],
158
42.0k
    u_row_1: &[u8],
159
42.0k
    u_row_2: &[u8],
160
42.0k
    v_row_1: &[u8],
161
42.0k
    v_row_2: &[u8],
162
42.0k
) {
163
    // need to do left pixel separately since it will only have one u/v value
164
42.0k
    {
165
42.0k
        let rgb1 = &mut row_buffer[0..3];
166
42.0k
        let y_value = y_row[0];
167
42.0k
        // first pixel uses the first u/v as the main one
168
42.0k
        let u_value = get_fancy_chroma_value(u_row_1[0], u_row_1[0], u_row_2[0], u_row_2[0]);
169
42.0k
        let v_value = get_fancy_chroma_value(v_row_1[0], v_row_1[0], v_row_2[0], v_row_2[0]);
170
42.0k
        set_pixel(rgb1, y_value, u_value, v_value);
171
42.0k
    }
172
173
42.0k
    let rest_row_buffer = &mut row_buffer[BPP..];
174
42.0k
    let rest_y_row = &y_row[1..];
175
176
    // we do two pixels at a time since they share the same u/v values
177
42.0k
    let mut main_row_chunks = rest_row_buffer.chunks_exact_mut(BPP * 2);
178
42.0k
    let mut main_y_chunks = rest_y_row.chunks_exact(2);
179
180
5.19M
    for (((((rgb, y_val), u_val_1), u_val_2), v_val_1), v_val_2) in (&mut main_row_chunks)
181
42.0k
        .zip(&mut main_y_chunks)
182
42.0k
        .zip(u_row_1.windows(2))
183
42.0k
        .zip(u_row_2.windows(2))
184
42.0k
        .zip(v_row_1.windows(2))
185
42.0k
        .zip(v_row_2.windows(2))
186
    {
187
5.19M
        {
188
5.19M
            let rgb1 = &mut rgb[0..3];
189
5.19M
            let y_value = y_val[0];
190
5.19M
            // first pixel uses the first u/v as the main one
191
5.19M
            let u_value = get_fancy_chroma_value(u_val_1[0], u_val_1[1], u_val_2[0], u_val_2[1]);
192
5.19M
            let v_value = get_fancy_chroma_value(v_val_1[0], v_val_1[1], v_val_2[0], v_val_2[1]);
193
5.19M
            set_pixel(rgb1, y_value, u_value, v_value);
194
5.19M
        }
195
5.19M
        {
196
5.19M
            let rgb2 = &mut rgb[BPP..];
197
5.19M
            let y_value = y_val[1];
198
5.19M
            let u_value = get_fancy_chroma_value(u_val_1[1], u_val_1[0], u_val_2[1], u_val_2[0]);
199
5.19M
            let v_value = get_fancy_chroma_value(v_val_1[1], v_val_1[0], v_val_2[1], v_val_2[0]);
200
5.19M
            set_pixel(rgb2, y_value, u_value, v_value);
201
5.19M
        }
202
    }
203
204
42.0k
    let final_pixel = main_row_chunks.into_remainder();
205
42.0k
    let final_y = main_y_chunks.remainder();
206
207
42.0k
    if let (rgb, [y_value]) = (final_pixel, final_y) {
208
39.9k
        let final_u_1 = *u_row_1.last().unwrap();
209
39.9k
        let final_u_2 = *u_row_2.last().unwrap();
210
39.9k
211
39.9k
        let final_v_1 = *v_row_1.last().unwrap();
212
39.9k
        let final_v_2 = *v_row_2.last().unwrap();
213
39.9k
214
39.9k
        let rgb1 = &mut rgb[0..3];
215
39.9k
        // first pixel uses the first u/v as the main one
216
39.9k
        let u_value = get_fancy_chroma_value(final_u_1, final_u_1, final_u_2, final_u_2);
217
39.9k
        let v_value = get_fancy_chroma_value(final_v_1, final_v_1, final_v_2, final_v_2);
218
39.9k
        set_pixel(rgb1, *y_value, u_value, v_value);
219
39.9k
    }
220
42.0k
}
221
222
1.18k
fn fill_row_fancy_with_1_uv_row<const BPP: usize>(
223
1.18k
    row_buffer: &mut [u8],
224
1.18k
    y_row: &[u8],
225
1.18k
    u_row: &[u8],
226
1.18k
    v_row: &[u8],
227
1.18k
) {
228
    // doing left pixel first
229
1.18k
    {
230
1.18k
        let rgb1 = &mut row_buffer[0..3];
231
1.18k
        let y_value = y_row[0];
232
1.18k
233
1.18k
        let u_value = u_row[0];
234
1.18k
        let v_value = v_row[0];
235
1.18k
        set_pixel(rgb1, y_value, u_value, v_value);
236
1.18k
    }
237
238
    // two pixels at a time since they share the same u/v value
239
1.18k
    let mut main_row_chunks = row_buffer[BPP..].chunks_exact_mut(BPP * 2);
240
1.18k
    let mut main_y_row_chunks = y_row[1..].chunks_exact(2);
241
242
506k
    for (((rgb, y_val), u_val), v_val) in (&mut main_row_chunks)
243
1.18k
        .zip(&mut main_y_row_chunks)
244
1.18k
        .zip(u_row.windows(2))
245
1.18k
        .zip(v_row.windows(2))
246
    {
247
506k
        {
248
506k
            let rgb1 = &mut rgb[0..3];
249
506k
            let y_value = y_val[0];
250
506k
            // first pixel uses the first u/v as the main one
251
506k
            let u_value = get_fancy_chroma_value(u_val[0], u_val[1], u_val[0], u_val[1]);
252
506k
            let v_value = get_fancy_chroma_value(v_val[0], v_val[1], v_val[0], v_val[1]);
253
506k
            set_pixel(rgb1, y_value, u_value, v_value);
254
506k
        }
255
506k
        {
256
506k
            let rgb2 = &mut rgb[BPP..];
257
506k
            let y_value = y_val[1];
258
506k
            let u_value = get_fancy_chroma_value(u_val[1], u_val[0], u_val[1], u_val[0]);
259
506k
            let v_value = get_fancy_chroma_value(v_val[1], v_val[0], v_val[1], v_val[0]);
260
506k
            set_pixel(rgb2, y_value, u_value, v_value);
261
506k
        }
262
    }
263
264
1.18k
    let final_pixel = main_row_chunks.into_remainder();
265
1.18k
    let final_y = main_y_row_chunks.remainder();
266
267
1.18k
    if let (rgb, [final_y]) = (final_pixel, final_y) {
268
933
        let final_u = *u_row.last().unwrap();
269
933
        let final_v = *v_row.last().unwrap();
270
933
271
933
        set_pixel(rgb, *final_y, final_u, final_v);
272
933
    }
273
1.18k
}
image_webp::yuv::fill_row_fancy_with_1_uv_row::<3>
Line
Count
Source
222
402
fn fill_row_fancy_with_1_uv_row<const BPP: usize>(
223
402
    row_buffer: &mut [u8],
224
402
    y_row: &[u8],
225
402
    u_row: &[u8],
226
402
    v_row: &[u8],
227
402
) {
228
    // doing left pixel first
229
402
    {
230
402
        let rgb1 = &mut row_buffer[0..3];
231
402
        let y_value = y_row[0];
232
402
233
402
        let u_value = u_row[0];
234
402
        let v_value = v_row[0];
235
402
        set_pixel(rgb1, y_value, u_value, v_value);
236
402
    }
237
238
    // two pixels at a time since they share the same u/v value
239
402
    let mut main_row_chunks = row_buffer[BPP..].chunks_exact_mut(BPP * 2);
240
402
    let mut main_y_row_chunks = y_row[1..].chunks_exact(2);
241
242
460k
    for (((rgb, y_val), u_val), v_val) in (&mut main_row_chunks)
243
402
        .zip(&mut main_y_row_chunks)
244
402
        .zip(u_row.windows(2))
245
402
        .zip(v_row.windows(2))
246
    {
247
460k
        {
248
460k
            let rgb1 = &mut rgb[0..3];
249
460k
            let y_value = y_val[0];
250
460k
            // first pixel uses the first u/v as the main one
251
460k
            let u_value = get_fancy_chroma_value(u_val[0], u_val[1], u_val[0], u_val[1]);
252
460k
            let v_value = get_fancy_chroma_value(v_val[0], v_val[1], v_val[0], v_val[1]);
253
460k
            set_pixel(rgb1, y_value, u_value, v_value);
254
460k
        }
255
460k
        {
256
460k
            let rgb2 = &mut rgb[BPP..];
257
460k
            let y_value = y_val[1];
258
460k
            let u_value = get_fancy_chroma_value(u_val[1], u_val[0], u_val[1], u_val[0]);
259
460k
            let v_value = get_fancy_chroma_value(v_val[1], v_val[0], v_val[1], v_val[0]);
260
460k
            set_pixel(rgb2, y_value, u_value, v_value);
261
460k
        }
262
    }
263
264
402
    let final_pixel = main_row_chunks.into_remainder();
265
402
    let final_y = main_y_row_chunks.remainder();
266
267
402
    if let (rgb, [final_y]) = (final_pixel, final_y) {
268
186
        let final_u = *u_row.last().unwrap();
269
186
        let final_v = *v_row.last().unwrap();
270
186
271
186
        set_pixel(rgb, *final_y, final_u, final_v);
272
216
    }
273
402
}
image_webp::yuv::fill_row_fancy_with_1_uv_row::<4>
Line
Count
Source
222
781
fn fill_row_fancy_with_1_uv_row<const BPP: usize>(
223
781
    row_buffer: &mut [u8],
224
781
    y_row: &[u8],
225
781
    u_row: &[u8],
226
781
    v_row: &[u8],
227
781
) {
228
    // doing left pixel first
229
781
    {
230
781
        let rgb1 = &mut row_buffer[0..3];
231
781
        let y_value = y_row[0];
232
781
233
781
        let u_value = u_row[0];
234
781
        let v_value = v_row[0];
235
781
        set_pixel(rgb1, y_value, u_value, v_value);
236
781
    }
237
238
    // two pixels at a time since they share the same u/v value
239
781
    let mut main_row_chunks = row_buffer[BPP..].chunks_exact_mut(BPP * 2);
240
781
    let mut main_y_row_chunks = y_row[1..].chunks_exact(2);
241
242
46.3k
    for (((rgb, y_val), u_val), v_val) in (&mut main_row_chunks)
243
781
        .zip(&mut main_y_row_chunks)
244
781
        .zip(u_row.windows(2))
245
781
        .zip(v_row.windows(2))
246
    {
247
46.3k
        {
248
46.3k
            let rgb1 = &mut rgb[0..3];
249
46.3k
            let y_value = y_val[0];
250
46.3k
            // first pixel uses the first u/v as the main one
251
46.3k
            let u_value = get_fancy_chroma_value(u_val[0], u_val[1], u_val[0], u_val[1]);
252
46.3k
            let v_value = get_fancy_chroma_value(v_val[0], v_val[1], v_val[0], v_val[1]);
253
46.3k
            set_pixel(rgb1, y_value, u_value, v_value);
254
46.3k
        }
255
46.3k
        {
256
46.3k
            let rgb2 = &mut rgb[BPP..];
257
46.3k
            let y_value = y_val[1];
258
46.3k
            let u_value = get_fancy_chroma_value(u_val[1], u_val[0], u_val[1], u_val[0]);
259
46.3k
            let v_value = get_fancy_chroma_value(v_val[1], v_val[0], v_val[1], v_val[0]);
260
46.3k
            set_pixel(rgb2, y_value, u_value, v_value);
261
46.3k
        }
262
    }
263
264
781
    let final_pixel = main_row_chunks.into_remainder();
265
781
    let final_y = main_y_row_chunks.remainder();
266
267
781
    if let (rgb, [final_y]) = (final_pixel, final_y) {
268
747
        let final_u = *u_row.last().unwrap();
269
747
        let final_v = *v_row.last().unwrap();
270
747
271
747
        set_pixel(rgb, *final_y, final_u, final_v);
272
747
    }
273
781
}
274
275
#[inline]
276
553M
fn get_fancy_chroma_value(main: u8, secondary1: u8, secondary2: u8, tertiary: u8) -> u8 {
277
553M
    let val0 = u16::from(main);
278
553M
    let val1 = u16::from(secondary1);
279
553M
    let val2 = u16::from(secondary2);
280
553M
    let val3 = u16::from(tertiary);
281
553M
    ((9 * val0 + 3 * val1 + 3 * val2 + val3 + 8) / 16) as u8
282
553M
}
283
284
#[inline]
285
276M
fn set_pixel(rgb: &mut [u8], y: u8, u: u8, v: u8) {
286
276M
    rgb[0] = yuv_to_r(y, v);
287
276M
    rgb[1] = yuv_to_g(y, u, v);
288
276M
    rgb[2] = yuv_to_b(y, u);
289
276M
}
290
291
/// Simple conversion, not currently used but could add a config to allow for using the simple
292
#[allow(unused)]
293
0
pub(crate) fn fill_rgb_buffer_simple<const BPP: usize>(
294
0
    buffer: &mut [u8],
295
0
    y_buffer: &[u8],
296
0
    u_buffer: &[u8],
297
0
    v_buffer: &[u8],
298
0
    width: usize,
299
0
    chroma_width: usize,
300
0
    buffer_width: usize,
301
0
) {
302
0
    let u_row_twice_iter = u_buffer
303
0
        .chunks_exact(buffer_width / 2)
304
0
        .flat_map(|n| std::iter::repeat(n).take(2));
Unexecuted instantiation: image_webp::yuv::fill_rgb_buffer_simple::<3>::{closure#0}
Unexecuted instantiation: image_webp::yuv::fill_rgb_buffer_simple::<4>::{closure#0}
305
0
    let v_row_twice_iter = v_buffer
306
0
        .chunks_exact(buffer_width / 2)
307
0
        .flat_map(|n| std::iter::repeat(n).take(2));
Unexecuted instantiation: image_webp::yuv::fill_rgb_buffer_simple::<3>::{closure#1}
Unexecuted instantiation: image_webp::yuv::fill_rgb_buffer_simple::<4>::{closure#1}
308
309
0
    for (((row, y_row), u_row), v_row) in buffer
310
0
        .chunks_exact_mut(width * BPP)
311
0
        .zip(y_buffer.chunks_exact(buffer_width))
312
0
        .zip(u_row_twice_iter)
313
0
        .zip(v_row_twice_iter)
314
0
    {
315
0
        fill_rgba_row_simple::<BPP>(
316
0
            &y_row[..width],
317
0
            &u_row[..chroma_width],
318
0
            &v_row[..chroma_width],
319
0
            row,
320
0
        );
321
0
    }
322
0
}
Unexecuted instantiation: image_webp::yuv::fill_rgb_buffer_simple::<3>
Unexecuted instantiation: image_webp::yuv::fill_rgb_buffer_simple::<4>
323
324
0
fn fill_rgba_row_simple<const BPP: usize>(
325
0
    y_vec: &[u8],
326
0
    u_vec: &[u8],
327
0
    v_vec: &[u8],
328
0
    rgba: &mut [u8],
329
0
) {
330
    // Fill 2 pixels per iteration: these pixels share `u` and `v` components
331
0
    let mut rgb_chunks = rgba.chunks_exact_mut(BPP * 2);
332
0
    let mut y_chunks = y_vec.chunks_exact(2);
333
0
    let mut u_iter = u_vec.iter();
334
0
    let mut v_iter = v_vec.iter();
335
336
0
    for (((rgb, y), &u), &v) in (&mut rgb_chunks)
337
0
        .zip(&mut y_chunks)
338
0
        .zip(&mut u_iter)
339
0
        .zip(&mut v_iter)
340
    {
341
0
        let coeffs = [
342
0
            mulhi(v, 26149),
343
0
            mulhi(u, 6419),
344
0
            mulhi(v, 13320),
345
0
            mulhi(u, 33050),
346
0
        ];
347
348
0
        let get_r = |y: u8| clip(mulhi(y, 19077) + coeffs[0] - 14234);
Unexecuted instantiation: image_webp::yuv::fill_rgba_row_simple::<3>::{closure#0}
Unexecuted instantiation: image_webp::yuv::fill_rgba_row_simple::<4>::{closure#0}
349
0
        let get_g = |y: u8| clip(mulhi(y, 19077) - coeffs[1] - coeffs[2] + 8708);
Unexecuted instantiation: image_webp::yuv::fill_rgba_row_simple::<3>::{closure#1}
Unexecuted instantiation: image_webp::yuv::fill_rgba_row_simple::<4>::{closure#1}
350
0
        let get_b = |y: u8| clip(mulhi(y, 19077) + coeffs[3] - 17685);
Unexecuted instantiation: image_webp::yuv::fill_rgba_row_simple::<3>::{closure#2}
Unexecuted instantiation: image_webp::yuv::fill_rgba_row_simple::<4>::{closure#2}
351
352
0
        let rgb1 = &mut rgb[0..3];
353
0
        rgb1[0] = get_r(y[0]);
354
0
        rgb1[1] = get_g(y[0]);
355
0
        rgb1[2] = get_b(y[0]);
356
357
0
        let rgb2 = &mut rgb[BPP..];
358
0
        rgb2[0] = get_r(y[1]);
359
0
        rgb2[1] = get_g(y[1]);
360
0
        rgb2[2] = get_b(y[1]);
361
    }
362
363
0
    let remainder = rgb_chunks.into_remainder();
364
0
    if remainder.len() >= 3 {
365
0
        if let (Some(&y), Some(&u), Some(&v)) = (
366
0
            y_chunks.remainder().iter().next(),
367
0
            u_iter.next(),
368
0
            v_iter.next(),
369
0
        ) {
370
0
            let coeffs = [
371
0
                mulhi(v, 26149),
372
0
                mulhi(u, 6419),
373
0
                mulhi(v, 13320),
374
0
                mulhi(u, 33050),
375
0
            ];
376
0
377
0
            remainder[0] = clip(mulhi(y, 19077) + coeffs[0] - 14234);
378
0
            remainder[1] = clip(mulhi(y, 19077) - coeffs[1] - coeffs[2] + 8708);
379
0
            remainder[2] = clip(mulhi(y, 19077) + coeffs[3] - 17685);
380
0
        }
381
0
    }
382
0
}
Unexecuted instantiation: image_webp::yuv::fill_rgba_row_simple::<3>
Unexecuted instantiation: image_webp::yuv::fill_rgba_row_simple::<4>
383
384
#[cfg(test)]
385
mod tests {
386
    use super::*;
387
388
    #[test]
389
    fn test_fancy_grid() {
390
        #[rustfmt::skip]
391
        let y_buffer = [
392
            77, 162, 202, 185,
393
            28, 13, 199, 182,
394
            135, 147, 164, 135, 
395
            66, 27, 171, 130,
396
        ];
397
398
        #[rustfmt::skip]
399
        let u_buffer = [
400
            34, 101, 
401
            123, 163
402
        ];
403
404
        #[rustfmt::skip]
405
        let v_buffer = [
406
            97, 167,
407
            149, 23,
408
        ];
409
410
        let mut rgb_buffer = [0u8; 16 * 3];
411
        fill_rgb_buffer_fancy::<3>(&mut rgb_buffer, &y_buffer, &u_buffer, &v_buffer, 4, 4, 4);
412
413
        #[rustfmt::skip]
414
        let upsampled_u_buffer = [
415
            34, 51, 84, 101,
416
            56, 71, 101, 117,
417
            101, 112, 136, 148,
418
            123, 133, 153, 163,
419
        ];
420
421
        #[rustfmt::skip]
422
        let upsampled_v_buffer = [
423
            97, 115, 150, 167,
424
            110, 115, 126, 131,
425
            136, 117, 78, 59,
426
            149, 118, 55, 23,
427
        ];
428
429
        let mut upsampled_rgb_buffer = [0u8; 16 * 3];
430
        for (((rgb_val, y), u), v) in upsampled_rgb_buffer
431
            .chunks_exact_mut(3)
432
            .zip(y_buffer)
433
            .zip(upsampled_u_buffer)
434
            .zip(upsampled_v_buffer)
435
        {
436
            rgb_val[0] = yuv_to_r(y, v);
437
            rgb_val[1] = yuv_to_g(y, u, v);
438
            rgb_val[2] = yuv_to_b(y, u);
439
        }
440
441
        assert_eq!(rgb_buffer, upsampled_rgb_buffer);
442
    }
443
444
    #[test]
445
    fn test_yuv_conversions() {
446
        let (y, u, v) = (203, 40, 42);
447
448
        assert_eq!(yuv_to_r(y, v), 80);
449
        assert_eq!(yuv_to_g(y, u, v), 255);
450
        assert_eq!(yuv_to_b(y, u), 40);
451
    }
452
}