o
    :ήc                     @   sN   d dl Zd dlZd dlmZ eddZeddZ					
		dddZ	dS )    N)div_round_upa  
extern "C" __global__
void resize_nearest(
    float *dst,
    const float *src,
    int w_src, int h_src,
    int w_dst, int h_dst,
    int depth
){
    int x_dst = blockDim.x * blockIdx.x + threadIdx.x;
    int y_dst = blockDim.y * blockIdx.y + threadIdx.y;

    if (x_dst >= w_dst || y_dst >= h_dst) return;

    int x_src = min(x_dst * w_src / w_dst, w_src - 1);
    int y_src = min(y_dst * h_src / h_dst, h_src - 1);

          float *ptr_dst = dst + (x_dst + y_dst * w_dst) * depth;
    const float *ptr_src = src + (x_src + y_src * w_src) * depth;

    for (int channel = 0; channel < depth; channel++){
        ptr_dst[channel] = ptr_src[channel];
    }
}
resize_nearestaW  
extern "C" __global__
void ml_iteration(
          float *F,
          float *B,
    const float *F_prev,
    const float *B_prev,
    const float *image,
    const float *alpha,
    int w,
    int h,
    float regularization
){
    int x = blockDim.x * blockIdx.x + threadIdx.x;
    int y = blockDim.y * blockIdx.y + threadIdx.y;

    int i = x + y * w;

    if (x >= w || y >= h) return;

    float a0 = alpha[i];
    float a1 = 1.0f - a0;

    float b00 = a0 * image[i * 3 + 0];
    float b01 = a0 * image[i * 3 + 1];
    float b02 = a0 * image[i * 3 + 2];

    float b10 = a1 * image[i * 3 + 0];
    float b11 = a1 * image[i * 3 + 1];
    float b12 = a1 * image[i * 3 + 2];

    int js[4] = {
        max(    0, x - 1) + y * w,
        min(w - 1, x + 1) + y * w,
        x + max(    0, y - 1) * w,
        x + min(h - 1, y + 1) * w,
    };

    float a_sum = 0.0f;

    for (int d = 0; d < 4; d++){
        int j = js[d];

        float da = regularization + fabsf(a0 - alpha[j]);

        a_sum += da;

        b00 += da * F_prev[j * 3 + 0];
        b01 += da * F_prev[j * 3 + 1];
        b02 += da * F_prev[j * 3 + 2];

        b10 += da * B_prev[j * 3 + 0];
        b11 += da * B_prev[j * 3 + 1];
        b12 += da * B_prev[j * 3 + 2];
    }

    float a00 = a0 * a0 + a_sum;
    float a11 = a1 * a1 + a_sum;
    float a01 = a0 * a1;

    float inv_det = 1.0f / (a00 * a11 - a01 * a01);

    F[i * 3 + 0] = fmaxf(0.0f, fminf(1.0f, inv_det * (a11 * b00 - a01 * b10)));
    F[i * 3 + 1] = fmaxf(0.0f, fminf(1.0f, inv_det * (a11 * b01 - a01 * b11)));
    F[i * 3 + 2] = fmaxf(0.0f, fminf(1.0f, inv_det * (a11 * b02 - a01 * b12)));

    B[i * 3 + 0] = fmaxf(0.0f, fminf(1.0f, inv_det * (a00 * b10 - a01 * b00)));
    B[i * 3 + 1] = fmaxf(0.0f, fminf(1.0f, inv_det * (a00 * b11 - a01 * b01)));
    B[i * 3 + 2] = fmaxf(0.0f, fminf(1.0f, inv_det * (a00 * b12 - a01 * b02)));
}
ml_iterationh㈵>
          r   r   Fc                    s>  | j \}}	}
|
dksJ ||	 |
 }t| tj } t|tj }tj|tjd}tj|tjd}tj|tjd}tj|tjd}tj|tjd}tj||	 tjd}t|	|d 	 }d}d} fdd}||| |	||||
 ||| |	||||
 t
|d D ]z}t|	||  }t|||  }||| |	||||
 ||||	|||d ||||||||
 ||||||||
 |}t|||kr|}t| d t| d f}t
|D ]}t| ||||||||t|f	 ||}}||}}q|}|}qt|||	|
}t|||	|
}|r||fS |S )z=See the :code:`estimate_foreground` method for documentation.   )dtype   c              
      s:   t | d t | d f}t| | ||||||f d S )Nr   r   )r   _resize_nearest)dstsrcw_srch_srcw_dsth_dstdepth	grid_size
block_size W/tmp/pip-target-vg8gfxp4/lib/python/pymatting/foreground/estimate_foreground_ml_cupy.pyr      s   z3estimate_foreground_ml_cupy.<locals>.resize_nearestr   )shapecpasarrayastypenpfloat32flattenzerosmax
bit_lengthrangeroundminr   r   asnumpyreshape)input_imageinput_alpharegularizationn_small_iterationsn_big_iterations
small_sizer   return_backgroundh0w0r   nF_prevB_prevFBimage_levelalpha_leveln_levelsw_prevh_prevr   i_levelwhn_iterr   i_iterF_hostB_hostr   r   r   estimate_foreground_ml_cupyn   sh   
rC   )r   r   r   r   r	   F)
numpyr   cupyr   pymatting.util.utilr   	RawKernelr   r   rC   r   r   r   r   <module>   s$    GO