o
    hP                     @  s  d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ dd	lmZ d
dlmZ d
dlmZmZmZmZ zddlZW n eyj Z zd
dlmZ eeZW Y dZ[ndZ[ww i dddejejfdddejejfdddejej fdddejejfdddejejfdddejej fdddej!ej!fdddej!ej!fdd
dej!ej!fdd
dej!ej!fdd
dej!ej!fdddejejfdddejejfd ddejej fd!ddejejfd"ddejejfd#ddejej fi d$ddej!ej!fd%ddej!ej!fd&d
dej!ej!fd'd
dej!ej!fd(d
dej!ej!fd)dd*ejejfd+dd*ejejfd,dd*ejej fd-dd*ejejfd.dd*ejejfd/dd*ejej fd0dd*ej!ej!fd1dd*ej!ej!fd2d
d*ej!ej!fd3d
d*ej!ej!fd4d
d*ej!ej!fd5dd6ejej"fdd6ejej"fdd6ejej#fdd6ejej"fdd6ejej"fdd6ejej#fdd6ej!ej!fdd6ej!ej!fd
d6ej!ej!fdd6ej$ej%fd7	Z&d8d9d:d;Z'd:d9d8d<Z(d|dAdBZ)d}dDdEZ*d~dGdHZ+dddMdNZ,dddOdPZ-ddRdSZ.dddUdVZ/ddZd[Z0dd\d]Z1dd`daZ2ddbdcZ3ddddeZ4G dfdg dgeZ5ddidjZ6ddldmZ7ddpdqZ8ddrdsZ9G dtdu duZ:eG dvdw dwZ;ddzd{Z<dS )zoDifferent miscellaneous helper functions.

Mostly for internal use, so prototypes can change between versions.
    )annotationsN)	dataclass)IntEnum)ceil)Path)packunpack)Image   )options)HeifChannel
HeifChromaHeifColorspaceHeifCompressionFormat)DeferredErrorzBGRA;16      zBGRa;16zBGR;16   zRGBA;16zRGBa;16zRGB;16zLA;16   zLa;16zL;16zI;16I;16LzBGRA;12   zBGRa;12zBGR;12zRGBA;12zRGBa;12zRGB;12zLA;12zLa;12zL;12zI;12zI;12LzBGRA;10
   zBGRa;10zBGR;10zRGBA;10zRGBa;10zRGB;10zLA;10zLa;10zL;10zI;10zI;10LRGBA   )	RGBaRGBBGRABGRaBGRLALaLYCbCri  i  i  )z4:4:4z4:2:2z4:2:0)r
   r   r   infodictreturnNonec                 C  s&   t | jd}|dur||d< dS dS )zYConverts `chroma` value from `c_image` to useful values and stores them in ``info`` dict.Nchroma)LIBHEIF_CHROMA_MAPgetr'   )c_imager#   r'    r+   P/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/pillow_heif/misc.pysave_colorspace_chromaY   s   r-   
int | Nonec                 C  s
   t | dS )a  Reset orientation in ``EXIF`` to ``1`` if any orientation present.

    Removes ``XMP`` orientation tag if it is present.
    In Pillow plugin mode, it is called automatically for images.
    When ``pillow_heif`` used in ``standalone`` mode, if you wish, you can call it manually.

    .. note:: If there is no orientation tag, this function will not add it and do nothing.

        If both XMP and EXIF orientation tags are present, EXIF orientation tag will be returned,
        but both tags will be removed.

    :param info: `info` dictionary from :external:py:class:`~PIL.Image.Image` or :py:class:`~pillow_heif.HeifImage`.
    :returns: Original orientation or None if it is absent.
    T_get_orientation)r#   r+   r+   r,   set_orientationa   s   
r1   intc                 C  s   t | d}|d u rdS |S )NFr
   r/   )r#   image_orientationr+   r+   r,   _get_orientation_for_encoders   s   
