summaryrefslogtreecommitdiff
path: root/src/mosaic.rs
blob: cbac4c4f68c2e0954c11746b83cb1627b664a403 (plain)
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
// SPDX-License-Identifier: LGPL-3.0-or-later
/*
 * libopenraw - mosaic.rs
 *
 * Copyright (C) 2023-2024 Hubert Figuière
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

//! CFA Mosaic

use num_enum::TryFromPrimitive;

/// C friendly enum for Pattern types.
#[repr(u8)]
pub enum PatternType {
    None = 0,
    NonRgb22 = 1,
    Rggb = 2,
    Gbrg = 3,
    Bggr = 4,
    Grbg = 5,
}

impl From<&Pattern> for PatternType {
    fn from(value: &Pattern) -> PatternType {
        match *value {
            Pattern::Empty => PatternType::None,
            Pattern::NonRgb22(_) => PatternType::NonRgb22,
            Pattern::Rggb => PatternType::Rggb,
            Pattern::Gbrg => PatternType::Gbrg,
            Pattern::Bggr => PatternType::Bggr,
            Pattern::Grbg => PatternType::Grbg,
        }
    }
}

/// A pattern colour component.
#[derive(Clone, Copy, Debug, PartialEq, TryFromPrimitive)]
#[repr(u8)]
pub enum PatternColour {
    Red = 0,
    Green = 1,
    Blue = 2,
}

impl PatternColour {
    pub fn to_char(self) -> char {
        match self {
            Self::Red => 'R',
            Self::Green => 'G',
            Self::Blue => 'B',
        }
    }
}

/// Describe a pattern
#[derive(Clone, Debug, Default, PartialEq)]
pub enum Pattern {
    /// Empty pattern. Most case it's an error.
    #[default]
    Empty,
    /// Non RGB22. Like X-Trans, the data is the actual pattern.
    NonRgb22(Vec<PatternColour>),
    /// RGGB 2x2 bayer
    Rggb,
    /// GBRG 2x2 bayer
    Gbrg,
    /// BGGR 2x2 bayer
    Bggr,
    /// GRBG 2x2 bayer
    Grbg,
}

impl std::fmt::Display for Pattern {
    /// `Display` will print a string of the pattern colour filters
    /// left - right & top - bottom
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match *self {
                Self::Empty => "NONE",
                Self::Rggb => "RGGB",
                Self::Gbrg => "GBRG",
                Self::Bggr => "BGGR",
                Self::Grbg => "GRBG",
                Self::NonRgb22(_) => "NON_RGB22",
                //p.iter().map(|c| c.to_char()).collect(),
            }
        )
    }
}

impl std::convert::TryFrom<&[u8]> for Pattern {
    type Error = &'static str;

    fn try_from(v: &[u8]) -> Result<Pattern, Self::Error> {
        use PatternColour::*;
        if v.len() == 4 {
            if v == [Red as u8, Green as u8, Green as u8, Blue as u8] {
                Ok(Self::Rggb)
            } else if v == [Green as u8, Blue as u8, Red as u8, Green as u8] {
                Ok(Self::Gbrg)
            } else if v == [Green as u8, Red as u8, Blue as u8, Green as u8] {
                Ok(Self::Grbg)
            } else if v == [Blue as u8, Green as u8, Green as u8, Red as u8] {
                Ok(Self::Bggr)
            } else {
                Err("Invalid pattern")
            }
        } else {
            let colours: Vec<PatternColour> = v
                .iter()
                .map(|colour| PatternColour::try_from_primitive(*colour))
                .take_while(Result::is_ok)
                .map(|v| v.unwrap())
                .collect();
            if colours.len() == v.len() {
                Ok(Self::NonRgb22(colours))
            } else {
                Err("Invalid colour found")
            }
        }
    }
}

impl Pattern {
    /// Return a `PatternType`, mostly for C API.
    pub fn pattern_type(&self) -> PatternType {
        self.into()
    }

    /// Return the pattern colour array
    pub fn pattern(&self) -> Vec<PatternColour> {
        use PatternColour::*;
        match *self {
            Self::Empty => Vec::default(),
            Self::Rggb => vec![Red, Green, Green, Blue],
            Self::Gbrg => vec![Green, Blue, Red, Green],
            Self::Bggr => vec![Blue, Green, Green, Red],
            Self::Grbg => vec![Green, Red, Blue, Green],
            Self::NonRgb22(ref p) => p.clone(),
        }
    }
}

#[cfg(test)]
mod test {
    use std::convert::TryFrom;

    use super::Pattern;
    use super::PatternColour::*;

    #[test]
    fn test_pattern_to_string() {
        let pattern = Pattern::default();
        assert_eq!(&pattern.to_string(), "NONE");

        let pattern = Pattern::Rggb;
        assert_eq!(&pattern.to_string(), "RGGB");

        let pattern = Pattern::NonRgb22(vec![Red, Green, Blue, Blue, Red, Green, Green, Blue, Red]);
        assert_eq!(&pattern.to_string(), "NON_RGB22");
    }

    #[test]
    fn test_pattern_try_from() {
        // Valid 2x2 pattern
        let pattern = vec![Red as u8, Green as u8, Green as u8, Blue as u8];
        assert_eq!(Pattern::try_from(pattern.as_slice()), Ok(Pattern::Rggb));

        // Invalid 2x2 pattern
        let pattern = vec![Green as u8, Green as u8, Red as u8, Blue as u8];
        assert!(Pattern::try_from(pattern.as_slice()).is_err());

        // Valid non 2x2 pattern
        let pattern = vec![
            Red as u8,
            Green as u8,
            Green as u8,
            Blue as u8,
            Red as u8,
            Green as u8,
            Green as u8,
            Blue as u8,
        ];
        assert_eq!(
            Pattern::try_from(pattern.as_slice()),
            Ok(Pattern::NonRgb22(vec![
                Red, Green, Green, Blue, Red, Green, Green, Blue
            ]))
        );

        // Test we return an error if one of the component is invalid
        let pattern = vec![
            Red as u8,
            Green as u8,
            100,
            Blue as u8,
            Red as u8,
            Green as u8,
            Green as u8,
            Blue as u8,
        ];
        assert!(Pattern::try_from(pattern.as_slice()).is_err());
    }
}