Refactor pixel format code