All pastes #61491 Raw Edit

Untitled

public python v1 · immutable
#61491 ·published 2006-06-03 23:48 UTC
rendered paste body
#!/usr/bin/pythonimport Imageclass logger:    # constructor    def __init__(self,filename,to_file_bol):        self.dump=to_file_bol        if self.dump:            self.log_file  = open(filename+'.log','wb')            # destructor    def __del__(self):        if self.dump:            self.log_file.close()    ####################################################################################################    #    # writes some_string to the logfile    #    ####################################################################################################    def write(self,some_string):        if self.dump:            self.log_file.write(str(some_string))        else:            print(str(some_string))        return 0########################################################################################################## class for handling geodata# needs geodata file as param# new_geodata = geodata('filename-without-.dat')#########################################################################################################class geodata:    ####################################################################################################    #    # 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        # checking min/max z        self._minz(z)        self._maxz(z)        # return value        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 = []        for item in range(0,64):            z = self._getz() / 2            return_data.append(z)        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()            return_data_sub.append(layercount)            # loop layercount times            if layercount > 20: # whats max layercount ?                z = layercount / 2            else:                for i in range(0,layercount):                    z = self._getz() / 2                    return_data_sub.append(z)            return_data.append(return_data_sub)        return return_data    ####################################################################################################    #    # Decodes the geodata    #    ####################################################################################################    def decode(self):        # open files        self.conv_file = open(self.filename+'.dat','rb')        # the image        self.geoimage = Image.new('RGB', (2048,2048), (92390))         # will be used soon        self.data_all = {}        # we dont need header for now        self.conv_file.seek(18)        data=[]        # make x loop        for x in range(0,2048,8):            # make y loop            for y in range(0,2048,8):                # debug output                self.log.write(' | x %s y: %s \n' %(str(x),str(y)))                # 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('error \n')                elif block1_hex == '00':                    # get data                    z1,z2 = self._handle_block0000()                    color_value = z1+50000                    for s_x in range(0,8):                        for s_y in range(0,8):                            self.geoimage.putpixel((x+s_x,y+s_y),color_value)                    # check for water                    if z1==z2:                        data.append([x,y,z1,'F'])                        self.log.write(' |- block0000 - Floor %s \n' %(str(z1)))                    else:                        self.log.write(' |- block0000 - Water from ocean floor %s to surface %s \n' %(str(z1),str(z2)))                        for item in range(z1,z2,32):                            data.append([x,y,item,'W'])                elif block1_hex == '40':                    self.log.write(" |- block0040 ")                                        z_data = self._handle_block0040()                    counter = 0                    for s_x in range(0,8):                        for s_y in range(0,8):                            self.log.write('%s ' %(z_data[counter]))                            color_value= z_data[counter]+50000                            counter+=1                            self.geoimage.putpixel((x+s_x,y+s_y),color_value)                    self.log.write('\n')                                            elif int(block1_hex,16)>=64 and int(block1_hex,16)<=121:                    z_data = self._handle_block0041()                    # debug output                    self.log.write(' |- block00%s \n' %(block1_hex))                    counter = 0                    for s_x in range(0,8):                        for s_y in range(0,8):                            count=z_data[counter][0]                            self.log.write('%s' %str((count)))                                                                            counter+=1                                                        data = z_data[s_x*s_y]                            color_value = max(data[1:count+1])+50000                            self.geoimage.putpixel((x+s_x,y+s_y),color_value)                    self.log.write('\n')                else:                    self.log.write('block00 %s ??? \n' %block1_hex)                        # self._writel(str(data))        # debug output        self.log.write(' |-- minz = %s \n' %(str(self._minz())))        self.log.write(' |-- maxz = %s \n' %(str(self._maxz())))        # close files        self.conv_file.close()        self.geoimage.save(self.filename+'.png')        return 'foobar'    def __init__(self,filename,log=True,image=True):        # init basic vars        self.log=logger(filename,log)        self.dump_image=image        self.filename=filename        self.minz=None        self.maxz=None########################################################################################################## Main Function#########################################################################################################field_of_silence = geodata('22_23_conv',True)field_of_silence.decode()#giants_cave = geodata('25_19_conv')#giants_cave.decode()#tower_of_insolence = geodata('23_18_conv')#tower_of_insolence.decode()