const std = @import("std"); const Allocator = std.mem.Allocator; pub fn Bitmap(PixelType: type) type { return struct { const Self = @This(); pixels: []PixelType, width: usize, height: usize, /// Creates a new Bitmap instance and allocates the `pixels` buffer. pub fn init_undefined(gpa: Allocator, width: usize, height: usize) !Self { const self: Self = .{ .width = width, .height = height, .pixels = try gpa.alloc(PixelType, width * height), }; return self; } pub fn from_buffer(buffer: []PixelType, width: usize, height: usize) Self { return .{ .width = width, .height = height, .pixels = buffer, }; } pub inline fn pixel(self: *Self, x: usize, y: usize) ?*PixelType { return if (x < self.width and y < self.height) self.pixels[x + self.width * y] else null; } pub fn deinit(self: Self, gpa: Allocator) void { gpa.free(self.pixels); } }; }