Skip to content

Commit

Permalink
v2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
sl2c committed Dec 14, 2024
1 parent 56686a8 commit a426d41
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 95 deletions.
37 changes: 25 additions & 12 deletions ccitt.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Group4Decoder(object):
MAKEUP_LOW_BLACK_DECODE = {v:(k+1)*64 for k,v in enumerate(MAKEUP_LOW_BLACK_ENCODE)}

MAKEUP_HIGH_ENCODE = [
'00000001000','00000001100','00000001001','000000010010','000000010011','000000010100','000000010101','000000010110',
'00000001000','00000001100','00000001101','000000010010','000000010011','000000010100','000000010101','000000010110',
'000000010111','000000011100','000000011101','000000011110','000000011111'
]
MAKEUP_HIGH_DECODE = {v:(k + 28)*64 for k,v in enumerate(MAKEUP_HIGH_ENCODE)}
Expand All @@ -83,26 +83,29 @@ def decode(self, data:bytes, Columns:int, EncodedByteAlign:bool = False):
with ends of lines padded with 0-bits to whole bytes, if necessary.
'''

def dump(outBits:str, Columns:int, a0:int, line:int, message:str):
MODES = self.MODES_DECODE
WHITE, BLACK = 0, 1

peek = lambda i: inBits[inPos:inPos+i]
getBit = lambda color: '0' if color == WHITE else '1'

def dump(inBits:str, outBits:str, Columns:int, a0:int, line:int, message:str):
nBytes = (Columns + 7 ) // 8
if a0 < nBytes * 8:
outBits += '0' * (nBytes * 8 - a0)
from PIL import Image, ImageChops
pil = Image.frombytes('1',(Columns, line+1), toBytes(outBits))
ImageChops.invert(pil).save('dump.tif')
raise ValueError(message + '\n' + 'salvaged parts of the image written to dump.tif')

MODES = self.MODES_DECODE
WHITE, BLACK = 0, 1
message += f'\nline = {line}, a0 = {a0}, peek = {peek(24)}'
message += '\nsalvaged parts of the image written to dump.tif'
raise ValueError(message)

toBytes = lambda bits: b''.join(int(bits[i:i+8],2).to_bytes(1,'big') for i in range(0,len(bits),8))

# Bit streams
inBits = ''.join(f'{d:08b}' for d in data)
inPos = 0
outBits = ''
peek = lambda i: inBits[inPos:inPos+i]
getBit = lambda color: '0' if color == WHITE else '1'

b = []
a = []
Expand Down Expand Up @@ -132,8 +135,18 @@ def dump(outBits:str, Columns:int, a0:int, line:int, message:str):
a0 = b2
elif l == 'HOR':
# Horizontal mode
M01, inPos = self.get_run_length(inBits, inPos, color)
M12, inPos = self.get_run_length(inBits, inPos, color^1)
try:
M01, inPos = self.get_run_length(inBits, inPos, color)
except:
clr = 'white' if color == WHITE else 'black'
dump(inBits, outBits, Columns, a0, line, f'failed to get 1st ({clr}) run length')

try:
M12, inPos = self.get_run_length(inBits, inPos, color^1)
except:
clr = 'black' if color == WHITE else 'white'
dump(inBits, outBits, Columns, a0, line, f'failed to get 2nd ({clr}) run length')

outBits += getBit(color)*M01 + getBit(color^1)*M12
a1, a2 = a0 + M01, a0 + M01 + M12
a.append(a1); a.append(a2)
Expand All @@ -155,14 +168,14 @@ def dump(outBits:str, Columns:int, a0:int, line:int, message:str):
if l is None:

if peek(24) != self.EOFB:
dump(outBits, Columns, a0, line, f'unrecognized bits at line = {line}, a0 = {a0}: {peek(24)}')
dump(inBits, outBits, Columns, a0, line, f'unrecognized bits')

if res := len(outBits) % 8:
outBits += '0'*(8-res)
return toBytes(outBits)

if a0 > Columns:
dump(outBits, Columns, a0, line, f'extra bits at line = {line}, a0 = {a0}')
dump(inBits, outBits, Columns, a0, line, f'extra bits at the end of line')

if a0 == Columns:
a0 = -1
Expand Down
18 changes: 18 additions & 0 deletions glyph-lists/my-glyphlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ FL107;2014
Accent;0301
accent;0301
bot;22A5
bar.one;2015
bar.two;2015
check;2713
equaldotleftright;2252
nobreakspace;00A0
Expand Down Expand Up @@ -50,6 +52,7 @@ lessthanorequalangled;2A7D
greaterthanorequalangled;2A7E
integralex;23ae
EurDig;20AC
zero.slash;0030
# ============================================== Numbers
0;0030
1;0031
Expand Down Expand Up @@ -873,20 +876,28 @@ VertBar1;007c
AE;00C6
Complex;2102
Delta;2206
Delta1;2206
Gamma;0393
Gamma1;0393
Ifractur;2111
Lambda;039B
Lambda1;039B
Natural;2115
OE;0152
Omega;2126
Omega1;2126
Oslash;00D8
Phi;03A6
Phi1;03A6
Pi;03A0
Psi;03A8
Psi1;03A8
Real;211D
Rfractur;211C
Sigma;03A3
Sigma1;03A3
Theta;0398
Theta1;0398
Upsilon;03A5
Xi;039E
Zinteger;2124
Expand Down Expand Up @@ -983,8 +994,11 @@ braceleftbig8;007B
braceleftbig9;007B
braceleftbigg;007B
braceleftbt;23A9
braceleft.bot;23A9
braceleftmid;23A8
braceleft.mid;23A8
bracelefttp;23A7
braceleft.top;23A7
braceright;007D
bracerightBig;007D
bracerightBigg;007D
Expand All @@ -1000,8 +1014,11 @@ bracerightbig8;007D
bracerightbig9;007D
bracerightbigg;007D
bracerightbt;23AD
braceright.bot;23AD
bracerightmid;23AC
braceright.mid;23AC
bracerighttp;23AB
braceright.top;23AB
bracketleftBig;005B
bracketleftBigg;005B
bracketleftbig;005B
Expand Down Expand Up @@ -1101,6 +1118,7 @@ endash;2013
epsilon1;1D716
#epsilon1;03B5
epsilon;03B5
equal1;003D
equivalence;2261
equivasymptotic;224D
eta;03B7
Expand Down
Loading

0 comments on commit a426d41

Please sign in to comment.