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); }