Skip to content

Commit

Permalink
fix: replace apng-encoder with png (#994)
Browse files Browse the repository at this point in the history
* replace apng-encoder with png

* bit depth
  • Loading branch information
montekki authored Apr 13, 2022
1 parent 47b176a commit d5e13aa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 94 deletions.
69 changes: 1 addition & 68 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/qrcode_rtx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2018"
hex = "0.4.3"
raptorq = "1.6.4"
qrcodegen = "1.6.0"
apng-encoder = "0.3.0"
png = "0.17.5"
constants = {path = "../constants"}
qrcode_static = {path = "../qrcode_static"}

Expand Down
39 changes: 14 additions & 25 deletions rust/qrcode_rtx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,22 @@ fn make_apng(data: Vec<QrCode>, output_name: &str) -> Result<(), Box<dyn std::er
let mut output_file = fs::File::create(output_name)?;
let frames_count: u32 = data.len() as u32;
let border_size = BORDER * SCALING;
let size: u32 = (data[0].size() as u32) * (SCALING as u32) + 2 * border_size as u32; // size is always positive and small
let apng_meta = apng_encoder::Meta {
width: size,
height: size,
color: apng_encoder::Color::Grayscale(8),
frames: frames_count,
plays: None,
};
let apng_frame = apng_encoder::Frame {
delay: Some(apng_encoder::Delay::new(FPS_NOM, FPS_DEN)),
..Default::default()
};
let mut apng_encoder = match apng_encoder::Encoder::create(&mut output_file, apng_meta) {
Ok(a) => a,
Err(e) => return Err(Box::from(format!("Apng encoder error. {}", e))),
};

// size is always positive and small
let size: u32 = (data[0].size() as u32) * (SCALING as u32) + 2 * border_size as u32;
let mut encoder = png::Encoder::new(&mut output_file, size, size);

encoder.set_color(png::ColorType::Grayscale);
encoder.set_animated(frames_count, 0)?;
encoder.set_frame_delay(FPS_NOM, FPS_DEN)?;
encoder.set_depth(png::BitDepth::Eight);

let mut writer = encoder.write_header()?;
// making actual apng
// qr.get_module(x,y) = false corresponds to back color (white by default)
// qr.get_module(x,y) = true corresponds to main color (black by default)
for qr in data.iter() {
let mut buffer: Vec<u8> = Vec::new();
let mut buffer: Vec<u8> = Vec::with_capacity((size * size) as usize);
for y in 0..size {
for x in 0..size {
if qr.get_module(x as i32 / SCALING - BORDER, y as i32 / SCALING - BORDER) {
Expand All @@ -87,15 +81,10 @@ fn make_apng(data: Vec<QrCode>, output_name: &str) -> Result<(), Box<dyn std::er
}
}
}
match apng_encoder.write_frame(&buffer, Some(&apng_frame), None, None) {
Ok(a) => a,
Err(e) => return Err(Box::from(format!("Apng encoder error. {}", e))),
}
}
match apng_encoder.finish() {
Ok(a) => a,
Err(e) => return Err(Box::from(format!("Apng encoder error. {}", e))),
writer.write_image_data(&buffer)?;
}
writer.finish()?;

Ok(())
}

Expand Down

0 comments on commit d5e13aa

Please sign in to comment.