function [ZoomOverlay,Zoom,ZoomBoxinImage] = f_ZoomBoxRGB_JJ(Image, topleft_x, topleft_y, width, hight, zoomfactor, linewidth)
%Author Paul Antony 2014
%Select a box within an image to zoom in and create a copy of the image
%which shows the zoomed selection in a box. The zoomed box is shown at the
%top right of the image. For custom application the function also provides
%the zoom alone
%   Image: the input image
%   topleft_x:  x coordinate of the bottomleft corner of the box to be zoomed
%   topleft_y:  y coordinate of the bottomleft corner of the box to be zoomed
%   width:      width of the box to be zoomed
%   hight:      hight of the box to be zoomed
%   zoomfactor: every pixel will be expanded to a zoomfactor x zoomfactor
%               matrix

ImSize = size(Image);
ImHight = ImSize(1);
ImWidth = ImSize(2);

ZoomBoxHight = hight * zoomfactor;
ZoomBoxWidth = width * zoomfactor;
BoxToZoom = Image((topleft_y - hight) : topleft_y, topleft_x : (topleft_x + width), :);
Zoom = imresize(BoxToZoom, zoomfactor);
ZoomSize = size(Zoom);
ZoomHight = ZoomSize(1);
ZoomWidth = ZoomSize(2);
ZoomOverlay = Image;
%ZoomOverlay((ImHight-(ZoomHight-1)):ImHight,(ImWidth-(ZoomWidth-1)):ImWidth,:) = Zoom; %bottom right
%ZoomOverlay(1:ZoomHight,1:ZoomWidth, :) = Zoom; %top left
ZoomOverlay(1:ZoomHight,(ImWidth-(ZoomWidth-1)):ImWidth, :) = Zoom; %top right

%% Create mask for the box to be zoomed
SmallBoxMask = zeros(ImHight, ImWidth);
SmallBoxMask((topleft_y - hight) : topleft_y, topleft_x : (topleft_x + width)) = 1;
SmallBox = bwmorph(SmallBoxMask, 'remove');

%% Create mask for the zoomed box
BigBoxMask = zeros(ImHight, ImWidth);
BigBoxMask(1:ZoomHight,(ImWidth-(ZoomWidth-1)):ImWidth) = 1;%top right
%BigBoxMask(1:ZoomHight,1:ZoomWidth) = 1;%top left
BigBox = bwmorph(BigBoxMask, 'remove'); 

%% Combine box masks
BoxinImage = bwmorph((SmallBox), 'thicken', linewidth);
BoxMask = bwmorph((SmallBox + BigBox), 'thicken', linewidth);
%imoverlay(I, MASK, 'color', [ 0 .5 0], 'alpha', .8)
ZoomOverlay = f_imoverlay_mask(ZoomOverlay, BoxMask,[1 1 1]);
ZoomBoxinImage = f_imoverlay_mask(Image, BoxinImage,[1 1 1]);
end

