forked from caj2pdf/caj2pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jbigdec.py
executable file
·82 lines (65 loc) · 2.51 KB
/
jbigdec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# Copyright 2020-2021 (c) Hin-Tak Leung <[email protected]>
# See The FreeType Project LICENSE for license terms.
#
# python ctypes module and short program decodes the image data in a CAJ file.
# To build, copy "libreaderex_x64.so" from the Ubuntu AppImage
# to the current directory.
# (See "Analysing libreaderex" in the Wiki on how to)
#
# Then, run
#
# cc -Wall -fPIC --shared -o libjbigdec.so jbigdec.cc JBigDecode.cc
from ctypes import *
import os
import struct
import platform
arch = platform.architecture()
if (arch[1] == 'WindowsPE'):
if (arch[0] == '64bit'):
libjbigdec = cdll.LoadLibrary("./lib/bin/libjbigdec-w64.dll")
else:
libjbigdec = cdll.LoadLibrary("./lib/bin/libjbigdec-w32.dll")
else:
libjbigdec = cdll.LoadLibrary("./libjbigdec.so")
#SaveJbigAsBmp = libjbigdec.SaveJbigAsBmp
#SaveJbigAsBmp.restype = None
#SaveJbigAsBmp.argtypes = [c_void_p, c_int, c_char_p]
#SaveJbig2AsBmp = libjbigdec.SaveJbig2AsBmp
#SaveJbig2AsBmp.restype = None
#SaveJbig2AsBmp.argtypes = [c_void_p, c_int, c_char_p]
jbigDecode = libjbigdec.jbigDecode
jbigDecode.restype = None
jbigDecode.argtypes = [c_void_p, c_int, c_int, c_int, c_int, c_void_p]
class CImage:
def __init__(self, buffer):
self.buffer = buffer
self.buffer_size=len(buffer)
(self.width, self.height,
self.num_planes, self.bits_per_pixel) = struct.unpack("<IIHH", buffer[4:16])
self.bytes_per_line = ((self.width * self.bits_per_pixel + 31) >> 5) << 2
def DecodeJbig(self):
out = create_string_buffer(self.height * self.bytes_per_line)
jbigDecode(self.buffer[48:], self.buffer_size-48, self.height, self.width, self.bytes_per_line, out)
return out
if __name__ == '__main__':
import sys, os
if len(sys.argv) < 3:
print("Usage: %s input output" % sys.argv[0])
sys.exit()
f = open(sys.argv[1], "rb")
buffer_size = os.stat(sys.argv[1]).st_size
buffer = f.read()
#SaveJbigAsBmp(buffer, buffer_size, sys.argv[2].encode("ascii"))
cimage = CImage(buffer)
out = cimage.DecodeJbig()
# PBM is only padded to 8 rather than 32.
# If the padding is larger, write padded file.
width = cimage.width
if (cimage.bytes_per_line > ((cimage.width +7) >> 3)):
width = cimage.bytes_per_line << 3
fout = open(sys.argv[2].replace(".bmp", ".pbm"), "wb")
fout.write("P4\n".encode("ascii"))
fout.write(("%d %d\n" % (width, cimage.height)).encode("ascii"))
fout.write(out)
fout.close()