pub fn to_dynamic_image(&self) -> Result<DynamicImage> {
        match self.samples_per_pixel {
            1 => {
                let mut pixel_array = self.to_ndarray::<f64>()?;
                // Only Monochrome images can have a Modality LUT
                pixel_array.mapv_inplace(|v| {
                    (v * self.rescale_slope as f64) + self.rescale_intercept as f64
                });

                // TODO(#122): Apply VOI LUT

                // Normalize to u16
                let min = pixel_array.min().unwrap();
                let max = pixel_array.max().unwrap();
                let mut pixel_array =
                    pixel_array.map(|v| (u16::MAX as f64 * (v - *min) / (*max - *min)) as u16);

                // Convert MONOCHROME1 to MONOCHROME2 if needed
                if self.photometric_interpretation == "MONOCHROME1" {
                    pixel_array.mapv_inplace(|v| u16::MAX - v);
                }

                let image_buffer: ImageBuffer<Luma<u16>, Vec<u16>> =
                    ImageBuffer::from_raw(self.cols, self.rows, pixel_array.into_raw_vec())
                        .context(InvalidImageBuffer)?;
                Ok(DynamicImage::ImageLuma16(image_buffer))
            }
            3 => {
                // RGB, YBR_FULL or YBR_FULL_422 colors
                match self.bits_allocated {
                    8 => {
                        // Convert YBR_FULL or YBR_FULL_422 to RGB
                        let pixel_array = match self.photometric_interpretation.as_str() {
                            "RGB" => self.data.to_vec(),
                            "YBR_FULL" | "YBR_FULL_422" => {
                                let mut pixel_array = self.data.to_vec();
                                convert_colorspace_u8(&mut pixel_array);
                                pixel_array
                            }
                            _ => UnsupportedColorSpace.fail()?,
                        };
                        let image_buffer: ImageBuffer<Rgb<u8>, Vec<u8>> =
                            ImageBuffer::from_raw(self.cols, self.rows, pixel_array)
                                .context(InvalidImageBuffer)?;
                        Ok(DynamicImage::ImageRgb8(image_buffer))
                    }
                    16 => {
                        let mut pixel_array = vec![0; self.data.len() / 2];
                        NativeEndian::read_u16_into(&self.data, &mut pixel_array);

                        // Convert YBR_FULL or YBR_FULL_422 to RGB
                        let pixel_array = match self.photometric_interpretation.as_str() {
                            "RGB" => pixel_array,
                            "YBR_FULL" | "YBR_FULL_422" => {
                                convert_colorspace_u16(&mut pixel_array);
                                pixel_array
                            }
                            _ => UnsupportedColorSpace.fail()?,
                        };
                        let image_buffer: ImageBuffer<Rgb<u16>, Vec<u16>> =
                            ImageBuffer::from_raw(self.cols, self.rows, pixel_array)
                                .context(InvalidImageBuffer)?;
                        Ok(DynamicImage::ImageRgb16(image_buffer))
                    }
                    _ => InvalidBitsAllocated.fail()?,
                }
            }
            _ => InvalidPixelRepresentation.fail()?,
        }
    }
/// Convert decoded pixel data into a DynamicImage.
/// A new <u8> or <u16> vector is created in memory
/// with normalized grayscale values after applying Modality LUT.
Convert decoded pixel data into a DynamicImage.
A new  or  vector is created in memory
with normalized grayscale values after applying Modality LUT.
Convert decoded pixel data into a DynamicImage . A new or vector is created in memory with normalized grayscale values after applying Modality LUT .
pub fn to_dynamic_image(&self) -> Result<DynamicImage> {
        match self.samples_per_pixel {
            1 => {
                let mut pixel_array = self.to_ndarray::<f64>()?;
                // Only Monochrome images can have a Modality LUT
                pixel_array.mapv_inplace(|v| {
                    (v * self.rescale_slope as f64) + self.rescale_intercept as f64
                });
                // TODO(#122): Apply VOI LUT
                // Normalize to u16
                let min = pixel_array.min().unwrap();
                let max = pixel_array.max().unwrap();
                let mut pixel_array =
                    pixel_array.map(|v| (u16::MAX as f64 * (v - *min) / (*max - *min)) as u16);
                // Convert MONOCHROME1 to MONOCHROME2 if needed
                if self.photometric_interpretation == "MONOCHROME1" {
                    pixel_array.mapv_inplace(|v| u16::MAX - v);
                }
                let image_buffer: ImageBuffer<Luma<u16>, Vec<u16>> =
                    ImageBuffer::from_raw(self.cols, self.rows, pixel_array.into_raw_vec())
                        .context(InvalidImageBuffer)?;
                Ok(DynamicImage::ImageLuma16(image_buffer))
            }
            3 => {
                // RGB, YBR_FULL or YBR_FULL_422 colors
                match self.bits_allocated {
                    8 => {
                        // Convert YBR_FULL or YBR_FULL_422 to RGB
                        let pixel_array = match self.photometric_interpretation.as_str() {
                            "RGB" => self.data.to_vec(),
                            "YBR_FULL" | "YBR_FULL_422" => {
                                let mut pixel_array = self.data.to_vec();
                                convert_colorspace_u8(&mut pixel_array);
                                pixel_array
                            }
                            _ => UnsupportedColorSpace.fail()?,
                        };
                        let image_buffer: ImageBuffer<Rgb<u8>, Vec<u8>> =
                            ImageBuffer::from_raw(self.cols, self.rows, pixel_array)
                                .context(InvalidImageBuffer)?;
                        Ok(DynamicImage::ImageRgb8(image_buffer))
                    }
                    16 => {
                        let mut pixel_array = vec![0; self.data.len() / 2];
                        NativeEndian::read_u16_into(&self.data, &mut pixel_array);
                        // Convert YBR_FULL or YBR_FULL_422 to RGB
                        let pixel_array = match self.photometric_interpretation.as_str() {
                            "RGB" => pixel_array,
                            "YBR_FULL" | "YBR_FULL_422" => {
                                convert_colorspace_u16(&mut pixel_array);
                                pixel_array
                            }
                            _ => UnsupportedColorSpace.fail()?,
                        };
                        let image_buffer: ImageBuffer<Rgb<u16>, Vec<u16>> =
                            ImageBuffer::from_raw(self.cols, self.rows, pixel_array)
                                .context(InvalidImageBuffer)?;
                        Ok(DynamicImage::ImageRgb16(image_buffer))
                    }
                    _ => InvalidBitsAllocated.fail()?,
                }
            }
            _ => InvalidPixelRepresentation.fail()?,
        }
    }
