
    0-Ph,                         d Z ddlZddlZddlmZmZ ddlmZm	Z	 ddl
mZ  ej        e          Z G d d	e          Z G d
 de          Z G d de          Z G d de          ZdS )zEPlugin for multi-image freeimafe formats, like animated GIF and ico.
    N   )Formatimage_as_uint   )fiIO_FLAGS)FreeimageFormatc                   f    e Zd ZdZdZdZ G d dej                  Z G d dej	                  Z	dS )	FreeimageMultiz>Base class for freeimage formats that support multiple images.iIc                   .    e Zd ZddZd Zd Zd Zd ZdS )	FreeimageMulti.Readerr   c                     t          |          }t          j        | j        j        | j        j        |          | _        | j                            | j        	                                           d S N)
intr   create_multipage_bitmaprequestfilenameformatfif_bmload_from_filenameget_local_filenameselfflagss     ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/plugins/freeimagemulti.py_openzFreeimageMulti.Reader._open   s]    JJE1%t{ DH H''(G(G(I(IJJJJJ    c                 8    | j                                          d S r   r   closer   s    r   _closezFreeimageMulti.Reader._close    s    HNNr    c                 *    t          | j                  S r   )lenr   r$   s    r   _get_lengthz!FreeimageMulti.Reader._get_length#   s    tx== r    c                     | j                             |          }	 |                                |                                f|                                 S # |                                 w xY wr   )r   get_pageget_image_dataget_meta_datar#   r   indexsubs      r   	_get_datazFreeimageMulti.Reader._get_data&   s]    (##E**C))++S->->-@-@@				s   'A A.c                    |pd}|dk     s|t          | j                  k    rt                      | j                            |          }	 |                                |                                 S # |                                 w xY w)Nr   )r'   r   
IndexErrorr*   r,   r#   r-   s      r   _get_meta_dataz$FreeimageMulti.Reader._get_meta_data-   sz    JQEqyyES]]22 ll"(##E**C((**				s   A4 4B
Nr   )__name__
__module____qualname__r   r%   r(   r0   r3    r    r   Readerr      sh        	K 	K 	K 	K	 	 		! 	! 	!	 	 		 	 	 	 	r    r9   c                   .    e Zd ZddZd Zd Zd Zd ZdS )	FreeimageMulti.Writerr   c                     t          |          x| _        }t          j        | j        j        | j        j        |          | _        | j        	                    | j        
                                           d S r   )r   _flagsr   r   r   r   r   r   r   save_to_filenamer   r   s     r   r   zFreeimageMulti.Writer._open:   sd    "%e**,DK%1%t{ DH H%%dl&E&E&G&GHHHHHr    c                 8    | j                                          d S r   r"   r$   s    r   r%   zFreeimageMulti.Writer._closeC   s    HNNr    c                    |j         dk    r |j        d         dk    r|d d d d df         }t          |d          }t          j        | j        j        | j        j                  }| 	                    |||          }| j        
                    |           |                                 ||ur|                                 d S d S )N   r   r   r      )bitdepth)ndimshaper   r   create_bitmapr   	_filenamer   r   _append_bitmapappend_bitmapr#   )r   immetasub1sub2s        r   _append_dataz"FreeimageMulti.Writer._append_dataG   s    w!|| 1 1111a[rA...B#DH$6HHD&&r466DH""4(((JJLLL4

  r    c                     |                     |           |                    |           |                    |           |S r   )allocateset_image_dataset_meta_data)r   rJ   rK   bitmaps       r   rH   z$FreeimageMulti.Writer._append_bitmapV   sA    OOB!!"%%%  &&&Mr    c                     d S r   r8   )r   rK   s     r   _set_meta_dataz$FreeimageMulti.Writer._set_meta_data^   s    Dr    Nr4   )r5   r6   r7   r   r%   rN   rH   rU   r8   r    r   Writerr;   9   sh        	I 	I 	I 	I	 	 		 	 		 	 		 	 	 	 	r    rV   N)
r5   r6   r7   __doc___modes_fifr   r9   r	   rV   r8   r    r   r   r      s~        HHFD       D& & & & &' & & & & &r    r   c                       e Zd ZdZdZd ZdS )	MngFormatzPAn Mng format based on the Freeimage library.

    Read only. Seems broken.
       c                     dS )NFr8   )r   r   s     r   
