71 lines
2.7 KiB
Rust
71 lines
2.7 KiB
Rust
|
|
use std::path::Path;
|
||
|
|
use hcie_psd::psd_reader::PsdReader;
|
||
|
|
|
||
|
|
fn main() {
|
||
|
|
let path = Path::new("/mnt/extra/00_PROJECTS/hcie-rust-v4/_tmp/Example3-mini-roundtrip.psd");
|
||
|
|
let bytes = std::fs::read(path).unwrap();
|
||
|
|
|
||
|
|
let mut r = PsdReader::new(&bytes);
|
||
|
|
r.read(26).unwrap();
|
||
|
|
let color_mode_len = r.read_u32().unwrap() as usize;
|
||
|
|
r.read(color_mode_len).unwrap();
|
||
|
|
let img_res_len = r.read_u32().unwrap() as usize;
|
||
|
|
r.read(img_res_len).unwrap();
|
||
|
|
let layer_mask_len = r.read_u32().unwrap() as usize;
|
||
|
|
println!("Layer/mask info len: {}", layer_mask_len);
|
||
|
|
|
||
|
|
let layer_info_len = r.read_u32().unwrap() as usize;
|
||
|
|
println!("Layer info len: {}", layer_info_len);
|
||
|
|
let _layer_info_start = r.pos();
|
||
|
|
|
||
|
|
let layer_count_raw = r.read_i16().unwrap();
|
||
|
|
println!("Layer count raw: {}", layer_count_raw);
|
||
|
|
let layer_count = layer_count_raw.abs() as usize;
|
||
|
|
|
||
|
|
let mut layer_records = Vec::new();
|
||
|
|
for idx in 0..layer_count {
|
||
|
|
let top = r.read_i32().unwrap();
|
||
|
|
let left = r.read_i32().unwrap();
|
||
|
|
let bottom = r.read_i32().unwrap();
|
||
|
|
let right = r.read_i32().unwrap();
|
||
|
|
let channel_count = r.read_u16().unwrap() as usize;
|
||
|
|
|
||
|
|
let mut channels = Vec::new();
|
||
|
|
for _ in 0..channel_count {
|
||
|
|
let id = r.read_i16().unwrap();
|
||
|
|
let len = r.read_u32().unwrap();
|
||
|
|
channels.push((id, len));
|
||
|
|
}
|
||
|
|
println!("Layer {}: top={} left={} bottom={} right={} channels={:?}", idx, top, left, bottom, right, &channels);
|
||
|
|
|
||
|
|
r.read(4).unwrap(); // sig
|
||
|
|
r.read(4).unwrap(); // blend
|
||
|
|
r.read_u8().unwrap(); // opacity
|
||
|
|
r.read_u8().unwrap(); // clipping
|
||
|
|
r.read_u8().unwrap(); // flags
|
||
|
|
r.read(1).unwrap(); // filler
|
||
|
|
|
||
|
|
let extra_data_len = r.read_u32().unwrap() as usize;
|
||
|
|
let extra_start = r.pos();
|
||
|
|
|
||
|
|
// Skip extra data (we don't need to parse it here)
|
||
|
|
r.read(extra_data_len).unwrap();
|
||
|
|
|
||
|
|
layer_records.push((idx, top, left, bottom, right, channels, extra_start, extra_data_len));
|
||
|
|
}
|
||
|
|
|
||
|
|
println!("\n=== Channel Data ===");
|
||
|
|
for (idx, top, left, bottom, right, channels, _, _) in layer_records {
|
||
|
|
let w = (right - left) as u32;
|
||
|
|
let h = (bottom - top) as u32;
|
||
|
|
println!("\nLayer {}: {}x{} at ({},{})", idx, w, h, left, top);
|
||
|
|
for (ch_id, ch_len) in channels {
|
||
|
|
let compression = r.read_u16().unwrap();
|
||
|
|
let data_len = ch_len as usize - 2;
|
||
|
|
let data = r.read(data_len).unwrap();
|
||
|
|
let nonzero = data.iter().filter(|&&v| v > 0).count();
|
||
|
|
println!(" Channel {}: compression={} data_len={} nonzero={}", ch_id, compression, data_len, nonzero);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|