Split invert out.

This commit is contained in:
treeform 2022-03-18 22:24:46 -07:00
parent 42bedda0a7
commit 6068a92363
4 changed files with 30 additions and 33 deletions

View file

@ -434,45 +434,31 @@ proc applyOpacity*(target: Image | Mask, opacity: float32) {.raises: [].} =
for j in i ..< target.data.len:
target.data[j] = ((target.data[j] * opacity) div 255).uint8
proc invert*(target: Image | Mask) {.raises: [].} =
proc invert*(target: Image) {.raises: [].} =
## Inverts all of the colors and alpha.
var i: int
when defined(amd64) and not defined(pixieNoSimd):
let vec255 = mm_set1_epi8(cast[int8](255))
when type(target) is Image:
let byteLen = target.data.len * 4
else:
let byteLen = target.data.len
let byteLen = target.data.len * 4
for _ in 0 ..< byteLen div 16:
when type(target) is Image:
let index = i div 4
else:
let index = i
let index = i div 4
var values = mm_loadu_si128(target.data[index].addr)
values = mm_sub_epi8(vec255, values)
mm_storeu_si128(target.data[index].addr, values)
i += 16
when type(target) is Image:
for j in i div 4 ..< target.data.len:
var rgba = target.data[j]
rgba.r = 255 - rgba.r
rgba.g = 255 - rgba.g
rgba.b = 255 - rgba.b
rgba.a = 255 - rgba.a
target.data[j] = rgba
for j in i div 4 ..< target.data.len:
var rgba = target.data[j]
rgba.r = 255 - rgba.r
rgba.g = 255 - rgba.g
rgba.b = 255 - rgba.b
rgba.a = 255 - rgba.a
target.data[j] = rgba
# Inverting rgbx(50, 100, 150, 200) becomes rgbx(205, 155, 105, 55). This
# is not a valid premultiplied alpha color.
# We need to convert back to premultiplied alpha after inverting.
target.data.toPremultipliedAlpha()
else:
for j in i ..< target.data.len:
target.data[j] = (255 - target.data[j]).uint8
# Inverting rgbx(50, 100, 150, 200) becomes rgbx(205, 155, 105, 55). This
# is not a valid premultiplied alpha color.
# We need to convert back to premultiplied alpha after inverting.
target.data.toPremultipliedAlpha()
proc blur*(
image: Image, radius: float32, outOfBounds: SomeColor = color(0, 0, 0, 0)

View file

@ -234,17 +234,28 @@ proc getValueSmooth*(mask: Mask, x, y: float32): uint8 {.raises: [].} =
else:
topMix
proc invert(mask: Mask) {.raises: [].} =
## Makes inverts all values - creates a negative of the mask.
for i in 0 ..< mask.data.len:
mask.data[i] = 255 - mask.data[i]
proc invert*(mask: Mask) {.raises: [].} =
## Inverts all of the values - creates a negative of the mask.
var i: int
when defined(amd64) and not defined(pixieNoSimd):
let vec255 = mm_set1_epi8(cast[int8](255))
let byteLen = mask.data.len
for _ in 0 ..< byteLen div 16:
let index = i
var values = mm_loadu_si128(mask.data[index].addr)
values = mm_sub_epi8(vec255, values)
mm_storeu_si128(mask.data[index].addr, values)
i += 16
for j in i ..< mask.data.len:
mask.data[j] = (255 - mask.data[j]).uint8
proc spread*(mask: Mask, spread: float32) {.raises: [PixieError].} =
## Grows the mask by spread.
let spread = round(spread).int
if spread == 0:
return
if spread < 0:
elif spread < 0:
mask.invert()
spread(mask, -spread.float32)
mask.invert()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB