
    OAi#                    6   S SK Jr  S SKrS SKJr  S SKJrJr  S SKJ	r	  S SK
Jr   " S S\5      r\\-  r\\-  rSS	 jr S         SS
 jjrSS jrSS jrSS jrSS jrSS jr            SS jr        SS jr              SS jrg)    )annotationsN)Callable)Any
NamedTuple)Image)TAGS_V2c                      \ rS rSrSrSrg)ImageDecompressionError   zImage decompression error. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       W/var/www/html/land-ocr/venv/lib/python3.13/site-packages/pikepdf/models/_transcoding.pyr
   r
      s    $r   r
   c                :    [        X5      u  p#US:  a  US-  nX!-  $ )zyReturn the multiple of k that is greater than or equal n.

>>> _next_multiple(101, 4)
104
>>> _next_multiple(100, 4)
100
r      )divmod)nkdivmods       r   _next_multipler      s&     a|HC
Qwq7Nr   c                   Uu  pESU-  n[        XF5      n[        Xg-  U-  5      n[        U5      U-  n	US:X  a  SSU-  S-
  -  nUS:X  a  [        U SU	 X5        O!US:X  a  [	        U SU	 X5        O[        U5      e[        U5      U4$ )aS  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)
packedsizebitsscalewidthheightbits_per_bytestridebuffermax_reads
             r   unpack_subbyte_pixelsr2   $   s     MEIME1F}-67F6{m+Hz41}%qy	*F:		*F: "$''fv%%r   c                   [        U 5       Hp  u  p4[        US-	  U-  5      USU-  '   [        US-	  S-  U-  5      USU-  S-   '   [        US-	  S-  U-  5      USU-  S-   '   [        US-  U-  5      USU-  S-   '   Mr     g)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	enumerateintin_outr+   r   vals        r   r%   r%   T   s     C.#(e+,AE
sax4/589AEAIsax4/589AEAIcDjE12AEAI	 !r   c                    [        U 5       H6  u  p4[        US-	  U-  5      USU-  '   [        US-  U-  5      USU-  S-   '   M8     g)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   r9   s        r   r$   r$   c   sN     C.#(e+,AE
cFle34AEAI !r   c           
     6   Sn [         R                  " SXSSX#5      $ ! [         ap  nS[        U5      ;   aQ   [         R                  " SXSSUS   S-   S-  U5      s SnA$ ! [         a  n[	        [        U5      5      UeSnAff = f[	        5       UeSnAff = f)	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rawzbuffer is not large enoughr   r5   r!   N)r   
frombuffer
ValueErrorstrr
   )r0   r)   r/   ystepee2s         r   image_from_byte_bufferrH   p   s     E3T5#vMM 3'3q61?''ucDGaKA3Eu   ?-c"g6B>? *+23s7    
BB#A!B!
B+B  BBBc                @    SnU  H  nU[        U/5      S-  -  nM     U$ )Nr   r5   )bytes)gray_palettepaletteentrys      r   _make_rgb_paletterN      s+    G5%>A%% Nr   c                    [        U 5       n[        S[        U5      -  5      n[        U5       H  u  pEUSU-  SUS-   -   USU-  SUS-   -  & M!     S S S 5        U$ ! , (       d  f       W$ = f)Nr!   r   )r'   r"   r#   r7   )r0   rL   mvoutputr   pal_idxs         r   _depalettize_cmykrS      sv    	F	r1s2w;'#B-JA*1!g+Wq[@Q*RF1q51A;' ( 
 M	 
	 Ms   AA
A+c                   US:X  a  [        XU5      nUR                  XCS9  U$ US:X  a)  [        U5      n[        XU5      nUR                  USS9  U$ US:X  a#  [        X5      n[        R
                  " SXSS9nU$ [        SU 35      e)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@   CMYKrA   )datadecoder_namezpalette with )rH   
putpaletterN   rS   r   rB   r&   )r0   r)   r/   	base_moderL   imrK   rQ   s           r   image_from_buffer_and_paletter^      s     E#F&9
g1 I 
c	 )1#F&9
lE2 I 
f	"63fdeL I "M)"=>>r   c                `   U R                  S5      n US:X  a;  [        U5      S:X  a,  SR                  USS SUSS /5      nU R                  USS9  U $ US	:X  a   U R                  US	S9  U $ U $ ! [         a5  nS
[        U5      ;   a  [        U5      nU R                  USS9   SnAU $ SnAff = f)zApply palettes to 1-bit images.PrU   r4   r   r   r5   s                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            rV   r@   zunrecognized raw modeN)convertr#   joinr[   rC   rD   rN   )r]   r\   rL   expanded_paletterF   rgb_palettes         r   fix_1bit_palette_imagere      s     
CBEc'la/88Qq\7'!A,G
 	&6 I 
c		:MM'3M/
 I2I	  	:&#a&0/8k59I		:s   A. .
B-8*B((B-c          	     &  ^^^^^^^ Sm[         R                  " 5        VVs0 s H  u  pgUR                  U_M     snnmSm " S S[        5      m/ mSUU4S jjnSSUUU4S jjjn	SmU u  pU	" S	U
5        U	" S
U5        U	" SS5        U	" SU5        U	" SS5        Ub	  U	" SU5        U	" SU5        U	" SU4S j5        U	" SU5        U	" SU5        SmU(       a  U	" SU4S j[	        U5      S9  U" [	        T5      5      mT[	        U5      -   mT VVs/ s H%  o  H  n[        U5      (       a  U" 5       OUPM     M'     nnn[        R                  " TTSS [	        T5      -  -   S-   SSS[	        T5      /UQSP76 nU(       a  X-  nU$ s  snnf s  snnf )z=Generate binary CCITT header for image with given parameters.z<2sHLHz<HHLLc                  >    \ rS rSr% S\S'   S\S'   S\S'   S\S'   S	rg
)"generate_ccitt_header.<locals>.IFD   r8   keyr   typecodecount_int | Callable[[], int | None]rY   r   N)r   r   r   r   __annotations__r   r   r   r   IFDrh      s    ,,r   ro   c                j   > [         R                  " T5      [         R                  " T5      U -  -   S-   $ )Nr!   )structcalcsize)	ifd_count
ifd_structtiff_header_structs    r   header_length,generate_ccitt_header.<locals>.header_length   s3    OO./ooj)I56	
r   r   c                f   > TU    n[         U   R                  nTR                  T" X4X!5      5        g N)	TIFF_TAGStypeappend)tag_namerY   countrj   rk   ro   ifdstag_keyss        r   add_ifd&generate_ccitt_header.<locals>.add_ifd   s/    x S>&&Cu34r   N
ImageWidthImageLengthBitsPerSampleCompression	FillOrder	T4OptionsPhotometricInterpretationStripOffsetsc                    > T $ ry   r   )image_offsets   r   <lambda>'generate_ccitt_header.<locals>.<lambda>   s    Lr   RowsPerStripStripByteCountsr   
ICCProfilec                    > T $ ry   r   )
icc_offsets   r   r   r      s    jr   )r~   r@   s   II*   r   )returnr8   )r   )r}   rD   rY   rm   r~   r8   )rz   itemsnamer   r#   callablerq   pack)r)   data_lengthccitt_group
t4_options
photometryiccrj   tagrv   r   r,   r-   ifdargifd_argstiff_headerro   r   rt   r   r   r   ru   s                   @@@@@@@r   generate_ccitt_headerr      s    6.7oo.?@.?(##.?@HJ-j - D
 
5 5
 LMEL% M6"OQM;'KZ('4N01NF#{+J
0CAs4y)JC(L=AQTcSc(3--S0S0THQ++	jns4y8	83	>
	D	 
 	
K s AX Rs   F,F)r   r8   r   r8   r   r8   )r   )
r(   	BytesLiker)   tuple[int, int]r*   r8   r+   r8   r   ztuple[BytesLike, int])r:   r   r;   MutableBytesLiker+   r8   r   None)r0   r   r)   r   r/   r8   )rK   rJ   r   rJ   )r0   r   rL   r   )r0   r   r)   r   r/   r8   r\   rD   rL   r   r   Image.Image)r]   r   r\   rD   rL   r   r   r   )r)   r   r   r8   r   r8   r   z
int | Noner   r8   r   rJ   r   rJ   )
__future__r   rq   collections.abcr   typingr   r   PILr   PIL.TiffTagsr   rz   	Exceptionr
   rJ   r'   r   r"   r   r   r2   r%   r$   rH   rN   rS   r^   re   r   r   r   r   <module>r      sB   #  $ "  -%i % J	z)  GH&&,&47&@C&&`3
534
  	
  < #.7*E
E E 	E
 E E 
E Er   