
    h><                     "   S r SSKrSSKJrJr  SSKJrJr  SSKJ	r	  SSK
Jr   " S S\	5      r " S	 S
\5      r " S S\5      r " S S5      r " S S5      r " S S5      rS r " S S5      r " S S\5      r " S S\5      r " S S\5      rg)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                   8    \ rS rSrSr\S 5       r\S 5       rSrg)Jpeg   z(Base class for JFIF and EXIF subclasses.c                 "    [         R                  $ )zOMIME content type for this image, unconditionally `image/jpeg` for JPEG
images.)r   JPEGselfs    A/var/www/html/env/lib/python3.13/site-packages/docx/image/jpeg.pycontent_typeJpeg.content_type   s     ~~    c                     g)z8Default filename extension, always 'jpg' for JPG images.jpg r   s    r   default_extJpeg.default_ext   s     r   r   N)	__name__
__module____qualname____firstlineno____doc__propertyr   r   __static_attributes__r   r   r   r
   r
      s+    2 
  r   r
   c                   (    \ rS rSrSr\S 5       rSrg)Exif   z*Image header parser for Exif image format.c                     [         R                  U5      nUR                  R                  nUR                  R                  nUR
                  R                  nUR
                  R                  nU " X4XV5      $ )zSReturn |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%   Exif.from_stream    s_     **62 ;;''KK))	<<((<<((8;;r   r   Nr   r   r   r   r   classmethodr%   r   r   r   r   r!   r!      s    4< <r   r!   c                   (    \ rS rSrSr\S 5       rSrg)Jfif/   z*Image header parser for JFIF image format.c                     [         R                  U5      nUR                  R                  nUR                  R                  nUR
                  R                  nUR
                  R                  nU " X4XV5      $ )zPReturn a |Jfif| instance having header properties parsed from image in
`stream`.)r$   r%   r&   r'   r(   app0r*   r+   r,   s          r   r%   Jfif.from_stream2   s]     **62;;''KK))	<<((<<((8;;r   r   Nr1   r   r   r   r4   r4   /   s    4
< 
<r   r4   c                   r   ^  \ rS rSrSrU 4S jrS r\S 5       r\	S 5       r
\	S 5       r\	S 5       rS	rU =r$ )
r$   @   zbSequence of markers in a JPEG file, perhaps truncated at first SOS marker for
performance reasons.c                 J   > [         [        U ]  5         [        U5      U l        g N)superr$   __init__list_markers)r   r/   	__class__s     r   r>   _JfifMarkers.__init__D   s    lD*,Wr   c           	          SnSn/ nU R                    HL  nUR                  UUR                  UR                  [	        UR
                  5      UR                  4-  5        MN     U/U-   nSR                  U5      $ )zsReturns 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
)r@   appendoffsetsegment_lengthordmarker_codenamejoin)r   headertmplrowsmarkerliness         r   __str___JfifMarkers.__str__H   s|     I#mmFKKMM))**+KK	 $ 4yyr   c                     [         R                  U5      n/ nUR                  5        H4  nUR                  U5        UR                  [
        R                  :X  d  M4    O   U " U5      $ )zjReturn a |_JfifMarkers| instance containing a |_JfifMarker| subclass instance
for each marker in `stream`.)_MarkerParserr%   iter_markersrE   rI   r   SOS)r-   r.   marker_parserr/   rO   s        r   r%   _JfifMarkers.from_stream[   s\     &11&9#002FNN6"!!%5%9%99 3 7|r   c                     U R                    H%  nUR                  [        R                  :X  d  M#  Us  $    [	        S5      e)z#First APP0 marker in image markers.zno APP0 marker in image)r@   rI   r   APP0KeyErrorr   ms     r   r7   _JfifMarkers.app0g   8     A}} 0 5 55  011r   c                     U R                    H%  nUR                  [        R                  :X  d  M#  Us  $    [	        S5      e)z#First APP1 marker in image markers.zno APP1 marker in image)r@   rI   r   APP1r[   r\   s     r   r)   _JfifMarkers.app1o   r_   r   c                     U R                    H%  nUR                  [        R                  ;   d  M#  Us  $    [	        S5      e)z4First start of frame (SOFn) marker in this sequence.z(no start of frame (SOFn) marker in image)r@   rI   r   SOF_MARKER_CODESr[   r\   s     r   r&   _JfifMarkers.sofw   s8     A}} 0 A AA  ABBr   )r@   )r   r   r   r   r   r>   rQ   r2   r%   r   r7   r)   r&   r   __classcell__rA   s   @r   r$   r$   @   sd    & & 	 	 2 2 2 2 C Cr   r$   c                   B   ^  \ rS rSrSrU 4S jr\S 5       rS rSr	U =r
$ )rT      zQService class that knows how to parse a JFIF stream and iterate over its
markers.c                 6   > [         [        U ]  5         Xl        g r<   )r=   rT   r>   _stream)r   stream_readerrA   s     r   r>   _MarkerParser.__init__   s    mT+-$r   c                 2    [        U[        5      nU " U5      $ )zFReturn a |_MarkerParser| instance to parse JFIF markers from `stream`.)r   r   )r-   r.   rl   s      r   r%   _MarkerParser.from_stream   s     %VZ8=!!r   c              #     #    [         R                  U R                  5      nSnSnU[        R                  :w  aR  UR                  U5      u  p4[        X0R                  U5      nUv   XER                  -   nU[        R                  :w  a  MQ  gg7f)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%   rk   r   EOInext_MarkerFactoryrG   )r   marker_finderstartrI   segment_offsetrO   s         r   rU   _MarkerParser.iter_markers   s{      &11$,,?-111*7*<*<U*C'K#K~NFL"%:%::E	 -111s   BBBrk   )r   r   r   r   r   r>   r2   r%   rU   r   rf   rg   s   @r   rT   rT      s+    % " "

; 
;r   rT   c                   T   ^  \ rS rSrSrU 4S jr\S 5       rS rS r	S r
S rS	rU =r$ )
rq      zFService class that knows how to find the next JFIF marker in a stream.c                 6   > [         [        U ]  5         Xl        g r<   )r=   rq   r>   rk   )r   r.   rA   s     r   r>   _MarkerFinder.__init__   s    mT+-r   c                     U " U5      $ )zCReturn a |_MarkerFinder| instance to find JFIF markers in `stream`.r   )r-   r.   s     r   r%   _MarkerFinder.from_stream   s     6{r   c                 p    Un U R                  US9nU R                  US-   S9u  p#US:X  a  M,  X2S-   pT XE4$ )a0  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.
)rv          )_offset_of_next_ff_byte_next_non_ff_byte)r   rv   positionbyte_rI   rw   s         r   rs   _MarkerFinder.next   sX     33(3CH"448a<4HOH*/A**r   c                     U R                   R                  U5        U R                  5       nUS:X  a  U R                  5       nUS:X  a  M  U R                   R                  5       S-
  nX24$ )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.
   r   rk   seek
