optimize the algorithm for layout of preview image