Skip to main content

astroimage/analysis/
mod.rs

1/// Image analysis: FWHM, eccentricity, SNR, PSF signal.
2
3#[cfg(feature = "debug-pipeline")]
4pub mod background;
5#[cfg(not(feature = "debug-pipeline"))]
6mod background;
7
8#[cfg(feature = "debug-pipeline")]
9pub mod convolution;
10#[cfg(not(feature = "debug-pipeline"))]
11mod convolution;
12
13#[cfg(feature = "debug-pipeline")]
14pub mod detection;
15#[cfg(not(feature = "debug-pipeline"))]
16mod detection;
17
18#[cfg(feature = "debug-pipeline")]
19pub mod fitting;
20#[cfg(not(feature = "debug-pipeline"))]
21mod fitting;
22
23#[cfg(feature = "debug-pipeline")]
24pub mod metrics;
25#[cfg(not(feature = "debug-pipeline"))]
26mod metrics;
27
28#[cfg(feature = "debug-pipeline")]
29pub mod snr;
30#[cfg(not(feature = "debug-pipeline"))]
31mod snr;
32
33#[cfg(feature = "debug-pipeline")]
34pub mod render;
35
36use std::path::Path;
37use std::sync::Arc;
38
39use anyhow::{Context, Result};
40
41use crate::formats;
42use crate::processing::color::u16_to_f32;
43use crate::processing::debayer;
44use crate::processing::stretch::find_median;
45use crate::types::{BayerPattern, ImageMetadata, PixelData};
46
47use detection::DetectionParams;
48
49/// Method used to measure this star's PSF.
50#[derive(Debug, Clone, Copy, PartialEq)]
51pub enum FitMethod {
52    /// Free-beta Moffat (8 params) — highest accuracy.
53    FreeMoffat,
54    /// Fixed-beta Moffat (7 params) — field median beta.
55    FixedMoffat,
56    /// Gaussian fallback (7 params).
57    Gaussian,
58    /// Windowed moments — lowest accuracy, flagged unreliable.
59    Moments,
60}
61
62/// Quantitative metrics for a single detected star.
63pub struct StarMetrics {
64    /// Subpixel centroid X.
65    pub x: f32,
66    /// Subpixel centroid Y.
67    pub y: f32,
68    /// Background-subtracted peak value (ADU).
69    pub peak: f32,
70    /// Total background-subtracted flux (ADU).
71    pub flux: f32,
72    /// FWHM along major axis (pixels).
73    pub fwhm_x: f32,
74    /// FWHM along minor axis (pixels).
75    pub fwhm_y: f32,
76    /// Geometric mean FWHM (pixels).
77    pub fwhm: f32,
78    /// Eccentricity: 0 = round, approaching 1 = elongated.
79    pub eccentricity: f32,
80    /// Per-star aperture photometry SNR.
81    pub snr: f32,
82    /// Half-flux radius (pixels).
83    pub hfr: f32,
84    /// PSF position angle in radians, counter-clockwise from +X axis.
85    /// Orientation of the major axis (fwhm_x direction).
86    /// 0.0 when Gaussian fit is disabled and star is nearly round.
87    pub theta: f32,
88    /// Moffat β parameter (None if Gaussian/moments fit was used).
89    pub beta: Option<f32>,
90    /// Which PSF fitting method produced this measurement.
91    pub fit_method: FitMethod,
92}
93
94/// Full analysis result for an image.
95pub struct AnalysisResult {
96    /// Image width (after debayer if applicable).
97    pub width: usize,
98    /// Image height (after debayer if applicable).
99    pub height: usize,
100    /// Number of source channels: 1 = mono, 3 = color (after debayer).
101    pub source_channels: usize,
102    /// Global background level (ADU).
103    pub background: f32,
104    /// Background noise sigma (ADU).
105    pub noise: f32,
106    /// Actual detection threshold used (ADU above background).
107    pub detection_threshold: f32,
108    /// Total stars with valid PSF measurements (statistics computed from all).
109    pub stars_detected: usize,
110    /// Per-star metrics, sorted by flux descending, capped at max_stars.
111    pub stars: Vec<StarMetrics>,
112    /// Median FWHM across all measured stars (pixels).
113    pub median_fwhm: f32,
114    /// Median eccentricity across all measured stars.
115    pub median_eccentricity: f32,
116    /// Median per-star SNR.
117    pub median_snr: f32,
118    /// Median half-flux radius (pixels).
119    pub median_hfr: f32,
120    /// SNR weight for frame ranking: (MeanDev / noise)².
121    pub snr_weight: f32,
122    /// PSF signal: median(star_peaks) / noise.
123    pub psf_signal: f32,
124    /// Rayleigh R² statistic for directional coherence of star position angles.
125    /// 0.0 = uniform (no trail), 1.0 = all stars aligned (strong trail).
126    /// Computed from detection-stage stamp moments on 2θ.
127    pub trail_r_squared: f32,
128    /// True if the image is likely trailed, based on the Rayleigh test.
129    /// Uses configurable R² threshold (default 0.5) and eccentricity gate.
130    pub possibly_trailed: bool,
131    /// Measured FWHM from adaptive two-pass detection (pixels).
132    /// This is the FWHM used for the final matched filter kernel.
133    /// If the first-pass FWHM was within 30% of 3.0, this equals 3.0.
134    pub measured_fwhm_kernel: f32,
135    /// Median Moffat β across all stars (None if Moffat fitting not used).
136    /// Typical range: 2.0-5.0 for real optics. Lower = broader wings.
137    pub median_beta: Option<f32>,
138}
139
140/// Builder configuration for analysis (internal).
141pub struct AnalysisConfig {
142    detection_sigma: f32,
143    min_star_area: usize,
144    max_star_area: usize,
145    saturation_fraction: f32,
146    max_stars: usize,
147    apply_debayer: bool,
148    trail_r_squared_threshold: f32,
149    /// MRS wavelet noise layers (default 1).
150    noise_layers: usize,
151}
152
153/// Image analyzer with builder pattern.
154pub struct ImageAnalyzer {
155    config: AnalysisConfig,
156    thread_pool: Option<Arc<rayon::ThreadPool>>,
157}
158
159impl ImageAnalyzer {
160    pub fn new() -> Self {
161        ImageAnalyzer {
162            config: AnalysisConfig {
163                detection_sigma: 5.0,
164                min_star_area: 5,
165                max_star_area: 2000,
166                saturation_fraction: 0.95,
167                max_stars: 200,
168                apply_debayer: true,
169                trail_r_squared_threshold: 0.5,
170                noise_layers: 1,
171            },
172            thread_pool: None,
173        }
174    }
175
176    /// Star detection threshold in σ above background.
177    pub fn with_detection_sigma(mut self, sigma: f32) -> Self {
178        self.config.detection_sigma = sigma.max(1.0);
179        self
180    }
181
182    /// Reject connected components with fewer pixels than this (filters hot pixels).
183    pub fn with_min_star_area(mut self, area: usize) -> Self {
184        self.config.min_star_area = area.max(1);
185        self
186    }
187
188    /// Reject connected components with more pixels than this (filters galaxies/nebulae).
189    pub fn with_max_star_area(mut self, area: usize) -> Self {
190        self.config.max_star_area = area;
191        self
192    }
193
194    /// Reject stars with peak > fraction × 65535 (saturated).
195    pub fn with_saturation_fraction(mut self, frac: f32) -> Self {
196        self.config.saturation_fraction = frac.clamp(0.5, 1.0);
197        self
198    }
199
200    /// Keep only the brightest N stars in the returned result.
201    pub fn with_max_stars(mut self, n: usize) -> Self {
202        self.config.max_stars = n.max(1);
203        self
204    }
205
206    /// Skip debayering for OSC images (less accurate but faster).
207    pub fn without_debayer(mut self) -> Self {
208        self.config.apply_debayer = false;
209        self
210    }
211
212    /// Set the R² threshold for trail detection.
213    /// Images with Rayleigh R² above this are flagged as possibly trailed.
214    /// Default: 0.5. Lower values are more aggressive (more false positives).
215    pub fn with_trail_threshold(mut self, threshold: f32) -> Self {
216        self.config.trail_r_squared_threshold = threshold.clamp(0.0, 1.0);
217        self
218    }
219
220    /// Set MRS wavelet noise layers.
221    /// Uses à trous B3-spline wavelet to isolate noise from nebulosity/gradients.
222    /// Default: 1.
223    pub fn with_mrs_layers(mut self, layers: usize) -> Self {
224        self.config.noise_layers = layers.max(1);
225        self
226    }
227
228    /// Use a custom rayon thread pool.
229    pub fn with_thread_pool(mut self, pool: Arc<rayon::ThreadPool>) -> Self {
230        self.thread_pool = Some(pool);
231        self
232    }
233
234    /// Analyze a FITS or XISF image file.
235    pub fn analyze<P: AsRef<Path>>(&self, path: P) -> Result<AnalysisResult> {
236        let path = path.as_ref();
237        match &self.thread_pool {
238            Some(pool) => pool.install(|| self.analyze_impl(path)),
239            None => self.analyze_impl(path),
240        }
241    }
242
243    /// Analyze pre-loaded f32 pixel data.
244    ///
245    /// `data`: planar f32 pixel data (for 3-channel: RRRGGGBBB layout).
246    /// `width`: image width.
247    /// `height`: image height.
248    /// `channels`: 1 for mono, 3 for RGB.
249    pub fn analyze_data(
250        &self,
251        data: &[f32],
252        width: usize,
253        height: usize,
254        channels: usize,
255    ) -> Result<AnalysisResult> {
256        match &self.thread_pool {
257            Some(pool) => pool.install(|| {
258                self.run_analysis(data, width, height, channels, None)
259            }),
260            None => self.run_analysis(data, width, height, channels, None),
261        }
262    }
263
264    /// Analyze pre-read raw pixel data (skips file I/O).
265    ///
266    /// Accepts `ImageMetadata` and borrows `PixelData`, handling u16→f32
267    /// conversion and green-channel interpolation for OSC images internally.
268    pub fn analyze_raw(
269        &self,
270        meta: &ImageMetadata,
271        pixels: &PixelData,
272    ) -> Result<AnalysisResult> {
273        match &self.thread_pool {
274            Some(pool) => pool.install(|| self.analyze_raw_impl(meta, pixels)),
275            None => self.analyze_raw_impl(meta, pixels),
276        }
277    }
278
279    fn analyze_raw_impl(
280        &self,
281        meta: &ImageMetadata,
282        pixels: &PixelData,
283    ) -> Result<AnalysisResult> {
284        let f32_data = match pixels {
285            PixelData::Float32(d) => std::borrow::Cow::Borrowed(d.as_slice()),
286            PixelData::Uint16(d) => std::borrow::Cow::Owned(u16_to_f32(d)),
287        };
288
289        let mut data = f32_data.into_owned();
290        let width = meta.width;
291        let height = meta.height;
292        let channels = meta.channels;
293
294        let green_mask = if self.config.apply_debayer
295            && meta.bayer_pattern != BayerPattern::None
296            && channels == 1
297        {
298            data = debayer::interpolate_green_f32(&data, width, height, meta.bayer_pattern);
299            Some(build_green_mask(width, height, meta.bayer_pattern))
300        } else {
301            None
302        };
303
304        self.run_analysis(&data, width, height, channels, green_mask.as_deref())
305    }
306
307    fn analyze_impl(&self, path: &Path) -> Result<AnalysisResult> {
308        let (meta, pixel_data) =
309            formats::read_image(path).context("Failed to read image for analysis")?;
310
311        // Convert to f32
312        let f32_data = match pixel_data {
313            PixelData::Float32(d) => d,
314            PixelData::Uint16(d) => u16_to_f32(&d),
315        };
316
317        let mut data = f32_data;
318        let width = meta.width;
319        let height = meta.height;
320        let channels = meta.channels;
321
322        // Green-channel interpolation for OSC: native-resolution mono green image
323        // (matches Siril's interpolate_nongreen — no PSF broadening from 2×2 binning)
324        let green_mask = if self.config.apply_debayer
325            && meta.bayer_pattern != BayerPattern::None
326            && channels == 1
327        {
328            data = debayer::interpolate_green_f32(&data, width, height, meta.bayer_pattern);
329            // width, height, channels unchanged — native resolution mono green
330            Some(build_green_mask(width, height, meta.bayer_pattern))
331        } else {
332            None
333        };
334
335        self.run_analysis(&data, width, height, channels, green_mask.as_deref())
336    }
337
338    fn run_analysis(
339        &self,
340        data: &[f32],
341        width: usize,
342        height: usize,
343        channels: usize,
344        green_mask: Option<&[bool]>,
345    ) -> Result<AnalysisResult> {
346        // Extract luminance if multi-channel
347        let lum = if channels == 3 {
348            extract_luminance(data, width, height)
349        } else {
350            data[..width * height].to_vec()
351        };
352
353        let det_params = DetectionParams {
354            detection_sigma: self.config.detection_sigma,
355            min_star_area: self.config.min_star_area,
356            max_star_area: self.config.max_star_area,
357            saturation_limit: self.config.saturation_fraction * 65535.0,
358        };
359
360        // ── Stage 1: Background & Noise ──────────────────────────────────
361        let cell_size = background::auto_cell_size(width, height);
362        let mut bg_result = background::estimate_background_mesh(&lum, width, height, cell_size);
363        bg_result.noise = background::estimate_noise_mrs(
364            &lum, width, height, self.config.noise_layers.max(1),
365        ).max(0.001);
366
367        // ── Stage 2, Pass 1: Discovery ───────────────────────────────────
368        let initial_fwhm = 3.0_f32;
369        let pass1_stars = {
370            let bg_map = bg_result.background_map.as_deref();
371            let noise_map = bg_result.noise_map.as_deref();
372            detection::detect_stars(
373                &lum, width, height,
374                bg_result.background, bg_result.noise,
375                bg_map, noise_map, &det_params, initial_fwhm,
376            )
377        };
378
379        // Select calibration stars: brightest, not saturated, not too elongated
380        let calibration_stars: Vec<&detection::DetectedStar> = pass1_stars
381            .iter()
382            .filter(|s| s.eccentricity < 0.5 && s.area >= 5)
383            .take(100)
384            .collect();
385
386        // Free-beta Moffat on calibration stars to discover field PSF model
387        let field_beta: Option<f64>;
388        let field_fwhm: f32;
389        if calibration_stars.len() >= 3 {
390            let cal_owned: Vec<detection::DetectedStar> = calibration_stars
391                .iter()
392                .map(|s| detection::DetectedStar {
393                    x: s.x, y: s.y, peak: s.peak, flux: s.flux,
394                    area: s.area, theta: s.theta, eccentricity: s.eccentricity,
395                })
396                .collect();
397            let cal_measured = metrics::measure_stars(
398                &lum, width, height, &cal_owned,
399                bg_result.background,
400                bg_result.background_map.as_deref(),
401                green_mask,
402                None, // free-beta Moffat
403            );
404
405            let mut beta_vals: Vec<f32> = cal_measured.iter().filter_map(|s| s.beta).collect();
406            let mut fwhm_vals: Vec<f32> = cal_measured.iter().map(|s| s.fwhm).collect();
407
408            if beta_vals.len() >= 3 {
409                field_beta = Some(sigma_clipped_median(&beta_vals) as f64);
410            } else if !beta_vals.is_empty() {
411                field_beta = Some(find_median(&mut beta_vals) as f64);
412            } else {
413                field_beta = None;
414            }
415
416            if fwhm_vals.len() >= 3 {
417                field_fwhm = sigma_clipped_median(&fwhm_vals);
418            } else if !fwhm_vals.is_empty() {
419                field_fwhm = find_median(&mut fwhm_vals);
420            } else {
421                field_fwhm = estimate_fwhm_from_stars(
422                    &lum, width, height, &pass1_stars,
423                    bg_result.background, bg_result.background_map.as_deref(),
424                );
425            }
426        } else {
427            // Too few calibration stars — fall back to halfmax estimate
428            field_beta = None;
429            field_fwhm = estimate_fwhm_from_stars(
430                &lum, width, height, &pass1_stars,
431                bg_result.background, bg_result.background_map.as_deref(),
432            );
433        }
434
435        // Source-mask background re-estimation
436        let mask_fwhm = if field_fwhm > 1.0 { field_fwhm } else { initial_fwhm };
437        if !pass1_stars.is_empty() {
438            let mask_radius = (2.5 * mask_fwhm).ceil() as i32;
439            let mask_r_sq = (mask_radius * mask_radius) as f32;
440            let mut source_mask = vec![false; width * height];
441            for star in &pass1_stars {
442                let cx = star.x.round() as i32;
443                let cy = star.y.round() as i32;
444                for dy in -mask_radius..=mask_radius {
445                    let py = cy + dy;
446                    if py < 0 || py >= height as i32 { continue; }
447                    for dx in -mask_radius..=mask_radius {
448                        let px = cx + dx;
449                        if px < 0 || px >= width as i32 { continue; }
450                        if (dx * dx + dy * dy) as f32 <= mask_r_sq {
451                            source_mask[py as usize * width + px as usize] = true;
452                        }
453                    }
454                }
455            }
456            bg_result = background::estimate_background_mesh_masked(
457                &lum, width, height, cell_size, &source_mask,
458            );
459            bg_result.noise = background::estimate_noise_mrs(
460                &lum, width, height, self.config.noise_layers.max(1),
461            ).max(0.001);
462        }
463
464        // ── Stage 2, Pass 2: Full detection with refined kernel ──────────
465        let final_fwhm = if field_fwhm > 1.0
466            && ((field_fwhm - initial_fwhm) / initial_fwhm).abs() > 0.30
467        {
468            field_fwhm.min(initial_fwhm * 2.0)
469        } else {
470            initial_fwhm
471        };
472
473        let detected = {
474            let bg_map = bg_result.background_map.as_deref();
475            let noise_map = bg_result.noise_map.as_deref();
476            detection::detect_stars(
477                &lum, width, height,
478                bg_result.background, bg_result.noise,
479                bg_map, noise_map, &det_params, final_fwhm,
480            )
481        };
482
483        let bg_map_ref = bg_result.background_map.as_deref();
484        let detection_threshold = self.config.detection_sigma * bg_result.noise;
485
486        // ── Trail detection (Rayleigh test on detection-stage moments) ────
487        let (trail_r_squared, possibly_trailed) = if detected.len() >= 5 {
488            let n = detected.len();
489            let (sum_cos, sum_sin) =
490                detected.iter().fold((0.0f64, 0.0f64), |(sc, ss), s| {
491                    let a = 2.0 * s.theta as f64;
492                    (sc + a.cos(), ss + a.sin())
493                });
494            let r_sq = (sum_cos * sum_cos + sum_sin * sum_sin) / (n as f64 * n as f64);
495            let p = (-(n as f64) * r_sq).exp();
496            let mut eccs: Vec<f32> = detected.iter().map(|s| s.eccentricity).collect();
497            eccs.sort_unstable_by(|a, b| a.total_cmp(b));
498            let median_ecc = eccs[eccs.len() / 2];
499            let threshold = self.config.trail_r_squared_threshold as f64;
500            let trailed = (r_sq > threshold && p < 0.01)
501                || (median_ecc > 0.6 && p < 0.05);
502            (r_sq as f32, trailed)
503        } else {
504            (0.0, false)
505        };
506
507        let snr_weight = snr::compute_snr_weight(&lum, bg_result.background, bg_result.noise);
508
509        let make_zero_result = |stars_detected: usize| {
510            Ok(AnalysisResult {
511                width, height, source_channels: channels,
512                background: bg_result.background, noise: bg_result.noise,
513                detection_threshold, stars_detected,
514                stars: Vec::new(),
515                median_fwhm: 0.0, median_eccentricity: 0.0,
516                median_snr: 0.0, median_hfr: 0.0,
517                snr_weight, psf_signal: 0.0,
518                trail_r_squared, possibly_trailed,
519                measured_fwhm_kernel: final_fwhm,
520                median_beta: field_beta.map(|b| b as f32),
521            })
522        };
523
524        if detected.is_empty() {
525            return make_zero_result(0);
526        }
527
528        // ── Stage 3: Fixed-beta Moffat on all stars ──────────────────────
529        let mut measured = metrics::measure_stars(
530            &lum, width, height, &detected,
531            bg_result.background, bg_map_ref,
532            green_mask, field_beta,
533        );
534
535        if measured.is_empty() {
536            return make_zero_result(0);
537        }
538
539        // ── Stage 4: Metrics ─────────────────────────────────────────────
540        let stars_detected = measured.len();
541
542        let fwhm_vals: Vec<f32> = measured.iter().map(|s| s.fwhm).collect();
543        let median_fwhm = sigma_clipped_median(&fwhm_vals);
544
545        snr::compute_star_snr(&lum, width, height, &mut measured, median_fwhm);
546
547        let ecc_vals: Vec<f32> = measured.iter().map(|s| s.eccentricity).collect();
548        let mut snr_vals: Vec<f32> = measured.iter().map(|s| s.snr).collect();
549        let hfr_vals: Vec<f32> = measured.iter().map(|s| s.hfr).collect();
550
551        let median_eccentricity = sigma_clipped_median(&ecc_vals);
552        let median_snr = find_median(&mut snr_vals);
553        let median_hfr = sigma_clipped_median(&hfr_vals);
554        let psf_signal = snr::compute_psf_signal(&measured, bg_result.noise);
555
556        // Median beta: use field_beta from calibration, or compute from all stars
557        let median_beta = if let Some(fb) = field_beta {
558            Some(fb as f32)
559        } else {
560            let mut beta_vals: Vec<f32> = measured.iter().filter_map(|s| s.beta).collect();
561            if beta_vals.is_empty() { None } else { Some(find_median(&mut beta_vals)) }
562        };
563
564        // Late cap: truncate to max_stars AFTER all statistics are computed
565        measured.truncate(self.config.max_stars);
566
567        let stars: Vec<StarMetrics> = measured
568            .into_iter()
569            .map(|m| StarMetrics {
570                x: m.x, y: m.y, peak: m.peak, flux: m.flux,
571                fwhm_x: m.fwhm_x, fwhm_y: m.fwhm_y, fwhm: m.fwhm,
572                eccentricity: m.eccentricity, snr: m.snr, hfr: m.hfr,
573                theta: m.theta, beta: m.beta, fit_method: m.fit_method,
574            })
575            .collect();
576
577        Ok(AnalysisResult {
578            width, height, source_channels: channels,
579            background: bg_result.background, noise: bg_result.noise,
580            detection_threshold, stars_detected, stars,
581            median_fwhm, median_eccentricity, median_snr, median_hfr,
582            snr_weight, psf_signal,
583            trail_r_squared, possibly_trailed,
584            measured_fwhm_kernel: final_fwhm,
585            median_beta,
586        })
587    }
588}
589
590impl Default for ImageAnalyzer {
591    fn default() -> Self {
592        Self::new()
593    }
594}
595
596/// Sigma-clipped median: 2-iteration, 3σ MAD-based clipping.
597///
598/// Standard in SExtractor/DAOPHOT for robust statistics:
599///   MAD = median(|x_i − median|)
600///   σ_MAD = 1.4826 × MAD
601///   reject: |x − median| > 3 × σ_MAD
602///
603/// Returns plain median if fewer than 3 values remain after clipping.
604pub fn sigma_clipped_median(values: &[f32]) -> f32 {
605    if values.is_empty() {
606        return 0.0;
607    }
608    let mut v: Vec<f32> = values.to_vec();
609    for _ in 0..2 {
610        if v.len() < 3 {
611            break;
612        }
613        let med = find_median(&mut v);
614        let mut abs_devs: Vec<f32> = v.iter().map(|&x| (x - med).abs()).collect();
615        let mad = find_median(&mut abs_devs);
616        let sigma_mad = 1.4826 * mad;
617        if sigma_mad < 1e-10 {
618            break; // all values identical
619        }
620        let clip = 3.0 * sigma_mad;
621        v.retain(|&x| (x - med).abs() <= clip);
622    }
623    if v.is_empty() {
624        return find_median(&mut values.to_vec());
625    }
626    find_median(&mut v)
627}
628
629/// Estimate FWHM from the brightest detected stars by extracting stamps
630/// and using `estimate_sigma_halfmax`. Returns median FWHM, or 0.0 if
631/// fewer than 3 stars yield valid measurements.
632pub fn estimate_fwhm_from_stars(
633    lum: &[f32],
634    width: usize,
635    height: usize,
636    stars: &[detection::DetectedStar],
637    background: f32,
638    bg_map: Option<&[f32]>,
639) -> f32 {
640    // Scan top 50 brightest (already sorted by flux descending), select up to 20
641    // with low eccentricity (≤ 0.7) to avoid elongated non-stellar objects
642    // poisoning the kernel estimate.
643    let scan_n = stars.len().min(50);
644    if scan_n < 3 {
645        return 0.0;
646    }
647
648    let round_stars: Vec<&detection::DetectedStar> = stars[..scan_n]
649        .iter()
650        .filter(|s| s.eccentricity <= 0.7)
651        .take(20)
652        .collect();
653    if round_stars.len() < 3 {
654        return 0.0;
655    }
656
657    let mut fwhm_vals = Vec::with_capacity(round_stars.len());
658    for star in &round_stars {
659        let stamp_radius = 12_usize; // enough for FWHM up to ~10px
660        let cx = star.x.round() as i32;
661        let cy = star.y.round() as i32;
662        let sr = stamp_radius as i32;
663        if cx - sr <= 0 || cy - sr <= 0
664            || cx + sr >= width as i32 - 1
665            || cy + sr >= height as i32 - 1
666        {
667            continue;
668        }
669        let x0 = (cx - sr) as usize;
670        let y0 = (cy - sr) as usize;
671        let x1 = (cx + sr) as usize;
672        let y1 = (cy + sr) as usize;
673        let stamp_w = x1 - x0 + 1;
674        let mut stamp = Vec::with_capacity(stamp_w * (y1 - y0 + 1));
675        for sy in y0..=y1 {
676            for sx in x0..=x1 {
677                let bg = bg_map.map_or(background, |m| m[sy * width + sx]);
678                stamp.push(lum[sy * width + sx] - bg);
679            }
680        }
681        let rel_cx = star.x - x0 as f32;
682        let rel_cy = star.y - y0 as f32;
683        let sigma = metrics::estimate_sigma_halfmax(&stamp, stamp_w, rel_cx, rel_cy);
684        let fwhm = sigma * 2.3548;
685        if fwhm > 1.0 && fwhm < 20.0 {
686            fwhm_vals.push(fwhm);
687        }
688    }
689
690    if fwhm_vals.len() < 3 {
691        return 0.0;
692    }
693    find_median(&mut fwhm_vals)
694}
695
696/// Build a boolean mask marking green CFA pixel positions.
697///
698/// Returns a `Vec<bool>` of length `width * height` where `true` marks pixels
699/// that are at green positions in the Bayer pattern. For GBRG/GRBG green is
700/// at (row + col) even; for RGGB/BGGR green is at (row + col) odd.
701fn build_green_mask(width: usize, height: usize, pattern: BayerPattern) -> Vec<bool> {
702    let green_at_even = matches!(pattern, BayerPattern::Gbrg | BayerPattern::Grbg);
703    (0..height)
704        .flat_map(|y| {
705            (0..width).map(move |x| {
706                let parity = (x + y) & 1;
707                if green_at_even { parity == 0 } else { parity == 1 }
708            })
709        })
710        .collect()
711}
712
713/// Extract luminance from planar RGB data: L = 0.2126R + 0.7152G + 0.0722B
714pub fn extract_luminance(data: &[f32], width: usize, height: usize) -> Vec<f32> {
715    use rayon::prelude::*;
716
717    let plane_size = width * height;
718    let r = &data[..plane_size];
719    let g = &data[plane_size..2 * plane_size];
720    let b = &data[2 * plane_size..3 * plane_size];
721
722    let mut lum = vec![0.0_f32; plane_size];
723    const CHUNK: usize = 8192;
724    lum.par_chunks_mut(CHUNK)
725        .enumerate()
726        .for_each(|(ci, chunk)| {
727            let off = ci * CHUNK;
728            for (i, dst) in chunk.iter_mut().enumerate() {
729                let idx = off + i;
730                *dst = 0.2126 * r[idx] + 0.7152 * g[idx] + 0.0722 * b[idx];
731            }
732        });
733    lum
734}
735
736/// Prepare luminance data from raw metadata + pixels.
737///
738/// Handles u16→f32 conversion, green-channel interpolation for OSC,
739/// and luminance extraction for multi-channel images.
740/// Returns `(luminance, width, height, channels, green_mask)`.
741#[cfg(feature = "debug-pipeline")]
742pub fn prepare_luminance(
743    meta: &crate::types::ImageMetadata,
744    pixels: &crate::types::PixelData,
745    apply_debayer: bool,
746) -> (Vec<f32>, usize, usize, usize, Option<Vec<bool>>) {
747    use crate::processing::color::u16_to_f32;
748    use crate::processing::debayer;
749
750    let f32_data = match pixels {
751        PixelData::Float32(d) => std::borrow::Cow::Borrowed(d.as_slice()),
752        PixelData::Uint16(d) => std::borrow::Cow::Owned(u16_to_f32(d)),
753    };
754
755    let mut data = f32_data.into_owned();
756    let width = meta.width;
757    let height = meta.height;
758    let channels = meta.channels;
759
760    let green_mask = if apply_debayer
761        && meta.bayer_pattern != BayerPattern::None
762        && channels == 1
763    {
764        data = debayer::interpolate_green_f32(&data, width, height, meta.bayer_pattern);
765        Some(build_green_mask(width, height, meta.bayer_pattern))
766    } else {
767        None
768    };
769
770    let lum = if channels == 3 {
771        extract_luminance(&data, width, height)
772    } else {
773        data[..width * height].to_vec()
774    };
775
776    (lum, width, height, channels, green_mask)
777}