mcv-rs 0.1.0

MCV computer-vision primitives
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
use std::{
    fs,
    path::Path,
    sync::{OnceLock, RwLock},
};

use regex::RegexBuilder;
use serde::{Deserialize, Serialize};

use super::{
    core::{
        default_ocr_case_sensitive, default_ocr_thread_count, default_ocr_threshold, MatchPoint,
        MatchResult, Roi,
    },
    error::{McvError, Result},
    frame::{RgbaFrame, VisionTemplate},
};

/// ocr-rs/MNN model file set. This is deliberately model-version agnostic:
/// any compatible MNN detection/recognition/charset trio can be used.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OcrModelFiles {
    pub model_dir: String,
    pub det_model: String,
    pub rec_model: String,
    pub charset: String,
}

impl OcrModelFiles {
    pub fn new(
        model_dir: impl AsRef<Path>,
        det_model: impl Into<String>,
        rec_model: impl Into<String>,
        charset: impl Into<String>,
    ) -> Self {
        Self {
            model_dir: model_dir.as_ref().to_string_lossy().into_owned(),
            det_model: det_model.into(),
            rec_model: rec_model.into(),
            charset: charset.into(),
        }
    }

    pub fn resolve(&self) -> Result<ResolvedOcrModelFiles> {
        let model_dir = std::path::PathBuf::from(&self.model_dir);
        let det_model = model_dir.join(&self.det_model);
        let rec_model = model_dir.join(&self.rec_model);
        let charset = model_dir.join(&self.charset);
        for path in [&det_model, &rec_model, &charset] {
            if !path.is_file() {
                return Err(McvError::MissingOcrModel(path.display().to_string()));
            }
        }
        Ok(ResolvedOcrModelFiles {
            model_dir,
            det_model,
            rec_model,
            charset,
        })
    }
}

static DEFAULT_OCR_MODELS: OnceLock<RwLock<Option<OcrModelFiles>>> = OnceLock::new();

fn default_ocr_models_store() -> &'static RwLock<Option<OcrModelFiles>> {
    DEFAULT_OCR_MODELS.get_or_init(|| RwLock::new(None))
}

/// Sets the custom OCR model files used by [`OcrTemplate::new`].
pub fn set_default_ocr_models(models: OcrModelFiles) -> Result<()> {
    models.resolve()?;
    let mut current = default_ocr_models_store()
        .write()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    *current = Some(models);
    Ok(())
}

/// Returns the custom OCR model files currently used by [`OcrTemplate::new`].
pub fn default_ocr_models() -> Option<OcrModelFiles> {
    default_ocr_models_store()
        .read()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
        .clone()
}

