zencodec/lib.rs
1//! Shared traits and types for zen* image codecs.
2//!
3//! This crate defines the common API surface that all zen* codecs implement.
4//!
5//! # Module organization
6//!
7//! - [`encode`] — encoder traits, dyn dispatch, output types
8//! - [`decode`] — decoder traits, streaming/animation decode, dyn dispatch, output types
9//! - Root — shared types used by both encode and decode paths
10//!
11//! # Shared types (root)
12//!
13//! - [`ImageFormat`] — format detection from magic bytes
14//! - [`ImageInfo`] / [`Metadata`] / [`Orientation`] / [`OrientationHint`] — image metadata
15//! - [`ResourceLimits`] / [`ThreadingPolicy`] — resource limit and threading configuration
16//! - [`UnsupportedOperation`] / [`CodecErrorExt`] — standard unsupported operation reporting and error chain inspection
17//!
18//! # Re-exported crates
19//!
20//! The [`enough`] crate is re-exported for cooperative cancellation
21//! (`enough::Stop`).
22//!
23//! ```rust,ignore
24//! use enough::Stop;
25//! ```
26//!
27//! Individual codecs (zenjpeg, zenwebp, zengif, zenavif) implement the
28//! [`encode`] and [`decode`] traits on their own config types.
29//! Format-specific methods live on the concrete types, not on the traits.
30//!
31//! `zencodecs` provides multi-format dispatch and convenience entry points.
32
33#![no_std]
34#![forbid(unsafe_code)]
35
36extern crate alloc;
37
38whereat::define_at_crate_info!();
39
40mod capabilities;
41mod cost;
42mod detect;
43mod error;
44mod extensions;
45mod format;
46/// Cross-codec gain map types (ISO 21496-1).
47pub mod gainmap;
48/// Codec implementation helpers (not consumer API).
49pub mod helpers;
50mod info;
51mod limits;
52mod metadata;
53mod negotiate;
54mod orientation;
55mod output;
56mod policy;
57mod sink;
58mod traits;
59
60// =========================================================================
61// Public root: shared types used by both encode and decode
62// =========================================================================
63
64pub use extensions::Extensions;
65pub use format::{ImageFormat, ImageFormatDefinition, ImageFormatRegistry};
66pub use gainmap::{GainMapChannel, GainMapDirection, GainMapInfo, GainMapParams, GainMapPresence};
67pub use info::{
68 Cicp, ContentLightLevel, ImageInfo, ImageSequence, MasteringDisplay, Resolution,
69 ResolutionUnit, SourceColor, Supplements,
70};
71pub use limits::{LimitExceeded, ResourceLimits, ThreadingPolicy};
72pub use metadata::Metadata;
73pub use orientation::{Orientation, OrientationHint};
74pub use output::{AnimationFrame, OwnedAnimationFrame};
75
76pub use capabilities::UnsupportedOperation;
77pub use detect::SourceEncodingDetails;
78pub use error::{CodecErrorExt, find_cause};
79pub use traits::Unsupported;
80
81// =========================================================================
82// Crate-level re-exports (qualified access, not individual types)
83// =========================================================================
84//
85/// Owned, clonable, type-erased stop token.
86///
87/// Re-exported from [`almost_enough::StopToken`]. Wraps any `Stop` in an
88/// enum that avoids vtable dispatch for `Stopper`/`SyncStopper`/`Unstoppable`,
89/// collapses nested tokens, and is `Clone + Send + Sync + 'static`.
90pub use almost_enough::StopToken;
91pub use enough;
92pub use enough::Unstoppable;
93
94// =========================================================================
95// pub(crate) re-exports — keep internal `use crate::Foo` paths working
96// for items that moved out of the public root into sub-modules.
97// =========================================================================
98
99pub(crate) use capabilities::{DecodeCapabilities, EncodeCapabilities};
100pub(crate) use cost::OutputInfo;
101pub(crate) use output::{DecodeOutput, EncodeOutput};
102pub(crate) use policy::{DecodePolicy, EncodePolicy};
103pub(crate) use sink::DecodeRowSink;
104
105// =========================================================================
106// Public sub-modules
107// =========================================================================
108
109/// Encode traits, types, and configuration.
110///
111/// # Trait hierarchy
112///
113/// ```text
114/// ┌→ Enc (implements Encoder)
115/// EncoderConfig → EncodeJob ──────┤
116/// └→ AnimationFrameEnc (implements AnimationFrameEncoder)
117/// ```
118///
119/// # Object-safe dyn dispatch
120///
121/// ```text
122/// DynEncoderConfig → DynEncodeJob → DynEncoder / DynAnimationFrameEncoder
123/// ```
124///
125/// Codec implementors implement the generic traits. Dispatch callers
126/// use the `Dyn*` variants for codec-agnostic operation.
127pub mod encode {
128 // Traits — config, job, execution
129 pub use crate::traits::{AnimationFrameEncoder, EncodeJob, Encoder, EncoderConfig};
130
131 // Object-safe dyn dispatch
132 pub use crate::traits::{
133 BoxedError, DynAnimationFrameEncoder, DynEncodeJob, DynEncoder, DynEncoderConfig,
134 };
135
136 // Types
137 pub use crate::capabilities::EncodeCapabilities;
138 pub use crate::negotiate::best_encode_format;
139 pub use crate::output::EncodeOutput;
140 pub use crate::policy::EncodePolicy;
141}
142
143/// Decode traits, types, and configuration.
144///
145/// # Trait hierarchy
146///
147/// ```text
148/// ┌→ Dec (implements Decode)
149/// DecoderConfig → DecodeJob<'a> ──┤→ StreamDec (implements StreamingDecode)
150/// └→ AnimationFrameDec (implements AnimationFrameDecoder)
151/// ```
152///
153/// # Object-safe dyn dispatch
154///
155/// ```text
156/// DynDecoderConfig → DynDecodeJob → DynDecoder / DynAnimationFrameDecoder / DynStreamingDecoder
157/// ```
158///
159/// Codec implementors implement the generic traits. Dispatch callers
160/// use the `Dyn*` variants for codec-agnostic operation.
161pub mod decode {
162 // Traits — config, job, execution
163 pub use crate::traits::{
164 AnimationFrameDecoder, Decode, DecodeJob, DecoderConfig, StreamingDecode,
165 };
166
167 // Object-safe dyn dispatch
168 pub use crate::traits::{
169 BoxedError, DynAnimationFrameDecoder, DynDecodeJob, DynDecoder, DynDecoderConfig,
170 DynStreamingDecoder,
171 };
172
173 // Types
174 pub use crate::capabilities::DecodeCapabilities;
175 pub use crate::cost::OutputInfo;
176 pub use crate::output::{AnimationFrame, DecodeOutput, OwnedAnimationFrame};
177 pub use crate::policy::DecodePolicy;
178 pub use crate::sink::{DecodeRowSink, SinkError};
179
180 pub use crate::negotiate::{is_format_available, negotiate_pixel_format};
181
182 // Source encoding detection
183 pub use crate::detect::SourceEncodingDetails;
184
185 // Shared types re-exported for convenience (commonly needed alongside decode)
186 pub use crate::info::{EmbeddedMetadata, SourceColor};
187}