aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2021-06-21 16:32:42 +0200
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2021-06-21 16:32:42 +0200
commite036a321a054261d35c3ff241c327e1ba1daf39e (patch)
tree438d495d6f8d8c2df7835b048714888a083986f6
parent18471f358ea0e096bfdfb9dc04cd3fcbbb3ea50b (diff)
Replace `core:image`'s `sidecar` with explicit `metadata_ptr` and `metadata_type`.
To unpack, use: ```odin v: ^png.Info; if img.metadata_ptr != nil && img.metadata_type == png.Info { v = (^png.Info)(img.metadata_ptr); ... } ```
-rw-r--r--core/image/common.odin31
-rw-r--r--core/image/png/example.odin6
-rw-r--r--core/image/png/png.odin9
3 files changed, 23 insertions, 23 deletions
diff --git a/core/image/common.odin b/core/image/common.odin
index 2d1dcf0f8..8443a2d22 100644
--- a/core/image/common.odin
+++ b/core/image/common.odin
@@ -4,18 +4,20 @@ import "core:bytes"
import "core:mem"
Image :: struct {
- width: int,
- height: int,
- channels: int,
- depth: int,
- pixels: bytes.Buffer,
+ width: int,
+ height: int,
+ channels: int,
+ depth: int,
+ pixels: bytes.Buffer,
/*
Some image loaders/writers can return/take an optional background color.
For convenience, we return them as u16 so we don't need to switch on the type
in our viewer, and can just test against nil.
*/
- background: Maybe([3]u16),
- sidecar: any,
+ background: Maybe([3]u16),
+
+ metadata_ptr: rawptr,
+ metadata_type: typeid,
}
/*
@@ -190,13 +192,14 @@ return_single_channel :: proc(img: ^Image, channel: Channel) -> (res: ^Image, ok
}
res = new(Image);
- res.width = img.width;
- res.height = img.height;
- res.channels = 1;
- res.depth = img.depth;
- res.pixels = t;
- res.background = img.background;
- res.sidecar = img.sidecar;
+ res.width = img.width;
+ res.height = img.height;
+ res.channels = 1;
+ res.depth = img.depth;
+ res.pixels = t;
+ res.background = img.background;
+ res.metadata_ptr = img.metadata_ptr;
+ res.metadata_type = img.metadata_type;
return res, true;
}
diff --git a/core/image/png/example.odin b/core/image/png/example.odin
index 2b95d724a..3dd4af2ff 100644
--- a/core/image/png/example.odin
+++ b/core/image/png/example.odin
@@ -14,7 +14,7 @@ import "core:os"
main :: proc() {
file: string;
- options := image.Options{};
+ options := image.Options{.return_metadata};
err: compress.Error;
img: ^image.Image;
@@ -27,11 +27,11 @@ main :: proc() {
fmt.printf("Trying to read PNG file %v returned %v\n", file, err);
} else {
v: ^png.Info;
- ok: bool;
fmt.printf("Image: %vx%vx%v, %v-bit.\n", img.width, img.height, img.channels, img.depth);
- if v, ok = img.sidecar.(^png.Info); ok {
+ if img.metadata_ptr != nil && img.metadata_type == png.Info {
+ v = (^png.Info)(img.metadata_ptr);
// Handle ancillary chunks as you wish.
// We provide helper functions for a few types.
for c in v.chunks {
diff --git a/core/image/png/png.odin b/core/image/png/png.odin
index 8d26d87d7..18295793d 100644
--- a/core/image/png/png.odin
+++ b/core/image/png/png.odin
@@ -392,10 +392,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
img = new(Image);
}
- info: ^Info;
- if img.sidecar == nil {
- info = new(Info);
- }
+ info := new(Info, context.allocator);
ctx := &compress.Context{
input = stream,
@@ -417,7 +414,6 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
header: IHDR;
- img.sidecar = info;
info.chunks.allocator = context.temp_allocator;
// State to ensure correct chunk ordering.
@@ -659,7 +655,8 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
}
if .return_header in options || .return_metadata in options {
- img.sidecar = info;
+ img.metadata_ptr = info;
+ img.metadata_type = typeid_of(Info);
}
if .do_not_decompress_image in options {
img.channels = final_image_channels;