Ë
    \Rði<  ã                   ó"  — d Z ddlZddlmZmZ ddlmZmZ ddlm	Z	 ddl
mZ  G d„ de	«      Z G d	„ d
e«      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y)zdObjects related to parsing headers of JPEG image streams.

Includes both JFIF and Exif sub-formats.
é    N)ÚJPEG_MARKER_CODEÚ	MIME_TYPE)Ú
BIG_ENDIANÚStreamReader)ÚBaseImageHeader)ÚTiffc                   ó0   — e Zd ZdZed„ «       Zed„ «       Zy)ÚJpegz(Base class for JFIF and EXIF subclasses.c                 ó"   — t         j                  S )zWMIME content type for this image, unconditionally `image/jpeg` for JPEG
        images.)r   ÚJPEG©Úselfs    úJ/var/www/html/Extract/venv/lib/python3.12/site-packages/docx/image/jpeg.pyÚcontent_typezJpeg.content_type   s   € ô �~‰~Ðó    c                  ó   — y)z8Default filename extension, always 'jpg' for JPG images.Újpg© r   s    r   Údefault_extzJpeg.default_ext   s   € ð r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úpropertyr   r   r   r   r   r
   r
      s+   „ Ù2àñó ðð
 ñó ñr   r
   c                   ó    — e Zd ZdZed„ «       Zy)ÚExifz*Image header parser for Exif image format.c                 óò   — t         j                  |«      }|j                  j                  }|j                  j                  }|j
                  j                  }|j
                  j                  } | ||||«      S )z[Return |Exif| instance having header properties parsed from Exif image in
        `stream`.)Ú_JfifMarkersÚfrom_streamÚsofÚpx_widthÚ	px_heightÚapp1Úhorz_dpiÚvert_dpi©ÚclsÚstreamÚmarkersr!   r"   r$   r%   s          r   r   zExif.from_stream    sc   € ô ×*Ñ*¨6Ó2ˆð —;‘;×'Ñ'ˆØ—K‘K×)Ñ)ˆ	Ø—<‘<×(Ñ(ˆØ—<‘<×(Ñ(ˆá�8˜Y¨°(Ó;Ð;r   N©r   r   r   r   Úclassmethodr   r   r   r   r   r      s   „ Ù4àñ<ó ñ<r   r   c                   ó    — e Zd ZdZed„ «       Zy)ÚJfifz*Image header parser for JFIF image format.c                 óò   — t         j                  |«      }|j                  j                  }|j                  j                  }|j
                  j                  }|j
                  j                  } | ||||«      S )zXReturn a |Jfif| instance having header properties parsed from image in
        `stream`.)r   r   r    r!   r"   Úapp0r$   r%   r&   s          r   r   zJfif.from_stream2   sa   € ô ×*Ñ*¨6Ó2ˆà—;‘;×'Ñ'ˆØ—K‘K×)Ñ)ˆ	Ø—<‘<×(Ñ(ˆØ—<‘<×(Ñ(ˆá�8˜Y¨°(Ó;Ð;r   Nr*   r   r   r   r-   r-   /   s   „ Ù4àñ
