
    JՁj,;                        d dl Z d dlmZ d dlmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZ  e j                  e      ZdZde	d	efd
Zde	d	efdZde	d	efdZde	d	efdZded	efdZdeded	efdZde	ded	efdZde	d	efdZddee   ded	efdZy)    N)BytesIO)IO   )WHITESPACESWHITESPACES_AS_BYTESBinaryStreamType
StreamTypelogger_warningread_non_whitespace)PdfReadErrori    streamreturnc                     t        |       }|| j                  d      z  }| j                  dd       |d d dk(  xr |dd dk(  xs |dd t        v S )Nr         EI       )r   readseekr   )r   ei_toks     B/tmp/pip-target-jj2dfcxx/lib/python/pypdf/generic/_image_inline.py_check_end_image_markerr   1   s]     (F
fkk!nF
KKA"1:UF1QK3$6$T&1+:TUr   c                    t               }	 t        |       | j                  t              x}z   }|r|st	        d      |j                  d      }|dk\  r.||d|dz    z  }| j                  t        |       |z   dz   d       n|j                  d      }|dk\  ru| j                  t        |       |z   dz
  d       | j                  d      }|t        v r1| j                  dd       | j                  d      }|dz  }|t        v r1||d| z  }n:t        |      dk(  r||z  }t	        d      ||dd z  }| j                  dd       ;t        |       st	        d	      t        |      S )
z`
    Extract HexEncoded stream from inline image.
    The stream will be moved onto the EI.
    Unexpected end of stream.   >r   Nr   r   r   EI stream not found.)	bytearrayr   r   BUFFER_SIZEr   findr   lenr   r   bytes)r   data_out
read_bytesdata_bufferedpos_tokpos_eics          r    extract_inline__ascii_hex_decoder*   8   s   
 {H
+F3V[[Q\E]7]z^J:;;$$T*a<m!44HKK]++g591=##E*Q;KK]++f4q8!<AA{"B"KKN! {" gv..H}"%H:;;M#2&&B1 4 #6*122?r   c                    t               }	 t        |       | j                  t              x}z   }|r|st	        d      |j                  d      }|dk\  r.||d|dz    z  }| j                  t        |       |z   dz   d       n9t        |      dk(  r||z  }t	        d      ||dd z  }| j                  dd       t        |       st	        d      t        |      S )	zY
    Extract A85 stream from inline image.
    The stream will be moved onto the EI.
    r   s   ~>r   Nr   r   r   r   )
r   r   r   r    r   r!   r   r"   r   r#   )r   r$   r%   r&   r'   s        r   extract_inline__ascii85_decoder,   ^   s    
 {H
+F3V[[Q\E]7]z^J:;;$$U+a<m!44HKK]++g591=}"%H:;;MR
 	
 	B " #6*122?r   c                 0   t               }	 | j                  t              }|st        d      |j	                  d      }|dk\  r||dz   |dz    }|j                  d      s|j                  d      r.||d|dz    z  }| j                  t        |       |z   dz   d       nQt        dt        	       |j	                  d      }|dkD  r*||d| z  }| j                  t        |       |z   dz
  d       n||z  }t        |       st        d
      t        |      S )zj
    Extract RL (RunLengthDecode) stream from inline image.
    The stream will be moved onto the EI.
    r      r   r      r   Nz=Early EOD in RunLengthDecode of inline image, using fallback.)sourcer   )r   r   r    r   r!   
startswithendswithr   r"   r
   __name__r   r#   )r   r$   r&   r'   after_token	ei_markers         r   !extract_inline__run_length_decoder6   {   s.   
 {H
K0:;;$$W-a< (!gkBK%%e,0D0DU0KM-GaK88S//'9A=qA^gop)..u5	q=k	 ::HKK]!3 3i ?! CQGM!3 6 #6*122?r   c                 z    dt         dt        f fd}t               }d}	  |d      }|s|dk(  r||z  }|dk7  rd} |d      }||z  }|dk(  r j                  dd       n9|d	k(  rn3|d
k(  rn.|dv r) |d      }||z  }|d   dz  |d   z   }| ||dz
        z  }{t	               st        d      t        |      S )z`
    Extract DCT (JPEG) stream from inline image.
    The stream will be moved onto the EI.
    lengthr   c                 `    j                  |       }|t        |      | k7  rt        d      |S )Nr   )r   r"   r   )r8   _resultr   s     r   r   z(extract_inline__dct_decode.<locals>.read   s4     ++f%?c'lf4:;;r   FTr             s&   r   r      r   )intr#   r   r   r   r   )r   r   r$   	not_firstr)   szs   `     r   extract_inline__dct_decoderC      s    
