ad-plugins-rs 0.8.0

NDPlugin implementations for areaDetector-rs
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
use std::sync::Arc;

use ad_core_rs::ndarray::{NDArray, NDDataBuffer, NDDataType, NDDimension};
use ad_core_rs::ndarray_pool::NDArrayPool;
use ad_core_rs::plugin::runtime::{
    NDPluginProcess, ParamUpdate, PluginParamSnapshot, ProcessResult,
};
use asyn_rs::param::ParamType;
use asyn_rs::port::PortDriverBase;

/// Per-dimension ROI configuration.
#[derive(Debug, Clone)]
pub struct ROIDimConfig {
    pub min: usize,
    pub size: usize,
    pub bin: usize,
    pub reverse: bool,
    pub enable: bool,
    /// If true, size is computed as src_dim - min.
    pub auto_size: bool,
}

impl Default for ROIDimConfig {
    fn default() -> Self {
        Self {
            min: 0,
            size: 0,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        }
    }
}

/// Auto-centering mode for ROI.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AutoCenter {
    None,
    CenterOfMass,
    PeakPosition,
}

/// ROI plugin configuration.
#[derive(Debug, Clone)]
pub struct ROIConfig {
    pub dims: [ROIDimConfig; 3],
    pub data_type: Option<NDDataType>,
    pub enable_scale: bool,
    pub scale: f64,
    pub collapse_dims: bool,
    pub autocenter: AutoCenter,
}

impl Default for ROIConfig {
    fn default() -> Self {
        Self {
            dims: [
                ROIDimConfig::default(),
                ROIDimConfig::default(),
                ROIDimConfig::default(),
            ],
            data_type: None,
            enable_scale: false,
            scale: 1.0,
            collapse_dims: false,
            autocenter: AutoCenter::None,
        }
    }
}

/// Compute the centroid (center of mass) of a 2D image.
fn find_centroid_2d(data: &NDDataBuffer, x_size: usize, y_size: usize) -> (usize, usize) {
    let mut cx = 0.0f64;
    let mut cy = 0.0f64;
    let mut total = 0.0f64;
    for iy in 0..y_size {
        for ix in 0..x_size {
            let val = data.get_as_f64(iy * x_size + ix).unwrap_or(0.0);
            total += val;
            cx += val * ix as f64;
            cy += val * iy as f64;
        }
    }
    if total > 0.0 {
        ((cx / total) as usize, (cy / total) as usize)
    } else {
        (x_size / 2, y_size / 2)
    }
}

/// Find the position of the maximum value in a 2D image.
fn find_peak_2d(data: &NDDataBuffer, x_size: usize, y_size: usize) -> (usize, usize) {
    let mut max_val = f64::NEG_INFINITY;
    let mut max_x = 0;
    let mut max_y = 0;
    for iy in 0..y_size {
        for ix in 0..x_size {
            let val = data.get_as_f64(iy * x_size + ix).unwrap_or(0.0);
            if val > max_val {
                max_val = val;
                max_x = ix;
                max_y = iy;
            }
        }
    }
    (max_x, max_y)
}