<ó ñ
<r   r-   c                   óh   ‡ — e Zd ZdZˆ fd„Zd„ Zed„ «       Zed„ «       Z	ed„ «       Z
ed„ «       Zˆ xZS )r   zfSequence of markers in a JPEG file, perhaps truncated at first SOS marker for
    performance reasons.c                 óJ   •— t         t        | �  «        t        |«      | _        y ©N)Úsuperr   Ú__init__ÚlistÚ_markers)r   r)   Ú	__class__s     €r   r4   z_JfifMarkers.__init__D   s   ø€ ÜŒl˜DÑ*Ô,Ü˜W›ˆ�r   c           	      óð   — d}d}g }| j                   D ]K  }|j                  ||j                  |j                  t	        |j
                  «      |j                  fz  «       ŒM |g|z   }dj                  |«      S )z{Returns a tabular listing of the markers in this instance, which can be handy
        for debugging and perhaps other uses.z4 offset  seglen  mc  name
=======  ======  ==  =====z%7d  %6d  %02X  %sú
)r6   ÚappendÚoffsetÚsegment_lengthÚordÚmarker_codeÚnameÚjoin)r   ÚheaderÚtmplÚrowsÚmarkerÚliness         r   Ú__str__z_JfifMarkers.__str__H   s�   € ð IˆØ#ˆØˆØ—m‘mò 		ˆFØ�K‰KØà—M‘MØ×)Ñ)Ü˜×*Ñ*Ó+Ø—K‘Kð	ñõð		ð �˜4‘ˆØ�y‰y˜ÓÐr   c                 óÊ   — t         j                  |«      }g }|j                  «       D ]2  }|j                  |«       |j                  t
        j                  k(  sŒ2 n  | |«      S )zrReturn a |_JfifMarkers| instance containing a |_JfifMarker| subclass instance
        for each marker in `stream`.)Ú_MarkerParserr   Úiter_markersr:   r>   r   ÚSOS)r'   r(   Úmarker_parserr)   rD   s        r   r   z_JfifMarkers.from_stream[   sa   € ô &×1Ñ1°&Ó9ˆØˆØ#×0Ñ0Ó2ò 	ˆFØ�N‰N˜6Ô"Ø×!Ñ!Ô%5×%9Ñ%9Ó9Ùð	ñ �7‹|Ðr   c                 ó|   — | j                   D ]#  }|j                  t        j                  k(  sŒ!|c S  t	        d«      ‚)z#First APP0 marker in image markers.zno APP0 marker in image)r6   r>   r   ÚAPP0ÚKeyError©r   Úms     r   r/   z_JfifMarkers.app0g   ó=   € ð —‘ò 	ˆAØ�}‰}Ô 0× 5Ñ 5Ó5Ø’ð	ô Ð0Ó1Ð1r   c                 ó|   — | j                   D ]#  }|j                  t        j                  k(  sŒ!|c S  t	        d«      ‚)z#First APP1 marker in image markers.zno APP1 marker in image)r6   r>   r   ÚAPP1rN   rO   s     r   r#   z_JfifMarkers.app1o   rQ   r   c                 óz   — | j                   D ]"  }|j                  t        j                  v sŒ |c S  t	        d«      ‚)z4First start of frame (SOFn) marker in this sequence.z(no start of frame (SOFn) marker in image)r6   r>   r   ÚSOF_MARKER_CODESrN   rO   s     r   r    z_JfifMarkers.sofw   s=   € ð —‘ò 	ˆAØ�}‰}Ô 0× AÑ AÒAØ’ð	ô ÐAÓBÐBr   )r   r   r   r   r4   rF   r+   r   r   r/   r#   r    Ú__classcell__©r7   s   @r   r   r   @   sd   ø„ ñô&ò ð& ñ	ó ð	ð ñ2ó ð2ð ñ2ó ð2ð ñCó ôCr   r   c                   ó8   ‡ — e Zd ZdZˆ fd„Zed„ «       Zd„ Zˆ xZS )rH   zUService class that knows how to parse a JFIF stream and iterate over its
    markers.c                 ó8   •— t         t        | �  «        || _        y r2   )r3   rH   r4   Ú_stream)r   Ústream_readerr7   s     €r   r4   z_MarkerParser.__init__„   s   ø€ ÜŒm˜TÑ+Ô-Ø$ˆ�r   c                 ó2   — t        |t        «      } | |«      S )zFReturn a |_MarkerParser| instance to parse JFIF markers from `stream`.)r   r   )r'   r(   r[   s      r   r   z_MarkerParser.from_streamˆ   s   € ô % V¬ZÓ8ˆÙ�=Ó!Ð!r   c              #   ó  K  — t         j                  | j                  «      }d}d}|t        j                  k7  rS|j                  |«      \  }}t        || j                  |«      }|–— ||j                  z   }|t        j                  k7  rŒRyy­w)z…Generate a (marker_code, segment_offset) 2-tuple for each marker in the JPEG
        `stream`, in the order they occur in the stream.r   N)Ú_MarkerFinderr   rZ   r   ÚEOIÚnextÚ_MarkerFactoryr<   )r   Úmarker_finderÚstartr>   Úsegment_offsetrD   s         r   rI   z_MarkerParser.iter_markersŽ   s‚   è ø€ ô &×1Ñ1°$·,±,Ó?ˆØˆØˆØÔ-×1Ñ1Ò1Ø*7×*<Ñ*<¸UÓ*CÑ'ˆK˜Ü# K°·±¸~ÓNˆFØŠLØ" V×%:Ñ%:Ñ:ˆEð	 Ô-×1Ñ1Õ1ùs   ‚BBÂB)	r   r   r   r   r4   r+   r   rI   rV   rW   s   @r   rH   rH   €   s&   ø„ ñô%ð ñ"ó ð"ö