_can_writezMngFormat._can_writej   s    ur    N)r5   r6   r7   rW   rY   r^   r8   r    r   r[   r[   b   s4         
 D    r    r[   c                   <    e Zd ZdZdZ G d dej                  ZdS )	IcoFormata  An ICO format based on the Freeimage library.

    This format supports grayscale, RGB and RGBA images.

    The freeimage plugin requires a `freeimage` binary. If this binary
    is not available on the system, it can be downloaded by either

    - the command line script ``imageio_download_bin freeimage``
    - the Python method ``imageio.plugins.freeimage.download()``

    Parameters for reading
    ----------------------
    makealpha : bool
        Convert to 32-bit and create an alpha channel from the AND-
        mask when loading. Default False. Note that this returns wrong
        results if the image was already RGBA.

    r   c                       e Zd ZddZdS )IcoFormat.Readerr   Fc                     t          |          }|r|t          j        z  }t          j                            | |          S r   )r   r   ICO_MAKEALPHAr   r9   r   )r   r   	makealphas      r   r   zIcoFormat.Reader._open   s;    JJE 0//!(..tU;;;r    N)r   F)r5   r6   r7   r   r8   r    r   r9   rb      s(        	< 	< 	< 	< 	< 	<r    r9   N)r5   r6   r7   rW   rY   r   r9   r8   r    r   r`   r`   n   sR         & D< < < < <& < < < < <r    r`   c                   b    e Zd ZdZdZ G d dej                  Z G d dej                  ZdS )	GifFormata  A format for reading and writing static and animated GIF, based
    on the Freeimage library.

    Images read with this format are always RGBA. Currently,
    the alpha channel is ignored when saving RGB images with this
    format.

    The freeimage plugin requires a `freeimage` binary. If this binary
    is not available on the system, it can be downloaded by either

    - the command line script ``imageio_download_bin freeimage``
    - the Python method ``imageio.plugins.freeimage.download()``

    Parameters for reading
    ----------------------
    playback : bool
        'Play' the GIF to generate each frame (as 32bpp) instead of
        returning raw frame data when loading. Default True.

    Parameters for saving
    ---------------------
    loop : int
        The number of iterations. Default 0 (meaning loop indefinitely)
    duration : {float, list}
        The duration (in seconds) of each frame. Either specify one value
        that is used for all frames, or one value for each frame.
        Note that in the GIF format the duration/delay is expressed in
        hundredths of a second, which limits the precision of the duration.
    fps : float
        The number of frames per second. If duration is not given, the
        duration for each frame is set to 1/fps. Default 10.
    palettesize : int
        The number of colors to quantize the image to. Is rounded to
        the nearest power of two. Default 256.
    quantizer : {'wu', 'nq'}
        The quantization algorithm:
            * wu - Wu, Xiaolin, Efficient Statistical Computations for
              Optimal Color Quantization
            * nq (neuqant) - Dekker A. H., Kohonen neural networks for
              optimal color quantization
    subrectangles : bool
        If True, will try and optimize the GIF by storing only the
        rectangular parts of each frame that change with respect to the
        previous. Unfortunately, this option seems currently broken
        because FreeImage does not handle DisposalMethod correctly.
        Default False.
       c                       e Zd ZddZd ZdS )GifFormat.Readerr   Tc                     t          |          }|r|t          j        z  }t          j                            | |           d S r   )r   r   GIF_PLAYBACKr   r9   r   )r   r   playbacks      r   r   zGifFormat.Reader._open   sA    JJE /..!''e44444r    c                 P    t           j                            | |          \  }}||fS r   )r   r9   r0   )r   r.   rJ   rK   s       r   r0   zGifFormat.Reader._get_data   s'    %,66tUCCHBt8Or    N)r   T)r5   r6   r7   r   r0   r8   r    r   r9   rj      s7        	5 	5 	5 	5	 	 	 	 	r    r9   c                   0    e Zd Z	 	 	 	 	 	 	 d
dZd Zd	 ZdS )GifFormat.Writerr   N
      WuFc                    |dk     s|dk    rt          d          |dvrDdt          t          j        d          dz             z  }t                              d|z             || _        dd	d
                    |                                d           | _	        | j	        t          d          |)t          dt          |          z  dz             g| _        nnt          |t                    rd |D             | _        nGt          |t          t          f          rt          d|z            g| _        nt          d|z            t          |          | _        d | _        t"          j                            | |           i | _        dt          j        |g                              t          j                  i| j        d<   d S )Nr   rr   z!GIF quantize param must be 2..256)r      rB          @      rr   ry   g+?zDWarning: palettesize (%r) modified to a factor of two between 2-256.r   r   )wunqz(Invalid quantizer, must be "wu" or "nq".  g      ?c                 2    g | ]}t          d |z            S )r|   )r   ).0ds     r   
