Add a safe locking function for direct pixel access