diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | gifed/examples/read.rs | 10 | ||||
-rw-r--r-- | gifed/src/colorimage.rs | 2 | ||||
-rw-r--r-- | gifed/src/gif.rs | 24 |
4 files changed, 32 insertions, 7 deletions
diff --git a/.gitignore b/.gitignore index 4699ed7..e1ad110 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ Cargo.lock *.gif *.txt -*.png \ No newline at end of file +*.png +**/examples/read \ No newline at end of file diff --git a/gifed/examples/read.rs b/gifed/examples/read.rs index 26c342a..3c3d611 100644 --- a/gifed/examples/read.rs +++ b/gifed/examples/read.rs @@ -17,8 +17,14 @@ fn main() { let file = File::create(filename).unwrap(); let mut encoder = png::Encoder::new(file, image.width as u32, image.height as u32); - encoder.set_color(png::ColorType::Rgba); + encoder.set_color(png::ColorType::Indexed); + encoder.set_palette(image.palette.as_bytes()); + + if let Some(trns) = image.png_trns() { + encoder.set_trns(trns); + } + let mut writer = encoder.write_header().unwrap(); - writer.write_image_data(&image.rgba().unwrap()).unwrap(); + writer.write_image_data(&image.indicies).unwrap(); } } diff --git a/gifed/src/colorimage.rs b/gifed/src/colorimage.rs index 93314ff..c9cae79 100644 --- a/gifed/src/colorimage.rs +++ b/gifed/src/colorimage.rs @@ -49,7 +49,7 @@ impl<'a> TryFrom<Image<'a>> for ColorImage { img.height, img.indicies, img.palette, - img.trans_index(), + img.transparent_index(), ) } } diff --git a/gifed/src/gif.rs b/gifed/src/gif.rs index 97da21e..3ccc185 100644 --- a/gifed/src/gif.rs +++ b/gifed/src/gif.rs @@ -122,7 +122,7 @@ impl<'a> Image<'a> { let mut rgba = vec![0; self.indicies.len() * 4]; for (image_index, &color_index) in self.indicies.iter().enumerate() { - match self.trans_index() { + match self.transparent_index() { Some(trans) if trans == color_index => { rgba[image_index as usize * 4] = 0; rgba[image_index * 4 + 1] = 0; @@ -149,7 +149,7 @@ impl<'a> Image<'a> { let mut rgb = vec![0; self.indicies.len() * 3]; for (image_index, &color_index) in self.indicies.iter().enumerate() { - match self.trans_index() { + match self.transparent_index() { Some(trans) if trans == color_index => { rgb[image_index as usize * 4] = transparent_replace.r; rgb[image_index * 3 + 1] = transparent_replace.g; @@ -180,11 +180,29 @@ impl<'a> Image<'a> { None } - pub fn trans_index(&self) -> Option<u8> { + pub fn transparent_index(&self) -> Option<u8> { self.graphic_control() .map(|gce| gce.transparent_index()) .flatten() } + + pub fn png_trns(&self) -> Option<Vec<u8>> { + if let Some(trans_idx) = self.transparent_index() { + let mut trns = Vec::with_capacity(self.palette.len()); + + for idx in 0..self.palette.len() as u8 { + if idx == trans_idx { + trns.push(0u8); + } else { + trns.push(255); + } + } + + return Some(trns); + } + + None + } } #[cfg(test)] |