image saving algo, not tested