Skip to main content

docling_pdf/
ocr_prep.rs

1//! ONNX-free half of the PP-OCRv3 recognition pipeline: everything before
2//! and after the `session.run` call — line segmentation, crop preparation,
3//! width-batching, CTC decoding, dictionary handling.
4//!
5//! Split out of `ocr.rs` (which keeps the `ort` session) so the browser build
6//! can reuse it (issue #79 phase 2): `docling-wasm` runs these exact
7//! functions and delegates only the inference call to ONNX Runtime Web on
8//! the JS side. Keeping one implementation is what makes the wasm output
9//! byte-comparable to the native CPU path — any drift then comes from the
10//! runtime, not from pre/post-processing.
11
12use image::{imageops, imageops::FilterType, Rgb, RgbImage};
13
14/// PP-OCRv3's fixed input height.
15pub const REC_HEIGHT: u32 = 48;
16
17/// Cap on the recognizer input width — a sanity bound only (#981). RapidOCR
18/// resizes every crop to `48 × ceil(48 · w/h)` with no cap, and the CTC
19/// recognizer reads any width; the 2400 px (50:1) cap this held before
20/// squeezed every longer line horizontally — a 700 px full-width line of
21/// 12 px print at 800 px page width, which the detector boxes tighter than
22/// the projection strips — and the recognizer then dropped letters and
23/// glued words along the whole line (`Baccalaatpoesne accurlatci usgers`)
24/// while short lines on the same page read fine. 16384 (341:1) is past any
25/// text line a page can carry at the render sizes allowed.
26pub const REC_MAX_WIDTH: u32 = 16384;
27
28/// Cap on lines per recognition run: bounds peak input-tensor memory
29/// (16 × 3 × 48 × 2400 px ≈ 22 MB f32) without costing measurable batching
30/// benefit — same-width groups are rarely larger.
31pub const REC_BATCH: usize = 16;
32
33/// A text-line crop prepared for recognition: resized to the fixed model
34/// height, normalised to `[-1, 1]`, laid out CHW.
35pub struct PrepLine {
36    /// Width after the aspect-preserving resize to [`REC_HEIGHT`].
37    pub w: usize,
38    /// `3 * REC_HEIGHT * w` values.
39    pub data: Vec<f32>,
40}
41
42/// Prepare one line crop, or `None` for a degenerate (zero-sized) crop.
43pub fn prep_line(line: &RgbImage) -> Option<PrepLine> {
44    let (w, h) = line.dimensions();
45    if w == 0 || h == 0 {
46        return None;
47    }
48    let new_w = ((w as f32) * REC_HEIGHT as f32 / h as f32)
49        .round()
50        .clamp(8.0, REC_MAX_WIDTH as f32) as u32;
51    let resized = imageops::resize(line, new_w, REC_HEIGHT, FilterType::Triangle);
52    let n = (REC_HEIGHT * new_w) as usize;
53    // Normalise to [-1, 1]: (x/255 - 0.5) / 0.5.
54    let mut data = vec![0f32; 3 * n];
55    for (i, px) in resized.pixels().enumerate() {
56        data[i] = px[0] as f32 / 127.5 - 1.0;
57        data[n + i] = px[1] as f32 / 127.5 - 1.0;
58        data[2 * n + i] = px[2] as f32 / 127.5 - 1.0;
59    }
60    Some(PrepLine {
61        w: new_w as usize,
62        data,
63    })
64}
65
66/// The CTC class table for a recognition dictionary file: index 0 = blank,
67/// then one class per dictionary line, then the space class.
68pub fn dict_chars(dict: &str) -> Vec<String> {
69    let mut chars = vec![String::new()]; // blank at 0
70    chars.extend(dict.lines().map(|s| s.to_string()));
71    chars.push(" ".to_string());
72    chars
73}
74
75/// Greedy CTC decode of one row's `(T, C)` probabilities.
76pub fn decode_row(chars: &[String], probs: &[f32], nc: usize) -> String {
77    decode_row_scored(chars, probs, nc).0
78}
79
80/// [`decode_row`] plus the line's recognition confidence: the mean probability
81/// of the emitted characters (PaddleOCR's per-line score — what docling's
82/// `TextCell.confidence` carries for OCR cells, feeding the `ocr_score`
83/// confidence aggregate, #183). `0.0` when nothing decodes.
84pub fn decode_row_scored(chars: &[String], probs: &[f32], nc: usize) -> (String, f32) {
85    let mut out = String::new();
86    let mut prev = 0usize;
87    let mut conf_sum = 0.0f32;
88    let mut conf_n = 0usize;
89    for row in probs.chunks_exact(nc) {
90        let mut best = 0usize;
91        let mut bestv = row[0];
92        for (c, &v) in row.iter().enumerate().skip(1) {
93            if v > bestv {
94                bestv = v;
95                best = c;
96            }
97        }
98        if best != prev && best != 0 {
99            if let Some(ch) = chars.get(best) {
100                out.push_str(ch);
101                conf_sum += bestv;
102                conf_n += 1;
103            }
104        }
105        prev = best;
106    }
107    let conf = if conf_n == 0 {
108        0.0
109    } else {
110        conf_sum / conf_n as f32
111    };
112    (out, conf)
113}
114
115pub(crate) fn luma(p: &Rgb<u8>) -> f32 {
116    0.299 * p[0] as f32 + 0.587 * p[1] as f32 + 0.114 * p[2] as f32
117}
118
119/// Split a region crop into text lines via a horizontal ink-projection profile.
120/// Returns tight `(l, t, r, b)` boxes in crop pixels.
121pub fn segment_lines(crop: &RgbImage) -> Vec<(u32, u32, u32, u32)> {
122    let (w, h) = crop.dimensions();
123    if w == 0 || h == 0 {
124        return Vec::new();
125    }
126    let mean: f32 = crop.pixels().map(luma).sum::<f32>() / (w * h) as f32;
127    let thresh = mean * 0.7; // ink = noticeably darker than the page average
128    let min_ink = ((w as f32) * 0.005).max(1.0) as u32;
129
130    // A column inked in nearly every row is a vertical rule — a panel border, a
131    // table line — not text; left in the profile it bridges every inter-line
132    // gap and the whole crop collapses into one giant "line" (a framed
133    // terms-and-conditions box OCR'd as a single unreadable strip). Mask those
134    // columns out of the row profile; glyph columns never come close (a
135    // descender-to-ascender stack is still far from 90 % of the crop height).
136    let mut col_ink = vec![0u32; w as usize];
137    for y in 0..h {
138        for x in 0..w {
139            if luma(crop.get_pixel(x, y)) < thresh {
140                col_ink[x as usize] += 1;
141            }
142        }
143    }
144    // Borders are *thin*: when "always-inked" columns make up a noticeable
145    // share of the width it is not a frame but a polarity/threshold artifact
146    // (an inverted dark-mode page classifies its whole background as ink) —
147    // leave the profile alone and let polarity normalization handle it.
148    let rule_cols = col_ink
149        .iter()
150        .filter(|&&c| c as f32 > 0.9 * h as f32)
151        .count();
152    let mask_rules = (rule_cols as f32) < 0.15 * w as f32;
153    let rule = |x: u32| mask_rules && col_ink[x as usize] as f32 > 0.9 * h as f32;
154
155    let mut profile = vec![0u32; h as usize];
156    for y in 0..h {
157        let mut row = 0u32;
158        for x in 0..w {
159            if !rule(x) && luma(crop.get_pixel(x, y)) < thresh {
160                row += 1;
161            }
162        }
163        profile[y as usize] = row;
164    }
165
166    // Maximal runs of text rows, separated by (near-)blank rows.
167    let mut runs: Vec<(u32, u32)> = Vec::new();
168    let mut start: Option<u32> = None;
169    for y in 0..h {
170        let text = profile[y as usize] >= min_ink;
171        if text && start.is_none() {
172            start = Some(y);
173        } else if !text {
174            if let Some(s) = start.take() {
175                if y - s >= 4 {
176                    runs.push((s, y));
177                }
178            }
179        }
180    }
181    if let Some(s) = start {
182        if h - s >= 4 {
183            runs.push((s, h));
184        }
185    }
186
187    // Tighten each line to its horizontal ink bounds.
188    runs.into_iter()
189        .map(|(t, b)| {
190            let (mut l, mut r) = (w, 0u32);
191            for y in t..b {
192                for x in 0..w {
193                    if luma(crop.get_pixel(x, y)) < thresh {
194                        l = l.min(x);
195                        r = r.max(x + 1);
196                    }
197                }
198            }
199            if l >= r {
200                (0, t, w, b)
201            } else {
202                (l, t, r, b)
203            }
204        })
205        .collect()
206}
207
208/// Layout labels whose content is recognised as running text.
209pub fn is_text_label(label: &str) -> bool {
210    matches!(
211        label,
212        "text"
213            | "title"
214            | "section_header"
215            | "list_item"
216            | "caption"
217            | "footnote"
218            | "code"
219            | "formula"
220    )
221}
222
223/// A line's page-point bounding box, `(l, t, r, b)`.
224pub type LineBox = (f32, f32, f32, f32);
225
226/// Gather every text-region line crop on a page, in page order: crop each
227/// text region (page points × `scale` → image px), split it into lines, prep
228/// each line, and keep the line's page-point bbox. The exact gathering the
229/// native `ocr_page` does — shared so the browser path produces the same
230/// cells given the same probabilities.
231pub fn prep_region_lines(
232    img: &RgbImage,
233    regions: &[crate::layout::Region],
234    scale: f32,
235) -> (Vec<LineBox>, Vec<PrepLine>) {
236    let (iw, ih) = img.dimensions();
237    let mut bboxes = Vec::new();
238    let mut lines = Vec::new();
239    for region in regions {
240        if !is_text_label(region.label) {
241            continue;
242        }
243        let l = (region.l * scale).max(0.0) as u32;
244        let t = (region.t * scale).max(0.0) as u32;
245        let r = ((region.r * scale).max(0.0) as u32).min(iw);
246        let b = ((region.b * scale).max(0.0) as u32).min(ih);
247        if r <= l || b <= t {
248            continue;
249        }
250        let crop = imageops::crop_imm(img, l, t, r - l, b - t).to_image();
251        for (lx, ly, rx, ry) in segment_lines(&crop) {
252            let line = imageops::crop_imm(&crop, lx, ly, rx - lx, ry - ly).to_image();
253            let Some(pl) = prep_line(&line) else {
254                continue;
255            };
256            bboxes.push((
257                (l + lx) as f32 / scale,
258                (t + ly) as f32 / scale,
259                (l + rx) as f32 / scale,
260                (t + ry) as f32 / scale,
261            ));
262            lines.push(pl);
263        }
264    }
265    (bboxes, lines)
266}
267
268/// [`prep_region_lines`] with the text detector's boxes as the line source
269/// (#570): inside each text region, the detected lines whose center falls in
270/// the region are the crops (whole, in the detector's reading order — see
271/// `det_boxes_inside`); a region the detector found nothing in falls back to
272/// the projection segmentation. `detected` is in image pixels (the detector ran
273/// on `img`).
274///
275/// Why: the projection profile cuts a region into full-width strips, so on a
276/// form — several fields on one baseline, separated by gaps the strip spans —
277/// one crop carries `FROM: J.Smith   DATE: 3/4/92   REF: 1138` and the
278/// recognizer glues or drops words (`OLDCOLDMENTHOLUIGHTS&ULTRA`). RapidOCR,
279/// docling's engine, recognizes the detector's own boxes, each a single text
280/// run with the detector's margin around it. On FUNSD this is the difference
281/// between 0.61 and 0.85 word recall (see the issue's measurements).
282///
283/// The crop is the detector's rotated rectangle warped upright
284/// ([`rotate_crop`], RapidOCR's `get_rotate_crop_image`), not its
285/// axis-aligned bounding box (#981): a scan is never quite straight, and the
286/// bounding box of a line skewed by θ is `width · tan θ` taller than the
287/// line — at 0.6° a 700 px line of 12 px print gains 7 px, a third of its
288/// height — so the recognizer got the long lines squeezed to two thirds of
289/// their width and slanted, and dropped letters and glued words along them
290/// (`Baccalaatpoesne accurlatci usgers`) while the short lines on the same
291/// page read fine; the same crops read 0 garbled lines in RapidOCR with the
292/// identical models. The cell keeps the axis-aligned box as its geometry.
293pub fn prep_region_lines_det(
294    img: &RgbImage,
295    regions: &[crate::layout::Region],
296    scale: f32,
297    detected: &[crate::ocr_det::DetBox],
298) -> (Vec<LineBox>, Vec<PrepLine>) {
299    prep_region_lines_det_by(img, regions, scale, detected, |img, _, d| {
300        rotate_crop(img, &d.quad)
301    })
302}
303
304/// [`prep_region_lines_det`] with each detected line cropped as its
305/// axis-aligned pixel rectangle — the pre-#981 crop, kept for the page
306/// orientation probe (`orient`): the probe scores these crops at 0° against
307/// the page turned three ways to tell a sideways raster, and the warp would
308/// defeat it — a sideways line's tall box comes out of [`rotate_crop`]
309/// already turned upright, so a page rotated 90° read as upright and was
310/// never un-rotated (the `ocr_test_raster_rot_90` fixture, whose two lines
311/// then came out in the sideways order). The page OCR itself never uses
312/// this crop.
313pub fn prep_region_lines_det_boxed(
314    img: &RgbImage,
315    regions: &[crate::layout::Region],
316    scale: f32,
317    detected: &[crate::ocr_det::DetBox],
318) -> (Vec<LineBox>, Vec<PrepLine>) {
319    prep_region_lines_det_by(img, regions, scale, detected, |img, (l, t, r, b), _| {
320        Some(imageops::crop_imm(img, l, t, r - l, b - t).to_image())
321    })
322}
323
324fn prep_region_lines_det_by(
325    img: &RgbImage,
326    regions: &[crate::layout::Region],
327    scale: f32,
328    detected: &[crate::ocr_det::DetBox],
329    crop: impl Fn(&RgbImage, PxRect, &crate::ocr_det::DetBox) -> Option<RgbImage>,
330) -> (Vec<LineBox>, Vec<PrepLine>) {
331    let (iw, ih) = img.dimensions();
332    let mut bboxes = Vec::new();
333    let mut lines = Vec::new();
334    for region in regions {
335        if !is_text_label(region.label) {
336            continue;
337        }
338        let l = (region.l * scale).max(0.0) as u32;
339        let t = (region.t * scale).max(0.0) as u32;
340        let r = ((region.r * scale).max(0.0) as u32).min(iw);
341        let b = ((region.b * scale).max(0.0) as u32).min(ih);
342        if r <= l || b <= t {
343            continue;
344        }
345        let inside = det_boxes_inside(detected, l, t, r, b, iw, ih);
346        if inside.is_empty() {
347            let (bb, pl) = prep_region_lines(img, std::slice::from_ref(region), scale);
348            bboxes.extend(bb);
349            lines.extend(pl);
350            continue;
351        }
352        for ((dl, dt, dr, db), d) in inside {
353            let Some(pl) = crop(img, (dl, dt, dr, db), d).as_ref().and_then(prep_line) else {
354                continue;
355            };
356            bboxes.push((
357                dl as f32 / scale,
358                dt as f32 / scale,
359                dr as f32 / scale,
360                db as f32 / scale,
361            ));
362            lines.push(pl);
363        }
364    }
365    (bboxes, lines)
366}
367
368/// Split a text line into word tokens by a vertical ink-projection profile:
369/// runs of ink columns separated by whitespace wider than ~0.6× the line
370/// height (an inter-word/-column gap, not an inter-character one). Returns tight
371/// `(l, 0, r, h)` boxes in line-crop pixels. The browser table path recognizes
372/// these individually so each word carries its own box for column matching —
373/// the native pipeline gets word boxes from the pdfium text layer instead.
374pub fn segment_words(line: &RgbImage) -> Vec<(u32, u32, u32, u32)> {
375    let (w, h) = line.dimensions();
376    if w == 0 || h == 0 {
377        return Vec::new();
378    }
379    let mean: f32 = line.pixels().map(luma).sum::<f32>() / (w * h) as f32;
380    let thresh = mean * 0.7;
381    let mut col_ink = vec![0u32; w as usize];
382    for y in 0..h {
383        for x in 0..w {
384            if luma(line.get_pixel(x, y)) < thresh {
385                col_ink[x as usize] += 1;
386            }
387        }
388    }
389    let min_gap = ((h as f32) * 0.6).max(4.0) as u32;
390    let mut words = Vec::new();
391    let mut start: Option<u32> = None;
392    let mut last_ink = 0u32;
393    let mut gap = 0u32;
394    for x in 0..w {
395        if col_ink[x as usize] > 0 {
396            if start.is_none() {
397                start = Some(x);
398            }
399            last_ink = x;
400            gap = 0;
401        } else if let Some(s) = start {
402            gap += 1;
403            if gap >= min_gap {
404                words.push((s, 0, last_ink + 1, h));
405                start = None;
406            }
407        }
408    }
409    if let Some(s) = start {
410        words.push((s, 0, last_ink + 1, h));
411    }
412    words
413}
414
415/// Gather word crops from a page's *table* regions (browser table path, #157
416/// stage 3): crop each table region, split it into lines, split each line into
417/// words ([`segment_words`]), prep each word for recognition, and keep the
418/// word's page-point bbox. Recognizing table interiors is what gives the cell
419/// matcher the word boxes it needs — the native pipeline reads those from
420/// pdfium's text layer on digital pages, and calls this on scanned ones
421/// (`OcrModel::ocr_table_words`, #173), same as the browser path.
422pub fn prep_table_words(
423    img: &RgbImage,
424    regions: &[crate::layout::Region],
425    scale: f32,
426) -> (Vec<LineBox>, Vec<PrepLine>) {
427    let (iw, ih) = img.dimensions();
428    let mut bboxes = Vec::new();
429    let mut lines = Vec::new();
430    for region in regions {
431        if !crate::assemble::is_table_like(region.label) {
432            continue;
433        }
434        let l = (region.l * scale).max(0.0) as u32;
435        let t = (region.t * scale).max(0.0) as u32;
436        let r = ((region.r * scale).max(0.0) as u32).min(iw);
437        let b = ((region.b * scale).max(0.0) as u32).min(ih);
438        if r <= l || b <= t {
439            continue;
440        }
441        let crop = imageops::crop_imm(img, l, t, r - l, b - t).to_image();
442        for (lx, ly, rx, ry) in segment_lines(&crop) {
443            let line = imageops::crop_imm(&crop, lx, ly, rx - lx, ry - ly).to_image();
444            for (wx0, _, wx1, _) in segment_words(&line) {
445                let word = imageops::crop_imm(&line, wx0, 0, wx1 - wx0, ry - ly).to_image();
446                let Some(pl) = prep_line(&word) else {
447                    continue;
448                };
449                bboxes.push((
450                    (l + lx + wx0) as f32 / scale,
451                    (t + ly) as f32 / scale,
452                    (l + lx + wx1) as f32 / scale,
453                    (t + ry) as f32 / scale,
454                ));
455                lines.push(pl);
456            }
457        }
458    }
459    (bboxes, lines)
460}
461
462/// [`prep_table_words`] with the text detector's boxes as the word source
463/// (#570): inside each table region the detected boxes whose center falls in
464/// it are the crops (whole) — RapidOCR's cells, which is what
465/// docling's TableFormer matcher receives on a scanned table; a table the
466/// detector found nothing in falls back to the projection line/word split.
467pub fn prep_table_words_det(
468    img: &RgbImage,
469    regions: &[crate::layout::Region],
470    scale: f32,
471    detected: &[crate::ocr_det::DetBox],
472) -> (Vec<LineBox>, Vec<PrepLine>) {
473    let (iw, ih) = img.dimensions();
474    let mut bboxes = Vec::new();
475    let mut lines = Vec::new();
476    for region in regions {
477        if !crate::assemble::is_table_like(region.label) {
478            continue;
479        }
480        let l = (region.l * scale).max(0.0) as u32;
481        let t = (region.t * scale).max(0.0) as u32;
482        let r = ((region.r * scale).max(0.0) as u32).min(iw);
483        let b = ((region.b * scale).max(0.0) as u32).min(ih);
484        if r <= l || b <= t {
485            continue;
486        }
487        let inside = det_boxes_inside(detected, l, t, r, b, iw, ih);
488        if inside.is_empty() {
489            let (bb, pl) = prep_table_words(img, std::slice::from_ref(region), scale);
490            bboxes.extend(bb);
491            lines.extend(pl);
492            continue;
493        }
494        // The quad's warp, as for the text lines (#981): a skewed table's
495        // long cells squeeze the same way.
496        for ((dl, dt, dr, db), d) in inside {
497            let Some(pl) = rotate_crop(img, &d.quad).as_ref().and_then(prep_line) else {
498                continue;
499            };
500            bboxes.push((
501                dl as f32 / scale,
502                dt as f32 / scale,
503                dr as f32 / scale,
504                db as f32 / scale,
505            ));
506            lines.push(pl);
507        }
508    }
509    (bboxes, lines)
510}
511
512/// The detected boxes whose center lies in the `l..r × t..b` image-pixel
513/// rectangle, in the detector's reading order, each with its axis-aligned
514/// pixel rectangle clamped to the image (the cell's geometry). A box is
515/// taken whole, not clipped to the region: the layout box is often tight on
516/// the ink while the detector's unclip margin reaches past it, and cutting
517/// that margin off clips the first and last glyphs — RapidOCR recognizes the
518/// detector's box as drawn. The crop may thus poke a little outside the
519/// region; the cell keeps the box geometry and assembly assigns it by
520/// overlap, as for any cell.
521/// A detected line's box clamped to the image, in whole pixels.
522type PxRect = (u32, u32, u32, u32);
523
524fn det_boxes_inside(
525    detected: &[crate::ocr_det::DetBox],
526    l: u32,
527    t: u32,
528    r: u32,
529    b: u32,
530    iw: u32,
531    ih: u32,
532) -> Vec<(PxRect, &crate::ocr_det::DetBox)> {
533    detected
534        .iter()
535        .filter(|d| {
536            let (cx, cy) = ((d.l + d.r) / 2.0, (d.t + d.b) / 2.0);
537            cx >= l as f32 && cx < r as f32 && cy >= t as f32 && cy < b as f32
538        })
539        .filter_map(|d| det_box_px(d, iw, ih).map(|px| (px, d)))
540        .collect()
541}
542
543/// A detector box's axis-aligned pixel rectangle, truncated and clamped to a
544/// `iw × ih` image — the plain crop the page OCR recognizes; `None` when
545/// nothing of it is left.
546fn det_box_px(d: &crate::ocr_det::DetBox, iw: u32, ih: u32) -> Option<PxRect> {
547    let dl = d.l.max(0.0) as u32;
548    let dt = d.t.max(0.0) as u32;
549    let dr = (d.r.max(0.0) as u32).min(iw);
550    let db = (d.b.max(0.0) as u32).min(ih);
551    (dr > dl && db > dt).then_some((dl, dt, dr, db))
552}
553
554/// The recognizer's crop of one detector box on `img` (#939), exactly as the
555/// page OCR takes it: RapidOCR's warp of the box's quad ([`rotate_crop`] —
556/// upright out of the detector's rotated rectangle, tall lines turned 90°;
557/// the plain pixels for an upright box). `None` for a box with nothing
558/// inside the image. Until #981 this was the axis-aligned box unless
559/// `ocr_textline_orientation` was on; the warp is now the crop for every
560/// detected line, the option adds only the 0/180° classifier.
561pub fn det_box_crop(img: &RgbImage, d: &crate::ocr_det::DetBox) -> Option<RgbImage> {
562    // Intersection first (#947): `rotate_crop` replicates border pixels, so
563    // a box wholly outside the image would otherwise come back as
564    // edge-colored pixels — the same box `det_boxes_inside`, which feeds
565    // the page OCR, drops.
566    det_box_px(d, img.width(), img.height())?;
567    rotate_crop(img, &d.quad)
568}
569
570/// A text line's pixels before recognition prep: an oriented crop still to
571/// classify (#666), or a projection strip already prepared.
572pub enum LineSrc {
573    Crop(RgbImage),
574    Prepared(PrepLine),
575}
576
577/// [`prep_region_lines_det`] for `ocr_textline_orientation` (#666): the
578/// same crops — each detected line warped upright out of its minimum-area
579/// rectangle and turned when tall ([`rotate_crop`]) — left unprepared so
580/// the orientation classifier can see them first. The boxes are the same
581/// axis-aligned ones, so reading order and geometry do not move; a region
582/// without detected lines falls back to the projection split as there.
583pub fn region_line_crops_det(
584    img: &RgbImage,
585    regions: &[crate::layout::Region],
586    scale: f32,
587    detected: &[crate::ocr_det::DetBox],
588) -> (Vec<LineBox>, Vec<LineSrc>) {
589    let (iw, ih) = img.dimensions();
590    let mut bboxes = Vec::new();
591    let mut lines = Vec::new();
592    for region in regions {
593        if !is_text_label(region.label) {
594            continue;
595        }
596        let l = (region.l * scale).max(0.0) as u32;
597        let t = (region.t * scale).max(0.0) as u32;
598        let r = ((region.r * scale).max(0.0) as u32).min(iw);
599        let b = ((region.b * scale).max(0.0) as u32).min(ih);
600        if r <= l || b <= t {
601            continue;
602        }
603        let inside = det_boxes_inside(detected, l, t, r, b, iw, ih);
604        if inside.is_empty() {
605            let (bb, pl) = prep_region_lines(img, std::slice::from_ref(region), scale);
606            bboxes.extend(bb);
607            lines.extend(pl.into_iter().map(LineSrc::Prepared));
608            continue;
609        }
610        for ((dl, dt, dr, db), d) in inside {
611            let Some(crop) = rotate_crop(img, &d.quad) else {
612                continue;
613            };
614            bboxes.push((
615                dl as f32 / scale,
616                dt as f32 / scale,
617                dr as f32 / scale,
618                db as f32 / scale,
619            ));
620            lines.push(LineSrc::Crop(crop));
621        }
622    }
623    (bboxes, lines)
624}
625
626/// RapidOCR's `get_rotate_crop_image` (#666): the quad `[tl, tr, br, bl]`
627/// warped onto an upright `w × h` rectangle — `w`, `h` the longer of each
628/// pair of opposite edges, truncated — with bicubic sampling (OpenCV's
629/// `INTER_CUBIC`, a = −0.75) and replicated borders; then, when the crop is
630/// at least 1.5× taller than wide, turned 90° counter-clockwise (numpy's
631/// `rot90`), so vertical text reaches the recognizer lying down. An
632/// axis-aligned integer quad samples exactly its pixels — the plain crop.
633/// `None` for a degenerate quad.
634pub fn rotate_crop(img: &RgbImage, quad: &[(f32, f32); 4]) -> Option<RgbImage> {
635    let dist = |a: (f32, f32), b: (f32, f32)| ((a.0 - b.0).powi(2) + (a.1 - b.1).powi(2)).sqrt();
636    let w = dist(quad[0], quad[1]).max(dist(quad[2], quad[3])) as u32;
637    let h = dist(quad[0], quad[3]).max(dist(quad[1], quad[2])) as u32;
638    if w == 0 || h == 0 || img.width() == 0 || img.height() == 0 {
639        return None;
640    }
641    let (wf, hf) = (w as f64, h as f64);
642    let map = perspective(
643        [(0.0, 0.0), (wf, 0.0), (wf, hf), (0.0, hf)],
644        quad.map(|(x, y)| (x as f64, y as f64)),
645    )?;
646    let mut out = RgbImage::new(w, h);
647    for (x, y, px) in out.enumerate_pixels_mut() {
648        let (x, y) = (x as f64, y as f64);
649        let d = map[6] * x + map[7] * y + 1.0;
650        if d.abs() < 1e-12 {
651            continue;
652        }
653        let sx = (map[0] * x + map[1] * y + map[2]) / d;
654        let sy = (map[3] * x + map[4] * y + map[5]) / d;
655        *px = bicubic_replicate(img, sx, sy);
656    }
657    Some(if h as f32 / w as f32 >= 1.5 {
658        imageops::rotate270(&out)
659    } else {
660        out
661    })
662}
663
664/// The homography taking each `from` point to its `to` point (OpenCV's
665/// `getPerspectiveTransform`): `[a, b, c, d, e, f, g, h]` with
666/// `x' = (ax + by + c) / (gx + hy + 1)`, `y' = (dx + ey + f) / (gx + hy + 1)`.
667/// `None` for a degenerate (collinear) quad.
668fn perspective(from: [(f64, f64); 4], to: [(f64, f64); 4]) -> Option<[f64; 8]> {
669    let mut m = [[0f64; 9]; 8];
670    for k in 0..4 {
671        let ((x, y), (u, v)) = (from[k], to[k]);
672        m[2 * k] = [x, y, 1.0, 0.0, 0.0, 0.0, -x * u, -y * u, u];
673        m[2 * k + 1] = [0.0, 0.0, 0.0, x, y, 1.0, -x * v, -y * v, v];
674    }
675    // Gaussian elimination with partial pivoting.
676    for col in 0..8 {
677        let pivot = (col..8).max_by(|&a, &b| m[a][col].abs().total_cmp(&m[b][col].abs()))?;
678        if m[pivot][col].abs() < 1e-12 {
679            return None;
680        }
681        m.swap(col, pivot);
682        let pivot_row = m[col];
683        for (row, r) in m.iter_mut().enumerate() {
684            let f = r[col] / pivot_row[col];
685            if row != col && f != 0.0 {
686                for (v, p) in r[col..].iter_mut().zip(&pivot_row[col..]) {
687                    *v -= f * p;
688                }
689            }
690        }
691    }
692    Some(std::array::from_fn(|i| m[i][8] / m[i][i]))
693}
694
695/// One bicubic sample at `(x, y)` (OpenCV `INTER_CUBIC`, a = −0.75),
696/// coordinates outside the image clamped to its edge (`BORDER_REPLICATE`).
697fn bicubic_replicate(img: &RgbImage, x: f64, y: f64) -> Rgb<u8> {
698    fn weights(t: f64) -> [f64; 4] {
699        const A: f64 = -0.75;
700        let w0 = ((A * (t + 1.0) - 5.0 * A) * (t + 1.0) + 8.0 * A) * (t + 1.0) - 4.0 * A;
701        let w1 = ((A + 2.0) * t - (A + 3.0)) * t * t + 1.0;
702        let w2 = ((A + 2.0) * (1.0 - t) - (A + 3.0)) * (1.0 - t) * (1.0 - t) + 1.0;
703        [w0, w1, w2, 1.0 - w0 - w1 - w2]
704    }
705    let (x0, y0) = (x.floor(), y.floor());
706    let (wx, wy) = (weights(x - x0), weights(y - y0));
707    let (mw, mh) = (img.width() as i64 - 1, img.height() as i64 - 1);
708    let mut acc = [0f64; 3];
709    for (j, wyj) in wy.iter().enumerate() {
710        let yy = (y0 as i64 - 1 + j as i64).clamp(0, mh) as u32;
711        for (i, wxi) in wx.iter().enumerate() {
712            let xx = (x0 as i64 - 1 + i as i64).clamp(0, mw) as u32;
713            let p = img.get_pixel(xx, yy);
714            let w = wxi * wyj;
715            for c in 0..3 {
716                acc[c] += w * p[c] as f64;
717            }
718        }
719    }
720    Rgb(acc.map(|v| v.round().clamp(0.0, 255.0) as u8))
721}
722
723/// Normalize an image to the scan polarity every stage assumes — dark ink
724/// on light paper (the segmentation threshold and the recognition model's
725/// training data both bake it in): a predominantly dark page (mean luma
726/// below mid-gray — a dark-mode screenshot, an inverted scan) is inverted.
727/// Browser-path helper; the native pipeline never calls it (its input is
728/// scanned paper, and the conformance baseline stays untouched).
729pub fn normalize_polarity(mut img: RgbImage) -> RgbImage {
730    let (w, h) = img.dimensions();
731    if w == 0 || h == 0 {
732        return img;
733    }
734    let mean: f32 = img.pixels().map(luma).sum::<f32>() / (w * h) as f32;
735    if mean < 128.0 {
736        for px in img.pixels_mut() {
737            px.0 = [255 - px.0[0], 255 - px.0[1], 255 - px.0[2]];
738        }
739    }
740    img
741}
742
743/// Whole-image line preparation for the browser OCR path (no layout model:
744/// the page itself is the single text region). Returns page-order prepared
745/// lines; callers that need geometry use [`segment_lines`] directly.
746pub fn prep_page_lines(img: &RgbImage) -> Vec<PrepLine> {
747    segment_lines(img)
748        .into_iter()
749        .filter_map(|(l, t, r, b)| {
750            let line = imageops::crop_imm(img, l, t, r - l, b - t).to_image();
751            prep_line(&line)
752        })
753        .collect()
754}
755
756/// RapidOCR's `text_score` (docling's `RapidOcrOptions.text_score`, 0.5): a
757/// recognized line whose mean emitted-character confidence falls below it is
758/// dropped — a shaded bar, a halftone, a rule read as a run of letters
759/// (`TUANEURHENSTGOFUPHYCLTOSHSWOYT` for a form's grey `GEOGRAPHY` band)
760/// would otherwise land in the document as a heading. `DOCLING_RS_OCR_TEXT_SCORE`
761/// overrides it (`0` keeps every line, the pre-#570 behavior). Cached.
762pub fn text_score() -> f32 {
763    static SCORE: std::sync::OnceLock<f32> = std::sync::OnceLock::new();
764    *SCORE.get_or_init(|| {
765        text_score_from(docling_core::env::nonempty("DOCLING_RS_OCR_TEXT_SCORE").as_deref())
766    })
767}
768
769/// [`text_score`] for one raw `DOCLING_RS_OCR_TEXT_SCORE` value: a score in
770/// 0–1, else RapidOCR's 0.5.
771fn text_score_from(raw: Option<&str>) -> f32 {
772    raw.and_then(|r| r.trim().parse::<f32>().ok())
773        .filter(|s| s.is_finite() && (0.0..=1.0).contains(s))
774        .unwrap_or(0.5)
775}
776
777/// Deterministic recognition batching: page-order line indices grouped by
778/// exact width (equal widths share a run — bit-identical to one-at-a-time
779/// recognition, see `ocr.rs`), each group split into [`REC_BATCH`] chunks.
780pub fn width_batches(lines: &[PrepLine]) -> Vec<(usize, Vec<usize>)> {
781    let mut by_width: std::collections::BTreeMap<usize, Vec<usize>> =
782        std::collections::BTreeMap::new();
783    for (ix, pl) in lines.iter().enumerate() {
784        by_width.entry(pl.w).or_default().push(ix);
785    }
786    let mut out = Vec::new();
787    for (w, ixs) in by_width {
788        for chunk in ixs.chunks(REC_BATCH) {
789            out.push((w, chunk.to_vec()));
790        }
791    }
792    out
793}
794
795/// Pack one width-batch into the model's `(N, 3, H, W)` input buffer.
796pub fn batch_input(w: usize, chunk: &[usize], lines: &[PrepLine]) -> Vec<f32> {
797    let hw = REC_HEIGHT as usize * w;
798    let mut data = vec![0f32; chunk.len() * 3 * hw];
799    for (i, &ix) in chunk.iter().enumerate() {
800        data[i * 3 * hw..(i + 1) * 3 * hw].copy_from_slice(&lines[ix].data);
801    }
802    data
803}
804
805#[cfg(test)]
806mod tests {
807    use super::*;
808
809    /// A synthetic "page": white background with two black text bars.
810    fn page() -> RgbImage {
811        let mut img = RgbImage::from_pixel(200, 100, Rgb([255, 255, 255]));
812        for y in 20..30 {
813            for x in 10..190 {
814                img.put_pixel(x, y, Rgb([0, 0, 0]));
815            }
816        }
817        for y in 60..72 {
818            for x in 10..120 {
819                img.put_pixel(x, y, Rgb([0, 0, 0]));
820            }
821        }
822        img
823    }
824
825    #[test]
826    fn segments_and_preps_page_lines() {
827        let lines = prep_page_lines(&page());
828        assert_eq!(lines.len(), 2);
829        for pl in &lines {
830            assert_eq!(pl.data.len(), 3 * REC_HEIGHT as usize * pl.w);
831        }
832        // Different aspect ratios → different widths → separate batches.
833        let batches = width_batches(&lines);
834        assert_eq!(batches.len(), 2);
835        let (w0, chunk0) = &batches[0];
836        assert_eq!(
837            batch_input(*w0, chunk0, &lines).len(),
838            3 * REC_HEIGHT as usize * w0
839        );
840    }
841
842    /// #570: inside a text region the detector's boxes are the crops — one
843    /// per box, clipped to the region, in the detector's order — and a region
844    /// the detector saw nothing in falls back to the projection strips. The
845    /// two-bar page as one region: two detected boxes → two det-shaped lines;
846    /// the region's second bar split by the detector into two words → three
847    /// lines where the projection found two; no boxes → the projection's two.
848    #[test]
849    fn detector_boxes_are_the_region_lines_with_projection_fallback() {
850        let img = page();
851        let region = crate::layout::Region {
852            label: "text",
853            score: 0.9,
854            l: 0.0,
855            t: 0.0,
856            r: 200.0,
857            b: 100.0,
858        };
859        let bx = |l: f32, t: f32, r: f32, b: f32| crate::ocr_det::DetBox::rect(l, t, r, b, 0.9);
860        let det = [bx(8.0, 18.0, 192.0, 32.0), bx(8.0, 58.0, 122.0, 74.0)];
861        let (boxes, lines) = prep_region_lines_det(&img, std::slice::from_ref(&region), 1.0, &det);
862        assert_eq!(
863            boxes,
864            vec![(8.0, 18.0, 192.0, 32.0), (8.0, 58.0, 122.0, 74.0)]
865        );
866        assert_eq!(lines.len(), 2);
867        // A box reaching past the region is taken whole (clipped only to the
868        // image); one centered outside the region is not its line.
869        let narrow = crate::layout::Region {
870            r: 150.0,
871            ..region.clone()
872        };
873        let det = [bx(-10.0, 18.0, 250.0, 32.0), bx(300.0, 60.0, 400.0, 70.0)];
874        let (boxes, _) = prep_region_lines_det(&img, std::slice::from_ref(&narrow), 1.0, &det);
875        assert_eq!(boxes, vec![(0.0, 18.0, 200.0, 32.0)]);
876        // The detector split the second bar into two words.
877        let det = [
878            bx(8.0, 18.0, 192.0, 32.0),
879            bx(8.0, 58.0, 60.0, 74.0),
880            bx(70.0, 58.0, 122.0, 74.0),
881        ];
882        let (boxes, _) = prep_region_lines_det(&img, std::slice::from_ref(&region), 1.0, &det);
883        assert_eq!(boxes.len(), 3);
884        // No detected box in the region → the projection segmentation.
885        let (fallback, _) = prep_region_lines_det(&img, std::slice::from_ref(&region), 1.0, &[]);
886        let (projection, _) = prep_region_lines(&img, std::slice::from_ref(&region), 1.0);
887        assert_eq!(fallback, projection);
888        assert_eq!(projection.len(), 2);
889        // Table regions take the same boxes as their word crops.
890        let table = crate::layout::Region {
891            label: "table",
892            ..region.clone()
893        };
894        let det = [bx(8.0, 18.0, 92.0, 32.0), bx(108.0, 18.0, 192.0, 32.0)];
895        let (words, _) = prep_table_words_det(&img, std::slice::from_ref(&table), 1.0, &det);
896        assert_eq!(
897            words,
898            vec![(8.0, 18.0, 92.0, 32.0), (108.0, 18.0, 192.0, 32.0)]
899        );
900        let (fallback, _) = prep_table_words_det(&img, std::slice::from_ref(&table), 1.0, &[]);
901        assert_eq!(
902            fallback,
903            prep_table_words(&img, std::slice::from_ref(&table), 1.0).0
904        );
905    }
906
907    /// #939: a detector box's recognizer crop — the quad's warp, which for
908    /// an integer rectangle is exactly its pixels; a box reaching past the
909    /// image edge warps whole (replicated border, RapidOCR's
910    /// `warpPerspective`), one with nothing inside the image → `None` (#947).
911    #[test]
912    fn det_box_crop_matches_the_page_ocr_crop() {
913        let img = RgbImage::from_fn(60, 40, |x, y| Rgb([(x * 4) as u8, (y * 6) as u8, 7]));
914        let d = crate::ocr_det::DetBox::rect(10.0, 5.0, 30.0, 25.0, 0.9);
915        let plain = det_box_crop(&img, &d).expect("crop");
916        assert_eq!(plain.dimensions(), (20, 20));
917        assert_eq!(plain.get_pixel(0, 0), img.get_pixel(10, 5));
918        assert_eq!(
919            plain,
920            image::imageops::crop_imm(&img, 10, 5, 20, 20).to_image()
921        );
922        let edge = crate::ocr_det::DetBox::rect(50.7, -3.0, 80.0, 10.0, 0.9);
923        assert_eq!(det_box_crop(&img, &edge).unwrap().dimensions(), (29, 13));
924        let off = crate::ocr_det::DetBox::rect(70.0, 0.0, 90.0, 10.0, 0.9);
925        assert_eq!(det_box_crop(&img, &off), None);
926        let tiny = RgbImage::from_pixel(2, 2, Rgb([255, 255, 255]));
927        let outside = crate::ocr_det::DetBox::rect(3.0, 0.0, 5.0, 2.0, 0.9);
928        assert_eq!(det_box_crop(&tiny, &outside), None);
929        let inside = crate::ocr_det::DetBox::rect(0.0, 0.0, 2.0, 2.0, 0.9);
930        assert_eq!(det_box_crop(&tiny, &inside).unwrap().dimensions(), (2, 2));
931    }
932
933    /// #981: a detected line on a skewed scan is cropped out of the
934    /// detector's rotated rectangle, not its axis-aligned bounding box — a
935    /// 60 × 12 bar at 8° has a 62 × 20 bounding box, whose crop would reach
936    /// the recognizer squeezed to 146 px wide and slanted; the warp gives
937    /// the bar's own 60 × 12, i.e. 240 px at the 48 px height. The cell's
938    /// geometry stays the bounding box.
939    #[test]
940    fn a_skewed_detected_line_is_warped_upright_not_boxed() {
941        let (cx, cy, hw, hh, a) = (60.0f32, 40.0f32, 30.0f32, 6.0f32, 8.0f32.to_radians());
942        let img = RgbImage::from_fn(120, 80, |x, y| {
943            let (dx, dy) = (x as f32 + 0.5 - cx, y as f32 + 0.5 - cy);
944            let (u, v) = (dx * a.cos() + dy * a.sin(), -dx * a.sin() + dy * a.cos());
945            if u.abs() <= hw && v.abs() <= hh {
946                Rgb([0, 0, 0])
947            } else {
948                Rgb([255, 255, 255])
949            }
950        });
951        let corner = |su: f32, sv: f32| {
952            (
953                cx + su * hw * a.cos() - sv * hh * a.sin(),
954                cy + su * hw * a.sin() + sv * hh * a.cos(),
955            )
956        };
957        let quad = crate::ocr_det::order_corners([
958            corner(-1.0, -1.0),
959            corner(1.0, -1.0),
960            corner(1.0, 1.0),
961            corner(-1.0, 1.0),
962        ]);
963        let (xs, ys): (Vec<f32>, Vec<f32>) = quad.iter().copied().unzip();
964        let (l, t) = (
965            xs.iter().cloned().fold(f32::MAX, f32::min),
966            ys.iter().cloned().fold(f32::MAX, f32::min),
967        );
968        let (r, b) = (
969            xs.iter().cloned().fold(f32::MIN, f32::max),
970            ys.iter().cloned().fold(f32::MIN, f32::max),
971        );
972        let det = crate::ocr_det::DetBox {
973            l,
974            t,
975            r,
976            b,
977            score: 0.9,
978            quad,
979        };
980        let region = crate::layout::Region {
981            label: "text",
982            score: 0.9,
983            l: 0.0,
984            t: 0.0,
985            r: 120.0,
986            b: 80.0,
987        };
988        let (boxes, lines) =
989            prep_region_lines_det(&img, std::slice::from_ref(&region), 1.0, &[det]);
990        assert_eq!(lines.len(), 1);
991        let bbox_w = (((r.floor() - l.floor()) * 48.0) / (b.floor() - t.floor())).round() as usize;
992        assert!(bbox_w < 160, "bounding box would give {bbox_w}");
993        assert!(
994            (236..=240).contains(&lines[0].w),
995            "warped width {}",
996            lines[0].w
997        );
998        // Geometry: the bounding box, in whole pixels.
999        assert_eq!(
1000            boxes,
1001            vec![(
1002                l as u32 as f32,
1003                t as u32 as f32,
1004                r as u32 as f32,
1005                b as u32 as f32
1006            )]
1007        );
1008        let (words, _) = prep_table_words_det(
1009            &img,
1010            &[crate::layout::Region {
1011                label: "table",
1012                score: 0.9,
1013                l: 0.0,
1014                t: 0.0,
1015                r: 120.0,
1016                b: 80.0,
1017            }],
1018            1.0,
1019            &[det],
1020        );
1021        assert_eq!(words, boxes);
1022    }
1023
1024    /// RapidOCR's `text_score` default, the pre-#570 "keep everything" with
1025    /// `0`, and garbage values ignored.
1026    #[test]
1027    fn text_score_default_is_rapidocrs() {
1028        assert_eq!(text_score_from(None), 0.5);
1029        assert_eq!(text_score_from(Some("0")), 0.0);
1030        assert_eq!(text_score_from(Some("0.7")), 0.7);
1031        for garbage in ["1.5", "-0.1", "NaN", "high", ""] {
1032            assert_eq!(text_score_from(Some(garbage)), 0.5, "{garbage:?}");
1033        }
1034    }
1035
1036    #[test]
1037    fn dark_mode_pages_normalize_to_scan_polarity() {
1038        // The same two-bar page, inverted (light text on dark) — the raw
1039        // segmentation misfires (it thresholds the dark *background* as ink,
1040        // so the "lines" it finds are the inter-bar gaps, not the bars);
1041        // polarity normalization recovers the true structure.
1042        let mut dark = page();
1043        for px in dark.pixels_mut() {
1044            px.0 = [255 - px.0[0], 255 - px.0[1], 255 - px.0[2]];
1045        }
1046        assert_ne!(segment_lines(&dark), segment_lines(&page()));
1047        let fixed = normalize_polarity(dark);
1048        assert_eq!(segment_lines(&fixed), segment_lines(&page()));
1049        assert_eq!(prep_page_lines(&fixed).len(), 2);
1050        // A light page passes through untouched.
1051        let light = page();
1052        assert_eq!(normalize_polarity(light.clone()), light);
1053    }
1054
1055    #[test]
1056    fn ctc_decode_collapses_repeats_and_blanks() {
1057        // 3 classes: blank, "a", "b"; timesteps a a blank b b → "ab".
1058        let chars = dict_chars("a\nb");
1059        assert_eq!(chars.len(), 4); // blank, a, b, space
1060        let probs = [
1061            0.1, 0.8, 0.1, 0.0, // a
1062            0.1, 0.8, 0.1, 0.0, // a (repeat collapses)
1063            0.9, 0.05, 0.05, 0.0, // blank
1064            0.1, 0.1, 0.8, 0.0, // b
1065            0.1, 0.1, 0.8, 0.0, // b (repeat collapses)
1066        ];
1067        assert_eq!(decode_row(&chars, &probs, 4), "ab");
1068    }
1069}
1070
1071#[cfg(test)]
1072mod word_segmentation {
1073    use image::{Rgb, RgbImage};
1074
1075    /// A white line carrying two ink blocks separated by `gap` pixels.
1076    fn line_with_gap(h: u32, gap: u32) -> RgbImage {
1077        let w = 30 + gap + 30 + 10;
1078        let mut img = RgbImage::from_pixel(w, h, Rgb([255, 255, 255]));
1079        for (x0, x1) in [(5u32, 35u32), (35 + gap, 65 + gap)] {
1080            for x in x0..x1.min(w) {
1081                for y in h / 4..(3 * h / 4) {
1082                    img.put_pixel(x, y, Rgb([0, 0, 0]));
1083                }
1084            }
1085        }
1086        img
1087    }
1088
1089    /// [`segment_words`] splits on a gap of `0.6 x line height`, which is the
1090    /// property the rest of the browser table path inherits: an inter-word
1091    /// space never splits, but a column gap wider than that does. Anything
1092    /// narrower stays a single box — and a single box can only ever land in one
1093    /// TableFormer cell, so this threshold is the floor on how tight a table's
1094    /// columns may be before its cells merge.
1095    #[test]
1096    fn words_split_only_on_gaps_above_six_tenths_of_the_line_height() {
1097        for h in [16u32, 24, 32, 40] {
1098            let split_at = (1..=40u32)
1099                .find(|&gap| super::segment_words(&line_with_gap(h, gap)).len() >= 2)
1100                .expect("some gap splits");
1101            let ratio = split_at as f32 / h as f32;
1102            assert!(
1103                (0.5..=0.65).contains(&ratio),
1104                "h={h}: split at {split_at}px ({ratio:.2} x height)"
1105            );
1106            // Just below the threshold the two blocks are one word.
1107            assert_eq!(
1108                super::segment_words(&line_with_gap(h, split_at - 1)).len(),
1109                1,
1110                "h={h}: a narrower gap must not split"
1111            );
1112        }
1113    }
1114
1115    /// #666: an axis-aligned integer quad warps to exactly its pixels — the
1116    /// plain crop, so upright lines prepare as before — and a crop at least
1117    /// 1.5× taller than wide is turned 90° counter-clockwise.
1118    #[test]
1119    fn rotate_crop_is_the_plain_crop_for_an_upright_box_and_turns_tall_ones() {
1120        let img = RgbImage::from_fn(40, 30, |x, y| Rgb([(x * 6) as u8, (y * 8) as u8, 77]));
1121        let quad = [(5.0, 4.0), (25.0, 4.0), (25.0, 14.0), (5.0, 14.0)];
1122        let warped = super::rotate_crop(&img, &quad).unwrap();
1123        let plain = image::imageops::crop_imm(&img, 5, 4, 20, 10).to_image();
1124        assert_eq!(warped, plain);
1125        // 6 wide, 12 tall: turned to 12 × 6, the top row now the left column.
1126        let tall = [(10.0, 2.0), (16.0, 2.0), (16.0, 14.0), (10.0, 14.0)];
1127        let turned = super::rotate_crop(&img, &tall).unwrap();
1128        assert_eq!(turned.dimensions(), (12, 6));
1129        assert_eq!(turned.get_pixel(0, 5), img.get_pixel(10, 2));
1130        assert!(super::rotate_crop(&img, &[(1.0, 1.0); 4]).is_none());
1131    }
1132
1133    /// A rectangle drawn at 30° comes out upright: its warp is a uniform
1134    /// dark bar, whatever the angle.
1135    #[test]
1136    fn rotate_crop_straightens_a_rotated_rectangle() {
1137        let (cx, cy, a) = (50.0f32, 40.0f32, 30f32.to_radians());
1138        let (hw, hh) = (30.0f32, 6.0f32);
1139        let inside = |x: f32, y: f32| {
1140            let (dx, dy) = (x - cx, y - cy);
1141            let u = dx * a.cos() + dy * a.sin();
1142            let v = -dx * a.sin() + dy * a.cos();
1143            u.abs() <= hw && v.abs() <= hh
1144        };
1145        let img = RgbImage::from_fn(100, 80, |x, y| {
1146            if inside(x as f32 + 0.5, y as f32 + 0.5) {
1147                Rgb([0, 0, 0])
1148            } else {
1149                Rgb([255, 255, 255])
1150            }
1151        });
1152        let corner = |su: f32, sv: f32| {
1153            (
1154                cx + su * hw * a.cos() - sv * hh * a.sin(),
1155                cy + su * hw * a.sin() + sv * hh * a.cos(),
1156            )
1157        };
1158        let quad = crate::ocr_det::order_corners([
1159            corner(-1.0, -1.0),
1160            corner(1.0, -1.0),
1161            corner(1.0, 1.0),
1162            corner(-1.0, 1.0),
1163        ]);
1164        let bar = super::rotate_crop(&img, &quad).unwrap();
1165        // 60 × 12, less what float edge lengths truncate.
1166        let (w, h) = bar.dimensions();
1167        assert!(
1168            (59..=60).contains(&w) && (11..=12).contains(&h),
1169            "{w} × {h}"
1170        );
1171        // The interior (away from the anti-aliased edge) is all ink.
1172        for y in 2..h - 2 {
1173            for x in 2..w - 2 {
1174                assert!(
1175                    bar.get_pixel(x, y)[0] < 60,
1176                    "({x}, {y}) {:?}",
1177                    bar.get_pixel(x, y)
1178                );
1179            }
1180        }
1181    }
1182}