
    j5j8                    @   d Z ddlmZ 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mZmZ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mZmZmZm Z m!Z!m"Z"  ejF                         Z$ G d
 d      Z%ddZ&ddZ'	 	 	 	 	 	 ddZ(ddZ)de"jT                  d	 	 	 	 	 ddZ+y)zPDF image analysis.    )annotationsN)Iterator)Decimal)	
DictionaryMatrixNameObjectPdfPdfImagePdfInlineImageStreamUnsupportedImageTypeError)
Resolution)ContentsInfo
TextMarkerVectorMarker_get_dpi_interpret_contents_is_unit_square)FRIENDLY_COLORSPACEFRIENDLY_COMPFRIENDLY_ENCODINGUNIT_SQUARE
ColorspaceEncodingInkc                  0   e Zd ZU dZ ed      Zded<   ded<   dddddd		 	 	 	 	 dd
ZddZe	d        Z
e	d        Ze	dd       Ze	dd       Ze	dd       Ze	d        Ze	d        Ze	d        Ze	d        Ze	dd       Ze	dd       Ze	dd       Zd Zy) 	ImageInfozInformation about an image found in a PDF.

    This gathers information from pikepdf and pdfminer.six, and is pickle-able
    so that it can be passed to a worker process, unlike objects from those
    libraries.
    z1.000z
