Fix isOneColor.

This commit is contained in:
treeform 2021-08-17 20:42:13 -07:00
parent b84746f6f2
commit ec8f812e66

View file

@ -126,13 +126,34 @@ proc fill*(image: Image, color: SomeColor) {.inline.} =
## Fills the image with the parameter color. ## Fills the image with the parameter color.
fillUnsafe(image.data, color, 0, image.data.len) fillUnsafe(image.data, color, 0, image.data.len)
proc isOneColor(image: Image, color: ColorRGBX): bool = proc isOneColor*(image: Image): bool =
## Checks if the entire image is color. ## Checks if the entire image is the same color.
result = true result = true
let color = image.getRgbaUnsafe(0, 0)
var i: int var i: int
when defined(amd64) and not defined(pixieNoSimd): when defined(amd64) and not defined(pixieNoSimd):
let colorVec = mm_set1_epi32(cast[int32](color)) let colorVec = mm_set1_epi32(cast[int32](color))
for j in countup(0, image.data.len - 4, 4):
let
values = mm_loadu_si128(image.data[j].addr)
mask = mm_movemask_epi8(mm_cmpeq_epi8(values, colorVec))
if mask != uint16.high.int:
return false
i += 4
for j in i ..< image.data.len:
if image.data[j] != color:
return false
proc isTransparent*(image: Image): bool =
## Checks if this image is fully transparent or not.
result = true
var i: int
when defined(amd64) and not defined(pixieNoSimd):
let transparent = mm_setzero_si128()
for j in countup(0, image.data.len - 16, 16): for j in countup(0, image.data.len - 16, 16):
let let
values0 = mm_loadu_si128(image.data[j].addr) values0 = mm_loadu_si128(image.data[j].addr)
@ -142,23 +163,15 @@ proc isOneColor(image: Image, color: ColorRGBX): bool =
values01 = mm_or_si128(values0, values1) values01 = mm_or_si128(values0, values1)
values23 = mm_or_si128(values2, values3) values23 = mm_or_si128(values2, values3)
values = mm_or_si128(values01, values23) values = mm_or_si128(values01, values23)
mask = mm_movemask_epi8(mm_cmpeq_epi8(values, colorVec)) mask = mm_movemask_epi8(mm_cmpeq_epi8(values, transparent))
if mask != uint16.high.int: if mask != uint16.high.int:
return false return false
i += 16 i += 16
for j in i ..< image.data.len: for j in i ..< image.data.len:
if image.data[j] != color: if image.data[j].a != 0:
return false return false
proc isOneColor*(image: Image): bool =
## Checks if the entire image is the same color.
image.isOneColor(image.getRgbaUnsafe(0, 0))
proc isTransparent*(image: Image): bool =
## Checks if this image is fully transparent or not.
image.isOneColor(rgbx(0, 0, 0, 0))
proc flipHorizontal*(image: Image) = proc flipHorizontal*(image: Image) =
## Flips the image around the Y axis. ## Flips the image around the Y axis.
let w = image.width div 2 let w = image.width div 2