From c516fb947f7aafd00363dfcdaefa79f96e4f4ee5 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 14 May 2022 15:11:23 +0100 Subject: [PATCH] Add `image.destroy` --- core/image/common.odin | 1 + core/image/general_loader.odin | 13 +++++++++++++ core/image/netpbm/netpbm.odin | 7 ++----- core/image/png/png.odin | 1 + core/image/qoi/qoi.odin | 1 + core/image/which.odin | 22 +++++++++++----------- 6 files changed, 29 insertions(+), 16 deletions(-) diff --git a/core/image/common.odin b/core/image/common.odin index 75a649e52..28129a6e1 100644 --- a/core/image/common.odin +++ b/core/image/common.odin @@ -54,6 +54,7 @@ Image :: struct { */ background: Maybe(RGB_Pixel_16), metadata: Image_Metadata, + which: Which_File_Type, } Image_Metadata :: union { diff --git a/core/image/general_loader.odin b/core/image/general_loader.odin index 3acffb452..79f5fb737 100644 --- a/core/image/general_loader.odin +++ b/core/image/general_loader.odin @@ -43,3 +43,16 @@ load_from_file :: proc(filename: string, options := Options{}, allocator := cont return nil, .Unable_To_Read_File } } + +destroy :: proc(img: ^Image, allocator := context.allocator) -> bool { + if img == nil { + return true + } + context.allocator = allocator + destroyer := _internal_destroyers[img.which] + if destroyer != nil { + destroyer(img) + } + free(img) + return true +} \ No newline at end of file diff --git a/core/image/netpbm/netpbm.odin b/core/image/netpbm/netpbm.odin index 778ec2c5e..5a504cd7c 100644 --- a/core/image/netpbm/netpbm.odin +++ b/core/image/netpbm/netpbm.odin @@ -47,6 +47,7 @@ load_from_bytes :: proc(data: []byte, allocator := context.allocator) -> (img: ^ context.allocator = allocator img = new(Image) + img.which = .NetPBM header: Header; defer header_destroy(&header) header_size: int @@ -758,9 +759,5 @@ _register :: proc() { destroyer :: proc(img: ^Image) { _ = destroy(img) } - image.register(.PBM, loader, destroyer) - image.register(.PGM, loader, destroyer) - image.register(.PPM, loader, destroyer) - image.register(.PAM, loader, destroyer) - image.register(.PFM, loader, destroyer) + image.register(.NetPBM, loader, destroyer) } \ No newline at end of file diff --git a/core/image/png/png.odin b/core/image/png/png.odin index ea888d0ad..35fdb58d8 100644 --- a/core/image/png/png.odin +++ b/core/image/png/png.odin @@ -372,6 +372,7 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a if img == nil { img = new(Image) } + img.which = .PNG info := new(image.PNG_Info) img.metadata = info diff --git a/core/image/qoi/qoi.odin b/core/image/qoi/qoi.odin index f10f2ff56..29a17d4f4 100644 --- a/core/image/qoi/qoi.odin +++ b/core/image/qoi/qoi.odin @@ -224,6 +224,7 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a if img == nil { img = new(Image) } + img.which = .QOI if .return_metadata in options { info := new(image.QOI_Info) diff --git a/core/image/which.odin b/core/image/which.odin index 30cb78405..82cb03ce6 100644 --- a/core/image/which.odin +++ b/core/image/which.odin @@ -14,7 +14,7 @@ Which_File_Type :: enum { JPEG, JPEG_2000, JPEG_XL, - PBM, PGM, PPM, PAM, PFM, // NetPBM family + NetPBM, // NetPBM family PIC, // Softimage PIC PNG, // Portable Network Graphics PSD, // Photoshop PSD @@ -111,16 +111,16 @@ which_bytes :: proc(data: []byte) -> Which_File_Type { switch s[2] { case '\t', '\n', '\r': switch s[1] { - case '1', '4': - return .PBM - case '2', '5': - return .PGM - case '3', '6': - return .PPM - case '7': - return .PAM - case 'F', 'f': - return .PFM + case '1', '4': // PBM + return .NetPBM + case '2', '5': // PGM + return .NetPBM + case '3', '6': // PPM + return .NetPBM + case '7': // PAM + return .NetPBM + case 'F', 'f': // PFM + return .NetPBM } } case s[:8] == "\x89PNG\r\n\x1a\n":