int | None_compstr_name N)namepdfimageinline	shorthandfill_inkc                  t        |      | _        || _        || _        |
d| _        |}n0|#t        |t              rd| _        t        |      }nt        d      |j                  | _
        |j                  | _        |j                  j                  t        j                   d      x}t        |t        t"        z        rrt%        |j                  t        j&                  d      | j                        | _
        t%        |j                  t        j(                  d      | j                        | _        |j                  j                  t        j*                  d      x}t        |t        t"        z        rrt%        |j                  t        j&                  d      | j                        | _
        t%        |j                  t        j(                  d      | j                        | _        |j,                  rd| _        nd| _        t1        |j2                        | _        t7        |j8                        dk(  r:|j8                  d   d	k(  r(|j8                  d
   dk(  rt:        j<                  | _        n(	 tA        j                  |j8                  d         | _        	 tE        j                  |jF                  xs d      | _$        | j>                  t:        jL                  k(  rtN        jL                  | _$        d| _(        | jH                  tN        jR                  k(  r't        |t              r| jU                  |      | _(        yt        | jH                  tN              r$tW        j                  | jH                        | _(        | jP                  J| j>                  t:        jX                  t:        jZ                  fv rtV        tN        j\                     | _(        yyy# tB        $ r d| _        Y Nw xY w# tJ        $ r d| _$        Y <w xY w)zInitialize an ImageInfo.Nr%   xobjectz%Either pdfimage or inline must be setr   stencilimage   z/FlateDecode   z
/DCTDecoder"   )/r    r!   
_shorthand	_fill_ink_origin
isinstancer   r   
ValueErrorwidth_widthheight_heightobjgetr   SMaskr   maxWidthHeightMask
image_mask_typeintbits_per_component_bpclenfiltersr   
flate_jpeg_encr   
IndexErrorr   
colorspace_colorNotImplementedErrorjpeg2000r   r   icc	_init_iccr   ccittjbig2gray)	selfr#   r$   r%   r&   r'   pimsmaskmasks	            M/var/www/html/qr/venv/lib/python3.12/site-packages/ocrmypdf/pdfinfo/_image.py__init__zImageInfo.__init__:   s    Y
#! #DLC!j6&B$DL8$CDEEiizzWW[[T22E?J6J&E
 eii

A6DDKuyya8$,,GDLGGKK		400D=*&:%C
 dhhtzz15t{{CDKtxxQ7FDL
 >>"DJ DJ../	!A.0A,. !++DI!-11#++a.A		-11#..2FBGDK 99)))$--DK
;;*..(ZX-F,DJ$++z2*..t{{;
 zz!diiHNNHNN3S&S*:??;
 'T!%  ! 	!
 # 	DK	s$   /'O (O( O%$O%(O<;O<c                V   	 |j                   }|t	        |d      st        j                  d|         y 	 |j
                  j                  dk(  ry|j
                  j                  dk(  ryy	# t        $ r%}t        j                  d| d|         Y d }~y d }~ww xY w# t        $ r Y y w xY w)
NznAn image with a corrupt or unreadable ICC profile was found. Output PDF may not match the input PDF visually: z. profilezuAn image with an ICC profile but no ICC profile data was found. The output PDF may not match the input PDF visually. GRAYr-   CMYK      )rL   r   loggerwarninghasattrrX   xcolor_spaceAttributeError)rQ   rR   rL   es       rU   rM   zImageInfo._init_icc   s    	''C ;gc95NNHHLvO 	{{''61))V3' ) 	NNDDE3bP 	(  		s.   A+ B B +	B4BB	B('B(c                    | j                   S )z+Name of the image as it appears in the PDF.)r!   rQ   s    rU   r#   zImageInfo.name        zz    c                    | j                   S )z+Type of image, either 'image' or 'stencil'.)r?   rd   s    rU   type_zImageInfo.type_   re   rf   c                <    | j                   dk(  r| j                  S dS )a@  Fill-color classification for stencil masks, else None.

        A stencil (image mask) is painted with the current fill color; this
        reports whether that color is mono/gray/color so the rasterizer can
        choose a device that does not discard the distinction. Non-stencil
        images return None.
        r*   N)r?   r/   rd   s    rU   inkzImageInfo.ink   s     "&y!8t~~BdBrf   c                    | j                   S )zWidth of the image in pixels.)r4   rd   s    rU   r3   zImageInfo.width   s     {{rf   c                    | j                   S )zHeight of the image in pixels.)r6   rd   s    rU   r5   zImageInfo.height   s     ||rf   c                    | j                   S )zBits per component.)rB   rd   s    rU   bpczImageInfo.bpc   s     yyrf   c                6    | j                   | j                   S dS )zColorspace of the image.?)rI   rd   s    rU   colorzImageInfo.color   s     #kk5t{{>3>rf   c                6    | j                   | j                   S dS )z+Number of components/channels in the image.rp   )r   rd   s    rU   compzImageInfo.comp   s     "ZZ3tzz<<rf   c                6    | j                   | j                   S dS )zEncoding of the image.r+   )rF   rd   s    rU   enczImageInfo.enc   s     !II1tyy>w>rf   c                    | j                   j                  xr1 | j                  dk\  xr  | j                  dk\  xr | j                  dk7  S )a  Whether the image is renderable.

        Some PDFs in the wild have invalid images that are not renderable,
        due to unusual dimensions.

        Stencil masks are not also not renderable, since they are not
        drawn, but rather they control how rendering happens.
        r   r*   )dpi	is_finiter3   r5   rh   rd   s    rU   
renderablezImageInfo.renderable   sJ     HH (

a(q ( 

i'		
rf   c                Z    t        | j                  | j                  | j                  f      S )znDots per inch of the image.

        Calculated based on where and how the image is drawn in the PDF.
        )r   r.   r4   r6   rd   s    rU   rw   zImageInfo.dpi   s"     $++t||)DEErf   c                    | j                   syt        | j                  | j                  j                  z  | j
                  | j                  j                  z  z        S )z,Physical area of the image in square inches.g        )ry   floatr3   rw   xr5   yrd   s    rU   printed_areazImageInfo.printed_area   s@     djj488::-$++

2JKLLrf   c                    d| j                    d| j                   d| j                   d| j                   d| j                   d| j
                   d| j                   d| j                   d| j                   dS )z,Return a string representation of the image.z<ImageInfo 'z'     ×>)	r#   rh   r3   r5   rq   rs   rn   ru   rw   rd   s    rU   __repr__zImageInfo.__repr__   sm     499+R

|1TZZL4;;-qzzl!DII;az488*AdhhZqJ	
rf   )r$   zObject | Noner%   zPdfInlineImage | Noner'   
Ink | None)rR   r   )returnr   )r   r@   )r   bool)r   r   )r   r|   )__name__
__module____qualname____doc__r   DPI_PREC__annotations__rV   rM   propertyr#   rh   rj   r3   r5   rn   rq   rs   ru   ry   rw   r   r    rf   rU   r   r   -   sW    wHJ
 "&(,#U<  	U<
 &U< U<n4     C C       ? ? = = ? ? 
 
  F F M M
rf   r   c              #     K   t        | j                        D ]8  \  }}t        d|d|j                  |j                  |j
                         : yw)z(Find inline images in the contentstream.zinline-02d)r#   r&   r%   r'   N)	enumerateinline_imagesr   r&   iimager'   )contentsinfonr%   s      rU   _find_inline_imagesr     sT     |99: 
	61S'"&&==__	
 	