/// Extract ROI sub-region from a 2D array.
pub fn extract_roi_2d(src: &NDArray, config: &ROIConfig) -> Option<NDArray> {
    if src.dims.len() < 2 {
        return None;
    }

    let src_x = src.dims[0].size;
    let src_y = src.dims[1].size;

    // Resolve effective min/size for X dimension
    let (eff_x_min, eff_x_size) = if !config.dims[0].enable {
        (0, src_x)
    } else if config.dims[0].auto_size {
        let min = config.dims[0].min.min(src_x);
        (min, src_x.saturating_sub(min))
    } else {
        let min = config.dims[0].min.min(src_x);
        let size = config.dims[0].size.min(src_x - min);
        (min, size)
    };

    // Resolve effective min/size for Y dimension
    let (eff_y_min, eff_y_size) = if !config.dims[1].enable {
        (0, src_y)
    } else if config.dims[1].auto_size {
        let min = config.dims[1].min.min(src_y);
        (min, src_y.saturating_sub(min))
    } else {
        let min = config.dims[1].min.min(src_y);
        let size = config.dims[1].size.min(src_y - min);
        (min, size)
    };

    // Apply autocenter: shift ROI min so that the ROI is centered on the
    // centroid or peak, keeping the effective size the same.
    let (roi_x_min, roi_y_min) = match config.autocenter {
        AutoCenter::None => (eff_x_min, eff_y_min),
        AutoCenter::CenterOfMass => {
            let (cx, cy) = find_centroid_2d(&src.data, src_x, src_y);
            let mx = cx
                .saturating_sub(eff_x_size / 2)
                .min(src_x.saturating_sub(eff_x_size));
            let my = cy
                .saturating_sub(eff_y_size / 2)
                .min(src_y.saturating_sub(eff_y_size));
            (mx, my)
        }
        AutoCenter::PeakPosition => {
            let (px, py) = find_peak_2d(&src.data, src_x, src_y);
            let mx = px
                .saturating_sub(eff_x_size / 2)
                .min(src_x.saturating_sub(eff_x_size));
            let my = py
                .saturating_sub(eff_y_size / 2)
                .min(src_y.saturating_sub(eff_y_size));
            (mx, my)
        }
    };

    let roi_x_size = eff_x_size;
    let roi_y_size = eff_y_size;

    if roi_x_size == 0 || roi_y_size == 0 {
        return None;
    }

    let bin_x = config.dims[0].bin.max(1);
    let bin_y = config.dims[1].bin.max(1);
    let out_x = roi_x_size / bin_x;
    let out_y = roi_y_size / bin_y;

    if out_x == 0 || out_y == 0 {
        return None;
    }

    macro_rules! extract {
        ($vec:expr, $T:ty, $zero:expr) => {{
            let mut out = vec![$zero; out_x * out_y];
            for oy in 0..out_y {
                for ox in 0..out_x {
                    let mut sum = 0.0f64;
                    let mut count = 0usize;
                    for by in 0..bin_y {
                        for bx in 0..bin_x {
                            let sx = roi_x_min + ox * bin_x + bx;
                            let sy = roi_y_min + oy * bin_y + by;
                            if sx < src_x && sy < src_y {
                                sum += $vec[sy * src_x + sx] as f64;
                                count += 1;
                            }
                        }
                    }
                    let val = if count > 0 { sum / count as f64 } else { 0.0 };
                    let idx = if config.dims[0].reverse {
                        out_x - 1 - ox
                    } else {
                        ox
                    } + if config.dims[1].reverse {
                        out_y - 1 - oy
                    } else {
                        oy
                    } * out_x;
                    let scaled = if config.enable_scale {
                        val * config.scale
                    } else {
                        val
                    };
                    out[idx] = scaled as $T;
                }
            }
            out
        }};
    }

    let out_data = match &src.data {
        NDDataBuffer::U8(v) => NDDataBuffer::U8(extract!(v, u8, 0)),
        NDDataBuffer::U16(v) => NDDataBuffer::U16(extract!(v, u16, 0)),
        NDDataBuffer::I8(v) => NDDataBuffer::I8(extract!(v, i8, 0)),
        NDDataBuffer::I16(v) => NDDataBuffer::I16(extract!(v, i16, 0)),
        NDDataBuffer::I32(v) => NDDataBuffer::I32(extract!(v, i32, 0)),
        NDDataBuffer::U32(v) => NDDataBuffer::U32(extract!(v, u32, 0)),
        NDDataBuffer::I64(v) => NDDataBuffer::I64(extract!(v, i64, 0)),
        NDDataBuffer::U64(v) => NDDataBuffer::U64(extract!(v, u64, 0)),
        NDDataBuffer::F32(v) => NDDataBuffer::F32(extract!(v, f32, 0.0)),
        NDDataBuffer::F64(v) => NDDataBuffer::F64(extract!(v, f64, 0.0)),
    };

    let out_dims = if config.collapse_dims && out_y == 1 {
        vec![NDDimension::new(out_x)]
    } else {
        vec![NDDimension::new(out_x), NDDimension::new(out_y)]
    };

    // Apply data type conversion if requested
    let target_type = config.data_type.unwrap_or(src.data.data_type());

    let mut arr = NDArray::new(out_dims, target_type);
    if target_type == src.data.data_type() {
        arr.data = out_data;
    } else {
        // Convert via color module
        let mut temp = NDArray::new(arr.dims.clone(), src.data.data_type());
        temp.data = out_data;
        if let Ok(converted) = ad_core_rs::color::convert_data_type(&temp, target_type) {
            arr.data = converted.data;
        } else {
            arr.data = out_data_fallback(&temp.data, target_type, temp.data.len());
        }
    }

    arr.unique_id = src.unique_id;
    arr.timestamp = src.timestamp;
    arr.attributes = src.attributes.clone();
    Some(arr)
}

