
    (e                     .   d dl Zd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZmZ 	 	 	 	 dde	d	ee	   d
ee	   dedee   f
dZd Z G d de      Zej.                  fdZedk(  r# ej4                   eej.                               yy)    N)Optional)gdal)PathLikeOrStr)get_color_table)GDALArgumentParser
GDALScript)GetOutputDriverForopen_dssrc_filenamepct_filenamedst_filenamecolor_countdriver_namec                    t        |       }|t        d|        |j                  dk  rt        |  d|j                   d      |st        |      }t	        j
                  |      }|t        d| d      |kt	        j                         }t	        j                  |j                  d      |j                  d      |j                  d      ||t        j                  	      }nt        |      }|j                         d
k(  r|}	ndd l}
|
j                  d      \  }}	t	        j
                  d      }|j                  |	|j                  |j                   d      }|j                  d      j#                  |       |j%                  |j'                                |j)                  |j+                                |j-                         dkD  r.|j/                  |j1                         |j3                                t	        j4                  |j                  d      |j                  d      |j                  d      |j                  d      |t        j                  	      }|t        j6                  k7  rt        d      |	|k(  r|}|S |j9                  |xs d|      }d }t;        j<                         |j?                  |	       |S )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                  5/usr/lib/python3/dist-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"l''0L  f((&*<*<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                  @    	 t        di | }|dfS # t        $ r Y yw xY w)Nr   )Nr    )r@   r   )kwargsdss     r?   doitrF      s/    v1u s    	c                   0     e Zd Z fdZdefdZd Z xZS )RGB2PCTc                 d    t         |           d| _        t        j                  d      | _        y )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?   rK   zRGB2PCT.__init__   s+    G
#??8
rA   returnc                 X   | j                   }|j                  dddd       |j                         }|j                  ddt        dd	t	        t        d
d            d       |j                  ddt        d       |j                  dt        d       |j                  dt        d       |S )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.)rU   typedefaultrV   choicesrW   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).)rU   rZ   rW   r   zThe input RGB file.)rZ   rW   r   z4The output pseudo-colored file that will be created.)parseradd_argumentadd_mutually_exclusive_groupinttuplerangestr)rP   argvr]   groups       r?   
get_parserzRGB2PCT.get_parser   s    !)	 	 	
 335%3-(: 	 		
 	M	 	 	
 	N;PQG 	 	
 rA   c                     t        di |S )NrC   )r@   )rP   rD   s     r?   rF   zRGB2PCT.doit   s       rA   )__name__
__module____qualname__rK   r   rf   rF   __classcell__)rQ   s   @r?   rH   rH      s    

*"4 *X!rA   rH   c                 4    t               j                  |       S )N)rH   main)rd   s    r?   rm   rm      s    9>>$rA   __main__)NNrX   N)os.pathr3   sysrM   typingr   osgeor   osgeo_utils.auxiliary.baser   !osgeo_utils.auxiliary.color_tabler   #osgeo_utils.auxiliary.gdal_argparser   r   osgeo_utils.auxiliary.utilr	   r
   r`   rc   r@   rF   rH   rd   rm   rh   exitrC   rA   r?   <module>rx      s   B  
    4 = N B
 -1,0!%WW=)W =)W 	W
 #Wt:!j :!z hh   zCHHT#((^ rA   