;r   rH   c                   óJ   ‡ — e Zd ZdZˆ fd„Zed„ «       Zd„ Zd„ Zd„ Z	d„ Z
ˆ xZS )r^   zFService class that knows how to find the next JFIF marker in a stream.c                 ó8   •— t         t        | �  «        || _        y r2   )r3   r^   r4   rZ   )r   r(   r7   s     €r   r4   z_MarkerFinder.__init__ž   s   ø€ ÜŒm˜TÑ+Ô-Øˆ�r   c                 ó   —  | |«      S )zCReturn a |_MarkerFinder| instance to find JFIF markers in `stream`.r   )r'   r(   s     r   r   z_MarkerFinder.from_stream¢   s   € ñ �6‹{Ðr   c                 ó€   — |}	 | j                  |¬«      }| j                  |dz   ¬«      \  }}|dk(  rŒ1||dz   }}	 ||fS )aX  Return a (marker_code, segment_offset) 2-tuple identifying and locating the
        first marker in `stream` occuring after offset `start`.

        The returned `segment_offset` points to the position immediately following the
        2-byte marker code, the start of the marker segment, for those markers that have
        a segment.
        )rc   é   ó    )Ú_offset_of_next_ff_byteÚ_next_non_ff_byte)r   rc   ÚpositionÚbyte_r>   rd   s         r   r`   z_MarkerFinder.next§   s`   € ð ˆØà×3Ñ3¸(Ð3ÓCˆHà"×4Ñ4¸8Àa¹<Ð4ÓH‰OˆH�eà˜ÒØà*/°¸A±˜ˆKØØ˜NÐ*Ð*r   c                 óÐ   — | j                   j                  |«       | j                  «       }|dk(  r| j                  «       }|dk(  rŒ| j                   j                  «       dz
  }||fS )uù   Return an offset, byte 2-tuple for the next byte in `stream` that is not
        'Ã¿', starting with the byte at offset `start`.

        If the byte at offset `start` is not 'Ã¿', `start` and the returned `offset`
        will be the same.
        ó   ÿri   ©rZ   ÚseekÚ
