zenresize 0.2.1

High-quality image resampling with 31 filters, streaming API, and SIMD acceleration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
//! Integration tests for zenresize.
//!
//! Tests cover: streaming vs full-frame parity, all filter types,
//! edge cases, stride handling, and format combinations.

use zenresize::{AlphaMode, Filter, PixelDescriptor, ResizeConfig, Resizer, StreamingResize};

fn config_srgb(in_w: u32, in_h: u32, out_w: u32, out_h: u32) -> ResizeConfig {
    ResizeConfig::builder(in_w, in_h, out_w, out_h)
        .format(PixelDescriptor::RGBA8_SRGB)
        .srgb()
        .build()
}

fn config_linear(in_w: u32, in_h: u32, out_w: u32, out_h: u32) -> ResizeConfig {
    ResizeConfig::builder(in_w, in_h, out_w, out_h)
        .format(PixelDescriptor::RGBA8_SRGB)
        .linear()
        .build()
}

/// Generate a gradient test image (RGBA u8).
fn gradient_image(w: u32, h: u32) -> Vec<u8> {
    let mut buf = Vec::with_capacity(w as usize * h as usize * 4);
    for y in 0..h {
        for x in 0..w {
            let r = ((x as f32 / w as f32) * 255.0) as u8;
            let g = ((y as f32 / h as f32) * 255.0) as u8;
            let b = (((x + y) as f32 / (w + h) as f32) * 255.0) as u8;
            buf.extend_from_slice(&[r, g, b, 255]);
        }
    }
    buf
}

/// Generate a gradient test image (RGB u8, 3 channels).
fn gradient_image_3ch(w: u32, h: u32) -> Vec<u8> {
    let mut buf = Vec::with_capacity(w as usize * h as usize * 3);
    for y in 0..h {
        for x in 0..w {
            let r = ((x as f32 / w as f32) * 255.0) as u8;
            let g = ((y as f32 / h as f32) * 255.0) as u8;
            let b = (((x + y) as f32 / (w + h) as f32) * 255.0) as u8;
            buf.extend_from_slice(&[r, g, b]);
        }
    }
    buf
}

/// Helper: run streaming resize with interleaved push/drain, collect u8 output.
fn streaming_collect(config: &ResizeConfig, input: &[u8]) -> Vec<u8> {
    let in_w = config.in_width as usize;
    let in_h = config.in_height as usize;
    let channels = config.input.channels();
    let row_len = in_w * channels;

    let mut resizer = StreamingResize::new(config);
    let mut output = Vec::new();
    for y in 0..in_h {
        resizer
            .push_row(&input[y * row_len..(y + 1) * row_len])
            .unwrap();
        while let Some(row) = resizer.next_output_row() {
            output.extend_from_slice(row);
        }
    }
    resizer.finish();
    while let Some(row) = resizer.next_output_row() {
        output.extend_from_slice(row);
    }
    output
}

// =============================================================================
// Streaming vs full-frame parity
// =============================================================================

