mirror of
https://github.com/lifegpc/msg-tool.git
synced 2026-06-07 13:28:47 +08:00
Add support to export multiple images
This commit is contained in:
@@ -282,6 +282,42 @@ pub trait Script: std::fmt::Debug {
|
||||
let f = std::io::BufWriter::new(f);
|
||||
self.import_image(data, Box::new(f))
|
||||
}
|
||||
|
||||
#[cfg(feature = "image")]
|
||||
fn is_multi_image(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[cfg(feature = "image")]
|
||||
fn export_multi_image<'a>(
|
||||
&'a self,
|
||||
) -> Result<Box<dyn Iterator<Item = Result<ImageDataWithName>> + 'a>> {
|
||||
Err(anyhow::anyhow!(
|
||||
"This script type does not support to export multi image."
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(feature = "image")]
|
||||
fn import_multi_image<'a>(
|
||||
&'a self,
|
||||
_data: Vec<ImageDataWithName>,
|
||||
_file: Box<dyn WriteSeek + 'a>,
|
||||
) -> Result<()> {
|
||||
Err(anyhow::anyhow!(
|
||||
"This script type does not support to import multi image."
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(feature = "image")]
|
||||
fn import_multi_image_filename(
|
||||
&self,
|
||||
data: Vec<ImageDataWithName>,
|
||||
filename: &str,
|
||||
) -> Result<()> {
|
||||
let f = std::fs::File::create(filename)?;
|
||||
let f = std::io::BufWriter::new(f);
|
||||
self.import_multi_image(data, Box::new(f))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Archive {
|
||||
|
||||
@@ -107,7 +107,11 @@ impl ScriptBuilder for CSIntArcBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
fn detect_script_type(_buf: &[u8], _buf_len: usize, _filename: &str) -> Option<&'static ScriptType> {
|
||||
fn detect_script_type(
|
||||
_buf: &[u8],
|
||||
_buf_len: usize,
|
||||
_filename: &str,
|
||||
) -> Option<&'static ScriptType> {
|
||||
#[cfg(feature = "cat-system-img")]
|
||||
if _buf_len >= 4 && _buf.starts_with(b"HG-3") {
|
||||
return Some(&ScriptType::CatSystemHg3);
|
||||
|
||||
@@ -149,6 +149,69 @@ impl Script for Hg3Image {
|
||||
}
|
||||
Ok(img)
|
||||
}
|
||||
|
||||
fn is_multi_image(&self) -> bool {
|
||||
self.entries.len() > 1
|
||||
}
|
||||
|
||||
fn export_multi_image<'a>(
|
||||
&'a self,
|
||||
) -> Result<Box<dyn Iterator<Item = Result<ImageDataWithName>> + 'a>> {
|
||||
Ok(Box::new(Hg3ImageIter {
|
||||
iter: self.entries.iter(),
|
||||
index: 0,
|
||||
data: self.data.to_ref(),
|
||||
draw_canvas: self.draw_canvas,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
struct Hg3ImageIter<'a, T: Iterator<Item = &'a (Hg3Entry, usize, usize)> + 'a> {
|
||||
iter: T,
|
||||
index: usize,
|
||||
data: MemReaderRef<'a>,
|
||||
draw_canvas: bool,
|
||||
}
|
||||
|
||||
impl<'a, T: Iterator<Item = &'a (Hg3Entry, usize, usize)> + 'a> Iterator for Hg3ImageIter<'a, T> {
|
||||
type Item = Result<ImageDataWithName>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if let Some((entry, offset, size)) = self.iter.next() {
|
||||
let data = &self.data.data[*offset..*offset + *size];
|
||||
let reader = Hg3Reader {
|
||||
m_input: MemReaderRef::new(data),
|
||||
m_info: entry.clone(),
|
||||
m_pixel_size: entry.bpp / 8,
|
||||
};
|
||||
self.index += 1;
|
||||
match reader.unpack() {
|
||||
Ok(mut img) => {
|
||||
if self.draw_canvas {
|
||||
if entry.canvas_width > 0 && entry.canvas_height > 0 {
|
||||
img = match draw_on_canvas(
|
||||
img,
|
||||
entry.canvas_width,
|
||||
entry.canvas_height,
|
||||
entry.offset_x,
|
||||
entry.offset_y,
|
||||
) {
|
||||
Ok(canvas_img) => canvas_img,
|
||||
Err(e) => return Some(Err(e)),
|
||||
};
|
||||
}
|
||||
}
|
||||
Some(Ok(ImageDataWithName {
|
||||
name: format!("{:04}", self.index - 1),
|
||||
data: img,
|
||||
}))
|
||||
},
|
||||
Err(e) => Some(Err(e)),
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Hg3Reader<'a> {
|
||||
|
||||
Reference in New Issue
Block a user