r4   Fexif_orientationresetboolc              	   C  s   d}|  dri| d dd}|d rid }dD ]}z
|d |}W  n	 ty,   Y qw |ritd|}|rMt|d }|rMtdd	|}td
d	|}|dksU|dkrid|	dt
|dkrcdndg| d< |d u rs|dkrs|S d S )Nr
   xmp    r   )utf-8latin1ztiff:Orientation(="|>)([0-9])r   ztiff:Orientation="([0-9])" z,<tiff:Orientation>([0-9])</tiff:Orientation>r:       )r)   rsplitdecode	Exceptionresearchr2   subjoinencodelen)r#   r5   r6   xmp_orientationxmp_datadecoded_xmp_dataencodingmatchr+   r+   r,   _get_orientation_xmpx   s,   
(rL   c                 C  s  d }|  drz| d }d}|drd}|dd  }|dd dkr%d	nd
}t|d |dd d }t|d |||d  d }|d }t|D ]d}	|d|	  }t|d |||d  d dkreqK||d |d  }
t|d |
dd d }|dkr|}|s n*|d }|r|d7 }t|d d}| d d | | | d |d d   | d<  nqKW n	 ty   Y nw t| ||d}|p|S )NexifF   Exif  T   r   r   s   II<>r!   r   r   Hr   i  r
   )r6   )r)   
startswithr   ranger   r@   rL   )r#   r6   original_orientationtif_tagskipped_exif00endian_markpointer	tag_countoffsettag_nvaluet_original_orientationp_valuenew_orientationrG   r+   r+   r,   r0      sF   

",
r0   strc                 C  sb   t | ddd }|r/|dkrdS |dkrdS |dv rd	S |d
v r#dS |dkr)dS |dkr/dS dS )a  Gets the MIME type of the HEIF(or AVIF) object.

    :param fp: A filename (string), pathlib.Path object, file object or bytes.
        The file object must implement ``file.read``, ``file.seek`` and ``file.tell`` methods,
        and be opened in binary mode.
    :returns: "image/heic", "image/heif", "image/heic-sequence", "image/heif-sequence",
        "image/avif", "image/avif-sequence" or "".
    r   r   Ns   avifz
image/avifs   aviszimage/avif-sequence)s   heics   heixs   heims   heisz
image/heic)s   hevcs   hevxs   hevms   hevszimage/heic-sequences   mif1z
image/heifs   msf1zimage/heif-sequencer<   )
_get_bytes)fp
heif_brandr+   r+   r,   get_file_mimetype   s   	re   bytesc                 C  s   t | ttfr$t| d}||pdW  d    S 1 sw   Y  t| drKt| dr2|  nd }| |p9d}|d urIt| drI| | |S t	| d | S )Nrbreadtellseek)

isinstancera   r   builtinsopenri   hasattrrj   rk   rf   )rc   lengthfiler[   resultr+   r+   r,   rb      s    

rb   metadata
list[dict]bytes | Nonec                 C  s   d }g }t | D ]P\}}|d dkrX|| tj|d d d ddd}|d7 }t|d | dkr6d}n|dkrJ|d |d | d	krJ|d8 }|d |d  }|sX|rX|}qt|D ]}| |= q]|S )
NtypeExifdatar   bigF)	byteordersignedrO   rN   )	enumerateappendr2   
from_bytesrF   reversed)rs   rr   purgeimd_block	skip_sizerx   r+   r+   r,   _retrieve_exif   s$   
 r   c                 C  sT   d }g }t | D ]\}}|d dkr|| |s|d }qt|D ]}| |= q"|S )Nrv   mimerx   )r|   r}   r   )rs   rr   r   r   r   r+   r+   r,   _retrieve_xmp   s   
r   imgImage.Imagec                 C  s6   d| j v r
| j d S t| dr|  }|r| S d S )NrM   getexif)r#   ro   r   tobytes)r   rM   r+   r+   r,   _exif_from_pillow   s   


r   c                 C  s   d }d| j v r| j d }n<d| j v r| j d }n1t| dr(d| jv r'| jd }n!t| drI| jD ]\}}|dkrH|dd\}}|d	krH|} nq0t|trS|d
}|S )Nr8   zXML:com.adobe.xmptag_v2i  applistAPP1r9   r
   s   http://ns.adobe.com/xap/1.0/r:   )r#   ro   r   r   r>   rl   ra   rE   )r   im_xmpsegmentcontentmarkerxmp_tagsr+   r+   r,   _xmp_from_pillow  s(   







