Skip to content

Instantly share code, notes, and snippets.

@selaux
Created April 25, 2022 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selaux/e0fecf9148d6dd573b564adfea86a338 to your computer and use it in GitHub Desktop.
Save selaux/e0fecf9148d6dd573b564adfea86a338 to your computer and use it in GitHub Desktop.
// content can be anything that implements read
let stci = Stci::from_input(&mut content.as_slice())
.map_err(|e| format!("error decoding `{:?}`: {}", file, e))?;
let size = match &stci {
Stci::Indexed { sub_images, .. } => sub_images
.get(0)
.ok_or_else(|| "indexed stci does not have at least one subimage".to_owned())
.map(|s| (u32::from(s.dimensions.0), u32::from(s.dimensions.1))),
Stci::Rgb { width, height, .. } => Ok((u32::from(*width), u32::from(*height))),
}?;
let mut image = RgbaImage::new(size.0, size.1);
match &stci {
Stci::Indexed {
sub_images,
palette,
} => {
let sub_image = &sub_images[0];
let width = usize::from(sub_image.dimensions.0);
let height = usize::from(sub_image.dimensions.1);
for y in 0..height {
for x in 0..width {
let index = usize::from((y * width) + x);
let stci_pixel = palette.colors[usize::from(sub_image.data[index])];
let pixel = image.get_pixel_mut(x as u32, y as u32);
if sub_image.data[index] == 0 {
pixel[3] = 0;
} else {
pixel[0] = stci_pixel.0;
pixel[1] = stci_pixel.1;
pixel[2] = stci_pixel.2;
pixel[3] = 255;
}
}
}
}
Stci::Rgb {
width,
height,
data,
} => {
let width = usize::from(*width);
let height = usize::from(*height);
for y in 0..height {
for x in 0..width {
let index = (y * width) + x;
let stci_pixel = StciRgb888::from(data[index]);
let pixel = image.get_pixel_mut(x as u32, y as u32);
pixel[0] = stci_pixel.0;
pixel[1] = stci_pixel.1;
pixel[2] = stci_pixel.2;
pixel[3] = 255;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment