
    Ng                         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d.d
Z	 d/d0dZd1dZd1dZd2dZd3dZd4d Zd5d$Zd6d&Zd7d-ZdS )8    )annotationsN)AnyCallable
NamedTupleUnion)Image)TAGS_V2nintkreturnc                H    t          | |          \  }}|dk    r|dz  }||z  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mods       W/var/www/html/ai-engine/env/lib/python3.11/site-packages/pikepdf/models/_transcoding.py_next_multipler      s0     a||HC
Qwwq7N    packed	BytesLikesizetuple[int, int]bitsscaletuple[BytesLike, int]c                f   |\  }}d|z  }t          ||          }t          ||z  |z            }t          |          |z  }	|dk    rdd|z  dz
  z  }|dk    rt          | d|	         ||           n/|dk    rt	          | 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heightbits_per_bytestridebuffermax_reads
             r   unpack_subbyte_pixelsr.      s     ME6IME=11F}v-677F6{{m+Hzz41}%qyy		*FE::::			*FE:::: "$'''fv%%r   in_outMutableBytesLikeNonec                &   t          |           D ]\  }}t          |dz	  |z            |d|z  <   t          |dz	  dz  |z            |d|z  dz   <   t          |dz	  dz  |z            |d|z  dz   <   t          |dz  |z            |d|z  dz   <   dS )zUnpack 2-bit values to their 8-bit equivalents.

    Thus *out* must be 4x at long as *in*.

    Images of this type are quite rare in practice, so we don't
    optimize this loop.
       r!      r   r    N	enumerater   r/   r0   r   r
   vals        r   r%   r%   N   s     C.. 3 33#(e+,,AE
sax4/5899AEAIsax4/5899AEAIcDjE122AEAI	3 3r   c                    t          |           D ]>\  }}t          |dz	  |z            |d|z  <   t          |dz  |z            |d|z  dz   <   ?dS )zUnpack 4-bit values to their 8-bit equivalents.

    Thus *out* must be 2x at long as *in*.

    Images of this type are quite rare in practice, so we don't
    optimize this loop.
    r!   r       r   Nr6   r8   s        r   r$   r$   ]   si     C.. 5 53#(e+,,AE
cFle344AEAI5 5r   r,   r+   c           	     :    d}t          j        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+   ysteps       r   image_from_byte_bufferrA   j   s&     ECvuc65IIIr   gray_palettebytesc                @    d}| D ]}|t          |g          dz  z  }|S )Nr   r5   )rC   )rB   paletteentrys      r   _make_rgb_paletterG   t   s4    G & &5%>>A%%Nr   rE   c                    t          |           5 }t          dt          |          z            }t          |          D ]&\  }}|d|z  d|dz   z           |d|z  d|dz   z  <   '	 d d d            n# 1 swxY w Y   |S )Nr!   r   )r'   r"   r#   r7   )r,   rE   mvoutputr
   pal_idxs         r   _depalettize_cmykrL   {   s    	F		 Sr1s2ww;''#B-- 	S 	SJAw*1!g+Wq[@Q2Q*RF1q51A;&''	SS S S S S S S S S S S S S S S Ms   AA33A7:A7	base_modestrImage.Imagec                b   |dk    r)t          | ||          }|                    ||           n|dk    r8t          |          }t          | ||          }|                    |d           nA|dk    r)t          | |          }t	          j        d||d          }nt          d|           |S )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rawmoder=   CMYKr>   )datadecoder_namezpalette with )rA   
putpaletterG   rL   r   r?   r&   )r,   r   r+   rM   rE   imrB   rJ   s           r   image_from_buffer_and_paletterY      s    " E#FD&99
gy1111	c		 )11#FD&99
lE2222	f		"6733fdeLLL!"=)"="=>>>Ir   rX   c                   |                      d          } |dk    rSt          |          dk    r@d                    |dd         d|dd         g          }|                     |d           nm|d	k    rg	 |                     |d	           nN# t          $ rA}d
t          |          v r&t          |          }|                     |d           Y d}~nd}~ww xY w| S )zApply palettes to 1-bit images.PrQ   r4   r   r   r5   s                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            rR   r=   zunrecognized raw modeN)convertr#   joinrW   
ValueErrorrN   rG   )rX   rM   rE   expanded_paletteergb_palettes         r   fix_1bit_palette_imagerb      s    
CBEc'lla//88QqS\7'!A#,G
 
 	&6666	c			:MM'3M//// 	: 	: 	:&#a&&00/88k5999	: Is   6B 
