Mercurial > touhou
annotate pytouhou/utils/bitstream.pyx @ 469:58b47e788c59
Move SDL context-manager and Window creation outside of main.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 14 Sep 2013 01:05:42 +0200 |
parents | feecdb4a8928 |
children | 1b532e7dd521 |
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 |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
367
diff
changeset
|
22 def __init__(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 | |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
367
diff
changeset
|
36 def seek(self, offset, whence=0): |
0 | 37 self.io.seek(offset, whence) |
38 self.byte = 0 | |
39 self.bits = 0 | |
40 | |
41 | |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
367
diff
changeset
|
42 def tell(self): |
0 | 43 return self.io.tell() |
44 | |
45 | |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
367
diff
changeset
|
46 def tell2(self): |
0 | 47 return self.io.tell(), self.bits |
48 | |
49 | |
468
feecdb4a8928
Add “except *” to cdef void functions, and type some more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
50 cpdef unsigned char read_bit(self) except? -1: |
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 | |
468
feecdb4a8928
Add “except *” to cdef void functions, and type some more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
417
diff
changeset
|
59 cpdef unsigned int read(self, unsigned int nb_bits) except? -1: |
367 | 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 | |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
367
diff
changeset
|
75 cpdef write_bit(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 | |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
367
diff
changeset
|
85 def write(self, bits, nb_bits): |
0 | 86 for i in range(nb_bits): |
87 self.write_bit(bits >> (nb_bits - 1 - i) & 0x01) | |
88 | |
89 | |
417
efae61ad6efe
Remove the type of the self argument in extension types, as it clutters the code with useless information.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
367
diff
changeset
|
90 def flush(self): |
0 | 91 self.io.write(chr(self.byte)) |
92 self.bits = 0 | |
93 self.byte = 0 | |
94 self.io.flush() | |
95 |