a
    ]j                      @   s   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 de	Z
G dd dZG d	d
 d
ZG dd dZdd ZG dd dZG dd deZG dd deZdS )   )	MIME_TYPEPNG_CHUNK_TYPE)InvalidImageStreamError)
BIG_ENDIANStreamReader)BaseImageHeaderc                   @   s4   e Zd ZdZedd Zedd Zedd ZdS )	Pngz#Image header parser for PNG images.c                 C   s   t jS )zUMIME content type for this image, unconditionally `image/png` for PNG
        images.)r   ZPNGself r   J/var/www/html/assistant/venv/lib/python3.9/site-packages/docx/image/png.pycontent_type
   s    zPng.content_typec                 C   s   dS )z8Default filename extension, always 'png' for PNG images.Zpngr   r	   r   r   r   default_ext   s    zPng.default_extc                 C   s0   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   r   r   r   from_stream   s    
zPng.from_streamN)	__name__
__module____qualname____doc__propertyr   r   classmethodr   r   r   r   r   r      s   

r   c                       sh   e Zd ZdZ fddZedd Zedd Zedd	 Z	ed
d Z
edd Zedd Z  ZS )r   zNParses a PNG image stream to extract the image properties found in its chunks.c                    s   t t|   || _d S N)superr   __init___chunks)r
   chunks	__class__r   r   r!   &   s    z_PngParser.__init__c                 C   s   t |}| |S )znReturn a |_PngParser| instance containing the header properties parsed from
        the PNG image in `stream`.)_Chunksr   )r   r   r#   r   r   r   r   *   s    
z_PngParser.parsec                 C   s   | j j}|jS )z.The number of pixels in each row of the image.)r"   IHDRr   r
   r'   r   r   r   r   1   s    z_PngParser.px_widthc                 C   s   | j j}|jS )z2The number of stacked rows of pixels in the image.)r"   r'   r   r(   r   r   r   r   7   s    z_PngParser.px_heightc                 C   s$   | j j}|du r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
   r*   r   r   r   r   =   s    z_PngParser.horz_dpic                 C   s$   | j j}|du r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.
        Nr)   )r"   r*   r+   r,   vert_px_per_unitr.   r   r   r   r   H   s    z_PngParser.vert_dpic                 C   s    | dkr|rt t|d S dS )zWReturn dots per inch value calculated from `units_specifier` and
        `px_per_unit`.r   g
F%u?r)   )intround)r,   Zpx_per_unitr   r   r   r+   S   s    z_PngParser._dpi)r   r   r   r   r!   r   r   r   r   r   r   r   staticmethodr+   __classcell__r   r   r$   r   r   #   s   






r   c                       sL   e Zd ZdZ fddZedd Zedd Zedd	 Z	d
d Z
  ZS )r&   z8Collection of the chunks parsed from a PNG image stream.c                    s   t t|   t|| _d S r   )r    r&   r!   listr"   )r
   Zchunk_iterabler$   r   r   r!   _   s    z_Chunks.__init__c                 C   s   t |}t| }| |S )zBReturn a |_Chunks| instance containing the PNG chunks in `stream`.)_ChunkParserr   r4   iter_chunks)r   r   Zchunk_parserr#   r   r   r   r   c   s    
z_Chunks.from_streamc                 C   s&   dd }|  |}|du r"td|S )zIHDR chunk in PNG image.c                 S   s   | j tjkS r   )	type_namer   r'   chunkr   r   r   <lambda>m       z_Chunks.IHDR.<locals>.<lambda>Nzno IHDR chunk in PNG image)_find_firstr   )r
   matchr'   r   r   r   r'   j   s
    
z_Chunks.IHDRc                 C   s   dd }|  |S )z2PHYs chunk in PNG image, or |None| if not present.c                 S   s   | j tjkS r   )r7   r   r*   r8   r   r   r   r:   v   r;   z_Chunks.pHYs.<locals>.<lambda>)r<   )r
   r=   r   r   r   r*   s   s    z_Chunks.pHYsc                 C   s    | j D ]}||r|  S qdS )zGReturn first chunk in stream order returning True for function `match`.N)r"   )r
   r=   r9   r   r   r   r<   y   s    

z_Chunks._find_first)r   r   r   r   r!   r   r   r   r'   r*   r<   r3   r   r   r$   r   r&   \   s   


