Mercurial > touhou
annotate pytouhou/utils/bitstream.pyx @ 362:3be4c1078095
(partially?) implement ECL's instruction 133
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Mon, 09 Jul 2012 21:12:10 +0200 |
parents | b5c7369abd7c |
children | 2674c789e0c3 |
rev | line source |
---|---|
52
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
2 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
4 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
8 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
12 ## GNU General Public License for more details. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
13 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
0
diff
changeset
|
14 |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
15 cdef class BitStream: |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
16 cdef public io |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
17 cdef public int bits |
252
b5c7369abd7c
Improve data reading perfs
Thibaut Girka <thib@sitedethib.com>
parents:
131
diff
changeset
|
18 cdef public unsigned char byte |
b5c7369abd7c
Improve data reading perfs
Thibaut Girka <thib@sitedethib.com>
parents:
131
diff
changeset
|
19 |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
20 |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
21 def __init__(BitStream self, io): |
0 | 22 self.io = io |
23 self.bits = 0 | |
24 self.byte = 0 | |
25 | |
26 | |
97 | 27 def __enter__(self): |
28 return self | |
29 | |
30 | |
31 def __exit__(self, type, value, traceback): | |
32 return self.io.__exit__(type, value, traceback) | |
33 | |
34 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
35 def seek(BitStream self, offset, whence=0): |
0 | 36 self.io.seek(offset, whence) |
37 self.byte = 0 | |
38 self.bits = 0 | |
39 | |
40 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
41 def tell(BitStream self): |
0 | 42 return self.io.tell() |
43 | |
44 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
45 def tell2(BitStream self): |
0 | 46 return self.io.tell(), self.bits |
47 | |
48 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
49 cpdef unsigned char read_bit(BitStream self): |
0 | 50 if not self.bits: |
51 self.byte = ord(self.io.read(1)) | |
52 self.bits = 8 | |
53 self.bits -= 1 | |
54 return (self.byte >> self.bits) & 0x01 | |
55 | |
56 | |
252
b5c7369abd7c
Improve data reading perfs
Thibaut Girka <thib@sitedethib.com>
parents:
131
diff
changeset
|
57 cpdef unsigned int read(BitStream self, int nb_bits): |
b5c7369abd7c
Improve data reading perfs
Thibaut Girka <thib@sitedethib.com>
parents:
131
diff
changeset
|
58 cdef unsigned int value = 0 |
b5c7369abd7c
Improve data reading perfs
Thibaut Girka <thib@sitedethib.com>
parents:
131
diff
changeset
|
59 cdef int i |
b5c7369abd7c
Improve data reading perfs
Thibaut Girka <thib@sitedethib.com>
parents:
131
diff
changeset
|
60 |
0 | 61 for i in range(nb_bits - 1, -1, -1): |
62 value |= self.read_bit() << i | |
63 return value | |
64 | |
65 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
66 cpdef write_bit(BitStream self, bit): |
0 | 67 if self.bits == 8: |
68 self.io.write(chr(self.byte)) | |
69 self.bits = 0 | |
70 self.byte = 0 | |
71 self.byte &= ~(1 << (7 - self.bits)) | |
72 self.byte |= bit << (7 - self.bits) | |
73 self.bits += 1 | |
74 | |
75 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
76 def write(BitStream self, bits, nb_bits): |
0 | 77 for i in range(nb_bits): |
78 self.write_bit(bits >> (nb_bits - 1 - i) & 0x01) | |
79 | |
80 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
81 def flush(BitStream self): |
0 | 82 self.io.write(chr(self.byte)) |
83 self.bits = 0 | |
84 self.byte = 0 | |
85 self.io.flush() | |
86 |