diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-03-18 22:23:53 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-03-18 22:24:10 +0100 |
| commit | 1e7b589e77c7935cbaa8381f50cdcb86c85fa532 (patch) | |
| tree | 6a087761a4b09cd01caff8ae51fb1971d9b0ba8a /src/bitmap.zig | |
Implement first structure of jpg and farbfeld
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); + } + }; +} |