pub fn to_dynamic_image ( & self ) -> Result < DynamicImage > { match self . samples_per_pixel { 1 => { let mut pixel_array = self . to_ndarray :: < f64 > ( ) ? ; // Only Monochrome images can have a Modality LUT pixel_array . mapv_inplace ( | v | { ( v * self . rescale_slope as f64 ) + self . rescale_intercept as f64 } ) ; // TODO(#122): Apply VOI LUT // Normalize to u16 let min = pixel_array . min ( ) . unwrap ( ) ; let max = pixel_array . max ( ) . unwrap ( ) ; let mut pixel_array = pixel_array . map ( | v | ( u16 :: MAX as f64 * ( v - * min ) / ( * max - * min ) ) as u16 ) ; // Convert MONOCHROME1 to MONOCHROME2 if needed if self . photometric_interpretation == " " { pixel_array . mapv_inplace ( | v | u16 :: MAX - v ) ; } let image_buffer : ImageBuffer < Luma < u16 > , Vec < u16 > > = ImageBuffer :: from_raw ( self . cols , self . rows , pixel_array . into_raw_vec ( ) ) . context ( InvalidImageBuffer ) ? ; Ok ( DynamicImage :: ImageLuma16 ( image_buffer ) ) } 3 => { // RGB, YBR_FULL or YBR_FULL_422 colors match self . bits_allocated { 8 => { // Convert YBR_FULL or YBR_FULL_422 to RGB let pixel_array = match self . photometric_interpretation . as_str ( ) { " " => self . data . to_vec ( ) , " " | " " => { let mut pixel_array = self . data . to_vec ( ) ; convert_colorspace_u8 ( & mut pixel_array ) ; pixel_array } _ => UnsupportedColorSpace . fail ( ) ? , } ; let image_buffer : ImageBuffer < Rgb < u8 > , Vec < u8 > > = ImageBuffer :: from_raw ( self . cols , self . rows , pixel_array ) . context ( InvalidImageBuffer ) ? ; Ok ( DynamicImage :: ImageRgb8 ( image_buffer ) ) } 16 => { let mut pixel_array = vec ! [ 0 self data len ( ) 2 ] ; NativeEndian :: read_u16_into ( & self . data , & mut pixel_array ) ; // Convert YBR_FULL or YBR_FULL_422 to RGB let pixel_array = match self . photometric_interpretation . as_str ( ) { " " => pixel_array , " " | " " => { convert_colorspace_u16 ( & mut pixel_array ) ; pixel_array } _ => UnsupportedColorSpace . fail ( ) ? , } ; let image_buffer : ImageBuffer < Rgb < u16 > , Vec < u16 > > = ImageBuffer :: from_raw ( self . cols , self . rows , pixel_array ) . context ( InvalidImageBuffer ) ? ; Ok ( DynamicImage :: ImageRgb16 ( image_buffer ) ) } _ => InvalidBitsAllocated . fail ( ) ? , } } _ => InvalidPixelRepresentation . fail ( ) ? , } }
Convert decoded pixel data into a DynamicImage.
Convert decoded pixel data into a DynamicImage .
&self
&self