
    TAi                       S r SSKJr  SSKrSSKJrJr  SSKJr  \R                  " \
5      rSS jrSSSS.           SS jjrSSSS.         SS	 jjr\R                  R                   S
S.         SS jjrg)zOCR-related image manipulation.    )annotationsN)floorsqrt)Imagec                L    U S;   a  gU S;   d  U R                  S5      (       a  gg)zReturn the number of padded bytes per pixel for a given PIL image mode.

In RGB mode we assume 4 bytes per pixel, which is the case for most
consumers.
)1LP   )LAPALazI;16      )
startswith)modes    M/var/www/html/land-ocr/venv/lib/python3.13/site-packages/ocrmypdf/imageops.pybytes_per_pixelr      s,     !!T__V%<%<    max_size
max_pixels	max_bytesc               (   U nUb  US   US   -  US   US   -  4n[        U5      nUS:  ar  [        R                  S5        [        US   U-  5      [        US   U-  5      4nUS   S:X  a  S[        US   US   5      4nOUS   S:X  a  [        US   US   5      S4nUb\  US   US   -  U:  aM  [        R                  S5        [	        X5S   US   -  -  5      n[        US   U-  5      [        US   U-  5      4nUb  Un	US   U	-  n
US   nX-  U:  az  [        R                  S5        [	        XJU-  -  5      n[        X-  5      n[        X-  5      nUS:X  a  U	n[        XI-  U5      nUS:X  a  Sn[        XN-  U5      n[        X-  5      U4nU$ )a:  Calculate image size required to downsample an image to fit limits.

If no limit is exceeded, the input image's size is returned.

Args:
    image_size: Dimensions of image.
    bytes_per_pixel: Number of bytes per pixel.
    max_size: The maximum width and height of the image.
    max_pixels: The maximum number of pixels in the image. Some image consumers
        limit the total number of pixels as some value other than width*height.
    max_bytes: The maximum number of bytes in the image. RGB is counted as 4
        bytes; all other modes are counted as 1 byte.
r   r   g      ?z,Resizing image to fit image dimensions limitz'Resizing image to fit image pixel limitz+Resizing image to fit image byte size limit)minlogdebugr   r   )
image_sizer   r   r   r   sizeoveragesize_factorpixels_factorbppstrideheightbytes_factorscaled_stridescaled_heights                  r   _calculate_downsampler)      s   * D1+Q'!tAw)>>'lIIDEa;./tAw7L1MMDAw!|#d1gx{33aA47HQK0!37T!Wz)II?@ Awa/@!ABMa=015a=9P3QQDa3a?Y&IICD	f_ =>L!&"78M!&"78M! # #I$4m D! ! #I$> N,-}<DKr   c               V    [        U R                  [        U R                  5      UUUS9$ )a	  Calculate image size required to downsample an image to fit limits.

If no limit is exceeded, the input image's size is returned.

Args:
    image: The image to downsample.
    max_size: The maximum width and height of the image.
    max_pixels: The maximum number of pixels in the image. Some image consumers
        limit the total number of pixels as some value other than width*height.
    max_bytes: The maximum number of bytes in the image. RGB is counted as 4
        bytes; all other modes are counted as 1 byte.
r   )r)   r   r   r   )imager   r   r   s       r   calculate_downsampler,   Z   s-    & !



# r      )resample_modereducing_gapc               h   XR                   :X  a  U $ U R                   nU R                  S   nU R                  UUUS9n [        US   US   -  US   -  5      [        US   US   -  US   -  5      4U R                  S'   [        R                  SU R                    SU R                  S    S35        U $ )a  Downsample an image to fit within the given limits.

The DPI is adjusted to match the new size, which is how we can ensure the
OCR is positioned correctly.

Args:
    image: The image to downsample
    new_size: The new size of the image.
    resample_mode: The resampling mode to use when downsampling.
    reducing_gap: The reducing gap to use when downsampling (for larger
        reductions).
dpi)resampler/   r   r   zRescaled image to z pixels and z dpi)r   inforesizeroundr   r   )r+   new_sizer.   r/   original_sizeoriginal_dpis         r   downsample_imager9   v   s    & ::JJM::e$LLL!  E 	l1o+mA.>>?l1o+mA.>>?EJJu II"5::,l5::e;L:MTRSLr   )r   strreturnint)r   tuple[int, int]r   r<   r   tuple[int, int] | Noner   
int | Noner   r?   r;   r=   )
r+   Image.Imager   r>   r   r?   r   r?   r;   r=   )
r+   r@   r6   r=   r.   zImage.Resamplingr/   r<   r;   r@   )__doc__
__future__r   loggingmathr   r   PILr   	getLogger__name__r   r   r)   r,   
ResamplingBICUBICr9    r   r   <module>rK      s    & "   !
" (,! ::: %	:
 : : :@ (,!  % 	
  @ ',&6&6&>&>""" $	"
 " "r   