<listcomp>z*GifFormat.Writer._open.<locals>.<listcomp>   s"    "C"C"CQ3tax=="C"C"Cr    zInvalid value for duration: %rLoop	ANIMATION)
ValueErrorr   nplog2loggerwarning_palettesizegetlower
_quantizerfloat
_frametime
isinstancelistbool_subrectangles_prev_imr   rV   r   _metaarrayastypeuint32)r   r   loopdurationfpspalettesize	quantizersubrectangless           r   r   zGifFormat.Writer._open   s    Q+"3"3 !DEEE"AAA3rws||e';#<#<<)+67   !,D%&a0044Y__5F5FMMDO& !KLLL#&teCjj'83'>#?#?"@Hd++ N"C"C("C"C"CHucl33 N#&th#7#7"8 !AH!LMMM"&}"5"5D DM!''e444DJ $((//	::'DJ{###r    c                    |                                 }i x}|d<   t          | j                  dk    r"|                    | j                   |d         }t          | j                  }|t          | j                  k     r| j        |         }n| j        d         }t          j        |g                              t          j	                  |d<   |j
        dk    r"|j        d         dk    r|d d d d d df         }|}| j        r| j        |                     | j        |          \  }}t          j        dg                              t          j                  |d<   t          j        |d         g                              t          j                  |d	<   t          j        |d         g                              t          j                  |d
<   || _        |x}	}
|
                    |           |
                    |           |j
        dk    r1|j        d         dk    r |
                    | j        | j                  }	|	                    |           |	S )Nr   r   r   	FrameTimerA   ru   r   DisposalMethod	FrameLeftFrameTop)copyr'   r   updater   r   r   r   r   r   rD   rE   r   r   _get_sub_rectanglesuint8uint16rP   rQ   quantizer   r   rR   )r   rJ   rK   rS   meta_ar.   ftim_uncroppedxyrM   rL   s              r   rH   zGifFormat.Writer._append_bitmap  s   99;;D)++FT+&48}}!!DJ'''k*MMEs4?++++_U+_R("$(B4.."7"7	"B"BF;w!|| 1 1111bqb\L" It}'@11$-DDB+-8QC==+?+?+I+I'(&(h1w&7&7&>&>ry&I&I{#%'Xr!ug%6%6%=%=bi%H%Hz"(DM  D4MM"###w!|| 1 1}}T_d6GHHt$$$Kr    c                    t          j        ||z
            }|j        dk    r|                    d          }t          j        |                    d                    }t          j        |                    d                    }|j        rb|j        r[t          |d                   t          |d                   dz   }}t          |d                   t          |d                   dz   }	}n
d\  }}d\  }}	|||	||f         ||ffS )z
            Calculate the minimal rectangles that need updating each frame.
            Returns a two-element tuple containing the cropped images and a
            list of x-y positions.
            rA   r   r   r   r   )r   r   )r   absrD   sumargwheresizer   )
r   prevrJ   diffXYx0x1y0y1s
             r   r   z$GifFormat.Writer._get_sub_rectangles'  s     6"t)$$DyA~~xx{{DHHQKK((ADHHQKK((Av !& QqTC"JJNBQqTC"JJNBBBbeRUl#b"X--r    )r   r   Nrq   rr   rs   F)r5   r6   r7   r   rH   r   r8   r    r   rV   rp      s_         -	 -	 -	 -	^#	 #	 #	J	. 	. 	. 	. 	.r    rV   N)r5   r6   r7   rW   rY   r   r9   rV   r8   r    r   rg   rg      s        . .` D    &   m. m. m. m. m.& m. m. m. m. m.r    rg   )rW   loggingnumpyr   corer   r   
_freeimager   r   	freeimager	   	getLoggerr5   r   r   r[   r`   rg   r8   r    r   <module>r      s?         ( ( ( ( ( ( ( ( $ $ $ $ $ $ $ $ & & & & & &		8	$	$N N N N N_ N N Nb	 	 	 	 	 	 	 	< < < < < < < <>o. o. o. o. o. o. o. o. o. o.r    