oxigdal-ml 0.1.4

Machine learning capabilities for OxiGDAL - ONNX Runtime integration for geospatial ML workflows
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
//! Data augmentation for geospatial imagery
//!
//! This module provides comprehensive data augmentation techniques specifically
//! designed for geospatial and satellite imagery.

use crate::error::{PreprocessingError, Result};
use oxigdal_core::buffer::RasterBuffer;
// use oxigdal_core::types::RasterDataType;
use scirs2_core::random::prelude::{StdRng, seeded_rng};
use tracing::debug;

/// Generates a Gaussian random number using Box-Muller transform
fn gaussian_random(rng: &mut StdRng, mean: f64, std_dev: f64) -> f64 {
    use std::f64::consts::PI;

    // Box-Muller transform
    let u1: f64 = rng.random();
    let u2: f64 = rng.random();

    let z0: f64 = (-2.0_f64 * u1.ln()).sqrt() * (2.0_f64 * PI * u2).cos();
    mean + std_dev * z0
}

/// Augmentation configuration
#[derive(Debug, Clone, Default)]
pub struct AugmentationConfig {
    /// Enable horizontal flip
    pub horizontal_flip: bool,
    /// Enable vertical flip
    pub vertical_flip: bool,
    /// Rotation angles (degrees)
    pub rotation_angles: Vec<f32>,
    /// Enable random crops
    pub random_crop: bool,
    /// Crop size (if random_crop enabled)
    pub crop_size: Option<(u64, u64)>,
    /// Brightness adjustment range
    pub brightness_range: Option<(f32, f32)>,
    /// Contrast adjustment range
    pub contrast_range: Option<(f32, f32)>,
    /// Saturation adjustment range (for RGB)
    pub saturation_range: Option<(f32, f32)>,
    /// Add Gaussian noise
    pub gaussian_noise: Option<f32>,
    /// Add salt and pepper noise
    pub salt_pepper_noise: Option<f32>,
    /// Gaussian blur kernel size
    pub blur_kernel: Option<usize>,
}

impl AugmentationConfig {
    /// Creates a builder for augmentation configuration
    #[must_use]
    pub fn builder() -> AugmentationConfigBuilder {
        AugmentationConfigBuilder::default()
    }

    /// Creates a standard augmentation configuration
    #[must_use]
    pub fn standard() -> Self {
        Self {
            horizontal_flip: true,
            vertical_flip: true,
            rotation_angles: vec![90.0, 180.0, 270.0],
            random_crop: false,
            crop_size: None,
            brightness_range: Some((-0.2, 0.2)),
            contrast_range: Some((0.8, 1.2)),
            saturation_range: Some((0.8, 1.2)),
            gaussian_noise: Some(0.01),
            salt_pepper_noise: None,
            blur_kernel: None,
        }
    }

    /// Creates an aggressive augmentation configuration
    #[must_use]
    pub fn aggressive() -> Self {
        Self {
            horizontal_flip: true,
            vertical_flip: true,
            rotation_angles: vec![45.0, 90.0, 135.0, 180.0, 225.0, 270.0, 315.0],
            random_crop: true,
            crop_size: Some((256, 256)),
            brightness_range: Some((-0.3, 0.3)),
            contrast_range: Some((0.7, 1.3)),
            saturation_range: Some((0.7, 1.3)),
            gaussian_noise: Some(0.02),
            salt_pepper_noise: Some(0.01),
            blur_kernel: Some(3),
        }
    }
}

/// Builder for augmentation configuration
#[derive(Debug, Default)]
pub struct AugmentationConfigBuilder {
    horizontal_flip: bool,
    vertical_flip: bool,
    rotation_angles: Vec<f32>,
    random_crop: bool,
    crop_size: Option<(u64, u64)>,
    brightness_range: Option<(f32, f32)>,
    contrast_range: Option<(f32, f32)>,
    saturation_range: Option<(f32, f32)>,
    gaussian_noise: Option<f32>,
    salt_pepper_noise: Option<f32>,
    blur_kernel: Option<usize>,
}

impl AugmentationConfigBuilder {
    /// Enables horizontal flip
    #[must_use]
    pub fn horizontal_flip(mut self, enable: bool) -> Self {
        self.horizontal_flip = enable;
        self
    }

    /// Enables vertical flip
    #[must_use]
    pub fn vertical_flip(mut self, enable: bool) -> Self {
        self.vertical_flip = enable;
        self
    }

    /// Sets rotation angles
    #[must_use]
    pub fn rotation_angles(mut self, angles: Vec<f32>) -> Self {
        self.rotation_angles = angles;
        self
    }

