leptonica 0.1.0

Rust port of Leptonica image processing library
Documentation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Color segmentation
//!
//! Unsupervised color segmentation for reducing an image to a small
//! number of colors. This is useful for image analysis, document
//! processing, and artistic effects.
//!
//! The algorithm proceeds in 4 phases:
//! 1. **Cluster**: Greedy assignment of pixels to color clusters
//! 2. **Refine**: Reassign pixels to nearest cluster average
//! 3. **Clean**: Morphological cleanup (optional)
//! 4. **Reduce**: Remove unpopular colors

use crate::color::{ColorError, ColorResult};
use crate::core::{Pix, PixColormap, PixelDepth, pixel};

// =============================================================================
// Constants
// =============================================================================

/// Maximum allowed iterations when expanding distance
const MAX_ALLOWED_ITERATIONS: u32 = 20;

/// Factor by which max_dist is increased on each iteration
const DIST_EXPAND_FACTOR: f32 = 1.3;

// =============================================================================
// Options
// =============================================================================

/// Options for color segmentation
///
/// The parameters interact as follows:
/// - `max_dist` controls how similar colors must be to join a cluster
/// - `max_colors` limits Phase 1 output (should be ~2x `final_colors`)
/// - `final_colors` is the target number of colors after Phase 4
///
/// # Guidelines
///
/// | final_colors | max_colors | max_dist |
/// |--------------|------------|----------|
/// | 3            | 6          | 100      |
/// | 4            | 8          | 90       |
/// | 5            | 10         | 75       |
/// | 6            | 12         | 60       |
#[derive(Debug, Clone)]
pub struct ColorSegmentOptions {
    /// Maximum Euclidean distance to existing cluster (Phase 1)
    ///
    /// Lower values create more clusters (more colors).
    /// Higher values merge more colors together.
    /// Range: typically 60-100.
    pub max_dist: u32,

    /// Maximum number of colors in Phase 1
    ///
    /// If exceeded, `max_dist` is automatically increased.
    /// Should be larger than `final_colors`, typically 2x.
    pub max_colors: u32,

    /// Linear size of structuring element for morphological cleanup (Phase 3)
    ///
    /// Set to 0 or 1 to skip cleanup phase.
    /// Larger values remove more noise but may lose detail.
    pub sel_size: u32,

    /// Maximum number of colors after Phase 4
    ///
    /// Unpopular colors are merged into similar popular colors.
    pub final_colors: u32,
}

impl Default for ColorSegmentOptions {
    fn default() -> Self {
        Self {
            max_dist: 75,
            max_colors: 10,
            sel_size: 4,
            final_colors: 5,
        }
    }
}

impl ColorSegmentOptions {
    /// Create options for a target number of final colors
    ///
    /// Uses the guidelines from the original Leptonica implementation.
    pub fn for_colors(final_colors: u32) -> Self {
        let (max_colors, max_dist) = match final_colors {
            1..=3 => (6, 100),
            4 => (8, 90),
            5 => (10, 75),
            _ => (final_colors * 2, 60),
        };
        Self {
            max_dist,
            max_colors,
            sel_size: 4,
            final_colors,
        }
    }
}

// =============================================================================
// Main API
// =============================================================================

