commit dcff1f69cc640ed9a26d8fabfd37093838fee96c
parent 37046d7f041fb6b73c21dd38b34197e62b3e1d79
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date: Mon, 31 Dec 2018 14:27:13 +1300
[-] Remove gradient and make image same size as largest OSType.
Diffstat:
2 files changed, 56 insertions(+), 10 deletions(-)
diff --git a/examples/fractal_icns.rs b/examples/fractal_icns.rs
@@ -5,8 +5,8 @@ use icns::encode::Encoder;
use image::{self, ConvertBuffer};
fn main() {
- let imgx = 800;
- let imgy = 800;
+ let imgx = 1024;
+ let imgy = 1024;
let scalex = 3.0 / imgx as f32;
let scaley = 3.0 / imgy as f32;
@@ -14,13 +14,6 @@ fn main() {
// Create a new ImgBuf with width: imgx and height: imgy.
let mut imgbuf = image::ImageBuffer::new(imgx, imgy);
- // 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 {
diff --git a/readme.md b/readme.md
@@ -18,11 +18,64 @@ Where the Go project builds on the standard library `image.Image` interface, thi
See the examples folder for more usage.
+### Generate a fractal and encode as icns
+> Adapted from [fractal.rs](https://github.com/PistonDevelopers/image/blob/master/examples/fractal.rs)
+
+```rust
+use std::fs::File;
+use std::io::BufWriter;
+use num_complex;
+use icns::encode::Encoder;
+use image::{self, ConvertBuffer};
+
+fn main() {
+ let imgx = 1024;
+ let imgy = 1024;
+
+ 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);
+
+ // 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 = 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");
+}
+```
+
## Features
- [x] Encode icns (mvp) `DynamicImage -> .icns`
- [x] Parallel resizing (thanks [rayon](https://github.com/rayon-rs/rayon))
- [x] Lanczos3 interpolation
-- [ ] Decode largest image from icns
+- [ ] Decode largest image from icns into standalone png
- [ ] Simple and robust cli app
- [ ] Flag based use
- [ ] Shell pipe use