
    }f.                    V   d dl mZ d dlZd dlZd dlZddlmZmZmZm	Z	  G d d          Z
d Zd Zd	 Z G d
 dej                  Zd Zd Z ej        ej        ee            ej        ej        e            ej        ej        g d            ej        ej        d           dS )    )annotationsN   )Image	ImageFileImagePalette_binaryc                  >    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
S )	BoxReaderz}
    A small helper class to read fields stored in JPEG2000 header boxes
    and to easily step into and read sub-boxes.
    c                F    || _         |dk    | _        || _        d| _        d S )Nr   r   )fp
has_lengthlengthremaining_in_box)selfr   r   s      N/var/www/rtfcompare/venv/lib/python3.11/site-packages/PIL/Jpeg2KImagePlugin.py__init__zBoxReader.__init__   s)     A+ "    c                    | j         r'| j                                        |z   | j        k    rdS | j        dk    r|| j        k    S dS )NFr   T)r   r   tellr   r   )r   	num_bytess     r   	_can_readzBoxReader._can_read%   sM    ? 	tw||~~	9DKGG5 A%% 5554r   c                ,   |                      |          sd}t          |          | j                            |          }t	          |          |k     r%d| dt	          |           d}t          |          | j        dk    r| xj        |z  c_        |S )NzNot enough data in headerzExpected to read z bytes but only got .r   )r   SyntaxErrorr   readlenOSErrorr   )r   r   msgdatas       r   _read_byteszBoxReader._read_bytes/   s    ~~i(( 	#-Cc"""w||I&&t99y  QiQQSYYQQQC#,, 1$$!!Y.!!r   c                ~    t          j        |          }|                     |          }t          j        ||          S N)structcalcsizer!   unpack)r   field_formatsizer    s       r   read_fieldszBoxReader.read_fields=   s6    |,,%%}\4000r   c                ~    | j         }|                     |          }t          t          j        |          |          S r#   )r   r!   r
   ioBytesIO)r   r(   r    s      r   
read_boxeszBoxReader.read_boxesB   s7    $%%D))4000r   c                h    | j         r*| j                                        | j        z   | j        k     S dS )NT)r   r   r   r   r   )r   s    r   has_next_boxzBoxReader.has_next_boxG   s0    ? 	7<<>>D$99DKGG4r   c                l   | j         dk    r*| j                            | j         t          j                   d| _         |                     d          \  }}|dk    r|                     d          d         }d}nd}||k     s|                     ||z
            sd}t          |          ||z
  | _         |S )	Nr   r   z>I4sr   z>Q      zInvalid header length)r   r   seekosSEEK_CURr)   r   r   )r   lboxtboxhlenr   s        r   next_box_typezBoxReader.next_box_typeM   s     1$$GLL.<<< " %%f--
d199##D))!,DDDD$;;dnnTD[99;)Cc""" $tr   N)r   )__name__
__module____qualname____doc__r   r   r!   r)   r-   r/   r9    r   r   r
   r
      s         
# # # #    1 1 1
1 1 1
      r   r
   c                   |                      d          }t          j        |          }||                      |dz
            z   }t          j        d|          \  }}}}}}}	}	}	}	}
||z
  ||z
  f}|
dk    r.t          j        d|d          }|d         dz  dz   dk    rd	}n d
}n|
dk    rd}n|
dk    rd}n|
dk    rd}nd}||fS )zParse the JPEG 2000 codestream to extract the size and component
    count from the SIZ marker segment, returning a PIL (size, mode) tuple.   z>HHIIIIIIIIHr   z>B&   r      r2   I;16LLA   RGB   RGBAN)r   r   i16ber$   unpack_from)r   hdrlsizsizrsizxsizysizxosizyosiz_csizr(   ssizmodes                 r   _parse_codestreamrX   c   s     ''!**C=D
q!!
!C=C=O> >:D$dE5!Q1d 5L$,'Dqyy!$R00GdNa!##DDDD			:r   c                4    |dk    rd| z  d|z  z  d|z  z  S dS )zConvert JPEG2000's (numerator, denominator, exponent-base-10) resolution,
    calculated as (num / denom) * 10^exp and stored in dots per meter,
    to floating-point dots per inch.r      
   i'  Nr>   )numdenomexps      r   _res_to_dpir_      s0     zzc	RW%%%-88 zr   c                P   t          |           }d}d}|                                rj|                                }|dk    r|                                }n;|dk    r!|                    d          d         dk    rd}|                                jd}d}d}d}d}	d}
|                                r|                                }|dk    rY|                    d	          \  }}}}||f}|d
k    r|dz  dk    rd}ny|d
k    rd}no|dk    rd}ne|dk    rd}n[|dk    rd}nQ|dk    r|dv r|                    d          \  }}|                    dd|z  z             }t          |          dk    r]t          j                    }
t          |          D ]0}|
                    |                    dd|z  z                        1|dk    rdnd}n|dk    r|                                }|                                ru|                                }|dk    rG|                    d          \  }}}}}}t          |||          }t          |||          }||||f}	n|                                u|                                ||d}t          |          ||||	|
