annotate pytouhou/utils/bitstream.pyx @ 522:e8496e5ba056

Add a compilation option to disable anmviewer.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 18 Dec 2013 17:53:16 +0100
parents 1b532e7dd521
children 3c2f96f1d715
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 self.io = io
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 self.bits = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 self.byte = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
22 def __enter__(self):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
23 return self
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
24
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
25
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
26 def __exit__(self, type, value, traceback):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
27 return self.io.__exit__(type, value, traceback)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
28
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
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
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 self.io.seek(offset, whence)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 self.byte = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 self.bits = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
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
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
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
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 self.bits = 8
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 self.bits -= 1
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 return (self.byte >> self.bits) & 0x01
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
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
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
47 cdef unsigned int value = 0, read = 0
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
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
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
51 while nb_bits2:
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
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
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
55 self.bits = 8
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
56 read = self.bits if nb_bits2 > self.bits else nb_bits2
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
57 nb_bits2 -= read
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
58 self.bits -= read
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
59 value |= (self.byte >> self.bits) << nb_bits2
2674c789e0c3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 252
diff changeset
60 return value & ((1 << nb_bits) - 1)
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
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
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64 if self.bits == 8:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 self.io.write(chr(self.byte))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 self.bits = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 self.byte = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68 self.byte &= ~(1 << (7 - self.bits))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 self.byte |= bit << (7 - self.bits)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 self.bits += 1
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
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
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 for i in range(nb_bits):
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75 self.write_bit(bits >> (nb_bits - 1 - i) & 0x01)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
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
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79 self.io.write(chr(self.byte))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80 self.bits = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81 self.byte = 0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 self.io.flush()
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83