visloc-io 0.5.0

COLMAP, descriptor, and query-feature IO helpers for visloc-rs visual maps.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use std::fs;
use std::path::Path;
#[cfg(feature = "image-io")]
use std::path::PathBuf;
use thiserror::Error;
#[cfg(feature = "image-io")]
use visloc_core::types::FrameId;
use visloc_vision::features::{GrayscaleImage, GrayscaleImageError};

#[derive(Debug, Error)]
pub enum PgmImageError {
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),
    #[error("invalid PGM header: {0}")]
    InvalidHeader(String),
    #[error("unsupported PGM magic {0}; expected P2 or P5")]
    UnsupportedMagic(String),
    #[error("invalid PGM integer: {0}")]
    InvalidInteger(String),
    #[error("unsupported PGM max value {0}; expected 1..=255")]
    UnsupportedMaxValue(u32),
    #[error("PGM pixel count mismatch: expected {expected_len}, got {actual_len}")]
    PixelCountMismatch {
        expected_len: usize,
        actual_len: usize,
    },
    #[error("grayscale image error: {0}")]
    Grayscale(#[from] GrayscaleImageError),
}

#[cfg(feature = "image-io")]
#[derive(Debug, Error)]
pub enum CommonImageError {
    #[error("image decode/encode error: {0}")]
    Image(#[from] image::ImageError),
    #[error("grayscale image error: {0}")]
    Grayscale(#[from] GrayscaleImageError),
    #[error("image dimensions are too large for common image encoders: {width}x{height}")]
    DimensionTooLarge { width: usize, height: usize },
    #[error("failed to build grayscale image buffer")]
    InvalidImageBuffer,
}

#[cfg(feature = "image-io")]
#[derive(Debug, Error)]
pub enum ImageSequenceError {
    #[error("image sequence I/O error: {0}")]
    Io(#[from] std::io::Error),
    #[error("image frame {path} failed to load: {source}")]
    FrameLoad {
        path: PathBuf,
        source: CommonImageError,
    },
    #[error("timestamp count mismatch: {path_count} image paths, {timestamp_count} timestamps")]
    TimestampCountMismatch {
        path_count: usize,
        timestamp_count: usize,
    },
    #[error("invalid timestamp line {line_number}: {line} ({message})")]
    InvalidTimestampLine {
        line_number: usize,
        line: String,
        message: String,
    },
}

#[cfg(feature = "image-io")]
#[derive(Debug, Clone, PartialEq)]
pub struct LoadedImageFrame {
    pub frame_id: FrameId,
    pub timestamp_nanoseconds: Option<i128>,
    pub path: PathBuf,
    pub image: GrayscaleImage,
}

#[cfg(feature = "image-io")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImageSequenceSummary {
    pub frame_count: usize,
    pub first_frame_id: Option<FrameId>,
    pub last_frame_id: Option<FrameId>,
    pub width: Option<usize>,
    pub height: Option<usize>,
    pub varying_dimensions: bool,
    pub timestamp_count: usize,
    pub first_timestamp_nanoseconds: Option<i128>,
    pub last_timestamp_nanoseconds: Option<i128>,
    pub timestamps_valid: bool,
}

#[cfg(feature = "image-io")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ImageSequenceValidationIssue {
    InconsistentDimensions {
        frame_id: FrameId,
        path: PathBuf,
        width: usize,
        height: usize,
        expected_width: usize,
        expected_height: usize,
    },
    MissingTimestamp {
        frame_id: FrameId,
        path: PathBuf,
    },
    NonMonotonicTimestamp {
        frame_id: FrameId,
        path: PathBuf,
        timestamp_nanoseconds: i128,
        previous_frame_id: FrameId,
        previous_timestamp_nanoseconds: i128,
    },
}

pub fn read_pgm(path: impl AsRef<Path>) -> Result<GrayscaleImage, PgmImageError> {
    parse_pgm(&fs::read(path)?)
}

pub fn write_pgm_ascii(
    path: impl AsRef<Path>,
    image: &GrayscaleImage,
) -> Result<(), PgmImageError> {
    fs::write(path, to_pgm_ascii(image)?)?;
    Ok(())
}

pub fn parse_pgm(bytes: &[u8]) -> Result<GrayscaleImage, PgmImageError> {
    let (magic, offset) = next_token(bytes, 0)
        .ok_or_else(|| PgmImageError::InvalidHeader("missing magic".to_owned()))?;
    let (width, offset) = parse_usize_token(bytes, offset, "width")?;
    let (height, offset) = parse_usize_token(bytes, offset, "height")?;
    let (max_value, offset) = parse_u32_token(bytes, offset, "max value")?;
    if max_value == 0 || max_value > 255 {
        return Err(PgmImageError::UnsupportedMaxValue(max_value));
    }

    let expected_len = width * height;
    match magic.as_str() {
        "P2" => parse_pgm_ascii_pixels(bytes, offset, width, height, expected_len, max_value),
        "P5" => parse_pgm_binary_pixels(bytes, offset, width, height, expected_len, max_value),
        other => Err(PgmImageError::UnsupportedMagic(other.to_owned())),
    }
}

pub fn to_pgm_ascii(image: &GrayscaleImage) -> Result<String, PgmImageError> {
    let mut output = format!("P2\n{} {}\n255\n", image.width(), image.height());
    for (index, pixel) in image.pixels().iter().enumerate() {
        let value = (pixel.clamp(0.0, 1.0) * 255.0).round() as u8;
        output.push_str(&value.to_string());
        if (index + 1) % image.width() == 0 {
            output.push('\n');
        } else {
            output.push(' ');
        }
    }
    Ok(output)
}

#[cfg(feature = "image-io")]
pub fn read_common_image(path: impl AsRef<Path>) -> Result<GrayscaleImage, CommonImageError> {
    dynamic_image_to_grayscale(image::open(path)?)
}

/// Read an image using the 8-bit RGB-to-gray conversion used by COLMAP's
/// `Bitmap::CloneAsGrey`.
///
/// The ordinary [`read_common_image`] path intentionally retains the
/// `image` crate's historical integer/floor conversion.  COLMAP first reads
/// RGB bytes and evaluates `.2126f * R + .7152f * G + .0722f * B + .5f`,
/// truncating the result to `uint8_t`.  Keeping this as a separate opt-in
/// entry point makes the preprocessing contract explicit without changing
/// existing callers.
#[cfg(feature = "image-io")]
pub fn read_common_image_colmap_grayscale(
    path: impl AsRef<Path>,
) -> Result<GrayscaleImage, CommonImageError> {
    dynamic_image_to_colmap_grayscale(image::open(path)?)
}

#[cfg(feature = "image-io")]
pub fn decode_common_image(bytes: &[u8]) -> Result<GrayscaleImage, CommonImageError> {
    dynamic_image_to_grayscale(image::load_from_memory(bytes)?)
}

/// Decode bytes using [`read_common_image_colmap_grayscale`]'s conversion.
#[cfg(feature = "image-io")]
pub fn decode_common_image_colmap_grayscale(
    bytes: &[u8],
) -> Result<GrayscaleImage, CommonImageError> {
    dynamic_image_to_colmap_grayscale(image::load_from_memory(bytes)?)
}

#[cfg(feature = "image-io")]
pub fn write_png_gray(
    path: impl AsRef<Path>,
    grayscale: &GrayscaleImage,
) -> Result<(), CommonImageError> {
    let width =
        u32::try_from(grayscale.width()).map_err(|_| CommonImageError::DimensionTooLarge {
            width: grayscale.width(),
            height: grayscale.height(),
        })?;
    let height =
        u32::try_from(grayscale.height()).map_err(|_| CommonImageError::DimensionTooLarge {
            width: grayscale.width(),
            height: grayscale.height(),
        })?;
    let pixels = grayscale
        .pixels()
        .iter()
        .map(|pixel| (pixel.clamp(0.0, 1.0) * 255.0).round() as u8)
        .collect();
    let buffer = image::GrayImage::from_raw(width, height, pixels)
        .ok_or(CommonImageError::InvalidImageBuffer)?;
    buffer.save_with_format(path, image::ImageFormat::Png)?;
    Ok(())
}

#[cfg(feature = "image-io")]
pub fn read_common_image_sequence<P>(
    paths: &[P],
) -> Result<Vec<LoadedImageFrame>, ImageSequenceError>
where
    P: AsRef<Path>,
{
    read_common_image_sequence_with_optional_timestamps(paths, None)
}

#[cfg(feature = "image-io")]
pub fn read_common_image_sequence_with_timestamps<P>(
    paths: &[P],
    timestamps_nanoseconds: &[i128],
) -> Result<Vec<LoadedImageFrame>, ImageSequenceError>
where
    P: AsRef<Path>,
{
    if paths.len() != timestamps_nanoseconds.len() {
        return Err(ImageSequenceError::TimestampCountMismatch {
            path_count: paths.len(),
            timestamp_count: timestamps_nanoseconds.len(),
        });
    }
    read_common_image_sequence_with_optional_timestamps(paths, Some(timestamps_nanoseconds))
}

#[cfg(feature = "image-io")]
pub fn read_timestamp_nanoseconds_txt(
    path: impl AsRef<Path>,
) -> Result<Vec<i128>, ImageSequenceError> {
    parse_timestamp_nanoseconds_txt(&fs::read_to_string(path)?)
}

#[cfg(feature = "image-io")]
pub fn parse_timestamp_nanoseconds_txt(text: &str) -> Result<Vec<i128>, ImageSequenceError> {
    let mut timestamps = Vec::new();
    for (line_index, line) in text.lines().enumerate() {
        let line_number = line_index + 1;
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }
        let token = trimmed.split_whitespace().next().ok_or_else(|| {
            ImageSequenceError::InvalidTimestampLine {
                line_number,
                line: line.to_owned(),
                message: "missing timestamp".to_owned(),
            }
        })?;
        let timestamp =
            token
                .parse::<i128>()
                .map_err(|error| ImageSequenceError::InvalidTimestampLine {
                    line_number,
                    line: line.to_owned(),
                    message: error.to_string(),
                })?;
        timestamps.push(timestamp);
    }
    Ok(timestamps)
}