    /// Enables random cropping
    #[must_use]
    pub fn random_crop(mut self, enable: bool, size: Option<(u64, u64)>) -> Self {
        self.random_crop = enable;
        self.crop_size = size;
        self
    }

    /// Sets brightness adjustment range
    #[must_use]
    pub fn brightness_range(mut self, min: f32, max: f32) -> Self {
        self.brightness_range = Some((min, max));
        self
    }

    /// Sets contrast adjustment range
    #[must_use]
    pub fn contrast_range(mut self, min: f32, max: f32) -> Self {
        self.contrast_range = Some((min, max));
        self
    }

    /// Sets saturation adjustment range
    #[must_use]
    pub fn saturation_range(mut self, min: f32, max: f32) -> Self {
        self.saturation_range = Some((min, max));
        self
    }

    /// Sets Gaussian noise standard deviation
    #[must_use]
    pub fn gaussian_noise(mut self, std_dev: f32) -> Self {
        self.gaussian_noise = Some(std_dev);
        self
    }

    /// Sets salt and pepper noise probability
    #[must_use]
    pub fn salt_pepper_noise(mut self, prob: f32) -> Self {
        self.salt_pepper_noise = Some(prob);
        self
    }

    /// Sets blur kernel size
    #[must_use]
    pub fn blur_kernel(mut self, size: usize) -> Self {
        self.blur_kernel = Some(size);
        self
    }

    /// Builds the configuration
    #[must_use]
    pub fn build(self) -> AugmentationConfig {
        AugmentationConfig {
            horizontal_flip: self.horizontal_flip,
            vertical_flip: self.vertical_flip,
            rotation_angles: self.rotation_angles,
            random_crop: self.random_crop,
            crop_size: self.crop_size,
            brightness_range: self.brightness_range,
            contrast_range: self.contrast_range,
            saturation_range: self.saturation_range,
            gaussian_noise: self.gaussian_noise,
            salt_pepper_noise: self.salt_pepper_noise,
            blur_kernel: self.blur_kernel,
        }
    }
}

/// Applies horizontal flip augmentation
///
/// # Errors
/// Returns an error if the operation fails
pub fn horizontal_flip(input: &RasterBuffer) -> Result<RasterBuffer> {
    debug!("Applying horizontal flip");
    let mut output = RasterBuffer::zeros(input.width(), input.height(), input.data_type());

    for y in 0..input.height() {
        for x in 0..input.width() {
            let flipped_x = input.width() - 1 - x;
            let value =
                input
                    .get_pixel(x, y)
                    .map_err(|e| PreprocessingError::AugmentationFailed {
                        reason: format!("Failed to read pixel: {}", e),
                    })?;
            output.set_pixel(flipped_x, y, value).map_err(|e| {
                PreprocessingError::AugmentationFailed {
                    reason: format!("Failed to write pixel: {}", e),
                }
            })?;
        }
    }

    Ok(output)
}

/// Applies vertical flip augmentation
///
/// # Errors
/// Returns an error if the operation fails
pub fn vertical_flip(input: &RasterBuffer) -> Result<RasterBuffer> {
    debug!("Applying vertical flip");
    let mut output = RasterBuffer::zeros(input.width(), input.height(), input.data_type());

    for y in 0..input.height() {
        for x in 0..input.width() {
            let flipped_y = input.height() - 1 - y;
            let value =
                input
                    .get_pixel(x, y)
                    .map_err(|e| PreprocessingError::AugmentationFailed {
                        reason: format!("Failed to read pixel: {}", e),
                    })?;
            output.set_pixel(x, flipped_y, value).map_err(|e| {
                PreprocessingError::AugmentationFailed {
                    reason: format!("Failed to write pixel: {}", e),
                }
            })?;
        }
    }

    Ok(output)
}

/// Applies rotation augmentation
///
/// # Errors
/// Returns an error if the operation fails
pub fn rotate(input: &RasterBuffer, angle_degrees: f32) -> Result<RasterBuffer> {
    debug!("Applying rotation: {} degrees", angle_degrees);

    // Simple rotation for 90-degree multiples
    if (angle_degrees % 90.0).abs() < 0.1 {
        let times = ((angle_degrees / 90.0).round() as i32).rem_euclid(4);
        return rotate_90_times(input, times as usize);
    }

    // General rotation with bilinear interpolation
    rotate_general(input, angle_degrees)
}

