jbig2enc-rust 0.5.3

JBIG2 encoder implementation in Rust with PDF fragment support
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
use crate::jbig2classify::{FamilyBucketKey, SymbolSignature, family_bucket_key_for_symbol};
use crate::jbig2enc::PageData;
use crate::jbig2structs::SymbolContextMode;
use crate::jbig2sym::Rect;
use rustc_hash::FxHashMap;

#[derive(Debug, Clone, Default)]
pub struct SymbolContextModel {
    symbol_contexts: Vec<SymbolContextStats>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContextDecision {
    Allow,
    Reject,
    Unknown,
}

#[derive(Debug, Clone, Copy, Default)]
pub struct ContextSimilarity {
    pub evidence: usize,
    pub left_jaccard: f32,
    pub right_jaccard: f32,
    pub left_cosine: f32,
    pub right_cosine: f32,
    pub left_jsd: f32,
    pub right_jsd: f32,
    pub start_delta: f32,
    pub end_delta: f32,
}

#[derive(Debug, Clone, Default)]
struct SymbolContextStats {
    occurrences: usize,
    left_tokens: FxHashMap<u64, u16>,
    right_tokens: FxHashMap<u64, u16>,
    start_like: usize,
    end_like: usize,
}

#[derive(Debug, Clone, Copy)]
struct LineInstance {
    symbol_index: usize,
    x: i32,
    y: i32,
    width: i32,
    height: i32,
}

#[derive(Debug, Clone, Default)]
struct LineGroup {
    min_y: i32,
    max_y: i32,
    instances: Vec<LineInstance>,
}

impl LineGroup {
    fn can_accept(&self, instance: LineInstance) -> bool {
        let center_y = instance.y + instance.height / 2;
        let line_center = (self.min_y + self.max_y) / 2;
        let tolerance = ((self.max_y - self.min_y).max(instance.height) / 2).max(3);
        (center_y - line_center).abs() <= tolerance
    }

    fn push(&mut self, instance: LineInstance) {
        self.min_y = self.min_y.min(instance.y);
        self.max_y = self.max_y.max(instance.y + instance.height);
        self.instances.push(instance);
    }
}

pub fn build_symbol_context_model(
    pages: &[PageData],
    symbols: &[crate::jbig2sym::BitImage],
    signatures: &[SymbolSignature],
) -> SymbolContextModel {
    let bucket_keys: Vec<FamilyBucketKey> = symbols
        .iter()
        .zip(signatures.iter())
        .map(|(symbol, signature)| family_bucket_key_for_symbol(symbol, signature))
        .collect();
    let mut symbol_contexts = vec![SymbolContextStats::default(); symbols.len()];

    for page in pages {
        let mut lines: Vec<LineGroup> = Vec::new();
        let mut instances: Vec<LineInstance> = page
            .symbol_instances
            .iter()
            .map(|instance| line_instance(instance.symbol_index, instance.position))
            .collect();
        instances.sort_unstable_by_key(|instance| (instance.y, instance.x));

        for instance in instances {
            if let Some(line) = lines.iter_mut().find(|line| line.can_accept(instance)) {
                line.push(instance);
            } else {
                lines.push(LineGroup {
                    min_y: instance.y,
                    max_y: instance.y + instance.height,
                    instances: vec![instance],
                });
            }
        }

        for line in &mut lines {
            line.instances.sort_unstable_by_key(|instance| instance.x);
            for (idx, instance) in line.instances.iter().enumerate() {
                let stats = &mut symbol_contexts[instance.symbol_index];
                stats.occurrences += 1;

                let left_gap = idx
                    .checked_sub(1)
                    .and_then(|i| line.instances.get(i))
                    .map(|left| {
                        bump_token(
                            &mut stats.left_tokens,
                            context_token(
                                true,
                                bucket_keys[left.symbol_index],
                                spacing_bin(*instance, *left, true),
                            ),
                        );
                        instance.x - (left.x + left.width)
                    });
                let right_gap = line.instances.get(idx + 1).map(|right| {
                    bump_token(
                        &mut stats.right_tokens,
                        context_token(
                            false,
                            bucket_keys[right.symbol_index],
                            spacing_bin(*instance, *right, false),
                        ),
                    );
                    right.x - (instance.x + instance.width)
                });

                if left_gap.is_none_or(|gap| gap >= word_gap_threshold(*instance)) {
                    stats.start_like += 1;
                }
                if right_gap.is_none_or(|gap| gap >= word_gap_threshold(*instance)) {
                    stats.end_like += 1;
                }
            }
        }
    }

    SymbolContextModel { symbol_contexts }
}

impl SymbolContextModel {
    pub fn similarity(&self, lhs: usize, rhs: usize) -> Option<ContextSimilarity> {
        let lhs_stats = self.symbol_contexts.get(lhs)?;
        let rhs_stats = self.symbol_contexts.get(rhs)?;

        let evidence = lhs_stats.occurrences.min(rhs_stats.occurrences);
        if evidence == 0 {
            return None;
        }

        Some(ContextSimilarity {
            evidence,
            left_jaccard: weighted_jaccard(&lhs_stats.left_tokens, &rhs_stats.left_tokens),
            right_jaccard: weighted_jaccard(&lhs_stats.right_tokens, &rhs_stats.right_tokens),
            left_cosine: cosine_similarity(&lhs_stats.left_tokens, &rhs_stats.left_tokens),
            right_cosine: cosine_similarity(&lhs_stats.right_tokens, &rhs_stats.right_tokens),
            left_jsd: jensen_shannon_divergence(&lhs_stats.left_tokens, &rhs_stats.left_tokens),
            right_jsd: jensen_shannon_divergence(&lhs_stats.right_tokens, &rhs_stats.right_tokens),
            start_delta: ratio_delta(
                lhs_stats.start_like,
                lhs_stats.occurrences,
                rhs_stats.start_like,
                rhs_stats.occurrences,
            ),
            end_delta: ratio_delta(
                lhs_stats.end_like,
                lhs_stats.occurrences,
                rhs_stats.end_like,
                rhs_stats.occurrences,
            ),
        })
    }