r   c                 C  s   | j dkr| jdd d urdnd}| j|d} | S | j dkr'| jdd} | S | j dkr4| jd	d} | S | j d
krA| jdd} | S | j dkrL| jdd} | S )NPtransparencyr   r   )modeIr   1r!   CMYKr"   )r   r#   r)   convert)r   r   r+   r+   r,   _pil_to_supported_mode  s    
	



r   c                   @  s,   e Zd ZdZdZdZdZdZdZdZ	dZ
d	S )
	TransposezkTemporary workaround till we support old Pillows, remove this when a minimum Pillow version will have this.r   r
   r   r   r      rO   N)__name__
__module____qualname____doc__FLIP_LEFT_RIGHTFLIP_TOP_BOTTOM	ROTATE_90
ROTATE_180
ROTATE_270	TRANSPOSE
TRANSVERSEr+   r+   r+   r,   r   +  s    r   orientationc                 C  s>   t jt jt jt jt jt jt jd|}|d ur| 	|S | S )N)r   r   r   r   rO      r   )
r   r   r   r   r   r   r   r   r)   	transpose)r   r   methodr+   r+   r,   _rotate_pil7  s   	
r   primary_indexc                 C  s\   dd | D }|d u rd}t |D ]\}}|r|}q|S |dks&|t|kr,t|d }|S )Nc                 S  s   g | ]	}|j d dqS )primaryF)r#   r)   ).0_r+   r+   r,   
<listcomp>I  s    z&_get_primary_index.<locals>.<listcomp>r   rh   r
   )r|   rF   )some_iteratorr   primary_attrsr   vr+   r+   r,   _get_primary_indexH  s   r   valuestuple | Nonec                 C  s,   | r| d | d | d | d | d dS d S )Nr   r
   r   r   r   )focal_length_xfocal_length_yprincipal_point_xprincipal_point_yskewr+   )r   r+   r+   r,   __get_camera_intrinsic_matrixT  s   		r   c                 C  s0   i }t | j}|r||d< | j}|r||d< |S )Ncamera_intrinsic_matrixcamera_extrinsic_matrix_rot)r   r   r   )r*   rr   r   r+   r+   r,   _get_heif_metab  s   
r   c                   @  sB   e Zd ZdZdddZdddZdddZdddZdddZdS )	CtxEncodez0Encoder bindings from python to python C module.compression_formatr   c                 K  s   | dtj}t||d u rdn|tj |tjkrdndd| _| di }d }d|v r5t	 |d d }|d u r>| d}|rD||d< |
 D ]\}}| j|t|trW|nt| qHd S )	NqualityHEIFAVIFr<   
enc_paramssubsamplingr'   )r)   r   QUALITY_pillow_heifCtxWritePREFERRED_ENCODERr   HEVC	ctx_writeSUBSAMPLING_CHROMA_MAPitemsset_parameterrl   ra   )selfr   kwargsr   r   r'   keyr]   r+   r+   r,   __init__p  s"   
"zCtxEncode.__init__sizetuple[int, int]r   ra   r%   r&   c           	      K  s2  |d dks|d dkrt dt| d }|dkrdn|dd}|dkr-tjr+dnd}t|jd	d
d d dk}| j|t| d t| d |}t| d dkrd|	|||||ddt
j n)t| d dkrz||||||dd n||||||ddk|dd | j||fi | dS )zAdds image to the encoder.r   r
   zEmpty images are not supported.r   	bit_depthr   r   r   ;)seprh   ar   r   strider   N)
ValueError	MODE_INFOr)   r   SAVE_HDR_TO_12_BITr2   splitr   create_imageadd_plane_lr   	CHANNEL_Yadd_plane_la	add_planefind_finish_add_image)	r   r   r   rx   r   bit_depth_inbit_depth_outpremultiplied_alphaim_outr+   r+   r,   	add_image  s   " &zCtxEncode.add_imager   r   c                 K  s   | j |jt|j d t|j d d}tjtjtjfD ]}|	|jddt
|||dd| q| j||jfi | dS )z*Adds image in `YCbCR` mode to the encoder.r   r   r   r   r   N)r   r   r   r   r   r   r   
CHANNEL_CB
CHANNEL_CRr   rf   getdatar)   r   )r   r   r   r   r   r+   r+   r,   add_image_ycbcr  s   (*zCtxEncode.add_image_ycbcrc           
        sf    d}|d ur|  dd|   dr$|j fdddD     dd	}|| j  d
