
    0-Ph                        d Z ddlZddlZddlZddlZddlZddlZddlmZm	Z	m
Z
mZmZmZmZmZ  ej        e          ZdZddddd	d
dZd"dZd Zd Z ej        dddej                  Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z  G d de          Z! G d de!          Z" G d  d!e!          Z# e             Z$dS )#z Module imageio/freeimage.py

This module contains the wrapper code for the freeimage library.
The functions defined in this module are relatively thin; just thin
enough so that arguments and results are native Python/numpy data
types.

    N   )get_remote_fileload_libDictresource_dirsIS_PYPYget_platformInternetNotAllowedErrorNeedDownloadErrorFz!libfreeimage-3.16.0-osx10.6.dylibzFreeImage-3.18.0-win32.dllzFreeImage-3.18.0-win64.dllzlibfreeimage-3.16.0-linux32.sozlibfreeimage-3.16.0-linux64.so)osx32osx64win32win64linux32linux64c                     t                      }|r9|t          v r2dt          |         z   }t          || |           dt          _        dS dS dS )a  Download the FreeImage library to your computer.

    Parameters
    ----------
    directory : str | None
        The directory where the file will be cached if a download was
        required to obtain the file. By default, the appdata directory
        is used. This is also the first directory that is checked for
        a local version of the file.
    force_download : bool | str
        If True, the file will be downloaded even if a local copy exists
        (and this copy will be overwritten). Can also be a YYYY-MM-DD date
        to ensure a file is up-to-date (modified date of a file on disk,
        if present, is checked).
    
