
    ,h                          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e	      Z
 G d d      Z G d	 d
      Z G d d      Zd Z G d d      Z G d de      Z G d de      Zy)   )	MIME_TYPEPNG_CHUNK_TYPE)InvalidImageStreamError)
BIG_ENDIANStreamReader)BaseImageHeaderc                   @    e Zd ZdZed        Zed        Zed        Zy)Pngz#Image header parser for PNG images.c                 "    t         j                  S )zUMIME content type for this image, unconditionally `image/png` for PNG
        images.)r   PNGselfs    P/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/docx/image/png.pycontent_typezPng.content_type
   s     }}    c                      y)z8Default filename extension, always 'png' for PNG images.png r   s    r   default_extzPng.default_ext   s     r   c                     t         j                  |      }|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   s          r   from_streamzPng.from_stream   sK     !!&)??$$	????8Y(;;r   N)	__name__
__module____qualname____doc__propertyr   r   classmethodr    r   r   r   r
   r
      s?    - 
   
< 
<r   r
   c                        e Zd ZdZ fdZed        Zed        Zed        Z	ed        Z
ed        Zed        Z xZS )	r   zNParses a PNG image stream to extract the image properties found in its chunks.c                 8    t         t        |           || _        y N)superr   __init___chunks)r   chunks	__class__s     r   r+   z_PngParser.__init__&   s    j$(*r   c                 <    t         j                  |      } | |      S )znReturn a |_PngParser| instance containing the header properties parsed from
        the PNG image in `stream`.)_Chunksr    )r   r   r-   s      r   r   z_PngParser.parse*   s     $$V,6{r   c                 F    | j                   j                  }|j                  S )z.The number of pixels in each row of the image.)r,   IHDRr   r   r2   s     r   r   z_PngParser.px_width1   s     ||  }}r   c                 F    | j                   j                  }|j                  S )z2The number of stacked rows of pixels in the image.)r,   r2   r   r3   s     r   r   z_PngParser.px_height7   s     ||  ~~r   c                     | j                   j                  }|y| j                  |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.
        H   )r,   pHYs_dpiunits_specifierhorz_px_per_unitr   r7   s     r   r   z_PngParser.horz_dpi=   8     ||  <yy--t/D/DEEr   c                     | j                   j                  }|y| j                  |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.
        r6   )r,   r7   r8   r9   vert_px_per_unitr;   s     r   r   z_PngParser.vert_dpiH   r<   r   c                 @    | dk(  r|rt        t        |dz              S y)zWReturn dots per inch value calculated from `units_specifier` and
        `px_per_unit`.r   g
F%u?r6   )intround)r9   px_per_units     r   r8   z_PngParser._dpiS   s&     aKu[61233r   )r!   r"   r#   r$   r+   r&   r   r%   r   r   r   r   staticmethodr8   __classcell__r.   s   @r   r   r   #   s    X    
  
 F F F F  r   r   c                   X     e Zd ZdZ fdZed        Zed        Zed        Z	d Z
 xZS )r0   z8Collection of the chunks parsed from a PNG image stream.c                 J    t         t        |           t        |      | _        y r)   )r*   r0   r+   listr,   )r   chunk_iterabler.   s     r   r+   z_Chunks.__init___   s    gt%'N+r   c                 n    t         j                  |      }t        |j                               } | |      S )zBReturn a |_Chunks| instance containing the PNG chunks in `stream`.)_ChunkParserr    rH   iter_chunks)r   r   chunk_parserr-   s       r   r    z_Chunks.from_streamc   s1     $//7l..016{r   c                 H    d }| j                  |      }|t        d      |S )zIHDR chunk in PNG image.c                 <    | j                   t        j                  k(  S r)   )	type_namer   r2   chunks    r   <lambda>z_Chunks.IHDR.<locals>.<lambda>m       eoo1D1DD r   zno IHDR chunk in PNG image)_find_firstr   )r   matchr2   s      r   r2   z_Chunks.IHDRj   s0     E&<)*FGGr   c                 *    d }| j                  |      S )z2PHYs chunk in PNG image, or |None| if not present.c                 <    | j                   t        j                  k(  S r)   )rP   r   r7   rQ   s    r   rS   z_Chunks.pHYs.<locals>.<lambda>v   rT   r   )rU   )r   rV   s     r   r7   z_Chunks.pHYss   s     E&&r   c                 >    | j                   D ]  } ||      s|c S  y)zGReturn first chunk in stream order returning True for function `match`.N)r,   )r   rV   rR   s      r   rU   z_Chunks._find_firsty   s&    \\ 	EU|	 r   )r!   r"   r#   r$   r+   r&   r    r%   r2   r7   rU   rD   rE   s   @r   r0   r0   \   sL    B,     ' '
r   r0   c                   >     e Zd ZdZ fdZed        Zd Zd Z xZ	S )rK   z(Extracts chunks from a PNG image stream.c                 8    t         t        |           || _        y r)   )r*   rK   r+   _stream_rdr)r   
stream_rdrr.   s     r   r+   z_ChunkParser.__init__   s    lD*,%r   c                 2    t        |t              } | |      S )zdReturn a |_ChunkParser| instance that can extract the chunks from the PNG
        image in `stream`.)r   r   )r   r   r]   s      r   r    z_ChunkParser.from_stream   s     "&*5
