actually, since the code is pretty compact, I would simply duplicate the above code twice (for 1- and 3-channel images) and put it inside the switch.