-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1cdf930
Showing
5 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "rust_raytracer" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
image = "0.13.0" | ||
cast = { version = "*", default-features = false } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// For reading and opening files | ||
use image::png::PNGEncoder; | ||
use image::ColorType; | ||
use std::fs::File; | ||
use std::io::BufWriter; | ||
use std::path::Path; | ||
|
||
pub fn write_image(path: &str, width: u32, height: u32, data: &[u8]) { | ||
let output = File::create(path).unwrap(); | ||
let encoder = PNGEncoder::new(output); | ||
encoder | ||
.encode(data, width, height, ColorType::RGB(8)) | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
pub mod image_encoder; | ||
|
||
use cast::u8; | ||
fn main() { | ||
const WIDTH: u32 = 256; | ||
const HEIGHT: u32 = 256; | ||
let mut img: image::RgbImage = image::ImageBuffer::new(WIDTH, HEIGHT); | ||
for j in 0..HEIGHT { | ||
for i in 0..WIDTH { | ||
let r = i as f64 / (WIDTH - 1) as f64; | ||
let g = j as f64 / (HEIGHT - 1) as f64; | ||
let b = 0.25 as f64; | ||
let ir: u8 = u8(255.0 * r).unwrap(); | ||
let ig: u8 = u8(255.0 * g).unwrap(); | ||
let ib: u8 = u8(255.0 * b).unwrap(); | ||
|
||
let p = image::Rgb([ir, ig, ib]); | ||
|
||
img.put_pixel(i, j, p); | ||
} | ||
} | ||
img.save("image.png").unwrap(); | ||
} |