s   AAc              #  T  K   t         j                  | vry| t         j                     }t         j                  |vry|t         j                     j                         D ]G  \  }}|t         j                  |vr|t         j                     t         j
                  k(  s@|}||f I yw)a  Search for all XObject-based images in the container.

    Usually the container is a page, but it could also be a Form XObject
    that contains images. Filter out the Form XObjects which are dealt with
    elsewhere.

    Generate a sequence of tuples (image, xobj container), where container,
    where xobj is the name of the object and image is the object itself,
    since the object does not know its own name.

    N)r   	ResourcesXObjectitemsSubtypeImage)	container	resourceskey	candidater$   s        rU   _image_xobjectsr     s      ~~Y&$..)I||9$#DLL1779 "YI =T\\"djj0 HS/!"s   BB(B(c              #     K   t        |       D ]|  \  }}||j                  vr|j                  |   D ]V  }|j                  dk(  rt        |j                        r(t        |j                  ||j                  |j                         X ~ yw)zFind images stored in the container's /Resources /XObject.

    Usually the container is a page, but it could also be a Form XObject
    that contains images.

    Generates images with their DPI at time of drawing.
    r   )r#   r$   r&   r'   N)r   
name_indexstack_depthr   r&   r   r#   r'   )r   r   r$   xobjdraws        rU   _find_regular_imagesr   *  s      *)4 $|... ++D1 	D1$)H YY!..	 	s   BBc              #    K   t         j                  |vry|t         j                     }t         j                  |vry|t         j                     j                         }|D ]  }||   }|0|j	                  t         j
                        t         j                  k7  r;|}|j                  D ]?  }|j                  |k7  r|j                  }	t        | ||	|j                        E d{    A  y7 	w)z}Find any images that are in Form XObjects in the container.

    The container may be a page, or a parent Form XObject.

    N)pdfr   r&   initial_fill_ink)r   r   r   as_dictr8   r   Formxobject_settingsr#   r&   _process_content_streamsr'   )
r   r   r   r   xobjsr   r   form_xobjectsettingsctm_shorthands
             rU   _find_form_xobject_imagesr   H  s      ~~Y&$..)I||9$dll#++-E $K		dll ;tyy H $55 	H}}$ %..M 0&'!)!2!2	  	$s   C!C/#C-$
C/)r&   r   c              #    K   |j                  t        j                        t        j                  k(  rt        j                  |v r|xs t
        }n|j                  t        j                        t        j                  k(  r|t        j                     t        j                  k(  r\|rt        |      n	t               }|j                  t        j                  t                     }t        |      }||z  }|j                  }nyt        |||      }|j                  rt                |j                  rt                t!        |      E d{    t#        ||      E d{    t%        | ||      E d{    y7 .7 7 	w)a  Find all individual instances of images drawn in the container.

    Usually the container is a page, but it may also be a Form XObject.

    On a typical page images are stored inline or as regular images
    in an XObject.

    Form XObjects may include inline images, XObject images,
    and recursively, other Form XObjects; and also vector graphic objects.

    Every instance of an image being drawn somewhere is flattened and
    treated as a unique image, since if the same image is drawn multiple times
    on one page it may be drawn at differing resolutions, and our objective
    is to find the resolution at which the page can be rastered without
    downsampling.

    N)r8   r   TypePageContentsr   r   r   r   r   r&   r   found_vectorr   
found_textr   r   r   r   )	r   r   r&   r   initial_shorthandctmform_shorthandform_matrixr   s	            rU   r   r   n  s!    ( }}TYY499,)1K%4dii DLL0dll#tyy0 $-fY&( #t{{FH=^, CMM&y2CEUVL  nl"<000#I|<<<(iFFF 1<Fs6   E
FE;F E=!F5E?6F=F?F)r   r   r   Iterator[ImageInfo])r   zIterator[tuple[Object, str]])r   r	   r   r   r   r   )r   r
   r   r	   r   r   )r   r
   r   r	   r   z/Iterator[VectorMarker | TextMarker | ImageInfo]),r   
__future__r   loggingcollections.abcr   decimalr   pikepdfr   r   r   r	   r
   r   r   r   r   ocrmypdf.helpersr   ocrmypdf.pdfinfo._contentstreamr   r   r   r   r   r   ocrmypdf.pdfinfo._typesr   r   r   r   r   r   r   	getLoggerr]   r   r   r   r   r   monor   r   rf   rU   <module>r      s     "  $ 
 
 
 (    
			V
 V
r
"2%1<#N /3SXX2G2G"2G42Grf   