Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
angad-k committed Feb 6, 2022
0 parents commit 1cdf930
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
308 changes: 308 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Cargo.toml
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 }
14 changes: 14 additions & 0 deletions src/image_encoder.rs
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();
}
23 changes: 23 additions & 0 deletions src/main.rs
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();
}

0 comments on commit 1cdf930

Please sign in to comment.