3333#[ cfg( test) ]
3434mod tests;
3535
36- use std:: {
37- iter,
38- num:: { NonZeroU8 , NonZeroUsize } ,
39- } ;
36+ use std:: num:: { NonZeroU8 , NonZeroUsize } ;
4037
41- use aligned_vec:: { ABox , AVec , ConstAlign } ;
38+ mod aligned;
39+ use aligned:: AlignedData ;
4240
4341use crate :: { error:: Error , pixel:: Pixel } ;
4442
45- /// Alignment for plane data on WASM platforms (8 bytes).
46- #[ cfg( target_arch = "wasm32" ) ]
47- const DATA_ALIGNMENT : usize = 1 << 3 ;
48-
49- /// Alignment for plane data on non-WASM platforms (64 bytes for SIMD optimization).
50- #[ cfg( not( target_arch = "wasm32" ) ) ]
51- const DATA_ALIGNMENT : usize = 1 << 6 ;
52-
5343/// A two-dimensional plane of pixel data with optional padding.
5444///
5545/// `Plane<T>` represents a rectangular array of pixels of type `T`, where `T` implements
@@ -74,10 +64,10 @@ const DATA_ALIGNMENT: usize = 1 << 6;
7464/// - [`rows()`](Plane::rows) / [`rows_mut()`](Plane::rows_mut): Iterate over all visible rows
7565/// - [`pixel()`](Plane::pixel) / [`pixel_mut()`](Plane::pixel_mut): Access individual pixels
7666/// - [`pixels()`](Plane::pixels) / [`pixels_mut()`](Plane::pixels_mut): Iterate over all visible pixels
77- #[ derive( Clone ) ]
67+ #[ derive( Debug , Clone ) ]
7868pub struct Plane < T : Pixel > {
7969 /// The underlying pixel data buffer, including padding.
80- pub ( crate ) data : ABox < [ T ] , ConstAlign < DATA_ALIGNMENT > > ,
70+ pub ( crate ) data : AlignedData < T > ,
8171 /// Geometry information describing dimensions and padding.
8272 pub ( crate ) geometry : PlaneGeometry ,
8373}
@@ -92,12 +82,11 @@ where
9282 . height
9383 . saturating_add ( geometry. pad_top )
9484 . saturating_add ( geometry. pad_bottom ) ;
85+
86+ let pixels = rows. get ( ) * geometry. stride . get ( ) ;
87+
9588 Self {
96- data : AVec :: from_iter (
97- DATA_ALIGNMENT ,
98- iter:: repeat_n ( T :: zero ( ) , geometry. stride . get ( ) * rows. get ( ) ) ,
99- )
100- . into_boxed_slice ( ) ,
89+ data : AlignedData :: new ( pixels) ,
10190 geometry,
10291 }
10392 }
@@ -137,11 +126,9 @@ where
137126 #[ inline]
138127 #[ must_use]
139128 pub fn rows ( & self ) -> impl DoubleEndedIterator < Item = & [ T ] > + ExactSizeIterator {
140- let origin = self . geometry . stride . get ( ) * self . geometry . pad_top ;
141- // SAFETY: The plane creation interface ensures the data is large enough
142- let visible_data = unsafe { self . data . get_unchecked ( origin..) } ;
143- visible_data
129+ self . data
144130 . chunks_exact ( self . geometry . stride . get ( ) )
131+ . skip ( self . geometry . pad_top )
145132 . take ( self . geometry . height . get ( ) )
146133 . map ( |row| {
147134 let start_idx = self . geometry . pad_left ;
@@ -155,11 +142,9 @@ where
155142 /// in the plane, from top to bottom.
156143 #[ inline]
157144 pub fn rows_mut ( & mut self ) -> impl DoubleEndedIterator < Item = & mut [ T ] > + ExactSizeIterator {
158- let origin = self . geometry . stride . get ( ) * self . geometry . pad_top ;
159- // SAFETY: The plane creation interface ensures the data is large enough
160- let visible_data = unsafe { self . data . get_unchecked_mut ( origin..) } ;
161- visible_data
145+ self . data
162146 . chunks_exact_mut ( self . geometry . stride . get ( ) )
147+ . skip ( self . geometry . pad_top )
163148 . take ( self . geometry . height . get ( ) )
164149 . map ( |row| {
165150 let start_idx = self . geometry . pad_left ;
0 commit comments