o
    hW4                     @   s  d dl Z d dlmZ d dlmZmZ d dlmZ G dd dZG dd dZ	G d	d
 d
eZ
G dd de
ZG dd de
ZG dd de
ZG dd de
ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd  d eZdS )!    N)InvalidFilterSpecError)RectVector)parse_color_stringc                   @      e Zd Zdd Zdd ZdS )	Operationc              
   G   sx   || _ || _ztj| jg|R   W n ty" } zt|d }~ww z| j|  W d S  ty; } zt|d }~ww N)methodargsinspectgetcallargs	construct	TypeErrorr   
ValueError)selfr	   r
   e r   _/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/images/image_operations.py__init__	   s   zOperation.__init__c                 G      t r   NotImplementedError)r   r
   r   r   r   r         zOperation.constructN)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   @   sX   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
edddZdS )ImageTransforma$  
    Tracks transformations that are performed on an image.

    This allows multiple transforms to be processed in a single operation and also
    accumulates the operations into a single scale/offset which can be used for
    features such as transforming the focal point of the image.
    Fc                 C   s*   | j ||d || _|| _d| _d| _d S )Nallow_floating_point)      ?r   )        r    )_check_sizeimage_is_svgsizescaleoffset)r   r#   r"   r   r   r   r   )   s
   
zImageTransform.__init__c                 C   s"   t | j| j}| j|_| j|_|S r   )r   r#   r"   r$   r%   )r   cloner   r   r   r&   0   s   zImageTransform.clonec                 C   s\   | j || jd |  }|jd |d  | jd  |jd |d  | jd  f|_||_|S )z^
        Change the image size, stretching the transform to make it fit the new size.
        r   r      )r!   r"   r&   r$   r#   )r   r#   r&   r   r   r   resize6   s   zImageTransform.resizec                 C   sd   | j t|j| jd |  }|jd |j| jd   |jd |j| jd   f|_t|j|_|S )z7
        Crop the image to the specified rect.
        r   r   r'   )	r!   tupler#   r"   r&   r%   leftr$   top)r   rectr&   r   r   r   cropC   s   zImageTransform.cropc                 C   s6   t |j| jd  | jd  |j| jd  | jd  S )a  
        Transforms the given vector into the coordinate space of the final image.

        Use this to find out where a point on the source image would end up in the
        final image after cropping/resizing has been performed.

        Returns a new vector.
        r   r'   )r   xr%   r$   yr   vectorr   r   r   transform_vectorR      	zImageTransform.transform_vectorc                 C   s6   t |j| jd  | jd  |j| jd  | jd  S )a'  
        Transforms the given vector back to the coordinate space of the source image.

        This performs the inverse of `transform_vector`. Use this to find where a point
        in the final cropped/resized image originated from in the source image.

        Returns a new vector.
        r   r'   )r   r.   r$   r%   r/   r0   r   r   r   untransform_vector`   r3   z!ImageTransform.untransform_vectorc                 C   sV   t | jd  | jd  | jd  | jd | jd   | jd  | jd | jd   S )z]
        Returns a Rect representing the region of the original image to be cropped.
        r   r'   )r   r%   r#   r$   r   r   r   r   get_rectn   s   

zImageTransform.get_rectc                 C   sv   t | trt| dkrtd|s)t| d | d ks%t| d | d kr)td| d dk s5| d dk r9tdd S )N   zImage size must be a 2-tupler   r'   z(Image size must be a 2-tuple of integersz0Image width and height must both be 1 or greater)
isinstancer)   lenr   intr   )r#   r   r   r   r   r!   y   s   (zImageTransform._check_sizeN)F)r   r   r   __doc__r   r&   r(   r-   r2   r4   r6   staticmethodr!   r   r   r   r   r       s    
r   c                   @      e Zd Zdd ZdS )TransformOperationc                 C   r   r   r   )r   image	transformr   r   r   run   r   zTransformOperation.runNr   r   r   rA   r   r   r   r   r>          r>   c                   @   s    e Zd ZdZdd Zdd ZdS )FillOperation)focal_point_widthfocal_point_heightfocal_point_xfocal_point_yc                 G   s   | d\}}t|| _t|| _d| _|D ]}|dr't|dd  | _qtd| |  jd  _| jdkr>d| _d S d S )Nr.   r   cr'   z!Unrecognised filter spec part: %sd   )splitr:   widthheightcrop_closeness
startswithr   )r   r#   extra	width_str
height_str
extra_partr   r   r   r      s   




zFillOperation.constructc                 C   s  |j \}}| }| j| j }t||| }|}|| }	|}
|	}|d urpt|j|j| }|}|| }||ksptd| j| ||   d| j| |	|   }t| j|}d|  kr^dkrpn n||| |  }
|	||	 |  }|d urz|j\}}n|d }|d }|| }|| }||d |
  }||d |  }t	|||
