Page MenuHome

import_TIM.py

Authored By
koil (koilz)
Nov 13 2013, 5:32 PM
Size
17 KB
Subscribers
None

import_TIM.py

# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
bl_info = {
"name": "Import TIM",
"author": "koil",
"version": (1, 0, 0),
"blender": (2, 66, 1),
"location": "Editors > 3D View > Properties (N) > TIM.",
"description": "Import TIM: PSone Texture Image.",
"wiki_url": "http://wiki.blender.org/index.php/User:Koilz/13.04.15_Import_TIM",
"category": "Import-Export"
}
""" ---------------------------------------------------------------- """
""" Header """
import bpy
import os
from bpy.types import Operator, Panel
""" ---------------------------------------------------------------- """
""" ---------------------------------------------------------------- """
""" Operator: Import TIM """
class OT_import_TIM(Operator):
bl_idname = "image.import_tim"
bl_label = "Import TIM"
bl_description = "Import TIM: PSone Texture Image."
bl_options = {'REGISTER', 'UNDO'}
TIM_file = bpy.props.StringProperty(default="FILE.TIM")
def execute(self, context):
""" -------------------------------- """
""" File """
dir = os.path.dirname(bpy.data.filepath)
f = open(dir+'\\'+self.TIM_file, 'rb')
data = f.read()
f.close()
#for i in range(80):
# print(str(i)+': '+str(data[i]))
""" -------------------------------- """
""" -------------------------------- """
""" Data """
error=False # Error
TIM_t=0 # Tag
TIM_v=0 # Version
TIM_c=0 # CLUT Present
TIM_b=0 # Bit Format
CLUT_s=0 # Size
CLUT_x=0 # X
CLUT_y=0 # Y
CLUT_w=0 # Width
CLUT_h=0 # Height
CLUT_data=[] # Data
IMAGE_s=0 # Size
IMAGE_x=0 # X
IMAGE_y=0 # Y
IMAGE_w=0 # Width
IMAGE_h=0 # Height
IMAGE_data=[] # Data
""" -------------------------------- """
""" -------------------------------- """
""" TIM """
""" http://wiki.python.org/moin/BitwiseOperators """
""" So -1 is complement(1 - 1) = complement(0) = "11111111", and -10 is """
""" complement(10 - 1) = complement(9) = complement("00001001") = "11110110". """
""" This means that negative numbers go all the way down to -128 ("10000000"). """
TIM_t = data[0]
TIM_v = data[1]
TIM_c = (data[4]&8)>>3
TIM_b = (data[4]&7)
print("--------------------------------")
if TIM_t == 16:
print("TIM_t: "+str(TIM_t))
else:
print("TIM_t: Error: "+str(TIM_t))
self.report({"WARNING"},"TIM_t: Error: "+str(TIM_t))
error=True
if TIM_v == 0:
print("TIM_v: "+str(TIM_v))
else:
print("TIM_v: Error: "+str(TIM_v))
self.report({"WARNING"},"TIM_v: Error: "+str(TIM_v))
error=True
if TIM_c == 0 or TIM_c == 1:
print("TIM_c: "+str(TIM_c))
else:
print("TIM_c: Error: "+str(TIM_c))
error=True
if TIM_b == 0 or TIM_b == 1 or TIM_b == 2:
print("TIM_b: "+str(TIM_b))
else:
print("TIM_b: Error: "+str(TIM_b))
self.report({"WARNING"},"TIM_b: Error: "+str(TIM_b))
error=True
if error==True:
print(str(data[0])+" "+str(data[1])+" "+str(data[2])+" "+str(data[3]))
print(str(data[4])+" "+str(data[5])+" "+str(data[6])+" "+str(data[7]))
""" -------------------------------- """
""" -------------------------------- """
""" CLUT """
if error==False:
if TIM_c==1:
if TIM_b==0:
CLUT_s = (data[8])+(data[9]<<8)+(data[10]<<16)+(data[11]<<24)
CLUT_x = (data[12])+(data[13]<<8)
CLUT_y = (data[14])+(data[15]<<8)
CLUT_w = (data[16])+(data[17]<<8)
CLUT_h = (data[18])+(data[19]<<8)
print("--------------------------------")
print("CLUT_s: "+str(CLUT_s))
print("CLUT_x: "+str(CLUT_x))
print("CLUT_y: "+str(CLUT_y))
print("CLUT_w: "+str(CLUT_w))
print("CLUT_h: "+str(CLUT_h))
for i in range(CLUT_s-12):
CLUT_data.append(data[20+i])
if TIM_c==1:
if TIM_b==1:
CLUT_s = (data[8])+(data[9]<<8)+(data[10]<<16)+(data[11]<<24)
CLUT_x = (data[12])+(data[13]<<8)
CLUT_y = (data[14])+(data[15]<<8)
CLUT_w = (data[16])+(data[17]<<8)
CLUT_h = (data[18])+(data[19]<<8)
print("--------------------------------")
print("CLUT_s: "+str(CLUT_s))
print("CLUT_x: "+str(CLUT_x))
print("CLUT_y: "+str(CLUT_y))
print("CLUT_w: "+str(CLUT_w))
print("CLUT_h: "+str(CLUT_h))
for i in range(CLUT_s-12):
CLUT_data.append(data[20+i])
#for i in range(CLUT_s-12):
# print(str(i)+": "+str(CLUT_data[i]))
""" -------------------------------- """
""" -------------------------------- """
""" IMAGE """
if error==False:
if TIM_c==1:
if TIM_b==0:
IMAGE_o = 8+CLUT_s
IMAGE_s = (data[IMAGE_o+0])+(data[IMAGE_o+1]<<8)+(data[IMAGE_o+2]<<16)+(data[IMAGE_o+3]<<24)
IMAGE_x = (data[IMAGE_o+4])+(data[IMAGE_o+5]<<8)
IMAGE_y = (data[IMAGE_o+6])+(data[IMAGE_o+7]<<8)
IMAGE_w = (data[IMAGE_o+8])+(data[IMAGE_o+9]<<8)
IMAGE_h = (data[IMAGE_o+10])+(data[IMAGE_o+11]<<8)
print("--------------------------------")
print("IMAGE_s: "+str(IMAGE_s))
print("IMAGE_x: "+str(IMAGE_x))
print("IMAGE_y: "+str(IMAGE_y))
print("IMAGE_w: "+str(IMAGE_w))
print("IMAGE_h: "+str(IMAGE_h))
IMAGE_w=int(((IMAGE_s-12)*2)/IMAGE_h)
print("IMAGE_c_width: "+str(IMAGE_w))
for i in range(IMAGE_s-12):
IMAGE_data.append(data[IMAGE_o+12+i])
#for i in range(IMAGE_s-12):
# print(str(i)+": "+str(IMAGE_data[i]))
for i in range(80):
print(str(i)+": "+str(IMAGE_data[i]))
if TIM_c==1:
if TIM_b==1:
IMAGE_o = 8+CLUT_s
IMAGE_s = (data[IMAGE_o+0])+(data[IMAGE_o+1]<<8)+(data[IMAGE_o+2]<<16)+(data[IMAGE_o+3]<<24)
IMAGE_x = (data[IMAGE_o+4])+(data[IMAGE_o+5]<<8)
IMAGE_y = (data[IMAGE_o+6])+(data[IMAGE_o+7]<<8)
IMAGE_w = (data[IMAGE_o+8])+(data[IMAGE_o+9]<<8)
IMAGE_h = (data[IMAGE_o+10])+(data[IMAGE_o+11]<<8)
print("--------------------------------")
print("IMAGE_s: "+str(IMAGE_s))
print("IMAGE_x: "+str(IMAGE_x))
print("IMAGE_y: "+str(IMAGE_y))
print("IMAGE_w: "+str(IMAGE_w))
print("IMAGE_h: "+str(IMAGE_h))
"""
FFVII: BINS.TIM
IMAGE_w: 24
IMAGE_h: 48
IMAGE_data size: 48x48
c_width = (IMAGE_s-12)/IMAGE_h
"""
IMAGE_w=int((IMAGE_s-12)/IMAGE_h)
print("IMAGE_c_width: "+str(IMAGE_w))
for i in range(IMAGE_s-12):
IMAGE_data.append(data[IMAGE_o+12+i])
#for i in range(IMAGE_s-12):
# print(str(i)+": "+str(IMAGE_data[i]))
if TIM_c==0:
if TIM_b==2:
IMAGE_o = 8+CLUT_s
IMAGE_s = (data[IMAGE_o+0])+(data[IMAGE_o+1]<<8)+(data[IMAGE_o+2]<<16)+(data[IMAGE_o+3]<<24)
IMAGE_x = (data[IMAGE_o+4])+(data[IMAGE_o+5]<<8)
IMAGE_y = (data[IMAGE_o+6])+(data[IMAGE_o+7]<<8)
IMAGE_w = (data[IMAGE_o+8])+(data[IMAGE_o+9]<<8)
IMAGE_h = (data[IMAGE_o+10])+(data[IMAGE_o+11]<<8)
print("--------------------------------")
print("IMAGE_s: "+str(IMAGE_s))
print("IMAGE_x: "+str(IMAGE_x))
print("IMAGE_y: "+str(IMAGE_y))
print("IMAGE_w: "+str(IMAGE_w))
print("IMAGE_h: "+str(IMAGE_h))
for i in range(IMAGE_s-12):
IMAGE_data.append(data[IMAGE_o+12+i])
#for i in range(IMAGE_s-12):
# print(str(i)+": "+str(IMAGE_data[i]))
""" -------------------------------- """
""" -------------------------------- """
""" PNG """
if error==False:
if TIM_c==1:
if TIM_b==0:
PNG_file = self.TIM_file
os.path.splitext(PNG_file)
PNG_name = os.path.splitext(PNG_file)[0]
PNG = bpy.data.images.new(str(PNG_name)+'.PNG',IMAGE_w,IMAGE_h,True,False)
iy=IMAGE_h-1
io=0
ion=0
ii=0
PNG_data = []
# 0101 1111 0101 0101
# 5f 55 55 55
for y in range(IMAGE_h):
print("--------")
for x in range(IMAGE_w):
io=int(((iy*IMAGE_w)+x)/2)
#io=int(((iy*IMAGE_w)/2)+(x/2))
if ion==0:
#print(str(io)+" "+str(ion))
#print("CLUT: "+str((IMAGE_data[io]&15)))
#### write ####
r=((CLUT_data[((IMAGE_data[io]&15)*2)]&31))
g=((CLUT_data[((IMAGE_data[io]&15)*2)]&224)>>5)+((CLUT_data[((IMAGE_data[io]&240>>4)*2)+1]&3)<<3)
b=((CLUT_data[((IMAGE_data[io]&15)*2)+1]&124)>>2)
s=((CLUT_data[((IMAGE_data[io]&15)*2)+1]&128)>>7)
#print(str(r/31)+" "+str(g/31)+" "+str(b/31))
PNG_data.append(r/31)
PNG_data.append(g/31)
PNG_data.append(b/31)
PNG_data.append(1.0)
ion=1
else:
#print(str(io)+" "+str(ion))
#print("CLUT: "+str(((IMAGE_data[io]&240)>>4)))
#### write ####
r=((CLUT_data[(((IMAGE_data[io]&240)>>4)*2)]&31))
g=((CLUT_data[(((IMAGE_data[io]&240)>>4)*2)]&224)>>5)+((CLUT_data[(((IMAGE_data[io]&240)>>4)*2)+1]&3)<<3)
b=((CLUT_data[(((IMAGE_data[io]&240)>>4)*2)+1]&124)>>2)
s=((CLUT_data[(((IMAGE_data[io]&240)>>4)*2)+1]&128)>>7)
#print(str(r/31)+" "+str(g/31)+" "+str(b/31))
PNG_data.append(r/31)
PNG_data.append(g/31)
PNG_data.append(b/31)
PNG_data.append(1.0)
ion=0
ii+=1
iy-=1
PNG.pixels=PNG_data
self.report({"INFO"},"Imported: "+str(self.TIM_file)+" as "+str(PNG.name))
if TIM_c==1:
if TIM_b==1:
################################################################
"""
>>> bpy.data.images['mr_cloud.PNG'].pixels[0:4]
(0.9725490808486938, 0.9725490808486938, 0.9725490808486938, 1.0)
"""
#for p in range(IMAGE_h*IMAGE_h):
#PNG_data = PNG.pixels
# >>> bpy.data.images.remove(bpy.data.images[2])
""" Y
PNG = bpy.data.images.new('TIFA.PNG',IMAGE_h,IMAGE_h,True,False)
for i in range(IMAGE_h*IMAGE_h):
r=((CLUT_data[(IMAGE_data[i]*2)]&31))
g=((CLUT_data[(IMAGE_data[i]*2)]&224)>>5)+((CLUT_data[(IMAGE_data[i]*2)+1]&3)<<3)
b=((CLUT_data[(IMAGE_data[i]*2)+1]&124)>>2)
s=((CLUT_data[(IMAGE_data[i]*2)+1]&128)>>7)
PNG.pixels[(i*4)+0]=r/31
PNG.pixels[(i*4)+1]=g/31
PNG.pixels[(i*4)+2]=b/31
PNG.pixels[(i*4)+3]=1.0
"""
# x>y^
################################################################
PNG_file = self.TIM_file
os.path.splitext(PNG_file)
PNG_name = os.path.splitext(PNG_file)[0]
PNG = bpy.data.images.new(str(PNG_name)+'.PNG',IMAGE_w,IMAGE_h,True,False)
iy=IMAGE_h-1
io=0
ii=0
PNG_data = []
for y in range(IMAGE_h):
for x in range(IMAGE_w):
io=(iy*IMAGE_w)+x
r=((CLUT_data[(IMAGE_data[io]*2)]&31))
g=((CLUT_data[(IMAGE_data[io]*2)]&224)>>5)+((CLUT_data[(IMAGE_data[io]*2)+1]&3)<<3)
b=((CLUT_data[(IMAGE_data[io]*2)+1]&124)>>2)
s=((CLUT_data[(IMAGE_data[io]*2)+1]&128)>>7)
PNG_data.append(r/31)
PNG_data.append(g/31)
PNG_data.append(b/31)
PNG_data.append(1.0)
ii+=1
iy-=1
PNG.pixels=PNG_data
self.report({"INFO"},"Imported: "+str(self.TIM_file)+" as "+str(PNG.name))
if TIM_c==0:
if TIM_b==2:
PNG_file = self.TIM_file
os.path.splitext(PNG_file)
PNG_name = os.path.splitext(PNG_file)[0]
PNG = bpy.data.images.new(str(PNG_name)+'.PNG',IMAGE_w,IMAGE_h,True,False)
iy=IMAGE_h-1
io=0
ii=0
PNG_data = []
for y in range(IMAGE_h):
for x in range(IMAGE_w):
io=((iy*IMAGE_w)+x)*2
r=(IMAGE_data[io]&31)
g=((IMAGE_data[io]&224)>>5)+((IMAGE_data[io+1]&3)<<3)
b=(IMAGE_data[io+1]&124)>>2
s=(IMAGE_data[io+1]&128)>>7
PNG_data.append(r/31)
PNG_data.append(g/31)
PNG_data.append(b/31)
PNG_data.append(1.0)
ii+=1
iy-=1
PNG.pixels=PNG_data
self.report({"INFO"},"Imported: "+str(self.TIM_file)+" as "+str(PNG.name))
""" -------------------------------- """
print("--------------------------------")
return {'FINISHED'}
bpy.utils.register_class(OT_import_TIM)
""" ---------------------------------------------------------------- """
""" ---------------------------------------------------------------- """
""" Panel: TIM """
bpy.types.Scene.TIM_filename = bpy.props.StringProperty(default="FILE.TIM")
class VIEW3D_PT_TIM(Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_label = "TIM"
def draw(self, context):
layout = self.layout
col = layout.column()
row = col.row()
row.prop(context.scene,"TIM_filename",text="TIM File")
row = col.row()
row.operator("image.import_tim").TIM_file=context.scene.TIM_filename
""" ---------------------------------------------------------------- """
""" ---------------------------------------------------------------- """
""" Register """
def register():
bpy.utils.register_class(VIEW3D_PT_TIM)
def unregister():
bpy.utils.unregister_class(VIEW3D_PT_TIM)
if __name__ == "__main__":
register()
""" ---------------------------------------------------------------- """

File Metadata

Mime Type
text/x-python
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
57/b1/9fc47cb2a7b0414e5af0b406c5b7

Event Timeline