Commit | Line | Data |
---|---|---|
33b5689a KS |
1 | //! Audio and image sample format definitions. |
2 | //! | |
3 | //! NihAV does not have a fixed list of supported formats but rather accepts format definitions both for audio and video. | |
4 | //! In result exotic formats like YUV410+alpha plane that is used by Indeo 4 are supported without any additional case handing. | |
5 | //! Some common format definitions are provided as constants for convenience. | |
32ce974d | 6 | use std::str::FromStr; |
fba6f8e4 KS |
7 | use std::string::*; |
8 | use std::fmt; | |
9 | ||
33b5689a KS |
10 | /// Audio format definition. |
11 | /// | |
12 | /// The structure describes how audio samples are stored and what characteristics they have. | |
b68ff5ae | 13 | #[derive(Debug,Copy,Clone,PartialEq)] |
fba6f8e4 | 14 | pub struct NASoniton { |
33b5689a | 15 | /// Bits per sample. |
a92a5113 | 16 | pub bits: u8, |
33b5689a | 17 | /// Audio format is big-endian. |
a92a5113 | 18 | pub be: bool, |
33b5689a | 19 | /// Audio samples are packed (e.g. 20-bit audio samples). |
a92a5113 | 20 | pub packed: bool, |
33b5689a | 21 | /// Audio data is stored in planar format instead of interleaving samples for different channels. |
a92a5113 | 22 | pub planar: bool, |
33b5689a | 23 | /// Audio data is in floating point format. |
a92a5113 | 24 | pub float: bool, |
33b5689a | 25 | /// Audio data is signed (usually only 8-bit audio is unsigned). |
a92a5113 | 26 | pub signed: bool, |
fba6f8e4 KS |
27 | } |
28 | ||
33b5689a KS |
29 | /// Flag for specifying that audio format is big-endian in `NASoniton::`[`new`]`()`. Related to [`be`] field of `NASoniton`. |
30 | /// | |
31 | /// [`new`]: ./struct.NASoniton.html#method.new | |
32 | /// [`be`]: ./struct.NASoniton.html#structfield.be | |
9e9a3af1 | 33 | pub const SONITON_FLAG_BE :u32 = 0x01; |
33b5689a KS |
34 | /// Flag for specifying that audio format has packed samples in `NASoniton::`[`new`]`()`. Related to [`packed`] field of `NASoniton`. |
35 | /// | |
36 | /// [`new`]: ./struct.NASoniton.html#method.new | |
37 | /// [`packed`]: ./struct.NASoniton.html#structfield.packed | |
9e9a3af1 | 38 | pub const SONITON_FLAG_PACKED :u32 = 0x02; |
33b5689a KS |
39 | /// Flag for specifying that audio data is stored as planar in `NASoniton::`[`new`]`()`. Related to [`planar`] field of `NASoniton`. |
40 | /// | |
41 | /// [`new`]: ./struct.NASoniton.html#method.new | |
42 | /// [`planar`]: ./struct.NASoniton.html#structfield.planar | |
9e9a3af1 | 43 | pub const SONITON_FLAG_PLANAR :u32 = 0x04; |
33b5689a KS |
44 | /// Flag for specifying that audio samples are in floating point format in `NASoniton::`[`new`]`()`. Related to [`float`] field of `NASoniton`. |
45 | /// | |
46 | /// [`new`]: ./struct.NASoniton.html#method.new | |
47 | /// [`float`]: ./struct.NASoniton.html#structfield.float | |
9e9a3af1 | 48 | pub const SONITON_FLAG_FLOAT :u32 = 0x08; |
33b5689a KS |
49 | /// Flag for specifying that audio format is signed in `NASoniton::`[`new`]`()`. Related to [`signed`] field of `NASoniton`. |
50 | /// | |
51 | /// [`new`]: ./struct.NASoniton.html#method.new | |
52 | /// [`signed`]: ./struct.NASoniton.html#structfield.signed | |
9e9a3af1 | 53 | pub const SONITON_FLAG_SIGNED :u32 = 0x10; |
fba6f8e4 | 54 | |
33b5689a | 55 | /// Predefined format for interleaved 8-bit unsigned audio. |
fba6f8e4 | 56 | pub const SND_U8_FORMAT: NASoniton = NASoniton { bits: 8, be: false, packed: false, planar: false, float: false, signed: false }; |
33b5689a | 57 | /// Predefined format for interleaved 16-bit signed audio. |
fba6f8e4 | 58 | pub const SND_S16_FORMAT: NASoniton = NASoniton { bits: 16, be: false, packed: false, planar: false, float: false, signed: true }; |
33b5689a | 59 | /// Predefined format for planar 16-bit signed audio. |
49fde921 | 60 | pub const SND_S16P_FORMAT: NASoniton = NASoniton { bits: 16, be: false, packed: false, planar: true, float: false, signed: true }; |
33b5689a | 61 | /// Predefined format for planar 32-bit floating point audio. |
126b7eb8 | 62 | pub const SND_F32P_FORMAT: NASoniton = NASoniton { bits: 32, be: false, packed: false, planar: true, float: true, signed: true }; |
fba6f8e4 KS |
63 | |
64 | impl NASoniton { | |
33b5689a KS |
65 | /// Constructs a new audio format definition using flags like [`SONITON_FLAG_BE`]. |
66 | /// | |
67 | /// [`SONITON_FLAG_BE`]: ./constant.SONITON_FLAG_BE.html | |
9e9a3af1 KS |
68 | pub fn new(bits: u8, flags: u32) -> Self { |
69 | let is_be = (flags & SONITON_FLAG_BE) != 0; | |
70 | let is_pk = (flags & SONITON_FLAG_PACKED) != 0; | |
71 | let is_pl = (flags & SONITON_FLAG_PLANAR) != 0; | |
72 | let is_fl = (flags & SONITON_FLAG_FLOAT) != 0; | |
73 | let is_sg = (flags & SONITON_FLAG_SIGNED) != 0; | |
e243ceb4 | 74 | NASoniton { bits, be: is_be, packed: is_pk, planar: is_pl, float: is_fl, signed: is_sg } |
fba6f8e4 KS |
75 | } |
76 | ||
33b5689a | 77 | /// Returns the number of bits per sample. |
e243ceb4 | 78 | pub fn get_bits(self) -> u8 { self.bits } |
33b5689a | 79 | /// Reports whether the format is big-endian. |
e243ceb4 | 80 | pub fn is_be(self) -> bool { self.be } |
33b5689a | 81 | /// Reports whether the format has packed samples. |
e243ceb4 | 82 | pub fn is_packed(self) -> bool { self.packed } |
33b5689a | 83 | /// Reports whether audio data is planar instead of interleaved. |
e243ceb4 | 84 | pub fn is_planar(self) -> bool { self.planar } |
33b5689a | 85 | /// Reports whether audio samples are in floating point format. |
e243ceb4 | 86 | pub fn is_float(self) -> bool { self.float } |
33b5689a | 87 | /// Reports whether audio samples are signed. |
e243ceb4 | 88 | pub fn is_signed(self) -> bool { self.signed } |
15e41b31 | 89 | |
33b5689a | 90 | /// Returns the amount of bytes needed to store the audio of requested length (in samples). |
e243ceb4 | 91 | pub fn get_audio_size(self, length: u64) -> usize { |
15e41b31 | 92 | if self.packed { |
e243ceb4 | 93 | ((length * u64::from(self.bits) + 7) >> 3) as usize |
15e41b31 | 94 | } else { |
e243ceb4 | 95 | (length * u64::from((self.bits + 7) >> 3)) as usize |
15e41b31 KS |
96 | } |
97 | } | |
11d889bb KS |
98 | |
99 | /// Returns soniton description as a short string. | |
100 | pub fn to_short_string(&self) -> String { | |
101 | let ltype = if self.float { 'f' } else if self.signed { 's' } else { 'u' }; | |
102 | let endianness = if self.bits == 8 { "" } else if self.be { "be" } else { "le" }; | |
103 | let planar = if self.planar { "p" } else { "" }; | |
104 | let packed = if self.packed { "x" } else { "" }; | |
105 | format!("{}{}{}{}{}", ltype, self.bits, endianness, planar, packed) | |
106 | } | |
fba6f8e4 KS |
107 | } |
108 | ||
109 | impl fmt::Display for NASoniton { | |
110 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
111 | let fmt = if self.float { "float" } else if self.signed { "int" } else { "uint" }; | |
112 | let end = if self.be { "BE" } else { "LE" }; | |
113 | write!(f, "({} bps, {} planar: {} packed: {} {})", self.bits, end, self.packed, self.planar, fmt) | |
114 | } | |
115 | } | |
116 | ||
11d889bb KS |
117 | /// Generic soniton parsing error. |
118 | #[derive(Clone,Copy,Debug,PartialEq)] | |
119 | pub struct SonitonParseError {} | |
120 | ||
121 | impl FromStr for NASoniton { | |
122 | type Err = SonitonParseError; | |
123 | ||
124 | fn from_str(s: &str) -> Result<Self, Self::Err> { | |
125 | match s { | |
126 | "u8" => Ok(NASoniton { bits: 8, be: true, packed: false, planar: false, float: false, signed: false }), | |
127 | "s16be" => Ok(NASoniton { bits: 16, be: true, packed: false, planar: false, float: false, signed: true }), | |
128 | "s16le" => Ok(NASoniton { bits: 16, be: false, packed: false, planar: false, float: false, signed: true }), | |
129 | "s24be" => Ok(NASoniton { bits: 24, be: true, packed: false, planar: false, float: false, signed: true }), | |
130 | "s24le" => Ok(NASoniton { bits: 24, be: false, packed: false, planar: false, float: false, signed: true }), | |
131 | "s32be" => Ok(NASoniton { bits: 32, be: true, packed: false, planar: false, float: false, signed: true }), | |
132 | "s32le" => Ok(NASoniton { bits: 32, be: false, packed: false, planar: false, float: false, signed: true }), | |
133 | "f32be" => Ok(NASoniton { bits: 32, be: true, packed: false, planar: false, float: true, signed: true }), | |
134 | "f32le" => Ok(NASoniton { bits: 32, be: false, packed: false, planar: false, float: true, signed: true }), | |
135 | _ => Err(SonitonParseError{}), | |
136 | } | |
137 | } | |
138 | } | |
139 | ||
33b5689a | 140 | /// Known channel types. |
10a00d52 | 141 | #[derive(Debug,Clone,Copy,PartialEq)] |
fba6f8e4 KS |
142 | pub enum NAChannelType { |
143 | C, L, R, Cs, Ls, Rs, Lss, Rss, LFE, Lc, Rc, Lh, Rh, Ch, LFE2, Lw, Rw, Ov, Lhs, Rhs, Chs, Ll, Rl, Cl, Lt, Rt, Lo, Ro | |
144 | } | |
145 | ||
146 | impl NAChannelType { | |
33b5689a | 147 | /// Reports whether this is some center channel. |
e243ceb4 KS |
148 | pub fn is_center(self) -> bool { |
149 | match self { | |
fba6f8e4 KS |
150 | NAChannelType::C => true, NAChannelType::Ch => true, |
151 | NAChannelType::Cl => true, NAChannelType::Ov => true, | |
152 | NAChannelType::LFE => true, NAChannelType::LFE2 => true, | |
153 | NAChannelType::Cs => true, NAChannelType::Chs => true, | |
154 | _ => false, | |
155 | } | |
156 | } | |
33b5689a | 157 | /// Reports whether this is some left channel. |
e243ceb4 KS |
158 | pub fn is_left(self) -> bool { |
159 | match self { | |
fba6f8e4 KS |
160 | NAChannelType::L => true, NAChannelType::Ls => true, |
161 | NAChannelType::Lss => true, NAChannelType::Lc => true, | |
162 | NAChannelType::Lh => true, NAChannelType::Lw => true, | |
163 | NAChannelType::Lhs => true, NAChannelType::Ll => true, | |
164 | NAChannelType::Lt => true, NAChannelType::Lo => true, | |
165 | _ => false, | |
166 | } | |
167 | } | |
33b5689a | 168 | /// Reports whether this is some right channel. |
e243ceb4 KS |
169 | pub fn is_right(self) -> bool { |
170 | match self { | |
fba6f8e4 KS |
171 | NAChannelType::R => true, NAChannelType::Rs => true, |
172 | NAChannelType::Rss => true, NAChannelType::Rc => true, | |
173 | NAChannelType::Rh => true, NAChannelType::Rw => true, | |
174 | NAChannelType::Rhs => true, NAChannelType::Rl => true, | |
175 | NAChannelType::Rt => true, NAChannelType::Ro => true, | |
176 | _ => false, | |
177 | } | |
178 | } | |
179 | } | |
180 | ||
33b5689a | 181 | /// Generic channel configuration parsing error. |
32ce974d KS |
182 | #[derive(Clone,Copy,Debug,PartialEq)] |
183 | pub struct ChannelParseError {} | |
184 | ||
185 | impl FromStr for NAChannelType { | |
186 | type Err = ChannelParseError; | |
187 | ||
188 | fn from_str(s: &str) -> Result<Self, Self::Err> { | |
189 | match s { | |
190 | "C" => Ok(NAChannelType::C), | |
191 | "L" => Ok(NAChannelType::L), | |
192 | "R" => Ok(NAChannelType::R), | |
193 | "Cs" => Ok(NAChannelType::Cs), | |
194 | "Ls" => Ok(NAChannelType::Ls), | |
195 | "Rs" => Ok(NAChannelType::Rs), | |
196 | "Lss" => Ok(NAChannelType::Lss), | |
197 | "Rss" => Ok(NAChannelType::Rss), | |
198 | "LFE" => Ok(NAChannelType::LFE), | |
199 | "Lc" => Ok(NAChannelType::Lc), | |
200 | "Rc" => Ok(NAChannelType::Rc), | |
201 | "Lh" => Ok(NAChannelType::Lh), | |
202 | "Rh" => Ok(NAChannelType::Rh), | |
203 | "Ch" => Ok(NAChannelType::Ch), | |
204 | "LFE2" => Ok(NAChannelType::LFE2), | |
205 | "Lw" => Ok(NAChannelType::Lw), | |
206 | "Rw" => Ok(NAChannelType::Rw), | |
207 | "Ov" => Ok(NAChannelType::Ov), | |
208 | "Lhs" => Ok(NAChannelType::Lhs), | |
209 | "Rhs" => Ok(NAChannelType::Rhs), | |
210 | "Chs" => Ok(NAChannelType::Chs), | |
211 | "Ll" => Ok(NAChannelType::Ll), | |
212 | "Rl" => Ok(NAChannelType::Rl), | |
213 | "Cl" => Ok(NAChannelType::Cl), | |
214 | "Lt" => Ok(NAChannelType::Lt), | |
215 | "Rt" => Ok(NAChannelType::Rt), | |
216 | "Lo" => Ok(NAChannelType::Lo), | |
217 | "Ro" => Ok(NAChannelType::Ro), | |
218 | _ => Err(ChannelParseError{}), | |
1a151e53 | 219 | } |
32ce974d KS |
220 | } |
221 | } | |
222 | ||
fba6f8e4 KS |
223 | impl fmt::Display for NAChannelType { |
224 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
225 | let name = match *self { | |
226 | NAChannelType::C => "C".to_string(), | |
227 | NAChannelType::L => "L".to_string(), | |
228 | NAChannelType::R => "R".to_string(), | |
229 | NAChannelType::Cs => "Cs".to_string(), | |
230 | NAChannelType::Ls => "Ls".to_string(), | |
231 | NAChannelType::Rs => "Rs".to_string(), | |
232 | NAChannelType::Lss => "Lss".to_string(), | |
233 | NAChannelType::Rss => "Rss".to_string(), | |
234 | NAChannelType::LFE => "LFE".to_string(), | |
235 | NAChannelType::Lc => "Lc".to_string(), | |
236 | NAChannelType::Rc => "Rc".to_string(), | |
237 | NAChannelType::Lh => "Lh".to_string(), | |
238 | NAChannelType::Rh => "Rh".to_string(), | |
239 | NAChannelType::Ch => "Ch".to_string(), | |
240 | NAChannelType::LFE2 => "LFE2".to_string(), | |
241 | NAChannelType::Lw => "Lw".to_string(), | |
242 | NAChannelType::Rw => "Rw".to_string(), | |
243 | NAChannelType::Ov => "Ov".to_string(), | |
244 | NAChannelType::Lhs => "Lhs".to_string(), | |
245 | NAChannelType::Rhs => "Rhs".to_string(), | |
246 | NAChannelType::Chs => "Chs".to_string(), | |
247 | NAChannelType::Ll => "Ll".to_string(), | |
248 | NAChannelType::Rl => "Rl".to_string(), | |
249 | NAChannelType::Cl => "Cl".to_string(), | |
250 | NAChannelType::Lt => "Lt".to_string(), | |
251 | NAChannelType::Rt => "Rt".to_string(), | |
252 | NAChannelType::Lo => "Lo".to_string(), | |
253 | NAChannelType::Ro => "Ro".to_string(), | |
254 | }; | |
255 | write!(f, "{}", name) | |
256 | } | |
257 | } | |
258 | ||
33b5689a KS |
259 | /// Channel map. |
260 | /// | |
261 | /// This is essentially an ordered sequence of channels. | |
e243ceb4 | 262 | #[derive(Clone,Default)] |
fba6f8e4 KS |
263 | pub struct NAChannelMap { |
264 | ids: Vec<NAChannelType>, | |
265 | } | |
266 | ||
62b33487 KS |
267 | const MS_CHANNEL_MAP: [NAChannelType; 11] = [ |
268 | NAChannelType::L, | |
269 | NAChannelType::R, | |
270 | NAChannelType::C, | |
271 | NAChannelType::LFE, | |
272 | NAChannelType::Ls, | |
273 | NAChannelType::Rs, | |
274 | NAChannelType::Lss, | |
275 | NAChannelType::Rss, | |
276 | NAChannelType::Cs, | |
277 | NAChannelType::Lc, | |
278 | NAChannelType::Rc, | |
279 | ]; | |
280 | ||
fba6f8e4 | 281 | impl NAChannelMap { |
33b5689a | 282 | /// Constructs a new `NAChannelMap` instance. |
fba6f8e4 | 283 | pub fn new() -> Self { NAChannelMap { ids: Vec::new() } } |
33b5689a | 284 | /// Adds a new channel to the map. |
fba6f8e4 KS |
285 | pub fn add_channel(&mut self, ch: NAChannelType) { |
286 | self.ids.push(ch); | |
287 | } | |
33b5689a | 288 | /// Adds several channels to the map at once. |
10a00d52 | 289 | pub fn add_channels(&mut self, chs: &[NAChannelType]) { |
e243ceb4 KS |
290 | for e in chs.iter() { |
291 | self.ids.push(*e); | |
10a00d52 KS |
292 | } |
293 | } | |
33b5689a | 294 | /// Returns the total number of channels. |
fba6f8e4 KS |
295 | pub fn num_channels(&self) -> usize { |
296 | self.ids.len() | |
297 | } | |
33b5689a | 298 | /// Reports channel type for a requested index. |
fba6f8e4 KS |
299 | pub fn get_channel(&self, idx: usize) -> NAChannelType { |
300 | self.ids[idx] | |
301 | } | |
33b5689a | 302 | /// Tries to find position of the channel with requested type. |
fba6f8e4 KS |
303 | pub fn find_channel_id(&self, t: NAChannelType) -> Option<u8> { |
304 | for i in 0..self.ids.len() { | |
305 | if self.ids[i] as i32 == t as i32 { return Some(i as u8); } | |
306 | } | |
307 | None | |
308 | } | |
33b5689a | 309 | /// Creates a new `NAChannelMap` using the channel mapping flags from WAVE format. |
62b33487 KS |
310 | pub fn from_ms_mapping(chmap: u32) -> Self { |
311 | let mut cm = NAChannelMap::new(); | |
e243ceb4 | 312 | for (i, ch) in MS_CHANNEL_MAP.iter().enumerate() { |
62b33487 | 313 | if ((chmap >> i) & 1) != 0 { |
e243ceb4 | 314 | cm.add_channel(*ch); |
62b33487 KS |
315 | } |
316 | } | |
317 | cm | |
318 | } | |
fba6f8e4 KS |
319 | } |
320 | ||
32ce974d KS |
321 | impl fmt::Display for NAChannelMap { |
322 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
323 | let mut map = String::new(); | |
324 | for el in self.ids.iter() { | |
e243ceb4 | 325 | if !map.is_empty() { map.push(','); } |
32ce974d KS |
326 | map.push_str(&*el.to_string()); |
327 | } | |
328 | write!(f, "{}", map) | |
329 | } | |
330 | } | |
331 | ||
332 | impl FromStr for NAChannelMap { | |
333 | type Err = ChannelParseError; | |
334 | ||
335 | fn from_str(s: &str) -> Result<Self, Self::Err> { | |
336 | let mut chm = NAChannelMap::new(); | |
337 | for tok in s.split(',') { | |
338 | chm.add_channel(NAChannelType::from_str(tok)?); | |
339 | } | |
340 | Ok(chm) | |
341 | } | |
342 | } | |
343 | ||
33b5689a | 344 | /// A list of RGB colour model variants. |
b68ff5ae | 345 | #[derive(Debug,Clone,Copy,PartialEq)] |
fba6f8e4 KS |
346 | pub enum RGBSubmodel { |
347 | RGB, | |
348 | SRGB, | |
349 | } | |
350 | ||
351 | impl fmt::Display for RGBSubmodel { | |
352 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
353 | let name = match *self { | |
354 | RGBSubmodel::RGB => "RGB".to_string(), | |
355 | RGBSubmodel::SRGB => "sRGB".to_string(), | |
356 | }; | |
357 | write!(f, "{}", name) | |
358 | } | |
359 | } | |
360 | ||
33b5689a | 361 | /// A list of YUV colour model variants. |
b68ff5ae | 362 | #[derive(Debug,Clone,Copy,PartialEq)] |
fba6f8e4 KS |
363 | pub enum YUVSubmodel { |
364 | YCbCr, | |
33b5689a | 365 | /// NTSC variant. |
fba6f8e4 | 366 | YIQ, |
33b5689a | 367 | /// The YUV variant used by JPEG. |
fba6f8e4 KS |
368 | YUVJ, |
369 | } | |
370 | ||
371 | impl fmt::Display for YUVSubmodel { | |
372 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
373 | let name = match *self { | |
374 | YUVSubmodel::YCbCr => "YCbCr".to_string(), | |
375 | YUVSubmodel::YIQ => "YIQ".to_string(), | |
376 | YUVSubmodel::YUVJ => "YUVJ".to_string(), | |
377 | }; | |
378 | write!(f, "{}", name) | |
379 | } | |
380 | } | |
381 | ||
33b5689a | 382 | /// A list of known colour models. |
b68ff5ae | 383 | #[derive(Debug, Clone,Copy,PartialEq)] |
fba6f8e4 KS |
384 | pub enum ColorModel { |
385 | RGB(RGBSubmodel), | |
386 | YUV(YUVSubmodel), | |
387 | CMYK, | |
388 | HSV, | |
389 | LAB, | |
390 | XYZ, | |
391 | } | |
392 | ||
393 | impl ColorModel { | |
33b5689a KS |
394 | /// Returns the number of colour model components. |
395 | /// | |
396 | /// The actual image may have more components e.g. alpha component. | |
e243ceb4 KS |
397 | pub fn get_default_components(self) -> usize { |
398 | match self { | |
fba6f8e4 KS |
399 | ColorModel::CMYK => 4, |
400 | _ => 3, | |
401 | } | |
402 | } | |
33b5689a | 403 | /// Reports whether the current colour model is RGB. |
e243ceb4 KS |
404 | pub fn is_rgb(self) -> bool { |
405 | match self { | |
386957f1 KS |
406 | ColorModel::RGB(_) => true, |
407 | _ => false, | |
408 | } | |
409 | } | |
33b5689a | 410 | /// Reports whether the current colour model is YUV. |
e243ceb4 KS |
411 | pub fn is_yuv(self) -> bool { |
412 | match self { | |
386957f1 KS |
413 | ColorModel::YUV(_) => true, |
414 | _ => false, | |
415 | } | |
416 | } | |
33b5689a | 417 | /// Returns short name for the current colour mode. |
e243ceb4 KS |
418 | pub fn get_short_name(self) -> &'static str { |
419 | match self { | |
8efb7386 KS |
420 | ColorModel::RGB(_) => "rgb", |
421 | ColorModel::YUV(_) => "yuv", | |
422 | ColorModel::CMYK => "cmyk", | |
423 | ColorModel::HSV => "hsv", | |
424 | ColorModel::LAB => "lab", | |
425 | ColorModel::XYZ => "xyz", | |
426 | } | |
427 | } | |
fba6f8e4 KS |
428 | } |
429 | ||
430 | impl fmt::Display for ColorModel { | |
431 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
432 | let name = match *self { | |
433 | ColorModel::RGB(fmt) => format!("RGB({})", fmt).to_string(), | |
434 | ColorModel::YUV(fmt) => format!("YUV({})", fmt).to_string(), | |
435 | ColorModel::CMYK => "CMYK".to_string(), | |
436 | ColorModel::HSV => "HSV".to_string(), | |
437 | ColorModel::LAB => "LAB".to_string(), | |
438 | ColorModel::XYZ => "XYZ".to_string(), | |
439 | }; | |
440 | write!(f, "{}", name) | |
441 | } | |
442 | } | |
443 | ||
33b5689a KS |
444 | /// Single colourspace component definition. |
445 | /// | |
446 | /// This structure defines how components of a colourspace are subsampled and where and how they are stored. | |
b68ff5ae | 447 | #[derive(Clone,Copy,PartialEq)] |
fba6f8e4 | 448 | pub struct NAPixelChromaton { |
33b5689a | 449 | /// Horizontal subsampling in power of two (e.g. `0` = no subsampling, `1` = only every second value is stored). |
386957f1 | 450 | pub h_ss: u8, |
33b5689a | 451 | /// Vertial subsampling in power of two (e.g. `0` = no subsampling, `1` = only every second value is stored). |
386957f1 | 452 | pub v_ss: u8, |
33b5689a | 453 | /// A flag to signal that component is packed. |
386957f1 | 454 | pub packed: bool, |
33b5689a | 455 | /// Bit depth of current component. |
386957f1 | 456 | pub depth: u8, |
33b5689a | 457 | /// Shift for packed components. |
386957f1 | 458 | pub shift: u8, |
33b5689a | 459 | /// Component offset for byte-packed components. |
386957f1 | 460 | pub comp_offs: u8, |
33b5689a | 461 | /// The distance to the next packed element in bytes. |
386957f1 | 462 | pub next_elem: u8, |
fba6f8e4 KS |
463 | } |
464 | ||
33b5689a KS |
465 | /// Flag for specifying that image data is stored big-endian in `NAPixelFormaton::`[`new`]`()`. Related to its [`be`] field. |
466 | /// | |
467 | /// [`new`]: ./struct.NAPixelFormaton.html#method.new | |
468 | /// [`be`]: ./struct.NAPixelFormaton.html#structfield.new | |
9e9a3af1 | 469 | pub const FORMATON_FLAG_BE :u32 = 0x01; |
33b5689a KS |
470 | /// Flag for specifying that image data has alpha plane in `NAPixelFormaton::`[`new`]`()`. Related to its [`alpha`] field. |
471 | /// | |
472 | /// [`new`]: ./struct.NAPixelFormaton.html#method.new | |
473 | /// [`alpha`]: ./struct.NAPixelFormaton.html#structfield.alpha | |
9e9a3af1 | 474 | pub const FORMATON_FLAG_ALPHA :u32 = 0x02; |
33b5689a KS |
475 | /// Flag for specifying that image data is stored in paletted form for `NAPixelFormaton::`[`new`]`()`. Related to its [`palette`] field. |
476 | /// | |
477 | /// [`new`]: ./struct.NAPixelFormaton.html#method.new | |
478 | /// [`palette`]: ./struct.NAPixelFormaton.html#structfield.palette | |
9e9a3af1 | 479 | pub const FORMATON_FLAG_PALETTE :u32 = 0x04; |
fba6f8e4 | 480 | |
33b5689a | 481 | /// The current limit on number of components in image colourspace model (including alpha component). |
8efb7386 | 482 | pub const MAX_CHROMATONS: usize = 5; |
fba6f8e4 | 483 | |
33b5689a KS |
484 | /// Image colourspace representation. |
485 | /// | |
486 | /// This structure includes both definitions for each component and some common definitions. | |
487 | /// For example the format can be paletted and then components describe the palette storage format while actual data is 8-bit palette indices. | |
b68ff5ae | 488 | #[derive(Clone,Copy,PartialEq)] |
fba6f8e4 | 489 | pub struct NAPixelFormaton { |
33b5689a | 490 | /// Image colour model. |
386957f1 | 491 | pub model: ColorModel, |
33b5689a | 492 | /// Actual number of components present. |
386957f1 | 493 | pub components: u8, |
33b5689a | 494 | /// Format definition for each component. |
8efb7386 | 495 | pub comp_info: [Option<NAPixelChromaton>; MAX_CHROMATONS], |
33b5689a | 496 | /// Single pixel size for packed formats. |
386957f1 | 497 | pub elem_size: u8, |
33b5689a | 498 | /// A flag signalling that data is stored as big-endian. |
386957f1 | 499 | pub be: bool, |
33b5689a | 500 | /// A flag signalling that image has alpha component. |
386957f1 | 501 | pub alpha: bool, |
33b5689a KS |
502 | /// A flag signalling that data is paletted. |
503 | /// | |
504 | /// This means that image data is stored as 8-bit indices (in the first image component) for the palette stored as second component of the image and actual palette format is described in this structure. | |
386957f1 | 505 | pub palette: bool, |
fba6f8e4 KS |
506 | } |
507 | ||
508 | macro_rules! chromaton { | |
509 | ($hs: expr, $vs: expr, $pck: expr, $d: expr, $sh: expr, $co: expr, $ne: expr) => ({ | |
510 | Some(NAPixelChromaton{ h_ss: $hs, v_ss: $vs, packed: $pck, depth: $d, shift: $sh, comp_offs: $co, next_elem: $ne }) | |
511 | }); | |
512 | (yuv8; $hs: expr, $vs: expr, $co: expr) => ({ | |
513 | Some(NAPixelChromaton{ h_ss: $hs, v_ss: $vs, packed: false, depth: 8, shift: 0, comp_offs: $co, next_elem: 1 }) | |
514 | }); | |
515 | (packrgb; $d: expr, $s: expr, $co: expr, $ne: expr) => ({ | |
516 | Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: $d, shift: $s, comp_offs: $co, next_elem: $ne }) | |
517 | }); | |
518 | (pal8; $co: expr) => ({ | |
519 | Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: $co, next_elem: 3 }) | |
520 | }); | |
521 | } | |
522 | ||
33b5689a | 523 | /// Predefined format for planar 8-bit YUV with 4:2:0 subsampling. |
fba6f8e4 KS |
524 | pub const YUV420_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 3, |
525 | comp_info: [ | |
526 | chromaton!(0, 0, false, 8, 0, 0, 1), | |
527 | chromaton!(yuv8; 1, 1, 1), | |
528 | chromaton!(yuv8; 1, 1, 2), | |
529 | None, None], | |
530 | elem_size: 0, be: false, alpha: false, palette: false }; | |
531 | ||
33b5689a | 532 | /// Predefined format for planar 8-bit YUV with 4:1:0 subsampling. |
b68ff5ae KS |
533 | pub const YUV410_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 3, |
534 | comp_info: [ | |
535 | chromaton!(0, 0, false, 8, 0, 0, 1), | |
536 | chromaton!(yuv8; 2, 2, 1), | |
537 | chromaton!(yuv8; 2, 2, 2), | |
538 | None, None], | |
539 | elem_size: 0, be: false, alpha: false, palette: false }; | |
33b5689a | 540 | /// Predefined format for planar 8-bit YUV with 4:1:0 subsampling and alpha component. |
a2a9732a KS |
541 | pub const YUVA410_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 4, |
542 | comp_info: [ | |
543 | chromaton!(0, 0, false, 8, 0, 0, 1), | |
544 | chromaton!(yuv8; 2, 2, 1), | |
545 | chromaton!(yuv8; 2, 2, 2), | |
546 | chromaton!(0, 0, false, 8, 0, 3, 1), | |
547 | None], | |
548 | elem_size: 0, be: false, alpha: true, palette: false }; | |
b68ff5ae | 549 | |
33b5689a | 550 | /// Predefined format with RGB24 palette. |
fba6f8e4 KS |
551 | pub const PAL8_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3, |
552 | comp_info: [ | |
553 | chromaton!(pal8; 0), | |
554 | chromaton!(pal8; 1), | |
555 | chromaton!(pal8; 2), | |
556 | None, None], | |
557 | elem_size: 3, be: false, alpha: false, palette: true }; | |
558 | ||
33b5689a | 559 | /// Predefined format for RGB565 packed video. |
fba6f8e4 KS |
560 | pub const RGB565_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3, |
561 | comp_info: [ | |
562 | chromaton!(packrgb; 5, 11, 0, 2), | |
563 | chromaton!(packrgb; 6, 5, 0, 2), | |
564 | chromaton!(packrgb; 5, 0, 0, 2), | |
565 | None, None], | |
566 | elem_size: 2, be: false, alpha: false, palette: false }; | |
567 | ||
33b5689a | 568 | /// Predefined format for RGB24. |
653e5afd KS |
569 | pub const RGB24_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3, |
570 | comp_info: [ | |
653e5afd | 571 | chromaton!(packrgb; 8, 0, 0, 3), |
c6297d1f KS |
572 | chromaton!(packrgb; 8, 0, 1, 3), |
573 | chromaton!(packrgb; 8, 0, 2, 3), | |
653e5afd KS |
574 | None, None], |
575 | elem_size: 3, be: false, alpha: false, palette: false }; | |
576 | ||
fba6f8e4 | 577 | impl NAPixelChromaton { |
33b5689a | 578 | /// Constructs a new `NAPixelChromaton` instance. |
0cc09358 KS |
579 | pub fn new(h_ss: u8, v_ss: u8, packed: bool, depth: u8, shift: u8, comp_offs: u8, next_elem: u8) -> Self { |
580 | Self { h_ss, v_ss, packed, depth, shift, comp_offs, next_elem } | |
581 | } | |
33b5689a | 582 | /// Returns subsampling for the current component. |
e243ceb4 | 583 | pub fn get_subsampling(self) -> (u8, u8) { (self.h_ss, self.v_ss) } |
33b5689a | 584 | /// Reports whether current component is packed. |
e243ceb4 | 585 | pub fn is_packed(self) -> bool { self.packed } |
33b5689a | 586 | /// Returns bit depth of current component. |
e243ceb4 | 587 | pub fn get_depth(self) -> u8 { self.depth } |
33b5689a | 588 | /// Returns bit shift for packed component. |
e243ceb4 | 589 | pub fn get_shift(self) -> u8 { self.shift } |
33b5689a | 590 | /// Returns byte offset for packed component. |
e243ceb4 | 591 | pub fn get_offset(self) -> u8 { self.comp_offs } |
33b5689a | 592 | /// Returns byte offset to the next element of current packed component. |
e243ceb4 | 593 | pub fn get_step(self) -> u8 { self.next_elem } |
b68ff5ae | 594 | |
33b5689a | 595 | /// Calculates the width for current component from general image width. |
e243ceb4 | 596 | pub fn get_width(self, width: usize) -> usize { |
15e41b31 KS |
597 | (width + ((1 << self.h_ss) - 1)) >> self.h_ss |
598 | } | |
33b5689a | 599 | /// Calculates the height for current component from general image height. |
e243ceb4 | 600 | pub fn get_height(self, height: usize) -> usize { |
15e41b31 KS |
601 | (height + ((1 << self.v_ss) - 1)) >> self.v_ss |
602 | } | |
33b5689a | 603 | /// Calculates the minimal stride for current component from general image width. |
e243ceb4 | 604 | pub fn get_linesize(self, width: usize) -> usize { |
b68ff5ae | 605 | let d = self.depth as usize; |
6c8e5c40 KS |
606 | if self.packed { |
607 | (self.get_width(width) * d + d - 1) >> 3 | |
608 | } else { | |
609 | self.get_width(width) | |
610 | } | |
b68ff5ae | 611 | } |
33b5689a | 612 | /// Calculates the required image size in pixels for current component from general image width. |
e243ceb4 | 613 | pub fn get_data_size(self, width: usize, height: usize) -> usize { |
b68ff5ae KS |
614 | let nh = (height + ((1 << self.v_ss) - 1)) >> self.v_ss; |
615 | self.get_linesize(width) * nh | |
616 | } | |
fba6f8e4 KS |
617 | } |
618 | ||
619 | impl fmt::Display for NAPixelChromaton { | |
620 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
621 | let pfmt = if self.packed { | |
622 | let mask = ((1 << self.depth) - 1) << self.shift; | |
623 | format!("packed(+{},{:X}, step {})", self.comp_offs, mask, self.next_elem) | |
624 | } else { | |
625 | format!("planar({},{})", self.comp_offs, self.next_elem) | |
626 | }; | |
627 | write!(f, "({}x{}, {})", self.h_ss, self.v_ss, pfmt) | |
628 | } | |
629 | } | |
630 | ||
631 | impl NAPixelFormaton { | |
33b5689a | 632 | /// Constructs a new instance of `NAPixelFormaton`. |
fba6f8e4 KS |
633 | pub fn new(model: ColorModel, |
634 | comp1: Option<NAPixelChromaton>, | |
635 | comp2: Option<NAPixelChromaton>, | |
636 | comp3: Option<NAPixelChromaton>, | |
637 | comp4: Option<NAPixelChromaton>, | |
638 | comp5: Option<NAPixelChromaton>, | |
9e9a3af1 | 639 | flags: u32, elem_size: u8) -> Self { |
8efb7386 | 640 | let mut chromatons: [Option<NAPixelChromaton>; MAX_CHROMATONS] = [None; MAX_CHROMATONS]; |
fba6f8e4 | 641 | let mut ncomp = 0; |
9e9a3af1 KS |
642 | let be = (flags & FORMATON_FLAG_BE) != 0; |
643 | let alpha = (flags & FORMATON_FLAG_ALPHA) != 0; | |
644 | let palette = (flags & FORMATON_FLAG_PALETTE) != 0; | |
fba6f8e4 KS |
645 | if let Some(c) = comp1 { chromatons[0] = Some(c); ncomp += 1; } |
646 | if let Some(c) = comp2 { chromatons[1] = Some(c); ncomp += 1; } | |
647 | if let Some(c) = comp3 { chromatons[2] = Some(c); ncomp += 1; } | |
648 | if let Some(c) = comp4 { chromatons[3] = Some(c); ncomp += 1; } | |
649 | if let Some(c) = comp5 { chromatons[4] = Some(c); ncomp += 1; } | |
e243ceb4 | 650 | NAPixelFormaton { model, |
fba6f8e4 KS |
651 | components: ncomp, |
652 | comp_info: chromatons, | |
e243ceb4 KS |
653 | elem_size, |
654 | be, alpha, palette } | |
fba6f8e4 KS |
655 | } |
656 | ||
33b5689a | 657 | /// Returns current colour model. |
fba6f8e4 | 658 | pub fn get_model(&self) -> ColorModel { self.model } |
33b5689a | 659 | /// Returns the number of components. |
b68ff5ae | 660 | pub fn get_num_comp(&self) -> usize { self.components as usize } |
33b5689a | 661 | /// Returns selected component information. |
fba6f8e4 KS |
662 | pub fn get_chromaton(&self, idx: usize) -> Option<NAPixelChromaton> { |
663 | if idx < self.comp_info.len() { return self.comp_info[idx]; } | |
664 | None | |
665 | } | |
33b5689a | 666 | /// Reports whether the packing format is big-endian. |
e243ceb4 | 667 | pub fn is_be(self) -> bool { self.be } |
33b5689a | 668 | /// Reports whether colourspace has alpha component. |
e243ceb4 | 669 | pub fn has_alpha(self) -> bool { self.alpha } |
33b5689a | 670 | /// Reports whether this is paletted format. |
e243ceb4 | 671 | pub fn is_paletted(self) -> bool { self.palette } |
33b5689a | 672 | /// Returns single packed pixel size. |
e243ceb4 | 673 | pub fn get_elem_size(self) -> u8 { self.elem_size } |
33b5689a | 674 | /// Reports whether the format is not packed. |
8efb7386 | 675 | pub fn is_unpacked(&self) -> bool { |
c7d8d948 | 676 | if self.palette { return false; } |
8efb7386 KS |
677 | for chr in self.comp_info.iter() { |
678 | if let Some(ref chromaton) = chr { | |
679 | if chromaton.is_packed() { return false; } | |
680 | } | |
681 | } | |
682 | true | |
683 | } | |
33b5689a | 684 | /// Returns the maximum component bit depth. |
8efb7386 KS |
685 | pub fn get_max_depth(&self) -> u8 { |
686 | let mut mdepth = 0; | |
687 | for chr in self.comp_info.iter() { | |
688 | if let Some(ref chromaton) = chr { | |
689 | mdepth = mdepth.max(chromaton.depth); | |
690 | } | |
691 | } | |
692 | mdepth | |
693 | } | |
8b746bf7 KS |
694 | /// Returns the total amount of bits needed for components. |
695 | pub fn get_total_depth(&self) -> u8 { | |
696 | let mut depth = 0; | |
697 | for chr in self.comp_info.iter() { | |
698 | if let Some(ref chromaton) = chr { | |
699 | depth += chromaton.depth; | |
700 | } | |
701 | } | |
702 | depth | |
703 | } | |
33b5689a | 704 | /// Returns the maximum component subsampling. |
8efb7386 KS |
705 | pub fn get_max_subsampling(&self) -> u8 { |
706 | let mut ssamp = 0; | |
707 | for chr in self.comp_info.iter() { | |
708 | if let Some(ref chromaton) = chr { | |
709 | let (ss_v, ss_h) = chromaton.get_subsampling(); | |
710 | ssamp = ssamp.max(ss_v).max(ss_h); | |
711 | } | |
712 | } | |
713 | ssamp | |
714 | } | |
fba6f8e4 KS |
715 | } |
716 | ||
717 | impl fmt::Display for NAPixelFormaton { | |
718 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
719 | let end = if self.be { "BE" } else { "LE" }; | |
720 | let palstr = if self.palette { "palette " } else { "" }; | |
721 | let astr = if self.alpha { "alpha " } else { "" }; | |
722 | let mut str = format!("Formaton for {} ({}{}elem {} size {}): ", self.model, palstr, astr,end, self.elem_size); | |
723 | for i in 0..self.comp_info.len() { | |
724 | if let Some(chr) = self.comp_info[i] { | |
725 | str = format!("{} {}", str, chr); | |
726 | } | |
727 | } | |
728 | write!(f, "[{}]", str) | |
729 | } | |
730 | } | |
731 | ||
732 | #[cfg(test)] | |
733 | mod test { | |
734 | use super::*; | |
735 | ||
736 | #[test] | |
737 | fn test_fmt() { | |
738 | println!("{}", SND_S16_FORMAT); | |
739 | println!("{}", SND_U8_FORMAT); | |
126b7eb8 | 740 | println!("{}", SND_F32P_FORMAT); |
11d889bb KS |
741 | assert_eq!(SND_U8_FORMAT.to_short_string(), "u8"); |
742 | assert_eq!(SND_F32P_FORMAT.to_short_string(), "f32lep"); | |
743 | let s16fmt = SND_S16_FORMAT.to_short_string(); | |
744 | assert_eq!(NASoniton::from_str(s16fmt.as_str()).unwrap(), SND_S16_FORMAT); | |
fba6f8e4 KS |
745 | println!("formaton yuv- {}", YUV420_FORMAT); |
746 | println!("formaton pal- {}", PAL8_FORMAT); | |
747 | println!("formaton rgb565- {}", RGB565_FORMAT); | |
748 | } | |
749 | } |