|}|d ur|
|}|tdd||}|| }|j \}}| j| }|dk r|| j| jf}|S )Nr'   r   r7   g      ?r   )r#   get_focal_pointrL   rM   minmaxrN   centroidr   
from_pointmove_to_covermove_to_clampr-   roundr(   )r   r@   r?   image_widthimage_heightfocal_pointcrop_aspect_ratiocrop_max_scalecrop_max_widthcrop_max_height
crop_widthcrop_heightcrop_min_scalecrop_min_widthcrop_min_heightmax_crop_closenessrN   fp_xfp_yfp_ufp_vcrop_xcrop_yr,   aftercrop_widthaftercrop_heightr$   r   r   r   rA      sj   
	




zFillOperation.runN)r   r   r   vary_fieldsr   rA   r   r   r   r   rD      s    rD   c                   @   r   )MinMaxOperationc                 C   s&   | d\}}t|| _t|| _d S )Nr.   )rK   r:   rL   rM   )r   r#   rQ   rR   r   r   r   r     s   
zMinMaxOperation.constructc           	      C   s   |j \}}| j| }| j| }| jdkr8|| jks|| jkr |S ||kr.| j}t|| }n5t|| }| j}n+| jdkra|| jkrI|| jkrI|S ||k rW| j}t|| }nt|| }| j}n|S |dkri|nd}|dkrq|nd}|||fS )NrU   rV   r   r'   )r#   rL   rM   r	   r:   r(   )	r   r@   r?   r\   r]   
horz_scale
vert_scalerL   rM   r   r   r   rA     s.   




zMinMaxOperation.runNr   r   r   r   rA   r   r   r   r   rr         rr   c                   @   r   )WidthHeightOperationc                 C      t || _d S r   )r:   r#   )r   r#   r   r   r   r   5     zWidthHeightOperation.constructc                 C   s   |j \}}| jdkr || j kr|S | j | }| j }t|| }n| jdkr;|| j kr,|S | j | }t|| }| j }n|S |dkrC|nd}|dkrK|nd}|||fS )NrL   rM   r   r'   )r#   r	   r:   r(   r   r@   r?   r\   r]   r$   rL   rM   r   r   r   rA   8  s"   






zWidthHeightOperation.runNru   r   r   r   r   rw   4      rw   c                   @   r   )ScaleOperationc                 C   rx   r   )floatpercent)r   r~   r   r   r   r   Y  ry   zScaleOperation.constructc                 C   sZ   |j \}}| jd }t|| }t|| }|dkr|nd}|dkr$|nd}|||fS )NrJ   r   r'   )r#   r~   r:   r(   rz   r   r   r   rA   \  s   

zScaleOperation.runNru   r   r   r   r   r|   X  r{   r|   c                   @   r=   )FilterOperationc                 C   r   r   r   r   willowr?   envr   r   r   rA   n  r   zFilterOperation.runNrB   r   r   r   r   r   m  rC   r   c                   @   r   )DoNothingOperationc                 C   s   d S r   r   r5   r   r   r   r   s  r   zDoNothingOperation.constructc                 C   s   |S r   r   r   r   r   r   rA   v  r   zDoNothingOperation.runNru   r   r   r   r   r   r  r{   r   c                   @   r   )JPEGQualityOperationc                 C       t || _| jdkrtdd S )NrJ   z(JPEG quality must not be higher than 100r:   qualityr   r   r   r   r   r   r   {     

zJPEGQualityOperation.constructc                 C      | j |d< d S )Nzjpeg-qualityr   r   r   r   r   rA     ry   zJPEGQualityOperation.runNru   r   r   r   r   r   z  rv   r   c                   @   r   )AvifQualityOperationc                 C   r   )NrJ   z(AVIF quality must not be higher than 100r   r   r   r   r   r     r   zAvifQualityOperation.constructc                 C   r   )Nzavif-qualityr   r   r   r   r   rA     ry   zAvifQualityOperation.runNru   r   r   r   r   r     rv   r   c                   @   r   )WebPQualityOperationc                 C   r   )NrJ   z(WebP quality must not be higher than 100r   r   r   r   r   r     r   zWebPQualityOperation.constructc                 C   r   )Nzwebp-qualityr   r   r   r   r   rA     ry   zWebPQualityOperation.runNru   r   r   r   r   r     rv   r   c                   @   s$   e Zd Zg dZdd Zdd ZdS )FormatOperation)jpegpnggifwebpavificoheicc                 G   s:   || _ || _| j | jvrtdd| j d| j  d S )NzFormat must be one of: z, z. Got: )formatoptionssupported_formatsr   join)r   r   r   r   r   r   r     s   zFormatOperation.constructc                 C   s   | j |d< | j|d< d S )Nzoutput-formatzoutput-format-options)r   r   r   r   r   r   rA     s   
zFormatOperation.runN)r   r   r   r   r   rA   r   r   r   r   r     s    	r   c                   @   r   )BackgroundColorOperationc                 C   rx   r   )r   color)r   color_stringr   r   r   r     ry   z"BackgroundColorOperation.constructc                 C   s   | | jS r   )set_background_color_rgbr   r   r   r   r   rA     s   zBackgroundColorOperation.runNru   r   r   r   r   r     r{   r   )r   wagtail.images.exceptionsr   wagtail.images.rectr   r   wagtail.images.utilsr   r   r   r>   rD   rr   rw   r|   r   r   r   r   r   r   r   r   r   r   r   <module>   s$    gz.$