Mercurial > touhou
annotate pytouhou/utils/bitstream.pyx @ 397:c5ba11ede097
Don’t duplicate values in sprite rendering data.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 06 Feb 2013 21:41:05 +0100 |
parents | 2674c789e0c3 |
children | efae61ad6efe |
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: |
367 | 16 cdef public object io |
17 cdef unsigned int bits | |
18 cdef unsigned char byte | |
19 cdef bytes bytes | |
252
b5c7369abd7c
Improve data reading perfs
Thibaut Girka <thib@sitedethib.com>
parents:
131
diff
changeset
|
20 |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
21 |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
22 def __init__(BitStream self, io): |
0 | 23 self.io = io |
24 self.bits = 0 | |
25 self.byte = 0 | |
26 | |
27 | |
97 | 28 def __enter__(self): |
29 return self | |
30 | |
31 | |
32 def __exit__(self, type, value, traceback): | |
33 return self.io.__exit__(type, value, traceback) | |
34 | |
35 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
36 def seek(BitStream self, offset, whence=0): |
0 | 37 self.io.seek(offset, whence) |
38 self.byte = 0 | |
39 self.bits = 0 | |
40 | |
41 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
42 def tell(BitStream self): |
0 | 43 return self.io.tell() |
44 | |
45 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
46 def tell2(BitStream self): |
0 | 47 return self.io.tell(), self.bits |
48 | |
49 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
50 cpdef unsigned char read_bit(BitStream self): |
0 | 51 if not self.bits: |
367 | 52 self.bytes = self.io.read(1) |
53 self.byte = (<unsigned char*> self.bytes)[0] | |
0 | 54 self.bits = 8 |
55 self.bits -= 1 | |
56 return (self.byte >> self.bits) & 0x01 | |
57 | |
58 | |
367 | 59 cpdef unsigned int read(BitStream self, unsigned int nb_bits): |
60 cdef unsigned int value = 0, read = 0 | |
61 cdef unsigned int nb_bits2 = nb_bits | |
252
b5c7369abd7c
Improve data reading perfs
Thibaut Girka <thib@sitedethib.com>
parents:
131
diff
changeset
|
62 |
367 | 63 while nb_bits2: |
64 if not self.bits: | |
65 self.bytes = self.io.read(1) | |
66 self.byte = (<unsigned char*> self.bytes)[0] | |
67 self.bits = 8 | |
68 read = self.bits if nb_bits2 > self.bits else nb_bits2 | |
69 nb_bits2 -= read | |
70 self.bits -= read | |
71 value |= (self.byte >> self.bits) << nb_bits2 | |
72 return value & ((1 << nb_bits) - 1) | |
0 | 73 |
74 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
75 cpdef write_bit(BitStream self, bit): |
0 | 76 if self.bits == 8: |
77 self.io.write(chr(self.byte)) | |
78 self.bits = 0 | |
79 self.byte = 0 | |
80 self.byte &= ~(1 << (7 - self.bits)) | |
81 self.byte |= bit << (7 - self.bits) | |
82 self.bits += 1 | |
83 | |
84 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
85 def write(BitStream self, bits, nb_bits): |
0 | 86 for i in range(nb_bits): |
87 self.write_bit(bits >> (nb_bits - 1 - i) & 0x01) | |
88 | |
89 | |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
90 def flush(BitStream self): |
0 | 91 self.io.write(chr(self.byte)) |
92 self.bits = 0 | |
93 self.byte = 0 | |
94 self.io.flush() | |
95 |