fn out_data_fallback(_src: &NDDataBuffer, target: NDDataType, len: usize) -> NDDataBuffer {
    NDDataBuffer::zeros(target, len)
}

/// Per-dimension param reasons.
#[derive(Default, Clone, Copy)]
pub struct ROIDimParams {
    pub min: usize,
    pub size: usize,
    pub bin: usize,
    pub reverse: usize,
    pub enable: usize,
    pub auto_size: usize,
    pub max_size: usize,
}

/// Param reasons for all ROI params.
#[derive(Default)]
pub struct ROIParams {
    pub dims: [ROIDimParams; 3],
    pub enable_scale: usize,
    pub scale: usize,
    pub data_type: usize,
    pub collapse_dims: usize,
    pub name: usize,
}

/// Pure ROI processing logic.
pub struct ROIProcessor {
    config: ROIConfig,
    params: ROIParams,
}

impl ROIProcessor {
    pub fn new(config: ROIConfig) -> Self {
        Self {
            config,
            params: ROIParams::default(),
        }
    }

    /// Access the registered ROI param reasons.
    pub fn params(&self) -> &ROIParams {
        &self.params
    }
}

impl NDPluginProcess for ROIProcessor {
    fn process_array(&mut self, array: &NDArray, _pool: &NDArrayPool) -> ProcessResult {
        // Report input array dimensions as MaxSize params
        let mut updates = Vec::new();
        for (i, dim_params) in self.params.dims.iter().enumerate() {
            let dim_size = array.dims.get(i).map(|d| d.size as i32).unwrap_or(0);
            updates.push(ParamUpdate::int32(dim_params.max_size, dim_size));
        }

        match extract_roi_2d(array, &self.config) {
            Some(roi_arr) => ProcessResult {
                output_arrays: vec![Arc::new(roi_arr)],
                param_updates: updates,
                scatter_index: None,
            },
            None => ProcessResult::sink(updates),
        }
    }

    fn plugin_type(&self) -> &str {
        "NDPluginROI"
    }

