main.rs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. use std::fs::File;
  2. use std::io::BufReader;
  3. use std::io::Read;
  4. // ┏━┓┏━┓┏┓╻╺┳┓┏━┓┏┳┓ ┏┓╻╻ ╻┏┳┓┏┓ ┏━╸┏━┓ ┏━╸┏━╸┏┓╻┏━╸┏━┓┏━┓╺┳╸╻┏━┓┏┓╻
  5. // ┣┳┛┣━┫┃┗┫ ┃┃┃ ┃┃┃┃ ┃┗┫┃ ┃┃┃┃┣┻┓┣╸ ┣┳┛ ┃╺┓┣╸ ┃┗┫┣╸ ┣┳┛┣━┫ ┃ ┃┃ ┃┃┗┫
  6. // ╹┗╸╹ ╹╹ ╹╺┻┛┗━┛╹ ╹ ╹ ╹┗━┛╹ ╹┗━┛┗━╸╹┗╸ ┗━┛┗━╸╹ ╹┗━╸╹┗╸╹ ╹ ╹ ╹┗━┛╹ ╹
  7. pub struct RNG {
  8. random_device: BufReader<File>,
  9. }
  10. impl RNG {
  11. pub fn next_float(&mut self) -> f32 {
  12. let mut buf: [u8; 4] = [0, 0, 0, 0];
  13. self.random_device
  14. .read(&mut buf)
  15. .expect("Read from /dev/random failed");
  16. let val: f32 = u32::from_be_bytes(buf) as f32;
  17. val as f32 / u32::max_value() as f32
  18. }
  19. pub fn create() -> RNG {
  20. let f = File::open("/dev/urandom").expect("Did not find source of randomness");
  21. let reader: BufReader<File> = BufReader::new(f);
  22. return RNG {
  23. random_device: reader,
  24. };
  25. }
  26. }
  27. // ┏━╸┏━╸┏━┓┏┳┓┏━╸╺┳╸┏━┓╻ ╻
  28. // ┃╺┓┣╸ ┃ ┃┃┃┃┣╸ ┃ ┣┳┛┗┳┛
  29. // ┗━┛┗━╸┗━┛╹ ╹┗━╸ ╹ ╹┗╸ ╹
  30. #[derive(Copy, Clone)]
  31. pub struct Vec3 {
  32. x: f32,
  33. y: f32,
  34. z: f32,
  35. }
  36. // ╻┏┳┓┏━┓┏━╸┏━╸ ╻┏━┓
  37. // ┃┃┃┃┣━┫┃╺┓┣╸ ┃┃ ┃
  38. // ╹╹ ╹╹ ╹┗━┛┗━╸ ╹┗━┛
  39. use std::convert::From;
  40. use std::io::BufWriter;
  41. use std::io::Write;
  42. use std::path::Path;
  43. pub struct Image {
  44. width: u16,
  45. height: u16,
  46. data: Vec<Vec3>,
  47. }
  48. impl Image {
  49. fn get_pixel(&self, x: usize, y: usize) -> &Vec3 {
  50. &self.data[y * self.width as usize + x]
  51. }
  52. fn mod_pixel(&mut self, x: usize, y: usize) -> &mut Vec3 {
  53. &mut self.data[y * self.width as usize + x]
  54. }
  55. fn new(width: u16, height: u16) -> Image {
  56. let mut data: Vec<Vec3> = Vec::new();
  57. data.resize(
  58. usize::from(width) * usize::from(height),
  59. Vec3 {
  60. x: 0.0,
  61. y: 0.0,
  62. z: 0.0,
  63. },
  64. );
  65. Image {
  66. width: width,
  67. height: height,
  68. data: data,
  69. }
  70. }
  71. }
  72. fn read_int(v: &[u8]) -> u32 {
  73. let mut result = 0;
  74. for i in 0..3 {
  75. result |= (v[i] as u32) << (i * 8);
  76. }
  77. result
  78. }
  79. fn read_int16(v: &[u8]) -> u16 {
  80. v[1] as u16 >> 8 | v[0] as u16
  81. }
  82. fn read_vec3(v: &[u8]) -> Vec3 {
  83. Vec3 {
  84. x: v[2] as f32,
  85. y: v[1] as f32,
  86. z: v[0] as f32,
  87. }
  88. }
  89. fn round_row_size(width: u32) -> u32 {
  90. ((24 * width + 31) / 32) * 4
  91. }
  92. pub fn load_bmp(path: &Path) -> Image {
  93. // See https://en.wikipedia.org/wiki/BMP_file_format for more information.
  94. // BMP is little-endian (least-significant bytes first)
  95. // Some limitations here:
  96. // - we only support bottom-up bitmaps
  97. // - without compression
  98. // - and a depth of 24 bits per pixel
  99. let mut buf = Vec::new();
  100. let mut f = BufReader::new(File::open(path).expect("Could not find image file"));
  101. f.read_to_end(&mut buf).expect("Could not read image file");
  102. assert!(
  103. buf[0] == 'B' as u8 && buf[1] == 'M' as u8,
  104. "File is not a bitmap file"
  105. );
  106. let width = read_int(&buf[18..22]);
  107. let height = read_int(&buf[22..26]);
  108. let compression = read_int(&buf[30..34]);
  109. assert!(compression == 0, "Only uncompressed BMPs are supported");
  110. let depth = read_int16(&buf[28..30]);
  111. assert!(depth == 24, "Only 24 bits per pixel are supported");
  112. let offset = read_int(&buf[10..14]);
  113. let row_size = round_row_size(width);
  114. let mut image_data = Vec::new();
  115. for row in 0..height {
  116. for col in 0..width {
  117. let y = height - row - 1;
  118. let x = 3 * col;
  119. let idx = (offset + y * row_size + x) as usize;
  120. let pixel = read_vec3(&buf[idx..(idx + 3)]);
  121. image_data.push(pixel);
  122. }
  123. }
  124. assert!(image_data.len() == (width * height) as usize);
  125. Image {
  126. width: width as u16,
  127. height: height as u16,
  128. data: image_data,
  129. }
  130. }
  131. fn write_int(f: &mut dyn Write, v: u32) -> std::io::Result<()> {
  132. let bytes = [v as u8, (v >> 8) as u8, (v >> 16) as u8, (v >> 24) as u8];
  133. f.write_all(&bytes)?;
  134. Ok(())
  135. }
  136. fn write_int16(f: &mut dyn Write, v: u16) -> std::io::Result<()> {
  137. let bytes = [v as u8, (v >> 8) as u8];
  138. f.write_all(&bytes)?;
  139. Ok(())
  140. }
  141. fn write_vec3(f: &mut dyn Write, v: &Vec3) -> std::io::Result<()> {
  142. let bytes = [v.z as u8, v.y as u8, v.x as u8];
  143. f.write_all(&bytes)?;
  144. Ok(())
  145. }
  146. fn save_bmp(p: &Path, i: &Image) -> std::io::Result<()> {
  147. let f: File = File::create(p)?;
  148. let mut w = BufWriter::new(f);
  149. // File header
  150. w.write_all(&['B' as u8, 'M' as u8])?;
  151. write_int(&mut w, 0)?; // size, left blank for now
  152. write_int(&mut w, 0)?; // reserved bytes
  153. write_int(&mut w, 54)?; // offset
  154. // Bitmap info header
  155. write_int(&mut w, 40)?; // header size in bytes
  156. write_int(&mut w, i.width as u32)?;
  157. write_int(&mut w, i.height as u32)?;
  158. write_int16(&mut w, 1)?; // number of color planes
  159. write_int16(&mut w, 24)?; // bits per pixel
  160. write_int(&mut w, 0)?; // no compression
  161. write_int(&mut w, 0)?; // image size
  162. write_int(&mut w, 1000)?; // horizontal resolution in pixels per metre
  163. write_int(&mut w, 1000)?; // vertical resolution
  164. write_int(&mut w, 0)?; // number of colors
  165. write_int(&mut w, 0)?; // number of important colors
  166. // Pixel data
  167. for y in (0..i.height).rev() {
  168. for x in 0..i.width {
  169. write_vec3(&mut w, i.get_pixel(x.into(), y.into()))?;
  170. }
  171. }
  172. Ok(())
  173. }
  174. fn main() {
  175. let mut rng = RNG::create();
  176. println!("{}", rng.next_float());
  177. let path = Path::new("skymap.bmp");
  178. let image = load_bmp(&path);
  179. println!(
  180. "Image length: {}, {}x{}",
  181. image.data.len(),
  182. image.width,
  183. image.height
  184. );
  185. let pix = image.get_pixel(0, 0);
  186. println!("RGB Val: ({}, {}, {})", pix.x, pix.y, pix.z);
  187. // Create a new pixel from scratch
  188. let mut rand_bitmap = Image::new(1024, 1024);
  189. for x in 0..1024 {
  190. for y in 0..1024 {
  191. let mut p = rand_bitmap.mod_pixel(x, y);
  192. p.x = rng.next_float() * 255.0;
  193. p.y = rng.next_float() * 255.0;
  194. p.z = rng.next_float() * 255.0;
  195. }
  196. }
  197. let outpath = Path::new("random.bmp");
  198. save_bmp(&outpath, &rand_bitmap).expect("Could not save bitmap :(");
  199. }