diff options
Diffstat (limited to 'crates/gipfel-core/src')
| -rw-r--r-- | crates/gipfel-core/src/hill.rs | 22 | ||||
| -rw-r--r-- | crates/gipfel-core/src/metadata.rs | 43 | ||||
| -rw-r--r-- | crates/gipfel-core/src/panorama.rs | 10 |
3 files changed, 56 insertions, 19 deletions
diff --git a/crates/gipfel-core/src/hill.rs b/crates/gipfel-core/src/hill.rs index a904948..4ae0f89 100644 --- a/crates/gipfel-core/src/hill.rs +++ b/crates/gipfel-core/src/hill.rs @@ -2,9 +2,6 @@ //! //! Faithful port of gipfel's `Hill.cxx`. -use std::fs::File; -use std::io::{BufRead, BufReader}; - pub const FLAG_DUPLIC: u32 = 1 << 0; pub const FLAG_KNOWN: u32 = 1 << 1; pub const FLAG_HIDDEN: u32 = 1 << 2; @@ -77,16 +74,20 @@ fn atof(s: &str) -> f64 { 0.0 } -/// Load a hill database. Two line formats are accepted: +/// Load a hill database from a file path. +pub fn load(path: &str) -> std::io::Result<Vec<Hill>> { + load_from_bytes(&std::fs::read(path)?) +} + +/// Load a hill database from raw bytes. Two line formats are accepted: /// - `SHORT,NAME,DESC,LAT,LON,HEIGHT` (6 fields) /// - `LAT,LON,HEIGHT` (3 fields, track point) -pub fn load(path: &str) -> std::io::Result<Vec<Hill>> { - let file = File::open(path)?; +/// +/// Byte-oriented entry point (no filesystem): used by the WASM build and +/// by tests. The database is Latin-1; bytes decode to Unicode code points. +pub fn load_from_bytes(raw: &[u8]) -> std::io::Result<Vec<Hill>> { let mut hills = Vec::new(); - - // the database is Latin-1; decode to Unicode code points - for line in BufReader::new(file).split(b'\n') { - let raw = line?; + for raw in raw.split(|b| *b == b'\n') { let line: String = raw.iter().map(|b| *b as char).collect(); // strsep() semantics: split into at most 10 fields, keeping empties let vals: Vec<&str> = line.split(',').take(10).collect(); @@ -109,7 +110,6 @@ pub fn load(path: &str) -> std::io::Result<Vec<Hill>> { hills.push(h); } } - Ok(hills) } diff --git a/crates/gipfel-core/src/metadata.rs b/crates/gipfel-core/src/metadata.rs index e2a888f..f55bf1c 100644 --- a/crates/gipfel-core/src/metadata.rs +++ b/crates/gipfel-core/src/metadata.rs @@ -3,9 +3,6 @@ //! //! Faithful port of gipfel's `ImageMetaData.cxx`. -use std::fs::File; -use std::io::{BufReader, Write}; - use exif::{In, Tag, Value}; pub const NAN: f64 = f64::NAN; @@ -66,6 +63,16 @@ impl ImageMetaData { self.load_image_exif(path); } + /// Like [`ImageMetaData::load_image`] but from raw bytes (no filesystem); + /// used by the WASM build. + pub fn load_image_from_bytes(&mut self, bytes: &[u8]) { + *self = ImageMetaData::new(); + if let Some(com) = read_jpeg_comment_from_bytes(bytes) { + self.parse_gipfel_comment(&com); + } + self.load_image_exif_bytes(bytes); + } + /// Write the gipfel comment into a copy of `in_img`, saved as `out_img`. /// Writes atomically via a temp file, like the C original. pub fn save_image(&self, in_img: &str, out_img: &str) -> std::io::Result<()> { @@ -76,6 +83,12 @@ impl ImageMetaData { Ok(()) } + /// Like [`ImageMetaData::save_image`] but bytes in / bytes out (no + /// filesystem); used by the WASM build. + pub fn save_image_to_bytes(&self, in_bytes: &[u8]) -> std::io::Result<Vec<u8>> { + write_jpeg_comment_to_bytes(in_bytes, self.format_comment().as_bytes()) + } + pub fn format_comment(&self) -> String { format!( "gipfel: longitude {}, latitude {}, height {}, direction {}, nick {}, tilt {}, focal_length_35mm {}, projection type {}, k0 {}, k1 {}, x0 {}", @@ -149,8 +162,12 @@ impl ImageMetaData { } fn load_image_exif(&mut self, path: &str) { - let Ok(file) = File::open(path) else { return }; - let mut reader = BufReader::new(file); + let Ok(bytes) = std::fs::read(path) else { return }; + self.load_image_exif_bytes(&bytes); + } + + fn load_image_exif_bytes(&mut self, bytes: &[u8]) { + let mut reader = std::io::Cursor::new(bytes); let Ok(exif) = exif::Reader::new().read_from_container(&mut reader) else { return; }; @@ -366,6 +383,11 @@ fn marker_len_ok(marker: u8) -> bool { /// Return the payload of the first COM segment before SOS, if any. pub fn read_jpeg_comment(path: &str) -> Option<Vec<u8>> { let bytes = std::fs::read(path).ok()?; + read_jpeg_comment_from_bytes(&bytes) +} + +/// Byte-oriented twin of [`read_jpeg_comment`] (no filesystem; WASM-safe). +pub fn read_jpeg_comment_from_bytes(bytes: &[u8]) -> Option<Vec<u8>> { if bytes.len() < 2 || bytes[0] != 0xFF || bytes[1] != 0xD8 { return None; } @@ -406,6 +428,13 @@ pub fn read_jpeg_comment(path: &str) -> Option<Vec<u8>> { /// fresh COM segment right after SOI. pub fn write_jpeg_comment(in_img: &str, out_img: &str, comment: &[u8]) -> std::io::Result<()> { let bytes = std::fs::read(in_img)?; + let out = write_jpeg_comment_to_bytes(&bytes, comment)?; + std::fs::write(out_img, out) +} + +/// Byte-oriented twin of [`write_jpeg_comment`] (no filesystem; WASM-safe): +/// drop all COM segments and insert a fresh COM segment after the last APPn. +pub fn write_jpeg_comment_to_bytes(bytes: &[u8], comment: &[u8]) -> std::io::Result<Vec<u8>> { if bytes.len() < 2 || bytes[0] != 0xFF || bytes[1] != 0xD8 { return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "not a JPEG file")); } @@ -469,9 +498,7 @@ pub fn write_jpeg_comment(in_img: &str, out_img: &str, comment: &[u8]) -> std::i out.extend_from_slice(&bytes[copy_rest_from..]); - let mut f = File::create(out_img)?; - f.write_all(&out)?; - f.flush() + Ok(out) } /// Read raw image dimensions (width, height) of a JPEG. diff --git a/crates/gipfel-core/src/panorama.rs b/crates/gipfel-core/src/panorama.rs index 9e27251..9216037 100644 --- a/crates/gipfel-core/src/panorama.rs +++ b/crates/gipfel-core/src/panorama.rs @@ -74,6 +74,16 @@ impl Panorama { Ok(()) } + /// Like [`Panorama::load_data`] but from raw bytes (no filesystem); + /// used by the WASM build. + pub fn load_data_from_bytes(&mut self, raw: &[u8]) -> std::io::Result<()> { + let mut hills = crate::hill::load_from_bytes(raw)?; + crate::hill::mark_duplicates(&mut hills, 0.00001); + self.hills = hills; + self.update_angles(); + Ok(()) + } + pub fn add_hills(&mut self, extra: &[Hill]) { self.hills.extend_from_slice(extra); crate::hill::mark_duplicates(&mut self.hills, 0.00001); |
