Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, zlib, re
- from PIL import Image
- from time import sleep
- def texture_export(texture_name, only_name):
- file2 = open(texture_name, "rb")
- out = open(only_name + '.png', "wb+")
- dados = file2.read()
- textura = dados.find(b'\x94\x72\x85\x29\x01')
- resolution1 = dados[20:21]
- #resolution1 = b'\x80'
- # print(resolution1)
- resolution1int= int.from_bytes(resolution1, byteorder='big', signed=False)
- # print(resolution1int)
- resolution2 = dados[22:23]
- # print(resolution2)
- resolution2int= int.from_bytes(resolution2, byteorder='big', signed=False)
- # print(resolution2int)
- idat_resX = resolution1int * resolution2int
- # print(idat_resX)
- cabecalho_bmp = (b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d")
- init_ihdr= (b"\x49\x48\x44\x52\x00\x00\x00")
- cabecalho_bmp2 = b'\x00\x00\x00'
- cabecalho_bmp3 = (b"\x08\x03\x00\x00\x00")
- fim_cabec = b"\x00\x00\x03\x00\x50\x4c\x54\x45"
- ihdr_complet = (init_ihdr +resolution1+ cabecalho_bmp2+ resolution2+cabecalho_bmp3 )
- # print(ihdr_complet)
- check_ihdr = (zlib.crc32(init_ihdr +resolution1+ cabecalho_bmp2+ resolution2 + cabecalho_bmp3 ))
- # print(hex(check_ihdr))
- #chunck_pallete = (zlib.crc32(pallete_cab + pallete_fim))
- es_ihrd = check_ihdr.to_bytes(4, byteorder='big', signed=False)
- out.write(cabecalho_bmp)
- out.write(init_ihdr)
- out.write(resolution1)
- out.write(cabecalho_bmp2)
- out.write(resolution2)
- out.write(cabecalho_bmp3)
- out.write(es_ihrd)
- out.write(fim_cabec)
- file2.seek(1152,0)
- IDAT = file2.read()
- iDAT = IDAT[:idat_resX]
- filex = open('idat.pct', "w+b")
- filex.seek(0,0)
- filex.write(b'\x00')
- filex.seek(1,0)
- recromp = filex.read()
- for xxx in range(0,idat_resX,resolution1int):
- filex.write(iDAT[xxx:xxx+ resolution1int])
- filex.write(b'\x00')
- filex.seek(0,0)
- recompp = filex.read()
- zzzz = (recompp[:idat_resX+ 256])
- #print(zzzz)
- data4 = zlib.compress(zzzz, 7)
- paleta_seek = file2.seek(128,0)
- paleta_read = file2.read()
- pallete = paleta_read[:1024]
- pallete_att = []
- pallete_out = open('pallete.plt', "wb+")
- pallete_att2 = pallete_out.read()
- opac_out = open('tRNS.opc', "wb+")
- opac_att2 = opac_out.read()
- #opacidade
- list_opac=[]
- for x in range(0,1024,4):
- cxcx = (pallete[x+3:x+4])
- opac_out.write(cxcx)
- list_opac.append(cxcx)
- opac_out.seek(0,0)
- ttt_opac = opac_out.read()
- opac_cor = open('tRNS_cor.opc', "wb+")
- opac_att3 = opac_cor.read()
- for z in range(0,256,24):
- dp1o = ttt_opac[z:z+6]
- # print(dp1o)
- dp2o = ttt_opac[z+6:z+12]
- dp3o = ttt_opac[z+12:z+18]
- dp4o = ttt_opac[z+18:z+24]
- #pallete_cor.write(dp4)
- opac_cor.write(dp1o)
- #list_opac.append(dp1o)
- opac_cor.write(dp3o)
- # list_opac.append(dp2o)
- opac_cor.write(dp2o)
- # list_opac.append(dp3o)
- opac_cor.write(dp4o)
- # list_opac.append(dp4o)
- opac_cor.seek(0,0)
- opac_fim = opac_cor.read()
- opac_cor.seek(0,0)
- len_opaccc = len(opac_fim)
- #print(len_opaccc, 'x')
- list_opc2 =[]
- for bb in range(len(list_opac)):
- v1= int.from_bytes(list_opac[bb], byteorder='big', signed=False)
- #print(v1)
- v2 = v1 * 2
- if v2 == 256:
- v3 = v2 -1
- v4 = v3.to_bytes(1, byteorder='little', signed=False)
- list_opc2.append(v4)
- # print(v4)
- else:
- v4 = v2.to_bytes(1, byteorder='little', signed=False)
- #print(v4)
- list_opc2.append(v4)
- opac_cor2 = open('tRNS_x2.opc', "wb+")
- for xxx in range(len(list_opc2)):
- opac_cor2.write(list_opc2[xxx])
- opac_cor.seek(0,0)
- for x in range(0,1024,4):
- aaa = (pallete[x:x+3])
- pallete_out.write(aaa)
- pallete_out.seek(0,0)
- ttt_pallete = pallete_out.read()
- pallete_cor = open('pallete_cor.plt', "wb+")
- pallete_att3 = pallete_cor.read()
- for z in range(0,1024,96):
- dp1 = ttt_pallete[z:z+24]
- dp2 = ttt_pallete[z+24:z+48]
- dp3 = ttt_pallete[z+48:z+72]
- dp4 = ttt_pallete[z+72:z+96]
- #pallete_cor.write(dp4)
- pallete_cor.write(dp1)
- pallete_cor.write(dp3)
- pallete_cor.write(dp2)
- pallete_cor.write(dp4)
- pallete_cor.seek(0,0)
- pallete_fim = pallete_cor.read()
- out.write(pallete_fim)
- # print(dp2)
- file2.seek(1152,0)
- dxdx =file2.read()
- #print(len(dxdx))
- text2 = []
- pallete_cab = b'\x50\x4c\x54\x45'
- chunck_pallete = (zlib.crc32(pallete_cab + pallete_fim))
- es_pallete = chunck_pallete.to_bytes(4, byteorder='big', signed=False)
- tam_pallete = len(pallete_fim)
- #print(tam_pallete)
- cabecalho_2 = (b"\x00\x00\x01\x00")
- opc_cab = b'\x74\x52\x4E\x53'
- out.write(es_pallete)
- out.write(cabecalho_2)
- out.write(opc_cab)
- #salvar opcd
- opac_cor2.seek(0,0)
- ocp_read = opac_cor2.read()
- teste = open('teste.pct', "wb+")
- for z in range(0,256,32):
- dp1 = ocp_read[z:z+8]
- #print(dp1)
- dp2 = ocp_read[z+8:z+16]
- dp3 = ocp_read[z+16:z+24]
- dp4 = ocp_read[z+24:z+32]
- #pallete_cor.write(dp4)
- out.write(dp1)
- out.write(dp3)
- out.write(dp2)
- out.write(dp4)
- teste.write(dp1)
- teste.write(dp3)
- teste.write(dp2)
- teste.write(dp4)
- teste.seek(0,0)
- teste_opc = teste.read()
- #print(teste_opc)
- opac_cor2.seek(0,0)
- ocp_read = opac_cor2.read()
- chunk_opc = zlib.crc32(opc_cab+ teste_opc)
- #print(hex(chunk_opc), 'opc')
- es_opc_chunk = chunk_opc.to_bytes(4, byteorder='big', signed=False)
- cabecalho_3 = (b"\x00\x00")
- tam_textura = len(data4)
- es_textura = tam_textura.to_bytes(2, byteorder='big', signed=True)
- cabecalho3_3 = b'\x49\x44\x41\x54'
- out.write(es_opc_chunk)
- out.write(cabecalho_3)
- out.write(es_textura)
- out.write(cabecalho3_3)
- out.write(data4)
- cabecalho_4 = b"\x00\x00\x00\x00"
- end_png = b'\x49\x45\x4e\x44\xae\x42\x60\x82'
- idat_desc = b'\x49\x44\x41\x54'
- idatx = zlib.crc32(idat_desc + data4)
- #print(hex(idatx), 'ida5')
- es_idat = idatx.to_bytes(4, byteorder='big', signed=False)
- out.write(es_idat)
- out.write(cabecalho_4)
- out.write(end_png)
- print('\nSuccessfully exported Texture PNG ... ')
- sleep(1)
- for arquivos in os.listdir():
- if arquivos.endswith('.plt'):
- os.remove(arquivos)
- for arquivos in os.listdir():
- if arquivos.endswith('.pct'):
- os.remove(arquivos)
- for arquivos in os.listdir():
- if arquivos.endswith('.opc'):
- os.remove(arquivos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement