lerc-reader 0.5.0

Pure-Rust decoder for the LERC raster compression format
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
use std::any::TypeId;

use crate::materialize::{copy_band_values_into_slice, BandMaterializer};
use lerc_core::{BandLayout, BandSetInfo, BlobInfo, Error, PixelData, Result};
#[cfg(feature = "ndarray")]
use ndarray::{ArrayD, IxDyn};

#[cfg(feature = "ndarray")]
use crate::allocation::{checked_mul, default_vec, vec_with_capacity};

/// Native-typed pixels, validated metadata, and an optional validity mask for one blob.
#[derive(Debug, Clone, PartialEq)]
pub struct Decoded {
    /// Validated blob metadata.
    pub info: BlobInfo,
    /// Pixel-interleaved samples in the blob's native numeric type.
    pub pixels: PixelData,
    /// Row-major validity bytes, when the blob has a mask.
    pub mask: Option<Vec<u8>>,
}

impl Decoded {
    #[cfg(feature = "ndarray")]
    /// Borrows this result and copies its pixels into an ndarray.
    ///
    /// # Errors
    /// Returns an error if `T` differs from the native type or the shape is invalid.
    pub fn to_ndarray<T: NdArrayElement>(&self) -> Result<ArrayD<T>> {
        self.pixels.clone().into_ndarray(&self.info.ndarray_shape())
    }

    #[cfg(feature = "ndarray")]
    /// Consumes this result and moves its pixels into an ndarray.
    ///
    /// # Errors
    /// Returns an error if `T` differs from the native type or the shape is invalid.
    pub fn into_ndarray<T: NdArrayElement>(self) -> Result<ArrayD<T>> {
        let shape = self.info.ndarray_shape();
        self.pixels.into_ndarray(&shape)
    }

    #[cfg(feature = "ndarray")]
    /// Consumes this result and moves its optional mask into an ndarray.
    ///
    /// # Errors
    /// Returns an error if decoded metadata and mask length disagree.
    pub fn into_mask_ndarray(self) -> Result<Option<ArrayD<u8>>> {
        let shape = self.info.mask_ndarray_shape();
        self.mask
            .map(|mask| {
                ArrayD::from_shape_vec(IxDyn(&shape), mask).map_err(|err| {
                    Error::invalid_blob(format!("failed to build ndarray from decoded mask: {err}"))
                })
            })
            .transpose()
    }

    #[cfg(feature = "ndarray")]
    /// Copies the optional validity mask into an ndarray.
    ///
    /// # Errors
    /// Returns an error if decoded metadata and mask length disagree.
    pub fn mask_ndarray(&self) -> Result<Option<ArrayD<u8>>> {
        let shape = self.info.mask_ndarray_shape();
        self.mask
            .as_ref()
            .map(|mask| {
                ArrayD::from_shape_vec(IxDyn(&shape), mask.clone()).map_err(|err| {
                    Error::invalid_blob(format!("failed to build ndarray from decoded mask: {err}"))
                })
            })
            .transpose()
    }
}

/// Promoted `f64` pixels, validated metadata, and an optional mask for one blob.
#[derive(Debug, Clone, PartialEq)]
pub struct DecodedF64 {
    /// Validated blob metadata.
    pub info: BlobInfo,
    /// Pixel-interleaved samples promoted to `f64`.
    pub pixels: Vec<f64>,
    /// Row-major validity bytes, when present.
    pub mask: Option<Vec<u8>>,
}

impl DecodedF64 {
    #[cfg(feature = "ndarray")]
    /// Copies the promoted pixels into an ndarray.
    ///
    /// # Errors
    /// Returns an error if metadata and sample length disagree.
    pub fn to_ndarray(&self) -> Result<ArrayD<f64>> {
        ArrayD::from_shape_vec(IxDyn(&self.info.ndarray_shape()), self.pixels.clone()).map_err(
            |err| {
                Error::invalid_blob(format!(
                    "failed to build ndarray from decoded pixels: {err}"
                ))
            },
        )
    }