/// Applies general rotation with bilinear interpolation
///
/// Uses affine transformation and bilinear interpolation for arbitrary angles.
/// Out-of-bounds pixels are filled with zeros.
///
/// # Errors
/// Returns an error if pixel access fails
fn rotate_general(input: &RasterBuffer, angle_degrees: f32) -> Result<RasterBuffer> {
    let width = input.width() as f64;
    let height = input.height() as f64;

    // Convert angle to radians
    let angle = angle_degrees as f64 * std::f64::consts::PI / 180.0;
    let cos_angle = angle.cos();
    let sin_angle = angle.sin();

    // Calculate output dimensions to fit the entire rotated image
    let corners = [(0.0, 0.0), (width, 0.0), (0.0, height), (width, height)];

    let mut min_x = f64::INFINITY;
    let mut max_x = f64::NEG_INFINITY;
    let mut min_y = f64::INFINITY;
    let mut max_y = f64::NEG_INFINITY;

    // Find bounding box of rotated image
    for (x, y) in &corners {
        let rotated_x = x * cos_angle - y * sin_angle;
        let rotated_y = x * sin_angle + y * cos_angle;
        min_x = min_x.min(rotated_x);
        max_x = max_x.max(rotated_x);
        min_y = min_y.min(rotated_y);
        max_y = max_y.max(rotated_y);
    }

    let out_width = (max_x - min_x).ceil() as u64;
    let out_height = (max_y - min_y).ceil() as u64;

    let mut output = RasterBuffer::zeros(out_width, out_height, input.data_type());

    // Center of input image
    let cx = width / 2.0;
    let cy = height / 2.0;

    // Center of output image
    let out_cx = (max_x - min_x) / 2.0;
    let out_cy = (max_y - min_y) / 2.0;

    // For each output pixel, find corresponding input pixel
    for out_y in 0..out_height {
        for out_x in 0..out_width {
            // Translate to center
            let x = out_x as f64 - out_cx;
            let y = out_y as f64 - out_cy;

            // Inverse rotation
            let src_x = x * cos_angle + y * sin_angle + cx;
            let src_y = -x * sin_angle + y * cos_angle + cy;

            // Apply bilinear interpolation
            let value = bilinear_interpolate(input, src_x, src_y)?;

            output.set_pixel(out_x, out_y, value).map_err(|e| {
                PreprocessingError::AugmentationFailed {
                    reason: format!("Failed to write pixel: {}", e),
                }
            })?;
        }
    }

    Ok(output)
}

/// Applies bilinear interpolation at the given coordinates
///
/// Returns 0.0 for out-of-bounds coordinates
///
/// # Errors
/// Returns an error if pixel access fails
fn bilinear_interpolate(input: &RasterBuffer, x: f64, y: f64) -> Result<f64> {
    // Check bounds
    if x < 0.0 || y < 0.0 || x >= input.width() as f64 - 1.0 || y >= input.height() as f64 - 1.0 {
        return Ok(0.0); // Fill with zeros for out-of-bounds
    }

    // Get the four surrounding pixels
    let x0 = x.floor() as u64;
    let y0 = y.floor() as u64;
    let x1 = x0 + 1;
    let y1 = y0 + 1;

    // Fractional parts
    let dx = x - x0 as f64;
    let dy = y - y0 as f64;

    // Get pixel values
    let p00 = input
        .get_pixel(x0, y0)
        .map_err(|e| PreprocessingError::AugmentationFailed {
            reason: format!("Failed to read pixel: {}", e),
        })?;
    let p10 = input
        .get_pixel(x1, y0)
        .map_err(|e| PreprocessingError::AugmentationFailed {
            reason: format!("Failed to read pixel: {}", e),
        })?;
    let p01 = input
        .get_pixel(x0, y1)
        .map_err(|e| PreprocessingError::AugmentationFailed {
            reason: format!("Failed to read pixel: {}", e),
        })?;
    let p11 = input
        .get_pixel(x1, y1)
        .map_err(|e| PreprocessingError::AugmentationFailed {
            reason: format!("Failed to read pixel: {}", e),
        })?;

    // Bilinear interpolation formula:
    // value = (1-dx)(1-dy)·p00 + dx(1-dy)·p10 + (1-dx)dy·p01 + dx·dy·p11
    let value = (1.0 - dx) * (1.0 - dy) * p00
        + dx * (1.0 - dy) * p10
        + (1.0 - dx) * dy * p01
        + dx * dy * p11;

    Ok(value)
}

/// Rotates image by 90 degrees multiple times
fn rotate_90_times(input: &RasterBuffer, times: usize) -> Result<RasterBuffer> {
    let mut current = input.clone();

    for _ in 0..times {
        current = rotate_90(&current)?;
    }

    Ok(current)
}

