diff --git a/src/pixie/images.nim b/src/pixie/images.nim index 1a81499..0368570 100644 --- a/src/pixie/images.nim +++ b/src/pixie/images.nim @@ -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. diff --git a/src/pixie/masks.nim b/src/pixie/masks.nim index 4ee0059..4ff81ac 100644 --- a/src/pixie/masks.nim +++ b/src/pixie/masks.nim @@ -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.