core: fix most clippy warnings
[nihav.git] / nihav-core / src / formats.rs
index 952e4c042953272be35bb17a9969fda202b987ed..9a1d4e8cf1d712fee53a6632b6d2956bb3c6b9d3 100644 (file)
@@ -30,21 +30,21 @@ impl NASoniton {
         let is_pl = (flags & SONITON_FLAG_PLANAR) != 0;
         let is_fl = (flags & SONITON_FLAG_FLOAT) != 0;
         let is_sg = (flags & SONITON_FLAG_SIGNED) != 0;
-        NASoniton { bits: bits, be: is_be, packed: is_pk, planar: is_pl, float: is_fl, signed: is_sg }
+        NASoniton { bits, be: is_be, packed: is_pk, planar: is_pl, float: is_fl, signed: is_sg }
     }
 
-    pub fn get_bits(&self)  -> u8   { self.bits }
-    pub fn is_be(&self)     -> bool { self.be }
-    pub fn is_packed(&self) -> bool { self.packed }
-    pub fn is_planar(&self) -> bool { self.planar }
-    pub fn is_float(&self)  -> bool { self.float }
-    pub fn is_signed(&self) -> bool { self.signed }
+    pub fn get_bits(self)   -> u8   { self.bits }
+    pub fn is_be(self)      -> bool { self.be }
+    pub fn is_packed(self)  -> bool { self.packed }
+    pub fn is_planar(self)  -> bool { self.planar }
+    pub fn is_float(self)   -> bool { self.float }
+    pub fn is_signed(self)  -> bool { self.signed }
 
-    pub fn get_audio_size(&self, length: u64) -> usize {
+    pub fn get_audio_size(self, length: u64) -> usize {
         if self.packed {
-            ((length * (self.bits as u64) + 7) >> 3) as usize
+            ((length * u64::from(self.bits) + 7) >> 3) as usize
         } else {
-            (length * (((self.bits + 7) >> 3) as u64)) as usize
+            (length * u64::from((self.bits + 7) >> 3)) as usize
         }
     }
 }
