1+ use crate :: CacheLineAlign ;
12use crate :: error:: Error ;
23use crate :: image:: Image ;
34use crate :: kmeans:: Kmeans ;
@@ -35,10 +36,10 @@ pub(crate) fn remap_to_palette<'x, 'b: 'x>(px: &mut DynamicRows, background: Opt
3536
3637 let tls = ThreadLocal :: new ( ) ;
3738 let width = px. width as usize ;
38- let per_thread_buffers = move || -> Result < _ , Error > { Ok ( RefCell :: new ( ( Kmeans :: new ( palette_len) ?, temp_buf ( width) ?, temp_buf ( width) ?, temp_buf ( width) ?) ) ) } ;
39+ let per_thread_buffers = move || -> Result < _ , Error > { Ok ( CacheLineAlign ( RefCell :: new ( ( Kmeans :: new ( palette_len) ?, temp_buf ( width) ?, temp_buf ( width) ?, temp_buf ( width) ?) ) ) ) } ;
3940
4041 let tls_tmp1 = tls. get_or_try ( per_thread_buffers) ?;
41- let mut tls_tmp = tls_tmp1. borrow_mut ( ) ;
42+ let mut tls_tmp = tls_tmp1. 0 . borrow_mut ( ) ;
4243
4344 let input_rows = px. rows_iter ( & mut tls_tmp. 1 ) ?;
4445 let ( background, transparent_index) = background. map ( |background| {
@@ -60,7 +61,7 @@ pub(crate) fn remap_to_palette<'x, 'b: 'x>(px: &mut DynamicRows, background: Opt
6061 Ok ( res) => res,
6162 Err ( _) => return f64:: NAN ,
6263 } ;
63- let ( kmeans, temp_row, temp_row_f, temp_row_f_bg) = & mut * tls_res. borrow_mut ( ) ;
64+ let ( kmeans, temp_row, temp_row_f, temp_row_f_bg) = & mut * tls_res. 0 . borrow_mut ( ) ;
6465
6566 let output_pixels_row = & mut output_pixels_row[ ..width] ;
6667 let importance_map = importance_map. and_then ( |m| m. get ( row * width..) ) . unwrap_or ( & [ ] ) ;
@@ -96,7 +97,7 @@ pub(crate) fn remap_to_palette<'x, 'b: 'x>(px: &mut DynamicRows, background: Opt
9697 }
9798
9899 if let Some ( kmeans) = tls. into_iter ( )
99- . map ( |t| RefCell :: into_inner ( t ) . 0 )
100+ . map ( |t| t . 0 . into_inner ( ) . 0 )
100101 . reduce ( Kmeans :: merge) { kmeans. finalize ( palette) ; }
101102
102103 let remapping_error = remapping_error / f64:: from ( px. width * px. height ) ;
@@ -188,11 +189,11 @@ pub(crate) fn remap_to_palette_floyd(input_image: &mut Image, mut output_pixels:
188189
189190 // Chunks have overhead, so should be big (more than 2 bring diminishing results). Chunks risk causing seams, so should be tall.
190191 let num_chunks = if quant. single_threaded_dithering { 1 } else { ( width * height / 524_288 ) . min ( height / 128 ) . max ( if height > 128 { 2 } else { 1 } ) . min ( num_cpus ( ) ) } ;
191- let chunks = output_pixels. chunks ( ( height + num_chunks - 1 ) / num_chunks) ;
192+ let chunks = output_pixels. chunks ( ( height + num_chunks - 1 ) / num_chunks) . map ( CacheLineAlign ) ;
192193 scope ( move |s| {
193194 let mut chunk_start_row = 0 ;
194195 for mut chunk in chunks {
195- let chunk_len = chunk. len ( ) ;
196+ let chunk_len = chunk. 0 . len ( ) ;
196197 let mut temp_row = temp_buf ( width) ?;
197198 let mut input_image_iter = input_image_px. rows_iter_prepared ( ) ?;
198199 let mut background = background. map ( |bg| bg. rows_iter_prepared ( ) ) . transpose ( ) ?;
@@ -218,7 +219,7 @@ pub(crate) fn remap_to_palette_floyd(input_image: &mut Image, mut output_pixels:
218219 return Err ( Error :: Aborted ) ;
219220 }
220221 s. spawn ( move |_| {
221- for ( chunk_row, output_pixels_row) in chunk. rows_mut ( ) . enumerate ( ) {
222+ for ( chunk_row, output_pixels_row) in chunk. 0 . rows_mut ( ) . enumerate ( ) {
222223 let row = chunk_start_row + chunk_row;
223224 let row_pixels = input_image_iter. row_f ( & mut temp_row, row as _ ) ;
224225 let bg_pixels = background. as_mut ( ) . map ( |b| b. row_f ( & mut temp_row, row as _ ) ) . unwrap_or ( & [ ] ) ;
0 commit comments