freeimage/)fname	directoryforce_downloadN)r	   FNAME_PER_PLATFORMr   fi_lib)r   r   platr   s       Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/plugins/_freeimage.pydownloadr   /   s`      >>D ***1$77eyXXXX **    c                  t   t          j        dd          } | r| S t                      }|r|t          v r	 t	          dt          |         z   d          S # t
          $ r Y dS t          $ r t          d          t          $ r2}t          	                    t          |                     Y d}~dS d}~ww xY wdS dS )z7Ensure we have our version of the binary freeimage lib.IMAGEIO_FREEIMAGE_LIBNr   F)autozNeed FreeImage library. You can obtain it with either:
  - download using the command: imageio_download_bin freeimage
  - download by calling (in Python): imageio.plugins.freeimage.download()
)osgetenvr	   r   r   r
   r   RuntimeErrorloggerwarningstr)libr   es      r   get_freeimage_libr)   F   s    )+T
2
2C
 
 >>D #***	#"<2DT2J#JQVWWWW& 	 	 	DD  	 	 	#9    	# 	# 	#NN3q66"""""""""	## #**s   A 
B3 !B3'B..B3c                 N    |                      t          j                              S N)encodesysgetfilesystemencoding)xs    r   efnr0   c   s    88C-//000r   i   i dtypec                   &   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZeej        eej        eej        eej        eej        e	ej        e
ej        eej        eej        eej        eej        eej        iZej        dfeej        dfeej        dfeej        dfeej        dfeej        dfeej        dfeej        dfe	ej        dfe
ej        dfeej        dfeej        dfeej        dfeej        dfeiZeg eg eg eg e	g e
g eg edgedgedgedgiZdS )FI_TYPESr      r                     	   
         N)__name__
__module____qualname__FIT_UNKNOWN
FIT_BITMAP
FIT_UINT16	FIT_INT16
FIT_UINT32	FIT_INT32	FIT_FLOAT
FIT_DOUBLEFIT_COMPLEX	FIT_RGB16
FIT_RGBA16FIT_RGBF	FIT_RGBAFnumpyuint8uint16int16uint32int32float32float64
complex128dtypesfi_types
extra_dims r   r   r4   r4   k   s       KJJIJIIJKIJHI 	EKEL5;EL5;5=EMU%5<EL%-5=F  
a*	a*	a*	q:	a)	q:	a)	I	J		1{	q9	q:	H	IH$ 	B2B22BRA3QC1#A3JJJr   r4   c                   b   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZd	ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZd
ZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2dZ3dZ4dZ5dZ6dZ7dZ8dZ9dZ:dZ;dZ<dZ=dZ>dZ?dZ@dZAdZBdZCdZDdZEdZFdZGdZHdZIdZJdZKdZLdZMdZNdZOdZPdZQdZRdZSdZTdZUdZVdZWdS )IO_FLAGSi   r   r5   r   r7   r;          @         i      i   i    i   i @  i   i   i   r6   r9   r<   N)Xr@   rA   rB   FIF_LOAD_NOPIXELSBMP_DEFAULTBMP_SAVE_RLECUT_DEFAULTDDS_DEFAULTEXR_DEFAULT	EXR_FLOATEXR_NONEEXR_ZIPEXR_PIZ	EXR_PXR24EXR_B44EXR_LCFAXG3_DEFAULTGIF_DEFAULTGIF_LOAD256GIF_PLAYBACKHDR_DEFAULTICO_DEFAULTICO_MAKEALPHAIFF_DEFAULTJ2K_DEFAULTJP2_DEFAULTJPEG_DEFAULT	JPEG_FASTJPEG_ACCURATE	JPEG_CMYKJPEG_EXIFROTATEJPEG_QUALITYSUPERBJPEG_QUALITYGOODJPEG_QUALITYNORMALJPEG_QUALITYAVERAGEJPEG_QUALITYBADJPEG_PROGRESSIVEJPEG_SUBSAMPLING_411JPEG_SUBSAMPLING_420JPEG_SUBSAMPLING_422JPEG_SUBSAMPLING_444JPEG_OPTIMIZEJPEG_BASELINEKOALA_DEFAULTLBM_DEFAULTMNG_DEFAULTPCD_DEFAULTPCD_BASEPCD_BASEDIV4PCD_BASEDIV16PCX_DEFAULTPFM_DEFAULTPICT_DEFAULTPNG_DEFAULTPNG_IGNOREGAMMAPNG_Z_BEST_SPEEDPNG_Z_DEFAULT_COMPRESSIONPNG_Z_BEST_COMPRESSIONPNG_Z_NO_COMPRESSIONPNG_INTERLACEDPNM_DEFAULTPNM_SAVE_RAWPNM_SAVE_ASCIIPSD_DEFAULTPSD_CMYKPSD_LABRAS_DEFAULTRAW_DEFAULTRAW_PREVIEWRAW_DISPLAYSGI_DEFAULTTARGA_DEFAULTTARGA_LOAD_RGB888TARGA_SAVE_RLETIFF_DEFAULT	TIFF_CMYKTIFF_PACKBITSTIFF_DEFLATETIFF_ADOBE_DEFLATE	TIFF_NONETIFF_CCITTFAX3TIFF_CCITTFAX4TIFF_LZW	TIFF_JPEGTIFF_LOGLUVWBMP_DEFAULTXBM_DEFAULTXPM_DEFAULTr\   r   r   r^   r^      s       KLKKKIHGGIGFMKKLKKMKKKLIMIO O!!!"MMMKKKHLMKKLKO &#!NKLNKHGKKKKKMNLIMLINNHIKLKKKKr   r^   c                   6    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdS )METADATA_MODELSr   r5   r   r6   r7   r8   r9   r:   r;   r<   N)r@   rA   rB   FIMD_COMMENTSFIMD_EXIF_MAINFIMD_EXIF_EXIFFIMD_EXIF_GPSFIMD_EXIF_MAKERNOTEFIMD_EXIF_INTEROP	FIMD_IPTCFIMD_XMPFIMD_GEOTIFFFIMD_ANIMATIONr\   r   r   r   r     sB        MNNMIHLNNNr   r   c                      e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZi eej        eej        eej        edej        fdej        fgeej        eej        eej        eej        e	ej        e
ej        eej        edej        fdej        fgeej        eej        eej        edej        fdej        fdej        fdej        fgZdS )METADATA_DATATYPEr5   r   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?         r_         	numeratordenominatorRGBAN) r@   rA   rB   	FIDT_BYTE
FIDT_ASCII
FIDT_SHORT	FIDT_LONGFIDT_RATIONAL
FIDT_SBYTEFIDT_UNDEFINEDFIDT_SSHORT
FIDT_SLONGFIDT_SRATIONAL
FIDT_FLOATFIDT_DOUBLEFIDT_IFDFIDT_PALETTE
FIDT_LONG8FIDT_SLONG8	FIDT_IFD8rP   rQ   rR   rT   uint64int64int8rS   rU   rV   rW   rY   r\   r   r   r   r   #  s       IJJIMJNKJNJKHLJKI5;EL 	5< 	el3mU\5RS	
 	EL 	U[ 	5< 	EJ 	 	U[ 	EK 	+u{3mU[5QR 	EM 	U] 	%,  	%+%+%+%+	
!FFFr   r   c            	          e Zd ZdZi dej        dfdej        dfdej        dfdej        dfdej        dfdej        dfd	ej        dfd
ej        dfdej        dfdej        dfdej        dfdej        dfdej        dfdej        dfdej        dfdej        dfdej        dfi dej        dfdej        dfdej        dfdej        dfdej        dfdej        dfdej        dfdej        dfdej        dfdej        dfdej        dfdej        dfd ej        dfd!ej        dfd"ej        dfd#ej        dfd$ej        dfej        dfej        dfej        dfej        dfej        dfej        dfej        dfd%Zd& Z	e
d'             Zd( Zd) Zd* Zd+ Zd, Zd- Zd. Zd/ Zd0 Zd1 Zd6d2Zd7d4Zd7d5ZdS )8	Freeimagea  Class to represent an interface to the FreeImage library.
    This class is relatively thin. It provides a Pythonic API that converts
    Freeimage objects to Python objects, but that's about it.
    The actual implementation should be provided by the plugins.

    The recommended way to call into the Freeimage library (so that
    errors and warnings show up in the right moment) is to use this
    object as a context manager:
    with imageio.fi as lib:
        lib.FreeImage_GetPalette()

    FreeImage_AllocateTNFreeImage_FindFirstMetadataFreeImage_GetBitsFreeImage_GetPaletteFreeImage_GetTagKeyFreeImage_GetTagValueFreeImage_CreateTagFreeImage_SaveFreeImage_LoadFreeImage_LoadFromMemoryFreeImage_OpenMultiBitmap#FreeImage_LoadMultiBitmapFromMemoryFreeImage_LockPageFreeImage_OpenMemoryFreeImage_GetVersionFreeImage_GetFIFExtensionListFreeImage_GetFormatFromFIFFreeImage_GetFIFDescriptionFreeImage_ColorQuantizeExFreeImage_IsLittleEndianFreeImage_SetOutputMessageFreeImage_GetFIFCountFreeImage_IsPluginEnabledFreeImage_GetFileTypeFreeImage_GetTagTypeFreeImage_GetTagLengthFreeImage_FindNextMetadataFreeImage_FindCloseMetadataFreeImage_GetFIFFromFilenameFreeImage_FIFSupportsReadingFreeImage_FIFSupportsWritingFreeImage_FIFSupportsExportTypeFreeImage_FIFSupportsExportBPPFreeImage_GetHeight)FreeImage_GetWidthFreeImage_GetImageTypeFreeImage_GetBPPFreeImage_GetColorsUsedFreeImage_ConvertTo32BitsFreeImage_GetPitchFreeImage_Unloadc                 *    d  _         t          j                     _        g  _        t
          j                            d          rt          j	        }nt          j
        } |d t          j        t          j                   fd            }| _        d S )Nwinc                     |                     d          }j                            |           t          j                  dk    r4j                            d           t          j                  dk    2d S d S )Nutf-8rc   r   )decode	_messagesappendlenpop)fifmessageselfs     r   error_handlerz)Freeimage.__init__.<locals>.error_handler  sx    nnW--GN!!'***t~&&#--""1%%% t~&&#------r   )r   	threadingRLock_lockr  r-   platform
startswithctypesWINFUNCTYPE	CFUNCTYPEc_intc_char_p_error_handler)r  functyper  s   `  r   __init__zFreeimage.__init__  s    	 _&&
  <""5)) 	()HH'H 
