Skip to content

Commit

Permalink
Speed up image stuff (#223)
Browse files Browse the repository at this point in the history
* Get the dimensions without reading the full image into memory

This is faster than fully loading the image and then getting its dimensions.

* Introduce a way to insert images as a byte buffer
  • Loading branch information
xamgore authored Sep 13, 2024
1 parent 2674f66 commit 93dc08a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "umya-spreadsheet"
version = "2.0.2"
version = "2.0.3"
authors = ["MathNya <[email protected]>"]
repository = "https://github.com/MathNya/umya-spreadsheet"
keywords = ["excel", "spreadsheet", "xlsx", "reader", "writer"]
Expand All @@ -24,7 +24,7 @@ getrandom = { version = "0.2.14" }
hashbrown = "0.14.3"
hmac = "0.12.1"
html_parser = "0.7.0"
image = "0.25.0"
image = { version = "0.25.0", optional = true }
lazy_static = "1.4.0"
md-5 = "0.10.6"
regex = "1.10.2"
Expand All @@ -38,3 +38,4 @@ doctest = false

[features]
js = ["getrandom/js"]
default = ["image"]
23 changes: 13 additions & 10 deletions src/structs/image.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use base64::{engine::general_purpose::STANDARD, Engine as _};
use image::GenericImageView;
use quick_xml::Writer;
use std::fs;
use std::fs::File;
use std::io::Cursor;
use std::io::Read;
use std::io::BufReader;
use structs::drawing::spreadsheet::MarkerType;
use structs::drawing::spreadsheet::OneCellAnchor;
use structs::drawing::spreadsheet::Picture;
Expand Down Expand Up @@ -97,18 +97,21 @@ impl Image {
self
}

#[cfg(feature = "image")]
pub fn new_image(&mut self, path: &str, marker: MarkerType) {
let path_str = path;
let path_obj = std::path::Path::new(path_str);
let image_name = path_obj.file_name().unwrap().to_str().unwrap();
let path = std::path::Path::new(path);

let img = image::open(path_obj).unwrap();
let (width, height) = img.dimensions();

let mut file = File::open(path_str).unwrap();
let (width, height) = image::image_dimensions(path).unwrap();
let image_name = path.file_name().unwrap().to_str().unwrap();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();

let file = File::open(path).unwrap();
BufReader::new(file).read_to_end(&mut buf).unwrap();

self.new_image_with_dimensions(height, width, image_name, buf, marker)
}

pub fn new_image_with_dimensions<B: Into<Vec<u8>>>(&mut self, height: u32, width: u32, image_name: &str, bytes: B, marker: MarkerType) {
let mut picture = Picture::default();
// filename and filedata.
picture
Expand All @@ -117,7 +120,7 @@ impl Image {
.set_cstate("print")
.get_image_mut()
.set_image_name(image_name)
.set_image_data(buf);
.set_image_data(bytes.into());

// name
picture
Expand Down

0 comments on commit 93dc08a

Please sign in to comment.