
    }fDS                        d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlZ	 d dlmZmZ d dlmZ 	 d d	lmZ ej        Zn# e$ r
 ej        ZY nw xY wn# e$ r dZY nw xY wd
dlmZ d
dlmZmZmZ d
dlmZ e G d d                      Z  ej!        e"          Z#dZ$ e             Z%g dZ&d"defdZ'd Z(d Z)d Z*d Z+d Z,d#dZ- G d d          Z.d Z/d Z0d Z1d$d Z2d! Z3dS )%    N)	dataclass)BytesIO)ceil)urlopen)Path)ImageTiffImagePlugin)ImageCms)
Resampling   )FPDFException)
ImageCacheRasterImageInfoVectorImageInfo)	SVGObjectc                       e Zd ZU dZeed<   dS )ImageSettingscompression_levelN)__name__
__module____qualname__r   int__annotations__     K/var/www/rtfcompare/venv/lib/python3.11/site-packages/fpdf/image_parsing.pyr   r      s%           sr   r   )AUTOFlateDecode	DCTDecode	JPXDecode(   r      @             `            P      0      p            H      (      h            X      8      x            D      $      d            T      4      t            L      ,      l            \      <      |            B      "      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            _      ?            image_cachec                 h   t          |                              d          r,t          |t          t          |                    |           S t	          |t
                    rEt          |                                          r$t          |t          j	        |          |           S t	          |t          j	                  rEt          |
                                                                          rt          d||           S t	          |t                     rd}n(t	          |t          j                  rV|                                }t          j        dd          }|                    |           |                                |}}nt	          |t
          t          j	        f          rt	          |t          j	                  r|
                                n|}|                                }t          j        dd          }|                    |           |                                |}}nt          |          |}}| j                            |          }|r|dxx         d	z  cc<   nt'          ||| j        |          }t+          | j                  d	z   |d
<   d	|d<   d|d<   |                    d          }|r]t,                              d|           || j        v r| j        |         |d<   n#t+          | j                  }|| j        |<   ||d<   d|d<   || j        |<   |||fS )ab  
    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.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   vector_imageN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
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imgbytes_img_hashinfor)  r(  s	            r   preload_imagerG  F   s    ( 4yy&!! RD*SYY"7"7[QQQQ$ M74::<<#8#8 MD"*T"2"2LLLL$
## K0E0E0G0G(H(H KNDkJJJJ $ $	D%+	&	& $;ue<<<&&(($c	D5"*-	.	. $$.tRZ$@$@Jd;ue<<<&&(($cIItc!!$''D (X!D#{'?FF*++a/S	XXxx 	 LLZ   {///!,!9$!?X[56617(.!'XDL#'4 d?r   c                 V    |                      d          p|                      d          S )Ns   <?xml s   <svg )
startswith)rD  s    r   r0  r0     s)    Y''F6+<+<X+F+FFr   c                    t          | t                    r| S t          | t                    rt          |           } |                     d          rIt          |           5 }t          |                                          cddd           S # 1 swxY w Y   n$|                     d          rt          |           S t          | d          5 }t          |                                          cddd           S # 1 swxY w Y   dS )z
    This method is used to load external resources, such as images.
    It is automatically called when resource added to document by `fpdf.FPDF.image()`.
    It always return a BytesIO buffer.
    )zhttp://zhttps://Nzdata:rb)	r.  r   r   r*  rI  r   read_decode_base64_imageopen)filenameurl_file
local_files      r   r-  r-     sx    (G$$ (D!! !x==233 . X 	,(8==??++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,			W	%	% .#H---	h		 *z(())* * * * * * * * * * * * * * * * * *s$    !BBB!C<<D D c                     |                      d          }t          |          dk    rt          d          |d         }t          j        |          }t          |          S )z7Decode the base 64 image string into an io byte stream.zbase64,ra   z!Unsupported non-base64 image datar   )splitr=  NotImplementedErrorbase64	b64decoder   )base64Imagefrags	imageDatadecodedDatas       r   rM  rM     sZ    i((E
5zzQ!"EFFFaI"9--K;r   c                 .   	 t          j        t          |                     }n1# t           j        $ r t                              d|           Y dS w xY w|j        j                                        }|dvrt                              d||           dS dS )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   
PyCMSErrorr>  rF  profilexcolor_spacer1  )r)  rO  r`  color_spaces       r   is_iccp_validrc     s    )'$--88   4h???uu /.4466K/))S	
 	
 	

 u4s   !$ *AAc                     t          |                                |          }|j        r|j        \  }}}}ndx}}|j        r|j        }|j        r|j        }t          |||          }| ||fS )Nr"  g        )datawh)r   r3  viewboxwidthheightr   )rO  rC  r   svg_rf  rg  rF  s           r   r,  r,     s    
