Skip to content

Commit

Permalink
image: add initFromBytes and toBytes methods
Browse files Browse the repository at this point in the history
  • Loading branch information
arrufat committed Oct 29, 2024
1 parent 50b4689 commit 929ff86
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/image.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,27 @@ pub fn Image(comptime T: type) type {
return .{ .rows = rows, .cols = cols, .data = data };
}

/// Constructs an image of rows and cols size allocating its own memory.
pub fn initAlloc(allocator: std.mem.Allocator, rows: usize, cols: usize) !Image(T) {
var array = std.ArrayList(T).init(allocator);
try array.resize(rows * cols);
return .{ .rows = rows, .cols = cols, .data = try array.toOwnedSlice() };
}

/// Contructs an image of rows and cols size reinterpreting the slice of bytes as a slice of T.
pub fn initFromBytes(rows: usize, cols: usize, bytes: []u8) Image(T) {
return .{
.rows = rows,
.cols = cols,
.data = @as([*]T, @ptrCast(@alignCast(bytes.ptr)))[0 .. bytes.len / @sizeOf(T)],
};
}

/// Returns the image data reinterpreted as a slice of bytes
pub fn toBytes(self: Self) []u8 {
return @as([*]u8, @ptrCast(@alignCast(self.data.ptr)))[0 .. self.data.len * @sizeOf(T)];
}

/// Sets the image rows and cols to zero and frees the memory from the image. It should
/// only be called if the image owns the memory.
pub fn deinit(self: *Self, allocator: Allocator) void {
Expand Down

0 comments on commit 929ff86

Please sign in to comment.