commit a3ffac057b80a53ee9a60c8499fd75e847994b38
parent 9c51f9edafded2729c4d0bc6cc3b791f28e8a0fb
Author: minerva-jupiter <ryouturn@gmail.com>
Date: Fri, 31 Oct 2025 07:02:57 +0900
fmt
Diffstat:
| M | src/main.rs | | | 50 | +++++++++++++++++++++++++++++--------------------- |
1 file changed, 29 insertions(+), 21 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -1,4 +1,4 @@
-use image::{GenericImageView, DynamicImage, Rgba, Pixel, Luma};
+use image::{DynamicImage, GenericImageView, Luma, Pixel, Rgba};
use std::env;
use std::fs::File;
use std::path::Path;
@@ -22,7 +22,7 @@ fn load_image(path: &Path) -> Result<Vec<Vec<Rgba<u8>>>, image::ImageError> {
const MIN_WAVELENGTH_NM: f64 = 400.0; // 紫の端
const MAX_WAVELENGTH_NM: f64 = 700.0; // 赤の端
const GREEN_WAVELENGTH_NM: f64 = 550.0; // 緑色の中央付近
- //
+//
fn rgba_to_approx_wavelength(pixel: Rgba<u8>) -> f64 {
let r_u8 = pixel.0[0] as f64;
let g_u8 = pixel.0[1] as f64;
@@ -54,24 +54,28 @@ fn rgba_to_approx_wavelength(pixel: Rgba<u8>) -> f64 {
hue_deg = h_prime * 60.0;
}
-
- let h_norm = hue_deg / 360.0;
-
+
+ let h_norm = hue_deg / 360.0;
+
let wavelength_nm;
-
- if h_norm <= 1.0 / 6.0 { // 赤〜黄 (0°〜60°) : 700nm から 590nm
+
+ if h_norm <= 1.0 / 6.0 {
+ // 赤〜黄 (0°〜60°) : 700nm から 590nm
let t = h_norm * 6.0; // tは 0.0 から 1.0
wavelength_nm = MAX_WAVELENGTH_NM - t * (MAX_WAVELENGTH_NM - 590.0);
- } else if h_norm <= 2.0 / 6.0 { // 黄〜緑 (60°〜120°) : 590nm から 550nm
+ } else if h_norm <= 2.0 / 6.0 {
+ // 黄〜緑 (60°〜120°) : 590nm から 550nm
let t = (h_norm - 1.0 / 6.0) * 6.0; // tは 0.0 から 1.0
wavelength_nm = 590.0 - t * (590.0 - GREEN_WAVELENGTH_NM);
- } else if h_norm <= 4.0 / 6.0 { // 緑〜青 (120°〜240°) : 550nm から 450nm
+ } else if h_norm <= 4.0 / 6.0 {
+ // 緑〜青 (120°〜240°) : 550nm から 450nm
let t = (h_norm - 2.0 / 6.0) * 3.0; // tは 0.0 から 1.0
wavelength_nm = GREEN_WAVELENGTH_NM - t * (GREEN_WAVELENGTH_NM - 450.0);
- } else { // 青〜赤 (240°〜360°) : 450nm から 700nm
+ } else {
+ // 青〜赤 (240°〜360°) : 450nm から 700nm
let t_total = h_norm - 4.0 / 6.0; // 240度 (4/6) から 1.0 までの範囲
- let t_norm = t_total * 3.0; // t_norm は 0.0 から 1.0
+ let t_norm = t_total * 3.0; // t_norm は 0.0 から 1.0
wavelength_nm = 450.0 - t_norm * (450.0 - MIN_WAVELENGTH_NM);
@@ -93,17 +97,19 @@ fn wavelength_to_grayscale(wavelength_nm: f64) -> u8 {
grayscale_f64.round() as u8
}
-fn main() -> Result<(), Box<dyn std::error::Error>>{
+fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = env::args().collect();
- if args.len() == 1 || args.len() > 4{
- println!("args are not collect. use this 'image-light-freq-con <inputFilePath> <outputFilePath>'");
+ if args.len() == 1 || args.len() > 4 {
+ println!(
+ "args are not collect. use this 'image-light-freq-con <inputFilePath> <outputFilePath>'"
+ );
return Ok(());
}
let path = Path::new(&args[1]);
- let pixels = match load_image(path){
+ let pixels = match load_image(path) {
Ok(pixels) => pixels,
Err(e) => {
- println!("Could not load image. {:?}",e);
+ println!("Could not load image. {:?}", e);
return Ok(());
}
};
@@ -121,17 +127,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>>{
grascale_img.push(grascale_img_row);
}
- let grayscale_data_flat: Vec<u8> = grascale_img.into_iter()
+ let grayscale_data_flat: Vec<u8> = grascale_img
+ .into_iter()
.flat_map(|row| row.into_iter())
.collect();
- let output_img = image::ImageBuffer::<Luma<u8>, _>::from_raw(width, height, grayscale_data_flat)
- .ok_or("Failed to create image buffer")?;
+ let output_img =
+ image::ImageBuffer::<Luma<u8>, _>::from_raw(width, height, grayscale_data_flat)
+ .ok_or("Failed to create image buffer")?;
let output_filename = Path::new(&args[2]);
-
+
let mut output_file = File::create(output_filename)?;
output_img.write_to(&mut output_file, image::ImageFormat::Png)?;
-
+
Ok(())
}