Mercurial > touhou
comparison pytouhou/utils/bitstream.pyx @ 490:1b532e7dd521
Decrease PBG3 loading time by improving lzss and bitstream integration.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 04 Oct 2013 14:28:49 +0200 |
parents | feecdb4a8928 |
children | 3c2f96f1d715 |
comparison
equal
deleted
inserted
replaced
489:59bd29568753 | 490:1b532e7dd521 |
---|---|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 ## GNU General Public License for more details. | 12 ## GNU General Public License for more details. |
13 ## | 13 ## |
14 | 14 |
15 cdef class BitStream: | 15 cdef class BitStream: |
16 cdef public object io | |
17 cdef unsigned int bits | |
18 cdef unsigned char byte | |
19 cdef bytes bytes | |
20 | |
21 | |
22 def __init__(self, io): | 16 def __init__(self, io): |
23 self.io = io | 17 self.io = io |
24 self.bits = 0 | 18 self.bits = 0 |
25 self.byte = 0 | 19 self.byte = 0 |
26 | 20 |
37 self.io.seek(offset, whence) | 31 self.io.seek(offset, whence) |
38 self.byte = 0 | 32 self.byte = 0 |
39 self.bits = 0 | 33 self.bits = 0 |
40 | 34 |
41 | 35 |
42 def tell(self): | 36 cdef bint read_bit(self): |
43 return self.io.tell() | 37 cdef bytes byte |
44 | |
45 | |
46 def tell2(self): | |
47 return self.io.tell(), self.bits | |
48 | |
49 | |
50 cpdef unsigned char read_bit(self) except? -1: | |
51 if not self.bits: | 38 if not self.bits: |
52 self.bytes = self.io.read(1) | 39 byte = self.io.read(1) |
53 self.byte = (<unsigned char*> self.bytes)[0] | 40 self.byte = (<unsigned char*>byte)[0] |
54 self.bits = 8 | 41 self.bits = 8 |
55 self.bits -= 1 | 42 self.bits -= 1 |
56 return (self.byte >> self.bits) & 0x01 | 43 return (self.byte >> self.bits) & 0x01 |
57 | 44 |
58 | 45 |
59 cpdef unsigned int read(self, unsigned int nb_bits) except? -1: | 46 cpdef unsigned int read(self, unsigned int nb_bits): |
60 cdef unsigned int value = 0, read = 0 | 47 cdef unsigned int value = 0, read = 0 |
61 cdef unsigned int nb_bits2 = nb_bits | 48 cdef unsigned int nb_bits2 = nb_bits |
49 cdef bytes byte | |
62 | 50 |
63 while nb_bits2: | 51 while nb_bits2: |
64 if not self.bits: | 52 if not self.bits: |
65 self.bytes = self.io.read(1) | 53 byte = self.io.read(1) |
66 self.byte = (<unsigned char*> self.bytes)[0] | 54 self.byte = (<unsigned char*>byte)[0] |
67 self.bits = 8 | 55 self.bits = 8 |
68 read = self.bits if nb_bits2 > self.bits else nb_bits2 | 56 read = self.bits if nb_bits2 > self.bits else nb_bits2 |
69 nb_bits2 -= read | 57 nb_bits2 -= read |
70 self.bits -= read | 58 self.bits -= read |
71 value |= (self.byte >> self.bits) << nb_bits2 | 59 value |= (self.byte >> self.bits) << nb_bits2 |
72 return value & ((1 << nb_bits) - 1) | 60 return value & ((1 << nb_bits) - 1) |
73 | 61 |
74 | 62 |
75 cpdef write_bit(self, bit): | 63 cpdef write_bit(self, bint bit): |
76 if self.bits == 8: | 64 if self.bits == 8: |
77 self.io.write(chr(self.byte)) | 65 self.io.write(chr(self.byte)) |
78 self.bits = 0 | 66 self.bits = 0 |
79 self.byte = 0 | 67 self.byte = 0 |
80 self.byte &= ~(1 << (7 - self.bits)) | 68 self.byte &= ~(1 << (7 - self.bits)) |
81 self.byte |= bit << (7 - self.bits) | 69 self.byte |= bit << (7 - self.bits) |
82 self.bits += 1 | 70 self.bits += 1 |
83 | 71 |
84 | 72 |
85 def write(self, bits, nb_bits): | 73 cpdef write(self, unsigned int bits, unsigned int nb_bits): |
86 for i in range(nb_bits): | 74 for i in range(nb_bits): |
87 self.write_bit(bits >> (nb_bits - 1 - i) & 0x01) | 75 self.write_bit(bits >> (nb_bits - 1 - i) & 0x01) |
88 | 76 |
89 | 77 |
90 def flush(self): | 78 cpdef flush(self): |
91 self.io.write(chr(self.byte)) | 79 self.io.write(chr(self.byte)) |
92 self.bits = 0 | 80 self.bits = 0 |
93 self.byte = 0 | 81 self.byte = 0 |
94 self.io.flush() | 82 self.io.flush() |
95 | 83 |