-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
204 lines (162 loc) · 5.44 KB
/
main.lua
File metadata and controls
204 lines (162 loc) · 5.44 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
function love.load(args)
moses = require('moses_min')
json = require('dkjson')
jsonData = {}
packingImage, width = verifyArguments(args)
cursorImage = nil
local resultingIslands = {}
images = {}
for x = 0, packingImage:getWidth() - 1 do
for y = 0, packingImage:getHeight() - 1 do
local r,g,b,a = packingImage:getData():getPixel(x, y)
if(a ~= 0)then
table.insert(resultingIslands, createImageDataFromPixelDump(generateIslandPixelDump(packingImage:getData(), x, y)))
end
end
end
moses.sort(resultingIslands, imageDataSortingHeuristic)
for k,v in pairs(resultingIslands) do
table.insert(images, love.graphics.newImage(v))
end
renderedResult = renderResult(determineRenderSheetSize())
local fileData = renderedResult:newImageData():encode('png','export.png')
success, message = love.filesystem.write('export.png', fileData, fileData:getSize())
if(not success)then
error(message)
end
fileData = json.encode(jsonData)
success, message = love.filesystem.write('export.json', fileData, string.len(fileData))
love.event.quit()
end
function verifyArguments(args)
local packingImage
if(args[2] == nil)then
error("No image supplied.")
end
packingImage = love.graphics.newImage(args[2])
if(packingImage == nil)then
error("Bad image.")
end
local width = tonumber(args[3]) or 1024
return packingImage, width
end
function generateIslandPixelDump(imageData, x, y)
local r,g,b,a = imageData:getPixel(x, y)
local result = {{x, y, r, g, b, a}}
imageData:setPixel(x, y, r, g, b, 0)
if(originx ~= x + 1 and x + 1 < imageData:getWidth()) then
r,g,b,a = imageData:getPixel(x + 1, y)
if(a ~= 0)then
result = moses.append(result, generateIslandPixelDump(imageData, x + 1, y))
end
end
if(originx ~= x - 1 and x - 1 >= 0) then
r,g,b,a = imageData:getPixel(x - 1, y)
if(a ~= 0)then
result = moses.append(result, generateIslandPixelDump(imageData, x - 1, y))
end
end
if(originy ~= y + 1 and y + 1 < imageData:getHeight()) then
r,g,b,a = imageData:getPixel(x, y + 1)
if(a ~= 0)then
result = moses.append(result, generateIslandPixelDump(imageData, x, y + 1))
end
end
if(originy ~= y - 1 and y - 1 >= 0) then
r,g,b,a = imageData:getPixel(x, y - 1)
if(a ~= 0)then
result = moses.append(result, generateIslandPixelDump(imageData, x, y - 1))
end
end
if(originx ~= x + 1 and originy ~= y + 1 and x + 1 < imageData:getWidth() and y + 1 < imageData:getHeight()) then
r,g,b,a = imageData:getPixel(x + 1, y + 1)
if(a ~= 0)then
result = moses.append(result, generateIslandPixelDump(imageData, x + 1, y + 1))
end
end
if(originx ~= x - 1 and originy ~= y - 1 and x - 1 >= 0 and y - 1 >= 0) then
r,g,b,a = imageData:getPixel(x - 1, y - 1)
if(a ~= 0)then
result = moses.append(result, generateIslandPixelDump(imageData, x - 1, y - 1))
end
end
if(originy ~= y + 1 and originx ~= x - 1 and x - 1 >= 0 and y + 1 < imageData:getHeight()) then
r,g,b,a = imageData:getPixel(x - 1, y + 1)
if(a ~= 0)then
result = moses.append(result, generateIslandPixelDump(imageData, x - 1, y + 1))
end
end
if(originy ~= y - 1 and originx ~= x + 1 and x + 1 < imageData:getWidth() and y - 1 >= 0) then
r,g,b,a = imageData:getPixel(x + 1, y - 1)
if(a ~= 0)then
result = moses.append(result, generateIslandPixelDump(imageData, x + 1, y - 1))
end
end
return result
end
function createImageDataFromPixelDump(pixelDump)
local pixXSort = moses.sort(moses.clone(pixelDump), function(a, b) return a[1] < b[1] end)
local pixYSort = moses.sort(moses.clone(pixelDump), function(a, b) return a[2] < b[2] end)
local xOffset = pixXSort[1][1]
local yOffset = pixYSort[1][2]
local width = math.abs(pixXSort[1][1] - pixXSort[#pixXSort][1]) + 1
local height = math.abs(pixYSort[1][2] - pixYSort[#pixYSort][2]) + 1
local newSprite = love.image.newImageData(width, height)
for k,v in pairs(pixelDump) do
newSprite:setPixel(v[1] - xOffset, v[2] - yOffset, v[3], v[4], v[5], v[6])
end
return newSprite
end
function imageDataSortingHeuristic(imageDataA, imageDataB)
return imageDataA:getWidth() * imageDataA:getHeight() < imageDataB:getWidth() * imageDataB:getHeight()
end
function determineRenderSheetSize()
local x, y = 0, 0
local offsetY = 0
local newY = 0
local sprite = 1
local spriteLimit = #images
love.graphics.setCanvas(canvas)
while(sprite <= spriteLimit) do
table.insert(jsonData, {index = sprite, x = x, y = y + offsetY, w = images[sprite]:getWidth(), h = images[sprite]:getHeight()})
x = x + images[sprite]:getWidth()
if(images[sprite]:getHeight() > newY)then
newY = images[sprite]:getHeight()
end
if(x >= width or ((sprite + 1 <= spriteLimit) and width - x < images[sprite + 1]:getWidth()))then
x = 0
y = offsetY + newY
offsetY = y
y = 0
newY = 0
end
sprite = sprite + 1
end
return width, offsetY + newY
end
function renderResult(canvasWidth, canvasHeight)
local canvas = love.graphics.newCanvas(canvasWidth, canvasHeight)
local x, y = 0, 0
local offsetY = 0
local newY = 0
local sprite = 1
local spriteLimit = #images
love.graphics.setCanvas(canvas)
while(sprite <= spriteLimit) do
love.graphics.draw(images[sprite],x,y + offsetY)
x = x + images[sprite]:getWidth()
if(images[sprite]:getHeight() > newY)then
newY = images[sprite]:getHeight()
end
if(x >= width or ((sprite + 1 <= spriteLimit) and width - x < images[sprite + 1]:getWidth()))then
x = 0
y = offsetY + newY
offsetY = y
y = 0
newY = 0
end
sprite = sprite + 1
end
love.graphics.setCanvas()
return canvas
end