1#![no_std]
50
51extern crate alloc;
52#[cfg(test)]
53extern crate std;
54
55use alloc::borrow::{Cow, ToOwned};
56use alloc::vec::Vec;
57use core::slice;
58
59mod traits;
60
61mod iter;
62mod ops;
63pub use iter::*;
64
65pub type ImgVec<Pixel> = Img<Vec<Pixel>>;
71
72pub type ImgRef<'slice, Pixel> = Img<&'slice [Pixel]>;
77
78pub type ImgRefMut<'slice, Pixel> = Img<&'slice mut [Pixel]>;
82
83pub trait ImgExt<Pixel> {
91 #[cfg(feature = "deprecated")]
97 fn width_padded(&self) -> usize;
98
99 #[cfg(feature = "deprecated")]
106 fn height_padded(&self) -> usize;
107
108 #[cfg(feature = "deprecated")]
112 fn rows_padded(&self) -> slice::Chunks<'_, Pixel>;
113
114 fn as_ref(&self) -> ImgRef<'_, Pixel>;
116}
117
118pub trait ImgExtMut<Pixel> {
126 #[cfg(feature = "deprecated")]
130 fn rows_padded_mut(&mut self) -> slice::ChunksMut<'_, Pixel>;
131
132 fn as_mut(&mut self) -> ImgRefMut<'_, Pixel>;
134}
135
136#[derive(Debug, Copy, Clone)]
140pub struct Img<Container> {
141 #[deprecated(note = "Don't access struct fields directly. Use buf(), buf_mut() or into_buf()")]
145 #[cfg(feature = "deprecated")]
146 pub buf: Container,
147
148 #[cfg(not(feature = "deprecated"))]
149 buf: Container,
150
151 #[deprecated(note = "Don't access struct fields directly. Use stride()")]
155 #[cfg(feature = "deprecated")]
156 pub stride: usize,
157
158 #[cfg(not(feature = "deprecated"))]
159 stride: usize,
160
161 #[deprecated(note = "Don't access struct fields directly. Use width()")]
165 #[cfg(feature = "deprecated")]
166 pub width: u32,
167
168 #[cfg(not(feature = "deprecated"))]
169 width: u32,
170
171 #[deprecated(note = "Don't access struct fields directly. Use height()")]
173 #[cfg(feature = "deprecated")]
174 pub height: u32,
175
176 #[cfg(not(feature = "deprecated"))]
177 height: u32,
178}
179
180impl<Container> Img<Container> {
181 #[inline(always)]
185 #[allow(deprecated)]
186 pub const fn width(&self) -> usize { self.width as usize }
187
188 #[inline(always)]
190 #[allow(deprecated)]
191 pub const fn height(&self) -> usize { self.height as usize }
192
193 #[inline(always)]
201 #[allow(deprecated)]
202 pub const fn stride(&self) -> usize { self.stride }
203
204 #[inline(always)]
208 #[allow(deprecated)]
209 pub const fn buf(&self) -> &Container { &self.buf }
210
211 #[inline(always)]
215 #[allow(deprecated)]
216 pub fn buf_mut(&mut self) -> &mut Container { &mut self.buf }
217
218 #[inline(always)]
220 #[allow(deprecated)]
221 pub fn into_buf(self) -> Container { self.buf }
222}
223
224impl<Pixel,Container> ImgExt<Pixel> for Img<Container> where Container: AsRef<[Pixel]> {
225 #[inline(always)]
226 #[cfg(feature = "deprecated")]
227 fn width_padded(&self) -> usize {
228 self.stride()
229 }
230
231 #[inline(always)]
232 #[cfg(feature = "deprecated")]
233 fn height_padded(&self) -> usize {
234 let len = self.buf().as_ref().len();
235 assert_eq!(0, len % self.stride());
236 len / self.stride()
237 }
238
239 #[inline(always)]
243 #[cfg(feature = "deprecated")]
244 fn rows_padded(&self) -> slice::Chunks<'_, Pixel> {
245 self.buf().as_ref().chunks(self.stride())
246 }
247
248 #[inline(always)]
249 #[allow(deprecated)]
250 fn as_ref(&self) -> ImgRef<'_, Pixel> {
251 Img {
252 buf: self.buf.as_ref(),
253 width: self.width,
254 height: self.height,
255 stride: self.stride,
256 }
257 }
258}
259
260impl<Pixel,Container> ImgExtMut<Pixel> for Img<Container> where Container: AsMut<[Pixel]> {
261 #[cfg(feature = "deprecated")]
269 fn rows_padded_mut(&mut self) -> slice::ChunksMut<'_, Pixel> {
270 let stride = self.stride();
271 self.buf_mut().as_mut().chunks_mut(stride)
272 }
273
274 #[inline(always)]
275 #[allow(deprecated)]
276 fn as_mut(&mut self) -> ImgRefMut<'_, Pixel> {
277 Img {
278 buf: self.buf.as_mut(),
279 width: self.width,
280 height: self.height,
281 stride: self.stride,
282 }
283 }
284}
285
286#[inline]
287fn sub_image(left: usize, top: usize, width: usize, height: usize, stride: usize, buf_len: usize) -> (usize, usize, usize) {
288 let start = stride * top + left;
289 let full_strides_end = start + stride * height;
290 let end = if buf_len >= full_strides_end {
292 full_strides_end
293 } else {
294 debug_assert!(height > 0);
295 let min_strides_len = full_strides_end + width - stride;
296 debug_assert!(buf_len >= min_strides_len, "the buffer is too small to fit the subimage");
297 min_strides_len
299 };
300 (start, end, stride)
301}
302
303impl<'slice, T> ImgRef<'slice, T> {
304 #[inline]
311 #[must_use]
312 #[track_caller]
313 pub fn sub_image(&self, left: usize, top: usize, width: usize, height: usize) -> Self {
314 assert!(top + height <= self.height());
315 assert!(left + width <= self.width());
316 let (start, end, stride) = sub_image(left, top, width, height, self.stride(), self.buf().len());
317 let buf = &self.buf()[start..end];
318 Self::new_stride(buf, width, height, stride)
319 }
320
321 #[inline]
322 #[cfg_attr(debug_assertions, track_caller)]
330 pub fn rows(&self) -> RowsIter<'slice, T> {
331 RowsIter {
332 inner: self.valid_buf().chunks(self.stride()),
333 width: self.width(),
334 }
335 }
336
337 #[deprecated(note = "Size of this buffer is unpredictable. Use .rows() instead")]
341 #[cfg(feature = "deprecated")]
342 #[doc(hidden)]
343 pub fn iter(&self) -> slice::Iter<'slice, T> {
344 self.buf().iter()
345 }
346}
347
348impl<'a, T: Clone> ImgRef<'a, T> {
349 #[allow(deprecated)]
355 #[must_use]
356 pub fn to_contiguous_buf(&self) -> (Cow<'a, [T]>, usize, usize) {
357 let width = self.width();
358 let height = self.height();
359 let stride = self.stride();
360 if width == stride {
361 return (Cow::Borrowed(self.buf), width, height);
362 }
363 let mut buf = Vec::with_capacity(width * height);
364 for row in self.rows() {
365 buf.extend_from_slice(row);
366 }
367 (Cow::Owned(buf), width, height)
368 }
369}
370
371impl<'slice, T> ImgRefMut<'slice, T> {
372 #[inline]
374 #[allow(deprecated)]
375 #[must_use]
376 pub fn sub_image(&'slice self, left: usize, top: usize, width: usize, height: usize) -> ImgRef<'slice, T> {
377 self.as_ref().sub_image(left, top, width, height)
378 }
379
380 #[inline]
389 #[allow(deprecated)]
390 #[must_use]
391 #[track_caller]
392 pub fn sub_image_mut(&mut self, left: usize, top: usize, width: usize, height: usize) -> ImgRefMut<'_, T> {
393 assert!(top + height <= self.height());
394 assert!(left + width <= self.width());
395 let (start, end, stride) = sub_image(left, top, width, height, self.stride(), self.buf.len());
396 let buf = &mut self.buf[start..end];
397 ImgRefMut::new_stride(buf, width, height, stride)
398 }
399
400 #[allow(deprecated)]
409 #[must_use]
410 #[track_caller]
411 pub fn into_sub_image_mut(self, left: usize, top: usize, width: usize, height: usize) -> Self {
412 assert!(top + height <= self.height());
413 assert!(left + width <= self.width());
414 let (start, end, stride) = sub_image(left, top, width, height, self.stride(), self.buf.len());
415 let buf = &mut self.buf[start..end];
416 ImgRefMut::new_stride(buf, width, height, stride)
417 }
418
419 #[inline]
421 #[must_use]
422 pub fn as_ref(&self) -> ImgRef<'_, T> {
423 self.new_buf(self.buf().as_ref())
424 }
425}
426
427impl<'slice, T: Copy> ImgRef<'slice, T> {
428 #[inline]
436 #[track_caller]
437 pub fn pixels(&self) -> PixelsIter<'slice, T> {
438 PixelsIter::new(*self)
439 }
440}
441
442impl<'slice, T> ImgRef<'slice, T> {
443 #[inline]
451 pub fn pixels_ref(&self) -> PixelsRefIter<'slice, T> {
452 PixelsRefIter::new(*self)
453 }
454}
455
456impl<T: Copy> ImgRefMut<'_, T> {
457 #[inline]
463 pub fn pixels(&self) -> PixelsIter<'_, T> {
464 PixelsIter::new(self.as_ref())
465 }
466}
467
468impl<T> ImgRefMut<'_, T> {
469 #[inline]
474 pub fn pixels_mut(&mut self) -> PixelsIterMut<'_, T> {
475 PixelsIterMut::new(self.as_mut())
476 }
477}
478
479impl<T: Copy> ImgVec<T> {
480 #[inline]
485 #[cfg_attr(debug_assertions, track_caller)]
486 pub fn pixels(&self) -> PixelsIter<'_, T> {
487 PixelsIter::new(self.as_ref())
488 }
489}
490
491impl<T> ImgVec<T> {
492 #[inline]
497 #[cfg_attr(debug_assertions, track_caller)]
498 pub fn pixels_mut(&mut self) -> PixelsIterMut<'_, T> {
499 PixelsIterMut::new(self.as_mut())
500 }
501}
502
503impl<T> ImgRefMut<'_, T> {
504 #[inline]
510 pub fn rows(&self) -> RowsIter<'_, T> {
511 self.as_ref().rows()
512 }
513
514 #[inline]
520 #[allow(deprecated)]
521 pub fn rows_mut(&mut self) -> RowsIterMut<'_, T> {
522 let stride = self.stride();
523 let width = self.width();
524 let height = self.height();
525 let non_padded = &mut self.buf[0..stride * height + width - stride];
526 RowsIterMut {
527 width,
528 inner: non_padded.chunks_mut(stride),
529 }
530 }
531}
532
533#[cfg(feature = "deprecated")]
535impl<Container> IntoIterator for Img<Container> where Container: IntoIterator {
536 type Item = Container::Item;
537 type IntoIter = Container::IntoIter;
538 fn into_iter(self) -> Container::IntoIter {
540 self.into_buf().into_iter()
541 }
542}
543
544impl<T> ImgVec<T> {
545 #[allow(deprecated)]
551 #[must_use]
552 #[track_caller]
553 pub fn sub_image_mut(&mut self, left: usize, top: usize, width: usize, height: usize) -> ImgRefMut<'_, T> {
554 assert!(top + height <= self.height());
555 assert!(left + width <= self.width());
556 let start = self.stride * top + left;
557 let min_buf_size = if self.height > 0 { self.stride * height + width - self.stride } else {0};
558 let buf = &mut self.buf[start .. start + min_buf_size];
559 Img::new_stride(buf, width, height, self.stride)
560 }
561
562 #[inline]
563 #[must_use]
564 pub fn sub_image(&self, left: usize, top: usize, width: usize, height: usize) -> ImgRef<'_, T> {
566 self.as_ref().sub_image(left, top, width, height)
567 }
568
569 #[inline]
575 #[must_use]
576 pub fn as_ref(&self) -> ImgRef<'_, T> {
577 self.new_buf(self.buf().as_ref())
578 }
579
580 #[inline]
586 pub fn as_mut(&mut self) -> ImgRefMut<'_, T> {
587 let width = self.width();
588 let height = self.height();
589 let stride = self.stride();
590 Img::new_stride(self.buf_mut().as_mut(), width, height, stride)
591 }
592
593 #[deprecated(note = "Size of this buffer may be unpredictable. Use .rows() instead")]
594 #[cfg(feature = "deprecated")]
595 #[doc(hidden)]
596 pub fn iter(&self) -> slice::Iter<'_, T> {
597 self.buf().iter()
598 }
599
600 #[inline]
606 #[cfg_attr(debug_assertions, track_caller)]
607 pub fn rows(&self) -> RowsIter<'_, T> {
608 self.as_ref().rows()
609 }
610
611 #[inline]
617 #[allow(deprecated)]
618 pub fn rows_mut(&mut self) -> RowsIterMut<'_, T> {
619 let stride = self.stride();
620 let width = self.width();
621 let height = self.height();
622 let non_padded = &mut self.buf[0..stride * height + width - stride];
623 RowsIterMut {
624 width,
625 inner: non_padded.chunks_mut(stride),
626 }
627 }
628}
629
630impl<Container> Img<Container> {
631 #[inline]
647 #[allow(deprecated)]
648 #[track_caller]
649 pub fn new_stride(buf: Container, width: usize, height: usize, stride: usize) -> Self {
650 assert!(stride > 0);
651 assert!(stride >= width);
652 debug_assert!(height < u32::MAX as usize);
653 debug_assert!(width < u32::MAX as usize);
654 Self {
655 buf,
656 width: width as u32,
657 height: height as u32,
658 stride,
659 }
660 }
661
662 #[inline]
676 pub fn new(buf: Container, width: usize, height: usize) -> Self {
677 Self::new_stride(buf, width, height, width)
678 }
679}
680
681#[cold]
682#[inline(never)]
683#[cfg_attr(debug_assertions, track_caller)]
684fn imgref_invalid_size(width: u32, height: u32, stride: usize, min_size: usize, len: usize) {
685 panic!("Invalid ImgRef params. Got stride={stride} for {width}×{height}; len={len} (needed {min_size})");
686}
687
688impl<T> ImgRefMut<'_, T> {
689 #[cfg_attr(debug_assertions, track_caller)]
690 fn valid_buf_mut(&mut self) -> &mut [T] {
691 let len = self.as_ref().valid_min_len();
692 &mut self.buf_mut()[..len]
693 }
694}
695
696impl<'buf, T> ImgRef<'buf, T> {
697 #[cfg_attr(debug_assertions, track_caller)]
698 fn valid_buf(&self) -> &'buf [T] {
699 &self.buf()[..self.valid_min_len()]
700 }
701
702 #[cfg_attr(debug_assertions, track_caller)]
703 #[inline(always)]
704 fn valid_min_len(&self) -> usize {
705 let stride = self.stride();
706 let width = self.width();
707 let height = self.height();
708 let min_size = if height == 0 || width == 0 { 0 } else { stride * height + width - stride };
709 let buf = self.buf();
710 #[allow(deprecated)]
711 if stride == 0 || stride < width || buf.len() < min_size {
712 imgref_invalid_size(self.width, self.height, stride, min_size, buf.len());
713 }
714 min_size
715 }
716}
717
718impl<T: Copy> Img<Vec<T>> {
719 #[allow(deprecated)]
724 #[must_use]
725 #[cfg_attr(debug_assertions, track_caller)]
726 pub fn into_contiguous_buf(mut self) -> (Vec<T>, usize, usize) {
727 let (_, w, h) = self.as_contiguous_buf();
728 (self.buf, w, h)
729 }
730
731 #[allow(deprecated)]
736 #[must_use]
737 #[cfg_attr(debug_assertions, track_caller)]
738 pub fn as_contiguous_buf(&mut self) -> (&[T], usize, usize) {
739 let width = self.width();
740 let height = self.height();
741 let stride = self.stride();
742 if width != stride {
743 let mut ref_mut = self.as_mut();
744 let buf = ref_mut.valid_buf_mut();
745 for row in 1..height {
746 buf.copy_within(row * stride .. row * stride + width, row * width);
747 }
748 }
749 self.buf.truncate(width * height);
750 (&mut self.buf, width, height)
751 }
752}
753
754impl<OldContainer> Img<OldContainer> {
755 #[inline]
761 #[track_caller]
762 pub fn map_buf<NewContainer, OldPixel, NewPixel, F>(self, callback: F) -> Img<NewContainer>
763 where NewContainer: AsRef<[NewPixel]>, OldContainer: AsRef<[OldPixel]>, F: FnOnce(OldContainer) -> NewContainer {
764 let width = self.width();
765 let height = self.height();
766 let stride = self.stride();
767 let old_buf_len = self.buf().as_ref().len();
768 #[allow(deprecated)]
769 let new_buf = callback(self.buf);
770 assert_eq!(old_buf_len, new_buf.as_ref().len());
771 Img::new_stride(new_buf, width, height, stride)
772 }
773
774 #[inline]
780 #[track_caller]
781 pub fn new_buf<NewContainer, OldPixel, NewPixel>(&self, new_buf: NewContainer) -> Img<NewContainer>
782 where NewContainer: AsRef<[NewPixel]>, OldContainer: AsRef<[OldPixel]> {
783 assert_eq!(self.buf().as_ref().len(), new_buf.as_ref().len());
784 Img::new_stride(new_buf, self.width(), self.height(), self.stride())
785 }
786}
787
788impl<T: Clone> From<Img<Cow<'_, [T]>>> for Img<Vec<T>> {
789 #[allow(deprecated)]
790 fn from(img: Img<Cow<'_, [T]>>) -> Self {
791 Self {
792 width: img.width,
793 height: img.height,
794 stride: img.stride,
795 buf: img.buf.into_owned(),
796 }
797 }
798}
799
800impl<T: Clone> From<ImgVec<T>> for Img<Cow<'static, [T]>> {
801 #[allow(deprecated)]
802 fn from(img: ImgVec<T>) -> Self {
803 Img {
804 width: img.width,
805 height: img.height,
806 stride: img.stride,
807 buf: img.buf.into(),
808 }
809 }
810}
811
812impl<'a, T: Clone> From<ImgRef<'a, T>> for Img<Cow<'a, [T]>> {
813 #[allow(deprecated)]
814 fn from(img: ImgRef<'a, T>) -> Self {
815 Img {
816 buf: img.buf.into(),
817 width: img.width,
818 height: img.height,
819 stride: img.stride,
820 }
821 }
822}
823
824impl<T: Clone> Img<Cow<'_, [T]>> {
825 #[allow(deprecated)]
829 #[must_use]
830 pub fn into_owned(self) -> ImgVec<T> {
831 match self.buf {
832 Cow::Borrowed(_) => {
833 let tmp = self.as_ref();
834 let (buf, w, h) = tmp.to_contiguous_buf();
835 ImgVec::new(buf.into_owned(), w, h)
836 },
837 Cow::Owned(buf) => Img {
838 buf,
839 width: self.width,
840 height: self.height,
841 stride: self.stride,
842 },
843 }
844 }
845}
846
847impl<T> Img<T> where T: ToOwned {
848 #[allow(deprecated)]
852 pub fn to_owned(&self) -> Img<T::Owned> {
853 Img {
854 buf: self.buf.to_owned(),
855 width: self.width,
856 height: self.height,
857 stride: self.stride,
858 }
859 }
860}
861
862#[cfg(test)]
863mod tests {
864 use super::*;
865 use alloc::vec;
866
867 mod with_opinionated_container {
868 use super::*;
869
870 struct IDontDeriveAnything;
871
872 #[test]
873 fn compiles() {
874 let _ = Img::new(IDontDeriveAnything, 1, 1);
875 }
876 }
877
878 #[test]
879 fn with_vec() {
880 let bytes = vec![0u8;20];
881 let old = Img::new_stride(bytes, 10,2,10);
882 let _ = old.new_buf(vec![6u16;20]);
883 }
884
885 #[test]
886 fn zero() {
887 let bytes = vec![0u8];
888 let mut img = Img::new_stride(bytes,0,0,1);
889 let _ = img.sub_image(0,0,0,0);
890 let _ = img.sub_image_mut(0,0,0,0);
891 let _ = img.as_ref();
892 }
893
894 #[test]
895 fn zero_width() {
896 let bytes = vec![0u8];
897 let mut img = Img::new_stride(bytes,0,1,1);
898 let _ = img.sub_image(0,1,0,0);
899 let _ = img.sub_image_mut(0,0,0,1);
900 }
901
902 #[test]
903 fn zero_height() {
904 let bytes = vec![0u8];
905 let mut img = Img::new_stride(bytes,1,0,1);
906 assert_eq!(0, img.rows().count());
907 let _ = img.sub_image(1,0,0,0);
908 let _ = img.sub_image_mut(0,0,1,0);
909 }
910
911 #[test]
912 #[allow(deprecated)]
913 fn with_slice() {
914 let bytes = vec![0u8;20];
915 let _ = Img::new_stride(bytes.as_slice(), 10,2,10);
916 let vec = ImgVec::new_stride(bytes, 10,2,10);
917
918 #[cfg(feature = "deprecated")]
919 for _ in vec.iter() {}
920
921 assert_eq!(2, vec.rows().count());
922 for _ in *vec.as_ref().buf() {}
923
924 #[cfg(feature = "deprecated")]
925 for _ in vec {}
926 }
927
928 #[test]
929 fn sub() {
930 let img = Img::new_stride(vec![1,2,3,4,
931 5,6,7,8,
932 9], 3, 2, 4);
933 assert_eq!(img.buf()[img.stride()], 5);
934 assert_eq!(img.buf()[img.stride() + img.width()-1], 7);
935
936 assert_eq!(img.pixels().count(), img.width() * img.height());
937 assert_eq!(img.pixels().sum::<i32>(), 24);
938
939 {
940 let refimg = img.as_ref();
941 let refimg2 = refimg; let s1 = refimg.sub_image(1, 0, refimg.width()-1, refimg.height());
945 let _ = s1.sub_image(1, 0, s1.width()-1, s1.height());
946
947 let subimg = refimg.sub_image(1, 1, 2, 1);
948 assert_eq!(subimg.pixels().count(), subimg.width() * subimg.height());
949
950 assert_eq!(subimg.buf()[0], 6);
951 assert_eq!(subimg.stride(), refimg2.stride());
952 assert!(subimg.stride() * subimg.height() + subimg.width() - subimg.stride() <= subimg.buf().len());
953 assert_eq!(refimg.buf()[0], 1);
954 assert_eq!(1, subimg.rows().count());
955 }
956
957 let mut img_for_mut_1 = img.clone();
959 let mut img_for_mut_2 = img.clone();
960 let mut img_for_mut_3 = img;
961 for mut subimg in [
962 img_for_mut_1.sub_image_mut(1, 1, 2, 1),
963 img_for_mut_2.as_mut().sub_image_mut(1, 1, 2, 1),
964 img_for_mut_3.as_mut().into_sub_image_mut(1, 1, 2, 1),
965 ] {
966 assert_eq!(1, subimg.rows().count());
967 assert_eq!(1, subimg.rows_mut().count());
968 assert_eq!(1, subimg.rows_mut().rev().count());
969 assert_eq!(1, subimg.rows_mut().fuse().rev().count());
970 assert_eq!(subimg.buf()[0], 6);
971 }
972 }
973
974 #[test]
975 fn rows() {
976 let img = ImgVec::new_stride(vec![0u8; 10000], 10, 15, 100);
977 assert_eq!(img.height(), img.rows().count());
978 assert_eq!(img.height(), img.rows().rev().count());
979 assert_eq!(img.height(), img.rows().fuse().rev().count());
980 }
981
982 #[test]
983 fn mut_pixels() {
984 for y in 1..15 {
985 for x in 1..10 {
986 let mut img = ImgVec::new_stride(vec![0u8; 10000], x, y, 100);
987 assert_eq!(x*y, img.pixels_mut().count());
988 assert_eq!(x*y, img.as_mut().pixels().count());
989 assert_eq!(x*y, img.as_mut().pixels_mut().count());
990 assert_eq!(x*y, img.as_mut().as_ref().pixels().count());
991 }
992 }
993 }
994
995 #[test]
996 fn into_contiguous_buf() {
997 for in_h in [1, 2, 3, 38, 39, 40, 41].iter().copied() {
998 for in_w in [1, 2, 3, 120, 121].iter().copied() {
999 for stride in [in_w, 121, 122, 166, 242, 243].iter().copied() {
1000 let img = ImgVec::new_stride((0..10000).map(|x| x as u8).collect(), in_w, in_h, stride)
1001 .map_buf(|x| x);
1002 let pixels: Vec<_> = img.pixels().collect();
1003 let (buf, w, h) = img.into_contiguous_buf();
1004 assert_eq!(pixels, buf);
1005 assert_eq!(in_w*in_h, buf.len());
1006 assert_eq!(10000, buf.capacity());
1007 assert_eq!(in_w, w);
1008 assert_eq!(in_h, h);
1009 }
1010 }
1011 }
1012
1013 let img = ImgVec::new((0..55*33).map(|x| x as u8).collect(), 55, 33);
1014 let pixels: Vec<_> = img.pixels().collect();
1015 let tmp = img.as_ref();
1016 let (buf, ..) = tmp.to_contiguous_buf();
1017 assert_eq!(&pixels[..], &buf[..]);
1018 let (buf, ..) = img.into_contiguous_buf();
1019 assert_eq!(pixels, buf);
1020 }
1021}