
    h5j                       U d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZmZmZ ddlmZ ddlmZmZ dd	lmZ ej,                  d
k(  rddl
mZ eZded<   ndZ G d de      Z G d de      Z e       aded<   ddZddZy)zwIntegrate JBIG2 image decoding.

Requires third-party JBIG2 decoder in the form of an external program, like
jbig2dec.
    )annotationsN)ABCabstractmethod)Path)DEVNULLPIPECalledProcessErrorrun)TemporaryDirectory)InvalidVersionVersion)DependencyErrorwin32)CREATE_NO_WINDOWintCREATION_FLAGSc                  <    e Zd ZdZedd       Zedd       ZddZy)	JBIG2DecoderInterfacezFpikepdf's C++ expects this Python interface to be available for JBIG2.c                     y)z=Check if decoder is available. Throws DependencyError if not.N selfs    C/var/www/html/qr/venv/lib/python3.12/site-packages/pikepdf/jbig2.pycheck_availablez%JBIG2DecoderInterface.check_available"           c                     y)z=Decode JBIG2 from jbig2 and globals, returning decoded bytes.Nr   )r   jbig2jbig2_globalss      r   decode_jbig2z"JBIG2DecoderInterface.decode_jbig2&   r   r   c                D    	 | j                          y# t        $ r Y yw xY w)z$Return True if decoder is available.TF)r   r   r   s    r   	availablezJBIG2DecoderInterface.available*   s,    	  "   		s    	NreturnNoner   bytesr   r'   r$   r'   )r$   bool)__name__
__module____qualname____doc__r   r   r    r"   r   r   r   r   r      s4    PL L L Lr   r   c                  6    e Zd ZdZeeddZddZd	dZd
dZ	y)JBIG2DecoderzJBIG2 decoder implementation.)subprocess_runcreationflagsc                    || _         || _        y)zInitialize the decoder.N)_run_creationflags)r   r/   r0   s      r   __init__zJBIG2Decoder.__init__7   s    "	+r   c                \    | j                         }||t        d      k  rt        d      yy)z*Check if jbig2dec is installed and usable.Nz0.15z-jbig2dec is too old (older than version 0.15))_versionr   r   )r   versions     r   r   zJBIG2Decoder.check_available<   s2    --/7WV_#<!"QRR $=r   c           	        t        dd      5 }t        |      dz  }t        |      dz  }t        |      dz  }ddd	d
dt        j                  |      g}|j	                  |       t        |      dkD  r5|j	                  |       |j                  t        j                  |             |j                  t        j                  |             | j                  |t        d| j                         ddl
m} |j                  |      5 }	|	j                         cddd       cddd       S # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)z6Decode JBIG2 from binary data, returning decode bytes.zpikepdf-z.jbig2)prefixsuffiximageglobaloutfilejbig2decz
--embeddedz--formatpngz--outputr   T)stdoutcheckr0   )ImageN)r   r   osfspathwrite_byteslenappendr2   r   r3   PILrB   opentobytes)
r   r   r   tmpdir
image_pathglobal_pathoutput_pathargsrB   ims
             r   r    zJBIG2Decoder.decode_jbig2B   s+   z(C  	$vf/Jv,1Kv,2K 		+&D ""5)=!A%''6BIIk23KK		*-.IIWD@S@S   "K( $Bzz|$ $? 	$  	$>$ $ $? 	$  	$  	$s$   C8D?D)	D?)D2	.D??Ec                $   	 | j                  ddgt        dd| j                        }|j                  }|j	                  dd      j                         }	 t        |      S # t        $ r Y y w xY w# t        t        f$ r}t        d      |d }~ww xY w)Nr>   z	--versionTascii)r@   rA   encodingr0    z%jbig2dec - not installed or not found)r2   r   r3   r@   replacestripr   r   r	   FileNotFoundErrorr   )r   procresultversion_stres        r   r6   zJBIG2Decoder._versionf   s    	99[) "11  D [[F ..Beg {++!  #$56 	R!"IJPQQ	Rs)   &A/ 
A   	A,+A,/B>B

BNr#   r&   )r$   zVersion | None)
r)   r*   r+   r,   r
   r   r4   r   r    r6   r   r   r   r.   r.   4   s     '),N ,
S"$Hr   r.   _jbig2_decoderc                     t         S )z&Return an instance of a JBIG2 decoder.r\   r   r   r   get_decoderr_      s    r   c                    | a y)zSet the JBIG2 decoder to use.Nr^   )jbig2_decoders    r   set_decoderrb      s	     #Nr   )r$   r   )ra   r   r$   r%   )r,   
__future__r   rC   sysabcr   r   pathlibr   
subprocessr   r   r	   r
   tempfiler   packaging.versionr   r   pikepdf._exceptionsr   platformr   r   __annotations__r   r.   r\   r_   rb   r   r   r   <module>rm      s}    # 	 
 #  = = ' 5 /<<7+*NC*NC *E( EP )5% 6
#r   