o
    Wf.                     @  s   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 dZ
dd Zd	d
 Zdd ZG dd dejZdd Zdd Zeejee eeje eejg d eejd dS )    )annotationsN   )Image	ImageFileImagePalette_binaryc                   @  sJ   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	d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                 C  s    || _ |dk| _|| _d| _d S )Nr   r	   )fp
has_lengthlengthremaining_in_box)selfr
   r    r   }/media/darth/New Volume E/PROJECTS/DRIVERZZ/DRIVERZZ_CUSTOMER_APIS/venv/lib/python3.10/site-packages/PIL/Jpeg2KImagePlugin.py__init__   s   

zBoxReader.__init__c                 C  s6   | j r| j | | jkrdS | jdkr|| jkS dS )NFr   T)r   r
   tellr   r   )r   	num_bytesr   r   r   	_can_read%   s
   

zBoxReader._can_readc                 C  sh   |  |sd}t|| j|}t||k r&d| dt| d}t|| jdkr2|  j|8  _|S )NzNot enough data in headerzExpected to read z bytes but only got .r   )r   SyntaxErrorr
   readlenOSErrorr   )r   r   msgdatar   r   r   _read_bytes/   s   

zBoxReader._read_bytesc                 C  s    t |}| |}t ||S N)structcalcsizer   unpack)r   field_formatsizer   r   r   r   read_fields=   s   

zBoxReader.read_fieldsc                 C  s    | j }| |}tt||S r   )r   r   r   ioBytesIO)r   r"   r   r   r   r   
read_boxesB   s   
zBoxReader.read_boxesc                 C  s    | j r| j | j | jk S dS )NT)r   r
   r   r   r   r   r   r   r   has_next_boxG   s   zBoxReader.has_next_boxc                 C  s   | j dkr| j| j tj d| _ | d\}}|dkr&| dd }d}nd}||k s3| || s9d}t||| | _ |S )	Nr   r	   z>I4sr   z>Q      zInvalid header length)r   r
   seekosSEEK_CURr#   r   r   )r   lboxtboxhlenr   r   r   r   next_box_typeM   s   

zBoxReader.next_box_typeN)r	   )__name__
__module____qualname____doc__r   r   r   r#   r&   r(   r1   r   r   r   r   r      s    

r   c                 C  s   |  d}t|}||  |d  }td|\}}}}}}}	}	}	}	}
|| || f}|
dkrMtd|d}|d d@ d dkrGd	}||fS d
}||fS |
dkrWd}||fS |
dkrad}||fS |
dkrkd}||fS 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      r*   I;16LLA   RGB   RGBAN)r   r   i16ber   unpack_from)r
   hdrlsizsizrsizxsizysizxosizyosiz_csizr"   ssizmoder   r   r   _parse_codestreamc   s2   


rN   c                 C  s$   |dkrd|  d|  d|  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expr   r   r   _res_to_dpi   s   rT   c                 C  s  t | }d}d}| r,| }|dkr| }n|dkr(|dd dkr(d}| sd}d}d}d}d}	d}
| r| }|dkrz|d	\}}}}||f}|d
kr^|d@ dkr^d}n|d
kred}n|dkrld}n|dkrsd}n|dkryd}nz|dkr|dv r|d\}}|dd|  }t|dkrt }
t|D ]}|
|dd|   q|dkrdnd}n<|dkr| }| r| }|dkr|d\}}}}}}t	|||}t	|||}|dur|dur||f}	n| s| s<|du s|du r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   r8   r*   r9   r:   r6   r;   r<   r=   r>   r?   s   pclr)r:   r;   z>HB>BPPAs   res s   rescz>HHHHBBzMalformed JP2 header)r   r(   r1   r&   r#   maxr   rangegetcolorrT   r   )r
   readerheadermimetyper/   r"   rM   bpcncdpipaletteheightwidthnenpc	bitdepthsirestresvrcnvrcdhrcnhrcdvrcehrcehresvresr   r   r   r   _parse_jp2_header   sx   
$rs   c                      sN   e Zd ZdZdZdd Zdd Ze fddZej	d	d Zd
d Z
  ZS )Jpeg2KImageFileJPEG2000zJPEG 2000 (ISO 15444)c                 C  s  | j d}|dkrd| _t| j \| _| _n<|| j d }|dkrMd| _t| j }|\| _| _| _}| _|d ur?|| j	d< | j d
d	rL|   nd
}t|| jd u s]| jd u rcd}t|d| _d| _d}d}z| j  }t|j}W n1 ty   d}z| j  }| j dtj | j  }| j | W n ty   d}Y nw Y nw dd| j d| j| j| j||ffg| _d S )Nr>      OQj2kr*         jP  

jp2ra      s   jp2cOQznot a JPEG 2000 filezunable to determine size/moder   r	   jpeg2kr   r   )r
   r   codecrN   _size_moders   custom_mimetyperb   infoendswith_parse_commentr   r"   rM   _reducelayersfilenor,   fstatst_size	Exceptionr   r+   r$   SEEK_ENDtile)r   sigr]   ra   r   fdr   posr   r   r   _open   sX   





zJpeg2KImageFile._openc                 C  s   | j d}t|}| j |d tj 	 | j d}|s d S |d }|dv r*d S | j d}t|}|dkrJ| j |d dd  | jd< d S | j |d tj q)Nr6   Tr   )      d   comment)r
   r   r   r@   r+   r,   r-   r   )r   rB   r   markertypr   r   r   r   
  s"   

zJpeg2KImageFile._parse_commentc                   s   | j pt jS r   )r   superreducer'   	__class__r   r   r      s   zJpeg2KImageFile.reducec                 C  s
   || _ d S r   )r   )r   valuer   r   r   r   '  s   
c                 C  s   | j rO| jrOd| j> }|d? }t| jd | | t| jd | | f| _| j d }|d d | j| j|d d |d d f}|d d| j |d |fg| _ tj| S )Nr   r   r<   r>   r|   r6   )r   r   intr"   r~   r   r   load)r   poweradjusttt3r   r   r   r   +  s   

*zJpeg2KImageFile.load)r2   r3   r4   formatformat_descriptionr   r   propertyr   setterr   __classcell__r   r   r   r   rt      s    4
rt   c                 C  s    | d d dkp| d d dkS )Nr>   rv   rz   rx   r   )prefixr   r   r   _accept<  s   r   c                 C  s  | j }|ds|ddrd}nd}|dd }|dd }|dd }|d	d
}|dd }	|	d urKt|	ttfrEtdd |	D sKd}
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rz| }W n ty   d}Y nw |||||	|||||||||||f| _t| |dd| j d|fg d S ) N.j2kno_jp2Frw   ry   offsettile_offset	tile_sizequality_moderatesquality_layersc                 s  s    | ]
}t |ttfV  qd S r   )
isinstancer   float).0quality_layerr   r   r   	<genexpr>W  s    
z_save.<locals>.<genexpr>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   r   r   r   r   G  sn   


"r   )z.jp2r   z.jpcz.jpfz.jpxz.j2cz	image/jp2)
__future__r   r$   r,   r    r   r   r   r   r   rN   rT   rs   rt   r   r   register_openr   register_saveregister_extensionsregister_mimer   r   r   r   <module>   s$   JIjE
