function [] = TiffStackWrite(output_stack,output_filename, bit_depth)

tiff_stack = Tiff([output_filename '.tiff'],'w');

img_height = size(output_stack,1);
img_width = size(output_stack,2);
num_slices = size(output_stack,3);


for j = 1:num_slices
    if j ~=1
        tiff_stack.writeDirectory
    end
    
    tiff_stack.setTag('Photometric',Tiff.Photometric.MinIsBlack);
    tiff_stack.setTag('BitsPerSample',bit_depth);
    tiff_stack.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
    tiff_stack.setTag('ImageWidth',img_width);
    tiff_stack.setTag('ImageLength',img_height);
    
    if bit_depth == 8
        tiff_stack.write(squeeze(uint8(output_stack(:,:,j))));
    elseif bit_depth == 16
        tiff_stack.write(squeeze(uint16(output_stack(:,:,j))));
    else
        fprintf('Did not output.  Currently only writes 8 bit and 16 bit images.  Edit TiffStackWrite to include your bit depth of choice if necessary.')
    end
    
end

tiff_stack.close

end