-
Notifications
You must be signed in to change notification settings - Fork 8
/
Starving_Artist_PNG_TO_ART_CONVERTER_SCRIPT.txt
213 lines (198 loc) · 7.14 KB
/
Starving_Artist_PNG_TO_ART_CONVERTER_SCRIPT.txt
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
205
206
207
208
209
210
211
212
213
local png = "sirme.png" --image in workspace folder
getfenv().bit32 = bit
local Unfilter = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Modules/Unfilter.lua"))()
local BinaryReader = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Modules/BinaryReader.lua"))()
local Deflate = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Modules/Deflate.lua"))()
local PNG = {}
PNG.__index = PNG
local chunks = {
IDAT = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Chunks/IDAT.lua"))(),
IEND = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Chunks/IEND.lua"))(),
IHDR = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Chunks/IHDR.lua"))(),
PLTE = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Chunks/PLTE.lua"))(),
bKGD = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Chunks/bKGD.lua"))(),
cHRM = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Chunks/cHRM.lua"))(),
gAMA = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Chunks/gAMA.lua"))(),
sRGB = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Chunks/sRGB.lua"))(),
tEXt = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Chunks/tEXt.lua"))(),
tIME = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Chunks/tIME.lua"))(),
tRNS = loadstring(game:HttpGet("https://raw.githubusercontent.com/CloneTrooper1019/Roblox-PNG-Library/master/Chunks/tRNS.lua"))()
}
local function getBytesPerPixel(colorType)
if colorType == 0 or colorType == 3 then
return 1
elseif colorType == 4 then
return 2
elseif colorType == 2 then
return 3
elseif colorType == 6 then
return 4
else
return 0
end
end
local function clampInt(value, min, max)
local num = tonumber(value) or 0
num = math.floor(num + 0.5)
return math.clamp(num, min, max)
end
local function indexBitmap(file, x, y)
local width = file.Width
local height = file.Height
local x = clampInt(x, 1, width)
local y = clampInt(y, 1, height)
local bitmap = file.Bitmap
local bpp = file.BytesPerPixel
local i0 = ((x - 1) * bpp) + 1
local i1 = i0 + bpp
return bitmap[y], i0, i1
end
function PNG:GetPixel(x, y)
local row, i0, i1 = indexBitmap(self, x, y)
local colorType = self.ColorType
local color, alpha
do
if colorType == 0 then
local gray = unpack(row, i0, i1)
color = Color3.fromHSV(0, 0, gray)
alpha = 255
elseif colorType == 2 then
local r, g, b = unpack(row, i0, i1)
color = Color3.fromRGB(r, g, b)
alpha = 255
elseif colorType == 3 then
local palette = self.Palette
local alphaData = self.AlphaData
local index = unpack(row, i0, i1)
index = index + 1
if palette then
color = palette[index]
end
if alphaData then
alpha = alphaData[index]
end
elseif colorType == 4 then
local gray, a = unpack(row, i0, i1)
color = Color3.fromHSV(0, 0, gray)
alpha = a
elseif colorType == 6 then
local r, g, b, a = unpack(row, i0, i1)
color = Color3.fromRGB(r, g, b, a)
alpha = a
end
end
if not color then
color = Color3.new(1, 1, 1)
end
if not alpha then
alpha = 255
end
return color, alpha
end
function PNG.new(buffer)
local reader = BinaryReader.new(buffer)
local file = {
Chunks = {},
Metadata = {},
Reading = true,
ZlibStream = ""
}
local header = reader:ReadString(8)
if header ~= "\137PNG\r\n\26\n" then
error("PNG - Input data is not a PNG file.", 2)
end
while file.Reading do
local length = reader:ReadInt32()
local chunkType = reader:ReadString(4)
print(length, chunkType)
local data, crc
if length > 0 then
data = reader:ForkReader(length)
crc = reader:ReadUInt32()
end
local chunk = {
Length = length,
Type = chunkType,
Data = data,
CRC = crc
}
local handler = chunks[chunkType]
if handler then
handler(file, chunk)
end
table.insert(file.Chunks, chunk)
end
local success, response = pcall(function()
local result = {}
local index = 0
Deflate:InflateZlib({
Input = BinaryReader.new(file.ZlibStream),
Output = function(byte)
index = index + 1
result[index] = string.char(byte)
end
})
return table.concat(result)
end)
if not success then
error("PNG - Unable to unpack PNG data. " .. tostring(response), 2)
end
local width = file.Width
local height = file.Height
local bitDepth = file.BitDepth
local colorType = file.ColorType
local buffer = BinaryReader.new(response)
file.ZlibStream = nil
local bitmap = {}
file.Bitmap = bitmap
local channels = getBytesPerPixel(colorType)
file.NumChannels = channels
local bpp = math.max(1, channels * (bitDepth / 8))
file.BytesPerPixel = bpp
for row = 1, height do
wait()
local filterType = buffer:ReadByte()
local scanline = buffer:ReadBytes(width * bpp, true)
bitmap[row] = {}
if filterType == 0 then
Unfilter:None(scanline, bitmap, bpp, row)
elseif filterType == 1 then
Unfilter:Sub(scanline, bitmap, bpp, row)
elseif FilterType == 2 then
Unfilter:Up(scanline, bitmap, bpp, row)
elseif FilterType == 3 then
Unfilter:Average(scanline, bitmap, bpp, row)
elseif FilterType == 4 then
Unfilter:Paeth(scanline, bitmap, bpp, row)
end
end
return setmetatable(file, PNG)
end
local function to_hex(color)
return string.format("%02X%02X%02X", color.R * 0xFF,
color.G * 0xFF, color.B * 0xFF)
end
local cells = {}
warn("Converting art, please wait!")
local i = 1;
local buf = readfile(png)
png = PNG.new(buf)
for x = 1, png.Width do
for y = 1, png.Height do
local color, a = png:GetPixel(x, y)
if a ~= 0 then
table.insert(cells, i, to_hex(color));
i = i+1;
end
end
end
warn("Done converting!")
-- total squares = 1024
-- 32 x 32
local args =
{
["FrameColor"] = "000000",
["Name"] = "Sir",
["Cells"] = cells
}
game:GetService("ReplicatedStorage").Remotes.CreateArt:InvokeServer(args)