Collection of the chunks parsed from a PNG image stream.
| 90 | |
| 91 | |
| 92 | class _Chunks: |
| 93 | """Collection of the chunks parsed from a PNG image stream.""" |
| 94 | |
| 95 | def __init__(self, chunk_iterable): |
| 96 | super(_Chunks, self).__init__() |
| 97 | self._chunks = list(chunk_iterable) |
| 98 | |
| 99 | @classmethod |
| 100 | def from_stream(cls, stream): |
| 101 | """Return a |_Chunks| instance containing the PNG chunks in `stream`.""" |
| 102 | chunk_parser = _ChunkParser.from_stream(stream) |
| 103 | chunks = list(chunk_parser.iter_chunks()) |
| 104 | return cls(chunks) |
| 105 | |
| 106 | @property |
| 107 | def IHDR(self): |
| 108 | """IHDR chunk in PNG image.""" |
| 109 | match = lambda chunk: chunk.type_name == PNG_CHUNK_TYPE.IHDR # noqa |
| 110 | IHDR = self._find_first(match) |
| 111 | if IHDR is None: |
| 112 | raise InvalidImageStreamError("no IHDR chunk in PNG image") |
| 113 | return IHDR |
| 114 | |
| 115 | @property |
| 116 | def pHYs(self): |
| 117 | """PHYs chunk in PNG image, or |None| if not present.""" |
| 118 | match = lambda chunk: chunk.type_name == PNG_CHUNK_TYPE.pHYs # noqa |
| 119 | return self._find_first(match) |
| 120 | |
| 121 | def _find_first(self, match): |
| 122 | """Return first chunk in stream order returning True for function `match`.""" |
| 123 | for chunk in self._chunks: |
| 124 | if match(chunk): |
| 125 | return chunk |
| 126 | return None |
| 127 | |
| 128 | |
| 129 | class _ChunkParser: |
no outgoing calls
searching dependent graphs…