summaryrefslogtreecommitdiff
path: root/src/farbfeld.zig
blob: dbdf3385e699b2aca90e382a18679e33a7959ee0 (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
const std = @import("std");
const Allocator = std.mem.Allocator;

const picasso = @import("root.zig");
const Rgba = picasso.color.Rgba;
const Bitmap = picasso.Bitmap;

pub const Image = struct {
    const Self = @This();

    pub const ReadError = error {
        InvalidMagicValue,
        InvalidSize,
    } || std.Io.Reader.Error || Allocator.Error;

    bitmap: Bitmap(Rgba(16)),

    pub fn read(gpa: Allocator, reader: *std.Io.Reader) ReadError!Self {
        if (!std.mem.eql(u8, "farbfeld", try reader.takeArray(8))) {
            return ReadError.InvalidMagicValue;
        }

        var self: Self = undefined;

        const width = try reader.takeInt(u32, .big);
        const height = try reader.takeInt(u32, .big);

        self.bitmap = try .init_undefined(gpa, width, height);
        errdefer self.bitmap.deinit(gpa);

        for (self.bitmap.pixels) |*pixel| {
            pixel.* = try reader.takeStruct(Rgba(16), .big);
        }

        return self;
    }

    pub fn deinit(self: Self, gpa: Allocator) void {
        self.bitmap.deinit(gpa);
    }
};

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

    const image: Image = try .read(gpa, &reader);
    defer image.deinit(gpa);

    try std.testing.expectEqual(600, image.bitmap.width);
    try std.testing.expectEqual(450, image.bitmap.height);
}