%% Shears an image by a user determined amount. Assumes the image is grayscale.
%% The retuned image is the same size as the original image, so a shear may 
%% cut off a section of the image, and by using forward mapping, holes are 
%% possible.

%% Colton Watson
%% Math 2270
%% Last updated: May 3, 2018

function [out_image] = math_shear(in_image, horizontal_shear, vertical_shear)
  
  % Get the size of the image, then find the midpoint
  [in_rows, in_cols] = size(in_image);
  
  mid_row  = ceil(in_rows / 2);
  mid_col = ceil(in_cols / 2);
  
  % Create what will be the sheared image.
  out_image = zeros(in_rows, in_cols);
  
  % Create the transformation matrix.
  T_matrix = [1, horizontal_shear; vertical_shear, 1];
  
  % Go through each pixel, multiply it by the tranformation matrix to find where
  % in the sheared image the pixel will go. 
  for rows = 1:in_rows
    for cols = 1:in_cols
      out_pixel = (T_matrix * [(cols - mid_col);(rows - mid_row)]) + [mid_col; mid_row];
            
      out_row = (round(out_pixel(2)));
      out_col = (round(out_pixel(1)));
      
      if (out_col >= 1 && out_row >= 1 && out_col <= in_cols && out_row <= in_rows)
        out_image(out_row,out_col) = in_image(rows,cols);
      endif  
    endfor
  endfor
endfunction