
    Pqhl                        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 ddlmZmZmZmZ  G d d	      Zd
 Z G d d      Zy)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)Self)UnrecognizedImageError)EmuInchesLengthlazypropertyc                  <    e Zd ZdZd fdZedd       Zed        Zed        Z	e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.c                T    t         t        |           || _        || _        || _        y N)superr   __init___blob	_filename_image_header)selfblobfilenameimage_header	__class__s       Q/srv/www/ollama/chat-ollama/venv/lib/python3.12/site-packages/docx/image/image.pyr   zImage.__init__   s&    eT#%
!)    c                P    t        j                  |      }| 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   c                b   t        |t              r\|}t        |d      5 }|j                         }t	        j
                  |      }ddd       t        j                  j                  |      }n%|}|j                  d       |j                         }d}| j                  |      S # 1 sw Y   axY w)zReturn a new |Image| subclass instance loaded from the image file identified
        by `image_descriptor`, a path or file-like object.rbNr   )
isinstancestropenreadr   r   ospathbasenameseekr   )r    image_descriptorr*   fr   r!   r   s          r   	from_filezImage.from_file%   s     &,#DdD! *QvvxD)* ww''-H%FKKN;;=DHh77* *s   &B%%B.c                    | j                   S )zThe bytes of the image 'file'.)r   r   s    r   r   z
Image.blob6   s     zzr   c                .    | j                   j                  S )zIMIME content type for this image, e.g. ``'image/jpeg'`` for a JPEG image.)r   content_typer1   s    r   r3   zImage.content_type;   s     !!...r   c                `    t         j                  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   r1   s    r   extz	Image.ext@   s)     ww/21266r   c                    | j                   S )zpOriginal image file name, if loaded from disk, or a generic filename if
        loaded from an anonymous stream.)r   r1   s    r   r   zImage.filenameJ   s     ~~r   c                .    | j                   j                  S z,The horizontal pixel dimension of the image.)r   px_widthr1   s    r   r;   zImage.px_widthP   s     !!***r   c                .    | j                   j                  S z*The vertical pixel dimension of the image.)r   	px_heightr1   s    r   r>   zImage.px_heightU   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_dpir1   s    r   rA   zImage.horz_dpiZ        !!***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_dpir1   s    r   rE   zImage.vert_dpib   rB   r   c                F    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;   rA   r1   s    r   widthzImage.widthj   s     dmmdmm344r   c                F    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	   r>   rE   r1   s    r   heightzImage.heightp   s     dnnt}}455r   c                Z   ||| j                   | j                  fS |=|J t        |      t        | j                        z  }t        | j                   |z        }|9t        |      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.
        )rG   rI   floatroundr   )r   rG   rI   scaling_factors       r   scaled_dimensionszImage.scaled_dimensionsv   s    $ =V^::t{{**=%%%"6]U4;;-??N$**~56E>"5\E$**,==N4;;78F5z3v;&&r   c                \    t        j                  | j                        j                         S )z#SHA1 hash digest of the image blob.)hashlibsha1r   	hexdigestr1   s    r   rQ   z
Image.sha1   s      ||DJJ'1133r   c                N    t        |      }|d|j                  z  } | |||      S )zhReturn an instance of the |Image| subclass corresponding to the format of the
        image in `stream`.zimage.%s)_ImageHeaderFactorydefault_ext)r    r!   r   r   r   s        r   r   zImage._from_stream   s3     +62!L$<$<<H4<00r   )r   bytesr   r&   r   BaseImageHeader)r   rV   returnr   )rX   int)rX   r	   )NN)rG   
int | NonerI   rZ   rX   zTuple[Length, Length]r   )r!   z	IO[bytes]r   rV   r   z
str | NonerX   r   )__name__
__module____qualname____doc__r   classmethodr"   r/   propertyr   r3   r   r7   r   r;   r>   rA   rE   rG   rI   rN   rQ   r   __classcell__)r   s   @r   r   r      st   * . . 8 8    / / 7 7  
 + + , , + + + + 5 5
 6 6 >B''0:'	'@ 4 4 
  $	11 1 	1
 
1 1r   r   c                    ddl m} d } ||       }|D ]1  \  }}}|t        |      z   }||| }||k(  s |j                  |       c S  t        )znReturn a |BaseImageHeader| subclass instance that knows how to parse the headers
    of the image in `stream`.r   )
SIGNATURESc                F    | j                  d       | j                  d      S )Nr       )r,   r(   )r!   s    r   read_32z$_ImageHeaderFactory.<locals>.read_32   s    A{{2r   )
docx.imagerc   lenfrom_streamr   )	r!   rc   rf   headerr    offsetsignature_bytesendfound_bytess	            r   rT   rT      sg     & V_F(2 +$V_s?++VC(/)??6**	+
 ! r   c                  v    e Zd ZdZd Zed        Zed        Zed        Zed        Z	ed        Z
ed        Zy	)
rW   z>Base class for image header subclasses like |Jpeg| and |Tiff|.c                <    || _         || _        || _        || _        y r   )	_px_width
_px_height	_horz_dpi	_vert_dpi)r   r;   r>   rA   rE   s        r   r   zBaseImageHeader.__init__   s    !#!!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   r3   zBaseImageHeader.content_type   s     	 "#&&r   c                    d}t        |      )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 BaseImageHeaderrv   rx   s     r   rU   zBaseImageHeader.default_ext   s     	 "#&&r   c                    | j                   S r:   )rq   r1   s    r   r;   zBaseImageHeader.px_width   s     ~~r   c                    | j                   S r=   )rr   r1   s    r   r>   zBaseImageHeader.px_height   s     r   c                    | j                   S r@   )rs   r1   s    r   rA   zBaseImageHeader.horz_dpi        ~~r   c                    | j                   S rD   )rt   r1   s    r   rE   zBaseImageHeader.vert_dpi   r~   r   N)r[   r\   r]   r^   r   r`   r3   rU   r;   r>   rA   rE    r   r   rW   rW      s    H" ' ' 	' 	'        r   rW   )r^   
__future__r   rP   r   r)   typingr   r   typing_extensionsr   docx.image.exceptionsr   docx.sharedr   r	   r
   r   r   rT   rW   r   r   r   <module>r      sC    #  	 	  " 8 9 9S1 S1l!$6 6r   