Fix for Mask.mask* issue.

This commit is contained in:
treeform 2022-05-12 09:25:40 -07:00
parent 0d3d16efb0
commit e694f810d6
2 changed files with 8 additions and 10 deletions

View file

@ -11,8 +11,7 @@ type
width*, height*: int
data*: seq[ColorRGBX]
UnsafeImage = object
image: Image
UnsafeImage = distinct Image
when defined(release):
{.push checks: off.}
@ -62,21 +61,21 @@ proc dataIndex*(image: Image, x, y: int): int {.inline, raises: [].} =
image.width * y + x
template unsafe*(src: Image): UnsafeImage =
UnsafeImage(image: src)
cast[UnsafeImage](src)
template `[]`*(view: UnsafeImage, x, y: int): ColorRGBX =
## Gets a color from (x, y) coordinates.
## * No bounds checking *
## Make sure that x, y are in bounds.
## Failure in the assumptions will cause unsafe memory reads.
view.image.data[view.image.dataIndex(x, y)]
cast[Image](view).data[cast[Image](view).dataIndex(x, y)]
template `[]=`*(view: UnsafeImage, x, y: int, color: ColorRGBX) =
## Sets a color from (x, y) coordinates.
## * No bounds checking *
## Make sure that x, y are in bounds.
## Failure in the assumptions will cause unsafe memory writes.
view.image.data[view.image.dataIndex(x, y)] = color
cast[Image](view).data[cast[Image](view).dataIndex(x, y)] = color
proc `[]`*(image: Image, x, y: int): ColorRGBX {.inline, raises: [].} =
## Gets a pixel at (x, y) or returns transparent black if outside of bounds.

View file

@ -9,8 +9,7 @@ type
width*, height*: int
data*: seq[uint8]
UnsafeMask = object
mask*: Mask
UnsafeMask = distinct Mask
when defined(release):
{.push checks: off.}
@ -42,21 +41,21 @@ proc dataIndex*(mask: Mask, x, y: int): int {.inline, raises: [].} =
mask.width * y + x
template unsafe*(src: Mask): UnsafeMask =
UnsafeMask(mask: src)
cast[UnsafeMask](src)
template `[]`*(view: UnsafeMask, x, y: int): uint8 =
## Gets a value from (x, y) coordinates.
## * No bounds checking *
## Make sure that x, y are in bounds.
## Failure in the assumptions will case unsafe memory reads.
view.mask.data[view.mask.dataIndex(x, y)]
cast[Mask](view).data[cast[Mask](view).dataIndex(x, y)]
template `[]=`*(view: UnsafeMask, x, y: int, color: uint8) =
## Sets a value from (x, y) coordinates.
## * No bounds checking *
## Make sure that x, y are in bounds.
## Failure in the assumptions will case unsafe memory writes.
view.mask.data[view.mask.dataIndex(x, y)] = color
cast[Mask](view).data[cast[Mask](view).dataIndex(x, y)] = color
proc `[]`*(mask: Mask, x, y: int): uint8 {.inline, raises: [].} =
## Gets a value at (x, y) or returns transparent black if outside of bounds.