From f24661aff300d769c7901cd7f349084e025f60a1 Mon Sep 17 00:00:00 2001
From: zdenop <zdenop@gmail.com>
Date: Tue, 3 Jul 2012 20:58:23 +0200
Subject: [PATCH] retains DPI from input images (thanks to Steven Lee)

---
 AUTHORS   |  4 ++++
 ChangeLog |  3 ++-
 pdf.py    | 31 +++++++++++++++++++++++++++----
 3 files changed, 33 insertions(+), 5 deletions(-)
 mode change 100644 => 100755 AUTHORS

diff --git a/AUTHORS b/AUTHORS
old mode 100644
new mode 100755
index 322fa80..75b0cac
--- a/AUTHORS
+++ b/AUTHORS
@@ -1,5 +1,9 @@
+Author:
+=======
 Adam Langley <agl@imperialviolet.org>
 
 Contributors:
+=============
 Misty De Meo <mistydemeo@gmail.com>
 zdenop <zdenop@gmail.com>
+Steven Lee http://www.rubypdf.com
diff --git a/ChangeLog b/ChangeLog
index d517444..4829a6f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,9 +1,10 @@
 0.28: (not yet released)
   * Update to the latest Leptonica (1.68)
-  * pdf.py now correctly retains DPI from input images (thanks to RubyPDF)
   * autotools support, VC++ 2008 solution
   * fix binary file open mode on Windows
   * version info (-V --version)
+  * pdf.py now correctly retains DPI from input images (thanks to Steven Lee
+    http://blog.rubypdf.com/2011/09/09/jbig2-pdf-py-patch-the-right-way-to-get-dpi/)
 
 0.27:
   * Update to the latest Leptonica (1.58)
diff --git a/pdf.py b/pdf.py
index a3c5f0a..9237141 100755
--- a/pdf.py
+++ b/pdf.py
@@ -1,4 +1,21 @@
 #!/usr/bin/python
+# Copyright 2006 Google Inc.
+# Author: agl@imperialviolet.org (Adam Langley)
+#
+# 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.
+
+# JBIG2 Encoder
+# https://github.com/agl/jbig2enc
 
 import sys
 import re
@@ -11,7 +28,7 @@
 # Run ./jbig2 -s -p <other options> image1.jpeg image1.jpeg ...
 # python pdf.py output > out.pdf
 
-dpi = 600
+dpi = 72
 
 class Ref:
   def __init__(self, x):
@@ -121,16 +138,22 @@ def main(symboltable='symboltable', pagefiles=glob.glob('page-*')):
     except IOError:
       sys.stderr.write("error reading page file %s\n"% p)
       continue
-    (width, height) = struct.unpack('>II', contents[11:19])
+    (width, height, xres, yres) = struct.unpack('>IIII', contents[11:27])
+
+    if xres == 0:
+        xres = dpi
+    if yres == 0:
+        yres = dpi
+
     xobj = Obj({'Type': '/XObject', 'Subtype': '/Image', 'Width':
         str(width), 'Height': str(height), 'ColorSpace': '/DeviceGray',
         'BitsPerComponent': '1', 'Filter': '/JBIG2Decode', 'DecodeParms':
         ' << /JBIG2Globals %d 0 R >>' % symd.id}, contents)
-    contents = Obj({}, 'q %f 0 0 %f 0 0 cm /Im1 Do Q' % (float(width * 72) / dpi, float(height * 72) / dpi))
+    contents = Obj({}, 'q %f 0 0 %f 0 0 cm /Im1 Do Q' % (float(width * 72) / xres, float(height * 72) / yres))
     resources = Obj({'ProcSet': '[/PDF /ImageB]',
         'XObject': '<< /Im1 %d 0 R >>' % xobj.id})
     page = Obj({'Type': '/Page', 'Parent': '3 0 R',
-        'MediaBox': '[ 0 0 %f %f ]' % (float(width * 72) / dpi, float(height * 72) / dpi),
+        'MediaBox': '[ 0 0 %f %f ]' % (float(width * 72) / xres, float(height * 72) / yres),
         'Contents': ref(contents.id),
         'Resources': ref(resources.id)})
     [doc.add_object(x) for x in [xobj, contents, resources, page]]