diff options
Diffstat (limited to 'src/bitmap.zig')
| -rw-r--r-- | src/bitmap.zig | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/bitmap.zig b/src/bitmap.zig new file mode 100644 index 0000000..9a9f7ac --- /dev/null +++ b/src/bitmap.zig @@ -0,0 +1,42 @@ +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); + } + }; +} |