B
    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   @/var/www/html/venv/lib/python3.7/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                s   t t|   || _d S )N)superr   __init___chunks)r   chunks)	__class__r   r   r#   3   s    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   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   @   s    z_PngParser.px_widthc             C   s   | j j}|jS )zD
        The number of stacked rows of pixels in the image.
        )r$   r(   r   )r   r(   r   r   r   r   H   s    z_PngParser.px_heightc             C   s$   | j j}|dk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   r*   r   r   r   r   P   s    z_PngParser.horz_dpic             C   s$   | j j}|dkrdS | |j|jS )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$   r*   r+   r,   vert_px_per_unit)r   r*   r   r   r   r   [   s    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?r)   )intround)r,   Zpx_per_unitr   r   r   r+   f   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'   zA
    Collection of the chunks parsed from a PNG image stream
    c                s   t t|   t|| _d S )N)r"   r'   r#   listr$   )r   Zchunk_iterable)r&   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kr"td|S )z)
        IHDR chunk in PNG image
        c             S   s   | j tjkS )N)	type_namer   r(   )r5   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   s   | j tjkS )N)r9   r   r*   )r5   r   r   r   r:      r;   z_Chunks.pHYs.<locals>.<lambda>)r<   )r   r=   r   r   r   r*      s    z_Chunks.pHYsc             C   s    x| j D ]}||r|S qW dS )za
        Return first chunk in stream order returning True for function
        *match*.
        N)r$   )r   r=   r5   r   r   r   r<      s    z_Chunks._find_first)r   r   r   r   r#   r!   r   r    r(   r*   r<   r2   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 )
r7   z1
    Extracts chunks from a PNG image stream
    c                s   t t|   || _d S )N)r"   r7   r#   _stream_rdr)r   
stream_rdr)r&   r   r   r#      s    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   r?   r   r   r   r      s    
z_ChunkParser.from_streamc             c   s.   x(|   D ]\}}t|| j|}|V  q
W 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_ChunkFactoryr>   )r   
chunk_typeoffsetr5   r   r   r   r8      s    z_ChunkParser.iter_chunksc             c   sV   d}xL| j |}| j d|d}|d }||fV  |dkr>P |d| d 7 }qW dS )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.
              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   r8   r@   r2   r   r   )r&   r   r7      s
   		r7   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(   
_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   zY
    Base class for specific chunk types. Also serves as the default chunk
    type.
    c                s   t t|   || _d S )N)r"   rJ   r#   _chunk_type)r   rB   )r&   r   r   r#      s    z_Chunk.__init__c             C   s   | |S )zR
        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   r9      s    z_Chunk.type_name)
r   r   r   r   r#   r!   rK   r    r9   r2   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   z3
    IHDR chunk, contains the image dimensions
    c                s    t t| | || _|| _d S )N)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 )N)rM   )r   r   r   r   r     s    z_IHDRChunk.px_widthc             C   s   | j S )N)rN   )r   r   r   r   r     s    z_IHDRChunk.px_height)r   r   r   r   r#   r!   rK   r    r   r   r2   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   z8
    pYHs chunk, contains the image dpi information
    c                s&   t t| | || _|| _|| _d S )N)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   Z	read_byte)r   rB   r?   rC   r-   r.   r,   r   r   r   rK     s
    
z_pHYsChunk.from_offsetc             C   s   | j S )N)rO   )r   r   r   r   r-   %  s    z_pHYsChunk.horz_px_per_unitc             C   s   | j S )N)rP   )r   r   r   r   r.   )  s    z_pHYsChunk.vert_px_per_unitc             C   s   | j S )N)rQ   )r   r   r   r   r,   -  s    z_pHYsChunk.units_specifier)r   r   r   r   r#   r!   rK   r    r-   r.   r,   r2   r   r   )r&   r   rH     s   rH   N)
__future__r   r   r   	constantsr   r   
exceptionsr   Zhelpersr	   r
   imager   r   objectr   r'   r7   rA   rJ   rG   rH   r   r   r   r   <module>   s   #C/,