use crate::error::{Error, Result};
pub fn write_gif(source: &[u8], new_comment: Option<&str>) -> Result<Vec<u8>> {
if source.len() < 13 || !source.starts_with(b"GIF8") {
return Err(Error::InvalidData("not a GIF file".into()));
}
let mut output = Vec::with_capacity(source.len());
output.extend_from_slice(&source[..13]);
let mut pos = 13;
let packed = source[10];
let has_gct = (packed & 0x80) != 0;
if has_gct {
let gct_size = 3 * (1 << ((packed & 0x07) + 1));
output.extend_from_slice(&source[pos..pos + gct_size]);
pos += gct_size;
}
if let Some(comment) = new_comment {
output.push(0x21); output.push(0xFE); let bytes = comment.as_bytes();
for chunk in bytes.chunks(255) {
output.push(chunk.len() as u8);
output.extend_from_slice(chunk);
}
output.push(0); }
while pos < source.len() {
match source[pos] {
0x21 if pos + 1 < source.len() && source[pos + 1] == 0xFE && new_comment.is_some() => {
pos += 2;
while pos < source.len() {
let block_size = source[pos] as usize;
pos += 1;
if block_size == 0 { break; }
pos += block_size;
}
}
_ => {
output.push(source[pos]);
pos += 1;
}
}
}
Ok(output)
}