@@ -521,13 +521,23 @@ fn process_halved_chroma_row<
521
521
522
522
let max_value = ( 1 << BIT_DEPTH ) - 1 ;
523
523
524
+ // If the stride is larger than the plane size,
525
+ // it might contain junk data beyond the actual valid region.
526
+ // To avoid processing artifacts when working with odd-sized images,
527
+ // the buffer is reshaped to its actual size,
528
+ // preventing accidental use of invalid values from the trailing region.
529
+
530
+ let y_plane = & image. y_plane [ 0 ..image. width ] ;
531
+ let chroma_size = image. width . div_ceil ( 2 ) ;
532
+ let u_plane = & image. u_plane [ 0 ..chroma_size] ;
533
+ let v_plane = & image. v_plane [ 0 ..chroma_size] ;
534
+ let rgba = & mut rgba[ 0 ..image. width * CHANNELS ] ;
535
+
524
536
let bias_y = range. bias_y as i32 ;
525
537
let bias_uv = range. bias_uv as i32 ;
526
- let y_iter = image . y_plane . chunks_exact ( 2 ) ;
538
+ let y_iter = y_plane. chunks_exact ( 2 ) ;
527
539
let rgb_chunks = rgba. chunks_exact_mut ( CHANNELS * 2 ) ;
528
- for ( ( ( y_src, & u_src) , & v_src) , rgb_dst) in
529
- y_iter. zip ( image. u_plane ) . zip ( image. v_plane ) . zip ( rgb_chunks)
530
- {
540
+ for ( ( ( y_src, & u_src) , & v_src) , rgb_dst) in y_iter. zip ( u_plane) . zip ( v_plane) . zip ( rgb_chunks) {
531
541
let y_value: i32 = ( y_src[ 0 ] . as_ ( ) - bias_y) * y_coef;
532
542
let cb_value: i32 = u_src. as_ ( ) - bias_uv;
533
543
let cr_value: i32 = v_src. as_ ( ) - bias_uv;
@@ -571,13 +581,13 @@ fn process_halved_chroma_row<
571
581
572
582
// Process remainder if width is odd.
573
583
if image. width & 1 != 0 {
574
- let y_left = image . y_plane . chunks_exact ( 2 ) . remainder ( ) ;
584
+ let y_left = y_plane. chunks_exact ( 2 ) . remainder ( ) ;
575
585
let rgb_chunks = rgba
576
586
. chunks_exact_mut ( CHANNELS * 2 )
577
587
. into_remainder ( )
578
588
. chunks_exact_mut ( CHANNELS ) ;
579
- let u_iter = image . u_plane . iter ( ) . rev ( ) ;
580
- let v_iter = image . v_plane . iter ( ) . rev ( ) ;
589
+ let u_iter = u_plane. iter ( ) . rev ( ) ;
590
+ let v_iter = v_plane. iter ( ) . rev ( ) ;
581
591
582
592
for ( ( ( y_src, u_src) , v_src) , rgb_dst) in
583
593
y_left. iter ( ) . zip ( u_iter) . zip ( v_iter) . zip ( rgb_chunks)
0 commit comments