
    ^jG                      d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZmZmZmZ ddlZddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z] ddl^m_Z_ dd	l`maZa dd
lbmcZc ddldmeZe ddlfmgZgmhZh ddlimjZjmkZkmlZl ddlmmnZo ddlmmpZq e	rddlmrZrmsZsmtZtmuZu ddlvmwZw ddlmmxZxmyZy  G d dez          Z{ G d de{          Z| G d de{          Z}d_d`d"Z~d_dad$Zdbdcd*Zddd+Zddd,Zded0Z G d1 d2e          Z G d3 d4e          Z G d5 d6e          Z G d7 d!e          Z G d8 d9e          Z G d: d;e          Z G d< d=e          Z G d> d?e          Z G d@ dAe          Z G dB dCe          Z G dD dEe          Z G dF dGe          Zee_         G dH dIee          Z G dJ dKe          Z G dL dMee          Zee_        ee_         G dN dOe          Z G dP dQee          Z G dR dSee          Z G dT dU          ZdfdVZ G dW dXe          Z G dY dZe          Z G d[ d\e          Z G d] d^e          Z eo             dS )ga  Image load, capture and high-level texture functions.

Only basic functionality is described here; for full reference see the
accompanying documentation.

To load an image::

    from pyglet import image
    pic = image.load('picture.png')

The supported image file types include PNG, BMP, GIF, JPG, and many more,
somewhat depending on the operating system.  To load an image from a file-like
object instead of a filename::

    pic = image.load('hint.jpg', file=fileobj)

The hint helps the module locate an appropriate decoder to use based on the
file extension.  It is optional.

Once loaded, images can be used directly by most other modules of pyglet.  All
images have a width and height you can access::

    width, height = pic.width, pic.height

You can extract a region of an image (this keeps the original image intact;
the memory is shared efficiently)::

    subimage = pic.get_region(x, y, width, height)

Remember that y-coordinates are always increasing upwards.

Drawing images
--------------

To draw an image at some point on the screen::

    pic.blit(x, y, z)

This assumes an appropriate view transform and projection have been applied.

Some images have an intrinsic "anchor point": this is the point which will be
aligned to the ``x`` and ``y`` coordinates when the image is drawn.  By
default, the anchor point is the lower-left corner of the image.  You can use
the anchor point to center an image at a given point, for example::

    pic.anchor_x = pic.width // 2
    pic.anchor_y = pic.height // 2
    pic.blit(x, y, z)

Texture access
--------------

If you are using OpenGL directly, you can access the image as a texture::

    texture = pic.get_texture()

(This is the most efficient way to obtain a texture; some images are
immediately loaded as textures, whereas others go through an intermediate
form).  To use a texture with pyglet.gl::

    from pyglet.gl import *
    glEnable(texture.target)        # typically target is GL_TEXTURE_2D
    glBindTexture(texture.target, texture.id)
    # ... draw with the texture

Pixel access
------------

To access raw pixel data of an image::

    rawimage = pic.get_image_data()

(If the image has just been loaded this will be a very quick operation;
however if the image is a texture a relatively expensive readback operation
will occur).  The pixels can be accessed as bytes::

    format = 'RGBA'
    pitch = rawimage.width * len(format)
    pixels = rawimage.get_bytes(format, pitch)

"format" strings consist of characters that give the byte order of each color
component.  For example, if rawimage.format is 'RGBA', there are four color
components: red, green, blue and alpha, in that order.  Other common format
strings are 'RGB', 'LA' (luminance, alpha) and 'I' (intensity).

The "pitch" of an image is the number of bytes in a row (this may validly be
more than the number required to make up the width of the image, it is common
to see this for word alignment).  If "pitch" is negative the rows of the image
are ordered from top to bottom, otherwise they are ordered from bottom to top.

Retrieving data with the format and pitch given in `ImageData.format` and
`ImageData.pitch` avoids the need for data conversion (assuming you can make
use of the data in this arbitrary format).

    )annotationsN)ABCabstractmethod)TYPE_CHECKING)byrefc_intc_ubytec_uintsizeof)LGLExceptionGL_ALPHAGL_BACKGL_BGRGL_BGRAGL_COLOR_ATTACHMENT0GL_DEPTH_COMPONENTGL_DEPTH_STENCILGL_DRAW_FRAMEBUFFERGL_FLOATGL_FRAMEBUFFER&GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE	GL_LINEARGL_LINEAR_MIPMAP_LINEARGL_LUMINANCEGL_MAX_TEXTURE_SIZEGL_MAX_ARRAY_TEXTURE_LAYERSGL_PACK_ALIGNMENTGL_READ_WRITEGL_REDGL_RGGL_RGBGL_RGBA
GL_RGBA32FGL_RGBA8
GL_STENCILGL_STENCIL_INDEXGL_TEXTURE0GL_TEXTURE_2DGL_TEXTURE_2D_ARRAYGL_TEXTURE_3DGL_TEXTURE_MAG_FILTERGL_TEXTURE_MIN_FILTERGL_TRIANGLESGL_UNPACK_ALIGNMENTGL_UNPACK_ROW_LENGTHGL_UNPACK_SKIP_PIXELSGL_UNPACK_SKIP_ROWSGL_UNSIGNED_BYTEGL_VIEWPORTGLintGLubyteGLuintglActiveTextureglBindFramebufferglBindImageTextureglBindTextureglBindVertexArrayglCheckFramebufferStatusglCompressedTexImage2DglCompressedTexSubImage2DglCompressedTexSubImage3DglCopyTexSubImage2DglCopyTexImage2DglDeleteFramebuffersglDeleteTexturesglDeleteVertexArraysglDrawElementsglFlushglFramebufferTexture2DglGenFramebuffersglGenTexturesglGenVertexArraysglGenerateMipmap%glGetFramebufferAttachmentParameterivglGetIntegervglGetTexImageglPixelStoreiglReadBufferglReadPixelsglTexImage2DglTexImage3DglTexParameteriglTexSubImage2DglTexSubImage3Dgl_info)	Attribute)BufferObject)asbytes   )atlas)	AnimationAnimationFrame)FramebufferRenderbufferget_max_color_attachments)add_default_codecs)registry)BinaryIOSequenceCallableLiteral)Iterator)ImageDecoderImageEncoderc                      e Zd ZdS )ImageExceptionN)__name__
__module____qualname__     P/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/image/__init__.pyrl   rl      s        Drq   rl   c                      e Zd ZdZdS )TextureArraySizeExceededzIException occurs ImageData dimensions are larger than the array supports.Nrm   rn   ro   __doc__rp   rq   rr   rt   rt      s        SSSSrq   rt   c                      e Zd ZdZdS )TextureArrayDepthExceededzGException occurs when depth has hit the maximum supported of the array.Nru   rp   rq   rr   rx   rx      s        QQQQrq   rx   filenamestrfileBinaryIO | NonedecoderImageDecoder | NonereturnAbstractImagec                \    |r|                     | |          S t          j         | |          S )a  Load an image from a file on disk, or from an open file-like object.

    Args:
        filename:
            Used to guess the image format, and to load the file if ``file``
            is unspecified.
        file:
            Optional file containing the image data in any supported format.
        decoder:
            If unspecified, all decoders that are registered for the filename
            extension are tried.  If none succeed, the exception from the
            first decoder is raised.

    .. note:: You can make no assumptions about the return type; usually it will
              be ImageData or CompressedImageData, but decoders are free to return
              any subclass of AbstractImage.
    )decode_codec_registryry   r{   r}   s      rr   loadr      s3    $  .~~h---!(D111rq   r]   c                \    |r|                     | |          S t          j         | |          S )a  Load an animation from a file on disk, or from an open file-like object.

    Args:
        filename:
            Used to guess the animation format, and to load the file if ``file``
            is unspecified.
        file:
            Optional file containing the animation data in any supported format.
        decoder:
            If unspecified, all decoders that are registered for the filename
            extension are tried.  If none succeed, the exception from the
            first decoder is raised.
    )decode_animationr   r   s      rr   load_animationr      s5      8''$777+Hd;;;rq   widthintheightpatternImagePattern | Nonec                N    |st                      }|                    | |          S )a  Create an image optionally filled with the given pattern.

    :Parameters:
        width:
            Width of image to create.
        height:
            Height of image to create.
        pattern:
            Optional pattern to fill image with. If unspecified, the image will
            initially be transparent.

    .. note:: You can make no assumptions about the return type; usually it will
              be ImageData or CompressedImageData, but patterns are free to return
              any subclass of AbstractImage.
    )SolidColorImagePatterncreate_image)r   r   r   s      rr   creater      s-       +(**v...rq   c                 V    t                      } t          t          |            | j        S )z(Query the maximum texture size available)r   rM   r   value)sizes    rr   get_max_texture_sizer      s$    77D%t,,,:rq   c                 V    t                      } t          t          |            | j        S )z$Query the maximum TextureArray depth)r   rM   r   r   )
max_layerss    rr   get_max_array_texture_layersr      s%    J-z:::rq   colorSequence[int, int, int, int]bytesc                d    t          |           dk    rt          d          t          |           S )N   z&color is expected to have 4 components)len	TypeErrorr   )r   s    rr   _color_as_bytesr      s,    
5zzQ@AAA<<rq   c                  *    e Zd ZdZed	d            ZdS )
ImagePatternzAbstract image creation class.r   r   r   r   r   c                     t          d          )z"Create an image of the given size.z"method must be defined in subclassNotImplementedErrorselfr   r   s      rr   r   zImagePattern.create_image   s     ""FGGGrq   Nr   r   r   r   r   r   )rm   rn   ro   rv   r   r   rp   rq   rr   r   r      s>        ((H H H ^H H Hrq   r   c                  $    e Zd ZdZdddZddZdS )r   z0Creates an image filled with a solid RGBA color.r   r   r   r   r   r   c                .    t          |          | _        dS )zCreate a solid image pattern with the given color.

        Args:
            color:
                4-tuple of ints in range [0,255] giving RGBA components of
                color to fill with.
        N)r   r   )r   r   s     rr   __init__zSolidColorImagePattern.__init__   s     %U++


