this implem looks expensive to me. Did you compare with Python version?
Working on the raw buffer should make it faster...