ToxicTheBoss

PNG bypass

May 23rd, 2020
1,497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.15 KB | None | 0 0
  1. -- // Config: \\ --
  2.  
  3. local pixelsize = 1; -- Pixel size, keep at 1 for smoothness.
  4. local pngimage = game:HttpGet("https://redphoenix.xyz/images/BRUH.png"); -- Your png link.
  5. local holder = Instance.new("ScreenGui",game.CoreGui); -- Any GUI object
  6. local transparentcolor = Color3.fromRGB(255,255,255); -- Transparent objects render as white, set a specific color for them and it should work out. Set the variable to "nil" to
  7.  
  8. -- // Positional Config: \\ --
  9.  
  10. local anchor = Vector2.new(.5,.5); -- The anchor point of the loaded PNG frame
  11. local position = UDim2.new(.5,0,.5,0); -- The position of the loaded PNG frame
  12.  
  13.  
  14. -- // Do not modify below unless you know what you're doing. \\ --
  15.  
  16. holder = Instance.new("Frame",holder);
  17. holder.AnchorPoint = anchor;
  18. holder.Position = position;
  19. holder.BackgroundTransparency = 1;
  20.  
  21. local layout = Instance.new("UIListLayout",holder);
  22. layout.VerticalAlignment = Enum.VerticalAlignment.Top;
  23. layout.FillDirection = Enum.FillDirection.Horizontal;
  24.  
  25. local pixel = Instance.new("Frame")
  26. pixel.Size = UDim2.new(0,pixelsize,0,pixelsize);
  27. pixel.Name = 'Pixel';
  28. pixel.Parent = workspace;
  29. pixel.BorderSizePixel = 0;
  30.  
  31. local Row = Instance.new("Frame")
  32. local UIListLayout = Instance.new("UIListLayout")
  33.  
  34. Row.Name = "Row"
  35. Row.Parent = workspace;
  36. Row.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
  37. Row.BackgroundTransparency = 1
  38. Row.Selectable = true
  39. Row.Size = UDim2.new(0, pixelsize, 1, 0)
  40.  
  41. UIListLayout.Parent = Row
  42. UIListLayout.SortOrder = Enum.SortOrder.LayoutOrder
  43.  
  44. local row = Row;
  45. local http = game:GetService("HttpService")
  46.  
  47. local PNG = {}
  48. PNG.__index = PNG
  49.  
  50. local chunks = {};
  51. local function IDAT(file, chunk)
  52. local crc = chunk.CRC
  53. local hash = file.Hash or 0
  54.  
  55. local data = chunk.Data
  56. local buffer = data.Buffer
  57.  
  58. file.Hash = bit32.bxor(hash, crc)
  59. file.ZlibStream = file.ZlibStream .. buffer
  60. end
  61. chunks['IDAT'] = IDAT;
  62.  
  63. local function IEND(file)
  64. file.Reading = nil
  65. end
  66. chunks['IEND'] = IEND;
  67.  
  68. local function IHDR(file, chunk)
  69. local data = chunk.Data
  70.  
  71. file.Width = data:ReadInt32();
  72. file.Height = data:ReadInt32();
  73.  
  74. file.BitDepth = data:ReadByte();
  75. file.ColorType = data:ReadByte();
  76.  
  77. file.Methods =
  78. {
  79. Compression = data:ReadByte();
  80. Filtering = data:ReadByte();
  81. Interlace = data:ReadByte();
  82. }
  83. end
  84.  
  85.  
  86. chunks['IHDR'] = IHDR;
  87.  
  88. local function PLTE(file, chunk)
  89. if not file.Palette then
  90. file.Palette = {}
  91. end
  92.  
  93. local data = chunk.Data
  94. local palette = data:ReadAllBytes()
  95.  
  96. if #palette % 3 ~= 0 then
  97. error("PNG - Invalid PLTE chunk.")
  98. end
  99.  
  100. for i = 1, #palette, 3 do
  101. local r = palette[i]
  102. local g = palette[i + 1]
  103. local b = palette[i + 2]
  104.  
  105. local color = Color3.fromRGB(r, g, b)
  106. local index = #file.Palette + 1
  107.  
  108. file.Palette[index] = color
  109. end
  110. end
  111.  
  112.  
  113. chunks['PLTE'] = PLTE;
  114.  
  115. local function bKGD(file, chunk)
  116. local data = chunk.Data
  117.  
  118. local bitDepth = file.BitDepth
  119. local colorType = file.ColorType
  120.  
  121. bitDepth = (2 ^ bitDepth) - 1
  122.  
  123. if colorType == 3 then
  124. local index = data:ReadByte()
  125. file.BackgroundColor = file.Palette[index]
  126. elseif colorType == 0 or colorType == 4 then
  127. local gray = data:ReadUInt16() / bitDepth
  128. file.BackgroundColor = Color3.fromHSV(0, 0, gray)
  129. elseif colorType == 2 or colorType == 6 then
  130. local r = data:ReadUInt16() / bitDepth
  131. local g = data:ReadUInt16() / bitDepth
  132. local b = data:ReadUInt16() / bitDepth
  133. file.BackgroundColor = Color3.new(r, g, b)
  134. end
  135. end
  136.  
  137. chunks['bKGD'] = bKGD;
  138.  
  139. local colors = {"White", "Red", "Green", "Blue"}
  140.  
  141. local function cHRM(file, chunk)
  142. local chrome = {}
  143. local data = chunk.Data
  144.  
  145. for i = 1, 4 do
  146. local color = colors[i]
  147.  
  148. chrome[color] =
  149. {
  150. [1] = data:ReadUInt32() / 10e4;
  151. [2] = data:ReadUInt32() / 10e4;
  152. }
  153. end
  154.  
  155. file.Chromaticity = chrome
  156. end
  157.  
  158. chunks['cHRM'] = cHRM;
  159.  
  160. local function gAMA(file, chunk)
  161. local data = chunk.Data
  162. local value = data:ReadUInt32()
  163. file.Gamma = value / 10e4
  164. end
  165.  
  166. chunks['gAMA'] = gAMA;
  167.  
  168. local function sRGB(file, chunk)
  169. local data = chunk.Data
  170. file.RenderIntent = data:ReadByte()
  171. end
  172.  
  173. chunks['sRGB'] = sRGB;
  174.  
  175. local function tEXt(file, chunk)
  176. local data = chunk.Data
  177. local key, value = "", ""
  178.  
  179. for byte in data:IterateBytes() do
  180. local char = string.char(byte)
  181.  
  182. if char == '\0' then
  183. key = value
  184. value = ""
  185. else
  186. value = value .. char
  187. end
  188. end
  189.  
  190. file.Metadata[key] = value
  191. end
  192.  
  193. chunks['tEXt'] = tEXt;
  194.  
  195. local function tIME(file, chunk)
  196. local data = chunk.Data
  197.  
  198. local timeStamp =
  199. {
  200. Year = data:ReadUInt16();
  201. Month = data:ReadByte();
  202. Day = data:ReadByte();
  203.  
  204. Hour = data:ReadByte();
  205. Minute = data:ReadByte();
  206. Second = data:ReadByte();
  207. }
  208.  
  209. file.TimeStamp = timeStamp
  210. end
  211.  
  212. chunks['tIME'] = tIME;
  213.  
  214. local function tRNS(file, chunk)
  215. local data = chunk.Data
  216.  
  217. local bitDepth = file.BitDepth
  218. local colorType = file.ColorType
  219.  
  220. bitDepth = (2 ^ bitDepth) - 1
  221.  
  222. if colorType == 3 then
  223. local palette = file.Palette
  224. local alphaMap = {}
  225.  
  226. for i = 1, #palette do
  227. local alpha = data:ReadByte()
  228.  
  229. if not alpha then
  230. alpha = 255
  231. end
  232.  
  233. alphaMap[i] = alpha
  234. end
  235.  
  236. file.AlphaData = alphaMap
  237. elseif colorType == 0 then
  238. local grayAlpha = data:ReadUInt16()
  239. file.Alpha = grayAlpha / bitDepth
  240. elseif colorType == 2 then
  241. -- TODO: This seems incorrect...
  242. local r = data:ReadUInt16() / bitDepth
  243. local g = data:ReadUInt16() / bitDepth
  244. local b = data:ReadUInt16() / bitDepth
  245. file.Alpha = Color3.new(r, g, b)
  246. else
  247. error("PNG - Invalid tRNS chunk")
  248. end
  249. end
  250.  
  251.  
  252. chunks['tRNS'] = tRNS;
  253.  
  254. --[[
  255.  
  256. LUA MODULE
  257.  
  258. compress.deflatelua - deflate (and zlib) implemented in Lua.
  259.  
  260. DESCRIPTION
  261.  
  262. This is a pure Lua implementation of decompressing the DEFLATE format,
  263. including the related zlib format.
  264.  
  265. Note: This library only supports decompression.
  266. Compression is not currently implemented.
  267.  
  268. REFERENCES
  269.  
  270. [1] DEFLATE Compressed Data Format Specification version 1.3
  271. http://tools.ietf.org/html/rfc1951
  272. [2] GZIP file format specification version 4.3
  273. http://tools.ietf.org/html/rfc1952
  274. [3] http://en.wikipedia.org/wiki/DEFLATE
  275. [4] pyflate, by Paul Sladen
  276. http://www.paul.sladen.org/projects/pyflate/
  277. [5] Compress::Zlib::Perl - partial pure Perl implementation of
  278. Compress::Zlib
  279. http://search.cpan.org/~nwclark/Compress-Zlib-Perl/Perl.pm
  280.  
  281. LICENSE
  282.  
  283. (c) 2008-2011 David Manura. Licensed under the same terms as Lua (MIT).
  284. Heavily modified by Max G. (2019)
  285.  
  286. Permission is hereby granted, free of charge, to any person obtaining a copy
  287. of this software and associated documentation files (the "Software"), to deal
  288. in the Software without restriction, including without limitation the rights
  289. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  290. copies of the Software, and to permit persons to whom the Software is
  291. furnished to do so, subject to the following conditions:
  292.  
  293. The above copyright notice and this permission notice shall be included in
  294. all copies or substantial portions of the Software.
  295.  
  296. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  297. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  298. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  299. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  300. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  301. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  302. THE SOFTWARE.
  303. (end license)
  304. --]]
  305.  
  306. local Deflate = {}
  307.  
  308. local band = bit32.band
  309. local lshift = bit32.lshift
  310. local rshift = bit32.rshift
  311.  
  312. local BTYPE_NO_COMPRESSION = 0
  313. local BTYPE_FIXED_HUFFMAN = 1
  314. local BTYPE_DYNAMIC_HUFFMAN = 2
  315.  
  316. local lens = -- Size base for length codes 257..285
  317. {
  318. [0] = 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  319. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258
  320. }
  321.  
  322. local lext = -- Extra bits for length codes 257..285
  323. {
  324. [0] = 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  325. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
  326. }
  327.  
  328. local dists = -- Offset base for distance codes 0..29
  329. {
  330. [0] = 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  331. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  332. 8193, 12289, 16385, 24577
  333. }
  334.  
  335. local dext = -- Extra bits for distance codes 0..29
  336. {
  337. [0] = 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  338. 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  339. 12, 12, 13, 13
  340. }
  341.  
  342. local order = -- Permutation of code length codes
  343. {
  344. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5,
  345. 11, 4, 12, 3, 13, 2, 14, 1, 15
  346. }
  347.  
  348. -- Fixed literal table for BTYPE_FIXED_HUFFMAN
  349. local fixedLit = {0, 8, 144, 9, 256, 7, 280, 8, 288}
  350.  
  351. -- Fixed distance table for BTYPE_FIXED_HUFFMAN
  352. local fixedDist = {0, 5, 32}
  353.  
  354. local function createState(bitStream)
  355. local state =
  356. {
  357. Output = bitStream;
  358. Window = {};
  359. Pos = 1;
  360. }
  361.  
  362. return state
  363. end
  364.  
  365. local function write(state, byte)
  366. local pos = state.Pos
  367. state.Output(byte)
  368. state.Window[pos] = byte
  369. state.Pos = pos % 32768 + 1 -- 32K
  370. end
  371.  
  372. local function memoize(fn)
  373. local meta = {}
  374. local memoizer = setmetatable({}, meta)
  375.  
  376. function meta:__index(k)
  377. local v = fn(k)
  378. memoizer[k] = v
  379.  
  380. return v
  381. end
  382.  
  383. return memoizer
  384. end
  385.  
  386. -- small optimization (lookup table for powers of 2)
  387. local pow2 = memoize(function (n)
  388. return 2 ^ n
  389. end)
  390.  
  391. -- weak metatable marking objects as bitstream type
  392. local isBitStream = setmetatable({}, { __mode = 'k' })
  393.  
  394. local function createBitStream(reader)
  395. local buffer = 0
  396. local bitsLeft = 0
  397.  
  398. local stream = {}
  399. isBitStream[stream] = true
  400.  
  401. function stream:GetBitsLeft()
  402. return bitsLeft
  403. end
  404.  
  405. function stream:Read(count)
  406. count = count or 1
  407.  
  408. while bitsLeft < count do
  409. local byte = reader:ReadByte()
  410.  
  411. if not byte then
  412. return
  413. end
  414.  
  415. buffer = buffer + lshift(byte, bitsLeft)
  416. bitsLeft = bitsLeft + 8
  417. end
  418.  
  419. local bits
  420.  
  421. if count == 0 then
  422. bits = 0
  423. elseif count == 32 then
  424. bits = buffer
  425. buffer = 0
  426. else
  427. bits = band(buffer, rshift(2^32 - 1, 32 - count))
  428. buffer = rshift(buffer, count)
  429. end
  430.  
  431. bitsLeft = bitsLeft - count
  432. return bits
  433. end
  434.  
  435. return stream
  436. end
  437.  
  438. local function getBitStream(obj)
  439. if isBitStream[obj] then
  440. return obj
  441. end
  442.  
  443. return createBitStream(obj)
  444. end
  445.  
  446. local function sortHuffman(a, b)
  447. return a.NumBits == b.NumBits and a.Value < b.Value or a.NumBits < b.NumBits
  448. end
  449.  
  450. local function msb(bits, numBits)
  451. local res = 0
  452.  
  453. for i = 1, numBits do
  454. res = lshift(res, 1) + band(bits, 1)
  455. bits = rshift(bits, 1)
  456. end
  457.  
  458. return res
  459. end
  460.  
  461. local function createHuffmanTable(init, isFull)
  462. local hTable = {}
  463.  
  464. if isFull then
  465. for val, numBits in pairs(init) do
  466. if numBits ~= 0 then
  467. hTable[#hTable + 1] =
  468. {
  469. Value = val;
  470. NumBits = numBits;
  471. }
  472. end
  473. end
  474. else
  475. for i = 1, #init - 2, 2 do
  476. local firstVal = init[i]
  477.  
  478. local numBits = init[i + 1]
  479. local nextVal = init[i + 2]
  480.  
  481. if numBits ~= 0 then
  482. for val = firstVal, nextVal - 1 do
  483. hTable[#hTable + 1] =
  484. {
  485. Value = val;
  486. NumBits = numBits;
  487. }
  488. end
  489. end
  490. end
  491. end
  492.  
  493. table.sort(hTable, sortHuffman)
  494.  
  495. local code = 1
  496. local numBits = 0
  497.  
  498. for i, slide in ipairs(hTable) do
  499. if slide.NumBits ~= numBits then
  500. code = code * pow2[slide.NumBits - numBits]
  501. numBits = slide.NumBits
  502. end
  503.  
  504. slide.Code = code
  505. code = code + 1
  506. end
  507.  
  508. local minBits = math.huge
  509. local look = {}
  510.  
  511. for i, slide in ipairs(hTable) do
  512. minBits = math.min(minBits, slide.NumBits)
  513. look[slide.Code] = slide.Value
  514. end
  515.  
  516. local firstCode = memoize(function (bits)
  517. return pow2[minBits] + msb(bits, minBits)
  518. end)
  519.  
  520. function hTable:Read(bitStream)
  521. local code = 1 -- leading 1 marker
  522. local numBits = 0
  523.  
  524. while true do
  525. if numBits == 0 then -- small optimization (optional)
  526. local index = bitStream:Read(minBits)
  527. numBits = numBits + minBits
  528. code = firstCode[index]
  529. else
  530. local bit = bitStream:Read()
  531. numBits = numBits + 1
  532. code = code * 2 + bit -- MSB first
  533. end
  534.  
  535. local val = look[code]
  536.  
  537. if val then
  538. return val
  539. end
  540. end
  541. end
  542.  
  543. return hTable
  544. end
  545.  
  546. local function parseZlibHeader(bitStream)
  547. -- Compression Method
  548. local cm = bitStream:Read(4)
  549.  
  550. -- Compression info
  551. local cinfo = bitStream:Read(4)
  552.  
  553. -- FLaGs: FCHECK (check bits for CMF and FLG)
  554. local fcheck = bitStream:Read(5)
  555.  
  556. -- FLaGs: FDICT (present dictionary)
  557. local fdict = bitStream:Read(1)
  558.  
  559. -- FLaGs: FLEVEL (compression level)
  560. local flevel = bitStream:Read(2)
  561.  
  562. -- CMF (Compresion Method and flags)
  563. local cmf = cinfo * 16 + cm
  564.  
  565. -- FLaGs
  566. local flg = fcheck + fdict * 32 + flevel * 64
  567.  
  568. if cm ~= 8 then -- not "deflate"
  569. error("unrecognized zlib compression method: " .. cm)
  570. end
  571.  
  572. if cinfo > 7 then
  573. error("invalid zlib window size: cinfo=" .. cinfo)
  574. end
  575.  
  576. local windowSize = 2 ^ (cinfo + 8)
  577.  
  578. if (cmf * 256 + flg) % 31 ~= 0 then
  579. error("invalid zlib header (bad fcheck sum)")
  580. end
  581.  
  582. if fdict == 1 then
  583. error("FIX:TODO - FDICT not currently implemented")
  584. end
  585.  
  586. return windowSize
  587. end
  588.  
  589. local function parseHuffmanTables(bitStream)
  590. local numLits = bitStream:Read(5) -- # of literal/length codes - 257
  591. local numDists = bitStream:Read(5) -- # of distance codes - 1
  592. local numCodes = bitStream:Read(4) -- # of code length codes - 4
  593.  
  594. local codeLens = {}
  595.  
  596. for i = 1, numCodes + 4 do
  597. local index = order[i]
  598. codeLens[index] = bitStream:Read(3)
  599. end
  600.  
  601. codeLens = createHuffmanTable(codeLens, true)
  602.  
  603. local function decode(numCodes)
  604. local init = {}
  605. local numBits
  606. local val = 0
  607.  
  608. while val < numCodes do
  609. local codeLen = codeLens:Read(bitStream)
  610. local numRepeats
  611.  
  612. if codeLen <= 15 then
  613. numRepeats = 1
  614. numBits = codeLen
  615. elseif codeLen == 16 then
  616. numRepeats = 3 + bitStream:Read(2)
  617. elseif codeLen == 17 then
  618. numRepeats = 3 + bitStream:Read(3)
  619. numBits = 0
  620. elseif codeLen == 18 then
  621. numRepeats = 11 + bitStream:Read(7)
  622. numBits = 0
  623. end
  624.  
  625. for i = 1, numRepeats do
  626. init[val] = numBits
  627. val = val + 1
  628. end
  629. end
  630.  
  631. return createHuffmanTable(init, true)
  632. end
  633.  
  634. local numLitCodes = numLits + 257
  635. local numDistCodes = numDists + 1
  636.  
  637. local litTable = decode(numLitCodes)
  638. local distTable = decode(numDistCodes)
  639.  
  640. return litTable, distTable
  641. end
  642.  
  643. local function parseCompressedItem(bitStream, state, litTable, distTable)
  644. local val = litTable:Read(bitStream)
  645.  
  646. if val < 256 then -- literal
  647. write(state, val)
  648. elseif val == 256 then -- end of block
  649. return true
  650. else
  651. local lenBase = lens[val - 257]
  652. local numExtraBits = lext[val - 257]
  653.  
  654. local extraBits = bitStream:Read(numExtraBits)
  655. local len = lenBase + extraBits
  656.  
  657. local distVal = distTable:Read(bitStream)
  658. local distBase = dists[distVal]
  659.  
  660. local distNumExtraBits = dext[distVal]
  661. local distExtraBits = bitStream:Read(distNumExtraBits)
  662.  
  663. local dist = distBase + distExtraBits
  664.  
  665. for i = 1, len do
  666. local pos = (state.Pos - 1 - dist) % 32768 + 1
  667. local byte = assert(state.Window[pos], "invalid distance")
  668. write(state, byte)
  669. end
  670. end
  671.  
  672. return false
  673. end
  674.  
  675. local function parseBlock(bitStream, state)
  676. local bFinal = bitStream:Read(1)
  677. local bType = bitStream:Read(2)
  678.  
  679. if bType == BTYPE_NO_COMPRESSION then
  680. local left = bitStream:GetBitsLeft()
  681. bitStream:Read(left)
  682.  
  683. local len = bitStream:Read(16)
  684. local nlen = bitStream:Read(16)
  685.  
  686. for i = 1, len do
  687. local byte = bitStream:Read(8)
  688. write(state, byte)
  689. end
  690. elseif bType == BTYPE_FIXED_HUFFMAN or bType == BTYPE_DYNAMIC_HUFFMAN then
  691. local litTable, distTable
  692.  
  693. if bType == BTYPE_DYNAMIC_HUFFMAN then
  694. litTable, distTable = parseHuffmanTables(bitStream)
  695. else
  696. litTable = createHuffmanTable(fixedLit)
  697. distTable = createHuffmanTable(fixedDist)
  698. end
  699.  
  700. repeat until parseCompressedItem(bitStream, state, litTable, distTable)
  701. else
  702. error("unrecognized compression type")
  703. end
  704.  
  705. return bFinal ~= 0
  706. end
  707.  
  708. function Deflate:Inflate(io)
  709. local state = createState(io.Output)
  710. local bitStream = getBitStream(io.Input)
  711.  
  712. repeat until parseBlock(bitStream, state)
  713. end
  714.  
  715. function Deflate:InflateZlib(io)
  716. local bitStream = getBitStream(io.Input)
  717. local windowSize = parseZlibHeader(bitStream)
  718.  
  719. self:Inflate
  720. {
  721. Input = bitStream;
  722. Output = io.Output;
  723. }
  724.  
  725. local bitsLeft = bitStream:GetBitsLeft()
  726. bitStream:Read(bitsLeft)
  727. end
  728.  
  729. local Unfilter = {}
  730.  
  731. function Unfilter:None(scanline, pixels, bpp, row)
  732. for i = 1, #scanline do
  733. pixels[row][i] = scanline[i]
  734. end
  735. end
  736.  
  737. function Unfilter:Sub(scanline, pixels, bpp, row)
  738. for i = 1, bpp do
  739. pixels[row][i] = scanline[i]
  740. end
  741.  
  742. for i = bpp + 1, #scanline do
  743. local x = scanline[i]
  744. local a = pixels[row][i - bpp]
  745. pixels[row][i] = bit32.band(x + a, 0xFF)
  746. end
  747. end
  748.  
  749. function Unfilter:Up(scanline, pixels, bpp, row)
  750. if row > 1 then
  751. local upperRow = pixels[row - 1]
  752.  
  753. for i = 1, #scanline do
  754. local x = scanline[i]
  755. local b = upperRow[i]
  756. pixels[row][i] = bit32.band(x + b, 0xFF)
  757. end
  758. else
  759. self:None(scanline, pixels, bpp, row)
  760. end
  761. end
  762.  
  763. function Unfilter:Average(scanline, pixels, bpp, row)
  764. if row > 1 then
  765. for i = 1, bpp do
  766. local x = scanline[i]
  767. local b = pixels[row - 1][i]
  768.  
  769. b = bit32.rshift(b, 1)
  770. pixels[row][i] = bit32.band(x + b, 0xFF)
  771. end
  772.  
  773. for i = bpp + 1, #scanline do
  774. local x = scanline[i]
  775. local b = pixels[row - 1][i]
  776.  
  777. local a = pixels[row][i - bpp]
  778. local ab = bit32.rshift(a + b, 1)
  779.  
  780. pixels[row][i] = bit32.band(x + ab, 0xFF)
  781. end
  782. else
  783. for i = 1, bpp do
  784. pixels[row][i] = scanline[i]
  785. end
  786.  
  787. for i = bpp + 1, #scanline do
  788. local x = scanline[i]
  789. local b = pixels[row - 1][i]
  790.  
  791. b = bit32.rshift(b, 1)
  792. pixels[row][i] = bit32.band(x + b, 0xFF)
  793. end
  794. end
  795. end
  796.  
  797. function Unfilter:Paeth(scanline, pixels, bpp, row)
  798. if row > 1 then
  799. local pr
  800.  
  801. for i = 1, bpp do
  802. local x = scanline[i]
  803. local b = pixels[row - 1][i]
  804. pixels[row][i] = bit32.band(x + b, 0xFF)
  805. end
  806.  
  807. for i = bpp + 1, #scanline do
  808. local a = pixels[row][i - bpp]
  809. local b = pixels[row - 1][i]
  810. local c = pixels[row - 1][i - bpp]
  811.  
  812. local x = scanline[i]
  813. local p = a + b - c
  814.  
  815. local pa = math.abs(p - a)
  816. local pb = math.abs(p - b)
  817. local pc = math.abs(p - c)
  818.  
  819. if pa <= pb and pa <= pc then
  820. pr = a
  821. elseif pb <= pc then
  822. pr = b
  823. else
  824. pr = c
  825. end
  826.  
  827. pixels[row][i] = bit32.band(x + pr, 0xFF)
  828. end
  829. else
  830. self:Sub(scanline, pixels, bpp, row)
  831. end
  832. end
  833.  
  834.  
  835. local BinaryReader = {}
  836. BinaryReader.__index = BinaryReader
  837.  
  838. function BinaryReader.new(buffer)
  839. local reader =
  840. {
  841. Position = 1;
  842. Buffer = buffer;
  843. Length = #buffer;
  844. }
  845.  
  846. return setmetatable(reader, BinaryReader)
  847. end
  848.  
  849. function BinaryReader:ReadByte()
  850. local buffer = self.Buffer
  851. local pos = self.Position
  852.  
  853. if pos <= self.Length then
  854. local result = buffer:sub(pos, pos)
  855. self.Position = pos + 1
  856.  
  857. return result:byte()
  858. end
  859. end
  860.  
  861. function BinaryReader:ReadBytes(count, asArray)
  862. local values = {}
  863.  
  864. for i = 1, count do
  865. values[i] = self:ReadByte()
  866. end
  867.  
  868. if asArray then
  869. return values
  870. end
  871.  
  872. return unpack(values)
  873. end
  874.  
  875. function BinaryReader:ReadAllBytes()
  876. return self:ReadBytes(self.Length, true)
  877. end
  878.  
  879. function BinaryReader:IterateBytes()
  880. return function ()
  881. return self:ReadByte()
  882. end
  883. end
  884.  
  885. function BinaryReader:TwosComplementOf(value, numBits)
  886. if value >= (2 ^ (numBits - 1)) then
  887. value = value - (2 ^ numBits)
  888. end
  889.  
  890. return value
  891. end
  892.  
  893. function BinaryReader:ReadUInt16()
  894. local upper, lower = self:ReadBytes(2)
  895. return (upper * 256) + lower
  896. end
  897.  
  898. function BinaryReader:ReadInt16()
  899. local unsigned = self:ReadUInt16()
  900. return self:TwosComplementOf(unsigned, 16)
  901. end
  902.  
  903. function BinaryReader:ReadUInt32()
  904. local upper = self:ReadUInt16()
  905. local lower = self:ReadUInt16()
  906.  
  907. return (upper * 65536) + lower
  908. end
  909.  
  910. function BinaryReader:ReadInt32()
  911. local unsigned = self:ReadUInt32()
  912. return self:TwosComplementOf(unsigned, 32)
  913. end
  914.  
  915. function BinaryReader:ReadString(length)
  916. if length == nil then
  917. length = self:ReadByte()
  918. end
  919.  
  920. local pos = self.Position
  921. local nextPos = math.min(self.Length, pos + length)
  922.  
  923. local result = self.Buffer:sub(pos, nextPos - 1)
  924. self.Position = nextPos
  925.  
  926. return result
  927. end
  928.  
  929. function BinaryReader:ForkReader(length)
  930. local chunk = self:ReadString(length)
  931. return BinaryReader.new(chunk)
  932. end
  933.  
  934.  
  935. local function getBytesPerPixel(colorType)
  936. if colorType == 0 or colorType == 3 then
  937. return 1
  938. elseif colorType == 4 then
  939. return 2
  940. elseif colorType == 2 then
  941. return 3
  942. elseif colorType == 6 then
  943. return 4
  944. else
  945. return 0
  946. end
  947. end
  948.  
  949. local function clampInt(value, min, max)
  950. local num = tonumber(value) or 0
  951. num = math.floor(num + .5)
  952.  
  953. return math.clamp(num, min, max)
  954. end
  955.  
  956. local function indexBitmap(file, x, y)
  957. local width = file.Width
  958. local height = file.Height
  959.  
  960. local x = clampInt(x, 1, width)
  961. local y = clampInt(y, 1, height)
  962.  
  963. local bitmap = file.Bitmap
  964. local bpp = file.BytesPerPixel
  965.  
  966. local i0 = ((x - 1) * bpp) + 1
  967. local i1 = i0 + bpp
  968.  
  969. return bitmap[y], i0, i1
  970. end
  971.  
  972. function PNG:GetPixel(t,x, y)
  973. local row, i0, i1 = indexBitmap(t, x, y)
  974. local colorType = t.ColorType
  975. self = t;
  976.  
  977. local color, alpha do
  978. if colorType == 0 then
  979. local gray = unpack(row, i0, i1)
  980. color = Color3.fromHSV(0, 0, gray)
  981. alpha = 255
  982. elseif colorType == 2 then
  983. local r, g, b = unpack(row, i0, i1)
  984. color = Color3.fromRGB(r, g, b)
  985. alpha = 255
  986. elseif colorType == 3 then
  987. local palette = self.Palette
  988. local alphaData = self.AlphaData
  989.  
  990. local index = unpack(row, i0, i1)
  991. index = index + 1
  992.  
  993. if palette then
  994. color = palette[index]
  995. end
  996.  
  997. if alphaData then
  998. alpha = alphaData[index]
  999. end
  1000. elseif colorType == 4 then
  1001. local gray, a = unpack(row, i0, i1)
  1002. color = Color3.fromHSV(0, 0, gray)
  1003. alpha = a
  1004. elseif colorType == 6 then
  1005. local r, g, b, a = unpack(row, i0, i1)
  1006. color = Color3.fromRGB(r, g, b, a)
  1007. alpha = a
  1008. end
  1009. end
  1010.  
  1011. if not color then
  1012. color = Color3.new()
  1013. end
  1014.  
  1015. if not alpha then
  1016. alpha = 255
  1017. end
  1018.  
  1019. return color, alpha
  1020. end
  1021.  
  1022. function PNG.new(buffer)
  1023. -- Create the reader.
  1024. local reader = BinaryReader.new(buffer)
  1025.  
  1026. -- Create the file object.
  1027. local file =
  1028. {
  1029. Chunks = {};
  1030. Metadata = {};
  1031.  
  1032. Reading = true;
  1033. ZlibStream = "";
  1034. }
  1035.  
  1036. -- Verify the file header.
  1037. local header = reader:ReadString(8)
  1038.  
  1039. if header ~= "\137PNG\r\n\26\n" then
  1040. error("PNG - Input data is not a PNG file.", 2)
  1041. end
  1042.  
  1043. while file.Reading do
  1044. local length = reader:ReadInt32()
  1045. local chunkType = reader:ReadString(4)
  1046.  
  1047. local data, crc
  1048.  
  1049. if length > 0 then
  1050. data = reader:ForkReader(length)
  1051. crc = reader:ReadUInt32()
  1052. end
  1053.  
  1054. local chunk =
  1055. {
  1056. Length = length;
  1057. Type = chunkType;
  1058.  
  1059. Data = data;
  1060. CRC = crc;
  1061. }
  1062.  
  1063. local handler = chunks[chunkType];
  1064.  
  1065. if handler then
  1066. handler(file, chunk)
  1067. end
  1068.  
  1069. table.insert(file.Chunks, chunk)
  1070. end
  1071.  
  1072. -- Decompress the zlib stream.
  1073. local success, response = pcall(function ()
  1074. local result = {}
  1075. local index = 0
  1076.  
  1077. Deflate:InflateZlib
  1078. {
  1079. Input = BinaryReader.new(file.ZlibStream);
  1080.  
  1081. Output = function (byte)
  1082. index = index + 1
  1083. result[index] = string.char(byte)
  1084. end
  1085. }
  1086.  
  1087. return table.concat(result)
  1088. end)
  1089.  
  1090. if not success then
  1091. error("PNG - Unable to unpack PNG data. " .. tostring(response), 2)
  1092. end
  1093.  
  1094. -- Grab expected info from the file.
  1095.  
  1096. local width = file.Width
  1097. local height = file.Height
  1098.  
  1099. local bitDepth = file.BitDepth
  1100. local colorType = file.ColorType
  1101.  
  1102. local buffer = BinaryReader.new(response)
  1103. file.ZlibStream = nil
  1104.  
  1105. local bitmap = {}
  1106. file.Bitmap = bitmap
  1107.  
  1108. local channels = getBytesPerPixel(colorType)
  1109. file.NumChannels = channels
  1110.  
  1111. local bpp = math.max(1, channels * (bitDepth / 8))
  1112. file.BytesPerPixel = bpp
  1113.  
  1114. -- Unfilter the buffer and
  1115. -- load it into the bitmap.
  1116.  
  1117. for row = 1, height do
  1118. local filterType = buffer:ReadByte()
  1119. local scanline = buffer:ReadBytes(width * bpp, true)
  1120.  
  1121. bitmap[row] = {}
  1122.  
  1123. if filterType == 0 then
  1124. -- None
  1125. Unfilter:None(scanline, bitmap, bpp, row)
  1126. elseif filterType == 1 then
  1127. -- Sub
  1128. Unfilter:Sub(scanline, bitmap, bpp, row)
  1129. elseif filterType == 2 then
  1130. -- Up
  1131. Unfilter:Up(scanline, bitmap, bpp, row)
  1132. elseif filterType == 3 then
  1133. -- Average
  1134. Unfilter:Average(scanline, bitmap, bpp, row)
  1135. elseif filterType == 4 then
  1136. -- Paeth
  1137. Unfilter:Paeth(scanline, bitmap, bpp, row)
  1138. end
  1139. end
  1140.  
  1141. return setmetatable(file, PNG)
  1142. end
  1143.  
  1144.  
  1145. local result = PNG.new(pngimage)
  1146. local pixels = {};
  1147. local y = 0;
  1148. for x = 1,result.Width do
  1149. local tbl = {};
  1150. for y = 1,result.Height do
  1151. local color,alpha = PNG:GetPixel(result,x,y)
  1152. table.insert(tbl,color);
  1153. end
  1154. local r = row:Clone();
  1155. r.Parent = holder;
  1156. for i,v in pairs(tbl) do
  1157. local p = pixel:Clone();
  1158. p.Parent = r;
  1159. p.BackgroundColor3 = v;
  1160. if v == transparentcolor then
  1161. p.BackgroundTransparency = 1;
  1162. end
  1163. end
  1164. end
Add Comment
Please, Sign In to add comment