_read_byteÚtell)r   rc   rn   Úoffset_of_non_ff_bytes       r   rl   z_MarkerFinder._next_non_ff_byte½   sc   € ð 	�‰×Ñ˜%Ô Ø—‘Ó!ˆØ�wÒØ—O‘OÓ%ˆEð �wÓà $§¡× 1Ñ 1Ó 3°aÑ 7ÐØ$ eÐ+Ð+r   c                 óÌ   — | j                   j                  |«       | j                  «       }|dk7  r| j                  «       }|dk7  rŒ| j                   j                  «       dz
  }|S )uç   Return the offset of the next 'Ã¿' byte in `stream` starting with the byte
        at offset `start`.

        Returns `start` if the byte at that offset is a hex 255; it does not necessarily
        advance in the stream.
        rp   ri   rq   )r   rc   rn   Úoffset_of_ff_bytes       r   rk   z%_MarkerFinder._offset_of_next_ff_byteË   s^   € ð 	�‰×Ñ˜%Ô Ø—‘Ó!ˆØ�wÒØ—O‘OÓ%ˆEð �wÓà ŸL™L×-Ñ-Ó/°!Ñ3ÐØ Ð r   c                 óV   — | j                   j                  d«      }|st        d«      ‚|S )zeReturn the next byte read from stream.

        Raise Exception if stream is at end of file.
        ri   zunexpected end of file)rZ   ÚreadÚ	Exception)r   rn   s     r   rs   z_MarkerFinder._read_byteÙ   s,   € ð
 —‘×!Ñ! !Ó$ˆÙÜÐ4Ó5Ð5Øˆr   )r   r   r   r   r4   r+   r   r`   rl   rk   rs   rV   rW   s   @r   r^   r^   ›   s3   ø„ ÙPôð ñó ðò+ò,,ò!ör   r^   c                 óÎ   — | t         j                  k(  rt        }n9| t         j                  k(  rt        }n| t         j
                  v rt        }nt        }|j                  || |«      S )znReturn |_Marker| or subclass instance appropriate for marker at `offset` in
    `stream` having `marker_code`.)	r   rM   Ú_App0MarkerrS   Ú_App1MarkerrU   Ú
_SofMarkerÚ_Markerr   )r>   r(   r;   Ú
marker_clss       r   ra   ra   ä   sZ   € ð Ô&×+Ñ+Ò+Ü ‰
Ø	Ô(×-Ñ-Ò	-Ü ‰
Ø	Ô(×9Ñ9Ñ	9Ü‰
äˆ
Ø×!Ñ! &¨+°vÓ>Ð>r   c                   ór   ‡ — e Zd ZdZˆ fd„Zed„ «       Zed„ «       Zed„ «       Z	ed„ «       Z
ed„ «       Zˆ xZS )r   zqBase class for JFIF marker classes.

    Represents a marker and its segment occuring in a JPEG byte stream.
    c                 óT   •— t         t        | �  «        || _        || _        || _        y r2   )r3   r   r4   Ú_marker_codeÚ_offsetÚ_segment_length)r   r>   r;   r<   r7   s       €r   r4   z_Marker.__init__ø   s'   ø€ ÜŒg�tÑ%Ô'Ø'ˆÔØˆŒØ-ˆÕr   c                 óh   — t        j                  |«      rd}n|j                  |«      } | |||«      S )zhReturn a generic |_Marker| instance for the marker at `offset` in `stream`
        having `marker_code`.r   )r   Úis_standaloneÚ
read_short)r'   r(   r>   r;   r<   s        r   r   z_Marker.from_streamþ   s6   € ô ×)Ñ)¨+Ô6Ø‰Nà#×.Ñ.¨vÓ6ˆNÙ�; ¨Ó7Ð7r   c                 ó   — | j                   S )um   The single-byte code that identifies the type of this marker, e.g. ``'Ã '``
        for start of image (SOI).)rƒ   r   s    r   r>   z_Marker.marker_code  s   € ð × Ñ Ð r   c                 ó<   — t         j                  | j                     S r2   )r   Úmarker_namesrƒ   r   s    r   r?   z_Marker.name  s   € ä×,Ñ,¨T×->Ñ->Ñ?Ð?r   c                 ó   — | j                   S r2   )r„   r   s    r   r;   z_Marker.offset  s   € à�|‰|Ðr   c                 ó   — | j                   S )z-The length in bytes of this marker's segment.)r…   r   s    r   r<   z_Marker.segment_length  s   € ð ×#Ñ#Ð#r   )r   r   r   r   r4   r+   r   r   r>   r?   r;   r<   rV   rW   s   @r   r   r   ò   ss   ø„ ñô
.ð ñ8ó ð8ð ñ!ó ð!ð
 ñ@ó ð@ð ñó ðð ñ$ó ô$r   r   c                   óX   ‡ — e Zd ZdZˆ fd„Zed„ «       Zed„ «       Zd„ Ze	d„ «       Z
ˆ xZS )r|   z&Represents a JFIF APP0 marker segment.c                 óZ   •— t         t        | �  |||«       || _        || _        || _        y r2   )r3   r|   r4   Ú_density_unitsÚ
_x_densityÚ
_y_density)r   r>   r;   ÚlengthÚdensity_unitsÚ	x_densityÚ	y_densityr7   s          €r   r4   z_App0Marker.__init__  s,   ø€ ÜŒk˜4Ñ)¨+°v¸vÔFØ+ˆÔØ#ˆŒØ#ˆ�r   c                 ó8   — | j                  | j                  «      S ©z[Horizontal dots per inch specified in this marker, defaults to 72 if not
        specified.)Ú_dpir‘   r   s    r   r$   z_App0Marker.horz_dpi%  ó   € ð �y‰y˜Ÿ™Ó)Ð)r   c                 ó8   — | j                  | j                  «      S ©zYVertical dots per inch specified in this marker, defaults to 72 if not
        specified.)r™   r’   r   s    r   r%   z_App0Marker.vert_dpi+  rš   r   c                 ó€   — | j                   dk(  r|}|S | j                   dk(  rt        t        |dz  «      «      }|S d}|S )z6Return dots per inch corresponding to `density` value.ri   é   gR¸…ëQ@éH   )r�   ÚintÚround)r   ÚdensityÚdpis      r   r™   z_App0Marker._dpi1  sR   € à×Ñ !Ò#ØˆCð
 ˆ
ð	 × Ñ  AÒ%Ü”e˜G d™NÓ+Ó,ˆCð ˆ
ð ˆCØˆ
r   c                 óª   — |j                  |«      }|j                  |d«      }|j                  |d«      }|j                  |d«      } | ||||||«      S )zUReturn an |_App0Marker| instance for the APP0 marker at `offset` in
        `stream`.é	   é
   é   )rˆ   Ú	read_byte)r'   r(   r>   r;   r<   r”   r•   r–   s           r   r   z_App0Marker.from_stream;  s`   € ð  ×*Ñ*¨6Ó2ˆØ×(Ñ(¨°Ó3ˆØ×%Ñ% f¨bÓ1ˆ	Ø×%Ñ% f¨bÓ1ˆ	Ù�; ¨¸ÀyÐR[Ó\Ð\r   )r   r   r   r   r4   r   r$   r%   r™   r+   r   rV   rW   s   @r   r|   r|     sN   ø„ Ù0ô$ð ñ*ó ð*ð
 ñ*ó ð*ò
ð ñ]ó ô]r   r|   c                   ór   ‡ — e Zd ZdZˆ fd„Zed„ «       Zed„ «       Zed„ «       Z	ed„ «       Z
ed„ «       Zˆ xZS )r}   z-Represents a JFIF APP1 (Exif) marker segment.c                 óL   •— t         t        | �  |||«       || _        || _        y r2   )r3   r}   r4   Ú	_horz_dpiÚ	_vert_dpi)r   r>   r;   r“   r$   r%   r7   s         €r   r4   z_App1Marker.__init__S  s$   ø€ ÜŒk˜4Ñ)¨+°v¸vÔFØ!ˆŒØ!ˆ�r   c                 óÆ   — |j                  |«      }| j                  ||«      r | |||dd«      S | j                  |||«      } | ||||j                  |j                  «      S )zmExtract the horizontal and vertical dots-per-inch value from the APP1 header
        at `offset` in `stream`.rŸ   )rˆ   Ú_is_non_Exif_APP1_segmentÚ_tiff_from_exif_segmentr$   r%   )r'   r(   r>   r;   r<   Útiffs         r   r   z_App1Marker.from_streamX  sf   € ð  ×*Ñ*¨6Ó2ˆØ×(Ñ(¨°Ô8Ù�{ F¨N¸BÀÓCÐCØ×*Ñ*¨6°6¸>ÓJˆÙ�; ¨¸¿¹ÀtÇ}Á}ÓUÐUr   c                 ó   — | j                   S r˜   )r«   r   s    r   r$   z_App1Marker.horz_dpij  ó   € ð �~‰~Ðr   c                 ó   — | j                   S rœ   )r¬   r   s    r   r%   z_App1Marker.vert_dpip  r²   r   c                 óV   — |j                  |dz   «       |j                  d«      }|dk7  S )z§Return True if the APP1 segment at `offset` in `stream` is NOT an Exif
        segment, as determined by the ``'Exif  '`` signature at offset 2 in the
        segment.rž   é   s   Exif  )rr   ry   )r'   r(   r;   Úexif_signatures       r   r®   z%_App1Marker._is_non_Exif_APP1_segmentv  s,   € ð
 	�‰�F˜Q‘JÔØŸ™ Q›ˆØ Ñ0Ð0r   c                 ó¦   — |j                  |dz   «       |j                  |dz
  «      }t        j                  |«      }t	        j
                  |«      S )zoReturn a |Tiff| instance parsed from the Exif APP1 segment of
        `segment_length` at `offset` in `stream`.é   )rr   ry   ÚioÚBytesIOr   r   )r'   r(   r;   r<   Úsegment_bytesÚ	substreams         r   r¯   z#_App1Marker._tiff_from_exif_segment  sF   € ð
 	�‰�F˜Q‘JÔØŸ™ N°QÑ$6Ó7ˆÜ—J‘J˜}Ó-ˆ	Ü×Ñ 	Ó*Ð*r   )r   r   r   r   r4   r+   r   r   r$   r%   r®   r¯   rV   rW   s   @r   r}   r}   P  sq   ø„ Ù7ô"ð
 ñVó ðVð" ñó ðð
 ñó ðð
 ñ1ó ð1ð ñ+ó ô+r   r}   c                   óR   ‡ — e Zd ZdZˆ fd„Zed„ «       Zed„ «       Zed„ «       Z	ˆ xZ