    #[cfg(feature = "ndarray")]
    /// Moves the promoted pixels into an ndarray.
    ///
    /// # Errors
    /// Returns an error if metadata and sample length disagree.
    pub fn into_ndarray(self) -> Result<ArrayD<f64>> {
        ArrayD::from_shape_vec(IxDyn(&self.info.ndarray_shape()), self.pixels).map_err(|err| {
            Error::invalid_blob(format!(
                "failed to build ndarray from decoded pixels: {err}"
            ))
        })
    }

    #[cfg(feature = "ndarray")]
    /// Moves the optional validity mask into an ndarray.
    ///
    /// # Errors
    /// Returns an error if metadata and mask length disagree.
    pub fn into_mask_ndarray(self) -> Result<Option<ArrayD<u8>>> {
        let shape = self.info.mask_ndarray_shape();
        self.mask
            .map(|mask| {
                ArrayD::from_shape_vec(IxDyn(&shape), mask).map_err(|err| {
                    Error::invalid_blob(format!("failed to build ndarray from decoded mask: {err}"))
                })
            })
            .transpose()
    }

    #[cfg(feature = "ndarray")]
    /// Copies the optional validity mask into an ndarray.
    ///
    /// # Errors
    /// Returns an error if metadata and mask length disagree.
    pub fn mask_ndarray(&self) -> Result<Option<ArrayD<u8>>> {
        let shape = self.info.mask_ndarray_shape();
        self.mask
            .as_ref()
            .map(|mask| {
                ArrayD::from_shape_vec(IxDyn(&shape), mask.clone()).map_err(|err| {
                    Error::invalid_blob(format!("failed to build ndarray from decoded mask: {err}"))
                })
            })
            .transpose()
    }
}

/// Native-typed bands, per-band masks, and shared band-set metadata.
#[derive(Debug, Clone, PartialEq)]
pub struct DecodedBandSet {
    /// Validated metadata for every band.
    info: BandSetInfo,
    /// Native-typed pixel buffers in band order.
    bands: Vec<PixelData>,
    /// Logical mask for each band, including inherited masks.
    band_masks: Vec<Option<Vec<u8>>>,
}

impl DecodedBandSet {
    pub(crate) fn new(
        info: BandSetInfo,
        bands: Vec<PixelData>,
        band_masks: Vec<Option<Vec<u8>>>,
    ) -> Result<Self> {
        validate_band_set_parts(&info, &bands, &band_masks)?;
        Ok(Self {
            info,
            bands,
            band_masks,
        })
    }

    /// Returns validated metadata for the decoded bands.
    pub fn info(&self) -> &BandSetInfo {
        &self.info
    }

    /// Returns native-typed pixel buffers in band order.
    pub fn bands(&self) -> &[PixelData] {
        &self.bands
    }

    /// Returns logical per-band masks, including inherited masks.
    pub fn band_masks(&self) -> &[Option<Vec<u8>>] {
        &self.band_masks
    }

    /// Consumes the result and returns its validated metadata, bands, and masks.
    pub fn into_parts(self) -> (BandSetInfo, Vec<PixelData>, Vec<Option<Vec<u8>>>) {
        (self.info, self.bands, self.band_masks)
    }

    #[cfg(feature = "ndarray")]
    /// Copies the band set into an interleaved ndarray.
    ///
    /// # Errors
    /// Returns an error for incompatible types or inconsistent shapes.
    pub fn to_ndarray<T: BandElement>(&self) -> Result<ArrayD<T>> {
        self.clone().into_ndarray()
    }

    #[cfg(feature = "ndarray")]
    /// Moves the band set into an interleaved ndarray.
    ///
    /// # Errors
    /// Returns an error for incompatible types or inconsistent shapes.
    pub fn into_ndarray<T: BandElement>(self) -> Result<ArrayD<T>> {
        self.into_ndarray_with_layout(BandLayout::Interleaved)
    }

    #[cfg(feature = "ndarray")]
    /// Moves the band set into an ndarray using the requested layout.
    ///
    /// # Errors
    /// Returns an error for incompatible types or inconsistent shapes.
    pub fn into_ndarray_with_layout<T: BandElement>(self, layout: BandLayout) -> Result<ArrayD<T>> {
        let shape = self.info.ndarray_shape_for_layout(layout);
        let values = self.into_vec_with_layout(layout)?;
        ArrayD::from_shape_vec(IxDyn(&shape), values).map_err(|err| {
            Error::invalid_blob(format!(
                "failed to build ndarray from decoded band set: {err}"
            ))
        })
    }