:r   c              #   r   K   | j                         D ]   \  }}t        || j                  |      }| " yw)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offsetrR   s       r   rL   z_ChunkParser.iter_chunks   s>      #'":":"< 	J!*d.>.>GEK	s   57c              #      K   d}	 | j                   j                  |      }| j                   j                  d|d      }|dz   }||f |dk(  ry|d|z   dz   z  }Uw)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.
              IENDN)r\   	read_longread_str)r   chunk_offsetchunk_data_lenrb   data_offsets        r   r`   z _ChunkParser._iter_chunk_offsets   sy      !--77EN))221lAFJ&*Kk))V#EN2Q66L s   AA)
r!   r"   r#   r$   r+   r&   r    rL   r`   rD   rE   s   @r   rK   rK      s)    2&  7r   rK   c                     t         j                  t        t         j                  t        i}|j                  | t              }|j                  | ||      S )ziReturn a |_Chunk| subclass instance appropriate to `chunk_type` parsed from
    `stream_rdr` at `offset`.)r   r2   
_IHDRChunkr7   
_pHYsChunkget_Chunkfrom_offset)rb   r]   rc   chunk_cls_map	chunk_clss        r   ra   ra      sJ     	ZZM !!*f5I  Z@@r   c                   B     e Zd ZdZ fdZed        Zed        Z xZ	S )rq   zUBase class for specific chunk types.

    Also serves as the default chunk type.
    c                 8    t         t        |           || _        y r)   )r*   rq   r+   _chunk_type)r   rb   r.   s     r   r+   z_Chunk.__init__   s    fd$&%r   c                      | |      S )z@Return a default _Chunk instance that only knows its chunk type.r   )r   rb   r]   rc   s       r   rr   z_Chunk.from_offset   s     :r   c                     | j                   S )z.The chunk type name, e.g. 'IHDR', 'pHYs', etc.)rw   r   s    r   rP   z_Chunk.type_name   s     r   )
r!   r"   r#   r$   r+   r&   rr   r%   rP   rD   rE   s   @r   rq   rq      s5    
&      r   rq   c                   R     e Zd ZdZ fdZed        Zed        Zed        Z	 xZ
S )rn   z*IHDR chunk, contains the image dimensions.c                 H    t         t        |   |       || _        || _        y r)   )r*   rn   r+   	_px_width
_px_height)r   rb   r   r   r.   s       r   r+   z_IHDRChunk.__init__   s     j$(4!#r   c                 \    |j                  |      }|j                  |d      } | |||      S )z|Return an _IHDRChunk instance containing the image dimensions extracted from
        the IHDR chunk in `stream` at `offset`.rf   )rh   )r   rb   r]   rc   r   r   s         r   rr   z_IHDRChunk.from_offset   s5     ''/((3	:x33r   c                     | j                   S r)   )r|   r   s    r   r   z_IHDRChunk.px_width   s    ~~r   c                     | j                   S r)   )r}   r   s    r   r   z_IHDRChunk.px_height   s    r   )r!   r"   r#   r$   r+   r&   rr   r%   r   r   rD   rE   s   @r   rn   rn      sG    4$
 4 4    r   rn   c                   b     e Zd ZdZ fdZed        Zed        Zed        Z	ed        Z
 xZS )ro   z/PYHs chunk, contains the image dpi information.c                 V    t         t        |   |       || _        || _        || _        y r)   )r*   ro   r+   _horz_px_per_unit_vert_px_per_unit_units_specifier)r   rb   r:   r>   r9   r.   s        r   r+   z_pHYsChunk.__init__   s*    j$(4!1!1 /r   c                     |j                  |      }|j                  |d      }|j                  |d      } | ||||      S )z{Return a _pHYsChunk instance containing the image resolution extracted from
        the pHYs chunk in `stream` at `offset`.rf   re   )rh   	read_byte)r   rb   r]   rc   r:   r>   r9   s          r   rr   z_pHYsChunk.from_offset   sM     &//7%//:$..vq9:/1A?SSr   c                     | j                   S r)   )r   r   s    r   r:   z_pHYsChunk.horz_px_per_unit       %%%r   c                     | j                   S r)   )r   r   s    r   r>   z_pHYsChunk.vert_px_per_unit   r   r   c                     | j                   S r)   )r   r   s    r   r9   z_pHYsChunk.units_specifier   s    $$$r   )r!   r"   r#   r$   r+   r&   rr   r%   r:   r>   r9   rD   rE   s   @r   ro   ro      s]    90 T T & & & & % %r   ro   N)	constantsr   r   
exceptionsr   helpersr   r   imager   r
   r   r0   rK   ra   rq   rn   ro   r   r   r   <module>r      sf    0 / - "</ <86 6r" "J$7 $7NA   * 2% %r   