Skip to content

Commit

Permalink
fix: free mem
Browse files Browse the repository at this point in the history
  • Loading branch information
rsuu committed Dec 7, 2022
1 parent c9c1126 commit 1ef136a
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 176 deletions.
116 changes: 78 additions & 38 deletions Cargo.lock

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

10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ zip = { version = "0.6.3", default-features = false, features = ["aes-crypto","b
# filetype
infer = "0.9.0"

# window
#sdl2 = { version = "0.35.2", features = ["image", "ttf"] }
minifb = { version = "0.23.0", features = ["x11", "wayland"] }

# image
image = { version = "0.24.4" }
fir = {package="fast_image_resize" ,version="1.0.0"}
libheif-rs = { version = "0.15.0", optional = true }
asefile ={ version = "0.3.5", optional = true }

# args
lexopt = "0.2.1"
Expand All @@ -51,7 +55,7 @@ lexopt = "0.2.1"
syn = { version = "1.0.102", features = ["full", "extra-traits"] }

# async
tokio = { version = "1.21.2", features = ["rt", "macros", "rt-multi-thread"] }
pollster = "0.2.5"

# other
cfg-if = "1.0.0"
Expand All @@ -60,6 +64,8 @@ cfg-if = "1.0.0"
log = "0.4.17"
simple_logger = { version = "2.3.0", features = ["colors", "threads"] }



[dev-dependencies]
# benchmark
criterion = "0.4.0"
Expand All @@ -82,4 +88,4 @@ avx2 = []

# image
de_heic = ["dep:libheif-rs"]
de_ase = []
de_ase = ["dep:asefile"]
21 changes: 15 additions & 6 deletions src/img/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn resize_bytes(
screen_size: Size<u32>,
window_size: Size<u32>,
filter: &fir::FilterType,
) {
) -> (u32, u32) {
let mut meta = MetaSize::<u32>::new(
screen_size.width,
screen_size.height,
Expand All @@ -42,28 +42,37 @@ pub fn resize_bytes(
0,
);

let mut res = (0, 0);

if let Ok(ref img) = image::load_from_memory(bytes) {
meta.image.width = img.width();
meta.image.height = img.height();
res = (img.width(), img.height());

meta.image.width = res.0;
meta.image.height = res.1;
meta.resize();

resize_rgba8(buffer, img.to_rgba8().into_vec(), &meta, filter).unwrap();
} else if let Ok(res) = heic::load_heic(bytes) {
} else if let Ok(img) = heic::load_heic(bytes) {
// heic

res = (img.0, img.1);

meta.image.width = res.0;
meta.image.height = res.1;
meta.resize();

resize_rgba8(buffer, res.2, &meta, filter).unwrap();
resize_rgba8(buffer, img.2, &meta, filter).unwrap();

log::debug!("{:?}", (res.0, res.1));
log::debug!("{:?}", (img.0, img.1));
log::debug!("{:?}", &meta);
} else {
todo!()
}

res
}

#[inline]
pub fn resize_rgba8(
buffer: &mut Vec<u8>,
bytes: Vec<u8>,
Expand Down
14 changes: 8 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ use rmg::{
archive::{self, ArchiveType},
config::rsconf::Config,
img::size::{MetaSize, TMetaSize},
reader::{
display,
view::{Img, Page},
},
reader::{display, view::Page},
utils::{cli, err::MyErr, file},
};
use simple_logger;
use std::path::Path;

#[tokio::main]
async fn main() {
fn main() {
init_log();

pollster::block_on(async {
start().await;
});
}

async fn start() {
let mut args = cli::Args::new();
let mut config: Config = args.init_config();

Expand Down
13 changes: 5 additions & 8 deletions src/reader/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use crate::{
},
utils::err::Res,
};
//use emeta::meta;

use log::{debug, info};
use std::{
path::PathBuf,
Expand All @@ -29,17 +27,16 @@ pub async fn cat_img(
) -> Res<()> {
let screen_size = meta_size.screen;
let window_size = meta_size.window;
let max_ram = window_size.width as usize * window_size.height as usize;
let limit_next = max_ram * 8;
let limit_prev = max_ram * 2;
let buffer_max = window_size.width as usize * window_size.height as usize;

let step = config.base.step as usize;
let filter = config.base.filter;
let keymaps = keymap::KeyMap::new();

let mut buf = Render {
buffer: Buffer::new(), // buffer
max_ram,
buffer_max,
mem_limit: buffer_max * 8,

head: 0,
tail: 0,
Expand All @@ -54,7 +51,7 @@ pub async fn cat_img(
page_end: page_list.len(), //
page_list, //
screen_size, //
y_step: max_ram / step, // drop 1/step part of image once
y_step: buffer_max / step, // drop 1/step part of image once
x_step: window_size.width as usize / step, //
window_position: (0, 0), //
window_size, //
Expand Down Expand Up @@ -111,7 +108,7 @@ pub async fn for_minifb_image(
_ => {}
}

canvas.flush(&buf.buffer.data[buf.rng..buf.rng + buf.max_ram]);
canvas.flush(&buf.buffer.data[buf.rng..buf.rng + buf.buffer_max]);

std::thread::sleep(std::time::Duration::from_millis(40));
}
Expand Down
Loading

0 comments on commit 1ef136a

Please sign in to comment.