rusty_ppm/utils.rs
1// Copyright (c) 2024 Remi A. Godin
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21#![allow(unused)]
22use crate::ppm_writer::*;
23use std::path::{Path, PathBuf};
24use std::fs;
25use std::io::Write;
26
27use cgmath::{vec3, Vector3};
28use simple_canvas::Canvas;
29
30pub fn generate_sample_string_image(width: usize, height: usize, directory: &Path, file_name: &str) {
31 let mut image: Canvas<Vector3<u8>> = Canvas::new(width, height, vec3(0,0,0));
32 for row in 0..height {
33 for col in 0..width {
34 *image.get_mut(row, col).unwrap() = vec3((row*256/image.height) as u8, (col*256/image.width) as u8, 0);
35 }
36 }
37 write_string_ppm(&image, directory, file_name);
38
39}
40
41pub fn generate_sample_binary_image(width: usize, height: usize, directory: &Path, file_name: &str) {
42 let mut image: Canvas<Vector3<u8>> = Canvas::new(width, height, vec3(0,0,0));
43 for row in 0..image.height {
44 for col in 0..image.width {
45 *image.get_mut(row, col).unwrap() = vec3((row*256/image.height) as u8, (col*256/image.width) as u8, 0);
46 }
47 }
48 write_binary_ppm(&image, directory, file_name);
49}
50
51pub fn complete_path(directory: &Path, file_name: &str) -> PathBuf {
52 let mut full_path = PathBuf::new();
53 full_path.push(directory);
54 full_path.set_file_name(file_name);
55 full_path.set_extension("ppm");
56 full_path
57
58}