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
#![no_std]
#![warn(missing_docs)]
#![forbid(unsafe_code)]
#![allow(unused_labels)]
//#![allow(unused_imports)]
#![allow(clippy::drop_copy)]
#![allow(clippy::get_first)]
#![allow(non_upper_case_globals)]
#![allow(clippy::upper_case_acronyms)]
#![cfg_attr(docs_rs, feature(doc_cfg))]
#![warn(missing_debug_implementations)]
#![warn(clippy::missing_inline_in_public_items)]

//! A crate to work with image data.

#[cfg(feature = "alloc")]
extern crate alloc;

use pixel_formats::*;

mod ascii_array;
mod error;
mod util;

mod borrowed_bitmap;
pub use self::{borrowed_bitmap::BorrowedBitmap, error::*};

#[cfg(feature = "alloc")]
#[cfg_attr(docs_rs, doc(cfg(feature = "alloc")))]
mod alloc_bitmap;
#[cfg(feature = "alloc")]
#[cfg_attr(docs_rs, doc(cfg(feature = "alloc")))]
pub use self::alloc_bitmap::{Bitmap, Palmap};

#[cfg(feature = "png")]
#[cfg_attr(docs_rs, doc(cfg(feature = "png")))]
pub mod png;

#[cfg(feature = "bmp")]
#[cfg_attr(docs_rs, doc(cfg(feature = "bmp")))]
pub mod bmp;

#[cfg(feature = "netpbm")]
#[cfg_attr(docs_rs, doc(cfg(feature = "netpbm")))]
pub mod netpbm;

/// sRGB Intent for an image.
///
/// Unless you're able to color correct, the exact value doesn't really matter.
/// However, knowing that image data is sRGB or not *at all* can be helpful.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[allow(nonstandard_style)]
#[allow(missing_docs)]
pub enum sRGBIntent {
  Perceptual,
  RelativeColorimetric,
  Saturation,
  AbsoluteColorimetric,
}

/// Automatically allocate and fill in a [Bitmap](crate::Bitmap).
///
/// This will try every format compiled into the library until one of them
/// works, or will return a parse error if no format works. The order of trying
/// each format is unspecified, but that basically doesn't matter because you
/// can't really have a file that successfully parses as more than one format at
/// the same time.
///
/// The output image will automatically be vertically flipped as necessary to
/// respect the `origin_top_left` value given.
///
/// The output image data will use pre-multiplied alpha.
#[inline]
#[cfg(feature = "alloc")]
#[cfg_attr(docs_rs, doc(cfg(feature = "alloc")))]
pub fn try_bitmap_rgba<P>(
  bytes: &[u8], origin_top_left: bool,
) -> Result<crate::Bitmap<P>, ImagineError>
where
  P: Copy + From<r32g32b32a32_Sfloat>,
{
  #[cfg(feature = "png")]
  if let Ok(bitmap) = png::png_try_bitmap_rgba(bytes, origin_top_left) {
    return Ok(bitmap);
  }
  #[cfg(feature = "bmp")]
  if let Ok(bitmap) = bmp::bmp_try_bitmap_rgba(bytes, origin_top_left) {
    return Ok(bitmap);
  }
  #[cfg(feature = "netpbm")]
  if let Ok(bitmap) = netpbm::netpbm_try_bitmap_rgba(bytes, origin_top_left) {
    return Ok(bitmap);
  }
  Err(ImagineError::Parse)
}