/// Rotates image by 90 degrees clockwise
fn rotate_90(input: &RasterBuffer) -> Result<RasterBuffer> {
    let mut output = RasterBuffer::zeros(input.height(), input.width(), input.data_type());

    for y in 0..input.height() {
        for x in 0..input.width() {
            let new_x = y;
            let new_y = input.width() - 1 - x;
            let value =
                input
                    .get_pixel(x, y)
                    .map_err(|e| PreprocessingError::AugmentationFailed {
                        reason: format!("Failed to read pixel: {}", e),
                    })?;
            output.set_pixel(new_x, new_y, value).map_err(|e| {
                PreprocessingError::AugmentationFailed {
                    reason: format!("Failed to write pixel: {}", e),
                }
            })?;
        }
    }

    Ok(output)
}

/// Applies random crop augmentation
///
/// # Errors
/// Returns an error if the operation fails
pub fn random_crop(
    input: &RasterBuffer,
    crop_width: u64,
    crop_height: u64,
) -> Result<RasterBuffer> {
    debug!("Applying random crop: {}x{}", crop_width, crop_height);

    if crop_width > input.width() || crop_height > input.height() {
        return Err(PreprocessingError::AugmentationFailed {
            reason: format!(
                "Crop size ({}x{}) larger than input ({}x{})",
                crop_width,
                crop_height,
                input.width(),
                input.height()
            ),
        }
        .into());
    }

    // Use SciRS2-Core RNG for random offset
    let mut rng = seeded_rng(
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0),
    );
    let max_x_offset = input.width() - crop_width;
    let max_y_offset = input.height() - crop_height;

    let x_offset = if max_x_offset > 0 {
        let random_val: f64 = rng.random();
        (random_val * (max_x_offset + 1) as f64) as u64
    } else {
        0
    };
    let y_offset = if max_y_offset > 0 {
        let random_val: f64 = rng.random();
        (random_val * (max_y_offset + 1) as f64) as u64
    } else {
        0
    };

    let mut output = RasterBuffer::zeros(crop_width, crop_height, input.data_type());

    for y in 0..crop_height {
        for x in 0..crop_width {
            let value = input.get_pixel(x + x_offset, y + y_offset).map_err(|e| {
                PreprocessingError::AugmentationFailed {
                    reason: format!("Failed to read pixel: {}", e),
                }
            })?;
            output
                .set_pixel(x, y, value)
                .map_err(|e| PreprocessingError::AugmentationFailed {
                    reason: format!("Failed to write pixel: {}", e),
                })?;
        }
    }

    Ok(output)
}

/// Adjusts brightness
///
/// # Errors
/// Returns an error if the operation fails
pub fn adjust_brightness(input: &RasterBuffer, delta: f32) -> Result<RasterBuffer> {
    debug!("Adjusting brightness by {}", delta);
    let mut output = input.clone();

    for y in 0..input.height() {
        for x in 0..input.width() {
            let value =
                input
                    .get_pixel(x, y)
                    .map_err(|e| PreprocessingError::AugmentationFailed {
                        reason: format!("Failed to read pixel: {}", e),
                    })?;
            let adjusted = (value as f32 + delta).clamp(0.0, 1.0) as f64;
            output.set_pixel(x, y, adjusted).map_err(|e| {
                PreprocessingError::AugmentationFailed {
                    reason: format!("Failed to write pixel: {}", e),
                }
            })?;
        }
    }

    Ok(output)
}

/// Adjusts contrast
///
/// # Errors
/// Returns an error if the operation fails
pub fn adjust_contrast(input: &RasterBuffer, factor: f32) -> Result<RasterBuffer> {
    debug!("Adjusting contrast by factor {}", factor);
    let mut output = input.clone();

    // Calculate mean
    let mut sum = 0.0;
    let mut count = 0u64;
    for y in 0..input.height() {
        for x in 0..input.width() {
            let value =
                input
                    .get_pixel(x, y)
                    .map_err(|e| PreprocessingError::AugmentationFailed {
                        reason: format!("Failed to read pixel: {}", e),
                    })?;
            sum += value;
            count += 1;
        }
    }
    let mean = sum / count as f64;

    // Adjust contrast
    for y in 0..input.height() {
        for x in 0..input.width() {
            let value =
                input
                    .get_pixel(x, y)
                    .map_err(|e| PreprocessingError::AugmentationFailed {
                        reason: format!("Failed to read pixel: {}", e),
                    })?;
            let adjusted = (mean + (value - mean) * factor as f64).clamp(0.0, 1.0);
            output.set_pixel(x, y, adjusted).map_err(|e| {
                PreprocessingError::AugmentationFailed {
                    reason: format!("Failed to write pixel: {}", e),
                }
            })?;
        }
    }

    Ok(output)
}

