
    j5j                         d Z ddlmZ ddlZddlmZmZ ddlmZ  ej                  e
      ZddZdddd	 	 	 	 	 	 	 	 	 	 	 ddZdddd	 	 	 	 	 	 	 	 	 dd	Zej                  j                   d
d	 	 	 	 	 	 	 	 	 ddZy)zOCR-related image manipulation.    )annotationsN)floorsqrt)Imagec                :    | dv ry| dv s| j                  d      ryy)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    G/var/www/html/qr/venv/lib/python3.12/site-packages/ocrmypdf/imageops.pybytes_per_pixelr      s)     !!T__V%<    max_size
max_pixels	max_bytesc               $   | }||d   |d   z  |d   |d   z  f}t        |      }|dk  rpt        j                  d       t        |d   |z        t        |d   |z        f}|d   dk(  rdt        |d   |d         f}n|d   dk(  rt        |d   |d         df}|\|d   |d   z  |kD  rNt        j                  d       t	        ||d   |d   z  z        }t        |d   |z        t        |d   |z        f}||}	|d   |	z  }
|d   }|
|z  |kD  r~t        j                  d       t	        ||
|z  z        }t        |
|z        }t        ||z        }|dk(  r|	}t        ||	z  |      }|dk(  rd}t        ||z  |      }t        ||	z        |f}|S )ab  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$q'DG"3j"@		;<Z47T!W+<=>T!W},-uT!W}5L/MMa3aF?Y&IICD	Vf_ =>L!&<"78M!&<"78M! # #I$4m D! ! #I$> N,-}<DKr   c               \    t        | j                  t        | j                        |||      S )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,   Y   s-    & !



# r      )resample_modereducing_gapc               n   || j                   k(  r| S | j                   }| j                  d   }| j                  |||      } t        |d   |d   z  |d   z        t        |d   |d   z  |d   z        f| j                  d<   t        j                  d| j                    d| j                  d    d       | S )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   u   s    & 5::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    & "   g!
" (,! 999 %	9
 9 9 9~ (,!  % 	
  @ ',&6&6&>&>""" $	"
 " "r   