
    Wwg'                         d 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
 ddlmZmZ ddlmZ dd	lmZ 	 dd
lmZ ddlmZ ddgZdej$                  defdZ G d de      Zy# e$ rZ ede      dZ[ww xY w)a}  Utilities for embedding bitmap image data into PDF files.

The image data handling is done by
`Pillow <https://github.com/python-pillow/Pillow>`_.

.. note::
    Note that also here we only support a subset of what the PDF standard
    provides for. Most RGB and grayscale images (with or without transparency)
    that can be read by PIL/Pillow can be used without issue.
    PNG images with an indexed palette backed by one of these colour spaces
    can also be used.

    Currently there is no support for CMYK images or (direct) support for
    embedding JPEG-encoded image data as such, but these features may be added
    later.

    N)OptionalUnion   )generic)
PdfContentPdfResourcesResourceType)IndirectObjectpdf_name)BoxConstraints)BasePdfFileWriter)Image)ImagePalettezpyhanko.pdf_utils.images requires pyHanko to be installed with the [image-support] option. You can install missing dependencies by running "pip install 'pyHanko[image-support]'".	pil_imagePdfImageimgwriterc           
         | j                   dvrt        t        d      t        d      t        d      t        d      t        d      t        j                  | j
                        t        d      t        j                  | j                        i}t        j                  d      }d	}| j                   j                  d
      rN| j                         d   }|j                   dk(  sJ t        ||      }| j                  | j                   d	d       } | j                   dk(  rt        d      n
t        d      }| j                   dk(  r| j                  }|J |j                  }|j                   dk7  rt        t        |      dz  }	t        j                  t        d      t        d      t        j                  |	dz
        t        j                  |      g      }|||t        d      <   ||t        d      <   ||t        d      <   | j                         }
t        j                   ||
      }|j#                          |j%                  |      S )a  
    This function writes a PIL/Pillow :class:`.Image` object to a PDF file
    writer, as an image XObject.

    :param img:
        A Pillow :class:`.Image` object
    :param writer:
        A PDF file writer
    :return:
        A reference to the image XObject written.
    )RGBRGBAPPALLAz/Typez/XObjectz/Subtypez/Imagez/Widthz/Height   NAr   z/DeviceGrayz
/DeviceRGBr   r      z/Indexedr   z/SMaskz/ColorSpacez/BitsPerComponent)stream_data)modeNotImplementedErrorr   r   NumberObjectwidthheightendswithsplitr   convertpalettelenArrayObjectByteStringObjecttobytesStreamObjectcompress
add_object)r   r   	dict_databpcsmask_imagesmask_pil_image	clr_spacer(   palette_arrpalette_sizeimage_bytesstreams               O/var/www/horilla/myenv/lib/python3.12/site-packages/pyhanko/pdf_utils/images.pyr   r   ,   s    xx<<!! 	8J/hx0G00;W11#**=	I 

q
!CK
xx))+b/##s***8kk#((3B-( $'88s?8N  xx3*-++"""oo<<5 %%;'1, ''$&$$\A%56((5	
	 (3	(8$%)2Ih}%&/2Ih*+,
 ++-K!!)EF
OOV$$    c                        e Zd ZdZ	 	 	 	 	 ddeej
                  ef   dee   dee	   dee   dee
   f
 fdZedej                  fd	       Zdefd
Z xZS )r   a  Wrapper class that implements the :class:`.PdfContent` interface for
    image objects.

    .. note::
        Instances of this class are reusable, in the sense that the
        implementation is aware of changes to the associated :attr:`writer`
        object. This allows the same image to be embedded into multiple files
        without instantiating a new :class:`.PdfImage` every time.

    imager   	resourcesnameboxc                 V   t        |t              rt        j                  |      }|| _        |xs t        t        j                               | _        || _        |4t        | j                  j                  | j                  j                        }t        | 5  |||       d | _        y )N)r   r?   )
isinstancestrr   openr<   uuiduuid4r>   opacityr   r#   r$   super__init__
_image_ref)selfr<   r   r=   r>   rF   r?   	__class__s          r9   rH   zPdfImage.__init__   s     eS!JJu%E"'
-C

-	; !1!14::3D3DEC6s;48r:   returnc                     | j                   J | j                  }||j                         | j                   ur't        | j                  | j                         x| _        }|J |S )a  Return a reference to the image XObject associated with this
        :class:`.PdfImage` instance.
        If no such reference is available, it will be created using
        :func:`.pil_image`, and the result will be cached until the
        :attr:`writer` attribute changes
        (see :meth:`~pyhanko.pdf_utils.content.PdfContent.set_writer`).

        :return: An indirect reference to an image XObject.
        )r   rI   get_pdf_handlerr   r<   )rJ   	image_refs     r9   rO   zPdfImage.image_ref   sd     {{&&&OO		 9 9 ;4;; N*3DJJ*LLDOi$$$r:   c           
      R   d| j                   z   }| j                  t        j                  t	        |      | j
                         d}| j                  dt        t        j                               z   }| j                  t        j                  t	        |      t        j                  t	        d      t        j                  | j                        i             |j                  d      dz   }| j                  j                   s%| j"                  j$                  | j                  _        | j                  j&                  s%| j"                  j(                  | j                  _        d| j                  j(                  | j                  j$                  |j                  d      fz  }d	||fz  S )
Nz/Img)categoryr>   valuer:   z/GSz/caasciis    gss   %g 0 0 %g 0 0 cm %s Dos	   q %s %s Q)r>   set_resourcer	   XOBJECTr   rO   rF   rB   rD   rE   EXT_G_STATEr   DictionaryObjectFloatObjectencoder?   height_definedr<   r$   width_definedr#   )rJ   img_ref_namerF   gs_namedraws        r9   renderzPdfImage.render   sM   		)!)),'.. 	 	
 <<#c$**,//G%11g&..e_g&9&9$,,&GH   nnW-6G
 xx&&"jj//DHHOxx%%!ZZ--DHHN(HHNNHHOO(,
 

 wo--r:   )NNNNN)__name__
__module____qualname____doc__r   r   rB   r   r   r   r   rH   propertyr   r
   rO   bytesr_   __classcell__)rK   s   @r9   r   r   u   s    	 /3,0"(,9U[[#%&9 *+9 L)	9
 sm9 n%9, 711  $!. !.r:   )rc   rD   typingr   r    r   contentr   r   r	   r
   r   layoutr   r   r   PILr   PIL.ImagePaletter   ImportErrore__all__r   r    r:   r9   <module>rq      s   $  "  ; ; - " %
- 

#F%5;; F%(9 F%RV.z V.m  
	4 	
 s   A" "A5'	A00A5