/// Adds Gaussian noise
///
/// # Errors
/// Returns an error if the operation fails
pub fn add_gaussian_noise(input: &RasterBuffer, std_dev: f32) -> Result<RasterBuffer> {
    debug!("Adding Gaussian noise with std_dev={}", std_dev);
    let mut output = input.clone();

    // Use SciRS2-Core RNG with Box-Muller transform for Gaussian noise
    let mut rng = seeded_rng(
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0),
    );

    for y in 0..input.height() {
        for x in 0..input.width() {
            let value =
                input
                    .get_pixel(x, y)
                    .map_err(|e| PreprocessingError::AugmentationFailed {
                        reason: format!("Failed to read pixel: {}", e),
                    })?;

            // Generate Gaussian noise using Box-Muller transform
            let noise = gaussian_random(&mut rng, 0.0, std_dev as f64);
            let noisy = (value + noise).clamp(0.0, 1.0);

            output
                .set_pixel(x, y, noisy)
                .map_err(|e| PreprocessingError::AugmentationFailed {
                    reason: format!("Failed to write pixel: {}", e),
                })?;
        }
    }

    Ok(output)
}

/// Applies Gaussian blur
///
/// Uses separable Gaussian convolution for efficiency (O(n·k) instead of O(n·k²)).
/// Applies horizontal blur pass followed by vertical blur pass.
/// Edges are handled with mirror padding.
///
/// # Errors
/// Returns an error if the operation fails
pub fn gaussian_blur(input: &RasterBuffer, kernel_size: usize) -> Result<RasterBuffer> {
    debug!("Applying Gaussian blur with kernel size {}", kernel_size);

    if kernel_size % 2 == 0 {
        return Err(PreprocessingError::AugmentationFailed {
            reason: "Kernel size must be odd".to_string(),
        }
        .into());
    }

    if kernel_size < 3 {
        return Err(PreprocessingError::AugmentationFailed {
            reason: "Kernel size must be at least 3".to_string(),
        }
        .into());
    }

    // Calculate sigma from kernel size using standard formula
    // sigma = 0.3 * ((kernel_size - 1) * 0.5 - 1) + 0.8
    let sigma = 0.3 * ((kernel_size as f64 - 1.0) * 0.5 - 1.0) + 0.8;

    // Generate 1D Gaussian kernel
    let kernel = generate_gaussian_kernel(kernel_size, sigma)?;

    // Apply horizontal blur
    let horizontal = apply_horizontal_blur(input, &kernel)?;

    // Apply vertical blur
    apply_vertical_blur(&horizontal, &kernel)
}

/// Generates a 1D Gaussian kernel
///
/// Uses the formula: G(x) = exp(-x²/(2σ²)) / √(2πσ²)
/// Normalizes the kernel to sum to 1.0
///
/// # Errors
/// Returns an error if sigma is invalid
fn generate_gaussian_kernel(size: usize, sigma: f64) -> Result<Vec<f64>> {
    if sigma <= 0.0 {
        return Err(PreprocessingError::AugmentationFailed {
            reason: "Sigma must be positive".to_string(),
        }
        .into());
    }

    let radius = (size / 2) as i32;
    let mut kernel = Vec::with_capacity(size);
    let mut sum = 0.0;

    // Generate Gaussian values
    for i in -radius..=radius {
        let x = i as f64;
        let value = (-x * x / (2.0 * sigma * sigma)).exp();
        kernel.push(value);
        sum += value;
    }

    // Normalize to sum to 1.0
    for value in &mut kernel {
        *value /= sum;
    }

    Ok(kernel)
}

/// Applies horizontal blur using the given kernel
///
/// Uses mirror padding at image edges
///
/// # Errors
/// Returns an error if pixel access fails
fn apply_horizontal_blur(input: &RasterBuffer, kernel: &[f64]) -> Result<RasterBuffer> {
    let width = input.width();
    let height = input.height();
    let radius = (kernel.len() / 2) as i64;

    let mut output = RasterBuffer::zeros(width, height, input.data_type());

    for y in 0..height {
        for x in 0..width {
            let mut sum = 0.0;

            // Apply convolution
            for (k_idx, &k_val) in kernel.iter().enumerate() {
                let offset = k_idx as i64 - radius;
                let src_x = x as i64 + offset;

                // Mirror padding for edges
                let safe_x = if src_x < 0 {
                    (-src_x) as u64
                } else if src_x >= width as i64 {
                    (2 * width as i64 - src_x - 2) as u64
                } else {
                    src_x as u64
                };

                // Clamp to valid range
                let clamped_x = safe_x.min(width - 1);

                let pixel = input.get_pixel(clamped_x, y).map_err(|e| {
                    PreprocessingError::AugmentationFailed {
                        reason: format!("Failed to read pixel: {}", e),
                    }
                })?;

                sum += pixel * k_val;
            }

            output
                .set_pixel(x, y, sum)
                .map_err(|e| PreprocessingError::AugmentationFailed {
                    reason: format!("Failed to write pixel: {}", e),
                })?;
        }
    }

    Ok(output)
}