S U  {H I
GgMH<	GA<KKA'\'\T

 QAMH1ad"BR!V$H/ 2 #6*122?r   
stream_outpositionc                     |dk  ry| j                         }| j                  |dz
         | j                  d      t        v }| j                  |       |S )a3  
    Check whether the byte in front of `position` is a whitespace.

    Returns:
        Whether the preceding byte is whitespace. If no preceding byte exists,
        returns ``False`` because the ``EI`` marker is at the beginning of the
        image data and is therefore not delimited by anything.
    r   Fr   )tellr   r   r   )rD   rE   saved_positionis_whitespaces       r   _is_preceded_by_whitespacerJ      sQ     1}__&N OOHqL!OOA&+5MOON#r   
first_bytec                    | j                         }|dk(  r!| j                  d      }|dk(  xs |t        v }n8|dk(  r1| j                  d      }|dd dk(  xr |dd dk(  xs |dd t        v }nd	}| j                  |d
       |S )a<  
    Check whether `first_byte` starts an exact `Q` or `EMC` operator.

    `first_byte` has already been consumed from the stream, which is restored to its
    current position afterwards. Looking at this byte alone is not sufficient, as
    binary image data can easily be mistaken for one of these operators.
       Qr   r      Er   Nr   s   MCFr   )rG   r   r   r   )r   rK   rE   suffixis_closing_operators        r    _is_followed_by_closing_operatorrQ      s     {{}HTQ$mDv/D	t	Q$RajE1hva{c7I7gVTUVW[\gMg#
KK!r   c                    t               }	 | j                  t              }|st        d      |j	                  d      }|dk(  r|j                  |       n|j                  |d|dz           |j                         dz
  }| j                  |dz   t        |      z
  d       | j                         }| j                  d      }|dk7  r| j                  |d       | j                  d      }|dk(  r7| j                  |dz
  d       |j                  |       	 |j                         S |t        vr| j                  |d       9|t        v r| j                  d      }|t        v rt        ||      s t        | |      s| j                  |d       t        |       r| j                  |d       | j                  |dz
  d       |j                  |       	 |j                         S )zLegacy method, used by defaultr   rN   r<   r   r      Ir   )r   r   r    r   r!   writerG   r   r"   truncater   rJ   rQ   is_followed_by_binary_datagetvalue)r   rD   r&   r(   
sav_pos_ei	saved_postok2tok3s           r   extract_inline_defaultr\      s   J
K0:;;##
 R<]+ ]1vz:;#*Q.JKK
S%77;I;;q>Dt|Iq);;q>Ds{ IM1-##J/2   1 ;&Iq)+%{{1~ +%
 .j*ENnO Iq))&1 Iq) KK	Aq)
+  m r   r8   c                    | j                         }| j                  |      }| j                  |       |syd}d}t        |      D ]+  \  }}|dk  r
|t        vr y|t        v }||s|}|&|s)|} n |y||}||z
  }	||| }
|
j                  d      r|	dkD  ry|
j                  dd      j                         ry|	d	kD  ryy)
a  
    Check if the next bytes of the stream look like binary image data or regular page content.

    This is just some heuristics due to the PDF specification being too imprecise about
    inline images containing the `EI` marker which would end an image. Starting with PDF 2.0,
    we finally get a mandatory length field, but with (proper) PDF 2.0 support being very limited
    everywhere, we should not expect to be able to remove such hacks in the near future - especially
    considering legacy documents as well.

    The actual implementation draws some inspiration from
    https://github.com/itext/itext-java/blob/9.1.0/kernel/src/main/java/com/itextpdf/kernel/pdf/canvas/parser/util/InlineImageParsingUtils.java
    FN    T   /r      .r   r   )rG   r   r   	enumerater   r1   replaceisdigit)r   r8   rE   dataoperator_startoperator_endindexbyterI   operator_lengthoperators              r   rV   rV   7  s     {{}H;;vD
KKNL  t"9%99  44!-"N%- !L "^3ON<0H4 _q%8c"**, r   )
   )loggingior   typingr   _utilsr   r   r   r	   r
   r   errorsr   	getLoggerr3   loggerr    boolr   r#   r*   r,   r6   rC   r@   rJ   rQ   r\   rV    r   r   <module>ru      s  8     "			8	$ VJ V4 V#Z #E #L: % :$j $U $N,'7 ,E ,^7 c d (Z U t *:!: :!% :!z6r%y 6# 6t 6r   