summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: a33eab5a81f8bd3dee821a90140c272ee4df88a8 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// SPDX-License-Identifier: LGPL-3.0-or-later
/*
 * libopenraw - lib.rs
 *
 * Copyright (C) 2022-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/>.
 */

// For mp4::parse
#[macro_use]
extern crate log;

use once_cell::sync::Lazy;

#[macro_use]
mod dump;
#[macro_use]
mod utils;

mod apple;
mod bitmap;
mod camera_ids;
mod canon;
mod capi;
mod colour;
mod container;
mod decompress;
mod dng;
mod epson;
mod factory;
mod fujifilm;
mod identify;
mod io;
mod jpeg;
mod leica;
pub mod metadata;
mod minolta;
mod mosaic;
mod mp4;
mod nikon;
mod olympus;
mod panasonic;
mod pentax;
mod rawfile;
mod rawimage;
mod render;
mod ricoh;
mod sigma;
mod sony;
mod thumbnail;
pub mod tiff;

pub use bitmap::{Bitmap, Image, Point, Rect, Size};
pub use colour::ColourSpace;
pub use dump::Dump;
pub use mosaic::Pattern as CfaPattern;
pub use rawfile::{RawFile, RawFileHandle, RawFileImpl};
pub use rawimage::RawImage;
pub use render::{RenderingOptions, RenderingStage};
pub use thumbnail::Thumbnail;
pub use tiff::Ifd;

#[cfg(any(feature = "fuzzing", feature = "bench"))]
pub use decompress::LJpeg;
#[cfg(any(feature = "fuzzing", feature = "bench"))]
pub use olympus::decompress::decompress_olympus;

pub use rawfile::rawfile_from_file;
pub use rawfile::rawfile_from_io;
pub use rawfile::rawfile_from_memory;

/// Standard Result for libopenraw
pub type Result<T> = std::result::Result<T, Error>;

use mp4::parse as mp4parse;

