From a05e659536b597012fa8f845812c1256e9f1cab2 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sat, 9 Nov 2024 03:55:47 +1100 Subject: [PATCH] Encode JBIG2 losslessly --- COPYING.APACHE2 | 202 +++++++++++++++++++++++++++++++++++ README.md | 12 ++- file-jbig2pdf.py | 58 +++++----- jbig2topdf_lossless.py | 234 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 469 insertions(+), 37 deletions(-) create mode 100644 COPYING.APACHE2 create mode 100644 jbig2topdf_lossless.py diff --git a/COPYING.APACHE2 b/COPYING.APACHE2 new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/COPYING.APACHE2 @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md index b17f872..860ce2a 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # file-jbig2pdf -GIMP 3.0 plug-in for exporting JBIG2-encoded PDF files +GIMP 3.0 plug-in for exporting lossless JBIG2-encoded PDF files ## Usage -file-jbig2pdf has only been tested on Linux. It requires [jbig2enc](https://github.com/agl/jbig2enc) version 0.28 to be installed, and its *jbig2* and *pdf.py* commands to be available on PATH. +file-jbig2pdf has only been tested on Linux. It requires [jbig2enc](https://github.com/agl/jbig2enc) to be installed, and its *jbig2* command to be available on PATH. -Install *file-jbig2pdf.py* at *$HOME/.config/GIMP/3.0/plug-ins/file-jbig2pdf/file-jbig2pdf.py* and ensure that the file is executable. +Install *file-jbig2pdf.py* and *jbig2topdf_lossless.py* under *$HOME/.config/GIMP/3.0/plug-ins/file-jbig2pdf/* and ensure that *file-jbig2pdf.py* is executable. Set up your GIMP image as desired in the standard fashion for PDF export (one layer per page, bottom layer first) and under *File* → *Export As…*, expand *Select File Type (By Extension)* and select *Portable Document Format (JBIG2)*. @@ -14,7 +14,9 @@ Set up your GIMP image as desired in the standard fashion for PDF export (one la Copyright © 2024 Lee Yingtong Li (RunasSudo) -Loosely adapted from GIMP file-openraster.py – Copyright © 2009 by Jon Nordby \, licensed under the GPLv3\ +*file-jbig2pdf.py* loosely adapted from GIMP file-openraster.py – Copyright © 2009 by Jon Nordby \, licensed under the GPLv3\ In turn based on MyPaint source code by Martin Renold -Licensed under the GNU General Public License version 3 or later. +*jbig2topdf_lossless.py* adapted from jbig2enc by Adam Langley - Copyright (C) 2006 Google Inc., licensed under Apache v2 + +The file-jbig2pdf project is licensed under the GNU General Public License version 3 or later. diff --git a/file-jbig2pdf.py b/file-jbig2pdf.py index 85f300f..e36c646 100755 --- a/file-jbig2pdf.py +++ b/file-jbig2pdf.py @@ -31,19 +31,16 @@ import subprocess import sys import tempfile +import jbig2topdf_lossless + def export_jbig2pdf(procedure, run_mode, image, file, options, metadata, config, data): # Check jbig2enc version try: - jbig2version = subprocess.run(['jbig2', '-V'], capture_output=True) + subprocess.run(['jbig2', '-V']) except FileNotFoundError: Gimp.message('jbig2 command (from jbig2enc) not in PATH') return Gimp.ValueArray.new_from_values([GObject.Value(Gimp.PDBStatusType, Gimp.PDBStatusType.EXECUTION_ERROR)]) - if jbig2version.stderr != b'jbig2enc 0.28\n': - # We only support 0.28 for now, because from 0.29 pdf.py is renamed to jbig2topdf.py - Gimp.message('Unsupported version of jbig2enc (expected 0.28, got "{}")'.format(jbig2version.stderr.decode('utf-8').strip())) - return Gimp.ValueArray.new_from_values([GObject.Value(Gimp.PDBStatusType, Gimp.PDBStatusType.EXECUTION_ERROR)]) - # jbig2enc OK! Gimp.progress_init('Exporting PDF JBIG2 image') @@ -57,7 +54,7 @@ def export_jbig2pdf(procedure, run_mode, image, file, options, metadata, config, for sublayer in unroll_layers(layer.get_children()): yield sublayer - pngs_out = [] + jb2s_out = [] # Export image layer by layer (bottom layer first) for i, layer in enumerate(reversed(list(unroll_layers(image.get_layers())))): @@ -91,33 +88,35 @@ def export_jbig2pdf(procedure, run_mode, image, file, options, metadata, config, tmp_img.delete() # Clean up buffer - if os.path.exists(png_out): - pngs_out.append(png_out) - else: - Gimp.message('Unknown error exporting layer {}'.format(i)) + # Execute jbig2enc + jb2_out = os.path.join(tempdir, '{:03d}.jb2'.format(i)) + + try: + with open(jb2_out, 'wb') as jb2_file: + # Passing "-s" uses lossly JBIG2 encoding, so we do not pass this option + subprocess.run(['jbig2', '-p', '-v', png_out], cwd=tempdir, check=True, stdout=jb2_file) + except subprocess.CalledProcessError: + Gimp.message('Unknown error in jbig2enc') os.rmdir(tempdir) Gimp.progress_end() return Gimp.ValueArray.new_from_values([GObject.Value(Gimp.PDBStatusType, Gimp.PDBStatusType.EXECUTION_ERROR)]) + + os.unlink(png_out) + jb2s_out.append(jb2_out) - # Execute jbig2enc - try: - subprocess.run(['jbig2', '-s', '-p', '-v'] + pngs_out, cwd=tempdir, check=True) - except subprocess.CalledProcessError: - Gimp.message('Unknown error in jbig2enc') - os.rmdir(tempdir) - Gimp.progress_end() - return Gimp.ValueArray.new_from_values([GObject.Value(Gimp.PDBStatusType, Gimp.PDBStatusType.EXECUTION_ERROR)]) - - # Execute pdf.py + # Convert to PDF # Use .tmpsave extension, so we don't overwrite a valid file if there is an exception pdf_tmpout = file.peek_path() + '.tmpsave' + try: with open(pdf_tmpout, 'wb') as pdf_file: - subprocess.run(['pdf.py', 'output'], cwd=tempdir, check=True, stdout=pdf_file) + # The Python script supplied with jbig2enc only supports lossy JBIG2 encoding (https://github.com/agl/jbig2enc/pull/77) + # So therefore we bundle a modified script which works on lossless JBIG2 images + jbig2topdf_lossless.create_pdf(None, jb2s_out, pdf_file) success = True - except subprocess.CalledProcessError: + except Exception: success = False - Gimp.message('Unknown error in jbig2enc pdf.py') + Gimp.message('Unknown error converting JBIG2 images to PDF') os.rmdir(tempdir) Gimp.progress_end() return Gimp.ValueArray.new_from_values([GObject.Value(Gimp.PDBStatusType, Gimp.PDBStatusType.EXECUTION_ERROR)]) @@ -127,16 +126,11 @@ def export_jbig2pdf(procedure, run_mode, image, file, options, metadata, config, # Success! - Finalise and clean up - # Remove intermediate PNG files - for png_out in pngs_out: - os.unlink(png_out) - # Remove intermediate JBIG2 files - for i in range(len(pngs_out)): - os.unlink(os.path.join(tempdir, 'output.{:04d}'.format(i))) + for i in range(len(jb2s_out)): + os.unlink(os.path.join(tempdir, '{:03d}.jb2'.format(i))) - # Remove intermediate JBIG2 symbol file and temp directory - os.unlink(os.path.join(tempdir, 'output.sym')) + # Remove temp directory os.rmdir(tempdir) # Save over destination file diff --git a/jbig2topdf_lossless.py b/jbig2topdf_lossless.py new file mode 100644 index 0000000..1d63353 --- /dev/null +++ b/jbig2topdf_lossless.py @@ -0,0 +1,234 @@ +#!/usr/bin/env python3 + +# GIMP plug-in for JBIG2-encoded PDF files +# Copyright (C) 2024 Lee Yingtong Li (RunasSudo) +# +# Adapted from jbig2enc by Adam Langley - Copyright (C) 2006 Google Inc., licensed under Apache v2 +# Loosely adapted from file-openraster.py - Copyright (C) 2009 by Jon Nordby , licensed under the GPLv3 +# In turn based on MyPaint source code by Martin Renold +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import glob +import struct +import sys +from pathlib import Path + +dpi = 72 # Default DPI value + +class Ref: + def __init__(self, x: int): + self.x = x + + def __str__(self) -> str: + return f"{self.x} 0 R" + +class Dict: + def __init__(self, values: dict = None): + if values is None: + values = {} + self.d = values.copy() + + def __str__(self) -> str: + entries = [f"/{key} {value}" for key, value in self.d.items()] + return f"<< {' '.join(entries)} >>\n" + +class Obj: + next_id = 1 + + def __init__(self, d: dict = None, stream: str = None): + if d is None: + d = {} + if stream is not None: + d["Length"] = str(len(stream)) + self.d = Dict(d) + self.stream = stream + self.id = Obj.next_id + Obj.next_id += 1 + + def __str__(self) -> str: + result = [str(self.d)] + if self.stream is not None: + result.append(f"stream\n{self.stream}\nendstream\n") + result.append("endobj\n") + return "".join(result) + +class Doc: + def __init__(self): + self.objs = [] + self.pages = [] + + def add_object(self, obj: Obj) -> Obj: + """Adds an object to the document.""" + self.objs.append(obj) + return obj + + def add_page(self, page: Obj) -> Obj: + """Adds a page to the document and the list of objects.""" + self.pages.append(page) + return self.add_object(page) + + def __str__(self) -> str: + output = [] + offsets = [] + current_offset = 0 + + def add_line(line: str): + nonlocal current_offset + output.append(line) + current_offset += len(line) + 1 # Adding 1 for the newline character + + # PDF header + add_line("%PDF-1.4") + + # Add each object and track its byte offset + for obj in self.objs: + offsets.append(current_offset) + add_line(f"{obj.id} 0 obj") + add_line(str(obj)) + + # Cross-reference table + xref_start = current_offset + add_line("xref") + add_line(f"0 {len(offsets) + 1}") + add_line("0000000000 65535 f ") + for offset in offsets: + add_line(f"{offset:010} 00000 n ") + + # Trailer and EOF + add_line("trailer") + add_line(f"<< /Size {len(offsets) + 1}\n/Root 1 0 R >>") + add_line("startxref") + add_line(str(xref_start)) + add_line("%%EOF") + + return "\n".join(output) + +def ref(x: int) -> str: + """Creates a PDF reference string.""" + return f"{x} 0 R" + +def create_pdf(symboltable: str = "symboltable", pagefiles: list = None, outf = sys.stdout.buffer): + """Creates a PDF document from a symbol table and a list of page files.""" + if pagefiles is None: + pagefiles = glob.glob("page-*") + + doc = Doc() + + # Add catalog and outlines objects + catalog_obj = Obj({"Type": "/Catalog", "Outlines": ref(2), "Pages": ref(3)}) + outlines_obj = Obj({"Type": "/Outlines", "Count": "0"}) + pages_obj = Obj({"Type": "/Pages"}) + + doc.add_object(catalog_obj) + doc.add_object(outlines_obj) + doc.add_object(pages_obj) + + # Read the symbol table + # RUNASSUDO: Remove this as unnecessary with lossless encoding + #try: + # with open(symboltable, "rb") as sym_file: + # symd = doc.add_object(Obj({}, sym_file.read().decode("latin1"))) + #except IOError: + # sys.stderr.write(f"Error reading symbol table: {symboltable}\n") + # return + + page_objs = [] + pagefiles.sort() + + for p in pagefiles: + try: + with open(p, mode="rb") as page_file: + contents = page_file.read() + except IOError: + sys.stderr.write(f"Error reading page file: {p}\n") + continue + + try: + width, height, xres, yres = struct.unpack(">IIII", contents[11:27]) + except struct.error: + sys.stderr.write(f"Error unpacking page file: {p}\n") + continue + + # Set default resolution if missing + xres = xres or dpi + yres = yres or dpi + + # Create XObject (image) for the page + xobj = Obj( + { + "Type": "/XObject", + "Subtype": "/Image", + "Width": str(width), + "Height": str(height), + "ColorSpace": "/DeviceGray", + "BitsPerComponent": "1", + "Filter": "/JBIG2Decode", + #"DecodeParms": f"<< /JBIG2Globals {symd.id} 0 R >>", # RUNASSUDO: Remove this as unnecessary with lossless encoding + }, + contents.decode("latin1"), + ) + + # Create content stream for the page + contents_obj = Obj( + {}, + f"q {float(width * 72) / xres} 0 0 {float(height * 72) / yres} 0 0 cm /Im1 Do Q", + ) + + # Create resource dictionary for the page + resources_obj = Obj( + {"ProcSet": "[/PDF /ImageB]", "XObject": f"<< /Im1 {xobj.id} 0 R >>"} + ) + + # Create the page object + page_obj = Obj( + { + "Type": "/Page", + "Parent": "3 0 R", + "MediaBox": f"[ 0 0 {float(width * 72) / xres} {float(height * 72) / yres} ]", + "Contents": ref(contents_obj.id), + "Resources": ref(resources_obj.id), + } + ) + + # Add objects to the document + for obj in (xobj, contents_obj, resources_obj, page_obj): + doc.add_object(obj) + + page_objs.append(page_obj) + + # Update pages object + pages_obj.d.d["Count"] = str(len(page_objs)) + pages_obj.d.d["Kids"] = "[" + " ".join([ref(x.id) for x in page_objs]) + "]" + + # Output the final PDF document to stdout + outf.write(str(doc).encode("latin1")) + +def usage(script, msg): + """Display usage information and an optional error message.""" + if msg: + sys.stderr.write("%s: %s\n" % (script, msg)) + sys.stderr.write("Usage: %s [file_basename] > out.pdf\n" % script) + sys.exit(1) + +if __name__ == "__main__": + # RUNASSUDO: Overhauled to use lossless encoding + + pages = sys.argv[1:] + + # Validate that pages were found + if not pages: + usage(sys.argv[0], "no pages found!") + + create_pdf(None, pages)