fS ) zParse the JP2 header box to extract size, component count,
    color space information, and optionally DPI information,
    returning a (size, mode, mimetype, dpi) tuple.Ns   jp2hs   ftypz>4sr   s   jpx z	image/jpxs   ihdrz>IIHBr   rB   r2   rC   rD   r@   rE   rF   rG   rH   rI   s   pclr)rD   rE   z>HB>BPPAs   res s   rescz>HHHHBBzMalformed JP2 header)r
   r/   r9   r-   r)   maxr   rangegetcolorr_   r   )r   readerheadermimetyper7   r(   rW   bpcncdpipaletteheightwidthnenpc	bitdepthsirestresvrcnvrcdhrcnhrcdvrcehrcehresvresr   s                               r   _parse_jp2_headerr      s$    r]]FFH




 '##%%7??&&((FW__!!%((+w66& 



 ' DD
C	B
CG




 "##%%7??%+%7%7%@%@"FE2s6?DQwwC$J!++qqqqW__!4!4((//GB**3#)+<==I9~~""&355r L LA$$V%7%7sSy8I%J%JKKKK"ckksstW__##%%C""$$ ((**7??9<9S9S6D$dD$&tT488D&tT488D'D,<#Tl ""$$ 5 



 "H |t|$#xg--r   c                  h     e Zd ZdZdZd Zd Ze fd            Zej	        d             Zd Z
 xZS )Jpeg2KImageFileJPEG2000zJPEG 2000 (ISO 15444)c                R   | j                             d          }|dk    r)d| _        t          | j                   \  | _        | _        n|| j                             d          z   }|dk    rd| _        t          | j                   }|\  | _        | _        | _        }| _        |
|| j	        d<   | j                             d          
                    d	          r|                                  nd
}t          |          | j        | j        d}t          |          d| _        d| _        d}d}	 | j                                         }t%          j        |          j        }n# t*          $ r d}	 | j                                         }| j                             dt0          j                   | j                                         }| j                             |           n# t*          $ r d}Y nw xY wY nw xY wdd| j        z   d| j        | j        | j        ||ffg| _        d S )NrH      OQj2kr2         jP  

jp2rm      s   jp2cOQznot a JPEG 2000 filezunable to determine size/moder   r   jpeg2kr   r   )r   r   codecrX   _size_moder   custom_mimetypern   infoendswith_parse_commentr   r(   rW   _reducelayersfilenor4   fstatst_size	Exceptionr   r3   r+   SEEK_ENDtile)r   sigri   rm   r   fdr   poss           r   _openzJpeg2KImageFile._open   s   gll1oo%%%DJ%6tw%?%?"DJ

