Encode image in memory