#![doc = include_str!("../README.md")]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

pub mod nvtiff;
pub mod result;
pub use nvtiff::*;
pub use result::{NvTiffResult, NvTiffResultCheck};

#[cfg(test)]
mod tests {
    use crate::{
        nvtiffCompression, nvtiffImageInfo, nvtiffPhotometricInt, nvtiffPlanarConfig,
        nvtiffSampleFormat, nvtiffStatus_t, nvtiffStream, nvtiffStreamCreate,
        nvtiffStreamGetImageInfo, nvtiffStreamGetNumImages, nvtiffStreamParseFromFile,
    };

    #[test]
    fn test_nvtiff_get_metadata_info() {
        // nvTIFF decoding!!
        // https://docs.nvidia.com/cuda/nvtiff/quickstartguide.html#nvtiff-decode

        // Step 0: Create bitstream
        // https://stackoverflow.com/questions/43350167/how-do-i-initialize-an-opaque-c-struct-when-using-rust-ffi
        let mut stream = std::mem::MaybeUninit::uninit();
        let mut tiff_stream: *mut nvtiffStream = stream.as_mut_ptr();

        let status_create: nvtiffStatus_t::Type =
            unsafe { nvtiffStreamCreate(&raw mut tiff_stream) };
        dbg!(status_create); // should be 0=SUCCESS
        assert_eq!(status_create, nvtiffStatus_t::NVTIFF_STATUS_SUCCESS);

        // Step 1: Parse the TIFF file from disk
        let tiff_cstr = std::ffi::CString::new("images/bali_notiles.tif").unwrap();
        let tiff_path: *const std::os::raw::c_char = tiff_cstr.as_ptr();

        let status_parse: u32 = unsafe { nvtiffStreamParseFromFile(tiff_path, tiff_stream) };
        dbg!(status_parse); // should be 0=SUCCESS
        assert_eq!(status_parse, nvtiffStatus_t::NVTIFF_STATUS_SUCCESS);

        // Step 2: Extract file-level metadata information from the TIFF stream
        let mut num_images: u32 = 0;

        let status_numimages: u32 =
            unsafe { nvtiffStreamGetNumImages(tiff_stream, &raw mut num_images) };
        assert_eq!(status_numimages, nvtiffStatus_t::NVTIFF_STATUS_SUCCESS);
        dbg!(status_numimages);
        assert_eq!(num_images, 1);

        // Step 3: Extract image-level metadata information from the TIFF stream
        let mut image_info = nvtiffImageInfo::default();

        let status_imageinfo: u32 =
            unsafe { nvtiffStreamGetImageInfo(tiff_stream, 0, &raw mut image_info) };
        assert_eq!(status_imageinfo, nvtiffStatus_t::NVTIFF_STATUS_SUCCESS);
        dbg!(image_info);
        assert_eq!(
            image_info,
            nvtiffImageInfo {
                image_type: 0, // nvtiffImageType::NVTIFF_IMAGETYPE_?,
                image_width: 725,
                image_height: 489,
                compression: nvtiffCompression::NVTIFF_COMPRESSION_LZW, // 5
                photometric_int: nvtiffPhotometricInt::NVTIFF_PHOTOMETRIC_RGB, // 2
                planar_config: nvtiffPlanarConfig::NVTIFF_PLANARCONFIG_CONTIG, // 1
                samples_per_pixel: 3,
                bits_per_pixel: 24,
                bits_per_sample: [8, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                sample_format: [
                    nvtiffSampleFormat::NVTIFF_SAMPLEFORMAT_UINT, // 1
                    nvtiffSampleFormat::NVTIFF_SAMPLEFORMAT_UINT, // 1
                    nvtiffSampleFormat::NVTIFF_SAMPLEFORMAT_UINT, // 1
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0
                ],
            }
        );
    }
}
