handle both RGB and gray inputs