#[cfg(feature = "image-io")]
fn read_common_image_sequence_with_optional_timestamps<P>(
    paths: &[P],
    timestamps_nanoseconds: Option<&[i128]>,
) -> Result<Vec<LoadedImageFrame>, ImageSequenceError>
where
    P: AsRef<Path>,
{
    let mut frames = Vec::with_capacity(paths.len());
    for (index, path) in paths.iter().enumerate() {
        let path = path.as_ref();
        let image = read_common_image(path).map_err(|source| ImageSequenceError::FrameLoad {
            path: path.to_path_buf(),
            source,
        })?;
        frames.push(LoadedImageFrame {
            frame_id: index as FrameId,
            timestamp_nanoseconds: timestamps_nanoseconds.map(|timestamps| timestamps[index]),
            path: path.to_path_buf(),
            image,
        });
    }
    Ok(frames)
}

#[cfg(feature = "image-io")]
pub fn read_common_image_sequence_dir(
    dir: impl AsRef<Path>,
) -> Result<Vec<LoadedImageFrame>, ImageSequenceError> {
    let paths = common_image_sequence_paths_dir(dir)?;
    read_common_image_sequence(&paths)
}

#[cfg(feature = "image-io")]
pub fn read_common_image_sequence_dir_with_timestamps(
    dir: impl AsRef<Path>,
    timestamps_nanoseconds: &[i128],
) -> Result<Vec<LoadedImageFrame>, ImageSequenceError> {
    let paths = common_image_sequence_paths_dir(dir)?;
    read_common_image_sequence_with_timestamps(&paths, timestamps_nanoseconds)
}