    fn register_params(
        &mut self,
        base: &mut PortDriverBase,
    ) -> Result<(), asyn_rs::error::AsynError> {
        let dim_names = ["DIM0", "DIM1", "DIM2"];
        for (i, prefix) in dim_names.iter().enumerate() {
            self.params.dims[i].min =
                base.create_param(&format!("{prefix}_MIN"), ParamType::Int32)?;
            self.params.dims[i].size =
                base.create_param(&format!("{prefix}_SIZE"), ParamType::Int32)?;
            self.params.dims[i].bin =
                base.create_param(&format!("{prefix}_BIN"), ParamType::Int32)?;
            self.params.dims[i].reverse =
                base.create_param(&format!("{prefix}_REVERSE"), ParamType::Int32)?;
            self.params.dims[i].enable =
                base.create_param(&format!("{prefix}_ENABLE"), ParamType::Int32)?;
            self.params.dims[i].auto_size =
                base.create_param(&format!("{prefix}_AUTO_SIZE"), ParamType::Int32)?;
            self.params.dims[i].max_size =
                base.create_param(&format!("{prefix}_MAX_SIZE"), ParamType::Int32)?;

            // Set initial values from config
            base.set_int32_param(self.params.dims[i].min, 0, self.config.dims[i].min as i32)?;
            base.set_int32_param(self.params.dims[i].size, 0, self.config.dims[i].size as i32)?;
            base.set_int32_param(self.params.dims[i].bin, 0, self.config.dims[i].bin as i32)?;
            base.set_int32_param(
                self.params.dims[i].reverse,
                0,
                self.config.dims[i].reverse as i32,
            )?;
            base.set_int32_param(
                self.params.dims[i].enable,
                0,
                self.config.dims[i].enable as i32,
            )?;
            base.set_int32_param(
                self.params.dims[i].auto_size,
                0,
                self.config.dims[i].auto_size as i32,
            )?;
        }
        self.params.enable_scale = base.create_param("ENABLE_SCALE", ParamType::Int32)?;
        self.params.scale = base.create_param("SCALE_VALUE", ParamType::Float64)?;
        self.params.data_type = base.create_param("ROI_DATA_TYPE", ParamType::Int32)?;
        self.params.collapse_dims = base.create_param("COLLAPSE_DIMS", ParamType::Int32)?;
        self.params.name = base.create_param("NAME", ParamType::Octet)?;

        base.set_int32_param(self.params.enable_scale, 0, self.config.enable_scale as i32)?;
        base.set_float64_param(self.params.scale, 0, self.config.scale)?;
        base.set_int32_param(self.params.data_type, 0, -1)?; // -1 = Automatic
        base.set_int32_param(
            self.params.collapse_dims,
            0,
            self.config.collapse_dims as i32,
        )?;

        Ok(())
    }

    fn on_param_change(
        &mut self,
        reason: usize,
        snapshot: &PluginParamSnapshot,
    ) -> ad_core_rs::plugin::runtime::ParamChangeResult {
        let p = &self.params;
        for i in 0..3 {
            if reason == p.dims[i].min {
                self.config.dims[i].min = snapshot.value.as_i32().max(0) as usize;
                return ad_core_rs::plugin::runtime::ParamChangeResult::empty();
            }
            if reason == p.dims[i].size {
                self.config.dims[i].size = snapshot.value.as_i32().max(0) as usize;
                return ad_core_rs::plugin::runtime::ParamChangeResult::empty();
            }
            if reason == p.dims[i].bin {
                self.config.dims[i].bin = snapshot.value.as_i32().max(1) as usize;
                return ad_core_rs::plugin::runtime::ParamChangeResult::empty();
            }
            if reason == p.dims[i].reverse {
                self.config.dims[i].reverse = snapshot.value.as_i32() != 0;
                return ad_core_rs::plugin::runtime::ParamChangeResult::empty();
            }
            if reason == p.dims[i].enable {
                self.config.dims[i].enable = snapshot.value.as_i32() != 0;
                return ad_core_rs::plugin::runtime::ParamChangeResult::empty();
            }
            if reason == p.dims[i].auto_size {
                self.config.dims[i].auto_size = snapshot.value.as_i32() != 0;
                return ad_core_rs::plugin::runtime::ParamChangeResult::empty();
            }
        }
        if reason == p.enable_scale {
            self.config.enable_scale = snapshot.value.as_i32() != 0;
        } else if reason == p.scale {
            self.config.scale = snapshot.value.as_f64();
        } else if reason == p.data_type {
            let v = snapshot.value.as_i32();
            self.config.data_type = if v < 0 {
                None
            } else {
                NDDataType::from_ordinal(v as u8)
            };
        } else if reason == p.collapse_dims {
            self.config.collapse_dims = snapshot.value.as_i32() != 0;
        }
        ad_core_rs::plugin::runtime::ParamChangeResult::empty()
    }
}

