Convert core:compress and core:image error checks to new union comparison.

No more need for `is_kind(err, Error_Value)`, just test err == Error_Value.
This commit is contained in:
Jeroen van Rijn
2021-05-03 15:08:34 +02:00
parent 357f66fcee
commit 59b3c472ca
8 changed files with 35 additions and 44 deletions
+2 -2
View File
@@ -23,7 +23,7 @@ main :: proc() {
img, err = png.load(file, options);
defer png.destroy(img);
if !png.is_kind(err, png.E_General.OK) {
if err != png.E_General.OK {
fmt.printf("Trying to read PNG file %v returned %v\n", file, err);
} else {
v: png.Info;
@@ -120,7 +120,7 @@ main :: proc() {
}
}
if is_kind(err, E_General.OK) && .do_not_decompress_image not_in options && .info not_in options {
if err == E_General.OK && .do_not_decompress_image not_in options && .info not_in options {
if ok := write_image_as_ppm("out.ppm", img); ok {
fmt.println("Saved decoded image.");
} else {
+4 -4
View File
@@ -107,7 +107,7 @@ text :: proc(c: Chunk) -> (res: Text, ok: bool) {
buf: bytes.Buffer;
zlib_error := zlib.inflate_from_byte_array(fields[2], &buf);
defer bytes.buffer_destroy(&buf);
if !is_kind(zlib_error, E_General.OK) {
if zlib_error != E_General.OK {
ok = false; return;
}
@@ -161,7 +161,7 @@ text :: proc(c: Chunk) -> (res: Text, ok: bool) {
buf: bytes.Buffer;
zlib_error := zlib.inflate_from_byte_array(rest, &buf);
defer bytes.buffer_destroy(&buf);
if !is_kind(zlib_error, E_General.OK) {
if zlib_error != E_General.OK {
ok = false; return;
}
@@ -200,7 +200,7 @@ iccp :: proc(c: Chunk) -> (res: iCCP, ok: bool) {
// Set up ZLIB context and decompress iCCP payload
buf: bytes.Buffer;
zlib_error := zlib.inflate_from_byte_array(fields[2], &buf);
if !is_kind(zlib_error, E_General.OK) {
if zlib_error != E_General.OK {
bytes.buffer_destroy(&buf);
ok = false; return;
}
@@ -498,7 +498,7 @@ when false {
err = zlib.write_zlib_stream_from_memory(&ctx);
b: []u8;
if is_kind(err, E_General, E_General.OK) {
if err == E_General.OK {
b = ctx.out_buf[:];
} else {
return err;
+10 -11
View File
@@ -16,7 +16,6 @@ Error :: compress.Error;
E_General :: compress.General_Error;
E_PNG :: image.Error;
E_Deflate :: compress.Deflate_Error;
is_kind :: compress.is_kind;
Image :: image.Image;
Options :: image.Options;
@@ -274,7 +273,7 @@ read_chunk :: proc(ctx: ^compress.Context) -> (Chunk, Error) {
read_header :: proc(ctx: ^compress.Context) -> (IHDR, Error) {
c, e := read_chunk(ctx);
if !is_kind(e, E_General.OK) {
if e != E_General.OK {
return {}, e;
}
@@ -454,7 +453,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
seen_ihdr = true;
header, err = read_header(&ctx);
if !is_kind(err, E_General.OK) {
if err != E_General.OK {
return img, err;
}
@@ -507,7 +506,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
}
c, err = read_chunk(&ctx);
if !is_kind(err, E_General.OK) {
if err != E_General.OK {
return img, err;
}
@@ -542,7 +541,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
next := ch.type;
for next == .IDAT {
c, err = read_chunk(&ctx);
if !is_kind(err, E_General.OK) {
if err != E_General.OK {
return img, err;
}
@@ -562,7 +561,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
seen_idat = true;
case .IEND:
c, err = read_chunk(&ctx);
if !is_kind(err, E_General.OK) {
if err != E_General.OK {
return img, err;
}
seen_iend = true;
@@ -571,7 +570,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
// TODO: Make sure that 16-bit bKGD + tRNS chunks return u16 instead of u16be
c, err = read_chunk(&ctx);
if !is_kind(err, E_General.OK) {
if err != E_General.OK {
return img, err;
}
seen_bkgd = true;
@@ -606,7 +605,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
}
case .tRNS:
c, err = read_chunk(&ctx);
if !is_kind(err, E_General.OK) {
if err != E_General.OK {
return img, err;
}
@@ -648,7 +647,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
case:
// Unhandled type
c, err = read_chunk(&ctx);
if !is_kind(err, E_General.OK) {
if err != E_General.OK {
return img, err;
}
if .return_metadata in options {
@@ -676,7 +675,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
zlib_error := zlib.inflate(idat, &buf);
defer bytes.buffer_destroy(&buf);
if !is_kind(zlib_error, E_General.OK) {
if zlib_error != E_General.OK {
return {}, zlib_error;
} else {
/*
@@ -713,7 +712,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
as metadata, and set it instead to the raw number of channels.
*/
defilter_error := defilter(img, &buf, &header, options);
if !is_kind(defilter_error, E_General.OK) {
if defilter_error != E_General.OK {
bytes.buffer_destroy(&img.pixels);
return {}, defilter_error;
}