Skip to content

Commit

Permalink
🛠️ (icu_lib): Update LVGL decoder to use serde_json for other_info
Browse files Browse the repository at this point in the history
Switched the `other_info` map in the LVGL decoder from `BTreeMap` to `serde_json::Value` for better serialization and readability. Also, updated the `ImageInfo` struct to include `Serialize` and changed the type of `other_info` to `serde_json::Value`. Added necessary serde dependencies to `Cargo.toml` files.

Signed-off-by: Benign X <1341398182@qq.com>
  • Loading branch information
W-Mai committed Nov 20, 2024
1 parent fde03ac commit 1996dfa
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ eframe = { version = "0.26.0", features = ["glow"] }
egui_plot = "0.26.0"
log = "0.4.20"
env_logger = "0.11.2"
serde_yaml = "0.9.33"

[patch.crates-io]
icu_lib = { path = "icu_lib" }
Expand Down
2 changes: 2 additions & 0 deletions icu_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ color_quant = "1.1.0"
log = "0.4.20"
env_logger = "0.11.2"
png = "0.17.13"
serde_json = "1.0.133"
serde = { version = "1.0.197", features = ["derive"] }

[dev-dependencies]
criterion = "0.5.1"
Expand Down
38 changes: 19 additions & 19 deletions icu_lib/src/endecoder/lvgl/lvgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::midata::MiData;
use crate::EncoderParams;
use image::imageops;
use image::RgbaImage;
use std::collections::BTreeMap;
use serde_json::{json, Value};
use std::io::{Cursor, Write};

impl EnDecoder for LVGL {
Expand Down Expand Up @@ -110,15 +110,22 @@ impl EnDecoder for LVGL {

let header = ImageHeader::decode(Vec::from(header_data));

let mut other_info = BTreeMap::new();
let mut other_info = serde_json::Map::new();

other_info.insert(
"LVGL Version".to_string(),
format!("{:?}", header.version()),
Value::from(format!("{:#?}", header.version())),
);
other_info.insert(
"Color Format".to_string(),
Value::from(format!("{:#?}", header.cf())),
);
other_info.insert(
"Flags".to_string(),
Value::from(format!("{:#?}", header.flags())),
);
other_info.insert("Color Format".to_string(), format!("{:?}", header.cf()));
other_info.insert("Flags".to_string(), format!("{:?}", header.flags()));
if header.version() == LVGLVersion::V9 {
other_info.insert("Stride".to_string(), format!("{:?}", header.stride()));
other_info.insert("Stride".to_string(), Value::from(header.stride()));
}

// Deal Flag has Compressed
Expand All @@ -129,20 +136,13 @@ impl EnDecoder for LVGL {
data[9], data[10], data[11],
]);

let compressed_info = BTreeMap::from([
("Method", format!("{:#?}", compressed_header.method())),
(
"Size",
format!("{:#?}", compressed_header.compressed_size()),
),
(
"Decompressed Size",
format!("{:#?}", compressed_header.decompressed_size()),
),
]);
other_info.insert(
"Compressed Info".to_owned(),
format!("{:#?}", compressed_info),
json!({
"Method": format!("{:#?}", compressed_header.method()),
"Size": compressed_header.compressed_size(),
"Decompressed Size": compressed_header.decompressed_size()
}),
);
}

Expand All @@ -151,7 +151,7 @@ impl EnDecoder for LVGL {
height: header.h() as u32,
data_size: data.len() as u32,
format: format!("LVGL.{:?}({:?})", header.version(), header.cf()),
other_info,
other_info: Value::from(other_info),
}
}
}
4 changes: 3 additions & 1 deletion icu_lib/src/endecoder/lvgl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@ impl ImageDescriptor {
use super::utils::rle::RleCoder;
let rle_coder = RleCoder::new(1).unwrap();
if compressed_header.decompressed_size() != data_size {
log::error!("Compressed data size mismatch, but still try to decode");
log::error!(
"Compressed data size mismatch, but still try to decode"
);
}
let decoded = rle_coder
.decode(&data[std::mem::size_of::<ImageCompressedHeader>()..]);
Expand Down
5 changes: 3 additions & 2 deletions icu_lib/src/endecoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ pub mod utils;

use crate::midata::MiData;
use crate::{endecoder, EncoderParams};
use serde::Serialize;

#[derive(Debug)]
#[derive(Debug, Serialize)]
pub struct ImageInfo {
pub width: u32,
pub height: u32,
pub data_size: u32,
pub format: String,

pub other_info: std::collections::BTreeMap<String, String>,
pub other_info: serde_json::Value,
}

pub trait EnDecoder {
Expand Down
15 changes: 9 additions & 6 deletions icu_lib/src/endecoder/utils/rle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ impl RleCoder {
let block = blocks[i];

// Count repeated blocks
let repeat_count = blocks[i..].iter()
let repeat_count = blocks[i..]
.iter()
.take_while(|&x| x == &block)
.count()
.min(127);
Expand All @@ -45,7 +46,8 @@ impl RleCoder {
i += repeat_count;
} else {
// Direct copy mode
let literal_end = blocks[i..].iter()
let literal_end = blocks[i..]
.iter()
.take(127)
.take_while(|&&x| {
let next_pos = i + 1;
Expand All @@ -54,7 +56,8 @@ impl RleCoder {
.count();

result.push(0x80 | (literal_end as u8));
blocks[i..i + literal_end].iter()
blocks[i..i + literal_end]
.iter()
.for_each(|block| result.extend_from_slice(block));
i += literal_end;
}
Expand All @@ -71,7 +74,8 @@ impl RleCoder {
let ctrl = *data.get(i).ok_or(RleError::InvalidInput)?;
i += 1;

let block = data.get(i..i + self.block_size)
let block = data
.get(i..i + self.block_size)
.ok_or(RleError::InvalidInput)?;

if ctrl & 0x80 == 0 {
Expand All @@ -81,8 +85,7 @@ impl RleCoder {
// Direct copy mode
let count = (ctrl & 0x7f) as usize;
let bytes = count * self.block_size;
let slice = data.get(i..i + bytes)
.ok_or(RleError::InvalidInput)?;
let slice = data.get(i..i + bytes).ok_or(RleError::InvalidInput)?;
result.extend_from_slice(slice);
i += bytes - self.block_size;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ fn process() -> Result<(), Box<dyn std::error::Error>> {
let data = fs::read(file)?;
let info = get_info_with(data, *input_format)?;

println!("{:#?}", info);
let yaml = serde_yaml::to_string(&info)?;

println!("{}", yaml);
}
SubCommands::Show { file, input_format } => {
let data = fs::read(file)?;
Expand Down

0 comments on commit 1996dfa

Please sign in to comment.