Diffraction of optical beams with arbitrary profiles by a periodically modulated layer