-
Notifications
You must be signed in to change notification settings - Fork 2
/
paint FINAL.lua
1572 lines (1241 loc) · 38.6 KB
/
paint FINAL.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--You hold IMMENSE power
--[[-------------------------Info--------------------------------
-Simply press execute to open the gui
-To use, you need a link to a png image, preferrably neither dimensions are much larger than 256,
but it works best (faster) under 128 (if a dimension is too large, it won't work)
-I personally use paint.net to resize the image, then paste it into a private discord to get the link
-Once you have the link, you can paste it into the link field
-The size field is optional and defaults to 0.2, and its max value is 1.2 (if you don't have the gamepass)
-If you have the gamepass (not reccomended cuz they ban for exploits and its too large to fit anyways)
you would need to change MAX_PIXEL_SIZE below
-Once you have the link and desired size, click "Go" and your next left click will attempt to draw the image (centered) on the surface
-If it gets stuck, you can simply close it with no consequences (I think) and execute it again
-Freecam (shift+p) helps a lot
-------------------------------------------------------------]]--
--------THIS SCRIPT WAS MADE WITH SYNAPSE X IN MIND----------
--IF YOU ARE NOT USING SYNAPSE X:
--Replace this with your executor's http request function
local REQUEST_FUNC = https://krnl.ca
--Replace this with your executor's gui protection function
--(This can be set to nil and the script will run fine, not very necessary tbh)
local PROTECT_FUNC = 0
-------------------------------------------------------------
--Constants
local MAX_PIXEL_SIZE = 1.2 --change this if you own the gamepass
local RAY_LENGTH = 1000 --how far the ray will try to go
local SURFACE_OFFSET = 0.2 --distance from the surface you place it on
local PIXEL_DEPTH = 0.001 --idk if this matters, but it's the Y size of the pixel (supposedly)
-------------------------------------------------------------
--pcall cuz im dumb and also for error messages in console
local succ,err = pcall(function()
-- PNG parsing module (very helpful and not mine)
-- My code starts around line 1084 or something if you are interested in it
-- Drawing Library [edited by spooky hack man on v3rmillion]
local PNG = {}
PNG.__index = PNG
local chunks = {};
local function IDAT(file, chunk)
local crc = chunk.CRC
local hash = file.Hash or 0
local data = chunk.Data
local buffer = data.Buffer
file.Hash = bit32.bxor(hash, crc)
file.ZlibStream = file.ZlibStream .. buffer
end
chunks['IDAT'] = IDAT;
local function IEND(file)
file.Reading = nil
end
chunks['IEND'] = IEND;
local function IHDR(file, chunk)
local data = chunk.Data
file.Width = data:ReadInt32();
file.Height = data:ReadInt32();
file.BitDepth = data:ReadByte();
file.ColorType = data:ReadByte();
file.Methods =
{
Compression = data:ReadByte();
Filtering = data:ReadByte();
Interlace = data:ReadByte();
}
end
chunks['IHDR'] = IHDR;
local function PLTE(file, chunk)
if not file.Palette then
file.Palette = {}
end
local data = chunk.Data
local palette = data:ReadAllBytes()
if #palette % 3 ~= 0 then
error("[ERROR]: Invalid PLTE chunk.")
end
for i = 1, #palette, 3 do
local r = palette[i]
local g = palette[i + 1]
local b = palette[i + 2]
local color = Color3.fromRGB(r, g, b)
local index = #file.Palette + 1
file.Palette[index] = color
end
end
chunks['PLTE'] = PLTE;
local function bKGD(file, chunk)
local data = chunk.Data
local bitDepth = file.BitDepth
local colorType = file.ColorType
bitDepth = (2 ^ bitDepth) - 1
if colorType == 3 then
local index = data:ReadByte()
file.BackgroundColor = file.Palette[index]
elseif colorType == 0 or colorType == 4 then
local gray = data:ReadUInt16() / bitDepth
file.BackgroundColor = Color3.fromHSV(0, 0, gray)
elseif colorType == 2 or colorType == 6 then
local r = data:ReadUInt16() / bitDepth
local g = data:ReadUInt16() / bitDepth
local b = data:ReadUInt16() / bitDepth
file.BackgroundColor = Color3.new(r, g, b)
end
end
chunks['bKGD'] = bKGD;
local colors = {"White", "Red", "Green", "Blue"}
local function cHRM(file, chunk)
local chrome = {}
local data = chunk.Data
for i = 1, 4 do
local color = colors[i]
chrome[color] =
{
[1] = data:ReadUInt32() / 10e4;
[2] = data:ReadUInt32() / 10e4;
}
end
file.Chromaticity = chrome
end
chunks['cHRM'] = cHRM;
local function gAMA(file, chunk)
local data = chunk.Data
local value = data:ReadUInt32()
file.Gamma = value / 10e4
end
chunks['gAMA'] = gAMA;
local function sRGB(file, chunk)
local data = chunk.Data
file.RenderIntent = data:ReadByte()
end
chunks['sRGB'] = sRGB;
local function tEXt(file, chunk)
local data = chunk.Data
local key, value = "", ""
for byte in data:IterateBytes() do
local char = string.char(byte)
if char == '\0' then
key = value
value = ""
else
value = value .. char
end
end
file.Metadata[key] = value
end
chunks['tEXt'] = tEXt;
local function tIME(file, chunk)
local data = chunk.Data
local timeStamp =
{
Year = data:ReadUInt16();
Month = data:ReadByte();
Day = data:ReadByte();
Hour = data:ReadByte();
Minute = data:ReadByte();
Second = data:ReadByte();
}
file.TimeStamp = timeStamp
end
chunks['tIME'] = tIME;
local function tRNS(file, chunk)
local data = chunk.Data
local bitDepth = file.BitDepth
local colorType = file.ColorType
bitDepth = (2 ^ bitDepth) - 1
if colorType == 3 then
local palette = file.Palette
local alphaMap = {}
for i = 1, #palette do
local alpha = data:ReadByte()
if not alpha then
alpha = 255
end
alphaMap[i] = alpha
end
file.AlphaData = alphaMap
elseif colorType == 0 then
local grayAlpha = data:ReadUInt16()
file.Alpha = grayAlpha / bitDepth
elseif colorType == 2 then
-- TODO: This seems incorrect...
local r = data:ReadUInt16() / bitDepth
local g = data:ReadUInt16() / bitDepth
local b = data:ReadUInt16() / bitDepth
file.Alpha = Color3.new(r, g, b)
else
error("[ERROR]: Invalid tRNS chunk.")
end
end
chunks['tRNS'] = tRNS;
local Deflate = {}
local band = bit32.band
local lshift = bit32.lshift
local rshift = bit32.rshift
local BTYPE_NO_COMPRESSION = 0
local BTYPE_FIXED_HUFFMAN = 1
local BTYPE_DYNAMIC_HUFFMAN = 2
local lens = -- Size base for length codes 257..285
{
[0] = 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258
}
local lext = -- Extra bits for length codes 257..285
{
[0] = 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
}
local dists = -- Offset base for distance codes 0..29
{
[0] = 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
8193, 12289, 16385, 24577
}
local dext = -- Extra bits for distance codes 0..29
{
[0] = 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
12, 12, 13, 13
}
local order = -- Permutation of code length codes
{
16, 17, 18, 0, 8, 7, 9, 6, 10, 5,
11, 4, 12, 3, 13, 2, 14, 1, 15
}
-- Fixed literal table for BTYPE_FIXED_HUFFMAN
local fixedLit = {0, 8, 144, 9, 256, 7, 280, 8, 288}
-- Fixed distance table for BTYPE_FIXED_HUFFMAN
local fixedDist = {0, 5, 32}
local function createState(bitStream)
local state =
{
Output = bitStream;
Window = {};
Pos = 1;
}
return state
end
local function write(state, byte)
local pos = state.Pos
state.Output(byte)
state.Window[pos] = byte
state.Pos = pos % 32768 + 1 -- 32K
end
local function memoize(fn)
local meta = {}
local memoizer = setmetatable({}, meta)
function meta:__index(k)
local v = fn(k)
memoizer[k] = v
return v
end
return memoizer
end
-- small optimization (lookup table for powers of 2)
local pow2 = memoize(function (n)
return 2 ^ n
end)
-- weak metatable marking objects as bitstream type
local isBitStream = setmetatable({}, { __mode = 'k' })
local function createBitStream(reader)
local buffer = 0
local bitsLeft = 0
local stream = {}
isBitStream[stream] = true
function stream:GetBitsLeft()
return bitsLeft
end
function stream:Read(count)
count = count or 1
while bitsLeft < count do
local byte = reader:ReadByte()
if not byte then
return
end
buffer = buffer + lshift(byte, bitsLeft)
bitsLeft = bitsLeft + 8
end
local bits
if count == 0 then
bits = 0
elseif count == 32 then
bits = buffer
buffer = 0
else
bits = band(buffer, rshift(2^32 - 1, 32 - count))
buffer = rshift(buffer, count)
end
bitsLeft = bitsLeft - count
return bits
end
return stream
end
local function getBitStream(obj)
if isBitStream[obj] then
return obj
end
return createBitStream(obj)
end
local function sortHuffman(a, b)
return a.NumBits == b.NumBits and a.Value < b.Value or a.NumBits < b.NumBits
end
local function msb(bits, numBits)
local res = 0
for i = 1, numBits do
res = lshift(res, 1) + band(bits, 1)
bits = rshift(bits, 1)
end
return res
end
local function createHuffmanTable(init, isFull)
local hTable = {}
if isFull then
for val, numBits in pairs(init) do
if numBits ~= 0 then
hTable[#hTable + 1] =
{
Value = val;
NumBits = numBits;
}
end
end
else
for i = 1, #init - 2, 2 do
local firstVal = init[i]
local numBits = init[i + 1]
local nextVal = init[i + 2]
if numBits ~= 0 then
for val = firstVal, nextVal - 1 do
hTable[#hTable + 1] =
{
Value = val;
NumBits = numBits;
}
end
end
end
end
table.sort(hTable, sortHuffman)
local code = 1
local numBits = 0
for i, slide in ipairs(hTable) do
if slide.NumBits ~= numBits then
code = code * pow2[slide.NumBits - numBits]
numBits = slide.NumBits
end
slide.Code = code
code = code + 1
end
local minBits = math.huge
local look = {}
for i, slide in ipairs(hTable) do
minBits = math.min(minBits, slide.NumBits)
look[slide.Code] = slide.Value
end
local firstCode = memoize(function (bits)
return pow2[minBits] + msb(bits, minBits)
end)
function hTable:Read(bitStream)
local code = 1 -- leading 1 marker
local numBits = 0
while true do
if numBits == 0 then -- small optimization (optional)
local index = bitStream:Read(minBits)
numBits = numBits + minBits
code = firstCode[index]
else
local bit = bitStream:Read()
numBits = numBits + 1
code = code * 2 + bit -- MSB first
end
local val = look[code]
if val then
return val
end
end
end
return hTable
end
local function parseZlibHeader(bitStream)
-- Compression Method
local cm = bitStream:Read(4)
-- Compression info
local cinfo = bitStream:Read(4)
-- FLaGs: FCHECK (check bits for CMF and FLG)
local fcheck = bitStream:Read(5)
-- FLaGs: FDICT (present dictionary)
local fdict = bitStream:Read(1)
-- FLaGs: FLEVEL (compression level)
local flevel = bitStream:Read(2)
-- CMF (Compresion Method and flags)
local cmf = cinfo * 16 + cm
-- FLaGs
local flg = fcheck + fdict * 32 + flevel * 64
if cm ~= 8 then -- not "deflate"
error("[ERROR]: Unrecognized image Compression Method: " .. tostring(cm))
end
if cinfo > 7 then
error("[ERROR]: Invalid image Window Size: cinfo -> " .. tostring(cinfo))
end
local windowSize = 2 ^ (cinfo + 8)
if (cmf * 256 + flg) % 31 ~= 0 then
error("[ERROR]: Invalid image header (bad fcheck sum)")
end
if fdict == 1 then
error("[TODO]: - FDICT not currently implemented")
end
return windowSize
end
local function parseHuffmanTables(bitStream)
local numLits = bitStream:Read(5) -- # of literal/length codes - 257
local numDists = bitStream:Read(5) -- # of distance codes - 1
local numCodes = bitStream:Read(4) -- # of code length codes - 4
local codeLens = {}
for i = 1, numCodes + 4 do
local index = order[i]
codeLens[index] = bitStream:Read(3)
end
codeLens = createHuffmanTable(codeLens, true)
local function decode(numCodes)
local init = {}
local numBits
local val = 0
while val < numCodes do
local codeLen = codeLens:Read(bitStream)
local numRepeats
if codeLen <= 15 then
numRepeats = 1
numBits = codeLen
elseif codeLen == 16 then
numRepeats = 3 + bitStream:Read(2)
elseif codeLen == 17 then
numRepeats = 3 + bitStream:Read(3)
numBits = 0
elseif codeLen == 18 then
numRepeats = 11 + bitStream:Read(7)
numBits = 0
end
for i = 1, numRepeats do
init[val] = numBits
val = val + 1
end
end
return createHuffmanTable(init, true)
end
local numLitCodes = numLits + 257
local numDistCodes = numDists + 1
local litTable = decode(numLitCodes)
local distTable = decode(numDistCodes)
return litTable, distTable
end
local function parseCompressedItem(bitStream, state, litTable, distTable)
local val = litTable:Read(bitStream)
if val < 256 then -- literal
write(state, val)
elseif val == 256 then -- end of block
return true
else
local lenBase = lens[val - 257]
local numExtraBits = lext[val - 257]
local extraBits = bitStream:Read(numExtraBits)
local len = lenBase + extraBits
local distVal = distTable:Read(bitStream)
local distBase = dists[distVal]
local distNumExtraBits = dext[distVal]
local distExtraBits = bitStream:Read(distNumExtraBits)
local dist = distBase + distExtraBits
for i = 1, len do
local pos = (state.Pos - 1 - dist) % 32768 + 1
local byte = assert(state.Window[pos], "invalid distance")
write(state, byte)
end
end
return false
end
local function parseBlock(bitStream, state)
local bFinal = bitStream:Read(1)
local bType = bitStream:Read(2)
if bType == BTYPE_NO_COMPRESSION then
local left = bitStream:GetBitsLeft()
bitStream:Read(left)
local len = bitStream:Read(16)
local nlen = bitStream:Read(16)
for i = 1, len do
local byte = bitStream:Read(8)
write(state, byte)
end
elseif bType == BTYPE_FIXED_HUFFMAN or bType == BTYPE_DYNAMIC_HUFFMAN then
local litTable, distTable
if bType == BTYPE_DYNAMIC_HUFFMAN then
litTable, distTable = parseHuffmanTables(bitStream)
else
litTable = createHuffmanTable(fixedLit)
distTable = createHuffmanTable(fixedDist)
end
repeat until parseCompressedItem(bitStream, state, litTable, distTable)
else
error("[ERROR]: Unrecognized compression type.")
end
return bFinal ~= 0
end
function Deflate:Inflate(io)
local state = createState(io.Output)
local bitStream = getBitStream(io.Input)
repeat until parseBlock(bitStream, state)
end
function Deflate:InflateZlib(io)
local bitStream = getBitStream(io.Input)
local windowSize = parseZlibHeader(bitStream)
self:Inflate
{
Input = bitStream;
Output = io.Output;
}
local bitsLeft = bitStream:GetBitsLeft()
bitStream:Read(bitsLeft)
end
local Unfilter = {}
function Unfilter:None(scanline, pixels, bpp, row)
for i = 1, #scanline do
pixels[row][i] = scanline[i]
end
end
function Unfilter:Sub(scanline, pixels, bpp, row)
for i = 1, bpp do
pixels[row][i] = scanline[i]
end
for i = bpp + 1, #scanline do
local x = scanline[i]
local a = pixels[row][i - bpp]
pixels[row][i] = bit32.band(x + a, 0xFF)
end
end
function Unfilter:Up(scanline, pixels, bpp, row)
if row > 1 then
local upperRow = pixels[row - 1]
for i = 1, #scanline do
local x = scanline[i]
local b = upperRow[i]
pixels[row][i] = bit32.band(x + b, 0xFF)
end
else
self:None(scanline, pixels, bpp, row)
end
end
function Unfilter:Average(scanline, pixels, bpp, row)
if row > 1 then
for i = 1, bpp do
local x = scanline[i]
local b = pixels[row - 1][i]
b = bit32.rshift(b, 1)
pixels[row][i] = bit32.band(x + b, 0xFF)
end
for i = bpp + 1, #scanline do
local x = scanline[i]
local b = pixels[row - 1][i]
local a = pixels[row][i - bpp]
local ab = bit32.rshift(a + b, 1)
pixels[row][i] = bit32.band(x + ab, 0xFF)
end
else
for i = 1, bpp do
pixels[row][i] = scanline[i]
end
for i = bpp + 1, #scanline do
local x = scanline[i]
local b = pixels[row - 1][i]
b = bit32.rshift(b, 1)
pixels[row][i] = bit32.band(x + b, 0xFF)
end
end
end
function Unfilter:Paeth(scanline, pixels, bpp, row)
if row > 1 then
local pr
for i = 1, bpp do
local x = scanline[i]
local b = pixels[row - 1][i]
pixels[row][i] = bit32.band(x + b, 0xFF)
end
for i = bpp + 1, #scanline do
local a = pixels[row][i - bpp]
local b = pixels[row - 1][i]
local c = pixels[row - 1][i - bpp]
local x = scanline[i]
local p = a + b - c
local pa = math.abs(p - a)
local pb = math.abs(p - b)
local pc = math.abs(p - c)
if pa <= pb and pa <= pc then
pr = a
elseif pb <= pc then
pr = b
else
pr = c
end
pixels[row][i] = bit32.band(x + pr, 0xFF)
end
else
self:Sub(scanline, pixels, bpp, row)
end
end
local BinaryReader = {}
BinaryReader.__index = BinaryReader
function BinaryReader.new(buffer)
local reader =
{
Position = 1;
Buffer = buffer;
Length = #buffer;
}
return setmetatable(reader, BinaryReader)
end
function BinaryReader:ReadByte()
local buffer = self.Buffer
local pos = self.Position
if pos <= self.Length then
local result = buffer:sub(pos, pos)
self.Position = pos + 1
return result:byte()
end
end
function BinaryReader:ReadBytes(count, asArray)
local values = {}
for i = 1, count do
values[i] = self:ReadByte()
end
if asArray then
return values
end
return unpack(values)
end
function BinaryReader:ReadAllBytes()
return self:ReadBytes(self.Length, true)
end
function BinaryReader:IterateBytes()
return function ()
return self:ReadByte()
end
end
function BinaryReader:TwosComplementOf(value, numBits)
if value >= (2 ^ (numBits - 1)) then
value = value - (2 ^ numBits)
end
return value
end
function BinaryReader:ReadUInt16()
local upper, lower = self:ReadBytes(2)
return (upper * 256) + lower
end
function BinaryReader:ReadInt16()
local unsigned = self:ReadUInt16()
return self:TwosComplementOf(unsigned, 16)
end
function BinaryReader:ReadUInt32()
local upper = self:ReadUInt16()
local lower = self:ReadUInt16()
return (upper * 65536) + lower
end
function BinaryReader:ReadInt32()
local unsigned = self:ReadUInt32()
return self:TwosComplementOf(unsigned, 32)
end
function BinaryReader:ReadString(length)
if length == nil then
length = self:ReadByte()
end
local pos = self.Position
local nextPos = math.min(self.Length, pos + length)
local result = self.Buffer:sub(pos, nextPos - 1)
self.Position = nextPos
return result
end
function BinaryReader:ForkReader(length)
local chunk = self:ReadString(length)
return BinaryReader.new(chunk)
end
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 + .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(t,x, y)
local row, i0, i1 = indexBitmap(t, x, y)
local colorType = t.ColorType
self = t;
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()
end
if not alpha then
alpha = 255
end
return color, alpha
end
function PNG.new(buffer)
-- Create the reader.
local reader = BinaryReader.new(buffer)
-- Create the file object.
local file =
{
Chunks = {};
Metadata = {};
Reading = true;
ZlibStream = "";
}
-- Verify the file header.
local header = reader:ReadString(8)
if header ~= "\137PNG\r\n\26\n" then
error("[ERROR]: Input data is not a PNG file.")
end
while file.Reading do
local length = reader:ReadInt32()
local chunkType = reader:ReadString(4)