/// Standard `Error` for libopenraw
#[derive(Debug, thiserror::Error)]
pub enum Error {
    // No error. For compatibility with `capi::or_error`
    #[error("No error")]
    None,
    /// This is unimplemented
    #[error("Unimplemented")]
    Unimplemented,
    /// File format is unrecognized
    #[error("Unrecognized format")]
    UnrecognizedFormat,
    /// Not supported
    #[error("Operation not supported")]
    NotSupported,
    /// Not found in file
    #[error("Data not found")]
    NotFound,
    /// Buffer too small: we expect a bigger amount of data
    #[error("Buffer is too small")]
    BufferTooSmall,
    /// Unexpected end of file
    #[error("Unexpected end-of-file")]
    UnexpectedEOF,
    /// IO Error
    #[error("IO Error: {0}")]
    IoError(#[from] std::io::Error),
    /// Error parsing format
    #[error("Format error")]
    FormatError,
    /// Already inited
    #[error("Already Inited")]
    AlreadyInited,
    /// Invalid parameter
    #[error("Invalid parameter")]
    InvalidParam,
    /// Invalid format: wrong kind of data found
    #[error("Invalid format")]
    InvalidFormat,
    /// Decompression error.
    #[error("Decompression error: {0}")]
    Decompression(String),
    /// MP4 parse error
    #[error("MP4 Parse Error: {0}")]
    Mp4Parse(#[from] mp4parse::Error),
    /// Jpeg decompress
    #[error("JPEG error: {0}")]
    JpegFormat(String),
    /// Bit reader error.
    #[error("BitReader error: {0}")]
    BitReaderError(#[from] bitreader::BitReaderError),
    #[error("Invalid address")]
    InvalidAddress,
    /// Other error.
    #[error("Other error: {0}")]
    Other(String),
    /// Unknown error: placeholder for anything else.
    #[error("Unknown error")]
    Unknown,
}

/// What type is the data.
///
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum DataType {
    /// JPEG stream
    Jpeg,
    /// RGB8 Pixmap
    PixmapRgb8,
    /// RGB8 Pixmap
    PixmapRgb16,
    /// RAW data compressed. (undetermined codec)
    CompressedRaw,
    /// RAW data uncompressed
    Raw,
    /// Unknown type
    #[default]
    Unknown,
}

impl From<&str> for DataType {
    fn from(s: &str) -> DataType {
        match s {
            "JPEG" => Self::Jpeg,
            "8RGB" => Self::PixmapRgb8,
            "16RGB" => Self::PixmapRgb16,
            "COMP_RAW" => Self::CompressedRaw,
            "RAW" => Self::Raw,
            _ => Self::Unknown,
        }
    }
}

impl From<DataType> for String {
    fn from(t: DataType) -> String {
        match t {
            DataType::Jpeg => "JPEG",
            DataType::Raw => "RAW",
            DataType::PixmapRgb8 => "8RGB",
            DataType::PixmapRgb16 => "16RGB",
            DataType::CompressedRaw => "COMP_RAW",
            DataType::Unknown => "UNKNOWN",
        }
        .to_string()
    }
}

/// RAW file type. This list the type of files, which
/// coincidentally match the vendor, except for DNG.
///
/// The value match the enum on the C++ headers.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(u32)]
pub enum Type {
    /// Unknown vendor
    Unknown = 0,
    /// Sony Alpha
    Arw = 5,
    /// Canon RAW CIFF based
    Crw = 2,
    /// Canon RAW TIFF based
    Cr2 = 1,
    /// Canon RAW MPEG4 ISO based
    Cr3 = 14,
    /// Adobe Digital Negative
    Dng = 6,
    /// Epson RAW
    Erf = 9,
    /// GoPro RAW
    Gpr = 15,
    /// Minolta RAW
    Mrw = 4,
    /// Nikon RAW
    Nef = 3,
    /// Nikon RAW (NRW variant)
    Nrw = 11,
    /// Olympus RAW
    Orf = 7,
    /// Pentax RAW
    Pef = 8,
    /// Fujfilm RAW
    Raf = 13,
    /// Panasonic old RAW
    Rw = 17,
    /// Panasonic RAW
    Rw2 = 12,
    /// Sony RAW (old)
    Sr2 = 16,
    /// JPEG (definitely not a Raw)
    Jpeg = 100,
    Tiff = 10,
    #[cfg(test)]
    /// Value for testing only
    Test = 200,
}

impl From<&str> for Type {
    fn from(s: &str) -> Type {
        match s {
            "ARW" => Self::Arw,
            "CR2" => Self::Cr2,
            "CR3" => Self::Cr3,
            "CRW" => Self::Crw,
            "DNG" => Self::Dng,
            "ERF" => Self::Erf,
            "GPR" => Self::Gpr,
            "MRW" => Self::Mrw,
            "NEF" => Self::Nef,
            "NRW" => Self::Nrw,
            "ORF" => Self::Orf,
            "PEF" => Self::Pef,
            "RAF" => Self::Raf,
            "RW2" => Self::Rw2,
            "SR2" => Self::Sr2,
            _ => Self::Unknown,
        }
    }
}

impl From<Type> for String {
    fn from(t: Type) -> String {
        match t {
            Type::Arw => "ARW",
            Type::Cr2 => "CR2",
            Type::Cr3 => "CR3",
            Type::Crw => "CRW",
            Type::Dng => "DNG",
            Type::Erf => "ERF",
            Type::Gpr => "GPR",
            Type::Jpeg => "JPEG",
            Type::Mrw => "MRW",
            Type::Nef => "NEF",
            Type::Nrw => "NRW",
            Type::Orf => "ORF",
            Type::Pef => "PEF",
            Type::Raf => "RAF",
            Type::Rw => "RW",
            Type::Rw2 => "RW2",
            Type::Sr2 => "SR2",
            #[cfg(test)]
            Type::Test => "TEST",
            Type::Tiff => "TIF",
            Type::Unknown => "UNKNOWN",
        }
        .to_string()
    }
}

/// Type ID (vendor, model)
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TypeId(u16, u16);

impl std::fmt::Display for TypeId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} {}", self.0, self.1)
    }
}

impl Default for TypeId {
    fn default() -> Self {
        TypeId(camera_ids::vendor::NONE, camera_ids::generic::UNKNOWN)
    }
}

impl From<u32> for TypeId {
    fn from(id: u32) -> TypeId {
        TypeId(((id & 0xffff0000) >> 16) as u16, (id & 0xffff) as u16)
    }
}
impl From<TypeId> for u32 {
    fn from(type_id: TypeId) -> u32 {
        ((type_id.0 as u32) << 16) | type_id.1 as u32
    }
}

static EXTENSIONS: Lazy<Vec<String>> = Lazy::new(|| {
    crate::identify::EXT_TO_TYPE
        .iter()
        .filter_map(|(e, t)| {
            if *t != Type::Jpeg {
                Some(String::from(*e))
            } else {
                None
            }
        })
        .collect()
});

/// Return the extensions for raw files (in lowercase).
pub fn extensions() -> &'static [String] {
    &EXTENSIONS
}

#[cfg(test)]
mod test {
    use super::TypeId;

    #[test]
    fn test_typeid_from_u32() {
        let id = 0x0001_0042;
        assert_eq!(TypeId(1, 0x42), TypeId::from(id));
    }

    #[test]
    fn test_typeid_to_u32() {
        let id: u32 = TypeId(1, 0x42).into();
        assert_eq!(id, 0x0001_0042);
    }
}