$fo	6	6	& 	& 	& 	& 
7	6	& ,r   c                    | j         Q	 |                                  n;# t          $ r.}d| _         | xj         t          |          z  c_         Y d }~nd }~ww xY wt	          | j         t                    rt          | j                   | j         S )Nz+The freeimage library could not be loaded: )r   load_freeimageOSErrorr&   
isinstancer#   )r  errs     r   r'   zFreeimage.lib  s    9&##%%%% & & &I			SXX%							& di%% 	*ty)))ys    
A$AAc                 :    	 | j          n# t          $ r Y dS w xY wdS )NFT)r'   	Exceptionr  s    r   has_libzFreeimage.has_lib  s7    	HHH 	 	 	55	ts   
 
c                    d}	 |                                   |                                  | j                                                            d          dk    rd}n# t
          $ r Y nw xY w|s6t                       |                                   |                                  | j                            | j                   | j                                                            d          | _	        dS )zTry to load the freeimage lib from the system. If not successful,
        try to download the imageio version and try again.
        Fr  z3.15TN)
_load_freeimage_register_apir'   r   r  r-  r)   r   r(  lib_version)r  successs     r   r,  zFreeimage.load_freeimage  s    
 	   """   x,,..55g>>&HH 	 	 	D	  	!  """    	++D,?@@@888::AA'JJs   AA 
A,+A,c           	         ddg}g d}t                      }t                      }|rGt          |         }|D ]7}|                    dt          j                            |d|                     8t	          j        dd           }||                    d|           	 t          |||          \  }}n3# t          $ r&}t          |          dz   }	t          |	          d }~ww xY w|| _        || _        d S )N	freeimagelibfreeimage)	FreeImagezlibfreeimage.dylibzlibfreeimage.sozlibfreeimage.so.3r   r   z&
Please install the FreeImage library.)r   r	   r   insertr!   pathjoinr"   r   r-  r&   r   	lib_fname)
r  	lib_namesexact_lib_namesres_dirsr   r   dirr'   r/  err_msgs
             r   r5  zFreeimage._load_freeimage  s    .1	
 
 
 !??~~ 	Q&t,E Q Q&&q"',,sK*O*OPPPP i/66?""1c***	#!/9hGGJC 	# 	# 	##hh!JJG'"""	#
 	s   B1 1
C!;!CC!c                     | j                                         D ]+\  }\  }}t          | j        |          }||_        ||_        ,d S r+   )_APIitemsgetattrr'   restypeargtypes)r  frJ  rK  funcs        r   r6  zFreeimage._register_api  sR    &*ioo&7&7 	% 	%"A"48Q''D"DL$DMM	% 	%r   c                 B    | j                                          | j        S r+   )r   acquirer'   r2  s    r   	__enter__zFreeimage.__enter__  s    
xr   c                 `    |                                   | j                                         d S r+   )_show_any_warningsr   release)r  argss     r   __exit__zFreeimage.__exit__  s.    !!!
r   c                     g | _         dS )zwReset the list of output messages. Call this before
        loading or saving an image with the FreeImage API.
        Nr  r2  s    r   
_reset_logzFreeimage._reset_log  s     r   c                 t    | j         r0d                    | j                   }|                                  |S dS )zGet the output messages produced since the last reset as
        one string. Returns 'No known reason.' if there are no messages.
        Also resets the log.
         zNo known reason.)r  r?  rX  )r  ress     r   _get_error_messagezFreeimage._get_error_message  s<    
 > 	&((4>**COOJ%%r   c                     | j         rEt                              d|                                 z              |                                  dS dS )zIf there were any messages since the last reset, show them
        as a warning. Otherwise do nothing. Also resets the messages.
        zimageio.freeimage warning: N)r  r$   r%   r\  rX  r2  s    r   rR  zFreeimage._show_any_warnings&  sR     > 	NN84;R;R;T;TTUUUOO	 	r   c                 $    d | j         D             S )zwReturn a list of the last 256 output messages
        (warnings and errors) produced by the FreeImage library.
        c                     g | ]}|S r\   r\   ).0ms     r   
<listcomp>z,Freeimage.get_output_log.<locals>.<listcomp>3  s    ***a***r   rW  r2  s    r   get_output_logzFreeimage.get_output_log.  s    
 +*4>****r   c                 ^   | 5 }d}|dvrt          d          |dk    r||                    t          j        |          t	          |                    }|                    t          j        |          t	          |                    }|                    t          j        |                     |dk    rBt          j	        
                    |          r#|                    t          |          d          }|dk    r"|                    t          |                    }|dk    rt          d|z            |dk    r'|                    |          st          d	|z            |dk    r'|                    |          st          d
|z            |cddd           S # 1 swxY w Y   dS )a  Get the freeimage Format (FIF) from a given filename.
        If mode is 'r', will try to determine the format by reading
        the file, otherwise only the filename is used.

        This function also tests whether the format supports reading/writing.
        rwz"Invalid mode (must be "r" or "w").rNr   z$Cannot determine format of file "%s"wz$Cannot write the format of file "%s"z#Cannot read the format of file "%s")
ValueErrorr   r#  r'  r  FreeImage_GetFileTypeFromMemoryc_void_pFreeImage_CloseMemoryr!   r>  isfiler   r0   r  r  r  )r  filenamemodebbr'   ftypefimemorys          r   getFIFzFreeimage.getFIF5  s     	SE4 !EFFF
 s{{>"778K8KSQSWWUUH??113r77 E --foh.G.GHHHRKKRW^^H%=%=K55c(mmQGGE{{88XGG {{ !G(!RSSSS%E%Ee%L%L !G(!RSSSS%E%Ee%L%L !F!QRRR;	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   FF""F&)F&r   c                 &    t          | |||          S )zXcreate_bitmap(filename, ftype, flags=0)
        Create a wrapped bitmap object.
        )FIBitmapr  rn  rq  flagss       r   create_bitmapzFreeimage.create_bitmap[  s     hu555r   c                 &    t          | |||          S )zlcreate_multipage_bitmap(filename, ftype, flags=0)
        Create a wrapped multipage bitmap object.
        )FIMultipageBitmaprv  s       r   create_multipage_bitmapz!Freeimage.create_multipage_bitmapa  s     !x>>>r   r+   )r   )r@   rA   rB   __doc__r#  rk  r'  r&  rG  r*  propertyr'   r3  r,  r5  r6  rP  rU  rX  r\  rR  rc  rs  rx  r{  r\   r   r   r   r   O  s2        ; 	6; 	&'>; 	fot4;  	$ 7!;" 	6#;$ 	 &/4!8%;& 	6';( 	6?D1);* 	6?D1+;, 	#V_d$;-;. 	$fot%</;0 	./F1;2 	v53;4 	$ 75;: 	$ 7;;< 	(&/4)@=;> 	%v&=?; ;@ 	&'>A;B 	$fot%<C;F 	#V\4$8G;H 	%v&=I;J 	 &,!5K;L 	$flD%9M;N 	 &,!5O;R 	t 4S;T 	!6<"6U;V 	%v|T&:W;X 	&'>Y;\ 	't(<];^ 	't(<_;` 	't(<a;b 	*FL$+?c;d 	)6<*>e;f 	d3g; ;h  &|T2#)<"6#\40$*L$#7&,ot%<%|T2#_d3u; ; ;Dz, , ,6 	 	 X	  K K K4  B% % %      
& 
& 
&  + + +$ $ $ $L6 6 6 6? ? ? ? ? ?r   r   c                   4    e Zd Zd Zd Zd ZddZd Zd ZdS )	FIBaseBitmapc                 Z    || _         || _        || _        || _        d | _        g | _        d S r+   )_fi	_filename_ftype_flags_bitmap_close_funcs)r  r   rn  rq  rw  s        r   r*  zFIBaseBitmap.__init__i  s3    !r   c                 .    |                                   d S r+   )closer2  s    r   __del__zFIBaseBitmap.__del__q  s    

r   c                     | j         f| j        ra| j        D ]G}	 | j        5  |d         } ||dd            d d d            n# 1 swxY w Y   8# t          $ r Y Dw xY wg | _        d | _         d S d S d S )Nr   r5   )r  r  r  r1  )r  
close_funcfuns      r   r  zFIBaseBitmap.closet  s    L$$*;$"/  
 - -(mZ^,,- - - - - - - - - - - - - - - !   D "DDLLL %$$$s3   AAAA	A	A	
A
AANc                     | j         	 || j        j        j        |f}|| _         |r| j                            |           dS dS )zAFunction to set the bitmap and specify the function to unload it.N)r  r  r'   r  r  r  )r  bitmapr  s      r   _set_bitmapzFIBaseBitmap._set_bitmap  sZ    <#6>J 	1$$Z00000	1 	1r   c           	         d t           j                                        D             }t                      }t	          j                    }| j        5 }|D ]\  }}|                    || j        t	          j	        |                    }t	          j        |          }|rd}|r|
                    |                              d          }	|                    |          }
|                    |          }t          j        |z  }|                    |                    |                    }t#          t%          |                    }|}|
t&          j        k    r|                    dd          }n|
t&          j        v rt&          j        |
         }t,          rt/          |t0          t2          f          rnU	 t5          j        ||                                          }t;          |          dk    r|d         }n# t<          $ r Y nw xY w|                    |t                                }|||	<   |                     |t	          j	        |                    }||!                    |            |cd d d            S # 1 swxY w Y   d S )Nc                 V    g | ]&\  }}|                     d           |dd         |f'S )FIMD_r8   N)r"  )r`  namenumbers      r   rb  z.FIBaseBitmap.get_meta_data.<locals>.<listcomp>  sK     
 
 
fw''
!""Xv
 
 
r   Tr  replacer1   r5   r   )"r   __dict__rH  r   r#  rk  r  r   r  byrefr   r  r   r  c_charfrom_addressr   bytes	bytearrayr   r   rY   r   r.  listtuplerP   
frombuffercopyr  r1  
setdefaultr  r  )r  modelsmetadatatagr'   
model_namer  mdhandlemoretag_nametag_type	byte_sizechar_ptrdata	tag_bytestag_valr2   subdicts                     r   get_meta_datazFIBaseBitmap.get_meta_data  s   

 
 / 8 > > @ @
 
 
 66oX 2	&, -> ->"
F::DL&,s*;*;  "?844 '>D !#&#:#:3#?#?#F#Fw#O#O#&#;#;C#@#@$'$>$>s$C$C	#)=9#<'44S5N5Ns5S5STT$))D//$:$:	"+#'8'CCC&/&6&6w	&J&JGG%):)AAA$5$<X$FE& 
):edE]+K+K 
) $!).3.>(1/& /& /&&*dff %, (+7||q'8'829!*'0 !) !) !)$(D!) #+"5"5j$&&"I"I,3)"==$fl3&7&7   ?  !H 33H=== e2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	 2	s9   E%J7AG;:J;
HJHA-JJ	Jc                 6   i }t           j                                        D ]'\  }}|                    d          r|||dd          <   (d }| j        5 }|                                D ]\  }}|                    |d           }||                                D ]a\  }	}
|                                }t          j        |          }	 d}t          |
t                    r)	 |
                    d          }d}n# t          $ r Y nw xY w|rt          j        }t          |          }nt!          |
d          st#          j        |
g          }
 ||
j                  }|4t(                              d|	z             	 |                    |           |
                                }|
j        }|                    ||	                    d	                     |                    ||           |                    ||           |                    |t          |                     |                    ||           |                    |          }|                    || j         ||           n^# tB          $ rQ}t(                              d
|	d| j        "                                dt          |                     Y d }~nd }~ww xY w|                    |           I# |                    |           w xY w	 d d d            d S # 1 swxY w Y   d S )Nr  r8   c                 b    t           j                                        D ]\  }}| |k    r|c S d S r+   )r   rY   rH  )r2   r  numpy_dtypes      r   get_tag_type_numberz7FIBaseBitmap.set_meta_data.<locals>.get_tag_type_number  sE    '8'?'E'E'G'G  #K''!MMM ( tr   FasciiTr2   z>imageio.freeimage warning: Could not determine tag type of %r.r  z,imagio.freeimage warning: Could not set tag z: z, )#r   r  rH  r"  r  getr   r#  rk  r.  r&   r,   UnicodeErrorr   r   r  hasattrrP   arrayr2   r$   r%   FreeImage_DeleteTagtobytessizeFreeImage_SetTagKeyFreeImage_SetTagTypeFreeImage_SetTagCountFreeImage_SetTagLengthFreeImage_SetTagValuer   FreeImage_SetMetadatar  r1  r\  )r  r  r  r  r  r  r'   r  r  r  r  r  is_asciir  r  	tag_counttag_keyr/  s                     r   set_meta_datazFIBaseBitmap.set_meta_data  s   +4::<< 	* 	*LD&w'' *#)tABBx 	 	 	 X 5	5'/~~'7'7 45 45#
GJ55>)0 .5 .5%Hg1133C /#..C)5#(%gs33 %%,3NN7,C,C	+/#/ % % % $%# 5'8'CH(+III#*7G#<#< A*/+wi*@*@':':7='I'IH'/ &%@BJ%K!" !" !" !)( //4444' )0(9(9I(/I//X__W5M5MNNN00h???11#yAAA223IGGG11#yAAA"%"9"9#">">11&$,QTUUUU$     (xx)D)D)F)F)F)FCR        //4444//4444].5455	5 5	5 5	5 5	5 5	5 5	5 5	5 5	5 5	5 5	5 5	5 5	5 5	5 5	5 5	5 5	5 5	5 5	5s   A6L
I/"C:9I/:
D	I/D	A5I/=LCI/.K$/
K
	9AK	 K$K
	
