a
    DgW4                     @   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                   @   s   e Zd Zdd Zdd ZdS )	Operationc              
   G   s   || _ || _ztj| jg|R   W n, tyP } zt|W Y d }~n
d }~0 0 z| j|  W n, ty } zt|W Y d }~n
d }~0 0 d S N)methodargsinspectgetcallargs	construct	TypeErrorr   
ValueError)selfr   r	   e r   ^/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/images/image_operations.py__init__	   s    zOperation.__init__c                 G   s   t d S r   NotImplementedError)r   r	   r   r   r   r      s    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   Zvectorr   r   r   transform_vectorR   s    	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,   r-   r   r   r   untransform_vector`   s    	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|sRt| d | d ksJt| d | d krRtd| d dk sj| d dk rrt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)F)r   r   r   __doc__r   r#   r%   r*   r.   r/   r1   staticmethodr   r   r   r   r   r       s   
r   c                   @   s   e Zd Zdd ZdS )TransformOperationc                 C   s   t d S r   r   )r   image	transformr   r   r   run   s    zTransformOperation.runNr   r   r   r;   r   r   r   r   r8      s   r8   c                   @   s    e Zd ZdZdd Zdd ZdS )FillOperation)Zfocal_point_widthZfocal_point_heightZfocal_point_xZfocal_point_yc                 G   s~   | d\}}t|| _t|| _d| _|D ].}|drNt|dd  | _q,td| q,|  jd  _| jdkrzd| _d S )Nr+   r   cr$   z!Unrecognised filter spec part: %sd   )splitr5   widthheightcrop_closeness
startswithr   )r   r    extra	width_str
height_strZ
extra_partr   r   r   r      s    



zFillOperation.constructc                 C   s  |j \}}| }| j| j }t||| }|}|| }	|}
|	}|d urt|j|j| }|}|| }||kstd| j| ||   d| j| |	|   }t| j|}d|  krdkrn n ||| |  }
|	||	 |  }|d ur|j\}}n|d }|d }|| }|| }||d |
  }||d |  }t	|||
|}|d urX|
|}|tdd||}|| }|j \}}| j| }|dk r|| j| jf}|S )Nr$   r   r2   g      ?r   )r    Zget_focal_pointrA   rB   minmaxrC   Zcentroidr   Z
from_pointZmove_to_coverZmove_to_clampr*   roundr%   )r   r:   r9   image_widthimage_heightZfocal_pointZcrop_aspect_ratioZcrop_max_scaleZcrop_max_widthZcrop_max_heightZ
crop_widthZcrop_heightZcrop_min_scaleZcrop_min_widthZcrop_min_heightZmax_crop_closenessrC   Zfp_xZfp_yZfp_uZfp_vZcrop_xZcrop_yr)   Zaftercrop_widthZaftercrop_heightr!   r   r   r   r;      sj    
	






zFillOperation.runN)r   r   r   Zvary_fieldsr   r;   r   r   r   r   r=      s   r=   c                   @   s   e Zd Zdd Zdd ZdS )MinMaxOperationc                 C   s&   | d\}}t|| _t|| _d S )Nr+   )r@   r5   rA   rB   )r   r    rF   rG   r   r   r   r     s    
zMinMaxOperation.constructc           	      C   s   |j \}}| j| }| j| }| jdkrp|| jks<|| jkr@|S ||kr\| j}t|| }qt|| }| j}nV| jdkr|| jkr|| jkr|S ||k r| j}t|| }qt|| }| j}n|S |dkr|nd}|dkr|nd}|||fS )NrH   rI   r   r$   )r    rA   rB   r   r5   r%   )	r   r:   r9   rK   rL   Z
horz_scaleZ
vert_scalerA   rB   r   r   r   r;     s.    




zMinMaxOperation.runNr   r   r   r   r;   r   r   r   r   rM     s   rM   c                   @   s   e Zd Zdd Zdd ZdS )WidthHeightOperationc                 C   s   t || _d S r   )r5   r    )r   r    r   r   r   r   5  s    zWidthHeightOperation.constructc                 C   s   |j \}}| jdkr@|| j kr"|S | j | }| j }t|| }n:| jdkrv|| j krX|S | j | }t|| }| j }n|S |dkr|nd}|dkr|nd}|||fS )NrA   rB   r   r$   )r    r   r5   r%   r   r:   r9   rK   rL   r!   rA   rB   r   r   r   r;   8  s"    






zWidthHeightOperation.runNrN   r   r   r   r   rO   4  s   rO   c                   @   s   e Zd Zdd Zdd ZdS )ScaleOperationc                 C   s   t || _d S r   )floatpercent)r   rS   r   r   r   r   Y  s    zScaleOperation.constructc                 C   sZ   |j \}}| jd }t|| }t|| }|dkr8|nd}|dkrH|nd}|||fS )Nr?   r   r$   )r    rS   r5   r%   rP   r   r   r   r;   \  s    

zScaleOperation.runNrN   r   r   r   r   rQ   X  s   rQ   c                   @   s   e Zd Zdd ZdS )FilterOperationc                 C   s   t d S r   r   r   Zwillowr9   envr   r   r   r;   n  s    zFilterOperation.runNr<   r   r   r   r   rT   m  s   rT   c                   @   s   e Zd Zdd Zdd ZdS )DoNothingOperationc                 C   s   d S r   r   r0   r   r   r   r   s  s    zDoNothingOperation.constructc                 C   s   |S r   r   rU   r   r   r   r;   v  s    zDoNothingOperation.runNrN   r   r   r   r   rW   r  s   rW   c                   @   s   e Zd Zdd Zdd ZdS )JPEGQualityOperationc                 C   s    t || _| jdkrtdd S )Nr?   z(JPEG quality must not be higher than 100r5   qualityr   r   rZ   r   r   r   r   {  s    

zJPEGQualityOperation.constructc                 C   s   | j |d< d S )Nzjpeg-qualityrZ   rU   r   r   r   r;     s    zJPEGQualityOperation.runNrN   r   r   r   r   rX   z  s   rX   c                   @   s   e Zd Zdd Zdd ZdS )AvifQualityOperationc                 C   s    t || _| jdkrtdd S )Nr?   z(AVIF quality must not be higher than 100rY   r[   r   r   r   r     s    

zAvifQualityOperation.constructc                 C   s   | j |d< d S )Nzavif-qualityr\   rU   r   r   r   r;     s    zAvifQualityOperation.runNrN   r   r   r   r   r]     s   r]   c                   @   s   e Zd Zdd Zdd ZdS )WebPQualityOperationc                 C   s    t || _| jdkrtdd S )Nr?   z(WebP quality must not be higher than 100rY   r[   r   r   r   r     s    

zWebPQualityOperation.constructc                 C   s   | j |d< d S )Nzwebp-qualityr\   rU   r   r   r   r;     s    zWebPQualityOperation.runNrN   r   r   r   r   r^     s   r^   c                   @   s$   e Zd Zg dZdd Zdd ZdS )FormatOperation)ZjpegZpngZgifZwebpZavifZicoZheicc                 G   s:   || _ || _| j | jvr6tdd| j d| j  d S )NzFormat must be one of: z, z. Got: )formatoptionssupported_formatsr   join)r   r`   ra   r   r   r   r     s    zFormatOperation.constructc                 C   s   | j |d< | j|d< d S )Nzoutput-formatzoutput-format-options)r`   ra   rU   r   r   r   r;     s    
zFormatOperation.runN)r   r   r   rb   r   r;   r   r   r   r   r_     s   	r_   c                   @   s   e Zd Zdd Zdd ZdS )BackgroundColorOperationc                 C   s   t || _d S r   )r   color)r   Zcolor_stringr   r   r   r     s    z"BackgroundColorOperation.constructc                 C   s   | | jS r   )Zset_background_color_rgbre   rU   r   r   r   r;     s    zBackgroundColorOperation.runNrN   r   r   r   r   rd     s   rd   )r
   Zwagtail.images.exceptionsr   Zwagtail.images.rectr   r   Zwagtail.images.utilsr   r   r   r8   r=   rM   rO   rQ   rT   rW   rX   r]   r^   r_   rd   r   r   r   r   <module>   s"   gz.$