@@ -4,7 +4,7 @@ use alloc::borrow::ToOwned;
44use alloc:: vec:: Vec ;
55
66use image_texel:: image:: {
7- AtomicImage , AtomicImageRef , CellImage , CellImageRef , Image , ImageMut , ImageRef ,
7+ data , AtomicImage , AtomicImageRef , CellImage , CellImageRef , Image , ImageMut , ImageRef ,
88} ;
99use image_texel:: texels:: U8 ;
1010
@@ -36,6 +36,24 @@ pub struct Plane {
3636 inner : Image < PlaneBytes > ,
3737}
3838
39+ /// A reference to bytes containing data of a single plane.
40+ ///
41+ /// This can be created from a [`Plane`] but also by validating an arbitrary slice of bytes against
42+ /// some of the known layouts. These layouts can be partially defined by the user.
43+ ///
44+ /// The common use for values is to pass an outside buffer to [`ConverterRun::
45+ pub struct PlaneDataRef < ' data > {
46+ pub ( crate ) inner : data:: DataRef < ' data , PlaneBytes > ,
47+ }
48+
49+ /// A reference to mutable bytes containing data of a single plane.
50+ ///
51+ /// This can be created from a [`Plane`] but also by validating an arbitrary slice of bytes against
52+ /// some of the known layouts. These layouts can be partially defined by the user.
53+ pub struct PlaneDataMut < ' data > {
54+ pub ( crate ) inner : data:: DataMut < ' data , PlaneBytes > ,
55+ }
56+
3957/// Represents a single matrix like layer of an image.
4058///
4159/// Created from [`Canvas::plane`].
@@ -645,3 +663,51 @@ impl From<ArcCanvas> for RcCanvas {
645663 RcCanvas { inner : out }
646664 }
647665}
666+
667+ impl < ' lt > From < & ' lt Plane > for PlaneDataRef < ' lt > {
668+ fn from ( plane : & ' _ Plane ) -> PlaneDataRef < ' _ > {
669+ PlaneDataRef {
670+ inner : plane. inner . as_ref ( ) . into_cloned_layout ( ) . into_data ( ) ,
671+ }
672+ }
673+ }
674+
675+ impl < ' lt > From < & ' lt mut Plane > for PlaneDataMut < ' lt > {
676+ fn from ( plane : & ' _ mut Plane ) -> PlaneDataMut < ' _ > {
677+ PlaneDataMut {
678+ inner : plane. inner . as_mut ( ) . into_cloned_layout ( ) . into_data ( ) ,
679+ }
680+ }
681+ }
682+
683+ impl < ' data > PlaneDataRef < ' data > {
684+ /// Create plane data from bytes and a layout.
685+ ///
686+ /// This returns `Some` if the data is long enough for the layout, and `None` otherwise.
687+ /// Extraneous data is ignored.
688+ pub fn new ( data : & ' data [ u8 ] , layout : PlaneBytes ) -> Result < Self , LayoutError > {
689+ let err = LayoutError :: length_required (
690+ data. len ( ) ..image_texel:: layout:: Layout :: byte_len ( & layout) ,
691+ ) ;
692+
693+ let inner = data:: DataRef :: with_layout_at ( data, layout, 0 ) . ok_or ( err) ?;
694+
695+ Ok ( PlaneDataRef { inner } )
696+ }
697+ }
698+
699+ impl < ' data > PlaneDataMut < ' data > {
700+ /// Create mutable plane data from bytes and a layout.
701+ ///
702+ /// This returns `Some` if the data is long enough for the layout, and `None` otherwise.
703+ /// Extraneous data is ignored.
704+ pub fn new ( data : & ' data mut [ u8 ] , layout : PlaneBytes ) -> Result < Self , LayoutError > {
705+ let err = LayoutError :: length_required (
706+ data. len ( ) ..image_texel:: layout:: Layout :: byte_len ( & layout) ,
707+ ) ;
708+
709+ let inner = data:: DataMut :: with_layout_at ( data, layout, 0 ) . ok_or ( err) ?;
710+
711+ Ok ( PlaneDataMut { inner } )
712+ }
713+ }
0 commit comments