commit 37046d7f041fb6b73c21dd38b34197e62b3e1d79
parent 54dd8283aa3e7f117616fa6341bdf71fa445586a
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date: Mon, 31 Dec 2018 14:17:34 +1300
[~] Cleanup code.
- Buffer IO
- Comments
- Spacing
- Generate the fractal
Diffstat:
3 files changed, 53 insertions(+), 14 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -10,4 +10,5 @@ byteorder = "1.2.7"
rayon = "1.0.3"
[dev-dependencies]
-clap = "2.32.0"
-\ No newline at end of file
+clap = "2.32.0"
+num-complex = "0.2"
+\ No newline at end of file
diff --git a/examples/convert_png.rs b/examples/convert_png.rs
@@ -1,7 +1,7 @@
+use std::fs::File;
+use std::io::{prelude::*, BufReader, BufWriter};
use clap::{App, Arg};
-use std::io::prelude::*;
-use std::fs;
-use image::{self};
+use image;
use icns::encode::Encoder;
fn main() {
@@ -20,17 +20,28 @@ fn main() {
.short("o")
.help("path to output icns image"))
.get_matches();
+
+ // Load inputs. Since we specified "required", these unwraps wont fail.
let input = matches.value_of("input").unwrap();
let output = matches.value_of("output").unwrap();
+
+ // Read the png file into a buffer.
let mut png: Vec<u8> = vec![];
- fs::File::open(&input)
- .expect("opening input file")
+ BufReader::new(File::open(&input)
+ .expect("opening input file"))
.read_to_end(&mut png)
.expect("buffering input file");
+
+ // Load a DynamicImage object from the raw png data.
let png = image::load_from_memory(&png)
.expect("decoding png from buffer");
- let mut output = fs::File::create(&output)
- .expect("creating output file");
+
+ // Create the output file.
+ let mut output = BufWriter::new(File::create(&output)
+ .expect("creating output file"));
+
+ // Encode the png as icns into the output file.
+ // Note we use to_rgba to convert the DynamicImage into an RgbaImage.
Encoder::new(&mut output)
.encode(&png.to_rgba())
.expect("encoding icns");
diff --git a/examples/fractal_icns.rs b/examples/fractal_icns.rs
@@ -1,26 +1,53 @@
-use image::{self, ConvertBuffer};
-use std::fs;
+use std::fs::File;
+use std::io::BufWriter;
+use num_complex;
use icns::encode::Encoder;
+use image::{self, ConvertBuffer};
fn main() {
let imgx = 800;
let imgy = 800;
+ let scalex = 3.0 / imgx as f32;
+ let scaley = 3.0 / imgy as f32;
+
// Create a new ImgBuf with width: imgx and height: imgy.
let mut imgbuf = image::ImageBuffer::new(imgx, imgy);
- // Iterate over the coordinates and pixels of the image.
+ // Generate gradient.
for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
let r = (0.3 * x as f32) as u8;
let b = (0.3 * y as f32) as u8;
*pixel = image::Rgb([r, 0, b]);
}
+ // Generate fractal.
+ for x in 0..imgx {
+ for y in 0..imgy {
+ let cx = y as f32 * scalex - 1.5;
+ let cy = x as f32 * scaley - 1.5;
+
+ let c = num_complex::Complex::new(-0.4, 0.6);
+ let mut z = num_complex::Complex::new(cx, cy);
+
+ let mut i = 0;
+ while i < 255 && z.norm() <= 2.0 {
+ z = z * z + c;
+ i += 1;
+ }
+
+ let pixel = imgbuf.get_pixel_mut(x, y);
+ let data = (*pixel as image::Rgb<u8>).data;
+ *pixel = image::Rgb([data[0], i as u8, data[2]]);
+ }
+ }
+
// Open output file.
- let mut output = fs::File::create("fractal.icns")
- .expect("creating output file");
+ let mut output = BufWriter::new(File::create("fractal.icns")
+ .expect("creating output file"));
// Encode the image as icns.
+ // Note that we use ConvertBuffer trait to convert from RGB to RGBA.
Encoder::new(&mut output)
.encode(&imgbuf.convert())
.expect("encoding icns");