S )r~   z7Represents a JFIF start of frame (SOFx) marker segment.c                 óL   •— t         t        | �  |||«       || _        || _        y r2   )r3   r~   r4   Ú	_px_widthÚ
_px_height)r   r>   r;   r<   r!   r"   r7   s         €r   r4   z_SofMarker.__init__�  s$   ø€ ÜŒj˜$Ñ(¨°f¸nÔMØ!ˆŒØ#ˆ�r   c                 ó„   — |j                  |«      }|j                  |d«      }|j                  |d«      } | |||||«      S )zJReturn an |_SofMarker| instance for the SOFn marker at `offset` in stream.é   é   )rˆ   )r'   r(   r>   r;   r<   r"   r!   s          r   r   z_SofMarker.from_stream’  sK   € ð  ×*Ñ*¨6Ó2ˆØ×%Ñ% f¨aÓ0ˆ	Ø×$Ñ$ V¨QÓ/ˆÙ�; ¨¸À)ÓLÐLr   c                 ó   — | j                   S )zImage height in pixels.)rÀ   r   s    r   r"   z_SofMarker.px_height¡  s   € ð �‰Ðr   c                 ó   — | j                   S )zImage width in pixels.)r¿   r   s    r   r!   z_SofMarker.px_width¦  s   € ð �~‰~Ðr   )r   r   r   r   r4   r+   r   r   r"   r!   rV   rW   s   @r   r~   r~   Š  sI   ø„ ÙAô$ð
 ñMó ðMð ñó ðð ñó ôr   r~   )r   r¹   Údocx.image.constantsr   r   Údocx.image.helpersr   r   Údocx.image.imager   Údocx.image.tiffr   r
   r   r-   r   rH   r^   ra   r   r|   r}   r~   r   r   r   ú<module>rÊ      s›   ðñó
 
ç <ß 7Ý ,Ý  ôˆ?ô ô<ˆ4ô <ô$<ˆ4ô <÷"=Cñ =C÷@;ñ ;÷6Fñ FòR?÷'$ñ '$ôT1]�'ô 1]ôh7+�'ô 7+ôt�õ r   