    /// Materializes every band as one homogeneous vector in `layout`.
    ///
    /// # Errors
    /// Returns an error when band types cannot convert to `T` or metadata is inconsistent.
    pub fn into_vec_with_layout<T: BandElement>(self, layout: BandLayout) -> Result<Vec<T>> {
        if self.bands.len() == 1 {
            let band = self
                .bands
                .into_iter()
                .next()
                .ok_or(Error::Internal("single-band decode lost its only band"))?;
            return T::from_pixel_data(band);
        }

        let mut materializer = BandMaterializer::new(
            self.info.first().pixel_count()?,
            self.info.depth() as usize,
            self.info.band_count(),
            layout,
        )?;
        for (band_index, band) in self.bands.into_iter().enumerate() {
            copy_pixel_data_into_materializer(&mut materializer, band_index, band)?;
        }
        materializer.finish()
    }

    /// Copies every band into an exactly sized caller-provided slice.
    ///
    /// # Errors
    /// Returns an error for the wrong output length, incompatible types, or bad metadata.
    pub fn copy_into_slice<T: BandElement>(self, layout: BandLayout, out: &mut [T]) -> Result<()> {
        let pixel_count = self.info.first().pixel_count()?;
        let depth = self.info.depth() as usize;
        let band_count = self.info.band_count();
        let expected_len = self.info.value_count()?;
        if out.len() != expected_len {
            return Err(Error::InvalidArgument(
                "output slice length does not match decoded band set length",
            ));
        }

        for (band_index, band) in self.bands.into_iter().enumerate() {
            copy_pixel_data_into_layout_slice(
                out,
                band_index,
                pixel_count,
                depth,
                band_count,
                layout,
                band,
            )?;
        }
        Ok(())
    }

    #[cfg(feature = "ndarray")]
    /// Moves per-band masks into an ndarray.
    ///
    /// # Errors
    /// Returns an error if mask metadata and lengths disagree.
    pub fn into_band_mask_ndarray(self) -> Result<Option<ArrayD<u8>>> {
        band_masks_into_ndarray(self.info, self.band_masks)
    }

    #[cfg(feature = "ndarray")]
    /// Copies per-band masks into an ndarray.
    ///
    /// # Errors
    /// Returns an error if mask metadata and lengths disagree.
    pub fn band_mask_ndarray(&self) -> Result<Option<ArrayD<u8>>> {
        band_masks_into_ndarray(self.info.clone(), self.band_masks.clone())
    }
}

fn validate_band_set_parts(
    info: &BandSetInfo,
    bands: &[PixelData],
    band_masks: &[Option<Vec<u8>>],
) -> Result<()> {
    if bands.len() != info.band_count() || band_masks.len() != info.band_count() {
        return Err(Error::invalid_blob(
            "decoded band and mask counts do not match band-set metadata",
        ));
    }
    for ((band_info, band), mask) in info.bands().iter().zip(bands).zip(band_masks) {
        if band.data_type() != band_info.data_type {
            return Err(Error::invalid_blob(
                "decoded band type does not match its metadata",
            ));
        }
        if band.len() != band_info.sample_count()? {
            return Err(Error::invalid_blob(
                "decoded band length does not match its metadata",
            ));
        }
        match mask {
            Some(mask) => {
                if mask.len() != band_info.pixel_count()? {
                    return Err(Error::invalid_blob(
                        "decoded band mask length does not match its metadata",
                    ));
                }
                let valid_pixel_count = mask.iter().filter(|&&value| value != 0).count();
                if valid_pixel_count != band_info.valid_pixel_count as usize {
                    return Err(Error::invalid_blob(
                        "decoded band mask count does not match its metadata",
                    ));
                }
            }
            None if band_info.valid_pixel_count as usize != band_info.pixel_count()? => {
                return Err(Error::invalid_blob(
                    "decoded band is missing the mask required by its metadata",
                ));
            }
            None => {}
        }
    }
    Ok(())
}