#[derive(Debug, Clone)]
pub struct ResolvedOcrModelFiles {
    pub model_dir: std::path::PathBuf,
    pub det_model: std::path::PathBuf,
    pub rec_model: std::path::PathBuf,
    pub charset: std::path::PathBuf,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OcrEngineOptions {
    pub model: OcrModelFiles,
    pub threshold: Option<f32>,
    pub thread_count: Option<i32>,
    pub roi: Option<Roi>,
}

pub type OcrPoint = MatchPoint;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OcrText {
    pub text: String,
    pub confidence: f32,
    pub bbox: Roi,
    pub points: Option<[OcrPoint; 4]>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OcrReport {
    pub text: String,
    pub results: Vec<OcrText>,
}

#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum OcrMatchMode {
    #[default]
    Contains,
    Exact,
    Regex,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OcrTemplateOptions {
    pub pattern: Option<String>,
    pub match_mode: Option<OcrMatchMode>,
    pub case_sensitive: Option<bool>,
    pub max_count: Option<usize>,
}

impl Default for OcrTemplateOptions {
    fn default() -> Self {
        Self {
            pattern: None,
            match_mode: Some(OcrMatchMode::Contains),
            case_sensitive: None,
            max_count: Some(1),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OcrFindOptions {
    #[serde(flatten)]
    pub ocr: OcrEngineOptions,
    pub pattern: Option<String>,
    pub match_mode: Option<OcrMatchMode>,
    pub case_sensitive: Option<bool>,
    pub max_count: Option<usize>,
}

impl OcrFindOptions {
    pub fn matcher_options(&self) -> OcrTemplateOptions {
        OcrTemplateOptions {
            pattern: self.pattern.clone(),
            match_mode: self.match_mode,
            case_sensitive: self.case_sensitive,
            max_count: self.max_count,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OcrMatchResult {
    pub top_left: (i32, i32),
    pub bottom_right: (i32, i32),
    pub center: (i32, i32),
    pub score: f64,
    pub text: String,
    pub confidence: f32,
    pub bbox: Roi,
    pub points: Option<[OcrPoint; 4]>,
}

impl OcrMatchResult {
    pub fn as_match_result(&self) -> MatchResult {
        MatchResult::from_box(self.bbox.x, self.bbox.y, self.bbox.width, self.bbox.height)
    }
}

impl From<OcrText> for OcrMatchResult {
    fn from(value: OcrText) -> Self {
        let result = MatchResult::from_box(
            value.bbox.x,
            value.bbox.y,
            value.bbox.width,
            value.bbox.height,
        );
        let top_left = result.top_left();
        let bottom_right = result.bottom_right();
        let center = result.center();
        Self {
            top_left,
            bottom_right,
            center,
            score: f64::from(value.confidence).clamp(0.0, 1.0),
            text: value.text,
            confidence: value.confidence,
            bbox: value.bbox,
            points: value.points,
        }
    }
}

pub struct OcrTextMatcher {
    pattern: Option<String>,
    mode: OcrMatchMode,
    case_sensitive: bool,
    regex: Option<regex::Regex>,
}

impl OcrTextMatcher {
    pub fn new(options: OcrTemplateOptions) -> Result<Self> {
        let mode = options.match_mode.unwrap_or_default();
        let case_sensitive = options
            .case_sensitive
            .unwrap_or_else(default_ocr_case_sensitive);
        let regex = match (&options.pattern, mode) {
            (Some(pattern), OcrMatchMode::Regex) => Some(
                RegexBuilder::new(pattern)
                    .case_insensitive(!case_sensitive)
                    .build()
                    .map_err(|err| McvError::Ocr(format!("invalid OCR regex: {err}")))?,
            ),
            _ => None,
        };
        Ok(Self {
            pattern: options.pattern,
            mode,
            case_sensitive,
            regex,
        })
    }

    pub fn is_match(&self, text: &str) -> bool {
        let Some(pattern) = self.pattern.as_deref() else {
            return true;
        };
        match self.mode {
            OcrMatchMode::Contains => {
                if self.case_sensitive {
                    text.contains(pattern)
                } else {
                    text.to_lowercase().contains(&pattern.to_lowercase())
                }
            }
            OcrMatchMode::Exact => {
                if self.case_sensitive {
                    text == pattern
                } else {
                    text.eq_ignore_ascii_case(pattern)
                        || text.to_lowercase() == pattern.to_lowercase()
                }
            }
            OcrMatchMode::Regex => self
                .regex
                .as_ref()
                .is_some_and(|regex| regex.is_match(text)),
        }
    }

    pub fn filter(&self, results: Vec<OcrText>, max_count: Option<usize>) -> Vec<OcrMatchResult> {
        if max_count == Some(0) {
            return Vec::new();
        }

        let mut matches = Vec::new();
        for item in results {
            if self.is_match(&item.text) {
                matches.push(OcrMatchResult::from(item));
                if max_count.is_some_and(|max_count| matches.len() >= max_count) {
                    break;
                }
            }
        }
        matches
    }
}

pub struct OcrTemplate {
    options: OcrFindOptions,
    engine: Option<ocr_rs::OcrEngine>,
}

#[derive(Debug, Clone)]
pub struct OcrTemplateBuilder {
    options: OcrFindOptions,
}

impl OcrTemplate {
    /// Creates a contains-text OCR template using the process-wide custom OCR
    /// models and defaults.
    pub fn new(pattern: impl Into<String>) -> Result<Self> {
        let pattern = pattern.into();
        if pattern.trim().is_empty() {
            return Err(McvError::Ocr(
                "OCR template text must not be empty".to_string(),
            ));
        }
        let models = default_ocr_models().ok_or(McvError::MissingDefaultOcrModels)?;
        Self::builder(models).contains(pattern).open()
    }

    pub fn with_options(options: OcrFindOptions) -> Self {
        Self {
            options,
            engine: None,
        }
    }

    pub fn builder(model: OcrModelFiles) -> OcrTemplateBuilder {
        let matcher = OcrTemplateOptions::default();
        OcrTemplateBuilder {
            options: OcrFindOptions {
                ocr: OcrEngineOptions {
                    model,
                    threshold: None,
                    thread_count: None,
                    roi: None,
                },
                pattern: matcher.pattern,
                match_mode: matcher.match_mode,
                case_sensitive: matcher.case_sensitive,
                max_count: matcher.max_count,
            },
        }
    }

    pub fn recognize_path(&mut self, image_path: impl AsRef<Path>) -> Result<OcrReport> {
        let image_bytes = fs::read(image_path.as_ref())?;
        let image = image::load_from_memory(&image_bytes)?;
        self.recognize_image(&image)
    }

    pub fn recognize_image(&mut self, image: &::image::DynamicImage) -> Result<OcrReport> {
        let options = self.options.ocr.clone();
        let engine = self.engine()?;
        recognize_ocr_image_with_engine(engine, image, &options)
    }

    pub fn find_path(&mut self, image_path: impl AsRef<Path>) -> Result<Option<OcrMatchResult>> {
        let results = self.find_all_path(image_path)?;
        Ok(results.into_iter().next())
    }

    pub fn find_image(&mut self, image: &::image::DynamicImage) -> Result<Option<OcrMatchResult>> {
        let results = self.find_all_image(image)?;
        Ok(results.into_iter().next())
    }

    /// Finds this text template in an RGBA frame using its configured ROI.
    ///
    /// This inherent method does not require importing [`VisionTemplate`].
    pub fn find(&mut self, image: &mut RgbaFrame<'_>) -> Result<Option<MatchResult>> {
        self.find_with_roi(image, None)
    }

    /// Finds this text template in an RGBA frame with a per-call ROI override.
    /// Passing `None` uses the ROI configured on the template.
    pub fn find_with_roi(
        &mut self,
        image: &mut RgbaFrame<'_>,
        roi: Option<Roi>,
    ) -> Result<Option<MatchResult>> {
        let mut options = self.options.clone();
        options.ocr.roi = roi.or(options.ocr.roi);
        let engine = self.engine()?;
        let report = recognize_ocr_image_with_engine(engine, image.dynamic_image()?, &options.ocr)?;
        let matcher = OcrTextMatcher::new(options.matcher_options())?;
        Ok(matcher
            .filter(report.results, options.max_count)
            .into_iter()
            .next()
            .map(|result| result.as_match_result()))
    }

    pub fn find_all_path(&mut self, image_path: impl AsRef<Path>) -> Result<Vec<OcrMatchResult>> {
        let report = self.recognize_path(image_path)?;
        let matcher = OcrTextMatcher::new(self.options.matcher_options())?;
        Ok(matcher.filter(report.results, self.options.max_count))
    }

    pub fn find_all_image(&mut self, image: &::image::DynamicImage) -> Result<Vec<OcrMatchResult>> {
        let report = self.recognize_image(image)?;
        let matcher = OcrTextMatcher::new(self.options.matcher_options())?;
        Ok(matcher.filter(report.results, self.options.max_count))
    }

    fn engine(&mut self) -> Result<&ocr_rs::OcrEngine> {
        if self.engine.is_none() {
            self.engine = Some(create_ocr_engine(&self.options.ocr)?);
        }
        Ok(self.engine.as_ref().expect("OCR engine initialized"))
    }
}

impl OcrTemplateBuilder {
    pub fn model(mut self, model: OcrModelFiles) -> Self {
        self.options.ocr.model = model;
        self
    }

    pub fn threshold(mut self, threshold: f32) -> Result<Self> {
        resolve_ocr_threshold(Some(threshold))?;
        self.options.ocr.threshold = Some(threshold);
        Ok(self)
    }

    pub fn threads(mut self, thread_count: i32) -> Result<Self> {
        if thread_count <= 0 {
            return Err(McvError::InvalidThreadCount);
        }
        self.options.ocr.thread_count = Some(thread_count);
        Ok(self)
    }

    pub fn roi(mut self, roi: Roi) -> Self {
        self.options.ocr.roi = Some(roi);
        self
    }

    pub fn optional_roi(mut self, roi: Option<Roi>) -> Self {
        self.options.ocr.roi = roi;
        self
    }

    pub fn any_text(mut self) -> Self {
        self.options.pattern = None;
        self
    }

    pub fn pattern(mut self, pattern: impl Into<String>) -> Self {
        self.options.pattern = Some(pattern.into());
        self
    }

    pub fn match_mode(mut self, match_mode: OcrMatchMode) -> Self {
        self.options.match_mode = Some(match_mode);
        self
    }

    pub fn contains(self, pattern: impl Into<String>) -> Self {
        self.pattern(pattern).match_mode(OcrMatchMode::Contains)
    }

    pub fn exact(self, pattern: impl Into<String>) -> Self {
        self.pattern(pattern).match_mode(OcrMatchMode::Exact)
    }

    pub fn regex(self, pattern: impl Into<String>) -> Result<Self> {
        let builder = self.pattern(pattern).match_mode(OcrMatchMode::Regex);
        OcrTextMatcher::new(builder.options.matcher_options())?;
        Ok(builder)
    }

    pub fn case_sensitive(mut self, case_sensitive: bool) -> Self {
        self.options.case_sensitive = Some(case_sensitive);
        self
    }

    pub fn case_insensitive(self) -> Self {
        self.case_sensitive(false)
    }

    pub fn max_count(mut self, max_count: usize) -> Self {
        self.options.max_count = Some(max_count);
        self
    }

    pub fn all_matches(mut self) -> Self {
        self.options.max_count = None;
        self
    }

    pub fn matcher_options(mut self, options: OcrTemplateOptions) -> Self {
        self.options.pattern = options.pattern;
        self.options.match_mode = options.match_mode;
        self.options.case_sensitive = options.case_sensitive;
        self.options.max_count = options.max_count;
        self
    }

    pub fn open(self) -> Result<OcrTemplate> {
        self.options.ocr.model.resolve()?;
        resolve_ocr_threshold(self.options.ocr.threshold)?;
        resolve_ocr_thread_count(self.options.ocr.thread_count)?;
        if self
            .options
            .ocr
            .roi
            .is_some_and(|roi| roi.width <= 0 || roi.height <= 0)
        {
            return Err(McvError::InvalidRoi(
                "OCR ROI width and height must be positive".to_string(),
            ));
        }
        OcrTextMatcher::new(self.options.matcher_options())?;
        Ok(OcrTemplate::with_options(self.options))
    }
}

impl VisionTemplate for OcrTemplate {
    fn find_with_roi(
        &mut self,
        image: &mut RgbaFrame<'_>,
        roi: Option<Roi>,
    ) -> Result<Option<MatchResult>> {
        OcrTemplate::find_with_roi(self, image, roi)
    }
}

pub fn find_ocr_text_path(
    image_path: impl AsRef<Path>,
    options: OcrFindOptions,
) -> Result<Vec<OcrMatchResult>> {
    OcrTemplate::with_options(options).find_all_path(image_path)
}

pub fn find_ocr_text_image(
    image: &::image::DynamicImage,
    options: OcrFindOptions,
) -> Result<Vec<OcrMatchResult>> {
    OcrTemplate::with_options(options).find_all_image(image)
}

pub fn recognize_ocr_path(
    image_path: impl AsRef<Path>,
    options: OcrEngineOptions,
) -> Result<OcrReport> {
    let image_bytes = fs::read(image_path.as_ref())?;
    let image = image::load_from_memory(&image_bytes)?;
    recognize_ocr_image(&image, options)
}

pub fn recognize_ocr_image(
    image: &::image::DynamicImage,
    options: OcrEngineOptions,
) -> Result<OcrReport> {
    let engine = create_ocr_engine(&options)?;
    recognize_ocr_image_with_engine(&engine, image, &options)
}

fn create_ocr_engine(options: &OcrEngineOptions) -> Result<ocr_rs::OcrEngine> {
    let model = options.model.resolve()?;
    preload_mnn_runtime()?;
    let threshold = resolve_ocr_threshold(options.threshold)?;
    let thread_count = resolve_ocr_thread_count(options.thread_count)?;

    let config = ocr_rs::OcrEngineConfig::new()
        .with_threads(thread_count)
        .with_min_result_confidence(threshold);
    ocr_rs::OcrEngine::new(
        model.det_model,
        model.rec_model,
        model.charset,
        Some(config),
    )
    .map_err(|err| McvError::Ocr(err.to_string()))
}

fn resolve_ocr_threshold(threshold: Option<f32>) -> Result<f32> {
    let threshold = threshold.unwrap_or_else(default_ocr_threshold);
    super::core::validate_threshold_value(f64::from(threshold))?;
    Ok(threshold)
}

fn resolve_ocr_thread_count(thread_count: Option<i32>) -> Result<i32> {
    let thread_count = thread_count.unwrap_or_else(default_ocr_thread_count);
    if thread_count <= 0 {
        return Err(McvError::InvalidThreadCount);
    }
    Ok(thread_count)
}

fn recognize_ocr_image_with_engine(
    engine: &ocr_rs::OcrEngine,
    image: &::image::DynamicImage,
    options: &OcrEngineOptions,
) -> Result<OcrReport> {
    let cropped;
    let (offset_x, offset_y, image) = if let Some(roi) = options.roi {
        let image_width = image.width() as i32;
        let image_height = image.height() as i32;
        let roi = roi.clamp(image_width, image_height).ok_or_else(|| {
            McvError::InvalidRoi(
                "OCR ROI is outside the image or has non-positive size".to_string(),
            )
        })?;
        cropped = image.crop_imm(
            roi.x as u32,
            roi.y as u32,
            roi.width as u32,
            roi.height as u32,
        );
        (roi.x, roi.y, &cropped)
    } else {
        (0, 0, image)
    };
    let raw_results = engine
        .recognize(image)
        .map_err(|err| McvError::Ocr(err.to_string()))?;

    let mut results = Vec::with_capacity(raw_results.len());
    for item in raw_results {
        let rect = item.bbox.rect;
        let x = rect.left() + offset_x;
        let y = rect.top() + offset_y;
        let points = item.bbox.points.map(|points| {
            points.map(|point| OcrPoint {
                x: point.x + offset_x as f32,
                y: point.y + offset_y as f32,
            })
        });
        results.push(OcrText {
            text: item.text,
            confidence: item.confidence,
            bbox: Roi {
                x,
                y,
                width: rect.width() as i32,
                height: rect.height() as i32,
            },
            points,
        });
    }
    let text = results
        .iter()
        .map(|result| result.text.as_str())
        .collect::<Vec<_>>()
        .join("\n");
    Ok(OcrReport { text, results })
}

#[cfg(windows)]
fn preload_mnn_runtime() -> Result<()> {
    use std::sync::OnceLock;
    use std::{collections::HashSet, sync::Mutex};

    static PRELOADED_DLLS: OnceLock<Mutex<HashSet<std::path::PathBuf>>> = OnceLock::new();

    let Some(dll_path) = std::env::var_os("MNN_DLL_PATH")
        .map(std::path::PathBuf::from)
        .filter(|path| path.is_file())
    else {
        return Ok(());
    };
    let dll_path = dll_path.canonicalize()?;

    let cache = PRELOADED_DLLS.get_or_init(|| Mutex::new(HashSet::new()));
    {
        let loaded = cache
            .lock()
            .map_err(|_| McvError::Ocr("MNN DLL preload cache mutex poisoned".to_string()))?;
        if loaded.contains(&dll_path) {
            return Ok(());
        }
    }

    let library = unsafe { libloading::Library::new(&dll_path) }
        .map_err(|err| McvError::Ocr(format!("failed to load {}: {err}", dll_path.display())))?;
    std::mem::forget(library);

    let mut loaded = cache
        .lock()
        .map_err(|_| McvError::Ocr("MNN DLL preload cache mutex poisoned".to_string()))?;
    loaded.insert(dll_path);
    Ok(())
}

#[cfg(not(windows))]
fn preload_mnn_runtime() -> Result<()> {
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::{SystemTime, UNIX_EPOCH};

    fn ocr_text(text: &str) -> OcrText {
        OcrText {
            text: text.to_string(),
            confidence: 0.9,
            bbox: Roi::full(10, 10),
            points: None,
        }
    }

    #[test]
    fn zero_max_count_returns_no_matches() -> Result<()> {
        let matcher = OcrTextMatcher::new(OcrTemplateOptions::default())?;
        let matches = matcher.filter(vec![ocr_text("first"), ocr_text("second")], Some(0));
        assert!(matches.is_empty());
        Ok(())
    }

    #[test]
    fn explicit_threshold_must_be_finite_and_in_range() {
        assert!(resolve_ocr_threshold(Some(f32::NAN)).is_err());
        assert!(resolve_ocr_threshold(Some(-0.01)).is_err());
        assert!(resolve_ocr_threshold(Some(1.01)).is_err());
        assert_eq!(resolve_ocr_threshold(Some(0.75)).unwrap(), 0.75);
    }

    #[test]
    fn global_case_and_thread_defaults_are_used_when_unspecified() -> Result<()> {
        crate::mcv::set_default_ocr_case_sensitive(false);
        let matcher = OcrTextMatcher::new(OcrTemplateOptions::default())?;
        assert!(!matcher.case_sensitive);

        crate::mcv::set_default_ocr_thread_count(7)?;
        assert_eq!(resolve_ocr_thread_count(None)?, 7);
        assert!(crate::mcv::set_default_ocr_thread_count(0).is_err());
        assert_eq!(resolve_ocr_thread_count(Some(2))?, 2);

        crate::mcv::set_default_ocr_case_sensitive(true);
        crate::mcv::set_default_ocr_thread_count(4)?;
        Ok(())
    }

    #[test]
    fn builder_creates_template_from_custom_models() -> Result<()> {
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time")
            .as_nanos();
        let model_dir = std::env::temp_dir().join(format!(
            "mcv-rs-ocr-builder-{}-{unique}",
            std::process::id()
        ));
        fs::create_dir_all(&model_dir)?;
        for file_name in ["detector.mnn", "recognizer.mnn", "characters.txt"] {
            fs::write(model_dir.join(file_name), [])?;
        }

        let models = OcrModelFiles::new(
            &model_dir,
            "detector.mnn",
            "recognizer.mnn",
            "characters.txt",
        );
        crate::mcv::set_default_ocr_models(models.clone())?;
        let simple_template = OcrTemplate::new("Start")?;
        assert_eq!(simple_template.options.pattern.as_deref(), Some("Start"));

        let template = OcrTemplate::builder(models)
            .contains("Start")
            .case_insensitive()
            .threshold(0.8)?
            .threads(2)?
            .max_count(3)
            .open()?;

        assert_eq!(template.options.pattern.as_deref(), Some("Start"));
        assert!(matches!(
            template.options.match_mode,
            Some(OcrMatchMode::Contains)
        ));
        assert_eq!(template.options.case_sensitive, Some(false));
        assert_eq!(template.options.max_count, Some(3));
        assert_eq!(template.options.ocr.threshold, Some(0.8));
        assert_eq!(template.options.ocr.thread_count, Some(2));

        fs::remove_dir_all(model_dir)?;
        Ok(())
    }

    #[test]
    fn simple_constructor_rejects_empty_text() {
        assert!(OcrTemplate::new("   ").is_err());
    }

    #[test]
    fn inherent_frame_find_api_is_available() {
        let _find = OcrTemplate::find;
        let _find_with_roi = OcrTemplate::find_with_roi;
    }

    #[test]
    fn builder_rejects_invalid_threads_and_regex() {
        let models =
            OcrModelFiles::new("models", "detector.mnn", "recognizer.mnn", "characters.txt");
        assert!(matches!(
            OcrTemplate::builder(models.clone()).threads(0),
            Err(McvError::InvalidThreadCount)
        ));
        assert!(OcrTemplate::builder(models).regex("[").is_err());
    }
}