1.1 released, comes with damaged flag
This commit is contained in:
parent
5f77ef9d94
commit
3e8fc078db
|
@ -9,28 +9,33 @@ class color_types(object):
|
|||
GREYSCALE_WITH_ALPHA = 4
|
||||
RGB_WITH_ALPHA = 6
|
||||
class PME(object):
|
||||
def _assert(self, statement):
|
||||
if not self._damaged:
|
||||
assert(statement)
|
||||
def _int(self, binary):
|
||||
return int.from_bytes(binary, byteorder="big")
|
||||
def _bytes(self, integer, length):
|
||||
return integer.to_bytes(length, "big")
|
||||
def __init__(self, filename=False):
|
||||
def __init__(self, filename=False, damaged=False):
|
||||
self._damaged = damaged
|
||||
self._init = False
|
||||
self._magic_number = b'\x89PNG\r\n\x1a\n'
|
||||
self.filename = filename
|
||||
self.chunks = []
|
||||
if filename:
|
||||
f = open(self.filename, "rb")
|
||||
assert(f.read(8) == self._magic_number)
|
||||
self._assert(f.read(8) == self._magic_number)
|
||||
while True:
|
||||
length = f.read(4)
|
||||
label = f.read(4)
|
||||
data = f.read(self._int(length))
|
||||
crc = f.read(4)
|
||||
if not self._damaged:
|
||||
self._verify_crc(label, data, crc)
|
||||
self.chunks.append([length, label, data, crc])
|
||||
if label == b"IEND":
|
||||
break
|
||||
assert(self.chunks[0][1] == b"IHDR")
|
||||
self._assert(self.chunks[0][1] == b"IHDR")
|
||||
self.recalculate_properties()
|
||||
else:
|
||||
self.chunks = [
|
||||
|
|
|
@ -128,3 +128,8 @@ In this example, we will draw a red sine wave on a new 100x100 file
|
|||
|
||||
The output should look something like this:
|
||||
![](http://i.imgur.com/vfa6lOR.png)
|
||||
|
||||
## Changelog
|
||||
|
||||
### 1.1
|
||||
Added the damaged argument to the constructor. If damaged is set to true, it will allow you to open potentially damaged png files, which maybe don't have IDAT as the first chunk, or have invalid CRCs
|
||||
|
|
Loading…
Reference in New Issue