leptonica 0.4.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
//! Regression test parameters and operations

use super::error::{TestError, TestResult};
use super::{golden_dir, regout_dir};
use leptonica::Pix;
use leptonica::io::ImageFormat;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::sync::{Mutex, OnceLock};

/// Regression test mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RegTestMode {
    /// Generate golden files
    Generate,
    /// Compare with golden files (default)
    #[default]
    Compare,
    /// Display mode - run without comparison
    Display,
}

impl RegTestMode {
    /// Parse mode from environment variable or string
    pub fn from_env() -> Self {
        match std::env::var("REGTEST_MODE")
            .unwrap_or_default()
            .to_lowercase()
            .as_str()
        {
            "generate" => Self::Generate,
            "display" => Self::Display,
            _ => Self::Compare,
        }
    }
}

// --- Content hash functions (FNV-1a, no external dependencies) ---

const FNV_OFFSET_BASIS: u64 = 0xcbf29ce484222325;
const FNV_PRIME: u64 = 0x100000001b3;

/// Hash pixel content of a Pix (format-independent)
pub fn pixel_content_hash(pix: &Pix) -> u64 {
    let mut h = FNV_OFFSET_BASIS;
    for b in pix.width().to_le_bytes() {
        h ^= b as u64;
        h = h.wrapping_mul(FNV_PRIME);
    }
    for b in pix.height().to_le_bytes() {
        h ^= b as u64;
        h = h.wrapping_mul(FNV_PRIME);
    }
    for b in (pix.depth() as u32).to_le_bytes() {
        h ^= b as u64;
        h = h.wrapping_mul(FNV_PRIME);
    }
    for y in 0..pix.height() {
        for x in 0..pix.width() {
            let px = pix.get_pixel(x, y).unwrap_or(0);
            for b in px.to_le_bytes() {
                h ^= b as u64;
                h = h.wrapping_mul(FNV_PRIME);
            }
        }
    }
    h
}

/// Hash raw byte data
fn data_content_hash(data: &[u8]) -> u64 {
    let mut h = FNV_OFFSET_BASIS;
    for &b in data {
        h ^= b as u64;
        h = h.wrapping_mul(FNV_PRIME);
    }
    h
}

// --- Manifest management ---

fn manifest_path() -> String {
    format!("{}/tests/golden_manifest.tsv", env!("CARGO_MANIFEST_DIR"))
}

fn manifest() -> &'static Mutex<HashMap<String, u64>> {
    static MANIFEST: OnceLock<Mutex<HashMap<String, u64>>> = OnceLock::new();
    MANIFEST.get_or_init(|| Mutex::new(load_manifest_from_file()))
}

fn load_manifest_from_file() -> HashMap<String, u64> {
    let path = manifest_path();
    let content = match fs::read_to_string(&path) {
        Ok(c) => c,
        Err(_) => return HashMap::new(),
    };
    let mut map = HashMap::new();
    for line in content.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        if let Some((name, hash_str)) = line.split_once('\t')
            && let Ok(hash) = u64::from_str_radix(hash_str, 16)
        {
            map.insert(name.to_string(), hash);
        }
    }
    map
}

fn update_manifest_and_save(name: &str, hash: u64) {
    let mut map = manifest().lock().unwrap();
    map.insert(name.to_string(), hash);
    let mut entries: Vec<_> = map.iter().map(|(k, v)| (k.clone(), *v)).collect();
    entries.sort_by(|(a, _), (b, _)| a.cmp(b));
    let mut content =
        String::from("# Golden manifest - content hashes for regression test outputs\n");
    content.push_str("# Format: name<TAB>hash (FNV-1a hex)\n");
    for (name, hash) in &entries {
        content.push_str(&format!("{}\t{:016x}\n", name, hash));
    }
    fs::write(manifest_path(), &content).expect("Failed to write manifest");
}

/// Regression test parameters
///
/// This structure tracks the state of a regression test, including
/// the test name, current index, mode, and success status.
pub struct RegParams {
    /// Name of the test (e.g., "conncomp")
    pub test_name: String,
    /// Current test index (incremented before each test)
    index: usize,
    /// Test mode (generate, compare, or display)
    pub mode: RegTestMode,
    /// Overall success status
    success: bool,
    /// Recorded failures
    failures: Vec<String>,
}

impl RegParams {
    fn ensure_dirs() {
        static DIRS_READY: OnceLock<()> = OnceLock::new();
        DIRS_READY.get_or_init(|| {
            if let Err(e) = fs::create_dir_all(golden_dir()) {
                eprintln!("Warning: failed to create golden directory: {e}");
            }
            if let Err(e) = fs::create_dir_all(regout_dir()) {
                eprintln!("Warning: failed to create regout directory: {e}");
            }
        });
    }

