~kikoodx/chazo

cd24c595d8c8ca4e5a35c3d1163e0cd3888fdb8f — KikooDX 3 months ago 3943089
parser done
3 files changed, 47 insertions(+), 3 deletions(-)

M src/main.zig
A src/util.zig
M src/uwo.zig
M src/main.zig => src/main.zig +1 -1
@@ 9,5 9,5 @@ pub fn main() !void {
    defer image.deinit(allocator);

    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello, {s}!\n", .{"world"});
    try stdout.print("{}", .{image});
}

A src/util.zig => src/util.zig +3 -0
@@ 0,0 1,3 @@
pub fn mergeu8(a: u8, b: u8) u16 {
    return @as(u16, a) + @as(u16, b) * 256;
}

M src/uwo.zig => src/uwo.zig +43 -2
@@ 1,10 1,11 @@
const std = @import("std");
const util = @import("util.zig");
const Allocator = std.mem.Allocator;

const UwoError = error{
    FileTooSmall,
    InvalidMagic,
    LazyKikoo,
    UnexpectedFileSize,
};
const header_len = 8;



@@ 31,7 32,47 @@ pub const UwoImage = struct {
            return UwoError.InvalidMagic;
        }

        return UwoError.LazyKikoo;
        const width = util.mergeu8(buf[3], buf[4]);
        const height = util.mergeu8(buf[5], buf[6]);
        const size = @as(u64, width) * @as(u64, height);
        const map_size = buf[7];
        var buf_cursor: usize = header_len;

        const expected_file_size = header_len + if (map_size > 0) size + 3 * map_size else 3 * size;
        if (expected_file_size != file_size) {
            return UwoError.UnexpectedFileSize;
        }

        var image = UwoImage{
            .width = width,
            .height = height,
            .map_size = map_size,
            .map = undefined,
            .data = undefined,
        };
        image.data = try allocator.alloc(u8, if (map_size > 0) size else 3 * size);
        errdefer allocator.free(image.data);

        if (map_size > 0) {
            var i: usize = 0;
            while (i < map_size) : (i += 1) {
                image.map[i] = buf[buf_cursor + i];
            }
            buf_cursor += i;
            i = 0;
            while (i < size) : (i += 1) {
                image.data[i] = buf[buf_cursor + i];
            }
            buf_cursor += i;
        } else {
            var i: usize = 0;
            while (i < size * 3) : (i += 1) {
                image.data[i] = buf[buf_cursor + i];
            }
            buf_cursor += i;
        }

        return image;
    }

    pub fn deinit(this: UwoImage, allocator: Allocator) void {