Converting matlab code to c for tone mapping algorithm