pnf.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 # Neatly prints out a node file.  This needs to be kept in sync with node_file.cpp
00003 
00004 import sys
00005 import StringIO
00006 
00007 PRINT_BIG_ENDIAN=1
00008 print "Using %s endian" % ("big" if PRINT_BIG_ENDIAN else "small")
00009 
00010 def b2s(x):
00011     if PRINT_BIG_ENDIAN:
00012         return ''.join(["%02x" % ord(c) for c in reversed(x)])
00013     return ''.join(["%02x" % ord(c) for c in x])
00014 
00015 def childrens(_x):
00016     x = ord(_x)
00017     return "(" + ''.join([str((x >> i) & 1) for i in reversed(range(8))]) + ")"
00018 
00019 def id2s(id, width=0):
00020     full = 0
00021     for i, c in enumerate(id):
00022         full += ord(c) << (i * 8)
00023     return "%06o" % full
00024 
00025 def dump(f):
00026     if type(f) == str:
00027         f = StringIO.StringIO(f)
00028     file_children = f.read(1)
00029     print "File children: %s   %s" % (b2s(file_children), childrens(file_children))
00030     i = 0
00031     print "         x    y    z    rgb     count             C  ShortId"
00032     while True:
00033         x = f.read(2)
00034         if x == "":
00035             break
00036         y = f.read(2)
00037         z = f.read(2)
00038         rgb = f.read(3)
00039         count = f.read(8)
00040         children = f.read(1)
00041 
00042         short_id = f.read(4)
00043         
00044         print "%6d:  %s %s %s %s  %s  %s %s" % \
00045             (i, b2s(x), b2s(y), b2s(z), b2s(rgb),
00046              b2s(count), b2s(children), id2s(short_id))
00047         i += 1
00048 
00049 if len(sys.argv) > 1:
00050     with open(sys.argv[1], 'rb') as f:
00051         dump(f)


megatree_cpp
Author(s): Stuart Glaser
autogenerated on Mon Dec 2 2013 13:01:28