/// Create an ROI plugin runtime, returning the handle and param reasons.
pub fn create_roi_runtime(
    port_name: &str,
    pool: Arc<NDArrayPool>,
    queue_size: usize,
    ndarray_port: &str,
    wiring: Arc<ad_core_rs::plugin::wiring::WiringRegistry>,
) -> (
    ad_core_rs::plugin::runtime::PluginRuntimeHandle,
    ROIParams,
    std::thread::JoinHandle<()>,
) {
    let processor = ROIProcessor::new(ROIConfig::default());
    let (handle, jh) = ad_core_rs::plugin::runtime::create_plugin_runtime(
        port_name,
        processor,
        pool,
        queue_size,
        ndarray_port,
        wiring,
    );
    // Recreate param layout on a scratch PortDriverBase to get matching reasons.
    let params = {
        let mut base =
            asyn_rs::port::PortDriverBase::new("_scratch_", 1, asyn_rs::port::PortFlags::default());
        let _ = ad_core_rs::params::ndarray_driver::NDArrayDriverParams::create(&mut base);
        let _ = ad_core_rs::plugin::params::PluginBaseParams::create(&mut base);
        let mut p = ROIParams::default();
        let dim_names = ["DIM0", "DIM1", "DIM2"];
        for (i, prefix) in dim_names.iter().enumerate() {
            p.dims[i].min = base
                .create_param(&format!("{prefix}_MIN"), asyn_rs::param::ParamType::Int32)
                .unwrap();
            p.dims[i].size = base
                .create_param(&format!("{prefix}_SIZE"), asyn_rs::param::ParamType::Int32)
                .unwrap();
            p.dims[i].bin = base
                .create_param(&format!("{prefix}_BIN"), asyn_rs::param::ParamType::Int32)
                .unwrap();
            p.dims[i].reverse = base
                .create_param(
                    &format!("{prefix}_REVERSE"),
                    asyn_rs::param::ParamType::Int32,
                )
                .unwrap();
            p.dims[i].enable = base
                .create_param(
                    &format!("{prefix}_ENABLE"),
                    asyn_rs::param::ParamType::Int32,
                )
                .unwrap();
            p.dims[i].auto_size = base
                .create_param(
                    &format!("{prefix}_AUTO_SIZE"),
                    asyn_rs::param::ParamType::Int32,
                )
                .unwrap();
            p.dims[i].max_size = base
                .create_param(
                    &format!("{prefix}_MAX_SIZE"),
                    asyn_rs::param::ParamType::Int32,
                )
                .unwrap();
        }
        p.enable_scale = base
            .create_param("ENABLE_SCALE", asyn_rs::param::ParamType::Int32)
            .unwrap();
        p.scale = base
            .create_param("SCALE_VALUE", asyn_rs::param::ParamType::Float64)
            .unwrap();
        p.data_type = base
            .create_param("ROI_DATA_TYPE", asyn_rs::param::ParamType::Int32)
            .unwrap();
        p.collapse_dims = base
            .create_param("COLLAPSE_DIMS", asyn_rs::param::ParamType::Int32)
            .unwrap();
        p.name = base
            .create_param("NAME", asyn_rs::param::ParamType::Octet)
            .unwrap();
        p
    };
    (handle, params, jh)
}

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

    fn make_4x4_u8() -> NDArray {
        let mut arr = NDArray::new(
            vec![NDDimension::new(4), NDDimension::new(4)],
            NDDataType::UInt8,
        );
        if let NDDataBuffer::U8(ref mut v) = arr.data {
            for i in 0..16 {
                v[i] = i as u8;
            }
        }
        arr
    }

    #[test]
    fn test_extract_sub_region() {
        let arr = make_4x4_u8();
        let mut config = ROIConfig::default();
        config.dims[0] = ROIDimConfig {
            min: 1,
            size: 2,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };
        config.dims[1] = ROIDimConfig {
            min: 1,
            size: 2,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };

        let roi = extract_roi_2d(&arr, &config).unwrap();
        assert_eq!(roi.dims[0].size, 2);
        assert_eq!(roi.dims[1].size, 2);
        if let NDDataBuffer::U8(ref v) = roi.data {
            // row 1, cols 1-2: [5,6], row 2, cols 1-2: [9,10]
            assert_eq!(v[0], 5);
            assert_eq!(v[1], 6);
            assert_eq!(v[2], 9);
            assert_eq!(v[3], 10);
        }
    }

    #[test]
    fn test_binning_2x2() {
        let arr = make_4x4_u8();
        let mut config = ROIConfig::default();
        config.dims[0] = ROIDimConfig {
            min: 0,
            size: 4,
            bin: 2,
            reverse: false,
            enable: true,
            auto_size: false,
        };
        config.dims[1] = ROIDimConfig {
            min: 0,
            size: 4,
            bin: 2,
            reverse: false,
            enable: true,
            auto_size: false,
        };

        let roi = extract_roi_2d(&arr, &config).unwrap();
        assert_eq!(roi.dims[0].size, 2);
        assert_eq!(roi.dims[1].size, 2);
        if let NDDataBuffer::U8(ref v) = roi.data {
            // top-left 2x2: (0+1+4+5)/4 = 2.5 → 2
            assert_eq!(v[0], 2);
        }
    }

    #[test]
    fn test_reverse() {
        let arr = make_4x4_u8();
        let mut config = ROIConfig::default();
        config.dims[0] = ROIDimConfig {
            min: 0,
            size: 4,
            bin: 1,
            reverse: true,
            enable: true,
            auto_size: false,
        };
        config.dims[1] = ROIDimConfig {
            min: 0,
            size: 1,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };

        let roi = extract_roi_2d(&arr, &config).unwrap();
        if let NDDataBuffer::U8(ref v) = roi.data {
            assert_eq!(v[0], 3);
            assert_eq!(v[1], 2);
            assert_eq!(v[2], 1);
            assert_eq!(v[3], 0);
        }
    }

    #[test]
    fn test_collapse_dims() {
        let arr = make_4x4_u8();
        let mut config = ROIConfig::default();
        config.dims[0] = ROIDimConfig {
            min: 0,
            size: 4,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };
        config.dims[1] = ROIDimConfig {
            min: 0,
            size: 1,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };
        config.collapse_dims = true;

        let roi = extract_roi_2d(&arr, &config).unwrap();
        assert_eq!(roi.dims.len(), 1);
        assert_eq!(roi.dims[0].size, 4);
    }

    #[test]
    fn test_scale() {
        let arr = make_4x4_u8();
        let mut config = ROIConfig::default();
        config.dims[0] = ROIDimConfig {
            min: 0,
            size: 2,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };
        config.dims[1] = ROIDimConfig {
            min: 0,
            size: 1,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };
        config.enable_scale = true;
        config.scale = 2.0;

        let roi = extract_roi_2d(&arr, &config).unwrap();
        if let NDDataBuffer::U8(ref v) = roi.data {
            assert_eq!(v[0], 0); // 0 * 2 = 0
            assert_eq!(v[1], 2); // 1 * 2 = 2
        }
    }

    #[test]
    fn test_type_convert() {
        let arr = make_4x4_u8();
        let mut config = ROIConfig::default();
        config.dims[0] = ROIDimConfig {
            min: 0,
            size: 2,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };
        config.dims[1] = ROIDimConfig {
            min: 0,
            size: 1,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };
        config.data_type = Some(NDDataType::UInt16);

        let roi = extract_roi_2d(&arr, &config).unwrap();
        assert_eq!(roi.data.data_type(), NDDataType::UInt16);
    }

    // --- New ROIProcessor tests ---

    #[test]
    fn test_roi_processor() {
        let mut config = ROIConfig::default();
        config.dims[0] = ROIDimConfig {
            min: 1,
            size: 2,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };
        config.dims[1] = ROIDimConfig {
            min: 1,
            size: 2,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };

        let mut proc = ROIProcessor::new(config);
        let pool = NDArrayPool::new(1_000_000);

        let arr = make_4x4_u8();
        let result = proc.process_array(&arr, &pool);
        assert_eq!(result.output_arrays.len(), 1);
        assert_eq!(result.output_arrays[0].dims[0].size, 2);
        assert_eq!(result.output_arrays[0].dims[1].size, 2);
    }

    // --- Auto-size / dim-disable / autocenter tests ---

    #[test]
    fn test_auto_size() {
        // 4x4 image, min_x=1 with auto_size => size_x = 4-1 = 3
        let arr = make_4x4_u8();
        let mut config = ROIConfig::default();
        config.dims[0] = ROIDimConfig {
            min: 1,
            size: 0,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: true,
        };
        config.dims[1] = ROIDimConfig {
            min: 0,
            size: 0,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: true,
        };

        let roi = extract_roi_2d(&arr, &config).unwrap();
        assert_eq!(roi.dims[0].size, 3); // 4 - 1 = 3
        assert_eq!(roi.dims[1].size, 4); // 4 - 0 = 4

        if let NDDataBuffer::U8(ref v) = roi.data {
            // First row (y=0): pixels at x=1,2,3 => values 1,2,3
            assert_eq!(v[0], 1);
            assert_eq!(v[1], 2);
            assert_eq!(v[2], 3);
        }
    }

    #[test]
    fn test_dim_disable() {
        // Disabled dim uses full range: min=0, size=src_dim
        let arr = make_4x4_u8();
        let mut config = ROIConfig::default();
        config.dims[0] = ROIDimConfig {
            min: 2,
            size: 1,
            bin: 1,
            reverse: false,
            enable: false,
            auto_size: false,
        };
        config.dims[1] = ROIDimConfig {
            min: 0,
            size: 4,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };

        let roi = extract_roi_2d(&arr, &config).unwrap();
        // X dim disabled, so full range: size=4
        assert_eq!(roi.dims[0].size, 4);
        assert_eq!(roi.dims[1].size, 4);
    }

    #[test]
    fn test_autocenter_peak() {
        // Create 8x8 image with a peak at (6, 5)
        let mut arr = NDArray::new(
            vec![NDDimension::new(8), NDDimension::new(8)],
            NDDataType::UInt8,
        );
        if let NDDataBuffer::U8(ref mut v) = arr.data {
            for i in 0..64 {
                v[i] = 1;
            }
            // Place peak at x=6, y=5
            v[5 * 8 + 6] = 255;
        }

        let mut config = ROIConfig::default();
        config.dims[0] = ROIDimConfig {
            min: 0,
            size: 4,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };
        config.dims[1] = ROIDimConfig {
            min: 0,
            size: 4,
            bin: 1,
            reverse: false,
            enable: true,
            auto_size: false,
        };
        config.autocenter = AutoCenter::PeakPosition;

        let roi = extract_roi_2d(&arr, &config).unwrap();
        assert_eq!(roi.dims[0].size, 4);
        assert_eq!(roi.dims[1].size, 4);

        // ROI should be centered on peak (6,5) with size 4x4
        // min_x = 6 - 4/2 = 4, clamped to min(4, 8-4)=4
        // min_y = 5 - 4/2 = 3, clamped to min(3, 8-4)=3
        // So ROI covers x=[4..8), y=[3..7) and the peak at (6,5) should be inside
        // In the ROI, the peak is at local (6-4, 5-3) = (2, 2)
        if let NDDataBuffer::U8(ref v) = roi.data {
            assert_eq!(v[2 * 4 + 2], 255); // peak at local (2,2)
        }
    }
}