修改该了关于info信息为json内部管理 该项目只适用于windows或linux 修改了c++及相应的rust代码 不适用于arm

This commit is contained in:
xin
2025-06-17 17:02:35 +08:00
parent a500813e45
commit ae30c24a8f
10 changed files with 464 additions and 218 deletions

View File

@ -1,4 +1,4 @@
use crate::{read, structures::{ImageInfo, OneIRISData, OtherInfo, SpectralData, SpectralInfo, TimeStruct, DATA_TYPE_FLOAT64}, write};
use crate::{read, structures::{ImageInfo, OneIRISData, SpectralData, TimeStruct, DATA_TYPE_FLOAT64}, write};
use std::{io::{Cursor, Write}, vec};
pub fn spectral_data_roundtrip() {
@ -30,45 +30,7 @@ pub fn spectral_data_roundtrip() {
println!("Roundtrip test passed successfully!");
}
pub fn spectral_info_roundtrip() {
// Create sample spectral info
let mut info = SpectralInfo::new();
info.sensor_id = "SENSOR-001".to_string();
info.wave_coeff = [400.0, 0.5, 0.01, 0.001];
// Write to buffer
let mut buffer = Vec::new();
write::write_spectral_info(&info, &mut buffer).unwrap();
// Read back from buffer
let mut reader = Cursor::new(buffer);
let read_info = read::read_spectral_info(&mut reader).unwrap();
// Verify roundtrip
assert_eq!(info.sensor_id, read_info.sensor_id);
assert_eq!(info.wave_coeff, read_info.wave_coeff);
println!("Spectral info roundtrip test passed successfully!");
}
pub fn other_info_roundtrip() {
// Create sample other info
let mut info = OtherInfo::new();
info.info_type = 1;
info.data = vec![1, 2, 3, 4, 5];
// Write to buffer
let mut buffer = Vec::new();
write::write_other_info(&info, &mut buffer).unwrap();
// Read back from buffer
let mut reader = Cursor::new(buffer);
let read_info = read::read_other_info(&mut reader).unwrap();
// Verify roundtrip
assert_eq!(info.info_type, read_info.info_type);
assert_eq!(info.data, read_info.data);
println!("Other info roundtrip test passed successfully!");
}
pub fn image_info_roundtrip() {
// Create sample image info
@ -104,8 +66,7 @@ pub fn read_iris_file_example() {
// test_data.spectral_data_section.push(spectral);
// Write to a temporary file
let path = "iris_data_example.iris";
// let path = "output_iris_data.iris";
// {
// let file = std::fs::File::create(path).unwrap();
// let mut writer = std::io::BufWriter::new(file);
@ -124,6 +85,8 @@ pub fn read_iris_file_example() {
// }
// Now read it back
let path = "output_iris_data1.iris";
// let path = "output_iris_data.iris";
let mut read=match read::read_iris_file(path) {
Ok(data) => {
println!("Successfully read IRIS file:");
@ -146,6 +109,11 @@ pub fn read_iris_file_example() {
OneIRISData::new()
}
};
let spectralinfonum = read.spectral_info_section.len();
for info in &read.spectral_info_section {
println!("{}",info)
}
let mut aaa=read.spectral_data_section[2].Get_Spectral_Data();
let mut bbb =vec![0f64; 256];
//随机生成一些数据
@ -157,5 +125,5 @@ pub fn read_iris_file_example() {
println!("Spectral Data Length: {}", ccc.len());
write::wirte_iris_data(&read, "output_iris_data1.iris").unwrap();
write::wirte_iris_data(&read, "output_iris_data2.iris").unwrap();
}

View File

@ -3,7 +3,7 @@ pub mod read;
pub mod write;
pub mod examples;
pub use structures::{TimeStruct, SpectralData, SpectralInfo, OtherInfo, ImageInfo, OneIRISData};
pub use read::{read_time, read_spectral_data, read_spectral_info, read_other_info, read_image_info, read_iris_file};
pub use structures::{TimeStruct, SpectralData, ImageInfo, OneIRISData};
pub use read::{read_time, read_spectral_data, read_image_info, read_iris_file};
pub use write::*;
pub use examples::{spectral_data_roundtrip, spectral_info_roundtrip};
pub use examples::{spectral_data_roundtrip};

View File

@ -1,7 +1,5 @@
use iris_rust::examples::{
spectral_data_roundtrip,
spectral_info_roundtrip,
other_info_roundtrip,
image_info_roundtrip,
read_iris_file_example
};

View File

@ -1,7 +1,8 @@
use std::io::{Read, Result, BufReader};
use std::fs::File;
use std::convert::TryInto;
use super::structures::{TimeStruct, SpectralData, SpectralInfo, OtherInfo, ImageInfo, OneIRISData};
use super::structures::{TimeStruct, SpectralData, ImageInfo, OneIRISData};
use serde_json::Value;
pub fn read_time<R: Read>(reader: &mut R) -> Result<TimeStruct> {
let mut buffer = [0u8; 10]; // Corrected buffer size to 10 bytes
@ -19,24 +20,7 @@ pub fn read_time<R: Read>(reader: &mut R) -> Result<TimeStruct> {
})
}
pub fn read_spectral_info<R: Read>(reader: &mut R) -> Result<SpectralInfo> {
let mut info = SpectralInfo::new();
// Read sensor ID
let mut sensor_buf = [0u8; 50];
reader.read_exact(&mut sensor_buf)?;
info.sensor_id = String::from_utf8_lossy(&sensor_buf).trim_end_matches('\0').to_string();
info.sensor_id = remove_after_null_split_once( info.sensor_id);
// Read wave coefficients
for i in 0..4 {
let mut float_buf = [0u8; 8];
reader.read_exact(&mut float_buf)?;
info.wave_coeff[i] = f64::from_le_bytes(float_buf);
}
Ok(info)
}
pub fn read_image_info<R: Read>(reader: &mut R) -> Result<ImageInfo> {
let mut info = ImageInfo::new();
@ -127,14 +111,79 @@ pub fn read_iris_file(path: &str) -> Result<OneIRISData> {
continue; // Skip this entry if parsing fails
}
}; // Handle parsing error gracefully
//判断json["info_type"]是否存在
if !json.get("info_type").is_some() {
eprintln!("JSON does not contain 'info_type': {}", json_string);
continue; // Skip this entry if "info_type" is missing
}
/* {
"info_type": "infolist", // 0 for device info
"info_number":3,
"info_list": [
{
"info_type": "devinfo", // 0 for device info
"sensor_id": "is30002",
"bandnum": 2048,
"wave_coeff": {
"a1": 0.0,
"a2": 0.0,
"a3": 400,
"a4": 1.0
}
},
{
"info_type": "environment", // 1 for gain info
"date": "2000-01-00 00:00:00",
//下面可选
"humidity":90.0,
"temperature":35.0 ,
"gps":{
"latitude":115.01,
"longitude": 39.01,
"altitude": 100.0
},
},
{
"info_type": "devinfo", // 0 for device info
"sensor_id": "is20001",
"bandnum": 512,
"wave_coeff": {
"a1": 0,
"a2": 0.0,
"a3":390,
"a4": 4
}
}
]
} */
//如果info_type是infolist 则需要逐个解析
if json.get("info_type").and_then(Value::as_str) == Some("infolist") {
let info_number = json.get("info_number").and_then(Value::as_u64).unwrap_or(0) as usize;
for i in 0 ..info_number{
//将对应的info加入到data中
if let Some(info) = json.get("info_list").and_then(|list| list.get(i)) {
data.push(info.clone());
}
}
continue; // Skip the rest of the loop for this entry
}
data.push(json);
//println!("Parsed JSON: {:?}", json);
let mut data_entry = SpectralInfo::new();
data_entry.sensor_id = json.get("SensorId").and_then(Value::as_str).unwrap_or_default().to_string();
data_entry.wave_coeff[0]=json["WaveCoeff"]["a1"].as_f64().unwrap_or(0.0);
data_entry.wave_coeff[1]=json["WaveCoeff"]["a2"].as_f64().unwrap_or(0.0);
data_entry.wave_coeff[2]=json["WaveCoeff"]["a3"].as_f64().unwrap_or(0.0);
data_entry.wave_coeff[3]=json["WaveCoeff"]["a4"].as_f64().unwrap_or(0.0);
data.push(data_entry);
// let mut data_entry = SpectralInfo::new();
// data_entry.sensor_id = json.get("SensorId").and_then(Value::as_str).unwrap_or_default().to_string();
// data_entry.wave_coeff[0]=json["WaveCoeff"]["a1"].as_f64().unwrap_or(0.0);
// data_entry.wave_coeff[1]=json["WaveCoeff"]["a2"].as_f64().unwrap_or(0.0);
// data_entry.wave_coeff[2]=json["WaveCoeff"]["a3"].as_f64().unwrap_or(0.0);
// data_entry.wave_coeff[3]=json["WaveCoeff"]["a4"].as_f64().unwrap_or(0.0);
// data.push(data_entry);
// Parse JSON string
@ -151,7 +200,25 @@ pub fn read_iris_file(path: &str) -> Result<OneIRISData> {
let count = read_section_count(&mut reader)?;
let mut data = Vec::with_capacity(count);
for _ in 0..count {
data.push(read_other_info(&mut reader)?);
let mut tempbuffer = [0u8; 3]; // Adjust size as needed
reader.read_exact(&mut tempbuffer)?;
let lenth = u16::from_le_bytes([tempbuffer[0], tempbuffer[1]]) as usize;
let info_type = u8::from_le_bytes([tempbuffer[2]]);
let mut tempvector = vec![0u8; lenth];
reader.read_exact(&mut tempvector)?;
// Convert to String
let json_string = String::from_utf8(tempvector).unwrap_or_default();
let json_string = json_string.trim_end_matches('\0').to_string();
//print!("JSON String: {}", json_string);
let json: Value = match serde_json::from_str(&json_string) {
Ok(json) => json,
Err(e) => {
eprintln!("Error parsing JSON: {}", e);
continue; // Skip this entry if parsing fails
}
}; // Handle parsing error gracefully
data.push(json);
}
iris_data.other_info_section = data;
},
@ -185,25 +252,25 @@ fn read_section_count<R: Read>(reader: &mut R) -> Result<usize> {
Ok(u16::from_le_bytes(count_buf) as usize)
}
pub fn read_other_info<R: Read>(reader: &mut R) -> Result<OtherInfo> {
let mut info = OtherInfo::new();
// pub fn read_other_info<R: Read>(reader: &mut R) -> Result<OtherInfo> {
// let mut info = OtherInfo::new();
// Read info type
let mut type_buf = [0u8; 1];
reader.read_exact(&mut type_buf)?;
info.info_type = type_buf[0];
// // Read info type
// let mut type_buf = [0u8; 1];
// reader.read_exact(&mut type_buf)?;
// info.info_type = type_buf[0];
// Read data length
let mut len_buf = [0u8; 8];
reader.read_exact(&mut len_buf)?;
let data_len = u64::from_le_bytes(len_buf) as usize;
// // Read data length
// let mut len_buf = [0u8; 8];
// reader.read_exact(&mut len_buf)?;
// let data_len = u64::from_le_bytes(len_buf) as usize;
// Read data
info.data.resize(data_len, 0);
reader.read_exact(&mut info.data)?;
// // Read data
// info.data.resize(data_len, 0);
// reader.read_exact(&mut info.data)?;
Ok(info)
}
// Ok(info)
// }
fn remove_after_null_split_once(s: String) -> String {
if let Some((before_null, _after_null)) = s.split_once('\0') {
// 返回 \0 之前的部分

View File

@ -13,12 +13,15 @@ pub const Target_Spectral_Type_DN:u8 = 0x00;
pub const Target_Spectral_Type_Rad:u8 = 0x01;
pub const Target_Spectral_Type_Ref:u8 = 0x02;
pub const Target_Spectral_Type_IRad:u8 = 0x03;
pub const Target_Spectral_Type_CaliFile:u8 = 0x04;
pub const Target_Spectral_Type_CaliFile_Gain:u8 = 0x04;
pub const Target_Spectral_Type_FlatRef:u8 = 0x05;
pub const Target_Spectral_Type_DarkDN:u8 = 0x06;
pub const Target_Spectral_Type_FlatDN:u8 = 0x07;
pub const Target_LAMP_VALUE_SCALED:u8 = 0x08;
use serde_json::json;
#[derive(Debug, Clone, PartialEq)]
@ -293,26 +296,26 @@ impl SpectralData {
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct OtherInfo {
pub info_type: u8,
pub data: Vec<u8>, // Assuming the data is variable length
}
// #[derive(Debug, Clone, PartialEq)]
// pub struct OtherInfo {
// pub info_type: u8,
// pub data: Vec<u8>, // Assuming the data is variable length
// }
impl OtherInfo {
pub fn new() -> Self {
OtherInfo {
info_type: 0,
data: Vec::new(),
}
}
}
// impl OtherInfo {
// pub fn new() -> Self {
// OtherInfo {
// info_type: 0,
// data: Vec::new(),
// }
// }
// }
#[derive(Debug, Clone, PartialEq)]
pub struct OneIRISData {
pub spectral_data_section: Vec<SpectralData>,
pub spectral_info_section: Vec<SpectralInfo>,
pub other_info_section: Vec<OtherInfo>,
pub spectral_info_section: Vec<serde_json::Value>, // Using serde_json::Value for flexibility
pub other_info_section: Vec<serde_json::Value>,
pub image_info_section: Vec<ImageInfo>,
}
@ -327,20 +330,21 @@ impl OneIRISData {
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct SpectralInfo {
pub sensor_id: String,
pub wave_coeff: [f64; 4],
}
// #[derive(Debug, Clone, PartialEq)]
// pub struct SpectralInfo {
// pub sensor_id: String,
// pub wave_coeff: [f64; 4],
// }
impl SpectralInfo {
pub fn new() -> Self {
SpectralInfo {
sensor_id: String::new(),
wave_coeff: [0.0; 4],
}
}
}
// impl SpectralInfo {
// pub fn new() -> Self {
// SpectralInfo {
// sensor_id: String::new(),
// wave_coeff: [0.0; 4],
// }
// }
// }
#[derive(Debug, Clone, PartialEq)]
pub struct ImageInfo {
@ -384,19 +388,19 @@ mod tests {
assert!(data.spectral_data.is_empty());
}
#[test]
fn test_spectral_info() {
let info = SpectralInfo::new();
assert_eq!(info.sensor_id, "");
assert_eq!(info.wave_coeff, [0.0; 4]);
}
// #[test]
// fn test_spectral_info() {
// let info = SpectralInfo::new();
// assert_eq!(info.sensor_id, "");
// assert_eq!(info.wave_coeff, [0.0; 4]);
// }
#[test]
fn test_other_info() {
let info = OtherInfo::new();
assert_eq!(info.info_type, 0);
assert!(info.data.is_empty());
}
// #[test]
// fn test_other_info() {
// let info = OtherInfo::new();
// assert_eq!(info.info_type, 0);
// assert!(info.data.is_empty());
// }
#[test]
fn test_image_info() {

View File

@ -3,9 +3,9 @@ use std::io::{self, Write, Result};
use std::vec;
use serde_json::json;
use crate::write;
// use crate::write;
use super::structures::{TimeStruct, SpectralData, SpectralInfo, OtherInfo, ImageInfo,OneIRISData};
use super::structures::{TimeStruct, SpectralData, ImageInfo,OneIRISData};
// Internal writer functions remain the same
fn write_time<W: Write>(time: &TimeStruct, writer: &mut W) -> Result<()> {
@ -20,19 +20,6 @@ fn write_time<W: Write>(time: &TimeStruct, writer: &mut W) -> Result<()> {
Ok(())
}
pub fn write_spectral_info<W: Write>(info: &SpectralInfo, writer: &mut W) -> Result<()> {
// Write sensor ID
let mut sensor_buf = [0u8; 50];
sensor_buf[..info.sensor_id.len().min(49)].copy_from_slice(info.sensor_id.as_bytes());
writer.write_all(&sensor_buf)?;
// Write wave coefficients
for coeff in &info.wave_coeff {
writer.write_all(&coeff.to_le_bytes())?;
}
Ok(())
}
pub fn write_image_info<W: Write>(info: &ImageInfo, writer: &mut W) -> Result<()> {
// Write data length
@ -57,17 +44,6 @@ pub fn write_image_info<W: Write>(info: &ImageInfo, writer: &mut W) -> Result<()
}
pub fn write_other_info<W: Write>(info: &OtherInfo, writer: &mut W) -> Result<()> {
// Write info type
writer.write_all(&[info.info_type])?;
// Write data length
writer.write_all(&(info.data.len() as u64).to_le_bytes())?;
// Write data
writer.write_all(&info.data)?;
Ok(())
}
pub fn write_spectral_data<W: Write>(data: &SpectralData, writer: &mut W) -> Result<()> {
// Write fixed-size fields
@ -82,7 +58,7 @@ pub fn write_spectral_data<W: Write>(data: &SpectralData, writer: &mut W) -> Res
sensor_buf[..data.sensor_id.len().min(49)].copy_from_slice(sensor_id_bytes[0..sensor_id_bytes.len().min(49)].as_ref());
writer.write_all(&sensor_buf)?;
writer.write_all(&[data.fiber_id])?;
write_time(&data.collection_time, writer)?;
writer.write_all(&data.exposure.to_le_bytes())?;
writer.write_all(&data.gain.to_le_bytes())?;
@ -140,7 +116,7 @@ pub fn caculate_image_info_length(data: &Vec<ImageInfo>) -> u64 {
}
pub fn cacluate_other_info_length(data: &Vec<OtherInfo>) -> (Vec<u8>, u64) {
pub fn cacluate_other_info_length(data: &Vec<serde_json::Value>) -> (Vec<u8>, u64) {
let mut lenth: u64 = 0;
let mut vecback= Vec::new();
if data.is_empty() {
@ -157,7 +133,7 @@ pub fn cacluate_other_info_length(data: &Vec<OtherInfo>) -> (Vec<u8>, u64) {
(vecback, lenth)
}
pub fn caculate_spectral_info_length(data: &Vec<SpectralInfo>) -> (Vec<u8>, u64) {
pub fn caculate_spectral_info_length(data: &Vec<serde_json::Value>) -> (Vec<u8>, u64) {
let mut lenth: u64 = 0;
let mut vecback= Vec::new();
if data.is_empty() {
@ -175,15 +151,7 @@ pub fn caculate_spectral_info_length(data: &Vec<SpectralInfo>) -> (Vec<u8>, u64)
lenth+=2;
lenth+=1;
let mut lenthofthisinfo:u16=0;
let json=json!({
"SensorId": info.sensor_id,
"WaveCoeff": {
"a1": info.wave_coeff[0],
"a2": info.wave_coeff[1],
"a3": info.wave_coeff[2],
"a4": info.wave_coeff[3]
}
});
let json=info;
let json_string = serde_json::to_string(&json).unwrap();
let json_bytes = json_string.as_bytes();
lenthofthisinfo= json_bytes.len() as u16+1;