/// Perform unsupervised color segmentation
///
/// This is the full 4-phase color segmentation algorithm.
/// Returns an 8-bpp image with a colormap.
///
/// # Arguments
///
/// * `pix` - 32-bpp RGB input image
/// * `options` - Segmentation parameters
///
/// # Returns
///
/// An 8-bpp colormapped image with at most `final_colors` colors.
///
/// # Example
///
/// ```no_run
/// use leptonica::color::segment::{color_segment, ColorSegmentOptions};
/// use leptonica::core::Pix;
///
/// let pix = Pix::new(100, 100, leptonica::core::PixelDepth::Bit32).unwrap();
/// let options = ColorSegmentOptions::for_colors(5);
/// let segmented = color_segment(&pix, &options).unwrap();
/// ```
pub fn color_segment(pix: &Pix, options: &ColorSegmentOptions) -> ColorResult<Pix> {
    // Validate input
    if pix.depth() != PixelDepth::Bit32 {
        return Err(ColorError::UnsupportedDepth {
            expected: "32 bpp",
            actual: pix.depth().bits(),
        });
    }

    if options.max_colors == 0 || options.max_colors > 256 {
        return Err(ColorError::InvalidParameters(
            "max_colors must be between 1 and 256".into(),
        ));
    }

    if options.final_colors == 0 || options.final_colors > options.max_colors {
        return Err(ColorError::InvalidParameters(
            "final_colors must be between 1 and max_colors".into(),
        ));
    }

    // Phase 1: Initial clustering
    let pix_clustered = color_segment_cluster(pix, options.max_dist, options.max_colors)?;

    // Phase 2: Refinement - reassign to nearest cluster average
    let pix_refined = Pix::new(pix.width(), pix.height(), PixelDepth::Bit8)?;
    let mut refined_mut = pix_refined.try_into_mut().unwrap();

    // Copy colormap from clustered result
    let colormap = pix_clustered
        .colormap()
        .ok_or_else(|| ColorError::InvalidParameters("clustered result has no colormap".into()))?;
    refined_mut.set_colormap(Some(colormap.clone()))?;

    let counts = assign_to_nearest_color(&mut refined_mut, pix, &pix_clustered, None)?;

    // Phase 3: Morphological cleanup (simplified - skip for now)
    // Full implementation would require morphological operations

    // Phase 4: Remove unpopular colors
    let refined_pix: Pix = refined_mut.into();
    let final_pix = color_segment_remove_colors(&refined_pix, pix, options.final_colors, &counts)?;

    Ok(final_pix)
}

/// Simple color segmentation with default parameters
///
/// Convenience function that uses recommended parameters for the
/// target number of colors.
///
/// # Arguments
///
/// * `pix` - 32-bpp RGB input image
/// * `final_colors` - Target number of colors (1-256)
///
/// # Example
///
/// ```no_run
/// use leptonica::color::segment::color_segment_simple;
/// use leptonica::core::Pix;
///
/// let pix = Pix::new(100, 100, leptonica::core::PixelDepth::Bit32).unwrap();
/// let segmented = color_segment_simple(&pix, 5).unwrap();
/// ```
pub fn color_segment_simple(pix: &Pix, final_colors: u32) -> ColorResult<Pix> {
    let options = ColorSegmentOptions::for_colors(final_colors);
    color_segment(pix, &options)
}

/// Perform greedy color clustering (Phase 1)
///
/// This is the first phase of color segmentation. Pixels are assigned
/// to clusters greedily: each pixel either joins an existing cluster
/// (if within `max_dist`) or starts a new cluster.
///
/// If the number of clusters exceeds `max_colors`, the distance is
/// automatically increased and clustering is retried.
///
/// # Arguments
///
/// * `pix` - 32-bpp RGB input image
/// * `max_dist` - Maximum Euclidean distance to join a cluster
/// * `max_colors` - Maximum number of clusters allowed
///
/// # Returns
///
/// An 8-bpp colormapped image where the colormap contains cluster
/// average colors.
pub fn color_segment_cluster(pix: &Pix, max_dist: u32, max_colors: u32) -> ColorResult<Pix> {
    if pix.depth() != PixelDepth::Bit32 {
        return Err(ColorError::UnsupportedDepth {
            expected: "32 bpp",
            actual: pix.depth().bits(),
        });
    }

    if max_colors == 0 || max_colors > 256 {
        return Err(ColorError::InvalidParameters(
            "max_colors must be between 1 and 256".into(),
        ));
    }

    // Try clustering with increasing distance until it succeeds
    let mut current_dist = max_dist;
    for _ in 0..MAX_ALLOWED_ITERATIONS {
        match cluster_try(pix, current_dist, max_colors) {
            Ok(result) => return Ok(result),
            Err(ClusterError::TooManyColors) => {
                // Increase distance and retry
                current_dist = (current_dist as f32 * DIST_EXPAND_FACTOR) as u32;
            }
            Err(ClusterError::Other(e)) => return Err(e),
        }
    }

    Err(ColorError::QuantizationError(format!(
        "failed to cluster after {} iterations (final dist={})",
        MAX_ALLOWED_ITERATIONS, current_dist
    )))
}

