File: //home/arjun/projects/env/lib/python3.10/site-packages/__pycache__/png.cpython-310.pyc
o
we]C � @ s` d Z dZddlZddlZddlZddlZddlZddlZddlZddl Z ddl
Z
ddlZddlmZ g d�Z
e�dddd d
ddd
d� ZdZdd� Ze�dd�Zdd� Zdd� Zdd� Zdd� Zdd� ZG dd� de�ZG dd � d e�ZG d!d"� d"e�ZG d#d$� d$e�ZG d%d&� d&�ZG d'd(� d(�Zd^d*d+�Z d,d-� Z!d.d/� Z"d0d1� Z#d2d3� Z$d4d5� Z%d6d7� Z&ej'd8ej(d9�Z)di fd:d;�Z*e*Z+G d<d=� d=�Z,G d>d?� d?�Z-d@dA� Z.dBdC� Z/dDdE� Z0dFdG� Z1dHdI� Z2dJdK� Z3dLdM� Z4dNdO� Z5dPdQ� Z6dRdS� Z7dTdU� Z8dVdW� Z9dXdY� Z:dZd[� Z;e<d\k�r.ze;e j=� W dS e�y- Z> ze?e>e j@d]� W Y dZ>[>dS dZ>[>ww dS )_a
The ``png`` module can read and write PNG files.
Installation and Overview
-------------------------
``pip install pypng``
For help, type ``import png; help(png)`` in your python interpreter.
A good place to start is the :class:`Reader` and :class:`Writer` classes.
Coverage of PNG formats is fairly complete;
all allowable bit depths (1/2/4/8/16/24/32/48/64 bits per pixel) and
colour combinations are supported:
- greyscale (1/2/4/8/16 bit);
- RGB, RGBA, LA (greyscale with alpha) with 8/16 bits per channel;
- colour mapped images (1/2/4/8 bit).
Interlaced images,
which support a progressive display when downloading,
are supported for both reading and writing.
A number of optional chunks can be specified (when writing)
and understood (when reading): ``tRNS``, ``bKGD``, ``gAMA``.
The ``sBIT`` chunk can be used to specify precision for
non-native bit depths.
Requires Python 3.5 or higher.
Installation is trivial,
but see the ``README.txt`` file (with the source distribution) for details.
Full use of all features will need some reading of the PNG specification
http://www.w3.org/TR/2003/REC-PNG-20031110/.
The package also comes with command line utilities.
- ``pripamtopng`` converts
`Netpbm <http://netpbm.sourceforge.net/>`_ PAM/PNM files to PNG;
- ``pripngtopam`` converts PNG to file PAM/PNM.
There are a few more for simple PNG manipulations.
Spelling and Terminology
------------------------
Generally British English spelling is used in the documentation.
So that's "greyscale" and "colour".
This not only matches the author's native language,
it's also used by the PNG specification.
Colour Models
-------------
The major colour models supported by PNG (and hence by PyPNG) are:
- greyscale;
- greyscale--alpha;
- RGB;
- RGB--alpha.
Also referred to using the abbreviations: L, LA, RGB, RGBA.
Each letter codes a single channel:
*L* is for Luminance or Luma or Lightness (greyscale images);
*A* stands for Alpha, the opacity channel
(used for transparency effects, but higher values are more opaque,
so it makes sense to call it opacity);
*R*, *G*, *B* stand for Red, Green, Blue (colour image).
Lists, arrays, sequences, and so on
-----------------------------------
When getting pixel data out of this module (reading) and
presenting data to this module (writing) there are
a number of ways the data could be represented as a Python value.
The preferred format is a sequence of *rows*,
which each row being a sequence of *values*.
In this format, the values are in pixel order,
with all the values from all the pixels in a row
being concatenated into a single sequence for that row.
Consider an image that is 3 pixels wide by 2 pixels high, and each pixel
has RGB components:
Sequence of rows::
list([R,G,B, R,G,B, R,G,B],
[R,G,B, R,G,B, R,G,B])
Each row appears as its own list,
but the pixels are flattened so that three values for one pixel
simply follow the three values for the previous pixel.
This is the preferred because
it provides a good compromise between space and convenience.
PyPNG regards itself as at liberty to replace any sequence type with
any sufficiently compatible other sequence type;
in practice each row is an array (``bytearray`` or ``array.array``).
To allow streaming the outer list is sometimes
an iterator rather than an explicit list.
An alternative format is a single array holding all the values.
Array of values::
[R,G,B, R,G,B, R,G,B,
R,G,B, R,G,B, R,G,B]
The entire image is one single giant sequence of colour values.
Generally an array will be used (to save space), not a list.
The top row comes first,
and within each row the pixels are ordered from left-to-right.
Within a pixel the values appear in the order R-G-B-A
(or L-A for greyscale--alpha).
There is another format, which should only be used with caution.
It is mentioned because it is used internally,
is close to what lies inside a PNG file itself,
and has some support from the public API.
This format is called *packed*.
When packed, each row is a sequence of bytes (integers from 0 to 255),
just as it is before PNG scanline filtering is applied.
When the bit depth is 8 this is the same as a sequence of rows;
when the bit depth is less than 8 (1, 2 and 4),
several pixels are packed into each byte;
when the bit depth is 16 each pixel value is decomposed into 2 bytes
(and `packed` is a misnomer).
This format is used by the :meth:`Writer.write_packed` method.
It isn't usually a convenient format,
but may be just right if the source data for
the PNG image comes from something that uses a similar format
(for example, 1-bit BMPs, or another PNG file).
z0.20220715.0� N��array)�Image�Reader�Writer�write_chunks�
from_array�8B� �P �N �G �
�
� ))r r � r )� r r r )r r r r )� r r r )r r r r )� r r r )r r r r c # s@ � t D ]\� }�}� | krq� �fdd�t|||�D �V qdS )au
Generate the coordinates for the reduced scanlines
of an Adam7 interlaced image
of size `width` by `height` pixels.
Yields a generator for each pass,
and each pass generator yields a series of (x, y, xstep) triples,
each one identifying a reduced scanline consisting of
pixels starting at (x, y) and taking every xstep pixel to the right.
c 3 s � | ]}� |�fV qd S �N� )�.0�y��xstart�xstepr �</home/arjun/projects/env/lib/python3.10/site-packages/png.py� <genexpr>� s � z!adam7_generate.<locals>.<genexpr>N)�adam7�range)�width�height�ystart�ystepr r r �adam7_generate� s � �r$ �_Resolutionzx y unit_is_meterc C s t tt| �g| � �S r )�list�zip�iter)�s�nr r r �group� s r+ c C s
t | t�S r )�
isinstancer )�xr r r �isarray� �
r. c C s� | du rdS t | �}dt|� k rdkstd�� td��d}t|�D ]B\}}t|�dvr4td| ��t|�dkr<d }|rJt|�d
krJtd| ��|D ]}t|�|ks_d| kr^dksen td
| ��qLq$|S )z�
Check a palette argument (to the :class:`Writer` class) for validity.
Returns the palette as a list if okay;
raises an exception otherwise.
Nr � zTa palette must have between 1 and 256 entries, see https://www.w3.org/TR/PNG/#11PLTEF)� r z1palette entry %d: entries must be 3- or 4-tuples.r1 Tr z8palette entry %d: all 4-tuples must precede all 3-tuples� z7palette entry %d: values must be integer: 0 <= x <= 255)r&