K$L$K;	;LLLr+   )	r@   rA   rB   r*  r  r  r  r  r  r\   r   r   r  r  h  s|            
  
  
 	1 	1 	1 	1A A AFD5 D5 D5 D5 D5r   r  c                   N    e Zd ZdZd ZddZddZd Zd Zd Z	d	 Z
d
 ZddZdS )ru  z!Wrapper for the FI bitmap object.c           
         t          |t          j                  sJ |j        }|j        }|d d         \  }}t          |          dk    rd}n$t          |          dk    r	|d         }n|d         }	 t          j        |j        |f         }|| _	        n# t          $ r t          d          w xY w| j        5 }d|j        z  |z  }	|                    ||||	ddd          }
t          j        |
          }
|
s)t#          d| j                                        z            |                     |
|j        |
f           d d d            d S # 1 swxY w Y   d S )Nr   r5   r6   r   z,Cannot write arrays of given type and shape.r;   z)Could not allocate bitmap for storage: %s)r.  rP   ndarrayshaper2   r  r4   rZ   type_fi_typeKeyErrorri  r  itemsizer   r#  rk  r#   r\  r  r  )r  r  r  r2   rg  c
n_channelsfi_typer'   bppr  s              r   allocatezFIBitmap.allocate  s   %///// RaRy1u::??JJZZ1__qJJqJ	M'Z(@AG#DMM 	M 	M 	MKLLL	M X 	Een$z1C,,WaCAqIIF_V,,F  "?h11334   Vc&:F%CDDD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	Es   3 B B.8BE

EENc                 |   || j         }| j        5 }|                    | j        t	          |          | j                  }t          j        |          }|s1t          d| j         d| j        	                                          | 
                    ||j        |f           d d d            d S # 1 swxY w Y   d S )NzCould not load bitmap "": )r  r  r   r  r0   r  r#  rk  ri  r\  r  r  )r  rn  r'   r  s       r   load_from_filenamezFIBitmap.load_from_filename<  s   ~HX 	E''S]]DKPPF_V,,F   j~~~tx'B'B'D'D'DF   Vc&:F%CDDD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	Es   BB11B58B5c                     || j         }| j        }| j        }| j        }| j        5 }|t
          j        k    r*|                    ||                    |                    }n|	                    ||          }|st          d          |                    ||t          |          | j                  }|1t          d| j          d| j                                                   	 d d d            d S # 1 swxY w Y   d S )Nz2Cannot save image of this format to this file typezCould not save file `z`: )r  r  r  r  r  r4   rD   r  r  r  	TypeErrorr   r0   r  r#   r\  )r  rn  rq  r  r  r'   	can_writer[  s           r   save_to_filenamezFIBitmap.save_to_filename^  sV   ~H-X 	(--->>3//77 		  ??wOO	 V TUUU $$UFCMM4;OOC{"^DN^^tx?Z?Z?\?\^^   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   B?C33C7:C7c                    |                                  \  }}}|                     ||d          }| j        5 }|                                }d d d            n# 1 swxY w Y   d }t	          |          dk    r|r|j        t          j        k    r ||d                   } ||d                   }	 ||d                   }
|d         dk    rt          j        |
|	|f          S |d         dk    r) ||d                   }t          j        |
|	||f          S t          d|z             ||          
                                }|S )	NFc                    | j         dk    r| d d d         j        S | j         dk    r| d d d d df         j        S | j         dk    r| d d d d d d df         j        S | j         dk    r| d d d d d d d d df         j        S d S )Nr5   re  r   r6   r7   )ndimTarrs    r   nz"FIBitmap.get_image_data.<locals>.n  s    x1}}44R4y{"Q111ddd7|~%Q111aaa2:((Q111aaaDDbD=)++ r   r6   r   r5   r   r7   z Cannot handle images of shape %s)_get_type_and_shape_wrap_bitmap_bits_in_arrayr  r   r  r  rP   rQ   dstackri  r  )r  r2   r  r  r  r'   isler  bgrg  as               r   get_image_datazFIBitmap.get_image_data  s    4466uc//ueDDX 	2//11D	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2		, 		, 		, u::??t?
ek(A(A%(A%(A%(AQx1}}|Q1I...qQAeAhKK|Q1aL111 !Ce!KLLL AeHHMMOOs   AAAc                    t          |t          j                  sJ |j        }|j        }| j        5 }|                                }d d d            n# 1 swxY w Y   |d d         \  }}t          |          dk    rd}||f}	n)t          |          dk    r|d         }|||f}	n|d         }d }
|                     |	|d          }t          |          dk    r|r|j	        t          j
        k    r|d d d d df         }|d d d d df         }|d d d d df         } |