rq   r   r   r   r   r   c                @    | j         |z  |z  }t          ||d|          S )NRGBA)r   	ImageData)r   r   r   datas       rr   r   z#SolidColorImagePattern.create_image   s'    zE!F*555rq   N)r   )r   r   r   rm   rn   ro   rv   r   r   rp   rq   rr   r   r      sG        ::, , , , ,6 6 6 6 6 6rq   r   c                  (    e Zd ZdZ	 	 dddZddZdS )CheckerImagePatternz.Create an image with a tileable checker image.   r   r         r   r   r   color1r   color2c                V    t          |          | _        t          |          | _        dS )a  Initialise with the given colors.

        Args:
            color1:
                4-tuple of ints in range [0,255] giving RGBA components of
                color to fill with.  This color appears in the top-left and
                bottom-right corners of the image.
            color2:
                4-tuple of ints in range [0,255] giving RGBA components of
                color to fill with.  This color appears in the top-right and
                bottom-left corners of the image.
        N)r   r   r   )r   r   r   s      rr   r   zCheckerImagePattern.__init__  s&     &f--%f--rq   r   r   r   r   r   c                    |dz  }|dz  }| j         |z  | j        |z  z   }| j        |z  | j         |z  z   }||z  ||z  z   }t          ||d|          S )N   r   )r   r   r   )r   r   r   hwhhrow1row2r   s           rr   r   z CheckerImagePattern.create_image  sj    aZq[{R$+"22{R$+"22by4"9$555rq   N)r   r   )r   r   r   r   r   r   rp   rq   rr   r   r     sM        88>R8L. . . . ."6 6 6 6 6 6rq   r   c                      e Zd ZU dZdZded<   	 dZded<   	 d'dZd(dZe	d)d            Z
e	d*d+d            Ze	d,d            Ze	d-d            Z	 	 d.d/dZe	d0d1d!            Ze	d1d"            Ze	d2d3d&            ZdS )4r   z%Abstract class representing an image.r   r   anchor_xanchor_yr   r   c                "    || _         || _        dS )zInitialized in subclass.Nr   r   r   s      rr   r   zAbstractImage.__init__+  s    
rq   r   rz   c                @    | j         j         d| j         d| j         dS )Nz(size=x))	__class__rm   r   r   r   s    rr   __repr__zAbstractImage.__repr__0  s*    .)LLLLdkLLLLrq   r   c                    dS )zGet an ImageData view of this image.

        Changes to the returned instance may or may not be reflected in this image.
        Nrp   r   s    rr   get_image_datazAbstractImage.get_image_data3        rq   F	rectangleboolTexturec                    dS )zA :py:class:`~pyglet.image.Texture` view of this image.

        Args:
            rectangle:
                Unused. Kept for backwards compatibility.
        Nrp   r   r   s     rr   get_texturezAbstractImage.get_texture:  r   rq   c                    dS )zWRetrieve a :py:class:`~pyglet.image.Texture` instance with all mipmap levels filled in.Nrp   r   s    rr   get_mipmapped_texturez#AbstractImage.get_mipmapped_textureC  r   rq   r   yc                    dS )z,Retrieve a rectangular region of this image.Nrp   r   r   r   r   r   s        rr   
get_regionzAbstractImage.get_regionG  r   rq   Nry   
str | Noner{   r|   encoderImageEncoder | NoneNonec                    ||
J d            t          |d          }||                    | ||           dS t          j        | ||           dS )a  Save this image to a file.

        Args:
            filename:
                Used to set the image file format, and to open the output file
                if ``file`` is unspecified.
            file:
                An optional file-like object to write image data to.
            encoder:
                If unspecified, all encoders matching the filename extension
                are tried, or all encoders if no filename is provided. If all
                fail, the exception from the first one attempted is raised.
        Nz*Either filename or file must be specified.wb)openencoder   )r   ry   r{   r   s       rr   savezAbstractImage.saveK  sj     <'')U'''$''DNN4400000"4488888rq   zc                    dS )a  Draw this image to the active framebuffer.

        The image will be drawn with the lower-left corner at
        (``x - anchor_x``, ``y - anchor_y``, ``z``).

        .. note:: This is a relatively slow method, as a full OpenGL setup and
                  draw call is required for each blit. For performance, consider
                  creating a Sprite from the Image, and adding it to a Batch.
        Nrp   r   r   r   r   s       rr   blitzAbstractImage.blitc  r   rq   c                    dS )al  Draw the provided ``source`` data INTO this image.

        ``source`` will be copied into this image such that its anchor point
        is aligned with the ``x`` and ``y`` parameters. If this image is a 3D
        Texture, the ``z`` coordinate gives the image slice to copy into.

        Note that if ``source`` is larger than this image (or the positioning
        would cause the copy to go out of bounds), an exception may be raised.
        To prevent errors, you can use the ``source.get_region(x, y, w, h)``
        method to get a smaller section that will fall within the bounds of
        this image.
        Nrp   r   sourcer   r   r   s        rr   	blit_intozAbstractImage.blit_intoo  r   rq   targetlevelinternalformatc                    dS )am  Draw this image on the currently bound texture at ``target``.

        This image is copied into the texture such that this image's anchor
        point is aligned with the given ``x`` and ``y`` coordinates of the
        destination texture.  If the currently bound texture is a 3D texture,
        the ``z`` coordinate gives the image slice to blit into.
        Nrp   r   r   r   r   r   r   r   s          rr   blit_to_texturezAbstractImage.blit_to_texture~  r   rq   )r   r   r   r   r   rz   r   r   Fr   r   r   r   r   r   
r   r   r   r   r   r   r   r   r   r   )NNN)ry   r   r{   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )rm   rn   ro   rv   r   __annotations__r   r   r   r   r   r   r   r   r   r   r   r   rp   rq   rr   r   r   "  s        //HFHH   
M M M M    ^     ^ f f f ^f ; ; ; ^; +/JN9 9 9 9 90 	 	 	 	 ^	    ^     ^  rq   c                      e Zd ZdZedd            ZdddZedd            Zedd            Zedd            Z	edd            Z
dS )AbstractImageSequencezAbstract sequence of images.

    Image sequence are useful for storing image animations or slices of a volume.
    The class implements the sequence interface (``__len__``, ``__getitem__``,
    ``__setitem__``).
    r   TextureSequencec                    dS )zaGet a TextureSequence.

        :rtype: `TextureSequence`

        .. versionadded:: 1.1
        Nrp   r   s    rr   get_texture_sequencez*AbstractImageSequence.get_texture_sequence  r   rq   Tperiodfloatloopr   r]   c                .    t          j        | ||          S )a  Create an animation over this image sequence for the given constant
        framerate.

        Args:
            period:
                Number of seconds to display each frame.
            loop:
                If True, the animation will loop continuously.
        )r]   from_image_sequence)r   r  r  s      rr   get_animationz#AbstractImageSequence.get_animation  s     ,T64@@@rq   r   c                    dS )z%Retrieve one or more images by index.Nrp   r   items     rr   __getitem__z!AbstractImageSequence.__getitem__  r   rq   imagec                    dS )zReplace one or more images in the sequence.

        Args:
            image:
                The replacement image. The actual instance may not be used,
                depending on this implementation.
        Nrp   )r   r
  r  s      rr   __setitem__z!AbstractImageSequence.__setitem__  r   rq   r   c                    dS )zLength of the image sequence.Nrp   r   s    rr   __len__zAbstractImageSequence.__len__  r   rq   Iterator[AbstractImage]c                    dS )z$Iterate over the images in sequence.Nrp   r   s    rr   __iter__zAbstractImageSequence.__iter__  r   rq   Nr   r   )T)r  r  r  r   r   r]   )r   r   )r  r   r   r   r   r   )r   r  )rm   rn   ro   rv   r   r  r  r  r  r  r  rp   rq   rr   r   r     s             ^
A 
A 
A 
A 
A 4 4 4 ^4    ^ , , , ^, 3 3 3 ^3 3 3rq   r   c                  :    e Zd ZdZddZddZdd
ZddZddZdS )r   zInterface for a sequence of textures.

    Typical implementations store multiple :py:class:`~pyglet.image.TextureRegion`s
    within one :py:class:`~pyglet.image.Texture` to minimise state changes.
    r   r   c                    t           r   r   r	  s     rr   r  zTextureSequence.__getitem__      !!rq   texturetype[Texture]r   c                    t           r   r   )r   r
  r  s      rr   r  zTextureSequence.__setitem__  r  rq   r   c                    t           r   r   r   s    rr   r  zTextureSequence.__len__  r  rq   Iterator[Texture]c                    t           r   r   r   s    rr   r  zTextureSequence.__iter__  r  rq   c                    | S r   rp   r   s    rr   r  z$TextureSequence.get_texture_sequence      rq   Nr   )r  r  r   r   r  )r   r  r  )	rm   rn   ro   rv   r  r  r  r  r  rp   rq   rr   r   r     s         " " " "" " " "" " " "" " " "     rq   r   c                      e Zd ZdZdS )UniformTextureSequencezDInterface for a sequence of textures, each with the same dimensions.Nru   rp   rq   rr   r"  r"    s        NNNNrq   r"  c                  "    e Zd ZdZ ej         ed          ej                  Z ej         ed          ej                  Z	 ej         ed          ej                  Z
 ej         ed          ej                  ZdZdZd:d; fdZd Zd<dZed=d            Zej        d>d            Zd?d@dZdAdZd?d@dZdAdZdBdZdCdDd&ZdCdEd'ZdFd(ZdGd,ZdHdId/ZdJd0Zd:dKd3Zd4 Z d5 Z!dLd6Z"dMd7Z#e$d8             Z%e$d9             Z& xZ'S )Nr   z3An image represented as a string of unsigned bytes.z(.)z(.)(.)z	(.)(.)(.)z(.)(.)(.)(.)Nr   r   r   fmtrz   r   r   pitch
int | Nonec                    t                                          ||           |                                x| _        | _        || _        |p|t          |          z  | _        | j        | _        g | _	        dS )a  Initialise image data.

        Args:
            width:
                Width of image data
            height:
                Height of image data
            fmt:
                A valid format string, such as 'RGB', 'RGBA', 'ARGB', etc.
            data:
                A sequence of bytes containing the raw image data.
            pitch:
                If specified, the number of bytes per row.  Negative values
                indicate a top-to-bottom arrangement.  Defaults to
                ``width * len(format)``.
        N)
superr   upper_current_format_desired_format_current_datar   r%  _current_pitchmipmap_images)r   r   r   r$  r   r%  r   s         rr   r   zImageData.__init__  sn    " 	'''69iikkAt3!.ec#hh.
"jrq   c           	         | j         | j        |                     | j        | j                  | j        | j        | j        | j        | j        dS )N)r   r   r,  r*  r+  r-  r%  r.  )r   r   	get_bytesr*  r-  r+  r%  r.  r   s    rr   __getstate__zImageData.__getstate__  sO    Zk!^^D,@$BUVV#3#3"1Z!/	
 	
 		
rq   r   c                    | S r   rp   r   s    rr   r   zImageData.get_image_data  r   rq   c                    | j         S )z&Format string of the data. Read-write.)r+  r   s    rr   formatzImageData.format  s     ##rq   c                F    |                                 | _        d | _        d S r   )r)  r+  _current_texture)r   r$  s     rr   r4  zImageData.format  s     "yy{{ $rq   r   c                    |p| j         }|p| j        }|| j        k    r|| j        k    r| j        S |                     ||          S )a  Get the byte data of the image

        This method returns the raw byte data of the image, with optional conversion.
        To convert the data into another format, you can provide ``fmt`` and ``pitch``
        arguments. For example, if the image format is ``RGBA``, and you wish to get
        the byte data in ``RGB`` format::

            rgb_pitch = my_image.width // len('RGB')
            rgb_img_bytes = my_image.get_bytes(fmt='RGB', pitch=rgb_pitch)

        The image ``pitch`` may be negative, so be sure to check that when converting
        to another format. Switching the sign of the ``pitch`` will cause the image
        to appear "upside-down".

        Args:
            fmt:
                If provided, get the data in another format.
            pitch:
                The number of bytes per row. This generally means the length
                of the format string * the number of pixels per row.
                Negative values indicate a top-to-bottom arrangement.

        Note:
             Conversion to another format is done on the CPU, and can be
             somewhat costly for larger images. Consider performing conversion
             at load time for framerate sensitive applications.
        )r+  r-  r*  r,  _convertr   r$  r%  s      rr   r0  zImageData.get_bytes  sV    8 )T),,$&&&5D4G+G+G%%}}S%(((rq   r   c                L    || _         || _        || _        d| _        d| _        dS )a  Set the byte data of the image.

        Args:
            fmt:
                The format string of the supplied data.
                For example: "RGB" or "RGBA"
            pitch:
                The number of bytes per row. This generally means the length
                of the format string * the number of pixels per row.
                Negative values indicate a top-to-bottom arrangement.
            data:
                Image data as bytes.
        N)r*  r-  r,  r6  _current_mipmap_texturer   r$  r%  r   s       rr   	set_byteszImageData.set_bytes8  s2      ##! $'+$$$rq   c                .    |                      ||          S )zGet the byte data of the image.

        Warning:
            This method is deprecated and will be removed in the next version.
            Use :py:meth:`~get_bytes` instead.
        )r0  r9  s      rr   get_datazImageData.get_dataL  s     ~~c5)))rq   c                4    |                      |||           dS )zSet the byte data of the image.

        Warning:
            This method is deprecated and will be removed in the next version.
            Use :py:meth:`~set_bytes` instead.
        N)r=  r<  s       rr   set_datazImageData.set_dataU  s      	sE4(((((rq   r   r  r   c                D   |dk    rd}t          |          | j        | j        }}t          |          D ]}|dz  }|dz  }||j        k    s||j        k    rt          d|           | xj        dg|t          | j                  z
  z  z  c_        || j        |dz
  <   dS )a  Set a user-defined mipmap image for a particular level.

        These mipmap images will be applied to textures obtained via
        :py:meth:`~get_mipmapped_texture`, instead of automatically
        generated images for each level.

        Args:
            level:
                Mipmap level to set image at, must be >= 1.
            image:
                Image to set.  Must have correct dimensions for that mipmap
                level (i.e., width >> level, height >> level)
        r   z5Cannot set mipmap image at level 0 (it is this image)r[   z,Mipmap image has wrong dimensions for level N)rl   r   r   ranger.  r   )r   r   r  msgr   r   is          rr   set_mipmap_imagezImageData.set_mipmap_image^  s     A::IC %%% 
