Skip to content

Commit

Permalink
Redo how we detect the images in a zip/cbr archive
Browse files Browse the repository at this point in the history
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
  • Loading branch information
sahlberg committed Jul 17, 2023
1 parent 37b60e7 commit f1570fe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 38 deletions.
40 changes: 13 additions & 27 deletions document.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
verbose = False


def create_document(source, gameid, maxysize, output):
def create_header(gameid):
def create_document(files, gameid, maxysize, output):
def create_header(gameid, imgs):
buf = bytearray(136)
struct.pack_into('<I', buf, 0, 0x20434F44)
struct.pack_into('<I', buf, 4, 0x10000)
struct.pack_into('<I', buf, 8, 0x10000)
buf[12:21] = bytes(gameid, encoding='utf-8')
struct.pack_into('<I', buf, 28, 1 if len(docs) <= 100 else 1)
struct.pack_into('<I', buf, 28, 1 if len(imgs) <= 100 else 1)
struct.pack_into('<I', buf, 128, 0xffffffff)
struct.pack_into('<I', buf, 132, len(docs))
struct.pack_into('<I', buf, 132, len(imgs))
return buf

def generate_document_entry(f, pos):
Expand All @@ -47,27 +47,13 @@ def generate_png(pic, maxysize):
image.save(f, 'PNG')
return f

docs = []
files.sort()
imgs = []
for i in range(100):
# Look for ...0001...
g = glob.glob(source + '/*' + f'{i:04d}' + '*')
if not g: # try a subdirectory
g = glob.glob(source + '/*/*' + f'{i:04d}' + '*')
if not g:
# Look for ...pag01...
g = glob.glob(source + '/*pag' + f'{i:02d}' + '*')
if not g: # try a subdirectory
g = glob.glob(source + '/*/*pag' + f'{i:02d}' + '*')
# Some archives start page numbers at 1 instead of 0
if not g and i == 0:
print('No page 0 found, skip and try page 1')
for file in files:
try:
pic = Image.open(file)
except:
continue
if not g:
break
docs.append(g[0])

pic = Image.open(g[0])

# images are supposed to be ~square but some scans contain two pages
# side by side. Split them.
Expand All @@ -81,15 +67,15 @@ def generate_png(pic, maxysize):
f = generate_png(pic, maxysize)
imgs.append(f)

if not docs:
if not imgs:
print('No images found. Can not create DOCUMENT.DAT')
return

with open(output, 'wb') as o:
o.write(create_header(gameid)) # size 0x88
o.write(create_header(gameid, imgs)) # size 0x88
for i in range(len(imgs)):
o.write(bytes(128)) # size 0x80
o.write(bytes(8)) # size 0x08, padding
o.write(bytes(128)) # size 0x80
o.write(bytes(8)) # size 0x08, padding

for idx, f in enumerate(imgs):
b = generate_document_entry(f, o.tell())
Expand Down
19 changes: 8 additions & 11 deletions pop-fe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1750,7 +1750,8 @@ def create_manual(source, gameid, subdir='./pop-fe-work/'):
return source

print('Create manual', source)

files = []

if source[:8] != 'https://':
with open(source, 'rb') as f:
buf = f.read(4)
Expand Down Expand Up @@ -1779,31 +1780,27 @@ def create_manual(source, gameid, subdir='./pop-fe-work/'):
if source[-4:] == '.zip':
print('Unzip manual', source, 'from ZIP')
subdir = subdir + '/DOCUMENT-tmp'
try:
os.stat(subdir)
except:
os.mkdir(subdir)
os.mkdir(subdir)
temp_files.append(subdir)

z = zipfile.ZipFile(source)
for f in z.namelist():
f = z.extract(f, path=subdir)
temp_files.append(f)
files.append(f)
source = subdir
if source[-4:] == '.cbr':
print('Unzip manual', source, 'from CBR')
subdir = subdir + '/DOCUMENT-tmp'
try:
os.stat(subdir)
except:
os.mkdir(subdir)
os.mkdir(subdir)
temp_files.append(subdir)

try:
r = rarfile.RarFile(source)
for f in r.namelist():
f = r.extract(f, path=subdir)
temp_files.append(f)
files.append(f)
source = subdir
except:
print('Failed to create SOFTWARE MANUAL. Could not extract images from CBR file. Make sure that UNRAR is installed.')
Expand All @@ -1815,8 +1812,7 @@ def create_manual(source, gameid, subdir='./pop-fe-work/'):

tmpfile = subdir + '/DOCUMENT.DAT'
temp_files.append(tmpfile)
print('Create manual from directory [%s]' % (source))
tmpfile = create_document(source, gameid, 480, tmpfile)
tmpfile = create_document(files, gameid, 480, tmpfile)
if not tmpfile:
print('Failed to create DOCUMENT.DAT')
return tmpfile
Expand Down Expand Up @@ -2272,3 +2268,4 @@ def create_manual(source, gameid, subdir='./pop-fe-work/'):
os.rmdir(f)
except:
True
shutil.rmtree('pop-fe-work', ignore_errors=True)

0 comments on commit f1570fe

Please sign in to comment.