@@ -582,11 +582,37 @@ impl<'data, L> ImageRef<'data, L> {
582582 }
583583
584584 /// Copy all bytes to a newly allocated image.
585- pub fn to_owned ( & self ) -> Image < L >
586- where
587- L : Layout + Clone ,
588- {
589- Image :: with_bytes ( self . inner . layout ( ) . clone ( ) , self . inner . as_bytes ( ) )
585+ ///
586+ /// Note this will allocate a buffer according to the capacity length of this reference, not
587+ /// merely the layout. When this is not the intention, consider calling [`Self::split_layout`]
588+ /// or [`Self::truncate_layout`] respectively.
589+ ///
590+ /// # Examples
591+ ///
592+ /// Here we make an independent copy of the second plane of a composite image.
593+ ///
594+ /// ```
595+ /// use image_texel::image::{Image, ImageRef};
596+ /// use image_texel::layout::{PlaneMatrices, Matrix};
597+ /// use image_texel::texels::U8;
598+ ///
599+ /// let mat = Matrix::from_width_height(U8, 8, 8).unwrap();
600+ /// let buffer = Image::new(PlaneMatrices::<_, 2>::from_repeated(mat));
601+ ///
602+ /// // … some code to initialize those planes.
603+ /// # let mut buffer = buffer;
604+ /// # buffer.as_mut().into_planes([1]).unwrap()[0]
605+ /// # .as_capacity_buf_mut()[..8].copy_from_slice(b"not zero");
606+ /// # let buffer = buffer;
607+ ///
608+ /// let [p1] = buffer.as_ref().into_planes([1]).unwrap();
609+ /// let clone_of: Image<_> = p1.into_owned();
610+ ///
611+ /// let [p1] = buffer.as_ref().into_planes([1]).unwrap();
612+ /// assert_eq!(clone_of.as_bytes(), p1.as_bytes());
613+ /// ```
614+ pub fn into_owned ( self ) -> Image < L > {
615+ self . inner . into_owned ( ) . into ( )
590616 }
591617
592618 /// Get a slice of the individual samples in the layout.
@@ -663,6 +689,18 @@ impl<'data, L> ImageRef<'data, L> {
663689 RawImage :: from_buffer ( Bytes ( next. len ( ) ) , next) . into ( )
664690 }
665691
692+ /// Remove all past-the-layout bytes.
693+ ///
694+ /// This is a utility to combine with pipelining. It is equivalent to calling
695+ /// [`Self::split_layout`] and discarding that result.
696+ pub fn truncate_layout ( mut self ) -> Self
697+ where
698+ L : Layout ,
699+ {
700+ let _ = self . split_layout ( ) ;
701+ self
702+ }
703+
666704 /// Split this reference into independent planes.
667705 ///
668706 /// If any plane fails their indexing operation or would not be aligned to the required
@@ -885,14 +923,6 @@ impl<'data, L> ImageMut<'data, L> {
885923 Some ( self . inner . checked_decay ( ) ?. into ( ) )
886924 }
887925
888- /// Copy the bytes and layout to an owned container.
889- pub fn to_owned ( & self ) -> Image < L >
890- where
891- L : Layout + Clone ,
892- {
893- Image :: with_bytes ( self . inner . layout ( ) . clone ( ) , self . inner . as_bytes ( ) )
894- }
895-
896926 /// Get a slice of the individual samples in the layout.
897927 pub fn as_slice ( & self ) -> & [ L :: Sample ]
898928 where
@@ -937,6 +967,38 @@ impl<'data, L> ImageMut<'data, L> {
937967 pixel. cast_mut_buf ( self . inner . as_mut_buf ( ) )
938968 }
939969
970+ /// Copy all bytes to a newly allocated image.
971+ ///
972+ /// Note this will allocate a buffer according to the capacity length of this reference, not
973+ /// merely the layout. When this is not the intention, consider calling [`Self::split_layout`]
974+ /// or [`Self::truncate_layout`] respectively.
975+ ///
976+ /// # Examples
977+ ///
978+ /// Here we make an independent copy of the second plane of a composite image.
979+ ///
980+ /// ```
981+ /// use image_texel::image::{Image, ImageRef};
982+ /// use image_texel::layout::{PlaneMatrices, Matrix};
983+ /// use image_texel::texels::U8;
984+ ///
985+ /// let mat = Matrix::from_width_height(U8, 8, 8).unwrap();
986+ /// let mut buffer = Image::new(PlaneMatrices::<_, 2>::from_repeated(mat));
987+ ///
988+ /// // … some code to initialize those planes.
989+ /// # buffer.as_mut().into_planes([1]).unwrap()[0]
990+ /// # .as_capacity_buf_mut()[..8].copy_from_slice(b"not zero");
991+ ///
992+ /// let [p1] = buffer.as_mut().into_planes([1]).unwrap();
993+ /// let clone_of: Image<_> = p1.into_owned();
994+ ///
995+ /// let [p1] = buffer.as_ref().into_planes([1]).unwrap();
996+ /// assert_eq!(clone_of.as_bytes(), p1.as_bytes());
997+ /// ```
998+ pub fn into_owned ( self ) -> Image < L > {
999+ self . inner . into_owned ( ) . into ( )
1000+ }
1001+
9401002 /// Turn into a slice of the individual samples in the layout.
9411003 ///
9421004 /// This preserves the lifetime with which the layout is borrowed from the underlying image,
@@ -1012,6 +1074,18 @@ impl<'data, L> ImageMut<'data, L> {
10121074 RawImage :: from_buffer ( Bytes ( next. len ( ) ) , next) . into ( )
10131075 }
10141076
1077+ /// Remove all past-the-layout bytes.
1078+ ///
1079+ /// This is a utility to combine with pipelining. It is equivalent to calling
1080+ /// [`Self::split_layout`] and discarding that result.
1081+ pub fn truncate_layout ( mut self ) -> Self
1082+ where
1083+ L : Layout ,
1084+ {
1085+ let _ = self . split_layout ( ) ;
1086+ self
1087+ }
1088+
10151089 /// Split this mutable reference into independent planes.
10161090 ///
10171091 /// If any plane fails their indexing operation or would not be aligned to the required
0 commit comments