DKvu 	 	AaKEqLFFEK6U\#9#9 !WPU!W!WXXX 	tfD4F0G0G(GHH(-519%%%rq   Fclsr  r   r   r   c                4   |                      | j                  }|                    | j        | j        t
          |d          }| j        s| j        r| j        |_        | j        |_        |                     |j	        |j
        | j        | j        dd           |S )z>Given a texture class, create a texture containing this image.F
blank_datar   N)_get_internalformatr+  r   r   r   r(   r   r   r   r   r   )r   rG  r   r   r  s        rr   create_texturezImageData.create_texture|  s    11$2FGG**TZm^`e*ff= 	-DM 	-#}G#}GW^W]DM4=Z[]abbbrq   c                \    | j         s|                     t                    | _         | j         S r   )r6  rL  r   r   s     rr   r   zImageData.get_texture  s-    $ 	A$($7$7$@$@D!$$rq   c           	        | j         r| j         S t                              | j        | j        t
          dd          }| j        s| j        r| j        |_        | j        |_        |                     | j	                  }t          |j        |j                   t          |j        t          t                     | j        rk|                     |j        |j        | j        | j        d|           d}| j        D ]2}|dz  }|r)|                    |j        || j        | j        d|           3nBt'          |j                   |                     |j        |j        | j        | j        d|           || _         |S )zReturn a Texture with mipmaps.

        If :py:class:`~pyglet.image.set_mipmap_Image` has been called with at least
        one image, the set of images defined will be used. Otherwise, mipmaps will be
        automatically generated.
        NFrI  r   r[   )r;  r   r   r   r   r(   r   r   rK  r4  r:   r   idrT   r,   r   r.  r   r   rK   )r   r  r   r   r  s        rr   r   zImageData.get_mipmapped_texture  ss    ' 	0//..T[-Z_.``= 	-DM 	-#}G#}G11$+>>gngj111(=?VWWW 
	q  t}^_aopppE+ r r
 r))'.%PTP]_`bpqqqr W^,,,  t}^_aoppp'.$rq   r   r   ImageDataRegionc                (    t          |||||           S )z1Retrieve a rectangular region of this image data.)rP  r   s        rr   r   zImageData.get_region  s    q!UFD999rq   r   r   c                \    |                                                      |||||           d S r   r   r   )r   r   r   r   r   r   s         rr   r   zImageData.blit  s0    1a77777rq   c                &    t          d|            NNot implemented for r   r   s        rr   r   zImageData.blit_into      !"?"?"?@@@rq   r   r   c                z   || j         z  }|| j        z  }| j        }t          | j                  }|                     |          \  }	}
|	?ddddd                    t          |                    }|                     |          \  }	}
|                     ||          }|dz  rd}n
|dz  rd}nd	}|t          |          z  }t          t          |           t          t          |           |                                  |t          k    s|t          k    r(|rJ t          |||||| j        | j        d|	|
|           nE|r"t%          |||| j        | j        d
|	|
|	  	         n!t'          ||||| j        | j        |	|
|	  	         t          t          d
           |                                  t+                       dS )a  Draw this image to the currently bound texture at ``target``.

        This image's anchor point will be aligned to the given ``x`` and ``y``
        coordinates.  If the currently bound texture is a 3D texture, the ``z``
        parameter gives the image slice to blit into.

        If ``internalformat`` is specified, ``glTexImage`` is used to initialise
        the texture; otherwise, ``glTexSubImage`` is used to update a region.
        NRRGRGBr   )r[   r      r   r[   r   r   r   )r   r   r4  absr-  _get_gl_format_and_typegetr   r8  rO   r.   r/   _apply_region_unpackr*   r)   rV   r   r   rR   rU   _default_region_unpackrF   )r   r   r   r   r   r   r   data_format
data_pitchr$  gl_typer   align
row_lengths                 rr   r   zImageData.blit_to_texture  s    	
T]	T]k,--
 33K@@W; 	 
 c#k""##   77DDLC }}[*55 	EE# 	EEE3{#3#33
)5111*J777!!###]""f0C&C&C%%%%FEq! JQ 	" " " "
  	"'T[g    FEq J 	" " " 	*A...##%%% 						rq   c                    d S r   rp   r   s    rr   r`  zImageData._apply_region_unpack      rq   c                    d S r   rp   r   s    rr   ra  z ImageData._default_region_unpack  rh  rq   c                  	
 || j         k    r|| j        k    r| j        S |                                  | j        	| j        }| j         }|t	          |          z  }|| j         k    rt          d          |D ]C}	 |                    |          dz   }n# t          $ r d}Y nw xY wt          d|z            z  Dt          |          dk    r| j	        n`t          |          dk    r| j
        nEt          |          dk    r| j        n*t          |          dk    r| j        nt          d          | j        t          |          z  t	          | j                  k    ret	          | j                  	fdt          d	t          	                    D             }fd
|D             }d                    |          	n                    	          	|t          |          | j        z  z  }||k    rKt	          |          t	          |          z
  

d	k    rQt	          |          	
fdt          d	t          	                    D             }d                    |          	ni
d	k     rct	          |          t%          d          
 z  	fdt          d	t          	                    D             }d                    |          	||z  d	k     rct	          |          	fdt          d	t          	                    D             }|                                 d                    |          		S )zxReturn data in the desired format.

        This method does not alter this instance's current format or pitch.
         r[   z\%dr   r\  r   z+Current image format is wider than 32 bits.c                *    g | ]}||z            S rp   rp   .0rE  r   	new_pitchs     rr   
<listcomp>z&ImageData._convert.<locals>.<listcomp>1  &    VVV!Qq9}_-VVVrq   r   c                L    g | ] }                     |d                    !S r   )sub)rn  rpacked_pitchreplswap_patterns     rr   rp  z&ImageData._convert.<locals>.<listcomp>2  s2    OOOQ((q,/?@@OOOrq   rq   c                0    g | ]}||z   z
           S rp   rp   )rn  rE  r   diffro  s     rr   rp  z&ImageData._convert.<locals>.<listcomp>@  s,    ]]]Qq9}t334]]]rq   c                0    g | ]}||z            z   S rp   rp   )rn  rE  r   ro  paddings     rr   rp  z&ImageData._convert.<locals>.<listcomp>G  s+    ```AQq9}_-7```rq   c                *    g | ]}||z            S rp   rp   rm  s     rr   rp  z&ImageData._convert.<locals>.<listcomp>M  rq  rq   )r*  r-  r,  _ensure_bytesr]  rZ   index
ValueErrorr   _swap1_pattern_swap2_pattern_swap3_pattern_swap4_patternrl   r   rC  joinrs  r   reverse)r   r$  r%  current_pitchcurrent_format
sign_pitchcidxrowsr   ry  ro  ru  r{  rv  rw  s            @@@@@@@rr   r8  zImageData._convert  s   
 $&&&5D4G+G+G%%!+-"c-&8&88
$&&&2;;D . .(..q11A5CC!   CCC--->""a''#2^$$))#2^$$))#2^$$))#2$%RSSS:N(;(;;L4&''<77 344	VVVVVuQD		97U7UVVVOOOOOO$OOOxx~~ $''d33 '#c((TZ*?@MM!!}%%E

2DaxxJJ	]]]]]]eAs4yyR[>\>\]]]xx~~..	((dU*``````q#d))U^A_A_```xx~~u$q((JJ	VVVVVuQD		97U7UVVVxx~~s    BB('B(c                r    t          | j                  t          urt          | j                  | _        d S d S r   )typer,  r   rZ   r   s    rr   r}  zImageData._ensure_bytesS  s9    "##500!();!<!<D 10rq   c                F   | dk    rt           t          fS | dk    rt          t          fS | dk    rt          t          fS | dk    rt          t          fS | dk    rt
          t          fS | dk    rt          t          fS | dk    rt          t          fS | dk    rt          t          fS d	S )
NrY  rZ  r[  BGRr   BGRALANN)	r   r2   r    r!   r   r"   r   r   r   r$  s    rr   r^  z!ImageData._get_gl_format_and_typeW  s    #::+++D[[***E\\+++E\\+++F]],,,F]],,,CZZ!111CZZ---zrq   c                    | dk    rt           S | dk    rt          S | dk    rt          S | dk    rt          S | dk    rt          S | dk    rt
          S | dk    rt          S | dk    rt          S t          S )	NrY  rZ  r[  r   DDSr  r  )r   r    r!   r"   r   r   r   r   r  s    rr   rK  zImageData._get_internalformatm  sz    #::MD[[LE\\MF]]NCZZ%%D[[##CZZCZZOrq   r   )
