Fixed the PNG predictor bug. Thanks to Gabor Molnar.

pull/1/head
Yusuke Shinyama 2014-03-24 19:57:05 +09:00
parent 0e7274de1b
commit 636d4caeb3
1 changed files with 14 additions and 13 deletions

View File

@ -16,35 +16,36 @@ def apply_png_predictor(pred, colors, columns, bitspercomponent, data):
i = 0 i = 0
buf = '' buf = ''
line0 = '\x00' * columns line0 = '\x00' * columns
while i < len(data): for i in xrange(0, len(data), nbytes+1):
pred = data[i] ft = data[i]
i += 1 i += 1
line1 = data[i:i+nbytes] line1 = data[i:i+nbytes]
i += nbytes line2 = ''
if pred == '\x00': if ft == '\x00':
# PNG none # PNG none
buf += line1 line2 += line1
elif pred == '\x01': elif ft == '\x01':
# PNG sub (UNTESTED) # PNG sub (UNTESTED)
c = 0 c = 0
for b in line1: for b in line1:
c = (c+ord(b)) & 255 c = (c+ord(b)) & 255
buf += chr(c) line2 += chr(c)
elif pred == '\x02': elif ft == '\x02':
# PNG up # PNG up
for (a, b) in zip(line0, line1): for (a, b) in zip(line0, line1):
c = (ord(a)+ord(b)) & 255 c = (ord(a)+ord(b)) & 255
buf += chr(c) line2 += chr(c)
elif pred == '\x03': elif ft == '\x03':
# PNG average (UNTESTED) # PNG average (UNTESTED)
c = 0 c = 0
for (a, b) in zip(line0, line1): for (a, b) in zip(line0, line1):
c = ((c+ord(a)+ord(b))//2) & 255 c = ((c+ord(a)+ord(b))//2) & 255
buf += chr(c) line2 += chr(c)
else: else:
# unsupported # unsupported
raise ValueError(pred) raise ValueError(ft)
line0 = line1 buf += line2
line0 = line2
return buf return buf