Minor stylistic code changes to compress and image packages

This commit is contained in:
gingerBill
2021-04-30 10:58:29 +01:00
parent 7ef30355cb
commit 5f617c56e1
8 changed files with 584 additions and 589 deletions
+2 -2
View File
@@ -34,7 +34,7 @@ main :: proc() {
if len(args) < 2 {
stderr("No input file specified.\n");
err := gzip.load(&TEST, &buf);
err := gzip.load(TEST, &buf);
if gzip.is_kind(err, gzip.E_General.OK) {
stdout("Displaying test vector: ");
stdout(bytes.buffer_to_string(&buf));
@@ -50,7 +50,7 @@ main :: proc() {
if file == "-" {
// Read from stdin
s := os.stream_from_handle(os.stdin);
err = gzip.load(&s, &buf);
err = gzip.load(s, &buf);
} else {
err = gzip.load(file, &buf);
}
+8 -10
View File
@@ -96,13 +96,13 @@ E_ZLIB :: compress.ZLIB_Error;
E_Deflate :: compress.Deflate_Error;
is_kind :: compress.is_kind;
load_from_slice :: proc(slice: ^[]u8, buf: ^bytes.Buffer, allocator := context.allocator) -> (err: Error) {
load_from_slice :: proc(slice: []u8, buf: ^bytes.Buffer, allocator := context.allocator) -> (err: Error) {
r := bytes.Reader{};
bytes.reader_init(&r, slice^);
bytes.reader_init(&r, slice);
stream := bytes.reader_to_stream(&r);
err = load_from_stream(&stream, buf, allocator);
err = load_from_stream(stream, buf, allocator);
return err;
}
@@ -111,18 +111,16 @@ load_from_file :: proc(filename: string, buf: ^bytes.Buffer, allocator := contex
data, ok := os.read_entire_file(filename, allocator);
defer delete(data);
err = E_General.File_Not_Found;
if ok {
err = load_from_slice(&data, buf, allocator);
return;
} else {
return E_General.File_Not_Found;
err = load_from_slice(data, buf, allocator);
}
return;
}
load_from_stream :: proc(stream: ^io.Stream, buf: ^bytes.Buffer, allocator := context.allocator) -> (err: Error) {
load_from_stream :: proc(stream: io.Stream, buf: ^bytes.Buffer, allocator := context.allocator) -> (err: Error) {
ctx := compress.Context{
input = stream^,
input = stream,
};
buf := buf;
ws := bytes.buffer_to_stream(buf);
+2 -2
View File
@@ -7,7 +7,7 @@ import "core:fmt"
main :: proc() {
ODIN_DEMO: []u8 = {
ODIN_DEMO := []u8{
120, 156, 101, 144, 77, 110, 131, 48, 16, 133, 215, 204, 41, 158, 44,
69, 73, 32, 148, 182, 75, 35, 14, 208, 125, 47, 96, 185, 195, 143,
130, 13, 50, 38, 81, 84, 101, 213, 75, 116, 215, 43, 246, 8, 53,
@@ -30,7 +30,7 @@ main :: proc() {
buf: bytes.Buffer;
// We can pass ", true" to inflate a raw DEFLATE stream instead of a ZLIB wrapped one.
err := zlib.inflate(&ODIN_DEMO, &buf);
err := zlib.inflate(ODIN_DEMO, &buf);
defer bytes.buffer_destroy(&buf);
if !zlib.is_kind(err, zlib.E_General.OK) {
+12 -11
View File
@@ -254,7 +254,6 @@ decode_huffman :: proc(z: ^Context, t: ^Huffman_Table) -> (r: u16, err: Error) #
}
parse_huffman_block :: proc(z: ^Context, z_repeat, z_offset: ^Huffman_Table) -> (err: Error) #no_bounds_check {
#no_bounds_check for {
value, e := decode_huffman(z, z_repeat);
if !is_kind(e, E_General.OK) {
@@ -449,7 +448,8 @@ inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) ->
// log.debugf("Final: %v | Type: %v\n", final, type);
if type == 0 {
switch type {
case 0:
// Uncompressed block
// Discard bits until next byte boundary
@@ -471,9 +471,9 @@ inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) ->
write_byte(z, u8(lit));
uncompressed_len -= 1;
}
} else if type == 3 {
case 3:
return E_Deflate.BType_3;
} else {
case:
// log.debugf("Err: %v | Final: %v | Type: %v\n", err, final, type);
if type == 1 {
// Use fixed code lengths.
@@ -525,17 +525,18 @@ inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) ->
} else {
fill := u8(0);
compress.refill_lsb(z, 7);
if c == 16 {
switch c {
case 16:
c = u16(compress.read_bits_no_refill_lsb(z, 2) + 3);
if n == 0 {
return E_Deflate.Huffman_Bad_Code_Lengths;
}
fill = lencodes[n - 1];
} else if c == 17 {
case 17:
c = u16(compress.read_bits_no_refill_lsb(z, 3) + 3);
} else if c == 18 {
case 18:
c = u16(compress.read_bits_no_refill_lsb(z, 7) + 11);
} else {
case:
return E_Deflate.Huffman_Bad_Code_Lengths;
}
@@ -577,11 +578,11 @@ inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) ->
return E_General.OK;
}
inflate_from_byte_array :: proc(input: ^[]u8, buf: ^bytes.Buffer, raw := false) -> (err: Error) {
inflate_from_byte_array :: proc(input: []u8, buf: ^bytes.Buffer, raw := false) -> (err: Error) {
ctx := Context{};
r := bytes.Reader{};
bytes.reader_init(&r, input^);
bytes.reader_init(&r, input);
rs := bytes.reader_to_stream(&r);
ctx.input = rs;
@@ -594,7 +595,7 @@ inflate_from_byte_array :: proc(input: ^[]u8, buf: ^bytes.Buffer, raw := false)
return err;
}
inflate_from_byte_array_raw :: proc(input: ^[]u8, buf: ^bytes.Buffer, raw := false) -> (err: Error) {
inflate_from_byte_array_raw :: proc(input: []u8, buf: ^bytes.Buffer, raw := false) -> (err: Error) {
return inflate_from_byte_array(input, buf, true);
}
+11 -16
View File
@@ -76,7 +76,8 @@ core_time :: proc(c: Chunk) -> (t: coretime.Time, ok: bool) {
using png_time;
return coretime.datetime_to_time(
int(year), int(month), int(day),
int(hour), int(minute), int(second));
int(hour), int(minute), int(second),
);
}
text :: proc(c: Chunk) -> (res: Text, ok: bool) {
@@ -104,7 +105,7 @@ text :: proc(c: Chunk) -> (res: Text, ok: bool) {
// Set up ZLIB context and decompress text payload.
buf: bytes.Buffer;
zlib_error := zlib.inflate_from_byte_array(&fields[2], &buf);
zlib_error := zlib.inflate_from_byte_array(fields[2], &buf);
defer bytes.buffer_destroy(&buf);
if !is_kind(zlib_error, E_General.OK) {
ok = false; return;
@@ -158,7 +159,7 @@ text :: proc(c: Chunk) -> (res: Text, ok: bool) {
} else {
// Set up ZLIB context and decompress text payload.
buf: bytes.Buffer;
zlib_error := zlib.inflate_from_byte_array(&rest, &buf);
zlib_error := zlib.inflate_from_byte_array(rest, &buf);
defer bytes.buffer_destroy(&buf);
if !is_kind(zlib_error, E_General.OK) {
@@ -171,7 +172,6 @@ text :: proc(c: Chunk) -> (res: Text, ok: bool) {
case:
// PNG text helper called with an unrecognized chunk type.
ok = false; return;
}
}
@@ -199,7 +199,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);
zlib_error := zlib.inflate_from_byte_array(fields[2], &buf);
if !is_kind(zlib_error, E_General.OK) {
bytes.buffer_destroy(&buf);
ok = false; return;
@@ -458,19 +458,14 @@ when false {
interlace_method = .None,
};
if channels == 1 {
ihdr.color_type = Color_Type{};
} else if channels == 2 {
ihdr.color_type = Color_Type{.Alpha};
} else if channels == 3 {
ihdr.color_type = Color_Type{.Color};
} else if channels == 4 {
ihdr.color_type = Color_Type{.Color, .Alpha};
} else {
// Unhandled
switch channels {
case 1: ihdr.color_type = Color_Type{};
case 2: ihdr.color_type = Color_Type{.Alpha};
case 3: ihdr.color_type = Color_Type{.Color};
case 4: ihdr.color_type = Color_Type{.Color, .Alpha};
case:// Unhandled
return E_PNG.Unknown_Color_Type;
}
h := make_chunk(ihdr, .IHDR);
write_chunk(fd, h);
+22 -21
View File
@@ -350,9 +350,9 @@ chunk_type_to_name :: proc(type: ^Chunk_Type) -> string {
return strings.string_from_ptr(t, 4);
}
load_from_slice :: proc(slice: ^[]u8, options: Options = {}, allocator := context.allocator) -> (img: ^Image, err: Error) {
load_from_slice :: proc(slice: []u8, options := Options{}, allocator := context.allocator) -> (img: ^Image, err: Error) {
r := bytes.Reader{};
bytes.reader_init(&r, slice^);
bytes.reader_init(&r, slice);
stream := bytes.reader_to_stream(&r);
/*
@@ -360,17 +360,17 @@ load_from_slice :: proc(slice: ^[]u8, options: Options = {}, allocator := contex
This way the stream reader could avoid the copy into the temp memory returned by it,
and instead return a slice into the original memory that's already owned by the caller.
*/
img, err = load_from_stream(&stream, options, allocator);
img, err = load_from_stream(stream, options, allocator);
return img, err;
}
load_from_file :: proc(filename: string, options: Options = {}, allocator := context.allocator) -> (img: ^Image, err: Error) {
load_from_file :: proc(filename: string, options := Options{}, allocator := context.allocator) -> (img: ^Image, err: Error) {
data, ok := os.read_entire_file(filename, allocator);
defer delete(data);
if ok {
img, err = load_from_slice(&data, options, allocator);
img, err = load_from_slice(data, options, allocator);
return;
} else {
img = new(Image);
@@ -378,7 +378,7 @@ load_from_file :: proc(filename: string, options: Options = {}, allocator := con
}
}
load_from_stream :: proc(stream: ^io.Stream, options: Options = {}, allocator := context.allocator) -> (img: ^Image, err: Error) {
load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := context.allocator) -> (img: ^Image, err: Error) {
options := options;
if .info in options {
options |= {.return_metadata, .do_not_decompress_image};
@@ -396,7 +396,7 @@ load_from_stream :: proc(stream: ^io.Stream, options: Options = {}, allocator :=
img.sidecar = nil;
ctx := compress.Context{
input = stream^,
input = stream,
};
signature, io_error := compress.read_data(&ctx, Signature);
@@ -669,7 +669,7 @@ load_from_stream :: proc(stream: ^io.Stream, options: Options = {}, allocator :=
}
buf: bytes.Buffer;
zlib_error := zlib.inflate(&idat, &buf);
zlib_error := zlib.inflate(idat, &buf);
defer bytes.buffer_destroy(&buf);
if !is_kind(zlib_error, E_General.OK) {
@@ -817,8 +817,7 @@ load_from_stream :: proc(stream: ^io.Stream, options: Options = {}, allocator :=
}
}
} else {
// This should be impossible.
assert(false);
unreachable();
}
img.pixels = t;
@@ -1181,13 +1180,13 @@ filter_paeth :: #force_inline proc(left, up, up_left: u8) -> u8 {
}
Filter_Params :: struct #packed {
src : []u8,
dest : []u8,
width : int,
height : int,
depth : int,
src: []u8,
dest: []u8,
width: int,
height: int,
depth: int,
channels: int,
rescale : bool,
rescale: bool,
}
depth_scale_table :: []u8{0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01};
@@ -1277,7 +1276,7 @@ defilter_less_than_8 :: proc(params: ^Filter_Params) -> (ok: bool) #no_bounds_ch
dest = dest[row_offset:];
filter := Row_Filter(src[0]); src = src[1:];
switch(filter) {
switch filter {
case .None:
copy(dest, src[:row_stride_in]);
case .Sub:
@@ -1334,7 +1333,8 @@ defilter_less_than_8 :: proc(params: ^Filter_Params) -> (ok: bool) #no_bounds_ch
for j := 0; j < height; j += 1 {
src = dest[row_offset:];
if depth == 4 {
switch depth {
case 4:
k := row_stride_out;
for ; k >= 2; k -= 2 {
c := src[0];
@@ -1347,7 +1347,7 @@ defilter_less_than_8 :: proc(params: ^Filter_Params) -> (ok: bool) #no_bounds_ch
dest[0] = scale * (c >> 4);
dest = dest[1:];
}
} else if depth == 2 {
case 2:
k := row_stride_out;
for ; k >= 4; k -= 4 {
c := src[0];
@@ -1368,7 +1368,7 @@ defilter_less_than_8 :: proc(params: ^Filter_Params) -> (ok: bool) #no_bounds_ch
}
dest = dest[k:];
}
} else if depth == 1 {
case 1:
k := row_stride_out;
for ; k >= 8; k -= 8 {
c := src[0];
@@ -1406,6 +1406,7 @@ defilter_less_than_8 :: proc(params: ^Filter_Params) -> (ok: bool) #no_bounds_ch
dest = dest[k:];
}
}
}
@@ -1429,7 +1430,7 @@ defilter_16 :: proc(params: ^Filter_Params) -> (ok: bool) {
nk := row_stride - stride;
filter := Row_Filter(src[0]); src = src[1:];
switch(filter) {
switch filter {
case .None:
copy(dest, src[:row_stride]);
case .Sub: