image saving changes

Thu, 07 Nov 2024 13:04:20 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 07 Nov 2024 13:04:20 -0500
changeset 31
49227d097d14
parent 30
4fc8c93ed7e8
child 32
eb07aee01d57

image saving changes

src/main.rs file | annotate | diff | comparison | revisions
--- a/src/main.rs	Thu Nov 07 13:35:28 2024 -0500
+++ b/src/main.rs	Thu Nov 07 13:04:20 2024 -0500
@@ -101,8 +101,8 @@
     std::fs::create_dir_all(PREFIX)?;
     for face in Face::all() {
         write_face_csv(format!("{PREFIX}/{face}"), face, 32, |x| f.apply(x) + g.apply(x))?;
-        write_face_img(format!("{PREFIX}/{face}"), face, 128, |x| f.apply(x) + g.apply(x))?;
     }
+    write_face_imgs(128, |x| f.apply(x) + g.apply(x))?;
 
     run_and_save("x1", &f, &g, OnCube::new(F3, Loc([0.1, 0.7])), τ)?;
     run_and_save("x2", &f, &g, OnCube::new(F2, Loc([0.1, 0.7])), τ)?;
@@ -145,26 +145,38 @@
 
 /// Writes the values of `f` on `face` of a [`OnCube`] into a PNG file
 /// with resolution `n × n`.
-fn write_face_img(filename : String, face : Face, n : usize, mut f : impl FnMut(&OnCube) -> f64) -> DynError {
-    let mut img = ImageBuffer::new(n as u32, n as u32);
+fn write_face_imgs(n : usize, mut f : impl FnMut(&OnCube) -> f64) -> DynError {
     let grid = LinSpace {
         start : Loc([0.0, 0.0]),
         end : Loc([1.0, 1.0]),
         count : [n, n]
     };
-    let rawdata : Vec<_> = grid.into_iter()
-                               .map(|x| f(&OnCube::new(face, x)))
-                               .collect();
-    let a = rawdata.iter().copied().reduce(f64::max).unwrap();
-    img.pixels_mut()
-        .zip(rawdata)
-        .for_each(|(p, v)| {
-            let t = v/a;
-            let rgb = [1.0-t, 1.0-t, 1.0];
-            *p = Rgb(rgb.map(|v| (v*(u8::RANGE_MAX as f64)) as u8))
-        });
+
+    let mut m = 0.0;
+    let mut datas = Vec::new();
+
+    for face in Face::all() {
+        let rawdata : Vec<_> = grid.into_iter()
+                                .map(|Loc([x,y])| f(&OnCube::new(face, Loc([x, 1.0-y]))))
+                                .collect();
+        m = rawdata.iter().copied().fold(m, f64::max);
+        datas.push((face, rawdata));
+    }
 
-    img.save_with_format(format!("{filename}.png"), ImageFormat::Png)?;
+    for (face, rawdata) in datas {
+        let mut img = ImageBuffer::new(n as u32, n as u32);
+        img.pixels_mut()
+            .zip(rawdata)
+            .for_each(|(p, v)| {
+                let t = v/m;
+                // A very colourful option for bug hunting.
+                //let rgb = [(50.0*t).cos(), (20.0*t).sin(), (3.0*t).cos()];
+                let rgb = [1.0-t, 1.0-t, 1.0];
+                *p = Rgb(rgb.map(|v| (v*(u8::RANGE_MAX as f64)) as u8))
+            });
+
+        img.save_with_format(format!("{PREFIX}/{face}.png"), ImageFormat::Png)?;
+    }
 
     Ok(())
 }

mercurial