j2k-transcode 0.7.0

JPEG-to-HTJ2K coefficient-domain transcode primitives for j2k
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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Error metrics for coefficient-domain validation.

use core::{fmt, mem::size_of};

use j2k_core::{try_host_vec_with_capacity, DEFAULT_MAX_HOST_ALLOCATION_BYTES};

/// One sorted absolute-error histogram bucket.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ErrorHistogramBucket {
    absolute_error: i64,
    count: usize,
}

impl ErrorHistogramBucket {
    /// Absolute coefficient error represented by this bucket.
    #[must_use]
    pub const fn absolute_error(self) -> i64 {
        self.absolute_error
    }

    /// Number of coefficients in this bucket.
    #[must_use]
    pub const fn count(self) -> usize {
        self.count
    }
}

/// Sorted, move-only absolute-error histogram.
///
/// Storage is a single fallibly reserved vector. Construction sorts and
/// coalesces that owner in place, so no second coefficient-sized allocation is
/// needed and its allocator-reported capacity remains inspectable.
#[derive(Debug, PartialEq, Eq)]
pub struct ErrorHistogram {
    buckets: Vec<ErrorHistogramBucket>,
}

impl ErrorHistogram {
    /// Number of distinct absolute-error buckets.
    #[must_use]
    pub fn len(&self) -> usize {
        self.buckets.len()
    }

    /// Whether the histogram contains no buckets.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.buckets.is_empty()
    }

    /// Count for one absolute error, or zero when that bucket is absent.
    #[must_use]
    pub fn count(&self, absolute_error: i64) -> usize {
        self.buckets
            .binary_search_by_key(&absolute_error, |bucket| bucket.absolute_error)
            .ok()
            .map_or(0, |index| self.buckets[index].count)
    }

    /// Iterate over sorted histogram buckets.
    pub fn iter(&self) -> impl ExactSizeIterator<Item = ErrorHistogramBucket> + '_ {
        self.buckets.iter().copied()
    }

    /// Allocator-reported bytes retained by the histogram backing vector.
    pub fn retained_bytes(&self) -> Result<usize, MetricsError> {
        checked_histogram_bytes(self.buckets.capacity(), DEFAULT_MAX_HOST_ALLOCATION_BYTES)
    }
}

impl IntoIterator for ErrorHistogram {
    type Item = ErrorHistogramBucket;
    type IntoIter = std::vec::IntoIter<ErrorHistogramBucket>;

    fn into_iter(self) -> Self::IntoIter {
        self.buckets.into_iter()
    }
}

/// Difference summary between two integer coefficient vectors.
#[derive(Debug, PartialEq, Eq)]
pub struct ErrorMetrics {
    /// Number of compared coefficients.
    pub total: usize,
    /// Number of coefficients with exact equality.
    pub exact_matches: usize,
    /// Maximum absolute coefficient error.
    pub max_abs_error: i64,
    /// Absolute-error histogram keyed by LSB distance.
    pub absolute_error_histogram: ErrorHistogram,
}

impl ErrorMetrics {
    /// Fraction of coefficients that match exactly.
    #[must_use]
    #[expect(
        clippy::cast_precision_loss,
        reason = "validation rates are intentionally reported as approximate f64 ratios"
    )]
    pub fn exact_match_rate(&self) -> f64 {
        if self.total == 0 {
            return 1.0;
        }

        self.exact_matches as f64 / self.total as f64
    }

    /// Number of coefficients at the given absolute error.
    #[must_use]
    pub fn absolute_error_count(&self, absolute_error: i64) -> usize {
        self.absolute_error_histogram.count(absolute_error)
    }

    /// Whether the metrics satisfy a one-LSB-bounded claim at the requested
    /// exact-match threshold.
    #[must_use]
    pub fn is_one_lsb_bounded(&self, exact_match_threshold: f64) -> bool {
        self.max_abs_error <= 1 && self.exact_match_rate() >= exact_match_threshold
    }
}

/// Typed validation-metrics construction failure.
#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum MetricsError {
    /// Actual and expected coefficients describe different sample counts.
    LengthMismatch {
        /// Actual coefficient count.
        actual: usize,
        /// Expected coefficient count.
        expected: usize,
    },
    /// Input owners plus histogram storage exceed the shared host cap.
    MemoryCapExceeded {
        /// Required live bytes, saturated on arithmetic overflow.
        requested: usize,
        /// Maximum accepted live bytes.
        cap: usize,
    },
    /// Histogram storage could not be reserved.
    HostAllocationFailed {
        /// Requested histogram allocation bytes.
        bytes: usize,
    },
}