r   r   r   r   r$  rz   r   r   r%  r&  r   r   )r$  rz   r  )r$  r   r%  r&  r   r   )r$  rz   r%  r   r   r   r   r   )r   r   r  r   r   r   r   )rG  r  r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   rP  r   NNr   r   r   r   r   r   r   r&  r   r&  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r$  rz   r%  r   r   r   r   r   )(rm   rn   ro   rv   recompilerZ   DOTALLr  r  r  r  r6  r;  r   r1  r   propertyr4  setterr0  r=  r?  rA  rF  rL  r   r   r   r   r   r   r`  ra  r8  r}  staticmethodr^  rK  __classcell__r   s   @rr   r   r     s       ==RZ	::NRZ 1 129==NRZ 4 4bi@@NRZ 7 7CCN"             2

 

 

    $ $ $ X$ ]% % % ]%!) !) !) !) !)F, , , ,(* * * * *) ) ) ). . . .<
 
 
 
 
% % % % %
! ! ! !F: : : :8 8 8 8 8A A A AG G G G GR    F F F FP= = = =   \*   \    rq   r   c                  P     e Zd Z fdZd Zd	 fd	Z fdZd Zd Z fdZ	 xZ
S )
rP  c                    t                                          |||j        |j        |j                   || _        || _        d S r   )r(  r   r*  r,  r-  r   r   )r   r   r   r   r   
image_datar   s         rr   r   zImageDataRegion.__init__  sI    #3#1#2	4 	4 	4 rq   c                    | j         | j        |                     | j        | j                  | j        | j        | j        | j        | j        | j        | j	        d
S )N)
r   r   r,  r*  r+  r-  r%  r.  r   r   )
r   r   r0  r*  r-  r+  r%  r.  r   r   r   s    rr   r1  zImageDataRegion.__getstate__  sY    Zk!^^D,@$BUVV#3#3"1Z!/
 
 	
rq   Nc                   t          | j                  | j        z  t          | j                  | j        | j        z   z  |                                  |                     | j        t          | j                            t          | j                  fdt          dt                              D             }fd|| j	        | j	        | j
        z            D             }d                    |          | _        | j        t          | j                  z  | _        d | _        d| _        d| _	        |p| j        }|p| j        }t                                          ||          S )Nc                *    g | ]}||z            S rp   rp   rm  s     rr   rp  z-ImageDataRegion.get_bytes.<locals>.<listcomp>  s&    NNN!Qq9}_%NNNrq   r   c                $    g | ]}|         S rp   rp   )rn  rowx1x2s     rr   rp  z-ImageDataRegion.get_bytes.<locals>.<listcomp>  s!    HHHsBrE
HHHrq   rq   )r   r*  r   r   r}  r8  r]  r-  rC  r   r   r  r,  r6  r+  r(  r0  )	r   r$  r%  r  r   ro  r  r  r   s	       @@@@rr   r0  zImageDataRegion.get_bytes  sT   %&&/%&&$&4:*=>}}T13t7J3K3KLL+,,	NNNNNuQD		9/M/MNNNHHHHHd46$&4;2F+F&GHHH XXd^^"j3t/C+D+DD $)T),,ww  e,,,rq   c                j    d| _         d| _        t                                          |||           d S Nr   )r   r   r(  r=  )r   r$  r%  r   r   s       rr   r=  zImageDataRegion.set_bytes  s3    #ud+++++rq   c                n    t          t          | j                   t          t          | j                   d S r   )rO   r0   r   r1   r   r   s    rr   r`  z$ImageDataRegion._apply_region_unpack  s.    +TV444)4622222rq   c                Z    t          t          d           t          t          d           d S r  )rO   r0   r1   r   s    rr   ra  z&ImageDataRegion._default_region_unpack  s*    +Q///)1-----rq   c                t    || j         z  }|| j        z  }t                                          ||||          S r   )r   r   r(  r   r   r   r   r   r   r   s        rr   r   zImageDataRegion.get_region  s7    	TV	TVww!!!Qv666rq   r  )rm   rn   ro   r   r1  r0  r=  r`  ra  r   r  r  s   @rr   rP  rP    s            
 
 
- - - - - -&, , , , ,
3 3 3. . .7 7 7 7 7 7 7 7 7rq   rP  c                       e Zd ZdZdZdZ	 	 d$d% fdZd&dZd'dZd(d)dZ	d)dZ
d*d+dZd,dZd-d Zd.d/d"Zd/d#Z xZS )0CompressedImageDataz;Compressed image data suitable for direct uploading to GPU.Nr   r   r   	gl_formatr   r   	extensionr   r}   1Callable[[bytes, int, int], AbstractImage] | Noner   r   c                    t                                          ||           || _        || _        || _        || _        g | _        dS )a  Construct a CompressedImageData with the given compressed data.

        Args:
            width:
                The width of the image.
            height:
                The height of the image.
            gl_format:
                GL constant giving the format of the compressed data.
                For example: ``GL_COMPRESSED_RGBA_S3TC_DXT5_EXT``.
            data:
                An array of bytes containing the compressed image data.
            extension:
                If specified, gives the name of a GL extension to check for
                before creating a texture.
            decoder:
                An optional fallback function used to decode the compressed data.
                This function is called if the required extension is not present.
        N)r(  r   r   r  r  r}   mipmap_data)r   r   r   r  r   r  r}   r   s          rr   r   zCompressedImageData.__init__  sI    , 	'''	""rq   r   c                r    | xj         dg|t          | j                   z
  z  z  c_         || j         |dz
  <   dS )a  Set compressed image data for a mipmap level.

        Supplied data gives a compressed image for the given mipmap level.
        This image data must be in the same format as was used in the
        constructor. The image data must also be of the correct dimensions for
        the level (i.e., width >> level, height >> level); but this is not checked.
        If *any* mipmap levels are specified, they are used; otherwise, mipmaps for
        ``mipmapped_texture`` are generated automatically.
        Nr[   )r  r   )r   r   r   s      rr   set_mipmap_dataz#CompressedImageData.set_mipmap_data  sF     	TFec$2B.C.C&CDD&*###rq   r   c                F    | j         d u pt          j        | j                   S r   )r  rW   have_extensionr   s    rr   _have_extensionz#CompressedImageData._have_extension  s!    ~%O)?)O)OOrq   Fr   c                p   | j         r| j         S |rt          d          t                              | j        | j        t          d           }| j        s| j        r| j        |_        | j        |_        t          |j
        |j                   t          |j
        t          |j                   t          |j
        t          |j                   |                                 rGt%          |j
        |j        | j        | j        | j        dt+          | j                  | j                   nz| j        r_|                     | j        | j        | j                  }|                                }|j        | j        k    sJ |j        | j        k    sJ nd|  }t          |          t3                       || _         |S )Nz+Compressed texture rectangles not supportedr   z8No extension or fallback decoder is available to decode )r6  rl   r   r   r   r   r(   r   r   r:   r   rO  rT   r,   
min_filterr+   
mag_filterr  r=   r   r  r   r   r}   r   rF   )r   r   r  r  rD  s        rr   r   zCompressedImageData.get_texture  s     	)(( 	P !NOOO..T[-NN= 	-DM 	-#}G#}Ggngj111(=w?QRRR(=w?QRRR!! 	&"7>7=#'>#':t{A#&ty>>49> > > > \ 	&LLDJDDE''))G=DJ....>T[00000STSSC %%%			 'rq   c                "   | j         r| j         S |                                 s|                                 S t                              | j        | j        t          d           }| j        s| j	        r| j        |_        | j	        |_	        t          |j        |j                   t          |j        t          t                     | j        st#          |j                   t%          |j        |j        | j        | j        | j        dt+          | j                  | j                   | j        | j        }}d}| j        D ]>}|dz  }|dz  }|dz  }t%          |j        || j        ||dt+          |          |           ?t/                       || _         |S )Nr   r[   )r;  r  r   r   r   r   r   r(   r   r   r:   r   rO  rT   r,   r   r  rK   r=   r   r  r   r   rF   )r   r  r   r   r   r   s         rr   r   z)CompressedImageData.get_mipmapped_texture  s   ' 	0//##%% 	& ##%%%..T[-NN= 	-DM 	-#}G#}Ggngj111(=?VWWW 	-W^,,,w~w}#~#z4;"49~~ty	: 	: 	:
 
DKv$ 	m 	mDaKEqLFQJE"7>5$.%QWYZ\_`d\e\egkllll			'.$rq   r   r   r   r   r   c                   |                                  st          | j         d|            |t          k    rQt	          |||| j        z
  || j        z
  || j        | j        d| j	        t          | j                  | j                   d S t          |||| j        z
  || j        z
  | j        | j        | j	        t          | j                  | j        	  	         d S )Nz is required to decode r[   )r  rl   r  r*   r?   r   r   r   r   r  r   r   r>   r   s          rr   r   z#CompressedImageData.blit_to_texture@  s    ##%% 	S DN!Q!Q4!Q!QRRR ]""%fe&'$-&7T]9JA&*j$+q&*n&)$)nndi	A A A A A &fe&'$-&7T]9J&*j$+&*n&)$)nndi	A A A A Arq   c                    | S r   rp   r   s    rr   r   z"CompressedImageData.get_image_dataS  r   rq   r   c                &    t          d|            rU  r   r   s        rr   r   zCompressedImageData.get_regionV  rW  rq   r   c                X    |                                                      |||           d S r   rS  r   s       rr   r   zCompressedImageData.blitY  s,    1a(((((rq   c                &    t          d|            rU  r   r   s        rr   r   zCompressedImageData.blit_into\  rW  rq   r  )r   r   r   r   r  r   r   r   r  r   r}   r  r   r   )r   r   r   r   r   r   )r   r   r   r   r   r  )r   r  r   r   r   )rm   rn   ro   rv   r6  r;  r   r  r  r   r   r   r   r   r   r   r  r  s   @rr   r  r    s6       EE" *.NR      :+ + + +P P P P! ! ! ! !F& & & &PA A A A A&   A A A A) ) ) ) )A A A A A A A Arq   r  c                  n    e Zd ZU dZded<   	 dZ	 dZded<   	 ded	<   	 d
Z	 dZded<   	 dZ	dZ
ded<   dZded<   dZded<   eZ	 eZ	 	 d@dA fdZdBdZd ZdCdDdZdddeefdEd&Zeeedded'fdFd*            ZdCdGd,ZdHdId.ZdJdKd/ZdLd0ZdMd3Z dNdOd4Z!dPd5Z"	 	 dQdRd:Z#d; Z$e%dSd=            Z&dTd?Z' xZ(S )Ur   zAn image loaded into GPU memory

    Typically, you will get an instance of Texture by accessing calling
    the ``get_texture()`` method of any AbstractImage class (such as ImageData).
    TextureRegionregion_class)r   r   r   r[   r   r   r[   r[   r   r   r[   r   r   r[   r   r\  ztuple[int, int, int, int]tex_coords_orderr   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r[   r   r   r   Nr   r   tex_idr  r&  r  r   r   c                    t                                          ||           || _        || _        |p| j        | _        |p| j        | _        t          j	        j
        | _        d S r   )r(  r   r   rO  default_min_filterr  default_mag_filterr  pygletglcurrent_context_context)r   r   r   r   r  r  r  r   s          rr   r   zTexture.__init__  sY    '''$?(?$?(?	1rq   c                X    t          dt          | j                             d| _        dS )zDelete this texture and the memory it occupies.

        Textures are invalid after deletion, and may no longer be used.
        r[   N)rC   r6   rO  r   s    rr   deletezTexture.delete  s'    
 	F47OO,,,rq   c                    | j         A	 | j                            | j                    d | _         d S # t          t          f$ r Y d S w xY wd S r   )rO  r  delete_textureAttributeErrorImportErrorr   s    rr   __del__zTexture.__del__  sa    7,,TW555"K0   	 s   &1 AAtexture_unitc                h    t          t          |z              t          | j        | j                   dS )z*Bind to a specific Texture Unit by number.N)r7   r'   r:   r   rO  )r   r  s     rr   bindzTexture.bind  s/    l2333dk47+++++rq   Funitlayeredr   layeraccessr$  c           	     :    t          || j        |||||           dS )zBind as an ImageTexture for use with a :py:class:`~pyglet.shader.ComputeShaderProgram`.

        .. note:: OpenGL 4.3, or 4.2 with the GL_ARB_compute_shader extension is required.
        N)r9   rO  )r   r  r   r  r  r  r$  s          rr   bind_image_texturezTexture.bind_image_texture  s&     	4%%MMMMMrq   Tr   rJ  c	                   |p| j         }|p| j        }t                      }	t          dt	          |	                     t          ||	j                   t          |t          |           t          |t          |           |E|rt          ||z  dz  z              nd}
