function moviecutfinder_v10(moviedir,moviename,resizef,init_frame,finit_frame)

% usage:
% moviecutfinder_v9(moviedir,moviename,resizef,init_frame,finit_frame)
%
% moviedir      = '24'
% moviename     = '40m_act_24_S06E01_30fps '
% resizef       = resize factor [default = 0.33]
% init_frame    = 1
% finit_frame   = []

%txt1=getenv('LD_LIBRARY_PATH');    
% Uncomment previous line in ensemble
%txt2=sprintf('%s:/groups/kreiman/compiled_software/lib',txt1);
%setenv('LD_LIBRARY_PATH',txt2)
% Uncomment previous line in ensemble

%code_exec=sprintf('export LD_LIBRARY_PATH=\"LD_LIBRARY_PATH:/groups/kreiman/compiled_software/lib\"');disp(code_exec);system(code_exec);
% Uncomment previous line in ensemble
program_name='moviecutfinder';
program_version=10;
addpath('~/Dropbox/slife/code/matlab/general');
%addpath('~/life/code/matlab/general');
% Uncomment previous line in ensemble
if (~exist('moviedir','var'))       moviedir='24';                          end
if (~exist('moviename','var'))      moviename='40m_act_24_S06E01_30fps';    end
if (~exist('resizef','var'))        resizef=0.33;                           end
if (~exist('init_frame'))           init_frame=1;                           end

coredir='';
coredir=sprintf('~/life/databh/mison/movies/%s',moviedir);
if (isempty(coredir)) txt=sprintf('error! i do not recognize moviename\n%s\ncoredir=\n%s',moviename,coredir);disp(txt);return; end

filename=sprintf('%s/%s.m4v',coredir,moviename);
if (~exist(filename))
    txt=sprintf('error! %s v%d\n\t%s does not exist',program_name,program_version,filename);disp(txt);return;
end
txt=sprintf('reading %s ...',filename);disp(txt);
m=VideoReader(filename);
disp('ready');
total_frames=m.NumberOfFrames;
%disp('31');keyboard;
if (~exist('init_frame'))  init_frame=1;                 end
if (~exist('finit_frame')) finit_frame=total_frames-200; end

partial_results_filename=sprintf('%s/partial_v%d_%s_f%.2f_%dto%d.mat',coredir,program_version,moviename,resizef,init_frame,finit_frame); %debug version
if (~exist(partial_results_filename))
    txt=sprintf('i could not find partial results filename\n%s',partial_results_filename);disp(txt);%keyboard;
end

img=read(m,1);
imgrs=imresize(img,resizef);
[szx,szy,szz]=size(imgrs);
x1=1;x2=ceil(szx/2);x3=szx;
y1=1;y2=ceil(szy/2);y3=szy;

% compute dd distribution
txt=sprintf('total_frames=%d',total_frames);disp(txt);
dd_rgb=zeros(total_frames,5,3);   % difference distribution with frame-1, frame-2, frame-3 (RGB)
dd_gray=zeros(total_frames,5,3);  % difference distribution with frame-1, frame-2, frame-3 (gray)

% Get the first 3 images
for i=1:3
    img=read(m,init_frame+i-1);
    curr_image_rgb=imresize(img,resizef);
    curr_image_gray=rgb2gray(curr_image_rgb);
    [szx,szy,szz]=size(curr_image_rgb);
    previous_image_rgb{i}=curr_image_rgb;
    previous_image_gray{i}=curr_image_gray;
end    
% i=3 (frame-1)
% i=2 (frame-2)
% i=1 (frame-3)

tic;
for curr_frame=(init_frame+3):finit_frame
    if (rem(curr_frame,100)==0)
        t=toc;        
        txt=sprintf('\tprocessing curr_frame=%d (%.2f secs)',curr_frame,t);disp(txt);
        save_s=sprintf('save %s dd_rgb dd_gray',partial_results_filename);disp(save_s);eval(save_s);
    end
    img=read(m,curr_frame);
    curr_image_rgb=imresize(img,resizef);
    curr_image_gray=rgb2gray(curr_image_rgb);
    [szx1,szy1,szz1]=size(curr_image_rgb);
    if ( (szx~=szx1) | (szy~=szy1) | (szz~=szz1) )
        txt=sprintf('error! curr_frame=%d\n\tszx=%d szy=%d szz=%d szx1=%d szy1=%d szz1=%d',curr_frame,szx,szy,szz,szx1,szy1,szz1);disp(txt);keyboard;
    end
    
    % gray scale mode
    for i=1:3
        previous_image=previous_image_gray{i};
        dd_gray(curr_frame,1,i)=diff_two_images(curr_image_gray,previous_image);
        for q=1:4
            [a1,a2,b1,b2]=get_a1_a2_b1_b2(q,x1,x2,x3,y1,y2,y3);
            dd_gray(curr_frame,q+1,i)=diff_two_images(curr_image_gray(a1:a2,b1:b2),previous_image(a1:a2,b1:b2));            
        end
    end
    
    % RGB mode
    for i=1:3
        previous_image=previous_image_rgb{i};
        dd_rgb(curr_frame,1,i)=diff_two_images(curr_image_rgb,previous_image);
        for q=1:4
            [a1,a2,b1,b2]=get_a1_a2_b1_b2(q,x1,x2,x3,y1,y2,y3);
            dd_rgb(curr_frame,q+1,i)=diff_two_images(curr_image_rgb(a1:a2,b1:b2),previous_image(a1:a2,b1:b2));            
        end
    end

    for i=1:2
        previous_image_rgb{i}=previous_image_rgb{i+1};
        previous_image_gray{i}=previous_image_gray{i+1};
    end
    previous_image_rgb{3}=curr_image_rgb;
    previous_image_gray{3}=curr_image_gray;    
end    % close curr_frame loop

save_s=sprintf('save %s dd_gray dd_rgb',partial_results_filename);disp(save_s);eval(save_s);

end

function [d]=diff_two_images(img1,img2)

% compute the euclidian distance between the two images
n_dimensions=ndims(img1);
if (n_dimensions==2)
    aux=img1-img2;
    aux=aux.^2;aux=aux(:);d=sum(aux);
else
    
    d=0;
    for curr_dimension=1:n_dimensions
        aux=img1(:,:,curr_dimension)-img2(:,:,curr_dimension);
        aux=aux.^2;aux=aux(:);d=d+sum(aux);
    end
end
end

function [a1,a2,b1,b2]=get_a1_a2_b1_b2(q,x1,x2,x3,y1,y2,y3)

switch q,
    case 1, a1=x1;a2=x2;b1=y1;b2=y2;
    case 2, a1=x2;a2=x3;b1=y1;b2=y2;
    case 3, a1=x1;a2=x1;b1=y2;b2=y3;
    case 4, a1=x2;a2=x3;b1=y2;b2=y3;
end
end