summaryrefslogtreecommitdiff
path: root/src/jpg/huffman-table.zig
blob: 8d653ab475aab9784a5fd11270e05ac164148412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const std = @import("std");

const Self = @This();

class: u4,
destination_identifier: u4,
codes_count: [16]u8,
values: [16][]u8,

pub fn read(gpa: std.mem.Allocator, reader: *std.Io.Reader) !Self {
    const kind = try reader.takeByte();
    var self: Self = undefined;

    self.class = @truncate(kind >> 4);
    self.destination_identifier = @truncate(kind);

    for (self.codes_count[0..]) |*count| {
        count.* = try reader.takeByte();
    }

    for (self.values[0..], self.codes_count) |*values, count| {
        values.* = try gpa.alloc(u8, count);

        for (values.*) |*value| {
            value.* = try reader.takeByte();
        }
    }

    return self;
}

pub fn deinit(self: *Self, gpa: std.mem.Allocator) void {
    for (self.values) |value| {
        gpa.free(value);
    }

    self.* = undefined;
}

pub fn size_in_file(self: Self) usize {
    var size: usize = 17;

    for (self.codes_count) |count| {
        size += count;
    }

    return size;
}