    /// Create new regression test parameters
    ///
    /// # Arguments
    ///
    /// * `test_name` - Name of the test (e.g., "conncomp")
    ///
    /// # Returns
    ///
    /// A new `RegParams` instance configured based on the `REGTEST_MODE`
    /// environment variable.
    pub fn new(test_name: &str) -> Self {
        let mode = RegTestMode::from_env();

        Self::ensure_dirs();
        if mode != RegTestMode::Display {
            eprintln!();
            eprintln!("////////////////////////////////////////////////");
            eprintln!("////////////////   {}_reg   ///////////////", test_name);
            eprintln!("////////////////////////////////////////////////");
            eprintln!("Mode: {:?}", mode);
        }

        Self {
            test_name: test_name.to_string(),
            index: 0,
            mode,
            success: true,
            failures: Vec::new(),
        }
    }

    /// Get the current test index
    pub fn index(&self) -> usize {
        self.index
    }

    /// Check if in display mode
    pub fn display(&self) -> bool {
        self.mode == RegTestMode::Display
    }

    /// Compare two floating-point values
    ///
    /// # Arguments
    ///
    /// * `expected` - Expected value (typically from golden/reference)
    /// * `actual` - Actual computed value
    /// * `delta` - Maximum allowed difference
    ///
    /// # Returns
    ///
    /// `true` if values match within delta, `false` otherwise.
    pub fn compare_values(&mut self, expected: f64, actual: f64, delta: f64) -> bool {
        self.index += 1;
        let diff = (expected - actual).abs();

        if diff > delta {
            let msg = format!(
                "Failure in {}_reg: value comparison for index {}\n\
                 difference = {} but allowed delta = {}\n\
                 expected = {}, actual = {}",
                self.test_name, self.index, diff, delta, expected, actual
            );
            eprintln!("{}", msg);
            self.failures.push(msg);
            self.success = false;
            false
        } else {
            true
        }
    }

    /// Compare two Pix images for exact equality
    ///
    /// # Arguments
    ///
    /// * `pix1` - First image
    /// * `pix2` - Second image
    ///
    /// # Returns
    ///
    /// `true` if images are identical, `false` otherwise.
    pub fn compare_pix(&mut self, pix1: &Pix, pix2: &Pix) -> bool {
        self.index += 1;

        // Check dimensions
        if pix1.width() != pix2.width()
            || pix1.height() != pix2.height()
            || pix1.depth() != pix2.depth()
        {
            let msg = format!(
                "Failure in {}_reg: pix comparison for index {} - dimension mismatch",
                self.test_name, self.index
            );
            eprintln!("{}", msg);
            self.failures.push(msg);
            self.success = false;
            return false;
        }

        // Compare pixel by pixel
        let width = pix1.width();
        let height = pix1.height();

        for y in 0..height {
            for x in 0..width {
                let p1 = pix1.get_pixel(x, y);
                let p2 = pix2.get_pixel(x, y);
                if p1 != p2 {
                    let msg = format!(
                        "Failure in {}_reg: pix comparison for index {} - pixel mismatch at ({}, {})",
                        self.test_name, self.index, x, y
                    );
                    eprintln!("{}", msg);
                    self.failures.push(msg);
                    self.success = false;
                    return false;
                }
            }
        }

        true
    }

    /// Write a Pix to file and check against golden file
    ///
    /// # Arguments
    ///
    /// * `pix` - Image to write
    /// * `format` - Output format (PNG, TIFF, etc.)
    ///
    /// # Returns
    ///
    /// `Ok(())` if successful, error otherwise.
    pub fn write_pix_and_check(&mut self, pix: &Pix, format: ImageFormat) -> TestResult<()> {
        self.index += 1;

        let ext = format.extension();
        let local_path = format!(
            "{}/{}.{:02}.{}",
            regout_dir(),
            self.test_name,
            self.index,
            ext
        );

        // Write the local file
        leptonica::io::write_image(pix, &local_path, format).map_err(|e| {
            TestError::ImageWrite {
                path: local_path.clone(),
                message: e.to_string(),
            }
        })?;

        let hash = pixel_content_hash(pix);
        self.check_hash(&local_path, hash)
    }

