o
    4\U"                     @   s   d dl mZmZmZ 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
eZG dd deZG dd deZdd ZG dd deZG dd deZG dd deZdS )    )absolute_importdivisionprint_function   )	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 )zg
        MIME content type for this image, unconditionally `image/png` for
        PNG images.
        )r   ZPNGself r   >D:\Projects\ConvertPro\env\Lib\site-packages\docx\image\png.pycontent_type   s   zPng.content_typec                 C   s   dS )zJ
        Default 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 )zi
        Return 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   z\
    Parses 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&   3      
z_PngParser.__init__c                 C   s   t |}| |S )z
        Return a |_PngParser| instance containing the header properties
        parsed from the PNG image in *stream*.
        )_Chunksr   )r   r   r(   r   r   r   r   7      
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   @      z_PngParser.px_widthc                 C   r.   )zD
        The number of stacked rows of pixels in the image.
        )r'   r/   r   r0   r   r   r   r   H   r1   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.
        NH   )r'   pHYs_dpiunits_specifierhorz_px_per_unitr   r4   r   r   r   r   P      z_PngParser.horz_dpic                 C   r2   )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.
        Nr3   )r'   r4   r5   r6   vert_px_per_unitr8   r   r   r   r   [   r9   z_PngParser.vert_dpic                 C   s    | dkr|rt t|d S dS )zi
        Return dots per inch value calculated from *units_specifier* and
        *px_per_unit*.
        r   g
F%u?r3   )intround)r6   Zpx_per_unitr   r   r   r5   f   s   z_PngParser._dpi)r   r   r   r    r&   r"   r   r!   r   r   r   r   staticmethodr5   __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,   zA
    Collection 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&   u   s   z_Chunks.__init__c                 C   s$   t |}dd | D }| |S )zT
        Return a |_Chunks| instance containing the PNG chunks in *stream*.
        c                 S   s   g | ]}|qS r   r   ).0chunkr   r   r   
<listcomp>   s    z'_Chunks.from_stream.<locals>.<listcomp>)_ChunkParserr   iter_chunks)r   r   Zchunk_parserr(   r   r   r   r   y   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/   rA   r   r   r   <lambda>       z_Chunks.IHDR.<locals>.<lambda>Nzno IHDR chunk in PNG image)_find_firstr   )r   matchr/   r   r   r   r/      s
   
z_Chunks.IHDRc                 C   s   dd }|  |S )zC
        pHYs chunk in PNG image, or |None| if not present
        c                 S   rE   r$   )rF   r   r4   rG   r   r   r   rH      rI   z_Chunks.pHYs.<locals>.<lambda>)rJ   )r   rK   r   r   r   r4      s   
z_Chunks.pHYsc                 C   s    | j D ]
}||r|  S qdS )za
        Return first chunk in stream order returning True for function
        *match*.
        N)r'   )r   rK   rA   r   r   r   rJ      s
   
z_Chunks._find_first)r   r   r   r    r&   r"   r   r!   r/   r4   rJ   r>   r   r   r)   r   r,   q   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 )
rC   z1
    Extracts chunks from a PNG image stream
    c                    r#   r$   )r%   rC   r&   _stream_rdr)r   
stream_rdrr)   r   r   r&      r+   z_ChunkParser.__init__c                 C   s   t |t}| |S )zv
        Return a |_ChunkParser| instance that can extract the chunks from the
        PNG image in *stream*.
        )r
   r	   )r   r   rM   r   r   r   r      r-   z_ChunkParser.from_streamc                 c   s,    |   D ]\}}t|| j|}|V  qdS )z
        Generate a |_Chunk| subclass instance for each chunk in this parser's
        PNG stream, in the order encountered in the stream.
        N)_iter_chunk_offsets_ChunkFactoryrL   )r   
chunk_typeoffsetrA   r   r   r   rD      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)rL   	read_longZread_str)r   Zchunk_offsetZchunk_data_lenrP   Zdata_offsetr   r   r   rN      s   
z _ChunkParser._iter_chunk_offsets)
r   r   r   r    r&   r"   r   rD   rN   r>   r   r   r)   r   rC      s    
	rC   c                 C   s*   t jtt jti}|| t}|| ||S )zs
    Return a |_Chunk| subclass instance appropriate to *chunk_type* parsed
    from *stream_rdr* at *offset*.
    )r   r/   
_IHDRChunkr4   
_pHYsChunkget_Chunkfrom_offset)rP   rM   rQ   Zchunk_cls_mapZ	chunk_clsr   r   r   rO      s
   rO   c                       s8   e Zd ZdZ fddZedd Zedd Z  Z	S )rX   zY
    Base class for specific chunk types. Also serves as the default chunk
    type.
    c                    r#   r$   )r%   rX   r&   _chunk_type)r   rP   r)   r   r   r&      r+   z_Chunk.__init__c                 C   s   | |S )zR
        Return a default _Chunk instance that only knows its chunk type.
        r   )r   rP   rM   rQ   r   r   r   rY      s   z_Chunk.from_offsetc                 C      | j S )z@
        The chunk type name, e.g. 'IHDR', 'pHYs', etc.
        )rZ   r   r   r   r   rF      s   z_Chunk.type_name)
r   r   r   r    r&   r"   rY   r!   rF   r>   r   r   r)   r   rX      s    
rX   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 )
rU   z3
    IHDR chunk, contains the image dimensions
    c                    s    t t| | || _|| _d S r$   )r%   rU   r&   	_px_width
_px_height)r   rP   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*.
        rS   )rT   )r   rP   rM   rQ   r   r   r   r   r   rY      s   
z_IHDRChunk.from_offsetc                 C   r[   r$   )r\   r   r   r   r   r        z_IHDRChunk.px_widthc                 C   r[   r$   )r]   r   r   r   r   r     r^   z_IHDRChunk.px_height)r   r   r   r    r&   r"   rY   r!   r   r   r>   r   r   r)   r   rU      s    
	
rU   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 )rV   z8
    pYHs chunk, contains the image dpi information
    c                    s&   t t| | || _|| _|| _d S r$   )r%   rV   r&   _horz_px_per_unit_vert_px_per_unit_units_specifier)r   rP   r7   r:   r6   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*.
        rS   rR   )rT   Z	read_byte)r   rP   rM   rQ   r7   r:   r6   r   r   r   rY     s   
z_pHYsChunk.from_offsetc                 C   r[   r$   )r_   r   r   r   r   r7   %  r^   z_pHYsChunk.horz_px_per_unitc                 C   r[   r$   )r`   r   r   r   r   r:   )  r^   z_pHYsChunk.vert_px_per_unitc                 C   r[   r$   )ra   r   r   r   r   r6   -  r^   z_pHYsChunk.units_specifier)r   r   r   r    r&   r"   rY   r!   r7   r:   r6   r>   r   r   r)   r   rV     s    


rV   N)Z
__future__r   r   r   	constantsr   r   
exceptionsr   helpersr	   r
   imager   r   objectr   r,   rC   rO   rX   rU   rV   r   r   r   r   <module>   s   #C/,