o
    	Ð'f   ã                   @   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   ÚPNG©Úself© r   úF/home/ubuntu/flask/venv/lib/python3.10/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.Ú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                    ó   t t| ƒ ¡  || _d S ©N)Úsuperr   Ú__init__Ú_chunks)r   Úchunks©Ú	__class__r   r   r$   &   ó   
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   *   ó   
z_PngParser.parsec                 C   ó   | j j}|jS )z.The number of pixels in each row of the image.)r%   ÚIHDRr   ©r   r-   r   r   r   r   1   ó   z_PngParser.px_widthc                 C   r,   )z2The number of stacked rows of pixels in the image.)r%   r-   r   r.   r   r   r   r   7   r/   z_PngParser.px_heightc                 C   ó$   | j j}|du r
dS |  |j|j¡S )z‡Integer dots per inch for the width of this image.

        Defaults to 72 when not present in the file, as is often the case.
        NéH   )r%   ÚpHYsÚ_dpiÚunits_specifierÚhorz_px_per_unit©r   r2   r   r   r   r   =   ó   z_PngParser.horz_dpic                 C   r0   )zˆInteger dots per inch for the height of this image.

        Defaults to 72 when not present in the file, as is often the case.
        Nr1   )r%   r2   r3   r4   Úvert_px_per_unitr6   r   r   r   r   H   r7   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š?r1   )ÚintÚround)r4   Úpx_per_unitr   r   r   r3   S   s   z_PngParser._dpi)r   r   r   r   r$   r    r   r   r   r   r   r   Ústaticmethodr3   Ú__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   Ú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   r>   Úiter_chunks)r   r   Ú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   ó   | 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   rC   r"   )rD   r   r2   rE   r   r   r   rG   v   rH   z_Chunks.pHYs.<locals>.<lambda>)rI   )r   rJ   r   r   r   r2   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   rJ   rF   r   r   r   rI   y   s
   
ÿz_Chunks._find_first)r   r   r   r   r$   r    r   r   r-   r2   rI   r=   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 )
r@   z(Extracts chunks from a PNG image stream.c                    r!   r"   )r#   r@   r$   Ú_stream_rdr)r   Ú
stream_rdrr'   r   r   r$   „   r)   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   rL   r   r   r   r   ˆ   r+   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Ú_ChunkFactoryrK   )r   Ú
chunk_typeÚoffsetrF   r   r   r   rA      s
   €þz_ChunkParser.iter_chunksc                 c   sT    d}	 | j  |¡}| j  d|d¡}|d }||fV  |dkr!dS |d| d 7 }q)z©Generate 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)rK   Ú	read_longÚread_str)r   Úchunk_offsetÚchunk_data_lenrO   Údata_offsetr   r   r   rM   –   s   €
øz _ChunkParser._iter_chunk_offsets)
r   r   r   r   r$   r    r   rA   rM   r=   r   r   r'   r   r@      s    
r@   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-   Ú
_IHDRChunkr2   Ú
_pHYsChunkÚgetÚ_ChunkÚfrom_offset)rO   rL   rP   Úchunk_cls_mapÚ	chunk_clsr   r   r   rN   ¨   s
   þrN   c                       s8   e Zd ZdZ‡ fdd„Zedd„ ƒZedd„ ƒZ‡  Z	S )r\   zUBase class for specific chunk types.

    Also serves as the default chunk type.
    c                    r!   r"   )r#   r\   r$   Ú_chunk_type)r   rO   r'   r   r   r$   ¹   r)   z_Chunk.__init__c                 C   s   | |ƒS )z@Return a default _Chunk instance that only knows its chunk type.r   )r   rO   rL   rP   r   r   r   r]   ½   s   z_Chunk.from_offsetc                 C   ó   | j S )z.The chunk type name, e.g. 'IHDR', 'pHYs', etc.)r`   r
   r   r   r   rD   Â   s   z_Chunk.type_name)
r   r   r   r   r$   r    r]   r   rD   r=   r   r   r'   r   r\   ³   s    
r\   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 )
rY   z*IHDR chunk, contains the image dimensions.c                    s    t t| ƒ |¡ || _|| _d S r"   )r#   rY   r$   Ú	_px_widthÚ
_px_height)r   rO   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`.rR   )rT   )r   rO   rL   rP   r   r   r   r   r   r]   Ð   s   
z_IHDRChunk.from_offsetc                 C   ra   r"   )rb   r
   r   r   r   r   Ø   ó   z_IHDRChunk.px_widthc                 C   ra   r"   )rc   r
   r   r   r   r   Ü   rd   z_IHDRChunk.px_height)r   r   r   r   r$   r    r]   r   r   r   r=   r   r   r'   r   rY   È   s    

rY   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 )rZ   z/PYHs chunk, contains the image dpi information.c                    s&   t t| ƒ |¡ || _|| _|| _d S r"   )r#   rZ   r$   Ú_horz_px_per_unitÚ_vert_px_per_unitÚ_units_specifier)r   rO   r5   r8   r4   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`.rR   rQ   )rT   Ú	read_byte)r   rO   rL   rP   r5   r8   r4   r   r   r   r]   ê   s   
z_pHYsChunk.from_offsetc                 C   ra   r"   )re   r
   r   r   r   r5   ó   rd   z_pHYsChunk.horz_px_per_unitc                 C   ra   r"   )rf   r
   r   r   r   r8   ÷   rd   z_pHYsChunk.vert_px_per_unitc                 C   ra   r"   )rg   r
   r   r   r   r4   û   rd   z_pHYsChunk.units_specifier)r   r   r   r   r$   r    r]   r   r5   r8   r4   r=   r   r   r'   r   rZ   á   s    


rZ   N)Ú	constantsr   r   Ú
exceptionsr   Úhelpersr   r   Úimager   r   r   r*   r@   rN   r\   rY   rZ   r   r   r   r   Ú<module>   s    9%'