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
//! A pure rust library to handle camera raw files.
//! 
//! **quickraw** is a pure rust library to decode and renderer image from camera raw files.
//! 
//! ## Examples
//! #### Export thumbnail
//! ```no_run
//! use quickraw::Export;
//! 
//! let raw_data = std::fs::read("sample.ARW").unwrap();
//! let (thumbnail_data, orientation) = Export::export_thumbnail_data(&raw_data).unwrap();
//! 
//! // notice that this function is available on feature `image` only.
//! quickraw::Export::export_thumbnail_to_file("sample.ARW", "sample.thumbnail.jpg").unwrap();
//! ```
//! 
//! #### Get EXIF data
//! ```no_run
//! use quickraw::Export;
//! let info = Export::export_exif_info(Input::ByFile("sample.ARW")).unwrap();
//! 
//! // info is a `quickexif::ParsedInfo` type, for more info please check https://docs.rs/quickexif
//! let width = info.usize("width").unwrap();
//! ```
//! #### Export image
//! ```no_run
//! use quickraw::{data, DemosaicingMethod, Input, Output, Export, OutputType};
//! 
//! let demosaicing_method = DemosaicingMethod::Linear;
//! let color_space = data::XYZ2SRGB;
//! let gamma = data::GAMMA_SRGB;
//! let output_type = OutputType::Raw16;
//! let auto_crop = false;
//! let auto_rotate = false;
//! 
//! let export_job = Export::new(
//!     Input::ByFile("sample.ARW"),
//!     Output::new(
//!         demosaicing_method,
//!         color_space,
//!         gamma,
//!         output_type,
//!         auto_crop,
//!         auto_rotate,
//!     ),
//! ).unwrap();
//! 
//! let (image, width, height) = export_job.export_16bit_image();
//! 
//! // or you can also export an image with quality(only works when the output type is JPEG).
//! // notice that this function is available on feature `image` only.
//! export_job.export_image(92).unwrap();
//! ```

#![cfg_attr(docsrs, feature(doc_auto_cfg))]

/// A flag to enable benchmark for several key processes.
pub const BENCH_FLAG: &str = "QUICKRAW_BENCH";

use thiserror::Error;

pub mod data;

mod color;
mod utility;

mod maker;
mod raw;

mod decode;
pub use decode::new_image_from_file;
pub use decode::new_image_from_buffer;

pub mod export;
pub use export::Export;

const BIT_SHIFT: u32 = 13u32;

#[derive(Debug)]
struct ColorConversion {
    white_balance: [i32; 3],
    gamma_lut: [u16; 65536],
    color_space: [i32; 9],
}

/// All the demosaicing method currently supported.
#[derive(Clone)]
pub enum DemosaicingMethod {
    None,
    SuperPixel,
    Linear,
}

/// Decides if the output should be 8bit or 16bit.
#[derive(Clone)]
pub enum OutputType {
    Raw8,
    Raw16,
    Image8(String),
    Image16(String),
}

/// Chooses the input from a file or a buffer.
pub enum Input<'a> {
    ByFile(&'a str),
    ByBuffer(Vec<u8>),
}

/// Contains options for image rendering.
#[allow(dead_code)]
#[derive(Clone)]
pub struct Output {
    demosaicing_method: DemosaicingMethod,
    color_space: [f32; 9],
    gamma: [f32; 2],
    output_type: OutputType,
    auto_crop: bool,
    auto_rotate: bool,
}
impl Output {
    pub fn new(
        demosaicing_method: DemosaicingMethod,
        color_space: [f32; 9],
        gamma: [f32; 2],
        output_type: OutputType,
        auto_crop: bool,
        auto_rotate: bool,
    ) -> Output {
        Output {
            demosaicing_method,
            color_space,
            gamma,
            output_type,
            auto_crop,
            auto_rotate,
        }
    }
}

/// Errors of raw file reading.
#[derive(Error, Debug)]
pub enum RawFileReadingError {
    #[error("Exif parsing error.")]
    ExifParseError(#[from] quickexif::parser::Error),
    #[error("Exif parsed info error.")]
    ExifParseInfoError(#[from] quickexif::parsed_info::Error),
    #[error("Cannot read the raw file.")]
    DecodingError(#[from] maker::DecodingError),
    #[error("The file '{0}' is not existed.")]
    FileNotExisted(String),
    #[error("The metadata of file '{0}' cannot be read.")]
    FileMetadataReadingError(String),
    #[error("The content of file '{0}' cannot be read.")]
    FileContentReadingError(String),
    #[error("Cannot read Make info from this raw file.")]
    CannotReadMake,
    #[error("Cannot read Model info from this raw file.")]
    CannotReadModel,
    #[error("This raw file from maker: '{0}' is not supported yet.")]
    MakerIsNotSupportedYet(String),
    #[error("This raw file model: '{0}' is not supported yet.")]
    ModelIsNotSupportedYet(String),
}