_read_bytetell)r   rv   r   offset_of_non_ff_bytes       r   r   _MarkerFinder._next_non_ff_byte   sa     	% !wOO%E w $ 1 1 3a 7$++r   c                     U R                   R                  U5        U R                  5       nUS:w  a  U R                  5       nUS:w  a  M  U R                   R                  5       S-
  nU$ )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.
r   r   r   )r   rv   r   offset_of_ff_bytes       r   r   %_MarkerFinder._offset_of_next_ff_byte   s^     	% !wOO%E w LL--/!3  r   c                 `    U R                   R                  S5      nU(       d  [        S5      eU$ )zUReturn the next byte read from stream.

Raise Exception if stream is at end of file.
r   zunexpected end of file)rk   read	Exception)r   r   s     r   r   _MarkerFinder._read_byte   s,    
 !!!$455r   ry   )r   r   r   r   r   r>   r2   r%   rs   r   r   r   r   rf   rg   s   @r   rq   rq      s8    P  +,,! r   rq   c                     U [         R                  :X  a  [        nO<U [         R                  :X  a  [        nO!U [         R
                  ;   a  [        nO[        nUR                  XU5      $ )zjReturn |_Marker| or subclass instance appropriate for marker at `offset` in
`stream` having `marker_code`.)	r   rZ   _App0Markerra   _App1Markerrd   
_SofMarker_Markerr%   )rI   r.   rF   
marker_clss       r   rt   rt      sX     &+++ 
	(--	- 
	(99	9

!!&v>>r   c                   |   ^  \ rS rSrSrU 4S jr\S 5       r\S 5       r	\S 5       r
\S 5       r\S 5       rS	rU =r$ )
r      ziBase class for JFIF marker classes.

Represents a marker and its segment occuring in a JPEG byte stream.
c                 N   > [         [        U ]  5         Xl        X l        X0l        g r<   )r=   r   r>   _marker_code_offset_segment_length)r   rI   rF   rG   rA   s       r   r>   _Marker.__init__   s!    gt%''-r   c                 r    [         R                  " U5      (       a  SnOUR                  U5      nU " X#U5      $ )z`Return a generic |_Marker| instance for the marker at `offset` in `stream`
having `marker_code`.r   )r   is_standalone
read_short)r-   r.   rI   rF   rG   s        r   r%   _Marker.from_stream   s7     ))+66N#..v6N;77r   c                     U R                   $ )ue   The single-byte code that identifies the type of this marker, e.g. ``'à'``
for start of image (SOI).)r   r   s    r   rI   _Marker.marker_code  s        r   c                 <    [         R                  U R                     $ r<   )r   marker_namesr   r   s    r   rJ   _Marker.name  s    ,,T->->??r   c                     U R                   $ r<   )r   r   s    r   rF   _Marker.offset  s    ||r   c                     U R                   $ )z-The length in bytes of this marker's segment.)r   r   s    r   rG   _Marker.segment_length  s     ###r   )r   r   r   )r   r   r   r   r   r>   r2   r%   r   rI   rJ   rF   rG   r   rf   rg   s   @r   r   r      ss    
. 8 8 ! !
 @ @   $ $r   r   c                   b   ^  \ rS rSrSrU 4S jr\S 5       r\S 5       rS r	\
