function  [ext_patch,ext_rain_patch, par]  =  Patch_Extension(index, rain, row, par)


if mod(index,row-par.patsize+1)~=0
    ori_top_bound   =  mod(index,row-par.patsize+1);
else
    ori_top_bound   =  row-par.patsize+1;
end
ori_down_bound  =  ori_top_bound + par.patsize - 1;

ori_left_bound  =  ceil(index/(row-par.patsize+1));

ori_right_bound =  ceil(index/(row-par.patsize+1)) + par.patsize - 1;
initial_points  =  [ori_left_bound ori_right_bound;ori_top_bound ori_down_bound] ;
%% Extend the patch
expand_rate     =  0.8;                                                                   % empirical parameter
ext_left_bound  =  ceil(max(initial_points(1, 1)-expand_rate*(initial_points(1, 2)-initial_points(1, 1)), 1));
ext_right_bound =  floor(min(initial_points(1, 2)+expand_rate*(initial_points(1, 2)-initial_points(1, 1)), size(par.input_image, 2)));
ext_top_bound   =  ceil(max(initial_points(2, 1)-expand_rate*(initial_points(2, 2)-initial_points(2, 1)), 1));
ext_down_bound  =  floor(min(initial_points(2, 2)+expand_rate*(initial_points(2, 2)-initial_points(2, 1)), size(par.input_image, 1)));
ext_patch       =  par.input_image(ext_top_bound:ext_down_bound, ext_left_bound:ext_right_bound);                         % for aviod boundary effect
ext_rain_patch  =  rain(ext_top_bound:ext_down_bound, ext_left_bound:ext_right_bound);
par.ori_center  =  floor(mean(initial_points, 2));
par.center      =  par.ori_center + [1 - ext_left_bound; 1 - ext_top_bound];                                              % obtain the center of boundary extension image
par.focus_size  = [par.patsize par.patsize];