rendered paste body#!/usr/bin/python
import Image as PIL
import time
import sys
try:
opengl_support = True
from OpenGL.GL import *
from OpenGL.Tk import *
except:
opengl_support=False
try:
psyco_enabled=True
import psyco
psyco.log()
psyco.profile()
psyco.full()
except:
psyco_enabled=False
########################################################################################################
#
# class for handling geodata
# needs geodata file as param
# new_geodata = geodata('filename-without-.dat')
#
# methods: encode() header()
#
########################################################################################################
class geodata:
class logger:
def write(self,some_string):
if self.dump:
self.log_file.write(str(some_string))
return 0
def __init__(self,filename,to_file_bol=True):
self.dump=to_file_bol
if self.dump:
self.log_file = open(filename+'.log','wb')
def __del__(self):
if self.dump:
self.log_file.close()
class geoimage:
def _color_value(self,z):
return z+50000
def putpixel(self,x,y,z):
self.image.putpixel((x,y),self._color_value(z))
def putpixel8x8(self,x,y,z):
if isinstance(z,int):
for sub_x in range(0,8):
for sub_y in range(0,8):
self.putpixel(x+sub_x,y+sub_y,z)
else:
counter=0
for sub_x in range(0,8):
for sub_y in range(0,8):
self.putpixel(x+sub_x,y+sub_y,max(z[counter]))
counter+=1
def __init__(self,filename):
self.filename=filename
self.image = PIL.new('RGB', (2048,2048), (52390))
def __del__(self):
self.image.save(self.filename+'.png')
def print_progress(self):
print "|",
####################################################################################################
#
# converts bytes hex values
#
####################################################################################################
def convert_to_hex(self,byte):
if len(byte)==0:
self.log.write('EOF REACHED')
return '00'
return hex(ord(byte))[2:].upper().zfill(2)
####################################################################################################
#
# test if z is > maxz if yes set maxz = z
#
####################################################################################################
def _maxz(self,z=None):
if z:
# getting max z
if z!=0:
if self.maxz == None:
self.maxz = z
else:
if self.maxz < z:
self.maxz = z
return self.maxz
####################################################################################################
#
# test if z is < minz if yes set minz = z
#
####################################################################################################
def _minz(self,z=None):
if z:
# getting min z
if self.minz == None:
self.minz = z
else:
if self.minz > z:
self.minz = z
return self.minz
####################################################################################################
#
# getz z value from file
#
####################################################################################################
def _getz(self,dprint=False):
# read bytes
z2 = self.convert_to_hex(self.conv_file.read(1))
z1 = self.convert_to_hex(self.conv_file.read(1))
if dprint:
self.log.write(z1 + ' ' + z2 + ' ')
# converts to int
z = int(z1+z2,16)
# get the real z1
if z > 55000:
z = z - 65336
self._maxz(z)
self._minz(z)
return z
####################################################################################################
#
# block 0000 handler
# z1 min-z z2 max-z
#
####################################################################################################
def _handle_block0000(self):
# getting the bytes for z1/z2
z1 = self._getz()
z2 = self._getz()
return z1,z2
####################################################################################################
#
# block 0040 handler
# return a 8x8 subcell
#
####################################################################################################
def _handle_block0040(self):
# loop 64 times
return_data = []
counter = 0
for sub_x in range(0,8):
for sub_y in range(0,8):
z = self._getz() / 2
if self.image: self.image.putpixel(self.x+sub_x,self.y+sub_y,z)
return_data.append(z)
counter+=1
return return_data
####################################################################################################
#
# block 0041 handler
#
####################################################################################################
def _handle_block0041(self,header):
return_data_sub = []
return_data = []
# loop 64 times
for item in range(0,64):
# get count of layers
layercount = self._getz()
for i in range(0,layercount):
z = self._getz() / 2
return_data_sub.append(z)
return_data.append(return_data_sub)
return return_data
def redraw(self,o):
glClearColor(0.5, 0.5, 0.5, 0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glOrtho(0,1,0,1,0,-6000)
glDisable(GL_LIGHTING)
point=[1.0,0.0,1.0]
glBegin(GL_POLYGON)
for x in range(0,256):
for y in range(0,256):
point=[x,y,-3000]
glVertex3fv(point)
glEnd()
glEnable(GL_LIGHTING)
####################################################################################################
#
# Decodes the geodata
#
####################################################################################################
def decode(self):
start = time.time()
if self.dump_image:
self.image = self.geoimage(self.filename)
else:
self.image =False
self.conv_file.seek(18)
self.data=[]
counter=0
counter_percent = range(0,256*256,((256*256)/25))
# make x loop
for self.x in range(0,2048,8):
# make y loop
for self.y in range(0,2048,8):
if counter in counter_percent:
self.print_progress()
counter= counter + 1
# reads data header
block1 = self.conv_file.read(1)
block2 = self.conv_file.read(1)
full_block_id = int(ord(block2)*256 + ord(block1))
block1_hex = self.convert_to_hex(block1)
# handles the data
if full_block_id > 2048:
self.log.write('x: %s y: %s block: >1024 ERROR \n' %(str(self.x),str(self.y)))
elif full_block_id == 0:
# get data
z1,z2 = self._handle_block0000()
if self.image: self.image.putpixel8x8(self.x,self.y,z1)
# check for water - z1!=z2 water
if z1==z2:
self.data.append([self.x,self.y,z1,'F'])
else:
for item in range(z1,z2,32):
self.data.append([self.x,self.y,item,'W'])
self.log.write('x: %s y: %s block: 00 ok \n' %(str(self.x),str(self.y)))
elif full_block_id == 64:
z_data = self._handle_block0040()
self.log.write('x: %s y: %s block: 40 ok \n' %(str(self.x),str(self.y)))
elif full_block_id > 64:
z_data = self._handle_block0041(block1_hex)
self.log.write('x: %s y: %s block: %s ok \n' %(str(self.x),str(self.y),block1_hex))
if self.image: self.image.putpixel8x8(self.x,self.y,z_data)
else:
self.log.write('x: %s y: %s block00 %s ERROR??? \n' %((str(self.x)),(str(self.y)),block1_hex))
# debug output
print str(counter)
self.log.write(' minz: %s maxz: %s \n' %(str(self._minz()),str(self._maxz())))
self.image = None
end = time.time()
print str(end-start)
o = Opengl(width = 400, height = 400, double = 1)
o.redraw = self.redraw
o.pack(side = 'top', expand = 1, fill = 'both')
o.mainloop()
return 'foobar'
def header(self):
position = int(self.conv_file.tell())
self.conv_file.seek(0)
mapx = self.conv_file.read(1)
mapx =int(self.convert_to_hex(mapx),16)
mapy = self.conv_file.read(1)
mapy = int(self.convert_to_hex(mapy),16)
self.conv_file.seek(position)
return mapx,mapy
def __init__(self,filename,log=True,image=True):
# init basic vars
self.log=self.logger(filename,log)
self.dump_image=image
self.filename=filename
self.minz=None
self.maxz=None
# open files
self.conv_file = open(self.filename+'.dat','rb')
def __del__(self):
self.conv_file.close()
self.log.write('** File closed\n\n')
########################################################################################################
#
# Main Function
#
########################################################################################################
#field_of_silence = geodata('22_23_conv',False,True)
#field_of_silence.decode()
#field_of_silence = None
#giants_cave = geodata('25_19_conv')
#giants_cave.decode()
#bla1= geodata('22_22_conv')
#bla1.decode()
tower_of_insolence = geodata('23_18_conv')
tower_of_insolence.decode()