#[cfg(feature = "image-io")]
pub fn read_common_image_sequence_dir_with_timestamp_file(
    dir: impl AsRef<Path>,
    timestamp_path: impl AsRef<Path>,
) -> Result<Vec<LoadedImageFrame>, ImageSequenceError> {
    let timestamps = read_timestamp_nanoseconds_txt(timestamp_path)?;
    read_common_image_sequence_dir_with_timestamps(dir, &timestamps)
}

#[cfg(feature = "image-io")]
fn common_image_sequence_paths_dir(
    dir: impl AsRef<Path>,
) -> Result<Vec<PathBuf>, ImageSequenceError> {
    let mut paths = Vec::new();
    for entry in fs::read_dir(dir)? {
        let path = entry?.path();
        if is_supported_common_image_path(&path) {
            paths.push(path);
        }
    }
    paths.sort();
    Ok(paths)
}

#[cfg(feature = "image-io")]
pub fn common_image_sequence_summary(frames: &[LoadedImageFrame]) -> ImageSequenceSummary {
    let first = frames.first();
    let timestamp_count = frames
        .iter()
        .filter(|frame| frame.timestamp_nanoseconds.is_some())
        .count();
    ImageSequenceSummary {
        frame_count: frames.len(),
        first_frame_id: first.map(|frame| frame.frame_id),
        last_frame_id: frames.last().map(|frame| frame.frame_id),
        width: first.map(|frame| frame.image.width()),
        height: first.map(|frame| frame.image.height()),
        varying_dimensions: !validate_common_image_sequence_dimensions(frames).is_empty(),
        timestamp_count,
        first_timestamp_nanoseconds: frames.iter().find_map(|frame| frame.timestamp_nanoseconds),
        last_timestamp_nanoseconds: frames
            .iter()
            .rev()
            .find_map(|frame| frame.timestamp_nanoseconds),
        timestamps_valid: validate_common_image_sequence_timestamps(frames).is_empty(),
    }
}

