lerc-rs 0.1.1

Pure Rust LERC (Limited Error Raster Compression) encoder and decoder
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
//! Pure Rust implementation of the LERC (Limited Error Raster Compression) format.
//!
//! Supports encoding and decoding of raster images with configurable lossy or lossless
//! compression. Compatible with ESRI's LERC2 format specification.

#![cfg_attr(not(feature = "std"), no_std)]
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    clippy::cast_lossless
)]

extern crate alloc;

/// Error types for LERC encoding and decoding.
pub mod error;
/// Pixel data types and the `Sample` trait for type-safe encoding/decoding.
pub mod types;

/// Validity bitmask for tracking valid/invalid pixels.
pub mod bitmask;
pub(crate) mod bitstuffer;
/// Fletcher-32 checksum used by the LERC2 format.
#[allow(dead_code)]
pub mod checksum;
#[allow(dead_code)]
pub(crate) mod header;
#[allow(dead_code)]
pub(crate) mod huffman;
pub(crate) mod rle;

pub(crate) mod decode;
pub(crate) mod encode;
#[allow(dead_code)]
pub(crate) mod fpl;
pub(crate) mod lerc1;
#[allow(dead_code)]
pub(crate) mod tiles;

pub use error::{LercError, Result};
pub use types::{DataType, Sample};

use alloc::vec;
use alloc::vec::Vec;

use bitmask::BitMask;

/// Controls the precision/error tolerance for LERC encoding.
///
/// `Lossless` preserves exact values. `Tolerance(x)` allows decoded values
/// to differ from originals by at most +/-x.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Precision<T> {
    /// Lossless compression. Exact round-trip for all pixel values.
    #[default]
    Lossless,
    /// Lossy compression. Decoded values are within the given tolerance of originals.
    Tolerance(T),
}

/// Metadata returned from a decode-into operation (no owned pixel data).
#[derive(Debug, Clone)]
pub struct DecodeResult {
    /// Image width in pixels.
    pub width: u32,
    /// Image height in pixels.
    pub height: u32,
    /// Number of values per pixel (depth slices).
    pub depth: u32,
    /// Number of bands in the image.
    pub bands: u32,
    /// Pixel data type of the decoded blob.
    pub data_type: DataType,
    /// Per-band validity masks indicating which pixels are valid.
    pub valid_masks: Vec<BitMask>,
    /// NoData sentinel value, if the blob uses NoData encoding.
    pub no_data_value: Option<f64>,
}

/// Header metadata extracted from a LERC blob without decoding pixel data.
#[derive(Debug, Clone, Default)]
pub struct LercInfo {
    /// LERC format version number.
    pub version: i32,
    /// Image width in pixels.
    pub width: u32,
    /// Image height in pixels.
    pub height: u32,
    /// Number of values per pixel (depth slices).
    pub depth: u32,
    /// Number of bands in the image.
    pub bands: u32,
    /// Pixel data type stored in the blob.
    pub data_type: DataType,
    /// Number of valid (non-masked) pixels.
    pub valid_pixels: u32,
    /// Maximum error tolerance used during encoding.
    pub tolerance: f64,
    /// Minimum pixel value across all valid pixels.
    pub min_value: f64,
    /// Maximum pixel value across all valid pixels.
    pub max_value: f64,
    /// Total size of the LERC blob in bytes.
    pub blob_size: u32,
    /// The original NoData value, if the blob uses NoData encoding (v6+, depth > 1).
    pub no_data_value: Option<f64>,
}

/// A decoded raster image with pixel data and validity masks.
#[derive(Debug, Clone)]
pub struct Image {
    /// Image width in pixels.
    pub width: u32,
    /// Image height in pixels.
    pub height: u32,
    /// Number of values per pixel (depth slices).
    pub depth: u32,
    /// Number of bands in the image.
    pub bands: u32,
    /// Pixel data type.
    pub data_type: DataType,
    /// Per-band validity masks indicating which pixels are valid.
    pub valid_masks: Vec<BitMask>,
    /// Pixel sample data stored as a typed vector.
    pub data: SampleData,
    /// The original NoData value, if any. When set during encoding with depth > 1,
    /// pixels matching this value in invalid depth slices are encoded with a sentinel.
    /// On decode, the sentinel is remapped back to this value.
    pub no_data_value: Option<f64>,
}

impl Default for Image {
    fn default() -> Self {
        Self {
            width: 0,
            height: 0,
            depth: 1,
            bands: 1,
            data_type: DataType::Byte,
            valid_masks: Vec::new(),
            data: SampleData::U8(Vec::new()),
            no_data_value: None,
        }
    }
}

