Skip to content

Commit b0c8359

Browse files
committed
Split export_multi_image to iter_multi_image_name and open_image (#12)
1 parent 258fb2c commit b0c8359

4 files changed

Lines changed: 338 additions & 344 deletions

File tree

src/scripts/base.rs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,14 +568,58 @@ pub trait Script: std::fmt::Debug {
568568
false
569569
}
570570

571+
#[cfg(feature = "image")]
572+
/// Returns an iterator over images filenames.
573+
fn iter_multi_image_name<'a>(
574+
&'a self,
575+
) -> Result<Box<dyn Iterator<Item = Result<String>> + 'a>> {
576+
Err(anyhow::anyhow!(
577+
"This script type does not support iter multi image name."
578+
))
579+
}
580+
581+
#[cfg(feature = "image")]
582+
/// Open a image in the multiple image file
583+
fn open_image<'a>(&'a self, _index: usize) -> Result<ImageDataWithName> {
584+
Err(anyhow::anyhow!(
585+
"This script type does not support open image."
586+
))
587+
}
588+
589+
#[cfg(feature = "image")]
590+
/// Open a image in the multiple image file by its name.
591+
///
592+
/// * `name` - The name of the file to open.
593+
/// * `ignore_case` - If true, the name comparison will be case-insensitive.
594+
fn open_image_by_name<'a>(
595+
&'a self,
596+
name: &str,
597+
ignore_case: bool,
598+
) -> Result<ImageDataWithName> {
599+
for (i, fname) in self.iter_multi_image_name()?.enumerate() {
600+
if let Ok(fname) = fname {
601+
if fname == name || (ignore_case && fname.eq_ignore_ascii_case(name)) {
602+
return self.open_image(i);
603+
}
604+
}
605+
}
606+
Err(anyhow::anyhow!(
607+
"File with name '{}' not found in archive.",
608+
name
609+
))
610+
}
611+
571612
#[cfg(feature = "image")]
572613
/// Exports multiple images from this script.
573614
fn export_multi_image<'a>(
574615
&'a self,
575616
) -> Result<Box<dyn Iterator<Item = Result<ImageDataWithName>> + 'a>> {
576-
Err(anyhow::anyhow!(
577-
"This script type does not support to export multi image."
578-
))
617+
Ok(Box::new(self.iter_multi_image_name()?.enumerate().map(
618+
|(i, fname)| {
619+
fname?;
620+
self.open_image(i)
621+
},
622+
)))
579623
}
580624

581625
#[cfg(feature = "image")]

src/scripts/cat_system/image/hg3.rs

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -165,63 +165,38 @@ impl Script for Hg3Image {
165165
self.entries.len() > 1
166166
}
167167

168-
fn export_multi_image<'a>(
168+
fn iter_multi_image_name<'a>(
169169
&'a self,
170-
) -> Result<Box<dyn Iterator<Item = Result<ImageDataWithName>> + 'a>> {
171-
Ok(Box::new(Hg3ImageIter {
172-
iter: self.entries.iter(),
173-
index: 0,
174-
data: self.data.to_ref(),
175-
draw_canvas: self.draw_canvas,
176-
}))
170+
) -> Result<Box<dyn Iterator<Item = Result<String>> + 'a>> {
171+
Ok(Box::new(
172+
(0..self.entries.len()).map(|i| Ok(format!("{:04}", i))),
173+
))
177174
}
178-
}
179-
180-
struct Hg3ImageIter<'a, T: Iterator<Item = &'a (Hg3Entry, usize, usize)> + 'a> {
181-
iter: T,
182-
index: usize,
183-
data: MemReaderRef<'a>,
184-
draw_canvas: bool,
185-
}
186175

187-
impl<'a, T: Iterator<Item = &'a (Hg3Entry, usize, usize)> + 'a> Iterator for Hg3ImageIter<'a, T> {
188-
type Item = Result<ImageDataWithName>;
189-
190-
fn next(&mut self) -> Option<Self::Item> {
191-
if let Some((entry, offset, size)) = self.iter.next() {
192-
let data = &self.data.data[*offset..*offset + *size];
193-
let reader = Hg3Reader {
194-
m_input: MemReaderRef::new(data),
195-
m_info: entry.clone(),
196-
m_pixel_size: entry.bpp / 8,
197-
};
198-
self.index += 1;
199-
match reader.unpack() {
200-
Ok(mut img) => {
201-
if self.draw_canvas {
202-
if entry.canvas_width > 0 && entry.canvas_height > 0 {
203-
img = match draw_on_canvas(
204-
img,
205-
entry.canvas_width,
206-
entry.canvas_height,
207-
entry.offset_x,
208-
entry.offset_y,
209-
) {
210-
Ok(canvas_img) => canvas_img,
211-
Err(e) => return Some(Err(e)),
212-
};
213-
}
214-
}
215-
Some(Ok(ImageDataWithName {
216-
name: format!("{:04}", self.index - 1),
217-
data: img,
218-
}))
219-
}
220-
Err(e) => Some(Err(e)),
176+
fn open_image<'a>(&'a self, index: usize) -> Result<ImageDataWithName> {
177+
let (entry, offset, size) = &self.entries[index];
178+
let data = &self.data.data[*offset..*offset + *size];
179+
let reader = Hg3Reader {
180+
m_input: MemReaderRef::new(data),
181+
m_info: entry.clone(),
182+
m_pixel_size: entry.bpp / 8,
183+
};
184+
let mut img = reader.unpack()?;
185+
if self.draw_canvas {
186+
if entry.canvas_width > 0 && entry.canvas_height > 0 {
187+
img = draw_on_canvas(
188+
img,
189+
entry.canvas_width,
190+
entry.canvas_height,
191+
entry.offset_x,
192+
entry.offset_y,
193+
)?;
221194
}
222-
} else {
223-
None
224195
}
196+
Ok(ImageDataWithName {
197+
name: format!("{:04}", index),
198+
data: img,
199+
})
225200
}
226201
}
227202

0 commit comments

Comments
 (0)