CLLNN
<
<
<C
{ [
1aA
y I
z JqA...DS$r   r   c                    t           t          d          d}d}d}d}|rt          |t          t          f          r&t          |           }t          j        |          }d}nkt          |t           j                   sQt          |t                    }t          |t                    rt          |          n|}t          j        |          }d}d}|r|	                    |t                    }d}|dk    r8|j        dk    rd}n*|j        d	k    rt          t           j        d
          rd}nd}|j        dv r|dk    r|                    d          }|j        dvr|                    d          }d}|j        \  }	}
t#                      }d}d|j        v r(t'          |j        d         |           r|j        d         }|w|st|j        dk    r|dk    r|j        dv rd\  }}}n=|j        dk    r	d\  }}}d}n)|j        dk    rd\  }}}nt)          d|j                   |                    d           |                    |                                |	|
||||||d| d|	 d
           |S |j        dk    r|dk    r|j        d         dk    rt1          |j        t4          j                           dk    ryt1          |j        t4          j                           dk    rP|j        t4          j                 }d}|dk    rd}n|dk    rt)          d |           t=          |          \  }}|                    |           |                    |          }|j                            t4          j                   }||dk    rn\|d!k    rDtC          |          }tE          |          D ]\  }}tF          |         ||<   t          |          }nt)          d"|           d#\  }}}|                    ||	|
d|||||d$t	          |           $                                 d|	 d%|
 d&
           |S d}|j        d	k    rd#\  }}}tK          ||          |d'<   n"|j        dk    rd\  }}}tK          ||          |d'<   n|j        d(k    rWd\  }}}tM          ddd!          }tK          |||)          |d'<   tO          ||          r|d*vrtK          |||+          |d,<   n|j        d-k    rd.\  }}}tK          ||          |d'<   |j(        j(        |d/<   |j                            d0d          ;|d*vr7tK          |                    d          |tM          d1dd2          +          |d,<   n|j        d3k    red.\  }}}|j(        j(        |d/<   tM          ddd!          }tK          |||)          |d'<   tO          ||          r|d*vrtK          |||+          |d,<   n|j        dk    rd\  }}}tK          ||          |d'<   nz|j        d4k    rd\  }}}tK          ||          |d'<   nUd\  }}}tM          d1dd2          }tK          |||)          |d'<   tO          ||          r|d*vrtK          |||+          |d,<   d| d|	 }|j        d	k    rd5|	 d%|
 }|s|rd|_)        n|*                                 |                    |	|
|||||||d6	           |S )7z
    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_support_custom_tagsCCITTFaxDecoder   )PPARGBA)rp  LLAr]  ru  rs  rt  CMYKicc_profile)r]  ru  )r   r1   	DeviceRGBrx  )rA   r1   
DeviceCMYKrv  )r   r1   
DeviceGrayzUnsupported image mode: r   z/Predictor 15 /Colors z
 /Columns )