/// Assign pixels to the nearest color in the colormap
///
/// This is Phase 2 of color segmentation. Each pixel in `src` is
/// assigned to the nearest color in the colormap of `dest`.
///
/// # Arguments
///
/// * `dest` - 8-bpp output image with colormap (modified in place)
/// * `src` - 32-bpp RGB source image
/// * `reference` - 8-bpp reference image with colormap to use
/// * `mask` - Optional 1-bpp mask (only fg pixels are processed)
///
/// # Returns
///
/// A vector of pixel counts per colormap index.
pub fn assign_to_nearest_color(
    dest: &mut crate::core::PixMut,
    src: &Pix,
    reference: &Pix,
    mask: Option<&Pix>,
) -> ColorResult<Vec<u32>> {
    let colormap = reference
        .colormap()
        .ok_or_else(|| ColorError::InvalidParameters("reference image has no colormap".into()))?;

    let w = src.width();
    let h = src.height();

    if dest.width() != w || dest.height() != h {
        return Err(ColorError::InvalidParameters(
            "source and dest dimensions must match".into(),
        ));
    }

    if let Some(m) = mask
        && (m.width() != w || m.height() != h)
    {
        return Err(ColorError::InvalidParameters(
            "mask dimensions must match source".into(),
        ));
    }

    let mut counts = vec![0u32; colormap.len()];

    for y in 0..h {
        for x in 0..w {
            // Check mask if present
            if let Some(m) = mask {
                let mask_val = m.get_pixel_unchecked(x, y);
                if mask_val == 0 {
                    continue;
                }
            }

            let pixel = src.get_pixel_unchecked(x, y);
            let (r, g, b) = pixel::extract_rgb(pixel);

            // Find nearest color in colormap
            let idx = colormap.find_nearest(r, g, b).unwrap_or(0);
            dest.set_pixel_unchecked(x, y, idx as u32);
            counts[idx] += 1;
        }
    }

    Ok(counts)
}

// =============================================================================
// Internal Implementation
// =============================================================================

/// Internal error type for clustering
enum ClusterError {
    TooManyColors,
    Other(ColorError),
}

/// Attempt to cluster pixels with given parameters
fn cluster_try(pix: &Pix, max_dist: u32, max_colors: u32) -> Result<Pix, ClusterError> {
    let w = pix.width();
    let h = pix.height();
    let max_dist_sq = (max_dist as i64) * (max_dist as i64);

    // Create output image
    let pix_out = Pix::new(w, h, PixelDepth::Bit8).map_err(|e| ClusterError::Other(e.into()))?;
    let mut out_mut = pix_out.try_into_mut().unwrap();

    // Cluster tracking
    // rmap/gmap/bmap: current cluster representative colors
    // rsum/gsum/bsum/counts: for computing averages
    let mut rmap: Vec<u8> = Vec::with_capacity(max_colors as usize);
    let mut gmap: Vec<u8> = Vec::with_capacity(max_colors as usize);
    let mut bmap: Vec<u8> = Vec::with_capacity(max_colors as usize);
    let mut rsum: Vec<u64> = Vec::with_capacity(max_colors as usize);
    let mut gsum: Vec<u64> = Vec::with_capacity(max_colors as usize);
    let mut bsum: Vec<u64> = Vec::with_capacity(max_colors as usize);
    let mut counts: Vec<u64> = Vec::with_capacity(max_colors as usize);

    for y in 0..h {
        for x in 0..w {
            let pixel = pix.get_pixel_unchecked(x, y);
            let (r, g, b) = pixel::extract_rgb(pixel);

            // Try to find an existing cluster within max_dist
            let mut found = false;
            let ncolors = rmap.len();

            for k in 0..ncolors {
                let dr = r as i64 - rmap[k] as i64;
                let dg = g as i64 - gmap[k] as i64;
                let db = b as i64 - bmap[k] as i64;
                let dist_sq = dr * dr + dg * dg + db * db;

                if dist_sq <= max_dist_sq {
                    // Assign to this cluster
                    out_mut.set_pixel_unchecked(x, y, k as u32);
                    rsum[k] += r as u64;
                    gsum[k] += g as u64;
                    bsum[k] += b as u64;
                    counts[k] += 1;
                    found = true;
                    break;
                }
            }

            if !found {
                // Need to create a new cluster
                if ncolors >= max_colors as usize {
                    return Err(ClusterError::TooManyColors);
                }

                let idx = ncolors;
                rmap.push(r);
                gmap.push(g);
                bmap.push(b);
                rsum.push(r as u64);
                gsum.push(g as u64);
                bsum.push(b as u64);
                counts.push(1);
                out_mut.set_pixel_unchecked(x, y, idx as u32);
            }
        }
    }

    // Create colormap with average colors
    let mut colormap = PixColormap::new(8).map_err(|e| ClusterError::Other(e.into()))?;

    for k in 0..rmap.len() {
        let count = counts[k];
        if count > 0 {
            let avg_r = (rsum[k] / count) as u8;
            let avg_g = (gsum[k] / count) as u8;
            let avg_b = (bsum[k] / count) as u8;
            colormap
                .add_rgb(avg_r, avg_g, avg_b)
                .map_err(|e| ClusterError::Other(e.into()))?;
        }
    }

    out_mut
        .set_colormap(Some(colormap))
        .map_err(|e| ClusterError::Other(e.into()))?;

    Ok(out_mut.into())
}

