function [ res ] = zoom2( im1, im2, subx, suby, scale, t, blendw )
%zoom2 Zooms in on an embedded image

im1 = im2double(im1);
im2 = im2double(im2);

res = im1;

[rows1, cols1, channels1] = size(im1);
[rows2, cols2, channels2] = size(im2);

sub_scale = t + (1-t) * scale;
im2z = imresize(im2, sub_scale, 'bilinear');
[rows2z, cols2z, channels2z] = size(im2z);

up_scale = sub_scale / scale;

%im1z = imresize(im1, up_scale, 'bilinear');
%[rows1z, cols1z, channels1z] = size(im1z);

x_offset = (1-t) * subx + t * (cols1 / 2);
y_offset = (1-t) * suby + t * (rows1 / 2);


%{
x1 = 1;
y1 = 1;

x2 = cols1;
y2 = rows1;

sx1 = round(subx - (cols2 * scale / 2));
sy1 = round(suby - (rows2 * scale / 2));

sx2 = round(subx + (cols2 * scale / 2));
sy2 = round(suby + (rows2 * scale / 2));


zx1 = round(t * sx1 + (1-t) * x1);
zy1 = round(t * sy1 + (1-t) * y1);

%zx2 = round(t * sx2 + (1-t) * x2);
%zy2 = round(t * sy2 + (1-t) * y2);

bgscale = (1 - t) + t * scale

w = cols1 * bgscale;
h = rows1 * bgscale;

zx2 = round(zx1 + w - 1);
zy2 = round(zy1 + h - 1);

fprintf('(%i, %i) - (%i, %i)\n', zy1, zy2, zx1, zx2);

sub_im1 = im1(zy1 : zy2, zx1 : zx2, :);

im1z = imresize(sub_im1, [rows1 cols1], 'bilinear');
%}

%sx = (1-t) * subx + t * (cols1 / 2);
%sy = (1-t) * suby + t * (rows1 / 2);

sx = subx;
sy = suby;

sx = (sx-1) * up_scale + 1;
sy = (sy-1) * up_scale + 1;

%fprintf('embed(im1z, im2z, %i, %i, 0)\n', round(sx - (cols2z / 2)), round(sy - (rows2z / 2)));

x1 = round(sx - x_offset + 1);
y1 = round(sy - y_offset + 1);

x2 = x1 + cols1 - 1;
y2 = y1 + rows1 - 1;

x1z = round(((x1 - 1) / up_scale) + 1);
y1z = round(((y1 - 1) / up_scale) + 1);
x2z = round(((x2 - 1) / up_scale) + 1);
y2z = round(((y2 - 1) / up_scale) + 1);
im1z = im1(y1z:y2z, x1z:x2z, :);

im1z = imresize(im1z, [rows1 cols1], 'bilinear');

sx = round(sx - x1 + 1);
sy = round(sy - y1 + 1);

zoomed = embed(im1z, im2z, sx, sy, blendw);

%res = zoomed(y1:y2, x1:x2, :);
res = zoomed;

end