Q'C==="
*4733RXO
DJ(<c4<?'*DIe$7<<##,,-DEE *''))),!#&&&9	 11Cc"""	!!BXb\\)FF 	 	 	BgllnnQ,,,S!!!!   	 "T\4;FC	
			s7   52E( (G<5A1G'&G<'G63G<5G66G<;G<c                    | j                             d          }t          j        |          }| j                             |dz
  t
          j                   	 | j                             d          }|sd S |d         }|dv rd S | j                             d          }t          j        |          }|dk    r/| j                             |dz
            dd          | j        d<   d S | j                             |dz
  t
          j                   )Nr@   Tr   )      d   comment)r   r   r   rJ   r3   r4   r5   r   )r   rL   r   markertyps        r   r   zJpeg2KImageFile._parse_comment
  s    gll1oos##VaZ---	6W\\!__F )Cl""',,q//C]3''Fd{{'+w||FQJ'?'?'C	)$VaZ555	6r   c                8    | j         pt                      j        S r#   )r   superreduce)r   	__class__s    r   r   zJpeg2KImageFile.reduce   s    
 |-uww~-r   c                    || _         d S r#   )r   )r   values     r   r   zJpeg2KImageFile.reduce'  s    r   c                   | j         r| j        rd| j        z  }|dz	  }t          | j        d         |z   |z            t          | j        d         |z   |z            f| _        | j         d         }|d         d         | j        | j        |d         d         |d         d         f}|d         d| j        z   |d         |fg| _         t          j                            |           S )Nr   r   rF   rH   r   r@   )r   r   intr(   r   r   r   load)r   poweradjusttt3s        r   r   zJpeg2KImageFile.load+  s    9 	? 	?%EaZFTYq\F*e344TYq\F*e344DJ 	!AA$q'4<ad1gqtAwGBA$ 2AaD"=>DI"''---r   )r:   r;   r<   formatformat_descriptionr   r   propertyr   setterr   __classcell__)r   s   @r   r   r      s        F02
 2
 2
h6 6 6, . . . . X. ]  ]. . . . . . .r   r   c                :    | d d         dk    p| d d         dk    S )NrH   r   r   r   r>   )prefixs    r   _acceptr   <  s/    rr
)) 	B#2#;AAr   c                   | j         }|                    d          s|                    dd          rd}nd}|                    dd           }|                    dd           }|                    dd           }|                    d	d
          }|                    dd           }	|	Ft          |	t          t
          f          rt          d |	D                       sd}
t          |
          |                    dd          }|                    dd           }|                    dd           }|                    dd          }|                    dd          }|                    dd          }|                    dd          }|                    dd          }|                    d          }t          |t                    r|	                                }|                    dd          }d}t          |d          r(	 |                                }n# t          $ r d}Y nw xY w|||||	|||||||||||f| _        t          j        | |dd| j        z   d|fg           d S )N.j2kno_jp2Fr   r   offsettile_offset	tile_sizequality_moderatesquality_layersc              3  N   K   | ] }t          |t          t          f          V  !d S r#   )
isinstancer   float).0quality_layers     r   	<genexpr>z_save.<locals>.<genexpr>W  sA       
 
8EJ}sEl33
 
 
 
 
 
r   z,quality_layers must be a sequence of numbersnum_resolutionsr   codeblock_sizeprecinct_sizeirreversibleprogressionLRCPcinema_modenomctsignedr   pltr   r   r   r   )encoderinfor   getr   listtupleall
ValueErrorstrencodehasattrr   r   encoderconfigr   _saver(   )imr   filenamer   kindr   r   r   r   r   r   r   	cblk_sizer   r   r   r   r   r   r   r   r   s                         r   r   r   G  s   >D   DHHXu$=$= XXh%%F((=$//Kd++I88NG44LXX.55N!>D%=11 " 
 
IW
 
 
 
 
 " =oohh0!44O)400IHH_d33M88NE22L((=&11K((=$//K
((5!

CXXh&&Fhhy!!G'3 #..""
((5%
 
 C	Br8 	BB 	 	 	BBB	 	
!B& OBh(8!TBCDDDDDs   H H)(H))z.jp2r   z.jpcz.jpfz.jpxz.j2cz	image/jp2)
__future__r   r+   r4   r$    r   r   r   r   r
   rX   r_   r   r   r   r   register_openr   register_saveregister_extensionsregister_mimer>   r   r   <module>r      s    # " " " " " 				 				  5 5 5 5 5 5 5 5 5 5 5 5G G G G G G G GT  <9 9 9B. B. B.Rg. g. g. g. g.i) g. g. g.T  >E >E >EJ  O*OW E E E  O*E 2 2 2  LLL    O*K 8 8 8 8 8r   