t          |d|||d|t          |
	  	         t                        | ||||	j        ||          S )a=  Create a Texture

        Create a Texture with the specified dimensions, target and format.
        On return, the texture will be bound.

        Args:
            width:
                Width of texture in pixels.
            height:
                Height of texture in pixels.
            target:
                GL constant giving texture target to use, typically ``GL_TEXTURE_2D``.
            internalformat:
                GL constant giving internal format of texture; for example, ``GL_RGBA``.
                The internal format decides how the texture data will be stored internally.
                If ``None``, the texture will be created but not initialized.
            min_filter:
                The minifaction filter used for this texture, commonly ``GL_LINEAR`` or ``GL_NEAREST``
            mag_filter:
                The magnification filter used for this texture, commonly ``GL_LINEAR`` or ``GL_NEAREST``
            fmt:
                GL constant giving format of texture; for example, ``GL_RGBA``.
                The format specifies what format the pixel data we're expecting to write
                to the texture and should ideally be the same as for internal format.
            blank_data:
                If True, initialize the texture data with all zeros. If False, do not pass initial data.
        r[   Nr   r   )r  r  r6   rI   r   r:   r   rT   r,   r+   r5   rR   r2   rF   )rG  r   r   r   r   r  r  r$  rJ  r  blanks              rr   r   zTexture.create  s    >  93#9
93#9
av'''ffl+++ 5zBBB 5zBBB%:DNW 23666$E')      IIIs5&&&,
JOOOrq   r   c           	        t          | j        | j                   d}t          }t	          | j        | j        z  | j        z  t          |          z  z              }t          j
        j                                                                        dk    rt                      }t          d|           t!          t"          |j                   t'          t(          d           t+          t"                     t-          t"          t.          t0          | j        | j                   t5          dd| j        | j        |t6          |           t!          t"          d           t9          d|           n7t'          t(          d           t;          | j        | j        |t6          |           t=          | j        | j        ||          }| j        dk    r*|                    d|| j        z  | j        | j                  }|S )a  Get the image data of this texture.

        Bind the texture, and read the pixel data back from the GPU.
        This can be a somewhat costly operation.

        Modifying the returned ImageData object has no effect on the
        texture itself. Uploading ImageData back to the GPU/texture
        can be done with the :py:meth:`~Texture.blit_into` method.

        Args:
            z:
                For 3D textures, the image slice to retrieve.
        r   glesr[   r   ) r:   r   rO  r"   r5   r   r   imagesr   r  r  r  get_infoget_opengl_apir
   rH   r8   r   r   rO   r   r<   rG   r   r(   r   rQ   r2   rB   rN   r   r   )r   r   r$  r  buffbor   s          rr   r   zTexture.get_image_data  s    	dk47+++ 	$*t{2T[@3s88KLOO 9$--//>>@@FJJ((Ca%%%nci888+Q///$^444">3GX\X_aeaklllAtz4;	CSUXYYYna000 C((((+Q///$+tz9>NPSTTTT[#s;;;????1a$+otz4;OODrq   r   c                    | S r   rp   r   s     rr   r   zTexture.get_texture  r   rq   c                8   || j         z
  }|| j        z
  }||du r| j        p|z   }||du r| j        p|z   }	||||||||	|||	|f}
g d}t	          t
                     t          | j        | j                   t                      }t          d|           t          |           t          j                                        }|                                 |j        d         }|j        d         }t#          d|d         |d         t$          dd          }t'          d	|j        z            } |j        t-          |
          z  |
 }|                    |           |                                 |                    |j                   t#          d|d         |d         t$          dd          }t'          d	|j        z            } |j        t-          | j                  z  | j         }|                    |           |                                 |                    |j                   t9          t-          |          z  | }t'          t;          |                    }|                    |           |                                 t?          t@          t-          |          tB          d
           tE                       |#                                 |$                                 |$                                 t          d
           tK          d|           dS )a  Blit the texture to the screen.

        This is a costly operation, and should not be used for performance critical
        code. Blitting a texture requires binding it, setting up throwaway buffers,
        creating a VAO, uploading attribute data, and then making a single draw call.
        This is quite wasteful and slow, so blitting should not be used for more than
        a few images. This method is provided to assist with debugging, but not intended
        for drawing of multiple images.

        Instead, consider creating a :py:class:`~pyglet.sprite.Sprite` with the Texture,
        and drawing it as part of a larger :py:class:`~pyglet.graphics.Batch`.
        Nr   r[   r   r   r   r\  r[   position
tex_coordslocationcountFr   r   )&r   r   r   r   r7   r'   r:   r   rO  r6   rJ   r;   r  graphicsget_default_blit_shaderuse
attributesrX   r   rY   stridec_typer   rA  enableset_pointerptrr  r	   r   bind_to_index_bufferrE   r-   r2   rF   stopr  rD   )r   r   r   r   r   r   r  y1r  y2r  indicesvao_idprogram	pos_attrs	tex_attrsposition_attributeposition_bufferr   texcoord_attributetexcoord_bufferindex_arrayindex_buffers                          rr   r   zTexture.blit  s    5D=/TZ8596T>1dk;V<r1b"aRBA=$$$$$$dk47+++ !V$$$&!!! /99;;&z2	&|4	 'z9Z3H)T[J\^fhmotuu&q+=+D'DEE9")CMM9HE  &&&!!###&&':;;; '|Yz5JIV]L^`hjoqvww&q+=+D'DEE@")C,@,@@4?S  &&&!!###&&':;;; W-8#F;$7$788k***))+++|S\\3CQGGG			 	      !Q'''''rq   c                (    t          d|  d          NrV  .r   r   s    rr   r   zTexture.get_mipmapped_texture^      !"@"@"@"@AAArq   r   r   c                    t          | j        | j                   |                    | j        | j        |||           d S r   )r:   r   rO  r   r   r   s        rr   r   zTexture.blit_intoa  s<    dk47+++t{DJ1a@@@@@rq   c                &    t          d|            rU  r   r   s          rr   r   zTexture.blit_to_texturee  rW  rq   c                6    |                      ||d|||           S r  )r  r   s        rr   r   zTexture.get_regionh  s       Aq%>>>rq   flip_xflip_yrotateLiteral[0, 90, 180, 270, 360]c                   |                      dd| j        | j                  }d\  }}}}| j        |_        | j        |_        |r||||f\  }}}}| j        | j        z
  |_        |r||||f\  }}}}| j        | j        z
  |_        |dz  }|dk     r|dz  }|dk    rn|dk    r-||||f\  }}}}|j        |j        |j        z
  c|_        |_        n||dk    r4||||f\  }}}}|j        |j        z
  |_        |j        |j        z
  |_        nB|dk    r-||||f\  }}}}|j        |j        z
  |j        c|_        |_        nt          d          |dv r|j        |j        c|_        |_        |                    ||||           |S )	a  Create a copy of this image applying a simple transformation.

        The transformation is applied to the texture coordinates only;
        :py:meth:`~pyglet.image.AbstractImage.get_image_data` will return the
        untransformed data. The transformation is applied around the anchor point.

        Args:
            flip_x:
                If True, the returned image will be flipped horizontally.
            flip_y:
                If True, the returned image will be flipped vertically.
            rotate:
                Degrees of clockwise rotation of the returned image.  Only
                90-degree increments are supported.
        r   r  ih  Z        z'Only 90 degree rotations are supported.)r  r  )r   r   r   r   r   rl   _set_tex_coords_order)	r   r  r  r  	transformblbrtrtls	            rr   get_transformzTexture.get_transformk  s   " OOAq$*dkBB	#BB!]	!]	 	<R^NBB!%dm!;I 	=R^NBB!%t}!<I#A::cMFQ;;r\\R^NBB5>5G[d[mIm2I	 2 2s]]R^NBB!*93E!EI!*!1I4F!FIs]]R^NBB5>5E	HZ5Z\e\n2I	 2 2 !JKKKY090@)/-IOY-''BB777rq   c                   | j         d d         | j         dd         | j         dd         | j         dd          f}||         ||         z   ||         z   ||         z   | _         | j        }||         ||         ||         ||         f| _        d S )Nr\     	   )r  r  )r   r  r   r!  r"  r  orders          rr   r  zTexture._set_tex_coords_order  s    obqb)oac*oac*oabb)+
 %R.:b>9JrNJZXZ^[%!&rE"IuRy%) Lrq   !tuple[float, float, float, float]c                L    | j         }|d         |d         |d         |d         fS )zETuple containing the left, bottom, right, top 2D texture coordinates.r   r[   r\     )r  )r   r  s     rr   uvz
Texture.uv  s*     _
!}jmZ]JqMIIrq   rz   c                P    | j         j         d| j         d| j         d| j         dS )N(id=, size=r   r   )r   rm   rO  r   r   r   s    rr   r   zTexture.__repr__  s4    .)ZZtwZZtzZZDKZZZZrq   r  )r   r   r   r   r   r   r  r   r  r&  r  r&  r   r   r  r   )r  r   r   r   )r  r   r   r   r  r   r  r   r  r   r$  r   )r   r   r   r   r   r   r   r&  r  r&  r  r&  r$  r   rJ  r   r   r   )r   r   r   r   r   r   r  r  r   )r   r   r   r   r   r   r   r   r   r  
r   r   r   r   r   r   r   r   r   r  )FFr   )r  r   r  r   r  r  r   r  )r   r(  r   ))rm   rn   ro   rv   r   r  r  colorsr   r  r   r   r   r   r  r  r   r  r  r  r   r#   r  classmethodr(   r$   r"   r   r   r   r   r   r   r   r   r#  r  r  r+  r   r  r  s   @rr   r   r   `  s            6J
 3?>>>> KKK:F:ENNNN+FAJJJJAJJJJAJJJJ"
 # JN2 2 2 2 2 2 2     , , , , ,
 :;E()S]N N N N N ;H,4tnr!d2P 2P 2P 2P [2Ph) ) ) ) )V    A( A( A( A( A(FB B B BA A A AA A A A A? ? ? ? BG>?/ / / / /bM M M J J J XJ
[ [ [ [ [ [ [ [rq   r   c                  P     e Zd ZdZd fd
Zd ZddZddZddZddZ	d Z
 xZS )r  zNA rectangular region of a texture, presented as if it were a separate texture.r   r   r   r   r   r   ownerr   c                   t                                          |||j        |j                   || _        || _        || _        || _        || _        || _	        |j
        d         }|j
        d         }|j
        d         }	|j
        d         }
|	|z
  }|
|z
  }||j        z  |z  |z   }||j        z  |z  |z   }||z   |j        z  |z  |z   }||z   |j        z  |z  |z   }||j        z  |j
        d         z   }||||||||||||f| _
        d S )Nr   r[   r\  r*  r   )r(  r   r   rO  r   r   r   _width_heightr3  r  r   r   r  )r   r   r   r   r   r   r3  owner_u1owner_v1owner_u2owner_v2scale_uscale_vu1v1u2v2rt  r   s                     rr   r   zTextureRegion.__init__  s3   eh???
