Read the next PNG chunk from the input file; returns a (*type*, *data*) tuple. *type* is the chunk's type as a byte string (all PNG chunk types are 4 bytes long). *data* is the chunk's data content, as a byte string. If the optional `lenient` argumen
(self, lenient=False)
| 1356 | raise ProtocolError("expecting filename, file or bytes array") |
| 1357 | |
| 1358 | def chunk(self, lenient=False): |
| 1359 | """ |
| 1360 | Read the next PNG chunk from the input file; |
| 1361 | returns a (*type*, *data*) tuple. |
| 1362 | *type* is the chunk's type as a byte string |
| 1363 | (all PNG chunk types are 4 bytes long). |
| 1364 | *data* is the chunk's data content, as a byte string. |
| 1365 | |
| 1366 | If the optional `lenient` argument evaluates to `True`, |
| 1367 | checksum failures will raise warnings rather than exceptions. |
| 1368 | """ |
| 1369 | |
| 1370 | self.validate_signature() |
| 1371 | |
| 1372 | # http://www.w3.org/TR/PNG/#5Chunk-layout |
| 1373 | if not self.atchunk: |
| 1374 | self.atchunk = self._chunk_len_type() |
| 1375 | if not self.atchunk: |
| 1376 | raise ChunkError("No more chunks.") |
| 1377 | length, type = self.atchunk |
| 1378 | self.atchunk = None |
| 1379 | |
| 1380 | data = self.file.read(length) |
| 1381 | if len(data) != length: |
| 1382 | raise ChunkError( |
| 1383 | "Chunk %s too short for required %i octets." % (type, length) |
| 1384 | ) |
| 1385 | checksum = self.file.read(4) |
| 1386 | if len(checksum) != 4: |
| 1387 | raise ChunkError("Chunk %s too short for checksum." % type) |
| 1388 | verify = zlib.crc32(type) |
| 1389 | verify = zlib.crc32(data, verify) |
| 1390 | verify = struct.pack("!I", verify) |
| 1391 | if checksum != verify: |
| 1392 | (a,) = struct.unpack("!I", checksum) |
| 1393 | (b,) = struct.unpack("!I", verify) |
| 1394 | message = "Checksum error in %s chunk: 0x%08X != 0x%08X." % ( |
| 1395 | type.decode("ascii"), |
| 1396 | a, |
| 1397 | b, |
| 1398 | ) |
| 1399 | if lenient: |
| 1400 | warnings.warn(message, RuntimeWarning) |
| 1401 | else: |
| 1402 | raise ChunkError(message) |
| 1403 | return type, data |
| 1404 | |
| 1405 | def chunks(self): |
| 1406 | """Return an iterator that will yield each chunk as a |
no test coverage detected