function save_image_array(img_file_ary, imgary, b_flip_ud, b_flip_lr)
	% save_image_array(img_file_ary, imgary, b_flip_ud, b_flip_lr, b_overwrite)
	% save Ni x Nj x Nk image array to file
	% optionally flip volume before saving
	% by default, will not overwrite
	
	%% get parameters
	imgary		= uint16(imgary);		% cast to 16 bit
	Ni				= size(imgary,1);
	Nj				= size(imgary,2);
	Nk				= size(imgary,3);
	
	%% format check
	[~,~,ext]	= fileparts(img_file_ary{1});
	is_raw12		= strcmpi(ext, '.raw12');	
	is_tiff		= any(strcmpi(ext, {'.tif','.tiff'}));
	is_multitiff= is_tiff && strcmp(img_file_ary{1}, img_file_ary{2});
	if ~is_tiff && ~is_raw12
		error('unrecognised format');
	end

	%% save images
	for k = 1 : Nk
		outfilename		= img_file_ary{k};
		img				= imgary(:,:,k);
		
		if b_flip_ud
			img		= fliplr(img);		% counter-intuitive, but this is the right operation
		end
		if b_flip_lr
			img		= flipud(img);
		end

		if is_raw12
			img			= fliplr(img);
			imwrite_raw12(outfilename, img);
		elseif is_tiff
			img			= fliplr(img)';
			if is_multitiff && k ~= 1
				% append 
				imwrite(img, outfilename, 'writemode', 'append');
			else
				imwrite(img, outfilename);
			end
		end
	end
end