/// Type-erased pixel data container, one variant per supported data type.
#[derive(Debug, Clone)]
pub enum SampleData {
    /// Signed 8-bit integer pixel data.
    I8(Vec<i8>),
    /// Unsigned 8-bit integer pixel data.
    U8(Vec<u8>),
    /// Signed 16-bit integer pixel data.
    I16(Vec<i16>),
    /// Unsigned 16-bit integer pixel data.
    U16(Vec<u16>),
    /// Signed 32-bit integer pixel data.
    I32(Vec<i32>),
    /// Unsigned 32-bit integer pixel data.
    U32(Vec<u32>),
    /// 32-bit floating-point pixel data.
    F32(Vec<f32>),
    /// 64-bit floating-point pixel data.
    F64(Vec<f64>),
}

/// Read header metadata from a LERC blob without decoding pixel data.
pub fn decode_info(data: &[u8]) -> Result<LercInfo> {
    decode::decode_info(data)
}

/// Decode a LERC blob, returning the image with pixel data and validity masks.
pub fn decode(data: &[u8]) -> Result<Image> {
    decode::decode(data)
}

/// Encode an image into a LERC blob with the given precision.
pub fn encode(image: &Image, precision: Precision<f64>) -> Result<Vec<u8>> {
    let max_z_error = match precision {
        Precision::Lossless => {
            if image.data_type.is_integer() {
                0.5
            } else {
                0.0
            }
        }
        Precision::Tolerance(val) => val,
    };
    encode::encode(image, max_z_error)
}

// ---------------------------------------------------------------------------
// Typed convenience encode/decode helpers
// ---------------------------------------------------------------------------

/// Encode a single-band image with all pixels valid.
///
/// The pixel type `T` determines the LERC data type automatically via `Sample`.
/// Returns an error if `data.len() != width * height`.
pub fn encode_slice<T: Sample>(
    width: u32,
    height: u32,
    data: &[T],
    precision: Precision<T>,
) -> Result<Vec<u8>> {
    let expected = (width as usize) * (height as usize);
    if data.len() != expected {
        return Err(LercError::InvalidData(alloc::format!(
            "data length {} does not match width*height {expected}",
            data.len()
        )));
    }
    let max_z_error: f64 = match precision {
        Precision::Lossless => {
            if T::is_integer() {
                0.5
            } else {
                0.0
            }
        }
        Precision::Tolerance(val) => val.to_f64(),
    };
    let image = Image {
        width,
        height,
        depth: 1,
        bands: 1,
        data_type: T::DATA_TYPE,
        valid_masks: vec![BitMask::all_valid(expected)],
        data: T::into_lerc_data(data.to_vec()),
        no_data_value: None,
    };
    encode::encode(&image, max_z_error)
}

/// Encode a single-band image with a validity mask.
///
/// The pixel type `T` determines the LERC data type automatically via `Sample`.
/// Returns an error if `data.len() != width * height` or if the mask size does not match.
pub fn encode_slice_masked<T: Sample>(
    width: u32,
    height: u32,
    data: &[T],
    mask: &BitMask,
    precision: Precision<T>,
) -> Result<Vec<u8>> {
    let expected = (width as usize) * (height as usize);
    if data.len() != expected {
        return Err(LercError::InvalidData(alloc::format!(
            "data length {} does not match width*height {expected}",
            data.len()
        )));
    }
    if mask.num_pixels() != expected {
        return Err(LercError::InvalidData(alloc::format!(
            "mask pixel count {} does not match width*height {expected}",
            mask.num_pixels()
        )));
    }
    let max_z_error: f64 = match precision {
        Precision::Lossless => {
            if T::is_integer() {
                0.5
            } else {
                0.0
            }
        }
        Precision::Tolerance(val) => val.to_f64(),
    };
    let image = Image {
        width,
        height,
        depth: 1,
        bands: 1,
        data_type: T::DATA_TYPE,
        valid_masks: vec![mask.clone()],
        data: T::into_lerc_data(data.to_vec()),
        no_data_value: None,
    };
    encode::encode(&image, max_z_error)
}

/// Decode a single-band, single-depth LERC blob, returning typed pixel data,
/// the validity mask, width, and height.
///
/// The pixel type `T` must match the blob's data type. Returns an error on type
/// mismatch or if the blob contains multiple bands or depths (use [`decode`] for
/// multi-band/multi-depth blobs to get full shape and per-band masks).
pub fn decode_slice<T: Sample>(blob: &[u8]) -> Result<(Vec<T>, BitMask, u32, u32)> {
    let image = decode::decode(blob)?;
    if image.bands > 1 {
        return Err(LercError::InvalidData(alloc::format!(
            "decode_slice requires single-band data, got {} bands (use decode() instead)",
            image.bands
        )));
    }
    if image.depth > 1 {
        return Err(LercError::InvalidData(alloc::format!(
            "decode_slice requires single-depth data, got depth={} (use decode() instead)",
            image.depth
        )));
    }
    let w = image.width;
    let h = image.height;
    let pixels = T::try_from_lerc_data(image.data).map_err(|_| {
        LercError::InvalidData(alloc::format!(
            "expected {:?} data but blob contains {:?}",
            T::DATA_TYPE,
            image.data_type
        ))
    })?;
    let mask = image
        .valid_masks
        .into_iter()
        .next()
        .unwrap_or_else(|| BitMask::all_valid((w as usize) * (h as usize)));
    Ok((pixels, mask, w, h))
}

