o
    ʷ6i                     @   s:   d dl mZ d dlmZ d dlmZmZ G dd dZdS )    )settings)toint)
parse_cropparse_cropboxc                   @   s  e Zd ZdZdd Zdd Zdd Zd@d	d
Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 ZdAd6d7Zd8d9 Zd:d; Zd<d= Z d>d? Z!dS )B
EngineBasez7
    ABC for Thumbnail engines, methods are static
    c                 C   s   |  |||}| |||}| |||}| ||}| |||}| |||}| |||}| |||}| |||}|S )zY
        Processing conductor, returns the thumbnail as an image engine instance
        )	cropboxorientation
colorspaceremove_borderscalecroproundedblurpaddingselfimagegeometryoptions r   `/var/www/hoanhtaovolam_webdjango/env/lib/python3.10/site-packages/sorl/thumbnail/engines/base.pycreate   s   zEngineBase.createc           	      C   s2   |d }|s|S t |\}}}}| |||||S )z*
        Wrapper for ``_cropbox``
        r   )r   _cropbox)	r   r   r   r   r   xyx2y2r   r   r   r      s
   zEngineBase.cropboxc                 C   s"   | dtjr| |S d| _|S )z.
        Wrapper for ``_orientation``
        r   T)getr   THUMBNAIL_ORIENTATION_orientation
reorientedr   r   r   r   r   $   s   
zEngineBase.orientationNc                 C   s2   |pi }t | d}|dtjr|s| |S dS )Nr    r   F)hasattrr   r   r   _flip_dimensions)r   r   r   r   r    r   r   r   flip_dimensions-   s
   

zEngineBase.flip_dimensionsc                 C   s   |d }|  ||S )z-
        Wrapper for ``_colorspace``
        r	   )_colorspace)r   r   r   r   r	   r   r   r   r	   4   s   zEngineBase.colorspacec                 C   s,   | ddr| |\}}| |||}|S )Nr
   F)r   get_image_size_remove_border)r   r   r   x_imagey_imager   r   r   r
   ;   s   zEngineBase.remove_borderc                 C   s4   |d }|d | |d | f}|rt |S t|S )Nr   r      )maxmin)r   r'   r(   r   r   r   factorsr   r   r   _calculate_scaling_factorC   s   z$EngineBase._calculate_scaling_factorc           
      C   sv   |d }t t| |\}}| |r||}}| ||||}|dk s&|r9t|| }t|| }	| |||	}|S )z(
        Wrapper for ``_scale``
        upscaler)   )mapfloatr%   r#   r-   r   _scale)
r   r   r   r   r.   r'   r(   factorwidthheightr   r   r   r   H   s   

zEngineBase.scalec           	      C   s   |d }|  |\}}|r|dkr|S |dkr$| ||d |d ||S t||d t||d f}t|||f|\}}| ||d |d ||S )z'
        Wrapper for ``_crop``
        r   noopsmartr   r)   )r%   _entropy_cropr+   r   _crop)	r   r   r   r   r   r'   r(   x_offsety_offsetr   r   r   r   Y   s   zEngineBase.cropc                 C   s    |d }|s|S |  |t|S )z*
        Wrapper for ``_rounded``
        r   )_roundedint)r   r   r   r   rr   r   r   r   l   s   zEngineBase.roundedc                 C   s$   | dr| |t| dS |S )z'
        Wrapper for ``_blur``
        r   )r   _blurr<   r   r   r   r   r   u   s   
zEngineBase.blurc                 C   s*   | dr| ||kr| |||S |S )z*
        Wrapper for ``_padding``
        r   )r   r%   _paddingr   r   r   r   r   }   s   zEngineBase.paddingc           	      C   sL   |d }|d }| di }| dtj}| j|||||d}|| dS )z(
        Wrapper for ``_write``
        formatquality
image_infoprogressive)rB   rC   N)r   r   THUMBNAIL_PROGRESSIVE_get_raw_datawrite)	r   r   r   	thumbnailformat_rA   rB   rC   raw_datar   r   r   rF      s   zEngineBase.writec                 C   s   dS )zCSome backends need to manually cleanup after thumbnails are createdNr   r   r   r   r   r   cleanup   s   zEngineBase.cleanupc           	      C   s^   |d }|rt |\}}}}|| }|| }n| |\}}t|| }| |r-d| }|S )zl
        Calculates the image ratio. If cropbox option is used, the ratio
        may have changed.
        r   g      ?)r   r%   r0   r#   )	r   r   r   r   r   r   r   r   ratior   r   r   get_image_ratio   s   

zEngineBase.get_image_ratioc                 C   s   i S )z;
        Returns metadata of an ImageFile instance
        r   rJ   r   r   r   get_image_info      zEngineBase.get_image_infoc                 C      t  )zN
        Returns the backend image objects from an ImageFile instance
        NotImplementedError)r   sourcer   r   r   	get_image      zEngineBase.get_imagec                 C   rP   )z?
        Returns the image width and height as a tuple
        rQ   rJ   r   r   r   r%      rU   zEngineBase.get_image_sizec                 C   rP   )zE
        Checks if the supplied raw data is valid image data
        rQ   )r   rI   r   r   r   is_valid_image   rU   zEngineBase.is_valid_imagec                 C   s   |S )zP
        Read orientation exif data and orientate the image accordingly
        r   rJ   r   r   r   r      rO   zEngineBase._orientationc                 C   rP   )z
        `Valid colorspaces
        <http://www.graphicsmagick.org/GraphicsMagick.html#details-colorspace>`_.
        Backends need to implement the following::

            RGB, GRAY
        rQ   )r   r   r	   r   r   r   r$      s   zEngineBase._colorspacec                 C   rP   )z.
        Remove borders around images
        rQ   )r   r   image_widthimage_heightr   r   r   r&      rU   zEngineBase._remove_borderc                 C   rP   )zi
        Crop the image to the correct aspect ratio
        by removing the lowest entropy parts
        rQ   )r   r   geometry_widthgeometry_heightrW   rX   r   r   r   r7         zEngineBase._entropy_cropc                 C   rP   )z0
        Does the resizing of the image
        rQ   )r   r   r3   r4   r   r   r   r1      rU   zEngineBase._scalec                 C   rP   )z!
        Crops the image
        rQ   )r   r   r3   r4   r9   r:   r   r   r   r8      rU   zEngineBase._cropFc                 C   rP   )zu
        Gets raw data given the image, format and quality. This method is
        called from :meth:`write`
        rQ   )r   r   rH   rA   rB   rC   r   r   r   rE      r[   zEngineBase._get_raw_datac                 C   rP   )z 
        Pads the image
        rQ   r   r   r   r   r?      rU   zEngineBase._paddingc                 C   rP   NrQ   )r   r   r   r   r   r   r   r   r   r         zEngineBase._cropboxc                 C   rP   r\   rQ   )r   r   r=   r   r   r   r;     r]   zEngineBase._roundedc                 C   rP   r\   rQ   )r   r   radiusr   r   r   r>     r]   zEngineBase._blur)NN)NF)"__name__
__module____qualname____doc__r   r   r   r#   r	   r
   r-   r   r   r   r   r   rF   rK   rM   rN   rT   r%   rV   r   r$   r&   r7   r1   r8   rE   r?   r   r;   r>   r   r   r   r   r      s@    

		

r   N)sorl.thumbnail.confr   sorl.thumbnail.helpersr   sorl.thumbnail.parsersr   r   r   r   r   r   r   <module>   s    