2013-11-17 05:04:36 -06:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2013-08-05 23:11:25 -05:00
|
|
|
|
2014-02-01 13:24:42 -06:00
|
|
|
use std::io;
|
2012-12-10 19:32:48 -06:00
|
|
|
|
2013-11-17 05:04:36 -06:00
|
|
|
struct DummyWriter;
|
|
|
|
impl Writer for DummyWriter {
|
2014-02-01 13:24:42 -06:00
|
|
|
fn write(&mut self, _: &[u8]) -> io::IoResult<()> { Ok(()) }
|
2013-11-17 05:04:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static ITER: int = 50;
|
2013-04-18 23:44:50 -05:00
|
|
|
static LIMIT: f64 = 2.0;
|
2012-02-03 19:46:17 -06:00
|
|
|
|
2013-04-18 23:44:50 -05:00
|
|
|
fn main() {
|
2013-11-17 05:04:36 -06:00
|
|
|
let args = std::os::args();
|
|
|
|
let (w, mut out) = if args.len() < 2 {
|
2014-01-09 04:06:55 -06:00
|
|
|
println!("Test mode: do not dump the image because it's not utf8, \
|
|
|
|
which interferes with the test runner.");
|
2013-11-17 05:04:36 -06:00
|
|
|
(1000, ~DummyWriter as ~Writer)
|
|
|
|
} else {
|
|
|
|
(from_str(args[1]).unwrap(),
|
2014-02-28 14:55:30 -06:00
|
|
|
~std::io::stdout() as ~Writer)
|
2013-11-17 05:04:36 -06:00
|
|
|
};
|
|
|
|
let h = w;
|
2013-11-17 05:47:47 -06:00
|
|
|
let mut byte_acc = 0u8;
|
2013-11-17 05:04:36 -06:00
|
|
|
let mut bit_num = 0;
|
2013-04-18 23:44:50 -05:00
|
|
|
|
2013-11-17 05:04:36 -06:00
|
|
|
writeln!(out, "P4\n{} {}", w, h);
|
2013-04-18 23:44:50 -05:00
|
|
|
|
2013-11-17 05:04:36 -06:00
|
|
|
for y in range(0, h) {
|
|
|
|
let y = y as f64;
|
|
|
|
for x in range(0, w) {
|
|
|
|
let mut z_r = 0f64;
|
|
|
|
let mut z_i = 0f64;
|
|
|
|
let mut t_r = 0f64;
|
|
|
|
let mut t_i = 0f64;
|
|
|
|
let c_r = 2.0 * (x as f64) / (w as f64) - 1.5;
|
|
|
|
let c_i = 2.0 * (y as f64) / (h as f64) - 1.0;
|
2013-04-18 23:44:50 -05:00
|
|
|
|
2013-11-17 05:04:36 -06:00
|
|
|
for _ in range(0, ITER) {
|
|
|
|
if t_r + t_i > LIMIT * LIMIT {
|
|
|
|
break;
|
2013-04-18 23:44:50 -05:00
|
|
|
}
|
|
|
|
|
2013-11-17 05:04:36 -06:00
|
|
|
z_i = 2.0 * z_r * z_i + c_i;
|
|
|
|
z_r = t_r - t_i + c_r;
|
|
|
|
t_r = z_r * z_r;
|
|
|
|
t_i = z_i * z_i;
|
|
|
|
}
|
2013-04-18 23:44:50 -05:00
|
|
|
|
2013-11-17 05:04:36 -06:00
|
|
|
byte_acc <<= 1;
|
|
|
|
if t_r + t_i <= LIMIT * LIMIT {
|
|
|
|
byte_acc |= 1;
|
|
|
|
}
|
2013-04-18 23:44:50 -05:00
|
|
|
|
2013-11-17 05:04:36 -06:00
|
|
|
bit_num += 1;
|
|
|
|
|
|
|
|
if bit_num == 8 {
|
2013-11-17 05:47:47 -06:00
|
|
|
out.write_u8(byte_acc);
|
2013-11-17 05:04:36 -06:00
|
|
|
byte_acc = 0;
|
|
|
|
bit_num = 0;
|
|
|
|
} else if x == w - 1 {
|
|
|
|
byte_acc <<= 8 - w % 8;
|
2013-11-17 05:47:47 -06:00
|
|
|
out.write_u8(byte_acc);
|
2013-11-17 05:04:36 -06:00
|
|
|
byte_acc = 0;
|
|
|
|
bit_num = 0;
|
2013-04-18 23:44:50 -05:00
|
|
|
}
|
2012-02-03 21:50:32 -06:00
|
|
|
}
|
|
|
|
}
|
2013-11-17 05:04:36 -06:00
|
|
|
|
|
|
|
out.flush();
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|