o
    ýÞh‹(  ã                   @   sÈ   d dl mZmZmZ d dlmZ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G dd„ deƒZG dd„ deƒZdS )é   )Ú	MIME_TYPEÚTIFF_FLDÚTIFF_TAG)Ú
BIG_ENDIANÚLITTLE_ENDIANÚStreamReader)ÚBaseImageHeaderc                   @   s4   e Zd ZdZedd„ ƒZedd„ ƒZedd„ ƒZdS )	ÚTiffz`Image header parser for TIFF images.

    Handles both big and little endian byte ordering.
    c                 C   s   t jS )z[Return the MIME type of this TIFF image, unconditionally the string
        ``image/tiff``.)r   ÚTIFF©Úself© r   úO/var/www/html/premium_crap/venv/lib/python3.10/site-packages/docx/image/tiff.pyÚcontent_type   s   zTiff.content_typec                 C   ó   dS )z:Default filename extension, always 'tiff' for TIFF images.Útiffr   r   r   r   r   Údefault_ext   s   zTiff.default_extc                 C   s0   t  |¡}|j}|j}|j}|j}| ||||ƒS )zYReturn a |Tiff| instance containing the properties of the TIFF image in
        `stream`.)Ú_TiffParserÚparseÚpx_widthÚ	px_heightÚhorz_dpiÚvert_dpi)ÚclsÚstreamÚparserr   r   r   r   r   r   r   Úfrom_stream   s   
zTiff.from_streamN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úpropertyr   r   Úclassmethodr   r   r   r   r   r	      s    

r	   c                       s|   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dd„ Zedd„ ƒZ‡  ZS )r   zkParses a TIFF image stream to extract the image properties found in its main
    image file directory (IFD)c                    ó   t t| ƒ ¡  || _d S ©N)Úsuperr   Ú__init__Ú_ifd_entries)r   Úifd_entries©Ú	__class__r   r   r&   )   ó   
z_TiffParser.__init__c                 C   s(   |   |¡}| d¡}t ||¡}| |ƒS )zmReturn an instance of |_TiffParser| containing the properties parsed from the
        TIFF image in `stream`.é   )Ú_make_stream_readerÚ	read_longÚ_IfdEntriesr   )r   r   Ú
stream_rdrÚifd0_offsetr(   r   r   r   r   -   s   

z_TiffParser.parsec                 C   ó   |   tj¡S )zœThe horizontal dots per inch value calculated from the XResolution and
        ResolutionUnit tags of the IFD; defaults to 72 if those tags are not present.)Ú_dpir   ÚX_RESOLUTIONr   r   r   r   r   6   ó   z_TiffParser.horz_dpic                 C   r2   )zšThe vertical dots per inch value calculated from the XResolution and
        ResolutionUnit tags of the IFD; defaults to 72 if those tags are not present.)r3   r   ÚY_RESOLUTIONr   r   r   r   r   <   r5   z_TiffParser.vert_dpic                 C   ó   | j  tj¡S )z²The number of stacked rows of pixels in the image, |None| if the IFD contains
        no ``ImageLength`` tag, the expected case when the TIFF is embeded in an Exif
        image.)r'   Úgetr   ÚIMAGE_LENGTHr   r   r   r   r   B   ó   z_TiffParser.px_heightc                 C   r7   )z­The number of pixels in each row in the image, |None| if the IFD contains no
        ``ImageWidth`` tag, the expected case when the TIFF is embeded in an Exif
        image.)r'   r8   r   ÚIMAGE_WIDTHr   r   r   r   r   I   r:   z_TiffParser.px_widthc                 C   s$   |  d¡ | d¡}|dkrtS tS )z‹Return either BIG_ENDIAN or LITTLE_ENDIAN depending on the endian indicator
        found in the TIFF `stream` header, either 'MM' or 'II'.é    é   s   MM)ÚseekÚreadr   r   )r   r   Ú
endian_strr   r   r   Ú_detect_endianP   s   

z_TiffParser._detect_endianc                 C   sT   | j }||vr	dS | tjd¡}|dkrdS |dkrdnd}|| }tt|| ƒƒS )a!  Return the dpi value calculated for `resolution_tag`, which can be either
        TIFF_TAG.X_RESOLUTION or TIFF_TAG.Y_RESOLUTION.

        The calculation is based on the values of both that tag and the
        TIFF_TAG.RESOLUTION_UNIT tag in this parser's |_IfdEntries| instance.
        éH   r=   r   gR¸…ëQ@)r'   r8   r   ÚRESOLUTION_UNITÚintÚround)r   Úresolution_tagr(   Úresolution_unitÚunits_per_inchÚdots_per_unitr   r   r   r3   X   s   z_TiffParser._dpic                 C   s   |   |¡}t||ƒS )z�Return a |StreamReader| instance with wrapping `stream` and having "endian-
        ness" determined by the 'MM' or 'II' indicator in the TIFF stream header.)rA   r   )r   r   Úendianr   r   r   r-   n   s   

z_TiffParser._make_stream_reader)r   r   r   r    r&   r"   r   r!   r   r   r   r   rA   r3   r-   Ú__classcell__r   r   r)   r   r   %   s$    





r   c                       sF   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zedd	„ ƒZddd„Z	‡  Z
S )r/   z}Image File Directory for a TIFF image, having mapping (dict) semantics allowing
    "tag" values to be retrieved by tag code.c                    r#   r$   )r%   r/   r&   Ú_entries)r   Úentriesr)   r   r   r&   z   r+   z_IfdEntries.__init__c                 C   ó   | j  |¡S )z5Provides ``in`` operator, e.g. ``tag in ifd_entries``)rL   Ú__contains__©r   Úkeyr   r   r   rO   ~   ó   z_IfdEntries.__contains__c                 C   rN   )zCProvides indexed access, e.g. ``tag_value = ifd_entries[tag_code]``)rL   Ú__getitem__rP   r   r   r   rS   ‚   rR   z_IfdEntries.__getitem__c                 C   s$   t ||ƒ}dd„ | ¡ D ƒ}| |ƒS )zVReturn a new |_IfdEntries| instance parsed from `stream` starting at
        `offset`.c                 S   s   i | ]}|j |j“qS r   )ÚtagÚvalue)Ú.0Úer   r   r   Ú
<dictcomp>‹   s    z+_IfdEntries.from_stream.<locals>.<dictcomp>)Ú
_IfdParserÚiter_entries)r   r   ÚoffsetÚ
ifd_parserrM   r   r   r   r   †   s   
z_IfdEntries.from_streamNc                 C   s   | j  ||¡S )zhReturn value of IFD entry having tag matching `tag_code`, or `default` if no
        matching tag found.)rL   r8   )r   Útag_codeÚdefaultr   r   r   r8   Ž   ó   z_IfdEntries.getr$   )r   r   r   r    r&   rO   rS   r"   r   r8   rK   r   r   r)   r   r/   v   s    
r/   c                       s4   e Zd ZdZ‡ fdd„Zdd„ Zedd„ ƒZ‡  ZS )rY   zaService object that knows how to extract directory entries from an Image File
    Directory (IFD)c                    ó   t t| ƒ ¡  || _|| _d S r$   )r%   rY   r&   Ú_stream_rdrÚ_offset)r   r0   r[   r)   r   r   r&   ˜   ó   
z_IfdParser.__init__c                 c   s:   � t | jƒD ]}| jd |d  }t| j|ƒ}|V  qdS )zVGenerate an |_IfdEntry| instance corresponding to each entry in the
        directory.r=   é   N)ÚrangeÚ_entry_countrb   Ú_IfdEntryFactoryra   )r   ÚidxÚdir_entry_offsetÚ	ifd_entryr   r   r   rZ   �   s   €ýz_IfdParser.iter_entriesc                 C   s   | j  | j¡S )zDThe count of directory entries, read from the top of the IFD header.)ra   Ú
read_shortrb   r   r   r   r   rf   ¥   r_   z_IfdParser._entry_count)	r   r   r   r    r&   rZ   r!   rf   rK   r   r   r)   r   rY   ”   s    rY   c                 C   s@   t jtt jtt jtt jti}|  	|d¡}| 
|t¡}| | |¡S )ztReturn an |_IfdEntry| subclass instance containing the value of the directory
    entry at `offset` in `stream_rdr`.r=   )r   ÚASCIIÚ_AsciiIfdEntryÚSHORTÚ_ShortIfdEntryÚLONGÚ_LongIfdEntryÚRATIONALÚ_RationalIfdEntryrk   r8   Ú	_IfdEntryr   )r0   r[   Úifd_entry_classesÚ
field_typeÚEntryClsr   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 )rt   zsBase class for IFD entry classes.

    Subclasses are differentiated by value type, e.g. ASCII, long int, etc.
    c                    r`   r$   )r%   rt   r&   Ú	_tag_codeÚ_value)r   r]   rU   r)   r   r   r&   ¿   rc   z_IfdEntry.__init__c                 C   s>   |  |d¡}| |d¡}| |d¡}|  ||||¡}| ||ƒS )a!  Return an |_IfdEntry| subclass instance containing the tag and value of the
        tag parsed from `stream_rdr` at `offset`.

        Note this method is common to all subclasses. Override the ``_parse_value()``
        method to provide distinctive behavior based on field type.
        r<   r,   é   )rk   r.   Ú_parse_value)r   r0   r[   r]   Úvalue_countÚvalue_offsetrU   r   r   r   r   Ä   s
   
