From cf7ce1c44199f91ba33999424d1232e74158dad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Arrufat?= Date: Thu, 12 Dec 2024 14:30:57 +0900 Subject: [PATCH] image: make sure rectangle in view stays in bounds --- src/image.zig | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/image.zig b/src/image.zig index bf730e2..eebb8ef 100644 --- a/src/image.zig +++ b/src/image.zig @@ -75,15 +75,20 @@ pub fn Image(comptime T: type) type { return self.rows == other.rows and self.cols == other.cols and self.data.len == other.data.len; } - /// Returns an image view with boundaries defined by `rect`. The returned image - /// references the memory of `self`, so there are no allocations or copies. + /// Returns an image view with boundaries defined by `rect` within the image boundaries. + /// The returned image references the memory of `self`, so there are no allocations + /// or copies. pub fn view(self: Self, rect: Rectangle(usize)) Image(T) { - assert(rect.r < self.cols); - assert(rect.b < self.rows); + const bounded = Rectangle(usize){ + .l = rect.l, + .t = rect.t, + .r = @min(rect.r, self.cols - 1), + .b = @min(rect.b, self.rows - 1), + }; return .{ - .rows = rect.height(), - .cols = rect.width(), - .data = self.data[rect.t * self.stride + rect.l .. rect.b * self.stride + rect.r + 1], + .rows = bounded.height(), + .cols = bounded.width(), + .data = self.data[bounded.t * self.stride + bounded.l .. bounded.b * self.stride + bounded.r + 1], .stride = self.cols, }; }