pixo 0.4.1

A minimal-dependency, high-performance image compression library
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
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
//! Image resizing algorithms.
//!
//! This module provides high-quality image resizing with multiple algorithms:
//! - **Nearest neighbor**: Fastest, pixelated results (good for pixel art)
//! - **Bilinear**: Fast, smooth results (good balance)
//! - **Lanczos3**: Highest quality, slower (best for photographs)
//!
//! # Example
//!
//! ```rust
//! use pixo::resize::{resize, ResizeOptions, ResizeAlgorithm};
//! use pixo::ColorType;
//!
//! // Resize a 100x100 RGBA image to 50x50 using Lanczos3
//! let pixels = vec![128u8; 100 * 100 * 4];
//! let options = ResizeOptions::builder(100, 100)
//!     .dst(50, 50)
//!     .color_type(ColorType::Rgba)
//!     .algorithm(ResizeAlgorithm::Lanczos3)
//!     .build();
//! let resized = resize(&pixels, &options).unwrap();
//! assert_eq!(resized.len(), 50 * 50 * 4);
//! ```

use crate::color::ColorType;
use crate::error::{Error, Result};
use std::f32::consts::PI;

/// Maximum supported dimension for resizing.
const MAX_DIMENSION: u32 = 1 << 24; // 16 million pixels

/// Resizing algorithm to use.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ResizeAlgorithm {
    /// Nearest neighbor: fastest, pixelated results.
    /// Best for pixel art or when speed is critical.
    Nearest,
    /// Bilinear interpolation: fast with smooth results.
    /// Good balance between quality and speed.
    #[default]
    Bilinear,
    /// Lanczos3 resampling: highest quality, slowest.
    /// Best for photographs and high-quality downscaling.
    Lanczos3,
}

/// Options for image resizing operations.
///
/// Use [`ResizeOptions::builder()`] to create options with a fluent API.
///
/// # Example
///
/// ```rust
/// use pixo::resize::{resize, ResizeOptions, ResizeAlgorithm};
/// use pixo::ColorType;
///
/// let pixels = vec![128u8; 100 * 100 * 4];
/// let options = ResizeOptions::builder(100, 100)
///     .dst(50, 50)
///     .color_type(ColorType::Rgba)
///     .algorithm(ResizeAlgorithm::Lanczos3)
///     .build();
/// let resized = resize(&pixels, &options).unwrap();
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ResizeOptions {
    /// Source image width in pixels.
    pub src_width: u32,
    /// Source image height in pixels.
    pub src_height: u32,
    /// Destination image width in pixels.
    pub dst_width: u32,
    /// Destination image height in pixels.
    pub dst_height: u32,
    /// Color type of the pixel data.
    pub color_type: ColorType,
    /// Resizing algorithm to use.
    pub algorithm: ResizeAlgorithm,
}

impl ResizeOptions {
    /// Create a builder for [`ResizeOptions`].
    ///
    /// The source dimensions are required; destination defaults to matching source
    /// (no resize), color type defaults to RGBA, and algorithm defaults to Bilinear.
    pub fn builder(src_width: u32, src_height: u32) -> ResizeOptionsBuilder {
        ResizeOptionsBuilder::new(src_width, src_height)
    }
}

/// Builder for [`ResizeOptions`].
#[derive(Debug, Clone)]
pub struct ResizeOptionsBuilder {
    src_width: u32,
    src_height: u32,
    dst_width: u32,
    dst_height: u32,
    color_type: ColorType,
    algorithm: ResizeAlgorithm,
}

impl ResizeOptionsBuilder {
    /// Create a new builder with source dimensions.
    pub fn new(src_width: u32, src_height: u32) -> Self {
        Self {
            src_width,
            src_height,
            dst_width: src_width,
            dst_height: src_height,
            color_type: ColorType::Rgba,
            algorithm: ResizeAlgorithm::default(),
        }
    }

    /// Set the destination dimensions.
    pub fn dst(mut self, width: u32, height: u32) -> Self {
        self.dst_width = width;
        self.dst_height = height;
        self
    }

    /// Set the color type of the pixel data.
    pub fn color_type(mut self, color_type: ColorType) -> Self {
        self.color_type = color_type;
        self
    }

    /// Set the resizing algorithm.
    pub fn algorithm(mut self, algorithm: ResizeAlgorithm) -> Self {
        self.algorithm = algorithm;
        self
    }

    /// Build the [`ResizeOptions`].
    #[must_use]
    pub fn build(self) -> ResizeOptions {
        ResizeOptions {
            src_width: self.src_width,
            src_height: self.src_height,
            dst_width: self.dst_width,
            dst_height: self.dst_height,
            color_type: self.color_type,
            algorithm: self.algorithm,
        }
    }
}

