� J�g�c���ddlZddlZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z ddl Z ddlmZmZddlmZ dd lmZejZn#e$r ejZYnwxYwn #e$rdZYnwxYwd d lmZd d lmZmZmZd d lmZeGd�d����Z e j!e"��Z#dZ$e ��Z%gd�Z&dZ'dZ(dZ)dZ*d)defd�Z+d�Z,d�Z-d�Z.d�Z/d�Z0d*d�Z1Gd�d ��Z2d!�Z3d"�Z4d+d#�Z5d$�Z6d%�Z7d&�Z8d+d'�Z9d(�Z:dS),�N)� dataclass)�BytesIO)�ceil)�urlopen)�Path)�Image�TiffImagePlugin)�ImageCms)� Resampling�)� FPDFException)� ImageCache�RasterImageInfo�VectorImageInfo)� SVGObjectc��eZdZUdZeed<dS)� ImageSettings������compression_levelN)�__name__� __module__� __qualname__r�int�__annotations__���b/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/fpdf/image_parsing.pyrrs%������� ��s�����rr)�AUTO� FlateDecode� DCTDecode� JPXDecode� LZWDecode(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�����_���?�����ir�rR� image_cachec��t|���d��rR t|tt|����|���S#t$r}t d|����|�d}~wwxYwt |t��rEt|� ����r$t|tj |��|���St |tj ��rEt|� ��� ����rtd||���St |t��rd}�n(t |tj ��rV|���}tjdd���}|�|��|���|}}n�t |ttj f��r�t |tj ��r|� ��n|}|� ��}tjdd���}|�|��|���|}}nt|��|}}|j�|��}|r|d xxd z cc<n�t+|||j|��}t/|j��d z|d <d |d <d|d <|�d ��}|r]t0�d|��||jvr|j||d <n#t/|j��} | |j|<| |d <d|d <||j|<|||fS)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"zCould not parse file: N� vector_image�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� Exception� ValueError� 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�error�img�bytes_�img_hash�infor+r*s r� preload_imagerLKsB��( �4�y�y���&�!�!�I� I���j��T���&;�&;��U�U�U� U��� I� I� I��<�d�<�<�=�=�5� H����� I�����$����M�7�4�:�:�<�<�#8�#8�M��D�"�*�T�"2�"2� �L�L�L�L��$�� �#�#�K��� � ���0E�0E�0G�0G�(H�(H�K��N�D�k�J�J�J�J��$����$���� �D�%�+� &� &� $��������;�u�e�<�<�<����������&�&�(�(�$�c��� �D�5�"�*�-� .� .�$�$.�t�R�Z�$@�$@�J�������d���������;�u�e�<�<�<����������&�&�(�(�$�c�����I�I�t�c�� � � !� !�$� '� '�D� �(� �X����!�������D�#�{�'?��F�F��� �*�+�+�a�/��S� ���X����X���x�x����� � � �L�L�Z�� � � ��{�/�/�/�!,�!9�$�!?��X����[�5�6�6��17� �(��.�!'��X���D��L�#'� ��4� � ��d�?�s�+A� A2�A-�-A2c�V�|�d��p|�d��S)Ns<?xml s<svg )� startswith)rIs rr4r4�s)�� � � �Y� '� '� F�6�+<�+<�X�+F�+F�Frc��t|t��r|St|t��rt|��}|�d��rIt |��5}t|�����cddd��S#1swxYwYn$|�d��rt|��St|d��5}t|�����cddd��S#1swxYwYdS)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. )zhttp://zhttps://Nzdata:�rb) r2rrr,rNr�read�_decode_base64_image�open)�filename�url_file� local_files rr/r/�sx���(�G�$�$�����(�D�!�!�!��x�=�=�����2�3�3�.��X� � � ,�(��8�=�=�?�?�+�+� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,���� ,� ,� ,� ,� ,� � � �W� %� %�.�#�H�-�-�-� �h�� � �*���z���(�(�)�)�*�*�*�*�*�*�*�*�*�*�*�*����*�*�*�*�*�*s$� !B�B�B�!C<�<D�Dc���|�d��}t|��dkrtd���|d}tj|��}t |��S)z7Decode the base 64 image string into an io byte stream.zbase64,rbz!Unsupported non-base64 image datar )�splitrA�NotImplementedError�base64� b64decoder)� base64Image�frags� imageData� decodedDatas rrRrR�sZ�� � � �i� (� (�E� �5�z�z�Q���!�"E�F�F�F��a��I��"�9�-�-�K� �;� � �rc�.� tjt|����}n1#tj$rt�d|��YdSwxYw|jj���}|dvrt�d||��dSdS)z%Checks the validity of an ICC profilezInvalid ICC Profile in file %sF)�GRAY�RGBzEUnsupported color space %s in ICC Profile of file %s - cf. issue #711T) r �getOpenProfiler� PyCMSErrorrBrK�profile� xcolor_spacer5)r+rTre� color_spaces r� is_iccp_validrh�s�����)�'�$�-�-�8�8���� � ����� � �4�h�?�?�?��u�u������/�.�4�4�6�6�K��/�)�)�� � � S� � � � � � �u� �4s�!$�*A�Ac���t|���|���}|jr |j\}}}}ndx}}|jr|j}|jr|j}t |||���}|||fS)Nr$g)�data�w�h)rr7�viewbox�width�heightr)rTrHr"�svg�_rkrlrKs rr.r.�s��� �C�L�L�N�N� � <� <� <�C� �{���[� ��1�a���� ��A� �y�� �I�� �z�� �J�� ��q�A� .� .� .�D� �S�$� �rrc�L�t�td���d}d}d}d}|rt|ttf��r&t |��}tj|��}d}nkt|tj��sQt|t��}t|t��rt|��n|}tj|��}d}d}|r|� |t���}d}|dkr8|j dkrd}n*|j d krttjd ��rd }nd }|j d vr|d kr|�d��}|j dvr|�d��}d}|j dvr|dkr|�d��}n$|j dvr|dkr|�d��}|j\} } t#��} d} d|jvr(t'|jd|��r |jd} |��w|�st|j dkr�|dkr�|j dvrd\} }}n=|j dkr d\} }}d}n)|j dkrd\} }}nt)d|j �����|�d��| �|���| | || | |||d| �d| ��d� ��| S|j d k�r�|d k�r�|jd!d"k�r�t1|jt4j��d#k�ryt1|jt4j��d#k�rP|jt4j}d}|dkrd}n|d#krt)d$|�����t=|��\}}|�|��|�|��}|j�t4j ��}|�|d#krn\|d%krDtC|��}tE|��D]\}}tF|||<�t|��}nt)d&|�����d'\} }}| �|| | d| ||||d(t | ���$���d| �d)| ��d*� ��| Sd}|j d krd'\} }}tK||��| d+<�n"|j dkrd\} }}tK||��| d+<�n�|j dkrWd\} }}tMd#dd%��}tK|||�,��| d+<tO||��r|d-vrtK|||�.��| d/<�n�|j d0kr�d1\} }}tK||��| d+<|j(j(| d2<|j�d3d���;|d-vr7tK|�d��|tMd4dd5���.��| d/<�n|j d6kred1\} }}|j(j(| d2<tMd#dd%��}tK|||�,��| d+<tO||��r|d-vrtK|||�.��| d/<n�|j dkrd\} }}tK||��| d+<nz|j dkrd\} }}tK||��| d+<nUd\} }}tMd4dd5��}tK|||�,��| d+<tO||��r|d-vrtK|||�.��| d/<d| �d| ��}|j d krd7| �d)| ��}|s|rd|_)n|�*��| �| | || || |||d8� ��| S)9z� 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)ru�L�LArbrzrxry�CMYK)rxrzr"rbr|r{� icc_profile)rbrz)r�r2� DeviceRGBr})rBr2� DeviceCMYK)r r2� DeviceGrayzUnsupported image mode: rz/Predictor 15 /Colors z /Columns ) rjrkrl�csr+�dpn�bpc�f�inverted�dp�TIFF� compression�group4r z4unsupported photometric interpretation for g4 tiff: rbzunsupported FillOrder: )r r r�z /BlackIs1 z /K -1 /Rows ) rjrkrlr+r�r�r�r�r�r�rj)� remove_slice)r r!)� select_slice�smaskrx)r r2�Indexed�pal� transparencyr�rBryz/BlackIs1 true /Columns ) rkrlr�r+r�r�r�r�r�)+r�EnvironmentErrorr2rr,r/rSrr3�resize�RESAMPLE�format�mode�hasattr�core�convert�sizerrKrhr1�seekr;rQrA�tag_v2r � STRIPOFFSETS�STRIPBYTECOUNTS�PHOTOMETRIC_INTERPRETATION�ccitt_payload_location_from_pilr>� FILLORDER� bytearray� enumerate�TIFFBitRevTable�lower�_to_data�slice� _has_alpha�palette�fp�close)rTrHr@rF� is_pil_img�keep_bytes_io_open� jpeg_inverted� img_raw_data� img_alteredrkrlrKr+r�r��colspace�photor��offset�length� ccittrawdata� fillorderr)�n� alpha_channelr�s rr?r?�s��� �}��R�S�S�S��J����M��L� ��*�S�4��+�.�.��!�(�+�+� ��j��&�&��� � � ��U�[� )� )��'��W�5�5��'1�#�u�'=�'=�F�w�s�|�|�|�3� ��j��&�&��� ��K� ���j�j���j�1�1��� ��v��� �:�� � �&�L�L� �X��_�_����5R�!S�!S�_�,�L�L�(�L� �x�;���<�=�#@�#@��k�k�&�!�!�� �x�I�I�I��k�k�&�!�!��� � �x�=� � �\�[�%@�%@��k�k�%� � ��� ��d� � � � � ;� ;��k�k�#���� �8�D�A�q� � � �D� �D���� � � ���-�0�(� ;� ;� +��8�M�*�D��� �� �:�� � �L�K�$?�$?��x�?�*�*�%6�"��S�(�(���V�#�#�%7�"��S�(� $� � ���S���%7�"��S�(�(� �!F�C�H�!F�!F�G�G�G� � � �a� � � � �K�K�(�-�-�/�/���"� ���%� -�E�3�E�E�!�E�E� � � � � ��K� �J�&� � �� 0�0�0����'�8�3�3��C�J��;�<�=�=��B�B��C�J��>�?�@�@�A�E�E��J��I�J�E��H���z�z�����!��� �R�5�R�R����=�S�A�A�N�F�F� � � �f� %� %� %�'�,�,�V�4�4�L�� ����'@�A�A�I�� �I��N�N���a���(��6�6� �%�l�3�3�9�9�D�A�q�&5�a�&8�L��O�O�$�\�2�2� � � �!F�9�!F�!F�G�G�G�!3� �C��h� �K�K�(��� ��"��%� -�_�s�x�<�'8�'8�'>�'>�'@�'@�_�_�A�_�_�\]�_�_� � � � � ��K��L� �x�3���/���S�(���\�2�2��V� � � ��S���/���S�(���\�2�2��V� � � ��T� � �/���S�(��a��q�)�)� ���\� �N�N�N��V� � �c�=� )� )� T�l�C �/ �/ �%�S�,�]�S�S�S�D��M�� ��S���,���S�(���\�2�2��V� ��k�)��U� � �8�<�<��� -� -� 9�l�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�S�D��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�S�D��M� 4�#� 4� 4�� 4� 4�B� �x�3��� ;�� ;� ;�� ;� ;�� �� � ��C�F�F� �I�I�K�K�K��K�K��������%�� � � � � � �Krc�$�eZdZdZd�Zd�Zd�ZdS)� temp_attrzM temporary change the attribute of an object using a context manager c�0�||_||_||_dS�N)�obj�field�value)�selfr�r�r�s r�__init__ztemp_attr.__init__�s������� ��� � � rc���d|_t|j|j��r&d|_t |j|j��|_t |j|j|j��dS)NFT)�existsr�r�r��getattr� old_value�setattrr�)r�s r� __enter__ztemp_attr.__enter__�sY���� � �4�8�T�Z� (� (� ;��D�K�$�T�X�t�z�:�:�D�N����$�*�d�j�1�1�1�1�1rc��|jr"t|j|j|j��dSt |j|j��dSr�)r�r�r�r�r��delattr)r��exctype�excinst�exctbs r�__exit__ztemp_attr.__exit__�sD�� �;� *� �D�H�d�j�$�.� 9� 9� 9� 9� 9� �D�H�d�j� )� )� )� )� )rN)rrr�__doc__r�r�r�rrrr�r��sK����������� 2�2�2�*�*�*�*�*rr�c���|jtj}|jtj}t |��dkst |��dkrt d���||c\}\}||fS)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�rArY)rH� strip_offsets� strip_bytesr�r�s rr�r��sz���J��;�<�M��*�_�<�=�K�  �=���Q���#�k�"2�"2�a�"7�"7�!� I� � � �)�+��I�V�y�� �6�>�rc�����t��}tj�j�j������}�jddzdz�jdz�t td��rGttd���5|� |dd���d d d ��n #1swxYwYnctj j ����fd �}ttj d |��5|� |dd���d d d ��n #1swxYwY|� d��tj |��}t|��\}}|� |��|�|��S) zN Convert the open PIL.Image imgdata to compressed CCITT Group4 data. rrr2r � STRIP_SIZEr�r�)r�r�Nc���tj�jdtj�gtjdgi}|�|�||����S)Nr r)r � ROWSPERSTRIPr�r�r�r>)r��tag� overridesrH�pillow__getitem__�tmp_strip_sizes ���r� __getitem__z)transcode_monochrome.<locals>.__getitem__sQ����,�c�h�q�k��/�.�1A��,�q�c��I� �=�=��&7�&7��c�&B�&B�C�C� Crr�)rr� frombytesr�r�r8r�r r��save�ImageFileDirectory_v2r�r�rSr�rQ) rH�newimgio�img2r��newimgr�r�r�r�s ` @@r�transcode_monochromer��s"������y�y�H� �?�3�8�S�X�s�{�{�}�}� =� =�D��h�q�k�A�o�!�+�c�h�q�k�9�N��� �-�-�E� �� �n� E� E� E� E� �I�I�h�v�8�I� 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� �I�I�h�v�8�I� D� D� D� E� E� E� E� E� E� E� E� E� E� E���� E� E� E� E�  �M�M�!���� �Z�� !� !�F�4�V�<�<�N�F�F� �M�M�&���� �=�=�� � � s$�B5�5B9�<B9�4D�D� Dc��t|�����}|r||=|r||}|jdkrt|jddz ��}n;t |��|jd|jdzz}|jd|z}t��}t dt |��|��D]7}|�d��|�||||z����8|}tg}t��\} } } } d} |D]�}| t|g��z}|| vr|} �|� | | ��| dtzdz kr)| | |<| dz } | | kr| tkr | dz } d| zdz } n-|� t��t��\} } } } t|g��} ��| r|� | | ��|� t��t|��S)Nrurr2r �r)r�r8r�rr�rA�range�extend�LZW_CLEAR_TABLE_MARKER� clear_tabler3�append�LZW_MAX_BITS_PER_CODE�LZW_EOD_MARKER�pack_codes_into_bytes)rHr�r�rj�row_size�channels_count�data_with_paddingr)� result_codes�table� next_code� bits_per_code�max_code_value�current_sequence�byte� next_sequences r� _to_lzwdatar�sG�� �S�[�[�]�]� #� #�D��� �� ��"��L�!�� �x�3������� �a��(�(����T���s�x��{�S�X�a�[�'@�A���8�A�;��/��!� � �� �1�c�$�i�i�� *� *�9�9��� � ��'�'�'�� � ��a�!�h�,�&6�!7�8�8�8�8� �D�+�+�L�6A�m�m�3�E�9�m�^����-�-��(�5�$��=�=�8� � �E� !� !�,� � � � � ��&6� 7� 8� 8� 8��Q�"7�7�1�<�<�<�'0��m�$��Q�� ��~�-�-�-�BW�2W�2W�!�Q�&�M�&'�=�&8�A�%=�N���#�#�$:�;�;�;�BM�-�-�?��y�-�� %�d�V�}�}� � ��5����E�"2�3�4�4�4�����'�'�'� �� .� .�.rc���t��\}}}}d}d}t��}|D]�}||z|z}||z }|dkr&|dz}|�||z dz��|dk�&|tkrt��\}}}}�Z|tkr#|dz }||kr|t kr |dz }d|zdz }��|dkr|�|d|z zdz��t |��S)z� Convert the list of result codes into a continuous byte stream, with codes packed as per the code bit-width. The bit-width starts at 9 bits and expands as needed. rr2r r )r�r�r�r�r�r�r3) �codesrqr�r�r��buffer�bits_in_buffer�output�codes rr�r�Xs3�� � � � � ���� �F��N� �[�[�F��:�:���M�)�T�1���-�'����!�!� �a� �N� �M�M�6�^�3�t�;� <� <� <���!�!� �)� )� )�:E�-�-� 7�A�y�-��� �^� #� #� ��N�I��>�)�)�m�>S�.S�.S���"� �"#�}�"4��!9�������� � �v�!�n�"4�5��=�>�>�>� ��=�=�rc�r�d�td��D��}tdz}t}d|zdz }||||fS)zK Reset the encoding table and coding state to initial conditions. c�0�i|]}t|g��|��Sr)r3)�.0r)s r� <dictcomp>zclear_table.<locals>.<dictcomp>�s"�� /� /� /�q�U�A�3�Z�Z�� /� /� /rr!r )r�r��LZW_INITIAL_BITS_PER_CODE)r�r�r�r�s rr�r�sJ�� 0� /�E�#�J�J� /� /� /�E���"�I�-�M��=�(�A�-�N� �)�]�N� :�:rc ��|dkr t|fi|��S|dkrt|��S|dkr t|fi|��S|jdkr|�d��}|jdkr|�d��}|dkr9t ��}|�|d � ��|���S|d kr9t ��}|�|d � ��|���Std |�d����)Nrrwr"r|r{rzrbr rt)r�r!�JPEG2000zUnsupported image filter: "�") � _to_zdatar�r�r�r�rr�r7r )rHr@�kwargs�compressed_bytess rr�r��s0���}�$�$���'�'��'�'�'��'�'�'�#�C�(�(�(��{�"�"��3�)�)�&�)�)�)� �x�4����k�k�#���� �x�6����k�k�%� � ���{�"�"�"�9�9�� ���!�&��1�1�1��(�(�*�*�*��{�"�"�"�9�9�� ���!�*��5�5�5��(�(�*�*�*� �E�l�E�E�E� F� F�Frc�2�t|�����}|r||=|r||}|jdkrt|jddz ��}n;t |��|jd|jdzz}|jd|z}t��}t dt |��|��D]7}|�d��|�||||z����8tj |tj ���S)Nrurr2r r�)�level) r�r8r�rr�rAr�r��zlib�compress�SETTINGSr)rHr�r�rjr�r�r�r)s rrr�s �� �S�[�[�]�]� #� #�D��� �� ��"��L�!�� �x�3������� �a��(�(����T���s�x��{�S�X�a�[�'@�A���8�A�;��/��!� � �� �1�c�$�i�i�� *� *�9�9��� � ��'�'�'�� � ��a�!�h�,�&6�!7�8�8�8�8� �=�*�(�2L� M� M� M�Mrc��t|�����|}td�|D����S)Nc3�"K�|] }|dkV�� dS)r Nr)r��cs r� <genexpr>z_has_alpha.<locals>.<genexpr>�s&����'�'�A�q�C�x�'�'�'�'�'�'r)r�r8�any)rHr��alphas rr�r��s9�� �c�k�k�m�m� $� $�]� 3�E� �'�'��'�'�'� '� '�'rr�)NrN)NN);rZr9r6r � dataclassesrr�mathr�urllib.requestr�pathlibr�logging�PILrr r � PIL.Imager �LANCZOSr�� ImportError� ANTIALIAS�errorsr �image_datastructuresrrrrprr� getLoggerrrB�SUPPORTED_IMAGE_FILTERSr r�r�r�rr�rLr4r/rRrhr.r?r�r�r�r�r�r�r�rr�rrr�<module>r!sH�� � � � � � � � � � � � � � � � �!�!�!�!�!�!�������������"�"�"�"�"�"����������� �*�*�*�*�*�*�*�*�������#�(�(�(�(�(�(��%���� �#�#�#��?����#���������� �E�E�E�����"�!�!�!�!�!�N�N�N�N�N�N�N�N�N�N������� � � � � � � � � �� � �� �8� $� $��X�� �=�?�?������<��������D�D�z�D�D�D�D�NG�G�G�*�*�*�. � � ����$ � � �]�]�]�]�@*�*�*�*�*�*�*�*�0���02!�2!�2!�j9/�9/�9/�9/�x$�$�$�N ;� ;� ;�G�G�G�:N�N�N�N�*(�(�(�(�(s5�A$� A�A$� A �A$�A � A$�$A.�-A.
Memory