comparison pytouhou/utils/bitstream.pyx @ 417:efae61ad6efe

Remove the type of the self argument in extension types, as it clutters the code with useless information.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 22 Aug 2013 12:21:12 +0200
parents 2674c789e0c3
children feecdb4a8928
comparison
equal deleted inserted replaced
416:e23871eddb7a 417:efae61ad6efe
17 cdef unsigned int bits 17 cdef unsigned int bits
18 cdef unsigned char byte 18 cdef unsigned char byte
19 cdef bytes bytes 19 cdef bytes bytes
20 20
21 21
22 def __init__(BitStream self, io): 22 def __init__(self, io):
23 self.io = io 23 self.io = io
24 self.bits = 0 24 self.bits = 0
25 self.byte = 0 25 self.byte = 0
26 26
27 27
31 31
32 def __exit__(self, type, value, traceback): 32 def __exit__(self, type, value, traceback):
33 return self.io.__exit__(type, value, traceback) 33 return self.io.__exit__(type, value, traceback)
34 34
35 35
36 def seek(BitStream self, offset, whence=0): 36 def seek(self, offset, whence=0):
37 self.io.seek(offset, whence) 37 self.io.seek(offset, whence)
38 self.byte = 0 38 self.byte = 0
39 self.bits = 0 39 self.bits = 0
40 40
41 41
42 def tell(BitStream self): 42 def tell(self):
43 return self.io.tell() 43 return self.io.tell()
44 44
45 45
46 def tell2(BitStream self): 46 def tell2(self):
47 return self.io.tell(), self.bits 47 return self.io.tell(), self.bits
48 48
49 49
50 cpdef unsigned char read_bit(BitStream self): 50 cpdef unsigned char read_bit(self):
51 if not self.bits: 51 if not self.bits:
52 self.bytes = self.io.read(1) 52 self.bytes = self.io.read(1)
53 self.byte = (<unsigned char*> self.bytes)[0] 53 self.byte = (<unsigned char*> self.bytes)[0]
54 self.bits = 8 54 self.bits = 8
55 self.bits -= 1 55 self.bits -= 1
56 return (self.byte >> self.bits) & 0x01 56 return (self.byte >> self.bits) & 0x01
57 57
58 58
59 cpdef unsigned int read(BitStream self, unsigned int nb_bits): 59 cpdef unsigned int read(self, unsigned int nb_bits):
60 cdef unsigned int value = 0, read = 0 60 cdef unsigned int value = 0, read = 0
61 cdef unsigned int nb_bits2 = nb_bits 61 cdef unsigned int nb_bits2 = nb_bits
62 62
63 while nb_bits2: 63 while nb_bits2:
64 if not self.bits: 64 if not self.bits:
70 self.bits -= read 70 self.bits -= read
71 value |= (self.byte >> self.bits) << nb_bits2 71 value |= (self.byte >> self.bits) << nb_bits2
72 return value & ((1 << nb_bits) - 1) 72 return value & ((1 << nb_bits) - 1)
73 73
74 74
75 cpdef write_bit(BitStream self, bit): 75 cpdef write_bit(self, bit):
76 if self.bits == 8: 76 if self.bits == 8:
77 self.io.write(chr(self.byte)) 77 self.io.write(chr(self.byte))
78 self.bits = 0 78 self.bits = 0
79 self.byte = 0 79 self.byte = 0
80 self.byte &= ~(1 << (7 - self.bits)) 80 self.byte &= ~(1 << (7 - self.bits))
81 self.byte |= bit << (7 - self.bits) 81 self.byte |= bit << (7 - self.bits)
82 self.bits += 1 82 self.bits += 1
83 83
84 84
85 def write(BitStream self, bits, nb_bits): 85 def write(self, bits, nb_bits):
86 for i in range(nb_bits): 86 for i in range(nb_bits):
87 self.write_bit(bits >> (nb_bits - 1 - i) & 0x01) 87 self.write_bit(bits >> (nb_bits - 1 - i) & 0x01)
88 88
89 89
90 def flush(BitStream self): 90 def flush(self):
91 self.io.write(chr(self.byte)) 91 self.io.write(chr(self.byte))
92 self.bits = 0 92 self.bits = 0
93 self.byte = 0 93 self.byte = 0
94 self.io.flush() 94 self.io.flush()
95 95