Mercurial > touhou
annotate pytouhou/utils/bitstream.pyx @ 596:ab131d04987d
Fix a regression introduced in the previous commit when the interface has a different resolution than 640×480 and framebuffer_blit is enabled.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 25 Oct 2014 18:49:41 +0200 |
parents | 1b532e7dd521 |
children | 3c2f96f1d715 |
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: |
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
|
16 def __init__(self, io): |
0 | 17 self.io = io |
18 self.bits = 0 | |
19 self.byte = 0 | |
20 | |
21 | |
97 | 22 def __enter__(self): |
23 return self | |
24 | |
25 | |
26 def __exit__(self, type, value, traceback): | |
27 return self.io.__exit__(type, value, traceback) | |
28 | |
29 | |
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
|
30 def seek(self, offset, whence=0): |
0 | 31 self.io.seek(offset, whence) |
32 self.byte = 0 | |
33 self.bits = 0 | |
34 | |
35 | |
490
1b532e7dd521
Decrease PBG3 loading time by improving lzss and bitstream integration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
468
diff
changeset
|
36 cdef bint read_bit(self): |
1b532e7dd521
Decrease PBG3 loading time by improving lzss and bitstream integration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
468
diff
changeset
|
37 cdef bytes byte |
0 | 38 if not self.bits: |
490
1b532e7dd521
Decrease PBG3 loading time by improving lzss and bitstream integration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
468
diff
changeset
|
39 byte = self.io.read(1) |
1b532e7dd521
Decrease PBG3 loading time by improving lzss and bitstream integration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
468
diff
changeset
|
40 self.byte = (<unsigned char*>byte)[0] |
0 | 41 self.bits = 8 |
42 self.bits -= 1 | |
43 return (self.byte >> self.bits) & 0x01 | |
44 | |
45 | |
490
1b532e7dd521
Decrease PBG3 loading time by improving lzss and bitstream integration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
468
diff
changeset
|
46 cpdef unsigned int read(self, unsigned int nb_bits): |
367 | 47 cdef unsigned int value = 0, read = 0 |
48 cdef unsigned int nb_bits2 = nb_bits | |
490
1b532e7dd521
Decrease PBG3 loading time by improving lzss and bitstream integration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
468
diff
changeset
|
49 cdef bytes byte |
252
b5c7369abd7c
Improve data reading perfs
Thibaut Girka <thib@sitedethib.com>
parents:
131
diff
changeset
|
50 |
367 | 51 while nb_bits2: |
52 if not self.bits: | |
490
1b532e7dd521
Decrease PBG3 loading time by improving lzss and bitstream integration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
468
diff
changeset
|
53 byte = self.io.read(1) |
1b532e7dd521
Decrease PBG3 loading time by improving lzss and bitstream integration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
468
diff
changeset
|
54 self.byte = (<unsigned char*>byte)[0] |
367 | 55 self.bits = 8 |
56 read = self.bits if nb_bits2 > self.bits else nb_bits2 | |
57 nb_bits2 -= read | |
58 self.bits -= read | |
59 value |= (self.byte >> self.bits) << nb_bits2 | |
60 return value & ((1 << nb_bits) - 1) | |
0 | 61 |
62 | |
490
1b532e7dd521
Decrease PBG3 loading time by improving lzss and bitstream integration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
468
diff
changeset
|
63 cpdef write_bit(self, bint bit): |
0 | 64 if self.bits == 8: |
65 self.io.write(chr(self.byte)) | |
66 self.bits = 0 | |
67 self.byte = 0 | |
68 self.byte &= ~(1 << (7 - self.bits)) | |
69 self.byte |= bit << (7 - self.bits) | |
70 self.bits += 1 | |
71 | |
72 | |
490
1b532e7dd521
Decrease PBG3 loading time by improving lzss and bitstream integration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
468
diff
changeset
|
73 cpdef write(self, unsigned int bits, unsigned int nb_bits): |
0 | 74 for i in range(nb_bits): |
75 self.write_bit(bits >> (nb_bits - 1 - i) & 0x01) | |
76 | |
77 | |
490
1b532e7dd521
Decrease PBG3 loading time by improving lzss and bitstream integration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
468
diff
changeset
|
78 cpdef flush(self): |
0 | 79 self.io.write(chr(self.byte)) |
80 self.bits = 0 | |
81 self.byte = 0 | |
82 self.io.flush() | |
83 |