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
//! # Palettize
//!
//! A library for applying ordered Bayer dithering to images with custom color palettes.
//!
//! Ordered dithering is a technique that reduces the number of colors in an image while
//! maintaining visual quality through the use of spatial patterns. This library provides
//! tools to:
//!
//! - Generate Bayer matrices of various sizes
//! - Parse color palettes from hex strings
//! - Apply dithering with customizable parameters
//!
//! ## Quick Start
//!
//! The simplest way to dither an image is with the [`dither()`] function:
//!
//! ```no_run
//! use palettize::{dither, grayscale};
//!
//! // Load an image
//! let img = image::open("input.png").unwrap();
//!
//! // Generate a grayscale palette (black & white)
//! let palette = grayscale(2);
//!
//! // Apply dithering with default settings
//! let output = dither(&img, &palette);
//!
//! // Save the result
//! output.save("output.png").unwrap();
//! ```
//!
//! For more control over dithering parameters, use [`dither_with_options()`]:
//!
//! ```no_run
//! use palettize::{dither_with_options, grayscale};
//!
//! let img = image::open("input.png").unwrap();
//! let palette = grayscale(6); // 6-level grayscale
//!
//! // Bayer level 3 = 16×16 matrix, noise 0.5 = subtle dithering
//! let output = dither_with_options(&img, &palette, 3, 0.5);
//! output.save("output.png").unwrap();
//! ```
//!
//! ## Custom Palettes
//!
//! You can create palettes from custom colors in several ways:
//!
//! ```
//! use palettize::{Color, Palette, parse_hex_color};
//!
//! // From Color structs
//! let palette = Palette::new(vec![
//! Color::new(0, 0, 0),
//! Color::new(255, 255, 255),
//! ]);
//!
//! // From tuples using Into
//! let palette: Palette = vec![
//! (0, 0, 0),
//! (255, 255, 255),
//! ].into();
//!
//! // From hex color strings
//! let palette = Palette::new(vec![
//! parse_hex_color("#1a1c2c").unwrap(),
//! parse_hex_color("#5d275d").unwrap(),
//! parse_hex_color("#b13e53").unwrap(),
//! ]);
//! ```
//!
//! ## Grayscale Palettes
//!
//! Use [`grayscale()`] to generate evenly-spaced grayscale palettes:
//!
//! - `grayscale(2)` - Black and white
//! - `grayscale(6)` - 6-level grayscale
//! - `grayscale(256)` - Full 8-bit grayscale
//!
//! ## Palette Remapping
//!
//! By default, dithering picks the palette color nearest to each pixel. That is
//! a colorimetric match. [`remap_to_palette()`] is the perceptual-intent
//! alternative: it first matches the image's lightness and chroma distribution
//! to the palette's distribution in Oklab, then you dither the result.
//!
//! Use it when an image is washed out or its colors sit far from the palette.
//! The remap spreads the image across the whole palette instead of collapsing
//! it onto the few nearest entries.
//!
//! ```no_run
//! use palettize::{dither, remap_to_palette, Palette};
//!
//! let img = image::open("input.png").unwrap();
//! let palette: Palette = vec![
//! (26, 28, 76), // Dark blue
//! (240, 208, 96), // Light yellow
//! ].into();
//!
//! // Match the distribution, then dither the full-color result
//! let remapped = remap_to_palette(&img, &palette, 1.0);
//! let output = dither(&image::DynamicImage::ImageRgb8(remapped), &palette);
//! output.save("output.png").unwrap();
//! ```
//!
//! ## Bayer Matrix Levels
//!
//! The Bayer matrix level controls the size of the dithering pattern:
//!
//! | Level | Matrix Size | Threshold Count |
//! |-------|-------------|-----------------|
//! | 0 | 2×2 | 4 |
//! | 1 | 4×4 | 16 |
//! | 2 | 8×8 | 64 (default) |
//! | 3 | 16×16 | 256 |
//! | 4 | 32×32 | 1024 |
//! | 5 | 64×64 | 4096 |
//!
//! Higher levels produce smoother gradients but larger repeating patterns.
//! Level 2 (8×8) is a good default for most images.
/// The package README is compiled as part of the test suite.
;
use ;
// Re-export main types and functions for convenience
pub use generate_bayer_matrix;
pub use ;
pub use ;
pub use ;
pub use remap_to_palette;
/// Default Bayer matrix level used by [`dither()`].
///
/// Level 2 produces an 8×8 matrix, which provides a good balance between
/// smooth gradients and visible dithering patterns for most images.
pub const DEFAULT_BAYER_LEVEL: u32 = 2;
/// Default noise intensity used by [`dither()`].
///
/// A value of 1.0 provides neutral dithering strength. Lower values (0.0-1.0)
/// produce subtler dithering, while higher values (1.0-2.0) increase contrast.
pub const DEFAULT_NOISE: f32 = 1.0;
/// Dithers an image using the given palette with default settings.
///
/// This is the simplest way to apply ordered Bayer dithering to an image.
/// It uses default parameters: Bayer level 2 (8×8 matrix) and noise intensity 1.0.
///
/// For more control over dithering parameters, use [`dither_with_options()`].
///
/// # Arguments
///
/// * `image` - The input image to dither
/// * `palette` - The color palette to quantize to
///
/// # Returns
///
/// A new [`RgbImage`] with the dithering applied.
///
/// # Examples
///
/// ```no_run
/// use palettize::{dither, grayscale};
///
/// let img = image::open("input.png").unwrap();
/// let palette = grayscale(2); // black & white
/// let output = dither(&img, &palette);
/// output.save("output.png").unwrap();
/// ```
///
/// Using a custom palette:
///
/// ```no_run
/// use palettize::{dither, Color, Palette};
///
/// let img = image::open("input.png").unwrap();
/// let palette: Palette = vec![
/// (0, 0, 0), // Black
/// (255, 255, 255), // White
/// ].into();
/// let output = dither(&img, &palette);
/// ```
/// Dithers an image using the given palette with custom settings.
///
/// This function provides full control over the dithering parameters.
/// For default settings, use [`dither()`] instead.
///
/// # Arguments
///
/// * `image` - The input image to dither
/// * `palette` - The color palette to quantize to
/// * `bayer_level` - The Bayer matrix level (0-5). Controls the size of the
/// dithering pattern. Level 0 = 2×2, level 5 = 64×64. Use 2 as a good default.
/// * `noise` - Dither strength (0.0-2.0). Controls the contrast of the dithering
/// pattern. 0.0 = minimal dithering, 1.0 = neutral, 2.0 = maximum contrast.
///
/// # Returns
///
/// A new [`RgbImage`] with the dithering applied.
///
/// # Bayer Matrix Levels
///
/// | Level | Matrix Size | Effect |
/// |-------|-------------|--------|
/// | 0 | 2×2 | Very coarse dithering |
/// | 1 | 4×4 | Coarse dithering |
/// | 2 | 8×8 | Good default |
/// | 3 | 16×16 | Smooth gradients |
/// | 4 | 32×32 | Very smooth |
/// | 5 | 64×64 | Extremely smooth |
///
/// # Examples
///
/// ```no_run
/// use palettize::{dither_with_options, grayscale};
///
/// let img = image::open("input.png").unwrap();
/// let palette = grayscale(6); // 6-level grayscale
///
/// // Smooth gradients with subtle dithering
/// let output = dither_with_options(&img, &palette, 3, 0.5);
/// output.save("smooth.png").unwrap();
///
/// // Coarse retro look with strong dithering
/// let output = dither_with_options(&img, &palette, 1, 1.5);
/// output.save("retro.png").unwrap();
/// ```