
    BPc                         d Z ddlZddlZddlmZ ddlmZmZ  G d deej                  Z	e
dk    r e	                                             dS dS )z!
Optimise PNG file using optipng
    N)TempDirMixin)ProgramRunErrorcallc                        e Zd Zd Zd Zd ZdS )	PngOutputc                 z   |                     d           |                     dt          j        d           |                     dt          d           |                     dt          j        d           |                     dt          j        d           |                     d	t          j        d           d S )
Nz--tabz--interlaceF)typedefaultz--levelr   z
--bitdepthz--colorz	--palette)add_argumentinkexBooleanint)selfparss     3/usr/share/inkscape/extensions/raster_output_png.pyadd_argumentszPngOutput.add_arguments   s    '"""-emUKKK)#q999,U]EJJJ)%-GGG+EM5IIIII    c                    t           j                            | j        d          | _        t          | j        d          5 }|                    |                                           ddd           dS # 1 swxY w Y   dS )z*Load the PNG file (prepare it for optipng)z	input.pngwbN)ospathjointempdirpng_fileopenwriteread)r   streamfhls      r   loadzPngOutput.load   s    T\;??$-&& 	%#IIfkkmm$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s    (A55A9<A9c                    | j         j        t          | j         j                  | j         j         | j         j         | j         j         d}	 t          d| j        fddd| n# t          $ r}d|j
                            d          v r!t          j        t          d                    t          j        t          d          d	z   |j
                            d          z             d
}~ww xY wt          j                            | j                  rWt%          | j        d          5 }|                    |                                           d
d
d
           d
S # 1 swxY w Y   d
S d
S )z-Pass the PNG file to optipng with the options)oinbncnpoptipngT)oldieclobberzIDAT recoding is necessaryzutf-8zThe optipng command failed, possibly due to a mismatch of theinterlacing and compression level options. Please try to disable "Interlaced" or set "Level" to 1 or higher.z6The optipng command failed with the following message:
Nrb)optionslevelr   	interlacebitdepthcolorpaletter   r   r   stderrdecoder   AbortExtension_r   r   isfiler   r   r   )r   r   r,   errr   s        r   savezPngOutput.save   s    #T\+,,l++l((l**
 
	DMOtOOwOOOO 	 	 	+sz/@/@/I/III*F    *NOOj''001  	  7>>$-(( 	)dmT** )cSXXZZ((() ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )	) 	)s+   A# #
C.-A<C))C.*(EE#&E#N)__name__
__module____qualname__r   r    r8    r   r   r   r      sD        J J J% % %) ) ) ) )r   r   __main__)__doc__r   r   inkex.extensionsr   inkex.commandr   r   RasterOutputExtensionr   r9   runr<   r   r   <module>rC      s     
			  ) ) ) ) ) ) / / / / / / / /.) .) .) .) .)e9 .) .) .)b zIKKOO r   