zenjxl-decoder 0.3.8

High performance Rust implementation of a JPEG XL decoder
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
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//! Streaming decoder tests
//!
//! Tests for incremental/streaming decoding behavior.
//! These verify that the decoder handles chunked input correctly.
//! DO NOT WEAKEN TOLERANCES or modify tests to pass when implementation is wrong.

use crate::api::{JxlDecoder, JxlDecoderOptions, ProcessingResult, states};

fn test_resources_dir() -> std::path::PathBuf {
    let manifest_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    manifest_dir.join("resources/test")
}

/// Helper to decode a JXL file with all data at once
fn decode_oneshot(data: &[u8]) -> Result<JxlDecoder<states::WithImageInfo>, crate::error::Error> {
    let options = JxlDecoderOptions::default();
    let mut decoder = JxlDecoder::<states::Initialized>::new(options);
    let mut input = data;

    loop {
        match decoder.process(&mut input)? {
            ProcessingResult::Complete { result } => return Ok(result),
            ProcessingResult::NeedsMoreInput { fallback, .. } => {
                if input.is_empty() {
                    return Err(crate::error::Error::OutOfBounds(0));
                }
                decoder = fallback;
            }
        }
    }
}

/// Helper to decode with chunked input, returning basic info
fn decode_chunked(
    data: &[u8],
    chunk_size: usize,
) -> Result<JxlDecoder<states::WithImageInfo>, crate::error::Error> {
    let options = JxlDecoderOptions::default();
    let mut decoder = JxlDecoder::<states::Initialized>::new(options);
    let mut offset = 0;

    loop {
        let end = (offset + chunk_size).min(data.len());
        let mut chunk = &data[offset..end];
        let chunk_len_before = chunk.len();

        match decoder.process(&mut chunk)? {
            ProcessingResult::Complete { result } => return Ok(result),
            ProcessingResult::NeedsMoreInput { fallback, .. } => {
                let consumed = chunk_len_before - chunk.len();
                offset += consumed;

                if offset >= data.len() {
                    return Err(crate::error::Error::OutOfBounds(0));
                }
                decoder = fallback;
            }
        }
    }
}

/// Tests for progressive decoding
/// Ported from decode_test.cc ProgressionTest, ProgressiveEventTest
#[cfg(test)]
mod progressive_tests {
    use super::*;

    /// Test that progressive images can be decoded
    #[test]
    fn test_progressive_decode_passes() {
        // Use progressive.jxl from conformance images if available
        let path = test_resources_dir().join("conformance_test_images/progressive.jxl");
        if !path.exists() {
            // Fall back to a regular test file
            let path = test_resources_dir().join("basic.jxl");
            let data = std::fs::read(&path).expect("Failed to read test file");

            let decoder = decode_oneshot(&data).expect("Failed to decode");
            let info = decoder.basic_info();

            // basic.jxl should decode successfully
            assert!(info.size.0 > 0);
            assert!(info.size.1 > 0);
            return;
        }

        let data = std::fs::read(&path).expect("Failed to read test file");
        let decoder = decode_oneshot(&data).expect("Failed to decode");
        let info = decoder.basic_info();

        // Progressive images should have valid dimensions
        assert!(info.size.0 > 0, "Width should be positive");
        assert!(info.size.1 > 0, "Height should be positive");

        // Check that we can access basic info
        assert!(info.bit_depth.bits_per_sample() > 0);
    }

    /// Test progressive decode with multiple passes
    #[test]
    fn test_progressive_multiple_passes() {
        let path = test_resources_dir().join("conformance_test_images/progressive.jxl");
        if !path.exists() {
            eprintln!("Skipping test - progressive.jxl not found");
            return;
        }

        let data = std::fs::read(&path).expect("Failed to read test file");

        // Decode with small chunks to exercise progressive path
        let decoder = decode_chunked(&data, 256).expect("Failed to decode");
        let info = decoder.basic_info();

        assert!(info.size.0 > 0);
        assert!(info.size.1 > 0);
    }
}

/// Tests for animation streaming
/// Ported from decode_test.cc AnimationTestStreaming
#[cfg(test)]
mod animation_streaming_tests {
    use super::*;

