16
from cStringIO import StringIO
18
from .djvubzzdec import BZZDecoder
17
from calibre.ebooks.djvu.djvubzzdec import BZZDecoder
18
from calibre.constants import plugins
20
20
class DjvuChunk(object):
21
21
def __init__(self, buf, start, end, align=True, bigendian=True,
22
22
inclheader=False, verbose=0):
23
self.speedup, err = plugins['bzzdec']
24
if self.speedup is None:
25
raise RuntimeError('Failed to load bzzdec plugin: %s' % err)
23
26
self.subtype = None
24
27
self._subchunks = []
65
68
out.write(b'%s%s [%d]\n' % (self.type,
66
69
b':' + self.subtype if self.subtype else b'', self.size))
67
70
if txtout and self.type == b'TXTz':
68
inbuf = StringIO(self.buf[self.datastart: self.dataend])
70
decoder = BZZDecoder(inbuf, outbuf)
72
xxres = decoder.convert(1024 * 1024)
75
res = outbuf.getvalue()
80
if verbose > 0 and out:
82
txtout.write(res[3:3+l])
72
# Use the C BZZ decode implementation
73
txtout.write(self.speedup.decompress(self.buf[self.datastart:self.dataend]))
75
inbuf = bytearray(self.buf[self.datastart: self.dataend])
77
decoder = BZZDecoder(inbuf, outbuf)
79
xxres = decoder.convert(1024 * 1024)
83
if not res.strip(b'\0'):
84
raise ValueError('TXTz block is completely null')
89
if verbose > 0 and out:
91
txtout.write(res[3:3+l])
84
93
if txtout and self.type == b'TXTa':
85
94
res = self.buf[self.datastart: self.dataend]
111
120
self.dc.dump(out=outfile, maxlevel=maxlevel)
114
from ruamel.util.program import Program
115
class DJVUDecoder(Program):
117
Program.__init__(self)
119
def parser_setup(self):
120
Program.parser_setup(self)
121
#self._argparser.add_argument('--combine', '-c', action=CountAction, const=1, nargs=0)
122
#self._argparser.add_argument('--combine', '-c', type=int, default=1)
123
#self._argparser.add_argument('--segments', '-s', action='append', nargs='+')
124
#self._argparser.add_argument('--force', '-f', action='store_true')
125
#self._argparser.add_argument('classname')
126
self._argparser.add_argument('--text', '-t', action='store_true')
127
self._argparser.add_argument('--dump', type=int, default=0)
128
self._argparser.add_argument('file', nargs='+')
131
if self._args.verbose > 1: # can be negative with --quiet
132
print (self._args.file)
133
x = DJVUFile(file(self._args.file[0], 'rb'), verbose=self._args.verbose)
135
print (x.get_text(sys.stdout))
137
x.dump(sys.stdout, maxlevel=self._args.dump)
123
f = DJVUFile(open(sys.argv[-1], 'rb'))
124
print (f.get_text(sys.stdout))
145
126
if __name__ == '__main__':