r&   c                       s<   e Zd ZdZ fddZedd Zdd Zdd	 Z  Z	S )
r5   z(Extracts chunks from a PNG image stream.c                    s   t t|   || _d S r   )r    r5   r!   _stream_rdr)r
   
stream_rdrr$   r   r   r!      s    z_ChunkParser.__init__c                 C   s   t |t}| |S )zdReturn a |_ChunkParser| instance that can extract the chunks from the PNG
        image in `stream`.)r   r   )r   r   r?   r   r   r   r      s    
z_ChunkParser.from_streamc                 c   s*   |   D ]\}}t|| j|}|V  q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offsetr9   r   r   r   r6      s    z_ChunkParser.iter_chunksc                 c   sR   d}| j |}| j d|d}|d }||fV  |dkr<qN|d| d 7 }qdS )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.
              ZIENDN)r>   	read_longZread_str)r
   Zchunk_offsetZchunk_data_lenrB   Zdata_offsetr   r   r   r@      s    
z _ChunkParser._iter_chunk_offsets)
r   r   r   r   r!   r   r   r6   r@   r3   r   r   r$   r   r5      s   
r5   c                 C   s*   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   r'   
_IHDRChunkr*   
_pHYsChunkget_Chunkfrom_offset)rB   r?   rC   Zchunk_cls_mapZ	chunk_clsr   r   r   rA      s
    rA   c                       s8   e Zd ZdZ fddZedd Zedd Z  Z	S )rJ   zUBase class for specific chunk types.

    Also serves as the default chunk type.
    c                    s   t t|   || _d S r   )r    rJ   r!   _chunk_type)r
   rB   r$   r   r   r!      s    z_Chunk.__init__c                 C   s   | |S )z@Return a default _Chunk instance that only knows its chunk type.r   )r   rB   r?   rC   r   r   r   rK      s    z_Chunk.from_offsetc                 C   s   | j S )z.The chunk type name, e.g. 'IHDR', 'pHYs', etc.)rL   r	   r   r   r   r7      s    z_Chunk.type_name)
r   r   r   r   r!   r   rK   r   r7   r3   r   r   r$   r   rJ      s   
rJ   c                       sD   e Zd ZdZ fddZedd Zedd Zedd	 Z	  Z
S )
rG   z*IHDR chunk, contains the image dimensions.c                    s    t t| | || _|| _d S r   )r    rG   r!   	_px_width
_px_height)r
   rB   r   r   r$   r   r   r!      s    z_IHDRChunk.__init__c                 C   s"   | |}| |d}| |||S )z|Return an _IHDRChunk instance containing the image dimensions extracted from
        the IHDR chunk in `stream` at `offset`.rE   )rF   )r   rB   r?   rC   r   r   r   r   r   rK      s    
z_IHDRChunk.from_offsetc                 C   s   | j S r   )rM   r	   r   r   r   r      s    z_IHDRChunk.px_widthc                 C   s   | j S r   )rN   r	   r   r   r   r      s    z_IHDRChunk.px_height)r   r   r   r   r!   r   rK   r   r   r   r3   r   r   r$   r   rG      s   

rG   c                       sP   e Zd ZdZ fddZedd Zedd Zedd	 Z	ed
d Z
  ZS )rH   z/PYHs chunk, contains the image dpi information.c                    s&   t t| | || _|| _|| _d S r   )r    rH   r!   _horz_px_per_unit_vert_px_per_unit_units_specifier)r
   rB   r-   r/   r,   r$   r   r   r!      s    z_pHYsChunk.__init__c                 C   s0   | |}| |d}||d}| ||||S )z{Return a _pHYsChunk instance containing the image resolution extracted from
        the pHYs chunk in `stream` at `offset`.rE   rD   )rF   	read_byte)r   rB   r?   rC   r-   r/   r,   r   r   r   rK      s    
z_pHYsChunk.from_offsetc                 C   s   | j S r   )rO   r	   r   r   r   r-      s    z_pHYsChunk.horz_px_per_unitc                 C   s   | j S r   )rP   r	   r   r   r   r/      s    z_pHYsChunk.vert_px_per_unitc                 C   s   | j S r   )rQ   r	   r   r   r   r,      s    z_pHYsChunk.units_specifier)r   r   r   r   r!   r   rK   r   r-   r/   r,   r3   r   r   r$   r   rH      s   


rH   N)	constantsr   r   
exceptionsr   Zhelpersr   r   imager   r   r   r&   r5   rA   rJ   rG   rH   r   r   r   r   <module>   s   9%'