Merge pull request #342 from guzba/master

[get/set]RgbaUnsafe, slow before faster after
This commit is contained in:
treeform 2021-12-11 15:53:27 -08:00 committed by GitHub
commit 0b1fe8378e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 * ## * No bounds checking *
## Make sure that x, y are in bounds. ## Make sure that x, y are in bounds.
## Failure in the assumptions will cause unsafe memory reads. ## 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: [].} = proc `[]`*(image: Image, x, y: int): ColorRGBX {.inline, raises: [].} =
## Gets a pixel at (x, y) or returns transparent black if outside of bounds. ## Gets a pixel at (x, y) or returns transparent black if outside of bounds.
if image.inside(x, y): if image.inside(x, y):
return image.getRgbaUnsafe(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: [].} = proc `[]=`*(image: Image, x, y: int, color: SomeColor) {.inline, raises: [].} =
## Sets a pixel at (x, y) or does nothing if outside of bounds. ## Sets a pixel at (x, y) or does nothing if outside of bounds.
if image.inside(x, y): if image.inside(x, y):
image.setRgbaUnsafe(x, y, color.asRgbx()) 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: [].} = proc setColor*(image: Image, x, y: int, color: Color) {.inline, raises: [].} =
## Sets a color at (x, y) or does nothing if outside of bounds. ## Sets a color at (x, y) or does nothing if outside of bounds.
image[x, y] = color.rgbx() image[x, y] = color.rgbx()

View file

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