#[cfg(feature = "image-io")]
pub fn validate_common_image_sequence_dimensions(
    frames: &[LoadedImageFrame],
) -> Vec<ImageSequenceValidationIssue> {
    let Some(first) = frames.first() else {
        return Vec::new();
    };
    let expected_width = first.image.width();
    let expected_height = first.image.height();

    frames
        .iter()
        .skip(1)
        .filter_map(|frame| {
            let width = frame.image.width();
            let height = frame.image.height();
            (width != expected_width || height != expected_height).then(|| {
                ImageSequenceValidationIssue::InconsistentDimensions {
                    frame_id: frame.frame_id,
                    path: frame.path.clone(),
                    width,
                    height,
                    expected_width,
                    expected_height,
                }
            })
        })
        .collect()
}

#[cfg(feature = "image-io")]
pub fn validate_common_image_sequence_timestamps(
    frames: &[LoadedImageFrame],
) -> Vec<ImageSequenceValidationIssue> {
    if frames
        .iter()
        .all(|frame| frame.timestamp_nanoseconds.is_none())
    {
        return Vec::new();
    }

    let mut issues = Vec::new();
    let mut previous: Option<(FrameId, i128)> = None;
    for frame in frames {
        let Some(timestamp_nanoseconds) = frame.timestamp_nanoseconds else {
            issues.push(ImageSequenceValidationIssue::MissingTimestamp {
                frame_id: frame.frame_id,
                path: frame.path.clone(),
            });
            continue;
        };

        if let Some((previous_frame_id, previous_timestamp_nanoseconds)) = previous {
            if timestamp_nanoseconds < previous_timestamp_nanoseconds {
                issues.push(ImageSequenceValidationIssue::NonMonotonicTimestamp {
                    frame_id: frame.frame_id,
                    path: frame.path.clone(),
                    timestamp_nanoseconds,
                    previous_frame_id,
                    previous_timestamp_nanoseconds,
                });
            }
        }
        previous = Some((frame.frame_id, timestamp_nanoseconds));
    }

    issues
}

#[cfg(feature = "image-io")]
fn is_supported_common_image_path(path: &Path) -> bool {
    let Some(extension) = path.extension().and_then(|extension| extension.to_str()) else {
        return false;
    };
    matches!(
        extension.to_ascii_lowercase().as_str(),
        "jpg" | "jpeg" | "png"
    )
}

#[cfg(feature = "image-io")]
fn dynamic_image_to_grayscale(
    dynamic: image::DynamicImage,
) -> Result<GrayscaleImage, CommonImageError> {
    let luma = dynamic.to_luma8();
    let (width, height) = luma.dimensions();
    GrayscaleImage::from_luma_u8(width as usize, height as usize, luma.into_raw())
        .map_err(CommonImageError::from)
}

