Xj<"dZddlZddlmZmZddlmZmZddlm Z ddl m Z Gdde Z Gd d e Z Gd d e ZGd dZGddZGddZdZGddZGddeZGddeZGddeZy)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)Tiffc0eZdZdZedZedZy)Jpegz(Base class for JFIF and EXIF subclasses.c"tjS)zWMIME content type for this image, unconditionally `image/jpeg` for JPEG images.)rJPEGselfs c/mnt/ssd/data/Dropbox/adrian/vault-secondbrain/venv/lib/python3.12/site-packages/docx/image/jpeg.py content_typezJpeg.content_types~~cy)z8Default filename extension, always 'jpg' for JPG images.jpgr s r default_extzJpeg.default_extsrN)__name__ __module__ __qualname____doc__propertyrrrrrr r s+2  rr c eZdZdZedZy)Exifz*Image header parser for Exif image format.ctj|}|jj}|jj}|j j }|j j}|||||S)z[Return |Exif| instance having header properties parsed from Exif image in `stream`.) _JfifMarkers from_streamsofpx_width px_heightapp1horz_dpivert_dpiclsstreammarkersr!r"r$r%s rrzExif.from_stream sc**62;;''KK)) <<((<<((8Y(;;rNrrrr classmethodrrrrrrs4 < rSOS)r'r( marker_parserr)rDs rrz_JfifMarkers.from_stream[sa&11&9 #002 F NN6 "!!%5%9%99 7|rc||jD]#}|jtjk(s!|cSt d)z#First APP0 marker in image markers.zno APP0 marker in image)r6r>rAPP0KeyErrorrms rr/z_JfifMarkers.app0g= A}} 0 5 55 011rc||jD]#}|jtjk(s!|cSt d)z#First APP1 marker in image markers.zno APP1 marker in image)r6r>rAPP1rNrOs rr#z_JfifMarkers.app1orQrcz|jD]"}|jtjvs |cSt d)z4First start of frame (SOFn) marker in this sequence.z(no start of frame (SOFn) marker in image)r6r>rSOF_MARKER_CODESrNrOs rr z_JfifMarkers.sofws= A}} 0 A AA ABBr) rrrrr4rFr+rrr/r#r __classcell__r7s@rrr@sd& &  2222CCrrc8eZdZdZfdZedZdZxZS)rHzUService class that knows how to parse a JFIF stream and iterate over its markers.c8tt| ||_yr2)r3rHr4_stream)r stream_readerr7s rr4z_MarkerParser.__init__s mT+-$ rc2t|t}||S)zFReturn a |_MarkerParser| instance to parse JFIF markers from `stream`.)rr)r'r(r[s rrz_MarkerParser.from_streams%VZ8 =!!rc#Ktj|j}d}d}|tjk7rS|j |\}}t ||j|}|||jz}|tjk7rRyyw)zGenerate a (marker_code, segment_offset) 2-tuple for each marker in the JPEG `stream`, in the order they occur in the stream.rN) _MarkerFinderrrZrEOInext_MarkerFactoryr<)r marker_finderstartr>segment_offsetrDs rrIz_MarkerParser.iter_markerss&11$,,?  -111*7*<*rds rr`z_MarkerFinder.nexts`33(3CH"448a<4HOHe*/AK N**rc|jj||j}|dk(r|j}|dk(r|jjdz }||fS)uReturn 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. rirZseek _read_bytetell)rrcrnoffset_of_non_ff_bytes rrlz_MarkerFinder._next_non_ff_bytesc % !wOO%Ew $ 1 1 3a 7$e++rc|jj||j}|dk7r|j}|dk7r|jjdz }|S)uReturn 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. rprirq)rrcrnoffset_of_ff_bytes rrkz%_MarkerFinder._offset_of_next_ff_bytes^ % !wOO%Ew LL--/!3  rcV|jjd}|s td|S)zeReturn the next byte read from stream. Raise Exception if stream is at end of file. rizunexpected end of file)rZread Exception)rrns rrsz_MarkerFinder._read_bytes,  !!!$45 5 r) rrrrr4r+rr`rlrkrsrVrWs@rr^r^s3P+, , !rr^c|tjk(rt}n9|tjk(rt}n|tj vrt }nt}|j|||S)znReturn |_Marker| or subclass instance appropriate for marker at `offset` in `stream` having `marker_code`.) rrM _App0MarkerrS _App1MarkerrU _SofMarker_Markerr)r>r(r; marker_clss rrarasZ&+++ (-- - (99 9   ! !&+v >>rcreZdZdZfdZedZedZedZ edZ edZ xZ S)rzqBase class for JFIF marker classes. Represents a marker and its segment occuring in a JPEG byte stream. cTtt| ||_||_||_yr2)r3rr4 _marker_code_offset_segment_length)rr>r;r<r7s rr4z_Marker.__init__s' gt%'' -rchtj|rd}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 rrz_Marker.from_streams6  ) )+ 6N#..v6N;77rc|jS)umThe single-byte code that identifies the type of this marker, e.g. ``'à'`` for start of image (SOI).)rr s rr>z_Marker.marker_codes   rc<tj|jSr2)r marker_namesrr s rr?z _Marker.names,,T->->??rc|jSr2)rr s rr;z_Marker.offsets ||rc|jS)z-The length in bytes of this marker's segment.)rr s rr<z_Marker.segment_lengths###r) rrrrr4r+rrr>r?r;r<rVrWs@rrrss . 88!! @@$$rrcXeZdZdZfdZedZedZdZe dZ xZ S)r|z&Represents a JFIF APP0 marker segment.cZtt| |||||_||_||_yr2)r3r|r4_density_units _x_density _y_density)rr>r;length density_units x_density y_densityr7s rr4z_App0Marker.__init__s, k4)+vvF+##rc8|j|jSz[Horizontal dots per inch specified in this marker, defaults to 72 if not specified.)_dpirr s rr$z_App0Marker.horz_dpi%yy))rc8|j|jSzYVertical dots per inch specified in this marker, defaults to 72 if not specified.)rrr s rr%z_App0Marker.vert_dpi+rrc|jdk(r|}|S|jdk(rtt|dz}|Sd}|S)z6Return dots per inch corresponding to `density` value.rigRQ@H)rintround)rdensitydpis rrz_App0Marker._dpi1sR   ! #C   A %eGdN+,C C rc|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<rrrs rrz_App0Marker.from_stream;s` **62((3 %%fb1 %%fb1 ; yR[\\r) rrrrr4rr$r%rr+rrVrWs@rr|r|sN0$ ** ** ]]rr|creZdZdZfdZedZedZedZ edZ edZ xZ S)r}z-Represents a JFIF APP1 (Exif) marker segment.cLtt| |||||_||_yr2)r3r}r4 _horz_dpi _vert_dpi)rr>r;rr$r%r7s rr4z_App1Marker.__init__Ss$ k4)+vvF!!rc|j|}|j||r ||||ddS|j|||}|||||j|jS)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 rrz_App1Marker.from_streamXsf **62  ( ( 8{FNBC C**66>J; t}}UUrc|jSr)rr s rr$z_App1Marker.horz_dpij~~rc|jSr)rr s rr%z_App1Marker.vert_dpiprrcV|j|dz|jd}|dk7S)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.rsExif)rrry)r'r(r;exif_signatures rrz%_App1Marker._is_non_Exif_APP1_segmentvs,  FQJQ00rc|j|dz|j|dz }tj|}t j |S)zoReturn a |Tiff| instance parsed from the Exif APP1 segment of `segment_length` at `offset` in `stream`.)rrryioBytesIOrr)r'r(r;r< segment_bytes substreams rrz#_App1Marker._tiff_from_exif_segmentsF  FQJ NQ$67 JJ}-  **r) rrrrr4r+rrr$r%rrrVrWs@rr}r}Psq7" VV"  11++rr}cReZdZdZfdZedZedZedZ xZ S)r~z7Represents a JFIF start of frame (SOFx) marker segment.cLtt| |||||_||_yr2)r3r~r4 _px_width _px_height)rr>r;r<r!r"r7s rr4z_SofMarker.__init__s$ j$(fnM!#rc|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 rrz_SofMarker.from_streamsK **62%%fa0 $$VQ/;)LLrc|jS)zImage height in pixels.)rr s rr"z_SofMarker.px_heightsrc|jS)zImage width in pixels.)rr s rr!z_SofMarker.px_widths~~r) rrrrr4r+rrr"r!rVrWs@rr~r~sIA$  M Mrr~)rrdocx.image.constantsrrdocx.image.helpersrrdocx.image.imagerdocx.image.tiffrr rr-rrHr^rarr|r}r~rrrrs  <7,  ? <4<$<4<"=C=C@;;6FFR ?'$'$T1]'1]h7+'7+tr