C7CCdata_lengthccitt_group
t4_options
int | None
photometryiccc          	        dd t          j                    D             d G d dt                    g d%fd}d&d'fd}d| \  }}	 |d|            |d|	            |dd	            |d|            |dd	           | |d|            |d|            |dfd            |d|	            |d|           d|r |dfdt          |                      |t                              t          |          z   d  D             }
t	          j        d	d         t                    z  z   d!z   d"d#d$t                    g|
dR  }|r||z  }|S )(z=Generate binary CCITT header for image with given parameters.z<2sHLHc                $    i | ]\  }}|j         |S  )name).0keytags      r   
<dictcomp>z)generate_ccitt_header.<locals>.<dictcomp>   s     @@@(#s#@@@r   z<HHLLc                  8    e Zd ZU ded<   ded<   ded<   ded<   dS )	"generate_ccitt_header.<locals>.IFDr   rn   r   typecodecount_int | Callable[[], int | None]rU   N)__name__
__module____qualname____annotations__rk   r   r   IFDrr      s9         ,,,,,,r   rz   r   r   c                b    t          j                  t          j                  | z  z   dz   S )Nr!   )structcalcsize)	ifd_count
ifd_structtiff_header_structs    r   header_lengthz,generate_ccitt_header.<locals>.header_length   s5    O.//oj))I56	
r   r   tag_namerN   rU   ru   countc                ~    |          }t           |         j        }                     ||||                     d S N)	TIFF_TAGStypeappend)r   rU   r   rn   rs   rz   ifdstag_keyss        r   add_ifdz&generate_ccitt_header.<locals>.add_ifd   sA    x S>&CCXud3344444r   N
ImageWidthImageLengthBitsPerSampleCompression	FillOrder	T4OptionsPhotometricInterpretationStripOffsetsc                      S r   rk   )image_offsets   r   <lambda>z'generate_ccitt_header.<locals>.<lambda>   s    L r   RowsPerStripStripByteCountsr   
ICCProfilec                      S r   rk   )
icc_offsets   r   r   z'generate_ccitt_header.<locals>.<lambda>   s    j r   )r   c                N    g | ]"}|D ]}t          |          r
 |            n|#S rk   )callable)rm   ifdargs      r   
<listcomp>z)generate_ccitt_header.<locals>.<listcomp>   s:    QQQcSQQc(3--0SQQQQr   r=   s   II*   r   )r   r   )r   )r   rN   rU   ru   r   r   )r   itemsr   r#   r|   pack)r   rc   rd   re   rg   rh   r   r   r(   r)   ifd_argstiff_headerrz   r   r   r   r   r   r   s               @@@@@@@r   generate_ccitt_headerr      sk    6@@io.?.?@@@HJ- - - - -j - - - D
 
 
 
 
 
 
5 5 5 5 5 5 5 5 5
 LME6GL%   GM6"""GOQGM;'''GKZ(((G'444GN0000111GNF###G{+++J
 B0000CAAAAs4yy))JC(LQQTQQQH+	jns4yy8	83	>
	D		 
 	
  K  sr   )r
   r   r   r   r   r   )r   )
r   r   r   r   r   r   r   r   r   r   )r/   r   r0   r1   r   r   r   r2   )r,   r   r   r   r+   r   )rB   rC   r   rC   )r,   r   rE   r   )r,   r   r   r   r+   r   rM   rN   rE   r   r   rO   )rX   rO   rM   rN   rE   r   r   rO   )r   r   rc   r   rd   r   re   rf   rg   r   rh   rC   r   rC   )
__future__r   r|   typingr   r   r   r   PILr   PIL.TiffTagsr	   r   rC   r'   r   r"   r1   r   r.   r%   r$   rA   rG   rL   rY   rb   r   rk   r   r   <module>r      ss   # " " " " "  3 3 3 3 3 3 3 3 3 3 3 3       - - - - - -%#$	J./     GH& & & & &`3 3 3 3
5 
5 
5 
5J J J J             F   *E E E E E Er   