% Function to take a nonsquare image matrix to square
%     Requires input of matrix A, multiple (i.e. 8 pixels) and crop option
%     Crop = 1 to crop picture
%     Crop = 0 to extend with black curtains

function sq=tosquare(A,multiple,crop)
    rows = size(A,1);
    cols = size(A,2);
    if crop
        rows = min(rows,cols);
        sq = A(1:multiple*round(rows/multiple-.5),1:multiple*(round(rows/multiple-.5)));
    else
        targetrows = multiple*(round(rows/multiple));
        targetcols = multiple*(round(cols/multiple));
        targetrows = max(targetrows,targetcols);
        targetcols = max(targetcols,targetrows);
        coladd=zeros(rows,targetcols-cols);
        rowadd=zeros(targetrows-rows,targetcols);
        sq = [A coladd];
        sq = [sq; rowadd];
    end