All pastes #61721 Raw Edit

Stuff

public text v1 · immutable
#61721 ·published 2006-06-04 16:02 UTC
rendered paste body
#!/usr/bin/python

import Image
import time

psyco_enabled=True
try:
    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 = Image.new('RGB', (2048,2048), (52390))

        def __del__(self):
            self.image.save(self.filename+'.png')

    ####################################################################################################
    #
    # converts bytes hex values
    #
    ####################################################################################################
    def convert_to_hex(self,some_byte):
        if len(some_byte)==0:
            return 'FF'
        charval = ord(some_byte)
        hexval = hex(charval)
        hexstrlen = len(hexval)
        startpos = hexstrlen - 2
        if hexval[startpos] == 'x':
            startpos += 1
            return_hex = '0'+hexval[startpos]
        else:
            return_hex = hexval[startpos:hexstrlen]
        return return_hex

    ####################################################################################################
    #
    # 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):
        # read bytes
        z2 = self.convert_to_hex(self.conv_file.read(1))
        z1 = self.convert_to_hex(self.conv_file.read(1))        
        # converts to int                                                                
        z = int(z1+z2,16)
        # get the real z1
        if z > 55000:
            z = z - 65336
        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):
        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)
                              
        go_on=False
        while go_on==False:
            position = int(self.conv_file.tell())
            testbyte1 = self.conv_file.read(1)
            testbyte2 = self.conv_file.read(1)
            if self.convert_to_hex(testbyte2) == '00':
                go_on=True
        self.conv_file.seek(position)
        return return_data

    ####################################################################################################
    #
    # Decodes the geodata
    #
    ####################################################################################################
    def decode(self):
#        self.handle_block = psyco.proxy(f)
        start = time.time()
        if self.dump_image:
            self.image = self.geoimage(self.filename)
        else:
            self.image =False
        self.conv_file.seek(18)
        data=[]
        # make x loop
        for self.x in range(0,2048,8):
            # make y loop
            for self.y in range(0,2048,8):
                # reads data header
                block1 = self.conv_file.read(1)
                block2 = self.conv_file.read(1)
                block1_hex = self.convert_to_hex(block1)
                # handles the data
                if block1_hex   == 'ff':
                    self.log.write('x: %s y: %s block: 00 ERROR \n' %(str(self.x),str(self.y)))                    

                elif block1_hex == '00':
                    # 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:
                        data.append([self.x,self.y,z1,'F'])
                    else:
                        for item in range(z1,z2,32):
                            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 block1_hex == '40':
                    z_data = self._handle_block0040()
                    self.log.write('x: %s y: %s block: 40 ok \n' %(str(self.x),str(self.y)))                    

                elif int(block1_hex,16)>=64 and int(block1_hex,16)<=254:
                    z_data = self._handle_block0041()
                    if self.image: self.image.putpixel8x8(self.x,self.y,z_data)                    
                    self.log.write('x: %s y: %s block: %s ok \n' %(str(self.x),str(self.y),block1_hex))                                        
                else:
                    self.log.write('x: %s y: %s block00 %s ERROR??? \n' %(self(str.x),self(str.y),block1_hex))
                    
        # debug output
        self.log.write(' minz: %s maxz: %s \n' %(str(self._minz()),str(self._maxz())))
        self.image = None
        end = time.time()
        print str(end-start)
        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,False)
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()