Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APNG Encode #255

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Chunk types and functions
#![allow(dead_code)]
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]
use core::fmt;

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -88,3 +89,42 @@ impl fmt::Debug for ChunkType {
.finish()
}
}

use crate::{BitDepth, ColorType};
use io::Write;
use std::io;

pub fn encode_chunk<W: Write>(w: &mut W, chunk: ChunkType, data: &[u8]) -> io::Result<()> {
w.write_all(&(data.len() as u32).to_be_bytes())?;
w.write_all(&chunk.0)?;
w.write_all(data)?;

let mut crc = crc32fast::Hasher::new();
crc.update(&chunk.0);
crc.update(data);
w.write_all(&crc.finalize().to_be_bytes())
}

pub fn IHDR_encode<W: Write>(
w: &mut W,
width: u32,
height: u32,
bit_depth: BitDepth,
color_type: ColorType,
interlaced: bool,
) -> io::Result<()> {
let mut data = [0; 13];
data[..4].copy_from_slice(&width.to_be_bytes());
data[4..8].copy_from_slice(&height.to_be_bytes());
data[8] = bit_depth as u8;
data[9] = color_type as u8;
data[12] = interlaced as u8;
encode_chunk(w, IHDR, &data)
}

pub fn fdAT_encode<W: Write>(w: &mut W, seq_num: u32, data: &[u8]) -> io::Result<()> {
let mut all_data = vec![0u8; 4 + data.len()];
all_data[..4].copy_from_slice(&seq_num.to_be_bytes());
all_data[4..].copy_from_slice(&data);
encode_chunk(w, fdAT, &all_data)
}
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub struct AnimationControl {
}

/// The type and strength of applied compression.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub enum Compression {
/// Default level
Default,
Expand Down
Loading