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; }