diff options
Diffstat (limited to 'src/jpg/frame.zig')
| -rw-r--r-- | src/jpg/frame.zig | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/jpg/frame.zig b/src/jpg/frame.zig new file mode 100644 index 0000000..35a6e8e --- /dev/null +++ b/src/jpg/frame.zig @@ -0,0 +1,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; +} |