o
    jqi   ã                   @   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   úU/home/app/PaddleOCR-VL/.venv_paddleocr/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.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                    ó   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
   r0   r   r   r   r   =   ó   z_PngParser.horz_dpic                 C   r.   )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.
        Nr/   )r#   r0   r1   r2   Úvert_px_per_unitr4   r   r   r   r   H   r5   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)r2   Zpx_per_unitr   r   r   r1   S   s   z_PngParser._dpi)r   r   r   r   r"   r   r   r   r   r   r   r   Ústaticmethodr1   Ú__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   r;   Ú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   ó   | 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   r>   r    )r?   r   r0   r@   r   r   r   rB   v   rC   z_Chunks.pHYs.<locals>.<lambda>)rD   )r
   rE   r   r   r   r0   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
   rE   rA   r   r   r   rD   y   s
   
ÿz_Chunks._find_first)r   r   r   r   r"   r   r   r   r+   r0   rD   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   rG   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Ú_ChunkFactoryrF   )r
   Ú
chunk_typeÚoffsetrA   r   r   r   r=      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é   ZIENDN)rF   Ú	read_longZread_str)r
   Zchunk_offsetZchunk_data_lenrJ   Zdata_offsetr   r   r   rH   –   s   €
øz _ChunkParser._iter_chunk_offsets)
r   r   r   r   r"   r   r   r=   rH   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+   Ú
_IHDRChunkr0   Ú
_pHYsChunkÚgetÚ_ChunkÚfrom_offset)rJ   rG   rK   Zchunk_cls_mapZ	chunk_clsr   r   r   rI   ¨   s
   þrI   c                       s8   e Zd ZdZ‡ fdd„Zedd„ ƒZedd„ ƒZ‡  Z	S )rR   zUBase class for specific chunk types.

    Also serves as the default chunk type.
    c                    r   r    )r!   rR   r"   Ú_chunk_type)r
   rJ   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   rJ   rG   rK   r   r   r   rS   ½   s   z_Chunk.from_offsetc                 C   ó   | j S )z.The chunk type name, e.g. 'IHDR', 'pHYs', etc.)rT   r	   r   r   r   r?   Â   s   z_Chunk.type_name)
r   r   r   r   r"   r   rS   r   r?   r:   r   r   r%   r   rR   ³   s    
rR   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 )
rO   z*IHDR chunk, contains the image dimensions.c                    s    t t| ƒ |¡ || _|| _d S r    )r!   rO   r"   Ú	_px_widthÚ
_px_height)r
   rJ   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`.rM   )rN   )r   rJ   rG   rK   r   r   r   r   r   rS   Ð   s   
z_IHDRChunk.from_offsetc                 C   rU   r    )rV   r	   r   r   r   r   Ø   ó   z_IHDRChunk.px_widthc                 C   rU   r    )rW   r	   r   r   r   r   Ü   rX   z_IHDRChunk.px_height)r   r   r   r   r"   r   rS   r   r   r   r:   r   r   r%   r   rO   È   s    

rO   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 )rP   z/PYHs chunk, contains the image dpi information.c                    s&   t t| ƒ |¡ || _|| _|| _d S r    )r!   rP   r"   Ú_horz_px_per_unitÚ_vert_px_per_unitÚ_units_specifier)r
   rJ   r3   r6   r2   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`.rM   rL   )rN   Ú	read_byte)r   rJ   rG   rK   r3   r6   r2   r   r   r   rS   ê   s   
z_pHYsChunk.from_offsetc                 C   rU   r    )rY   r	   r   r   r   r3   ó   rX   z_pHYsChunk.horz_px_per_unitc                 C   rU   r    )rZ   r	   r   r   r   r6   ÷   rX   z_pHYsChunk.vert_px_per_unitc                 C   rU   r    )r[   r	   r   r   r   r2   û   rX   z_pHYsChunk.units_specifier)r   r   r   r   r"   r   rS   r   r3   r6   r2   r:   r   r   r%   r   rP   á   s    


rP   N)Ú	constantsr   r   Ú
exceptionsr   Úhelpersr   r   Úimager   r   r   r(   r<   rI   rR   rO   rP   r   r   r   r   Ú<module>   s    9%'