    pub fn merge_decision(
        &self,
        lhs: usize,
        rhs: usize,
        mode: SymbolContextMode,
    ) -> ContextDecision {
        let Some(lhs_stats) = self.symbol_contexts.get(lhs) else {
            return ContextDecision::Unknown;
        };
        let Some(rhs_stats) = self.symbol_contexts.get(rhs) else {
            return ContextDecision::Unknown;
        };
        let Some(similarity) = self.similarity(lhs, rhs) else {
            return ContextDecision::Unknown;
        };

        let left_mass: u32 = lhs_stats
            .left_tokens
            .values()
            .map(|&v| v as u32)
            .sum::<u32>()
            .min(
                rhs_stats
                    .left_tokens
                    .values()
                    .map(|&v| v as u32)
                    .sum::<u32>(),
            );
        let right_mass: u32 = lhs_stats
            .right_tokens
            .values()
            .map(|&v| v as u32)
            .sum::<u32>()
            .min(
                rhs_stats
                    .right_tokens
                    .values()
                    .map(|&v| v as u32)
                    .sum::<u32>(),
            );

        if similarity.evidence < 4 || left_mass + right_mass < 4 {
            return ContextDecision::Unknown;
        }

        let combined_jaccard = 0.5 * (similarity.left_jaccard + similarity.right_jaccard);
        let combined_cosine = 0.5 * (similarity.left_cosine + similarity.right_cosine);
        let max_jsd = similarity.left_jsd.max(similarity.right_jsd);
        let edge_delta = similarity.start_delta.max(similarity.end_delta);

        match mode {
            SymbolContextMode::WeightedJaccard => {
                if combined_jaccard >= 0.55
                    && similarity.left_jaccard >= 0.40
                    && similarity.right_jaccard >= 0.40
                    && edge_delta <= 0.50
                {
                    ContextDecision::Allow
                } else if similarity.evidence >= 6
                    && (combined_jaccard <= 0.15 || edge_delta >= 0.75)
                {
                    ContextDecision::Reject
                } else {
                    ContextDecision::Unknown
                }
            }
            SymbolContextMode::Cosine => {
                if combined_cosine >= 0.82
                    && similarity.left_cosine >= 0.70
                    && similarity.right_cosine >= 0.70
                    && edge_delta <= 0.45
                {
                    ContextDecision::Allow
                } else if similarity.evidence >= 6
                    && (combined_cosine <= 0.35 || edge_delta >= 0.75)
                {
                    ContextDecision::Reject
                } else {
                    ContextDecision::Unknown
                }
            }
            SymbolContextMode::Hybrid => {
                if combined_jaccard >= 0.42
                    && combined_cosine >= 0.72
                    && max_jsd <= 0.45
                    && edge_delta <= 0.40
                {
                    ContextDecision::Allow
                } else if similarity.evidence >= 6
                    && (combined_jaccard <= 0.12
                        || combined_cosine <= 0.35
                        || max_jsd >= 0.75
                        || edge_delta >= 0.75)
                {
                    ContextDecision::Reject
                } else {
                    ContextDecision::Unknown
                }
            }
        }
    }
}

fn line_instance(symbol_index: usize, rect: Rect) -> LineInstance {
    LineInstance {
        symbol_index,
        x: rect.x as i32,
        y: rect.y as i32,
        width: rect.width as i32,
        height: rect.height as i32,
    }
}

#[inline]
fn bump_token(map: &mut FxHashMap<u64, u16>, token: u64) {
    let entry = map.entry(token).or_insert(0);
    *entry = entry.saturating_add(1);
}

#[inline]
fn context_token(is_left: bool, key: FamilyBucketKey, spacing_bin: u8) -> u64 {
    let dir = if is_left { 1u64 } else { 2u64 };
    (dir << 56)
        | ((key.w_bin as u64) << 40)
        | ((key.h_bin as u64) << 24)
        | ((key.density_bin as u64) << 16)
        | ((key.aspect_bin as u64) << 12)
        | ((key.centroid_y_bin as u64) << 8)
        | ((key.lr_balance_bin as u64) << 4)
        | (spacing_bin as u64)
}

#[inline]
fn spacing_bin(curr: LineInstance, neighbor: LineInstance, is_left: bool) -> u8 {
    let gap = if is_left {
        curr.x - (neighbor.x + neighbor.width)
    } else {
        neighbor.x - (curr.x + curr.width)
    };

    if gap <= 0 {
        0
    } else if gap <= 2 {
        1
    } else if gap <= 6 {
        2
    } else {
        3
    }
}

#[inline]
fn word_gap_threshold(instance: LineInstance) -> i32 {
    (instance.width.max(instance.height) / 2).clamp(3, 8)
}

fn weighted_jaccard(a: &FxHashMap<u64, u16>, b: &FxHashMap<u64, u16>) -> f32 {
    let mut min_sum = 0u32;
    let mut max_sum = 0u32;
    let mut seen = rustc_hash::FxHashSet::default();

    for (&k, &av) in a {
        let bv = b.get(&k).copied().unwrap_or(0);
        min_sum += av.min(bv) as u32;
        max_sum += av.max(bv) as u32;
        seen.insert(k);
    }

    for (&k, &bv) in b {
        if seen.contains(&k) {
            continue;
        }
        max_sum += bv as u32;
    }

    if max_sum == 0 {
        1.0
    } else {
        min_sum as f32 / max_sum as f32
    }
}

fn cosine_similarity(a: &FxHashMap<u64, u16>, b: &FxHashMap<u64, u16>) -> f32 {
    let mut dot = 0f32;
    let mut norm_a = 0f32;
    let mut norm_b = 0f32;

    for &av in a.values() {
        let av = av as f32;
        norm_a += av * av;
    }
    for &bv in b.values() {
        let bv = bv as f32;
        norm_b += bv * bv;
    }
    for (&k, &av) in a {
        if let Some(&bv) = b.get(&k) {
            dot += (av as f32) * (bv as f32);
        }
    }

    if norm_a == 0.0 || norm_b == 0.0 {
        0.0
    } else {
        dot / (norm_a.sqrt() * norm_b.sqrt())
    }
}

fn jensen_shannon_divergence(a: &FxHashMap<u64, u16>, b: &FxHashMap<u64, u16>) -> f32 {
    let mut keys = rustc_hash::FxHashSet::default();
    for &k in a.keys() {
        keys.insert(k);
    }
    for &k in b.keys() {
        keys.insert(k);
    }

    let total_a: f32 = a.values().map(|&v| v as f32).sum::<f32>() + keys.len() as f32;
    let total_b: f32 = b.values().map(|&v| v as f32).sum::<f32>() + keys.len() as f32;

    let mut js = 0.0f32;

    for &k in &keys {
        let pa = (a.get(&k).copied().unwrap_or(0) as f32 + 1.0) / total_a;
        let pb = (b.get(&k).copied().unwrap_or(0) as f32 + 1.0) / total_b;
        let m = 0.5 * (pa + pb);

        js += 0.5 * pa * (pa / m).ln();
        js += 0.5 * pb * (pb / m).ln();
    }

    js
}

#[inline]
fn ratio_delta(lhs_hits: usize, lhs_total: usize, rhs_hits: usize, rhs_total: usize) -> f32 {
    let lhs_ratio = if lhs_total == 0 {
        0.0
    } else {
        lhs_hits as f32 / lhs_total as f32
    };
    let rhs_ratio = if rhs_total == 0 {
        0.0
    } else {
        rhs_hits as f32 / rhs_total as f32
    };
    (lhs_ratio - rhs_ratio).abs()
}