Block-Diagonalization of Operators with Gaps, with Applications to Dirac Operators