
    nj                     6   S SK rS SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
Jr  S SKJrJr  S SKJrJr      SS\	S	\\	   S
\\	   S\S\\   4
S jjrS r " S S\5      r\R.                  4S jr\S:X  a#  \R4                  " \" \R.                  5      5        gg)    N)Optional)gdal)PathLikeOrStr)get_color_table)GDALArgumentParser
GDALScript)GetOutputDriverForopen_dssrc_filenamepct_filenamedst_filenamecolor_countdriver_namec           	         [        U 5      nUc  [        SU  35      eUR                  S:  a  [        U  SUR                   S35      eU(       d  [        U5      n[        R
                  " U5      nUc  [        SU S35      eUcj  [        R                  " 5       n[        R                  " UR                  S5      UR                  S5      UR                  S5      UU[        R                  S	9nO[        U5      nUR                  5       S
:X  a  Un	OSS Kn
U
R                  SS9u  p[        R
                  " S5      nUR                  XR                  UR                   S5      nUR                  S5      R#                  U5        UR%                  UR'                  5       5        UR)                  UR+                  5       5        UR-                  5       S:  a.  UR/                  UR1                  5       UR3                  5       5        [        R4                  " UR                  S5      UR                  S5      UR                  S5      UR                  S5      U[        R                  S	9nU[        R6                  :w  a  [        S5      eX:X  a  UnU$ UR9                  U=(       d    SU5      nS n[:        R<                  " W5        UR?                  U	5        U$ )NzUnable to open    z has z0 band(s), need 3 for inputs red, green and blue."z" driver not registered.      )callbackgtiffr   z.tif)suffixGTiffzDitherRGB2PCT failed ) r
   	ExceptionRasterCountr	   r   GetDriverByName
ColorTableComputeMedianCutPCTGetRasterBandTermProgress_nocbr   lowertempfilemkstempCreateRasterXSizeRasterYSizeSetRasterColorTableSetProjectionGetProjectionSetGeoTransformGetGeoTransformGetGCPCountSetGCPsGetGCPsGetGCPProjectionDitherRGB2PCTCE_None
CreateCopyoscloseDelete)r   r   r   r   r   src_ds
dst_drivercterrtif_filenamer"   tif_filedescgtiff_drivertif_dsdst_dss                  O/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/osgeo_utils/rgb2pct.pyrgb2pctr@   -   s    \"F~/,899AnE&"4"4!55ef
 	

 (6%%k2J!K=(@ABB __&&  #  #  #++
 \*
 g%#%-%5%5V%5%D"''0L  ((&*<*<aF //3
 --/0
61134av~~')@)@)BC
 

QQQQ
''C dll.//# M &&|'9r6B
L)M    c                  B     [        S0 U D6nUS4$ ! [         a     gf = f)Nr   )Nr    )r@   r   )kwargsdss     r?   doitrF      s/    v1u s    
c                   <   ^  \ rS rSrU 4S jrS\4S jrS rSrU =r	$ )RGB2PCT   c                 f   > [         TU ]  5         SU l        [        R                  " S5      U l        g )Nz0Convert a 24bit RGB image to 8bit paletted imagea              This utility will compute an optimal pseudo-color table for a given RGB image
            using a median cut algorithm on a downsampled RGB histogram.
            Then it converts the image into a pseudo-colored image using the color table.
            This conversion utilizes Floyd-Steinberg dithering (error diffusion)
            to maximize output image visual quality.)super__init__titletextwrapdedentdescription)self	__class__s    r?   rL   RGB2PCT.__init__   s+    G
#??8
rA   returnc                 :   U R                   nUR                  SSSSS9  UR                  5       nUR                  SS[        SS	[	        [        S
S5      5      SS9  UR                  SS[        SS9  UR                  S[        SS9  UR                  S[        SS9  U$ )Nz-ofr   gdal_formatzpSelect the output format. if not specified, the format is guessed from the extension. Use the short format name.)destmetavarhelpz-nr      colorr   i  zeSelect the number of colors in the generated color table. Defaults to 256. Must be between 2 and 256.)rW   typedefaultrX   choicesrY   z-pctr   a  Extract the color table from <palette_file> instead of computing it. Can be used to have a consistent color table for multiple files. The palette file must be either a raster file in a GDAL supported format with a palette or a color file in a supported format (txt, qml, qlr).)rW   r\   rY   r   zThe input RGB file.)r\   rY   r   z4The output pseudo-colored file that will be created.)parseradd_argumentadd_mutually_exclusive_groupinttuplerangestr)rQ   argvr_   groups       r?   
get_parserRGB2PCT.get_parser   s    !)	 	 	
 335%3-(: 	 		
 	M	 	 	
 	N;PQG 	 	
 rA   c                     [        S0 UD6$ )NrC   )r@   )rQ   rD   s     r?   rF   RGB2PCT.doit   s       rA   )rP   rM   )
__name__
__module____qualname____firstlineno__rL   r   rh   rF   __static_attributes____classcell__)rR   s   @r?   rH   rH      s     

*"4 *X! !rA   rH   c                 4    [        5       R                  U 5      $ )N)rH   main)rf   s    r?   rs   rs      s    9>>$rA   __main__)NNrZ   N)os.pathr3   sysrN   typingr   osgeor   osgeo_utils.auxiliary.baser   !osgeo_utils.auxiliary.color_tabler   #osgeo_utils.auxiliary.gdal_argparser   r   osgeo_utils.auxiliary.utilr	   r
   rb   re   r@   rF   rH   rf   rs   rl   exitrC   rA   r?   <module>r~      s   B  
    4 = N B
 -1,0!%WW=)W =)W 	W
 #Wt:!j :!z hh   zHHT#((^ rA   