+ string = format!("{} {}", string, chr);
+ }
+ }
+ write!(f, "[{}]", string)
+ }
+}
+
+fn parse_rgb_format(s: &str) -> Result<NAPixelFormaton, FormatParseError> {
+ let mut order = [0; 4];
+ let mut is_be = s.ends_with("be");
+ let mut has_alpha = false;
+ let mut pstate = 0;
+ let mut bits = 0;
+ let mut bits_start = 0;
+ for (i, ch) in s.chars().enumerate() {
+ match pstate {
+ 0 => {
+ if i > 4 { return Err(FormatParseError {}); }
+ match ch {
+ 'R' | 'r' => { order[0] = i; },
+ 'G' | 'g' => { order[1] = i; },
+ 'B' | 'b' => { order[2] = i; },
+ 'A' | 'a' => { order[3] = i; has_alpha = true; },
+ '0'..='9' => {
+ pstate = 1; bits_start = i;
+ bits = u32::from((ch as u8) - b'0');
+ },
+ _ => return Err(FormatParseError {}),
+ };
+ },
+ 1 => {
+ if i > 4 + bits_start { return Err(FormatParseError {}); }
+ match ch {
+ '0'..='9' => {
+ bits = (bits * 10) + u32::from((ch as u8) - b'0');
+ },
+ 'B' | 'b' => { pstate = 2; }
+ 'L' | 'l' => { pstate = 2; is_be = false; }
+ _ => return Err(FormatParseError {}),
+ }
+ },
+ 2 => {
+ if ch != 'e' && ch != 'E' { return Err(FormatParseError {}); }
+ pstate = 3;
+ },
+ _ => return Err(FormatParseError {}),
+ };
+ }
+ let components: u8 = if has_alpha { 4 } else { 3 };
+ for el in order.iter() {
+ if *el >= (components as usize) {
+ return Err(FormatParseError {});
+ }
+ }
+ if order[0] == order[1] || order[0] == order[2] || order[1] == order[2] {
+ return Err(FormatParseError {});
+ }
+ if has_alpha && order[0..3].contains(&order[3]) {
+ return Err(FormatParseError {});
+ }
+ let mut chromatons = [None; 5];
+ let elem_size = match bits {
+ 0 | 24 => {
+ for (chro, ord) in chromatons.iter_mut().take(components as usize).zip(order.iter()) {
+ *chro = Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: *ord as u8, next_elem: components });
+ }
+ components
+ },
+ 555 => {
+ let rshift = (order[0] * 5) as u8;
+ let gshift = (order[1] * 5) as u8;
+ let bshift = (order[2] * 5) as u8;
+ chromatons[0] = Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: rshift, comp_offs: 0, next_elem: 2 });
+ chromatons[1] = Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: gshift, comp_offs: 0, next_elem: 2 });
+ chromatons[2] = Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: bshift, comp_offs: 0, next_elem: 2 });
+ if has_alpha { return Err(FormatParseError {}); }
+ 2
+ },
+ 565 => {
+ let mut offs = [0; 3];
+ for (ord, off) in order.iter().zip(offs.iter_mut()) {
+ *off = (*ord * 5) as u8;
+ }
+ match order[1] {
+ 0 => { offs[0] += 1; offs[2] += 1; },
+ 1 => { for el in offs.iter_mut() { if *el == 10 { *el += 1; break; } } },
+ _ => {},
+ };
+ chromatons[0] = Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: offs[0], comp_offs: 0, next_elem: 2 });
+ chromatons[1] = Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 6, shift: offs[1], comp_offs: 0, next_elem: 2 });
+ chromatons[2] = Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: offs[2], comp_offs: 0, next_elem: 2 });
+ if has_alpha { return Err(FormatParseError {}); }
+ 2
+ },
+ 5551 => {
+ let mut offs = [0; 4];
+ let depth = [ 5, 5, 5, 1 ];
+ let mut cur_off = 0;
+ for comp in 0..4 {
+ for (off, ord) in offs.iter_mut().zip(order.iter()) {
+ if *ord == comp {
+ *off = cur_off;
+ cur_off += depth[comp];
+ break;
+ }
+ }
+ }
+ chromatons[0] = Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: offs[0], comp_offs: 0, next_elem: 2 });
+ chromatons[1] = Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: offs[1], comp_offs: 0, next_elem: 2 });
+ chromatons[2] = Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: offs[2], comp_offs: 0, next_elem: 2 });
+ chromatons[3] = Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 1, shift: offs[3], comp_offs: 0, next_elem: 2 });
+ if !has_alpha { return Err(FormatParseError {}); }
+ 2
+ },
+ _ => return Err(FormatParseError {}),
+ };
+ Ok(NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB),
+ components,
+ comp_info: chromatons,
+ elem_size,
+ be: is_be, alpha: has_alpha, palette: false })
+}
+
+fn parse_yuv_format(s: &str) -> Result<NAPixelFormaton, FormatParseError> {
+ match s {
+ "y8" | "y400" | "gray" => {
+ return Ok(NAPixelFormaton {
+ model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 1,
+ comp_info: [
+ Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 0, next_elem: 1 }),
+ None, None, None, None],
+ elem_size: 1, be: true, alpha: false, palette: false });
+ },
+ "y8a" | "y400a" | "graya" => {
+ return Ok(NAPixelFormaton {
+ model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 2,
+ comp_info: [
+ Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 0, next_elem: 2 }),
+ Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 1, next_elem: 2 }),
+ None, None, None],
+ elem_size: 1, be: true, alpha: true, palette: false });
+ },
+ "uyvy" | "y422" => {
+ return Ok(NAPixelFormaton {
+ model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 3,
+ comp_info: [
+ Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 1, next_elem: 2 }),
+ Some(NAPixelChromaton{ h_ss: 1, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 0, next_elem: 4 }),
+ Some(NAPixelChromaton{ h_ss: 1, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 2, next_elem: 4 }),
+ None, None],
+ elem_size: 4, be: false, alpha: false, palette: false });
+ },
+ "yuy2" | "yuyv" | "v422" => {
+ return Ok(NAPixelFormaton {
+ model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 3,
+ comp_info: [
+ Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 0, next_elem: 2 }),
+ Some(NAPixelChromaton{ h_ss: 1, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 1, next_elem: 4 }),
+ Some(NAPixelChromaton{ h_ss: 1, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 3, next_elem: 4 }),
+ None, None],
+ elem_size: 4, be: false, alpha: false, palette: false });
+ },
+ "yvyu" => {
+ return Ok(NAPixelFormaton {
+ model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 3,
+ comp_info: [
+ Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 0, next_elem: 2 }),
+ Some(NAPixelChromaton{ h_ss: 1, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 3, next_elem: 4 }),
+ Some(NAPixelChromaton{ h_ss: 1, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 1, next_elem: 4 }),
+ None, None],
+ elem_size: 4, be: false, alpha: false, palette: false });
+ },
+ "vyuy" => {
+ return Ok(NAPixelFormaton {
+ model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 3,
+ comp_info: [
+ Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 1, next_elem: 2 }),
+ Some(NAPixelChromaton{ h_ss: 1, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 2, next_elem: 4 }),
+ Some(NAPixelChromaton{ h_ss: 1, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 0, next_elem: 4 }),
+ None, None],
+ elem_size: 4, be: false, alpha: false, palette: false });
+ },
+ _ => {},
+ };
+ if !s.starts_with("yuv") {
+ return Err(FormatParseError {});
+ }
+ let has_alpha = s.starts_with("yuva");
+ let components: u8 = if has_alpha { 4 } else { 3 };
+ let mut is_planar = false;
+ let mut format = 0;
+ let mut parse_end = components as usize;
+ for ch in s.chars().skip(components as usize) {
+ parse_end += 1;
+ if ch.is_ascii_digit() {
+ format = format * 10 + u32::from((ch as u8) - b'0');
+ if format > 444 { return Err(FormatParseError {}); }
+ } else {
+ is_planar = ch == 'p';
+ break;
+ }
+ }
+ if format == 0 { return Err(FormatParseError {}); }
+ let depth = if s.len() == parse_end { 8 } else {
+ let mut val = 0;
+ for ch in s.chars().skip(parse_end) {
+ if ch.is_ascii_digit() {
+ val = val * 10 + ((ch as u8) - b'0');
+ if val > 16 { return Err(FormatParseError {}); }
+ } else {
+ break;
+ }