
    hi(                     4   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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 G d de          Z G d de          ZdS )   )	MIME_TYPETIFF_FLDTIFF_TAG)
BIG_ENDIANLITTLE_ENDIANStreamReader)BaseImageHeaderc                   T    e Zd ZdZed             Zed             Zed             ZdS )Tiffz`Image header parser for TIFF images.

    Handles both big and little endian byte ordering.
    c                     t           j        S )z[Return the MIME type of this TIFF image, unconditionally the string
        ``image/tiff``.)r   TIFFselfs    g/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/docx/image/tiff.pycontent_typezTiff.content_type   s     ~    c                     dS )z:Default filename extension, always 'tiff' for TIFF images.tiff r   s    r   default_extzTiff.default_ext   s	     vr   c                     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   s          r   from_streamzTiff.from_stream   sJ     ""6**?$	??s8Y(;;;r   N)	__name__
__module____qualname____doc__propertyr   r   classmethodr!   r   r   r   r   r      so         
   X
   X 
< 
< [
< 
< 
<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d	 Zed
             Z xZS )r   zkParses a TIFF image stream to extract the image properties found in its main
    image file directory (IFD)c                 d    t          t          |                                            || _        d S N)superr   __init___ifd_entries)r   ifd_entries	__class__s     r   r,   z_TiffParser.__init__)   s-    k4  ))+++'r   c                     |                      |          }|                    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.   s        r   r   z_TiffParser.parse-   sO     ,,V44
 **1--!--j+FFs;r   c                 @    |                      t          j                  S )zThe 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   s    r   r   z_TiffParser.horz_dpi6        yy.///r   c                 @    |                      t          j                  S )zThe vertical dots per inch value calculated from the XResolution and
        ResolutionUnit tags of the IFD; defaults to 72 if those tags are not present.)r8   r   Y_RESOLUTIONr   s    r   r   z_TiffParser.vert_dpi<   r:   r   c                 J    | j                             t          j                  S )zThe 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   s    r   r   z_TiffParser.px_heightB   s    
  $$X%:;;;r   c                 J    | j                             t          j                  S )zThe 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-   r>   r   IMAGE_WIDTHr   s    r   r   z_TiffParser.px_widthI   s    
  $$X%9:::r   c                 ~    |                     d           |                    d          }|dk    rt          nt          S )zReturn 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_strs      r   _detect_endianz_TiffParser._detect_endianP   s6     	A[[^^
'500zzmCr   c                     | j         }||vrdS |                    t          j        d          }|dk    rdS |dk    rdnd}||         }t	          t          ||z                      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   rD   r   gRQ@)r-   r>   r   RESOLUTION_UNITintround)r   resolution_tagr.   resolution_unitunits_per_inchdots_per_units         r   r8   z_TiffParser._dpiX   s|     ',,2 &//(*BAFFa2-22#N35788999r   c                 L    |                      |          }t          ||          S )zReturn a |StreamReader| instance with wrapping `stream` and having "endian-
        ness" determined by the 'MM' or 'II' indicator in the TIFF stream header.)rH   r   )r   r   endians      r   r2   z_TiffParser._make_stream_readern   s'     ##F++FF+++r   )r"   r#   r$   r%   r,   r'   r   r&   r   r   r   r   rH   r8   r2   __classcell__r/   s   @r   r   r   %   s       " "( ( ( ( (     [  0 0 X0
 0 0 X0
 < < X< ; ; X; D D [D: : :, , , [, , , , ,r   r   c                   L     e Zd ZdZ fdZd Zd Zed             ZddZ	 xZ
S )	r4   z}Image File Directory for a TIFF image, having mapping (dict) semantics allowing
    "tag" values to be retrieved by tag code.c                 d    t          t          |                                            || _        d S r*   )r+   r4   r,   _entries)r   entriesr/   s     r   r,   z_IfdEntries.__init__z   s*    k4  ))+++r   c                 6    | j                             |          S )z5Provides ``in`` operator, e.g. ``tag in ifd_entries``)rX   __contains__r   keys     r   r[   z_IfdEntries.__contains__~   s    }))#...r   c                 6    | j                             |          S )zCProvides indexed access, e.g. ``tag_value = ifd_entries[tag_code]``)rX   __getitem__r\   s     r   r_   z_IfdEntries.__getitem__   s    }((---r   c                 t    t          ||          }d |                                D             } | |          S )zVReturn a new |_IfdEntries| instance parsed from `stream` starting at
        `offset`.c                 (    i | ]}|j         |j        S r   )tagvalue).0es     r   
<dictcomp>z+_IfdEntries.from_stream.<locals>.<dictcomp>   s    EEEa15!'EEEr   )
_IfdParseriter_entries)r   r   offset
ifd_parserrY   s        r   r!   z_IfdEntries.from_stream   s@      //
EE:+B+B+D+DEEEs7||r   Nc                 8    | j                             ||          S )zhReturn value of IFD entry having tag matching `tag_code`, or `default` if no
        matching tag found.)rX   r>   )r   tag_codedefaults      r   r>   z_IfdEntries.get   s     }  7333r   r*   )r"   r#   r$   r%   r,   r[   r_   r'   r!   r>   rT   rU   s   @r   r4   r4   v   s        1 1         / / /. . .   [4 4 4 4 4 4 4 4r   r4   c                   >     e Zd ZdZ fdZd Zed             Z xZS )rg   zaService object that knows how to extract directory entries from an Image File
    Directory (IFD)c                 r    t          t          |                                            || _        || _        d S r*   )r+   rg   r,   _stream_rdr_offset)r   r5   ri   r/   s      r   r,   z_IfdParser.__init__   s2    j$((***%r   c              #      K   t          | j                  D ]+}| j        dz   |dz  z   }t          | j        |          }|V  ,dS )zVGenerate an |_IfdEntry| instance corresponding to each entry in the
        directory.rD      N)range_entry_countrq   _IfdEntryFactoryrp   )r   idxdir_entry_offset	ifd_entrys       r   rh   z_IfdParser.iter_entries   s`       *++ 	 	C#|a/38<()9;KLLIOOOO	 	r   c                 @    | j                             | j                  S )zDThe count of directory entries, read from the top of the IFD header.)rp   