#A&#A&#A&#A&X%X%_w&1'(2%i5;&08;&jEL(72X=u/22r1b"aRBAFrq   c                    | j                             | j                  }|                    | j        | j        | j        | j                  S r   )r3  r   r   r   r   r   r   r   r   r  s     rr   r   zTextureRegion.get_image_data  s;    Z..tv66
$$TVTVTZMMMrq   r   c                    || j         z  }|| j        z  }|                     ||| j        ||| j                  } |j        | j          |S r   )r   r   r  r   r3  r  r  r   r   r   r   r   regions         rr   r   zTextureRegion.get_region  sQ    	TV	TV""1a
KK$$d&;<<rq   r   r   r   c                    |j         | j        k    r|j        | j        k    sJ | d|              | j                            ||| j        z   || j        z   || j        z              d S )Nz is larger than )	r   r5  r   r6  r3  r   r   r   r   r   s        rr   r   zTextureRegion.blit_into  sq    |t{**v}/L/L/LQWNoNoimNoNo/L/LL
VQZTVQZHHHHHrq   rz   c                    | j         j         d| j         d| j         d| j         d| j        j         d| j        j         dS )Nr-  r.  r   z, owner=r   )r   rm   rO  r   r   r3  r   s    rr   r   zTextureRegion.__repr__  s|    >* c c c cc c&*kc c;?:;Kc cNRjN_c c c 	drq   c                    dS )zNDeleting a TextureRegion has no effect. Operate on the owning texture instead.Nrp   r   s    rr   r  zTextureRegion.delete  r   rq   c                    d S r   rp   r   s    rr   r  zTextureRegion.__del__  s    rq   )r   r   r   r   r   r   r   r   r   r   r3  r   r/  )
r   r   r   r   r   r   r   r   r   r   r   r  )rm   rn   ro   rv   r   r   r   r   r   r  r  r  r  s   @rr   r  r    s        XXG G G G G G,N N N   I I I Id d d d] ] ] ]      rq   r  c                      e Zd ZU dZdZded<   dZded<   ded<   eedfd	            Z	eefd
            Z
d Zd Zd ZddZdS )	Texture3DzA texture with more than one image slice.

    Use the :py:meth:`create_for_images` or :py:meth:`create_for_image_grid`
    classmethod to construct a Texture3D.
    r   r   
item_widthitem_heighttupleitemsTc                  
 |d         j         |d         j        
t          
fd|D                       st          d          |                     
t
          d           }|d         j        s|d         j        r$|d         j        |_        |d         j        |_        t          |          |_	        |r)t          |j         |j        z  |j	        z  dz  z              nd }t          |j        |j                   t          |j        |j        ||j         |j        |j	        d|t           |
  
         g }t#          |          D ]b\  }}|                     dd|
