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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//! Buffer-first rust dithering and halftoning library.
//!
//! Quantizing grayscale/RGB/RGBA buffers without dithering creates visible
//! banding and contouring. `dithr` provides deterministic ordered dithering,
//! diffusion, stochastic binary methods, palette-constrained workflows, and
//! advanced halftoning methods over typed mutable slices.
//!
//! ## Overview
//!
//! - **Buffer-first API**: Operates on mutable pixel slices with explicit
//! width/height/stride.
//! - **Typed sample support**: `u8`, `u16`, and `f32`.
//! - **Typed layout support**: Gray, Rgb, and Rgba.
//! - **Shared quantization model**: [`QuantizeMode`] is used across families.
//! - **Algorithm coverage**: Stochastic, ordered, palette-oriented ordered,
//! diffusion, variable diffusion, and advanced halftoning.
//! - **Optional integrations**: `image` adapters and `rayon` parallel wrappers.
//!
//! ## Quick Start
//!
//! ```rust
//! use dithr::{gray_u8, QuantizeMode, Result};
//! use dithr::ordered::bayer_8x8_in_place;
//!
//! fn main() -> Result<()> {
//! let width = 64_usize;
//! let height = 64_usize;
//! let mut data = Vec::with_capacity(width * height);
//!
//! for y in 0..height {
//! for x in 0..width {
//! let value = ((x + y * width) * 255 / (width * height - 1)) as u8;
//! data.push(value);
//! }
//! }
//!
//! let mut buffer = gray_u8(&mut data, width, height, width)?;
//! bayer_8x8_in_place(&mut buffer, QuantizeMode::gray_bits(1)?)?;
//!
//! assert!(data.iter().all(|&v| v == 0 || v == 255));
//! Ok(())
//! }
//! ```
//!
//! ## Core Types and Buffer Model
//!
//! Buffer and format types:
//!
//! - [`Buffer`]
//! - [`BufferKind`] (runtime format metadata)
//! - [`PixelFormat`] (type alias of [`BufferKind`])
//! - [`BufferError`]
//!
//! Generic and typed buffer aliases:
//!
//! - [`GrayBuffer`], [`RgbBuffer`], [`RgbaBuffer`]
//! - [`GrayBuffer8`], [`RgbBuffer8`], [`RgbaBuffer8`]
//! - [`GrayBuffer16`], [`RgbBuffer16`], [`RgbaBuffer16`]
//! - [`GrayBuffer32F`], [`RgbBuffer32F`], [`RgbaBuffer32F`]
//!
//! Constructors:
//!
//! - Gray: [`gray_u8`], [`gray_u16`], [`gray_32f`]
//! - Gray packed: [`gray_u8_packed`], [`gray_u16_packed`], [`gray_32f_packed`]
//! - Rgb: [`rgb_u8`], [`rgb_u16`], [`rgb_32f`]
//! - Rgb packed: [`rgb_u8_packed`], [`rgb_u16_packed`], [`rgb_32f_packed`]
//! - Rgba: [`rgba_u8`], [`rgba_u16`], [`rgba_32f`]
//! - Rgba packed: [`rgba_u8_packed`], [`rgba_u16_packed`], [`rgba_32f_packed`]
//!
//! Runtime kinds covered by [`BufferKind`]:
//!
//! - `Gray8`, `Rgb8`, `Rgba8`
//! - `Gray16`, `Rgb16`, `Rgba16`
//! - `Gray32F`, `Rgb32F`, `Rgba32F`
//!
//! ## Quantization API
//!
//! Quantization mode enum:
//!
//! - [`QuantizeMode::GrayLevels`]
//! - [`QuantizeMode::RgbLevels`]
//! - [`QuantizeMode::Palette`]
//! - [`QuantizeMode::SingleColor`]
//!
//! Quantization constructors/helpers:
//!
//! - [`QuantizeMode::gray_levels`]
//! - [`QuantizeMode::rgb_levels`]
//! - [`QuantizeMode::palette`]
//! - [`QuantizeMode::single_color`]
//! - [`QuantizeMode::gray_bits`] (`u8` specialization)
//! - [`QuantizeMode::rgb_bits`] (`u8` specialization)
//! - [`levels_from_bits`]
//!
//! Quantization functions:
//!
//! - [`quantize_gray`]
//! - [`quantize_rgb`]
//! - [`quantize_pixel`]
//! - [`quantize_error`]
//! - [`quantize_gray_u8`]
//! - [`quantize_rgb_u8`]
//!
//! Error alias:
//!
//! - [`QuantizeError`] (alias of crate [`Error`])
//!
//! ## Palette and Indexed Output API
//!
//! Palette types:
//!
//! - [`Palette`]
//! - [`Palette8`], [`Palette16`], [`Palette32F`]
//! - [`PaletteError`]
//!
//! Indexed image types:
//!
//! - [`IndexedImage`]
//! - [`IndexedImage8`], [`IndexedImage16`], [`IndexedImage32F`]
//!
//! Built-in palette helpers:
//!
//! - [`cga_palette`]
//! - [`grayscale_2`]
//! - [`grayscale_4`]
//! - [`grayscale_16`]
//!
//! ## Algorithm Families
//!
//! Binary stochastic:
//!
//! - [`threshold_binary_in_place`]
//! - [`random_binary_in_place`]
//!
//! Ordered:
//!
//! - Bayer: [`bayer_2x2_in_place`], [`bayer_4x4_in_place`],
//! [`bayer_8x8_in_place`], [`bayer_16x16_in_place`]
//! - Cluster-dot: [`cluster_dot_4x4_in_place`], [`cluster_dot_8x8_in_place`]
//! - Custom map: [`custom_ordered_in_place`]
//! - Void-and-cluster: `ordered::void_and_cluster_in_place`
//! - Adaptive ordered: `ordered::adaptive_ordered_dither_in_place`
//! - Space-filling curve ordered: `ordered::space_filling_curve_ordered_dither_in_place`
//! - Ranked dither: `ordered::ranked_dither_in_place`
//! - Image-based dither screens: `ordered::image_based_dither_screen_in_place`
//! - Polyomino ordered: `ordered::polyomino_ordered_dither_in_place`
//! - Stochastic clustered-dot: `ordered::stochastic_clustered_dot_in_place`
//! - AM/FM hybrid halftoning: `ordered::am_fm_hybrid_halftoning_in_place`
//! - Clustered AM/FM halftoning: `ordered::clustered_am_fm_halftoning_in_place`
//! - Blue-noise multitone dithering: `ordered::blue_noise_multitone_dither_in_place`
//!
//! Palette-oriented ordered (Yliluoma):
//!
//! - [`yliluoma_1_in_place`]
//! - [`yliluoma_2_in_place`]
//! - [`yliluoma_3_in_place`]
//!
//! Diffusion (classic):
//!
//! - [`floyd_steinberg_in_place`]
//! - [`false_floyd_steinberg_in_place`]
//! - [`jarvis_judice_ninke_in_place`]
//! - [`stucki_in_place`]
//! - [`burkes_in_place`]
//! - [`sierra_in_place`]
//! - [`two_row_sierra_in_place`]
//! - [`sierra_lite_in_place`]
//! - [`stevenson_arce_in_place`]
//! - [`atkinson_in_place`]
//!
//! Diffusion (extended):
//!
//! - [`fan_in_place`]
//! - [`shiau_fan_in_place`]
//! - [`shiau_fan_2_in_place`]
//!
//! Diffusion (block):
//!
//! - `diffusion::block_error_diffusion_in_place`
//!
//! Variable diffusion:
//!
//! - [`ostromoukhov_in_place`]
//! - [`zhou_fang_in_place`]
//! - [`hvs_optimized_error_diffusion_in_place`]
//! - [`gradient_based_error_diffusion_in_place`]
//! - [`multiscale_error_diffusion_in_place`]
//! - [`feature_preserving_msed_in_place`]
//! - [`green_noise_msed_in_place`]
//! - [`linear_pixel_shuffling_in_place`]
//! - [`tone_dependent_error_diffusion_in_place`]
//! - [`structure_aware_error_diffusion_in_place`]
//! - [`adaptive_vector_error_diffusion_in_place`]
//! - [`vector_error_diffusion_in_place`]
//! - [`semivector_error_diffusion_in_place`]
//! - [`hierarchical_error_diffusion_in_place`]
//! - `diffusion::mbvq_color_error_diffusion_in_place`
//! - `diffusion::neugebauer_color_error_diffusion_in_place`
//! - `diffusion::multichannel_green_noise_error_diffusion_in_place`
//!
//! Advanced halftoning:
//!
//! - [`riemersma_in_place`]
//! - [`knuth_dot_diffusion_in_place`]
//! - [`optimized_dot_diffusion_in_place`]
//! - [`direct_binary_search_in_place`]
//! - `dbs::clustered_dot_direct_multibit_search_in_place`
//! - `dbs::direct_pattern_control_in_place`
//! - `dbs::hierarchical_colorant_dbs_in_place`
//! - [`lattice_boltzmann_in_place`]
//! - [`electrostatic_halftoning_in_place`]
//! - [`model_based_med_in_place`]
//! - [`least_squares_model_based_in_place`]
//!
//! Scope notes:
//!
//! - [`ostromoukhov_in_place`], [`zhou_fang_in_place`], and
//! [`hvs_optimized_error_diffusion_in_place`], and
//! [`gradient_based_error_diffusion_in_place`], and
//! [`multiscale_error_diffusion_in_place`], and
//! [`feature_preserving_msed_in_place`], and
//! [`green_noise_msed_in_place`], and
//! [`linear_pixel_shuffling_in_place`], and
//! [`tone_dependent_error_diffusion_in_place`], and
//! [`structure_aware_error_diffusion_in_place`] are grayscale-only.
//! - [`adaptive_vector_error_diffusion_in_place`] supports `Rgb` and `Rgba`
//! layouts; alpha is preserved for `Rgba`.
//! - [`vector_error_diffusion_in_place`] and
//! [`semivector_error_diffusion_in_place`] support `Rgb` and `Rgba` layouts;
//! alpha is preserved for `Rgba`.
//! - [`hierarchical_error_diffusion_in_place`] supports `Rgb` and `Rgba`
//! layouts; alpha is preserved for `Rgba`.
//! - `diffusion::mbvq_color_error_diffusion_in_place` and
//! `diffusion::neugebauer_color_error_diffusion_in_place` support `Rgb` and
//! `Rgba` layouts; alpha is preserved for `Rgba`.
//! - `diffusion::multichannel_green_noise_error_diffusion_in_place` supports
//! `Rgb` and `Rgba` layouts; alpha is preserved for `Rgba`.
//! - `diffusion::block_error_diffusion_in_place` is grayscale-only.
//! - `ordered::am_fm_hybrid_halftoning_in_place` and
//! `ordered::clustered_am_fm_halftoning_in_place` are grayscale-only.
//! - `ordered::blue_noise_multitone_dither_in_place` is grayscale-only.
//! - [`direct_binary_search_in_place`], [`lattice_boltzmann_in_place`], and
//! [`electrostatic_halftoning_in_place`] are integer grayscale-only.
//! - [`model_based_med_in_place`] and [`least_squares_model_based_in_place`]
//! are integer grayscale-only.
//! - `dbs::clustered_dot_direct_multibit_search_in_place` is integer
//! grayscale-only.
//! - `dbs::direct_pattern_control_in_place` supports integer `Rgb`/`Rgba`
//! buffers; alpha is preserved for `Rgba`.
//! - `dbs::hierarchical_colorant_dbs_in_place` supports integer `Rgb` buffers.
//!
//! ## Parallel API (`rayon` feature)
//!
//! Parallel ordered wrappers:
//!
//! - `bayer_2x2_in_place_par`
//! - `bayer_4x4_in_place_par`
//! - `bayer_8x8_in_place_par`
//! - `bayer_16x16_in_place_par`
//! - `cluster_dot_4x4_in_place_par`
//! - `cluster_dot_8x8_in_place_par`
//! - `custom_ordered_in_place_par`
//! - `yliluoma_1_in_place_par`
//! - `yliluoma_2_in_place_par`
//! - `yliluoma_3_in_place_par`
//!
//! Parallel stochastic wrappers:
//!
//! - `threshold_binary_in_place_par`
//! - `random_binary_in_place_par`
//!
//! ## Image Adapter API (`image` feature)
//!
//! Adapter enum:
//!
//! - `DynamicImageBuffer`
//! - `Gray8`, `Rgb8`, `Rgba8`
//! - `Gray16`, `Rgb16`, `Rgba16`
//! - `Rgb32F`, `Rgba32F`
//!
//! Typed adapters:
//!
//! - `gray8_image_as_buffer`
//! - `rgb8_image_as_buffer`
//! - `rgba8_image_as_buffer`
//! - `gray16_image_as_buffer`
//! - `rgb16_image_as_buffer`
//! - `rgba16_image_as_buffer`
//! - `rgb32f_image_as_buffer`
//! - `rgba32f_image_as_buffer`
//!
//! Dynamic adapter:
//!
//! - `dynamic_image_as_buffer`
//! - `DynamicImage::ImageLumaA8` is promoted to `DynamicImageBuffer::Rgba8`
//! - `DynamicImage::ImageLumaA16` is promoted to `DynamicImageBuffer::Rgba16`
//!
//! ## Errors and Result Types
//!
//! Crate-level error/result:
//!
//! - [`Error`]
//! - [`Result`]
//!
//! Family-specific error types re-exported at crate root:
//!
//! - [`OrderedError`]
//! - [`PaletteError`]
//! - [`BufferError`]
//!
//! For complete workflows and runnable examples, see `examples/` and the
//! repository README.
pub use ;
pub use ;
pub use ;
pub use ;
pub use OrderedError;
pub use ;
pub use ;