From 636d4caeb3bc8d41a944b326649f5bd769be5298 Mon Sep 17 00:00:00 2001 From: Yusuke Shinyama Date: Mon, 24 Mar 2014 19:57:05 +0900 Subject: [PATCH] Fixed the PNG predictor bug. Thanks to Gabor Molnar. --- pdfminer/utils.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/pdfminer/utils.py b/pdfminer/utils.py index 5329640..ca1b30f 100644 --- a/pdfminer/utils.py +++ b/pdfminer/utils.py @@ -16,35 +16,36 @@ def apply_png_predictor(pred, colors, columns, bitspercomponent, data): i = 0 buf = '' line0 = '\x00' * columns - while i < len(data): - pred = data[i] + for i in xrange(0, len(data), nbytes+1): + ft = data[i] i += 1 line1 = data[i:i+nbytes] - i += nbytes - if pred == '\x00': + line2 = '' + if ft == '\x00': # PNG none - buf += line1 - elif pred == '\x01': + line2 += line1 + elif ft == '\x01': # PNG sub (UNTESTED) c = 0 for b in line1: c = (c+ord(b)) & 255 - buf += chr(c) - elif pred == '\x02': + line2 += chr(c) + elif ft == '\x02': # PNG up for (a, b) in zip(line0, line1): c = (ord(a)+ord(b)) & 255 - buf += chr(c) - elif pred == '\x03': + line2 += chr(c) + elif ft == '\x03': # PNG average (UNTESTED) c = 0 for (a, b) in zip(line0, line1): c = ((c+ord(a)+ord(b))//2) & 255 - buf += chr(c) + line2 += chr(c) else: # unsupported - raise ValueError(pred) - line0 = line1 + raise ValueError(ft) + buf += line2 + line0 = line2 return buf