
    hiE                        d Z ddlmZ ddlZddlZddlZddlmZmZ ddl	m
Z
 ddlmZmZmZmZ  G d d          ZddZ G d d          ZdS )zProvides objects that can characterize image streams.

That characterization is as to content type and size, as a required step in including
them in a document.
    )annotationsN)IOTuple)UnrecognizedImageError)EmuInchesLengthlazypropertyc                  |    e Zd ZdZd% fdZed&d
            Zed'd            Zed             Z	ed(d            Z
ed             Ze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,dZed              Ze	 d-d.d$            Z xZS )/ImagezgGraphical image stream such as JPEG, PNG, or GIF with properties and methods
    required by ImagePart.blobbytesfilenamestrimage_headerBaseImageHeaderc                    t          t          |                                            || _        || _        || _        d S N)superr   __init___blob	_filename_image_header)selfr   r   r   	__class__s       h/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/docx/image/image.pyr   zImage.__init__   s;    eT##%%%
!)    returnc                V    t          j        |          }|                     ||          S )z`Return a new |Image| subclass instance parsed from the image binary contained
        in `blob`.)ioBytesIO_from_stream)clsr   streams      r   	from_blobzImage.from_blob   s)     D!!---r   image_descriptorstr | IO[bytes]c                   t          |t                    rr|}t          |d          5 }|                                }t	          j        |          }ddd           n# 1 swxY w Y   t          j                            |          }n-|}|	                    d           |                                }d}| 
                    |||          S )zReturn a new |Image| subclass instance loaded from the image file identified
        by `image_descriptor`, a path or file-like object.rbNr   )
isinstancer   openreadr    r!   ospathbasenameseekr"   )r#   r&   r.   fr   r$   r   s          r   	from_filezImage.from_file#   s     &,, 
	#DdD!! *QvvxxD))* * * * * * * * * * * * * * * w''--HH%FKKNNN;;==DHh777s   )AA!$A!c                    | j         S )zThe bytes of the image 'file'.)r   r   s    r   r   z
Image.blob4   s     zr   c                    | j         j        S )zIMIME content type for this image, e.g. ``'image/jpeg'`` for a JPEG image.)r   content_typer4   s    r   r6   zImage.content_type9   s     !..r   c                f    t           j                            | j                  d         dd         S )a  The file extension for the image.

        If an actual one is available from a load filename it is used. Otherwise a
        canonical extension is assigned based on the content type. Does not contain the
        leading period, e.g. 'jpg', not '.jpg'.
           N)r-   r.   splitextr   r4   s    r   extz	Image.ext>   s*     w//212266r   c                    | j         S )zpOriginal image file name, if loaded from disk, or a generic filename if
        loaded from an anonymous stream.)r   r4   s    r   r   zImage.filenameH   s     ~r   intc                    | j         j        S z,The horizontal pixel dimension of the image.)r   px_widthr4   s    r   r?   zImage.px_widthN   s     !**r   c                    | j         j        S z*The vertical pixel dimension of the image.)r   	px_heightr4   s    r   rB   zImage.px_heightS   s     !++r   c                    | 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.
        )r   horz_dpir4   s    r   rE   zImage.horz_dpiX        !**r   c                    | 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.
        )r   vert_dpir4   s    r   rI   zImage.vert_dpi`   rF   r   r   c                :    t          | j        | j        z            S )z}A |Length| value representing the native width of the image, calculated from
        the values of `px_width` and `horz_dpi`.)r   r?   rE   r4   s    r   widthzImage.widthh   s     dmdm3444r   c                :    t          | j        | j        z            S )zA |Length| value representing the native height of the image, calculated from
        the values of `px_height` and `vert_dpi`.)r   rB   rI   r4   s    r   heightzImage.heightn   s     dnt}4555r   NrK   int | Length | NonerM   Tuple[Length, Length]c                ^   ||| j         | j        fS |?|J t          |          t          | j                  z  }t          | j         |z            }|;t          |          t          | j                   z  }t          | j        |z            }t	          |          t	          |          fS )a?  (cx, cy) pair representing scaled dimensions of this image.

        The native dimensions of the image are scaled by applying the following rules to
        the `width` and `height` arguments.

        * If both `width` and `height` are specified, the return value is (`width`,
        `height`); no scaling is performed.
        * If only one is specified, it is used to compute a scaling factor that is then
        applied to the unspecified dimension, preserving the aspect ratio of the image.
        * If both `width` and `height` are |None|, the native dimensions are returned.

        The native dimensions are calculated using the dots-per-inch (dpi) value
        embedded in the image, defaulting to 72 dpi if no value is specified, as is
        often the case. The returned values are both |Length| objects.
        )rK   rM   floatroundr   )r   rK   rM   scaling_factors       r   scaled_dimensionszImage.scaled_dimensionst   s    $ =V^:t{**=%%%"6]]U4;-?-??N$*~566E>"5\\E$*,=,==N4;788F5zz3v;;&&r   c                X    t          j        | j                                                  S )z#SHA1 hash digest of the image blob.)hashlibsha1r   	hexdigestr4   s    r   rW   z
