Well the suggestion you made is something I am already doing. In my case that is a solution with hundreds of lines of code. It i will involve atleast 3 nested for loops.
and when you do not allow for padding  and you wish overlapping of the data instead it becomes even more complex.
I don’t now if it is not general. I can tell by now that it must be needed in almost any project that involves medical imaging.