    /// Test that we can decode animation frame by frame
    #[test]
    fn test_animation_frame_by_frame() {
        let path = test_resources_dir().join("conformance_test_images/animation_spline.jxl");
        if !path.exists() {
            eprintln!("Skipping test - animation_spline.jxl not found");
            return;
        }
        let data = std::fs::read(&path).expect("Failed to read test file");

        let decoder = decode_oneshot(&data).expect("Failed to decode");
        let info = decoder.basic_info();

        // Verify this is an animated image
        assert!(info.animation.is_some(), "Should be animated");

        let animation = info.animation.as_ref().unwrap();

        // Animation should have valid timing info
        assert!(
            animation.tps_numerator > 0,
            "TPS numerator should be positive"
        );
        assert!(
            animation.tps_denominator > 0,
            "TPS denominator should be positive"
        );

        // Check dimensions
        assert!(info.size.0 > 0);
        assert!(info.size.1 > 0);
    }

    /// Test that animation can be decoded with various chunk sizes
    #[test]
    fn test_animation_streaming_chunks() {
        let path =
            test_resources_dir().join("conformance_test_images/animation_newtons_cradle.jxl");
        if !path.exists() {
            eprintln!("Skipping test - animation_newtons_cradle.jxl not found");
            return;
        }
        let data = std::fs::read(&path).expect("Failed to read test file");

        // Test various chunk sizes
        let chunk_sizes = [64, 256, 1024, 4096];

        for &chunk_size in &chunk_sizes {
            let decoder = decode_chunked(&data, chunk_size).unwrap_or_else(|e| {
                panic!("Failed to decode with chunk size {}: {:?}", chunk_size, e)
            });

            let info = decoder.basic_info();
            assert!(
                info.animation.is_some(),
                "Should be animated with chunk size {}",
                chunk_size
            );
        }
    }

    /// Test animation with splines
    #[test]
    fn test_animation_with_splines() {
        let path = test_resources_dir().join("conformance_test_images/animation_spline.jxl");
        if !path.exists() {
            eprintln!("Skipping test - animation_spline.jxl not found");
            return;
        }
        let data = std::fs::read(&path).expect("Failed to read test file");

        let decoder = decode_oneshot(&data).expect("Failed to decode");
        let info = decoder.basic_info();

        // Verify dimensions match expected (320x320 for animation_spline.jxl)
        assert_eq!(info.size.0, 320, "Width should be 320");
        assert_eq!(info.size.1, 320, "Height should be 320");
        assert!(info.animation.is_some(), "Should be animated");
    }
}

/// Tests for flush behavior
/// Ported from decode_test.cc FlushTest, FlushTestImageOutCallback
#[cfg(test)]
mod flush_tests {
    use super::*;

    /// Test that partial decoding doesn't corrupt state
    #[test]
    fn test_flush_partial_image() {
        let path = test_resources_dir().join("basic.jxl");
        let data = std::fs::read(&path).expect("Failed to read test file");

        // Decode with very small chunks to simulate streaming
        let decoder = decode_chunked(&data, 16).expect("Failed to decode");
        let info = decoder.basic_info();

        // Should still get valid basic info
        assert!(info.size.0 > 0);
        assert!(info.size.1 > 0);
    }

    /// Test flush behavior with alpha channel
    #[test]
    fn test_flush_with_alpha() {
        let path = test_resources_dir().join("3x3a_srgb_lossless.jxl");
        let data = std::fs::read(&path).expect("Failed to read test file");

        // Decode with small chunks
        let decoder = decode_chunked(&data, 32).expect("Failed to decode");
        let info = decoder.basic_info();

        // Should have alpha channel
        assert!(
            !info.extra_channels.is_empty(),
            "Should have extra channels for alpha"
        );
    }

    /// Test that we can abort and restart decoding
    #[test]
    fn test_restart_after_partial() {
        let path = test_resources_dir().join("basic.jxl");
        let data = std::fs::read(&path).expect("Failed to read test file");

        // Start decoding with only part of the data
        let partial_data = &data[..data.len() / 2];
        let options = JxlDecoderOptions::default();
        let decoder = JxlDecoder::<states::Initialized>::new(options);
        let mut input = partial_data;

        // Try to process - should need more input
        let result = decoder.process(&mut input);
        match result {
            Ok(ProcessingResult::NeedsMoreInput { .. }) => {
                // Expected - we didn't provide enough data
            }
            Ok(ProcessingResult::Complete { .. }) => {
                // Also valid if file is very small
            }
            Err(_) => {
                // Also acceptable - partial data may be invalid
            }
        }

        // Now decode the full file fresh
        let decoder = decode_oneshot(&data).expect("Failed to decode full file");
        let info = decoder.basic_info();
        assert!(info.size.0 > 0);
    }
}