#[cfg(feature = "ndarray")]
pub(crate) fn band_masks_into_ndarray(
    info: BandSetInfo,
    band_masks: Vec<Option<Vec<u8>>>,
) -> Result<Option<ArrayD<u8>>> {
    if band_masks.iter().all(Option::is_none) {
        return Ok(None);
    }

    let pixel_count = info.first().pixel_count()?;
    let band_count = info.band_count();
    let shape = info.band_mask_ndarray_shape();

    if band_count == 1 {
        let mask = band_masks
            .into_iter()
            .next()
            .flatten()
            .map(Ok)
            .unwrap_or_else(|| default_vec(pixel_count, "decoded band mask"))?;
        return ArrayD::from_shape_vec(IxDyn(&shape), mask)
            .map(Some)
            .map_err(|err| {
                Error::invalid_blob(format!("failed to build ndarray from decoded mask: {err}"))
            });
    }

    let merged_len = checked_mul(pixel_count, band_count, "decoded band mask length")?;
    let mut merged = vec_with_capacity(merged_len, "decoded band mask")?;
    for pixel in 0..pixel_count {
        for band_mask in &band_masks {
            merged.push(band_mask.as_ref().map(|mask| mask[pixel]).unwrap_or(1));
        }
    }

    ArrayD::from_shape_vec(IxDyn(&shape), merged)
        .map(Some)
        .map_err(|err| {
            Error::invalid_blob(format!(
                "failed to build ndarray from decoded band mask: {err}"
            ))
        })
}

trait SupportedElementValue: Copy + 'static + IntoF64 {
    const KIND: BandElementKind;
}

macro_rules! match_pixel_data_values {
    ($band:expr, |$values:ident| $body:expr) => {
        match $band {
            PixelData::I8($values) => $body,
            PixelData::U8($values) => $body,
            PixelData::I16($values) => $body,
            PixelData::U16($values) => $body,
            PixelData::I32($values) => $body,
            PixelData::U32($values) => $body,
            PixelData::F32($values) => $body,
            PixelData::F64($values) => $body,
        }
    };
}

fn copy_pixel_data_into_materializer<T: BandElement>(
    materializer: &mut BandMaterializer<T>,
    band_index: usize,
    band: PixelData,
) -> Result<()> {
    match_pixel_data_values!(band, |values| {
        copy_typed_values_into_materializer(materializer, band_index, &values)
    })
}

fn copy_typed_values_into_materializer<T: BandElement, U: SupportedElementValue>(
    materializer: &mut BandMaterializer<T>,
    band_index: usize,
    values: &[U],
) -> Result<()> {
    if T::KIND == U::KIND {
        // SAFETY: equal BandElementKind values mean T and U are the same
        // supported primitive type, so the slice layout and alignment are
        // unchanged.
        let typed = unsafe { cast_slice::<U, T>(values) };
        return materializer.copy_band(band_index, typed);
    }
    if T::KIND == BandElementKind::F64 {
        return materializer.copy_band_with(band_index, |index| {
            // SAFETY: this branch is only entered when T is f64.
            unsafe_cast::<T, f64>(values[index].into_f64())
        });
    }
    Err(Error::invalid_blob(format!(
        "cannot decode {} pixels into ndarray<{}>",
        data_type_name::<U>(),
        std::any::type_name::<T>()
            .rsplit("::")
            .next()
            .unwrap_or("unknown"),
    )))
}

fn copy_pixel_data_into_layout_slice<T: BandElement>(
    out: &mut [T],
    band_index: usize,
    pixel_count: usize,
    depth: usize,
    band_count: usize,
    layout: BandLayout,
    band: PixelData,
) -> Result<()> {
    match_pixel_data_values!(band, |values| {
        copy_typed_values_into_layout_slice(
            out,
            band_index,
            pixel_count,
            depth,
            band_count,
            layout,
            &values,
        )
    })
}