/// Resize an image to new dimensions.
///
/// # Arguments
///
/// * `data` - Raw pixel data (row-major order)
/// * `options` - Resize options specifying dimensions, color type, and algorithm
///
/// # Returns
///
/// Resized pixel data with the same color type.
///
/// # Errors
///
/// Returns an error if dimensions are invalid or data length doesn't match.
#[must_use = "resizing produces pixel data that should be used"]
pub fn resize(data: &[u8], options: &ResizeOptions) -> Result<Vec<u8>> {
    let mut output = Vec::new();
    resize_into(&mut output, data, options)?;
    Ok(output)
}

/// Resize an image into a caller-provided buffer.
///
/// The `output` buffer will be cleared and reused, allowing callers to avoid
/// repeated allocations across multiple resize operations.
///
/// # Arguments
///
/// * `output` - Buffer to write resized data into (will be cleared)
/// * `data` - Raw pixel data (row-major order)
/// * `options` - Resize options specifying dimensions, color type, and algorithm
#[must_use = "this `Result` may indicate a resize error"]
pub fn resize_into(output: &mut Vec<u8>, data: &[u8], options: &ResizeOptions) -> Result<()> {
    resize_impl(
        output,
        data,
        options.src_width,
        options.src_height,
        options.dst_width,
        options.dst_height,
        options.color_type,
        options.algorithm,
    )
}

/// Internal resize implementation.
#[allow(clippy::too_many_arguments)]
fn resize_impl(
    output: &mut Vec<u8>,
    data: &[u8],
    src_width: u32,
    src_height: u32,
    dst_width: u32,
    dst_height: u32,
    color_type: ColorType,
    algorithm: ResizeAlgorithm,
) -> Result<()> {
    // Validate source dimensions
    if src_width == 0 || src_height == 0 {
        return Err(Error::InvalidDimensions {
            width: src_width,
            height: src_height,
        });
    }

    // Validate destination dimensions
    if dst_width == 0 || dst_height == 0 {
        return Err(Error::InvalidDimensions {
            width: dst_width,
            height: dst_height,
        });
    }

    // Check maximum dimensions
    if src_width > MAX_DIMENSION
        || src_height > MAX_DIMENSION
        || dst_width > MAX_DIMENSION
        || dst_height > MAX_DIMENSION
    {
        return Err(Error::ImageTooLarge {
            width: src_width.max(dst_width),
            height: src_height.max(dst_height),
            max: MAX_DIMENSION,
        });
    }

    let bytes_per_pixel = color_type.bytes_per_pixel();

    // Validate input data length
    let expected_len = (src_width as usize)
        .checked_mul(src_height as usize)
        .and_then(|v| v.checked_mul(bytes_per_pixel))
        .ok_or(Error::InvalidDataLength {
            expected: usize::MAX,
            actual: data.len(),
        })?;

    if data.len() != expected_len {
        return Err(Error::InvalidDataLength {
            expected: expected_len,
            actual: data.len(),
        });
    }

    // Calculate output size
    let output_len = (dst_width as usize)
        .checked_mul(dst_height as usize)
        .and_then(|v| v.checked_mul(bytes_per_pixel))
        .ok_or(Error::InvalidDataLength {
            expected: usize::MAX,
            actual: 0,
        })?;

    output.clear();
    output.resize(output_len, 0);

    // Dispatch to appropriate algorithm
    match algorithm {
        ResizeAlgorithm::Nearest => resize_nearest(
            output,
            data,
            src_width as usize,
            src_height as usize,
            dst_width as usize,
            dst_height as usize,
            bytes_per_pixel,
        ),
        ResizeAlgorithm::Bilinear => resize_bilinear(
            output,
            data,
            src_width as usize,
            src_height as usize,
            dst_width as usize,
            dst_height as usize,
            bytes_per_pixel,
        ),
        ResizeAlgorithm::Lanczos3 => resize_lanczos3(
            output,
            data,
            src_width as usize,
            src_height as usize,
            dst_width as usize,
            dst_height as usize,
            bytes_per_pixel,
        ),
    }

    Ok(())
}