/// Tests for frame skipping
/// Ported from decode_test.cc SkipFrameTest, SkipFrameWithBlendingTest
#[cfg(test)]
mod frame_skip_tests {
    use super::*;

    /// Test that animation info is available before decoding frames
    #[test]
    fn test_animation_info_available() {
        let path = test_resources_dir().join("conformance_test_images/animation_spline.jxl");
        if !path.exists() {
            eprintln!("Skipping test - animation_spline.jxl not found");
            return;
        }
        let data = std::fs::read(&path).expect("Failed to read test file");

        let decoder = decode_oneshot(&data).expect("Failed to decode");
        let info = decoder.basic_info();

        // Animation info should be available from basic info
        assert!(
            info.animation.is_some(),
            "Animation info should be available"
        );

        let anim = info.animation.as_ref().unwrap();
        // Animation should have reasonable timing
        assert!(anim.tps_numerator > 0);
        assert!(anim.tps_denominator > 0);
    }

    // TODO: Frame skipping API not yet implemented in jxl-rs.
    // These tests would verify:
    // - JxlDecoderSkipFrames functionality
    // - Correct handling of frame dependencies when skipping
    // - Blending requirements when skipping to specific frames
    //
    // The libjxl tests (SkipFrameTest, SkipFrameWithBlendingTest) use
    // JxlDecoderSkipFrames which doesn't exist in jxl-rs yet.

    /// Test blending mode detection
    #[test]
    fn test_blendmodes_file() {
        let path = test_resources_dir().join("conformance_test_images/blendmodes.jxl");
        if !path.exists() {
            eprintln!("Skipping test - blendmodes.jxl not found");
            return;
        }
        let data = std::fs::read(&path).expect("Failed to read test file");

        let decoder = decode_oneshot(&data).expect("Failed to decode");
        let info = decoder.basic_info();

        // blendmodes.jxl should have animation or multiple frames
        assert!(info.size.0 > 0);
        assert!(info.size.1 > 0);
    }
}

/// Tests for input handling edge cases
/// Ported from decode_test.cc InputHandlingTestOneShot, InputHandlingTestStreaming
#[cfg(test)]
mod input_handling_tests {
    use super::*;

    /// Test one-shot decode with all data available
    #[test]
    fn test_one_shot_decode() {
        let path = test_resources_dir().join("basic.jxl");
        let data = std::fs::read(&path).expect("Failed to read test file");

        let decoder = decode_oneshot(&data).expect("Failed to decode");
        let info = decoder.basic_info();

        // Verify decode succeeded
        assert!(info.size.0 > 0, "Width should be positive");
        assert!(info.size.1 > 0, "Height should be positive");
    }

    /// Test byte-by-byte streaming (most extreme case)
    #[test]
    fn test_byte_by_byte_streaming() {
        let path = test_resources_dir().join("basic.jxl");
        let data = std::fs::read(&path).expect("Failed to read test file");

        // Decode one byte at a time
        let decoder = decode_chunked(&data, 1).expect("Failed to decode byte-by-byte");
        let oneshot = decode_oneshot(&data).expect("Failed to decode oneshot");

        // Both should produce the same basic info
        assert_eq!(
            decoder.basic_info().size,
            oneshot.basic_info().size,
            "Dimensions should match between byte-by-byte and oneshot"
        );
    }

    /// Test with various chunk sizes
    #[test]
    fn test_various_chunk_sizes() {
        let path = test_resources_dir().join("basic.jxl");
        let data = std::fs::read(&path).expect("Failed to read test file");

        // Get reference from oneshot decode
        let reference = decode_oneshot(&data).expect("Failed to decode reference");
        let ref_size = reference.basic_info().size;

        // Test various chunk sizes
        let chunk_sizes = [
            1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 64, 128, 256, 512, 1024,
        ];

        for &chunk_size in &chunk_sizes {
            let decoder = decode_chunked(&data, chunk_size)
                .unwrap_or_else(|e| panic!("Failed with chunk size {}: {:?}", chunk_size, e));

            assert_eq!(
                decoder.basic_info().size,
                ref_size,
                "Size mismatch with chunk size {}",
                chunk_size
            );
        }
    }

