tga_os.odin 859 B

12345678910111213141516171819202122232425262728293031323334
  1. #+build !js
  2. package tga
  3. import "core:os"
  4. import "core:bytes"
  5. save :: proc{save_to_buffer, save_to_file}
  6. save_to_file :: proc(output: string, img: ^Image, options := Options{}, allocator := context.allocator) -> (err: Error) {
  7. context.allocator = allocator
  8. out := &bytes.Buffer{}
  9. defer bytes.buffer_destroy(out)
  10. save_to_buffer(out, img, options) or_return
  11. write_ok := os.write_entire_file(output, out.buf[:])
  12. return nil if write_ok else .Unable_To_Write_File
  13. }
  14. load :: proc{load_from_file, load_from_bytes, load_from_context}
  15. load_from_file :: proc(filename: string, options := Options{}, allocator := context.allocator) -> (img: ^Image, err: Error) {
  16. context.allocator = allocator
  17. data, ok := os.read_entire_file(filename)
  18. defer delete(data)
  19. if ok {
  20. return load_from_bytes(data, options)
  21. } else {
  22. return nil, .Unable_To_Read_File
  23. }
  24. }