rust/land_dump/src/main.rs
changeset 14126 69db1d2e4cec
child 14130 376a0551b00a
equal deleted inserted replaced
14125:7bb0e27910aa 14126:69db1d2e4cec
       
     1 extern crate integral_geometry;
       
     2 extern crate land2d;
       
     3 extern crate landgen;
       
     4 extern crate lfprng;
       
     5 extern crate png;
       
     6 extern crate structopt;
       
     7 
       
     8 use png::HasParameters;
       
     9 use std::fs::File;
       
    10 use std::io::BufWriter;
       
    11 use std::path::Path;
       
    12 use structopt::StructOpt;
       
    13 
       
    14 use integral_geometry::{Point, Rect, Size};
       
    15 use landgen::outline_template::OutlineTemplate;
       
    16 use landgen::template_based::TemplatedLandGenerator;
       
    17 use landgen::LandGenerationParameters;
       
    18 use landgen::LandGenerator;
       
    19 use lfprng::LaggedFibonacciPRNG;
       
    20 
       
    21 #[derive(StructOpt, Debug)]
       
    22 #[structopt(name = "basic")]
       
    23 struct Opt {
       
    24     #[structopt(short = "s", long = "seed", default_value = "TEST_SEED")]
       
    25     seed: String,
       
    26     #[structopt(short = "d", long = "dump-before-distort")]
       
    27     dump_before_distort: bool,
       
    28     #[structopt(short = "b", long = "dump-before-bezierize")]
       
    29     dump_before_bezierize: bool,
       
    30 }
       
    31 
       
    32 fn template() -> OutlineTemplate {
       
    33     let mut template = OutlineTemplate::new(Size::new(4096, 2048));
       
    34     template.islands = vec![vec![
       
    35         Rect::new(100, 2050, 1, 1),
       
    36         Rect::new(100, 500, 400, 1200),
       
    37         Rect::new(3600, 500, 400, 1200),
       
    38         Rect::new(3900, 2050, 1, 1),
       
    39     ]];
       
    40     template.fill_points = vec![Point::new(2047, 2047)];
       
    41 
       
    42     template
       
    43 }
       
    44 
       
    45 fn dump(
       
    46     seed: &[u8],
       
    47     skip_distort: bool,
       
    48     skip_bezier: bool,
       
    49     file_name: &Path,
       
    50 ) -> std::io::Result<()> {
       
    51     let params = LandGenerationParameters::new(0 as u8, 255, 100, skip_distort, skip_bezier);
       
    52     let landgen = TemplatedLandGenerator::new(template());
       
    53     let mut prng = LaggedFibonacciPRNG::new(seed);
       
    54     let land = landgen.generate_land(&params, &mut prng);
       
    55 
       
    56     let file = File::create(file_name)?;
       
    57     let ref mut w = BufWriter::new(file);
       
    58 
       
    59     let mut encoder = png::Encoder::new(w, land.width() as u32, land.height() as u32); // Width is 2 pixels and height is 1.
       
    60     encoder
       
    61         .set(png::ColorType::Grayscale)
       
    62         .set(png::BitDepth::Eight);
       
    63     let mut writer = encoder.write_header()?;
       
    64 
       
    65     writer.write_image_data(land.raw_pixels()).unwrap();
       
    66 
       
    67     Ok(())
       
    68 }
       
    69 
       
    70 fn main() {
       
    71     let opt = Opt::from_args();
       
    72     println!("{:?}", opt);
       
    73 
       
    74     if opt.dump_before_distort {
       
    75         dump(
       
    76             opt.seed.as_str().as_bytes(),
       
    77             true,
       
    78             true,
       
    79             Path::new("out.before_distort.png"),
       
    80         )
       
    81         .unwrap();
       
    82     }
       
    83     if opt.dump_before_bezierize {
       
    84         dump(
       
    85             opt.seed.as_str().as_bytes(),
       
    86             false,
       
    87             true,
       
    88             Path::new("out.before_bezier.png"),
       
    89         )
       
    90         .unwrap();
       
    91     }
       
    92     dump(
       
    93         opt.seed.as_str().as_bytes(),
       
    94         false,
       
    95         true,
       
    96         Path::new("out.full.png"),
       
    97     )
       
    98     .unwrap();
       
    99 }