From dcd6a4c298208d021471e7891d7f096a9407b038 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Tue, 28 Nov 2023 16:03:37 +0500 Subject: [PATCH] boiler to manage scan encoding --- src/output.zig | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/output.zig b/src/output.zig index c48b9f6..95d4603 100644 --- a/src/output.zig +++ b/src/output.zig @@ -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