#[test]
fn streaming_matches_fullframe_downscale() {
    let config = config_srgb(40, 40, 20, 20);
    let input = gradient_image(40, 40);

    // Full-frame
    let full_output = Resizer::new(&config).resize(&input);

    // Streaming
    let stream_output = streaming_collect(&config, &input);

    let max_diff: u8 = full_output
        .iter()
        .zip(stream_output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(
        max_diff <= 1,
        "streaming vs full-frame max diff {} exceeds tolerance 1",
        max_diff
    );
}

#[test]
fn streaming_matches_fullframe_upscale() {
    let config = config_srgb(10, 10, 30, 30);
    let input = gradient_image(10, 10);

    let full_output = Resizer::new(&config).resize(&input);
    let stream_output = streaming_collect(&config, &input);

    let max_diff: u8 = full_output
        .iter()
        .zip(stream_output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(
        max_diff <= 1,
        "streaming vs full-frame max diff {} exceeds tolerance 1",
        max_diff
    );
}

#[test]
fn streaming_matches_fullframe_linear() {
    let config = config_linear(30, 30, 15, 15);
    let input = gradient_image(30, 30);

    let full_output = Resizer::new(&config).resize(&input);
    let stream_output = streaming_collect(&config, &input);

    let max_diff: u8 = full_output
        .iter()
        .zip(stream_output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(
        max_diff <= 1,
        "streaming vs full-frame linear max diff {} exceeds tolerance 1",
        max_diff
    );
}

// =============================================================================
// Streaming vs full-frame parity at realistic sizes
// =============================================================================

#[test]
fn streaming_matches_fullframe_srgb_1024() {
    let config = ResizeConfig::builder(1024, 1024, 512, 512)
        .filter(Filter::Lanczos)
        .format(PixelDescriptor::RGBA8_SRGB)
        .srgb()
        .build();
    let input = gradient_image(1024, 1024);
    let full_output = Resizer::new(&config).resize(&input);
    let stream_output = streaming_collect(&config, &input);
    let max_diff: u8 = full_output
        .iter()
        .zip(stream_output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(max_diff <= 1, "max diff {} exceeds 1", max_diff);
}

#[test]
fn streaming_matches_fullframe_linear_1024() {
    let config = ResizeConfig::builder(1024, 1024, 512, 512)
        .filter(Filter::Lanczos)
        .format(PixelDescriptor::RGBX8_SRGB)
        .linear()
        .build();
    let input = gradient_image(1024, 1024);
    let full_output = Resizer::new(&config).resize(&input);
    let stream_output = streaming_collect(&config, &input);
    let max_diff: u8 = full_output
        .iter()
        .zip(stream_output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(max_diff <= 1, "max diff {} exceeds 1", max_diff);
}

#[test]
fn streaming_matches_fullframe_srgb_upscale() {
    let config = ResizeConfig::builder(512, 512, 1024, 1024)
        .filter(Filter::Lanczos)
        .format(PixelDescriptor::RGBA8_SRGB)
        .srgb()
        .build();
    let input = gradient_image(512, 512);
    let full_output = Resizer::new(&config).resize(&input);
    let stream_output = streaming_collect(&config, &input);
    let max_diff: u8 = full_output
        .iter()
        .zip(stream_output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(max_diff <= 1, "max diff {} exceeds 1", max_diff);
}

#[test]
fn streaming_matches_fullframe_linear_upscale() {
    let config = ResizeConfig::builder(512, 512, 1024, 1024)
        .filter(Filter::Lanczos)
        .format(PixelDescriptor::RGBX8_SRGB)
        .linear()
        .build();
    let input = gradient_image(512, 512);
    let full_output = Resizer::new(&config).resize(&input);
    let stream_output = streaming_collect(&config, &input);
    let max_diff: u8 = full_output
        .iter()
        .zip(stream_output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(max_diff <= 1, "max diff {} exceeds 1", max_diff);
}

#[test]
fn streaming_matches_fullframe_srgb_10x() {
    let config = ResizeConfig::builder(2000, 1500, 200, 150)
        .filter(Filter::Lanczos)
        .format(PixelDescriptor::RGBA8_SRGB)
        .srgb()
        .build();
    let input = gradient_image(2000, 1500);
    let full_output = Resizer::new(&config).resize(&input);
    let stream_output = streaming_collect(&config, &input);
    let max_diff: u8 = full_output
        .iter()
        .zip(stream_output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(max_diff <= 1, "max diff {} exceeds 1", max_diff);
}

#[test]
fn streaming_matches_fullframe_linear_10x() {
    let config = ResizeConfig::builder(2000, 1500, 200, 150)
        .filter(Filter::Lanczos)
        .format(PixelDescriptor::RGBX8_SRGB)
        .linear()
        .build();
    let input = gradient_image(2000, 1500);
    let full_output = Resizer::new(&config).resize(&input);
    let stream_output = streaming_collect(&config, &input);
    let max_diff: u8 = full_output
        .iter()
        .zip(stream_output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(max_diff <= 1, "max diff {} exceeds 1", max_diff);
}

#[test]
fn streaming_matches_fullframe_f32_1024() {
    let config = ResizeConfig::builder(1024, 1024, 512, 512)
        .filter(Filter::Lanczos)
        .format(PixelDescriptor::RGB8_SRGB)
        .linear()
        .build();
    let input = gradient_image_3ch(1024, 1024);
    let full_output = Resizer::new(&config).resize(&input);
    let stream_output = streaming_collect(&config, &input);
    let max_diff: u8 = full_output
        .iter()
        .zip(stream_output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(max_diff <= 1, "max diff {} exceeds 1", max_diff);
}

// =============================================================================
// All filter types produce valid output
// =============================================================================

#[test]
fn all_filters_produce_valid_output() {
    let filters = [
        Filter::Robidoux,
        Filter::RobidouxSharp,
        Filter::RobidouxFast,
        Filter::Mitchell,
        Filter::CatmullRom,
        Filter::Lanczos,
        Filter::LanczosSharp,
        Filter::Lanczos2,
        Filter::Lanczos2Sharp,
        Filter::Fastest,
        Filter::NCubic,
        Filter::NCubicSharp,
        Filter::Box,
        Filter::Triangle,
        Filter::Hermite,
        Filter::CubicBSpline,
        Filter::Ginseng,
        Filter::GinsengSharp,
        Filter::Jinc,
        Filter::RawLanczos3,
        Filter::RawLanczos3Sharp,
        Filter::RawLanczos2,
        Filter::RawLanczos2Sharp,
        Filter::CubicFast,
        Filter::Cubic,
        Filter::CubicSharp,
        Filter::CatmullRomFast,
        Filter::CatmullRomFastSharp,
        Filter::MitchellFast,
        Filter::Linear,
    ];

    let input = gradient_image(32, 32);

    for filter in &filters {
        let config = ResizeConfig::builder(32, 32, 16, 16)
            .filter(*filter)
            .format(PixelDescriptor::RGBA8_SRGB)
            .srgb()
            .build();

        let output = Resizer::new(&config).resize(&input);
        assert_eq!(
            output.len(),
            16 * 16 * 4,
            "Wrong output size for filter {:?}",
            filter
        );

        // Verify output is reasonable (not all zeros, not all 255s)
        let sum: u64 = output.iter().map(|&b| b as u64).sum();
        assert!(
            sum > 0 && sum < output.len() as u64 * 255,
            "Filter {:?} produced degenerate output (sum={})",
            filter,
            sum
        );
    }
}

// =============================================================================
// Edge cases
// =============================================================================

#[test]
fn resize_1x1_to_1x1() {
    let config = config_srgb(1, 1, 1, 1);
    let input = vec![128, 64, 32, 255];
    let output = Resizer::new(&config).resize(&input);
    assert_eq!(output.len(), 4);
    // Single pixel should be approximately preserved
    assert!((output[0] as i16 - 128).unsigned_abs() <= 2);
    assert!((output[1] as i16 - 64).unsigned_abs() <= 2);
    assert!((output[2] as i16 - 32).unsigned_abs() <= 2);
    assert!((output[3] as i16 - 255).unsigned_abs() <= 1);
}

#[test]
fn resize_1xn() {
    let config = config_srgb(1, 10, 1, 5);
    let input = vec![128u8; 10 * 4];
    let output = Resizer::new(&config).resize(&input);
    assert_eq!(output.len(), 5 * 4);
}

#[test]
fn resize_nx1() {
    let config = config_srgb(10, 1, 5, 1);
    let input = vec![128u8; 10 * 4];
    let output = Resizer::new(&config).resize(&input);
    assert_eq!(output.len(), 5 * 4);
}

#[test]
fn resize_same_size() {
    let config = config_srgb(20, 20, 20, 20);
    let input = gradient_image(20, 20);
    let output = Resizer::new(&config).resize(&input);
    assert_eq!(output.len(), input.len());

    // Same-size resize should be very close to identity
    let max_diff: u8 = input
        .iter()
        .zip(output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(
        max_diff <= 3,
        "Same-size resize drifted too much: max diff {}",
        max_diff
    );
}

#[test]
fn resize_non_square() {
    let config = config_srgb(100, 50, 30, 75);
    let input = gradient_image(100, 50);
    let output = Resizer::new(&config).resize(&input);
    assert_eq!(output.len(), 30 * 75 * 4);
}

#[test]
fn resize_large_downscale() {
    let config = config_srgb(200, 200, 10, 10);
    let input = vec![128u8; 200 * 200 * 4];
    let output = Resizer::new(&config).resize(&input);
    assert_eq!(output.len(), 10 * 10 * 4);

    // Constant input → output should be close to constant
    for px in output.chunks_exact(4) {
        assert!(
            (px[0] as i16 - 128).unsigned_abs() <= 3,
            "pixel value drifted: {}",
            px[0]
        );
    }
}

#[test]
fn resize_large_upscale() {
    let config = config_srgb(5, 5, 100, 100);
    let input = vec![100u8; 5 * 5 * 4];
    let output = Resizer::new(&config).resize(&input);
    assert_eq!(output.len(), 100 * 100 * 4);
}

// =============================================================================
// Channel count variations
// =============================================================================

#[test]
fn resize_rgb_3ch() {
    let config = ResizeConfig::builder(20, 20, 10, 10)
        .format(PixelDescriptor::RGB8_SRGB)
        .srgb()
        .build();

    let input = vec![128u8; 20 * 20 * 3];
    let output = Resizer::new(&config).resize(&input);
    assert_eq!(output.len(), 10 * 10 * 3);
}

#[test]
fn resize_gray_1ch() {
    let config = ResizeConfig::builder(20, 20, 10, 10)
        .format(PixelDescriptor::GRAY8_SRGB)
        .srgb()
        .build();

    let input = vec![128u8; 20 * 20];
    let output = Resizer::new(&config).resize(&input);
    assert_eq!(output.len(), 10 * 10);
}

#[test]
fn resize_rgbx_no_premul() {
    // RGBX: 4 channels, no alpha premultiply/unpremultiply
    let config = ResizeConfig::builder(20, 20, 10, 10)
        .format(PixelDescriptor::RGBX8_SRGB)
        .srgb()
        .build();

    let mut input = vec![0u8; 20 * 20 * 4];
    for px in input.chunks_exact_mut(4) {
        px[0] = 128;
        px[1] = 64;
        px[2] = 32;
        px[3] = 0; // padding
    }
    let output = Resizer::new(&config).resize(&input);
    assert_eq!(output.len(), 10 * 10 * 4);
}

#[test]
fn resize_premultiplied_alpha() {
    // RgbaPremul: 4 channels, already premultiplied — skip premul/unpremul
    let config = ResizeConfig::builder(20, 20, 10, 10)
        .format(PixelDescriptor::RGBA8_SRGB.with_alpha(Some(AlphaMode::Premultiplied)))
        .srgb()
        .build();

    let mut input = vec![0u8; 20 * 20 * 4];
    for px in input.chunks_exact_mut(4) {
        px[0] = 64; // R * A/255 = 128 * 128/255 ≈ 64
        px[1] = 32;
        px[2] = 16;
        px[3] = 128; // alpha
    }
    let output = Resizer::new(&config).resize(&input);
    assert_eq!(output.len(), 10 * 10 * 4);

    // Constant input → output should be close to constant
    for px in output.chunks_exact(4) {
        assert!((px[0] as i16 - 64).unsigned_abs() <= 3, "R: {}", px[0]);
        assert!((px[3] as i16 - 128).unsigned_abs() <= 2, "A: {}", px[3]);
    }
}

// =============================================================================
// f32 path
// =============================================================================

#[test]
fn resize_f32_constant() {
    let config = ResizeConfig::builder(20, 20, 10, 10)
        .format(PixelDescriptor::RGBAF32_LINEAR)
        .linear()
        .build();

    let input = vec![0.5f32; 20 * 20 * 4];
    let output = Resizer::new(&config).resize_f32(&input);
    assert_eq!(output.len(), 10 * 10 * 4);

    for &v in &output {
        assert!((v - 0.5).abs() < 0.02, "f32 constant value drifted: {}", v);
    }
}

#[test]
fn resize_f32_gradient() {
    let config = ResizeConfig::builder(30, 30, 15, 15)
        .format(PixelDescriptor::RGBAF32_LINEAR.with_alpha(Some(AlphaMode::Undefined)))
        .linear()
        .build();

    let mut input = Vec::with_capacity(30 * 30 * 4);
    for y in 0..30 {
        for x in 0..30 {
            input.push(x as f32 / 29.0);
            input.push(y as f32 / 29.0);
            input.push(0.5);
            input.push(1.0);
        }
    }

    let output = Resizer::new(&config).resize_f32(&input);
    assert_eq!(output.len(), 15 * 15 * 4);

    // All values should be in [0, 1] range (approximately)
    for &v in &output {
        assert!((-0.1..=1.1).contains(&v), "f32 output out of range: {}", v);
    }
}

// =============================================================================
// Linear vs sRGB color space
// =============================================================================

#[test]
fn linear_and_srgb_produce_different_results() {
    let input = gradient_image(30, 30);

    let config_lin = config_linear(30, 30, 15, 15);
    let config_s = config_srgb(30, 30, 15, 15);

    let out_lin = Resizer::new(&config_lin).resize(&input);
    let out_srgb = Resizer::new(&config_s).resize(&input);

    // They should produce different results (linear is gamma-correct)
    assert_ne!(
        out_lin, out_srgb,
        "linear and sRGB resize should differ on gradients"
    );
}

// =============================================================================
// Stride handling
// =============================================================================

#[test]
fn strided_input_matches_tight() {
    let w = 20u32;
    let h = 20u32;
    let channels = 4;

    // Tight input
    let tight_input = gradient_image(w, h);
    let config_tight = config_srgb(w, h, 10, 10);
    let out_tight = Resizer::new(&config_tight).resize(&tight_input);

    // Strided input (16 bytes of padding per row)
    let padding = 16;
    let tight_stride = w as usize * channels;
    let padded_stride = tight_stride + padding;
    let mut strided_input = vec![0u8; h as usize * padded_stride];
    for y in 0..h as usize {
        let src = &tight_input[y * tight_stride..(y + 1) * tight_stride];
        let dst = &mut strided_input[y * padded_stride..y * padded_stride + tight_stride];
        dst.copy_from_slice(src);
    }

    let config_strided = ResizeConfig::builder(w, h, 10, 10)
        .format(PixelDescriptor::RGBA8_SRGB)
        .srgb()
        .in_stride(padded_stride)
        .build();

    let out_strided = Resizer::new(&config_strided).resize(&strided_input);
    assert_eq!(
        out_tight, out_strided,
        "strided input should match tight input"
    );
}

// =============================================================================
// Interleaved push/drain matches batch-then-drain (via streaming_collect)
// =============================================================================

#[test]
fn interleaved_push_drain_produces_correct_output() {
    let config = config_srgb(20, 20, 10, 10);
    let input = gradient_image(20, 20);

    // Use streaming_collect helper (interleaved push/drain)
    let output = streaming_collect(&config, &input);
    assert_eq!(output.len(), 10 * 10 * 4);

    // Compare with fullframe
    let fullframe = Resizer::new(&config).resize(&input);
    let max_diff: u8 = fullframe
        .iter()
        .zip(output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(
        max_diff <= 1,
        "interleaved push/drain vs fullframe max diff {} exceeds tolerance 1",
        max_diff
    );
}

// =============================================================================
// Linear i16 vs f32 parity
// =============================================================================

/// The linear i16 path (4ch, no alpha, linearize) should produce output
/// within ±1 of the f32 linear path (streaming, which always uses f32).
#[test]
fn linear_i16_matches_f32_downscale() {
    let config = ResizeConfig::builder(64, 64, 32, 32)
        .filter(Filter::Lanczos)
        .format(PixelDescriptor::RGBX8_SRGB)
        .linear()
        .build();

    let input = gradient_image(64, 64);

    // Full-frame: uses i16 linear path (linearize + 4ch + !has_alpha)
    let i16_output = Resizer::new(&config).resize(&input);

    // Streaming: always uses f32 path
    let f32_output = streaming_collect(&config, &input);

    let max_diff: u8 = i16_output
        .iter()
        .zip(f32_output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(
        max_diff <= 1,
        "linear i16 vs f32 max diff {} exceeds tolerance 1",
        max_diff
    );
}

/// Parity test with upscale to catch different edge cases.
#[test]
fn linear_i16_matches_f32_upscale() {
    let config = ResizeConfig::builder(16, 16, 48, 48)
        .filter(Filter::Lanczos)
        .format(PixelDescriptor::RGBX8_SRGB)
        .linear()
        .build();

    let input = gradient_image(16, 16);

    let i16_output = Resizer::new(&config).resize(&input);
    let f32_output = streaming_collect(&config, &input);

    let max_diff: u8 = i16_output
        .iter()
        .zip(f32_output.iter())
        .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
        .max()
        .unwrap_or(0);
    assert!(
        max_diff <= 2,
        "linear i16 vs f32 upscale max diff {} exceeds tolerance 2",
        max_diff
    );
}

// =============================================================================
// Comprehensive regression: all filter × scale × path combinations
// =============================================================================

/// Compare fullframe (i16 path) vs streaming (f32 path) across all filters,
/// multiple scales, and path configurations. Catches weight normalization bugs.
#[test]
fn no_catastrophic_errors_across_all_combinations() {
    let scales: &[(u32, u32, &str)] = &[
        (200, 100, "2x_down"),
        (200, 50, "4x_down"),
        (200, 25, "8x_down"),
        (50, 100, "2x_up"),
        (50, 200, "4x_up"),
        (50, 400, "8x_up"),
    ];

    // Path configs: (format, color_space_fn, label)
    let path_configs: Vec<(PixelDescriptor, bool, &str)> = vec![
        (PixelDescriptor::RGBX8_SRGB, false, "srgb-noalpha"),
        (PixelDescriptor::RGBA8_SRGB, false, "srgb-alpha"),
        (PixelDescriptor::RGBX8_SRGB, true, "linear-noalpha"),
        (PixelDescriptor::RGBA8_SRGB, true, "linear-alpha"),
    ];

    let mut failures: Vec<String> = Vec::new();

    for &filter in Filter::all() {
        for &(in_size, out_size, scale_name) in scales {
            for &(format, linearize, path_name) in &path_configs {
                let config = {
                    let mut b = ResizeConfig::builder(in_size, in_size, out_size, out_size)
                        .filter(filter)
                        .format(format);
                    if linearize {
                        b = b.linear();
                    } else {
                        b = b.srgb();
                    }
                    b.build()
                };

                let input = gradient_image(in_size, in_size);

                // Full-frame
                let full_output = Resizer::new(&config).resize(&input);

                // Streaming (always f32 weights with f64 normalization)
                let stream_output = streaming_collect(&config, &input);

                assert_eq!(
                    full_output.len(),
                    stream_output.len(),
                    "size mismatch: {:?} {} {}",
                    filter,
                    scale_name,
                    path_name
                );

                let max_diff: u8 = full_output
                    .iter()
                    .zip(stream_output.iter())
                    .map(|(&a, &b)| (a as i16 - b as i16).unsigned_abs() as u8)
                    .max()
                    .unwrap_or(0);

                // Threshold: ±2 for i16 vs f32 quantization differences
                if max_diff > 2 {
                    failures.push(format!(
                        "  {:20?} {:8} {:16} max_diff={}",
                        filter, scale_name, path_name, max_diff,
                    ));
                }
            }
        }
    }

    if !failures.is_empty() {
        panic!(
            "Catastrophic errors found in {} combinations:\n{}",
            failures.len(),
            failures.join("\n")
        );
    }
}

// =============================================================================
// BGRA / BGRX: native zero-swizzle support
// =============================================================================
// The pipeline is channel-order-agnostic. sRGB transfer is identical for R/G/B,
// and convolution kernels just multiply N floats. BGRA data passes through
// unchanged — use RGBA8_SRGB, same as RGBA.

#[test]
fn bgra_preserves_channel_order() {
    // BGRA input should come out as BGRA — no swizzle, no reordering.
    let w = 20u32;
    let h = 20u32;
    let mut bgra_input = vec![0u8; (w * h * 4) as usize];
    for px in bgra_input.chunks_exact_mut(4) {
        px[0] = 200; // B
        px[1] = 100; // G
        px[2] = 50; // R
        px[3] = 255; // A
    }

    // Same config as RGBA8_SRGB — pipeline doesn't care about channel names
    let config = ResizeConfig::builder(w, h, 10, 10)
        .format(PixelDescriptor::RGBA8_SRGB)
        .srgb()
        .build();

    let output = Resizer::new(&config).resize(&bgra_input);
    assert_eq!(output.len(), 10 * 10 * 4);

    // Output preserves BGRA order
    for px in output.chunks_exact(4) {
        assert!((px[0] as i16 - 200).unsigned_abs() <= 3, "B: {}", px[0]);
        assert!((px[1] as i16 - 100).unsigned_abs() <= 3, "G: {}", px[1]);
        assert!((px[2] as i16 - 50).unsigned_abs() <= 3, "R: {}", px[2]);
        assert!((px[3] as i16 - 255).unsigned_abs() <= 1, "A: {}", px[3]);
    }
}

#[test]
fn bgrx_as_4ch_no_alpha() {
    // BGRX: 4 bytes, no alpha. Use RGBX8_SRGB.
    // The X channel is just another data channel — passes through like any other.
    let w = 20u32;
    let h = 20u32;
    let mut bgrx_input = vec![0u8; (w * h * 4) as usize];
    for px in bgrx_input.chunks_exact_mut(4) {
        px[0] = 200; // B
        px[1] = 100; // G
        px[2] = 50; // R
        px[3] = 0; // X (padding)
    }

    let config = ResizeConfig::builder(w, h, 10, 10)
        .format(PixelDescriptor::RGBX8_SRGB)
        .srgb()
        .build();

    let output = Resizer::new(&config).resize(&bgrx_input);
    assert_eq!(output.len(), 10 * 10 * 4);

    // Channel order preserved, X stays ~0 (it was 0 in constant input)
    for px in output.chunks_exact(4) {
        assert!((px[0] as i16 - 200).unsigned_abs() <= 3, "B: {}", px[0]);
        assert!((px[1] as i16 - 100).unsigned_abs() <= 3, "G: {}", px[1]);
        assert!((px[2] as i16 - 50).unsigned_abs() <= 3, "R: {}", px[2]);
        assert!(px[3] <= 3, "X should stay ~0, got {}", px[3]);
    }
}

#[test]
fn bgra_linear_preserves_order() {
    // Even with linear-light processing, channel order is preserved.
    // sRGB→linear curve is the same for channels 0, 1, and 2 regardless of
    // whether they represent R,G,B or B,G,R.
    let w = 20u32;
    let h = 20u32;
    let mut bgra_input = vec![0u8; (w * h * 4) as usize];
    for px in bgra_input.chunks_exact_mut(4) {
        px[0] = 200; // B
        px[1] = 100; // G
        px[2] = 50; // R
        px[3] = 255; // A
    }

    let config = ResizeConfig::builder(w, h, 10, 10)
        .format(PixelDescriptor::RGBA8_SRGB)
        .linear() // linear-light processing
        .build();

    let output = Resizer::new(&config).resize(&bgra_input);

    for px in output.chunks_exact(4) {
        assert!((px[0] as i16 - 200).unsigned_abs() <= 3, "B: {}", px[0]);
        assert!((px[1] as i16 - 100).unsigned_abs() <= 3, "G: {}", px[1]);
        assert!((px[2] as i16 - 50).unsigned_abs() <= 3, "R: {}", px[2]);
        assert!((px[3] as i16 - 255).unsigned_abs() <= 1, "A: {}", px[3]);
    }
}

// =============================================================================
// Post-resize blur tests
// =============================================================================

#[test]
fn identity_resize_no_blur_is_bit_identical() {
    let w = 64u32;
    let h = 64u32;
    let input = gradient_image(w, h);

    let config = ResizeConfig::builder(w, h, w, h)
        .format(PixelDescriptor::RGBA8_SRGB)
        .srgb()
        .build();

    let output = Resizer::new(&config).resize(&input);
    assert_eq!(input, output, "ratio=1 sRGB path should be bit-identical");
}

#[test]
fn identity_resize_linear_no_blur_max_diff_1() {
    let w = 64u32;
    let h = 64u32;
    let input = gradient_image(w, h);

    let config = ResizeConfig::builder(w, h, w, h)
        .format(PixelDescriptor::RGBA8_SRGB)
        .linear()
        .build();

    let output = Resizer::new(&config).resize(&input);

    let max_diff: u8 = input
        .iter()
        .zip(output.iter())
        .map(|(&a, &b)| a.abs_diff(b))
        .max()
        .unwrap_or(0);
    assert!(
        max_diff <= 1,
        "linear identity max diff = {max_diff}, expected <= 1"
    );
}

#[test]
fn blur_reduces_high_frequency_content() {
    let w = 64u32;
    let h = 64u32;

    let mut input = vec![0u8; w as usize * h as usize * 4];
    for y in 0..h as usize {
        for x in 0..w as usize {
            let idx = (y * w as usize + x) * 4;
            let val = if (x + y) % 2 == 0 { 200u8 } else { 50u8 };
            input[idx] = val;
            input[idx + 1] = val;
            input[idx + 2] = val;
            input[idx + 3] = 255;
        }
    }

    let config = ResizeConfig::builder(w, h, w, h)
        .format(PixelDescriptor::RGBA8_SRGB)
        .srgb()
        .post_blur(1.5)
        .build();

    let output = Resizer::new(&config).resize(&input);

    let mut input_diff_sum = 0u64;
    let mut output_diff_sum = 0u64;
    for y in 0..h as usize {
        for x in 1..w as usize {
            let idx = (y * w as usize + x) * 4;
            let prev = (y * w as usize + x - 1) * 4;
            input_diff_sum += (input[idx] as i32 - input[prev] as i32).unsigned_abs() as u64;
            output_diff_sum += (output[idx] as i32 - output[prev] as i32).unsigned_abs() as u64;
        }
    }

    assert!(
        output_diff_sum < input_diff_sum / 2,
        "blur should reduce neighbor diffs: input={input_diff_sum}, output={output_diff_sum}"
    );
}

#[test]
fn blur_uniform_image_stays_uniform() {
    let w = 32u32;
    let h = 32u32;
    let input = vec![128u8; w as usize * h as usize * 4];

    let config = ResizeConfig::builder(w, h, w, h)
        .format(PixelDescriptor::RGBA8_SRGB)
        .srgb()
        .post_blur(2.0)
        .build();

    let output = Resizer::new(&config).resize(&input);

    let max_diff: u8 = input
        .iter()
        .zip(output.iter())
        .map(|(&a, &b)| a.abs_diff(b))
        .max()
        .unwrap_or(0);
    assert!(
        max_diff <= 1,
        "uniform image should stay uniform, max diff = {max_diff}"
    );
}

#[test]
fn blur_f32_reduces_variance() {
    let w = 32u32;
    let h = 32u32;
    let channels = 4;
    let len = w as usize * h as usize * channels;

    let mut input = vec![0.0f32; len];
    for y in 0..h as usize {
        for x in 0..w as usize {
            let idx = (y * w as usize + x) * channels;
            let val = if (x + y) % 2 == 0 { 0.8 } else { 0.2 };
            input[idx] = val;
            input[idx + 1] = val;
            input[idx + 2] = val;
            input[idx + 3] = 1.0;
        }
    }

    let config = ResizeConfig::builder(w, h, w, h)
        .format(PixelDescriptor::RGBAF32_LINEAR)
        .post_blur(1.0)
        .build();

    let output = Resizer::new(&config).resize_f32(&input);

    let max_deviation: f32 = output
        .chunks(channels)
        .flat_map(|px| px[..3].iter())
        .map(|&v| (v - 0.5).abs())
        .fold(0.0f32, f32::max);
    assert!(
        max_deviation < 0.15,
        "blurred pattern should converge toward mean, max_deviation = {max_deviation}"
    );
}