use log::warn; use xisf::{ ImageGeometry::Geometry, Variant::Variant, XISFReader::XISFReader, XISF::{SampleFormat, XISFImage, XISFProperty}, }; use xmltree::Element; struct Options { ignore_properties: bool, ignore_missing_encoding: bool, } fn init() { let _ = env_logger::builder().is_test(true).try_init(); } #[test] fn main() { init(); let mut metadata_properties: Vec = vec![]; let mut image: XISFImage; let data: &[u8; 1430] = include_bytes!("../resources/simple_10by10.xisf"); let reader_options = Options { ignore_properties: false, ignore_missing_encoding: true, }; let mut xisf_reader = XISFReader::new((*data).as_slice()); let signature = xisf_reader.read_file_signature(); match signature.validate() { Ok(_) => (), Err(x) => panic!("Invalid XISF signature: {}", x.kind), } let root = Element::parse(xisf_reader.reader).unwrap(); if root.name != "xisf" { panic!("Expected root element=xisf, instead found: {}", root.name); } let v = root.attributes.get("version"); match v { Some(x) => { if x != "1.0" { panic!("Expected XISF version: 1.0, instead found: {x}"); } } None => { panic!("Missing XISF version attribute"); } } let v = root.attributes.get("encoding"); match v { Some(x) => { if x != "UTF-8" { panic!("Expected XISF encoding UTF-8, instead found: {x}"); } } None => { if reader_options.ignore_missing_encoding { warn!("Missing XISF encoding"); } else { panic!("Missing XISF encoding"); } } } for n in root.children { let e = n.as_element(); if e.is_none() { println!("Ignoring unexpected XML root child node of type: {n:#?}"); continue; } let e = e.unwrap(); if e.name == "Image" { println!("Found {:#?}", e.name); let geometry = e.attributes.get("geometry").unwrap().to_owned(); let sample_format = e.attributes.get("sampleFormat").unwrap().to_owned(); let location = e.attributes.get("location").unwrap().to_owned(); image = XISFImage { geometry: Geometry::from(geometry), sample_format: SampleFormat::from_string(&sample_format), lower_bound: 0.0, upper_bound: 0.0, color_space: None, compression: None, location: location.clone(), raw_bytes: None }; if location == "embedded" { for ic in e.children.iter() { let ie = ic.as_element(); if ie.is_none() { println!("Ignoring unexpected XML root child node of type: {n:#?}"); continue; } let ie = ie.unwrap(); if ie.name == "Data" { let encoding = ie.attributes.get("encoding").unwrap().to_owned(); let encoded_data = ie.children.first().unwrap().as_text().unwrap().trim(); let decoded_data:Vec; if (encoding == "base64") { decoded_data = base64::decode(encoded_data.as_bytes()).unwrap(); image.raw_bytes=Some(decoded_data); } else if (encoding == "hex") { decoded_data = hex::decode(encoded_data).unwrap(); image.raw_bytes=Some(decoded_data); } else { panic!("Invalid/unsupported data encoding '{encoding}'"); } } } } } else if e.name == "Property" { if !reader_options.ignore_properties { println!("Found property: {} and not ignoring", e.name); todo!(); } } else if e.name == "Metadata" { println!("Found {:#?}", e.name); /* * The Metadata element contains a sequence of property child * elements. All of these properties are internal, i.e. they * are in the XISF namespace. */ for cn in e.children.iter() { let ce = cn.as_element(); if ce.is_none() { println!("Ignoring unexpected Meta child node of type: {cn:#?}"); continue; } let ce = ce.unwrap(); if ce.name == "Property" { println!("Found metadata property and not ignoring"); println!("{ce:#?}"); let id = ce.attributes.get("id").unwrap().to_owned(); let typ = ce.attributes.get("type").unwrap().to_owned(); let val = ce.attributes.get("value"); let val2 = match val { Some(c) => c.to_owned(), None => ce.get_text().unwrap().into_owned(), }; let prop = XISFProperty::new(id, Variant::from(typ, val2), None); metadata_properties.push(prop); } else { println!("Skipping unknown metadata element: {}", ce.name); } } } else { println!("Skipping unknown element: {}", e.name); } } print!("Found these props"); }