image_canvas/lib.rs
1//! An opinionated in-memory buffer for image data.
2//!
3//! # General Usage
4//!
5//! Let us start by creating a simple RgbA buffer. For this we need to:
6//!
7//! 1. Specify the texel type with the right depth and channels.
8//! 2. Define the layout, a plain matrix with width and height
9//! 3. Allocate the frame with the layout
10//!
11//! [1]: https://crates.io/crates/rgb
12//! [2]: https://crates.io/crates/palette
13//!
14//! ```
15//! use image_canvas::Canvas;
16//! use image_canvas::layout::{CanvasLayout, SampleParts, Texel};
17//!
18//! // Define what type of color we want to store...
19//! let texel = Texel::new_u8(SampleParts::RgbA);
20//! // and which dimensions to use, chooses a stride for us.
21//! let layout = CanvasLayout::with_texel(&texel, 32, 32)?;
22//!
23//! let frame = Canvas::new(layout);
24//! # use image_canvas::layout::LayoutError;
25//! # Ok::<(), LayoutError>(())
26//! ```
27//!
28//! Converting to a different color is also possible:
29//! 1. Explicitly assign a fitting `Color` to source and target
30//! 2. Call the conversion method.
31//!
32//! ```
33//! use image_canvas::Canvas;
34//! use image_canvas::color::Color;
35//! use image_canvas::layout::{CanvasLayout, SampleParts, Texel};
36//!
37//! let layout = CanvasLayout::with_texel(&Texel::new_u8(SampleParts::Lab), 32, 32)?;
38//! let mut from = Canvas::new(layout.clone());
39//! from.set_color(Color::Oklab)?;
40//!
41//! let layout = CanvasLayout::with_texel(&Texel::new_u8(SampleParts::Rgb), 32, 32)?;
42//! let mut into = Canvas::new(layout);
43//! into.set_color(Color::SRGB)?;
44//!
45//! // … omitted: some pixel initialization
46//! from.convert(&mut into);
47//!
48//!// Now read the sRGB frame, e.g. to initialize an HTTP canvas
49//! into.as_bytes();
50//!
51//! # use image_canvas::layout::LayoutError;
52//! # Ok::<(), LayoutError>(())
53//! ```
54// Deny, not forbid, unsafe code. In `arch` module we have inherently unsafe code, for the moment.
55// Maybe at a future point we gain some possibility to write such code safely.
56#![deny(unsafe_code)]
57// Be std for doctests, avoids a weird warning about missing allocator.
58#![cfg_attr(not(doctest), no_std)]
59
60#[cfg(feature = "runtime-features")]
61extern crate std;
62
63#[macro_use]
64extern crate alloc;
65
66mod arch;
67mod bits;
68/// Putting it all together with a buffer type.
69pub mod color;
70mod color_matrix;
71/// The main frame module.
72mod frame;
73/// The layout implementation, builders, descriptors.
74pub mod layout;
75/// Conversion operation.
76mod shader;
77
78#[cfg(test)]
79mod tests;
80
81pub use self::frame::{Canvas, Plane};
82
83pub use self::shader::{ConversionError, Converter, ConverterPlaneHandle, ConverterRun};
84
85pub mod canvas {
86 pub use crate::frame::{
87 ArcCanvas, BytePlaneAtomics, BytePlaneCells, BytePlaneMut, BytePlaneRef,
88 BytePlaneRef as BytePlane, ChannelsMut, ChannelsRef, PlaneMut, PlaneRef, RcCanvas,
89 };
90}
91
92#[doc(hidden)]
93pub use self::canvas::{PlaneMut, PlaneRef};