From 1996dfa999b0f68c295bce3b49a8a440c0317b1e Mon Sep 17 00:00:00 2001 From: Benign X <1341398182@qq.com> Date: Wed, 20 Nov 2024 17:30:43 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20(icu=5Flib):=20Update?= =?UTF-8?q?=20LVGL=20decoder=20to=20use=20serde=5Fjson=20for=20other=5Finf?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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> --- Cargo.toml | 1 + icu_lib/Cargo.toml | 2 ++ icu_lib/src/endecoder/lvgl/lvgl.rs | 38 +++++++++++++++--------------- icu_lib/src/endecoder/lvgl/mod.rs | 4 +++- icu_lib/src/endecoder/mod.rs | 5 ++-- icu_lib/src/endecoder/utils/rle.rs | 15 +++++++----- src/main.rs | 4 +++- 7 files changed, 40 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fba8791..07e6e0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/icu_lib/Cargo.toml b/icu_lib/Cargo.toml index 2bcfa08..7f451a0 100644 --- a/icu_lib/Cargo.toml +++ b/icu_lib/Cargo.toml @@ -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" diff --git a/icu_lib/src/endecoder/lvgl/lvgl.rs b/icu_lib/src/endecoder/lvgl/lvgl.rs index aeb925f..a6ca7e4 100644 --- a/icu_lib/src/endecoder/lvgl/lvgl.rs +++ b/icu_lib/src/endecoder/lvgl/lvgl.rs @@ -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 { @@ -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 @@ -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() + }), ); } @@ -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), } } } diff --git a/icu_lib/src/endecoder/lvgl/mod.rs b/icu_lib/src/endecoder/lvgl/mod.rs index 71ee5cf..8e7d244 100644 --- a/icu_lib/src/endecoder/lvgl/mod.rs +++ b/icu_lib/src/endecoder/lvgl/mod.rs @@ -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::()..]); diff --git a/icu_lib/src/endecoder/mod.rs b/icu_lib/src/endecoder/mod.rs index 7f24db3..4b8e1d8 100644 --- a/icu_lib/src/endecoder/mod.rs +++ b/icu_lib/src/endecoder/mod.rs @@ -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, + pub other_info: serde_json::Value, } pub trait EnDecoder { diff --git a/icu_lib/src/endecoder/utils/rle.rs b/icu_lib/src/endecoder/utils/rle.rs index 5dcbc2c..2672cbb 100644 --- a/icu_lib/src/endecoder/utils/rle.rs +++ b/icu_lib/src/endecoder/utils/rle.rs @@ -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); @@ -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; @@ -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; } @@ -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 { @@ -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; } diff --git a/src/main.rs b/src/main.rs index bbfdd05..8416d1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,9 @@ fn process() -> Result<(), Box> { 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)?;