|          |d<    |
|          |d<    |
|          |d<   |d         dk    r|d d d d df         } |
|          |d<   n |
|          |d d <   | j        r|                     |           t          |          dk    r|j	        t          j
        k    r| j        5 }|                    | j                  }d d d            n# 1 swxY w Y   t          j        |          }|st#          d          	 t$          j        j        }n(# t(          $ r t$          j        d	         d         }Y nw xY wt          j        ||d
           d S d S d S )Nr   r5   r6   r   c                 "    | d d d         j         S )Nre  )r  r  s    r   r  z"FIBitmap.set_image_data.<locals>.n  s    ttt9;r   Tr7   zCould not get image paletter  rd   )r.  rP   r  r  r2   r  r   r  r  r  rQ   _need_finish_finish_wrapped_arrayr   r  r#  rk  r#   GREY_PALETTEr  r1  __array_interface__memmove)r  r  r  r2   r'   r  rg  r  r  w_shaper  wrapped_arrayr   r   r   r   palettepalette_datas                     r   set_image_datazFIBitmap.set_image_data  sX   %/////X 	2//11D	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 RaRy1u::??J!fGGZZ1__qJ!1a(GGqJ	 	 	 77MM u::??t?
ek(A(AaaaAgAaaaAgAaaaAgA qttM! qttM! qttM!Qx1}}!!!QQQ'N#$1Q44a  qxxM!!! 	6&&}555u::??uzU[88 AS224<@@A A A A A A A A A A A A A A Aog..G B"#@AAAK+27 K K K+?GJKN7L$77777 ?88s5   AAAG55G9<G9&H8 8"IIc                 n   | j         5 }|                    | j                  }|                    | j                  }ddd           n# 1 swxY w Y   |d         }||z  }|j        }	t          |          dk    r|	|d         |	z  |f}
n|	|f}
t          j        |z                      |          }	 d| _	        t          rt                      t          j        ||||
          S # t          $ rD |rd| _	        t          j        ||          cY S t          t!          |                    }t          j        ||                                          }t          |          dk    r|d	         |
d         |d         z  |d         f|_        |d|d	         d|d
         d|d         f         }t          j        ||j                  }t+          |d                   D ]}|dddd|f         j        ||<   n4|d
         |
d         f|_        |d|d
         d|d         f         j        }|cY S w xY w)a  Return an ndarray view on the data in a FreeImage bitmap. Only
        valid for as long as the bitmap is loaded (if single page) / locked
        in memory (if multipage). This is used in loading data, but
        also during saving, to prepare a strided numpy array buffer.

        Nre  r6   r   F)r2   bufferstridesTr1   r   r5   )r  r  r  r   r  r  r#  r  r  r  TEST_NUMPY_NO_STRIDESNotImplementedErrorrP   r  zerosr  r  r  r  r  r2   ranger  )r  r  r2   saver'   pitchbitsheightr  r  r  r  rp  r  array2is                   r   r  z#FIBitmap._wrap_bitmap_bits_in_array  s    X 	7**4<88E((66D	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7
 rUN	> u::??qH!4e<GG'G 	)77==	 %D$ ,)+++=eD'RRRR" 	 	 	  $(!{56666669T??++(5999>>@@u::??"'(GBK58,CU1X"MEK":U1X:zqz:U1X:#EFF!KU[AAAE"58__ 5 5#)!!!QQQ'?#4a5 #((GBK"7EK!*E!H*jaj"89;E)	s*   5A		AA13C% %*H4D H43H4c                    | j         5 }|                    | j                  }|                    | j                  }|                    | j                  }ddd           n# 1 swxY w Y   |dz  |j        z  }||z  }||j        d         z
  }d|cxk    rdk     sn J |j        d         ||f}	t          j        |	|j	                  }
|dk    r|j
        |
ddd|j        d         df<   n;t          |          D ]+}||ddddf         j
        |
ddd|j        d         |f<   ,|
j        d         d         }t          j        |||
j                   ~
dS )	z-Hardcore way to inject numpy array in bitmap.Nr;   r   r=   re  r5   r  )r  r  r  r   r  r  r  rP   r  r2   r  r  r  r#  r  nbytes)r  r  r'   r  r  r  	nchannels	realwidthextranewshaper   r  data_ptrs                r   r  zFIBitmap._finish_wrapped_array.  s    X 	5**4<88E((66D&&t|44C	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5
 1H.	Y&	EKO+EB;r?Iy8Xu{33>>.3gF111'B'*++9%% C C27111aaa.2Bqqq+EKO+Q.//-f5a8tXv}555FFs   AA##A'*A'c                 B   | j         }| j        5 }|                    |          }|                    |          }|                    |          x| _        }|st          d          	 d d d            n# 1 swxY w Y   d }t          j        |         }|t          j	        k    r| j        5 }|
                    |          }|                    |          }d d d            n# 1 swxY w Y   |r|dk    r|                    |          }	t          j        |	          }	t          j        dz                      |	j                  }
