flif/
lib.rs

1//! # Example
2//! ```rust
3//! extern crate flif;
4//!
5//! use std::fs::File;
6//! use std::io::BufReader;
7//! use flif::Flif;
8//!
9//! fn main() {
10//!     let file = std::fs::File::open("../resources/flif_logo.flif").unwrap();
11//!     // use `BufReader` to improve performance
12//!     let reader = BufReader::new(file);
13//!     let image = Flif::decode(reader).unwrap();
14//!     println!("image info: {:?}", image.info());
15//!     let raw_pixels = image.raw();
16//! }
17//! ```
18
19use std::io::Read;
20
21use components::header::{Header, SecondHeader};
22use components::metadata::Metadata;
23use components::transformations::Transform;
24use decoding_image::DecodingImage;
25
26pub use decoder::Decoder;
27pub use error::{Error, Result};
28
29pub mod components;
30mod decoder;
31mod decoding_image;
32mod error;
33mod maniac;
34mod numbers;
35mod pixels;
36
37pub struct Flif {
38    info: FlifInfo,
39    raw: Box<[u8]>,
40}
41
42impl Flif {
43    pub fn decode<R: Read>(reader: R) -> Result<Self> {
44        Decoder::new(reader)?.decode_image()
45    }
46
47    pub fn decode_with_limits<R: Read>(reader: R, limits: Limits) -> Result<Self> {
48        Decoder::with_limits(reader, limits)?.decode_image()
49    }
50
51    pub fn info(&self) -> &FlifInfo {
52        &self.info
53    }
54
55    pub fn raw(&self) -> &Box<[u8]> {
56        &self.raw
57    }
58
59    pub fn into_raw(self) -> Box<[u8]> {
60        self.raw
61    }
62}
63
64/// Limits on input images to prevent OOM based DoS
65#[derive(Copy, Clone, Debug, Eq, PartialEq)]
66pub struct Limits {
67    /// max size of the compressed metadata in bytes (default: 1 MB)
68    pub metadata_chunk: u32,
69    /// max number of metadata entries (default: 8)
70    pub metadata_count: u32,
71    /// max number of pixels: `width * height * frames` (default: 67M = 2<sup>26</sup>)
72    pub pixels: u64,
73    /// max number of MANIAC nodes (default: 16384 = 2<sup>14</sup>)
74    pub maniac_nodes: u32,
75}
76
77impl Default for Limits {
78    fn default() -> Self {
79        Self {
80            metadata_chunk: 1 << 20,
81            metadata_count: 8,
82            pixels: 1 << 26,
83            maniac_nodes: 1 << 14,
84        }
85    }
86}
87
88#[derive(Debug)]
89pub struct FlifInfo {
90    pub header: Header,
91    pub metadata: Vec<Metadata>,
92    pub second_header: SecondHeader,
93    transform: Box<Transform>,
94}