Image.sha1   s"     |DJ''11333r   r$   	IO[bytes]
str | Nonec                R    t          |          }|
d|j        z  } | |||          S )zhReturn an instance of the |Image| subclass corresponding to the format of the
        image in `stream`.Nzimage.%s)_ImageHeaderFactorydefault_ext)r#   r$   r   r   r   s        r   r"   zImage._from_stream   s8     +622!L$<<Hs4<000r   )r   r   r   r   r   r   )r   r   r   r   )r&   r'   r   r   )r   r<   )r   r   )NN)rK   rN   rM   rN   r   rO   r   )r$   rY   r   r   r   rZ   r   r   )__name__
__module____qualname____doc__r   classmethodr%   r2   propertyr   r6   r
   r:   r   r?   rB   rE   rI   rK   rM   rT   rW   r"   __classcell__)r   s   @r   r   r      s        * * * * * * . . . [. 8 8 8 [8    X / / / X/ 7 7 \7   X
 + + + X+ , , , X, + + + X+ + + + X+ 5 5 5 X5
 6 6 6 X6 PT' ' ' ' '@ 4 4 \4 
  $	1 1 1 1 [1 1 1 1 1r   r   r$   rY   c                    ddl m} dd} ||           }|D ]?\  }}}|t          |          z   }|||         }||k    r|                    |           c S @t          )zRA |BaseImageHeader| subclass instance that can parse headers of image in `stream`.r   )
SIGNATURESr$   rY   c                V    |                      d           |                     d          S )Nr       )r0   r,   )r$   s    r   read_32z$_ImageHeaderFactory.<locals>.read_32   s!    A{{2r   Nr$   rY   )
docx.imagerg   lenfrom_streamr   )	r$   rg   rj   headerr#   offsetsignature_bytesendfound_bytess	            r   r\   r\      s    %%%%%%    WV__F(2 + +$V_s?+++VCZ(/))??6***** *
  r   c                      e Zd ZdZddZedd
            Zedd            Zed             Zed             Z	ed             Z
ed             ZdS )r   z>Base class for image header subclasses like |Jpeg| and |Tiff|.r?   r<   rB   rE   rI   c                >    || _         || _        || _        || _        d S r   )	_px_width
_px_height	_horz_dpi	_vert_dpi)r   r?   rB   rE   rI   s        r   r   zBaseImageHeader.__init__   s"    !#!!r   r   r   c                $    d}t          |          )zDAbstract property definition, must be implemented by all subclasses.zNcontent_type property must be implemented by all subclasses of BaseImageHeaderNotImplementedError)r   msgs     r   r6   zBaseImageHeader.content_type   s     _!#&&&r   c                     t          d          )zDefault filename extension for images of this type.

        An abstract property definition, must be implemented by all subclasses.
        zMdefault_ext property must be implemented by all subclasses of BaseImageHeaderr{   r4   s    r   r]   zBaseImageHeader.default_ext   s     "[
 
 	
r   c                    | j         S r>   )rv   r4   s    r   r?   zBaseImageHeader.px_width   s     ~r   c                    | j         S rA   )rw   r4   s    r   rB   zBaseImageHeader.px_height   s     r   c                    | j         S rD   )rx   r4   s    r   rE   zBaseImageHeader.horz_dpi        ~r   c                    | j         S rH   )ry   r4   s    r   rI   zBaseImageHeader.vert_dpi   r   r   N)r?   r<   rB   r<   rE   r<   rI   r<   r^   )r_   r`   ra   rb   r   rd   r6   r]   r?   rB   rE   rI    r   r   r   r      s        HH" " " " ' ' ' X'
 
 
 
 X
   X   X   X   X  r   r   rk   )rb   
__future__r   rV   r    r-   typingr   r   docx.image.exceptionsr   docx.sharedr   r   r	   r
   r   r\   r   r   r   r   <module>r      s    # " " " " "  				 				         8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9S1 S1 S1 S1 S1 S1 S1 S1l! ! ! !"1 1 1 1 1 1 1 1 1 1r   