z_IfdEntry.from_streamc                 C   r   )zReturn the value of this field parsed from `stream_rdr` at `offset`.

        Intended to be overridden by subclasses.
        zUNIMPLEMENTED FIELD TYPEr   ©r   r0   r[   r|   r}   r   r   r   r{   Ò   s   z_IfdEntry._parse_valuec                 C   ó   | j S )z.Short int code that identifies this IFD entry.)rx   r   r   r   r   rT   Ú   ó   z_IfdEntry.tagc                 C   r   )z7Value of this tag, its type being dependent on the tag.)ry   r   r   r   r   rU   ß   r€   z_IfdEntry.value)r   r   r   r    r&   r"   r   r{   r!   rT   rU   rK   r   r   r)   r   rt   ¹   s    


rt   c                   @   ó   e Zd ZdZedd„ ƒZdS )rm   z<IFD entry having the form of a NULL-terminated ASCII string.c                 C   s   |  |d |¡S )z¹Return the ASCII string parsed from `stream_rdr` at `value_offset`.

        The length of the string, including a terminating ' ' (NUL) character, is in
        `value_count`.
        r   )Úread_strr~   r   r   r   r{   è   s   z_AsciiIfdEntry._parse_valueN©r   r   r   r    r"   r{   r   r   r   r   rm   å   ó    rm   c                   @   r�   )ro   z0IFD entry expressed as a short (2-byte) integer.c                 C   ó   |dkr
|  |d¡S dS )z‘Return the short int value contained in the `value_offset` field of this
        entry.

        Only supports single values at present.
        r   rz   z)Multi-value short integer NOT IMPLEMENTED)rk   r~   r   r   r   r{   õ   ó   z_ShortIfdEntry._parse_valueNrƒ   r   r   r   r   ro   ò   r„   ro   c                   @   r�   )rq   z/IFD entry expressed as a long (4-byte) integer.c                 C   r…   )z�Return the long int value contained in the `value_offset` field of this
        entry.

        Only supports single values at present.
        r   rz   z(Multi-value long integer NOT IMPLEMENTED©r.   r~   r   r   r   r{     r†   z_LongIfdEntry._parse_valueNrƒ   r   r   r   r   rq     r„   rq   c                   @   r�   )rs   z5IFD entry expressed as a numerator, denominator pair.c                 C   s*   |dkr|  |¡}|  |d¡}|| S dS )z³Return the rational (numerator / denominator) value at `value_offset` in
        `stream_rdr` as a floating-point number.

        Only supports single values at present.
        r   r,   z$Multi-value Rational NOT IMPLEMENTEDr‡   )r   r0   r[   r|   r}   Ú	numeratorÚdenominatorr   r   r   r{     s
   
z_RationalIfdEntry._parse_valueNrƒ   r   r   r   r   rs     r„   rs   N)Ú	constantsr   r   r   Úhelpersr   r   r   Úimager   r	   r   r/   rY   rg   rt   rm   ro   rq   rs   r   r   r   r   Ú<module>   s    Q,