optimize image pixels loop