function [It x y] = imtile(I, h, k, overlap, graphic)

% [It x y] = imtile(I, h, k, overlap, graphic)
%
% DESC:
% divide an image in (possibly overlapping) tiles.
%
% AUTHOR
% Marco Zuliani - zuliani@ece.ucsb.edu
%
% VERSION:
% 1.0.0
%
% INPUT:
% I                 = input image
% h                 = >0 indicates the number of vertical tiles
%                     <0 indicates the vertical size of a tile [pixels]
% k                 = >0 indicates the number of horizontal tiles
%                     <0 indicates the horizontal size of a tile [pixels]
% overlap           = pixel overlap (defualt = 0) [pixels]
% graphic           = true to show the results
%
% OUTPUT:
% It                = cell array of tiles
% x, y              = tile boundaries (without overlapping)

% HISTORY:
% 1.0.0             - 03/08/2007 - initial version
% 1.0.1             - 03/22/2007 - bug fix
% 1.0.2             - 03/23/2007 - graphic flag / fixed help

if (nargin < 4), overlap = 0; end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if (nargin < 5)
    graphic = false;
end;

height  = size(I, 1);
width   = size(I, 2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute the tiling limits
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (h < 0)
    delta_x = -h;
else
    delta_x = height/h;
end;
N_vertical_tiles = floor(height/delta_x);

x = zeros(1, N_vertical_tiles+1);
x(1) = 1;
for l = 1:N_vertical_tiles
    x(l+1) = l*delta_x;
end;
if (x(end) < height)
    x(end+1) = height;
end;

if (k < 0)
    delta_y = -k;
else
    delta_y = width/k;
end;
N_horiziontal_tiles = floor(width/delta_y);

y = zeros(1, N_horiziontal_tiles+1);
y(1) = 1;
for l = 1:N_horiziontal_tiles
    y(l+1) = l*delta_y;
end;
if (y(end) < width)
    y(end+1) = width;
end;

x = floor(x);
y = floor(y);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tile the image
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N_vertical_tiles = length(x)-1;
N_horiziontal_tiles = length(y)-1;

l = 1;
It = cell(N_vertical_tiles,N_horiziontal_tiles);
for i = 1:N_horiziontal_tiles

    y_in = y(i)-overlap;
    if (y_in < 1), y_in = 1; end;

    y_fin = y(i+1)+overlap;
    if (y_fin > width), y_fin = width; end;

    yy = y_in:y_fin-1;

    for j = 1:N_vertical_tiles

        x_in = x(j)-overlap;
        if (x_in < 1), x_in = 1; end;
        x_fin = x(j+1)+overlap;
        if (x_fin > height), x_fin = height; end;

        xx = x_in:x_fin-1;

        It{j,i} = I(xx,yy,:);

        % fprintf('\nTile (%d,%d) is %dx%d', i, j, size(It{j,i}, 1), size(It{j,i}, 1))

        l = l + 1;
    end;

end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Show debug
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if graphic

    figure;
    imshow(I);
    hold on
    for i = 1:N_vertical_tiles+1
        plot([1 width], [x(i) x(i)], 'r', 'LineWidth', 2)
    end;
    for j = 1:N_horiziontal_tiles+1
        plot([y(j) y(j)], [1 height], 'r', 'LineWidth', 2)
    end;
    for i = 1:N_vertical_tiles
        for j = 1:N_horiziontal_tiles
            text(0.5*(y(j)+y(j+1)), 0.5*(x(i)+x(i+1)), sprintf('(%d,%d)',i,j), ...
                'Color', 'r', 'FontName', 'Arial', 'FontSize', 10, 'FontAngle', 'normal', ...
                'HorizontalAlignment','center',...
                'BackgroundColor', [0.9 0.9 0.0]);
        end;
    end;
    axis on

end;

return