#[cfg(feature = "image-io")]
fn dynamic_image_to_colmap_grayscale(
    dynamic: image::DynamicImage,
) -> Result<GrayscaleImage, CommonImageError> {
    let rgb = dynamic.to_rgb8();
    let (width, height) = rgb.dimensions();
    let pixels = rgb
        .as_raw()
        .chunks_exact(3)
        .map(|channels| {
            // Keep the operation order and f32 constants aligned with
            // COLMAP's Bitmap::CloneAsGrey implementation.  `as u8` is a
            // truncating conversion, matching static_cast<uint8_t> for this
            // non-negative luminance value.
            let luminance = 0.2126f32 * f32::from(channels[0])
                + 0.7152f32 * f32::from(channels[1])
                + 0.0722f32 * f32::from(channels[2])
                + 0.5f32;
            luminance as u8
        })
        .collect();
    GrayscaleImage::from_luma_u8(width as usize, height as usize, pixels)
        .map_err(CommonImageError::from)
}

fn parse_pgm_ascii_pixels(
    bytes: &[u8],
    mut offset: usize,
    width: usize,
    height: usize,
    expected_len: usize,
    max_value: u32,
) -> Result<GrayscaleImage, PgmImageError> {
    let mut pixels = Vec::with_capacity(expected_len);
    while let Some((token, next_offset)) = next_token(bytes, offset) {
        let value = parse_u32(&token)?;
        if value > max_value {
            return Err(PgmImageError::InvalidInteger(format!(
                "pixel value {value} exceeds max value {max_value}"
            )));
        }
        pixels.push(value as f32 / max_value as f32);
        offset = next_offset;
    }

    if pixels.len() != expected_len {
        return Err(PgmImageError::PixelCountMismatch {
            expected_len,
            actual_len: pixels.len(),
        });
    }
    GrayscaleImage::new(width, height, pixels).map_err(PgmImageError::from)
}

fn parse_pgm_binary_pixels(
    bytes: &[u8],
    offset: usize,
    width: usize,
    height: usize,
    expected_len: usize,
    max_value: u32,
) -> Result<GrayscaleImage, PgmImageError> {
    let pixel_offset = skip_single_whitespace(bytes, offset);
    let data = bytes.get(pixel_offset..).unwrap_or_default();
    if data.len() != expected_len {
        return Err(PgmImageError::PixelCountMismatch {
            expected_len,
            actual_len: data.len(),
        });
    }

    let pixels = data
        .iter()
        .map(|value| *value as f32 / max_value as f32)
        .collect();
    GrayscaleImage::new(width, height, pixels).map_err(PgmImageError::from)
}

fn parse_usize_token(
    bytes: &[u8],
    offset: usize,
    label: &str,
) -> Result<(usize, usize), PgmImageError> {
    let (token, offset) = next_token(bytes, offset)
        .ok_or_else(|| PgmImageError::InvalidHeader(format!("missing {label}")))?;
    Ok((parse_usize(&token)?, offset))
}

fn parse_u32_token(
    bytes: &[u8],
    offset: usize,
    label: &str,
) -> Result<(u32, usize), PgmImageError> {
    let (token, offset) = next_token(bytes, offset)
        .ok_or_else(|| PgmImageError::InvalidHeader(format!("missing {label}")))?;
    Ok((parse_u32(&token)?, offset))
}

fn parse_usize(token: &str) -> Result<usize, PgmImageError> {
    token
        .parse::<usize>()
        .map_err(|error| PgmImageError::InvalidInteger(error.to_string()))
}

fn parse_u32(token: &str) -> Result<u32, PgmImageError> {
    token
        .parse::<u32>()
        .map_err(|error| PgmImageError::InvalidInteger(error.to_string()))
}

fn next_token(bytes: &[u8], mut offset: usize) -> Option<(String, usize)> {
    loop {
        while offset < bytes.len() && bytes[offset].is_ascii_whitespace() {
            offset += 1;
        }
        if offset >= bytes.len() {
            return None;
        }
        if bytes[offset] != b'#' {
            break;
        }
        while offset < bytes.len() && bytes[offset] != b'\n' {
            offset += 1;
        }
    }

    let start = offset;
    while offset < bytes.len() && !bytes[offset].is_ascii_whitespace() {
        offset += 1;
    }
    Some((
        String::from_utf8_lossy(&bytes[start..offset]).to_string(),
        offset,
    ))
}

fn skip_single_whitespace(bytes: &[u8], offset: usize) -> usize {
    if offset < bytes.len() && bytes[offset].is_ascii_whitespace() {
        offset + 1
    } else {
        offset
    }
}