|          }	|                    |	           |                    |j        |j        |j        |j        |           ct+                       ||_        |_        
|_        |S )Nr   c              3  D   K   | ]}|j         k    o
|j        k    V  d S r   r   )rn  imgrM  rL  s     rr   	<genexpr>z.Texture3D.create_for_images.<locals>.<genexpr>  s6      [[S39
*Hsz[/H[[[[[[rq   z#Images do not have same dimensions.r   )r   )r   r   allrl   r   r*   r   r   r   r  r5   r:   r   rO  rS   r   r2   	enumerater  appendr   rF   rO  rL  rM  )rG  r  r   rJ  r  r  rO  rE  r  r
  rM  rL  s             @@rr   create_for_imageszTexture3D.create_for_images  s   AY_
Qi&[[[[[TZ[[[[[ 	H !FGGG**ZmTJJ!9 	2!3 	2%ay1G%ay1GVWakGMGN:W^KaOPSSSgkgngj111W^W]#]GNGNA#%5		 	 	 !&)) 	f 	fHAu##Aq!ZgNNDLL!!'.'-QVQ_cd!eeee			')rq   c                >    |                      |d d          |          S r   )rW  )rG  gridr   s      rr   create_for_image_gridzTexture3D.create_for_image_grid  s     $$T!!!Wn===rq   c                *    t          | j                  S r   r   rO  r   s    rr   r  zTexture3D.__len__      4:rq   c                    | j         |         S r   rO  r   r~  s     rr   r  zTexture3D.__getitem__      z%  rq   c                `   t          |          t          u rjt          | j        | j                   t          | |         |          D ]7\  }}|                    | j        | j        |j        |j	        |j
                   8d S |                     ||j        |j	        | |         j
                   d S r   )r  slicer:   r   rO  zipr   r   r   r   r   r   )r   r~  r   r
  r  s        rr   r  zTexture3D.__setitem__  s    ;;%$+tw///"4;66 g ge%%dk4:u~u~_c_effffg g NN5%.%.$u+-PPPPPrq   r   Iterator[TextureRegion]c                *    t          | j                  S r   iterrO  r   s    rr   r  zTexture3D.__iter__'      DJrq   Nr   re  )rm   rn   ro   rv   rL  r   rM  r1  r"   rW  rZ  r  r  r  r  rp   rq   rr   rK  rK    s          
 JKLLL6=$ ! ! ! [!F 8? > > > [>  ! ! !Q Q Q           rq   rK  c                      e Zd ZdZd ZdS )TextureArrayRegionzGA region of a TextureArray, presented as if it were a separate texture.c           
     `    | j         j         d| j         d| j         d| j         d| j         d
S )Nr-  r.  r   z, layer=r   )r   rm   rO  r   r   r   r   s    rr   r   zTextureArrayRegion.__repr__.  s@    .)jjtwjjtzjjDKjjaeagjjjjrq   N)rm   rn   ro   rv   r   rp   rq   rr   rl  rl  +  s.        QQk k k k krq   rl  c                       e Zd Z fdZeedddfdd            ZddZd dZd!dZ	eefd"d            Z
d#dZd$dZd%dZd&dZ xZS )'TextureArrayc                l    t                                          ||||           || _        g | _        d S r   )r(  r   	max_depthrO  )r   r   r   r   r  rq  r   s         rr   r   zTextureArray.__init__3  s3    777"


rq   N   r   r   r   r   r  r&  r  rq  r   c                   |p| j         }|p| j        }t                      }||k    sJ d| d            t                      }t	          dt          |                     t          t          |j                   t          t          t          |           t          t          t          |           t          t          d||||d|t          d
  
         t                        | ||t          |j        |          }	||	_        ||	_        |	S )aH  Create an empty TextureArray.

        You may specify the maximum depth, or layers, the Texture Array should have. This defaults
        to 256, but will be hardware and driver dependent.

        Args:
            width:
                Width of the texture.
            height:
                Height of the texture.
            internalformat:
                GL constant giving the internal format of the texture array; for example, ``GL_RGBA``.
            min_filter:
                The minifaction filter used for this texture array, commonly ``GL_LINEAR`` or ``GL_NEAREST``
            mag_filter:
                The magnification filter used for this texture array, commonly ``GL_LINEAR`` or ``GL_NEAREST``
            max_depth:
                The number of layers in the texture array.

        .. versionadded:: 2.0
        z$TextureArray max_depth supported is r  r[   r   )r  r  r   r6   rI   r   r:   r)   r   rT   r,   r+   rS   r2   rF   r  r  )
rG  r   r   r   r  r  rq  max_depth_limitr  r  s
             rr   r   zTextureArray.create8  s   0  93#9
93#9
688O+++-fTc-f-f-f+++av''')6<888+-BJOOO+-BJOOO(!#FI#%5	 	 	 				#eV%8&,	RR''rq   r  r   r   c                    |j         | j         k    s|j        | j        k    r0t          d|j          d|j         d| j          d| j         d	          d S )NzImage (r   z() exceeds the size of the TextureArray (r   )r   r   rt   )r   r  s     rr   _verify_sizezTextureArray._verify_sizej  s    ;##u|dk'A'A*"%+ " " " "^b^h " ";" " "# # # (B'Arq   pyglet.image.ImageDatarl  c                p   t          | j                  | j        k    rt          d          |                     |           t          | j                  }|                     dd||j        |j        |           }|                     ||j	        |j
        |           | j                            |           |S )NzTextureArray is full.r   )r   rO  rq  rx   rv  r  r   r   r   r   r   rV  )r   r  start_lengthr
  s       rr   addzTextureArray.addp  s    tz??dn,,+,CDDD%   4:  A|U[%,PTUUuenenlKKK
$rq   r  list[TextureArrayRegion]c           	     *   t          | j                  t          |          z   | j        k    rt          d          t	          | j        | j                   t          | j                  }t          |          D ]\  }}|                     |           | 	                    dd||z   |j
        |j        |           }| j                            |           |                    | j        | j        |j        |j        ||z              | j        |d         S )z"Allocates multiple images at once.zHThe amount of images being added exceeds the depth of this TextureArray.r   N)r   rO  rq  rx   r:   r   rO  rU  rv  r  r   r   rV  r   r   r   r   )r   r  ry  rE  r  r
  s         rr   allocatezTextureArray.allocate|  s    tz??S[[(4>99+,vwwwdk47+++4:!&)) 	m 	mHAue$$$$$Q<!+;U[%,X\]]DJd###!!$+tz5>5>[gjk[kllllz,--((rq   c                    |                      |d         j        |d         j        |t          |                    } |j        |d d            |S )Nr   )rq  )r   r   r   r   r}  )rG  rY  r   texture_arrays       rr   rZ  z"TextureArray.create_for_image_grid  sO    

47=$q'..\_`d\e\e
ffQQQ((rq   c                *    t          | j                  S r   r\  r   s    rr   r  zTextureArray.__len__  r]  rq   c                    | j         |         S r   r_  r`  s     rr   r  zTextureArray.__getitem__  ra  rq   c           	     j   t          |          t          u rt          | j        | j                   t          | |         |          D ]\  }}|                     |           |                     dd|j        |j	        |j
        |           }|                    | j        | j        |j        |j        |j                   || j        |j        <   d S |                     |           |                     dd||j	        |j
        |           }|                     ||j        |j        |           || j        |<   d S r  )r  rc  r:   r   rO  rd  rv  r  r   r   r   r   r   r   r   rO  r   )r   r~  r   old_itemr  r
  s         rr   r  zTextureArray.__setitem__  s(   ;;%$+tw///#&tE{E#:#: . .%!!%(((((Axz5;VZ[[%%dk4:u~u~_g_ijjj)-
8:&&	. . e$$$$$Q5%+u|TRRDNN5%.%.%HHH $DJurq   re  c                *    t          | j                  S r   rg  r   s    rr   r  zTextureArray.__iter__  ri  rq   )r   r   r   r   r   r   r  r&  r  r&  rq  r   r   ro  )r  r   r   r   )r  rw  r   rl  )r  r   r   r{  )r   r   r   ro  r  )r   rl  r  rj  )rm   rn   ro   r   r1  r"   r   rv  rz  r}  rZ  r  r  r  r  r  r  s   @rr   ro  ro  2  s           
 CJ(,t^a/ / / / [/b# # # #
 
 
 
) ) ) )  ?F     [
   ! ! ! !% % % %               rq   ro  c                  :    e Zd ZdZddZddZedd            ZdS )TileableTexturez{A texture that can be tiled efficiently.

    Use :py:class:`~pyglet.image.create_for_image` classmethod to construct.
    r   r   r   r   r   c                &    t          d|            )NzCannot get region of )rl   r   s        rr   r   zTileableTexture.get_region  s    ;T;;<<<rq   r   r   r   c                   | j         | j        z  }| j        | j        z  }||| j        z  z   }||| j        z  z   }	||}}
| j        }|||||
z   ||||
z   ||z   ||||z   |f}|||d         |||d         ||	|d         ||	|d         f}t          t                     t          | j        | j	                   t          j                            dt          g dd|fd|f           t          | j        d	           d
S )zBlit this texture tiled over the given area.

        The image will be tiled with the bottom-left corner of the destination
        rectangle aligned with the anchor point of this texture.
        r            r   r  f)r  r  r   N)r   r   r   r   r  r7   r'   r:   r   rO  r  r  draw_indexedr-   )r   r   r   r   r   r   r=  r>  r?  r@  whtverticesr  s                  rr   
blit_tiledzTileableTexture.blit_tiled  s6    ]TZ']T[(%$*$$&4;&&f1Oq!E1aE1q5!q1ua!
 "ad"ad"ad"ae%

 	$$$dk47+++$$Q6H6H6H/2Ho14j0A 	% 	C 	C 	C 	dk1%%%%%rq   r  r   r   c                T    |                                 }|                    |           S r   )r   rL  )rG  r  s     rr   create_for_imagez TileableTexture.create_for_image  s'    $$&&##C(((rq   N)r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   )r  r   r   r   )rm   rn   ro   rv   r   r  r1  r  rp   rq   rr   r  r    sf         
= = = =& & & &< ) ) ) [) ) )rq   r  c                       e Zd ZU dZded<   dZded<   	 	 d3d4 fdZd5d6dZd7dZd8dZ	d9dZ
d:d"Zd;d<d$Zd<d%Zd=d>d)Zd?d*Zd@d,ZdAd/ZdBd0ZdCd2Z xZS )D	ImageGrida  An imaginary grid placed over an image allowing easy access to
    regular regions of that image.

    The grid can be accessed either as a complete image, or as a sequence
    of images. The most useful applications are to access the grid
    as a :py:class:`~pyglet.image.TextureGrid`::

        image_grid = ImageGrid(...)
        texture_grid = image_grid.get_texture_sequence()

    or as a :py:class:`~pyglet.image.Texture3D`::

        image_grid = ImageGrid(...)
        texture_3d = Texture3D.create_for_image_grid(image_grid)

    list_itemsNTextureGrid_texture_gridr   r  r   r  r   columnsrL  r&  rM  row_paddingcolumn_paddingr   r   c                   t                                          |j        |j                   g | _        || _        || _        || _        |p|j        ||dz
  z  z
  |z  | _        |p|j        ||dz
  z  z
  |z  | _	        || _
        || _        dS )a<  Construct a grid for the given image.

        You can specify parameters for the grid, for example setting
        the padding between cells.  Grids are always aligned to the
        bottom-left corner of the image.

        Args:
            image:
                Image over which to construct the grid.
            rows:
                Number of rows in the grid.
            columns:
                Number of columns in the grid.
            item_width:
                Width of each column.  If unspecified, is calculated such
                that the entire image width is used.
            item_height:
                Height of each row.  If unspecified, is calculated such that
                the entire image height is used.
            row_padding:
                Pixels separating adjacent rows.  The padding is only
                inserted between rows, not at the edges of the grid.
            column_padding:
                Pixels separating adjacent columns.  The padding is only
                inserted between columns, not at the edges of the grid.
        r[   N)r(  r   r   r   r  r  r  r  rL  rM  r  r  )	r   r  r  r  rL  rM  r  r  r   s	           rr   r   zImageGrid.__init__  s    8 	el333
	$a~ST7U)UZa(a&[5<+PQ:R+RW[*[&,rq   Fr   r   r   c                6    | j                             |          S r   )r  r   r   s     rr   r   zImageGrid.get_texture  s    z%%i000rq   r   c                4    | j                                         S r   )r  r   r   s    rr   r   zImageGrid.get_image_data  s    z((***rq   c                F    | j         st          |           | _         | j         S r   )r  r  r   s    rr   r  zImageGrid.get_texture_sequence  s&    ! 	3!,T!2!2D!!rq   c                (    t          d|  d          r  r   r   s    rr   r   zImageGrid.get_mipmapped_texture"  r  rq   r   r   r   r   c                (    t          d|  d          r  r   r   s        rr   r   zImageGrid.get_region%  r  rq   r   c                (    t          d|  d          r  r   r   s       rr   r   zImageGrid.blit(  r  rq   c                (    t          d|  d          r  r   r   s        rr   r   zImageGrid.blit_into+  r  rq   r   r   r   c                (    t          d|  d          r  r   r   s          rr   r   zImageGrid.blit_to_texture.  r  rq   c           
     B   | j         sd}t          | j                  D ]}d}t          | j                  D ]S}| j                             | j                            ||| j        | j                             || j        | j	        z   z  }T|| j        | j
        z   z  }d S d S r  )r  rC  r  r  rV  r  r   rL  rM  r  r  )r   r   r  r   cols        rr   _update_itemszImageGrid._update_items1  s    { 	9ATY'' 9 9 .. ? ?CK&&tz'<'<Q4?TXTd'e'efff4+>>>AAT%(888	9 	99 9rq   rP  c                    |                                   t          |          t          u rH|\  }}d|cxk    r| j        k     rn nd|cxk    r| j        k     sn J | j        || j        z  |z            S | j        |         S r  )r  r  rN  r  r  r  )r   r~  r  columns       rr   r  zImageGrid.__getitem__;  s    ;;%KC''''di'''''A,F,F,F,F$,,F,F,F,F,FF;sT\1F:;;;u%%rq   r~  r   c                    t           r   r   )r   r~  r   s      rr   r  zImageGrid.__setitem__D  r  rq   c                     | j         | j        z  S r   )r  r  r   s    rr   r  zImageGrid.__len__G  s    y4<''rq   Iterator[ImageDataRegion]c                R    |                                   t          | j                  S r   )r  rh  r  r   s    rr   r  zImageGrid.__iter__J  s$    DK   rq   )NNr   r   )r  r   r  r   r  r   rL  r&  rM  r&  r  r   r  r   r   r   r   r   r   )r   r  r   r   r   r   r   r  r  )r   rP  )r~  r   r   r   r  )r   r  )rm   rn   ro   rv   r   r  r   r   r   r  r   r   r   r   r   r  r  r  r  r  r  r  s   @rr   r  r    s         " LLL!%M%%%%_c]^$- $- $- $- $- $- $-L1 1 1 1 1+ + + +" " " "
B B B BB B B BB B B B BB B B BB B B B B9 9 9 9& & & &" " " "( ( ( (! ! ! ! ! ! ! !rq   r  c                       e Zd ZU dZded<   ded<   ded<   ded<   ded<   d fdZddZddZddZd dZ	d!dZ
 xZS )"r  a^  A texture containing a regular grid of texture regions.

    To construct, create an :py:class:`~pyglet.image.ImageGrid` first::

        image_grid = ImageGrid(...)
        texture_grid = TextureGrid(image_grid)

    The texture grid can be accessed as a single texture, or as a sequence
    of :py:class:`~pyglet.image.TextureRegion`.  When accessing as a sequence, you can specify
    integer indexes, in which the images are arranged in rows from the
    bottom-left to the top-right::

        # assume the texture_grid is 3x3:
        current_texture = texture_grid[3] # get the middle-left image

    You can also specify tuples in the sequence methods, which are addressed
    as ``row, column``::

        # equivalent to the previous example:
        current_texture = texture_grid[1, 0]

    When using tuples in a slice, the returned sequence is over the
    rectangular region defined by the slice::

        # returns center, center-right, center-top, top-right images in that
        # order:
        images = texture_grid[(1,1):]
        # equivalent to
        images = texture_grid[(1,1):(3,3)]

    r  rO  r   r  r  rL  rM  rY  r  r   r   c           
     t   |                                 }t          |t                    r|j        }n|}t	                                          |j        |j        |j        |j	        |j
        |           g }d}t          |j                  D ]t}d}t          |j                  D ]I}|                    |                     |||j        |j                             ||j        |j        z   z  }J||j        |j        z   z  }u|| _        |j        | _        |j        | _        |j        | _        |j        | _        d S r  )r   
isinstancer  r3  r(  r   r   r   r   r   r   rC  r  r  rV  r   rL  rM  r  r  rO  )
r   rY  r  r3  rO  r   r  r   r  r   s
            rr   r   zTextureGrid.__init__u  s*     ""e]++ 	KEEE%'57EKuUUU## 	5 	5CAT\** ; ;T__Q4?DDTUUVVVT_t':::!D$444AA
I	|/+rq   r  r  c                    | ||f         S r   rp   )r   r  r  s      rr   r_  zTextureGrid.get  s    S&M""rq   r~  int | tuple[int, int] | slice#TextureRegion | list[TextureRegion]c                   t          |          t          u rt          |j                  t          ur(t          |j                  t          ur| j        |         S d}d}| j        }| j        }t          |j                  t          u r|j        \  }}n9t          |j                  t          u r|j        | j        z  }|j        | j        z  }d|cxk    r| j        k     rn nd|cxk    r| j        k     sn J t          |j                  t          u r|j        \  }}n9t          |j                  t          u r|j        | j        z  }|j        | j        z  }d|cxk    r| j        k    rn nd|cxk    r| j        k    sn J g }|| j        z  }t          ||          D ]$}|| j        ||z   ||z            z  }|| j        z  }%|S t          |          t          u rH|\  }}	d|cxk    r| j        k     rn nd|	cxk    r| j        k     sn J | j        || j        z  |	z            S t          |          t          u r| j        |         S d S r  )
r  rc  startrN  r   rO  r  r  r   rC  )
r   r~  r   col1r   col2resultrE  r  r  s
             rr   r  zTextureGrid.__getitem__  s   ;;%EK  --$uz2B2B%2O2Oz%((y|$$--!&JD$$%+&&#-- ;$,6D ;5DD,,,,49,,,,,d1I1I1I1IT\1I1I1I1I1II
##u,,!&JD$$%*%%,, :5D :4DD----DI-----!t2K2K2K2Kt|2K2K2K2K2KK4<' t,, & &CdjT!d():;;F%AAE{{e###VC++++$)+++++V0J0J0J0Jdl0J0J0J0J0JJz#"4v"=>>e##z%(( $#rq   int | slicer   'AbstractImage | Sequence[AbstractImage]c                   t          |          t          u rot          | |         |          D ]V\  }}|j        | j        k    s|j        | j        k    rt          d          |                    ||j	        |j
        d           Wd S |}|j        | j        k    s|j        | j        k    rt          d          |                    | |         |j	        |j
        d           d S )NzImage has incorrect dimensionsr   )r  rc  rd  r   rL  r   rM  rl   r   r   r   )r   r~  r   rE  r  s        rr   r  zTextureGrid.__setitem__  s    ;;%!$T%[%!8!8 K K;$/11U\TEU5U5U()IJJJJJJJK K
 E{do--AQ1Q1Q$%EFFFOODKKKKKKrq   c                *    t          | j                  S r   r\  r   s    rr   r  zTextureGrid.__len__  r]  rq   re  c                *    t          | j                  S r   rg  r   s    rr   r  zTextureGrid.__iter__  ri  rq   )rY  r  r   r   )r  r   r  r   )r~  r  r   r  )r~  r  r   r  r  rj  )rm   rn   ro   rv   r   r   r_  r  r  r  r  r  r  s   @rr   r  r  O  s          > KKKIIILLLOOO, , , , , ,0# # # ##) #) #) #)J
L 
L 
L 
L                  rq   r  c                  H    e Zd ZdZd Zedd            ZddZdd	ZddZ	dS )BufferManagerzManages the set of framebuffers for a context.

    Use :py:func:`~pyglet.image.get_buffer_manager` to obtain the instance
    of this class for the current context.
    c                >    d | _         d | _        d | _        g | _        d S r   )_color_buffer_depth_bufferfree_stencil_bits_refsr   s    rr   r   zBufferManager.__init__  s%    !!!%


rq   r   rN  c                 l    t          dz              } t          t          |            t          |           S )zFGet the current OpenGL viewport dimensions (left, bottom, right, top).r   )r4   rM   r3   rN  )viewports    rr   get_viewportzBufferManager.get_viewport  s-     AI==k8,,,Xrq   ColorBufferImagec                    |                                  }|d         }|d         }| j        r || j        j        k    s|| j        j        k    rt	          | | _        | j        S )zGet the color buffer.r   r\  )r  r  r   r   r  r   r  viewport_widthviewport_heights       rr   get_color_bufferzBufferManager.get_color_buffer  h    $$&&!!"1+" 	=$"4":::4#5#<<<!18!<D!!rq   DepthBufferImagec                    |                                  }|d         }|d         }| j        r || j        j        k    s|| j        j        k    rt	          | | _        | j        S )zGet the depth buffer.r   r\  )r  r  r   r   r  r  s       rr   get_depth_bufferzBufferManager.get_depth_buffer  r  rq   BufferImageMaskc                   | j         g	 t                      }t          t          t          t
          |           t          t          |j                            | _         n# t          $ r Y nw xY w| j         st          d          | j                             d          |                                 \  }}}}t          ||||          }|_        | ffd	}| j                            t#          j        ||                     |S )zGet a free bitmask buffer.

        A bitmask buffer is a buffer referencing a single bit in the stencil
        buffer.  If no bits are free, ``ImageException`` is raised.  Bits are
        released when the bitmask buffer is garbage collected.
        Nz#No free stencil bits are available.r   c                >    |j                             d           d S r  )r  insert)refr3  stencil_bits     rr   release_bufferz5BufferManager.get_buffer_mask.<locals>.release_buffer  s"    #**1k:::::rq   )r  r4   rL   r   r%   r   r  rC  r   r   rl   popr  r  r  r  rV  weakrefr  )	r   stencil_bitsr   r   r   r   bufimgr  r  s	           @rr   get_buffer_maskzBufferManager.get_buffer_mask  s/    !)$ww56I6@6\6BD D D *.eL4F.G.G)H)H&&    % 	H !FGGG,0033"//111eV Auf55(&* 	; 	; 	; 	; 	; 	; 	
'+fn==>>>s   AA   
A-,A-N)r   rN  )r   r  )r   r  )r   r  )
rm   rn   ro   rv   r   r  r  r  r  r  rp   rq   rr   r  r    s               \	" 	" 	" 	"	" 	" 	" 	"     rq   r  c                 x    t           j        j        } t          | d          st	                      | _        | j        S )z6Get the buffer manager for the current OpenGL context.image_buffer_manager)r  r  r  hasattrr  r  )contexts    rr   get_buffer_managerr    s4    i'G7233 7'4$''rq   c                  j     e Zd ZdZeZdZdZdZ fdZ	d Z
d Zdd
ZddZdddZddZdddZ xZS )BufferImagez"An abstract "default" framebuffer.r   rk  Nc                    t                                          ||           || _        || _        || _        || _        d S r   )r(  r   r   r   r   r   r  s        rr   r   zBufferImage.__init__.  s=    '''
rq   c           	        t          t          | j                  | j        z  | j        z  z              }| j        }| j        }| j        r|| j        j        z  }|| j        j        z  }t          | j	                   t          t          d           t          ||| j        | j        | j        t          |           t          | j        | j        | j        |          S )Nr[   )r5   r   r4  r   r   r   r   r3  rP   	gl_bufferrO   r   rQ   r  r2   r   )r   r  r   r   s       rr   r   zBufferImage.get_image_data5  s    #dk**TZ7$+EFIIFF: 	AAT^$$$'+++Q4:t{DNDTVYZZZT[$+sCCCrq   c                    | j         r-| j                             || j        z   || j        z   ||          S |                     || j        z   || j        z   ||          }| j        |_        | |_         |S r   )r3  r   r   r   r   r  rD  s         rr   r   zBufferImage.get_regionC  sn    : 	P:((TVQZOOODF
AJvFF>rq   r   r   c                &    t          d|            rU  r   r   s    rr   r   zBufferImage.get_textureL  rW  rq   c                &    t          d|            rU  r   r   s    rr   r   z!BufferImage.get_mipmapped_textureO  rW  rq   r   r   r   r   r   c                &    t          d|            rU  r   r   s       rr   r   zBufferImage.blitR  rW  rq   c                &    t          d|            rU  r   r   s        rr   r   zBufferImage.blit_intoU  rW  rq   r   r   r   c                &    t          d|            rU  r   r   s          rr   r   zBufferImage.blit_to_textureX  rW  rq   r   r   r   r   r  )rm   rn   ro   rv   r   r  r  r4  r3  r   r   r   r   r   r   r   r   r  r  s   @rr   r  r     s       ,, I I FE    D D D  A A A AA A A AA A A A AA A A AA A A A A A A A Arq   r  c                  *    e Zd ZdZeZdZd ZdddZdS )r  znA color framebuffer.

    This class is used to wrap the primary color buffer (i.e., the back
    buffer)
    r   c                    t                               | j        | j        t          t
          d          }|                     |j        |j        | j	        | j
        d           |S )NFrI  r   )r   r   r   r   r(   r"   r   r   r   r   r   )r   r  s     rr   r   zColorBufferImage.get_texturee  sL    ..T[-]b.ccW^W]DM4=Z[\\\rq   Nr   r   r   r   r   r   r   c           
         t          | j                   t          |||| j        z
  || j        z
  | j        | j        | j        | j                   d S r   	rP   r  r@   r   r   r   r   r   r   r   s          rr   r   z ColorBufferImage.blit_to_texturej  N    T^$$$FE1T]?AdmOTVUYU[]a]gimituuuuurq   r   r  )	rm   rn   ro   rv   r"   r  r4  r   r   rp   rq   rr   r  r  \  sY         
 IF  
v v v v v v vrq   r  c                  *    e Zd ZdZeZdZd ZdddZdS )r  zThe depth buffer.
    rY  c                R    |                                  }|                                S r   )r   r   rB  s     rr   r   zDepthBufferImage.get_texturev  s%    ((**
%%'''rq   Nr   r   r   r   r   r   r   c           
         t          | j                   t          |||| j        z
  || j        z
  | j        | j        | j        | j                   d S r   r  r   s          rr   r   z DepthBufferImage.blit_to_texturez  r  rq   r   r  )	rm   rn   ro   rv   r   r  r4  r   r   rp   rq   rr   r  r  p  sW         "IF( ( (v v v v v v vrq   r  c                      e Zd ZdZeZdZdS )r  z#A single bit of the stencil buffer.rY  N)rm   rn   ro   rv   r&   r  r4  rp   rq   rr   r  r    s        -- IFFFrq   r  r  )ry   rz   r{   r|   r}   r~   r   r   )ry   rz   r{   r|   r}   r~   r   r]   r   )r   r   r   r   r   r   r   r   r  )r   r   r   r   )r   r  )rv   
__future__r   r  r  abcr   r   typingr   ctypesr   r   r	   r
   r   r  	pyglet.glr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   pyglet.graphics.shaderrX   pyglet.graphics.vertexbufferrY   pyglet.utilrZ   rk  r\   	animationr]   r^   bufferr_   r`   ra   codecsrb   _add_default_codecsrc   r   rd   re   rf   rg   collections.abcrh   ri   rj   	Exceptionrl   rt   rx   r   r   r   r   r   r   r   r   r   r   r   r   r"  r   rP  r  r   r  r  rK  rl  ro  r  r  r  r  r  r  r  r  r  rp   rq   rr   <module>r     s_
  ^ ^~ # " " " " " 				  # # # # # # # #             8 8 8 8 8 8 8 8 8 8 8 8 8 8                                                                                                                                                             - , , , , , 5 5 5 5 5 5             0 0 0 0 0 0 0 0 H H H H H H H H H H = = = = = = / / / / / / 3<<<<<<<<<<<<((((((22222222	 	 	 	 	Y 	 	 	T T T T T~ T T TR R R R R R R R2 2 2 2 20< < < < <(/ / / / /*         H H H H H3 H H H6 6 6 6 6\ 6 6 6$6 6 6 6 6, 6 6 6:d d d d dC d d dN13 13 13 13 13C 13 13 13h    +   .O O O O O_ O O Oi i i i i i i iX:7 :7 :7 :7 :7i :7 :7 :7z\A \A \A \A \A- \A \A \A~M[ M[ M[ M[ M[m M[ M[ M[`
0 0 0 0 0G 0 0 0f % B  B  B  B  B / B  B  B Jk k k k k k k kv  v  v  v  v 72 v  v  v r / "4  *) *) *) *) *)g *) *) *)Zp! p! p! p! p!4 p! p! p!fv  v  v  v  v -!7 v  v  v zI I I I I I I IX( ( ( (9A 9A 9A 9A 9A- 9A 9A 9Axv v v v v{ v v v(v v v v v{ v v v     k          rq   