|
1 |
| -// xfail-test reading from os::args()[1] - bogus! |
| 1 | +// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
2 | 10 |
|
3 |
| -use std::cast::transmute; |
4 |
| -use std::from_str::FromStr; |
5 |
| -use std::libc::{STDOUT_FILENO, c_int, fdopen, fputc}; |
6 |
| -use std::os; |
| 11 | +use std::io::buffered::BufferedWriter; |
7 | 12 |
|
8 |
| -static ITER: uint = 50; |
| 13 | +struct DummyWriter; |
| 14 | +impl Writer for DummyWriter { |
| 15 | + fn write(&mut self, _: &[u8]) {} |
| 16 | +} |
| 17 | + |
| 18 | +static ITER: int = 50; |
9 | 19 | static LIMIT: f64 = 2.0;
|
10 | 20 |
|
11 | 21 | fn main() {
|
12 |
| - unsafe { |
13 |
| - let w: i32 = FromStr::from_str(os::args()[1]).unwrap(); |
14 |
| - let h = w; |
15 |
| - let mut byte_acc: i8 = 0; |
16 |
| - let mut bit_num: i32 = 0; |
17 |
| - |
18 |
| - println!("P4\n{} {}", w, h); |
| 22 | + let args = std::os::args(); |
| 23 | + let (w, mut out) = if args.len() < 2 { |
| 24 | + println("Test mode: do not dump the image because it's not utf8, \ |
| 25 | + which interferes with the test runner."); |
| 26 | + (1000, ~DummyWriter as ~Writer) |
| 27 | + } else { |
| 28 | + (from_str(args[1]).unwrap(), |
| 29 | + ~BufferedWriter::new(std::io::stdout()) as ~Writer) |
| 30 | + }; |
| 31 | + let h = w; |
| 32 | + let mut byte_acc = 0u8; |
| 33 | + let mut bit_num = 0; |
19 | 34 |
|
20 |
| - let mode = "w"; |
21 |
| - let stdout = fdopen(STDOUT_FILENO as c_int, transmute(&mode[0])); |
| 35 | + writeln!(out, "P4\n{} {}", w, h); |
22 | 36 |
|
23 |
| - for y in range(0i32, h) { |
24 |
| - let y = y as f64; |
25 |
| - for x in range(0i32, w) { |
26 |
| - let mut Zr = 0f64; |
27 |
| - let mut Zi = 0f64; |
28 |
| - let mut Tr = 0f64; |
29 |
| - let mut Ti = 0f64; |
30 |
| - let Cr = 2.0 * (x as f64) / (w as f64) - 1.5; |
31 |
| - let Ci = 2.0 * (y as f64) / (h as f64) - 1.0; |
| 37 | + for y in range(0, h) { |
| 38 | + let y = y as f64; |
| 39 | + for x in range(0, w) { |
| 40 | + let mut z_r = 0f64; |
| 41 | + let mut z_i = 0f64; |
| 42 | + let mut t_r = 0f64; |
| 43 | + let mut t_i = 0f64; |
| 44 | + let c_r = 2.0 * (x as f64) / (w as f64) - 1.5; |
| 45 | + let c_i = 2.0 * (y as f64) / (h as f64) - 1.0; |
32 | 46 |
|
33 |
| - for _ in range(0i32, ITER as i32) { |
34 |
| - if Tr + Ti > LIMIT * LIMIT { |
35 |
| - break; |
36 |
| - } |
37 |
| - |
38 |
| - Zi = 2.0*Zr*Zi + Ci; |
39 |
| - Zr = Tr - Ti + Cr; |
40 |
| - Tr = Zr * Zr; |
41 |
| - Ti = Zi * Zi; |
| 47 | + for _ in range(0, ITER) { |
| 48 | + if t_r + t_i > LIMIT * LIMIT { |
| 49 | + break; |
42 | 50 | }
|
43 | 51 |
|
44 |
| - byte_acc <<= 1; |
45 |
| - if Tr + Ti <= LIMIT * LIMIT { |
46 |
| - byte_acc |= 1; |
47 |
| - } |
| 52 | + z_i = 2.0 * z_r * z_i + c_i; |
| 53 | + z_r = t_r - t_i + c_r; |
| 54 | + t_r = z_r * z_r; |
| 55 | + t_i = z_i * z_i; |
| 56 | + } |
48 | 57 |
|
49 |
| - bit_num += 1; |
| 58 | + byte_acc <<= 1; |
| 59 | + if t_r + t_i <= LIMIT * LIMIT { |
| 60 | + byte_acc |= 1; |
| 61 | + } |
50 | 62 |
|
51 |
| - if bit_num == 8 { |
52 |
| - fputc(byte_acc as c_int, stdout); |
53 |
| - byte_acc = 0; |
54 |
| - bit_num = 0; |
55 |
| - } else if x == w - 1 { |
56 |
| - byte_acc <<= 8 - w%8; |
57 |
| - fputc(byte_acc as c_int, stdout); |
58 |
| - byte_acc = 0; |
59 |
| - bit_num = 0; |
60 |
| - } |
| 63 | + bit_num += 1; |
| 64 | + |
| 65 | + if bit_num == 8 { |
| 66 | + out.write_u8(byte_acc); |
| 67 | + byte_acc = 0; |
| 68 | + bit_num = 0; |
| 69 | + } else if x == w - 1 { |
| 70 | + byte_acc <<= 8 - w % 8; |
| 71 | + out.write_u8(byte_acc); |
| 72 | + byte_acc = 0; |
| 73 | + bit_num = 0; |
61 | 74 | }
|
62 | 75 | }
|
63 | 76 | }
|
| 77 | + |
| 78 | + out.flush(); |
64 | 79 | }
|
0 commit comments