impl fmt::Display for MetricsError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LengthMismatch { actual, expected } => {
                write!(
                    f,
                    "metric input lengths differ: actual {actual}, expected {expected}"
                )
            }
            Self::MemoryCapExceeded { requested, cap } => write!(
                f,
                "metrics host workspace requires {requested} bytes, exceeding the {cap}-byte cap"
            ),
            Self::HostAllocationFailed { bytes } => {
                write!(f, "metrics host allocation failed for {bytes} bytes")
            }
        }
    }
}

impl std::error::Error for MetricsError {}

/// Compute exact-match rate, max absolute error, and absolute-LSB histogram for
/// two integer coefficient vectors.
pub fn error_metrics_i32(actual: &[i32], expected: &[i32]) -> Result<ErrorMetrics, MetricsError> {
    error_metrics_i32_with_live_budget(actual, expected, 0, DEFAULT_MAX_HOST_ALLOCATION_BYTES)
}

pub(crate) fn error_metrics_i32_with_live_budget(
    actual: &[i32],
    expected: &[i32],
    external_live_bytes: usize,
    cap: usize,
) -> Result<ErrorMetrics, MetricsError> {
    if actual.len() != expected.len() {
        return Err(MetricsError::LengthMismatch {
            actual: actual.len(),
            expected: expected.len(),
        });
    }

    checked_histogram_live_capacity(external_live_bytes, actual.len(), cap)?;
    let mut buckets = try_host_vec_with_capacity(actual.len()).map_err(|error| {
        MetricsError::HostAllocationFailed {
            bytes: error.requested_bytes(),
        }
    })?;
    checked_histogram_live_capacity(external_live_bytes, buckets.capacity(), cap)?;

    let mut exact_matches = 0;
    let mut max_abs_error = 0;

    for (&actual, &expected) in actual.iter().zip(expected.iter()) {
        let abs_error = (i64::from(actual) - i64::from(expected)).abs();
        if abs_error == 0 {
            exact_matches += 1;
        }
        max_abs_error = max_abs_error.max(abs_error);
        buckets.push(ErrorHistogramBucket {
            absolute_error: abs_error,
            count: 1,
        });
    }

    buckets.sort_unstable_by_key(|bucket| bucket.absolute_error);
    let mut output_len = 0usize;
    for input_index in 0..buckets.len() {
        let bucket = buckets[input_index];
        if output_len > 0 && buckets[output_len - 1].absolute_error == bucket.absolute_error {
            buckets[output_len - 1].count = buckets[output_len - 1]
                .count
                .checked_add(bucket.count)
                .ok_or_else(cap_overflow)?;
        } else {
            buckets[output_len] = bucket;
            output_len += 1;
        }
    }
    buckets.truncate(output_len);

    Ok(ErrorMetrics {
        total: actual.len(),
        exact_matches,
        max_abs_error,
        absolute_error_histogram: ErrorHistogram { buckets },
    })
}

fn checked_histogram_bytes(capacity: usize, cap: usize) -> Result<usize, MetricsError> {
    capacity
        .checked_mul(size_of::<ErrorHistogramBucket>())
        .ok_or(MetricsError::MemoryCapExceeded {
            requested: usize::MAX,
            cap,
        })
}

fn checked_histogram_live_capacity(
    external_live_bytes: usize,
    histogram_capacity: usize,
    cap: usize,
) -> Result<usize, MetricsError> {
    let histogram_bytes = checked_histogram_bytes(histogram_capacity, cap)?;
    checked_metrics_live_bytes(external_live_bytes, histogram_bytes, cap)
}

fn checked_metrics_live_bytes(
    external_live_bytes: usize,
    histogram_bytes: usize,
    cap: usize,
) -> Result<usize, MetricsError> {
    let requested = external_live_bytes.checked_add(histogram_bytes).ok_or(
        MetricsError::MemoryCapExceeded {
            requested: usize::MAX,
            cap,
        },
    )?;
    if requested > cap {
        return Err(MetricsError::MemoryCapExceeded { requested, cap });
    }
    Ok(requested)
}

fn cap_overflow() -> MetricsError {
    MetricsError::MemoryCapExceeded {
        requested: usize::MAX,
        cap: DEFAULT_MAX_HOST_ALLOCATION_BYTES,
    }
}