/// Applies vertical blur using the given kernel
///
/// Uses mirror padding at image edges
///
/// # Errors
/// Returns an error if pixel access fails
fn apply_vertical_blur(input: &RasterBuffer, kernel: &[f64]) -> Result<RasterBuffer> {
    let width = input.width();
    let height = input.height();
    let radius = (kernel.len() / 2) as i64;

    let mut output = RasterBuffer::zeros(width, height, input.data_type());

    for y in 0..height {
        for x in 0..width {
            let mut sum = 0.0;

            // Apply convolution
            for (k_idx, &k_val) in kernel.iter().enumerate() {
                let offset = k_idx as i64 - radius;
                let src_y = y as i64 + offset;

                // Mirror padding for edges
                let safe_y = if src_y < 0 {
                    (-src_y) as u64
                } else if src_y >= height as i64 {
                    (2 * height as i64 - src_y - 2) as u64
                } else {
                    src_y as u64
                };

                // Clamp to valid range
                let clamped_y = safe_y.min(height - 1);

                let pixel = input.get_pixel(x, clamped_y).map_err(|e| {
                    PreprocessingError::AugmentationFailed {
                        reason: format!("Failed to read pixel: {}", e),
                    }
                })?;

                sum += pixel * k_val;
            }

            output
                .set_pixel(x, y, sum)
                .map_err(|e| PreprocessingError::AugmentationFailed {
                    reason: format!("Failed to write pixel: {}", e),
                })?;
        }
    }

    Ok(output)
}

