;i<`dZddlZddlmZmZddlmZmZddlm Z ddl m Z Gdde Z Gd d e Z Gd d e ZGd dZGddZGddZdZGddZGddeZGddeZGddeZdS)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>eZdZdZedZedZdS)Jpegz(Base class for JFIF and EXIF subclasses.ctjS)zWMIME content type for this image, unconditionally `image/jpeg` for JPEG images.)rJPEGselfs CC:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\docx/image/jpeg.py content_typezJpeg.content_types ~cdS)z8Default filename extension, always 'jpg' for JPG images.jpgr s r default_extzJpeg.default_exts urN)__name__ __module__ __qualname____doc__propertyrrrrrr r sN22 X Xrr c(eZdZdZedZdS)Exifz*Image header parser for Exif image format.ct|}|jj}|jj}|jj}|jj}|||||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 sW**622;'K) <(<(s8Y(;;;rNrrrr classmethodrrrrrrs344 < <[ < < rSOS)r'r( marker_parserr)rDs rrz_JfifMarkers.from_stream[ss&11&99 #0022  F NN6 " " "!%5%999:s7||rcf|jD]}|jtjkr|cSt d)z#First APP0 marker in image markers.zno APP0 marker in image)r6r>rAPP0KeyErrorrms rr/z_JfifMarkers.app0gC  A} 0 55560111rcf|jD]}|jtjkr|cSt d)z#First APP1 marker in image markers.zno APP1 marker in image)r6r>rAPP1rNrOs rr#z_JfifMarkers.app1orQrcb|jD]}|jtjvr|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.sofwsD  A} 0 AAABABBBr) rrrrr4rFr+rrr/r#r __classcell__r7s@rrr@s&&&&&   &  [ 22X222X2CCXCCCCCrrc>eZdZdZfdZedZdZxZS)rHzUService class that knows how to parse a JFIF stream and iterate over its markers.cdtt|||_dSr2)r3rHr4_stream)r stream_readerr7s rr4z_MarkerParser.__init__s* mT""++---$ rcBt|t}||S)zFReturn a |_MarkerParser| instance to parse JFIF markers from `stream`.)rr)r'r(r[s rrz_MarkerParser.from_streams#%VZ88 s=!!!rc# Kt|j}d}d}|tjkrN||\}}t ||j|}|V||jz}|tjkLdSdS)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.nextsk 33(3CCH"448a<4HHOHe*/AK N**rc|j||}|dkr|}|dk|jdz }||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_bytest %   !!wOO%%Ew $ 1 1 3 3a 7$e++rc|j||}|dkr|}|dk|jdz }|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_byteso %   !!wOO%%Ew L--//!3  rc\|jd}|std|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_bytes5  !!!$$ 6455 5 r) rrrrr4r+rr`rlrkrsrVrWs@rr^r^sPP[+++, , , , ! ! !rr^c|tjkrt}n5|tjkrt}n|tjvrt }nt}||||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 rrarasc&+++ (- - - (9 9 9   ! !&+v > >>rceZdZdZfdZedZedZedZ edZ edZ xZ S)rzqBase class for JFIF marker classes. Represents a marker and its segment occuring in a JPEG byte stream. ctt|||_||_||_dSr2)r3rr4 _marker_code_offset_segment_length)rr>r;r<r7s rr4z_Marker.__init__s< gt%%'''' -rcttj|rd}n||}||||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_streamsE  )+ 6 6 7NN#..v66Ns;777rc|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   rc0tj|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@rrrs ..... 88[8!!X! @@X@X$$X$$$$$rrcjeZdZdZfdZedZedZdZe dZ xZ S)r|z&Represents a JFIF APP0 marker segment.ctt||||||_||_||_dSr2)r3r|r4_density_units _x_density _y_density)rr>r;length density_units x_density y_densityr7s rr4z_App0Marker.__init__s? k4  ))+vvFFF+##rc6||jSz[Horizontal dots per inch specified in this marker, defaults to 72 if not specified.)_dpirr s rr$z_App0Marker.horz_dpi%yy)))rc6||jSzYVertical dots per inch specified in this marker, defaults to 72 if not specified.)rrr s rr%z_App0Marker.vert_dpi+rrc||jdkr|}n-|jdkr tt|dz}nd}|S)z6Return dots per inch corresponding to `density` value.rigRQ@H)rintround)rdensitydpis rrz_App0Marker._dpi1sJ  ! # #CC  A % %eGdN++,,CCC rc||}||d}||d}||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;sq **622((33 %%fb11 %%fb11 s; yR[\\\r) rrrrr4rr$r%rr+rrVrWs@rr|r|s00$$$$$ **X* **X* ]][]]]]]rr|ceZdZdZfdZedZedZedZ edZ edZ xZ S)r}z-Represents a JFIF APP1 (Exif) marker segment.cxtt||||||_||_dSr2)r3r}r4 _horz_dpi _vert_dpi)rr>r;rr$r%r7s rr4z_App1Marker.__init__Ss7 k4  ))+vvFFF!!rc||}|||r||||ddS||||}|||||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_streamXs{ **622  ( ( 8 8 D3{FNBCC C**66>JJs; t}UUUrc|jSr)rr s rr$z_App1Marker.horz_dpij ~rc|jSr)rr s rr%z_App1Marker.vert_dpiprrch||dz|d}|dkS)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_segmentvs4  FQJQ00rc||dz||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_segmentsP  FQJ NQ$677 J}--  ***r) rrrrr4r+rrr$r%rrrVrWs@rr}r}Ps77""""" VV[V"X X 11[1++[+++++rr}cdeZdZdZfdZedZedZedZ xZ S)r~z7Represents a JFIF start of frame (SOFx) marker segment.cxtt||||||_||_dSr2)r3r~r4 _px_width _px_height)rr>r;r<r!r"r7s rr4z_SofMarker.__init__s7 j$((fnMMM!#rc||}||d}||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_streamsY **622%%fa00 $$VQ//s;)LLLrc|jS)zImage height in pixels.)rr s rr"z_SofMarker.px_heights rc|jS)zImage width in pixels.)rr s rr!z_SofMarker.px_widths ~r) rrrrr4r+rrr"r!rVrWs@rr~r~sAA$$$$$  M M[ MXXrr~)rrdocx.image.constantsrrdocx.image.helpersrrdocx.image.imagerdocx.image.tiffrr rr-rrHr^rarr|r}r~rrrrs@  <<<<<<<<77777777,,,,,,      ?   <<<<<4<<<$<<<<<4<<<"=C=C=C=C=C=C=C=C@;;;;;;;;6FFFFFFFFR ? ? ?'$'$'$'$'$'$'$'$T1]1]1]1]1]'1]1]1]h7+7+7+7+7+'7+7+7+tr