/// Nearest neighbor resizing - fastest, pixelated results.
fn resize_nearest(
    output: &mut [u8],
    data: &[u8],
    src_width: usize,
    src_height: usize,
    dst_width: usize,
    dst_height: usize,
    bytes_per_pixel: usize,
) {
    let x_ratio = src_width as f32 / dst_width as f32;
    let y_ratio = src_height as f32 / dst_height as f32;

    for dst_y in 0..dst_height {
        let src_y = ((dst_y as f32 + 0.5) * y_ratio - 0.5)
            .round()
            .max(0.0)
            .min((src_height - 1) as f32) as usize;

        for dst_x in 0..dst_width {
            let src_x = ((dst_x as f32 + 0.5) * x_ratio - 0.5)
                .round()
                .max(0.0)
                .min((src_width - 1) as f32) as usize;

            let src_idx = (src_y * src_width + src_x) * bytes_per_pixel;
            let dst_idx = (dst_y * dst_width + dst_x) * bytes_per_pixel;

            output[dst_idx..dst_idx + bytes_per_pixel]
                .copy_from_slice(&data[src_idx..src_idx + bytes_per_pixel]);
        }
    }
}

/// Bilinear interpolation - good balance of quality and speed.
fn resize_bilinear(
    output: &mut [u8],
    data: &[u8],
    src_width: usize,
    src_height: usize,
    dst_width: usize,
    dst_height: usize,
    bytes_per_pixel: usize,
) {
    let x_ratio = if dst_width > 1 {
        (src_width - 1) as f32 / (dst_width - 1) as f32
    } else {
        0.0
    };
    let y_ratio = if dst_height > 1 {
        (src_height - 1) as f32 / (dst_height - 1) as f32
    } else {
        0.0
    };

    for dst_y in 0..dst_height {
        let src_y_f = dst_y as f32 * y_ratio;
        let src_y0 = src_y_f.floor() as usize;
        let src_y1 = (src_y0 + 1).min(src_height - 1);
        let y_frac = src_y_f - src_y0 as f32;

        for dst_x in 0..dst_width {
            let src_x_f = dst_x as f32 * x_ratio;
            let src_x0 = src_x_f.floor() as usize;
            let src_x1 = (src_x0 + 1).min(src_width - 1);
            let x_frac = src_x_f - src_x0 as f32;

            // Get the four surrounding pixels
            let idx00 = (src_y0 * src_width + src_x0) * bytes_per_pixel;
            let idx01 = (src_y0 * src_width + src_x1) * bytes_per_pixel;
            let idx10 = (src_y1 * src_width + src_x0) * bytes_per_pixel;
            let idx11 = (src_y1 * src_width + src_x1) * bytes_per_pixel;

            let dst_idx = (dst_y * dst_width + dst_x) * bytes_per_pixel;

            // Interpolate each channel
            for c in 0..bytes_per_pixel {
                let p00 = data[idx00 + c] as f32;
                let p01 = data[idx01 + c] as f32;
                let p10 = data[idx10 + c] as f32;
                let p11 = data[idx11 + c] as f32;

                // Bilinear interpolation
                let top = p00 * (1.0 - x_frac) + p01 * x_frac;
                let bottom = p10 * (1.0 - x_frac) + p11 * x_frac;
                let value = top * (1.0 - y_frac) + bottom * y_frac;

                output[dst_idx + c] = value.round().clamp(0.0, 255.0) as u8;
            }
        }
    }
}

/// Lanczos kernel function.
#[inline]
fn lanczos_kernel(x: f32, a: f32) -> f32 {
    if x.abs() < f32::EPSILON {
        1.0
    } else if x.abs() >= a {
        0.0
    } else {
        let pi_x = PI * x;
        let pi_x_a = PI * x / a;
        (a * pi_x.sin() * pi_x_a.sin()) / (pi_x * pi_x_a)
    }
}

/// Precomputed contribution for a source pixel to destination pixels.
#[derive(Clone)]
struct Contribution {
    /// Starting source pixel index
    start: usize,
    /// Weights for each source pixel (already normalized)
    weights: Vec<f32>,
}

/// Precompute Lanczos3 contributions for a 1D resample.
/// Returns a vec of Contribution, one for each destination pixel.
fn precompute_contributions(src_size: usize, dst_size: usize) -> Vec<Contribution> {
    const A: f32 = 3.0;

    let scale = src_size as f32 / dst_size as f32;
    // For downscaling, expand the kernel support proportionally
    let filter_scale = scale.max(1.0);
    let support = A * filter_scale;

    let mut contributions = Vec::with_capacity(dst_size);

    for dst_idx in 0..dst_size {
        // Map destination pixel center to source coordinates
        let src_center = (dst_idx as f32 + 0.5) * scale - 0.5;

        // Determine the range of source pixels that contribute
        let start = ((src_center - support).floor() as isize).max(0) as usize;
        let end = ((src_center + support).ceil() as usize + 1).min(src_size);

        // Compute weights
        let mut weights = Vec::with_capacity(end - start);
        let mut weight_sum = 0.0f32;

        for src_idx in start..end {
            let x = (src_idx as f32 - src_center) / filter_scale;
            let w = lanczos_kernel(x, A);
            weights.push(w);
            weight_sum += w;
        }

        // Normalize weights
        if weight_sum.abs() > f32::EPSILON {
            for w in &mut weights {
                *w /= weight_sum;
            }
        }

        contributions.push(Contribution { start, weights });
    }

    contributions
}