    /// Test that prime chunk sizes work correctly
    #[test]
    fn test_prime_chunk_sizes() {
        let path = test_resources_dir().join("3x3_srgb_lossless.jxl");
        let data = std::fs::read(&path).expect("Failed to read test file");

        // Prime numbers as chunk sizes to test edge cases
        let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47];

        for &prime in &primes {
            let decoder = decode_chunked(&data, prime)
                .unwrap_or_else(|e| panic!("Failed with prime chunk size {}: {:?}", prime, e));

            let info = decoder.basic_info();
            assert_eq!(
                info.size.0, 3,
                "Width should be 3 with chunk size {}",
                prime
            );
            assert_eq!(
                info.size.1, 3,
                "Height should be 3 with chunk size {}",
                prime
            );
        }
    }

    /// Test large file with streaming
    #[test]
    fn test_large_file_streaming() {
        // Use cafe.jxl which is larger
        let path = test_resources_dir().join("conformance_test_images/cafe.jxl");
        if !path.exists() {
            eprintln!("Skipping test - cafe.jxl not found");
            return;
        }
        let data = std::fs::read(&path).expect("Failed to read test file");

        // Test with various chunk sizes
        for &chunk_size in &[1024, 4096, 16384] {
            let decoder = decode_chunked(&data, chunk_size)
                .unwrap_or_else(|e| panic!("Failed with chunk size {}: {:?}", chunk_size, e));

            let info = decoder.basic_info();
            // cafe.jxl is 1280x1600
            assert_eq!(info.size.0, 1280, "Width should be 1280");
            assert_eq!(info.size.1, 1600, "Height should be 1600");
        }
    }
}

/// Tests for container format handling
#[cfg(test)]
mod container_tests {
    use super::*;

    /// Test that container format files can be decoded
    #[test]
    fn test_container_format() {
        // Files with _with_container suffix are in container format
        let path = test_resources_dir().join("has_permutation_with_container.jxl");
        if !path.exists() {
            eprintln!("Skipping test - has_permutation_with_container.jxl not found");
            return;
        }
        let data = std::fs::read(&path).expect("Failed to read test file");

        let decoder = decode_oneshot(&data).expect("Failed to decode container format");
        let info = decoder.basic_info();

        assert!(info.size.0 > 0);
        assert!(info.size.1 > 0);
    }

    /// Test container with streaming
    #[test]
    fn test_container_streaming() {
        let path = test_resources_dir().join("has_permutation_with_container.jxl");
        if !path.exists() {
            eprintln!("Skipping test - has_permutation_with_container.jxl not found");
            return;
        }
        let data = std::fs::read(&path).expect("Failed to read test file");

        // Container format should work with streaming
        let decoder =
            decode_chunked(&data, 256).expect("Failed to decode container with streaming");
        let info = decoder.basic_info();

        assert!(info.size.0 > 0);
        assert!(info.size.1 > 0);
    }
}

/// Tests for HDR content
#[cfg(test)]
mod hdr_tests {
    use super::*;

    /// Test HDR PQ file decoding
    #[test]
    fn test_hdr_pq_streaming() {
        let path = test_resources_dir().join("hdr_pq_test.jxl");
        if !path.exists() {
            eprintln!("Skipping test - hdr_pq_test.jxl not found");
            return;
        }
        let data = std::fs::read(&path).expect("Failed to read test file");

        let decoder = decode_chunked(&data, 512).expect("Failed to decode HDR PQ");
        let info = decoder.basic_info();

        assert!(info.size.0 > 0);
        assert!(info.size.1 > 0);
    }

    /// Test HDR HLG file decoding
    #[test]
    fn test_hdr_hlg_streaming() {
        let path = test_resources_dir().join("hdr_hlg_test.jxl");
        if !path.exists() {
            eprintln!("Skipping test - hdr_hlg_test.jxl not found");
            return;
        }
        let data = std::fs::read(&path).expect("Failed to read test file");

        let decoder = decode_chunked(&data, 512).expect("Failed to decode HDR HLG");
        let info = decoder.basic_info();

        assert!(info.size.0 > 0);
        assert!(info.size.1 > 0);
    }
}