re  rf  rg  csr)  dpnbpcfinverteddpTIFFcompressiongroup4r   z4unsupported photometric interpretation for g4 tiff: ra   zunsupported FillOrder: )r   r   r|  z
/BlackIs1 z /K -1 /Rows )
re  rf  rg  r)  r~  r}  r  r  r  r  re  rw  )remove_slice)r    r!   )select_slicesmaskrs  )r   r1   Indexedpaltransparencyr   rA   rt  r]  z/BlackIs1 true /Columns )	rf  rg  r}  r)  r  r~  r  r  r  )+r   EnvironmentErrorr.  r   r*  r-  rN  r   r/  resizeRESAMPLEformatmodehasattrcoreconvertsizer   rF  rc  
ValueErrorseekr7  rL  r=  tag_v2r	   STRIPOFFSETSSTRIPBYTECOUNTSPHOTOMETRIC_INTERPRETATIONccitt_payload_location_from_pilr:  	FILLORDER	bytearray	enumerateTIFFBitRevTablelower_to_dataslice
_has_alphapalettefpclose)rO  rC  r<  rB  
is_pil_imgkeep_bytes_io_openjpeg_invertedimg_raw_dataimg_alteredrf  rg  rF  r)  r~  r  colspacephotor  offsetlengthccittrawdata	fillorderr'  nalpha_channelr  s                             r   r;  r;     sG    }RSSSJ ML *S4+.. !(++j&&

U[)) 'W55'1#u'='=Fws|||3j&&
K jjj11v:&LLX__5R!S!S_ ,LL(L
x;<=#@#@kk&!!
xIIIkk&!!8DAqDD  -0(;; 	+8M*D:LK$?$?x?**%6"S((V##%7"S( $S%7"S(( !FCH!F!FGGGa   KK(--//" % -E3EE!EE    K J&   000'833CJ;<==BBCJ>?@@AEEJIJEHzz! R5RR   =SAANFFf%%%',,V44L
'@AAI INNa(66%l33 9 9DAq&5a&8LOO$\22 !F9!F!FGGG!3ChKK( "% -_sx<'8'8'>'>'@'@__A__\]__    K L
x3/S(\22V	S/S(\22V	T		/S(aq))\NNNVc=)) 	Tl C
 /
 /
 %S,]SSSDM	S,S(\22Vk)U 8<<--9l S
 ?
 ?

 %F##\aq@Q@Q  DM 
T		,S(k)Uaq))\NNNVc=)) 	Tl C
 /
 /
 %S,]SSSDM	V		/S(\22V	U		.S(\22V.S(aq))\NNNVc=)) 	Tl C
 /
 /
 %S,]SSSDM	4#	4	4	4	4B
x3;;;;;  	CFFIIKKKKK%
	
 
	
   Kr   c                   $    e Zd ZdZd Zd Zd ZdS )	temp_attrzM
    temporary change the attribute of an object using a context manager
    c                 0    || _         || _        || _        d S N)objfieldvalue)selfr  r  r  s       r   __init__ztemp_attr.__init__  s    



r   c                     d| _         t          | j        | j                  r&d| _         t	          | j        | j                  | _        t          | j        | j        | j                   d S )NFT)existsr  r  r  getattr	old_valuesetattrr  )r  s    r   	__enter__ztemp_attr.__enter__  sY    48TZ(( 	;DK$TXtz::DN$*dj11111r   c                     | j         r"t          | j        | j        | j                   d S t          | j        | j                   d S r  )r  r  r  r  r  delattr)r  exctypeexcinstexctbs       r   __exit__ztemp_attr.__exit__  sD    ; 	*DHdj$.99999DHdj)))))r   N)r   r   r   __doc__r  r  r  r   r   r   r  r    sK           
2 2 2* * * * *r   r  c                     | j         t          j                 }| j         t          j                 }t	          |          dk    st	          |          dk    rt          d          ||c\  }\  }||fS )z[
    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  r=  rT  )rC  strip_offsetsstrip_bytesr  r  s        r   r  r    sz     J;<M*_<=K =Q#k"2"2a"7"7!I
 
 	
 )+IVy6>r   c                     t                      }t          j         j         j                                                   } j        d         dz   dz   j        d         z  t          t          d          rGt          t          d          5  |	                    |dd           d	d	d	           n# 1 swxY w Y   nct          j
        j         fd
}t          t          j
        d|          5  |	                    |dd           d	d	d	           n# 1 swxY w Y   |                    d           t          j        |          }t          |          \  }}|                    |           |                    |          S )zN
    Convert the open PIL.Image imgdata to compressed CCITT Group4 data.

    r   r   r1   r   
