Refactor device independent pixel calculation.
Update gradle files.

This makes it possible to compile the tests.
Cleanup and safety checks.
Fix tests.