im1_str = 'village_oceanview1.jpg';
%im1_str = 'torre.bmp';
%im1_str = 'test5.bmp';
%im1_str = 'vacation1.jpg';
%im1_str = 'city_night.jpg';
%im1_str = 'supermarket.jpg';
%im1_str = 'fractal.jpg';

im2_str = 'village_oceanview1.jpg';
%im2_str = 'torre.bmp';
%im2_str = 'test5.bmp';
%im2_str = 'vacation1.jpg';
%im2_str = 'city_night.jpg';
%im2_str = 'supermarket.jpg';
%im2_str = 'fractal.jpg';

%im1 = im2double(imread('test2.bmp'));  % our first image
%im1 = im2double(imread('torre.bmp'));  % our first image
im1 = im2double(imread(im1_str));  % our first image
[rows, cols, channels] = size(im1);

%im2 = im2double(imread('torre.bmp')); % target image
%im2 = im2double(imread('test2.bmp'));  % target image
im2 = im2double(imread(im2_str));  % target image

[im1gx,im1gy] = gradient(im1);
[im2gx,im2gy] = gradient(im2);

im1g = im1gx + im1gy;
im2g = im2gx + im2gy;

%im1g = abs(im1g(:,:,1) + im1g(:,:,2) + im1g(:,:,3));

figure(1);
imshow(im1);

figure(2);
imshow(im2);


lowest_dist = 999999999999999;
lowest_dist_x = -1;
lowest_dist_y = -1;
lowest_dist_scale = -1;

% edit these to change starting scale and number of iterations
starting_scale = 0.09;
num_iters = 1;


% do not change these
iter = 1;
scale = starting_scale;

distmap = zeros(num_iters, rows, cols);

while (iter <= num_iters)

fprintf('scale = %f\n', scale);

% generate our downsampled target patch
patch = imresize(im2, scale, 'bilinear');

patchfile = sprintf('%s_%f.jpg', im2_str, scale);
imwrite(patch,patchfile);

[patchgx,patchgy] = gradient(patch);
patchg = patchgx + patchgy;

%patchg = abs(patchg(:,:,1) + patchg(:,:,2) + patchg(:,:,3));


%{
tx = 73;
ty = 73;
patch = im1(tx:tx+20, ty:ty+20,:);
%}

[prows, pcols, pchannels] = size(patch);

figure(3);
imshow(patch);
drawnow;

%{
figure(1);
imshow(im1); 
hold on;
%}


for x = 2 : 1 : cols - pcols - 1
    for y = 2 : 1 : rows - prows - 1
        
%{
        imshow(im1);
        box(x, y, x + pcols - 1, y + prows - 1, 'r');
        drawnow;
%}      
        
        % compute L2 distance in RGB space        
        % RGB distance
        diff = im1(y:y+prows-1, x:x+pcols-1,:) - patch;
        diff = diff .* diff;
        %diff = diff - min(min(min(diff)));
        %diff = diff / max(max(max(diff)));
        
        % gradient distance
        diffg = im1g(y:y+prows-1, x:x+pcols-1,:) - patchg;
        diffg = diffg .* diffg;
        %diffg = diffg - min(min(min(diffg)));
        %diffg = diffg / max(max(max(diffg)));
        
        % weight RBG distance the same as gradient dist for now
        dist = (sum(sum(sum(diff))) + sum(sum(sum(diffg)))) / (pcols * prows);
        
        % only look at RGB distance
        %dist = (sum(sum(sum(diff)))) / (pcols * prows);
        
        % only look at gradient distance
        %dist = (sum(sum(sum(diffg)))) / (pcols * prows);
   
        % save our distance in the distmap
        distmap(iter,y,x) = dist;
        
        % check to see if our current (x, y) is the closest
        if (dist < lowest_dist)
            lowest_dist = dist;
            lowest_dist_x = x;
            lowest_dist_y = y;
            lowest_dist_scale = scale;
            fprintf('new lowest distance: %f at (%i, %i) with scale %f\n', lowest_dist, lowest_dist_x, lowest_dist_y, lowest_dist_scale);
        elseif (dist == lowest_dist)
            fprintf('MULTIPLE BEST MATCHES\n');
        end
    end
    %fprintf('x=%i ', x);
    if (mod(x,20) == 0)
        fprintf('.');
    end
end

fprintf('\n');

iter = iter + 1;

scale = scale + 0.01;

end % end while (iter <

figure(1);
patch = imresize(im2, lowest_dist_scale, 'bilinear');
[prows, pcols, pchannels] = size(patch);
%{
hold on;
%box(lowest_dist_x - 1, lowest_dist_y - 1, lowest_dist_x + pcols, lowest_dist_y + prows, 'r');
subimage(lowest_dist_x, lowest_dist_y, patch);
hold off;
%}
res = embed(im1, patch, lowest_dist_x, lowest_dist_y, 4);
imshow(res);

f = getframe(gcf);
result_str = sprintf('%s_%s_%f_%i.jpg', im1_str, im2_str, starting_scale, num_iters);
imwrite(f.cdata, result_str);

figure(4);

distslice = -1 * squeeze(distmap(1,:,:));
imshow(distslice,[]);


figure(2);
imshow(im1g);




















