boiler to manage scan encoding

This commit is contained in:
Muaz Ahmad 2023-11-28 16:03:37 +05:00
parent 6e27ca09d5
commit dcd6a4c298

View file

@ -2,9 +2,53 @@ const std = @import("std");
const util = @import("util.zig");
const RLE_Seq = std.ArrayList(RLE_Unit);
const RLE_Unit = struct {
symbol: u8,
value: i16,
};
const Huffman = std.AutoHashMap(u8, HuffCode);
const HuffCode = struct {
n_bits: u8,
value: u16,
};
const Scan = struct {
arena: std.heap.ArenaAllocator,
diffs: [3]i16,
rles: [3]RLE_Seq,
freqs: [4][]u32,
huffs: [4]Huffman,
const Self = @This();
fn init(alloc_: std.mem.Allocator) !Self {
var arena = std.heap.ArenaAllocator.init(alloc_);
var alloc = arena.allocator();
return Self{ .arena = arena, .diffs = [3]i16{ 0, 0, 0 }, .rles = [3]RLE_Seq{
RLE_Seq.init(alloc),
RLE_Seq.init(alloc),
RLE_Seq.init(alloc),
}, .freqs = [4][]u32{ try alloc.alloc(u32, 257), try alloc.alloc(u32, 257), try alloc.alloc(u32, 257), try alloc.alloc(u32, 257) }, .huffs = [4]Huffman{
Huffman.init(alloc),
Huffman.init(alloc),
Huffman.init(alloc),
Huffman.init(alloc),
} };
}
fn deinit(self: *Self) void {
self.arena.deinit();
}
};
pub fn generate_jpg(buff: util.Buffers, alloc: std.mem.Allocator) !void {
_ = alloc;
_ = buff;
var scan_data = try Scan.init(alloc);
defer scan_data.deinit();
scan_data.do_rle_freq_pass(buff);
// rle, huffman pass
// file headers
// quant + huffman write