S 5       rSrU =r$ )	r   i  z&Represents a JFIF APP0 marker segment.c                 R   > [         [        U ]  XU5        X@l        XPl        X`l        g r<   )r=   r   r>   _density_units
_x_density
_y_density)r   rI   rF   lengthdensity_units	x_density	y_densityrA   s          r   r>   _App0Marker.__init__  s&     	k4)+vF+##r   c                 8    U R                  U R                  5      $ zSHorizontal dots per inch specified in this marker, defaults to 72 if not
specified.)_dpir   r   s    r   r*   _App0Marker.horz_dpi'       yy))r   c                 8    U R                  U R                  5      $ zQVertical dots per inch specified in this marker, defaults to 72 if not
specified.)r   r   r   s    r   r+   _App0Marker.vert_dpi-  r   r   c                     U R                   S:X  a  UnU$ U R                   S:X  a  [        [        US-  5      5      nU$ SnU$ )z6Return dots per inch corresponding to `density` value.r      gRQ@H   )r   intround)r   densitydpis      r   r   _App0Marker._dpi3  sR    !#C
 
	   A%eGdN+,C 
 C
r   c                     UR                  U5      nUR                  US5      nUR                  US5      nUR                  US5      nU " X#XEXg5      $ )zMReturn an |_App0Marker| instance for the APP0 marker at `offset` in
`stream`.	   
      )r   	read_byte)r-   r.   rI   rF   rG   r   r   r   s           r   r%   _App0Marker.from_stream=  s^      **62((3%%fb1	%%fb1		
 	
r   )r   r   r   )r   r   r   r   r   r>   r   r*   r+   r   r2   r%   r   rf   rg   s   @r   r   r     sL    0$ * *
 * *
 
 
r   r   c                   |   ^  \ rS rSrSrU 4S jr\S 5       r\S 5       r	\S 5       r
\S 5       r\S 5       rS	rU =r$ )
r   iT  z-Represents a JFIF APP1 (Exif) marker segment.c                 F   > [         [        U ]  XU5        X@l        XPl        g r<   )r=   r   r>   	_horz_dpi	_vert_dpi)r   rI   rF   r   r*   r+   rA   s         r   r>   _App1Marker.__init__W  s    k4)+vF!!r   c                     UR                  U5      nU R                  X5      (       a  U " X#USS5      $ U R                  XU5      nU " X#XER                  UR                  5      $ )zeExtract 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.   rI   rF   rG   tiffs         r   r%   _App1Marker.from_stream\  s_      **62((88{NBCC**6>J;t}}UUr   c                     U R                   $ r   )r   r   s    r   r*   _App1Marker.horz_dpin       ~~r   c                     U R                   $ r   )r   r   s    r   r+   _App1Marker.vert_dpit  r   r   c                 V    UR                  US-   5        UR                  S5      nUS:g  $ )zReturn 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  )r   r   )r-   r.   rF   exif_signatures       r   r   %_App1Marker._is_non_Exif_APP1_segmentz  s,    
 	FQJQ00r   c                     UR                  US-   5        UR                  US-
  5      n[        R                  " U5      n[        R
                  " U5      $ )zgReturn a |Tiff| instance parsed from the Exif APP1 segment of
`segment_length` at `offset` in `stream`.   )r   r   ioBytesIOr   r%   )r-   r.   rF   rG   segment_bytes	substreams         r   r   #_App1Marker._tiff_from_exif_segment  sF    
 	FQJNQ$67JJ}-		**r   )r   r   )r   r   r   r   r   r>   r2   r%   r   r*   r+   r   r   r   rf   rg   s   @r   r   r   T  sq    7"
 V V"  
  
 1 1 + +r   r   c                   \   ^  \ rS rSrSrU 4S jr\S 5       r\S 5       r	\S 5       r
SrU =r$ )r   i  z7Represents a JFIF start of frame (SOFx) marker segment.c                 F   > [         [        U ]  XU5        X@l        XPl        g r<   )r=   r   r>   	_px_width
_px_height)r   rI   rF   rG   r'   r(   rA   s         r   r>   _SofMarker.__init__  s    j$(nM!#r   c                     UR                  U5      nUR                  US5      nUR                  US5      nU " X#XFU5      $ )zJReturn an |_SofMarker| instance for the SOFn marker at `offset` in stream.      )r   )r-   r.   rI   rF   rG   r(   r'   s          r   r%   _SofMarker.from_stream  sG      **62%%fa0	$$VQ/;)LLr   c                     U R                   $ )zImage height in pixels.)r   r   s    r   r(   _SofMarker.px_height  s     r   c                     U R                   $ )zImage width in pixels.)r   r   s    r   r'   _SofMarker.px_width  s     ~~r   )r   r   )r   r   r   r   r   r>   r2   r%   r   r(   r'   r   rf   rg   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!   r4   r$   rT   rq   rt   r   r   r   r   r   r   r   <module>r      s   
 
 < 7 ,  ? <4 <$<4 <"=C =C@; ;6F FR?'$ '$T5
' 5
p7+' 7+t r   