/// Remove unpopular colors (Phase 4)
///
/// Returns a new Pix with only the most popular colors.
fn color_segment_remove_colors(
    pix_dest: &Pix,
    _pix_src: &Pix,
    final_colors: u32,
    counts: &[u32],
) -> ColorResult<Pix> {
    let colormap = pix_dest
        .colormap()
        .ok_or_else(|| ColorError::InvalidParameters("dest image has no colormap".into()))?;

    let ncolors = colormap.len();
    if ncolors <= final_colors as usize {
        // Already few enough colors, return clone
        return Ok(pix_dest.clone());
    }

    // Sort colors by popularity (descending)
    let mut indices: Vec<usize> = (0..ncolors).collect();
    indices.sort_by(|a, b| counts[*b].cmp(&counts[*a]));

    // Build mapping from old index to new index
    // Colors not in keep_set get mapped to nearest kept color
    let mut index_map: Vec<u8> = vec![0; ncolors];
    let mut new_colormap = PixColormap::new(8)?;

    // First, add the kept colors
    for (new_idx, &old_idx) in indices[..final_colors as usize].iter().enumerate() {
        let (r, g, b) = colormap.get_rgb(old_idx).unwrap();
        new_colormap.add_rgb(r, g, b)?;
        index_map[old_idx] = new_idx as u8;
    }

    // Map removed colors to nearest kept color
    for &old_idx in &indices[final_colors as usize..] {
        let (r, g, b) = colormap.get_rgb(old_idx).unwrap();

        // Find nearest color among kept colors
        let mut min_dist = i64::MAX;
        let mut best_new_idx = 0;

        for (new_idx, &kept_idx) in indices[..final_colors as usize].iter().enumerate() {
            let (kr, kg, kb) = colormap.get_rgb(kept_idx).unwrap();
            let dr = r as i64 - kr as i64;
            let dg = g as i64 - kg as i64;
            let db = b as i64 - kb as i64;
            let dist = dr * dr + dg * dg + db * db;
            if dist < min_dist {
                min_dist = dist;
                best_new_idx = new_idx;
            }
        }

        index_map[old_idx] = best_new_idx as u8;
    }

    // Remap pixels
    let pix_out = Pix::new(pix_dest.width(), pix_dest.height(), PixelDepth::Bit8)?;
    let mut out_mut = pix_out.try_into_mut().unwrap();
    out_mut.set_colormap(Some(new_colormap))?;

    for y in 0..pix_dest.height() {
        for x in 0..pix_dest.width() {
            let old_idx = pix_dest.get_pixel_unchecked(x, y) as usize;
            let new_idx = if old_idx < index_map.len() {
                index_map[old_idx]
            } else {
                0
            };
            out_mut.set_pixel_unchecked(x, y, new_idx as u32);
        }
    }

    Ok(out_mut.into())
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    /// Create a test image with distinct color regions
    fn create_test_image() -> Pix {
        let pix = Pix::new(60, 60, PixelDepth::Bit32).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();

        for y in 0..60 {
            for x in 0..60 {
                let pixel = if x < 20 {
                    pixel::compose_rgb(255, 0, 0) // Red
                } else if x < 40 {
                    pixel::compose_rgb(0, 255, 0) // Green
                } else {
                    pixel::compose_rgb(0, 0, 255) // Blue
                };
                pix_mut.set_pixel_unchecked(x, y, pixel);
            }
        }

        pix_mut.into()
    }

    /// Create a gradient image
    fn create_gradient_image() -> Pix {
        let pix = Pix::new(64, 64, PixelDepth::Bit32).unwrap();
        let mut pix_mut = pix.try_into_mut().unwrap();

        for y in 0..64 {
            for x in 0..64 {
                let r = (x * 4) as u8;
                let g = (y * 4) as u8;
                let b = 128;
                let pixel = pixel::compose_rgb(r, g, b);
                pix_mut.set_pixel_unchecked(x, y, pixel);
            }
        }

        pix_mut.into()
    }

    #[test]
    fn test_color_segment_simple_colors() {
        let pix = create_test_image();
        let result = color_segment_simple(&pix, 3).unwrap();

        assert_eq!(result.depth(), PixelDepth::Bit8);
        assert!(result.colormap().is_some());

        let cmap = result.colormap().unwrap();
        assert!(cmap.len() <= 3);
    }

    #[test]
    fn test_color_segment_gradient() {
        let pix = create_gradient_image();
        let result = color_segment_simple(&pix, 5).unwrap();

        assert_eq!(result.depth(), PixelDepth::Bit8);
        assert!(result.colormap().is_some());

        let cmap = result.colormap().unwrap();
        assert!(cmap.len() <= 5);
    }

    #[test]
    fn test_cluster_phase_only() {
        let pix = create_test_image();
        let result = color_segment_cluster(&pix, 100, 10).unwrap();

        assert_eq!(result.depth(), PixelDepth::Bit8);
        assert!(result.colormap().is_some());

        // With distinct colors and large max_dist, should get ~3 clusters
        let cmap = result.colormap().unwrap();
        assert!(cmap.len() <= 10);
    }

    #[test]
    fn test_wrong_depth() {
        let pix = Pix::new(10, 10, PixelDepth::Bit8).unwrap();

        let result = color_segment_simple(&pix, 5);
        assert!(result.is_err());

        let result = color_segment_cluster(&pix, 75, 10);
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_params() {
        let pix = create_test_image();

        // max_colors = 0
        let result = color_segment_cluster(&pix, 75, 0);
        assert!(result.is_err());

        // max_colors > 256
        let result = color_segment_cluster(&pix, 75, 257);
        assert!(result.is_err());
    }

    #[test]
    fn test_options_for_colors() {
        let opts = ColorSegmentOptions::for_colors(3);
        assert_eq!(opts.final_colors, 3);
        assert_eq!(opts.max_colors, 6);
        assert_eq!(opts.max_dist, 100);

        let opts = ColorSegmentOptions::for_colors(5);
        assert_eq!(opts.final_colors, 5);
        assert_eq!(opts.max_colors, 10);
        assert_eq!(opts.max_dist, 75);
    }

    #[test]
    fn test_default_options() {
        let opts = ColorSegmentOptions::default();
        assert_eq!(opts.final_colors, 5);
        assert_eq!(opts.max_colors, 10);
        assert_eq!(opts.max_dist, 75);
        assert_eq!(opts.sel_size, 4);
    }
}