1#!/usr/bin/env python
2
3# $URL: http://pypng.googlecode.com/svn/trunk/code/png.py $
4# $Rev: 228 $
5
6# png.py - PNG encoder/decoder in pure Python
7#
8# Modified for Pygame in Oct., 2012 to work with Python 3.x.
9#
10# Copyright (C) 2006 Johann C. Rocholl <johann@browsershots.org>
11# Portions Copyright (C) 2009 David Jones <drj@pobox.com>
12# And probably portions Copyright (C) 2006 Nicko van Someren <nicko@nicko.org>
13#
14# Original concept by Johann C. Rocholl.
15#
16# LICENSE (The MIT License)
17#
18# Permission is hereby granted, free of charge, to any person
19# obtaining a copy of this software and associated documentation files
20# (the "Software"), to deal in the Software without restriction,
21# including without limitation the rights to use, copy, modify, merge,
22# publish, distribute, sublicense, and/or sell copies of the Software,
23# and to permit persons to whom the Software is furnished to do so,
24# subject to the following conditions:
25#
26# The above copyright notice and this permission notice shall be
27# included in all copies or substantial portions of the Software.
28#
29# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
33# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
34# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
35# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36# SOFTWARE.
37#
38# Changelog (recent first):
39# 2009-03-11 David: interlaced bit depth < 8 (writing).
40# 2009-03-10 David: interlaced bit depth < 8 (reading).
41# 2009-03-04 David: Flat and Boxed pixel formats.
42# 2009-02-26 David: Palette support (writing).
43# 2009-02-23 David: Bit-depths < 8; better PNM support.
44# 2006-06-17 Nicko: Reworked into a class, faster interlacing.
45# 2006-06-17 Johann: Very simple prototype PNG decoder.
46# 2006-06-17 Nicko: Test suite with various image generators.
47# 2006-06-17 Nicko: Alpha-channel, grey-scale, 16-bit/plane support.
48# 2006-06-15 Johann: Scanline iterator interface for large input files.
49# 2006-06-09 Johann: Very simple prototype PNG encoder.
50
51# Incorporated into Bangai-O Development Tools by drj on 2009-02-11 from
52# http://trac.browsershots.org/browser/trunk/pypng/lib/png.py?rev=2885
53
54# Incorporated into pypng by drj on 2009-03-12 from
55# //depot/prj/bangaio/master/code/png.py#67
56
57
58"""
59Pure Python PNG Reader/Writer
60
61This Python module implements support for PNG images (see PNG
62specification at http://www.w3.org/TR/2003/REC-PNG-20031110/ ). It reads
63and writes PNG files with all allowable bit depths (1/2/4/8/16/24/32/48/64
64bits per pixel) and colour combinations: greyscale (1/2/4/8/16 bit); RGB,
65RGBA, LA (greyscale with alpha) with 8/16 bits per channel; colour mapped
66images (1/2/4/8 bit).  Adam7 interlacing is supported for reading and
67writing.  A number of optional chunks can be specified (when writing)
68and understood (when reading): ``tRNS``, ``bKGD``, ``gAMA``.
69
70For help, type ``import png; help(png)`` in your python interpreter.
71
72A good place to start is the :class:`Reader` and :class:`Writer` classes.
73
74This file can also be used as a command-line utility to convert
75`Netpbm <http://netpbm.sourceforge.net/>`_ PNM files to PNG, and the reverse conversion from PNG to
76PNM. The interface is similar to that of the ``pnmtopng`` program from
77Netpbm.  Type ``python png.py --help`` at the shell prompt
78for usage and a list of options.
79
80A note on spelling and terminology
81----------------------------------
82
83Generally British English spelling is used in the documentation.  So
84that's "greyscale" and "colour".  This not only matches the author's
85native language, it's also used by the PNG specification.
86
87The major colour models supported by PNG (and hence by PyPNG) are:
88greyscale, RGB, greyscale--alpha, RGB--alpha.  These are sometimes
89referred to using the abbreviations: L, RGB, LA, RGBA.  In this case
90each letter abbreviates a single channel: *L* is for Luminance or Luma or
91Lightness which is the channel used in greyscale images; *R*, *G*, *B* stand
92for Red, Green, Blue, the components of a colour image; *A* stands for
93Alpha, the opacity channel (used for transparency effects, but higher
94values are more opaque, so it makes sense to call it opacity).
95
96A note on formats
97-----------------
98
99When getting pixel data out of this module (reading) and presenting
100data to this module (writing) there are a number of ways the data could
101be represented as a Python value.  Generally this module uses one of
102three formats called "flat row flat pixel", "boxed row flat pixel", and
103"boxed row boxed pixel".  Basically the concern is whether each pixel
104and each row comes in its own little tuple (box), or not.
105
106Consider an image that is 3 pixels wide by 2 pixels high, and each pixel
107has RGB components:
108
109Boxed row flat pixel::
110
111  list([R,G,B, R,G,B, R,G,B],
112       [R,G,B, R,G,B, R,G,B])
113
114Each row appears as its own list, but the pixels are flattened so that
115three values for one pixel simply follow the three values for the previous
116pixel.  This is the most common format used, because it provides a good
117compromise between space and convenience.  PyPNG regards itself as
118at liberty to replace any sequence type with any sufficiently compatible
119other sequence type; in practice each row is an array (from the array
120module), and the outer list is sometimes an iterator rather than an
121explicit list (so that streaming is possible).
122
123Flat row flat pixel::
124
125  [R,G,B, R,G,B, R,G,B,
126   R,G,B, R,G,B, R,G,B]
127
128The entire image is one single giant sequence of colour values.
129Generally an array will be used (to save space), not a list.
130
131Boxed row boxed pixel::
132
133  list([ (R,G,B), (R,G,B), (R,G,B) ],
134       [ (R,G,B), (R,G,B), (R,G,B) ])
135
136Each row appears in its own list, but each pixel also appears in its own
137tuple.  A serious memory burn in Python.
138
139In all cases the top row comes first, and for each row the pixels are
140ordered from left-to-right.  Within a pixel the values appear in the
141order, R-G-B-A (or L-A for greyscale--alpha).
142
143There is a fourth format, mentioned because it is used internally,
144is close to what lies inside a PNG file itself, and has some support
145from the public API.  This format is called packed.  When packed,
146each row is a sequence of bytes (integers from 0 to 255), just as
147it is before PNG scanline filtering is applied.  When the bit depth
148is 8 this is essentially the same as boxed row flat pixel; when the
149bit depth is less than 8, several pixels are packed into each byte;
150when the bit depth is 16 (the only value more than 8 that is supported
151by the PNG image format) each pixel value is decomposed into 2 bytes
152(and `packed` is a misnomer).  This format is used by the
153:meth:`Writer.write_packed` method.  It isn't usually a convenient
154format, but may be just right if the source data for the PNG image
155comes from something that uses a similar format (for example, 1-bit
156BMPs, or another PNG file).
157
158And now, my famous members
159--------------------------
160"""
161
162__version__ = "$URL: http://pypng.googlecode.com/svn/trunk/code/png.py $ $Rev: 228 $"
163
164from pygame.compat import geterror, imap_
165from array import array
166from pygame.tests.test_utils import tostring
167import itertools
168import math
169import operator
170import struct
171import sys
172import zlib
173import warnings
174
175__all__ = ["Image", "Reader", "Writer", "write_chunks", "from_array"]
176
177
178# The PNG signature.
179# http://www.w3.org/TR/PNG/#5PNG-file-signature
180_signature = struct.pack("8B", 137, 80, 78, 71, 13, 10, 26, 10)
181
182_adam7 = (
183    (0, 0, 8, 8),
184    (4, 0, 8, 8),
185    (0, 4, 4, 8),
186    (2, 0, 4, 4),
187    (0, 2, 2, 4),
188    (1, 0, 2, 2),
189    (0, 1, 1, 2),
190)
191
192
193def group(s, n):
194    # See
195    # http://www.python.org/doc/2.6/library/functions.html#zip
196    return zip(*[iter(s)] * n)
197
198
199def isarray(x):
200    """Same as ``isinstance(x, array)``.
201    """
202    return isinstance(x, array)
203
204
205# Conditionally convert to bytes.  Works on Python 2 and Python 3.
206try:
207    bytes("", "ascii")
208
209    def strtobytes(x):
210        return bytes(x, "iso8859-1")
211
212    def bytestostr(x):
213        return str(x, "iso8859-1")
214
215
216except:
217    strtobytes = str
218    bytestostr = str
219
220
221def interleave_planes(ipixels, apixels, ipsize, apsize):
222    """
223    Interleave (colour) planes, e.g. RGB + A = RGBA.
224
225    Return an array of pixels consisting of the `ipsize` elements of data
226    from each pixel in `ipixels` followed by the `apsize` elements of data
227    from each pixel in `apixels`.  Conventionally `ipixels` and
228    `apixels` are byte arrays so the sizes are bytes, but it actually
229    works with any arrays of the same type.  The returned array is the
230    same type as the input arrays which should be the same type as each other.
231    """
232
233    itotal = len(ipixels)
234    atotal = len(apixels)
235    newtotal = itotal + atotal
236    newpsize = ipsize + apsize
237    # Set up the output buffer
238    # See http://www.python.org/doc/2.4.4/lib/module-array.html#l2h-1356
239    out = array(ipixels.typecode)
240    # It's annoying that there is no cheap way to set the array size :-(
241    out.extend(ipixels)
242    out.extend(apixels)
243    # Interleave in the pixel data
244    for i in range(ipsize):
245        out[i:newtotal:newpsize] = ipixels[i:itotal:ipsize]
246    for i in range(apsize):
247        out[i + ipsize : newtotal : newpsize] = apixels[i:atotal:apsize]
248    return out
249
250
251def check_palette(palette):
252    """Check a palette argument (to the :class:`Writer` class) for validity.
253    Returns the palette as a list if okay; raises an exception otherwise.
254    """
255
256    # None is the default and is allowed.
257    if palette is None:
258        return None
259
260    p = list(palette)
261    if not (0 < len(p) <= 256):
262        raise ValueError("a palette must have between 1 and 256 entries")
263    seen_triple = False
264    for i, t in enumerate(p):
265        if len(t) not in (3, 4):
266            raise ValueError("palette entry %d: entries must be 3- or 4-tuples." % i)
267        if len(t) == 3:
268            seen_triple = True
269        if seen_triple and len(t) == 4:
270            raise ValueError(
271                "palette entry %d: all 4-tuples must precede all 3-tuples" % i
272            )
273        for x in t:
274            if int(x) != x or not (0 <= x <= 255):
275                raise ValueError(
276                    "palette entry %d: values must be integer: 0 <= x <= 255" % i
277                )
278    return p
279
280
281class Error(Exception):
282    prefix = "Error"
283
284    def __str__(self):
285        return self.prefix + ": " + " ".join(self.args)
286
287
288class FormatError(Error):
289    """Problem with input file format.  In other words, PNG file does
290    not conform to the specification in some way and is invalid.
291    """
292
293    prefix = "FormatError"
294
295
296class ChunkError(FormatError):
297    prefix = "ChunkError"
298
299
300class Writer:
301    """
302    PNG encoder in pure Python.
303    """
304
305    def __init__(
306        self,
307        width=None,
308        height=None,
309        size=None,
310        greyscale=False,
311        alpha=False,
312        bitdepth=8,
313        palette=None,
314        transparent=None,
315        background=None,
316        gamma=None,
317        compression=None,
318        interlace=False,
319        bytes_per_sample=None,  # deprecated
320        planes=None,
321        colormap=None,
322        maxval=None,
323        chunk_limit=2 ** 20,
324    ):
325        """
326        Create a PNG encoder object.
327
328        Arguments:
329
330        width, height
331          Image size in pixels, as two separate arguments.
332        size
333          Image size (w,h) in pixels, as single argument.
334        greyscale
335          Input data is greyscale, not RGB.
336        alpha
337          Input data has alpha channel (RGBA or LA).
338        bitdepth
339          Bit depth: from 1 to 16.
340        palette
341          Create a palette for a colour mapped image (colour type 3).
342        transparent
343          Specify a transparent colour (create a ``tRNS`` chunk).
344        background
345          Specify a default background colour (create a ``bKGD`` chunk).
346        gamma
347          Specify a gamma value (create a ``gAMA`` chunk).
348        compression
349          zlib compression level (1-9).
350        interlace
351          Create an interlaced image.
352        chunk_limit
353          Write multiple ``IDAT`` chunks to save memory.
354
355        The image size (in pixels) can be specified either by using the
356        `width` and `height` arguments, or with the single `size`
357        argument.  If `size` is used it should be a pair (*width*,
358        *height*).
359
360        `greyscale` and `alpha` are booleans that specify whether
361        an image is greyscale (or colour), and whether it has an
362        alpha channel (or not).
363
364        `bitdepth` specifies the bit depth of the source pixel values.
365        Each source pixel value must be an integer between 0 and
366        ``2**bitdepth-1``.  For example, 8-bit images have values
367        between 0 and 255.  PNG only stores images with bit depths of
368        1,2,4,8, or 16.  When `bitdepth` is not one of these values,
369        the next highest valid bit depth is selected, and an ``sBIT``
370        (significant bits) chunk is generated that specifies the original
371        precision of the source image.  In this case the supplied pixel
372        values will be rescaled to fit the range of the selected bit depth.
373
374        The details of which bit depth / colour model combinations the
375        PNG file format supports directly, are somewhat arcane
376        (refer to the PNG specification for full details).  Briefly:
377        "small" bit depths (1,2,4) are only allowed with greyscale and
378        colour mapped images; colour mapped images cannot have bit depth
379        16.
380
381        For colour mapped images (in other words, when the `palette`
382        argument is specified) the `bitdepth` argument must match one of
383        the valid PNG bit depths: 1, 2, 4, or 8.  (It is valid to have a
384        PNG image with a palette and an ``sBIT`` chunk, but the meaning
385        is slightly different; it would be awkward to press the
386        `bitdepth` argument into service for this.)
387
388        The `palette` option, when specified, causes a colour mapped image
389        to be created: the PNG colour type is set to 3; greyscale
390        must not be set; alpha must not be set; transparent must
391        not be set; the bit depth must be 1,2,4, or 8.  When a colour
392        mapped image is created, the pixel values are palette indexes
393        and the `bitdepth` argument specifies the size of these indexes
394        (not the size of the colour values in the palette).
395
396        The palette argument value should be a sequence of 3- or
397        4-tuples.  3-tuples specify RGB palette entries; 4-tuples
398        specify RGBA palette entries.  If both 4-tuples and 3-tuples
399        appear in the sequence then all the 4-tuples must come
400        before all the 3-tuples.  A ``PLTE`` chunk is created; if there
401        are 4-tuples then a ``tRNS`` chunk is created as well.  The
402        ``PLTE`` chunk will contain all the RGB triples in the same
403        sequence; the ``tRNS`` chunk will contain the alpha channel for
404        all the 4-tuples, in the same sequence.  Palette entries
405        are always 8-bit.
406
407        If specified, the `transparent` and `background` parameters must
408        be a tuple with three integer values for red, green, blue, or
409        a simple integer (or singleton tuple) for a greyscale image.
410
411        If specified, the `gamma` parameter must be a positive number
412        (generally, a float).  A ``gAMA`` chunk will be created.  Note that
413        this will not change the values of the pixels as they appear in
414        the PNG file, they are assumed to have already been converted
415        appropriately for the gamma specified.
416
417        The `compression` argument specifies the compression level
418        to be used by the ``zlib`` module.  Higher values are likely
419        to compress better, but will be slower to compress.  The
420        default for this argument is ``None``; this does not mean
421        no compression, rather it means that the default from the
422        ``zlib`` module is used (which is generally acceptable).
423
424        If `interlace` is true then an interlaced image is created
425        (using PNG's so far only interlace method, *Adam7*).  This does not
426        affect how the pixels should be presented to the encoder, rather
427        it changes how they are arranged into the PNG file.  On slow
428        connexions interlaced images can be partially decoded by the
429        browser to give a rough view of the image that is successively
430        refined as more image data appears.
431
432        .. note ::
433
434          Enabling the `interlace` option requires the entire image
435          to be processed in working memory.
436
437        `chunk_limit` is used to limit the amount of memory used whilst
438        compressing the image.  In order to avoid using large amounts of
439        memory, multiple ``IDAT`` chunks may be created.
440        """
441
442        # At the moment the `planes` argument is ignored;
443        # its purpose is to act as a dummy so that
444        # ``Writer(x, y, **info)`` works, where `info` is a dictionary
445        # returned by Reader.read and friends.
446        # Ditto for `colormap`.
447
448        # A couple of helper functions come first.  Best skipped if you
449        # are reading through.
450
451        def isinteger(x):
452            try:
453                return int(x) == x
454            except:
455                return False
456
457        def check_color(c, which):
458            """Checks that a colour argument for transparent or
459            background options is the right form.  Also "corrects" bare
460            integers to 1-tuples.
461            """
462
463            if c is None:
464                return c
465            if greyscale:
466                try:
467                    l = len(c)
468                except TypeError:
469                    c = (c,)
470                if len(c) != 1:
471                    raise ValueError("%s for greyscale must be 1-tuple" % which)
472                if not isinteger(c[0]):
473                    raise ValueError("%s colour for greyscale must be integer" % which)
474            else:
475                if not (
476                    len(c) == 3
477                    and isinteger(c[0])
478                    and isinteger(c[1])
479                    and isinteger(c[2])
480                ):
481                    raise ValueError("%s colour must be a triple of integers" % which)
482            return c
483
484        if size:
485            if len(size) != 2:
486                raise ValueError("size argument should be a pair (width, height)")
487            if width is not None and width != size[0]:
488                raise ValueError(
489                    "size[0] (%r) and width (%r) should match when both are used."
490                    % (size[0], width)
491                )
492            if height is not None and height != size[1]:
493                raise ValueError(
494                    "size[1] (%r) and height (%r) should match when both are used."
495                    % (size[1], height)
496                )
497            width, height = size
498        del size
499
500        if width <= 0 or height <= 0:
501            raise ValueError("width and height must be greater than zero")
502        if not isinteger(width) or not isinteger(height):
503            raise ValueError("width and height must be integers")
504        # http://www.w3.org/TR/PNG/#7Integers-and-byte-order
505        if width > 2 ** 32 - 1 or height > 2 ** 32 - 1:
506            raise ValueError("width and height cannot exceed 2**32-1")
507
508        if alpha and transparent is not None:
509            raise ValueError("transparent colour not allowed with alpha channel")
510
511        if bytes_per_sample is not None:
512            warnings.warn(
513                "please use bitdepth instead of bytes_per_sample", DeprecationWarning
514            )
515            if bytes_per_sample not in (0.125, 0.25, 0.5, 1, 2):
516                raise ValueError("bytes per sample must be .125, .25, .5, 1, or 2")
517            bitdepth = int(8 * bytes_per_sample)
518        del bytes_per_sample
519        if not isinteger(bitdepth) or bitdepth < 1 or 16 < bitdepth:
520            raise ValueError("bitdepth (%r) must be a positive integer <= 16"
521                             % bitdepth)
522
523        self.rescale = None
524        if palette:
525            if bitdepth not in (1, 2, 4, 8):
526                raise ValueError("with palette, bitdepth must be 1, 2, 4, or 8")
527            if transparent is not None:
528                raise ValueError("transparent and palette not compatible")
529            if alpha:
530                raise ValueError("alpha and palette not compatible")
531            if greyscale:
532                raise ValueError("greyscale and palette not compatible")
533        else:
534            # No palette, check for sBIT chunk generation.
535            if alpha or not greyscale:
536                if bitdepth not in (8, 16):
537                    targetbitdepth = (8, 16)[bitdepth > 8]
538                    self.rescale = (bitdepth, targetbitdepth)
539                    bitdepth = targetbitdepth
540                    del targetbitdepth
541            else:
542                assert greyscale
543                assert not alpha
544                if bitdepth not in (1, 2, 4, 8, 16):
545                    if bitdepth > 8:
546                        targetbitdepth = 16
547                    elif bitdepth == 3:
548                        targetbitdepth = 4
549                    else:
550                        assert bitdepth in (5, 6, 7)
551                        targetbitdepth = 8
552                    self.rescale = (bitdepth, targetbitdepth)
553                    bitdepth = targetbitdepth
554                    del targetbitdepth
555
556        if bitdepth < 8 and (alpha or not greyscale and not palette):
557            raise ValueError("bitdepth < 8 only permitted with greyscale or palette")
558        if bitdepth > 8 and palette:
559            raise ValueError("bit depth must be 8 or less for images with palette")
560
561        transparent = check_color(transparent, "transparent")
562        background = check_color(background, "background")
563
564        # It's important that the true boolean values (greyscale, alpha,
565        # colormap, interlace) are converted to bool because Iverson's
566        # convention is relied upon later on.
567        self.width = width
568        self.height = height
569        self.transparent = transparent
570        self.background = background
571        self.gamma = gamma
572        self.greyscale = bool(greyscale)
573        self.alpha = bool(alpha)
574        self.colormap = bool(palette)
575        self.bitdepth = int(bitdepth)
576        self.compression = compression
577        self.chunk_limit = chunk_limit
578        self.interlace = bool(interlace)
579        self.palette = check_palette(palette)
580
581        self.color_type = 4 * self.alpha + 2 * (not greyscale) + 1 * self.colormap
582        assert self.color_type in (0, 2, 3, 4, 6)
583
584        self.color_planes = (3, 1)[self.greyscale or self.colormap]
585        self.planes = self.color_planes + self.alpha
586        # :todo: fix for bitdepth < 8
587        self.psize = (self.bitdepth / 8) * self.planes
588
589    def make_palette(self):
590        """Create the byte sequences for a ``PLTE`` and if necessary a
591        ``tRNS`` chunk.  Returned as a pair (*p*, *t*).  *t* will be
592        ``None`` if no ``tRNS`` chunk is necessary.
593        """
594
595        p = array("B")
596        t = array("B")
597
598        for x in self.palette:
599            p.extend(x[0:3])
600            if len(x) > 3:
601                t.append(x[3])
602        p = tostring(p)
603        t = tostring(t)
604        if t:
605            return p, t
606        return p, None
607
608    def write(self, outfile, rows):
609        """Write a PNG image to the output file.  `rows` should be
610        an iterable that yields each row in boxed row flat pixel format.
611        The rows should be the rows of the original image, so there
612        should be ``self.height`` rows of ``self.width * self.planes`` values.
613        If `interlace` is specified (when creating the instance), then
614        an interlaced PNG file will be written.  Supply the rows in the
615        normal image order; the interlacing is carried out internally.
616
617        .. note ::
618
619          Interlacing will require the entire image to be in working memory.
620        """
621
622        if self.interlace:
623            fmt = "BH"[self.bitdepth > 8]
624            a = array(fmt, itertools.chain(*rows))
625            return self.write_array(outfile, a)
626        else:
627            nrows = self.write_passes(outfile, rows)
628            if nrows != self.height:
629                raise ValueError(
630                    "rows supplied (%d) does not match height (%d)"
631                    % (nrows, self.height)
632                )
633
634    def write_passes(self, outfile, rows, packed=False):
635        """
636        Write a PNG image to the output file.
637
638        Most users are expected to find the :meth:`write` or
639        :meth:`write_array` method more convenient.
640
641        The rows should be given to this method in the order that
642        they appear in the output file.  For straightlaced images,
643        this is the usual top to bottom ordering, but for interlaced
644        images the rows should have already been interlaced before
645        passing them to this function.
646
647        `rows` should be an iterable that yields each row.  When
648        `packed` is ``False`` the rows should be in boxed row flat pixel
649        format; when `packed` is ``True`` each row should be a packed
650        sequence of bytes.
651
652        """
653
654        # http://www.w3.org/TR/PNG/#5PNG-file-signature
655        outfile.write(_signature)
656
657        # http://www.w3.org/TR/PNG/#11IHDR
658        write_chunk(
659            outfile,
660            "IHDR",
661            struct.pack(
662                "!2I5B",
663                self.width,
664                self.height,
665                self.bitdepth,
666                self.color_type,
667                0,
668                0,
669                self.interlace,
670            ),
671        )
672
673        # See :chunk:order
674        # http://www.w3.org/TR/PNG/#11gAMA
675        if self.gamma is not None:
676            write_chunk(
677                outfile, "gAMA", struct.pack("!L", int(round(self.gamma * 1e5)))
678            )
679
680        # See :chunk:order
681        # http://www.w3.org/TR/PNG/#11sBIT
682        if self.rescale:
683            write_chunk(
684                outfile,
685                "sBIT",
686                struct.pack("%dB" % self.planes, *[self.rescale[0]] * self.planes),
687            )
688
689        # :chunk:order: Without a palette (PLTE chunk), ordering is
690        # relatively relaxed.  With one, gAMA chunk must precede PLTE
691        # chunk which must precede tRNS and bKGD.
692        # See http://www.w3.org/TR/PNG/#5ChunkOrdering
693        if self.palette:
694            p, t = self.make_palette()
695            write_chunk(outfile, "PLTE", p)
696            if t:
697                # tRNS chunk is optional.  Only needed if palette entries
698                # have alpha.
699                write_chunk(outfile, "tRNS", t)
700
701        # http://www.w3.org/TR/PNG/#11tRNS
702        if self.transparent is not None:
703            if self.greyscale:
704                write_chunk(outfile, "tRNS", struct.pack("!1H", *self.transparent))
705            else:
706                write_chunk(outfile, "tRNS", struct.pack("!3H", *self.transparent))
707
708        # http://www.w3.org/TR/PNG/#11bKGD
709        if self.background is not None:
710            if self.greyscale:
711                write_chunk(outfile, "bKGD", struct.pack("!1H", *self.background))
712            else:
713                write_chunk(outfile, "bKGD", struct.pack("!3H", *self.background))
714
715        # http://www.w3.org/TR/PNG/#11IDAT
716        if self.compression is not None:
717            compressor = zlib.compressobj(self.compression)
718        else:
719            compressor = zlib.compressobj()
720
721        # Choose an extend function based on the bitdepth.  The extend
722        # function packs/decomposes the pixel values into bytes and
723        # stuffs them onto the data array.
724        data = array("B")
725        if self.bitdepth == 8 or packed:
726            extend = data.extend
727        elif self.bitdepth == 16:
728            # Decompose into bytes
729            def extend(sl):
730                fmt = "!%dH" % len(sl)
731                data.extend(array("B", struct.pack(fmt, *sl)))
732
733        else:
734            # Pack into bytes
735            assert self.bitdepth < 8
736            # samples per byte
737            spb = int(8 / self.bitdepth)
738
739            def extend(sl):
740                a = array("B", sl)
741                # Adding padding bytes so we can group into a whole
742                # number of spb-tuples.
743                l = float(len(a))
744                extra = math.ceil(l / float(spb)) * spb - l
745                a.extend([0] * int(extra))
746                # Pack into bytes
747                l = group(a, spb)
748                l = map(lambda e: reduce(lambda x, y: (x << self.bitdepth) + y, e), l)
749                data.extend(l)
750
751        if self.rescale:
752            oldextend = extend
753            factor = float(2 ** self.rescale[1] - 1) / float(2 ** self.rescale[0] - 1)
754
755            def extend(sl):
756                oldextend(map(lambda x: int(round(factor * x)), sl))
757
758        # Build the first row, testing mostly to see if we need to
759        # changed the extend function to cope with NumPy integer types
760        # (they cause our ordinary definition of extend to fail, so we
761        # wrap it).  See
762        # http://code.google.com/p/pypng/issues/detail?id=44
763        enumrows = enumerate(rows)
764        del rows
765
766        # First row's filter type.
767        data.append(0)
768        # :todo: Certain exceptions in the call to ``.next()`` or the
769        # following try would indicate no row data supplied.
770        # Should catch.
771        i, row = next(enumrows)
772        try:
773            # If this fails...
774            extend(row)
775        except:
776            # ... try a version that converts the values to int first.
777            # Not only does this work for the (slightly broken) NumPy
778            # types, there are probably lots of other, unknown, "nearly"
779            # int types it works for.
780            def wrapmapint(f):
781                return lambda sl: f(map(int, sl))
782
783            extend = wrapmapint(extend)
784            del wrapmapint
785            extend(row)
786
787        for i, row in enumrows:
788            # Add "None" filter type.  Currently, it's essential that
789            # this filter type be used for every scanline as we do not
790            # mark the first row of a reduced pass image; that means we
791            # could accidentally compute the wrong filtered scanline if
792            # we used "up", "average", or "paeth" on such a line.
793            data.append(0)
794            extend(row)
795            if len(data) > self.chunk_limit:
796                compressed = compressor.compress(tostring(data))
797                if len(compressed):
798                    # print >> sys.stderr, len(data), len(compressed)
799                    write_chunk(outfile, "IDAT", compressed)
800                # Because of our very witty definition of ``extend``,
801                # above, we must re-use the same ``data`` object.  Hence
802                # we use ``del`` to empty this one, rather than create a
803                # fresh one (which would be my natural FP instinct).
804                del data[:]
805        if len(data):
806            compressed = compressor.compress(tostring(data))
807        else:
808            compressed = ""
809        flushed = compressor.flush()
810        if len(compressed) or len(flushed):
811            # print >> sys.stderr, len(data), len(compressed), len(flushed)
812            write_chunk(outfile, "IDAT", compressed + flushed)
813        # http://www.w3.org/TR/PNG/#11IEND
814        write_chunk(outfile, "IEND")
815        return i + 1
816
817    def write_array(self, outfile, pixels):
818        """
819        Write an array in flat row flat pixel format as a PNG file on
820        the output file.  See also :meth:`write` method.
821        """
822
823        if self.interlace:
824            self.write_passes(outfile, self.array_scanlines_interlace(pixels))
825        else:
826            self.write_passes(outfile, self.array_scanlines(pixels))
827
828    def write_packed(self, outfile, rows):
829        """
830        Write PNG file to `outfile`.  The pixel data comes from `rows`
831        which should be in boxed row packed format.  Each row should be
832        a sequence of packed bytes.
833
834        Technically, this method does work for interlaced images but it
835        is best avoided.  For interlaced images, the rows should be
836        presented in the order that they appear in the file.
837
838        This method should not be used when the source image bit depth
839        is not one naturally supported by PNG; the bit depth should be
840        1, 2, 4, 8, or 16.
841        """
842
843        if self.rescale:
844            raise Error(
845                "write_packed method not suitable for bit depth %d" % self.rescale[0]
846            )
847        return self.write_passes(outfile, rows, packed=True)
848
849    def convert_pnm(self, infile, outfile):
850        """
851        Convert a PNM file containing raw pixel data into a PNG file
852        with the parameters set in the writer object.  Works for
853        (binary) PGM, PPM, and PAM formats.
854        """
855
856        if self.interlace:
857            pixels = array("B")
858            pixels.fromfile(
859                infile,
860                (self.bitdepth / 8) * self.color_planes * self.width * self.height,
861            )
862            self.write_passes(outfile, self.array_scanlines_interlace(pixels))
863        else:
864            self.write_passes(outfile, self.file_scanlines(infile))
865
866    def convert_ppm_and_pgm(self, ppmfile, pgmfile, outfile):
867        """
868        Convert a PPM and PGM file containing raw pixel data into a
869        PNG outfile with the parameters set in the writer object.
870        """
871        pixels = array("B")
872        pixels.fromfile(
873            ppmfile, (self.bitdepth / 8) * self.color_planes * self.width * self.height
874        )
875        apixels = array("B")
876        apixels.fromfile(pgmfile, (self.bitdepth / 8) * self.width * self.height)
877        pixels = interleave_planes(
878            pixels,
879            apixels,
880            (self.bitdepth / 8) * self.color_planes,
881            (self.bitdepth / 8),
882        )
883        if self.interlace:
884            self.write_passes(outfile, self.array_scanlines_interlace(pixels))
885        else:
886            self.write_passes(outfile, self.array_scanlines(pixels))
887
888    def file_scanlines(self, infile):
889        """
890        Generates boxed rows in flat pixel format, from the input file
891        `infile`.  It assumes that the input file is in a "Netpbm-like"
892        binary format, and is positioned at the beginning of the first
893        pixel.  The number of pixels to read is taken from the image
894        dimensions (`width`, `height`, `planes`) and the number of bytes
895        per value is implied by the image `bitdepth`.
896        """
897
898        # Values per row
899        vpr = self.width * self.planes
900        row_bytes = vpr
901        if self.bitdepth > 8:
902            assert self.bitdepth == 16
903            row_bytes *= 2
904            fmt = ">%dH" % vpr
905
906            def line():
907                return array("H", struct.unpack(fmt, infile.read(row_bytes)))
908
909        else:
910
911            def line():
912                scanline = array("B", infile.read(row_bytes))
913                return scanline
914
915        for y in range(self.height):
916            yield line()
917
918    def array_scanlines(self, pixels):
919        """
920        Generates boxed rows (flat pixels) from flat rows (flat pixels)
921        in an array.
922        """
923
924        # Values per row
925        vpr = self.width * self.planes
926        stop = 0
927        for y in range(self.height):
928            start = stop
929            stop = start + vpr
930            yield pixels[start:stop]
931
932    def array_scanlines_interlace(self, pixels):
933        """
934        Generator for interlaced scanlines from an array.  `pixels` is
935        the full source image in flat row flat pixel format.  The
936        generator yields each scanline of the reduced passes in turn, in
937        boxed row flat pixel format.
938        """
939
940        # http://www.w3.org/TR/PNG/#8InterlaceMethods
941        # Array type.
942        fmt = "BH"[self.bitdepth > 8]
943        # Value per row
944        vpr = self.width * self.planes
945        for xstart, ystart, xstep, ystep in _adam7:
946            if xstart >= self.width:
947                continue
948            # Pixels per row (of reduced image)
949            ppr = int(math.ceil((self.width - xstart) / float(xstep)))
950            # number of values in reduced image row.
951            row_len = ppr * self.planes
952            for y in range(ystart, self.height, ystep):
953                if xstep == 1:
954                    offset = y * vpr
955                    yield pixels[offset : offset + vpr]
956                else:
957                    row = array(fmt)
958                    # There's no easier way to set the length of an array
959                    row.extend(pixels[0:row_len])
960                    offset = y * vpr + xstart * self.planes
961                    end_offset = (y + 1) * vpr
962                    skip = self.planes * xstep
963                    for i in range(self.planes):
964                        row[i :: self.planes] = pixels[offset + i : end_offset : skip]
965                    yield row
966
967
968def write_chunk(outfile, tag, data=strtobytes("")):
969    """
970    Write a PNG chunk to the output file, including length and
971    checksum.
972    """
973
974    # http://www.w3.org/TR/PNG/#5Chunk-layout
975    outfile.write(struct.pack("!I", len(data)))
976    tag = strtobytes(tag)
977    outfile.write(tag)
978    outfile.write(data)
979    checksum = zlib.crc32(tag)
980    checksum = zlib.crc32(data, checksum)
981    checksum &= 2 ** 32 - 1
982    outfile.write(struct.pack("!I", checksum))
983
984
985def write_chunks(out, chunks):
986    """Create a PNG file by writing out the chunks."""
987
988    out.write(_signature)
989    for chunk in chunks:
990        write_chunk(out, *chunk)
991
992
993def filter_scanline(type, line, fo, prev=None):
994    """Apply a scanline filter to a scanline.  `type` specifies the
995    filter type (0 to 4); `line` specifies the current (unfiltered)
996    scanline as a sequence of bytes; `prev` specifies the previous
997    (unfiltered) scanline as a sequence of bytes. `fo` specifies the
998    filter offset; normally this is size of a pixel in bytes (the number
999    of bytes per sample times the number of channels), but when this is
1000    < 1 (for bit depths < 8) then the filter offset is 1.
1001    """
1002
1003    assert 0 <= type < 5
1004
1005    # The output array.  Which, pathetically, we extend one-byte at a
1006    # time (fortunately this is linear).
1007    out = array("B", [type])
1008
1009    def sub():
1010        ai = -fo
1011        for x in line:
1012            if ai >= 0:
1013                x = (x - line[ai]) & 0xFF
1014            out.append(x)
1015            ai += 1
1016
1017    def up():
1018        for i, x in enumerate(line):
1019            x = (x - prev[i]) & 0xFF
1020            out.append(x)
1021
1022    def average():
1023        ai = -fo
1024        for i, x in enumerate(line):
1025            if ai >= 0:
1026                x = (x - ((line[ai] + prev[i]) >> 1)) & 0xFF
1027            else:
1028                x = (x - (prev[i] >> 1)) & 0xFF
1029            out.append(x)
1030            ai += 1
1031
1032    def paeth():
1033        # http://www.w3.org/TR/PNG/#9Filter-type-4-Paeth
1034        ai = -fo  # also used for ci
1035        for i, x in enumerate(line):
1036            a = 0
1037            b = prev[i]
1038            c = 0
1039
1040            if ai >= 0:
1041                a = line[ai]
1042                c = prev[ai]
1043            p = a + b - c
1044            pa = abs(p - a)
1045            pb = abs(p - b)
1046            pc = abs(p - c)
1047            if pa <= pb and pa <= pc:
1048                Pr = a
1049            elif pb <= pc:
1050                Pr = b
1051            else:
1052                Pr = c
1053
1054            x = (x - Pr) & 0xFF
1055            out.append(x)
1056            ai += 1
1057
1058    if not prev:
1059        # We're on the first line.  Some of the filters can be reduced
1060        # to simpler cases which makes handling the line "off the top"
1061        # of the image simpler.  "up" becomes "none"; "paeth" becomes
1062        # "left" (non-trivial, but true). "average" needs to be handled
1063        # specially.
1064        if type == 2:  # "up"
1065            return line  # type = 0
1066        elif type == 3:
1067            prev = [0] * len(line)
1068        elif type == 4:  # "paeth"
1069            type = 1
1070    if type == 0:
1071        out.extend(line)
1072    elif type == 1:
1073        sub()
1074    elif type == 2:
1075        up()
1076    elif type == 3:
1077        average()
1078    else:  # type == 4
1079        paeth()
1080    return out
1081
1082
1083def from_array(a, mode=None, info={}):
1084    """Create a PNG :class:`Image` object from a 2- or 3-dimensional array.
1085    One application of this function is easy PIL-style saving:
1086    ``png.from_array(pixels, 'L').save('foo.png')``.
1087
1088    .. note :
1089
1090      The use of the term *3-dimensional* is for marketing purposes
1091      only.  It doesn't actually work.  Please bear with us.  Meanwhile
1092      enjoy the complimentary snacks (on request) and please use a
1093      2-dimensional array.
1094
1095    Unless they are specified using the *info* parameter, the PNG's
1096    height and width are taken from the array size.  For a 3 dimensional
1097    array the first axis is the height; the second axis is the width;
1098    and the third axis is the channel number.  Thus an RGB image that is
1099    16 pixels high and 8 wide will use an array that is 16x8x3.  For 2
1100    dimensional arrays the first axis is the height, but the second axis
1101    is ``width*channels``, so an RGB image that is 16 pixels high and 8
1102    wide will use a 2-dimensional array that is 16x24 (each row will be
1103    8*3==24 sample values).
1104
1105    *mode* is a string that specifies the image colour format in a
1106    PIL-style mode.  It can be:
1107
1108    ``'L'``
1109      greyscale (1 channel)
1110    ``'LA'``
1111      greyscale with alpha (2 channel)
1112    ``'RGB'``
1113      colour image (3 channel)
1114    ``'RGBA'``
1115      colour image with alpha (4 channel)
1116
1117    The mode string can also specify the bit depth (overriding how this
1118    function normally derives the bit depth, see below).  Appending
1119    ``';16'`` to the mode will cause the PNG to be 16 bits per channel;
1120    any decimal from 1 to 16 can be used to specify the bit depth.
1121
1122    When a 2-dimensional array is used *mode* determines how many
1123    channels the image has, and so allows the width to be derived from
1124    the second array dimension.
1125
1126    The array is expected to be a ``numpy`` array, but it can be any
1127    suitable Python sequence.  For example, a list of lists can be used:
1128    ``png.from_array([[0, 255, 0], [255, 0, 255]], 'L')``.  The exact
1129    rules are: ``len(a)`` gives the first dimension, height;
1130    ``len(a[0])`` gives the second dimension; ``len(a[0][0])`` gives the
1131    third dimension, unless an exception is raised in which case a
1132    2-dimensional array is assumed.  It's slightly more complicated than
1133    that because an iterator of rows can be used, and it all still
1134    works.  Using an iterator allows data to be streamed efficiently.
1135
1136    The bit depth of the PNG is normally taken from the array element's
1137    datatype (but if *mode* specifies a bitdepth then that is used
1138    instead).  The array element's datatype is determined in a way which
1139    is supposed to work both for ``numpy`` arrays and for Python
1140    ``array.array`` objects.  A 1 byte datatype will give a bit depth of
1141    8, a 2 byte datatype will give a bit depth of 16.  If the datatype
1142    does not have an implicit size, for example it is a plain Python
1143    list of lists, as above, then a default of 8 is used.
1144
1145    The *info* parameter is a dictionary that can be used to specify
1146    metadata (in the same style as the arguments to the
1147    :class:``png.Writer`` class).  For this function the keys that are
1148    useful are:
1149
1150    height
1151      overrides the height derived from the array dimensions and allows
1152      *a* to be an iterable.
1153    width
1154      overrides the width derived from the array dimensions.
1155    bitdepth
1156      overrides the bit depth derived from the element datatype (but
1157      must match *mode* if that also specifies a bit depth).
1158
1159    Generally anything specified in the
1160    *info* dictionary will override any implicit choices that this
1161    function would otherwise make, but must match any explicit ones.
1162    For example, if the *info* dictionary has a ``greyscale`` key then
1163    this must be true when mode is ``'L'`` or ``'LA'`` and false when
1164    mode is ``'RGB'`` or ``'RGBA'``.
1165    """
1166
1167    # We abuse the *info* parameter by modifying it.  Take a copy here.
1168    # (Also typechecks *info* to some extent).
1169    info = dict(info)
1170
1171    # Syntax check mode string.
1172    bitdepth = None
1173    try:
1174        mode = mode.split(";")
1175        if len(mode) not in (1, 2):
1176            raise Error()
1177        if mode[0] not in ("L", "LA", "RGB", "RGBA"):
1178            raise Error()
1179        if len(mode) == 2:
1180            try:
1181                bitdepth = int(mode[1])
1182            except:
1183                raise Error()
1184    except Error:
1185        raise Error("mode string should be 'RGB' or 'L;16' or similar.")
1186    mode = mode[0]
1187
1188    # Get bitdepth from *mode* if possible.
1189    if bitdepth:
1190        if info.get("bitdepth") and bitdepth != info["bitdepth"]:
1191            raise Error(
1192                "mode bitdepth (%d) should match info bitdepth (%d)."
1193                % (bitdepth, info["bitdepth"])
1194            )
1195        info["bitdepth"] = bitdepth
1196
1197    # Fill in and/or check entries in *info*.
1198    # Dimensions.
1199    if "size" in info:
1200        # Check width, height, size all match where used.
1201        for dimension, axis in [("width", 0), ("height", 1)]:
1202            if dimension in info:
1203                if info[dimension] != info["size"][axis]:
1204                    raise Error(
1205                        "info[%r] should match info['size'][%r]." % (dimension, axis)
1206                    )
1207        info["width"], info["height"] = info["size"]
1208    if "height" not in info:
1209        try:
1210            l = len(a)
1211        except:
1212            raise Error("len(a) does not work, supply info['height'] instead.")
1213        info["height"] = l
1214    # Colour format.
1215    if "greyscale" in info:
1216        if bool(info["greyscale"]) != ("L" in mode):
1217            raise Error("info['greyscale'] should match mode.")
1218    info["greyscale"] = "L" in mode
1219    if "alpha" in info:
1220        if bool(info["alpha"]) != ("A" in mode):
1221            raise Error("info['alpha'] should match mode.")
1222    info["alpha"] = "A" in mode
1223
1224    planes = len(mode)
1225    if "planes" in info:
1226        if info["planes"] != planes:
1227            raise Error("info['planes'] should match mode.")
1228
1229    # In order to work out whether we the array is 2D or 3D we need its
1230    # first row, which requires that we take a copy of its iterator.
1231    # We may also need the first row to derive width and bitdepth.
1232    a, t = itertools.tee(a)
1233    row = next(t)
1234    del t
1235    try:
1236        row[0][0]
1237        threed = True
1238        testelement = row[0]
1239    except:
1240        threed = False
1241        testelement = row
1242    if "width" not in info:
1243        if threed:
1244            width = len(row)
1245        else:
1246            width = len(row) // planes
1247        info["width"] = width
1248
1249    # Not implemented yet
1250    assert not threed
1251
1252    if "bitdepth" not in info:
1253        try:
1254            dtype = testelement.dtype
1255            # goto the "else:" clause.  Sorry.
1256        except:
1257            try:
1258                # Try a Python array.array.
1259                bitdepth = 8 * testelement.itemsize
1260            except:
1261                # We can't determine it from the array element's
1262                # datatype, use a default of 8.
1263                bitdepth = 8
1264        else:
1265            # If we got here without exception, we now assume that
1266            # the array is a numpy array.
1267            if dtype.kind == "b":
1268                bitdepth = 1
1269            else:
1270                bitdepth = 8 * dtype.itemsize
1271        info["bitdepth"] = bitdepth
1272
1273    for thing in "width height bitdepth greyscale alpha".split():
1274        assert thing in info
1275    return Image(a, info)
1276
1277
1278# So that refugee's from PIL feel more at home.  Not documented.
1279fromarray = from_array
1280
1281
1282class Image:
1283    """A PNG image.
1284    You can create an :class:`Image` object from an array of pixels by calling
1285    :meth:`png.from_array`.  It can be saved to disk with the
1286    :meth:`save` method."""
1287
1288    def __init__(self, rows, info):
1289        """
1290        .. note ::
1291
1292          The constructor is not public.  Please do not call it.
1293        """
1294
1295        self.rows = rows
1296        self.info = info
1297
1298    def save(self, file):
1299        """Save the image to *file*.  If *file* looks like an open file
1300        descriptor then it is used, otherwise it is treated as a
1301        filename and a fresh file is opened.
1302
1303        In general, you can only call this method once; after it has
1304        been called the first time and the PNG image has been saved, the
1305        source data will have been streamed, and cannot be streamed
1306        again.
1307        """
1308
1309        w = Writer(**self.info)
1310
1311        try:
1312            file.write
1313
1314            def close():
1315                pass
1316
1317        except:
1318            file = open(file, "wb")
1319
1320            def close():
1321                file.close()
1322
1323        try:
1324            w.write(file, self.rows)
1325        finally:
1326            close()
1327
1328
1329class _readable:
1330    """
1331    A simple file-like interface for strings and arrays.
1332    """
1333
1334    def __init__(self, buf):
1335        self.buf = buf
1336        self.offset = 0
1337
1338    def read(self, n):
1339        r = self.buf[self.offset : self.offset + n]
1340        if isarray(r):
1341            r = tostring(r)
1342        self.offset += n
1343        return r
1344
1345
1346class Reader:
1347    """
1348    PNG decoder in pure Python.
1349    """
1350
1351    def __init__(self, _guess=None, **kw):
1352        """
1353        Create a PNG decoder object.
1354
1355        The constructor expects exactly one keyword argument. If you
1356        supply a positional argument instead, it will guess the input
1357        type. You can choose among the following keyword arguments:
1358
1359        filename
1360          Name of input file (a PNG file).
1361        file
1362          A file-like object (object with a read() method).
1363        bytes
1364          ``array`` or ``string`` with PNG data.
1365
1366        """
1367        if (_guess is not None and len(kw) != 0) or (_guess is None and len(kw) != 1):
1368            raise TypeError("Reader() takes exactly 1 argument")
1369
1370        # Will be the first 8 bytes, later on.  See validate_signature.
1371        self.signature = None
1372        self.transparent = None
1373        # A pair of (len,type) if a chunk has been read but its data and
1374        # checksum have not (in other words the file position is just
1375        # past the 4 bytes that specify the chunk type).  See preamble
1376        # method for how this is used.
1377        self.atchunk = None
1378
1379        if _guess is not None:
1380            if isarray(_guess):
1381                kw["bytes"] = _guess
1382            elif isinstance(_guess, str):
1383                kw["filename"] = _guess
1384            elif isinstance(_guess, file):
1385                kw["file"] = _guess
1386
1387        if "filename" in kw:
1388            self.file = open(kw["filename"], "rb")
1389        elif "file" in kw:
1390            self.file = kw["file"]
1391        elif "bytes" in kw:
1392            self.file = _readable(kw["bytes"])
1393        else:
1394            raise TypeError("expecting filename, file or bytes array")
1395
1396    def chunk(self, seek=None):
1397        """
1398        Read the next PNG chunk from the input file; returns a
1399        (*type*,*data*) tuple.  *type* is the chunk's type as a string
1400        (all PNG chunk types are 4 characters long).  *data* is the
1401        chunk's data content, as a string.
1402
1403        If the optional `seek` argument is
1404        specified then it will keep reading chunks until it either runs
1405        out of file or finds the type specified by the argument.  Note
1406        that in general the order of chunks in PNGs is unspecified, so
1407        using `seek` can cause you to miss chunks.
1408        """
1409
1410        self.validate_signature()
1411
1412        while True:
1413            # http://www.w3.org/TR/PNG/#5Chunk-layout
1414            if not self.atchunk:
1415                self.atchunk = self.chunklentype()
1416            length, type = self.atchunk
1417            self.atchunk = None
1418            data = self.file.read(length)
1419            if len(data) != length:
1420                raise ChunkError(
1421                    "Chunk %s too short for required %i octets." % (type, length)
1422                )
1423            checksum = self.file.read(4)
1424            if len(checksum) != 4:
1425                raise ValueError("Chunk %s too short for checksum.", tag)
1426            if seek and type != seek:
1427                continue
1428            verify = zlib.crc32(strtobytes(type))
1429            verify = zlib.crc32(data, verify)
1430            # Whether the output from zlib.crc32 is signed or not varies
1431            # according to hideous implementation details, see
1432            # http://bugs.python.org/issue1202 .
1433            # We coerce it to be positive here (in a way which works on
1434            # Python 2.3 and older).
1435            verify &= 2 ** 32 - 1
1436            verify = struct.pack("!I", verify)
1437            if checksum != verify:
1438                # print repr(checksum)
1439                (a,) = struct.unpack("!I", checksum)
1440                (b,) = struct.unpack("!I", verify)
1441                raise ChunkError(
1442                    "Checksum error in %s chunk: 0x%08X != 0x%08X." % (type, a, b)
1443                )
1444            return type, data
1445
1446    def chunks(self):
1447        """Return an iterator that will yield each chunk as a
1448        (*chunktype*, *content*) pair.
1449        """
1450
1451        while True:
1452            t, v = self.chunk()
1453            yield t, v
1454            if t == "IEND":
1455                break
1456
1457    def undo_filter(self, filter_type, scanline, previous):
1458        """Undo the filter for a scanline.  `scanline` is a sequence of
1459        bytes that does not include the initial filter type byte.
1460        `previous` is decoded previous scanline (for straightlaced
1461        images this is the previous pixel row, but for interlaced
1462        images, it is the previous scanline in the reduced image, which
1463        in general is not the previous pixel row in the final image).
1464        When there is no previous scanline (the first row of a
1465        straightlaced image, or the first row in one of the passes in an
1466        interlaced image), then this argument should be ``None``.
1467
1468        The scanline will have the effects of filtering removed, and the
1469        result will be returned as a fresh sequence of bytes.
1470        """
1471
1472        # :todo: Would it be better to update scanline in place?
1473
1474        # Create the result byte array.  It seems that the best way to
1475        # create the array to be the right size is to copy from an
1476        # existing sequence.  *sigh*
1477        # If we fill the result with scanline, then this allows a
1478        # micro-optimisation in the "null" and "sub" cases.
1479        result = array("B", scanline)
1480
1481        if filter_type == 0:
1482            # And here, we _rely_ on filling the result with scanline,
1483            # above.
1484            return result
1485
1486        if filter_type not in (1, 2, 3, 4):
1487            raise FormatError(
1488                "Invalid PNG Filter Type."
1489                "  See http://www.w3.org/TR/2003/REC-PNG-20031110/#9Filters ."
1490            )
1491
1492        # Filter unit.  The stride from one pixel to the corresponding
1493        # byte from the previous previous.  Normally this is the pixel
1494        # size in bytes, but when this is smaller than 1, the previous
1495        # byte is used instead.
1496        fu = max(1, self.psize)
1497
1498        # For the first line of a pass, synthesize a dummy previous
1499        # line.  An alternative approach would be to observe that on the
1500        # first line 'up' is the same as 'null', 'paeth' is the same
1501        # as 'sub', with only 'average' requiring any special case.
1502        if not previous:
1503            previous = array("B", [0] * len(scanline))
1504
1505        def sub():
1506            """Undo sub filter."""
1507
1508            ai = 0
1509            # Loops starts at index fu.  Observe that the initial part
1510            # of the result is already filled in correctly with
1511            # scanline.
1512            for i in range(fu, len(result)):
1513                x = scanline[i]
1514                a = result[ai]
1515                result[i] = (x + a) & 0xFF
1516                ai += 1
1517
1518        def up():
1519            """Undo up filter."""
1520            for i in range(len(result)):  # pylint: disable=consider-using-enumerate
1521                x = scanline[i]
1522                b = previous[i]
1523                result[i] = (x + b) & 0xFF
1524
1525        def average():
1526            """Undo average filter."""
1527
1528            ai = -fu
1529            for i in range(len(result)):  # pylint: disable=consider-using-enumerate
1530                x = scanline[i]
1531                if ai < 0:
1532                    a = 0
1533                else:
1534                    a = result[ai]
1535                b = previous[i]
1536                result[i] = (x + ((a + b) >> 1)) & 0xFF
1537                ai += 1
1538
1539        def paeth():
1540            """Undo Paeth filter."""
1541
1542            # Also used for ci.
1543            ai = -fu
1544            for i in range(len(result)):  # pylint: disable=consider-using-enumerate
1545                x = scanline[i]
1546                if ai < 0:
1547                    a = c = 0
1548                else:
1549                    a = result[ai]
1550                    c = previous[ai]
1551                b = previous[i]
1552                p = a + b - c
1553                pa = abs(p - a)
1554                pb = abs(p - b)
1555                pc = abs(p - c)
1556                if pa <= pb and pa <= pc:
1557                    pr = a
1558                elif pb <= pc:
1559                    pr = b
1560                else:
1561                    pr = c
1562                result[i] = (x + pr) & 0xFF
1563                ai += 1
1564
1565        # Call appropriate filter algorithm.  Note that 0 has already
1566        # been dealt with.
1567        (None, sub, up, average, paeth)[filter_type]()
1568        return result
1569
1570    def deinterlace(self, raw):
1571        """
1572        Read raw pixel data, undo filters, deinterlace, and flatten.
1573        Return in flat row flat pixel format.
1574        """
1575
1576        # print >> sys.stderr, ("Reading interlaced, w=%s, r=%s, planes=%s," +
1577        #     " bpp=%s") % (self.width, self.height, self.planes, self.bps)
1578        # Values per row (of the target image)
1579        vpr = self.width * self.planes
1580
1581        # Make a result array, and make it big enough.  Interleaving
1582        # writes to the output array randomly (well, not quite), so the
1583        # entire output array must be in memory.
1584        fmt = "BH"[self.bitdepth > 8]
1585        a = array(fmt, [0] * vpr * self.height)
1586        source_offset = 0
1587
1588        for xstart, ystart, xstep, ystep in _adam7:
1589            # print >> sys.stderr, "Adam7: start=%s,%s step=%s,%s" % (
1590            #     xstart, ystart, xstep, ystep)
1591            if xstart >= self.width:
1592                continue
1593            # The previous (reconstructed) scanline.  None at the
1594            # beginning of a pass to indicate that there is no previous
1595            # line.
1596            recon = None
1597            # Pixels per row (reduced pass image)
1598            ppr = int(math.ceil((self.width - xstart) / float(xstep)))
1599            # Row size in bytes for this pass.
1600            row_size = int(math.ceil(self.psize * ppr))
1601            for y in range(ystart, self.height, ystep):
1602                filter_type = raw[source_offset]
1603                source_offset += 1
1604                scanline = raw[source_offset : source_offset + row_size]
1605                source_offset += row_size
1606                recon = self.undo_filter(filter_type, scanline, recon)
1607                # Convert so that there is one element per pixel value
1608                flat = self.serialtoflat(recon, ppr)
1609                if xstep == 1:
1610                    assert xstart == 0
1611                    offset = y * vpr
1612                    a[offset : offset + vpr] = flat
1613                else:
1614                    offset = y * vpr + xstart * self.planes
1615                    end_offset = (y + 1) * vpr
1616                    skip = self.planes * xstep
1617                    for i in range(self.planes):
1618                        a[offset + i : end_offset : skip] = flat[i :: self.planes]
1619        return a
1620
1621    def iterboxed(self, rows):
1622        """Iterator that yields each scanline in boxed row flat pixel
1623        format.  `rows` should be an iterator that yields the bytes of
1624        each row in turn.
1625        """
1626
1627        def asvalues(raw):
1628            """Convert a row of raw bytes into a flat row.  Result may
1629            or may not share with argument"""
1630
1631            if self.bitdepth == 8:
1632                return raw
1633            if self.bitdepth == 16:
1634                raw = tostring(raw)
1635                return array("H", struct.unpack("!%dH" % (len(raw) // 2), raw))
1636            assert self.bitdepth < 8
1637            width = self.width
1638            # Samples per byte
1639            spb = 8 // self.bitdepth
1640            out = array("B")
1641            mask = 2 ** self.bitdepth - 1
1642            shifts = map(self.bitdepth.__mul__, reversed(range(spb)))
1643            for o in raw:
1644                out.extend(map(lambda i: mask & (o >> i), shifts))
1645            return out[:width]
1646
1647        return imap_(asvalues, rows)
1648
1649    def serialtoflat(self, bytes, width=None):
1650        """Convert serial format (byte stream) pixel data to flat row
1651        flat pixel.
1652        """
1653
1654        if self.bitdepth == 8:
1655            return bytes
1656        if self.bitdepth == 16:
1657            bytes = tostring(bytes)
1658            return array("H", struct.unpack("!%dH" % (len(bytes) // 2), bytes))
1659        assert self.bitdepth < 8
1660        if width is None:
1661            width = self.width
1662        # Samples per byte
1663        spb = 8 // self.bitdepth
1664        out = array("B")
1665        mask = 2 ** self.bitdepth - 1
1666        shifts = map(self.bitdepth.__mul__, reversed(range(spb)))
1667        l = width
1668        for o in bytes:
1669            out.extend([(mask & (o >> s)) for s in shifts][:l])
1670            l -= spb
1671            if l <= 0:
1672                l = width
1673        return out
1674
1675    def iterstraight(self, raw):
1676        """Iterator that undoes the effect of filtering, and yields each
1677        row in serialised format (as a sequence of bytes).  Assumes input
1678        is straightlaced.  `raw` should be an iterable that yields the
1679        raw bytes in chunks of arbitrary size."""
1680
1681        # length of row, in bytes
1682        rb = self.row_bytes
1683        a = array("B")
1684        # The previous (reconstructed) scanline.  None indicates first
1685        # line of image.
1686        recon = None
1687        for some in raw:
1688            a.extend(some)
1689            while len(a) >= rb + 1:
1690                filter_type = a[0]
1691                scanline = a[1 : rb + 1]
1692                del a[: rb + 1]
1693                recon = self.undo_filter(filter_type, scanline, recon)
1694                yield recon
1695        if len(a) != 0:
1696            # :file:format We get here with a file format error: when the
1697            # available bytes (after decompressing) do not pack into exact
1698            # rows.
1699            raise FormatError("Wrong size for decompressed IDAT chunk.")
1700        assert len(a) == 0
1701
1702    def validate_signature(self):
1703        """If signature (header) has not been read then read and
1704        validate it; otherwise do nothing.
1705        """
1706
1707        if self.signature:
1708            return
1709        self.signature = self.file.read(8)
1710        if self.signature != _signature:
1711            raise FormatError("PNG file has invalid signature.")
1712
1713    def preamble(self):
1714        """
1715        Extract the image metadata by reading the initial part of the PNG
1716        file up to the start of the ``IDAT`` chunk.  All the chunks that
1717        precede the ``IDAT`` chunk are read and either processed for
1718        metadata or discarded.
1719        """
1720
1721        self.validate_signature()
1722
1723        while True:
1724            if not self.atchunk:
1725                self.atchunk = self.chunklentype()
1726                if self.atchunk is None:
1727                    raise FormatError("This PNG file has no IDAT chunks.")
1728            if self.atchunk[1] == "IDAT":
1729                return
1730            self.process_chunk()
1731
1732    def chunklentype(self):
1733        """Reads just enough of the input to determine the next
1734        chunk's length and type, returned as a (*length*, *type*) pair
1735        where *type* is a string.  If there are no more chunks, ``None``
1736        is returned.
1737        """
1738
1739        x = self.file.read(8)
1740        if not x:
1741            return None
1742        if len(x) != 8:
1743            raise FormatError("End of file whilst reading chunk length and type.")
1744        length, type = struct.unpack("!I4s", x)
1745        type = bytestostr(type)
1746        if length > 2 ** 31 - 1:
1747            raise FormatError("Chunk %s is too large: %d." % (type, length))
1748        return length, type
1749
1750    def process_chunk(self):
1751        """Process the next chunk and its data.  This only processes the
1752        following chunk types, all others are ignored: ``IHDR``,
1753        ``PLTE``, ``bKGD``, ``tRNS``, ``gAMA``, ``sBIT``.
1754        """
1755
1756        type, data = self.chunk()
1757        if type == "IHDR":
1758            # http://www.w3.org/TR/PNG/#11IHDR
1759            if len(data) != 13:
1760                raise FormatError("IHDR chunk has incorrect length.")
1761            (
1762                self.width,
1763                self.height,
1764                self.bitdepth,
1765                self.color_type,
1766                self.compression,
1767                self.filter,
1768                self.interlace,
1769            ) = struct.unpack("!2I5B", data)
1770
1771            # Check that the header specifies only valid combinations.
1772            if self.bitdepth not in (1, 2, 4, 8, 16):
1773                raise Error("invalid bit depth %d" % self.bitdepth)
1774            if self.color_type not in (0, 2, 3, 4, 6):
1775                raise Error("invalid colour type %d" % self.color_type)
1776            # Check indexed (palettized) images have 8 or fewer bits
1777            # per pixel; check only indexed or greyscale images have
1778            # fewer than 8 bits per pixel.
1779            if (self.color_type & 1 and self.bitdepth > 8) or (
1780                self.bitdepth < 8 and self.color_type not in (0, 3)
1781            ):
1782                raise FormatError(
1783                    "Illegal combination of bit depth (%d)"
1784                    " and colour type (%d)."
1785                    " See http://www.w3.org/TR/2003/REC-PNG-20031110/#table111 ."
1786                    % (self.bitdepth, self.color_type)
1787                )
1788            if self.compression != 0:
1789                raise Error("unknown compression method %d" % self.compression)
1790            if self.filter != 0:
1791                raise FormatError(
1792                    "Unknown filter method %d,"
1793                    " see http://www.w3.org/TR/2003/REC-PNG-20031110/#9Filters ."
1794                    % self.filter
1795                )
1796            if self.interlace not in (0, 1):
1797                raise FormatError(
1798                    "Unknown interlace method %d,"
1799                    " see http://www.w3.org/TR/2003/REC-PNG-20031110/#8InterlaceMethods ."
1800                    % self.interlace
1801                )
1802
1803            # Derived values
1804            # http://www.w3.org/TR/PNG/#6Colour-values
1805            colormap = bool(self.color_type & 1)
1806            greyscale = not (self.color_type & 2)
1807            alpha = bool(self.color_type & 4)
1808            color_planes = (3, 1)[greyscale or colormap]
1809            planes = color_planes + alpha
1810
1811            self.colormap = colormap
1812            self.greyscale = greyscale
1813            self.alpha = alpha
1814            self.color_planes = color_planes
1815            self.planes = planes
1816            self.psize = float(self.bitdepth) / float(8) * planes
1817            if int(self.psize) == self.psize:
1818                self.psize = int(self.psize)
1819            self.row_bytes = int(math.ceil(self.width * self.psize))
1820            # Stores PLTE chunk if present, and is used to check
1821            # chunk ordering constraints.
1822            self.plte = None
1823            # Stores tRNS chunk if present, and is used to check chunk
1824            # ordering constraints.
1825            self.trns = None
1826            # Stores sbit chunk if present.
1827            self.sbit = None
1828        elif type == "PLTE":
1829            # http://www.w3.org/TR/PNG/#11PLTE
1830            if self.plte:
1831                warnings.warn("Multiple PLTE chunks present.")
1832            self.plte = data
1833            if len(data) % 3 != 0:
1834                raise FormatError("PLTE chunk's length should be a multiple of 3.")
1835            if len(data) > (2 ** self.bitdepth) * 3:
1836                raise FormatError("PLTE chunk is too long.")
1837            if len(data) == 0:
1838                raise FormatError("Empty PLTE is not allowed.")
1839        elif type == "bKGD":
1840            try:
1841                if self.colormap:
1842                    if not self.plte:
1843                        warnings.warn("PLTE chunk is required before bKGD chunk.")
1844                    self.background = struct.unpack("B", data)
1845                else:
1846                    self.background = struct.unpack("!%dH" % self.color_planes, data)
1847            except struct.error:
1848                raise FormatError("bKGD chunk has incorrect length.")
1849        elif type == "tRNS":
1850            # http://www.w3.org/TR/PNG/#11tRNS
1851            self.trns = data
1852            if self.colormap:
1853                if not self.plte:
1854                    warnings.warn("PLTE chunk is required before tRNS chunk.")
1855                else:
1856                    if len(data) > len(self.plte) / 3:
1857                        # Was warning, but promoted to Error as it
1858                        # would otherwise cause pain later on.
1859                        raise FormatError("tRNS chunk is too long.")
1860            else:
1861                if self.alpha:
1862                    raise FormatError(
1863                        "tRNS chunk is not valid with colour type %d." % self.color_type
1864                    )
1865                try:
1866                    self.transparent = struct.unpack("!%dH" % self.color_planes, data)
1867                except struct.error:
1868                    raise FormatError("tRNS chunk has incorrect length.")
1869        elif type == "gAMA":
1870            try:
1871                self.gamma = struct.unpack("!L", data)[0] / 100000.0
1872            except struct.error:
1873                raise FormatError("gAMA chunk has incorrect length.")
1874        elif type == "sBIT":
1875            self.sbit = data
1876            if (
1877                self.colormap
1878                and len(data) != 3
1879                or not self.colormap
1880                and len(data) != self.planes
1881            ):
1882                raise FormatError("sBIT chunk has incorrect length.")
1883
1884    def read(self):
1885        """
1886        Read the PNG file and decode it.  Returns (`width`, `height`,
1887        `pixels`, `metadata`).
1888
1889        May use excessive memory.
1890
1891        `pixels` are returned in boxed row flat pixel format.
1892        """
1893
1894        def iteridat():
1895            """Iterator that yields all the ``IDAT`` chunks as strings."""
1896            while True:
1897                try:
1898                    type, data = self.chunk()
1899                except ValueError:
1900                    e = geterror()
1901                    raise ChunkError(e.args[0])
1902                if type == "IEND":
1903                    # http://www.w3.org/TR/PNG/#11IEND
1904                    break
1905                if type != "IDAT":
1906                    continue
1907                # type == 'IDAT'
1908                # http://www.w3.org/TR/PNG/#11IDAT
1909                if self.colormap and not self.plte:
1910                    warnings.warn("PLTE chunk is required before IDAT chunk")
1911                yield data
1912
1913        def iterdecomp(idat):
1914            """Iterator that yields decompressed strings.  `idat` should
1915            be an iterator that yields the ``IDAT`` chunk data.
1916            """
1917
1918            # Currently, with no max_length parameter to decompress, this
1919            # routine will do one yield per IDAT chunk.  So not very
1920            # incremental.
1921            d = zlib.decompressobj()
1922            # Each IDAT chunk is passed to the decompressor, then any
1923            # remaining state is decompressed out.
1924            for data in idat:
1925                # :todo: add a max_length argument here to limit output
1926                # size.
1927                yield array("B", d.decompress(data))
1928            yield array("B", d.flush())
1929
1930        self.preamble()
1931        raw = iterdecomp(iteridat())
1932
1933        if self.interlace:
1934            raw = array("B", itertools.chain(*raw))
1935            arraycode = "BH"[self.bitdepth > 8]
1936            # Like :meth:`group` but producing an array.array object for
1937            # each row.
1938            pixels = imap_(
1939                lambda *row: array(arraycode, row),
1940                *[iter(self.deinterlace(raw))] * self.width * self.planes
1941            )
1942        else:
1943            pixels = self.iterboxed(self.iterstraight(raw))
1944        meta = dict()
1945        for attr in "greyscale alpha planes bitdepth interlace".split():
1946            meta[attr] = getattr(self, attr)
1947        meta["size"] = (self.width, self.height)
1948        for attr in "gamma transparent background".split():
1949            a = getattr(self, attr, None)
1950            if a is not None:
1951                meta[attr] = a
1952        return self.width, self.height, pixels, meta
1953
1954    def read_flat(self):
1955        """
1956        Read a PNG file and decode it into flat row flat pixel format.
1957        Returns (*width*, *height*, *pixels*, *metadata*).
1958
1959        May use excessive memory.
1960
1961        `pixels` are returned in flat row flat pixel format.
1962
1963        See also the :meth:`read` method which returns pixels in the
1964        more stream-friendly boxed row flat pixel format.
1965        """
1966
1967        x, y, pixel, meta = self.read()
1968        arraycode = "BH"[meta["bitdepth"] > 8]
1969        pixel = array(arraycode, itertools.chain(*pixel))
1970        return x, y, pixel, meta
1971
1972    def palette(self, alpha="natural"):
1973        """Returns a palette that is a sequence of 3-tuples or 4-tuples,
1974        synthesizing it from the ``PLTE`` and ``tRNS`` chunks.  These
1975        chunks should have already been processed (for example, by
1976        calling the :meth:`preamble` method).  All the tuples are the
1977        same size: 3-tuples if there is no ``tRNS`` chunk, 4-tuples when
1978        there is a ``tRNS`` chunk.  Assumes that the image is colour type
1979        3 and therefore a ``PLTE`` chunk is required.
1980
1981        If the `alpha` argument is ``'force'`` then an alpha channel is
1982        always added, forcing the result to be a sequence of 4-tuples.
1983        """
1984
1985        if not self.plte:
1986            raise FormatError("Required PLTE chunk is missing in colour type 3 image.")
1987        plte = group(array("B", self.plte), 3)
1988        if self.trns or alpha == "force":
1989            trns = array("B", self.trns or "")
1990            trns.extend([255] * (len(plte) - len(trns)))
1991            plte = map(operator.add, plte, group(trns, 1))
1992        return plte
1993
1994    def asDirect(self):
1995        """Returns the image data as a direct representation of an
1996        ``x * y * planes`` array.  This method is intended to remove the
1997        need for callers to deal with palettes and transparency
1998        themselves.  Images with a palette (colour type 3)
1999        are converted to RGB or RGBA; images with transparency (a
2000        ``tRNS`` chunk) are converted to LA or RGBA as appropriate.
2001        When returned in this format the pixel values represent the
2002        colour value directly without needing to refer to palettes or
2003        transparency information.
2004
2005        Like the :meth:`read` method this method returns a 4-tuple:
2006
2007        (*width*, *height*, *pixels*, *meta*)
2008
2009        This method normally returns pixel values with the bit depth
2010        they have in the source image, but when the source PNG has an
2011        ``sBIT`` chunk it is inspected and can reduce the bit depth of
2012        the result pixels; pixel values will be reduced according to
2013        the bit depth specified in the ``sBIT`` chunk (PNG nerds should
2014        note a single result bit depth is used for all channels; the
2015        maximum of the ones specified in the ``sBIT`` chunk.  An RGB565
2016        image will be rescaled to 6-bit RGB666).
2017
2018        The *meta* dictionary that is returned reflects the `direct`
2019        format and not the original source image.  For example, an RGB
2020        source image with a ``tRNS`` chunk to represent a transparent
2021        colour, will have ``planes=3`` and ``alpha=False`` for the
2022        source image, but the *meta* dictionary returned by this method
2023        will have ``planes=4`` and ``alpha=True`` because an alpha
2024        channel is synthesized and added.
2025
2026        *pixels* is the pixel data in boxed row flat pixel format (just
2027        like the :meth:`read` method).
2028
2029        All the other aspects of the image data are not changed.
2030        """
2031
2032        self.preamble()
2033
2034        # Simple case, no conversion necessary.
2035        if not self.colormap and not self.trns and not self.sbit:
2036            return self.read()
2037
2038        x, y, pixels, meta = self.read()
2039
2040        if self.colormap:
2041            meta["colormap"] = False
2042            meta["alpha"] = bool(self.trns)
2043            meta["bitdepth"] = 8
2044            meta["planes"] = 3 + bool(self.trns)
2045            plte = self.palette()
2046
2047            def iterpal(pixels):
2048                for row in pixels:
2049                    row = map(plte.__getitem__, row)
2050                    yield array("B", itertools.chain(*row))
2051
2052            pixels = iterpal(pixels)
2053        elif self.trns:
2054            # It would be nice if there was some reasonable way of doing
2055            # this without generating a whole load of intermediate tuples.
2056            # But tuples does seem like the easiest way, with no other way
2057            # clearly much simpler or much faster.  (Actually, the L to LA
2058            # conversion could perhaps go faster (all those 1-tuples!), but
2059            # I still wonder whether the code proliferation is worth it)
2060            it = self.transparent
2061            maxval = 2 ** meta["bitdepth"] - 1
2062            planes = meta["planes"]
2063            meta["alpha"] = True
2064            meta["planes"] += 1
2065            typecode = "BH"[meta["bitdepth"] > 8]
2066
2067            def itertrns(pixels):
2068                for row in pixels:
2069                    # For each row we group it into pixels, then form a
2070                    # characterisation vector that says whether each pixel
2071                    # is opaque or not.  Then we convert True/False to
2072                    # 0/maxval (by multiplication), and add it as the extra
2073                    # channel.
2074                    row = group(row, planes)
2075                    opa = map(it.__ne__, row)
2076                    opa = map(maxval.__mul__, opa)
2077                    opa = zip(opa)  # convert to 1-tuples
2078                    yield array(typecode, itertools.chain(*map(operator.add, row, opa)))
2079
2080            pixels = itertrns(pixels)
2081        targetbitdepth = None
2082        if self.sbit:
2083            sbit = struct.unpack("%dB" % len(self.sbit), self.sbit)
2084            targetbitdepth = max(sbit)
2085            if targetbitdepth > meta["bitdepth"]:
2086                raise Error("sBIT chunk %r exceeds bitdepth %d" % (sbit, self.bitdepth))
2087            if min(sbit) <= 0:
2088                raise Error("sBIT chunk %r has a 0-entry" % sbit)
2089            if targetbitdepth == meta["bitdepth"]:
2090                targetbitdepth = None
2091        if targetbitdepth:
2092            shift = meta["bitdepth"] - targetbitdepth
2093            meta["bitdepth"] = targetbitdepth
2094
2095            def itershift(pixels):
2096                for row in pixels:
2097                    yield map(shift.__rrshift__, row)
2098
2099            pixels = itershift(pixels)
2100        return x, y, pixels, meta
2101
2102    def asFloat(self, maxval=1.0):
2103        """Return image pixels as per :meth:`asDirect` method, but scale
2104        all pixel values to be floating point values between 0.0 and
2105        *maxval*.
2106        """
2107
2108        x, y, pixels, info = self.asDirect()
2109        sourcemaxval = 2 ** info["bitdepth"] - 1
2110        del info["bitdepth"]
2111        info["maxval"] = float(maxval)
2112        factor = float(maxval) / float(sourcemaxval)
2113
2114        def iterfloat():
2115            for row in pixels:
2116                yield map(factor.__mul__, row)
2117
2118        return x, y, iterfloat(), info
2119
2120    def _as_rescale(self, get, targetbitdepth):
2121        """Helper used by :meth:`asRGB8` and :meth:`asRGBA8`."""
2122
2123        width, height, pixels, meta = get()
2124        maxval = 2 ** meta["bitdepth"] - 1
2125        targetmaxval = 2 ** targetbitdepth - 1
2126        factor = float(targetmaxval) / float(maxval)
2127        meta["bitdepth"] = targetbitdepth
2128
2129        def iterscale():
2130            for row in pixels:
2131                yield map(lambda x: int(round(x * factor)), row)
2132
2133        return width, height, iterscale(), meta
2134
2135    def asRGB8(self):
2136        """Return the image data as an RGB pixels with 8-bits per
2137        sample.  This is like the :meth:`asRGB` method except that
2138        this method additionally rescales the values so that they
2139        are all between 0 and 255 (8-bit).  In the case where the
2140        source image has a bit depth < 8 the transformation preserves
2141        all the information; where the source image has bit depth
2142        > 8, then rescaling to 8-bit values loses precision.  No
2143        dithering is performed.  Like :meth:`asRGB`, an alpha channel
2144        in the source image will raise an exception.
2145
2146        This function returns a 4-tuple:
2147        (*width*, *height*, *pixels*, *metadata*).
2148        *width*, *height*, *metadata* are as per the :meth:`read` method.
2149
2150        *pixels* is the pixel data in boxed row flat pixel format.
2151        """
2152
2153        return self._as_rescale(self.asRGB, 8)
2154
2155    def asRGBA8(self):
2156        """Return the image data as RGBA pixels with 8-bits per
2157        sample.  This method is similar to :meth:`asRGB8` and
2158        :meth:`asRGBA`:  The result pixels have an alpha channel, *and*
2159        values are rescaled to the range 0 to 255.  The alpha channel is
2160        synthesized if necessary (with a small speed penalty).
2161        """
2162
2163        return self._as_rescale(self.asRGBA, 8)
2164
2165    def asRGB(self):
2166        """Return image as RGB pixels.  RGB colour images are passed
2167        through unchanged; greyscales are expanded into RGB
2168        triplets (there is a small speed overhead for doing this).
2169
2170        An alpha channel in the source image will raise an
2171        exception.
2172
2173        The return values are as for the :meth:`read` method
2174        except that the *metadata* reflect the returned pixels, not the
2175        source image.  In particular, for this method
2176        ``metadata['greyscale']`` will be ``False``.
2177        """
2178
2179        width, height, pixels, meta = self.asDirect()
2180        if meta["alpha"]:
2181            raise Error("will not convert image with alpha channel to RGB")
2182        if not meta["greyscale"]:
2183            return width, height, pixels, meta
2184        meta["greyscale"] = False
2185        typecode = "BH"[meta["bitdepth"] > 8]
2186
2187        def iterrgb():
2188            for row in pixels:
2189                a = array(typecode, [0]) * 3 * width
2190                for i in range(3):
2191                    a[i::3] = row
2192                yield a
2193
2194        return width, height, iterrgb(), meta
2195
2196    def asRGBA(self):
2197        """Return image as RGBA pixels.  Greyscales are expanded into
2198        RGB triplets; an alpha channel is synthesized if necessary.
2199        The return values are as for the :meth:`read` method
2200        except that the *metadata* reflect the returned pixels, not the
2201        source image.  In particular, for this method
2202        ``metadata['greyscale']`` will be ``False``, and
2203        ``metadata['alpha']`` will be ``True``.
2204        """
2205
2206        width, height, pixels, meta = self.asDirect()
2207        if meta["alpha"] and not meta["greyscale"]:
2208            return width, height, pixels, meta
2209        typecode = "BH"[meta["bitdepth"] > 8]
2210        maxval = 2 ** meta["bitdepth"] - 1
2211
2212        def newarray():
2213            return array(typecode, [0]) * 4 * width
2214
2215        if meta["alpha"] and meta["greyscale"]:
2216            # LA to RGBA
2217            def convert():
2218                for row in pixels:
2219                    # Create a fresh target row, then copy L channel
2220                    # into first three target channels, and A channel
2221                    # into fourth channel.
2222                    a = newarray()
2223                    for i in range(3):
2224                        a[i::4] = row[0::2]
2225                    a[3::4] = row[1::2]
2226                    yield a
2227
2228        elif meta["greyscale"]:
2229            # L to RGBA
2230            def convert():
2231                for row in pixels:
2232                    a = newarray()
2233                    for i in range(3):
2234                        a[i::4] = row
2235                    a[3::4] = array(typecode, [maxval]) * width
2236                    yield a
2237
2238        else:
2239            assert not meta["alpha"] and not meta["greyscale"]
2240            # RGB to RGBA
2241            def convert():
2242                for row in pixels:
2243                    a = newarray()
2244                    for i in range(3):
2245                        a[i::4] = row[i::3]
2246                    a[3::4] = array(typecode, [maxval]) * width
2247                    yield a
2248
2249        meta["alpha"] = True
2250        meta["greyscale"] = False
2251        return width, height, convert(), meta
2252
2253
2254# === Internal Test Support ===
2255
2256# This section comprises the tests that are internally validated (as
2257# opposed to tests which produce output files that are externally
2258# validated).  Primarily they are unittests.
2259
2260# Note that it is difficult to internally validate the results of
2261# writing a PNG file.  The only thing we can do is read it back in
2262# again, which merely checks consistency, not that the PNG file we
2263# produce is valid.
2264
2265# Run the tests from the command line:
2266# python -c 'import png;png.test()'
2267
2268# (For an in-memory binary file IO object) We use BytesIO where
2269# available, otherwise we use StringIO, but name it BytesIO.
2270try:
2271    from io import BytesIO
2272except:
2273    from StringIO import StringIO as BytesIO
2274import tempfile
2275import unittest
2276
2277
2278def test():
2279    unittest.main(__name__)
2280
2281
2282def topngbytes(name, rows, x, y, **k):
2283    """Convenience function for creating a PNG file "in memory" as a
2284    string.  Creates a :class:`Writer` instance using the keyword arguments,
2285    then passes `rows` to its :meth:`Writer.write` method.  The resulting
2286    PNG file is returned as a string.  `name` is used to identify the file for
2287    debugging.
2288    """
2289
2290    import os
2291
2292    print(name)
2293    f = BytesIO()
2294    w = Writer(x, y, **k)
2295    w.write(f, rows)
2296    if os.environ.get("PYPNG_TEST_TMP"):
2297        w = open(name, "wb")
2298        w.write(f.getvalue())
2299        w.close()
2300    return f.getvalue()
2301
2302
2303def testWithIO(inp, out, f):
2304    """Calls the function `f` with ``sys.stdin`` changed to `inp`
2305    and ``sys.stdout`` changed to `out`.  They are restored when `f`
2306    returns.  This function returns whatever `f` returns.
2307    """
2308
2309    import os
2310
2311    try:
2312        oldin, sys.stdin = sys.stdin, inp
2313        oldout, sys.stdout = sys.stdout, out
2314        x = f()
2315    finally:
2316        sys.stdin = oldin
2317        sys.stdout = oldout
2318    if os.environ.get("PYPNG_TEST_TMP") and hasattr(out, "getvalue"):
2319        name = mycallersname()
2320        if name:
2321            w = open(name + ".png", "wb")
2322            w.write(out.getvalue())
2323            w.close()
2324    return x
2325
2326
2327def mycallersname():
2328    """Returns the name of the caller of the caller of this function
2329    (hence the name of the caller of the function in which
2330    "mycallersname()" textually appears).  Returns None if this cannot
2331    be determined."""
2332
2333    # http://docs.python.org/library/inspect.html#the-interpreter-stack
2334    import inspect
2335
2336    frame = inspect.currentframe()
2337    if not frame:
2338        return None
2339    frame_, filename_, lineno_, funname, linelist_, listi_ = inspect.getouterframes(
2340        frame
2341    )[2]
2342    return funname
2343
2344
2345def seqtobytes(s):
2346    """Convert a sequence of integers to a *bytes* instance.  Good for
2347    plastering over Python 2 / Python 3 cracks.
2348    """
2349
2350    return strtobytes("".join(chr(x) for x in s))
2351
2352
2353class Test(unittest.TestCase):
2354    # This member is used by the superclass.  If we don't define a new
2355    # class here then when we use self.assertRaises() and the PyPNG code
2356    # raises an assertion then we get no proper traceback.  I can't work
2357    # out why, but defining a new class here means we get a proper
2358    # traceback.
2359    class failureException(Exception):
2360        pass
2361
2362    def helperLN(self, n):
2363        mask = (1 << n) - 1
2364        # Use small chunk_limit so that multiple chunk writing is
2365        # tested.  Making it a test for Issue 20.
2366        w = Writer(15, 17, greyscale=True, bitdepth=n, chunk_limit=99)
2367        f = BytesIO()
2368        w.write_array(f, array("B", map(mask.__and__, range(1, 256))))
2369        r = Reader(bytes=f.getvalue())
2370        x, y, pixels, meta = r.read()
2371        self.assertEqual(x, 15)
2372        self.assertEqual(y, 17)
2373        self.assertEqual(
2374            list(itertools.chain(*pixels)), map(mask.__and__, range(1, 256))
2375        )
2376
2377    def testL8(self):
2378        return self.helperLN(8)
2379
2380    def testL4(self):
2381        return self.helperLN(4)
2382
2383    def testL2(self):
2384        "Also tests asRGB8."
2385        w = Writer(1, 4, greyscale=True, bitdepth=2)
2386        f = BytesIO()
2387        w.write_array(f, array("B", range(4)))
2388        r = Reader(bytes=f.getvalue())
2389        x, y, pixels, meta = r.asRGB8()
2390        self.assertEqual(x, 1)
2391        self.assertEqual(y, 4)
2392        for i, row in enumerate(pixels):
2393            self.assertEqual(len(row), 3)
2394            self.assertEqual(list(row), [0x55 * i] * 3)
2395
2396    def testP2(self):
2397        "2-bit palette."
2398        a = (255, 255, 255)
2399        b = (200, 120, 120)
2400        c = (50, 99, 50)
2401        w = Writer(1, 4, bitdepth=2, palette=[a, b, c])
2402        f = BytesIO()
2403        w.write_array(f, array("B", (0, 1, 1, 2)))
2404        r = Reader(bytes=f.getvalue())
2405        x, y, pixels, meta = r.asRGB8()
2406        self.assertEqual(x, 1)
2407        self.assertEqual(y, 4)
2408        self.assertEqual(list(pixels), map(list, [a, b, b, c]))
2409
2410    def testPtrns(self):
2411        "Test colour type 3 and tRNS chunk (and 4-bit palette)."
2412        a = (50, 99, 50, 50)
2413        b = (200, 120, 120, 80)
2414        c = (255, 255, 255)
2415        d = (200, 120, 120)
2416        e = (50, 99, 50)
2417        w = Writer(3, 3, bitdepth=4, palette=[a, b, c, d, e])
2418        f = BytesIO()
2419        w.write_array(f, array("B", (4, 3, 2, 3, 2, 0, 2, 0, 1)))
2420        r = Reader(bytes=f.getvalue())
2421        x, y, pixels, meta = r.asRGBA8()
2422        self.assertEqual(x, 3)
2423        self.assertEqual(y, 3)
2424        c = c + (255,)
2425        d = d + (255,)
2426        e = e + (255,)
2427        boxed = [(e, d, c), (d, c, a), (c, a, b)]
2428        flat = map(lambda row: itertools.chain(*row), boxed)
2429        self.assertEqual(map(list, pixels), map(list, flat))
2430
2431    def testRGBtoRGBA(self):
2432        "asRGBA8() on colour type 2 source." ""
2433        # Test for Issue 26
2434        r = Reader(bytes=_pngsuite["basn2c08"])
2435        x, y, pixels, meta = r.asRGBA8()
2436        # Test the pixels at row 9 columns 0 and 1.
2437        row9 = list(pixels)[9]
2438        self.assertEqual(row9[0:8], [0xFF, 0xDF, 0xFF, 0xFF, 0xFF, 0xDE, 0xFF, 0xFF])
2439
2440    def testLtoRGBA(self):
2441        "asRGBA() on grey source." ""
2442        # Test for Issue 60
2443        r = Reader(bytes=_pngsuite["basi0g08"])
2444        x, y, pixels, meta = r.asRGBA()
2445        row9 = list(list(pixels)[9])
2446        self.assertEqual(row9[0:8], [222, 222, 222, 255, 221, 221, 221, 255])
2447
2448    def testCtrns(self):
2449        "Test colour type 2 and tRNS chunk."
2450        # Test for Issue 25
2451        r = Reader(bytes=_pngsuite["tbrn2c08"])
2452        x, y, pixels, meta = r.asRGBA8()
2453        # I just happen to know that the first pixel is transparent.
2454        # In particular it should be #7f7f7f00
2455        row0 = list(pixels)[0]
2456        self.assertEqual(tuple(row0[0:4]), (0x7F, 0x7F, 0x7F, 0x00))
2457
2458    def testAdam7read(self):
2459        """Adam7 interlace reading.
2460        Specifically, test that for images in the PngSuite that
2461        have both an interlaced and straightlaced pair that both
2462        images from the pair produce the same array of pixels."""
2463        for candidate in _pngsuite:
2464            if not candidate.startswith("basn"):
2465                continue
2466            candi = candidate.replace("n", "i")
2467            if candi not in _pngsuite:
2468                continue
2469            print("adam7 read %s" % (candidate,))
2470            straight = Reader(bytes=_pngsuite[candidate])
2471            adam7 = Reader(bytes=_pngsuite[candi])
2472            # Just compare the pixels.  Ignore x,y (because they're
2473            # likely to be correct?); metadata is ignored because the
2474            # "interlace" member differs.  Lame.
2475            straight = straight.read()[2]
2476            adam7 = adam7.read()[2]
2477            self.assertEqual(map(list, straight), map(list, adam7))
2478
2479    def testAdam7write(self):
2480        """Adam7 interlace writing.
2481        For each test image in the PngSuite, write an interlaced
2482        and a straightlaced version.  Decode both, and compare results.
2483        """
2484        # Not such a great test, because the only way we can check what
2485        # we have written is to read it back again.
2486
2487        for name, bytes in _pngsuite.items():
2488            # Only certain colour types supported for this test.
2489            if name[3:5] not in ["n0", "n2", "n4", "n6"]:
2490                continue
2491            it = Reader(bytes=bytes)
2492            x, y, pixels, meta = it.read()
2493            pngi = topngbytes(
2494                "adam7wn" + name + ".png",
2495                pixels,
2496                x=x,
2497                y=y,
2498                bitdepth=it.bitdepth,
2499                greyscale=it.greyscale,
2500                alpha=it.alpha,
2501                transparent=it.transparent,
2502                interlace=False,
2503            )
2504            x, y, ps, meta = Reader(bytes=pngi).read()
2505            it = Reader(bytes=bytes)
2506            x, y, pixels, meta = it.read()
2507            pngs = topngbytes(
2508                "adam7wi" + name + ".png",
2509                pixels,
2510                x=x,
2511                y=y,
2512                bitdepth=it.bitdepth,
2513                greyscale=it.greyscale,
2514                alpha=it.alpha,
2515                transparent=it.transparent,
2516                interlace=True,
2517            )
2518            x, y, pi, meta = Reader(bytes=pngs).read()
2519            self.assertEqual(map(list, ps), map(list, pi))
2520
2521    def testPGMin(self):
2522        """Test that the command line tool can read PGM files."""
2523
2524        def do():
2525            return _main(["testPGMin"])
2526
2527        s = BytesIO()
2528        s.write(strtobytes("P5 2 2 3\n"))
2529        s.write(strtobytes("\x00\x01\x02\x03"))
2530        s.flush()
2531        s.seek(0)
2532        o = BytesIO()
2533        testWithIO(s, o, do)
2534        r = Reader(bytes=o.getvalue())
2535        x, y, pixels, meta = r.read()
2536        self.assertTrue(r.greyscale)
2537        self.assertEqual(r.bitdepth, 2)
2538
2539    def testPAMin(self):
2540        """Test that the command line tool can read PAM file."""
2541
2542        def do():
2543            return _main(["testPAMin"])
2544
2545        s = BytesIO()
2546        s.write(
2547            strtobytes(
2548                "P7\nWIDTH 3\nHEIGHT 1\nDEPTH 4\nMAXVAL 255\n"
2549                "TUPLTYPE RGB_ALPHA\nENDHDR\n"
2550            )
2551        )
2552        # The pixels in flat row flat pixel format
2553        flat = [255, 0, 0, 255, 0, 255, 0, 120, 0, 0, 255, 30]
2554        asbytes = seqtobytes(flat)
2555        s.write(asbytes)
2556        s.flush()
2557        s.seek(0)
2558        o = BytesIO()
2559        testWithIO(s, o, do)
2560        r = Reader(bytes=o.getvalue())
2561        x, y, pixels, meta = r.read()
2562        self.assertTrue(r.alpha)
2563        self.assertTrue(not r.greyscale)
2564        self.assertEqual(list(itertools.chain(*pixels)), flat)
2565
2566    def testLA4(self):
2567        """Create an LA image with bitdepth 4."""
2568        bytes = topngbytes(
2569            "la4.png", [[5, 12]], 1, 1, greyscale=True, alpha=True, bitdepth=4
2570        )
2571        sbit = Reader(bytes=bytes).chunk("sBIT")[1]
2572        self.assertEqual(sbit, strtobytes("\x04\x04"))
2573
2574    def testPNMsbit(self):
2575        """Test that PNM files can generates sBIT chunk."""
2576
2577        def do():
2578            return _main(["testPNMsbit"])
2579
2580        s = BytesIO()
2581        s.write(strtobytes("P6 8 1 1\n"))
2582        for pixel in range(8):
2583            s.write(struct.pack("<I", (0x4081 * pixel) & 0x10101)[:3])
2584        s.flush()
2585        s.seek(0)
2586        o = BytesIO()
2587        testWithIO(s, o, do)
2588        r = Reader(bytes=o.getvalue())
2589        sbit = r.chunk("sBIT")[1]
2590        self.assertEqual(sbit, strtobytes("\x01\x01\x01"))
2591
2592    def testLtrns0(self):
2593        """Create greyscale image with tRNS chunk."""
2594        return self.helperLtrns(0)
2595
2596    def testLtrns1(self):
2597        """Using 1-tuple for transparent arg."""
2598        return self.helperLtrns((0,))
2599
2600    def helperLtrns(self, transparent):
2601        """Helper used by :meth:`testLtrns*`."""
2602        pixels = zip([0x00, 0x38, 0x4C, 0x54, 0x5C, 0x40, 0x38, 0x00])
2603        o = BytesIO()
2604        w = Writer(8, 8, greyscale=True, bitdepth=1, transparent=transparent)
2605        w.write_packed(o, pixels)
2606        r = Reader(bytes=o.getvalue())
2607        x, y, pixels, meta = r.asDirect()
2608        self.assertTrue(meta["alpha"])
2609        self.assertTrue(meta["greyscale"])
2610        self.assertEqual(meta["bitdepth"], 1)
2611
2612    def testWinfo(self):
2613        """Test the dictionary returned by a `read` method can be used
2614        as args for :meth:`Writer`.
2615        """
2616        r = Reader(bytes=_pngsuite["basn2c16"])
2617        info = r.read()[3]
2618        w = Writer(**info)
2619
2620    def testPackedIter(self):
2621        """Test iterator for row when using write_packed.
2622
2623        Indicative for Issue 47.
2624        """
2625        w = Writer(16, 2, greyscale=True, alpha=False, bitdepth=1)
2626        o = BytesIO()
2627        w.write_packed(
2628            o, [itertools.chain([0x0A], [0xAA]), itertools.chain([0x0F], [0xFF])]
2629        )
2630        r = Reader(bytes=o.getvalue())
2631        x, y, pixels, info = r.asDirect()
2632        pixels = list(pixels)
2633        self.assertEqual(len(pixels), 2)
2634        self.assertEqual(len(pixels[0]), 16)
2635
2636    def testInterlacedArray(self):
2637        """Test that reading an interlaced PNG yields each row as an
2638        array."""
2639        r = Reader(bytes=_pngsuite["basi0g08"])
2640        list(r.read()[2])[0].tostring
2641
2642    def testTrnsArray(self):
2643        """Test that reading a type 2 PNG with tRNS chunk yields each
2644        row as an array (using asDirect)."""
2645        r = Reader(bytes=_pngsuite["tbrn2c08"])
2646        list(r.asDirect()[2])[0].tostring
2647
2648    # Invalid file format tests.  These construct various badly
2649    # formatted PNG files, then feed them into a Reader.  When
2650    # everything is working properly, we should get FormatError
2651    # exceptions raised.
2652    def testEmpty(self):
2653        """Test empty file."""
2654
2655        r = Reader(bytes="")
2656        self.assertRaises(FormatError, r.asDirect)
2657
2658    def testSigOnly(self):
2659        """Test file containing just signature bytes."""
2660
2661        r = Reader(bytes=_signature)
2662        self.assertRaises(FormatError, r.asDirect)
2663
2664    def testExtraPixels(self):
2665        """Test file that contains too many pixels."""
2666
2667        def eachchunk(chunk):
2668            if chunk[0] != "IDAT":
2669                return chunk
2670            data = zlib.decompress(chunk[1])
2671            data += strtobytes("\x00garbage")
2672            data = zlib.compress(data)
2673            chunk = (chunk[0], data)
2674            return chunk
2675
2676        self.assertRaises(FormatError, self.helperFormat, eachchunk)
2677
2678    def testNotEnoughPixels(self):
2679        def eachchunk(chunk):
2680            if chunk[0] != "IDAT":
2681                return chunk
2682            # Remove last byte.
2683            data = zlib.decompress(chunk[1])
2684            data = data[:-1]
2685            data = zlib.compress(data)
2686            return (chunk[0], data)
2687
2688        self.assertRaises(FormatError, self.helperFormat, eachchunk)
2689
2690    def helperFormat(self, f):
2691        r = Reader(bytes=_pngsuite["basn0g01"])
2692        o = BytesIO()
2693
2694        def newchunks():
2695            for chunk in r.chunks():
2696                yield f(chunk)
2697
2698        write_chunks(o, newchunks())
2699        r = Reader(bytes=o.getvalue())
2700        return list(r.asDirect()[2])
2701
2702    def testBadFilter(self):
2703        def eachchunk(chunk):
2704            if chunk[0] != "IDAT":
2705                return chunk
2706            data = zlib.decompress(chunk[1])
2707            # Corrupt the first filter byte
2708            data = strtobytes("\x99") + data[1:]
2709            data = zlib.compress(data)
2710            return (chunk[0], data)
2711
2712        self.assertRaises(FormatError, self.helperFormat, eachchunk)
2713
2714    def testFlat(self):
2715        """Test read_flat."""
2716        import hashlib
2717
2718        r = Reader(bytes=_pngsuite["basn0g02"])
2719        x, y, pixel, meta = r.read_flat()
2720        d = hashlib.md5(seqtobytes(pixel)).digest()
2721        self.assertEqual(_enhex(d), "255cd971ab8cd9e7275ff906e5041aa0")
2722
2723    def testfromarray(self):
2724        img = from_array([[0, 0x33, 0x66], [0xFF, 0xCC, 0x99]], "L")
2725        img.save("testfromarray.png")
2726
2727    def testfromarrayL16(self):
2728        img = from_array(group(range(2 ** 16), 256), "L;16")
2729        img.save("testL16.png")
2730
2731    def testfromarrayRGB(self):
2732        img = from_array(
2733            [
2734                [0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1],
2735                [1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1],
2736            ],
2737            "RGB;1",
2738        )
2739        o = BytesIO()
2740        img.save(o)
2741
2742    def testfromarrayIter(self):
2743        import itertools
2744
2745        i = itertools.islice(itertools.count(10), 20)
2746        i = imap_(lambda x: [x, x, x], i)
2747        img = from_array(i, "RGB;5", dict(height=20))
2748        f = open("testiter.png", "wb")
2749        img.save(f)
2750        f.close()
2751
2752    # numpy dependent tests.  These are skipped (with a message to
2753    # sys.stderr) if numpy cannot be imported.
2754    def testNumpyuint16(self):
2755        """numpy uint16."""
2756
2757        try:
2758            import numpy
2759        except ImportError:
2760            sys.stderr.write("skipping numpy test\n")
2761            return
2762
2763        rows = [map(numpy.uint16, range(0, 0x10000, 0x5555))]
2764        b = topngbytes(
2765            "numpyuint16.png", rows, 4, 1, greyscale=True, alpha=False, bitdepth=16
2766        )
2767
2768    def testNumpyuint8(self):
2769        """numpy uint8."""
2770
2771        try:
2772            import numpy
2773        except ImportError:
2774            sys.stderr.write("skipping numpy test\n")
2775            return
2776
2777        rows = [map(numpy.uint8, range(0, 0x100, 0x55))]
2778        b = topngbytes(
2779            "numpyuint8.png", rows, 4, 1, greyscale=True, alpha=False, bitdepth=8
2780        )
2781
2782    def testNumpybool(self):
2783        """numpy bool."""
2784
2785        try:
2786            import numpy
2787        except ImportError:
2788            sys.stderr.write("skipping numpy test\n")
2789            return
2790
2791        rows = [map(numpy.bool, [0, 1])]
2792        b = topngbytes(
2793            "numpybool.png", rows, 2, 1, greyscale=True, alpha=False, bitdepth=1
2794        )
2795
2796    def testNumpyarray(self):
2797        """numpy array."""
2798        try:
2799            import numpy
2800        except ImportError:
2801            sys.stderr.write("skipping numpy test\n")
2802            return
2803
2804        pixels = numpy.array([[0, 0x5555], [0x5555, 0xAAAA]], numpy.uint16)
2805        img = from_array(pixels, "L")
2806        img.save("testnumpyL16.png")
2807
2808
2809# === Command Line Support ===
2810
2811
2812def _dehex(s):
2813    """Liberally convert from hex string to binary string."""
2814    import re
2815    import binascii
2816
2817    # Remove all non-hexadecimal digits
2818    s = re.sub(r"[^a-fA-F\d]", "", s)
2819    # binscii.unhexlify works in Python 2 and Python 3 (unlike
2820    # thing.decode('hex')).
2821    return binascii.unhexlify(strtobytes(s))
2822
2823
2824def _enhex(s):
2825    """Convert from binary string (bytes) to hex string (str)."""
2826
2827    import binascii
2828
2829    return bytestostr(binascii.hexlify(s))
2830
2831
2832# Copies of PngSuite test files taken
2833# from http://www.schaik.com/pngsuite/pngsuite_bas_png.html
2834# on 2009-02-19 by drj and converted to hex.
2835# Some of these are not actually in PngSuite (but maybe they should
2836# be?), they use the same naming scheme, but start with a capital
2837# letter.
2838_pngsuite = {
2839    "basi0g01": _dehex(
2840        """
284189504e470d0a1a0a0000000d49484452000000200000002001000000012c0677
2842cf0000000467414d41000186a031e8965f0000009049444154789c2d8d310ec2
2843300c45dfc682c415187a00a42e197ab81e83b127e00c5639001363a580d8582c
284465c910357c4b78b0bfbfdf4f70168c19e7acb970a3f2d1ded9695ce5bf5963df
2845d92aaf4c9fd927ea449e6487df5b9c36e799b91bdf082b4d4bd4014fe4014b01
2846ab7a17aee694d28d328a2d63837a70451e1648702d9a9ff4a11d2f7a51aa21e5
2847a18c7ffd0094e3511d661822f20000000049454e44ae426082
2848"""
2849    ),
2850    "basi0g02": _dehex(
2851        """
285289504e470d0a1a0a0000000d49484452000000200000002002000000016ba60d
28531f0000000467414d41000186a031e8965f0000005149444154789c635062e860
285400e17286bb609c93c370ec189494960631366e4467b3ae675dcf10f521ea0303
285590c1ca006444e11643482064114a4852c710baea3f18c31918020c30410403a6
28560ac1a09239009c52804d85b6d97d0000000049454e44ae426082
2857"""
2858    ),
2859    "basi0g04": _dehex(
2860        """
286189504e470d0a1a0a0000000d4948445200000020000000200400000001e4e6f8
2862bf0000000467414d41000186a031e8965f000000ae49444154789c658e5111c2
2863301044171c141c141c041c843a287510ea20d441c041c141c141c04191102454
286403994998cecd7edcecedbb9bdbc3b2c2b6457545fbc4bac1be437347f7c66a77
28653c23d60db15e88f5c5627338a5416c2e691a9b475a89cd27eda12895ae8dfdab
286643d61e590764f5c83a226b40d669bec307f93247701687723abf31ff83a2284b
2867a5b4ae6b63ac6520ad730ca4ed7b06d20e030369bd6720ed383290360406d24e
286813811f2781eba9d34d07160000000049454e44ae426082
2869"""
2870    ),
2871    "basi0g08": _dehex(
2872        """
287389504e470d0a1a0a0000000d4948445200000020000000200800000001211615
2874be0000000467414d41000186a031e8965f000000b549444154789cb5905d0ac2
28753010849dbac81c42c47bf843cf253e8878b0aa17110f214bdca6be240f5d21a5
287694ced3e49bcd322c1624115515154998aa424822a82a5624a1aa8a8b24c58f99
2877999908130989a04a00d76c2c09e76cf21adcb209393a6553577da17140a2c59e
287870ecbfa388dff1f03b82fb82bd07f05f7cb13f80bb07ad2fd60c011c3c588eef
2879f1f4e03bbec7ce832dca927aea005e431b625796345307b019c845e6bfc3bb98
2880769d84f9efb02ea6c00f9bb9ff45e81f9f280000000049454e44ae426082
2881"""
2882    ),
2883    "basi0g16": _dehex(
2884        """
288589504e470d0a1a0a0000000d49484452000000200000002010000000017186c9
2886fd0000000467414d41000186a031e8965f000000e249444154789cb5913b0ec2
2887301044c7490aa8f85d81c3e4301c8f53a4ca0da8902c8144b3920b4043111282
288823bc4956681a6bf5fc3c5a3ba0448912d91a4de2c38dd8e380231eede4c4f7a1
28894677700bec7bd9b1d344689315a3418d1a6efbe5b8305ba01f8ff4808c063e26
2890c60d5c81edcf6c58c535e252839e93801b15c0a70d810ae0d306b205dc32b187
2891272b64057e4720ff0502154034831520154034c3df81400510cdf0015c86e5cc
28925c79c639fddba9dcb5456b51d7980eb52d8e7d7fa620a75120d6064641a05120
2893b606771a05626b401a05f1f589827cf0fe44c1f0bae0055698ee8914fffffe00
289400000049454e44ae426082
2895"""
2896    ),
2897    "basi2c08": _dehex(
2898        """
289989504e470d0a1a0a0000000d49484452000000200000002008020000018b1fdd
2900350000000467414d41000186a031e8965f000000f249444154789cd59341aa04
2901210c44abc07b78133d59d37333bd89d76868b566d10cf4675af8596431a11662
29027c5688919280e312257dd6a0a4cf1a01008ee312a5f3c69c37e6fcc3f47e6776
2903a07f8bdaf5b40feed2d33e025e2ff4fe2d4a63e1a16d91180b736d8bc45854c5
29046d951863f4a7e0b66dcf09a900f3ffa2948d4091e53ca86c048a64390f662b50
29054a999660ced906182b9a01a8be00a56404a6ede182b1223b4025e32c4de34304
290663457680c93aada6c99b73865aab2fc094920d901a203f5ddfe1970d28456783
290726cffbafeffcd30654f46d119be4793f827387fc0d189d5bc4d69a3c23d45a7f
2908db803146578337df4d0a3121fc3d330000000049454e44ae426082
2909"""
2910    ),
2911    "basi2c16": _dehex(
2912        """
291389504e470d0a1a0a0000000d4948445200000020000000201002000001db8f01
2914760000000467414d41000186a031e8965f0000020a49444154789cd5962173e3
29153010853fcf1838cc61a1818185a53e56787fa13fa130852e3b5878b4b0b03081
2916b97f7030070b53e6b057a0a8912bbb9163b9f109ececbc59bd7dcf2b45492409
2917d66f00eb1dd83cb5497d65456aeb8e1040913b3b2c04504c936dd5a9c7e2c6eb
2918b1b8f17a58e8d043da56f06f0f9f62e5217b6ba3a1b76f6c9e99e8696a2a72e2
2919c4fb1e4d452e92ec9652b807486d12b6669be00db38d9114b0c1961e375461a5
29205f76682a85c367ad6f682ff53a9c2a353191764b78bb07d8ddc3c97c1950f391
29216745c7b9852c73c2f212605a466a502705c8338069c8b9e84efab941eb393a97
2922d4c9fd63148314209f1c1d3434e847ead6380de291d6f26a25c1ebb5047f5f24
2923d85c49f0f22cc1d34282c72709cab90477bf25b89d49f0f351822297e0ea9704
2924f34c82bc94002448ede51866e5656aef5d7c6a385cb4d80e6a538ceba04e6df2
2925480e9aa84ddedb413bb5c97b3838456df2d4fec2c7a706983e7474d085fae820
2926a841776a83073838973ac0413fea2f1dc4a06e71108fda73109bdae48954ad60
2927bf867aac3ce44c7c1589a711cf8a81df9b219679d96d1cec3d8bbbeaa2012626
2928df8c7802eda201b2d2e0239b409868171fc104ba8b76f10b4da09f6817ffc609
2929c413ede267fd1fbab46880c90f80eccf0013185eb48b47ba03df2bdaadef3181
2930cb8976f18e13188768170f98c0f844bb78cb04c62ddac59d09fc3fa25dfc1da4
293114deb3df1344f70000000049454e44ae426082
2932"""
2933    ),
2934    "basi3p08": _dehex(
2935        """
293689504e470d0a1a0a0000000d494844520000002000000020080300000133a3ba
2937500000000467414d41000186a031e8965f00000300504c5445224400f5ffed77
2938ff77cbffff110a003a77002222ffff11ff110000222200ffac5566ff66ff6666
2939ff01ff221200dcffffccff994444ff005555220000cbcbff44440055ff55cbcb
294000331a00ffecdcedffffe4ffcbffdcdc44ff446666ff330000442200ededff66
29416600ffa444ffffaaeded0000cbcbfefffffdfffeffff0133ff33552a000101ff
29428888ff00aaaa010100440000888800ffe4cbba5b0022ff22663200ffff99aaaa
2943ff550000aaaa00cb630011ff11d4ffaa773a00ff4444dc6b0066000001ff0188
29444200ecffdc6bdc00ffdcba00333300ed00ed7300ffff88994a0011ffff770000
2945ff8301ffbabafe7b00fffeff00cb00ff999922ffff880000ffff77008888ffdc
2946ff1a33000000aa33ffff009900990000000001326600ffbaff44ffffffaaff00
2947770000fefeaa00004a9900ffff66ff22220000998bff1155ffffff0101ff88ff
2948005500001111fffffefffdfea4ff4466ffffff66ff003300ffff55ff77770000
294988ff44ff00110077ffff006666ffffed000100fff5ed1111ffffff44ff22ffff
2950eded11110088ffff00007793ff2200dcdc3333fffe00febabaff99ffff333300
295163cb00baba00acff55ffffdcffff337bfe00ed00ed5555ffaaffffdcdcff5555
295200000066dcdc00dc00dc83ff017777fffefeffffffcbff5555777700fefe00cb
295300cb0000fe010200010000122200ffff220044449bff33ffd4aa0000559999ff
2954999900ba00ba2a5500ffcbcbb4ff66ff9b33ffffbaaa00aa42880053aa00ffaa
2955aa0000ed00babaffff1100fe00000044009999990099ffcc99ba000088008800
2956dc00ff93220000dcfefffeaa5300770077020100cb0000000033ffedff00ba00
2957ff3333edffedffc488bcff7700aa00660066002222dc0000ffcbffdcffdcff8b
2958110000cb00010155005500880000002201ffffcbffcbed0000ff88884400445b
2959ba00ffbc77ff99ff006600baffba00777773ed00fe00003300330000baff77ff
2960004400aaffaafffefe000011220022c4ff8800eded99ff99ff55ff002200ffb4
2961661100110a1100ff1111dcffbabaffff88ff88010001ff33ffb98ed362000002
2962a249444154789c65d0695c0b001806f03711a9904a94d24dac63292949e5a810
2963d244588a14ca5161d1a1323973252242d62157d12ae498c8124d25ca3a11398a
296416e55a3cdffab0ffe7f77d7fcff3528645349b584c3187824d9d19d4ec2e3523
29659eb0ae975cf8de02f2486d502191841b42967a1ad49e5ddc4265f69a899e26b5
2966e9e468181baae3a71a41b95669da8df2ea3594c1b31046d7b17bfb86592e4cbe
2967d89b23e8db0af6304d756e60a8f4ad378bdc2552ae5948df1d35b52143141533
296833bbbbababebeb3b3bc9c9c9c6c6c0c0d7b7b535323225a5aa8a02024a4bedec
29690a0a2a2bcdcd7d7cf2f3a9a9c9cdcdd8b8adcdd5b5ababa828298982824a4ab2
2970b21212acadbdbc1414e2e24859b9a72730302f4f49292c4c57373c9c0a0b7372
29718c8c1c1c3a3a92936d6dfdfd293e3e26262a4a4eaea2424b4b5fbfbc9c323278
29723c0b0ba1303abaae8ecdeeed950d6669a9a7a7a141d4de9e9d5d5cdcd2229b94
2973c572716132f97cb1d8db9bc3110864a39795d9db6b6a26267a7a9a98d4d6a6a7
2974cb76090ef6f030354d4d75766e686030545464cb393a1a1ac6c68686eae8f8f9
2975a9aa4644c8b66d6e1689dcdd2512a994cb35330b0991ad9f9b6b659596a6addd
2976d8282fafae5e5323fb8f41d01f76c22fd8061be01bfc041a0323e1002c81cd30
29770b9ec027a0c930014ec035580fc3e112bc069a0b53e11c0c8095f00176c163a0
2978e5301baec06a580677600ddc05ba0f13e120bc81a770133ec355a017300d4ec2
29790c7800bbe1219c02fa08f3e13c1c85dbb00a2ec05ea0dff00a6ec15a98027360
2980070c047a06d7e1085c84f1b014f6c03fa0b33018b6c0211801ebe018fc00da0a
29816f61113c877eb01d4ec317a085700f26c130f80efbe132bc039a0733e106fc81
2982f7f017f6c10aa0d1300a0ec374780943e1382c06fa0a9b60238c83473016cec0
298302f80f73fefe1072afc1e50000000049454e44ae426082
2984"""
2985    ),
2986    "basi6a08": _dehex(
2987        """
298889504e470d0a1a0a0000000d4948445200000020000000200806000001047d4a
2989620000000467414d41000186a031e8965f0000012049444154789cc595414ec3
29903010459fa541b8bbb26641b8069b861e8b4d12c1c112c1452a710a2a65d840d5
2991949041fc481ec98ae27c7f3f8d27e3e4648047600fec0d1f390fbbe2633a31e2
29929389e4e4ea7bfdbf3d9a6b800ab89f1bd6b553cfcbb0679e960563d72e0a9293
2993b7337b9f988cc67f5f0e186d20e808042f1c97054e1309da40d02d7e27f92e03
29946cbfc64df0fc3117a6210a1b6ad1a00df21c1abcf2a01944c7101b0cb568a001
2995909c9cf9e399cf3d8d9d4660a875405d9a60d000b05e2de55e25780b7a5268e0
2996622118e2399aab063a815808462f1ab86890fc2e03e48bb109ded7d26ce4bf59
29970db91bac0050747fec5015ce80da0e5700281be533f0ce6d5900b59bcb00ea6d
2998200314cf801faab200ea752803a8d7a90c503a039f824a53f4694e7342000000
29990049454e44ae426082
3000"""
3001    ),
3002    "basn0g01": _dehex(
3003        """
300489504e470d0a1a0a0000000d49484452000000200000002001000000005b0147
3005590000000467414d41000186a031e8965f0000005b49444154789c2dccb10903
3006300c05d1ebd204b24a200b7a346f90153c82c18d0a61450751f1e08a2faaead2
3007a4846ccea9255306e753345712e211b221bf4b263d1b427325255e8bdab29e6f
30086aca30692e9d29616ee96f3065f0bf1f1087492fd02f14c90000000049454e44
3009ae426082
3010"""
3011    ),
3012    "basn0g02": _dehex(
3013        """
301489504e470d0a1a0a0000000d49484452000000200000002002000000001ca13d
3015890000000467414d41000186a031e8965f0000001f49444154789c6360085df5
30161f8cf1308850c20053868f0133091f6390b90700bd497f818b0989a900000000
301749454e44ae426082
3018"""
3019    ),
3020    # A version of basn0g04 dithered down to 3 bits.
3021    "Basn0g03": _dehex(
3022        """
302389504e470d0a1a0a0000000d494844520000002000000020040000000093e1c8
30242900000001734249540371d88211000000fd49444154789c6d90d18906210c84
3025c356f22356b2889588604301b112112b11d94a96bb495cf7fe87f32d996f2689
302644741cc658e39c0b118f883e1f63cc89dafbc04c0f619d7d898396c54b875517
302783f3a2e7ac09a2074430e7f497f00f1138a5444f82839c5206b1f51053cca968
302863258821e7f2b5438aac16fbecc052b646e709de45cf18996b29648508728612
3029952ca606a73566d44612b876845e9a347084ea4868d2907ff06be4436c4b41a3
3030a3e1774285614c5affb40dbd931a526619d9fa18e4c2be420858de1df0e69893
3031a0e3e5523461be448561001042b7d4a15309ce2c57aef2ba89d1c13794a109d7
3032b5880aa27744fc5c4aecb5e7bcef5fe528ec6293a930690000000049454e44ae
3033426082
3034"""
3035    ),
3036    "basn0g04": _dehex(
3037        """
303889504e470d0a1a0a0000000d494844520000002000000020040000000093e1c8
3039290000000467414d41000186a031e8965f0000004849444154789c6360601014
3040545232367671090d4d4b2b2f6720430095dbd1418e002a77e64c720450b9ab56
3041912380caddbd9b1c0154ee9933e408a072efde25470095fbee1d1902001f14ee
304201eaff41fa0000000049454e44ae426082
3043"""
3044    ),
3045    "basn0g08": _dehex(
3046        """
304789504e470d0a1a0a0000000d4948445200000020000000200800000000561125
3048280000000467414d41000186a031e8965f0000004149444154789c6364602400
30491408c8b30c05058c0f0829f8f71f3f6079301c1430ca11906764a2795c0c0605
30508c8ff0cafeffcff887e67131181430cae0956564040050e5fe7135e2d8590000
3051000049454e44ae426082
3052"""
3053    ),
3054    "basn0g16": _dehex(
3055        """
305689504e470d0a1a0a0000000d49484452000000200000002010000000000681f9
30576b0000000467414d41000186a031e8965f0000005e49444154789cd5d2310ac0
3058300c4351395bef7fc6dca093c0287b32d52a04a3d98f3f3880a7b857131363a0
30593a82601d089900dd82f640ca04e816dc06422640b7a03d903201ba05b7819009
3060d02d680fa44c603f6f07ec4ff41938cf7f0016d84bd85fae2b9fd70000000049
3061454e44ae426082
3062"""
3063    ),
3064    "basn2c08": _dehex(
3065        """
306689504e470d0a1a0a0000000d4948445200000020000000200802000000fc18ed
3067a30000000467414d41000186a031e8965f0000004849444154789cedd5c10900
3068300c024085ec91fdb772133b442bf4a1f8cee12bb40d043b800a14f81ca0ede4
30697d4c784081020f4a871fc284071428f0a0743823a94081bb7077a3c00182b1f9
30705e0f40cf4b0000000049454e44ae426082
3071"""
3072    ),
3073    "basn2c16": _dehex(
3074        """
307589504e470d0a1a0a0000000d4948445200000020000000201002000000ac8831
3076e00000000467414d41000186a031e8965f000000e549444154789cd596c10a83
3077301044a7e0417fcb7eb7fdadf6961e06039286266693cc7a188645e43dd6a08f
30781042003e2fe09aef6472737e183d27335fcee2f35a77b702ebce742870a23397
3079f3edf2705dd10160f3b2815fe8ecf2027974a6b0c03f74a6e4192843e75c6c03
308035e8ec3202f5e84c0181bbe8cca967a00d9df3491bb040671f2e6087ce1c2860
30818d1e05f8c7ee0f1d00b667e70df44467ef26d01fbd9bc028f42860f71d188bce
3082fb8d3630039dbd59601e7ab3c06cf428507f0634d039afdc80123a7bb1801e7a
3083b1802a7a14c89f016d74ce331bf080ce9e08f8414f04bca133bfe642fe5e07bb
3084c4ec0000000049454e44ae426082
3085"""
3086    ),
3087    "basn6a08": _dehex(
3088        """
308989504e470d0a1a0a0000000d4948445200000020000000200806000000737a7a
3090f40000000467414d41000186a031e8965f0000006f49444154789cedd6310a80
3091300c46e12764684fa1f73f55048f21c4ddc545781d52e85028fc1f4d28d98a01
3092305e7b7e9cffba33831d75054703ca06a8f90d58a0074e351e227d805c8254e3
30931bb0420f5cdc2e0079208892ffe2a00136a07b4007943c1004d900195036407f
3094011bf00052201a9c160fb84c0000000049454e44ae426082
3095"""
3096    ),
3097    "cs3n3p08": _dehex(
3098        """
309989504e470d0a1a0a0000000d494844520000002000000020080300000044a48a
3100c60000000467414d41000186a031e8965f0000000373424954030303a392a042
310100000054504c544592ff0000ff9200ffff00ff0000dbff00ff6dffb600006dff
3102b6ff00ff9200dbff000049ffff2400ff000024ff0049ff0000ffdb00ff4900ff
3103b6ffff0000ff2400b6ffffdb000092ffff6d000024ffff49006dff00df702b17
31040000004b49444154789c85cac70182000000b1b3625754b0edbfa72324ef7486
3105184ed0177a437b680bcdd0031c0ed00ea21f74852ed00a1c9ed0086da0057487
31066ed0121cd6d004bda0013a421ff803224033e177f4ae260000000049454e44ae
3107426082
3108"""
3109    ),
3110    "s09n3p02": _dehex(
3111        """
311289504e470d0a1a0a0000000d49484452000000090000000902030000009dffee
3113830000000467414d41000186a031e8965f000000037342495404040477f8b5a3
31140000000c504c544500ff000077ffff00ffff7700ff5600640000001f49444154
3115789c63600002fbff0c0c56ab19182ca381581a4283f82071200000696505c36a
3116437f230000000049454e44ae426082
3117"""
3118    ),
3119    "tbgn3p08": _dehex(
3120        """
312189504e470d0a1a0a0000000d494844520000002000000020080300000044a48a
3122c60000000467414d41000186a031e8965f00000207504c54457f7f7fafafafab
3123abab110000222200737300999999510d00444400959500959595e6e600919191
31248d8d8d620d00898989666600b7b700911600000000730d007373736f6f6faaaa
3125006b6b6b676767c41a00cccc0000f30000ef00d51e0055555567670000dd0051
3126515100d1004d4d4de61e0038380000b700160d0d00ab00560d00090900009500
3127009100008d003333332f2f2f2f2b2f2b2b000077007c7c001a05002b27000073
3128002b2b2b006f00bb1600272727780d002323230055004d4d00cc1e00004d00cc
31291a000d00003c09006f6f00002f003811271111110d0d0d55554d090909001100
31304d0900050505000d00e2e200000900000500626200a6a6a6a2a2a29e9e9e8484
313100fb00fbd5d500801100800d00ea00ea555500a6a600e600e6f7f700e200e233
31320500888888d900d9848484c01a007777003c3c05c8c8008080804409007c7c7c
3133bb00bbaa00aaa600a61e09056262629e009e9a009af322005e5e5e05050000ee
3134005a5a5adddd00a616008d008d00e20016050027270088110078780000c40078
313500787300736f006f44444400aa00c81e004040406600663c3c3c090000550055
31361a1a00343434d91e000084004d004d007c004500453c3c00ea1e00222222113c
3137113300331e1e1efb22001a1a1a004400afaf00270027003c001616161e001e0d
3138160d2f2f00808000001e00d1d1001100110d000db7b7b7090009050005b3b3b3
31396d34c4230000000174524e530040e6d86600000001624b474402660b7c640000
314001f249444154789c6360c0048c8c58049100575f215ee92e6161ef109cd2a15e
31414b9645ce5d2c8f433aa4c24f3cbd4c98833b2314ab74a186f094b9c2c27571d2
31426a2a58e4253c5cda8559057a392363854db4d9d0641973660b0b0bb76bb16656
314306970997256877a07a95c75a1804b2fbcd128c80b482a0b0300f8a824276a9a8
3144ec6e61612b3e57ee06fbf0009619d5fac846ac5c60ed20e754921625a2daadc6
31451967e29e97d2239c8aec7e61fdeca9cecebef54eb36c848517164514af16169e
3146866444b2b0b7b55534c815cc2ec22d89cd1353800a8473100a4485852d924a6a
3147412adc74e7ad1016ceed043267238c901716f633a812022998a4072267c4af02
314892127005c0f811b62830054935ce017b38bf0948cc5c09955f030a24617d9d46
314963371fd940b0827931cbfdf4956076ac018b592f72d45594a9b1f307f3261b1a
3150084bc2ad50018b1900719ba6ba4ca325d0427d3f6161449486f981144cf3100e
31512a5f2a1ce8683e4ddf1b64275240c8438d98af0c729bbe07982b8a1c94201dc2
3152b3174c9820bcc06201585ad81b25b64a2146384e3798290c05ad280a18c0a62e
3153e898260c07fca80a24c076cc864b777131a00190cdfa3069035eccbc038c30e1
31543e88b46d16b6acc5380d6ac202511c392f4b789aa7b0b08718765990111606c2
31559e854c38e5191878fbe471e749b0112bb18902008dc473b2b2e8e72700000000
315649454e44ae426082
3157"""
3158    ),
3159    "Tp2n3p08": _dehex(
3160        """
316189504e470d0a1a0a0000000d494844520000002000000020080300000044a48a
3162c60000000467414d41000186a031e8965f00000300504c544502ffff80ff05ff
31637f0703ff7f0180ff04ff00ffff06ff000880ff05ff7f07ffff06ff000804ff00
31640180ff02ffff03ff7f02ffff80ff0503ff7f0180ffff0008ff7f0704ff00ffff
316506ff000802ffffff7f0704ff0003ff7fffff0680ff050180ff04ff000180ffff
31660008ffff0603ff7f80ff05ff7f0702ffffff000880ff05ffff0603ff7f02ffff
3167ff7f070180ff04ff00ffff06ff000880ff050180ffff7f0702ffff04ff0003ff
31687fff7f0704ff0003ff7f0180ffffff06ff000880ff0502ffffffff0603ff7fff
31697f0702ffff04ff000180ff80ff05ff0008ff7f07ffff0680ff0504ff00ff0008
31700180ff03ff7f02ffff02ffffffff0604ff0003ff7f0180ffff000880ff05ff7f
31710780ff05ff00080180ff02ffffff7f0703ff7fffff0604ff00ff7f07ff0008ff
3172ff0680ff0504ff0002ffff0180ff03ff7fff0008ffff0680ff0504ff000180ff
317302ffff03ff7fff7f070180ff02ffff04ff00ffff06ff0008ff7f0780ff0503ff
31747fffff06ff0008ff7f0780ff0502ffff03ff7f0180ff04ff0002ffffff7f07ff
3175ff0604ff0003ff7fff00080180ff80ff05ffff0603ff7f0180ffff000804ff00
317680ff0502ffffff7f0780ff05ffff0604ff000180ffff000802ffffff7f0703ff
31777fff0008ff7f070180ff03ff7f02ffff80ff05ffff0604ff00ff0008ffff0602
3178ffff0180ff04ff0003ff7f80ff05ff7f070180ff04ff00ff7f0780ff0502ffff
3179ff000803ff7fffff0602ffffff7f07ffff0680ff05ff000804ff0003ff7f0180
3180ff02ffff0180ffff7f0703ff7fff000804ff0080ff05ffff0602ffff04ff00ff
3181ff0603ff7fff7f070180ff80ff05ff000803ff7f0180ffff7f0702ffffff0008
318204ff00ffff0680ff0503ff7f0180ff04ff0080ff05ffff06ff000802ffffff7f
31830780ff05ff0008ff7f070180ff03ff7f04ff0002ffffffff0604ff00ff7f07ff
3184000880ff05ffff060180ff02ffff03ff7f80ff05ffff0602ffff0180ff03ff7f
318504ff00ff7f07ff00080180ffff000880ff0502ffff04ff00ff7f0703ff7fffff
318606ff0008ffff0604ff00ff7f0780ff0502ffff03ff7f0180ffdeb83387000000
3187f874524e53000000000000000008080808080808081010101010101010181818
31881818181818202020202020202029292929292929293131313131313131393939
3189393939393941414141414141414a4a4a4a4a4a4a4a52525252525252525a5a5a
31905a5a5a5a5a62626262626262626a6a6a6a6a6a6a6a73737373737373737b7b7b
31917b7b7b7b7b83838383838383838b8b8b8b8b8b8b8b94949494949494949c9c9c
31929c9c9c9c9ca4a4a4a4a4a4a4a4acacacacacacacacb4b4b4b4b4b4b4b4bdbdbd
3193bdbdbdbdbdc5c5c5c5c5c5c5c5cdcdcdcdcdcdcdcdd5d5d5d5d5d5d5d5dedede
3194dededededee6e6e6e6e6e6e6e6eeeeeeeeeeeeeeeef6f6f6f6f6f6f6f6b98ac5
3195ca0000012c49444154789c6360e7169150d230b475f7098d4ccc28a96ced9e32
319663c1da2d7b8e9fb97af3d1fb8f3f18e8a0808953544a4dd7c4c2c9233c2621bf
3197b4aab17fdacce5ab36ee3a72eafaad87efbefea68702362e7159652d031b07cf
3198c0b8a4cce28aa68e89f316aedfb4ffd0b92bf79fbcfcfe931e0a183904e55435
31998decdcbcc22292b3caaadb7b27cc5db67af3be63e72fdf78fce2d31f7a2860e5
3200119356d037b374f10e8a4fc92eaa6fee99347fc9caad7b0f9ebd74f7c1db2fbf
3201e8a180995f484645dbdccad12f38363dafbcb6a573faeca5ebb6ed3e7ce2c29d
3202e76fbefda38702063e0149751d537b67ff80e8d4dcc29a86bea97316add9b0e3
3203c0e96bf79ebdfafc971e0a587885e515f58cad5d7d43a2d2720aeadaba26cf5a
3204bc62fbcea3272fde7efafac37f3a28000087c0fe101bc2f85f0000000049454e
320544ae426082
3206"""
3207    ),
3208    "tbbn1g04": _dehex(
3209        """
321089504e470d0a1a0a0000000d494844520000002000000020040000000093e1c8
3211290000000467414d41000186a031e8965f0000000274524e530007e8f7589b00
3212000002624b47440000aa8d23320000013e49444154789c55d1cd4b024118c7f1
3213efbe6419045b6a48a72d352808b435284f9187ae9b098627a1573a19945beba5
3214e8129e8222af11d81e3a4545742de8ef6af6d5762e0fbf0fc33c33f36085cb76
3215bc4204778771b867260683ee57e13f0c922df5c719c2b3b6c6c25b2382cea4b9
32169f7d4f244370746ac71f4ca88e0f173a6496749af47de8e44ba8f3bf9bdfa98a
32170faf857a7dd95c7dc8d7c67c782c99727997f41eb2e3c1e554152465bb00fe8e
3218b692d190b718d159f4c0a45c4435915a243c58a7a4312a7a57913f05747594c6
321946169866c57101e4d4ce4d511423119c419183a3530cc63db88559ae28e7342a
32201e9c8122b71139b8872d6e913153224bc1f35b60e4445bd4004e20ed6682c759
32211d9873b3da0fbf50137dc5c9bde84fdb2ec8bde1189e0448b63584735993c209
32227a601bd2710caceba6158797285b7f2084a2f82c57c01a0000000049454e44ae
3223426082
3224"""
3225    ),
3226    "tbrn2c08": _dehex(
3227        """
322889504e470d0a1a0a0000000d4948445200000020000000200802000000fc18ed
3229a30000000467414d41000186a031e8965f0000000674524e53007f007f007f8a
323033334f00000006624b474400ff0000000033277cf3000004d649444154789cad
3231965f68537714c73fd912d640235e692f34d0406fa0c1663481045ab060065514
323256660a295831607df0a1488715167060840a1614e6431e9cb34fd2c00a762c85
3233f6a10f816650c13b0cf40612e1822ddc4863bd628a8924d23d6464f9d3665dd9
3234f7e977ce3dbff3cd3939bfdfef6bb87dfb364782dbed065ebe7cd93acc78b4ec
3235a228debd7bb7bfbfbfbbbbfb7f261045311a8d261209405194274f9ea4d3e916
3236f15f1c3eb5dd6e4fa5fecce526239184a2b0b8486f6f617171b1f5ae4311381c
32378e57af5e5dbd7a351088150a78bd389d44222c2f93cdfe66b7db8f4ee07038b6
3238b6b6bebf766d7e7e7e60a06432313b4ba984c3c1c4049a46b95c5a58583822c1
3239dbb76f27272733d1b9df853c3030c0f232562b9108cf9eb1b888d7cbf030abab
324031abd5fa1f08dc6ef7e7cf9f1f3f7e1c8944745d4f1400c62c001313acad21cb
3241b8dd2c2c603271eb1640341aad4c6d331aa7e8c48913a150a861307ecc11e964
324274899919bc5e14e56fffc404f1388502f178dceff7ef4bf0a5cfe7abb533998c
3243e5f9ea2f1dd88c180d64cb94412df3dd57e83a6b3b3c7a84c98420100c72fd3a
3244636348bae726379fe69e8e8d8dbd79f3a6558b0607079796965256479b918085
32457b02db12712b6181950233023f3f647494ee6e2e5ea45864cce5b8a7fe3acffc
32463aebb22c2bd5d20e22d0757d7b7bbbbdbd3d94a313bed1b0aa3cd069838b163a
32478d4c59585f677292d0b84d9a995bd337def3fe6bbe5e6001989b9b6bfe27ea08
324836373781542ab56573248b4c5bc843ac4048c7ab21aa24ca00534c25482828a3
32498c9ee67475bbaaaab22cb722c8e57240a150301a8d219de94e44534d7d90e885
325087acb0e2c4f9800731629b6c5ee14a35a6b9887d2a0032994cb9cf15dbe59650
3251ff7b46a04c9a749e7cc5112214266cc65c31354d5b5d5d3d90209bcd5616a552
3252a95c2e87f2a659bd9ee01c2cd73964e438f129a6aa9e582c363838b80f81d7eb
32535555b56a2a8ad2d9d7affd0409f8015c208013fea00177b873831b0282c964f2
3254783c1e8fa7582cee5f81a669b5e6eeeeaee58e8559b0c233d8843c7c0b963a82
325534e94b5cb2396d7d7d7db22c8ba258fb0afd43f0e2c58b919191ba9de9b4d425
3256118329b0c3323c8709d02041b52b4ea7f39de75d2a934a2693c0a953a76a93d4
32575d157ebf7f6565a5542a553df97c5e10045dd731c130b86113cc300cbd489224
325808422a952a140a95788fc763b1d41558d7a2d7af5f5fb870a1d6a3aaaacd6603
325918802da84c59015bd2e6897b745d9765b99a1df0f97c0daf74e36deaf7fbcd66
326073ad2797cb89a2c839880188a2e8743a8bc5a22ccbba5e376466b3b9bdbdbd21
32616123413a9d0e0402b51e4dd3bababa788eb022b85caeb6b6364551b6b7b76942
326243f7f727007a7a7a04a1ee8065b3595fde2768423299ac1ec6669c3973e65004
3263c0f8f878ad69341a33994ced2969c0d0d0502412f9f8f163f3a7fd654b474787
3264288ad53e74757535df6215b85cae60302849d2410aecc037f9f2e5cbd5b5c160
3265680eb0dbede170381c0e7ff8f0a185be3b906068684892a4ca7a6f6faff69328
32668ad3d3d3f7efdfdfdbdbfb57e96868a14d0d0643381c96242997cbe5f3794010
326784603078fcf8f1d6496bd14a3aba5c2ea7d369341a5555b5582c8140e0fcf9f3
32681b1b1b87cf4eeb0a8063c78e45a3d19e9e1ebfdfdf5a831e844655d18093274f
32699e3d7bf6d3a74f3b3b3b47c80efc05ff7af28fefb70d9b0000000049454e44ae
3270426082
3271"""
3272    ),
3273    "basn6a16": _dehex(
3274        """
327589504e470d0a1a0a0000000d494844520000002000000020100600000023eaa6
3276b70000000467414d41000186a031e8965f00000d2249444154789cdd995f6c1c
3277d775c67ff38fb34b724d2ee55a8e4b04a0ac87049100cab4dbd8c6528902cb4d
327810881620592e52d4325ac0905bc98a94025e71fd622cb5065ac98a0c283050c0
3279728a00b6e542a1d126885cd3298928891d9a0444037e904434951d4b90b84b2f
3280c9dde1fcebc33977a95555348f411e16dfce9d3b77ee77eebde77ce78c95a669
32810ad07c17009a13edd898b87dfb1fcb7d2b4d1bff217f33df80deb1e6267df0ff
3282c1e6e6dfafdf1f5a7fd30f9aef66b6d546dd355bf02c40662e3307f9725a96c6
3283744c3031f83782f171c148dbc3bf1774f5dad1e79d6f095a3f54d4fbec5234ef
3284d9a2f8d73afe4f14f57ef4f42def7b44f19060f06b45bddf1c5534d77fd922be
32852973a15a82e648661c6e3240aa3612ead952b604bde57458894f29deaf133bac
328613d2766f5227a4a3b8cf08da7adfd6fbd6bd8a4fe9dbb43d35e3dfa3f844fbf8
32879119bf4f7144094fb56333abf8a86063ca106f94b3a3b512343765e60082097f
32881bb86ba72439a653519b09f5cee1ce61c897d37eedf5553580ae60f4af8af33a
3289b14fd400b6a0f34535c0434afc0b3a9f07147527a5fa7ca218ff56c74d74dc3f
3290155cfd3325fc278acf2ae1cb4a539f5f9937c457263b0bd51234c732a300cdd1
3291cc1840f0aaff54db0e4874ed5a9b5d6d27d4bb36746d80de72baa877ff4b275a
3292d7895ed1897ea4139b5143fcbb1a62560da1ed9662aaed895ec78a91c18795b8
32935e07ab4af8ba128e95e682e0728bf8f2e5ae815a091a53d902ac1920d8e05f06
3294589de8d8d66680789f4e454fb9d9ec66cd857af796ee2d902fa73fd5bba775a2
3295153580ae44705ed0d37647d15697cb8f14bfa3e3e8fdf8031d47af571503357c
3296f30d25acedcbbf135c9a35c49766ba07ab255859e8ec03684e66860182dff8f7
32970304bff6ff1c20fc81b7afdd00a71475539a536e36bb5973a19e3b923b02bde5
3298e4efd4003ac170eb2d13fe274157afedbd82d6fb3a9a1e85e4551d47cf7078f8
32999671fe4289ebf5f2bf08d63f37c4eb4773c55a0996efeefa0ca011671d8060ca
33002f0004c7fcc300e166ef0240f825efe3361f106d57d423d0723f7acacd66376b
33012ed47b7a7a7a205f4ef4ac4691e0aad9aa0d41cf13741c3580a506487574ddca
330261a8c403c1863ebfbcac3475168b2de28b8b3d77544bb05ce92a02aceced3c0d
3303d0cc65ea371b201cf1c601c24dde1c4078cedbdeb60322f50126a019bf6edc9b
330439e566b39b3517eaf97c3e0fbde5e4491d45bd74537145d155b476aa0176e868
3305c6abebf30dbd5e525c54ac8e18e2d56abeb756827a3d970358a97416019a6f64
3306f60004fdfe1580d5c98e618070cc1b05887eee7e0d209a70db7d8063029889b4
3307c620ead78d7b33a7dc6c76b3e6427ddddbebde867c393aa7845e5403e8ca794a
3308d0d6fb897af5f03525fe5782f5e7046bdaef468bf88d1debc6ab25583cd17310
33096079b9ab0ba059c914018245bf076075b5a303200c3c1f209a733701444fbbaf
331000c4134ebb016c5d0b23614c243701cdf875e3decce9349bddacb9505fbf7dfd
331176e82d87736a00f5d2b5ffd4b7dce2719a4d25ae717ee153c1abef18e257cfad
33127fa45682da48ef38c052b53b0fd06864b300c151ff08c0ea431de701a287dd5f
3313004497dc7b01a253ee3e80b8c7f91c20f967fb6fdb7c80ada7d8683723614c24
33143701cdf875e3decc29379bddacb950ef3fd47f08f2e5a61ea4aa2a3eb757cd55
331513345efcfa59c12b2f19e2578ef77fb75a82854ffbee01a83f977b11a031931d
3316040802df07082b5e11207cc17b1e209a770700e2df0a83e409fb7580f827c230
331799b06fd901fb058d6835dacd481813c94d40337eddb83773cacd66376b2ed437
3318bebcf165e82d2f4e4beb7f3fa6e652c2d7ee10bc78c010bfb87fe3c95a09ae9f
3319bd732740bd2fb700d0f865f64180e059ff044018ca0ca28a5b04883f701e0088
3320bfec7c0c909cb71f0448c6ec518074b375012079d9dedf66004bcfbc51eb2dd1
3321aadacd481813c94d40337eddb83773cacd66376b2ed487868686205fbe7c49ef
33225605a73f34c4a7a787eeab96e0da81bb4e022c15ba27019a5b339300e16bf286
3323a8eae601e25866907cdf3e0890acb36f00245fb57f05904e59c300e92561946e
3324b2e600d209ab7d07f04d458dfb46ad1bd16ab49b913026929b8066fcba716fe6
3325949bcd6ed65ca8ef7e7cf7e3d05b7e7c8f217ee6cdddbb6a25a856f37980e0c7
3326fe4e80a82623c48193014846ec7180f4acf518409aca0cd28a5504e03b32c374
3327de1a00608a0240faaa327a4b19fe946fb6f90054dbb5f2333d022db56eb4966a
33283723614c243701cdf8f556bea8a7dc6c76b3e66bd46584ddbbcebc0990cf4b0f
3329ff4070520c282338a7e26700ec725202b01e4bcf0258963c6f1d4d8f0030cb20
3330805549c520930c03584fa522b676f11600ffc03fde3e1b3489a9c9054c9aa23b
3331c08856a3dd8c843191dc0434e3d78d7b33a75c36fb993761f7ae5a69f72ef97f
3332e6ad336fed7e1c60e8bee96980bbdebbb60da07b7069062033d9dc0ae03d296f
333370ab511ec071640676252902d833c916007b3e1900b0a6d2028035968e025861
3334ea01581369fb11488c34d18cbc95989afccca42baad65ba2d5683723614c24d7
33358066fcbab8b7e96918baaf5aaa56219f975fb50a43f7c9bde90fa73f1c1a02d8
333678f2e27e803b77ca08b90519315b6fe400fc1392097a9eccc0ad444500e70199
3337a1331f0f00d8934901c07e5d526ceb87c2d07e2579badd005a2b31a5089391b7
33381253358049535a6add8856dd0146c298482e01ede27ed878b256ba7600ee3a09
3339c18fc1df09fe01084ec25defc1b56db0f1a4f4bd78e0e2818d2f0334e7330300
33407df7c888b917e50dd9c1c60c80efcb0cbc63e1f700bce7c31700dccbd1060027
33418add9b0de06c8e2f00d84962b7d7030e2a61538331b98051f92631bd253f336a
3342dd8856a3dd44c25c390efddfad96ae9f853b77c25201ba27c533b8bdf28b6ad0
33433d084b33d2e7fa59099e9901b8f2d29597fa0f01848f78e70082117f1ca07b76
33446910209b9519f895a008d031bbba05c09d8f06005c5b18b8fba25300cea6780e
3345c03e911c6ccf06d507b48a4fa606634a114609de929f9934c5a87511ad57cfc1
3346fa476aa5854fa1ef1e3910b905686e85cc24c40138198915f133d2d6dc2a7dea
33477df2ccc2a752faf2cec1d577aebeb37e3b4034eeee0008dff3be0e6b923773b4
33487904c0ef9119767cb4fa1500ef1361e08e452500f71561e84cc4ed3e20fab6a2
3349c905f40cb76a3026bf3319b91ac2e46792a6dcd801ebc6aba5da08f48ecb81c8
3350bd088d5f42f6417191de93908c803d0e76199292b485af41b60e8d9c3c537f0e
33518211f0c7211a077707dc18b931b2ee6d80a4d7ae024491ebc24d4a708ff70680
33527f25e807e8785f1878e322d6ddaf453f0770ff2dfa769b01423dbbad72a391b6
33535a7c3235985629423372494cab55c8f7d64a8b27a0e7202c55a13b0f8d19c80e
33544ae9ca3f015115dc3ca467c17a4c7ee95970ab10e5a54ff0ac3cd39881ee5958
33551a84f03df0be0e492fd855a8d6aa35d10b4962dbb0a604a3d3ee5e80a8eee600
3356a24977f8660378bf0bbf00e01d0a8fb7f980f04b8aa6ce6aca8d5a7533c52753
3357839152c4e222f4dc512dd5eb90cbc981e8ea12cf90cd8a8bf47d89159e2741d3
33587124f65b96fcd254dae258fa84a13c13043246a32129574787e49eae2b49b86d
3359c3e2e78b9ff7f4002415bb08907c66df0d103b4e0c104db90500ff70700c203a
3360ee1e82dba4c3e16e256c0acca6ceaae9afd1f612d7eb472157ac95962bd05594
33617dd1598466053245088e827f44628657942a825b84e4fb601f84b4025611aca3
3362901e01bb024911dc0a4445f08e41f83df02b10142173149ab71baf027611ea95
33637a257704201d14cd9af4d90b00f194530088cb4e09c0df1c5c0088f7393f6833
3364c0aa3ac156655de3bca9b34ab9716906ba07aba5e5bba1eb3358d90b9da7c533
336564f6888bf47b60f521e8380fe10be03d2feac17900927560df40f4e48f805960
336650328d648bf4893f9067c217a0631656b7c898c122847bc07b03a2d3e0ee85e4
336733b0ef867450c4fad2ecd26cf7168074c0ba0c904cdac300c9cfec4701924df6
33681cdca61e10685c6f7d52d0caba1498972f43d740adb4b2009d7d7220b20e3473
336990a943d00ffe959bb6eac3e0fe42ea49ee00c45f06e76329b1dabf127d690d80
33705581b408f63c2403e0cc433c00ee658836803b0fd100747c04ab5f917704fd10
3371d5c1cd41ec801343d207f602a403605d86e5f9e5f9ae0d00e994556833806685
3372c931fb709b0f08b4e869bea5c827859549e82c544b8d29c816a0390999613920
33737e610d5727a16318c2003c1fa24be0de2b32caf92224e7c17e5004b6350c4c01
337405601218066b0ad28224e149019c086257ca315102de2712903bde97b8144d82
33753b2c6ac52d403c054e019249b087f53d0558995a99ea946c70cc927458b3c1ff
3376550f30050df988d4284376b4566a8e416654cc921985e037e0df0fc131f00f4b
3377acf0c6211c036f14a239703741740adc7da227edd7e56b833d0ae92549b4d357
337825dfb49ed2ff63908e6adf27d6d0dda7638d4154d2778daca17f58e61297c129
337941f233b01f5dc3740cac51688c35c6b22580f48224fee9b83502569a66b629f1
338009f3713473413e2666e7fe6f6c6efefdfafda1f56f6e06f93496d9d67cb7366a
33819964b6f92e64b689196ec6c604646fd3fe4771ff1bf03f65d8ecc3addbb5f300
338200000049454e44ae426082
3383"""
3384    ),
3385}
3386
3387
3388def test_suite(options, args):
3389    """
3390    Create a PNG test image and write the file to stdout.
3391    """
3392
3393    # Below is a big stack of test image generators.
3394    # They're all really tiny, so PEP 8 rules are suspended.
3395
3396    def test_gradient_horizontal_lr(x, y):
3397        return x
3398
3399    def test_gradient_horizontal_rl(x, y):
3400        return 1 - x
3401
3402    def test_gradient_vertical_tb(x, y):
3403        return y
3404
3405    def test_gradient_vertical_bt(x, y):
3406        return 1 - y
3407
3408    def test_radial_tl(x, y):
3409        return max(1 - math.sqrt(x * x + y * y), 0.0)
3410
3411    def test_radial_center(x, y):
3412        return test_radial_tl(x - 0.5, y - 0.5)
3413
3414    def test_radial_tr(x, y):
3415        return test_radial_tl(1 - x, y)
3416
3417    def test_radial_bl(x, y):
3418        return test_radial_tl(x, 1 - y)
3419
3420    def test_radial_br(x, y):
3421        return test_radial_tl(1 - x, 1 - y)
3422
3423    def test_stripe(x, n):
3424        return float(int(x * n) & 1)
3425
3426    def test_stripe_h_2(x, y):
3427        return test_stripe(x, 2)
3428
3429    def test_stripe_h_4(x, y):
3430        return test_stripe(x, 4)
3431
3432    def test_stripe_h_10(x, y):
3433        return test_stripe(x, 10)
3434
3435    def test_stripe_v_2(x, y):
3436        return test_stripe(y, 2)
3437
3438    def test_stripe_v_4(x, y):
3439        return test_stripe(y, 4)
3440
3441    def test_stripe_v_10(x, y):
3442        return test_stripe(y, 10)
3443
3444    def test_stripe_lr_10(x, y):
3445        return test_stripe(x + y, 10)
3446
3447    def test_stripe_rl_10(x, y):
3448        return test_stripe(1 + x - y, 10)
3449
3450    def test_checker(x, y, n):
3451        return float((int(x * n) & 1) ^ (int(y * n) & 1))
3452
3453    def test_checker_8(x, y):
3454        return test_checker(x, y, 8)
3455
3456    def test_checker_15(x, y):
3457        return test_checker(x, y, 15)
3458
3459    def test_zero(x, y):
3460        return 0
3461
3462    def test_one(x, y):
3463        return 1
3464
3465    test_patterns = {
3466        "GLR": test_gradient_horizontal_lr,
3467        "GRL": test_gradient_horizontal_rl,
3468        "GTB": test_gradient_vertical_tb,
3469        "GBT": test_gradient_vertical_bt,
3470        "RTL": test_radial_tl,
3471        "RTR": test_radial_tr,
3472        "RBL": test_radial_bl,
3473        "RBR": test_radial_br,
3474        "RCTR": test_radial_center,
3475        "HS2": test_stripe_h_2,
3476        "HS4": test_stripe_h_4,
3477        "HS10": test_stripe_h_10,
3478        "VS2": test_stripe_v_2,
3479        "VS4": test_stripe_v_4,
3480        "VS10": test_stripe_v_10,
3481        "LRS": test_stripe_lr_10,
3482        "RLS": test_stripe_rl_10,
3483        "CK8": test_checker_8,
3484        "CK15": test_checker_15,
3485        "ZERO": test_zero,
3486        "ONE": test_one,
3487    }
3488
3489    def test_pattern(width, height, bitdepth, pattern):
3490        """Create a single plane (monochrome) test pattern.  Returns a
3491        flat row flat pixel array.
3492        """
3493
3494        maxval = 2 ** bitdepth - 1
3495        if maxval > 255:
3496            a = array("H")
3497        else:
3498            a = array("B")
3499        fw = float(width)
3500        fh = float(height)
3501        pfun = test_patterns[pattern]
3502        for y in range(height):
3503            fy = float(y) / fh
3504            for x in range(width):
3505                a.append(int(round(pfun(float(x) / fw, fy) * maxval)))
3506        return a
3507
3508    def test_rgba(size=256, bitdepth=8, red="GTB", green="GLR", blue="RTL", alpha=None):
3509        """
3510        Create a test image.  Each channel is generated from the
3511        specified pattern; any channel apart from red can be set to
3512        None, which will cause it not to be in the image.  It
3513        is possible to create all PNG channel types (L, RGB, LA, RGBA),
3514        as well as non PNG channel types (RGA, and so on).
3515        """
3516
3517        i = test_pattern(size, size, bitdepth, red)
3518        psize = 1
3519        for channel in (green, blue, alpha):
3520            if channel:
3521                c = test_pattern(size, size, bitdepth, channel)
3522                i = interleave_planes(i, c, psize, 1)
3523                psize += 1
3524        return i
3525
3526    def pngsuite_image(name):
3527        """
3528        Create a test image by reading an internal copy of the files
3529        from the PngSuite.  Returned in flat row flat pixel format.
3530        """
3531
3532        if name not in _pngsuite:
3533            raise NotImplementedError(
3534                "cannot find PngSuite file %s (use -L for a list)" % name
3535            )
3536        r = Reader(bytes=_pngsuite[name])
3537        w, h, pixels, meta = r.asDirect()
3538        assert w == h
3539        # LAn for n < 8 is a special case for which we need to rescale
3540        # the data.
3541        if meta["greyscale"] and meta["alpha"] and meta["bitdepth"] < 8:
3542            factor = 255 // (2 ** meta["bitdepth"] - 1)
3543
3544            def rescale(data):
3545                for row in data:
3546                    yield map(factor.__mul__, row)
3547
3548            pixels = rescale(pixels)
3549            meta["bitdepth"] = 8
3550        arraycode = "BH"[meta["bitdepth"] > 8]
3551        return w, array(arraycode, itertools.chain(*pixels)), meta
3552
3553    # The body of test_suite()
3554    size = 256
3555    if options.test_size:
3556        size = options.test_size
3557    options.bitdepth = options.test_depth
3558    options.greyscale = bool(options.test_black)
3559
3560    kwargs = {}
3561    if options.test_red:
3562        kwargs["red"] = options.test_red
3563    if options.test_green:
3564        kwargs["green"] = options.test_green
3565    if options.test_blue:
3566        kwargs["blue"] = options.test_blue
3567    if options.test_alpha:
3568        kwargs["alpha"] = options.test_alpha
3569    if options.greyscale:
3570        if options.test_red or options.test_green or options.test_blue:
3571            raise ValueError(
3572                "cannot specify colours (R, G, B) when greyscale image (black channel, K) is specified"
3573            )
3574        kwargs["red"] = options.test_black
3575        kwargs["green"] = None
3576        kwargs["blue"] = None
3577    options.alpha = bool(options.test_alpha)
3578    if not args:
3579        pixels = test_rgba(size, options.bitdepth, **kwargs)
3580    else:
3581        size, pixels, meta = pngsuite_image(args[0])
3582        for k in ["bitdepth", "alpha", "greyscale"]:
3583            setattr(options, k, meta[k])
3584
3585    writer = Writer(
3586        size,
3587        size,
3588        bitdepth=options.bitdepth,
3589        transparent=options.transparent,
3590        background=options.background,
3591        gamma=options.gamma,
3592        greyscale=options.greyscale,
3593        alpha=options.alpha,
3594        compression=options.compression,
3595        interlace=options.interlace,
3596    )
3597    writer.write_array(sys.stdout, pixels)
3598
3599
3600def read_pam_header(infile):
3601    """
3602    Read (the rest of a) PAM header.  `infile` should be positioned
3603    immediately after the initial 'P7' line (at the beginning of the
3604    second line).  Returns are as for `read_pnm_header`.
3605    """
3606
3607    # Unlike PBM, PGM, and PPM, we can read the header a line at a time.
3608    header = dict()
3609    while True:
3610        l = infile.readline().strip()
3611        if l == strtobytes("ENDHDR"):
3612            break
3613        if not l:
3614            raise EOFError("PAM ended prematurely")
3615        if l[0] == strtobytes("#"):
3616            continue
3617        l = l.split(None, 1)
3618        if l[0] not in header:
3619            header[l[0]] = l[1]
3620        else:
3621            header[l[0]] += strtobytes(" ") + l[1]
3622
3623    required = ["WIDTH", "HEIGHT", "DEPTH", "MAXVAL"]
3624    required = [strtobytes(x) for x in required]
3625    WIDTH, HEIGHT, DEPTH, MAXVAL = required
3626    present = [x for x in required if x in header]
3627    if len(present) != len(required):
3628        raise Error("PAM file must specify WIDTH, HEIGHT, DEPTH, and MAXVAL")
3629    width = int(header[WIDTH])
3630    height = int(header[HEIGHT])
3631    depth = int(header[DEPTH])
3632    maxval = int(header[MAXVAL])
3633    if width <= 0 or height <= 0 or depth <= 0 or maxval <= 0:
3634        raise Error("WIDTH, HEIGHT, DEPTH, MAXVAL must all be positive integers")
3635    return "P7", width, height, depth, maxval
3636
3637
3638def read_pnm_header(infile, supported=("P5", "P6")):
3639    """
3640    Read a PNM header, returning (format,width,height,depth,maxval).
3641    `width` and `height` are in pixels.  `depth` is the number of
3642    channels in the image; for PBM and PGM it is synthesized as 1, for
3643    PPM as 3; for PAM images it is read from the header.  `maxval` is
3644    synthesized (as 1) for PBM images.
3645    """
3646
3647    # Generally, see http://netpbm.sourceforge.net/doc/ppm.html
3648    # and http://netpbm.sourceforge.net/doc/pam.html
3649
3650    supported = [strtobytes(x) for x in supported]
3651
3652    # Technically 'P7' must be followed by a newline, so by using
3653    # rstrip() we are being liberal in what we accept.  I think this
3654    # is acceptable.
3655    type = infile.read(3).rstrip()
3656    if type not in supported:
3657        raise NotImplementedError("file format %s not supported" % type)
3658    if type == strtobytes("P7"):
3659        # PAM header parsing is completely different.
3660        return read_pam_header(infile)
3661    # Expected number of tokens in header (3 for P4, 4 for P6)
3662    expected = 4
3663    pbm = ("P1", "P4")
3664    if type in pbm:
3665        expected = 3
3666    header = [type]
3667
3668    # We have to read the rest of the header byte by byte because the
3669    # final whitespace character (immediately following the MAXVAL in
3670    # the case of P6) may not be a newline.  Of course all PNM files in
3671    # the wild use a newline at this point, so it's tempting to use
3672    # readline; but it would be wrong.
3673    def getc():
3674        c = infile.read(1)
3675        if not c:
3676            raise Error("premature EOF reading PNM header")
3677        return c
3678
3679    c = getc()
3680    while True:
3681        # Skip whitespace that precedes a token.
3682        while c.isspace():
3683            c = getc()
3684        # Skip comments.
3685        while c == "#":
3686            while c not in "\n\r":
3687                c = getc()
3688        if not c.isdigit():
3689            raise Error("unexpected character %s found in header" % c)
3690        # According to the specification it is legal to have comments
3691        # that appear in the middle of a token.
3692        # This is bonkers; I've never seen it; and it's a bit awkward to
3693        # code good lexers in Python (no goto).  So we break on such
3694        # cases.
3695        token = strtobytes("")
3696        while c.isdigit():
3697            token += c
3698            c = getc()
3699        # Slight hack.  All "tokens" are decimal integers, so convert
3700        # them here.
3701        header.append(int(token))
3702        if len(header) == expected:
3703            break
3704    # Skip comments (again)
3705    while c == "#":
3706        while c not in "\n\r":
3707            c = getc()
3708    if not c.isspace():
3709        raise Error("expected header to end with whitespace, not %s" % c)
3710
3711    if type in pbm:
3712        # synthesize a MAXVAL
3713        header.append(1)
3714    depth = (1, 3)[type == strtobytes("P6")]
3715    return header[0], header[1], header[2], depth, header[3]
3716
3717
3718def write_pnm(file, width, height, pixels, meta):
3719    """Write a Netpbm PNM/PAM file."""
3720
3721    bitdepth = meta["bitdepth"]
3722    maxval = 2 ** bitdepth - 1
3723    # Rudely, the number of image planes can be used to determine
3724    # whether we are L (PGM), LA (PAM), RGB (PPM), or RGBA (PAM).
3725    planes = meta["planes"]
3726    # Can be an assert as long as we assume that pixels and meta came
3727    # from a PNG file.
3728    assert planes in (1, 2, 3, 4)
3729    if planes in (1, 3):
3730        if 1 == planes:
3731            # PGM
3732            # Could generate PBM if maxval is 1, but we don't (for one
3733            # thing, we'd have to convert the data, not just blat it
3734            # out).
3735            fmt = "P5"
3736        else:
3737            # PPM
3738            fmt = "P6"
3739        file.write("%s %d %d %d\n" % (fmt, width, height, maxval))
3740    if planes in (2, 4):
3741        # PAM
3742        # See http://netpbm.sourceforge.net/doc/pam.html
3743        if 2 == planes:
3744            tupltype = "GRAYSCALE_ALPHA"
3745        else:
3746            tupltype = "RGB_ALPHA"
3747        file.write(
3748            "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\n"
3749            "TUPLTYPE %s\nENDHDR\n" % (width, height, planes, maxval, tupltype)
3750        )
3751    # Values per row
3752    vpr = planes * width
3753    # struct format
3754    fmt = ">%d" % vpr
3755    if maxval > 0xFF:
3756        fmt = fmt + "H"
3757    else:
3758        fmt = fmt + "B"
3759    for row in pixels:
3760        file.write(struct.pack(fmt, *row))
3761    file.flush()
3762
3763
3764def color_triple(color):
3765    """
3766    Convert a command line colour value to a RGB triple of integers.
3767    FIXME: Somewhere we need support for greyscale backgrounds etc.
3768    """
3769    if color.startswith("#") and len(color) == 4:
3770        return (int(color[1], 16), int(color[2], 16), int(color[3], 16))
3771    if color.startswith("#") and len(color) == 7:
3772        return (int(color[1:3], 16), int(color[3:5], 16), int(color[5:7], 16))
3773    elif color.startswith("#") and len(color) == 13:
3774        return (int(color[1:5], 16), int(color[5:9], 16), int(color[9:13], 16))
3775
3776
3777def _main(argv):
3778    """
3779    Run the PNG encoder with options from the command line.
3780    """
3781
3782    # Parse command line arguments
3783    from optparse import OptionParser
3784    import re
3785
3786    version = "%prog " + re.sub(r"( ?\$|URL: |Rev:)", "", __version__)
3787    parser = OptionParser(version=version)
3788    parser.set_usage("%prog [options] [imagefile]")
3789    parser.add_option(
3790        "-r",
3791        "--read-png",
3792        default=False,
3793        action="store_true",
3794        help="Read PNG, write PNM",
3795    )
3796    parser.add_option(
3797        "-i",
3798        "--interlace",
3799        default=False,
3800        action="store_true",
3801        help="create an interlaced PNG file (Adam7)",
3802    )
3803    parser.add_option(
3804        "-t",
3805        "--transparent",
3806        action="store",
3807        type="string",
3808        metavar="color",
3809        help="mark the specified colour (#RRGGBB) as transparent",
3810    )
3811    parser.add_option(
3812        "-b",
3813        "--background",
3814        action="store",
3815        type="string",
3816        metavar="color",
3817        help="save the specified background colour",
3818    )
3819    parser.add_option(
3820        "-a",
3821        "--alpha",
3822        action="store",
3823        type="string",
3824        metavar="pgmfile",
3825        help="alpha channel transparency (RGBA)",
3826    )
3827    parser.add_option(
3828        "-g",
3829        "--gamma",
3830        action="store",
3831        type="float",
3832        metavar="value",
3833        help="save the specified gamma value",
3834    )
3835    parser.add_option(
3836        "-c",
3837        "--compression",
3838        action="store",
3839        type="int",
3840        metavar="level",
3841        help="zlib compression level (0-9)",
3842    )
3843    parser.add_option(
3844        "-T",
3845        "--test",
3846        default=False,
3847        action="store_true",
3848        help="create a test image (a named PngSuite image if an argument is supplied)",
3849    )
3850    parser.add_option(
3851        "-L",
3852        "--list",
3853        default=False,
3854        action="store_true",
3855        help="print list of named test images",
3856    )
3857    parser.add_option(
3858        "-R",
3859        "--test-red",
3860        action="store",
3861        type="string",
3862        metavar="pattern",
3863        help="test pattern for the red image layer",
3864    )
3865    parser.add_option(
3866        "-G",
3867        "--test-green",
3868        action="store",
3869        type="string",
3870        metavar="pattern",
3871        help="test pattern for the green image layer",
3872    )
3873    parser.add_option(
3874        "-B",
3875        "--test-blue",
3876        action="store",
3877        type="string",
3878        metavar="pattern",
3879        help="test pattern for the blue image layer",
3880    )
3881    parser.add_option(
3882        "-A",
3883        "--test-alpha",
3884        action="store",
3885        type="string",
3886        metavar="pattern",
3887        help="test pattern for the alpha image layer",
3888    )
3889    parser.add_option(
3890        "-K",
3891        "--test-black",
3892        action="store",
3893        type="string",
3894        metavar="pattern",
3895        help="test pattern for greyscale image",
3896    )
3897    parser.add_option(
3898        "-d",
3899        "--test-depth",
3900        default=8,
3901        action="store",
3902        type="int",
3903        metavar="NBITS",
3904        help="create test PNGs that are NBITS bits per channel",
3905    )
3906    parser.add_option(
3907        "-S",
3908        "--test-size",
3909        action="store",
3910        type="int",
3911        metavar="size",
3912        help="width and height of the test image",
3913    )
3914    (options, args) = parser.parse_args(args=argv[1:])
3915
3916    # Convert options
3917    if options.transparent is not None:
3918        options.transparent = color_triple(options.transparent)
3919    if options.background is not None:
3920        options.background = color_triple(options.background)
3921
3922    if options.list:
3923        names = list(_pngsuite)
3924        names.sort()
3925        for name in names:
3926            print(name)
3927        return
3928
3929    # Run regression tests
3930    if options.test:
3931        return test_suite(options, args)
3932
3933    # Prepare input and output files
3934    if len(args) == 0:
3935        infilename = "-"
3936        infile = sys.stdin
3937    elif len(args) == 1:
3938        infilename = args[0]
3939        infile = open(infilename, "rb")
3940    else:
3941        parser.error("more than one input file")
3942    outfile = sys.stdout
3943
3944    if options.read_png:
3945        # Encode PNG to PPM
3946        png = Reader(file=infile)
3947        width, height, pixels, meta = png.asDirect()
3948        write_pnm(outfile, width, height, pixels, meta)
3949    else:
3950        # Encode PNM to PNG
3951        format, width, height, depth, maxval = read_pnm_header(
3952            infile, ("P5", "P6", "P7")
3953        )
3954        # When it comes to the variety of input formats, we do something
3955        # rather rude.  Observe that L, LA, RGB, RGBA are the 4 colour
3956        # types supported by PNG and that they correspond to 1, 2, 3, 4
3957        # channels respectively.  So we use the number of channels in
3958        # the source image to determine which one we have.  We do not
3959        # care about TUPLTYPE.
3960        greyscale = depth <= 2
3961        pamalpha = depth in (2, 4)
3962        supported = map(lambda x: 2 ** x - 1, range(1, 17))
3963        try:
3964            mi = supported.index(maxval)
3965        except ValueError:
3966            raise NotImplementedError(
3967                "your maxval (%s) not in supported list %s" % (maxval, str(supported))
3968            )
3969        bitdepth = mi + 1
3970        writer = Writer(
3971            width,
3972            height,
3973            greyscale=greyscale,
3974            bitdepth=bitdepth,
3975            interlace=options.interlace,
3976            transparent=options.transparent,
3977            background=options.background,
3978            alpha=bool(pamalpha or options.alpha),
3979            gamma=options.gamma,
3980            compression=options.compression,
3981        )
3982        if options.alpha:
3983            pgmfile = open(options.alpha, "rb")
3984            format, awidth, aheight, adepth, amaxval = read_pnm_header(pgmfile, "P5")
3985            if amaxval != "255":
3986                raise NotImplementedError(
3987                    "maxval %s not supported for alpha channel" % amaxval
3988                )
3989            if (awidth, aheight) != (width, height):
3990                raise ValueError(
3991                    "alpha channel image size mismatch"
3992                    " (%s has %sx%s but %s has %sx%s)"
3993                    % (infilename, width, height, options.alpha, awidth, aheight)
3994                )
3995            writer.convert_ppm_and_pgm(infile, pgmfile, outfile)
3996        else:
3997            writer.convert_pnm(infile, outfile)
3998
3999
4000if __name__ == "__main__":
4001    try:
4002        _main(sys.argv)
4003    except Error:
4004        e = geterror()
4005        sys.stderr.write("%s\n" % (e,))
4006