From 520b548fc5b934cc477a2dd8412beb129049ba0b Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Sat, 11 Dec 2021 16:19:32 -0600 Subject: [PATCH] faster get/set unsafe --- src/pixie/images.nim | 28 ++++++++++++++-------------- src/pixie/masks.nim | 20 ++++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/pixie/images.nim b/src/pixie/images.nim index 529d066..6945fb4 100644 --- a/src/pixie/images.nim +++ b/src/pixie/images.nim @@ -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() diff --git a/src/pixie/masks.nim b/src/pixie/masks.nim index c09607e..525ee15 100644 --- a/src/pixie/masks.nim +++ b/src/pixie/masks.nim @@ -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