Skip to content

Commit

Permalink
image: add isView and tests for view
Browse files Browse the repository at this point in the history
  • Loading branch information
arrufat committed Dec 20, 2024
1 parent 2348b85 commit e9efaeb
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/image.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const assert = std.debug.assert;
const expectEqual = std.testing.expectEqual;
const expectEqual = std.testing.expectEqualDeep;
const expectEqualDeep = std.testing.expectEqualDeep;
const Allocator = std.mem.Allocator;

const as = @import("meta.zig").as;
Expand Down Expand Up @@ -56,6 +57,7 @@ pub fn Image(comptime T: type) type {
pub fn deinit(self: *Self, allocator: Allocator) void {
self.rows = 0;
self.cols = 0;
self.stride = 0;
allocator.free(self.data);
}

Expand Down Expand Up @@ -98,6 +100,12 @@ pub fn Image(comptime T: type) type {
};
}

/// Returns true if, and only if, self is not a view of another image. This is computed
/// by comparing the `rows` and `stride` fields.
pub fn isView(self: Self) bool {
return self.cols != self.stride;
}

/// Returns the value at position row, col. It assumes the coordinates are in bounds and
/// triggers safety-checked undefined behavior when they aren't.
pub inline fn at(self: Self, row: usize, col: usize) *T {
Expand Down Expand Up @@ -593,3 +601,13 @@ test "getRectangle" {
try expectEqual(rect.width(), image.cols);
try expectEqual(rect.height(), image.rows);
}

test "view" {
var image = try Image(Rgba).initAlloc(std.testing.allocator, 21, 13);
defer image.deinit(std.testing.allocator);
const rect: Rectangle(usize) = .{ .l = 0, .t = 0, .r = 8, .b = 10 };
const view = image.view(rect);
try expectEqual(view.isView(), true);
try expectEqual(image.isView(), false);
try expectEqualDeep(rect, view.getRectangle());
}

0 comments on commit e9efaeb

Please sign in to comment.