opaqueBounds
This commit is contained in:
parent
8bee289435
commit
4effe38236
4 changed files with 21 additions and 32 deletions
|
@ -751,15 +751,18 @@ proc superImage*(image: Image, x, y, w, h: int): Image {.raises: [PixieError].}
|
||||||
result = newImage(w, h)
|
result = newImage(w, h)
|
||||||
result.draw(image, translate(vec2(-x.float32, -y.float32)), OverwriteBlend)
|
result.draw(image, translate(vec2(-x.float32, -y.float32)), OverwriteBlend)
|
||||||
|
|
||||||
proc cropAlpha*(image: Image): (Image, Rect) =
|
proc opaqueBounds*(image: Image): Rect =
|
||||||
## Crops the alpha off the edges of an image.
|
## Returns the bounds of opaque pixels.
|
||||||
## Returns the new cropped image and the rectangle it used for cropping.
|
## Some images have transparency around them, use this to find just the
|
||||||
|
## visible part of the image and then use subImage to cut it out.
|
||||||
|
## Returns zero rect if whole image is transparent.
|
||||||
|
## Returns just the size of the image if no edge is transparent.
|
||||||
var
|
var
|
||||||
xMin = image.width
|
xMin = image.width
|
||||||
xMax = 0
|
xMax = 0
|
||||||
yMin = image.height
|
yMin = image.height
|
||||||
yMax = 0
|
yMax = 0
|
||||||
# Find the crop coordinates.
|
# Find the trim coordinates.
|
||||||
for y in 0 ..< image.height:
|
for y in 0 ..< image.height:
|
||||||
for x in 0 ..< image.width:
|
for x in 0 ..< image.width:
|
||||||
if image.unsafe[x, y].a != 0:
|
if image.unsafe[x, y].a != 0:
|
||||||
|
@ -768,18 +771,13 @@ proc cropAlpha*(image: Image): (Image, Rect) =
|
||||||
yMin = min(yMin, y)
|
yMin = min(yMin, y)
|
||||||
yMax = max(yMax, y + 1)
|
yMax = max(yMax, y + 1)
|
||||||
if xMax <= xMin or yMax <= yMin:
|
if xMax <= xMin or yMax <= yMin:
|
||||||
raise newException(PixieError, "Cannot cropAlpha fully transparent image")
|
return rect(0, 0, 0, 0)
|
||||||
let
|
rect(
|
||||||
corpImage = newImage(xMax - xMin, yMax - yMin)
|
xMin.float32,
|
||||||
cropRect = rect(
|
yMin.float32,
|
||||||
xMin.float32,
|
(xMax - xMin).float32,
|
||||||
yMin.float32,
|
(yMax - yMin).float32
|
||||||
corpImage.width.float32,
|
)
|
||||||
corpImage.height.float32
|
|
||||||
)
|
|
||||||
# Draw the bigger image into the cropped image.
|
|
||||||
corpImage.draw(image, translate(vec2(-xMin.float32, -yMin.float32)))
|
|
||||||
return (corpImage, cropRect)
|
|
||||||
|
|
||||||
when defined(release):
|
when defined(release):
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 1 KiB |
BIN
tests/images/opaqueBounds.png
Normal file
BIN
tests/images/opaqueBounds.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
|
@ -233,23 +233,15 @@ block:
|
||||||
doAssert image[10, 10] == rgba(255, 255, 255, 255)
|
doAssert image[10, 10] == rgba(255, 255, 255, 255)
|
||||||
|
|
||||||
block:
|
block:
|
||||||
# Make sure cropAlpha rases error.
|
# opaqueBounds of fully transparent image.
|
||||||
let image = newImage(100, 100)
|
let image = newImage(100, 100)
|
||||||
var hadError = false
|
doAssert image.opaqueBounds() == rect(0, 0, 0, 0)
|
||||||
try:
|
|
||||||
discard image.cropAlpha()
|
|
||||||
except PixieError:
|
|
||||||
hadError = true
|
|
||||||
doAssert hadError
|
|
||||||
|
|
||||||
block:
|
block:
|
||||||
# Make sure cropAlpha does nothing to full images.
|
# opaqueBounds of fully opaque image.
|
||||||
let image = newImage(100, 100)
|
let image = newImage(100, 100)
|
||||||
image.fill(rgbx(255, 255, 255, 255))
|
image.fill(rgbx(255, 255, 255, 255))
|
||||||
let (crop, rect) = image.cropAlpha()
|
doAssert image.opaqueBounds() == rect(0.0, 0.0, 100.0, 100.0)
|
||||||
doAssert crop.width == image.width
|
|
||||||
doAssert crop.height == image.height
|
|
||||||
doAssert rect == rect(0.0, 0.0, 100.0, 100.0)
|
|
||||||
|
|
||||||
block:
|
block:
|
||||||
let image = newImage(100, 100)
|
let image = newImage(100, 100)
|
||||||
|
@ -265,8 +257,7 @@ block:
|
||||||
parseHtmlColor("#FC427B").rgba,
|
parseHtmlColor("#FC427B").rgba,
|
||||||
scale(vec2(0.3, 0.3))
|
scale(vec2(0.3, 0.3))
|
||||||
)
|
)
|
||||||
let (crop, rect) = image.cropAlpha()
|
let rect = image.opaqueBounds()
|
||||||
doAssert crop.width == 48
|
|
||||||
doAssert crop.height == 48
|
|
||||||
doAssert rect == rect(6.0, 6.0, 48.0, 48.0)
|
doAssert rect == rect(6.0, 6.0, 48.0, 48.0)
|
||||||
crop.xray("tests/images/cropHeart.png")
|
let trimmedImage = image.subImage(rect.x.int, rect.y.int, rect.w.int, rect.h.int)
|
||||||
|
trimmedImage.xray("tests/images/opaqueBounds.png")
|
||||||
|
|
Loading…
Reference in a new issue