2025-12-06 21:58:23 +00:00

58 lines
1.2 KiB
Odin

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])
}
}
}