/// Applies a sequence of augmentations according to configuration
///
/// # Errors
/// Returns an error if any augmentation fails
pub fn apply_augmentation(
    input: &RasterBuffer,
    config: &AugmentationConfig,
) -> Result<Vec<RasterBuffer>> {
    let mut augmented = vec![input.clone()]; // Original

    // Horizontal flip
    if config.horizontal_flip {
        augmented.push(horizontal_flip(input)?);
    }

    // Vertical flip
    if config.vertical_flip {
        augmented.push(vertical_flip(input)?);
    }

    // Rotations
    for angle in &config.rotation_angles {
        augmented.push(rotate(input, *angle)?);
    }

    // Brightness adjustments
    if let Some((min, max)) = config.brightness_range {
        augmented.push(adjust_brightness(input, min)?);
        augmented.push(adjust_brightness(input, max)?);
    }

    // Contrast adjustments
    if let Some((min, max)) = config.contrast_range {
        augmented.push(adjust_contrast(input, min)?);
        augmented.push(adjust_contrast(input, max)?);
    }

    // Noise
    if let Some(std_dev) = config.gaussian_noise {
        augmented.push(add_gaussian_noise(input, std_dev)?);
    }

    // Blur
    if let Some(kernel_size) = config.blur_kernel {
        augmented.push(gaussian_blur(input, kernel_size)?);
    }

    Ok(augmented)
}

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

    #[test]
    fn test_augmentation_config_builder() {
        let config = AugmentationConfig::builder()
            .horizontal_flip(true)
            .vertical_flip(true)
            .rotation_angles(vec![90.0, 180.0])
            .brightness_range(-0.2, 0.2)
            .build();

        assert!(config.horizontal_flip);
        assert!(config.vertical_flip);
        assert_eq!(config.rotation_angles.len(), 2);
    }

    #[test]
    fn test_standard_config() {
        let config = AugmentationConfig::standard();
        assert!(config.horizontal_flip);
        assert!(config.brightness_range.is_some());
    }

    #[test]
    fn test_aggressive_config() {
        let config = AugmentationConfig::aggressive();
        assert!(config.random_crop);
        assert!(config.salt_pepper_noise.is_some());
        assert!(config.rotation_angles.len() > 3);
    }

    #[test]
    fn test_horizontal_flip() {
        let input = RasterBuffer::zeros(4, 4, RasterDataType::Float32);
        let result = horizontal_flip(&input);
        assert!(result.is_ok());

        let output = result.expect("Should succeed");
        assert_eq!(output.width(), input.width());
        assert_eq!(output.height(), input.height());
    }

    #[test]
    fn test_vertical_flip() {
        let input = RasterBuffer::zeros(4, 4, RasterDataType::Float32);
        let result = vertical_flip(&input);
        assert!(result.is_ok());
    }

    #[test]
    fn test_rotate_90() {
        let input = RasterBuffer::zeros(4, 4, RasterDataType::Float32);
        let result = rotate(&input, 90.0);
        assert!(result.is_ok());

        let output = result.expect("Should succeed");
        assert_eq!(output.width(), input.height());
        assert_eq!(output.height(), input.width());
    }

    #[test]
    fn test_random_crop() {
        let input = RasterBuffer::zeros(100, 100, RasterDataType::Float32);
        let result = random_crop(&input, 64, 64);
        assert!(result.is_ok());

        let output = result.expect("Should succeed");
        assert_eq!(output.width(), 64);
        assert_eq!(output.height(), 64);
    }

    #[test]
    fn test_random_crop_invalid_size() {
        let input = RasterBuffer::zeros(50, 50, RasterDataType::Float32);
        let result = random_crop(&input, 100, 100);
        assert!(result.is_err());
    }

    #[test]
    fn test_rotate_45_degrees() {
        // Test rotation at 45 degrees
        let mut input = RasterBuffer::zeros(10, 10, RasterDataType::Float32);

        // Set a specific pixel to test rotation
        let _ = input.set_pixel(5, 5, 1.0);

        let result = rotate(&input, 45.0);
        assert!(result.is_ok());

        let output = result.expect("Should succeed");
        // Output dimensions should be larger to accommodate rotation
        assert!(output.width() > input.width() || output.height() > input.height());
    }

    #[test]
    fn test_rotate_180_degrees() {
        // Test rotation at 180 degrees (should use optimized path)
        let mut input = RasterBuffer::zeros(8, 8, RasterDataType::Float32);

        // Set corner pixels
        let _ = input.set_pixel(0, 0, 1.0);
        let _ = input.set_pixel(7, 7, 0.5);

        let result = rotate(&input, 180.0);
        assert!(result.is_ok());

        let output = result.expect("Should succeed");
        assert_eq!(output.width(), input.width());
        assert_eq!(output.height(), input.height());

        // Check that corners are swapped
        let top_left = output.get_pixel(0, 0).expect("Should succeed");
        let bottom_right = output.get_pixel(7, 7).expect("Should succeed");

        assert!((top_left - 0.5).abs() < 0.01, "Top-left should be ~0.5");
        assert!(
            (bottom_right - 1.0).abs() < 0.01,
            "Bottom-right should be ~1.0"
        );
    }

    #[test]
    fn test_rotate_arbitrary_angle() {
        // Test rotation at arbitrary angle
        let input = RasterBuffer::zeros(16, 16, RasterDataType::Float32);

        let result = rotate(&input, 30.0);
        assert!(result.is_ok());

        let output = result.expect("Should succeed");
        // Verify output is created (dimensions may vary)
        assert!(output.width() > 0);
        assert!(output.height() > 0);
    }

    #[test]
    fn test_bilinear_interpolation() {
        // Test bilinear interpolation function
        let mut input = RasterBuffer::zeros(4, 4, RasterDataType::Float32);

        // Set up a gradient
        for y in 0..4 {
            for x in 0..4 {
                let value = (x + y) as f64 * 0.1;
                let _ = input.set_pixel(x, y, value);
            }
        }

        // Test interpolation at fractional coordinates
        let result = bilinear_interpolate(&input, 1.5, 1.5);
        assert!(result.is_ok());

        let value = result.expect("Should succeed");
        // Value should be between surrounding pixels
        assert!(
            (0.2..=0.4).contains(&value),
            "Value {} not in expected range",
            value
        );
    }

    #[test]
    fn test_bilinear_interpolation_out_of_bounds() {
        let input = RasterBuffer::zeros(4, 4, RasterDataType::Float32);

        // Test out-of-bounds coordinates
        let result = bilinear_interpolate(&input, -1.0, 2.0);
        assert!(result.is_ok());

        let value = result.expect("Should succeed");
        assert_eq!(value, 0.0, "Out-of-bounds should return 0.0");
    }

    #[test]
    fn test_gaussian_blur_basic() {
        // Test basic Gaussian blur
        let input = RasterBuffer::zeros(16, 16, RasterDataType::Float32);

        let result = gaussian_blur(&input, 3);
        assert!(result.is_ok());

        let output = result.expect("Should succeed");
        assert_eq!(output.width(), input.width());
        assert_eq!(output.height(), input.height());
    }

    #[test]
    fn test_gaussian_blur_larger_kernel() {
        // Test with larger kernel
        let input = RasterBuffer::zeros(20, 20, RasterDataType::Float32);

        let result = gaussian_blur(&input, 5);
        assert!(result.is_ok());

        let output = result.expect("Should succeed");
        assert_eq!(output.width(), input.width());
        assert_eq!(output.height(), input.height());
    }

    #[test]
    fn test_gaussian_blur_even_kernel_fails() {
        // Test that even kernel size fails
        let input = RasterBuffer::zeros(10, 10, RasterDataType::Float32);

        let result = gaussian_blur(&input, 4);
        assert!(result.is_err());
    }

    #[test]
    fn test_gaussian_blur_too_small_kernel_fails() {
        // Test that kernel size < 3 fails
        let input = RasterBuffer::zeros(10, 10, RasterDataType::Float32);

        let result = gaussian_blur(&input, 1);
        assert!(result.is_err());
    }

    #[test]
    fn test_gaussian_blur_smoothing() {
        // Test that blur actually smooths the image
        let mut input = RasterBuffer::zeros(10, 10, RasterDataType::Float32);

        // Create a sharp edge
        for y in 0..10 {
            for x in 0..5 {
                let _ = input.set_pixel(x, y, 1.0);
            }
        }

        let result = gaussian_blur(&input, 3);
        assert!(result.is_ok());

        let output = result.expect("Should succeed");

        // Check that the edge is smoothed (pixel at boundary should be between 0 and 1)
        let edge_value = output.get_pixel(4, 5).expect("Should succeed");
        assert!(
            edge_value > 0.4 && edge_value < 0.85,
            "Edge value {} should be smoothed",
            edge_value
        );

        // Check that the transition area has smoothing
        let inside_value = output.get_pixel(2, 5).expect("Should succeed");
        let outside_value = output.get_pixel(7, 5).expect("Should succeed");

        // Inside should be close to 1, outside should be close to 0
        assert!(
            inside_value > 0.9,
            "Inside value {} should be high",
            inside_value
        );
        assert!(
            outside_value < 0.15,
            "Outside value {} should be low",
            outside_value
        );
    }

    #[test]
    fn test_generate_gaussian_kernel() {
        // Test kernel generation
        let result = generate_gaussian_kernel(5, 1.0);
        assert!(result.is_ok());

        let kernel = result.expect("Should succeed");
        assert_eq!(kernel.len(), 5);

        // Kernel should sum to approximately 1.0
        let sum: f64 = kernel.iter().sum();
        assert!(
            (sum - 1.0).abs() < 1e-10,
            "Kernel sum {} should be ~1.0",
            sum
        );

        // Kernel should be symmetric
        assert!((kernel[0] - kernel[4]).abs() < 1e-10);
        assert!((kernel[1] - kernel[3]).abs() < 1e-10);

        // Center should be largest value
        assert!(kernel[2] > kernel[1]);
        assert!(kernel[2] > kernel[0]);
    }

    #[test]
    fn test_generate_gaussian_kernel_invalid_sigma() {
        // Test that invalid sigma fails
        let result = generate_gaussian_kernel(5, 0.0);
        assert!(result.is_err());

        let result = generate_gaussian_kernel(5, -1.0);
        assert!(result.is_err());
    }

    #[test]
    fn test_horizontal_blur() {
        // Test horizontal blur function
        let mut input = RasterBuffer::zeros(10, 10, RasterDataType::Float32);

        // Set middle row to 1.0
        for x in 0..10 {
            let _ = input.set_pixel(x, 5, 1.0);
        }

        let kernel = vec![0.25, 0.5, 0.25]; // Simple box-like kernel
        let result = apply_horizontal_blur(&input, &kernel);
        assert!(result.is_ok());

        let output = result.expect("Should succeed");

        // Middle row should still be high, but slightly smoothed
        let value = output.get_pixel(5, 5).expect("Should succeed");
        assert!(value > 0.4, "Value {} should be high", value);
    }

    #[test]
    fn test_vertical_blur() {
        // Test vertical blur function
        let mut input = RasterBuffer::zeros(10, 10, RasterDataType::Float32);

        // Set middle column to 1.0
        for y in 0..10 {
            let _ = input.set_pixel(5, y, 1.0);
        }

        let kernel = vec![0.25, 0.5, 0.25]; // Simple box-like kernel
        let result = apply_vertical_blur(&input, &kernel);
        assert!(result.is_ok());

        let output = result.expect("Should succeed");

        // Middle column should still be high, but slightly smoothed
        let value = output.get_pixel(5, 5).expect("Should succeed");
        assert!(value > 0.4, "Value {} should be high", value);
    }

    #[test]
    fn test_blur_edge_handling() {
        // Test that edge handling works correctly
        let mut input = RasterBuffer::zeros(5, 5, RasterDataType::Float32);

        // Set corner pixel
        let _ = input.set_pixel(0, 0, 1.0);

        let result = gaussian_blur(&input, 3);
        assert!(result.is_ok());

        let output = result.expect("Should succeed");

        // Corner should be smoothed but not zero (due to mirror padding)
        let corner = output.get_pixel(0, 0).expect("Should succeed");
        assert!(
            corner > 0.0,
            "Corner should be non-zero due to mirror padding"
        );
    }
}