summaryrefslogtreecommitdiff
path: root/src/jpg/segment.zig
blob: 9b5578047fae8d7ba05a2e3a19bfe86679675aa4 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const std = @import("std");
const jpg = @import("root.zig");

pub const Segment = union(enum) {
    temporary: void,
    reset: void,
    start_of_image: void,
    end_of_image: void,
    start_of_frame: void,
    define_quantization_table: void,
    define_huffman_table: void,
    define_restart_interval: void,
    define_number_of_lines: void,
    define_hierarchy_progression: void,
    extend_huffman_table: void,
    jfif: jpg.Jfif,
    application_segment: void,
    comment: void,
    start_of_scan: void,

    const Self = @This();

    pub fn read(gpa: std.mem.Allocator, reader: *std.Io.Reader) !Self {
        _ = gpa;

        if (try reader.peekByte() != 0xff) {
            return error.InvalidMarkerHeader;
        }
        reader.toss(1);

        const marker  = jpg.Marker.from_u8(try reader.peekByte())
            catch return error.InvalidMarkerByte;
        reader.toss(1);

        return switch (marker) {
            .temporary => .{ .temporary = void{} },
            .reset => .{ .reset = void{} },
            .start_of_image => .{ .start_of_image = void{} },
            .end_of_image => .{ .end_of_image = void{} },
            .application_segment => |n| switch (n) {
                0 => jfif: {
                    // NOTE: we can toss the length of this segment
                    //       since the JFIF segment has a predefined size
                    reader.toss(2);
                    break :jfif .{
                        .jfif = try jpg.Jfif.read(reader)
                    };
                },
                else => unreachable,
            },
            else => {
                std.debug.panic("unimplemented tag '{s}'\n", .{@tagName(marker)});
            },
        };
    }
};


test "file" {
    const buffer = @embedFile("../testing/rgb.jpg");
    var reader: std.Io.Reader = .fixed(buffer);

    var segment: Segment = try .read(std.testing.allocator, &reader);
    try std.testing.expectEqual(.start_of_image, std.meta.activeTag(segment));

    segment = try .read(std.testing.allocator, &reader);
    try std.testing.expectEqual(.jfif, std.meta.activeTag(segment));

    segment = try .read(std.testing.allocator, &reader);
    try std.testing.expectEqual(.jfif, std.meta.activeTag(segment));
}