def test_chunk_order(self): im = Image.open("Tests/images/icc_profile.png") test_file = self.tempfile("temp.png") im.convert("P").save(test_file, dpi=(100, 100)) chunks = [] with open(test_file, "rb") as fp: fp.read(8) with PngImagePlugin.PngStream(fp) as png: while True: cid, pos, length = png.read() chunks.append(cid) try: s = png.call(cid, pos, length) except EOFError: break png.crc(cid, s) # https://www.w3.org/TR/PNG/#5ChunkOrdering # IHDR - shall be first self.assertEqual(chunks.index(b"IHDR"), 0) # PLTE - before first IDAT self.assertLess(chunks.index(b"PLTE"), chunks.index(b"IDAT")) # iCCP - before PLTE and IDAT self.assertLess(chunks.index(b"iCCP"), chunks.index(b"PLTE")) self.assertLess(chunks.index(b"iCCP"), chunks.index(b"IDAT")) # tRNS - after PLTE, before IDAT self.assertGreater(chunks.index(b"tRNS"), chunks.index(b"PLTE")) self.assertLess(chunks.index(b"tRNS"), chunks.index(b"IDAT")) # pHYs - before IDAT self.assertLess(chunks.index(b"pHYs"), chunks.index(b"IDAT"))
def test_truncated_chunks(self, cid): fp = BytesIO() with PngImagePlugin.PngStream(fp) as png: with pytest.raises(ValueError): png.call(cid, 0, 0) ImageFile.LOAD_TRUNCATED_IMAGES = True png.call(cid, 0, 0) ImageFile.LOAD_TRUNCATED_IMAGES = False
def get_chunks(self, filename): chunks = [] with open(filename, "rb") as fp: fp.read(8) with PngImagePlugin.PngStream(fp) as png: while True: cid, pos, length = png.read() chunks.append(cid) try: s = png.call(cid, pos, length) except EOFError: break png.crc(cid, s) return chunks