@@ -63,8 +63,8 @@ pub enum NAChannelType {
 }
 
 impl NAChannelType {
-    pub fn is_center(&self) -> bool {
-        match *self {
+    pub fn is_center(self) -> bool {
+        match self {
             NAChannelType::C => true,   NAChannelType::Ch => true,
             NAChannelType::Cl => true,  NAChannelType::Ov => true,
             NAChannelType::LFE => true, NAChannelType::LFE2 => true,
@@ -72,8 +72,8 @@ impl NAChannelType {
             _ => false,
         }
     }
-    pub fn is_left(&self) -> bool {
-        match *self {
+    pub fn is_left(self) -> bool {
+        match self {
             NAChannelType::L   => true, NAChannelType::Ls => true,
             NAChannelType::Lss => true, NAChannelType::Lc => true,
             NAChannelType::Lh  => true, NAChannelType::Lw => true,
@@ -82,8 +82,8 @@ impl NAChannelType {
             _ => false,
         }
     }
-    pub fn is_right(&self) -> bool {
-        match *self {
+    pub fn is_right(self) -> bool {
+        match self {
             NAChannelType::R   => true, NAChannelType::Rs => true,
             NAChannelType::Rss => true, NAChannelType::Rc => true,
             NAChannelType::Rh  => true, NAChannelType::Rw => true,
@@ -171,7 +171,7 @@ impl fmt::Display for NAChannelType {
     }
 }
 
-#[derive(Clone)]
+#[derive(Clone,Default)]
 pub struct NAChannelMap {
     ids: Vec<NAChannelType>,
 }
@@ -196,8 +196,8 @@ impl NAChannelMap {
         self.ids.push(ch);
     }
     pub fn add_channels(&mut self, chs: &[NAChannelType]) {
-        for i in 0..chs.len() {
-            self.ids.push(chs[i]);
+        for e in chs.iter() {
+            self.ids.push(*e);
         }
     }
     pub fn num_channels(&self) -> usize {
@@ -214,9 +214,9 @@ impl NAChannelMap {
     }
     pub fn from_ms_mapping(chmap: u32) -> Self {
         let mut cm = NAChannelMap::new();
-        for i in 0..MS_CHANNEL_MAP.len() {
+        for (i, ch) in MS_CHANNEL_MAP.iter().enumerate() {
             if ((chmap >> i) & 1) != 0 {
-                cm.add_channel(MS_CHANNEL_MAP[i]);
+                cm.add_channel(*ch);
             }
         }
         cm
@@ -227,7 +227,7 @@ impl fmt::Display for NAChannelMap {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         let mut map = String::new();
         for el in self.ids.iter() {
-            if map.len() > 0 { map.push(','); }
+            if !map.is_empty() { map.push(','); }
             map.push_str(&*el.to_string());
         }
         write!(f, "{}", map)
@@ -291,12 +291,34 @@ pub enum ColorModel {
 }
 
 impl ColorModel {
-    pub fn get_default_components(&self) -> usize {
-        match *self {
+    pub fn get_default_components(self) -> usize {
+        match self {
             ColorModel::CMYK => 4,
             _                => 3,
         }
     }
+    pub fn is_rgb(self) -> bool {
+        match self {
+            ColorModel::RGB(_) => true,
+            _ => false,
+        }
+    }
+    pub fn is_yuv(self) -> bool {
+        match self {
+            ColorModel::YUV(_) => true,
+            _ => false,
+        }
+    }
+    pub fn get_short_name(self) -> &'static str {
+        match self {
+            ColorModel::RGB(_)   => "rgb",
+            ColorModel::YUV(_)   => "yuv",
+            ColorModel::CMYK     => "cmyk",
+            ColorModel::HSV      => "hsv",
+            ColorModel::LAB      => "lab",
+            ColorModel::XYZ      => "xyz",
+        }
+    }
 }
 
 impl fmt::Display for ColorModel {
@@ -315,29 +337,30 @@ impl fmt::Display for ColorModel {
 
 #[derive(Clone,Copy,PartialEq)]
 pub struct NAPixelChromaton {
-    h_ss:           u8,
-    v_ss:           u8,
-    packed:         bool,
-    depth:          u8,
-    shift:          u8,
-    comp_offs:      u8,
-    next_elem:      u8,
+    pub h_ss:           u8,
+    pub v_ss:           u8,
+    pub packed:         bool,
+    pub depth:          u8,
+    pub shift:          u8,
+    pub comp_offs:      u8,
+    pub next_elem:      u8,
 }
 
 pub const FORMATON_FLAG_BE      :u32 = 0x01;
 pub const FORMATON_FLAG_ALPHA   :u32 = 0x02;
 pub const FORMATON_FLAG_PALETTE :u32 = 0x04;
 
+pub const MAX_CHROMATONS: usize = 5;
 
 #[derive(Clone,Copy,PartialEq)]
 pub struct NAPixelFormaton {
-    model:      ColorModel,
-    components: u8,
-    comp_info:  [Option<NAPixelChromaton>; 5],
-    elem_size:  u8,
-    be:         bool,
-    alpha:      bool,
-    palette:    bool,
+    pub model:      ColorModel,
+    pub components: u8,
+    pub comp_info:  [Option<NAPixelChromaton>; MAX_CHROMATONS],
+    pub elem_size:  u8,
+    pub be:         bool,
+    pub alpha:      bool,
+    pub palette:    bool,
 }
 
 macro_rules! chromaton {
@@ -397,9 +420,9 @@ pub const RGB565_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::
 
 pub const RGB24_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3,
                                         comp_info: [
-                                            chromaton!(packrgb; 8, 0, 2, 3),
-                                            chromaton!(packrgb; 8, 0, 1, 3),
                                             chromaton!(packrgb; 8, 0, 0, 3),
+                                            chromaton!(packrgb; 8, 0, 1, 3),
+                                            chromaton!(packrgb; 8, 0, 2, 3),
                                             None, None],
                                         elem_size: 3, be: false, alpha: false, palette: false };
 
@@ -407,20 +430,20 @@ impl NAPixelChromaton {
     pub fn new(h_ss: u8, v_ss: u8, packed: bool, depth: u8, shift: u8, comp_offs: u8, next_elem: u8) -> Self {
         Self { h_ss, v_ss, packed, depth, shift, comp_offs, next_elem }
     }
-    pub fn get_subsampling(&self) -> (u8, u8) { (self.h_ss, self.v_ss) }
-    pub fn is_packed(&self) -> bool { self.packed }
-    pub fn get_depth(&self) -> u8   { self.depth }
-    pub fn get_shift(&self) -> u8   { self.shift }
-    pub fn get_offset(&self) -> u8  { self.comp_offs }
-    pub fn get_step(&self)  -> u8   { self.next_elem }
-
-    pub fn get_width(&self, width: usize) -> usize {
+    pub fn get_subsampling(self) -> (u8, u8) { (self.h_ss, self.v_ss) }
+    pub fn is_packed(self) -> bool { self.packed }
+    pub fn get_depth(self) -> u8   { self.depth }
+    pub fn get_shift(self) -> u8   { self.shift }
+    pub fn get_offset(self) -> u8  { self.comp_offs }
+    pub fn get_step(self)  -> u8   { self.next_elem }
+
+    pub fn get_width(self, width: usize) -> usize {
         (width  + ((1 << self.h_ss) - 1)) >> self.h_ss
     }
-    pub fn get_height(&self, height: usize) -> usize {
+    pub fn get_height(self, height: usize) -> usize {
         (height + ((1 << self.v_ss) - 1)) >> self.v_ss
     }
-    pub fn get_linesize(&self, width: usize) -> usize {
+    pub fn get_linesize(self, width: usize) -> usize {
         let d = self.depth as usize;
         if self.packed {
             (self.get_width(width) * d + d - 1) >> 3
@@ -428,7 +451,7 @@ impl NAPixelChromaton {
             self.get_width(width)
         }
     }
-    pub fn get_data_size(&self, width: usize, height: usize) -> usize {
+    pub fn get_data_size(self, width: usize, height: usize) -> usize {
         let nh = (height + ((1 << self.v_ss) - 1)) >> self.v_ss;
         self.get_linesize(width) * nh
     }
@@ -454,7 +477,7 @@ impl NAPixelFormaton {
                comp4: Option<NAPixelChromaton>,
                comp5: Option<NAPixelChromaton>,
                flags: u32, elem_size: u8) -> Self {
-        let mut chromatons: [Option<NAPixelChromaton>; 5] = [None; 5];
+        let mut chromatons: [Option<NAPixelChromaton>; MAX_CHROMATONS] = [None; MAX_CHROMATONS];
         let mut ncomp = 0;
         let be      = (flags & FORMATON_FLAG_BE)      != 0;
         let alpha   = (flags & FORMATON_FLAG_ALPHA)   != 0;
@@ -464,11 +487,11 @@ impl NAPixelFormaton {
         if let Some(c) = comp3 { chromatons[2] = Some(c); ncomp += 1; }
         if let Some(c) = comp4 { chromatons[3] = Some(c); ncomp += 1; }
         if let Some(c) = comp5 { chromatons[4] = Some(c); ncomp += 1; }
-        NAPixelFormaton { model: model,
+        NAPixelFormaton { model,
                           components: ncomp,
                           comp_info: chromatons,
-                          elem_size: elem_size,
-                         be: be, alpha: alpha, palette: palette }
+                          elem_size,
+                          be, alpha, palette }
     }
 
     pub fn get_model(&self) -> ColorModel { self.model }
@@ -477,10 +500,38 @@ impl NAPixelFormaton {
         if idx < self.comp_info.len() { return self.comp_info[idx]; }
         None
     }
-    pub fn is_be(&self) -> bool { self.be }
-    pub fn has_alpha(&self) -> bool { self.alpha }
-    pub fn is_paletted(&self) -> bool { self.palette }
-    pub fn get_elem_size(&self) -> u8 { self.elem_size }
+    pub fn is_be(self) -> bool { self.be }
+    pub fn has_alpha(self) -> bool { self.alpha }
+    pub fn is_paletted(self) -> bool { self.palette }
+    pub fn get_elem_size(self) -> u8 { self.elem_size }
+    pub fn is_unpacked(&self) -> bool {
+        if self.palette { return false; }
+        for chr in self.comp_info.iter() {
+            if let Some(ref chromaton) = chr {
+                if chromaton.is_packed() { return false; }
+            }
+        }
+        true
+    }
+    pub fn get_max_depth(&self) -> u8 {
+        let mut mdepth = 0;
+        for chr in self.comp_info.iter() {
+            if let Some(ref chromaton) = chr {
+                mdepth = mdepth.max(chromaton.depth);
+            }
+        }
+        mdepth
+    }
+    pub fn get_max_subsampling(&self) -> u8 {
+        let mut ssamp = 0;
+        for chr in self.comp_info.iter() {
+            if let Some(ref chromaton) = chr {
+                let (ss_v, ss_h) = chromaton.get_subsampling();
+                ssamp = ssamp.max(ss_v).max(ss_h);
+            }
+        }
+        ssamp
+    }
 }
 
 impl fmt::Display for NAPixelFormaton {