Skip to content

Instantly share code, notes, and snippets.

@tompng tompng/.gitignore

Last active Oct 17, 2018
Embed
What would you like to do?
s1: png viewer
out.rb
tmp.rb
*.png
CHEADER
X=%(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaa------------aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaa--------------aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaa---aaaaaaaa----aaaaaaaaa---aaaaaaaaaa---aaaaaaaaaaaaaaaaaa
aaaaaaa---aaaaaaaaa---aaaaaaaaa---aaaaaaaaaa---aaaaaaaaaaaaaaaaaa
aaaaaaa---aaaaaaaa----aaaaaaaaa---aaaaaaaaaa---aaaaaaaaaaaaaaaaaa
aaaaaaa--------------aaaaaaaaaa---aaaaaaaaaa---aaaaaaaaaaaaaaaaaa
aaaaaaa------------aaaaaaaaaaaa---aaaaaaaaaa---aaaaaaaaaaaaaaaaaa
aaaaaaa---aaaaaaa---aaaaaaaaaaa---aaaaaaaaaa---aaaaaaaaaaaaaaaaaa
aaaaaaa---aaaaaaaa---aaaaaaaaaa---aaaaaaaaaa---aaaaaaaaaaaaaaaaaa
aaaaaaa---aaaaaaaaa---aaaaaaaaa---aaaaaaaaaa---aaaaaaaaaaaaaaaaaa
aaaaaaa---aaaaaaaaaa---aaaaaaaa----aaaaaaaa----aaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa--------------aaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaa------------aaaaa------------aaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaa-------------aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaa---aaaaaaa---aaaaaaaaa---aaaaaaaaaaaaa---aaaaaaaa
aaaaaaaaaaaaaaaa---aaaaaaaa---aaaaaaaaa---aaaaaaaaaaa---aaaaaaaaa
aaaaaaaaaaaaaaaa---aaaaaaa---aaaaaaaaaaa---aaaaaaaaa---aaaaaaaaaa
aaaaaaaaaaaaaaaa------------aaaaaaaaaaaaaa---aaaaa---aaaaaaaaaaaa
aaaaaaaaaaaaaaaa---aaaaaaa---aaaaaaaaaaaaaa----a----aaaaaaaaaaaaa
aaaaaaaaaaaaaaaa---aaaaaaaa---aaaaaaaaaaaaaaa-----aaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaa---aaaaaaa----aaaaaaaaaaaaaaaa---aaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaa-------------aaaaaaaaaaaaaaaaa---aaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaa------------aaaaaaaaaaaaaaaaaa---aaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa---aaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa---aaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
)
puts %(CHEADER)
deflated = Zlib.deflate(C)
unpacked = deflated.unpack('C*')
num = unpacked.reverse.reduce{|a,b|a*256+b}
target_size = X.join.size-X.join.count('-')
padnummax = ((26**target_size-num) / 256**unpacked.size).floor
padnummin = ((26**(target_size-1)-num) / 256**unpacked.size).ceil
paddednum = num+rand(padnummin+1..padnummax)*256**unpacked.size
encoded = paddednum.to_s(26).tr('0-9','q-z')
raise if encoded.size != target_size
i=0
puts X.map{|s|s.gsub(/\s/){encoded[(i+=1)-1]}}
X=[];class String def-@;replace ?-+self end;def-a;X.reject!{|x|x.
__id__==__id__};a.replace(self+?-+a) end end;at_exit{eval C=(Zlib
.inflate((X*?-).tr(?-,'').tr('q-z','0-9').to_i(26).digits(0x100).
pack'C*'))};def method_missing n;(X<<n.to_s)[-1]end;require'zlib'
fzygtoxyzgntmdmuwvfoffbpmvzojpkhczvjvjdbtscnldwbdoprackddovivvmkz
ponzmosvtjciwkgaslscxxxwudeesmmqpfhislxuxnnypulxstzgobyaekqqhbjcg
mvko------------ddkeys----eivhnccaqyiw---bzyccmt-----------ymtnge
jwhi--------------pjxf------mdarbtumnv---qasda--------------gmwdt
wrtk---qtpzgnce----fsl-------fkgzgtbpp---gwnm----pxkpqkdiw---owga
momz---yjjvpnvar---zeo---v-----duvalwu---nsqt---waofemwakivnyqkjd
fzag---uhvusmkl----kzb---rhc----iutzjr---mqlh---ayijpwativpweaato
xexs--------------rvgv---pjdz-----lkkg---uiaw---lovitupw-----fwmn
kfru------------jvjpgv---jskycf----pal---gbuf---hfdnywog-----iuca
pntn---apmkqroeuzwuwkw---gqnmgof-----b---hlpl---vkkyhfyrqfr--jwrl
kmdb---dhspujhmtgrkccu---uonfummdt-------rqfw----bpiactehwp--fncq
yzvz---gdaxebplhfndran---ytfmviryeh------hqwkl---------------nced
bibu---fnkdthgldhkxxjg---rwnmpudhbqin----gucoyki------------hfura
cqdgqpyzqfzknvdjoxxhpjulwwyebtocxdrvklbuviwwcatlmdosxfvwntzbijguy
iglrvvzlxerflupxvsyujfacuwhrvmnecgtewtqkhtdggcltejiyqcluclkycwvzg
vvxfysvttfbeglvrlngntdngzyhqrmltazwdydxrsvjploembhgxdvfmmhepbschm
brn--iqrcdb--evv----tqp------lg--uein-wzut--mr------wkh------foqz
zsf--srjnjp--ampb--pfio--hgtekx--rrr---fwd--jn--xqkezcz--vsb--nya
khrc--evlr--oioxs--mqce--bqfmag--bwz---xda--qw--jnuzelr--qzi--itx
mdxd--duso--wxbot--nmon--ugnbdpc--a--c--e--hlg--twxndre--tby--rhg
evhbn--zb--dtxmiz--dpia------vie--h--i--t--shh------kfn------owna
ealmt--kb--scxdjy--smvl--dqmgebk--t--s--t--gfd--updcbnc--rh--dwwp
dvpnxb----wpljjdy--kolc--qflyleok---xkv---usbj--jhrawbn--ewx--bgf
eaqwrw----ejwxhet--dice--eoczconm---urz---rqyp--hovvvfc--bskj--el
aocjcts--jtumwxm----mgy------xpaoq-jtwqr-aipay------dhy--iync--hk
sckddmvuvvuhhqstumaykvczaaujrumqbbqsdvdycplyrlkkojlxnkrhbbrmnjxyf
cdtcmpfmjvthwkpzucbblttgumomlxnxwjeypfeagaukfzeokzxjebkpigcvlqnso
require 'zlib'
CHEADER=<<-CODE
X=[];class String def-@;replace ?-+self end;def-a;X.reject!{|x|x.
__id__==__id__};a.replace(self+?-+a) end end;at_exit{eval C=(Zlib
.inflate((X*?-).tr(?-,'').tr('q-z','0-9').to_i(26).digits(0x100).
pack'C*'))};def method_missing n;(X<<n.to_s)[-1]end;require'zlib'
CODE
HEADER64=<<CODE
X=[];class String def-@;replace ?-+self end;def-a;X.reject!{|x|x
.__id__==__id__};a.replace(self+?-+a) end end;at_exit{eval Zlib.
inflate((X*?-).tr(?-,'').tr('q-z','0-9').to_i(26).digits(0x100).
pack'C*')};def method_missing n;(X<<n.to_s)[-1]end;require'zlib'
CODE
QHEADER=<<-CODE
eval H=%(X=[];class String def-a;X.reject!{|x|x.__id__==__id__};a.
replace(self+?-+a) end;def-@;replace ?-+self end end;at_exit{eval(
Zlib.inflate (X*?-).tr(?-,'').tr('q-z','0-9').to_i(26).digits(256)
.pack'C*')};def method_missing n;(X<<n.to_s)[-1]end;require'zlib')
CODE
raise 'argv[0] should be an existing filename' unless ARGV[0] && File.exist?(ARGV[0])
type, *shape_and_codes = File.read(ARGV[0]).lines
shape_and_code = shape_and_codes.join
SHAPE = shape_and_code.match(/X=%\(([^)]+)\)/)[1].strip
CODE = shape_and_code.gsub(/X=%\([^)]+\)/,'').gsub(/CHEADER/,CHEADER).gsub(/HEADER64/,HEADER64).gsub(/QHEADER/,QHEADER)
deflated = Zlib.deflate(CODE)
unpacked = deflated.unpack('C*')
num = unpacked.reverse.reduce{|a,b|a*256+b}
target_size = SHAPE.count('a')
raise "overflow #{num.to_s(26).size} > #{target_size}" if target_size < num.to_s(26).size
padnummax = ((26**target_size-num) / 256**unpacked.size).floor
padnummin = ((26**(target_size-1)-num) / 256**unpacked.size).ceil
srand 0
10.times{
paddednum = num+rand(padnummin+1..padnummax)*256**unpacked.size
encoded = paddednum.to_s(26).tr('0-9','q-z')
i=0
output = Object.const_get(type.scan(/[A-Z0-9]+/)[0])+SHAPE.gsub(/a/){encoded[(i+=1)-1]}
p num.to_s(26).size.fdiv target_size
p [output.delete(' ').size, output.size, output.lines.size]
next if output =~ /-pp?-/
File.write('out.rb',output+"\n")
exit if `ruby -c out.rb` =~ /Syntax OK/
}
raise 'failed'
CHEADER
X=%(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaa------------aaaaaa----aaaaaaaaaaaa---aaaaaaa-----------aaaaaa
aaaa--------------aaaa------aaaaaaaaaa---aaaaa--------------aaaaa
aaaa---aaaaaaaa----aaa-------aaaaaaaaa---aaaa----aaaaaaaaa---aaaa
aaaa---aaaaaaaaa---aaa---a-----aaaaaaa---aaaa---aaaaaaaaaaaaaaaaa
aaaa---aaaaaaaa----aaa---aaa----aaaaaa---aaaa---aaaaaaaaaaaaaaaaa
aaaa--------------aaaa---aaaa-----aaaa---aaaa---aaaaaaaa-----aaaa
aaaa------------aaaaaa---aaaaaa----aaa---aaaa---aaaaaaaa-----aaaa
aaaa---aaaaaaaaaaaaaaa---aaaaaaa-----a---aaaa---aaaaaaaaaaa--aaaa
aaaa---aaaaaaaaaaaaaaa---aaaaaaaaa-------aaaa----aaaaaaaaaa--aaaa
aaaa---aaaaaaaaaaaaaaa---aaaaaaaaaa------aaaaa---------------aaaa
aaaa---aaaaaaaaaaaaaaa---aaaaaaaaaaaa----aaaaaaa------------aaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaa--aaaaaa--aaa----aaa------aa--aaaa-aaaa--aa------aaa------aaaa
aaa--aaaaaa--aaaa--aaaa--aaaaaa--aaa---aaa--aa--aaaaaaa--aaa--aaa
aaaa--aaaa--aaaaa--aaaa--aaaaaa--aaa---aaa--aa--aaaaaaa--aaa--aaa
aaaa--aaaa--aaaaa--aaaa--aaaaaaa--a--a--a--aaa--aaaaaaa--aaa--aaa
aaaaa--aa--aaaaaa--aaaa------aaa--a--a--a--aaa------aaa------aaaa
aaaaa--aa--aaaaaa--aaaa--aaaaaaa--a--a--a--aaa--aaaaaaa--aa--aaaa
aaaaaa----aaaaaaa--aaaa--aaaaaaaa---aaa---aaaa--aaaaaaa--aaa--aaa
aaaaaa----aaaaaaa--aaaa--aaaaaaaa---aaa---aaaa--aaaaaaa--aaaa--aa
aaaaaaa--aaaaaaa----aaa------aaaaa-aaaaa-aaaaa------aaa--aaaa--aa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
)
def method_missing*;super end
require'chunky_png'
raise'ARGV[0]: no file'if !File.exist?(i=ARGV[0].to_s)
i=ChunkyPNG::Image.from_file i
w,h=i.width,i.height
s=[h/42.0,w/65.0].max
x=42.times.map{|y|132.times.map{|x|x=i[(x-66+w/s)*s/2,((y-21+h/s/2)*s).to_i]rescue nil
x ?((x/256).digits(256).sum/3)*(a=(x&0xff)/256.0)+256*(1-a)+rand*rand*100:0}}
z=x.flatten.select{|x|x!=0}
z=[x.flatten.sort[2000],z.sum/z.size].max
s=x.map{|s|s.map{|s|z>=s ? ?a: ?-}*''}
z=Zlib.deflate(C).unpack('C*')
n=z.reverse.reduce{|s,n|s*256+n}
loop{e=(n+rand(((26**((t=s.join.count ?a)-1)-n)/256**z.size).ceil+1..((26**t-n)/256**z.size).floor)*256**z.size).to_s(26).tr('0-9','q-z').chars
c=(s.map{|s|s.gsub(?a){e.shift}}*$/).gsub(/pp/,'pP').gsub(/in/,'iN').gsub(/if/,'iF')
next if (e=c.scan /\w+/).any?{|c|method c rescue nil}||(e&%w|self true false nil|)[0]
begin
eval c
rescue SyntaxError
rescue
puts %(undef p;X=[];class String def-@;replace ?-+dup end;def-a;X.reject!{|x|x.__id__==__id__};a.replace(self+?-+a) end end;at_exit{eval C=
(Zlib.inflate (X*?-).tr(?-,'').tr('q-z','0-9').to_i(26).digits(256).pack'C*')};def method_missing n;(X<<n.to_s)[-1]end;require'zlib'),c
exit
end}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.