]>
Commit | Line | Data |
---|---|---|
1 | use std::string::*; | |
2 | use std::fmt; | |
3 | ||
4 | #[derive(Debug,Copy,Clone,PartialEq)] | |
5 | pub struct NASoniton { | |
6 | bits: u8, | |
7 | be: bool, | |
8 | packed: bool, | |
9 | planar: bool, | |
10 | float: bool, | |
11 | signed: bool, | |
12 | } | |
13 | ||
14 | bitflags! { | |
15 | pub flags SonitonFlags: u8 { | |
16 | const SONITON_FLAG_BE = 0x01, | |
17 | const SONITON_FLAG_PACKED = 0x02, | |
18 | const SONITON_FLAG_PLANAR = 0x04, | |
19 | const SONITON_FLAG_FLOAT = 0x08, | |
20 | const SONITON_FLAG_SIGNED = 0x10, | |
21 | } | |
22 | } | |
23 | ||
24 | pub const SND_U8_FORMAT: NASoniton = NASoniton { bits: 8, be: false, packed: false, planar: false, float: false, signed: false }; | |
25 | pub const SND_S16_FORMAT: NASoniton = NASoniton { bits: 16, be: false, packed: false, planar: false, float: false, signed: true }; | |
26 | pub const SND_F32_FORMAT: NASoniton = NASoniton { bits: 32, be: false, packed: false, planar: false, float: true, signed: true }; | |
27 | ||
28 | impl NASoniton { | |
29 | pub fn new(bits: u8, flags: SonitonFlags) -> Self { | |
30 | let is_be = (flags.bits & SONITON_FLAG_BE.bits) != 0; | |
31 | let is_pk = (flags.bits & SONITON_FLAG_PACKED.bits) != 0; | |
32 | let is_pl = (flags.bits & SONITON_FLAG_PLANAR.bits) != 0; | |
33 | let is_fl = (flags.bits & SONITON_FLAG_FLOAT.bits) != 0; | |
34 | let is_sg = (flags.bits & SONITON_FLAG_SIGNED.bits) != 0; | |
35 | NASoniton { bits: bits, be: is_be, packed: is_pk, planar: is_pl, float: is_fl, signed: is_sg } | |
36 | } | |
37 | ||
38 | pub fn get_bits(&self) -> u8 { self.bits } | |
39 | pub fn is_be(&self) -> bool { self.be } | |
40 | pub fn is_packed(&self) -> bool { self.packed } | |
41 | pub fn is_planar(&self) -> bool { self.planar } | |
42 | pub fn is_float(&self) -> bool { self.float } | |
43 | pub fn is_signed(&self) -> bool { self.signed } | |
44 | } | |
45 | ||
46 | impl fmt::Display for NASoniton { | |
47 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
48 | let fmt = if self.float { "float" } else if self.signed { "int" } else { "uint" }; | |
49 | let end = if self.be { "BE" } else { "LE" }; | |
50 | write!(f, "({} bps, {} planar: {} packed: {} {})", self.bits, end, self.packed, self.planar, fmt) | |
51 | } | |
52 | } | |
53 | ||
54 | #[derive(Debug,Clone,Copy)] | |
55 | pub enum NAChannelType { | |
56 | 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 | |
57 | } | |
58 | ||
59 | impl NAChannelType { | |
60 | pub fn is_center(&self) -> bool { | |
61 | match *self { | |
62 | NAChannelType::C => true, NAChannelType::Ch => true, | |
63 | NAChannelType::Cl => true, NAChannelType::Ov => true, | |
64 | NAChannelType::LFE => true, NAChannelType::LFE2 => true, | |
65 | NAChannelType::Cs => true, NAChannelType::Chs => true, | |
66 | _ => false, | |
67 | } | |
68 | } | |
69 | pub fn is_left(&self) -> bool { | |
70 | match *self { | |
71 | NAChannelType::L => true, NAChannelType::Ls => true, | |
72 | NAChannelType::Lss => true, NAChannelType::Lc => true, | |
73 | NAChannelType::Lh => true, NAChannelType::Lw => true, | |
74 | NAChannelType::Lhs => true, NAChannelType::Ll => true, | |
75 | NAChannelType::Lt => true, NAChannelType::Lo => true, | |
76 | _ => false, | |
77 | } | |
78 | } | |
79 | pub fn is_right(&self) -> bool { | |
80 | match *self { | |
81 | NAChannelType::R => true, NAChannelType::Rs => true, | |
82 | NAChannelType::Rss => true, NAChannelType::Rc => true, | |
83 | NAChannelType::Rh => true, NAChannelType::Rw => true, | |
84 | NAChannelType::Rhs => true, NAChannelType::Rl => true, | |
85 | NAChannelType::Rt => true, NAChannelType::Ro => true, | |
86 | _ => false, | |
87 | } | |
88 | } | |
89 | } | |
90 | ||
91 | impl fmt::Display for NAChannelType { | |
92 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
93 | let name = match *self { | |
94 | NAChannelType::C => "C".to_string(), | |
95 | NAChannelType::L => "L".to_string(), | |
96 | NAChannelType::R => "R".to_string(), | |
97 | NAChannelType::Cs => "Cs".to_string(), | |
98 | NAChannelType::Ls => "Ls".to_string(), | |
99 | NAChannelType::Rs => "Rs".to_string(), | |
100 | NAChannelType::Lss => "Lss".to_string(), | |
101 | NAChannelType::Rss => "Rss".to_string(), | |
102 | NAChannelType::LFE => "LFE".to_string(), | |
103 | NAChannelType::Lc => "Lc".to_string(), | |
104 | NAChannelType::Rc => "Rc".to_string(), | |
105 | NAChannelType::Lh => "Lh".to_string(), | |
106 | NAChannelType::Rh => "Rh".to_string(), | |
107 | NAChannelType::Ch => "Ch".to_string(), | |
108 | NAChannelType::LFE2 => "LFE2".to_string(), | |
109 | NAChannelType::Lw => "Lw".to_string(), | |
110 | NAChannelType::Rw => "Rw".to_string(), | |
111 | NAChannelType::Ov => "Ov".to_string(), | |
112 | NAChannelType::Lhs => "Lhs".to_string(), | |
113 | NAChannelType::Rhs => "Rhs".to_string(), | |
114 | NAChannelType::Chs => "Chs".to_string(), | |
115 | NAChannelType::Ll => "Ll".to_string(), | |
116 | NAChannelType::Rl => "Rl".to_string(), | |
117 | NAChannelType::Cl => "Cl".to_string(), | |
118 | NAChannelType::Lt => "Lt".to_string(), | |
119 | NAChannelType::Rt => "Rt".to_string(), | |
120 | NAChannelType::Lo => "Lo".to_string(), | |
121 | NAChannelType::Ro => "Ro".to_string(), | |
122 | }; | |
123 | write!(f, "{}", name) | |
124 | } | |
125 | } | |
126 | ||
127 | pub struct NAChannelMap { | |
128 | ids: Vec<NAChannelType>, | |
129 | } | |
130 | ||
131 | impl NAChannelMap { | |
132 | pub fn new() -> Self { NAChannelMap { ids: Vec::new() } } | |
133 | pub fn add_channel(&mut self, ch: NAChannelType) { | |
134 | self.ids.push(ch); | |
135 | } | |
136 | pub fn num_channels(&self) -> usize { | |
137 | self.ids.len() | |
138 | } | |
139 | pub fn get_channel(&self, idx: usize) -> NAChannelType { | |
140 | self.ids[idx] | |
141 | } | |
142 | pub fn find_channel_id(&self, t: NAChannelType) -> Option<u8> { | |
143 | for i in 0..self.ids.len() { | |
144 | if self.ids[i] as i32 == t as i32 { return Some(i as u8); } | |
145 | } | |
146 | None | |
147 | } | |
148 | } | |
149 | ||
150 | #[derive(Debug,Clone,Copy,PartialEq)] | |
151 | pub enum RGBSubmodel { | |
152 | RGB, | |
153 | SRGB, | |
154 | } | |
155 | ||
156 | impl fmt::Display for RGBSubmodel { | |
157 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
158 | let name = match *self { | |
159 | RGBSubmodel::RGB => "RGB".to_string(), | |
160 | RGBSubmodel::SRGB => "sRGB".to_string(), | |
161 | }; | |
162 | write!(f, "{}", name) | |
163 | } | |
164 | } | |
165 | ||
166 | #[derive(Debug,Clone,Copy,PartialEq)] | |
167 | pub enum YUVSubmodel { | |
168 | YCbCr, | |
169 | YIQ, | |
170 | YUVJ, | |
171 | } | |
172 | ||
173 | impl fmt::Display for YUVSubmodel { | |
174 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
175 | let name = match *self { | |
176 | YUVSubmodel::YCbCr => "YCbCr".to_string(), | |
177 | YUVSubmodel::YIQ => "YIQ".to_string(), | |
178 | YUVSubmodel::YUVJ => "YUVJ".to_string(), | |
179 | }; | |
180 | write!(f, "{}", name) | |
181 | } | |
182 | } | |
183 | ||
184 | #[derive(Debug, Clone,Copy,PartialEq)] | |
185 | pub enum ColorModel { | |
186 | RGB(RGBSubmodel), | |
187 | YUV(YUVSubmodel), | |
188 | CMYK, | |
189 | HSV, | |
190 | LAB, | |
191 | XYZ, | |
192 | } | |
193 | ||
194 | impl ColorModel { | |
195 | pub fn get_default_components(&self) -> usize { | |
196 | match *self { | |
197 | ColorModel::CMYK => 4, | |
198 | _ => 3, | |
199 | } | |
200 | } | |
201 | } | |
202 | ||
203 | impl fmt::Display for ColorModel { | |
204 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
205 | let name = match *self { | |
206 | ColorModel::RGB(fmt) => format!("RGB({})", fmt).to_string(), | |
207 | ColorModel::YUV(fmt) => format!("YUV({})", fmt).to_string(), | |
208 | ColorModel::CMYK => "CMYK".to_string(), | |
209 | ColorModel::HSV => "HSV".to_string(), | |
210 | ColorModel::LAB => "LAB".to_string(), | |
211 | ColorModel::XYZ => "XYZ".to_string(), | |
212 | }; | |
213 | write!(f, "{}", name) | |
214 | } | |
215 | } | |
216 | ||
217 | #[derive(Clone,Copy,PartialEq)] | |
218 | pub struct NAPixelChromaton { | |
219 | h_ss: u8, | |
220 | v_ss: u8, | |
221 | packed: bool, | |
222 | depth: u8, | |
223 | shift: u8, | |
224 | comp_offs: u8, | |
225 | next_elem: u8, | |
226 | } | |
227 | ||
228 | bitflags! { | |
229 | pub flags FormatonFlags: u8 { | |
230 | const FORMATON_FLAG_BE = 0x01, | |
231 | const FORMATON_FLAG_ALPHA = 0x02, | |
232 | const FORMATON_FLAG_PALETTE = 0x04, | |
233 | } | |
234 | } | |
235 | ||
236 | ||
237 | #[derive(Clone,Copy,PartialEq)] | |
238 | pub struct NAPixelFormaton { | |
239 | model: ColorModel, | |
240 | components: u8, | |
241 | comp_info: [Option<NAPixelChromaton>; 5], | |
242 | elem_size: u8, | |
243 | be: bool, | |
244 | alpha: bool, | |
245 | palette: bool, | |
246 | } | |
247 | ||
248 | macro_rules! chromaton { | |
249 | ($hs: expr, $vs: expr, $pck: expr, $d: expr, $sh: expr, $co: expr, $ne: expr) => ({ | |
250 | Some(NAPixelChromaton{ h_ss: $hs, v_ss: $vs, packed: $pck, depth: $d, shift: $sh, comp_offs: $co, next_elem: $ne }) | |
251 | }); | |
252 | (yuv8; $hs: expr, $vs: expr, $co: expr) => ({ | |
253 | Some(NAPixelChromaton{ h_ss: $hs, v_ss: $vs, packed: false, depth: 8, shift: 0, comp_offs: $co, next_elem: 1 }) | |
254 | }); | |
255 | (packrgb; $d: expr, $s: expr, $co: expr, $ne: expr) => ({ | |
256 | Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: $d, shift: $s, comp_offs: $co, next_elem: $ne }) | |
257 | }); | |
258 | (pal8; $co: expr) => ({ | |
259 | Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: $co, next_elem: 3 }) | |
260 | }); | |
261 | } | |
262 | ||
263 | pub const YUV420_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 3, | |
264 | comp_info: [ | |
265 | chromaton!(0, 0, false, 8, 0, 0, 1), | |
266 | chromaton!(yuv8; 1, 1, 1), | |
267 | chromaton!(yuv8; 1, 1, 2), | |
268 | None, None], | |
269 | elem_size: 0, be: false, alpha: false, palette: false }; | |
270 | ||
271 | pub const YUV410_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 3, | |
272 | comp_info: [ | |
273 | chromaton!(0, 0, false, 8, 0, 0, 1), | |
274 | chromaton!(yuv8; 2, 2, 1), | |
275 | chromaton!(yuv8; 2, 2, 2), | |
276 | None, None], | |
277 | elem_size: 0, be: false, alpha: false, palette: false }; | |
278 | ||
279 | pub const PAL8_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3, | |
280 | comp_info: [ | |
281 | chromaton!(pal8; 0), | |
282 | chromaton!(pal8; 1), | |
283 | chromaton!(pal8; 2), | |
284 | None, None], | |
285 | elem_size: 3, be: false, alpha: false, palette: true }; | |
286 | ||
287 | pub const RGB565_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3, | |
288 | comp_info: [ | |
289 | chromaton!(packrgb; 5, 11, 0, 2), | |
290 | chromaton!(packrgb; 6, 5, 0, 2), | |
291 | chromaton!(packrgb; 5, 0, 0, 2), | |
292 | None, None], | |
293 | elem_size: 2, be: false, alpha: false, palette: false }; | |
294 | ||
295 | pub const RGB24_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3, | |
296 | comp_info: [ | |
297 | chromaton!(packrgb; 8, 0, 2, 3), | |
298 | chromaton!(packrgb; 8, 0, 1, 3), | |
299 | chromaton!(packrgb; 8, 0, 0, 3), | |
300 | None, None], | |
301 | elem_size: 3, be: false, alpha: false, palette: false }; | |
302 | ||
303 | impl NAPixelChromaton { | |
304 | pub fn get_subsampling(&self) -> (u8, u8) { (self.h_ss, self.v_ss) } | |
305 | pub fn is_packed(&self) -> bool { self.packed } | |
306 | pub fn get_depth(&self) -> u8 { self.depth } | |
307 | pub fn get_shift(&self) -> u8 { self.shift } | |
308 | pub fn get_offset(&self) -> u8 { self.comp_offs } | |
309 | pub fn get_step(&self) -> u8 { self.next_elem } | |
310 | ||
311 | pub fn get_linesize(&self, width: usize) -> usize { | |
312 | let nw = (width + ((1 << self.h_ss) - 1)) >> self.h_ss; | |
313 | let d = self.depth as usize; | |
314 | (nw * d + d - 1) >> 3 | |
315 | } | |
316 | pub fn get_data_size(&self, width: usize, height: usize) -> usize { | |
317 | let nh = (height + ((1 << self.v_ss) - 1)) >> self.v_ss; | |
318 | self.get_linesize(width) * nh | |
319 | } | |
320 | } | |
321 | ||
322 | impl fmt::Display for NAPixelChromaton { | |
323 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
324 | let pfmt = if self.packed { | |
325 | let mask = ((1 << self.depth) - 1) << self.shift; | |
326 | format!("packed(+{},{:X}, step {})", self.comp_offs, mask, self.next_elem) | |
327 | } else { | |
328 | format!("planar({},{})", self.comp_offs, self.next_elem) | |
329 | }; | |
330 | write!(f, "({}x{}, {})", self.h_ss, self.v_ss, pfmt) | |
331 | } | |
332 | } | |
333 | ||
334 | impl NAPixelFormaton { | |
335 | pub fn new(model: ColorModel, | |
336 | comp1: Option<NAPixelChromaton>, | |
337 | comp2: Option<NAPixelChromaton>, | |
338 | comp3: Option<NAPixelChromaton>, | |
339 | comp4: Option<NAPixelChromaton>, | |
340 | comp5: Option<NAPixelChromaton>, | |
341 | flags: FormatonFlags, elem_size: u8) -> Self { | |
342 | let mut chromatons: [Option<NAPixelChromaton>; 5] = [None; 5]; | |
343 | let mut ncomp = 0; | |
344 | let be = (flags.bits & FORMATON_FLAG_BE.bits) != 0; | |
345 | let alpha = (flags.bits & FORMATON_FLAG_ALPHA.bits) != 0; | |
346 | let palette = (flags.bits & FORMATON_FLAG_PALETTE.bits) != 0; | |
347 | if let Some(c) = comp1 { chromatons[0] = Some(c); ncomp += 1; } | |
348 | if let Some(c) = comp2 { chromatons[1] = Some(c); ncomp += 1; } | |
349 | if let Some(c) = comp3 { chromatons[2] = Some(c); ncomp += 1; } | |
350 | if let Some(c) = comp4 { chromatons[3] = Some(c); ncomp += 1; } | |
351 | if let Some(c) = comp5 { chromatons[4] = Some(c); ncomp += 1; } | |
352 | NAPixelFormaton { model: model, | |
353 | components: ncomp, | |
354 | comp_info: chromatons, | |
355 | elem_size: elem_size, | |
356 | be: be, alpha: alpha, palette: palette } | |
357 | } | |
358 | ||
359 | pub fn get_model(&self) -> ColorModel { self.model } | |
360 | pub fn get_num_comp(&self) -> usize { self.components as usize } | |
361 | pub fn get_chromaton(&self, idx: usize) -> Option<NAPixelChromaton> { | |
362 | if idx < self.comp_info.len() { return self.comp_info[idx]; } | |
363 | None | |
364 | } | |
365 | pub fn is_be(&self) -> bool { self.be } | |
366 | pub fn has_alpha(&self) -> bool { self.alpha } | |
367 | pub fn is_paletted(&self) -> bool { self.palette } | |
368 | pub fn get_elem_size(&self) -> u8 { self.elem_size } | |
369 | } | |
370 | ||
371 | impl fmt::Display for NAPixelFormaton { | |
372 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
373 | let end = if self.be { "BE" } else { "LE" }; | |
374 | let palstr = if self.palette { "palette " } else { "" }; | |
375 | let astr = if self.alpha { "alpha " } else { "" }; | |
376 | let mut str = format!("Formaton for {} ({}{}elem {} size {}): ", self.model, palstr, astr,end, self.elem_size); | |
377 | for i in 0..self.comp_info.len() { | |
378 | if let Some(chr) = self.comp_info[i] { | |
379 | str = format!("{} {}", str, chr); | |
380 | } | |
381 | } | |
382 | write!(f, "[{}]", str) | |
383 | } | |
384 | } | |
385 | ||
386 | #[cfg(test)] | |
387 | mod test { | |
388 | use super::*; | |
389 | ||
390 | #[test] | |
391 | fn test_fmt() { | |
392 | println!("{}", SND_S16_FORMAT); | |
393 | println!("{}", SND_U8_FORMAT); | |
394 | println!("{}", SND_F32_FORMAT); | |
395 | println!("formaton yuv- {}", YUV420_FORMAT); | |
396 | println!("formaton pal- {}", PAL8_FORMAT); | |
397 | println!("formaton rgb565- {}", RGB565_FORMAT); | |
398 | } | |
399 | } |