# process_raster.rs - source
Source of the Rust file `maplibre/src/raster/process_raster.rs`.

    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

    use std::marker::PhantomData;

    use image::RgbaImage;
    use thiserror::Error;

    use crate::{
        coords::WorldTileCoords,
        io::apc::Context,
        raster::transferables::{LayerRaster, RasterTransferables},
    };

    #[derive(Error, Debug)]
    pub enum ProcessRasterError {
        /// Error during processing of the pipeline
        #[error("processing data in pipeline failed")]
        Processing(Box<dyn std::error::Error>),
    }

    pub struct RasterTileRequest {
        pub coords: WorldTileCoords,
    }

    pub fn process_raster_tile<T: RasterTransferables, C: Context>(
        data: &[u8],
        tile_request: RasterTileRequest,
        context: &mut ProcessRasterContext<T, C>,
    ) -> Result<(), ProcessRasterError> {
        let coords = &tile_request.coords;
        let img = image::load_from_memory(data).unwrap();
        let rgba = img.to_rgba8();

        context.layer_raster_finished(coords, "raster".to_string(), rgba)?;

        Ok(())
    }
    pub struct ProcessRasterContext<T: RasterTransferables, C: Context> {
        context: C,
        phantom_t: PhantomData<T>,
    }

    impl<T: RasterTransferables, C: Context> ProcessRasterContext<T, C> {
        pub fn new(context: C) -> Self {
            Self {
                context,
                phantom_t: Default::default(),
            }
        }
    }

    impl<T: RasterTransferables, C: Context> ProcessRasterContext<T, C> {
        fn layer_raster_finished(
            &mut self,
            coords: &WorldTileCoords,
            layer_name: String,
            image_data: RgbaImage,
        ) -> Result<(), ProcessRasterError> {
            self.context
                .send(T::LayerRaster::build_from(*coords, layer_name, image_data))
                .map_err(|e| ProcessRasterError::Processing(Box::new(e)))
        }
    }

    #[cfg(test)]
    mod tests {
        use super::process_raster_tile;
        use crate::{
            coords::ZoomLevel,
            io::apc::tests::DummyContext,
            raster::{
                process_raster::{ProcessRasterContext, RasterTileRequest},
                DefaultRasterTransferables,
            },
        };

        #[test] // TODO: Add proper tile byte array
        #[ignore]
        fn test() {
            let _output = process_raster_tile(
                &[0],
                RasterTileRequest {
                    coords: (0, 0, ZoomLevel::default()).into(),
                },
                &mut ProcessRasterContext::<DefaultRasterTransferables, _>::new(DummyContext),
            );
        }
    }