Minor formatting changes

This commit is contained in:
gingerBill
2021-06-14 11:30:00 +01:00
parent 86649e6b44
commit 6f745677b4
5 changed files with 372 additions and 383 deletions
+6 -11
View File
@@ -145,7 +145,7 @@ build_huffman :: proc(z: ^Huffman_Table, code_lengths: []u8) -> (err: Error) {
mem.zero_slice(sizes[:]); mem.zero_slice(sizes[:]);
mem.zero_slice(z.fast[:]); mem.zero_slice(z.fast[:]);
for v, _ in code_lengths { for v in code_lengths {
sizes[v] += 1; sizes[v] += 1;
} }
sizes[0] = 0; sizes[0] = 0;
@@ -163,7 +163,7 @@ build_huffman :: proc(z: ^Huffman_Table, code_lengths: []u8) -> (err: Error) {
z.firstsymbol[i] = u16(k); z.firstsymbol[i] = u16(k);
code = code + sizes[i]; code = code + sizes[i];
if sizes[i] != 0 { if sizes[i] != 0 {
if (code - 1 >= (1 << u16(i))) { if code - 1 >= (1 << u16(i)) {
return E_Deflate.Huffman_Bad_Code_Lengths; return E_Deflate.Huffman_Bad_Code_Lengths;
} }
} }
@@ -181,7 +181,7 @@ build_huffman :: proc(z: ^Huffman_Table, code_lengths: []u8) -> (err: Error) {
fastv := u16((u16(v) << 9) | u16(ci)); fastv := u16((u16(v) << 9) | u16(ci));
z.size[c] = u8(v); z.size[c] = u8(v);
z.value[c] = u16(ci); z.value[c] = u16(ci);
if (v <= ZFAST_BITS) { if v <= ZFAST_BITS {
j := z_bit_reverse(u16(next_code[v]), v); j := z_bit_reverse(u16(next_code[v]), v);
for j < (1 << ZFAST_BITS) { for j < (1 << ZFAST_BITS) {
z.fast[j] = fastv; z.fast[j] = fastv;
@@ -195,15 +195,10 @@ build_huffman :: proc(z: ^Huffman_Table, code_lengths: []u8) -> (err: Error) {
} }
decode_huffman_slowpath :: proc(z: ^Context, t: ^Huffman_Table) -> (r: u16, err: Error) #no_bounds_check { decode_huffman_slowpath :: proc(z: ^Context, t: ^Huffman_Table) -> (r: u16, err: Error) #no_bounds_check {
r = 0;
err = nil;
k: int;
s: u8;
code := u16(compress.peek_bits_lsb(z, 16)); code := u16(compress.peek_bits_lsb(z, 16));
k = int(z_bit_reverse(code, 16)); k := int(z_bit_reverse(code, 16));
s: u8;
#no_bounds_check for s = HUFFMAN_FAST_BITS+1; ; { #no_bounds_check for s = HUFFMAN_FAST_BITS+1; ; {
if k < t.maxcode[s] { if k < t.maxcode[s] {
@@ -211,7 +206,7 @@ decode_huffman_slowpath :: proc(z: ^Context, t: ^Huffman_Table) -> (r: u16, err:
} }
s += 1; s += 1;
} }
if (s >= 16) { if s >= 16 {
return 0, E_Deflate.Bad_Huffman_Code; return 0, E_Deflate.Bad_Huffman_Code;
} }
// code size is s, so: // code size is s, so:
+1 -3
View File
@@ -127,7 +127,6 @@ Error :: enum {
*/ */
compute_buffer_size :: proc(width, height, channels, depth: int, extra_row_bytes := int(0)) -> (size: int) { compute_buffer_size :: proc(width, height, channels, depth: int, extra_row_bytes := int(0)) -> (size: int) {
size = ((((channels * width * depth) + 7) >> 3) + extra_row_bytes) * height; size = ((((channels * width * depth) + 7) >> 3) + extra_row_bytes) * height;
return; return;
} }
@@ -144,7 +143,6 @@ Channel :: enum u8 {
} }
return_single_channel :: proc(img: ^Image, channel: Channel) -> (res: ^Image, ok: bool) { return_single_channel :: proc(img: ^Image, channel: Channel) -> (res: ^Image, ok: bool) {
ok = false; ok = false;
t: bytes.Buffer; t: bytes.Buffer;
@@ -159,7 +157,7 @@ return_single_channel :: proc(img: ^Image, channel: Channel) -> (res: ^Image, ok
return {}, false; return {}, false;
} }
switch(img.depth) { switch img.depth {
case 8: case 8:
buffer_size := compute_buffer_size(img.width, img.height, 1, 8); buffer_size := compute_buffer_size(img.width, img.height, 1, 8);
t = bytes.Buffer{}; t = bytes.Buffer{};
+1 -1
View File
@@ -35,7 +35,7 @@ main :: proc() {
// Handle ancillary chunks as you wish. // Handle ancillary chunks as you wish.
// We provide helper functions for a few types. // We provide helper functions for a few types.
for c in v.chunks { for c in v.chunks {
#partial switch (c.header.type) { #partial switch c.header.type {
case .tIME: case .tIME:
t, _ := png.core_time(c); t, _ := png.core_time(c);
fmt.printf("[tIME]: %v\n", t); fmt.printf("[tIME]: %v\n", t);
+19 -23
View File
@@ -236,10 +236,7 @@ ADAM7_Y_SPACING := []int{ 8,8,8,4,4,2,2 };
// Implementation starts here // Implementation starts here
read_chunk :: proc(ctx: ^compress.Context) -> (Chunk, Error) { read_chunk :: proc(ctx: ^compress.Context) -> (chunk: Chunk, err: Error) {
chunk := Chunk{};
ch, e := compress.read_data(ctx, Chunk_Header); ch, e := compress.read_data(ctx, Chunk_Header);
if e != .None { if e != .None {
return {}, E_General.Stream_Too_Short; return {}, E_General.Stream_Too_Short;
@@ -271,7 +268,6 @@ read_chunk :: proc(ctx: ^compress.Context) -> (Chunk, Error) {
} }
read_header :: proc(ctx: ^compress.Context) -> (IHDR, Error) { read_header :: proc(ctx: ^compress.Context) -> (IHDR, Error) {
c, e := read_chunk(ctx); c, e := read_chunk(ctx);
if e != nil { if e != nil {
return {}, e; return {}, e;
@@ -297,7 +293,7 @@ read_header :: proc(ctx: ^compress.Context) -> (IHDR, Error) {
} }
switch (transmute(u8)color_type) { switch transmute(u8)color_type {
case 0: case 0:
/* /*
Grayscale. Grayscale.
@@ -398,11 +394,11 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
img.sidecar = nil; img.sidecar = nil;
ctx := compress.Context{ ctx := &compress.Context{
input = stream, input = stream,
}; };
signature, io_error := compress.read_data(&ctx, Signature); signature, io_error := compress.read_data(ctx, Signature);
if io_error != .None || signature != .PNG { if io_error != .None || signature != .PNG {
return img, E_PNG.Invalid_PNG_Signature; return img, E_PNG.Invalid_PNG_Signature;
} }
@@ -435,11 +431,11 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
read_error: io.Error; read_error: io.Error;
// 12 bytes is the size of a chunk with a zero-length payload. // 12 bytes is the size of a chunk with a zero-length payload.
for (read_error == .None && !seen_iend) { for read_error == .None && !seen_iend {
// Peek at next chunk's length and type. // Peek at next chunk's length and type.
// TODO: Some streams may not provide seek/read_at // TODO: Some streams may not provide seek/read_at
ch, e = compress.peek_data(&ctx, Chunk_Header); ch, e = compress.peek_data(ctx, Chunk_Header);
if e != .None { if e != .None {
return img, E_General.Stream_Too_Short; return img, E_General.Stream_Too_Short;
} }
@@ -452,7 +448,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
} }
seen_ihdr = true; seen_ihdr = true;
header, err = read_header(&ctx); header, err = read_header(ctx);
if err != nil { if err != nil {
return img, err; return img, err;
} }
@@ -505,7 +501,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
return img, E_PNG.PLTE_Encountered_Unexpectedly; return img, E_PNG.PLTE_Encountered_Unexpectedly;
} }
c, err = read_chunk(&ctx); c, err = read_chunk(ctx);
if err != nil { if err != nil {
return img, err; return img, err;
} }
@@ -540,7 +536,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
next := ch.type; next := ch.type;
for next == .IDAT { for next == .IDAT {
c, err = read_chunk(&ctx); c, err = read_chunk(ctx);
if err != nil { if err != nil {
return img, err; return img, err;
} }
@@ -548,7 +544,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
bytes.buffer_write(&idat_b, c.data); bytes.buffer_write(&idat_b, c.data);
idat_length += c.header.length; idat_length += c.header.length;
ch, e = compress.peek_data(&ctx, Chunk_Header); ch, e = compress.peek_data(ctx, Chunk_Header);
if e != .None { if e != .None {
return img, E_General.Stream_Too_Short; return img, E_General.Stream_Too_Short;
} }
@@ -560,7 +556,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
} }
seen_idat = true; seen_idat = true;
case .IEND: case .IEND:
c, err = read_chunk(&ctx); c, err = read_chunk(ctx);
if err != nil { if err != nil {
return img, err; return img, err;
} }
@@ -569,7 +565,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 // TODO: Make sure that 16-bit bKGD + tRNS chunks return u16 instead of u16be
c, err = read_chunk(&ctx); c, err = read_chunk(ctx);
if err != nil { if err != nil {
return img, err; return img, err;
} }
@@ -604,7 +600,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
img.background = [3]u16{u16(col[0]), u16(col[1]), u16(col[2])}; img.background = [3]u16{u16(col[0]), u16(col[1]), u16(col[2])};
} }
case .tRNS: case .tRNS:
c, err = read_chunk(&ctx); c, err = read_chunk(ctx);
if err != nil { if err != nil {
return img, err; return img, err;
} }
@@ -646,7 +642,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
return img, E_PNG.PNG_Does_Not_Adhere_to_Spec; return img, E_PNG.PNG_Does_Not_Adhere_to_Spec;
case: case:
// Unhandled type // Unhandled type
c, err = read_chunk(&ctx); c, err = read_chunk(ctx);
if err != nil { if err != nil {
return img, err; return img, err;
} }
@@ -695,7 +691,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
for p := 0; p < 7; p += 1 { for p := 0; p < 7; p += 1 {
x := (int(header.width) - ADAM7_X_ORIG[p] + ADAM7_X_SPACING[p] - 1) / ADAM7_X_SPACING[p]; x := (int(header.width) - ADAM7_X_ORIG[p] + ADAM7_X_SPACING[p] - 1) / ADAM7_X_SPACING[p];
y := (int(header.height) - ADAM7_Y_ORIG[p] + ADAM7_Y_SPACING[p] - 1) / ADAM7_Y_SPACING[p]; y := (int(header.height) - ADAM7_Y_ORIG[p] + ADAM7_Y_SPACING[p] - 1) / ADAM7_Y_SPACING[p];
if (x > 0 && y > 0) { if x > 0 && y > 0 {
expected_size += compute_buffer_size(int(x), int(y), int(img.channels), int(header.bit_depth), 1); expected_size += compute_buffer_size(int(x), int(y), int(img.channels), int(header.bit_depth), 1);
} }
} }
@@ -854,7 +850,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
p16 := mem.slice_data_cast([]u16, temp.buf[:]); p16 := mem.slice_data_cast([]u16, temp.buf[:]);
o16 := mem.slice_data_cast([]u16, t.buf[:]); o16 := mem.slice_data_cast([]u16, t.buf[:]);
switch (raw_image_channels) { switch raw_image_channels {
case 1: case 1:
// Gray without Alpha. Might have tRNS alpha. // Gray without Alpha. Might have tRNS alpha.
key := u16(0); key := u16(0);
@@ -1051,7 +1047,7 @@ load_from_stream :: proc(stream: io.Stream, options := Options{}, allocator := c
p := mem.slice_data_cast([]u8, temp.buf[:]); p := mem.slice_data_cast([]u8, temp.buf[:]);
o := mem.slice_data_cast([]u8, t.buf[:]); o := mem.slice_data_cast([]u8, t.buf[:]);
switch (raw_image_channels) { switch raw_image_channels {
case 1: case 1:
// Gray without Alpha. Might have tRNS alpha. // Gray without Alpha. Might have tRNS alpha.
key := u8(0); key := u8(0);
@@ -1276,7 +1272,7 @@ defilter_8 :: proc(params: ^Filter_Params) -> (ok: bool) {
nk := row_stride - channels; nk := row_stride - channels;
filter := Row_Filter(src[0]); src = src[1:]; filter := Row_Filter(src[0]); src = src[1:];
switch(filter) { switch filter {
case .None: case .None:
copy(dest, src[:row_stride]); copy(dest, src[:row_stride]);
case .Sub: case .Sub:
@@ -1590,7 +1586,7 @@ defilter :: proc(img: ^Image, filter_bytes: ^bytes.Buffer, header: ^IHDR, option
i,j,x,y: int; i,j,x,y: int;
x = (width - ADAM7_X_ORIG[p] + ADAM7_X_SPACING[p] - 1) / ADAM7_X_SPACING[p]; x = (width - ADAM7_X_ORIG[p] + ADAM7_X_SPACING[p] - 1) / ADAM7_X_SPACING[p];
y = (height - ADAM7_Y_ORIG[p] + ADAM7_Y_SPACING[p] - 1) / ADAM7_Y_SPACING[p]; y = (height - ADAM7_Y_ORIG[p] + ADAM7_Y_SPACING[p] - 1) / ADAM7_Y_SPACING[p];
if (x > 0 && y > 0) { if x > 0 && y > 0 {
temp: bytes.Buffer; temp: bytes.Buffer;
temp_len := compute_buffer_size(x, y, channels, depth == 16 ? 16 : 8); temp_len := compute_buffer_size(x, y, channels, depth == 16 ? 16 : 8);
resize(&temp.buf, temp_len); resize(&temp.buf, temp_len);