Skip to content

Commit

Permalink
Move from image to png crate
Browse files Browse the repository at this point in the history
We used the famous and good `image` crate to load the favicon. Its a good crate but its in fact has features we don't need, Its in fact a "An Image Processing Library" like it is mentioned on their README. Its also supports multiple image formats which we don't need. The `png` is a PNG & APNG Decoder & Encoder, Exactly what we need. Its about Half the size of the image crate (while only having png feature)
  • Loading branch information
Snowiiii committed Oct 7, 2024
1 parent ff125c0 commit a0b4785
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 36 deletions.
28 changes: 5 additions & 23 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions pumpkin-protocol/src/client/play/c_entity_animation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use num_derive::ToPrimitive;
use pumpkin_macros::packet;
use serde::Serialize;

Expand All @@ -21,7 +20,7 @@ impl CEntityAnimation {
}
}

#[derive(ToPrimitive)]
#[repr(u8)]
pub enum Animation {
SwingMainArm,
LeaveBed,
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ thiserror = "1.0"

# icon loading
base64 = "0.22.1"
image = { version = "0.25", default-features = false, features = ["png"] }
png = "0.17.14"

# logging
simple_logger = "5.0.0"
Expand Down
23 changes: 13 additions & 10 deletions pumpkin/src/server/connection_cache.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{io::Cursor, path::Path};
use std::{fs::File, path::Path};

use base64::{engine::general_purpose, Engine as _};
use image::GenericImageView as _;
use pumpkin_config::{BasicConfiguration, BASIC_CONFIG};
use pumpkin_protocol::{
client::{config::CPluginMessage, status::CStatusResponse},
Expand Down Expand Up @@ -85,15 +84,19 @@ impl CachedStatus {
}

fn load_icon(path: &str) -> String {
let icon = image::open(path).expect("Failed to load icon");
let dimension = icon.dimensions();
assert!(dimension.0 == 64, "Icon width must be 64");
assert!(dimension.1 == 64, "Icon height must be 64");
let mut image = Vec::with_capacity(64 * 64 * 4);
icon.write_to(&mut Cursor::new(&mut image), image::ImageFormat::Png)
.unwrap();
let icon = png::Decoder::new(File::open(path).expect("Failed to load icon"));
let mut reader = icon.read_info().unwrap();
let info = reader.info();
assert!(info.width == 64, "Icon width must be 64");
assert!(info.height == 64, "Icon height must be 64");
// Allocate the output buffer.
let mut buf = vec![0; reader.output_buffer_size()];
// Read the next frame. An APNG might contain multiple frames.
let info = reader.next_frame(&mut buf).unwrap();
// Grab the bytes of the image.
let bytes = &buf[..info.buffer_size()];
let mut result = "data:image/png;base64,".to_owned();
general_purpose::STANDARD.encode_string(image, &mut result);
general_purpose::STANDARD.encode_string(bytes, &mut result);
result
}
}

0 comments on commit a0b4785

Please sign in to comment.