d  dtj  dd  dd  dd  dd|   d}|d urjt|tj	rc|
 }|| j|   d}|d urz|| j|   dg D ]}|| j|d |d |d  q  dg D ]}	t||	  krdkrn q|| j|	| qd S )Nicc_profileicc_profile_typeprofnclx_profilec                   s   g | ]} d  | qS )r   r+   )r   r   r   r+   r,   r     s    
z/CtxEncode._finish_add_image.<locals>.<listcomp>)color_primariestransfer_characteristicsmatrix_coefficientsfull_range_flagr3   r
   r   Fsave_nclx_profiler   rh   r   r   r   rM   r8   rs   rv   content_typerx   
thumbnailsr   )r)   set_icc_profileset_nclx_profilerE   r   r   SAVE_NCLX_PROFILErl   r	   rw   r   set_exifset_xmpset_metadatamaxencode_thumbnail)
r   r   r   r   r   r3   rM   r8   rs   	thumb_boxr+   r   r,   r     sF   









 zCtxEncode._finish_add_imagec                 C  sJ   | j  }t|ttfrt|| dS t|dr!|| dS td)z?Ask encoder to produce output based on previously added images.writez=`fp` must be a path to file or an object with `write` method.N)	r   finalizerl   ra   r   write_bytesro   r	  	TypeError)r   rc   rx   r+   r+   r,   save  s   

zCtxEncode.saveN)r   r   )r   r   r   ra   r%   r&   )r   r   r%   r&   )r   r   )r%   r&   )	r   r   r   r   r   r   r   r   r  r+   r+   r+   r,   r   m  s    



	)r   c                   @  s4   e Zd ZdZddd	Zed
d ZedddZdS )	MimCImagezMimicry of the HeifImage class.r   ra   r   r   rx   rf   c                 K  s   || _ || _|d|d t| d  tt| d d  | _|| _g | _d | _g | _	g | _
g | _d| _tjj| _tjj| _d | _d | _d S )Nr   r   r
   r   F)r   r   r)   r   r   r   rx   rs   color_profiler   depth_image_listaux_image_idsr   r   	UNDEFINEDr]   r'   r   
colorspacer   r   )r   r   r   rx   r   r+   r+   r,   r     s   2


zMimCImage.__init__c                 C  s   | j | jfS )zMimicry of c_image property.)r   r   r   r+   r+   r,   	size_mode  s   zMimCImage.size_moder%   r2   c                 C  s   t | j d S )z%Return bit-depth based on image mode.r
   )r   r   r  r+   r+   r,   r     s   zMimCImage.bit_depthN)r   ra   r   r   rx   rf   )r%   r2   )r   r   r   r   r   propertyr  r   r+   r+   r+   r,   r    s    

r  plugin_path
str | Pathc                 C  s   t |  dS )zLoad specified LibHeif plugin.N)r   load_plugin)r  r+   r+   r,   load_libheif_plugin  s   r  )r#   r$   r%   r&   )r#   r$   r%   r.   )r#   r$   r%   r2   )F)r#   r$   r5   r.   r6   r7   r%   r.   )r#   r$   r6   r7   r%   r.   )r%   ra   )N)r%   rf   )rs   rt   r%   ru   )r   r   r%   ru   )r   r   r%   r   )r   r   r   r2   r%   r   )r   r.   r%   r2   )r   r   )r%   r$   )r  r  r%   r&   )=r   
__future__r   rm   rA   dataclassesr   enumr   mathr   pathlibr   structr   r   PILr	   r<   r   	constantsr   r   r   r   r   ImportErrorex_deffered_errorr   r   INTERLEAVED_RRGGBBAA_LEINTERLEAVED_RRGGBB_LE
MONOCHROMEINTERLEAVED_RGBAINTERLEAVED_RGBYCBCR
CHROMA_444r   r   r(   r-   r1   r4   rL   r0   re   rb   r   r   r   r   r   r   r   r   r   r   r   r  r  r+   r+   r+   r,   <module>   s    	
 !"#$0



#









f