Collect every IDAT chunk, for there may be more than one of them, and then decompress it. After that, separate them by row. Return a list [b'...', ...] What's worth noting is that the first byte of every row represent the filter type instead of pixel data
(self)
| 205 | |
| 206 | |
| 207 | def get_all_idat_data(self) -> list: |
| 208 | """ |
| 209 | Collect every IDAT chunk, for there may be more than one of them, and then |
| 210 | decompress it. After that, separate them by row. |
| 211 | Return a list [b'...', ...] |
| 212 | What's worth noting is that the first byte of every row represent the filter |
| 213 | type instead of pixel data |
| 214 | """ |
| 215 | # Identify all starting indices of idat chucks since there can be multiple of them |
| 216 | idat_chunks_indices = [] |
| 217 | start_finding_index = 0 |
| 218 | for _ in range(self.bin.count(b'IDAT')): |
| 219 | idat_chunks_indices.append(self.bin.index(b'IDAT', start_finding_index) - 4) |
| 220 | start_finding_index = (idat_chunks_indices[-1] + |
| 221 | Png.get_chunk_length(self.bin, idat_chunks_indices[-1]) + |
| 222 | 8) |
| 223 | # Check crc |
| 224 | if self.crc == True: |
| 225 | for idat_chunk_index in idat_chunks_indices: |
| 226 | Png.check_crc(self.bin, idat_chunk_index) |
| 227 | # Merge all chunks into one bytes |
| 228 | idat_data = b'' |
| 229 | for idat_chunk_index in idat_chunks_indices: |
| 230 | idat_data += self.bin[idat_chunk_index + 8 : |
| 231 | idat_chunk_index + 8 + |
| 232 | Png.get_chunk_length(self.bin,idat_chunk_index)] |
| 233 | # Decompress (Huffman & LZSS) |
| 234 | self.idat_data = zlib.decompress(idat_data) |
| 235 | |
| 236 | # Store the data as rows [b'...', b'...', ...] |
| 237 | rows = [] |
| 238 | row_length = ceil(self.width * self.channels * self.bit_depth / 8 + 1) |
| 239 | for row_num in range(self.height): |
| 240 | rows.append(self.idat_data[row_num * row_length:(row_num + 1) * row_length]) |
| 241 | |
| 242 | return rows |
| 243 | |
| 244 | |
| 245 | def defilter(self, rows:list,) -> list: |
no test coverage detected