
    hi                          d dl mZmZ d dlmZ d dlmZmZ d dlm	Z	  G d de	          Z
 G d d          Z G d	 d
          Z G d d          Zd Z G d d          Z G d de          Z G d de          ZdS )   )	MIME_TYPEPNG_CHUNK_TYPE)InvalidImageStreamError)
BIG_ENDIANStreamReader)BaseImageHeaderc                   T    e Zd ZdZed             Zed             Zed             ZdS )Pngz#Image header parser for PNG images.c                     t           j        S )zUMIME content type for this image, unconditionally `image/png` for PNG
        images.)r   PNGselfs    f/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/docx/image/png.pycontent_typezPng.content_type
   s     }    c                     dS )z8Default filename extension, always 'png' for PNG images.png r   s    r   default_extzPng.default_ext   s	     ur   c                     t                               |          }|j        }|j        }|j        }|j        } | ||||          S )zWReturn a |Png| instance having header properties parsed from image in
        `stream`.)
_PngParserparsepx_width	px_heighthorz_dpivert_dpi)clsstreamparserr   r   r   r   s          r   from_streamzPng.from_stream   sJ     !!&))?$	??s8Y(;;;r   N)	__name__
__module____qualname____doc__propertyr   r   classmethodr    r   r   r   r
   r
      si        --  X
   X 
< 
< [
< 
< 
<r   r
   c                        e Zd ZdZ fdZed             Zed             Zed             Z	ed             Z
ed             Zed             Z xZS )	r   zNParses a PNG image stream to extract the image properties found in its chunks.c                 d    t          t          |                                            || _        d S N)superr   __init___chunks)r   chunks	__class__s     r   r+   z_PngParser.__init__&   s*    j$((***r   c                 L    t                               |          } | |          S )znReturn a |_PngParser| instance containing the header properties parsed from
        the PNG image in `stream`.)_Chunksr    )r   r   r-   s      r   r   z_PngParser.parse*   s%     $$V,,s6{{r   c                 (    | j         j        }|j        S )z.The number of pixels in each row of the image.)r,   IHDRr   r   r2   s     r   r   z_PngParser.px_width1   s     | }r   c                 (    | j         j        }|j        S )z2The number of stacked rows of pixels in the image.)r,   r2   r   r3   s     r   r   z_PngParser.px_height7   s     | ~r   c                 b    | j         j        }|dS |                     |j        |j                  S )zInteger dots per inch for the width of this image.

        Defaults to 72 when not present in the file, as is often the case.
        NH   )r,   pHYs_dpiunits_specifierhorz_px_per_unitr   r7   s     r   r   z_PngParser.horz_dpi=   2     | <2yy-t/DEEEr   c                 b    | j         j        }|dS |                     |j        |j                  S )zInteger dots per inch for the height of this image.

        Defaults to 72 when not present in the file, as is often the case.
        Nr6   )r,   r7   r8   r9   vert_px_per_unitr;   s     r   r   z_PngParser.vert_dpiH   r<   r   c                 T    | dk    r!|rt          t          |dz                      S dS )zWReturn dots per inch value calculated from `units_specifier` and
        `px_per_unit`.r   g