fn copy_typed_values_into_layout_slice<T: BandElement, U: SupportedElementValue>(
    out: &mut [T],
    band_index: usize,
    pixel_count: usize,
    depth: usize,
    band_count: usize,
    layout: BandLayout,
    values: &[U],
) -> Result<()> {
    if T::KIND == U::KIND {
        // SAFETY: equal BandElementKind values mean T and U are the same
        // supported primitive type, so the slice layout and alignment are
        // unchanged.
        let typed = unsafe { cast_slice::<U, T>(values) };
        return copy_band_values_into_slice(
            out,
            typed,
            pixel_count,
            depth,
            band_index,
            band_count,
            layout,
        );
    }
    if T::KIND == BandElementKind::F64 {
        let band_len = pixel_count
            .checked_mul(depth.max(1))
            .ok_or(Error::SizeOverflow("decoded band length"))?;
        if values.len() != band_len {
            return Err(Error::invalid_blob(
                "decoded band length does not match its metadata",
            ));
        }
        for (value_index, value) in values.iter().copied().enumerate() {
            let out_index = match layout {
                BandLayout::Interleaved => {
                    if depth <= 1 {
                        value_index * band_count + band_index
                    } else {
                        let pixel = value_index / depth;
                        let sample = value_index % depth;
                        (pixel * band_count + band_index) * depth + sample
                    }
                }
                BandLayout::Bsq => band_index * band_len + value_index,
            };
            // SAFETY: this branch is only entered when T is f64.
            out[out_index] = unsafe_cast::<T, f64>(value.into_f64());
        }
        return Ok(());
    }
    Err(Error::invalid_blob(format!(
        "cannot decode {} pixels into ndarray<{}>",
        data_type_name::<U>(),
        std::any::type_name::<T>()
            .rsplit("::")
            .next()
            .unwrap_or("unknown"),
    )))
}

unsafe fn cast_slice<U, T>(values: &[U]) -> &[T] {
    debug_assert_eq!(std::mem::size_of::<U>(), std::mem::size_of::<T>());
    debug_assert_eq!(std::mem::align_of::<U>(), std::mem::align_of::<T>());
    // SAFETY: callers must guarantee U and T are the same primitive element type.
    unsafe { &*(values as *const [U] as *const [T]) }
}

fn unsafe_cast<T, U: Copy>(value: U) -> T {
    debug_assert_eq!(std::mem::size_of::<U>(), std::mem::size_of::<T>());
    debug_assert_eq!(std::mem::align_of::<U>(), std::mem::align_of::<T>());
    // SAFETY: callers only use this helper for same-size primitive casts where
    // T is known by branch guards to match the source value representation.
    unsafe { std::mem::transmute_copy(&value) }
}

trait IntoF64 {
    fn into_f64(self) -> f64;
}

impl IntoF64 for i8 {
    fn into_f64(self) -> f64 {
        self as f64
    }
}
impl IntoF64 for u8 {
    fn into_f64(self) -> f64 {
        self as f64
    }
}
impl IntoF64 for i16 {
    fn into_f64(self) -> f64 {
        self as f64
    }
}
impl IntoF64 for u16 {
    fn into_f64(self) -> f64 {
        self as f64
    }
}
impl IntoF64 for i32 {
    fn into_f64(self) -> f64 {
        self as f64
    }
}
impl IntoF64 for u32 {
    fn into_f64(self) -> f64 {
        self as f64
    }
}
impl IntoF64 for f32 {
    fn into_f64(self) -> f64 {
        self as f64
    }
}
impl IntoF64 for f64 {
    fn into_f64(self) -> f64 {
        self
    }
}

fn data_type_name<T: 'static>() -> &'static str {
    if TypeId::of::<T>() == TypeId::of::<i8>() {
        "i8"
    } else if TypeId::of::<T>() == TypeId::of::<u8>() {
        "u8"
    } else if TypeId::of::<T>() == TypeId::of::<i16>() {
        "i16"
    } else if TypeId::of::<T>() == TypeId::of::<u16>() {
        "u16"
    } else if TypeId::of::<T>() == TypeId::of::<i32>() {
        "i32"
    } else if TypeId::of::<T>() == TypeId::of::<u32>() {
        "u32"
    } else if TypeId::of::<T>() == TypeId::of::<f32>() {
        "f32"
    } else if TypeId::of::<T>() == TypeId::of::<f64>() {
        "f64"
    } else {
        "unknown"
    }
}