    /// Check content hash against the golden manifest.
    ///
    /// In generate mode, copies the file to golden and updates the manifest.
    /// In compare mode, compares the hash against the manifest entry.
    /// In display mode, does nothing.
    fn check_hash(&mut self, local_path: &str, hash: u64) -> TestResult<()> {
        let ext = Path::new(local_path)
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("");

        let manifest_key = format!("{}.{:02}.{}", self.test_name, self.index, ext);

        match self.mode {
            RegTestMode::Generate => {
                let golden_path = format!(
                    "{}/{}_golden.{:02}.{}",
                    golden_dir(),
                    self.test_name,
                    self.index,
                    ext
                );
                fs::copy(local_path, &golden_path)?;
                update_manifest_and_save(&manifest_key, hash);
                eprintln!("Generated: {} (hash: {:016x})", manifest_key, hash);
            }
            RegTestMode::Compare => {
                let map = manifest().lock().unwrap();
                match map.get(&manifest_key) {
                    Some(&expected) if expected == hash => {}
                    Some(&expected) => {
                        let msg = format!(
                            "Failure in {}_reg, index {}: hash mismatch for {}\n\
                             \x20 expected: {:016x}\n\
                             \x20 actual:   {:016x}",
                            self.test_name, self.index, manifest_key, expected, hash
                        );
                        eprintln!("{}", msg);
                        self.failures.push(msg);
                        self.success = false;
                    }
                    None => {
                        eprintln!("Warning: no manifest entry for {manifest_key}, skipping");
                    }
                }
            }
            RegTestMode::Display => {}
        }

        Ok(())
    }

    /// Compare two binary data arrays
    ///
    /// # Arguments
    ///
    /// * `data1` - First byte array
    /// * `data2` - Second byte array
    ///
    /// # Returns
    ///
    /// `true` if data is identical, `false` otherwise.
    pub fn compare_strings(&mut self, data1: &[u8], data2: &[u8]) -> bool {
        self.index += 1;

        if data1 != data2 {
            let msg = format!(
                "Failure in {}_reg: string comparison for index {}\n\
                 sizes: {} vs {}",
                self.test_name,
                self.index,
                data1.len(),
                data2.len()
            );
            eprintln!("{}", msg);
            self.failures.push(msg);
            self.success = false;
            false
        } else {
            true
        }
    }

    /// Write data to file and check against golden file
    ///
    /// # Arguments
    ///
    /// * `data` - Data to write
    /// * `ext` - File extension (e.g., "ba", "pta")
    ///
    /// # Returns
    ///
    /// `Ok(())` if successful, error otherwise.
    pub fn write_data_and_check(&mut self, data: &[u8], ext: &str) -> TestResult<()> {
        self.index += 1;

        let local_path = format!(
            "{}/{}.{:02}.{}",
            regout_dir(),
            self.test_name,
            self.index,
            ext
        );

        fs::write(&local_path, data)?;
        let hash = data_content_hash(data);
        self.check_hash(&local_path, hash)
    }

    /// Clean up and report results
    ///
    /// # Returns
    ///
    /// `true` if all tests passed, `false` if any failed.
    pub fn cleanup(self) -> bool {
        if self.success {
            if self.mode != RegTestMode::Display {
                eprintln!("SUCCESS: {}_reg", self.test_name);
                eprintln!();
            }
        } else {
            eprintln!("FAILURE: {}_reg", self.test_name);
            for failure in &self.failures {
                eprintln!("  {}", failure);
            }
            eprintln!();
        }

        self.success
    }

    /// Check if all tests have passed so far
    pub fn is_success(&self) -> bool {
        self.success
    }

    /// Get list of failures
    pub fn failures(&self) -> &[String] {
        &self.failures
    }
}

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

    #[test]
    fn test_mode_from_env() {
        // Default should be Compare
        // Note: We can't safely remove env var in tests as it may affect other tests
        // Just test that from_env returns a valid mode
        let mode = RegTestMode::from_env();
        assert!(matches!(
            mode,
            RegTestMode::Compare | RegTestMode::Generate | RegTestMode::Display
        ));
    }

    #[test]
    fn test_compare_values_success() {
        let mut rp = RegParams::new("test");
        assert!(rp.compare_values(100.0, 100.0, 0.0));
        assert!(rp.is_success());
    }

    #[test]
    fn test_compare_values_within_delta() {
        let mut rp = RegParams::new("test");
        assert!(rp.compare_values(100.0, 100.5, 1.0));
        assert!(rp.is_success());
    }

    #[test]
    fn test_compare_values_failure() {
        let mut rp = RegParams::new("test");
        assert!(!rp.compare_values(100.0, 200.0, 0.0));
        assert!(!rp.is_success());
    }
}