Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.30 KB | None | 0 0
  1. import os, zlib, re
  2. from PIL import Image
  3. from time import sleep
  4.  
  5. def texture_export(texture_name, only_name):
  6.    
  7.     file2 = open(texture_name, "rb")
  8.     out = open(only_name + '.png', "wb+")
  9.     dados = file2.read()
  10.     textura = dados.find(b'\x94\x72\x85\x29\x01')
  11.  
  12.    
  13.     resolution1 = dados[20:21]
  14.     #resolution1 = b'\x80'
  15. #   print(resolution1)
  16.     resolution1int= int.from_bytes(resolution1, byteorder='big', signed=False)
  17.    
  18. #   print(resolution1int)
  19.     resolution2 = dados[22:23]
  20. #   print(resolution2)
  21.     resolution2int= int.from_bytes(resolution2, byteorder='big', signed=False)
  22. #   print(resolution2int)
  23.    
  24.     idat_resX = resolution1int * resolution2int
  25. #   print(idat_resX)
  26.    
  27.    
  28.    
  29.    
  30.     cabecalho_bmp = (b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d")
  31.     init_ihdr= (b"\x49\x48\x44\x52\x00\x00\x00")
  32.     cabecalho_bmp2 = b'\x00\x00\x00'
  33.     cabecalho_bmp3 = (b"\x08\x03\x00\x00\x00")
  34.    
  35.    
  36.     fim_cabec = b"\x00\x00\x03\x00\x50\x4c\x54\x45"
  37.    
  38.    
  39.     ihdr_complet = (init_ihdr +resolution1+ cabecalho_bmp2+ resolution2+cabecalho_bmp3 )
  40. #   print(ihdr_complet)
  41.     check_ihdr = (zlib.crc32(init_ihdr +resolution1+ cabecalho_bmp2+ resolution2 + cabecalho_bmp3 ))
  42. #   print(hex(check_ihdr))
  43.    
  44.    
  45.     #chunck_pallete = (zlib.crc32(pallete_cab + pallete_fim))
  46.     es_ihrd = check_ihdr.to_bytes(4, byteorder='big', signed=False)
  47.    
  48.     out.write(cabecalho_bmp)
  49.     out.write(init_ihdr)
  50.     out.write(resolution1)
  51.     out.write(cabecalho_bmp2)
  52.     out.write(resolution2)
  53.     out.write(cabecalho_bmp3)
  54.     out.write(es_ihrd)
  55.     out.write(fim_cabec)
  56.    
  57.    
  58.    
  59.    
  60.     file2.seek(1152,0)
  61.    
  62.     IDAT = file2.read()
  63.     iDAT = IDAT[:idat_resX]
  64.    
  65.     filex = open('idat.pct', "w+b")
  66.    
  67.     filex.seek(0,0)
  68.     filex.write(b'\x00')
  69.     filex.seek(1,0)
  70.     recromp = filex.read()
  71.    
  72.     for xxx in range(0,idat_resX,resolution1int):
  73.        
  74.         filex.write(iDAT[xxx:xxx+ resolution1int])
  75.        
  76.         filex.write(b'\x00')
  77.     filex.seek(0,0)
  78.     recompp = filex.read()
  79.     zzzz = (recompp[:idat_resX+ 256])
  80.     #print(zzzz)
  81.    
  82.     data4 = zlib.compress(zzzz, 7)
  83.    
  84.     paleta_seek = file2.seek(128,0)
  85.     paleta_read = file2.read()
  86.     pallete = paleta_read[:1024]
  87.     pallete_att = []
  88.     pallete_out = open('pallete.plt', "wb+")
  89.     pallete_att2 = pallete_out.read()
  90.     opac_out = open('tRNS.opc', "wb+")
  91.     opac_att2 = opac_out.read()
  92.    
  93.     #opacidade
  94.     list_opac=[]
  95.     for x in range(0,1024,4):
  96.    
  97.         cxcx = (pallete[x+3:x+4])
  98.        
  99.         opac_out.write(cxcx)
  100.         list_opac.append(cxcx)
  101.    
  102.     opac_out.seek(0,0)
  103.    
  104.     ttt_opac = opac_out.read()
  105.     opac_cor = open('tRNS_cor.opc', "wb+")
  106.    
  107.     opac_att3 = opac_cor.read()
  108.     for z in range(0,256,24):
  109.         dp1o = ttt_opac[z:z+6]
  110.     #   print(dp1o)
  111.        
  112.        
  113.    
  114.         dp2o = ttt_opac[z+6:z+12]
  115.    
  116.        
  117.         dp3o = ttt_opac[z+12:z+18]
  118.        
  119.        
  120.        
  121.         dp4o = ttt_opac[z+18:z+24]
  122.         #pallete_cor.write(dp4)
  123.        
  124.         opac_cor.write(dp1o)
  125.         #list_opac.append(dp1o)
  126.         opac_cor.write(dp3o)
  127.     #   list_opac.append(dp2o)
  128.         opac_cor.write(dp2o)
  129.     #   list_opac.append(dp3o)
  130.         opac_cor.write(dp4o)
  131.     #   list_opac.append(dp4o)
  132.     opac_cor.seek(0,0)
  133.     opac_fim = opac_cor.read()
  134.     opac_cor.seek(0,0)
  135.     len_opaccc = len(opac_fim)
  136.     #print(len_opaccc, 'x')
  137.     list_opc2 =[]
  138.     for bb in range(len(list_opac)):
  139.         v1= int.from_bytes(list_opac[bb], byteorder='big', signed=False)
  140.         #print(v1)
  141.         v2 = v1 * 2
  142.         if v2 == 256:
  143.             v3 = v2 -1
  144.             v4  = v3.to_bytes(1, byteorder='little', signed=False)
  145.             list_opc2.append(v4)
  146.             #   print(v4)
  147.         else:
  148.             v4  = v2.to_bytes(1, byteorder='little', signed=False)
  149.             #print(v4)
  150.             list_opc2.append(v4)
  151.     opac_cor2 = open('tRNS_x2.opc', "wb+")
  152.     for xxx in range(len(list_opc2)):
  153.         opac_cor2.write(list_opc2[xxx])
  154.        
  155.    
  156.    
  157.    
  158.    
  159.     opac_cor.seek(0,0)
  160.        
  161.    
  162.     for x in range(0,1024,4):
  163.    
  164.         aaa = (pallete[x:x+3])
  165.        
  166.         pallete_out.write(aaa)
  167.    
  168.     pallete_out.seek(0,0)
  169.     ttt_pallete = pallete_out.read()
  170.     pallete_cor = open('pallete_cor.plt', "wb+")
  171.    
  172.     pallete_att3 = pallete_cor.read()
  173.    
  174.     for z in range(0,1024,96):
  175.         dp1 = ttt_pallete[z:z+24]
  176.        
  177.    
  178.         dp2 = ttt_pallete[z+24:z+48]
  179.    
  180.        
  181.         dp3 = ttt_pallete[z+48:z+72]
  182.        
  183.        
  184.        
  185.         dp4 = ttt_pallete[z+72:z+96]
  186.         #pallete_cor.write(dp4)
  187.         pallete_cor.write(dp1)
  188.         pallete_cor.write(dp3)
  189.         pallete_cor.write(dp2)
  190.         pallete_cor.write(dp4)
  191.     pallete_cor.seek(0,0)
  192.     pallete_fim = pallete_cor.read()
  193.     out.write(pallete_fim)
  194.     #   print(dp2)
  195.     file2.seek(1152,0)
  196.     dxdx =file2.read()
  197.     #print(len(dxdx))
  198.     text2 = []
  199.     pallete_cab = b'\x50\x4c\x54\x45'
  200.     chunck_pallete = (zlib.crc32(pallete_cab + pallete_fim))
  201.     es_pallete = chunck_pallete.to_bytes(4, byteorder='big', signed=False)
  202.    
  203.     tam_pallete = len(pallete_fim)
  204.     #print(tam_pallete)
  205.     cabecalho_2 = (b"\x00\x00\x01\x00")
  206.     opc_cab = b'\x74\x52\x4E\x53'
  207.     out.write(es_pallete)
  208.     out.write(cabecalho_2)
  209.     out.write(opc_cab)
  210.     #salvar opcd
  211.     opac_cor2.seek(0,0)
  212.     ocp_read = opac_cor2.read()
  213.     teste = open('teste.pct', "wb+")
  214.     for z in range(0,256,32):
  215.         dp1 = ocp_read[z:z+8]
  216.         #print(dp1)
  217.        
  218.    
  219.         dp2 = ocp_read[z+8:z+16]
  220.    
  221.        
  222.         dp3 = ocp_read[z+16:z+24]
  223.        
  224.         dp4 = ocp_read[z+24:z+32]
  225.         #pallete_cor.write(dp4)
  226.         out.write(dp1)
  227.        
  228.         out.write(dp3)
  229.         out.write(dp2)
  230.         out.write(dp4)
  231.         teste.write(dp1)
  232.         teste.write(dp3)
  233.         teste.write(dp2)
  234.         teste.write(dp4)
  235.     teste.seek(0,0)
  236.     teste_opc = teste.read()
  237.     #print(teste_opc)
  238.    
  239.     opac_cor2.seek(0,0)
  240.     ocp_read = opac_cor2.read()
  241.    
  242.     chunk_opc = zlib.crc32(opc_cab+ teste_opc)
  243.     #print(hex(chunk_opc), 'opc')
  244.     es_opc_chunk = chunk_opc.to_bytes(4, byteorder='big', signed=False)
  245.     cabecalho_3 = (b"\x00\x00")
  246.     tam_textura = len(data4)
  247.     es_textura = tam_textura.to_bytes(2, byteorder='big', signed=True)
  248.    
  249.    
  250.     cabecalho3_3 = b'\x49\x44\x41\x54'
  251.    
  252.     out.write(es_opc_chunk)
  253.     out.write(cabecalho_3)
  254.     out.write(es_textura)
  255.     out.write(cabecalho3_3)
  256.     out.write(data4)
  257.     cabecalho_4 = b"\x00\x00\x00\x00"
  258.     end_png = b'\x49\x45\x4e\x44\xae\x42\x60\x82'
  259.     idat_desc = b'\x49\x44\x41\x54'
  260.     idatx = zlib.crc32(idat_desc + data4)
  261.     #print(hex(idatx), 'ida5')
  262.     es_idat = idatx.to_bytes(4, byteorder='big', signed=False)
  263.     out.write(es_idat)
  264.     out.write(cabecalho_4)
  265.     out.write(end_png)
  266.     print('\nSuccessfully exported Texture PNG ... ')
  267.     sleep(1)
  268.     for arquivos in os.listdir():
  269.             if arquivos.endswith('.plt'):
  270.                 os.remove(arquivos)
  271.    
  272.     for arquivos in os.listdir():
  273.             if arquivos.endswith('.pct'):
  274.                 os.remove(arquivos)
  275.    
  276.     for arquivos in os.listdir():
  277.             if arquivos.endswith('.opc'):
  278.                 os.remove(arquivos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement