o
    i                     @   s  d Z ddlmZmZmZmZmZ ddlZddlmZ ed Z	ed Z
eeeee ee eef Zdedefd	d
Zdededeeddf fddZde
defddZde
defddZdedede	defddZdedede
de	de	defddZddddde
d ee de	d!ed"edefd#d$Zd%dddd ee ded& d!ed"efd'd(Zd%dddd ee ded) d!ed"efd*d+Z		d0d,ed-ededefd.d/ZdS )1a{  This file contains useful layout utilities for images. They are:

- add_border: Add a border to an image.
- cat/hcat/vcat: Join images by arranging them in a line. If the images have different
  sizes, they are aligned as specified (start, end, center). Allows you to specify a gap
  between images.

Images are assumed to be float32 tensors with shape (channel, height, width).
    )Any	GeneratorIterableLiteralUnionN)Tensorstartcenterend
horizontalverticalcolorreturnc                 C   s>   t | tjr
|  } t | trt| } n| g} tj| tjdS )N)dtype)
isinstancetorchr   tolistr   listtensorfloat32)r    r   C/data/cameron/da3_repo/src/depth_anything_3/utils/layout_helpers.py_sanitize_color)   s   

r   iterable	delimiterc                 c   s.    t | }t|V  |D ]}|V  |V  qd S N)iternext)r   r   ititemr   r   r   _intersperse7   s   
r"   	main_axisc                 C      ddd|  S )N      r   r   r#   r   r   r   _get_main_dim?   
   r(   c                 C   r$   )Nr&   r%   r   r   r'   r   r   r   _get_cross_dimF   r)   r*   baseoverlayalignc                 C   s6   | |ksJ d| | d | | d| }t ||| S )Nr   r%   r   )slice)r+   r,   r-   offsetr   r   r   _compute_offsetM   s   
r0   main_axis_alignmentcross_axis_alignmentc                 C   s   | j \}}}|j \}}}	||kr||	ksJ t|}
t| j |
 |j |
 |}t|}t| j | |j | |}g d}|||
< |||< |  }|||< |S )N).NN)shaper(   r0   r*   clone)r+   r,   r#   r1   r2   _base_heightZ
base_widthZoverlay_heightZoverlay_widthZmain_dimZ
main_slice	cross_dimZcross_sliceselectorresultr   r   r   r,   W   s"   r
      r&   r-   gap	gap_colorimagesr<   r=   c             	      s  |d j }t||}t|  t fdd|D }g }|D ]*}t|j}	||	 < tj|	tj	|d}
|
|ddddf  }
|
t|
|| d| q|dkr|d j\}}}||g}|| d < tj|g|R tj	|d}||ddddf  }tt||}tj|t| dS )	zIArrange images in a line. The interface resembles a CSS div with flexbox.r   c                 3   s    | ]}|j   V  qd S r   )r3   ).0imager7   r   r   	<genexpr>   s    zcat.<locals>.<genexpr>r   deviceNr	   r&   )dim)rD   r   tor*   maxr   r3   r   onesr   appendr,   r"   catr(   )r#   r-   r<   r=   r>   rD   Zcross_axis_lengthZpadded_imagesr@   padded_shaper+   cr5   separator_size	separatorr   rA   r   rJ   x   s&   

rJ   r	   r	   r
   r   topbottomc                 G   *   t dg|R dddddd|  ||dS )0Shorthand for a horizontal linear concatenation.r   r	   r
   r   rO   r;   rJ   r-   r<   r=   r>   r   r   r   hcat       rV   r	   r
   r   leftrightc                 G   rR   )rS   r   r	   r
   r   rX   r;   rT   rU   r   r   r   vcat   rW   r[   r@   borderc                 C   s   t || }| j\}}}tj||d|  |d|  ftj| jd}|d d d d f |d d < | |d d ||| ||| f< |S )Nr%   rC   )r   rF   r3   r   emptyr   rD   )r@   r\   r   rL   hwr9   r   r   r   
add_border   s    "r`   )r:   r&   )__doc__typingr   r   r   r   r   r   r   Z	AlignmentAxisintfloatColorr   r"   r(   r*   r.   r0   r,   rJ   rV   r[   r`   r   r   r   r   <module>   s   

 

$
*

