o
    ei                      @  s   d dl mZ d dlZd dlmZmZmZmZ d dlm	Z	 d dl
mZ eeef Zeeef Zd6d
dZ	 d7d8ddZd9ddZd9ddZd:dd Zd;d#d$Zd<d&d'Zd=d+d,Zd>d.d/Zd?d4d5ZdS )@    )annotationsN)AnyCallable
NamedTupleUnion)Image)TAGS_V2nintkreturnc                 C  s&   t | |\}}|dkr|d7 }|| S )zReturn the multiple of k that is greater than or equal n.

    >>> _next_multiple(101, 4)
    104
    >>> _next_multiple(100, 4)
    100
    r      )divmod)r	   r   divmod r   KD:\Projects\ConvertPro\env\Lib\site-packages\pikepdf/models/_transcoding.py_next_multiple   s   r   packed	BytesLikesizetuple[int, int]bitsscaletuple[BytesLike, int]c           
      C  s   |\}}d| }t ||}t|| | }t|| }	|dkr'dd| d  }|dkr6t| d|	 || n|dkrEt| d|	 || nt|t||fS )aw  Unpack subbyte *bits* pixels into full bytes and rescale.

    When scale is 0, the appropriate scale is calculated.
    e.g. for 2-bit, the scale is adjusted so that
        0b00 = 0.00 = 0x00
        0b01 = 0.33 = 0x55
        0b10 = 0.66 = 0xaa
        0b11 = 1.00 = 0xff
    When scale is 1, no scaling is applied, appropriate when
    the bytes are palette indexes.
       r         r      N)r   	bytearraylen_4bit_inner_loop_2bit_inner_loopNotImplementedError
memoryview)
r   r   r   r   widthheightZbits_per_bytestridebufferZmax_readr   r   r   unpack_subbyte_pixels   s   
r)   in_outMutableBytesLikeNonec                 C  s   t | D ]>\}}t|d? | |d| < t|d? d@ | |d| d < t|d? d@ | |d| d < t|d@ | |d| d < qdS )z`Unpack 2-bit values to their 8-bit equivalents.

    Thus *out* must be 4x at long as *in*.
       r      r   r   N	enumerater
   r*   r+   r   r	   valr   r   r   r"   N   s     r"   c                 C  sJ   t | D ]\}}t|d? | |d| < t|d@ | |d| d < qdS )z`Unpack 4-bit values to their 8-bit equivalents.

    Thus *out* must be 2x at long as *in*.
    r   r      r   Nr0   r2   r   r   r   r!   Z   s   r!   r(   r'   c              	   C  s   d}t d|| dd||S )zUse Pillow to create one-component image from a byte buffer.

    *stride* is the number of bytes per row, and is essential for packed bits
    with odd image widths.
    r   Lraw)r   
frombuffer)r(   r   r'   Zystepr   r   r   image_from_byte_bufferd   s   r8   gray_palettebytesc                 C  s$   d}| D ]}|t |gd 7 }q|S )N    r/   )r:   )r9   paletteentryr   r   r   _make_rgb_paletten   s   r>   r<   c                 C  s|   t | 0}tdt| }t|D ]\}}|d| d|d   |d| d|d  < qW d    |S 1 s7w   Y  |S )Nr   r   )r$   r   r    r1   )r(   r<   mvoutputr	   Zpal_idxr   r   r   _depalettize_cmyku   s   
.
rA   	base_modestrImage.Imagec                 C  s   |dkrt | ||}|j||d |S |dkr*t|}t | ||}|j|dd |S |dkr>t| |}tjd||dd}|S td| )zConstruct an image from a byte buffer and apply the palette.

    1/2/4-bit images must be unpacked (no scaling!) to byte buffers first, such
    that every 8-bit integer is an index into the palette.
    RGBrawmoder5   ZCMYKr6   )dataZdecoder_namezpalette with )r8   
putpaletter>   rA   r   r7   r#   )r(   r   r'   rB   r<   imr9   r@   r   r   r   image_from_buffer_and_palette}   s   
rK   rJ   c              
   C  s   |  d} |dkr&t|dkr&|dd d |dd  }| j|dd | S |dkr`z
| j|dd W | S  ty_ } zd	t|v rTt|}| j|dd W Y d
}~| S W Y d
}~| S d
}~ww | S )zApply palettes to 1-bit images.PrE   r.   r   r/   s                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            rF   r5   zunrecognized raw modeN)convertr    rI   
ValueErrorrC   r>   )rJ   rB   r<   eZrgb_paletter   r   r   fix_1bit_palette_image   s&   

rP   data_lengthccitt_group
photometryiccc                   sL  ddd t  D dG dd dt g d(fd	d
}d)d* fdd}d| \}}|d| |d| |dd |d| |dt| |dfdd |d| |d| d|rn|dfd dt|d! |tt| d"d# D }	tjdd t  d$ d%d&d'tg|	dR  }
|r|
|7 }
|
S )+z=Generate binary CCITT header for image with given parameters.z<2sHLHc                 S  s   i | ]\}}|j |qS r   )name).0keytagr   r   r   
<dictcomp>   s    z)generate_ccitt_header.<locals>.<dictcomp>z<HHLLc                   @  s.   e Zd ZU ded< ded< ded< ded< dS )	z"generate_ccitt_header.<locals>.IFDr
   rW   r   typecodeZcount_int | Callable[[], int | None]rH   N)__name__
__module____qualname____annotations__r   r   r   r   IFD   s
   
 r`   r   r
   c                   s   t t  |   d S )Nr   )structcalcsize)Z	ifd_count)
ifd_structtiff_header_structr   r   header_length   s   z,generate_ccitt_header.<locals>.header_lengthr   tag_namerC   rH   r[   countc                   s*   |  }t | j} |||| d S N)	TIFF_TAGStypeappend)rf   rH   rg   rW   rZ   )r`   ifdstag_keysr   r   add_ifd   s   
z&generate_ccitt_header.<locals>.add_ifdNZ
ImageWidthZImageLengthZBitsPerSampleZCompressionZPhotometricInterpretationZStripOffsetsc                         S rh   r   r   )image_offsetr   r   <lambda>       z'generate_ccitt_header.<locals>.<lambda>ZRowsPerStripZStripByteCountsr   Z
ICCProfilec                     ro   rh   r   r   )
icc_offsetr   r   rq      rr   )rg   c                 S  s(   g | ]}|D ]}t |r| n|qqS r   )callable)rV   Zifdargr   r   r   
<listcomp>   s   ( z)generate_ccitt_header.<locals>.<listcomp>r5   s   II*   r   )r   r
   )r   )rf   rC   rH   r[   rg   r
   )ri   itemsr   r
   r    ra   pack)r   rQ   rR   rS   rT   re   rn   r%   r&   Zifd_argsZtiff_headerr   )r`   rs   rc   rl   rp   rm   rd   r   generate_ccitt_header   sJ   






rz   )r	   r
   r   r
   r   r
   )r   )
r   r   r   r   r   r
   r   r
   r   r   )r*   r   r+   r,   r   r
   r   r-   )r(   r   r   r   r'   r
   )r9   r:   r   r:   )r(   r   r<   r   )r(   r   r   r   r'   r
   rB   rC   r<   r   r   rD   )rJ   rD   rB   rC   r<   r   r   rD   )r   r   rQ   r
   rR   r
   rS   r
   rT   r:   r   r:   )
__future__r   ra   typingr   r   r   r   ZPILr   ZPIL.TiffTagsr   ri   r:   r$   r   r   r,   r   r)   r"   r!   r8   r>   rA   rK   rP   rz   r   r   r   r   <module>   s$   

0







#