summaryrefslogtreecommitdiff
path: root/src/jpg/frame.zig
blob: 35a6e8ed6cb3ff21e1379de0e144a22b663f9a39 (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
49
50
const std = @import("std");

const Self = @This();

pub const Component = packed struct(u24) {
    identifier: u8,
    horizontal_sampling_factor: u4,
    vertical_sampling_factor: u4,
    quantization_table_destination_selector: u8,
};

index: usize,
sample_precision: u8,
number_of_lines: u16,
samples_per_line: u16,
components: []Component,


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

    self.index = index;
    self.sample_precision = try reader.takeByte();
    self.number_of_lines = try reader.takeInt(u16, .big);
    self.samples_per_line = try reader.takeInt(u16, .big);

    const number_of_components = try reader.takeByte();

    self.components = try gpa.alloc(Component, number_of_components);
    errdefer gpa.free(self.components);

    for (self.components) |*component| {
        component.identifier = try reader.takeByte();
        const sampling = try reader.takeByte();
        component.horizontal_sampling_factor = @truncate(sampling >> 4);
        component.vertical_sampling_factor = @truncate(sampling);
        component.quantization_table_destination_selector = try reader.takeByte();
    }

    return self;
}

pub fn deinit(self: *Self, gpa: std.mem.Allocator) void {
    gpa.free(self.components);
    self.* = undefined;
}