F%u?r6   )intround)r9   px_per_units     r   r8   z_PngParser._dpiS   s4     aKu[6122333rr   )r!   r"   r#   r$   r+   r&   r   r%   r   r   r   r   staticmethodr8   __classcell__r.   s   @r   r   r   #   s        XX       [   X
   X
 F F XF F F XF   \    r   r   c                   j     e Zd ZdZ fdZed             Zed             Zed             Z	d Z
 xZS )r0   z8Collection of the chunks parsed from a PNG image stream.c                 ~    t          t          |                                            t          |          | _        d S r)   )r*   r0   r+   listr,   )r   chunk_iterabler.   s     r   r+   z_Chunks.__init___   s2    gt%%'''N++r   c                     t                               |          }t          |                                          } | |          S )zBReturn a |_Chunks| instance containing the PNG chunks in `stream`.)_ChunkParserr    rH   iter_chunks)r   r   chunk_parserr-   s       r   r    z_Chunks.from_streamc   s>     $//77l..0011s6{{r   c                 X    d }|                      |          }|t          d          |S )zIHDR chunk in PNG image.c                 ,    | j         t          j        k    S r)   )	type_namer   r2   chunks    r   <lambda>z_Chunks.IHDR.<locals>.<lambda>m       eo1DD r   Nzno IHDR chunk in PNG image)_find_firstr   )r   matchr2   s      r   r2   z_Chunks.IHDRj   s9     ED&&<)*FGGGr   c                 2    d }|                      |          S )z2PHYs chunk in PNG image, or |None| if not present.c                 ,    | j         t          j        k    S r)   )rP   r   r7   rQ   s    r   rS   z_Chunks.pHYs.<locals>.<lambda>v   rT   r   )rU   )r   rV   s     r   r7   z_Chunks.pHYss   s!     ED&&&r   c                 8    | j         D ]} ||          r|c S dS )zGReturn first chunk in stream order returning True for function `match`.N)r,   )r   rV   rR   s      r   rU   z_Chunks._find_firsty   s6    \ 	 	EuU|| tr   )r!   r"   r#   r$   r+   r&   r    r%   r2   r7   rU   rD   rE   s   @r   r0   r0   \   s        BB, , , , ,   [   X ' ' X'
      r   r0   c                   D     e Zd ZdZ fdZed             Zd Zd Z xZ	S )rK   z(Extracts chunks from a PNG image stream.c                 d    t          t          |                                            || _        d S r)   )r*   rK   r+   _stream_rdr)r   
stream_rdrr.   s     r   r+   z_ChunkParser.__init__   s-    lD!!**,,,%r   c                 B    t          |t                    } | |          S )zdReturn a |_ChunkParser| instance that can extract the chunks from the PNG
        image in `stream`.)r   r   )r   r   r]   s      r   r    z_ChunkParser.from_stream   s!     "&*55
s:r   c              #   r   K   |                                  D ]\  }}t          || j        |          }|V   dS )zGenerate a |_Chunk| subclass instance for each chunk in this parser's PNG
        stream, in the order encountered in the stream.N)_iter_chunk_offsets_ChunkFactoryr\   )r   
chunk_typeoffsetrR   s       r   rL   z_ChunkParser.iter_chunks   sQ       #'":":"<"< 	 	J!*d.>GGEKKKK	 	r   c              #      K   d}	 | j                             |          }| j                             d|d          }|dz   }||fV  |dk    rdS |d|z   dz   z  }U)zGenerate a (chunk_type, chunk_offset) 2-tuple for each of the chunks in the
        PNG image stream.

        Iteration stops after the IEND chunk is returned.
           T   IENDN)r\   	read_longread_str)r   chunk_offsetchunk_data_lenrb   data_offsets        r   r`   z _ChunkParser._iter_chunk_offsets   s       	7!-77EEN)221lAFFJ&*Kk))))V##EN2Q66L	7r   )
r!   r"   r#   r$   r+   r&   r    rL   r`   rD   rE   s   @r   rK   rK      sw        22& & & & &   [  7 7 7 7 7 7 7r   rK   c                     t           j        t          t           j        t          i}|                    | t                    }|                    | ||          S )ziReturn a |_Chunk| subclass instance appropriate to `chunk_type` parsed from
    `stream_rdr` at `offset`.)r   r2   
_IHDRChunkr7   
_pHYsChunkget_Chunkfrom_offset)rb   r]   rc   chunk_cls_map	chunk_clss        r   ra   ra      sJ     	ZZM !!*f55I  Z@@@r   c                   N     e Zd ZdZ fdZed             Zed             Z xZ	S )rq   zUBase class for specific chunk types.

    Also serves as the default chunk type.
    c                 d    t          t          |                                            || _        d S r)   )r*   rq   r+   _chunk_type)r   rb   r.   s     r   r+   z_Chunk.__init__   s-    fd$$&&&%r   c                      | |          S )z@Return a default _Chunk instance that only knows its chunk type.r   )r   rb   r]   rc   s       r   rr   z_Chunk.from_offset   s     s:r   c                     | j         S )z.The chunk type name, e.g. 'IHDR', 'pHYs', etc.)rw   r   s    r   rP   z_Chunk.type_name   s     r   )
r!   r"   r#   r$   r+   r&   rr   r%   rP   rD   rE   s   @r   rq   rq      sx         
& & & & &   [     X         r   rq   c                   d     e Zd ZdZ fdZed             Zed             Zed             Z	 xZ
S )rn   z*IHDR chunk, contains the image dimensions.c                 t    t          t          |                               |           || _        || _        d S r)   )r*   rn   r+   	_px_width
_px_height)r   rb   r   r   r.   s       r   r+   z_IHDRChunk.__init__   s3    j$((444!#r   c                 r    |                     |          }|                     |d          } | |||          S )z|Return an _IHDRChunk instance containing the image dimensions extracted from
        the IHDR chunk in `stream` at `offset`.rf   )rh   )r   rb   r]   rc   r   r   s         r   rr   z_IHDRChunk.from_offset   s@     ''//((33	s:x333r   c                     | j         S r)   )r|   r   s    r   r   z_IHDRChunk.px_width   s
    ~r   c                     | j         S r)   )r}   r   s    r   r   z_IHDRChunk.px_height   s
    r   )r!   r"   r#   r$   r+   r&   rr   r%   r   r   rD   rE   s   @r   rn   rn      s        44$ $ $ $ $
 4 4 [4   X   X    r   rn   c                   z     e Zd ZdZ fdZed             Zed             Zed             Z	ed             Z
 xZS )ro   z/PYHs chunk, contains the image dpi information.c                     t          t          |                               |           || _        || _        || _        d S r)   )r*   ro   r+   _horz_px_per_unit_vert_px_per_unit_units_specifier)r   rb   r:   r>   r9   r.   s        r   r+   z_pHYsChunk.__init__   s?    j$((444!1!1 /r   c                     |                     |          }|                     |d          }|                    |d          } | ||||          S )z{Return a _pHYsChunk instance containing the image resolution extracted from
        the pHYs chunk in `stream` at `offset`.rf   re   )rh   	read_byte)r   rb   r]   rc   r:   r>   r9   s          r   rr   z_pHYsChunk.from_offset   s[     &//77%//::$..vq99s:/1A?SSSr   c                     | j         S r)   )r   r   s    r   r:   z_pHYsChunk.horz_px_per_unit       %%r   c                     | j         S r)   )r   r   s    r   r>   z_pHYsChunk.vert_px_per_unit   r   r   c                     | j         S r)   )r   r   s    r   r9   z_pHYsChunk.units_specifier   s    $$r   )r!   r"   r#   r$   r+   r&   rr   r%   r:   r>   r9   rD   rE   s   @r   ro   ro      s        990 0 0 0 0 T T [T & & X& & & X& % % X% % % % %r   ro   N)	constantsr   r   
exceptionsr   helpersr   r   imager   r
   r   r0   rK   ra   rq   rn   ro   r   r   r   <module>r      s   0 0 0 0 0 0 0 0 / / / / / / - - - - - - - - " " " " " "< < < < </ < < <86 6 6 6 6 6 6 6r" " " " " " " "J$7 $7 $7 $7 $7 $7 $7 $7NA A A               *       2% % % % % % % % % %r   