faster get/set unsafe

This commit is contained in:
Ryan Oldenburg 2021-12-11 16:19:32 -06:00
parent 431c279a2a
commit 520b548fc5
2 changed files with 24 additions and 24 deletions

View file

@ -63,31 +63,31 @@ proc getRgbaUnsafe*(image: Image, x, y: int): ColorRGBX {.inline, raises: [].} =
## * No bounds checking *
## Make sure that x, y are in bounds.
## Failure in the assumptions will cause unsafe memory reads.
result = image.data[image.width * y + x]
image.data[image.dataIndex(x, y)]
proc setRgbaUnsafe*(
image: Image, x, y: int, color: ColorRGBX
) {.inline, raises: [].} =
## 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.
image.data[image.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.
if image.inside(x, y):
return image.getRgbaUnsafe(x, y)
proc getColor*(image: Image, x, y: int): Color {.inline, raises: [].} =
## Gets a color at (x, y) or returns transparent black if outside of bounds.
image[x, y].color()
proc setRgbaUnsafe*(
image: Image, x, y: int, color: SomeColor
) {.inline, raises: [].} =
## 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.
image.data[image.dataIndex(x, y)] = color.asRgbx()
proc `[]=`*(image: Image, x, y: int, color: SomeColor) {.inline, raises: [].} =
## Sets a pixel at (x, y) or does nothing if outside of bounds.
if image.inside(x, y):
image.setRgbaUnsafe(x, y, color.asRgbx())
proc getColor*(image: Image, x, y: int): Color {.inline, raises: [].} =
## Gets a color at (x, y) or returns transparent black if outside of bounds.
image[x, y].color()
proc setColor*(image: Image, x, y: int, color: Color) {.inline, raises: [].} =
## Sets a color at (x, y) or does nothing if outside of bounds.
image[x, y] = color.rgbx()

View file

@ -43,16 +43,7 @@ proc getValueUnsafe*(mask: Mask, x, y: int): uint8 {.inline, raises: [].} =
## * No bounds checking *
## Make sure that x, y are in bounds.
## Failure in the assumptions will case unsafe memory reads.
result = mask.data[mask.width * y + x]
proc `[]`*(mask: Mask, x, y: int): uint8 {.inline, raises: [].} =
## Gets a value at (x, y) or returns transparent black if outside of bounds.
if mask.inside(x, y):
return mask.getValueUnsafe(x, y)
proc getValue*(mask: Mask, x, y: int): uint8 {.inline, raises: [].} =
## Gets a value at (x, y) or returns transparent black if outside of bounds.
mask[x, y]
result = mask.data[mask.dataIndex(x, y)]
proc setValueUnsafe*(mask: Mask, x, y: int, value: uint8) {.inline, raises: [].} =
## Sets a value from (x, y) coordinates.
@ -61,11 +52,20 @@ proc setValueUnsafe*(mask: Mask, x, y: int, value: uint8) {.inline, raises: [].}
## Failure in the assumptions will case unsafe memory writes.
mask.data[mask.dataIndex(x, y)] = value
proc `[]`*(mask: Mask, x, y: int): uint8 {.inline, raises: [].} =
## Gets a value at (x, y) or returns transparent black if outside of bounds.
if mask.inside(x, y):
return mask.getValueUnsafe(x, y)
proc `[]=`*(mask: Mask, x, y: int, value: uint8) {.inline, raises: [].} =
## Sets a value at (x, y) or does nothing if outside of bounds.
if mask.inside(x, y):
mask.setValueUnsafe(x, y, value)
proc getValue*(mask: Mask, x, y: int): uint8 {.inline, raises: [].} =
## Gets a value at (x, y) or returns transparent black if outside of bounds.
mask[x, y]
proc setValue*(mask: Mask, x, y: int, value: uint8) {.inline, raises: [].} =
## Sets a value at (x, y) or does nothing if outside of bounds.
mask[x, y] = value