extern crate ndarray;
extern crate ndarray_ndimage;

use ndarray::prelude::*;
use ndarray_ndimage::{gaussian_filter, BorderMode};

pub fn high_pass_gaussian_filter(input: Vec<f64>, sigma: f64) -> Vec<f64> {
    // 将输入 Vec<f64> 转换为 Array1<f64>
    let input_array = Array1::from_vec(input);
    // for i in 0..input_array.len(){
    //
    //     input_array[i]=input_array[i]*input_array[i]/( 65535f64);
    // }
    // return input_array.to_vec();



    // 高斯低通滤波
    let low_pass = gaussian_filter(&input_array, sigma, 0, BorderMode::Reflect, 3);
    // Modify the result: set values less than zero to zero
    println!("{:?}",low_pass);
      // 高通滤波：原始信号 - 低通滤波结果
    let mut addarry=&input_array - &low_pass;
    for i in 0..addarry.len(){
        if addarry[i] < 0.0 {
            addarry[i] = 0.0;
        }
    }

  
    let high_pass =&input_array+ &addarry;

    // 找到原始数据和锐化后数据的最大值
    let max_original = input_array.iter().cloned().fold(f64::MIN, f64::max);
    let max_sharpened = high_pass.iter().cloned().fold(f64::MIN, f64::max);

    // 计算系数
    let coefficient = max_original / max_sharpened;

    // 应用系数并将输出 Array1<f64> 转换回 Vec<f64>
    high_pass.map(|&x| x * coefficient).to_vec()
}
