(self, outfile)
| 761 | return i + 1 |
| 762 | |
| 763 | def write_preamble(self, outfile): |
| 764 | # http://www.w3.org/TR/PNG/#5PNG-file-signature |
| 765 | outfile.write(signature) |
| 766 | |
| 767 | # http://www.w3.org/TR/PNG/#11IHDR |
| 768 | write_chunk( |
| 769 | outfile, |
| 770 | b"IHDR", |
| 771 | struct.pack( |
| 772 | "!2I5B", |
| 773 | self.width, |
| 774 | self.height, |
| 775 | self.bitdepth, |
| 776 | self.color_type, |
| 777 | 0, |
| 778 | 0, |
| 779 | self.interlace, |
| 780 | ), |
| 781 | ) |
| 782 | |
| 783 | # See :chunk:order |
| 784 | # http://www.w3.org/TR/PNG/#11gAMA |
| 785 | if self.gamma is not None: |
| 786 | write_chunk( |
| 787 | outfile, b"gAMA", struct.pack("!L", int(round(self.gamma * 1e5))) |
| 788 | ) |
| 789 | |
| 790 | # See :chunk:order |
| 791 | # http://www.w3.org/TR/PNG/#11sBIT |
| 792 | if self.rescale: |
| 793 | write_chunk( |
| 794 | outfile, |
| 795 | b"sBIT", |
| 796 | struct.pack("%dB" % self.planes, *[s[0] for s in self.rescale]), |
| 797 | ) |
| 798 | |
| 799 | # :chunk:order: Without a palette (PLTE chunk), |
| 800 | # ordering is relatively relaxed. |
| 801 | # With one, gAMA chunk must precede PLTE chunk |
| 802 | # which must precede tRNS and bKGD. |
| 803 | # See http://www.w3.org/TR/PNG/#5ChunkOrdering |
| 804 | if self.palette: |
| 805 | p, t = make_palette_chunks(self.palette) |
| 806 | write_chunk(outfile, b"PLTE", p) |
| 807 | if t: |
| 808 | # tRNS chunk is optional; |
| 809 | # Only needed if palette entries have alpha. |
| 810 | write_chunk(outfile, b"tRNS", t) |
| 811 | |
| 812 | # http://www.w3.org/TR/PNG/#11tRNS |
| 813 | if self.transparent is not None: |
| 814 | if self.greyscale: |
| 815 | fmt = "!1H" |
| 816 | else: |
| 817 | fmt = "!3H" |
| 818 | write_chunk(outfile, b"tRNS", struct.pack(fmt, *self.transparent)) |
| 819 | |
| 820 | # http://www.w3.org/TR/PNG/#11bKGD |
no test coverage detected