
    i92id                     D   S SK r S SKrS SKrS SKrS SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJr  S SKr S SKJrJr  S SKJrJr   S S	KJr  \R,                  r S SKrS
SKJr  S
SKJrJrJ r   S
SK!J"r"  \ " S S5      5       r#\RH                  " \%5      r&Sr'\#" 5       r(/ SQr)Sr*Sr+Sr,Sr-S(S\4S jjr.S r/S r0S r1S r2S r3S)S jr4 " S S5      r5S  r6S! r7S*S" jr8S# r9S$ r:S% r;S*S& jr<S' r=g! \ a    \R2                  r Nf = f! \ a    SrSr Nf = f! \ a    Sr Nf = f)+    N)	dataclass)BytesIO)ceil)urlopen)Path)ImageTiffImagePlugin)ImageCmsfeatures)
Resampling   )FPDFException)
ImageCacheRasterImageInfoVectorImageInfo)	SVGObjectc                   $    \ rS rSr% Sr\\S'   Srg)ImageSettings"   compression_level N)__name__
__module____qualname____firstlineno__r   int__annotations____static_attributes__r       L/home/james-whalen/.local/lib/python3.13/site-packages/fpdf/image_parsing.pyr   r   "   s      sr    r   )AUTOFlateDecode	DCTDecode	JPXDecode	LZWDecode(   r      @             `            P      0      p            H      (      h            X      8      x            D      $      d            T      4      t            L      ,      l            \      <      |            B      r      b            R      2      r      
      J      *      j            Z      :      z            F      &      f            V      6      v            N      .      n            ^      >      ~      r      A      !      a            Q      1      q      	      I      )      i            Y      9      y            E      %      e            U      5      u            M      -      m            ]      =      }            C      #      c            S      3      s            K      +      k            [      ;      {            G      '      g            W      7      w            O      /      o            _      ?               i  r   rV   image_cachec                 T   [        U5      R                  S5      (       a   [        U[        [        U5      5      U S9$ [        U[        5      (       a=  [        UR                  5       5      (       a  [        U[        R                  " U5      U S9$ [        U[        R                  5      (       a6  [        UR                  5       R                  5       5      (       a
  [        SXS9$ [        U[         5      (       a  SnGO[        U[        R                  5      (       aH  UR                  5       n[        R                   " SSS9nUR#                  U5        UR%                  5       UpAO[        U[        [        R                  45      (       ay  [        U[        R                  5      (       a  UR                  5       OUnUR                  5       n[        R                   " SSS9nUR#                  U5        UR%                  5       UpAO[        U5      UpAU R&                  R)                  U5      nU(       a  US	==   S
-  ss'   O[+        XU R,                  U5      n[/        U R&                  5      S
-   US'   S
US	'   SUS'   UR)                  S5      nU(       ad  [0        R3                  SU5        XR4                  ;   a  U R4                  U   US'   O'[/        U R4                  5      n	XR4                  U'   XS'   SUS'   XpR&                  U'   XU4$ ! [         a  n[        SU 35      UeSnAff = f)a8  
Read an image and load it into memory.

For raster images: following this call, the image is inserted in `image_cache.images`,
and following calls to `fpdf.fpdf.FPDF.image()` will re-use the same cached values, without re-reading the image.

For vector images: the data is loaded and the metadata extracted.

Args:
    image_cache: an `ImageCache` instance, usually the `.image_cache` attribute of a `FPDF` instance.
    name: either a string representing a file path to an image, an URL to an image,
        an io.BytesIO, or a instance of `PIL.Image.Image`.
    dims (Tuple[float]): optional dimensions as a tuple (width, height) to resize the image
        (raster only) before storing it in the PDF.

Returns: A tuple, consisting of 3 values: the name, the image data,
    and an instance of a subclass of `ImageInfo`.
z.svgr%  zCould not parse file: Nvector_imagemd5F)usedforsecurityusagesr   iiccp_iiccpzHICC profile found for image %s - It will be inserted in the PDF document)strendswithget_svg_info
load_image	Exception
ValueError
isinstancebytes_is_svgstripior   getvaluer   tobyteshashlibnewupdate	hexdigestimagesgetget_img_infoimage_filterlenLOGGERdebugicc_profiles)
r%  namedimserrorimgbytes_img_hashinfor.  r-  s
             r!   preload_imagerO  Q   s   ( 4y&!!	IjT&;UU $74::<#8#8D"**T"2LL$

##0E0E0G(H(HNDJJ $	D%++	&	&;;ue<&&($c	D5"**-	.	.$.tRZZ$@$@d;;ue<&&($cItc!!$'DX!D{'?'?F**+a/S	XXxxLLZ ///!,!9!9$!?X[55617((.!'XDL#'4 d?[  	I5dV<=5H	Is   L	 	
L'L""L'c                 T    U R                  S5      =(       d    U R                  S5      $ )Ns   <?xml s   <svg )
startswith)rL  s    r!   r7  r7     s#    Y'F6+<+<X+FFr    c                    [        U [        5      (       a  U $ [        U [        5      (       a  [        U 5      n U R	                  S5      (       a.  [        U 5       n[        UR                  5       5      sSSS5        $ U R	                  S5      (       a  [        U 5      $ [        U S5       n[        UR                  5       5      sSSS5        $ ! , (       d  f       N== f! , (       d  f       g= f)z
This method is used to load external resources, such as images.
It is automatically called when resource added to document by `fpdf.fpdf.FPDF.image()`.
It always return a BytesIO buffer.
)zhttp://zhttps://Nzdata:rb)	r5  r   r   r/  rQ  r   read_decode_base64_imageopen)filenameurl_file
local_files      r!   r2  r2     s     (G$$(D!!x=233 X(8==?+ 			W	%	%#H--	h	z() 
		  
	s   C)C
C
C+c                     U R                  S5      n[        U5      S:w  a  [        S5      eUS   n[        R                  " U5      n[        U5      $ )z7Decode the base 64 image string into an io byte stream.zbase64,rf   z!Unsupported non-base64 image datar   )splitrD  NotImplementedErrorbase64	b64decoder   )base64Imagefrags	imageDatadecodedDatas       r!   rU  rU     sM    i(E
5zQ!"EFFaI""9-K;r    c                 (    [         R                  " [        U 5      5      nUR                  R                  R                  5       nUS;  a  [        R                  SUU5        gg! [         R                   a    [        R                  SU5         gf = f)z%Checks the validity of an ICC profilezInvalid ICC Profile in file %sF)GRAYRGBzEUnsupported color space %s in ICC Profile of file %s - cf. issue #711T)	r
   getOpenProfiler   
PyCMSErrorrE  rN  profilexcolor_spacer8  )r.  rW  rh  color_spaces       r!   is_iccp_validrk     s    ))'$-8 //..446K/)S	

   4h?s   A$ $*BBc                    [        UR                  5       US9nUR                  (       a  UR                  u    pEnOS=pVUR                  (       a  UR                  nUR                  (       a  UR                  n[        X5US9nXU4$ )Nr'  g        )datawh)r   r:  viewboxwidthheightr   )rW  rK  r%  svg_rn  ro  rN  s           r!   r1  r1     sh    
CLLN
<C
{{[[
1
yyII
zzJJA.D$r    c                 8   [         c  [        S5      eSnSnSnSnU(       a  [        U[        [        45      (       a$  [        U 5      n[         R                  " U5      nSnOi[        U[         R                   5      (       dJ  [        U[        5      n[        U[        5      (       a  [        U5      OUn[         R                  " U5      nSnSnU(       a  UR                  U[        S9nSnUS:X  aC  UR                  S:X  a  SnO0UR                  S	:X  a  [        R                  " S
5      (       a  SnOSnUR                  S;   a  US:w  a  UR                  S5      nUR                  S;  a  UR                  S5      nSnUR                  S;   a  US:X  a  UR                  S5      nO'UR                  S;   a  US:X  a  UR                  S5      nUR                   u  p[#        5       nSnSUR$                  ;   a-  ['        UR$                  S   U 5      (       a  UR$                  S   nUGbr  U(       Gdj  UR                  S:X  a  US:X  a  UR                  S;   a  Su  pnOFUR                  S:X  a  Su  pnSnO.UR                  S:X  a  Su  pnO[)        SUR                   35      eUR+                  S5        UR-                  UR/                  5       U	U
UUUUUUSU SU	 3S.
5        U$ UR                  S :X  Ga  US:X  Ga  UR$                  S!   S":X  Ga  [1        UR2                  [4        R6                     5      S#:X  Ga^  [1        UR2                  [4        R8                     5      S#:X  Ga3  UR2                  [4        R:                     nSnUS:X  a  SnOUS#:w  a  [)        S$U 35      e[=        U5      u  nnUR+                  U5        UR/                  U5      nUR2                  R?                  [4        R@                  5      nUb  US#:X  a  OLUS%:X  a8  [C        U5      n[E        U5       H  u  nn[F        U   UU'   M     [        U5      nO[)        S&U 35      eS'u  pnUR-                  UU	U
SUUUUUS([	        U(       + 5      RI                  5        SU	 S)U
 3S*.
5        U$ SnUR                  S	:X  a  S'u  pn[K        X5      US+'   GOUR                  S:X  a  Su  pn[K        X5      US+'   GOUR                  S:X  aE  Su  pn[M        S#SS%5      n[K        XUS,9US+'   [O        UU5      (       a  US-;  a  [K        XUS.9US/'   GOUR                  S0:X  ax  S1u  pn[K        X5      US+'   URP                  RP                  US2'   UR$                  R?                  S3S5      b.  US-;  a(  [K        UR                  S5      U[M        S4SS55      S.9US/'   OUR                  S6:X  a]  S1u  pnURP                  RP                  US2'   [M        S#SS%5      n[K        XUS,9US+'   [O        UU5      (       a  US-;  a  [K        XUS.9US/'   OUR                  S:X  a  Su  pn[K        X5      US+'   OgUR                  S:X  a  Su  pn[K        X5      US+'   OCSu  pn[M        S4SS55      n[K        XUS,9US+'   [O        UU5      (       a  US-;  a  [K        XUS.9US/'   SU SU	 3nUR                  S	:X  a  S7U	 S)U
 3nU(       d  U(       a  SUl)        OURU                  5         UR-                  U	U
UUUUUUUS8.	5        U$ )9z
Args:
    filename: in a format that can be passed to load_image
    img: optional `bytes`, `BytesIO` or `PIL.Image.Image` instance
    image_filter (str): one of the SUPPORTED_IMAGE_FILTERS
Nz1Pillow not available - fpdf2 cannot insert imagesTF)resampler"   JPEGr$   1libtiffCCITTFaxDecoder#   )PPARGBA)rx  LLAre  r}  r{  r|  CMYK)r{  r}  r&   re  r  r~  icc_profile)re  r}  )r   r6   	DeviceRGBr  )rF   r6   
DeviceCMYK)r   r6   
DeviceGrayzUnsupported image mode: r   z/Predictor 15 /Colors z
 /Columns )
rm  rn  ro  csr.  dpnbpcfinverteddpTIFFcompressiongroup4r   z4unsupported photometric interpretation for g4 tiff: rf   zunsupported FillOrder: )r   r   r  z
/BlackIs1 z /K -1 /Rows )
rm  rn  ro  r.  r  r  r  r  r  r  rm  )remove_slice)r$   r%   )select_slicesmaskr{  )r   r6   Indexedpaltransparencyr   rF   r|  z/BlackIs1 true /Columns )	rn  ro  r  r.  r  r  r  r  r  )+r   EnvironmentErrorr5  r   r/  r2  rV  r   r6  resizeRESAMPLEformatmodePIL_featurescheckconvertsizer   rN  rk  r4  seekr>  rT  rD  tag_v2r	   STRIPOFFSETSSTRIPBYTECOUNTSPHOTOMETRIC_INTERPRETATIONccitt_payload_location_from_pilrA  	FILLORDER	bytearray	enumerateTIFFBitRevTablelower_to_dataslice
_has_alphapalettefpclose)rW  rK  rC  rI  
is_pil_imgkeep_bytes_io_openjpeg_invertedimg_raw_dataimg_alteredrn  ro  rN  r.  r  r  colspacephotor  offsetlengthccittrawdata	fillorderr,  nalpha_channelr  s                             r!   rB  rB     s	    }RSSJ ML*S4+..!(+jj&
U[[))'W5'1#u'='=ws|3jj&
Kjjj1v::&LXX_!3!3I!>!> ,L(L
xx;<=#@kk&!
xxIIkk&!
xx= \[%@kk% 	d	 ;kk#88DADD -0(;;88M*D::LK$?xx?*%6"(V#%7"( $S%7"( #;CHH:!FGGa KK(--/" % -23%z!E K JJ&  00'83CJJ;;<=BCJJ>>?@AEJJIIJEHz! J5'R  =SANFFf%',,V4L

'@'@AI INa(6%l3DAq&5a&8LO 4$\2 #:9+!FGG!3ChKK( "% -&sx<'8'>'>'@&AA3m\][^_ K L
xx3/(2V	S/(2V	T	/(aq)NVc=))l C
 /
 %S]SDM	S,(2Vkk))U 88<<-9l S
 ?

 %F#\aq@QDM 
T	,(kk))Uaq)NVc=))l C
 /
 %S]SDM	V	/(2V	U	.(2V.(aq)NVc=))l C
 /
 %S]SDM!#j	4B
xx3's-s;CFIIKKK%
	
 Kr    c                   *    \ rS rSrSrS rS rS rSrg)	temp_attri  zE
temporary change the attribute of an object using a context manager
c                 (    Xl         X l        X0l        g N)objfieldvalue)selfr  r  r  s       r!   __init__temp_attr.__init__  s    

r    c                 
   SU l         [        U R                  U R                  5      (       a,  SU l         [	        U R                  U R                  5      U l        [        U R                  U R                  U R                  5        g )NFT)existshasattrr  r  getattr	old_valuesetattrr  )r  s    r!   	__enter__temp_attr.__enter__  sS    488TZZ((DK$TXXtzz:DN$**djj1r    c                     U R                   (       a,  [        U R                  U R                  U R                  5        g [        U R                  U R                  5        g r  )r  r  r  r  r  delattr)r  exctypeexcinstexctbs       r!   __exit__temp_attr.__exit__  s3    ;;DHHdjj$..9DHHdjj)r    )r  r  r  r  r  N)	r   r   r   r   __doc__r  r  r  r   r   r    r!   r  r    s    
2*r    r  c                     U R                   [        R                     nU R                   [        R                     n[	        U5      S:w  d  [	        U5      S:w  a  [        S5      eXsu  nu  nX44$ )zS
returns the byte offset and length of the CCITT payload in the original TIFF data
r   z;Transcoding multiple strips not supported by the PDF format)r  r	   r  r  rD  r\  )rK  strip_offsetsstrip_bytesr  r  s        r!   r  r    so     JJ;;<M**_<<=K =Q#k"2a"7!I
 	
 )IVy>r    c                   ^ ^^ [        5       n[        R                  " T R                  T R                  T R                  5       5      nT R                  S   S-   S-  T R                  S   -  m[        [        S5      (       a,  [        [        ST5         UR                  USSS9  S	S	S	5        OW[        R                  R                  mU UU4S
 jn[        [        R                  SU5         UR                  USSS9  S	S	S	5        UR                  S5        [        R                  " U5      n[        U5      u  pVUR                  U5        UR                  U5      $ ! , (       d  f       Nd= f! , (       d  f       Nu= f)zF
Convert the open PIL.Image imgdata to compressed CCITT Group4 data.

r   r  r6   r   
STRIP_SIZEr  r  )r  r  Nc                    > [         R                  TR                  S   [         R                  T/[         R                  S/0nUR                  UT" X5      5      $ )Nr   r   )r	   ROWSPERSTRIPr  r  r  rA  )r  tag	overridesrK  pillow__getitem__tmp_strip_sizes      r!   __getitem__)transcode_monochrome.<locals>.__getitem__  sQ    ,,chhqk//.1A,,qcI
 ==&7&BCCr    r  )r   r   	frombytesr  r  r;  r  r	   r  saveImageFileDirectory_v2r  r  rV  r  rT  )	rK  newimgioimg2r  newimgr  r  r  r  s	   `      @@r!   transcode_monochromer    s)    yH
 ??388SXXs{{}=D hhqkAo!+chhqk9N--nEIIhv8ID FE
 ,AAMM	D 11=+
 IIhv8ID
 MM!ZZ!F4V<NFMM&==  7 FE
 
s   E+E,
E),
E:c                    [        U R                  5       5      nU(       a  X1	 U(       a  X2   nU R                  S:X  a  [        U R                  S   S-  5      nO=[        U5      U R                  S   U R                  S   -  -  nU R                  S   U-  n[        5       n[        S[        U5      U5       H)  nUR                  S5        UR                  X7Xt-    5        M+     Un[        /n[        5       u  ppSnU H  nU[        U/5      -   nX;   a  UnM  UR                  X   5        U
S[        -  S-
  ::  a&  XU'   U
S-  n
X:  a  U[        :  a  US-  nSU-  S-
  nO"UR                  [        5        [        5       u  pp[        U/5      nM     U(       a  UR                  X   5        UR                  [        5        [        U5      $ )Nrx  r   r6   r       r    )r  r;  r  r   r  rD  rangeextendLZW_CLEAR_TABLE_MARKERclear_tabler6  appendLZW_MAX_BITS_PER_CODELZW_EOD_MARKERpack_codes_into_bytes)rK  r  r  rm  row_sizechannels_countdata_with_paddingr,  result_codestable	next_codebits_per_codemax_code_valuecurrent_sequencebytenext_sequences                   r!   _to_lzwdatar  "  s   S[[]#D!
xx3a(Tsxx{SXXa['@A88A;/!1c$i*  '  !,!78 + D ++L6Am3Em(5$=8!,  78 Q"771<<'0m$Q	--BW2W!Q&M&'=&8A%=N ##$:;BM-?-  %dV}/ 4 E34' ..r    c                    [        5       u  nnnnSnSn[        5       n[        b#  [        R                  " U [        R                  S9n U  H  nXS-  U-  nXc-  nUS:  a#  US-  nUR                  XV-	  S-  5        US:  a  M#  U[        :X  a  [        5       u  pp4MP  U[        :w  d  M\  US-  nX$:  d  Mh  U[        :  d  Mt  US-  nSU-  S-
  nM     US:  a  UR                  USU-
  -  S-  5        [        U5      $ )z
Convert the list of result codes into a continuous byte stream, with codes packed as per the code bit-width.
The bit-width starts at 9 bits and expands as needed.

r   )dtyper6   r#  r   )
r  r  numpyarrayuint32r  r  r  r  r6  )	codesrt  r  r  r  bufferbits_in_bufferoutputcodes	            r!   r  r  ^  s    		FN[F E6)T1'!aNMM63t;< ! )):E-7A-^#NI)m>S.S""#}"4!9   v!n"45=>=r    c                      [        S5       V s0 s H  n [        U /5      U _M     nn [        S-   n[        nSU-  S-
  nXX44$ s  sn f )zC
Reset the encoding table and coding state to initial conditions.

r$  r   )r  r6  r  LZW_INITIAL_BITS_PER_CODE)r,  r  r  r  r  s        r!   r  r    sT     %*#J/JqUA3Z]JE/"I-M=(A-N]::	 0s   Ac                    US:X  a  [        U 40 UD6$ US:X  a  [        U 5      $ US:X  a  [        U 40 UD6$ U R                  S:X  a  U R	                  S5      n U R                  S:X  a  U R	                  S5      n US:X  a*  [        5       nU R                  US	S
9  UR                  5       $ US:X  a*  [        5       nU R                  USS
9  UR                  5       $ [        SU S35      e)Nr#   rz  r&   r  r~  r}  re  r$   rw  )r  r%   JPEG2000zUnsupported image filter: "")		_to_zdatar  r  r  r  r   r  r:  r   )rK  rC  kwargscompressed_bytess       r!   r  r    s    }$'''''#C(({"3)&))
xx4kk#
xx6kk% {""9!&1((**{""9!*5((**
5l^1E
FFr    c                    [        U R                  5       5      nU(       a  X1	 U(       a  X2   nU R                  S:X  a  [        U R                  S   S-  5      nO=[        U5      U R                  S   U R                  S   -  -  nU R                  S   U-  n[        5       n[        S[        U5      U5       H)  nUR                  S5        UR                  X7Xt-    5        M+     [        R                  " U[        R                  S9$ )Nrx  r   r6   r   r  )level)r  r;  r  r   r  rD  r  r  zlibcompressSETTINGSr   )rK  r  r  rm  r  r  r  r,  s           r!   r  r    s    S[[]#D!
xx3a(Tsxx{SXXa['@A88A;/!1c$i*  '  !,!78 + ==*(2L2LMMr    c                 ^    [        U R                  5       5      U   n[        S U 5       5      $ )Nc              3   *   #    U  H	  oS :g  v   M     g7f)r#  Nr   ).0cs     r!   	<genexpr>_has_alpha.<locals>.<genexpr>  s     'ACxs   )r  r;  any)rK  r  alphas      r!   r  r    s(    ckkm$]3E''''r    r  )Nr"   N)NN)>r]  r<  r9  r  dataclassesr   r   mathr   urllib.requestr   pathlibr   loggingPILr   r	   r
   r   r  	PIL.Imager   LANCZOSr  ImportError	ANTIALIASr  errorsr   image_datastructuresr   r   r   rs  r   r   	getLoggerr   rE  SUPPORTED_IMAGE_FILTERSr  r  r  r  r
  r  rO  r7  r2  rU  rk  r1  rB  r  r  r  r  r  r  r  r  r  r   r    r!   <module>r,     sZ       !   "  *6#(%% " N N       
 
		8	$X ?<    Dz DNG*. $]@* *002!j9/x(V
;G:N*(o  #??#  EL  EsA   D C, D ,D>D  DD 
DDDD