/// Apply 1D horizontal resampling to a single row.
#[inline]
fn resample_row_horizontal(
    src_row: &[u8],
    dst_row: &mut [u8],
    contributions: &[Contribution],
    bytes_per_pixel: usize,
) {
    for (dst_x, contrib) in contributions.iter().enumerate() {
        let dst_idx = dst_x * bytes_per_pixel;

        // Accumulate weighted samples for each channel
        let mut channel_sums = [0.0f32; 4];

        for (i, &weight) in contrib.weights.iter().enumerate() {
            let src_x = contrib.start + i;
            let src_idx = src_x * bytes_per_pixel;
            for c in 0..bytes_per_pixel {
                channel_sums[c] += src_row[src_idx + c] as f32 * weight;
            }
        }

        for c in 0..bytes_per_pixel {
            dst_row[dst_idx + c] = channel_sums[c].round().clamp(0.0, 255.0) as u8;
        }
    }
}

/// Apply 1D vertical resampling to produce one destination row.
#[inline]
fn resample_column_vertical(
    temp: &[u8],
    dst_row: &mut [u8],
    contrib: &Contribution,
    temp_width: usize,
    bytes_per_pixel: usize,
) {
    let row_stride = temp_width * bytes_per_pixel;

    for dst_x in 0..temp_width {
        let dst_idx = dst_x * bytes_per_pixel;
        let mut channel_sums = [0.0f32; 4];

        for (i, &weight) in contrib.weights.iter().enumerate() {
            let src_y = contrib.start + i;
            let src_idx = src_y * row_stride + dst_x * bytes_per_pixel;
            for c in 0..bytes_per_pixel {
                channel_sums[c] += temp[src_idx + c] as f32 * weight;
            }
        }

        for c in 0..bytes_per_pixel {
            dst_row[dst_idx + c] = channel_sums[c].round().clamp(0.0, 255.0) as u8;
        }
    }
}