#[cfg(test)]
mod tests {
    use core::mem::size_of;

    use super::{
        cap_overflow, checked_histogram_live_capacity, error_metrics_i32, ErrorHistogramBucket,
        MetricsError, DEFAULT_MAX_HOST_ALLOCATION_BYTES,
    };

    #[test]
    fn histogram_is_sorted_and_coalesced_in_place() -> Result<(), MetricsError> {
        let metrics = error_metrics_i32(&[10, 3, -4, 9, 8], &[10, 1, -3, 8, 10])?;
        let buckets = metrics
            .absolute_error_histogram
            .iter()
            .map(|bucket| (bucket.absolute_error(), bucket.count()))
            .collect::<Vec<_>>();

        assert_eq!(buckets, [(0, 1), (1, 2), (2, 2)]);
        assert_eq!(metrics.absolute_error_count(0), 1);
        assert_eq!(metrics.absolute_error_count(7), 0);
        assert_eq!(metrics.max_abs_error, 2);
        Ok(())
    }

    #[test]
    fn all_unique_errors_keep_one_sorted_bucket_per_coefficient() -> Result<(), MetricsError> {
        let metrics = error_metrics_i32(&[0, 1, 2, 3], &[0, 0, 0, 0])?;
        let buckets = metrics
            .absolute_error_histogram
            .into_iter()
            .map(|bucket| (bucket.absolute_error(), bucket.count()))
            .collect::<Vec<_>>();

        assert_eq!(buckets, [(0, 1), (1, 1), (2, 1), (3, 1)]);
        Ok(())
    }

    #[test]
    fn histogram_length_and_empty_contracts_cover_both_states() -> Result<(), MetricsError> {
        let empty = error_metrics_i32(&[], &[])?.absolute_error_histogram;
        assert!(empty.is_empty());
        assert_eq!(empty.len(), 0);

        let populated = error_metrics_i32(&[2, 5, 9], &[2, 3, 6])?.absolute_error_histogram;
        assert!(!populated.is_empty());
        assert_eq!(populated.len(), 3);
        Ok(())
    }

    #[test]
    fn metrics_error_display_and_overflow_contracts_are_stable() {
        let errors = [
            MetricsError::LengthMismatch {
                actual: 3,
                expected: 2,
            },
            MetricsError::MemoryCapExceeded {
                requested: 17,
                cap: 16,
            },
            MetricsError::HostAllocationFailed { bytes: 32 },
        ];
        assert_eq!(
            errors.map(|error| error.to_string()),
            [
                "metric input lengths differ: actual 3, expected 2",
                "metrics host workspace requires 17 bytes, exceeding the 16-byte cap",
                "metrics host allocation failed for 32 bytes",
            ]
        );

        assert_eq!(
            cap_overflow(),
            MetricsError::MemoryCapExceeded {
                requested: usize::MAX,
                cap: DEFAULT_MAX_HOST_ALLOCATION_BYTES,
            }
        );
    }

    #[test]
    fn length_mismatch_remains_typed() {
        assert_eq!(
            error_metrics_i32(&[1, 2], &[1]),
            Err(MetricsError::LengthMismatch {
                actual: 2,
                expected: 1,
            })
        );
    }

    #[test]
    fn histogram_live_budget_accepts_exact_cap_and_rejects_one_over() {
        let bucket_bytes = size_of::<ErrorHistogramBucket>();
        let cap = bucket_bytes * 3;

        assert_eq!(
            checked_histogram_live_capacity(bucket_bytes, 2, cap),
            Ok(cap)
        );
        assert_eq!(
            checked_histogram_live_capacity(bucket_bytes + 1, 2, cap),
            Err(MetricsError::MemoryCapExceeded {
                requested: cap + 1,
                cap,
            })
        );
    }

    #[test]
    fn histogram_live_budget_uses_allocator_capacity_not_logical_length() {
        let bucket_bytes = size_of::<ErrorHistogramBucket>();
        let planned_len = 2;
        let allocator_capacity = 3;
        let cap = bucket_bytes * planned_len;

        assert_eq!(
            checked_histogram_live_capacity(0, planned_len, cap),
            Ok(cap)
        );
        assert_eq!(
            checked_histogram_live_capacity(0, allocator_capacity, cap),
            Err(MetricsError::MemoryCapExceeded {
                requested: bucket_bytes * allocator_capacity,
                cap,
            })
        );
    }
}