-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.lua
40 lines (33 loc) · 1.32 KB
/
util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
-- Copyright (c) 2021 EngineerSmith
-- Under the MIT license, see license suppiled with this file
local util = {}
util.getImageDimensions = function(image)
if image:typeOf("Texture") then
return image:getPixelDimensions()
else
return image:getDimensions()
end
end
-- TODO: Remove closure usage
local fillImageData = function(imageData, x, y, w, h, r, g, b, a)
imageData:mapPixel(function()
return r, g, b, a
end, x, y, w, h)
end
local extrudeImageData = function(dest, src, n, x, y, dx, dy, sx, sy, sw, sh)
for i = 1, n do
dest:paste(src, x + i * dx, y + i * dy, sx, sy, sw, sh)
end
end
util.extrudeWithFill = function(dest, src, n, x, y)
local iw, ih = src:getDimensions()
extrudeImageData(dest, src, n, x, y, 0, -1, 0, 0, iw, 1) -- top
extrudeImageData(dest, src, n, x, y, -1, 0, 0, 0, 1, ih) -- left
extrudeImageData(dest, src, n, x, y + ih - 1, 0, 1, 0, ih - 1, iw, 1) -- bottom
extrudeImageData(dest, src, n, x + iw - 1, y, 1, 0, iw - 1, 0, 1, ih) -- right
fillImageData(dest, x - n, y - n, n, n, src:getPixel(0, 0)) -- top-left
fillImageData(dest, x + iw, y - n, n, n, src:getPixel(iw - 1, 0)) -- top-right
fillImageData(dest, x + iw, y + ih, n, n, src:getPixel(iw - 1, ih - 1)) -- bottom-right
fillImageData(dest, x - n, y + ih, n, n, src:getPixel(0, ih - 1)) -- bottom-left
end
return util