opaqueBounds
This commit is contained in:
parent
8bee289435
commit
4effe38236
|
@ -751,15 +751,18 @@ proc superImage*(image: Image, x, y, w, h: int): Image {.raises: [PixieError].}
|
|||
result = newImage(w, h)
|
||||
result.draw(image, translate(vec2(-x.float32, -y.float32)), OverwriteBlend)
|
||||
|
||||
proc cropAlpha*(image: Image): (Image, Rect) =
|
||||
## Crops the alpha off the edges of an image.
|
||||
## Returns the new cropped image and the rectangle it used for cropping.
|
||||
proc opaqueBounds*(image: Image): Rect =
|
||||
## Returns the bounds of opaque pixels.
|
||||
## 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
|
||||
xMin = image.width
|
||||
xMax = 0
|
||||
yMin = image.height
|
||||
yMax = 0
|
||||
# Find the crop coordinates.
|
||||
# Find the trim coordinates.
|
||||
for y in 0 ..< image.height:
|
||||
for x in 0 ..< image.width:
|
||||
if image.unsafe[x, y].a != 0:
|
||||
|
@ -768,18 +771,13 @@ proc cropAlpha*(image: Image): (Image, Rect) =
|
|||
yMin = min(yMin, y)
|
||||
yMax = max(yMax, y + 1)
|
||||
if xMax <= xMin or yMax <= yMin:
|
||||
raise newException(PixieError, "Cannot cropAlpha fully transparent image")
|
||||
let
|
||||
corpImage = newImage(xMax - xMin, yMax - yMin)
|
||||
cropRect = rect(
|
||||
xMin.float32,
|
||||
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)
|
||||
return rect(0, 0, 0, 0)
|
||||
rect(
|
||||
xMin.float32,
|
||||
yMin.float32,
|
||||
(xMax - xMin).float32,
|
||||
(yMax - yMin).float32
|
||||
)
|
||||
|
||||
when defined(release):
|
||||
{.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)
|
||||
|
||||
block:
|
||||
# Make sure cropAlpha rases error.
|
||||
# opaqueBounds of fully transparent image.
|
||||
let image = newImage(100, 100)
|
||||
var hadError = false
|
||||
try:
|
||||
discard image.cropAlpha()
|
||||
except PixieError:
|
||||
hadError = true
|
||||
doAssert hadError
|
||||
doAssert image.opaqueBounds() == rect(0, 0, 0, 0)
|
||||
|
||||
block:
|
||||
# Make sure cropAlpha does nothing to full images.
|
||||
# opaqueBounds of fully opaque image.
|
||||
let image = newImage(100, 100)
|
||||
image.fill(rgbx(255, 255, 255, 255))
|
||||
let (crop, rect) = image.cropAlpha()
|
||||
doAssert crop.width == image.width
|
||||
doAssert crop.height == image.height
|
||||
doAssert rect == rect(0.0, 0.0, 100.0, 100.0)
|
||||
doAssert image.opaqueBounds() == rect(0.0, 0.0, 100.0, 100.0)
|
||||
|
||||
block:
|
||||
let image = newImage(100, 100)
|
||||
|
@ -265,8 +257,7 @@ block:
|
|||
parseHtmlColor("#FC427B").rgba,
|
||||
scale(vec2(0.3, 0.3))
|
||||
)
|
||||
let (crop, rect) = image.cropAlpha()
|
||||
doAssert crop.width == 48
|
||||
doAssert crop.height == 48
|
||||
let rect = image.opaqueBounds()
|
||||
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