// ---------------------------------------------------------------------------
// Typed accessor methods on Image
// ---------------------------------------------------------------------------

impl Image {
    /// Try to borrow the pixel data as `&[T]`.
    ///
    /// Returns `None` if the image's data type does not match `T`.
    pub fn as_typed<T: Sample>(&self) -> Option<&[T]> {
        T::try_ref_lerc_data(&self.data)
    }

    /// Return the validity mask for the first band, or `None` if no masks are present.
    pub fn mask(&self) -> Option<&BitMask> {
        self.valid_masks.first()
    }

    /// Get the pixel value at `(row, col)` for single-band, single-depth images.
    ///
    /// Returns `None` if the data type does not match `T`, if `bands > 1` or
    /// `depth > 1`, or if the coordinates are out of bounds.
    pub fn pixel<T: Sample>(&self, row: u32, col: u32) -> Option<T> {
        if self.bands != 1 || self.depth != 1 {
            return None;
        }
        if row >= self.height || col >= self.width {
            return None;
        }
        let data = self.as_typed::<T>()?;
        let idx = row as usize * self.width as usize + col as usize;
        Some(data[idx])
    }

    /// Iterate over valid pixels as `(row, col, value)` tuples.
    ///
    /// Only works for single-band, single-depth images. Returns `None` if the data
    /// type does not match `T` or if `bands > 1` or `depth > 1`.
    /// The iterator respects the validity mask, skipping invalid pixels.
    pub fn valid_pixels<'a, T: Sample + 'a>(
        &'a self,
    ) -> Option<impl Iterator<Item = (u32, u32, T)> + 'a> {
        if self.bands != 1 || self.depth != 1 {
            return None;
        }
        let data = self.as_typed::<T>()?;
        let width = self.width;
        let mask = self.valid_masks.first();
        Some(data.iter().enumerate().filter_map(move |(idx, &val)| {
            let is_valid = match mask {
                Some(m) => m.is_valid(idx),
                None => true,
            };
            if is_valid {
                let row = (idx / width as usize) as u32;
                let col = (idx % width as usize) as u32;
                Some((row, col, val))
            } else {
                None
            }
        }))
    }

    /// Get dimensions as `(width, height)`.
    pub fn dimensions(&self) -> (u32, u32) {
        (self.width, self.height)
    }

    /// Total number of pixels (`width * height`).
    pub fn num_pixels(&self) -> usize {
        self.width as usize * self.height as usize
    }

    /// Check if all pixels are valid (in the first band's mask).
    ///
    /// Returns `true` if there is no mask (all pixels are implicitly valid)
    /// or if every pixel in the mask is marked valid.
    pub fn all_valid(&self) -> bool {
        match self.valid_masks.first() {
            Some(m) => m.count_valid() == m.num_pixels(),
            None => true,
        }
    }

    /// Create a single-band, all-valid `Image` from a typed pixel vector
    /// and dimensions.
    ///
    /// Returns an error if `data.len() != width * height`.
    pub fn from_pixels<T: Sample>(width: u32, height: u32, data: Vec<T>) -> Result<Self> {
        let expected = width as usize * height as usize;
        if data.len() != expected {
            return Err(LercError::InvalidData(alloc::format!(
                "data length {} does not match width*height {expected}",
                data.len()
            )));
        }
        Ok(Self {
            width,
            height,
            depth: 1,
            bands: 1,
            data_type: T::DATA_TYPE,
            valid_masks: vec![BitMask::all_valid(expected)],
            data: T::into_lerc_data(data),
            no_data_value: None,
        })
    }
}

// ---------------------------------------------------------------------------
// Zero-copy decode-into API
// ---------------------------------------------------------------------------

/// Decode a LERC blob into a pre-allocated buffer, returning metadata.
///
/// The type `T` must match the blob's data type (e.g., `f32` for `DataType::Float`).
/// The buffer must have at least `width * height * n_depth * n_bands` elements.
///
/// Returns `LercError::TypeMismatch` if `T` does not match the blob's data type.
/// Returns `LercError::OutputBufferTooSmall` if the buffer is too small.
pub fn decode_into<T: Sample>(data: &[u8], output: &mut [T]) -> Result<DecodeResult> {
    decode::decode_into(data, output)
}