STRIP_SIZEr  r  )r  r  Nc                     t           j        j        d         t           j        gt           j        dgi}|                    | | |                    S )Nr   r   )r	   ROWSPERSTRIPr  r  r  r:  )r  tag	overridesrC  pillow__getitem__tmp_strip_sizes      r   __getitem__z)transcode_monochrome.<locals>.__getitem__  sQ    ,chqk/.1A,qcI
 ==&7&7c&B&BCCCr   r  )r   r   	frombytesr  r  r4  r  r	   r  saveImageFileDirectory_v2r  r  rN  r  rL  )	rC  newimgioimg2r  newimgr  r  r  r  s	   `      @@r   transcode_monochromer    s"    yyH
 ?38SXs{{}}==D hqkAo!+chqk9N-- EnEE 	E 	EIIhv8IDDD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E
 ,AM	D 	D 	D 	D 	D 	D 	D 1=+
 
 	E 	E IIhv8IDDD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E MM!Z!!F4V<<NFFMM&==   s$   B55B9<B94DD Dc                    |dk    rt          | fi |S |dk    rt          |           S | j        dk    r|                     d          } | j        dk    r|                     d          } |dk    r9t	                      }|                     |d	           |                                S |d
k    r9t	                      }|                     |d	           |                                S t          d| d          )Nr   rr  rw  rv  ru  r]  r    ro  )r  r!   JPEG2000zUnsupported image filter: "")	_to_zdatar  r  r  r   r  r3  r   )rC  r<  kwargscompressed_bytess       r   r  r    s   }$$''''''''#C(((
x4kk#
x6kk%  {"""99!&111((***{"""99!*555((***
ElEEE
F
FFr   c                 2   t          |                                           }|r||= |r||         }| j        dk    rt          | j        d         dz            }n;t          |          | j        d         | j        d         z  z  }| j        d         |z  }t                      }t          dt          |          |          D ]7}|                    d           |                    ||||z                       8t          j	        |t          j                  S )Nrp  r   r1   r       )level)r  r4  r  r   r  r=  rangeextendzlibcompressSETTINGSr   )rC  r  r  re  row_sizechannels_countdata_with_paddingr'  s           r   r  r  )  s   S[[]]##D  "L!
x3a((Tsx{SXa['@A8A;/!1c$ii** 9 9  '''  a!h,&6!78888=*(2LMMMMr   c                     t          |                                           |         }t          d |D                       S )Nc              3   "   K   | ]
}|d k    V  dS )r  Nr   ).0cs     r   	<genexpr>z_has_alpha.<locals>.<genexpr>@  s&      ''AqCx''''''r   )r  r4  any)rC  r  alphas      r   r  r  >  s9    ckkmm$$]3E''''''''r   r  )Nr   N)NN)4rU  r5  r2  r  dataclassesr   r   mathr   urllib.requestr   pathlibr   loggingPILr   r	   r
   	PIL.Imager   LANCZOSr  ImportError	ANTIALIASerrorsr   image_datastructuresr   r   r   rk  r   r   	getLoggerr   r>  SUPPORTED_IMAGE_FILTERSr  r  rG  r0  r-  rM  rc  r,  r;  r  r  r  r  r  r  r   r   r   <module>r     s                                   ! ! ! ! ! !             " " " " " "       ********#((((((% # # #?#    EEE " ! ! ! ! ! N N N N N N N N N N                      
 
	8	$	$K =??  >A Az A A A AHG G G* * *.       $  X X X Xv* * * * * * * *0  02! 2! 2!jG G G4N N N N*( ( ( ( (s5   A$ A A$ A A$ A  A$ $A.-A.