t%          j        |
t$          j                                                  }
t,          |
k                                    rg }t%          j        |          |||gz   |fS |                    |          }t          j        |          }|                     |           |                                 S |dk    rg }nx|dk    rdg}nn|dk    rdg}nd|                    |          }t          j        |          }|                     |           |                                 S t          j        |         }t%          j        |          |||gz   |fS )	NzUnknown image pixel typerc   rd   r;      r6   r`   r7   )r  r  r
  r	  r  r  ri  r4   rY   rD   r  r  r   r#  rk  c_uint8r  valuerP   r  rT   r  r  allr2   r  r  r  r[   )r  r  r'   rh  hr  r  r2   has_palletter  pr[   	newbitmaps                r   r  zFIBitmap._get_type_and_shapeH  s    X 	=&&v..A''//A&)&@&@&H&HHDMG = !;<<<=		= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= (h))) CS**622"::6BBC C C C C C C C C C C C C C C  23&&!66v>>G$og66G73AA'-PPA(EL99>>@@A$)..00 L%'
${511:A3FKK99&AA	"OI66	  +++//111

S

S

  99&AA	"OI66	  +++//111!,W5J {5!!:A#6;;s$   AA44A8;A8++C""C&)C&r   rc   c                    | j         5 }|                    | j        ||dd          }t          j        |          }|s1t          d| j        d| j                                                   t          | j         | j        | j	        | j
                  }|                    ||j        |f           | j        |_        |cddd           S # 1 swxY w Y   dS )zQuantize the bitmap to make it 8-bit (paletted). Returns a new
        FIBitmap object.
        Only for 24 bit images.
        r   NzCould not quantize bitmap "r  )r  r   r  r#  rk  ri  r  r\  ru  r  r  r  r  r  )r  	quantizerpalettesizer'   r  news         r   quantizezFIBitmap.quantize}  s%   
 X 	22ia F _V,,F   j~~~tx'B'B'D'D'DF  
 48T^T[$+NNCOOFS%96$BCCC=CL#	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   B6CCCr+   )r   rc   )r@   rA   rB   r|  r  r  r  r  r  r  r  r  r  r\   r   r   ru  ru    s        ++"E "E "EHE E E ED   @# # #J58 58 58n2 2 2h  43< 3< 3<j     r   ru  c                   4    e Zd ZdZddZddZd Zd Zd ZdS )	rz  z+Wrapper for the multipage FI bitmap object.Nc           	         || j         }d}d}d}| j        5 }|                    | j        t	          |          |||| j                  }t          j        |          }|s3| j                                        }t          d| j         d|          | 
                    ||j        |f           d d d            d S # 1 swxY w Y   d S )NFTCould not open file "z" as multi-image: )r  r  r   r  r0   r  r#  rk  r\  ri  r  FreeImage_CloseMultiBitmap)r  rn  
create_new	read_onlykeep_cache_in_memoryr'   multibitmapr/  s           r   r  z$FIMultipageBitmap.load_from_filename  sD   ~H 
	$ X 	Y77H$ K !/+66K  h1133 j~~~ss,   [3+I;*WXXX'	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Ys   BB<<C C c           	         || j         }d}d}d}| j        5 }|                    | j        t	          |          |||d          }t          j        |          }|s3d| j         d| j                                        }t          |          | 	                    ||j
        |f           d d d            d S # 1 swxY w Y   d S )NTFr   r  z" for writing multi-image: )r  r  r   r  r0   r#  rk  r\  ri  r  r  )r  rn  r  r  r  r'   r  msgs           r   r  z"FIMultipageBitmap.save_to_filename  s>   ~H 
	$ X 	Y77H$ K !/+66K  & &NNNH//111 !oo%[3+I;*WXXX%	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Ys   BB77B;>B;c                 x    | j         5 }|                    | j                  cd d d            S # 1 swxY w Y   d S r+   )r  FreeImage_GetPageCountr  )r  r'   s     r   __len__zFIMultipageBitmap.__len__  s~    X 	<--dl;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<s   /33c                    | j         5 }|                    | j        |          }t          j        |          }|s1t          d|| j        | j                                         fz            t          | j         | j        | j	        | j
                  }|                    ||j        | j        |df           |cddd           S # 1 swxY w Y   dS )zlReturn the sub-bitmap for the given page index.
        Please close the returned bitmap when done.
        z%Could not open sub-image %i in %r: %sFN)r  r   r  r#  rk  ri  r  r\  ru  r  r  r  FreeImage_UnlockPage)r  indexr'   r  bms        r   get_pagezFIMultipageBitmap.get_page  s    X 	++DL%@@F_V,,F  ;dndh.I.I.K.KLM   $(DNDKMMBNN14<O   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   B.CC
Cc                     | j         5 }|                    | j        |j                   ddd           dS # 1 swxY w Y   dS )z*Add a sub-bitmap to the multi-page bitmap.N)r  FreeImage_AppendPager  )r  r  r'   s      r   append_bitmapzFIMultipageBitmap.append_bitmap  s    X 	C$$T\6>BBB	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	Cs   !6::r+   )	r@   rA   rB   r|  r  r  r$  r)  r,  r\   r   r   rz  rz    s}        55Y Y Y YdY Y Y Y>< < <  *C C C C Cr   rz  )NF)%r|  r!   r-   r#  r  loggingrP   corer   r   r   r   r   r	   r
   r   	getLoggerr@   r$   r  r   r   r)   r0   arangerT   r  objectr4   r^   r   r   r   r  ru  rz  r   r\   r   r   <module>r2     s    
			 



       	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
	8	$	$  10))//     .# # #:1 1 1
 u|Az:U\JJJ; ; ; ; ;v ; ; ;|j j j j jv j j jZ
 
 
 
 
f 
 
 
) ) ) ) ) ) ) )XV? V? V? V? V? V? V? V?rj5 j5 j5 j5 j56 j5 j5 j5Z~ ~ ~ ~ ~| ~ ~ ~lqC qC qC qC qC qC qC qCj Y[[r   