read_shortrq   r   s    r   ru   z_IfdParser._entry_count   s     **4<888r   )	r"   r#   r$   r%   r,   rh   r&   ru   rT   rU   s   @r   rg   rg      sl             
   9 9 X9 9 9 9 9r   rg   c                    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`.rD   )r   ASCII_AsciiIfdEntrySHORT_ShortIfdEntryLONG_LongIfdEntryRATIONAL_RationalIfdEntryr{   r>   	_IfdEntryr!   )r5   ri   ifd_entry_classes
field_typeEntryClss        r   rv   rv      sl     	},	 &&vq11J $$Z;;H
F333r   c                   z     e Zd ZdZ fdZed             Zed             Zed             Z	ed             Z
 xZS )r   zsBase class for IFD entry classes.

    Subclasses are differentiated by value type, e.g. ASCII, long int, etc.
    c                 r    t          t          |                                            || _        || _        d S r*   )r+   r   r,   	_tag_code_value)r   rl   rc   r/   s      r   r,   z_IfdEntry.__init__   s1    i'')))!r   c                     |                     |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.
        rC   r1      )r{   r3   _parse_value)r   r5   ri   rl   value_countvalue_offsetrc   s          r   r!   z_IfdEntry.from_stream   sn     ((33 **6155!++FA66  V[,OOs8U###r   c                     dS )zReturn the value of this field parsed from `stream_rdr` at `offset`.

        Intended to be overridden by subclasses.
        zUNIMPLEMENTED FIELD TYPEr   r   r5   ri   r   r   s        r   r   z_IfdEntry._parse_value   s
     *)r   c                     | j         S )z.Short int code that identifies this IFD entry.)r   r   s    r   rb   z_IfdEntry.tag   s     ~r   c                     | j         S )z7Value of this tag, its type being dependent on the tag.)r   r   s    r   rc   z_IfdEntry.value   s     {r   )r"   r#   r$   r%   r,   r'   r!   r   r&   rb   rc   rT   rU   s   @r   r   r      s         
    
 $ $ [$ * * [*   X   X    r   r   c                   (    e Zd ZdZed             ZdS )r~   z<IFD entry having the form of a NULL-terminated ASCII string.c                 4    |                     |dz
  |          S )zReturn 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   s        r   r   z_AsciiIfdEntry._parse_value   s     "";?LAAAr   Nr"   r#   r$   r%   r'   r   r   r   r   r~   r~      s8        FFB B [B B Br   r~   c                   (    e Zd ZdZed             ZdS )r   z0IFD entry expressed as a short (2-byte) integer.c                 >    |dk    r|                     |d          S dS )zReturn the short int value contained in the `value_offset` field of this
        entry.

        Only supports single values at present.
        r   r   z)Multi-value short integer NOT IMPLEMENTED)r{   r   s        r   r   z_ShortIfdEntry._parse_value   s+     !((333>>r   Nr   r   r   r   r   r      s3        ::	? 	? [	? 	? 	?r   r   c                   (    e Zd ZdZed             ZdS )r   z/IFD entry expressed as a long (4-byte) integer.c                 >    |dk    r|                     |d          S dS )zReturn the long int value contained in the `value_offset` field of this
        entry.

        Only supports single values at present.
        r   r   z(Multi-value long integer NOT IMPLEMENTEDr3   r   s        r   r   z_LongIfdEntry._parse_value  s+     !''222==r   Nr   r   r   r   r   r     s3        99	> 	> [	> 	> 	>r   r   c                   (    e Zd ZdZed             ZdS )r   z5IFD entry expressed as a numerator, denominator pair.c                 r    |dk    r0|                     |          }|                     |d          }||z  S dS )zReturn the rational (numerator / denominator) value at `value_offset` in
        `stream_rdr` as a floating-point number.

        Only supports single values at present.
        r   r1   z$Multi-value Rational NOT IMPLEMENTEDr   )r   r5   ri   r   r   	numeratordenominators          r   r   z_RationalIfdEntry._parse_value  sH     !",,\::I$..|Q??K{**99r   Nr   r   r   r   r   r     s3        ??: : [: : :r   r   N)	constantsr   r   r   helpersr   r   r   imager	   r   r   r4   rg   rv   r   r~   r   r   r   r   r   r   <module>r      s   4 4 4 4 4 4 4 4 4 4 < < < < < < < < < < " " " " " "< < < < <? < < <>N, N, N, N, N, N, N, N,b4 4 4 4 4 4 4 4<9 9 9 9 9 9 9 9.4 4 4) ) ) ) ) ) ) )X
B 
B 
B 
B 
BY 
B 
B 
B? ? ? ? ?Y ? ? ? > > > > >I > > > : : : : :	 : : : : :r   