Skip to content

Commit a19c71a

Browse files
committed
fix(canvas): asset loader from path
Load images w/o known extension in path
1 parent 59b0d60 commit a19c71a

File tree

1 file changed

+40
-5
lines changed
  • packages/canvas/src-native/canvas-native/canvas-core/src/common

1 file changed

+40
-5
lines changed

packages/canvas/src-native/canvas-native/canvas-core/src/common/image_asset.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::ffi::{CStr, CString};
2+
use std::io::{Read, Seek, SeekFrom};
23
use std::mem;
34
use std::os::raw::{c_char, c_longlong, c_uint};
45
use std::ptr::{null, null_mut};
@@ -82,14 +83,48 @@ impl NativeImageAsset {
8283
}
8384
self.image = None;
8485
let real_path = unsafe { CStr::from_ptr(path) }.to_str().unwrap_or("");
85-
let result = image::open(real_path);
86-
match result {
87-
Ok(result) => {
88-
self.image = Some(result);
89-
1
86+
let file = std::fs::File::open(real_path);
87+
match file {
88+
Ok(file) => {
89+
let mut reader = std::io::BufReader::new(file);
90+
let mut bytes = [0; 16];
91+
let result = reader.read(&mut bytes);
92+
match result {
93+
Ok(_) => {
94+
let _ = reader.seek(SeekFrom::Start(0));
95+
let mime = image::guess_format(&bytes);
96+
match mime {
97+
Ok(mime) => match image::load(reader, mime) {
98+
Ok(result) => {
99+
self.image = Some(result);
100+
1
101+
}
102+
Err(e) => {
103+
let error = e.to_string();
104+
self.error.clear();
105+
self.error.push_str(error.as_str());
106+
0
107+
}
108+
},
109+
Err(e) => {
110+
let error = e.to_string();
111+
self.error.clear();
112+
self.error.push_str(error.as_str());
113+
0
114+
}
115+
}
116+
}
117+
Err(e) => {
118+
let error = e.to_string();
119+
self.error.clear();
120+
self.error.push_str(error.as_str());
121+
0
122+
}
123+
}
90124
}
91125
Err(e) => {
92126
let error = e.to_string();
127+
self.error.clear();
93128
self.error.push_str(error.as_str());
94129
0
95130
}

0 commit comments

Comments
 (0)