/// Lanczos3 resampling using separable filtering (horizontal then vertical).
/// This is O(2n) per pixel instead of O(n²), providing massive speedup.
fn resize_lanczos3(
    output: &mut [u8],
    data: &[u8],
    src_width: usize,
    src_height: usize,
    dst_width: usize,
    dst_height: usize,
    bytes_per_pixel: usize,
) {
    // Precompute contribution weights (computed once, reused for all rows/cols)
    let h_contribs = precompute_contributions(src_width, dst_width);
    let v_contribs = precompute_contributions(src_height, dst_height);

    // Intermediate buffer: src_height rows × dst_width columns
    let temp_size = src_height * dst_width * bytes_per_pixel;
    let mut temp = vec![0u8; temp_size];

    // Pass 1: Horizontal resampling (parallel when available)
    #[cfg(feature = "parallel")]
    {
        use rayon::prelude::*;

        let src_row_stride = src_width * bytes_per_pixel;
        let temp_row_stride = dst_width * bytes_per_pixel;

        temp.par_chunks_mut(temp_row_stride)
            .enumerate()
            .for_each(|(y, temp_row)| {
                let src_start = y * src_row_stride;
                let src_row = &data[src_start..src_start + src_row_stride];
                resample_row_horizontal(src_row, temp_row, &h_contribs, bytes_per_pixel);
            });
    }

    #[cfg(not(feature = "parallel"))]
    {
        let src_row_stride = src_width * bytes_per_pixel;
        let temp_row_stride = dst_width * bytes_per_pixel;

        for y in 0..src_height {
            let src_start = y * src_row_stride;
            let src_row = &data[src_start..src_start + src_row_stride];
            let temp_start = y * temp_row_stride;
            let temp_row = &mut temp[temp_start..temp_start + temp_row_stride];
            resample_row_horizontal(src_row, temp_row, &h_contribs, bytes_per_pixel);
        }
    }

    // Pass 2: Vertical resampling (parallel when available)
    #[cfg(feature = "parallel")]
    {
        use rayon::prelude::*;

        let dst_row_stride = dst_width * bytes_per_pixel;

        output
            .par_chunks_mut(dst_row_stride)
            .enumerate()
            .for_each(|(dst_y, dst_row)| {
                resample_column_vertical(
                    &temp,
                    dst_row,
                    &v_contribs[dst_y],
                    dst_width,
                    bytes_per_pixel,
                );
            });
    }

    #[cfg(not(feature = "parallel"))]
    {
        let dst_row_stride = dst_width * bytes_per_pixel;

        for dst_y in 0..dst_height {
            let dst_start = dst_y * dst_row_stride;
            let dst_row = &mut output[dst_start..dst_start + dst_row_stride];
            resample_column_vertical(
                &temp,
                dst_row,
                &v_contribs[dst_y],
                dst_width,
                bytes_per_pixel,
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Helper for tests: builds ResizeOptions from positional args
    fn test_resize(
        data: &[u8],
        src_width: u32,
        src_height: u32,
        dst_width: u32,
        dst_height: u32,
        color_type: ColorType,
        algorithm: ResizeAlgorithm,
    ) -> Result<Vec<u8>> {
        let options = ResizeOptions::builder(src_width, src_height)
            .dst(dst_width, dst_height)
            .color_type(color_type)
            .algorithm(algorithm)
            .build();
        resize(data, &options)
    }

    /// Helper for tests: resize_into with positional args
    #[allow(dead_code)]
    #[allow(clippy::too_many_arguments)]
    fn test_resize_into(
        output: &mut Vec<u8>,
        data: &[u8],
        src_width: u32,
        src_height: u32,
        dst_width: u32,
        dst_height: u32,
        color_type: ColorType,
        algorithm: ResizeAlgorithm,
    ) -> Result<()> {
        let options = ResizeOptions::builder(src_width, src_height)
            .dst(dst_width, dst_height)
            .color_type(color_type)
            .algorithm(algorithm)
            .build();
        resize_into(output, data, &options)
    }

    #[test]
    fn test_resize_nearest_basic() {
        // 2x2 RGBA image -> 4x4
        let pixels = vec![
            255, 0, 0, 255, // Red
            0, 255, 0, 255, // Green
            0, 0, 255, 255, // Blue
            255, 255, 0, 255, // Yellow
        ];

        let result = test_resize(
            &pixels,
            2,
            2,
            4,
            4,
            ColorType::Rgba,
            ResizeAlgorithm::Nearest,
        )
        .unwrap();
        assert_eq!(result.len(), 4 * 4 * 4);
    }

    #[test]
    fn test_resize_bilinear_basic() {
        // 2x2 RGB image -> 4x4
        let pixels = vec![
            255, 0, 0, // Red
            0, 255, 0, // Green
            0, 0, 255, // Blue
            255, 255, 0, // Yellow
        ];

        let result = test_resize(
            &pixels,
            2,
            2,
            4,
            4,
            ColorType::Rgb,
            ResizeAlgorithm::Bilinear,
        )
        .unwrap();
        assert_eq!(result.len(), 4 * 4 * 3);
    }

    #[test]
    fn test_resize_lanczos3_basic() {
        // 4x4 grayscale image -> 2x2
        let pixels = vec![0u8; 4 * 4];

        let result = test_resize(
            &pixels,
            4,
            4,
            2,
            2,
            ColorType::Gray,
            ResizeAlgorithm::Lanczos3,
        )
        .unwrap();
        assert_eq!(result.len(), 2 * 2);
    }

    #[test]
    fn test_resize_same_size() {
        // Resize to same size should be near-identity
        let pixels = vec![128u8; 8 * 8 * 4];

        let result = test_resize(
            &pixels,
            8,
            8,
            8,
            8,
            ColorType::Rgba,
            ResizeAlgorithm::Bilinear,
        )
        .unwrap();
        assert_eq!(result.len(), pixels.len());
    }

    #[test]
    fn test_resize_downscale() {
        // 16x16 -> 4x4
        let pixels: Vec<u8> = (0..16 * 16 * 3).map(|i| (i % 256) as u8).collect();

        let result = test_resize(
            &pixels,
            16,
            16,
            4,
            4,
            ColorType::Rgb,
            ResizeAlgorithm::Lanczos3,
        )
        .unwrap();
        assert_eq!(result.len(), 4 * 4 * 3);
    }

    #[test]
    fn test_resize_upscale() {
        // 4x4 -> 16x16
        let pixels: Vec<u8> = (0..4 * 4 * 3).map(|i| (i % 256) as u8).collect();

        let result = test_resize(
            &pixels,
            4,
            4,
            16,
            16,
            ColorType::Rgb,
            ResizeAlgorithm::Bilinear,
        )
        .unwrap();
        assert_eq!(result.len(), 16 * 16 * 3);
    }

    #[test]
    fn test_resize_non_square() {
        // 8x4 -> 4x8
        let pixels = vec![200u8; 8 * 4 * 4];

        let result = test_resize(
            &pixels,
            8,
            4,
            4,
            8,
            ColorType::Rgba,
            ResizeAlgorithm::Nearest,
        )
        .unwrap();
        assert_eq!(result.len(), 4 * 8 * 4);
    }

    #[test]
    fn test_resize_invalid_src_dimensions() {
        let pixels = vec![0u8; 0];
        let result = test_resize(
            &pixels,
            0,
            10,
            5,
            5,
            ColorType::Rgb,
            ResizeAlgorithm::Nearest,
        );
        assert!(matches!(result, Err(Error::InvalidDimensions { .. })));
    }

    #[test]
    fn test_resize_invalid_dst_dimensions() {
        let pixels = vec![0u8; 10 * 10 * 3];
        let result = test_resize(
            &pixels,
            10,
            10,
            0,
            5,
            ColorType::Rgb,
            ResizeAlgorithm::Nearest,
        );
        assert!(matches!(result, Err(Error::InvalidDimensions { .. })));
    }

    #[test]
    fn test_resize_invalid_data_length() {
        let pixels = vec![0u8; 10]; // Wrong size for 10x10 RGB
        let result = test_resize(
            &pixels,
            10,
            10,
            5,
            5,
            ColorType::Rgb,
            ResizeAlgorithm::Nearest,
        );
        assert!(matches!(result, Err(Error::InvalidDataLength { .. })));
    }

    #[test]
    fn test_resize_1x1_to_larger() {
        // 1x1 -> 4x4 (edge case)
        let pixels = vec![255, 128, 64, 255]; // RGBA

        let result = test_resize(
            &pixels,
            1,
            1,
            4,
            4,
            ColorType::Rgba,
            ResizeAlgorithm::Bilinear,
        )
        .unwrap();
        assert_eq!(result.len(), 4 * 4 * 4);

        // All pixels should be the same as the source (single color)
        for i in 0..16 {
            assert_eq!(result[i * 4], 255);
            assert_eq!(result[i * 4 + 1], 128);
            assert_eq!(result[i * 4 + 2], 64);
            assert_eq!(result[i * 4 + 3], 255);
        }
    }

    #[test]
    fn test_resize_to_1x1() {
        // 4x4 -> 1x1 (edge case)
        let pixels = vec![128u8; 4 * 4 * 3];

        let result = test_resize(
            &pixels,
            4,
            4,
            1,
            1,
            ColorType::Rgb,
            ResizeAlgorithm::Lanczos3,
        )
        .unwrap();
        assert_eq!(result.len(), 3);
    }

    #[test]
    fn test_resize_gray_alpha() {
        // Test GrayAlpha (2 bytes per pixel)
        let pixels = vec![100, 200, 150, 250]; // 2x1 GrayAlpha

        let result = test_resize(
            &pixels,
            2,
            1,
            4,
            2,
            ColorType::GrayAlpha,
            ResizeAlgorithm::Bilinear,
        )
        .unwrap();
        assert_eq!(result.len(), 4 * 2 * 2);
    }

    #[test]
    fn test_resize_buffer_reuse() {
        let mut output = Vec::with_capacity(1024);
        let pixels = vec![128u8; 8 * 8 * 4];

        let options1 = ResizeOptions::builder(8, 8)
            .dst(4, 4)
            .color_type(ColorType::Rgba)
            .algorithm(ResizeAlgorithm::Nearest)
            .build();
        resize_into(&mut output, &pixels, &options1).unwrap();

        let first_cap = output.capacity();
        assert_eq!(output.len(), 4 * 4 * 4);

        // Resize again with same output buffer
        let options2 = ResizeOptions::builder(8, 8)
            .dst(4, 4)
            .color_type(ColorType::Rgba)
            .algorithm(ResizeAlgorithm::Bilinear)
            .build();
        resize_into(&mut output, &pixels, &options2).unwrap();

        // Capacity should be preserved (buffer reuse)
        assert!(output.capacity() >= first_cap);
    }

    #[test]
    fn test_resize_algorithm_default() {
        // Default should be Bilinear
        assert_eq!(ResizeAlgorithm::default(), ResizeAlgorithm::Bilinear);
    }

    #[test]
    fn test_lanczos_kernel() {
        // At x=0, kernel should be 1
        assert!((lanczos_kernel(0.0, 3.0) - 1.0).abs() < 0.001);

        // At x >= a, kernel should be 0
        assert!(lanczos_kernel(3.0, 3.0).abs() < 0.001);
        assert!(lanczos_kernel(4.0, 3.0).abs() < f32::EPSILON);

        // Kernel should be symmetric
        assert!((lanczos_kernel(1.5, 3.0) - lanczos_kernel(-1.5, 3.0)).abs() < 0.001);
    }

    #[test]
    fn test_resize_large_dimension_error() {
        let pixels = vec![0u8; 3];
        let result = test_resize(
            &pixels,
            1,
            1,
            (1 << 25) as u32,
            1,
            ColorType::Rgb,
            ResizeAlgorithm::Nearest,
        );
        assert!(matches!(result, Err(Error::ImageTooLarge { .. })));
    }

    #[test]
    fn test_all_algorithms_produce_valid_output() {
        let pixels: Vec<u8> = (0..32 * 32 * 4).map(|i| (i % 256) as u8).collect();

        for algo in [
            ResizeAlgorithm::Nearest,
            ResizeAlgorithm::Bilinear,
            ResizeAlgorithm::Lanczos3,
        ] {
            let result = test_resize(&pixels, 32, 32, 16, 16, ColorType::Rgba, algo).unwrap();
            assert_eq!(result.len(), 16 * 16 * 4);

            // All values should be valid u8
            assert!(!result.is_empty());
        }
    }

    // ============ Tests for separable filtering and precomputed contributions ============

    #[test]
    fn test_precompute_contributions_basic() {
        // Test that contributions are computed correctly for simple cases
        let contribs = precompute_contributions(100, 50); // 2x downscale

        assert_eq!(contribs.len(), 50);

        // Each contribution should have weights that sum to ~1.0
        for contrib in &contribs {
            let sum: f32 = contrib.weights.iter().sum();
            assert!(
                (sum - 1.0).abs() < 0.01,
                "Weights should sum to 1.0, got {sum}"
            );
        }
    }

    #[test]
    fn test_precompute_contributions_upscale() {
        // Test upscaling contributions
        let contribs = precompute_contributions(50, 100); // 2x upscale

        assert_eq!(contribs.len(), 100);

        for contrib in &contribs {
            let sum: f32 = contrib.weights.iter().sum();
            assert!(
                (sum - 1.0).abs() < 0.01,
                "Weights should sum to 1.0, got {sum}"
            );
            // Upscaling should have reasonable kernel support (Lanczos3 has radius 3)
            assert!(
                contrib.weights.len() <= 8,
                "Kernel too large: {}",
                contrib.weights.len()
            );
        }
    }

    #[test]
    fn test_precompute_contributions_same_size() {
        // Same size should produce identity-like contributions
        let contribs = precompute_contributions(100, 100);

        assert_eq!(contribs.len(), 100);

        for (i, contrib) in contribs.iter().enumerate() {
            // The primary weight should be very close to 1.0 at the center
            let max_weight = contrib.weights.iter().cloned().fold(0.0f32, f32::max);
            assert!(
                max_weight > 0.9,
                "Max weight at {i} should be near 1.0, got {max_weight}"
            );
        }
    }

    #[test]
    fn test_lanczos3_large_downscale() {
        // Test significant downscaling (simulates the 5000x6000 -> smaller case)
        let src_w = 500;
        let src_h = 600;
        let dst_w = 100;
        let dst_h = 120;

        let pixels: Vec<u8> = (0..(src_w * src_h * 4))
            .map(|i| ((i * 7) % 256) as u8)
            .collect();

        let result = test_resize(
            &pixels,
            src_w as u32,
            src_h as u32,
            dst_w as u32,
            dst_h as u32,
            ColorType::Rgba,
            ResizeAlgorithm::Lanczos3,
        )
        .unwrap();

        assert_eq!(result.len(), dst_w * dst_h * 4);

        // Verify output is reasonable (not all zeros, not all 255s)
        let sum: u64 = result.iter().map(|&x| x as u64).sum();
        let avg = sum as f64 / result.len() as f64;
        assert!(
            avg > 10.0 && avg < 245.0,
            "Average pixel value {avg} is suspicious"
        );
    }

    #[test]
    fn test_lanczos3_preserves_solid_color() {
        // A solid color image should remain solid after resize
        let color = [100u8, 150, 200, 255];
        let pixels: Vec<u8> = (0..64 * 64).flat_map(|_| color).collect();

        let result = test_resize(
            &pixels,
            64,
            64,
            32,
            32,
            ColorType::Rgba,
            ResizeAlgorithm::Lanczos3,
        )
        .unwrap();

        // All pixels should be very close to the original color
        for i in 0..(32 * 32) {
            let idx = i * 4;
            for c in 0..4 {
                let diff = (result[idx + c] as i32 - color[c] as i32).abs();
                assert!(diff <= 1, "Color drift too large at pixel {i}, channel {c}");
            }
        }
    }

    #[test]
    fn test_lanczos3_upscale_quality() {
        // Test that Lanczos3 produces smooth gradients on upscale
        // Create a simple 2x2 gradient
        let pixels = vec![
            0, 0, 0, 255, // Black
            255, 255, 255, 255, // White
            128, 128, 128, 255, // Gray
            64, 64, 64, 255, // Dark gray
        ];

        let result = test_resize(
            &pixels,
            2,
            2,
            8,
            8,
            ColorType::Rgba,
            ResizeAlgorithm::Lanczos3,
        )
        .unwrap();

        assert_eq!(result.len(), 8 * 8 * 4);

        // Corner pixels should be close to original values
        // Top-left should be near black
        assert!(result[0] < 30, "Top-left should be near black");
        // Top-right (pixel 7) should be near white
        let tr_idx = 7 * 4;
        assert!(result[tr_idx] > 200, "Top-right should be near white");
    }

    #[test]
    fn test_lanczos3_asymmetric_resize() {
        // Test non-uniform scaling (different x and y ratios)
        let pixels: Vec<u8> = (0..100 * 50 * 3).map(|i| (i % 256) as u8).collect();

        let result = test_resize(
            &pixels,
            100,
            50,
            25,
            200, // 4x downscale in X, 4x upscale in Y
            ColorType::Rgb,
            ResizeAlgorithm::Lanczos3,
        )
        .unwrap();

        assert_eq!(result.len(), 25 * 200 * 3);
    }

    #[test]
    fn test_resample_row_horizontal_basic() {
        // Test the horizontal resampling helper directly
        let contribs = precompute_contributions(4, 2);
        let src_row = vec![100u8, 150, 200, 250]; // 4 gray pixels
        let mut dst_row = vec![0u8; 2];

        resample_row_horizontal(&src_row, &mut dst_row, &contribs, 1);

        // Output should be reasonable averages
        assert!(dst_row[0] > 50 && dst_row[0] < 200);
        assert!(dst_row[1] > 100 && dst_row[1] < 255);
    }

    #[test]
    fn test_lanczos3_1x1_edge_case() {
        // 1x1 -> 10x10 with Lanczos3
        let pixels = vec![128, 64, 192, 255]; // Single RGBA pixel

        let result = test_resize(
            &pixels,
            1,
            1,
            10,
            10,
            ColorType::Rgba,
            ResizeAlgorithm::Lanczos3,
        )
        .unwrap();

        assert_eq!(result.len(), 10 * 10 * 4);

        // All pixels should match the source (single color expansion)
        for i in 0..100 {
            let idx = i * 4;
            assert_eq!(result[idx], 128, "R mismatch at {i}");
            assert_eq!(result[idx + 1], 64, "G mismatch at {i}");
            assert_eq!(result[idx + 2], 192, "B mismatch at {i}");
            assert_eq!(result[idx + 3], 255, "A mismatch at {i}");
        }
    }

    #[test]
    fn test_contribution_bounds_are_valid() {
        // Ensure precomputed contributions don't go out of bounds
        for (src, dst) in [(100, 10), (10, 100), (1000, 50), (50, 1000)] {
            let contribs = precompute_contributions(src, dst);

            for (i, contrib) in contribs.iter().enumerate() {
                assert!(
                    contrib.start < src,
                    "Contribution {} start {} >= src {}",
                    i,
                    contrib.start,
                    src
                );
                let end = contrib.start + contrib.weights.len();
                assert!(end <= src, "Contribution {i} end {end} > src {src}");
            }
        }
    }

    #[test]
    fn test_lanczos3_extreme_downscale() {
        // 1000x1000 -> 10x10 (100x downscale)
        let pixels: Vec<u8> = (0..1000 * 1000 * 3).map(|i| (i % 256) as u8).collect();

        let result = test_resize(
            &pixels,
            1000,
            1000,
            10,
            10,
            ColorType::Rgb,
            ResizeAlgorithm::Lanczos3,
        )
        .unwrap();

        assert_eq!(result.len(), 10 * 10 * 3);
    }

    #[test]
    fn test_lanczos3_prime_dimensions() {
        // Test with prime number dimensions (edge case for algorithms)
        let pixels: Vec<u8> = (0..97 * 89 * 4).map(|i| (i % 256) as u8).collect();

        let result = test_resize(
            &pixels,
            97,
            89,
            41,
            37,
            ColorType::Rgba,
            ResizeAlgorithm::Lanczos3,
        )
        .unwrap();

        assert_eq!(result.len(), 41 * 37 * 4);
    }
}