Fixed the PNG predictor bug. Thanks to Gabor Molnar.
parent
0e7274de1b
commit
636d4caeb3
|
@ -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
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue