From 8298586d9f3fbc4b58191bf6780b455ddd0e9d2f Mon Sep 17 00:00:00 2001 From: meh Date: Mon, 26 Oct 2015 14:44:06 +0100 Subject: [PATCH] util/frame/video: refactor data() and data_mut() --- src/util/frame/video.rs | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/util/frame/video.rs b/src/util/frame/video.rs index 8a4aa33..8e447a9 100644 --- a/src/util/frame/video.rs +++ b/src/util/frame/video.rs @@ -267,32 +267,26 @@ impl Video { } } - pub fn data(&self) -> Vec<&[u8]> { - let mut result = Vec::new(); - - unsafe { - for (i, length) in (*self.as_ptr()).linesize.iter().take_while(|l| **l > 0).enumerate() { - result.push(slice::from_raw_parts( - (*self.as_ptr()).data[i], - *length as usize * self.height() as usize)); - } + pub fn data(&self, index: usize) -> &[u8] { + if index >= self.planes() { + panic!("out of bounds"); } - result + unsafe { + slice::from_raw_parts((*self.as_ptr()).data[index], + (*self.as_ptr()).linesize[index] as usize * self.height() as usize) + } } - pub fn data_mut(&mut self) -> Vec<&mut [u8]> { - let mut result = Vec::new(); - - unsafe { - for (i, length) in (*self.as_ptr()).linesize.iter().take_while(|l| **l > 0).enumerate() { - result.push(slice::from_raw_parts_mut( - (*self.as_mut_ptr()).data[i], - *length as usize * self.height() as usize)); - } + pub fn data_mut(&mut self, index: usize) -> &mut [u8] { + if index >= self.planes() { + panic!("out of bounds"); } - result + unsafe { + slice::from_raw_parts_mut((*self.as_mut_ptr()).data[index], + (*self.as_ptr()).linesize[index] as usize * self.height() as usize) + } } }