mod private {
    pub trait Sealed {}

    impl Sealed for i8 {}
    impl Sealed for u8 {}
    impl Sealed for i16 {}
    impl Sealed for u16 {}
    impl Sealed for i32 {}
    impl Sealed for u32 {}
    impl Sealed for f32 {}
    impl Sealed for f64 {}
}

/// Runtime discriminator for supported homogeneous band output types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BandElementKind {
    /// Signed 8-bit integer.
    I8,
    /// Unsigned 8-bit integer.
    U8,
    /// Signed 16-bit integer.
    I16,
    /// Unsigned 16-bit integer.
    U16,
    /// Signed 32-bit integer.
    I32,
    /// Unsigned 32-bit integer.
    U32,
    /// Single-precision float.
    F32,
    /// Double-precision float.
    F64,
}

/// Sealed primitive types accepted by homogeneous band-set decode APIs.
pub trait BandElement: private::Sealed + Copy + Default + Send + Sync + 'static {
    /// Runtime kind corresponding to this primitive.
    const KIND: BandElementKind;
    /// Converts a native decoded buffer when its representation is compatible.
    fn from_pixel_data(pixels: PixelData) -> Result<Vec<Self>>;
}

#[cfg(feature = "ndarray")]
/// Sealed primitive types accepted by single-blob ndarray decode APIs.
pub trait NdArrayElement: BandElement {}

macro_rules! impl_exact_band_element {
    ($ty:ty, $variant:ident, $name:literal) => {
        impl BandElement for $ty {
            const KIND: BandElementKind = BandElementKind::$variant;

            fn from_pixel_data(pixels: PixelData) -> Result<Vec<Self>> {
                match pixels {
                    PixelData::$variant(values) => Ok(values),
                    other => Err(Error::invalid_blob(format!(
                        "cannot decode {} pixels into ndarray<{}>",
                        other.data_type().name(),
                        $name
                    ))),
                }
            }
        }
    };
}

impl_exact_band_element!(i8, I8, "i8");
impl_exact_band_element!(u8, U8, "u8");
impl_exact_band_element!(i16, I16, "i16");
impl_exact_band_element!(u16, U16, "u16");
impl_exact_band_element!(i32, I32, "i32");
impl_exact_band_element!(u32, U32, "u32");
impl_exact_band_element!(f32, F32, "f32");

impl BandElement for f64 {
    const KIND: BandElementKind = BandElementKind::F64;

    fn from_pixel_data(pixels: PixelData) -> Result<Vec<Self>> {
        Ok(pixels.to_f64())
    }
}

#[cfg(feature = "ndarray")]
macro_rules! impl_ndarray_element {
    ($($ty:ty),+ $(,)?) => { $(impl NdArrayElement for $ty {})+ };
}

#[cfg(feature = "ndarray")]
impl_ndarray_element!(i8, u8, i16, u16, i32, u32, f32, f64);

macro_rules! impl_band_element {
    ($ty:ty, $kind:ident) => {
        impl SupportedElementValue for $ty {
            const KIND: BandElementKind = BandElementKind::$kind;
        }
    };
}

impl_band_element!(i8, I8);
impl_band_element!(u8, U8);
impl_band_element!(i16, I16);
impl_band_element!(u16, U16);
impl_band_element!(i32, I32);
impl_band_element!(u32, U32);
impl_band_element!(f32, F32);
impl_band_element!(f64, F64);

#[cfg(feature = "ndarray")]
trait PixelDataExt {
    fn into_ndarray<T: NdArrayElement>(self, shape: &[usize]) -> Result<ArrayD<T>>;
}

#[cfg(feature = "ndarray")]
impl PixelDataExt for PixelData {
    fn into_ndarray<T: NdArrayElement>(self, shape: &[usize]) -> Result<ArrayD<T>> {
        ArrayD::from_shape_vec(IxDyn(shape), T::from_pixel_data(self)?).map_err(|err| {
            Error::invalid_blob(format!(
                "failed to build ndarray from decoded pixels: {err}"
            ))
        })
    }
}