icns-rs

Easily create .icns files (Mac Icons) with this Rust library or the included CLI app.
Log | Files | Refs | LICENSE

fractal_icns.rs (1308B)


      1 use icns::Encoder;
      2 use image::buffer::ConvertBuffer;
      3 use num_complex;
      4 use std::fs::File;
      5 use std::io::BufWriter;
      6 
      7 fn main() {
      8     let imgx = 1024;
      9     let imgy = 1024;
     10 
     11     let scalex = 3.0 / imgx as f32;
     12     let scaley = 3.0 / imgy as f32;
     13 
     14     // Create a new ImgBuf with width: imgx and height: imgy.
     15     let mut imgbuf = image::ImageBuffer::new(imgx, imgy);
     16 
     17     // Generate fractal.
     18     for x in 0..imgx {
     19         for y in 0..imgy {
     20             let cx = y as f32 * scalex - 1.5;
     21             let cy = x as f32 * scaley - 1.5;
     22 
     23             let c = num_complex::Complex::new(-0.4, 0.6);
     24             let mut z = num_complex::Complex::new(cx, cy);
     25 
     26             let mut i = 0;
     27             while i < 255 && z.norm() <= 2.0 {
     28                 z = z * z + c;
     29                 i += 1;
     30             }
     31 
     32             let pixel = imgbuf.get_pixel_mut(x, y);
     33             let data = (*pixel as image::Rgb<u8>).0;
     34             *pixel = image::Rgb([data[0], i as u8, data[2]]);
     35         }
     36     }
     37 
     38     // Open output file.
     39     let mut output = BufWriter::new(File::create("fractal.icns").expect("creating output file"));
     40 
     41     // Encode the image as icns.
     42     // Note that we use ConvertBuffer trait to convert from RGB to RGBA.
     43     Encoder::new(&mut output)
     44         .encode(&imgbuf.convert())
     45         .expect("encoding icns");
     46 }