From 55f0d4d9a8a86fddba705141770a2e0a08966642 Mon Sep 17 00:00:00 2001 From: Simon Kellet Date: Sat, 6 Dec 2025 21:58:23 +0000 Subject: [PATCH] began ppm struct passing --- ppm.odin | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 ppm.odin diff --git a/ppm.odin b/ppm.odin new file mode 100644 index 0000000..6d4ab88 --- /dev/null +++ b/ppm.odin @@ -0,0 +1,57 @@ +package main + +import "core:c" +import "core:fmt" +import "core:os" +import "core:strconv" +import "core:strings" + +PPM_MAGIC_NUMBER :: enum { + P6, + P3, +} + +Ppm_file :: struct { + magic_num: PPM_MAGIC_NUMBER, + width: int, + height: int, + data: []byte, +} + +load_ppm :: proc(ppm_file: ^Ppm_file, filename: string) -> (success: bool) { + if !strings.has_suffix(filename, ".ppm") { + fmt.eprintfln("ERROR: File %s does not have a valid ppm extention (.ppm)", filename) + return false + } + + succ: bool + ppm_file.data, succ = os.read_entire_file_from_filename(filename, context.temp_allocator) + if !succ { + fmt.eprintfln("ERROR: Failed to load file: %s ", filename) + return false + } + return true +} + +ppm_parse :: proc(ppm_file: ^Ppm_file) { + + it := string(ppm_file.data) + for line in strings.split_lines_iterator(&it) { + if strings.starts_with(line, "#") {continue} //ignore comments + if strings.starts_with(line, "P") { + num, _ := strconv.parse_int(strings.trim(line, "P")) + if num == 3 { + ppm_file.magic_num = .P3 + } else if num == 6 { + ppm_file.magic_num = .P6 + } + } + + w_h_parts := strings.split(line, " ") + if len(w_h_parts) == 2 { + ppm_file.width, _ = strconv.parse_int(w_h_parts[0]) + ppm_file.height, _ = strconv.parse_int(w_h_parts[1]) + + } + } +}